blob: e7b85fb0fbe7c7616da99aee98e5d0474edba85d
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 RECEIVER_NAME "v4l2-video" |
27 | #define PROVIDER_NAME "v4l2-video" |
28 | |
29 | static struct vframe_s *vdec_vf_peek(void *op_arg) |
30 | { |
31 | struct vcodec_vfm_s *vfm = (struct vcodec_vfm_s *)op_arg; |
32 | |
33 | return vfq_peek(&vfm->vf_que); |
34 | } |
35 | |
36 | static struct vframe_s *vdec_vf_get(void *op_arg) |
37 | { |
38 | struct vcodec_vfm_s *vfm = (struct vcodec_vfm_s *)op_arg; |
39 | |
40 | return vfq_pop(&vfm->vf_que); |
41 | } |
42 | |
43 | static void vdec_vf_put(struct vframe_s *vf, void *op_arg) |
44 | { |
45 | struct vcodec_vfm_s *vfm = (struct vcodec_vfm_s *)op_arg; |
46 | |
47 | /* If the video frame from amvide that means */ |
48 | /* the data has been processed and finished, */ |
49 | /* then push back to VDA. thus we don't put the */ |
50 | /* buffer to the decoder directly.*/ |
51 | |
52 | //vf_put(vf, vfm->recv_name); |
53 | //vf_notify_provider(vfm->recv_name, VFRAME_EVENT_RECEIVER_PUT, NULL); |
54 | |
55 | if (vfq_level(&vfm->vf_que_recycle) > POOL_SIZE - 1) { |
56 | pr_info("%s %d vfq full.\n", __func__, __LINE__); |
57 | return; |
58 | } |
59 | |
60 | atomic_set(&vf->use_cnt, 1); |
61 | |
62 | vfq_push(&vfm->vf_que_recycle, vf); |
63 | |
64 | /* schedule capture work. */ |
65 | vdec_device_vf_run(vfm->ctx); |
66 | } |
67 | |
68 | static int vdec_event_cb(int type, void *data, void *private_data) |
69 | { |
70 | |
71 | if (type & VFRAME_EVENT_RECEIVER_PUT) { |
72 | } else if (type & VFRAME_EVENT_RECEIVER_GET) { |
73 | } else if (type & VFRAME_EVENT_RECEIVER_FRAME_WAIT) { |
74 | } |
75 | return 0; |
76 | } |
77 | |
78 | static int vdec_vf_states(struct vframe_states *states, void *op_arg) |
79 | { |
80 | struct vcodec_vfm_s *vfm = (struct vcodec_vfm_s *)op_arg; |
81 | |
82 | states->vf_pool_size = POOL_SIZE; |
83 | states->buf_recycle_num = 0; |
84 | states->buf_free_num = POOL_SIZE - vfq_level(&vfm->vf_que); |
85 | states->buf_avail_num = vfq_level(&vfm->vf_que); |
86 | |
87 | return 0; |
88 | } |
89 | |
90 | void video_vf_put(char *receiver, struct vdec_v4l2_buffer *fb, int id) |
91 | { |
92 | struct vframe_provider_s *vfp = vf_get_provider(receiver); |
93 | struct vframe_s *vf = (struct vframe_s *)fb->vf_handle; |
94 | |
95 | aml_v4l2_debug(3, "[%d] TO (%s) vf: %p, idx: %d", |
96 | id, vfp->name, vf, vf->index); |
97 | |
98 | aml_v4l2_debug(4, "[%d] TO Y:(%lx, %u) C/U:(%lx, %u) V:(%lx, %u)", |
99 | id, fb->m.mem[0].addr, fb->m.mem[0].size, |
100 | fb->m.mem[1].addr, fb->m.mem[1].size, |
101 | fb->m.mem[2].addr, fb->m.mem[2].size); |
102 | |
103 | if (vfp && vf && atomic_dec_and_test(&vf->use_cnt)) |
104 | vf_put(vf, receiver); |
105 | } |
106 | |
107 | static const struct vframe_operations_s vf_provider = { |
108 | .peek = vdec_vf_peek, |
109 | .get = vdec_vf_get, |
110 | .put = vdec_vf_put, |
111 | .event_cb = vdec_event_cb, |
112 | .vf_states = vdec_vf_states, |
113 | }; |
114 | |
115 | static int video_receiver_event_fun(int type, void *data, void *private_data) |
116 | { |
117 | int ret = 0; |
118 | struct vframe_states states; |
119 | struct vcodec_vfm_s *vfm = (struct vcodec_vfm_s *)private_data; |
120 | |
121 | //aml_v4l2_debug(4, "[%d] type: %d, vfm: %p", vfm->ctx->id, type, vfm); |
122 | |
123 | switch (type) { |
124 | case VFRAME_EVENT_PROVIDER_UNREG: { |
125 | if (vf_get_receiver(vfm->prov_name)) { |
126 | aml_v4l2_debug(4, "[%d] unreg %s provider.", |
127 | vfm->ctx->id, vfm->prov_name); |
128 | vf_unreg_provider(&vfm->vf_prov); |
129 | } |
130 | |
131 | break; |
132 | } |
133 | |
134 | case VFRAME_EVENT_PROVIDER_START: { |
135 | if (vf_get_receiver(vfm->prov_name)) { |
136 | aml_v4l2_debug(4, "[%d] reg %s provider.", |
137 | vfm->ctx->id, vfm->prov_name); |
138 | vf_provider_init(&vfm->vf_prov, vfm->prov_name, |
139 | &vf_provider, vfm); |
140 | vf_reg_provider(&vfm->vf_prov); |
141 | vf_notify_receiver(vfm->prov_name, |
142 | VFRAME_EVENT_PROVIDER_START, NULL); |
143 | } |
144 | |
145 | vfq_init(&vfm->vf_que, POOL_SIZE + 1, &vfm->pool[0]); |
146 | vfq_init(&vfm->vf_que_recycle, POOL_SIZE + 1, &vfm->pool_recycle[0]); |
147 | |
148 | break; |
149 | } |
150 | |
151 | case VFRAME_EVENT_PROVIDER_QUREY_STATE: { |
152 | vdec_vf_states(&states, vfm); |
153 | if (states.buf_avail_num > 0) |
154 | ret = RECEIVER_ACTIVE; |
155 | break; |
156 | } |
157 | |
158 | case VFRAME_EVENT_PROVIDER_VFRAME_READY: { |
159 | if (vfq_level(&vfm->vf_que) > POOL_SIZE - 1) |
160 | ret = -1; |
161 | |
162 | if (!vf_peek(vfm->recv_name)) |
163 | ret = -1; |
164 | |
165 | vfm->vf = vf_get(vfm->recv_name); |
166 | if (!vfm->vf) |
167 | ret = -1; |
168 | |
169 | if (ret < 0) { |
170 | pr_err("[%d] receiver vf err.\n", vfm->ctx->id); |
171 | break; |
172 | } |
173 | |
174 | vfq_push(&vfm->vf_que, vfm->vf); |
175 | |
176 | if (vfm->ada_ctx->vfm_path == FRAME_BASE_PATH_V4L_VIDEO) { |
177 | vf_notify_receiver(vfm->prov_name, |
178 | VFRAME_EVENT_PROVIDER_VFRAME_READY, NULL); |
179 | break; |
180 | } |
181 | |
182 | /* schedule capture work. */ |
183 | vdec_device_vf_run(vfm->ctx); |
184 | |
185 | break; |
186 | } |
187 | |
188 | default: |
189 | aml_v4l2_debug(4, "[%d] the vf event is %d", vfm->ctx->id, type); |
190 | } |
191 | |
192 | return ret; |
193 | } |
194 | |
195 | static const struct vframe_receiver_op_s vf_receiver = { |
196 | .event_cb = video_receiver_event_fun |
197 | }; |
198 | |
199 | struct vframe_s *peek_video_frame(struct vcodec_vfm_s *vfm) |
200 | { |
201 | if (vfm->ada_ctx->vfm_path == FRAME_BASE_PATH_V4L_VIDEO) |
202 | return vfq_peek(&vfm->vf_que_recycle); |
203 | else |
204 | return vfq_peek(&vfm->vf_que); |
205 | } |
206 | |
207 | struct vframe_s *get_video_frame(struct vcodec_vfm_s *vfm) |
208 | { |
209 | if (vfm->ada_ctx->vfm_path == FRAME_BASE_PATH_V4L_VIDEO) |
210 | return vfq_pop(&vfm->vf_que_recycle); |
211 | else |
212 | return vfq_pop(&vfm->vf_que); |
213 | } |
214 | |
215 | int vcodec_vfm_init(struct vcodec_vfm_s *vfm) |
216 | { |
217 | snprintf(vfm->recv_name, VF_NAME_SIZE, "%s-%d", |
218 | RECEIVER_NAME, vfm->ctx->id); |
219 | snprintf(vfm->prov_name, VF_NAME_SIZE, "%s-%d", |
220 | PROVIDER_NAME, vfm->ctx->id); |
221 | |
222 | vfm->ada_ctx->recv_name = vfm->recv_name; |
223 | |
224 | vf_receiver_init(&vfm->vf_recv, vfm->recv_name, &vf_receiver, vfm); |
225 | vf_reg_receiver(&vfm->vf_recv); |
226 | |
227 | return 0; |
228 | } |
229 | |
230 | void vcodec_vfm_release(struct vcodec_vfm_s *vfm) |
231 | { |
232 | vf_unreg_receiver(&vfm->vf_recv); |
233 | } |
234 | |
235 |