Browse code

lavc: Add support for RockChip Media Process Platform

This adds hardware decoding for H.264 / HEVC / VP8 / VP9 using the MPP
Rockchip API. It returns frames holding an AVDRMFrameDescriptor struct
in buf[0] that allows drm / dmabuf usage. Tested on RK3288 (TinkerBoard)
and RK3328.

Signed-off-by: Mark Thompson <sw@jkqxz.net>

Lionel CHAZALLON authored on 2017/09/24 14:39:45
Showing 6 changed files
... ...
@@ -50,6 +50,7 @@ version <next>:
50 50
 - KMS screen grabber
51 51
 - CUDA thumbnail filter
52 52
 - V4L2 mem2mem HW assisted codecs
53
+- Rockchip MPP hardware decoding
53 54
 
54 55
 
55 56
 version 3.3:
... ...
@@ -316,6 +316,7 @@ External library support:
316 316
   --disable-nvenc          disable Nvidia video encoding code [autodetect]
317 317
   --enable-omx             enable OpenMAX IL code [no]
318 318
   --enable-omx-rpi         enable OpenMAX IL code for Raspberry Pi [no]
319
+  --enable-rkmpp           enable Rockchip Media Process Platform code [no]
319 320
   --disable-vaapi          disable Video Acceleration API (mainly Unix/Intel) code [autodetect]
320 321
   --disable-vda            disable Apple Video Decode Acceleration code [autodetect]
321 322
   --disable-vdpau          disable Nvidia Video Decode and Presentation API for Unix code [autodetect]
... ...
@@ -1567,6 +1568,7 @@ EXTERNAL_LIBRARY_VERSION3_LIST="
1567 1567
     libopencore_amrnb
1568 1568
     libopencore_amrwb
1569 1569
     libvo_amrwbenc
1570
+    rkmpp
1570 1571
 "
1571 1572
 
1572 1573
 EXTERNAL_LIBRARY_GPLV3_LIST="
... ...
@@ -2801,6 +2803,8 @@ h264_qsv_decoder_deps="libmfx"
2801 2801
 h264_qsv_decoder_select="h264_mp4toannexb_bsf h264_parser qsvdec h264_qsv_hwaccel"
2802 2802
 h264_qsv_encoder_deps="libmfx"
2803 2803
 h264_qsv_encoder_select="qsvenc"
2804
+h264_rkmpp_decoder_deps="rkmpp"
2805
+h264_rkmpp_decoder_select="h264_mp4toannexb_bsf"
2804 2806
 h264_vaapi_encoder_deps="VAEncPictureParameterBufferH264"
2805 2807
 h264_vaapi_encoder_select="vaapi_encode golomb"
2806 2808
 h264_vda_decoder_deps="vda"
... ...
@@ -2818,6 +2822,8 @@ hevc_qsv_decoder_deps="libmfx"
2818 2818
 hevc_qsv_decoder_select="hevc_mp4toannexb_bsf hevc_parser qsvdec hevc_qsv_hwaccel"
2819 2819
 hevc_qsv_encoder_deps="libmfx"
2820 2820
 hevc_qsv_encoder_select="hevcparse qsvenc"
2821
+hevc_rkmpp_decoder_deps="rkmpp"
2822
+hevc_rkmpp_decoder_select="hevc_mp4toannexb_bsf"
2821 2823
 hevc_vaapi_encoder_deps="VAEncPictureParameterBufferHEVC"
2822 2824
 hevc_vaapi_encoder_select="vaapi_encode golomb"
2823 2825
 hevc_v4l2m2m_decoder_deps="v4l2_m2m hevc_v4l2_m2m"
... ...
@@ -2864,12 +2870,14 @@ vp8_cuvid_decoder_deps="cuda cuvid"
2864 2864
 vp8_mediacodec_decoder_deps="mediacodec"
2865 2865
 vp8_qsv_decoder_deps="libmfx"
2866 2866
 vp8_qsv_decoder_select="qsvdec vp8_qsv_hwaccel vp8_parser"
2867
+vp8_rkmpp_decoder_deps="rkmpp"
2867 2868
 vp8_vaapi_encoder_deps="VAEncPictureParameterBufferVP8"
2868 2869
 vp8_vaapi_encoder_select="vaapi_encode"
2869 2870
 vp8_v4l2m2m_decoder_deps="v4l2_m2m vp8_v4l2_m2m"
2870 2871
 vp8_v4l2m2m_encoder_deps="v4l2_m2m vp8_v4l2_m2m"
2871 2872
 vp9_cuvid_decoder_deps="cuda cuvid"
2872 2873
 vp9_mediacodec_decoder_deps="mediacodec"
2874
+vp9_rkmpp_decoder_deps="rkmpp"
2873 2875
 vp9_vaapi_encoder_deps="VAEncPictureParameterBufferVP9"
2874 2876
 vp9_vaapi_encoder_select="vaapi_encode"
2875 2877
 vp9_v4l2m2m_decoder_deps="v4l2_m2m vp9_v4l2_m2m"
