1 /*
2 * RockChip MPP Video Decoder
3 * Copyright (c) 2017 Lionel CHAZALLON
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <drm_fourcc.h>
23 #include <pthread.h>
24 #include <rockchip/mpp_buffer.h>
25 #include <rockchip/rk_mpi.h>
27 #include <unistd.h>
28
42
43 #define RECEIVE_FRAME_TIMEOUT 100
44 #define FRAMEGROUP_MAX_FRAMES 16
45 #define INPUT_MAX_PACKETS 4
46
51
54
58
63
68
70 {
76 default: return MPP_VIDEO_CodingUnused;
77 }
78 }
79
81 {
82 switch (mppformat) {
83 case MPP_FMT_YUV420SP: return DRM_FORMAT_NV12;
84 #ifdef DRM_FORMAT_NV12_10
85 case MPP_FMT_YUV420SP_10BIT: return DRM_FORMAT_NV12_10;
86 #endif
87 default: return 0;
88 }
89 }
90
92 {
96 MppPacket packet;
97
98 // create the MPP packet
103 }
104
105 mpp_packet_set_pts(packet,
pts);
106
108 mpp_packet_set_eos(packet);
109
112 if (
ret == MPP_ERR_BUFFER_FULL) {
115 } else
117 }
118 else
120
121 mpp_packet_deinit(&packet);
122
124 }
125
127 {
130 return 0;
131 }
132
134 {
136
141 }
142
144 mpp_buffer_group_put(
decoder->frame_group);
146 }
147
150 }
151
153 {
156 MppCodingType codectype = MPP_VIDEO_CodingUnused;
158 RK_S64 paramS64;
159 RK_S32 paramS32;
160
162
163 // create a decoder and a ref to it
169 }
171
173
175 if (codectype == MPP_VIDEO_CodingUnused) {
179 }
180
181 ret = mpp_check_support_format(MPP_CTX_DEC, codectype);
186 }
187
188 // Create the MPP context
194 }
195
196 // initialize mpp
197 ret = mpp_init(
decoder->ctx, MPP_CTX_DEC, codectype);
202 }
203
204 // make decode calls blocking with a timeout
205 paramS32 = MPP_POLL_BLOCK;
211 }
212
214 ret =
decoder->mpi->control(
decoder->ctx, MPP_SET_OUTPUT_BLOCK_TIMEOUT, ¶mS64);
219 }
220
221 ret = mpp_buffer_group_get_internal(&
decoder->frame_group, MPP_BUFFER_TYPE_ION);
226 }
227
233 }
234
240 }
241
243
245
250 }
254
255 return 0;
256
261 }
262
264 {
268
269 // handle EOF
277 }
278
279 // on first packet, send extradata
288 }
289 }
291 }
292
293 // now send packet
297
299 }
300
302 {
305
306 mpp_frame_deinit(&framecontext->
frame);
308
310 }
311
313 {
317 MppFrame mppframe =
NULL;
321 MppFrameFormat mppformat;
322 uint32_t drmformat;
323
325 if (
ret != MPP_OK &&
ret != MPP_ERR_TIMEOUT) {
328 }
329
330 if (mppframe) {
331 // Check whether we have a special frame or not
332 if (mpp_frame_get_info_change(mppframe)) {
334
335 av_log(avctx,
AV_LOG_INFO,
"Decoder noticed an info change (%dx%d), format=%d\n",
336 (int)mpp_frame_get_width(mppframe), (int)mpp_frame_get_height(mppframe),
337 (int)mpp_frame_get_fmt(mppframe));
338
339 avctx->
width = mpp_frame_get_width(mppframe);
340 avctx->
height = mpp_frame_get_height(mppframe);
341
343
345
350 }
351
352 mppformat = mpp_frame_get_fmt(mppframe);
354
363
364 // here decoder is fully initialized, we need to feed it again with data
367 } else if (mpp_frame_get_eos(mppframe)) {
372 } else if (mpp_frame_get_discard(mppframe)) {
376 } else if (mpp_frame_get_errinfo(mppframe)) {
380 }
381
382 // here we should have a valid frame
384
385 // setup general frame fields
387 frame->width = mpp_frame_get_width(mppframe);
388 frame->height = mpp_frame_get_height(mppframe);
389 frame->pts = mpp_frame_get_pts(mppframe);
390 frame->color_range = mpp_frame_get_color_range(mppframe);
391 frame->color_primaries = mpp_frame_get_color_primaries(mppframe);
392 frame->color_trc = mpp_frame_get_color_trc(mppframe);
393 frame->colorspace = mpp_frame_get_colorspace(mppframe);
394
395 mode = mpp_frame_get_mode(mppframe);
396 if ((
mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_DEINTERLACED)
398 if ((
mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_TOP_FIRST)
400
401 mppformat = mpp_frame_get_fmt(mppframe);
403
404 // now setup the frame buffer info
405 buffer = mpp_frame_get_buffer(mppframe);
409 // We allocate the descriptor in buf[0] jointly with a structure
410 // that will allow to hold additional information
411 // for properly releasing MPP frames and decoder.
412 struct {
415 } *combined_desc =
av_mallocz(
sizeof(*combined_desc));
416 if (!combined_desc) {
419 }
420 desc = &combined_desc->desc;
421 framecontext = &combined_desc->framecontext;
422
423 desc->nb_objects = 1;
424 desc->objects[0].fd = mpp_buffer_get_fd(
buffer);
425 desc->objects[0].size = mpp_buffer_get_size(
buffer);
426
428 layer = &
desc->layers[0];
429 layer->
format = drmformat;
431
434 layer->
planes[0].
pitch = mpp_frame_get_hor_stride(mppframe);
435
439
440 // MPP decoder needs to be closed only when all frames have been released.
441 framecontext->
frame = mppframe;
442
446
447 if (!
frame->buf[0]) {
451 }
453
455 if (!
frame->hw_frames_ctx) {
458 }
459
460 return 0;
461 } else {
462 av_log(avctx,
AV_LOG_ERROR,
"Failed to retrieve the frame buffer, frame is dropped (code = %d)\n",
ret);
463 mpp_frame_deinit(&mppframe);
464 }
465 }
else if (
decoder->eos_reached) {
467 }
else if (
ret == MPP_ERR_TIMEOUT) {
469 }
470
472
474 if (mppframe)
475 mpp_frame_deinit(&mppframe);
476
478 }
479
481 {
486 RK_S32 usedslots, freeslots;
487
489 // we get the available slots in decoder
494 }
495
497 if (freeslots > 0) {
501 }
502
505
509 }
510 }
511
512 // make sure we keep decoder full
513 if (freeslots > 1)
515 }
516
518 }
519
521 {
525
527
531 } else
533 }
534
538 };
539
540 #define RKMPP_DEC_CLASS(NAME) \
541 static const AVClass rkmpp_##NAME##_dec_class = { \
542 .class_name = "rkmpp_" #NAME "_dec", \
543 .version = LIBAVUTIL_VERSION_INT, \
544 };
545
546 #define RKMPP_DEC(NAME, ID, BSFS) \
547 RKMPP_DEC_CLASS(NAME) \
548 const FFCodec ff_##NAME##_rkmpp_decoder = { \
549 .p.name = #NAME "_rkmpp", \
550 CODEC_LONG_NAME(#NAME " (rkmpp)"), \
551 .p.type = AVMEDIA_TYPE_VIDEO, \
552 .p.id = ID, \
553 .priv_data_size = sizeof(RKMPPDecodeContext), \
554 .init = rkmpp_init_decoder, \
555 .close = rkmpp_close_decoder, \
556 FF_CODEC_RECEIVE_FRAME_CB(rkmpp_receive_frame), \
557 .flush = rkmpp_flush, \
558 .p.priv_class = &rkmpp_##NAME##_dec_class, \
559 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
560 .hw_configs = rkmpp_hw_configs, \
561 .bsfs = BSFS, \
562 .p.wrapper_name = "rkmpp", \
563 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \
564 };
565