blob: b64be0c227ffbe9604fe0f4071c175f8af7fc903
1 | /* |
2 | * Copyright (C) 2017 Amlogic, Inc. All rights reserved. |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
12 | * more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
17 | * |
18 | * Description: |
19 | */ |
20 | #include "aml_vcodec_vfm.h" |
21 | #include "aml_vcodec_vfq.h" |
22 | #include "aml_vcodec_util.h" |
23 | #include "aml_vcodec_adapt.h" |
24 | #include <media/v4l2-mem2mem.h> |
25 | |
26 | #define KERNEL_ATRACE_TAG KERNEL_ATRACE_TAG_VIDEO_COMPOSER |
27 | #include <trace/events/meson_atrace.h> |
28 | |
29 | #define RECEIVER_NAME "v4l2-video" |
30 | #define PROVIDER_NAME "v4l2-video" |
31 | |
32 | static struct vframe_s *vdec_vf_peek(void *op_arg) |
33 | { |
34 | struct vcodec_vfm_s *vfm = (struct vcodec_vfm_s *)op_arg; |
35 | |
36 | return vfq_peek(&vfm->vf_que); |
37 | } |
38 | |
39 | static struct vframe_s *vdec_vf_get(void *op_arg) |
40 | { |
41 | struct vcodec_vfm_s *vfm = (struct vcodec_vfm_s *)op_arg; |
42 | |
43 | return vfq_pop(&vfm->vf_que); |
44 | } |
45 | |
46 | static void vdec_vf_put(struct vframe_s *vf, void *op_arg) |
47 | { |
48 | struct vcodec_vfm_s *vfm = (struct vcodec_vfm_s *)op_arg; |
49 | |
50 | /* If the video frame from amvide that means */ |
51 | /* the data has been processed and finished, */ |
52 | /* then push back to VDA. thus we don't put the */ |
53 | /* buffer to the decoder directly.*/ |
54 | |
55 | //vf_put(vf, vfm->recv_name); |
56 | //vf_notify_provider(vfm->recv_name, VFRAME_EVENT_RECEIVER_PUT, NULL); |
57 | |
58 | if (vfq_level(&vfm->vf_que_recycle) > POOL_SIZE - 1) { |
59 | pr_info("%s %d vfq full.\n", __func__, __LINE__); |
60 | return; |
61 | } |
62 | |
63 | atomic_set(&vf->use_cnt, 1); |
64 | |
65 | vfq_push(&vfm->vf_que_recycle, vf); |
66 | |
67 | /* schedule capture work. */ |
68 | vdec_device_vf_run(vfm->ctx); |
69 | } |
70 | |
71 | static int vdec_event_cb(int type, void *data, void *private_data) |
72 | { |
73 | |
74 | if (type & VFRAME_EVENT_RECEIVER_PUT) { |
75 | } else if (type & VFRAME_EVENT_RECEIVER_GET) { |
76 | } else if (type & VFRAME_EVENT_RECEIVER_FRAME_WAIT) { |
77 | } |
78 | return 0; |
79 | } |
80 | |
81 | static int vdec_vf_states(struct vframe_states *states, void *op_arg) |
82 | { |
83 | struct vcodec_vfm_s *vfm = (struct vcodec_vfm_s *)op_arg; |
84 | |
85 | states->vf_pool_size = POOL_SIZE; |
86 | states->buf_recycle_num = 0; |
87 | states->buf_free_num = POOL_SIZE - vfq_level(&vfm->vf_que); |
88 | states->buf_avail_num = vfq_level(&vfm->vf_que); |
89 | |
90 | return 0; |
91 | } |
92 | |
93 | void video_vf_put(char *receiver, struct vdec_v4l2_buffer *fb, int id) |
94 | { |
95 | struct vframe_provider_s *vfp = vf_get_provider(receiver); |
96 | struct vframe_s *vf = (struct vframe_s *)fb->vf_handle; |
97 | |
98 | aml_v4l2_debug(3, "[%d] TO (%s) vf: %p, idx: %d", |
99 | id, vfp->name, vf, vf->index); |
100 | ATRACE_COUNTER("v4l2_to", vf->index_disp); |
101 | |
102 | aml_v4l2_debug(4, "[%d] TO Y:(%lx, %u) C/U:(%lx, %u) V:(%lx, %u)", |
103 | id, fb->m.mem[0].addr, fb->m.mem[0].size, |
104 | fb->m.mem[1].addr, fb->m.mem[1].size, |
105 | fb->m.mem[2].addr, fb->m.mem[2].size); |
106 | |
107 | if (vfp && vf && atomic_dec_and_test(&vf->use_cnt)) |
108 | vf_put(vf, receiver); |
109 | } |
110 | |
111 | static const struct vframe_operations_s vf_provider = { |
112 | .peek = vdec_vf_peek, |
113 | .get = vdec_vf_get, |
114 | .put = vdec_vf_put, |
115 | .event_cb = vdec_event_cb, |
116 | .vf_states = vdec_vf_states, |
117 | }; |
118 | |
119 | static int video_receiver_event_fun(int type, void *data, void *private_data) |
120 | { |
121 | int ret = 0; |
122 | struct vframe_states states; |
123 | struct vcodec_vfm_s *vfm = (struct vcodec_vfm_s *)private_data; |
124 | |
125 | //aml_v4l2_debug(4, "[%d] type: %d, vfm: %p", vfm->ctx->id, type, vfm); |
126 | |
127 | switch (type) { |
128 | case VFRAME_EVENT_PROVIDER_UNREG: { |
129 | if (vf_get_receiver(vfm->prov_name)) { |
130 | aml_v4l2_debug(4, "[%d] unreg %s provider.", |
131 | vfm->ctx->id, vfm->prov_name); |
132 | vf_unreg_provider(&vfm->vf_prov); |
133 | } |
134 | |
135 | break; |
136 | } |
137 | |
138 | case VFRAME_EVENT_PROVIDER_START: { |
139 | if (vf_get_receiver(vfm->prov_name)) { |
140 | aml_v4l2_debug(4, "[%d] reg %s provider.", |
141 | vfm->ctx->id, vfm->prov_name); |
142 | vf_provider_init(&vfm->vf_prov, vfm->prov_name, |
143 | &vf_provider, vfm); |
144 | vf_reg_provider(&vfm->vf_prov); |
145 | vf_notify_receiver(vfm->prov_name, |
146 | VFRAME_EVENT_PROVIDER_START, NULL); |
147 | } |
148 | |
149 | vfq_init(&vfm->vf_que, POOL_SIZE + 1, &vfm->pool[0]); |
150 | vfq_init(&vfm->vf_que_recycle, POOL_SIZE + 1, &vfm->pool_recycle[0]); |
151 | |
152 | break; |
153 | } |
154 | |
155 | case VFRAME_EVENT_PROVIDER_QUREY_STATE: { |
156 | vdec_vf_states(&states, vfm); |
157 | if (states.buf_avail_num > 0) |
158 | ret = RECEIVER_ACTIVE; |
159 | break; |
160 | } |
161 | |
162 | case VFRAME_EVENT_PROVIDER_VFRAME_READY: { |
163 | if (vfq_level(&vfm->vf_que) > POOL_SIZE - 1) |
164 | ret = -1; |
165 | |
166 | if (!vf_peek(vfm->recv_name)) |
167 | ret = -1; |
168 | |
169 | vfm->vf = vf_get(vfm->recv_name); |
170 | if (!vfm->vf) |
171 | ret = -1; |
172 | |
173 | if (ret < 0) { |
174 | pr_err("[%d] receiver vf err.\n", vfm->ctx->id); |
175 | break; |
176 | } |
177 | |
178 | vfq_push(&vfm->vf_que, vfm->vf); |
179 | |
180 | if (vfm->ada_ctx->vfm_path == FRAME_BASE_PATH_V4L_VIDEO) { |
181 | vf_notify_receiver(vfm->prov_name, |
182 | VFRAME_EVENT_PROVIDER_VFRAME_READY, NULL); |
183 | break; |
184 | } |
185 | |
186 | /* schedule capture work. */ |
187 | vdec_device_vf_run(vfm->ctx); |
188 | |
189 | break; |
190 | } |
191 | |
192 | default: |
193 | aml_v4l2_debug(4, "[%d] the vf event is %d", vfm->ctx->id, type); |
194 | } |
195 | |
196 | return ret; |
197 | } |
198 | |
199 | static const struct vframe_receiver_op_s vf_receiver = { |
200 | .event_cb = video_receiver_event_fun |
201 | }; |
202 | |
203 | struct vframe_s *peek_video_frame(struct vcodec_vfm_s *vfm) |
204 | { |
205 | if (vfm->ada_ctx->vfm_path == FRAME_BASE_PATH_V4L_VIDEO) |
206 | return vfq_peek(&vfm->vf_que_recycle); |
207 | else |
208 | return vfq_peek(&vfm->vf_que); |
209 | } |
210 | |
211 | struct vframe_s *get_video_frame(struct vcodec_vfm_s *vfm) |
212 | { |
213 | if (vfm->ada_ctx->vfm_path == FRAME_BASE_PATH_V4L_VIDEO) |
214 | return vfq_pop(&vfm->vf_que_recycle); |
215 | else |
216 | return vfq_pop(&vfm->vf_que); |
217 | } |
218 | |
219 | int vcodec_vfm_init(struct vcodec_vfm_s *vfm) |
220 | { |
221 | int ret; |
222 | |
223 | snprintf(vfm->recv_name, VF_NAME_SIZE, "%s-%d", |
224 | RECEIVER_NAME, vfm->ctx->id); |
225 | snprintf(vfm->prov_name, VF_NAME_SIZE, "%s-%d", |
226 | PROVIDER_NAME, vfm->ctx->id); |
227 | |
228 | vfm->ada_ctx->recv_name = vfm->recv_name; |
229 | |
230 | vf_receiver_init(&vfm->vf_recv, vfm->recv_name, &vf_receiver, vfm); |
231 | ret = vf_reg_receiver(&vfm->vf_recv); |
232 | |
233 | vfm->vfm_initialized = ret ? false : true; |
234 | |
235 | return ret; |
236 | } |
237 | |
238 | void vcodec_vfm_release(struct vcodec_vfm_s *vfm) |
239 | { |
240 | if (vfm->vfm_initialized) |
241 | vf_unreg_receiver(&vfm->vf_recv); |
242 | } |
243 | |
244 |