... ...
@@ -6066,6 +6074,13 @@ enabled openssl           && { use_pkg_config "" openssl openssl/ssl.h OPENSSL_i
6066 6066
                                check_lib openssl openssl/ssl.h SSL_library_init -lssl32 -leay32 ||
6067 6067
                                check_lib openssl openssl/ssl.h SSL_library_init -lssl -lcrypto -lws2_32 -lgdi32 ||
6068 6068
                                die "ERROR: openssl not found"; }
6069
+enabled rkmpp             && { { require_pkg_config rockchip_mpp rockchip_mpp rockchip/rk_mpi.h mpp_create ||
6070
+                                 die "ERROR : Rockchip MPP was not found."; } &&
6071
+                               { check_func_headers rockchip/rk_mpi_cmd.h "MPP_DEC_GET_FREE_PACKET_SLOT_COUNT" ||
6072
+                                 die "ERROR: Rockchip MPP is outdated, please get a more recent one."; } &&
6073
+                               { enabled libdrm ||
6074
+                                 die "ERROR: rkmpp requires --enable-libdrm"; }
6075
+                             }
6069 6076
 
6070 6077
 if enabled gcrypt; then
6071 6078
     GCRYPT_CONFIG="${cross_prefix}libgcrypt-config"
... ...
@@ -341,6 +341,7 @@ OBJS-$(CONFIG_H264_VDA_DECODER)        += vda_h264_dec.o
341 341
 OBJS-$(CONFIG_H264_OMX_ENCODER)        += omx.o
342 342
 OBJS-$(CONFIG_H264_QSV_DECODER)        += qsvdec_h2645.o
343 343
 OBJS-$(CONFIG_H264_QSV_ENCODER)        += qsvenc_h264.o
344
+OBJS-$(CONFIG_H264_RKMPP_DECODER)      += rkmppdec.o
344 345
 OBJS-$(CONFIG_H264_VAAPI_ENCODER)      += vaapi_encode_h264.o vaapi_encode_h26x.o
345 346
 OBJS-$(CONFIG_H264_VIDEOTOOLBOX_ENCODER) += videotoolboxenc.o
346 347
 OBJS-$(CONFIG_H264_V4L2M2M_DECODER)    += v4l2_m2m_dec.o
... ...
@@ -357,6 +358,7 @@ OBJS-$(CONFIG_NVENC_HEVC_ENCODER)      += nvenc_hevc.o
357 357
 OBJS-$(CONFIG_HEVC_QSV_DECODER)        += qsvdec_h2645.o
358 358
 OBJS-$(CONFIG_HEVC_QSV_ENCODER)        += qsvenc_hevc.o hevc_ps_enc.o       \
359 359
                                           hevc_data.o
360
+OBJS-$(CONFIG_HEVC_RKMPP_DECODER)      += rkmppdec.o
360 361
 OBJS-$(CONFIG_HEVC_VAAPI_ENCODER)      += vaapi_encode_h265.o vaapi_encode_h26x.o
361 362
 OBJS-$(CONFIG_HEVC_V4L2M2M_DECODER)    += v4l2_m2m_dec.o
362 363
 OBJS-$(CONFIG_HEVC_V4L2M2M_ENCODER)    += v4l2_m2m_enc.o
... ...
@@ -636,6 +638,7 @@ OBJS-$(CONFIG_VP8_DECODER)             += vp8.o vp56rac.o
636 636
 OBJS-$(CONFIG_VP8_CUVID_DECODER)       += cuvid.o
637 637
 OBJS-$(CONFIG_VP8_MEDIACODEC_DECODER)  += mediacodecdec.o
638 638
 OBJS-$(CONFIG_VP8_QSV_DECODER)         += qsvdec_other.o
639
+OBJS-$(CONFIG_VP8_RKMPP_DECODER)       += rkmppdec.o
639 640
 OBJS-$(CONFIG_VP8_VAAPI_ENCODER)       += vaapi_encode_vp8.o
640 641
 OBJS-$(CONFIG_VP8_V4L2M2M_DECODER)     += v4l2_m2m_dec.o
641 642
 OBJS-$(CONFIG_VP8_V4L2M2M_ENCODER)     += v4l2_m2m_enc.o
... ...
@@ -644,6 +647,7 @@ OBJS-$(CONFIG_VP9_DECODER)             += vp9.o vp9data.o vp9dsp.o vp9lpf.o vp9r
644 644
                                           vp9dsp_8bpp.o vp9dsp_10bpp.o vp9dsp_12bpp.o
645 645
 OBJS-$(CONFIG_VP9_CUVID_DECODER)       += cuvid.o
646 646
 OBJS-$(CONFIG_VP9_MEDIACODEC_DECODER)  += mediacodecdec.o
647
+OBJS-$(CONFIG_VP9_RKMPP_DECODER)       += rkmppdec.o
647 648
 OBJS-$(CONFIG_VP9_VAAPI_ENCODER)       += vaapi_encode_vp9.o
