forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/drivers/media/platform/mtk-vcodec/mtk_vcodec_enc.c
....@@ -1,16 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * Copyright (c) 2016 MediaTek Inc.
34 * Author: PC Chen <pc.chen@mediatek.com>
45 * Tiffany Lin <tiffany.lin@mediatek.com>
5
-*
6
-* This program is free software; you can redistribute it and/or modify
7
-* it under the terms of the GNU General Public License version 2 as
8
-* published by the Free Software Foundation.
9
-*
10
-* This program is distributed in the hope that it will be useful,
11
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
-* GNU General Public License for more details.
146 */
157
168 #include <media/v4l2-event.h>
....@@ -31,58 +23,15 @@
3123 #define DFT_CFG_WIDTH MTK_VENC_MIN_W
3224 #define DFT_CFG_HEIGHT MTK_VENC_MIN_H
3325 #define MTK_MAX_CTRLS_HINT 20
34
-#define OUT_FMT_IDX 0
35
-#define CAP_FMT_IDX 4
3626
27
+#define MTK_DEFAULT_FRAMERATE_NUM 1001
28
+#define MTK_DEFAULT_FRAMERATE_DENOM 30000
3729
3830 static void mtk_venc_worker(struct work_struct *work);
3931
40
-static struct mtk_video_fmt mtk_video_formats[] = {
41
- {
42
- .fourcc = V4L2_PIX_FMT_NV12M,
43
- .type = MTK_FMT_FRAME,
44
- .num_planes = 2,
45
- },
46
- {
47
- .fourcc = V4L2_PIX_FMT_NV21M,
48
- .type = MTK_FMT_FRAME,
49
- .num_planes = 2,
50
- },
51
- {
52
- .fourcc = V4L2_PIX_FMT_YUV420M,
53
- .type = MTK_FMT_FRAME,
54
- .num_planes = 3,
55
- },
56
- {
57
- .fourcc = V4L2_PIX_FMT_YVU420M,
58
- .type = MTK_FMT_FRAME,
59
- .num_planes = 3,
60
- },
61
- {
62
- .fourcc = V4L2_PIX_FMT_H264,
63
- .type = MTK_FMT_ENC,
64
- .num_planes = 1,
65
- },
66
- {
67
- .fourcc = V4L2_PIX_FMT_VP8,
68
- .type = MTK_FMT_ENC,
69
- .num_planes = 1,
70
- },
71
-};
72
-
73
-#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
74
-
75
-static const struct mtk_codec_framesizes mtk_venc_framesizes[] = {
76
- {
77
- .fourcc = V4L2_PIX_FMT_H264,
78
- .stepwise = { MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16,
79
- MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16 },
80
- },
81
- {
82
- .fourcc = V4L2_PIX_FMT_VP8,
83
- .stepwise = { MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16,
84
- MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16 },
85
- },
32
+static const struct v4l2_frmsize_stepwise mtk_venc_framesizes = {
33
+ MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16,
34
+ MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16,
8635 };
8736
8837 #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_venc_framesizes)
....@@ -164,67 +113,85 @@
164113 .s_ctrl = vidioc_venc_s_ctrl,
165114 };
166115
167
-static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
116
+static int vidioc_enum_fmt(struct v4l2_fmtdesc *f,
117
+ const struct mtk_video_fmt *formats,
118
+ size_t num_formats)
168119 {
169
- struct mtk_video_fmt *fmt;
170
- int i, j = 0;
120
+ if (f->index >= num_formats)
121
+ return -EINVAL;
171122
172
- for (i = 0; i < NUM_FORMATS; ++i) {
173
- if (output_queue && mtk_video_formats[i].type != MTK_FMT_FRAME)
174
- continue;
175
- if (!output_queue && mtk_video_formats[i].type != MTK_FMT_ENC)
176
- continue;
123
+ f->pixelformat = formats[f->index].fourcc;
124
+ memset(f->reserved, 0, sizeof(f->reserved));
177125
178
- if (j == f->index) {
179
- fmt = &mtk_video_formats[i];
180
- f->pixelformat = fmt->fourcc;
181
- memset(f->reserved, 0, sizeof(f->reserved));
182
- return 0;
183
- }
184
- ++j;
126
+ return 0;
127
+}
128
+
129
+static const struct mtk_video_fmt *
130
+mtk_venc_find_format(u32 fourcc, const struct mtk_vcodec_enc_pdata *pdata)
131
+{
132
+ const struct mtk_video_fmt *fmt;
133
+ unsigned int k;
134
+
135
+ for (k = 0; k < pdata->num_capture_formats; k++) {
136
+ fmt = &pdata->capture_formats[k];
137
+ if (fmt->fourcc == fourcc)
138
+ return fmt;
185139 }
186140
187
- return -EINVAL;
141
+ for (k = 0; k < pdata->num_output_formats; k++) {
142
+ fmt = &pdata->output_formats[k];
143
+ if (fmt->fourcc == fourcc)
144
+ return fmt;
145
+ }
146
+
147
+ return NULL;
188148 }
189149
190150 static int vidioc_enum_framesizes(struct file *file, void *fh,
191151 struct v4l2_frmsizeenum *fsize)
192152 {
193
- int i = 0;
153
+ const struct mtk_video_fmt *fmt;
194154
195155 if (fsize->index != 0)
196156 return -EINVAL;
197157
198
- for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
199
- if (fsize->pixel_format != mtk_venc_framesizes[i].fourcc)
200
- continue;
158
+ fmt = mtk_venc_find_format(fsize->pixel_format,
159
+ fh_to_ctx(fh)->dev->venc_pdata);
160
+ if (!fmt)
161
+ return -EINVAL;
201162
202
- fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
203
- fsize->stepwise = mtk_venc_framesizes[i].stepwise;
204
- return 0;
205
- }
163
+ fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
164
+ fsize->stepwise = mtk_venc_framesizes;
206165
207
- return -EINVAL;
166
+ return 0;
208167 }
209168
210
-static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
211
- struct v4l2_fmtdesc *f)
169
+static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
170
+ struct v4l2_fmtdesc *f)
212171 {
213
- return vidioc_enum_fmt(f, false);
172
+ const struct mtk_vcodec_enc_pdata *pdata =
173
+ fh_to_ctx(priv)->dev->venc_pdata;
174
+
175
+ return vidioc_enum_fmt(f, pdata->capture_formats,
176
+ pdata->num_capture_formats);
214177 }
215178
216
-static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
217
- struct v4l2_fmtdesc *f)
179
+static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
180
+ struct v4l2_fmtdesc *f)
218181 {
219
- return vidioc_enum_fmt(f, true);
182
+ const struct mtk_vcodec_enc_pdata *pdata =
183
+ fh_to_ctx(priv)->dev->venc_pdata;
184
+
185
+ return vidioc_enum_fmt(f, pdata->output_formats,
186
+ pdata->num_output_formats);
220187 }
221188
222189 static int vidioc_venc_querycap(struct file *file, void *priv,
223190 struct v4l2_capability *cap)
224191 {
225
- strlcpy(cap->driver, MTK_VCODEC_ENC_NAME, sizeof(cap->driver));
226
- strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
227
- strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
192
+ strscpy(cap->driver, MTK_VCODEC_ENC_NAME, sizeof(cap->driver));
193
+ strscpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
194
+ strscpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
228195
229196 return 0;
230197 }
....@@ -233,14 +200,18 @@
233200 struct v4l2_streamparm *a)
234201 {
235202 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
203
+ struct v4l2_fract *timeperframe = &a->parm.output.timeperframe;
236204
237205 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
238206 return -EINVAL;
239207
240
- ctx->enc_params.framerate_num =
241
- a->parm.output.timeperframe.denominator;
242
- ctx->enc_params.framerate_denom =
243
- a->parm.output.timeperframe.numerator;
208
+ if (timeperframe->numerator == 0 || timeperframe->denominator == 0) {
209
+ timeperframe->numerator = MTK_DEFAULT_FRAMERATE_NUM;
210
+ timeperframe->denominator = MTK_DEFAULT_FRAMERATE_DENOM;
211
+ }
212
+
213
+ ctx->enc_params.framerate_num = timeperframe->denominator;
214
+ ctx->enc_params.framerate_denom = timeperframe->numerator;
244215 ctx->param_change |= MTK_ENCODE_PARAM_FRAMERATE;
245216
246217 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
....@@ -274,24 +245,11 @@
274245 return &ctx->q_data[MTK_Q_DATA_DST];
275246 }
276247
277
-static struct mtk_video_fmt *mtk_venc_find_format(struct v4l2_format *f)
278
-{
279
- struct mtk_video_fmt *fmt;
280
- unsigned int k;
281
-
282
- for (k = 0; k < NUM_FORMATS; k++) {
283
- fmt = &mtk_video_formats[k];
284
- if (fmt->fourcc == f->fmt.pix.pixelformat)
285
- return fmt;
286
- }
287
-
288
- return NULL;
289
-}
290
-
291248 /* V4L2 specification suggests the driver corrects the format struct if any of
292249 * the dimensions is unsupported
293250 */
294
-static int vidioc_try_fmt(struct v4l2_format *f, struct mtk_video_fmt *fmt)
251
+static int vidioc_try_fmt(struct v4l2_format *f,
252
+ const struct mtk_video_fmt *fmt)
295253 {
296254 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
297255 int i;
....@@ -393,7 +351,7 @@
393351 param->input_yuv_fmt = VENC_YUV_FORMAT_NV21;
394352 break;
395353 default:
396
- mtk_v4l2_err("Unsupport fourcc =%d", q_data_src->fmt->fourcc);
354
+ mtk_v4l2_err("Unsupported fourcc =%d", q_data_src->fmt->fourcc);
397355 break;
398356 }
399357 param->h264_profile = enc_params->h264_profile;
....@@ -424,10 +382,11 @@
424382 struct v4l2_format *f)
425383 {
426384 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
385
+ const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
427386 struct vb2_queue *vq;
428387 struct mtk_q_data *q_data;
429388 int i, ret;
430
- struct mtk_video_fmt *fmt;
389
+ const struct mtk_video_fmt *fmt;
431390
432391 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
433392 if (!vq) {
....@@ -446,10 +405,10 @@
446405 return -EINVAL;
447406 }
448407
449
- fmt = mtk_venc_find_format(f);
408
+ fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
450409 if (!fmt) {
451
- f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
452
- fmt = mtk_venc_find_format(f);
410
+ fmt = &ctx->dev->venc_pdata->capture_formats[0];
411
+ f->fmt.pix.pixelformat = fmt->fourcc;
453412 }
454413
455414 q_data->fmt = fmt;
....@@ -486,10 +445,11 @@
486445 struct v4l2_format *f)
487446 {
488447 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
448
+ const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
489449 struct vb2_queue *vq;
490450 struct mtk_q_data *q_data;
491451 int ret, i;
492
- struct mtk_video_fmt *fmt;
452
+ const struct mtk_video_fmt *fmt;
493453 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
494454
495455 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
....@@ -509,10 +469,10 @@
509469 return -EINVAL;
510470 }
511471
512
- fmt = mtk_venc_find_format(f);
472
+ fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
513473 if (!fmt) {
514
- f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
515
- fmt = mtk_venc_find_format(f);
474
+ fmt = &ctx->dev->venc_pdata->output_formats[0];
475
+ f->fmt.pix.pixelformat = fmt->fourcc;
516476 }
517477
518478 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
....@@ -588,13 +548,14 @@
588548 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
589549 struct v4l2_format *f)
590550 {
591
- struct mtk_video_fmt *fmt;
551
+ const struct mtk_video_fmt *fmt;
592552 struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
553
+ const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
593554
594
- fmt = mtk_venc_find_format(f);
555
+ fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
595556 if (!fmt) {
596
- f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
597
- fmt = mtk_venc_find_format(f);
557
+ fmt = &ctx->dev->venc_pdata->capture_formats[0];
558
+ f->fmt.pix.pixelformat = fmt->fourcc;
598559 }
599560 f->fmt.pix_mp.colorspace = ctx->colorspace;
600561 f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc;
....@@ -607,12 +568,14 @@
607568 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
608569 struct v4l2_format *f)
609570 {
610
- struct mtk_video_fmt *fmt;
571
+ const struct mtk_video_fmt *fmt;
572
+ struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
573
+ const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
611574
612
- fmt = mtk_venc_find_format(f);
575
+ fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
613576 if (!fmt) {
614
- f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
615
- fmt = mtk_venc_find_format(f);
577
+ fmt = &ctx->dev->venc_pdata->output_formats[0];
578
+ f->fmt.pix.pixelformat = fmt->fourcc;
616579 }
617580 if (!f->fmt.pix_mp.colorspace) {
618581 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
....@@ -725,8 +688,8 @@
725688 .vidioc_dqbuf = vidioc_venc_dqbuf,
726689
727690 .vidioc_querycap = vidioc_venc_querycap,
728
- .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
729
- .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
691
+ .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
692
+ .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
730693 .vidioc_enum_framesizes = vidioc_enum_framesizes,
731694
732695 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
....@@ -805,13 +768,14 @@
805768 container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
806769
807770 struct mtk_video_enc_buf *mtk_buf =
808
- container_of(vb2_v4l2, struct mtk_video_enc_buf, vb);
771
+ container_of(vb2_v4l2, struct mtk_video_enc_buf,
772
+ m2m_buf.vb);
809773
810774 if ((vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
811775 (ctx->param_change != MTK_ENCODE_PARAM_NONE)) {
812776 mtk_v4l2_debug(1, "[%d] Before id=%d encode parameter change %x",
813777 ctx->id,
814
- mtk_buf->vb.vb2_buf.index,
778
+ vb2_v4l2->vb2_buf.index,
815779 ctx->param_change);
816780 mtk_buf->param_change = ctx->param_change;
817781 mtk_buf->enc_params = ctx->enc_params;
....@@ -872,12 +836,18 @@
872836
873837 err_set_param:
874838 for (i = 0; i < q->num_buffers; ++i) {
875
- if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
839
+ struct vb2_buffer *buf = vb2_get_buffer(q, i);
840
+
841
+ /*
842
+ * FIXME: This check is not needed as only active buffers
843
+ * can be marked as done.
844
+ */
845
+ if (buf->state == VB2_BUF_STATE_ACTIVE) {
876846 mtk_v4l2_debug(0, "[%d] id=%d, type=%d, %d -> VB2_BUF_STATE_QUEUED",
877847 ctx->id, i, q->type,
878
- (int)q->bufs[i]->state);
879
- v4l2_m2m_buf_done(to_vb2_v4l2_buffer(q->bufs[i]),
880
- VB2_BUF_STATE_QUEUED);
848
+ (int)buf->state);
849
+ v4l2_m2m_buf_done(to_vb2_v4l2_buffer(buf),
850
+ VB2_BUF_STATE_QUEUED);
881851 }
882852 }
883853
....@@ -887,21 +857,19 @@
887857 static void vb2ops_venc_stop_streaming(struct vb2_queue *q)
888858 {
889859 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
890
- struct vb2_buffer *src_buf, *dst_buf;
860
+ struct vb2_v4l2_buffer *src_buf, *dst_buf;
891861 int ret;
892862
893863 mtk_v4l2_debug(2, "[%d]-> type=%d", ctx->id, q->type);
894864
895865 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
896866 while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
897
- dst_buf->planes[0].bytesused = 0;
898
- v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
899
- VB2_BUF_STATE_ERROR);
867
+ dst_buf->vb2_buf.planes[0].bytesused = 0;
868
+ v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
900869 }
901870 } else {
902871 while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx)))
903
- v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
904
- VB2_BUF_STATE_ERROR);
872
+ v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
905873 }
906874
907875 if ((q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
....@@ -923,8 +891,17 @@
923891 ctx->state = MTK_STATE_FREE;
924892 }
925893
894
+static int vb2ops_venc_buf_out_validate(struct vb2_buffer *vb)
895
+{
896
+ struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
897
+
898
+ vbuf->field = V4L2_FIELD_NONE;
899
+ return 0;
900
+}
901
+
926902 static const struct vb2_ops mtk_venc_vb2_ops = {
927903 .queue_setup = vb2ops_venc_queue_setup,
904
+ .buf_out_validate = vb2ops_venc_buf_out_validate,
928905 .buf_prepare = vb2ops_venc_buf_prepare,
929906 .buf_queue = vb2ops_venc_buf_queue,
930907 .wait_prepare = vb2_ops_wait_prepare,
....@@ -937,8 +914,7 @@
937914 {
938915 struct mtk_vcodec_ctx *ctx = priv;
939916 int ret;
940
- struct vb2_buffer *src_buf, *dst_buf;
941
- struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
917
+ struct vb2_v4l2_buffer *src_buf, *dst_buf;
942918 struct mtk_vcodec_mem bs_buf;
943919 struct venc_done_result enc_result;
944920
....@@ -948,14 +924,14 @@
948924 return -EINVAL;
949925 }
950926
951
- bs_buf.va = vb2_plane_vaddr(dst_buf, 0);
952
- bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
953
- bs_buf.size = (size_t)dst_buf->planes[0].length;
927
+ bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
928
+ bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
929
+ bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
954930
955931 mtk_v4l2_debug(1,
956932 "[%d] buf id=%d va=0x%p dma_addr=0x%llx size=%zu",
957933 ctx->id,
958
- dst_buf->index, bs_buf.va,
934
+ dst_buf->vb2_buf.index, bs_buf.va,
959935 (u64)bs_buf.dma_addr,
960936 bs_buf.size);
961937
....@@ -964,26 +940,23 @@
964940 NULL, &bs_buf, &enc_result);
965941
966942 if (ret) {
967
- dst_buf->planes[0].bytesused = 0;
943
+ dst_buf->vb2_buf.planes[0].bytesused = 0;
968944 ctx->state = MTK_STATE_ABORT;
969
- v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
970
- VB2_BUF_STATE_ERROR);
945
+ v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
971946 mtk_v4l2_err("venc_if_encode failed=%d", ret);
972947 return -EINVAL;
973948 }
974949 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
975950 if (src_buf) {
976
- src_vb2_v4l2 = to_vb2_v4l2_buffer(src_buf);
977
- dst_vb2_v4l2 = to_vb2_v4l2_buffer(dst_buf);
978
- dst_buf->timestamp = src_buf->timestamp;
979
- dst_vb2_v4l2->timecode = src_vb2_v4l2->timecode;
951
+ dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
952
+ dst_buf->timecode = src_buf->timecode;
980953 } else {
981954 mtk_v4l2_err("No timestamp for the header buffer.");
982955 }
983956
984957 ctx->state = MTK_STATE_HEADER;
985
- dst_buf->planes[0].bytesused = enc_result.bs_size;
986
- v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf), VB2_BUF_STATE_DONE);
958
+ dst_buf->vb2_buf.planes[0].bytesused = enc_result.bs_size;
959
+ v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
987960
988961 return 0;
989962 }
....@@ -991,11 +964,10 @@
991964 static int mtk_venc_param_change(struct mtk_vcodec_ctx *ctx)
992965 {
993966 struct venc_enc_param enc_prm;
994
- struct vb2_buffer *vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
995
- struct vb2_v4l2_buffer *vb2_v4l2 =
996
- container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
967
+ struct vb2_v4l2_buffer *vb2_v4l2 = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
997968 struct mtk_video_enc_buf *mtk_buf =
998
- container_of(vb2_v4l2, struct mtk_video_enc_buf, vb);
969
+ container_of(vb2_v4l2, struct mtk_video_enc_buf,
970
+ m2m_buf.vb);
999971
1000972 int ret = 0;
1001973
....@@ -1007,7 +979,7 @@
1007979 enc_prm.bitrate = mtk_buf->enc_params.bitrate;
1008980 mtk_v4l2_debug(1, "[%d] id=%d, change param br=%d",
1009981 ctx->id,
1010
- mtk_buf->vb.vb2_buf.index,
982
+ vb2_v4l2->vb2_buf.index,
1011983 enc_prm.bitrate);
1012984 ret |= venc_if_set_param(ctx,
1013985 VENC_SET_PARAM_ADJUST_BITRATE,
....@@ -1018,7 +990,7 @@
1018990 mtk_buf->enc_params.framerate_denom;
1019991 mtk_v4l2_debug(1, "[%d] id=%d, change param fr=%d",
1020992 ctx->id,
1021
- mtk_buf->vb.vb2_buf.index,
993
+ vb2_v4l2->vb2_buf.index,
1022994 enc_prm.frm_rate);
1023995 ret |= venc_if_set_param(ctx,
1024996 VENC_SET_PARAM_ADJUST_FRAMERATE,
....@@ -1035,7 +1007,7 @@
10351007 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FORCE_INTRA) {
10361008 mtk_v4l2_debug(1, "[%d] id=%d, change param force I=%d",
10371009 ctx->id,
1038
- mtk_buf->vb.vb2_buf.index,
1010
+ vb2_v4l2->vb2_buf.index,
10391011 mtk_buf->enc_params.force_intra);
10401012 if (mtk_buf->enc_params.force_intra)
10411013 ret |= venc_if_set_param(ctx,
....@@ -1067,12 +1039,11 @@
10671039 {
10681040 struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
10691041 encode_work);
1070
- struct vb2_buffer *src_buf, *dst_buf;
1042
+ struct vb2_v4l2_buffer *src_buf, *dst_buf;
10711043 struct venc_frm_buf frm_buf;
10721044 struct mtk_vcodec_mem bs_buf;
10731045 struct venc_done_result enc_result;
10741046 int ret, i;
1075
- struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
10761047
10771048 /* check dst_buf, dst_buf may be removed in device_run
10781049 * to stored encdoe header so we need check dst_buf and
....@@ -1086,54 +1057,43 @@
10861057
10871058 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
10881059 memset(&frm_buf, 0, sizeof(frm_buf));
1089
- for (i = 0; i < src_buf->num_planes ; i++) {
1090
- frm_buf.fb_addr[i].va = vb2_plane_vaddr(src_buf, i);
1060
+ for (i = 0; i < src_buf->vb2_buf.num_planes ; i++) {
10911061 frm_buf.fb_addr[i].dma_addr =
1092
- vb2_dma_contig_plane_dma_addr(src_buf, i);
1062
+ vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, i);
10931063 frm_buf.fb_addr[i].size =
1094
- (size_t)src_buf->planes[i].length;
1064
+ (size_t)src_buf->vb2_buf.planes[i].length;
10951065 }
1096
- bs_buf.va = vb2_plane_vaddr(dst_buf, 0);
1097
- bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0);
1098
- bs_buf.size = (size_t)dst_buf->planes[0].length;
1066
+ bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1067
+ bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1068
+ bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
10991069
11001070 mtk_v4l2_debug(2,
1101
- "Framebuf VA=%p PA=%llx Size=0x%zx;VA=%p PA=0x%llx Size=0x%zx;VA=%p PA=0x%llx Size=%zu",
1102
- frm_buf.fb_addr[0].va,
1071
+ "Framebuf PA=%llx Size=0x%zx;PA=0x%llx Size=0x%zx;PA=0x%llx Size=%zu",
11031072 (u64)frm_buf.fb_addr[0].dma_addr,
11041073 frm_buf.fb_addr[0].size,
1105
- frm_buf.fb_addr[1].va,
11061074 (u64)frm_buf.fb_addr[1].dma_addr,
11071075 frm_buf.fb_addr[1].size,
1108
- frm_buf.fb_addr[2].va,
11091076 (u64)frm_buf.fb_addr[2].dma_addr,
11101077 frm_buf.fb_addr[2].size);
11111078
11121079 ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_FRAME,
11131080 &frm_buf, &bs_buf, &enc_result);
11141081
1115
- src_vb2_v4l2 = to_vb2_v4l2_buffer(src_buf);
1116
- dst_vb2_v4l2 = to_vb2_v4l2_buffer(dst_buf);
1117
-
1118
- dst_buf->timestamp = src_buf->timestamp;
1119
- dst_vb2_v4l2->timecode = src_vb2_v4l2->timecode;
1082
+ dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1083
+ dst_buf->timecode = src_buf->timecode;
11201084
11211085 if (enc_result.is_key_frm)
1122
- dst_vb2_v4l2->flags |= V4L2_BUF_FLAG_KEYFRAME;
1086
+ dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
11231087
11241088 if (ret) {
1125
- v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
1126
- VB2_BUF_STATE_ERROR);
1127
- dst_buf->planes[0].bytesused = 0;
1128
- v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
1129
- VB2_BUF_STATE_ERROR);
1089
+ v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1090
+ dst_buf->vb2_buf.planes[0].bytesused = 0;
1091
+ v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
11301092 mtk_v4l2_err("venc_if_encode failed=%d", ret);
11311093 } else {
1132
- v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf),
1133
- VB2_BUF_STATE_DONE);
1134
- dst_buf->planes[0].bytesused = enc_result.bs_size;
1135
- v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
1136
- VB2_BUF_STATE_DONE);
1094
+ v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1095
+ dst_buf->vb2_buf.planes[0].bytesused = enc_result.bs_size;
1096
+ v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
11371097 mtk_v4l2_debug(2, "venc_if_encode bs size=%d",
11381098 enc_result.bs_size);
11391099 }
....@@ -1141,7 +1101,7 @@
11411101 v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
11421102
11431103 mtk_v4l2_debug(1, "<=== src_buf[%d] dst_buf[%d] venc_if_encode ret=%d Size=%u===>",
1144
- src_buf->index, dst_buf->index, ret,
1104
+ src_buf->vb2_buf.index, dst_buf->vb2_buf.index, ret,
11451105 enc_result.bs_size);
11461106 }
11471107
....@@ -1209,7 +1169,7 @@
12091169 q_data->coded_height = DFT_CFG_HEIGHT;
12101170 q_data->field = V4L2_FIELD_NONE;
12111171
1212
- q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
1172
+ q_data->fmt = &ctx->dev->venc_pdata->output_formats[0];
12131173
12141174 v4l_bound_align_image(&q_data->coded_width,
12151175 MTK_VENC_MIN_W,
....@@ -1238,12 +1198,14 @@
12381198 memset(q_data, 0, sizeof(struct mtk_q_data));
12391199 q_data->coded_width = DFT_CFG_WIDTH;
12401200 q_data->coded_height = DFT_CFG_HEIGHT;
1241
- q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
1201
+ q_data->fmt = &ctx->dev->venc_pdata->capture_formats[0];
12421202 q_data->field = V4L2_FIELD_NONE;
12431203 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
12441204 DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
12451205 ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
12461206
1207
+ ctx->enc_params.framerate_num = MTK_DEFAULT_FRAMERATE_NUM;
1208
+ ctx->enc_params.framerate_denom = MTK_DEFAULT_FRAMERATE_DENOM;
12471209 }
12481210
12491211 int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx)
....@@ -1253,8 +1215,11 @@
12531215
12541216 v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT);
12551217
1218
+ v4l2_ctrl_new_std(handler, ops, V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
1219
+ 1, 1, 1, 1);
12561220 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE,
1257
- 1, 4000000, 1, 4000000);
1221
+ ctx->dev->venc_pdata->min_bitrate,
1222
+ ctx->dev->venc_pdata->max_bitrate, 1, 4000000);
12581223 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES,
12591224 0, 2, 1, 0);
12601225 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,