hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
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,
....@@ -766,6 +729,8 @@
766729 return -EINVAL;
767730
768731 if (*nplanes) {
732
+ if (*nplanes != q_data->fmt->num_planes)
733
+ return -EINVAL;
769734 for (i = 0; i < *nplanes; i++)
770735 if (sizes[i] < q_data->sizeimage[i])
771736 return -EINVAL;
....@@ -805,13 +770,14 @@
805770 container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
806771
807772 struct mtk_video_enc_buf *mtk_buf =
808
- container_of(vb2_v4l2, struct mtk_video_enc_buf, vb);
773
+ container_of(vb2_v4l2, struct mtk_video_enc_buf,
774
+ m2m_buf.vb);
809775
810776 if ((vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
811777 (ctx->param_change != MTK_ENCODE_PARAM_NONE)) {
812778 mtk_v4l2_debug(1, "[%d] Before id=%d encode parameter change %x",
813779 ctx->id,
814
- mtk_buf->vb.vb2_buf.index,
780
+ vb2_v4l2->vb2_buf.index,
815781 ctx->param_change);
816782 mtk_buf->param_change = ctx->param_change;
817783 mtk_buf->enc_params = ctx->enc_params;
....@@ -872,12 +838,18 @@
872838
873839 err_set_param:
874840 for (i = 0; i < q->num_buffers; ++i) {
875
- if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) {
841
+ struct vb2_buffer *buf = vb2_get_buffer(q, i);
842
+
843
+ /*
844
+ * FIXME: This check is not needed as only active buffers
845
+ * can be marked as done.
846
+ */
847
+ if (buf->state == VB2_BUF_STATE_ACTIVE) {
876848 mtk_v4l2_debug(0, "[%d] id=%d, type=%d, %d -> VB2_BUF_STATE_QUEUED",
877849 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);
850
+ (int)buf->state);
851
+ v4l2_m2m_buf_done(to_vb2_v4l2_buffer(buf),
852
+ VB2_BUF_STATE_QUEUED);
881853 }
882854 }
883855
....@@ -887,21 +859,19 @@
887859 static void vb2ops_venc_stop_streaming(struct vb2_queue *q)
888860 {
889861 struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
890
- struct vb2_buffer *src_buf, *dst_buf;
862
+ struct vb2_v4l2_buffer *src_buf, *dst_buf;
891863 int ret;
892864
893865 mtk_v4l2_debug(2, "[%d]-> type=%d", ctx->id, q->type);
894866
895867 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
896868 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);
869
+ dst_buf->vb2_buf.planes[0].bytesused = 0;
870
+ v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
900871 }
901872 } else {
902873 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);
874
+ v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
905875 }
906876
907877 if ((q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
....@@ -923,8 +893,17 @@
923893 ctx->state = MTK_STATE_FREE;
924894 }
925895
896
+static int vb2ops_venc_buf_out_validate(struct vb2_buffer *vb)
897
+{
898
+ struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
899
+
900
+ vbuf->field = V4L2_FIELD_NONE;
901
+ return 0;
902
+}
903
+
926904 static const struct vb2_ops mtk_venc_vb2_ops = {
927905 .queue_setup = vb2ops_venc_queue_setup,
906
+ .buf_out_validate = vb2ops_venc_buf_out_validate,
928907 .buf_prepare = vb2ops_venc_buf_prepare,
929908 .buf_queue = vb2ops_venc_buf_queue,
930909 .wait_prepare = vb2_ops_wait_prepare,
....@@ -937,8 +916,7 @@
937916 {
938917 struct mtk_vcodec_ctx *ctx = priv;
939918 int ret;
940
- struct vb2_buffer *src_buf, *dst_buf;
941
- struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
919
+ struct vb2_v4l2_buffer *src_buf, *dst_buf;
942920 struct mtk_vcodec_mem bs_buf;
943921 struct venc_done_result enc_result;
944922
....@@ -948,14 +926,14 @@
948926 return -EINVAL;
949927 }
950928
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;
929
+ bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
930
+ bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
931
+ bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
954932
955933 mtk_v4l2_debug(1,
956934 "[%d] buf id=%d va=0x%p dma_addr=0x%llx size=%zu",
957935 ctx->id,
958
- dst_buf->index, bs_buf.va,
936
+ dst_buf->vb2_buf.index, bs_buf.va,
959937 (u64)bs_buf.dma_addr,
960938 bs_buf.size);
961939
....@@ -964,26 +942,23 @@
964942 NULL, &bs_buf, &enc_result);
965943
966944 if (ret) {
967
- dst_buf->planes[0].bytesused = 0;
945
+ dst_buf->vb2_buf.planes[0].bytesused = 0;
968946 ctx->state = MTK_STATE_ABORT;
969
- v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf),
970
- VB2_BUF_STATE_ERROR);
947
+ v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
971948 mtk_v4l2_err("venc_if_encode failed=%d", ret);
972949 return -EINVAL;
973950 }
974951 src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
975952 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;
953
+ dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
954
+ dst_buf->timecode = src_buf->timecode;
980955 } else {
981956 mtk_v4l2_err("No timestamp for the header buffer.");
982957 }
983958
984959 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);
960
+ dst_buf->vb2_buf.planes[0].bytesused = enc_result.bs_size;
961
+ v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
987962
988963 return 0;
989964 }
....@@ -991,11 +966,10 @@
991966 static int mtk_venc_param_change(struct mtk_vcodec_ctx *ctx)
992967 {
993968 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);
969
+ struct vb2_v4l2_buffer *vb2_v4l2 = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
997970 struct mtk_video_enc_buf *mtk_buf =
998
- container_of(vb2_v4l2, struct mtk_video_enc_buf, vb);
971
+ container_of(vb2_v4l2, struct mtk_video_enc_buf,
972
+ m2m_buf.vb);
999973
1000974 int ret = 0;
1001975
....@@ -1007,7 +981,7 @@
1007981 enc_prm.bitrate = mtk_buf->enc_params.bitrate;
1008982 mtk_v4l2_debug(1, "[%d] id=%d, change param br=%d",
1009983 ctx->id,
1010
- mtk_buf->vb.vb2_buf.index,
984
+ vb2_v4l2->vb2_buf.index,
1011985 enc_prm.bitrate);
1012986 ret |= venc_if_set_param(ctx,
1013987 VENC_SET_PARAM_ADJUST_BITRATE,
....@@ -1018,7 +992,7 @@
1018992 mtk_buf->enc_params.framerate_denom;
1019993 mtk_v4l2_debug(1, "[%d] id=%d, change param fr=%d",
1020994 ctx->id,
1021
- mtk_buf->vb.vb2_buf.index,
995
+ vb2_v4l2->vb2_buf.index,
1022996 enc_prm.frm_rate);
1023997 ret |= venc_if_set_param(ctx,
1024998 VENC_SET_PARAM_ADJUST_FRAMERATE,
....@@ -1035,7 +1009,7 @@
10351009 if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FORCE_INTRA) {
10361010 mtk_v4l2_debug(1, "[%d] id=%d, change param force I=%d",
10371011 ctx->id,
1038
- mtk_buf->vb.vb2_buf.index,
1012
+ vb2_v4l2->vb2_buf.index,
10391013 mtk_buf->enc_params.force_intra);
10401014 if (mtk_buf->enc_params.force_intra)
10411015 ret |= venc_if_set_param(ctx,
....@@ -1067,12 +1041,11 @@
10671041 {
10681042 struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
10691043 encode_work);
1070
- struct vb2_buffer *src_buf, *dst_buf;
1044
+ struct vb2_v4l2_buffer *src_buf, *dst_buf;
10711045 struct venc_frm_buf frm_buf;
10721046 struct mtk_vcodec_mem bs_buf;
10731047 struct venc_done_result enc_result;
10741048 int ret, i;
1075
- struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2;
10761049
10771050 /* check dst_buf, dst_buf may be removed in device_run
10781051 * to stored encdoe header so we need check dst_buf and
....@@ -1086,54 +1059,43 @@
10861059
10871060 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
10881061 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);
1062
+ for (i = 0; i < src_buf->vb2_buf.num_planes ; i++) {
10911063 frm_buf.fb_addr[i].dma_addr =
1092
- vb2_dma_contig_plane_dma_addr(src_buf, i);
1064
+ vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, i);
10931065 frm_buf.fb_addr[i].size =
1094
- (size_t)src_buf->planes[i].length;
1066
+ (size_t)src_buf->vb2_buf.planes[i].length;
10951067 }
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;
1068
+ bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1069
+ bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1070
+ bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
10991071
11001072 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,
1073
+ "Framebuf PA=%llx Size=0x%zx;PA=0x%llx Size=0x%zx;PA=0x%llx Size=%zu",
11031074 (u64)frm_buf.fb_addr[0].dma_addr,
11041075 frm_buf.fb_addr[0].size,
1105
- frm_buf.fb_addr[1].va,
11061076 (u64)frm_buf.fb_addr[1].dma_addr,
11071077 frm_buf.fb_addr[1].size,
1108
- frm_buf.fb_addr[2].va,
11091078 (u64)frm_buf.fb_addr[2].dma_addr,
11101079 frm_buf.fb_addr[2].size);
11111080
11121081 ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_FRAME,
11131082 &frm_buf, &bs_buf, &enc_result);
11141083
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;
1084
+ dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1085
+ dst_buf->timecode = src_buf->timecode;
11201086
11211087 if (enc_result.is_key_frm)
1122
- dst_vb2_v4l2->flags |= V4L2_BUF_FLAG_KEYFRAME;
1088
+ dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
11231089
11241090 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);
1091
+ v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1092
+ dst_buf->vb2_buf.planes[0].bytesused = 0;
1093
+ v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
11301094 mtk_v4l2_err("venc_if_encode failed=%d", ret);
11311095 } 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);
1096
+ v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1097
+ dst_buf->vb2_buf.planes[0].bytesused = enc_result.bs_size;
1098
+ v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
11371099 mtk_v4l2_debug(2, "venc_if_encode bs size=%d",
11381100 enc_result.bs_size);
11391101 }
....@@ -1141,7 +1103,7 @@
11411103 v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
11421104
11431105 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,
1106
+ src_buf->vb2_buf.index, dst_buf->vb2_buf.index, ret,
11451107 enc_result.bs_size);
11461108 }
11471109
....@@ -1209,7 +1171,7 @@
12091171 q_data->coded_height = DFT_CFG_HEIGHT;
12101172 q_data->field = V4L2_FIELD_NONE;
12111173
1212
- q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
1174
+ q_data->fmt = &ctx->dev->venc_pdata->output_formats[0];
12131175
12141176 v4l_bound_align_image(&q_data->coded_width,
12151177 MTK_VENC_MIN_W,
....@@ -1238,12 +1200,14 @@
12381200 memset(q_data, 0, sizeof(struct mtk_q_data));
12391201 q_data->coded_width = DFT_CFG_WIDTH;
12401202 q_data->coded_height = DFT_CFG_HEIGHT;
1241
- q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
1203
+ q_data->fmt = &ctx->dev->venc_pdata->capture_formats[0];
12421204 q_data->field = V4L2_FIELD_NONE;
12431205 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
12441206 DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
12451207 ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
12461208
1209
+ ctx->enc_params.framerate_num = MTK_DEFAULT_FRAMERATE_NUM;
1210
+ ctx->enc_params.framerate_denom = MTK_DEFAULT_FRAMERATE_DENOM;
12471211 }
12481212
12491213 int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx)
....@@ -1253,8 +1217,11 @@
12531217
12541218 v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT);
12551219
1220
+ v4l2_ctrl_new_std(handler, ops, V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
1221
+ 1, 1, 1, 1);
12561222 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE,
1257
- 1, 4000000, 1, 4000000);
1223
+ ctx->dev->venc_pdata->min_bitrate,
1224
+ ctx->dev->venc_pdata->max_bitrate, 1, 4000000);
12581225 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES,
12591226 0, 2, 1, 0);
12601227 v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,