648 649
 OBJS-$(CONFIG_VPLAYER_DECODER)         += textdec.o ass.o
649 650
 OBJS-$(CONFIG_VP9_V4L2M2M_DECODER)     += v4l2_m2m_dec.o
... ...
@@ -215,6 +215,7 @@ static void register_all(void)
215 215
     REGISTER_DECODER(H264_MEDIACODEC,   h264_mediacodec);
216 216
     REGISTER_DECODER(H264_MMAL,         h264_mmal);
217 217
     REGISTER_DECODER(H264_QSV,          h264_qsv);
218
+    REGISTER_DECODER(H264_RKMPP,        h264_rkmpp);
218 219
     REGISTER_DECODER(H264_VDA,          h264_vda);
219 220
 #if FF_API_VDPAU
220 221
     REGISTER_DECODER(H264_VDPAU,        h264_vdpau);
... ...
@@ -222,6 +223,7 @@ static void register_all(void)
222 222
     REGISTER_ENCDEC (HAP,               hap);
223 223
     REGISTER_DECODER(HEVC,              hevc);
224 224
     REGISTER_DECODER(HEVC_QSV,          hevc_qsv);
225
+    REGISTER_DECODER(HEVC_RKMPP,        hevc_rkmpp);
225 226
     REGISTER_ENCDEC (HEVC_V4L2M2M,      hevc_v4l2m2m);
226 227
     REGISTER_DECODER(HNM4_VIDEO,        hnm4_video);
227 228
     REGISTER_DECODER(HQ_HQA,            hq_hqa);
... ...
@@ -380,8 +382,10 @@ static void register_all(void)
380 380
     REGISTER_DECODER(VP6F,              vp6f);
381 381
     REGISTER_DECODER(VP7,               vp7);
382 382
     REGISTER_DECODER(VP8,               vp8);
383
+    REGISTER_DECODER(VP8_RKMPP,         vp8_rkmpp);
383 384
     REGISTER_ENCDEC (VP8_V4L2M2M,       vp8_v4l2m2m);
384 385
     REGISTER_DECODER(VP9,               vp9);
386
+    REGISTER_DECODER(VP9_RKMPP,         vp9_rkmpp);
385 387
     REGISTER_DECODER(VP9_V4L2M2M,       vp9_v4l2m2m);
386 388
     REGISTER_DECODER(VQA,               vqa);
387 389
     REGISTER_DECODER(BITPACKED,         bitpacked);
388 390
new file mode 100644
... ...
@@ -0,0 +1,596 @@
0
+/*
1
+ * RockChip MPP Video Decoder
2
+ * Copyright (c) 2017 Lionel CHAZALLON
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <drm_fourcc.h>
22
+#include <pthread.h>
23
+#include <rockchip/mpp_buffer.h>
24
+#include <rockchip/rk_mpi.h>
25
+#include <time.h>
26
+#include <unistd.h>
27
+
28
+#include "avcodec.h"
29
+#include "decode.h"
30
+#include "internal.h"
31
+#include "libavutil/buffer.h"
32
+#include "libavutil/common.h"
33
+#include "libavutil/frame.h"
34
+#include "libavutil/hwcontext.h"
35
+#include "libavutil/hwcontext_drm.h"
36
+#include "libavutil/imgutils.h"
37
+#include "libavutil/log.h"
38
+
39
+#define RECEIVE_FRAME_TIMEOUT   100
40
+#define FRAMEGROUP_MAX_FRAMES   16
41
+
42
+typedef struct {
43
+    MppCtx ctx;
44
+    MppApi *mpi;
45
+    MppBufferGroup frame_group;
46
+
47
+    char first_frame;
48
+    char first_packet;
49
+    char eos_reached;
50
+
51
+    AVBufferRef *frames_ref;
52
+    AVBufferRef *device_ref;
53
+} RKMPPDecoder;
54
+
55
+typedef struct {
56
+    AVClass *av_class;
57
+    AVBufferRef *decoder_ref;
58
+} RKMPPDecodeContext;
59
+
60
+typedef struct {
61
+    MppFrame frame;
62
+    AVBufferRef *decoder_ref;
63
+} RKMPPFrameContext;
64
+
65
+static MppCodingType rkmpp_get_codingtype(AVCodecContext *avctx)
66
+{
67
+    switch (avctx->codec_id) {
68
+    case AV_CODEC_ID_H264:          return MPP_VIDEO_CodingAVC;
69
+    case AV_CODEC_ID_HEVC:          return MPP_VIDEO_CodingHEVC;
70
+    case AV_CODEC_ID_VP8:           return MPP_VIDEO_CodingVP8;
71
+    case AV_CODEC_ID_VP9:           return MPP_VIDEO_CodingVP9;
72
+    default:                        return MPP_VIDEO_CodingUnused;
73
+    }
74
+}
75
+
76
+static uint32_t rkmpp_get_frameformat(MppFrameFormat mppformat)
77
+{
78
+    switch (mppformat) {
79
+    case MPP_FMT_YUV420SP:          return DRM_FORMAT_NV12;
80
+#ifdef DRM_FORMAT_NV12_10
81
+    case MPP_FMT_YUV420SP_10BIT:    return DRM_FORMAT_NV12_10;
82
+#endif
83
+    default:                        return 0;
84
+    }
85
+}
86
+
87
+static int rkmpp_write_data(AVCodecContext *avctx, uint8_t *buffer, int size, int64_t pts)
88
+{
89
+    RKMPPDecodeContext *rk_context = avctx->priv_data;
90
+    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;
91
+    int ret;
92
+    MppPacket packet;
93
+
94
+    // create the MPP packet
95
+    ret = mpp_packet_init(&packet, buffer, size);
96
+    if (ret != MPP_OK) {
97
+        av_log(avctx, AV_LOG_ERROR, "Failed to init MPP packet (code = %d)\n", ret);
98
+        return AVERROR_UNKNOWN;
99
+    }
100
+
101
+    mpp_packet_set_pts(packet, pts);
102
+
103
+    if (!buffer)
104
+        mpp_packet_set_eos(packet);
105
+
106
+    ret = decoder->mpi->decode_put_packet(decoder->ctx, packet);
107
+    if (ret != MPP_OK) {
108
+        if (ret == MPP_ERR_BUFFER_FULL) {
109
+            av_log(avctx, AV_LOG_DEBUG, "Buffer full writing %d bytes to decoder\n", size);
110
+            ret = AVERROR(EAGAIN);
111
+        } else
112
+            ret = AVERROR_UNKNOWN;
113
+    }
114
+    else
115
+        av_log(avctx, AV_LOG_DEBUG, "Wrote %d bytes to decoder\n", size);
116
+
117
+    mpp_packet_deinit(&packet);
118
+
119
+    return ret;
120
+}
121
+
122
+static int rkmpp_close_decoder(AVCodecContext *avctx)
123
+{
124
+    RKMPPDecodeContext *rk_context = avctx->priv_data;
125
+    av_buffer_unref(&rk_context->decoder_ref);
126
+    return 0;
127
+}
128
+
129
+static void rkmpp_release_decoder(void *opaque, uint8_t *data)
130
+{
131
+    RKMPPDecoder *decoder = (RKMPPDecoder *)data;
132
+
133
+    if (decoder->mpi) {
134
+        decoder->mpi->reset(decoder->ctx);
135
+        mpp_destroy(decoder->ctx);
136
+        decoder->ctx = NULL;
137
+    }
138
+
139
+    if (decoder->frame_group) {
140
+        mpp_buffer_group_put(decoder->frame_group);
141
+        decoder->frame_group = NULL;
142
+    }
143
+
144
+    av_buffer_unref(&decoder->frames_ref);
145
+    av_buffer_unref(&decoder->device_ref);
146
+
147
+    av_free(decoder);
148
+}
149
+
150
+static int rkmpp_init_decoder(AVCodecContext *avctx)
151
+{
152
+    RKMPPDecodeContext *rk_context = avctx->priv_data;
153
+    RKMPPDecoder *decoder = NULL;
154
+    MppCodingType codectype = MPP_VIDEO_CodingUnused;
155
+    int ret;
156
+    RK_S64 paramS64;
157
+    RK_S32 paramS32;
158
+
159
+    avctx->pix_fmt = AV_PIX_FMT_DRM_PRIME;
160
+
161
+    // create a decoder and a ref to it
162
+    decoder = av_mallocz(sizeof(RKMPPDecoder));
163
+    if (!decoder) {
164
+        ret = AVERROR(ENOMEM);
165
+        goto fail;
166
+    }
167
+
168
+    rk_context->decoder_ref = av_buffer_create((uint8_t *)decoder, sizeof(*decoder), rkmpp_release_decoder,
169
+                                               NULL, AV_BUFFER_FLAG_READONLY);
170
+    if (!rk_context->decoder_ref) {
171
+        av_free(decoder);
172
+        ret = AVERROR(ENOMEM);
173
+        goto fail;
174
+    }
175
+
176
+    av_log(avctx, AV_LOG_DEBUG, "Initializing RKMPP decoder.\n");
177
+
178
+    codectype = rkmpp_get_codingtype(avctx);
179
+    if (codectype == MPP_VIDEO_CodingUnused) {
180
+        av_log(avctx, AV_LOG_ERROR, "Unknown codec type (%d).\n", avctx->codec_id);
181
+        ret = AVERROR_UNKNOWN;
182
+        goto fail;
183
+    }
184
+
185
+    ret = mpp_check_support_format(MPP_CTX_DEC, codectype);
186
+    if (ret != MPP_OK) {
187
+        av_log(avctx, AV_LOG_ERROR, "Codec type (%d) unsupported by MPP\n", avctx->codec_id);
188
+        ret = AVERROR_UNKNOWN;
189
+        goto fail;
190
+    }
191
+
192
+    // Create the MPP context
193
+    ret = mpp_create(&decoder->ctx, &decoder->mpi);
194
+    if (ret != MPP_OK) {
195
+        av_log(avctx, AV_LOG_ERROR, "Failed to create MPP context (code = %d).\n", ret);
196
+        ret = AVERROR_UNKNOWN;
197
+        goto fail;
198
+    }
199
+
200
+    // initialize mpp
201
+    ret = mpp_init(decoder->ctx, MPP_CTX_DEC, codectype);
202
+    if (ret != MPP_OK) {
203
+        av_log(avctx, AV_LOG_ERROR, "Failed to initialize MPP context (code = %d).\n", ret);
204
+        ret = AVERROR_UNKNOWN;
205
+        goto fail;
206
+    }
207
+
208
+    // make decode calls blocking with a timeout
209
+    paramS32 = MPP_POLL_BLOCK;
210
+    ret = decoder->mpi->control(decoder->ctx, MPP_SET_OUTPUT_BLOCK, &paramS32);
211
+    if (ret != MPP_OK) {
212
+        av_log(avctx, AV_LOG_ERROR, "Failed to set blocking mode on MPI (code = %d).\n", ret);
213
+        ret = AVERROR_UNKNOWN;
214
+        goto fail;
215
+    }
216
+
217
+    paramS64 = RECEIVE_FRAME_TIMEOUT;
218
+    ret = decoder->mpi->control(decoder->ctx, MPP_SET_OUTPUT_BLOCK_TIMEOUT, &paramS64);
219
+    if (ret != MPP_OK) {
220
+        av_log(avctx, AV_LOG_ERROR, "Failed to set block timeout on MPI (code = %d).\n", ret);
221
+        ret = AVERROR_UNKNOWN;
222
+        goto fail;
223
+    }
224
+
225
+    ret = mpp_buffer_group_get_internal(&decoder->frame_group, MPP_BUFFER_TYPE_ION);
226
+    if (ret) {
227
+       av_log(avctx, AV_LOG_ERROR, "Failed to retrieve buffer group (code = %d)\n", ret);
228
+       ret = AVERROR_UNKNOWN;
229
+       goto fail;
230
+    }
231
+
232
+    ret = decoder->mpi->control(decoder->ctx, MPP_DEC_SET_EXT_BUF_GROUP, decoder->frame_group);
233
+    if (ret) {
234
+        av_log(avctx, AV_LOG_ERROR, "Failed to assign buffer group (code = %d)\n", ret);
235
+        ret = AVERROR_UNKNOWN;
236
+        goto fail;
237
+    }
238
+
239
+    ret = mpp_buffer_group_limit_config(decoder->frame_group, 0, FRAMEGROUP_MAX_FRAMES);
240
+    if (ret) {
241
+        av_log(avctx, AV_LOG_ERROR, "Failed to set buffer group limit (code = %d)\n", ret);
242
+        ret = AVERROR_UNKNOWN;
243
+        goto fail;
244
+    }
245
+
246
+    decoder->first_packet = 1;
247
+
248
+    av_log(avctx, AV_LOG_DEBUG, "RKMPP decoder initialized successfully.\n");
249
+
250
+    decoder->device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DRM);
251
+    if (!decoder->device_ref) {
252
+        ret = AVERROR(ENOMEM);
253
+        goto fail;
254
+    }
255
+    ret = av_hwdevice_ctx_init(decoder->device_ref);
256
+    if (ret < 0)
257
+        goto fail;
258
+
259
+    return 0;
260
+
261
+fail:
262
+    av_log(avctx, AV_LOG_ERROR, "Failed to initialize RKMPP decoder.\n");
263
+    rkmpp_close_decoder(avctx);
264
+    return ret;
265
+}
266
+
267
+static int rkmpp_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
268
+{
269
+    RKMPPDecodeContext *rk_context = avctx->priv_data;
270
+    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;
271
+    int ret;
272
+
273
+    // handle EOF
274
+    if (!avpkt->size) {
275
+        av_log(avctx, AV_LOG_DEBUG, "End of stream.\n");
276
+        decoder->eos_reached = 1;
277
+        ret = rkmpp_write_data(avctx, NULL, 0, 0);
278
+        if (ret)
279
+            av_log(avctx, AV_LOG_ERROR, "Failed to send EOS to decoder (code = %d)\n", ret);
280
+        return ret;
281
+    }
282
+
283
+    // on first packet, send extradata
284
+    if (decoder->first_packet) {
285
+        if (avctx->extradata_size) {
286
+            ret = rkmpp_write_data(avctx, avctx->extradata,
287
+                                            avctx->extradata_size,
288
+                                            avpkt->pts);
289
+            if (ret) {
290
+                av_log(avctx, AV_LOG_ERROR, "Failed to write extradata to decoder (code = %d)\n", ret);
291
+                return ret;
292
+            }
293
+        }
294
+        decoder->first_packet = 0;
295
+    }
296
+
297
+    // now send packet
298
+    ret = rkmpp_write_data(avctx, avpkt->data, avpkt->size, avpkt->pts);
299
+    if (ret && ret!=AVERROR(EAGAIN))
300
+        av_log(avctx, AV_LOG_ERROR, "Failed to write data to decoder (code = %d)\n", ret);
301
+
302
+    return ret;
303
+}
304
+
305
+static void rkmpp_release_frame(void *opaque, uint8_t *data)
306
+{
307
+    AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)data;
308
+    AVBufferRef *framecontextref = (AVBufferRef *)opaque;
309
+    RKMPPFrameContext *framecontext = (RKMPPFrameContext *)framecontextref->data;
310
+
311
+    mpp_frame_deinit(&framecontext->frame);
312
+    av_buffer_unref(&framecontext->decoder_ref);
313
+    av_buffer_unref(&framecontextref);
314
+
315
+    av_free(desc);
316
+}
317
+
318
+static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame)
319
+{
320
+    RKMPPDecodeContext *rk_context = avctx->priv_data;
321
+    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;
322
+    RKMPPFrameContext *framecontext = NULL;
323
+    AVBufferRef *framecontextref = NULL;
324
+    int ret;
325
+    MppFrame mppframe = NULL;
326
+    MppBuffer buffer = NULL;
327
+    AVDRMFrameDescriptor *desc = NULL;
328
+    AVDRMLayerDescriptor *layer = NULL;
329
+    int retrycount = 0;
330
+    int mode;
331
+    MppFrameFormat mppformat;
332
+    uint32_t drmformat;
333
+
334
+    // on start of decoding, MPP can return -1, which is supposed to be expected
335
+    // this is due to some internal MPP init which is not completed, that will
336
+    // only happen in the first few frames queries, but should not be interpreted
337
+    // as an error, Therefore we need to retry a couple times when we get -1
338
+    // in order to let it time to complete it's init, then we sleep a bit between retries.
339
+retry_get_frame:
340
+    ret = decoder->mpi->decode_get_frame(decoder->ctx, &mppframe);
341
+    if (ret != MPP_OK && ret != MPP_ERR_TIMEOUT && !decoder->first_frame) {
342
+        if (retrycount < 5) {
343
+            av_log(avctx, AV_LOG_DEBUG, "Failed to get a frame, retrying (code = %d, retrycount = %d)\n", ret, retrycount);
344
+            usleep(10000);
345
+            retrycount++;
346
+            goto retry_get_frame;
347
+        } else {
348
+            av_log(avctx, AV_LOG_ERROR, "Failed to get a frame from MPP (code = %d)\n", ret);
349
+            goto fail;
350
+        }
351
+    }
352
+
353
+    if (mppframe) {
354
+        // Check wether we have a special frame or not
355
+        if (mpp_frame_get_info_change(mppframe)) {
356
+            AVHWFramesContext *hwframes;
357
+
358
+            av_log(avctx, AV_LOG_INFO, "Decoder noticed an info change (%dx%d), format=%d\n",
359
+                                        (int)mpp_frame_get_width(mppframe), (int)mpp_frame_get_height(mppframe),
360
+                                        (int)mpp_frame_get_fmt(mppframe));
361
+
362
+            avctx->width = mpp_frame_get_width(mppframe);
363
+            avctx->height = mpp_frame_get_height(mppframe);
364
+
365
+            decoder->mpi->control(decoder->ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);
366
+            decoder->first_frame = 1;
367
+
368
+            av_buffer_unref(&decoder->frames_ref);
369
+
370
+            decoder->frames_ref = av_hwframe_ctx_alloc(decoder->device_ref);
371
+            if (!decoder->frames_ref) {
372
+                ret = AVERROR(ENOMEM);
373
+                goto fail;
374
+            }
375
+
376
+            mppformat = mpp_frame_get_fmt(mppframe);
377
+            drmformat = rkmpp_get_frameformat(mppformat);
378
+
379
+            hwframes = (AVHWFramesContext*)decoder->frames_ref->data;
380
+            hwframes->format    = AV_PIX_FMT_DRM_PRIME;
381
+            hwframes->sw_format = drmformat == DRM_FORMAT_NV12 ? AV_PIX_FMT_NV12 : AV_PIX_FMT_NONE;
382
+            hwframes->width     = avctx->width;
383
+            hwframes->height    = avctx->height;
384
+            ret = av_hwframe_ctx_init(decoder->frames_ref);
385
+            if (ret < 0)
386
+                goto fail;
387
+
388
+            // here decoder is fully initialized, we need to feed it again with data
389
+            ret = AVERROR(EAGAIN);
390
+            goto fail;
391
+        } else if (mpp_frame_get_eos(mppframe)) {
392
+            av_log(avctx, AV_LOG_DEBUG, "Received a EOS frame.\n");
393
+            decoder->eos_reached = 1;
394
+            ret = AVERROR_EOF;
395
+            goto fail;
396
+        } else if (mpp_frame_get_discard(mppframe)) {
397
+            av_log(avctx, AV_LOG_DEBUG, "Received a discard frame.\n");
398
+            ret = AVERROR(EAGAIN);
399
+            goto fail;
400
+        } else if (mpp_frame_get_errinfo(mppframe)) {
401
+            av_log(avctx, AV_LOG_ERROR, "Received a errinfo frame.\n");
402
+            ret = AVERROR_UNKNOWN;
403
+            goto fail;
404
+        }
405
+
406
+        // here we should have a valid frame
407
+        av_log(avctx, AV_LOG_DEBUG, "Received a frame.\n");
408
+
409
+        // setup general frame fields
410
+        frame->format           = AV_PIX_FMT_DRM_PRIME;
411
+        frame->width            = mpp_frame_get_width(mppframe);
412
+        frame->height           = mpp_frame_get_height(mppframe);
413
+        frame->pts              = mpp_frame_get_pts(mppframe);
414
+        frame->color_range      = mpp_frame_get_color_range(mppframe);
415
+        frame->color_primaries  = mpp_frame_get_color_primaries(mppframe);
416
+        frame->color_trc        = mpp_frame_get_color_trc(mppframe);
417
+        frame->colorspace       = mpp_frame_get_colorspace(mppframe);
418
+
419
+        mode = mpp_frame_get_mode(mppframe);
420
+        frame->interlaced_frame = ((mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_DEINTERLACED);
421
+        frame->top_field_first  = ((mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_TOP_FIRST);
422
+
423
+        mppformat = mpp_frame_get_fmt(mppframe);
424
+        drmformat = rkmpp_get_frameformat(mppformat);
425
+
426
+        // now setup the frame buffer info
427
+        buffer = mpp_frame_get_buffer(mppframe);
428
+        if (buffer) {
429
+            desc = av_mallocz(sizeof(AVDRMFrameDescriptor));
430
+            if (!desc) {
431
+                ret = AVERROR(ENOMEM);
432
+                goto fail;
433
+            }
434
+
435
+            desc->nb_objects = 1;
436
+            desc->objects[0].fd = mpp_buffer_get_fd(buffer);
437
+            desc->objects[0].size = mpp_buffer_get_size(buffer);
438
+
439
+            desc->nb_layers = 1;
440
+            layer = &desc->layers[0];
441
+            layer->format = drmformat;
442
+            layer->nb_planes = 2;
443
+
444
+            layer->planes[0].object_index = 0;
445
+            layer->planes[0].offset = 0;
446
+            layer->planes[0].pitch = mpp_frame_get_hor_stride(mppframe);
447
+
448
+            layer->planes[1].object_index = 0;
449
+            layer->planes[1].offset = layer->planes[0].pitch * mpp_frame_get_ver_stride(mppframe);
450
+            layer->planes[1].pitch = layer->planes[0].pitch;
451
+
452
+            // we also allocate a struct in buf[0] that will allow to hold additionnal information
453
+            // for releasing properly MPP frames and decoder
454
+            framecontextref = av_buffer_allocz(sizeof(*framecontext));
455
+            if (!framecontextref) {
456
+                ret = AVERROR(ENOMEM);
457
+                goto fail;
458
+            }
459
+
460
+            // MPP decoder needs to be closed only when all frames have been released.
461
+            framecontext = (RKMPPFrameContext *)framecontextref->data;
462
+            framecontext->decoder_ref = av_buffer_ref(rk_context->decoder_ref);
463
+            framecontext->frame = mppframe;
464
+
465
+            frame->data[0]  = (uint8_t *)desc;
466
+            frame->buf[0]   = av_buffer_create((uint8_t *)desc, sizeof(*desc), rkmpp_release_frame,
467
+                                               framecontextref, AV_BUFFER_FLAG_READONLY);
468
+
469
+            if (!frame->buf[0]) {
470
+                ret = AVERROR(ENOMEM);
471
+                goto fail;
472
+            }
473
+
474
+            frame->hw_frames_ctx = av_buffer_ref(decoder->frames_ref);
475
+            if (!frame->hw_frames_ctx) {
476
+                ret = AVERROR(ENOMEM);
477
+                goto fail;
478
+            }
479
+
480
+            decoder->first_frame = 0;
481
+            return 0;
482
+        } else {
483
+            av_log(avctx, AV_LOG_ERROR, "Failed to retrieve the frame buffer, frame is dropped (code = %d)\n", ret);
484
+            mpp_frame_deinit(&mppframe);
485
+        }
486
+    } else if (decoder->eos_reached) {
487
+        return AVERROR_EOF;
488
+    } else if (ret == MPP_ERR_TIMEOUT) {
489
+        av_log(avctx, AV_LOG_DEBUG, "Timeout when trying to get a frame from MPP\n");
490
+    }
491
+
492
+    return AVERROR(EAGAIN);
493
+
494
+fail:
495
+    if (mppframe)
496
+        mpp_frame_deinit(&mppframe);
497
+
498
+    if (framecontext)
499
+        av_buffer_unref(&framecontext->decoder_ref);
500
+
501
+    if (framecontextref)
502
+        av_buffer_unref(&framecontextref);
503
+
504
+    if (desc)
505
+        av_free(desc);
506
+
507
+    return ret;
508
+}
509
+
510
+static int rkmpp_receive_frame(AVCodecContext *avctx, AVFrame *frame)
511
+{
512
+    RKMPPDecodeContext *rk_context = avctx->priv_data;
513
+    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;
514
+    int ret = MPP_NOK;
515
+    AVPacket pkt = {0};
516
+    RK_S32 freeslots;
517
+
518
+    if (!decoder->eos_reached) {
519
+        // we get the available slots in decoder
520
+        ret = decoder->mpi->control(decoder->ctx, MPP_DEC_GET_FREE_PACKET_SLOT_COUNT, &freeslots);
521
+        if (ret != MPP_OK) {
522
+            av_log(avctx, AV_LOG_ERROR, "Failed to get decoder free slots (code = %d).\n", ret);
523
+            return ret;
524
+        }
525
+
526
+        if (freeslots > 0) {
527
+            ret = ff_decode_get_packet(avctx, &pkt);
528
+            if (ret < 0 && ret != AVERROR_EOF) {
529
+                return ret;
530
+            }
531
+
532
+            ret = rkmpp_send_packet(avctx, &pkt);
533
+            av_packet_unref(&pkt);
534
+
535
+            if (ret < 0) {
536
+                av_log(avctx, AV_LOG_ERROR, "Failed to send packet to decoder (code = %d)\n", ret);
537
+                return ret;
538
+            }
539
+        }
540
+
541
+        // make sure we keep decoder full
542
+        if (freeslots > 1 && decoder->first_frame)
543
+            return AVERROR(EAGAIN);
544
+    }
545
+
546
+    return rkmpp_retrieve_frame(avctx, frame);
547
+}
548
+
549
+static void rkmpp_flush(AVCodecContext *avctx)
550
+{
551
+    RKMPPDecodeContext *rk_context = avctx->priv_data;
552
+    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;
553
+    int ret = MPP_NOK;
554
+
555
+    av_log(avctx, AV_LOG_DEBUG, "Flush.\n");
556
+
557
+    ret = decoder->mpi->reset(decoder->ctx);
558
+    if (ret == MPP_OK) {
559
+        decoder->first_frame = 1;
560
+        decoder->first_packet = 1;
561
+    } else
562
+        av_log(avctx, AV_LOG_ERROR, "Failed to reset MPI (code = %d)\n", ret);
563
+}
564
+
565
+
566
+#define RKMPP_DEC_CLASS(NAME) \
567
+    static const AVClass rkmpp_##NAME##_dec_class = { \
568
+        .class_name = "rkmpp_" #NAME "_dec", \
569
+        .version    = LIBAVUTIL_VERSION_INT, \
570
+    };
571
+
572
+#define RKMPP_DEC(NAME, ID, BSFS) \
573
+    RKMPP_DEC_CLASS(NAME) \
574
+    AVCodec ff_##NAME##_rkmpp_decoder = { \
575
+        .name           = #NAME "_rkmpp", \
576
+        .long_name      = NULL_IF_CONFIG_SMALL(#NAME " (rkmpp)"), \
577
+        .type           = AVMEDIA_TYPE_VIDEO, \
578
+        .id             = ID, \
579
+        .priv_data_size = sizeof(RKMPPDecodeContext), \
580
+        .init           = rkmpp_init_decoder, \
581
+        .close          = rkmpp_close_decoder, \
582
+        .receive_frame  = rkmpp_receive_frame, \
583
+        .flush          = rkmpp_flush, \
584
+        .priv_class     = &rkmpp_##NAME##_dec_class, \
585
+        .capabilities   = AV_CODEC_CAP_DELAY, \
586
+        .caps_internal  = AV_CODEC_CAP_AVOID_PROBING, \
587
+        .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_DRM_PRIME, \
588
+                                                         AV_PIX_FMT_NONE}, \
589
+        .bsfs           = BSFS, \
590
+    };
591
+
592
+RKMPP_DEC(h264,  AV_CODEC_ID_H264,          "h264_mp4toannexb")
593
+RKMPP_DEC(hevc,  AV_CODEC_ID_HEVC,          "hevc_mp4toannexb")
594
+RKMPP_DEC(vp8,   AV_CODEC_ID_VP8,           NULL)
595
+RKMPP_DEC(vp9,   AV_CODEC_ID_VP9,           NULL)
... ...
@@ -29,7 +29,7 @@
29 29
 
30 30
 #define LIBAVCODEC_VERSION_MAJOR  57
31 31
 #define LIBAVCODEC_VERSION_MINOR 106
32
-#define LIBAVCODEC_VERSION_MICRO 102
32
+#define LIBAVCODEC_VERSION_MICRO 103
33 33
 
34 34
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
35 35
                                                LIBAVCODEC_VERSION_MINOR, \