2 * Android MediaCodec MPEG-2 / H.264 / H.265 / MPEG-4 / VP8 / VP9 decoders
4 * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/avassert.h"
27 #include "libavutil/common.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/pixfmt.h"
31 #include "libavutil/internal.h"
35 #include "h264_parse.h"
36 #include "hevc_parse.h"
39 #include "mediacodec_wrapper.h"
40 #include "mediacodecdec_common.h"
42 typedef struct MediaCodecH264DecContext {
46 MediaCodecDecContext *ctx;
48 AVPacket buffered_pkt;
51 int amlogic_mpeg2_api23_workaround;
53 } MediaCodecH264DecContext;
55 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
57 MediaCodecH264DecContext *s = avctx->priv_data;
59 ff_mediacodec_dec_close(avctx, s->ctx);
62 av_packet_unref(&s->buffered_pkt);
67 #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
68 static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
73 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
75 if (!out || !out_size) {
76 return AVERROR(EINVAL);
79 p = av_malloc(sizeof(nalu_header) + src_size);
81 return AVERROR(ENOMEM);
85 *out_size = sizeof(nalu_header) + src_size;
87 memcpy(p, nalu_header, sizeof(nalu_header));
88 memcpy(p + sizeof(nalu_header), src, src_size);
90 /* Escape 0x00, 0x00, 0x0{0-3} pattern */
91 for (i = 4; i < *out_size; i++) {
92 if (i < *out_size - 3 &&
99 new = av_realloc(*out, *out_size);
101 ret = AVERROR(ENOMEM);
107 memmove(p + i + 1, p + i, *out_size - (i + 1));
121 #if CONFIG_H264_MEDIACODEC_DECODER
122 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
128 const PPS *pps = NULL;
129 const SPS *sps = NULL;
131 int nal_length_size = 0;
133 memset(&ps, 0, sizeof(ps));
135 ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
136 &ps, &is_avc, &nal_length_size, 0, avctx);
141 for (i = 0; i < MAX_PPS_COUNT; i++) {
142 if (ps.pps_list[i]) {
143 pps = (const PPS*)ps.pps_list[i]->data;
149 if (ps.sps_list[pps->sps_id]) {
150 sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
155 uint8_t *data = NULL;
158 if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
161 ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
164 if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
167 ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
170 av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
171 ret = AVERROR_INVALIDDATA;
175 ff_h264_ps_uninit(&ps);
181 #if CONFIG_HEVC_MEDIACODEC_DECODER
182 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
190 const HEVCVPS *vps = NULL;
191 const HEVCPPS *pps = NULL;
192 const HEVCSPS *sps = NULL;
194 int nal_length_size = 0;
196 uint8_t *vps_data = NULL;
197 uint8_t *sps_data = NULL;
198 uint8_t *pps_data = NULL;
199 int vps_data_size = 0;
200 int sps_data_size = 0;
201 int pps_data_size = 0;
203 memset(&ps, 0, sizeof(ps));
204 memset(&sei, 0, sizeof(sei));
206 ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
207 &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
212 for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
213 if (ps.vps_list[i]) {
214 vps = (const HEVCVPS*)ps.vps_list[i]->data;
219 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
220 if (ps.pps_list[i]) {
221 pps = (const HEVCPPS*)ps.pps_list[i]->data;
227 if (ps.sps_list[pps->sps_id]) {
228 sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
232 if (vps && pps && sps) {
236 if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
237 (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
238 (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
242 data_size = vps_data_size + sps_data_size + pps_data_size;
243 data = av_mallocz(data_size);
245 ret = AVERROR(ENOMEM);
249 memcpy(data , vps_data, vps_data_size);
250 memcpy(data + vps_data_size , sps_data, sps_data_size);
251 memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
253 ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
257 av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
258 ret = AVERROR_INVALIDDATA;
262 ff_hevc_ps_uninit(&ps);
272 #if CONFIG_MPEG2_MEDIACODEC_DECODER || \
273 CONFIG_MPEG4_MEDIACODEC_DECODER || \
274 CONFIG_VP8_MEDIACODEC_DECODER || \
275 CONFIG_VP9_MEDIACODEC_DECODER
276 static int common_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
280 if (avctx->extradata) {
281 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
288 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
293 const char *codec_mime = NULL;
295 FFAMediaFormat *format = NULL;
296 MediaCodecH264DecContext *s = avctx->priv_data;
298 format = ff_AMediaFormat_new();
300 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
301 ret = AVERROR_EXTERNAL;
305 switch (avctx->codec_id) {
306 #if CONFIG_H264_MEDIACODEC_DECODER
307 case AV_CODEC_ID_H264:
308 codec_mime = "video/avc";
310 ret = h264_set_extradata(avctx, format);
315 #if CONFIG_HEVC_MEDIACODEC_DECODER
316 case AV_CODEC_ID_HEVC:
317 codec_mime = "video/hevc";
319 ret = hevc_set_extradata(avctx, format);
324 #if CONFIG_MPEG2_MEDIACODEC_DECODER
325 case AV_CODEC_ID_MPEG2VIDEO:
326 codec_mime = "video/mpeg2";
328 ret = common_set_extradata(avctx, format);
333 #if CONFIG_MPEG4_MEDIACODEC_DECODER
334 case AV_CODEC_ID_MPEG4:
335 codec_mime = "video/mp4v-es",
337 ret = common_set_extradata(avctx, format);
342 #if CONFIG_VP8_MEDIACODEC_DECODER
343 case AV_CODEC_ID_VP8:
344 codec_mime = "video/x-vnd.on2.vp8";
346 ret = common_set_extradata(avctx, format);
351 #if CONFIG_VP9_MEDIACODEC_DECODER
352 case AV_CODEC_ID_VP9:
353 codec_mime = "video/x-vnd.on2.vp9";
355 ret = common_set_extradata(avctx, format);
364 ff_AMediaFormat_setString(format, "mime", codec_mime);
365 ff_AMediaFormat_setInt32(format, "width", avctx->width);
366 ff_AMediaFormat_setInt32(format, "height", avctx->height);
368 s->ctx = av_mallocz(sizeof(*s->ctx));
370 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
371 ret = AVERROR(ENOMEM);
375 s->ctx->delay_flush = s->delay_flush;
377 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
382 av_log(avctx, AV_LOG_INFO,
383 "MediaCodec started successfully: codec = %s, ret = %d\n",
384 s->ctx->codec_name, ret);
386 sdk_int = ff_Build_SDK_INT(avctx);
388 strcmp(s->ctx->codec_name, "OMX.amlogic.mpeg2.decoder.awesome") == 0) {
389 av_log(avctx, AV_LOG_INFO, "Enabling workaround for %s on API=%d\n",
390 s->ctx->codec_name, sdk_int);
391 s->amlogic_mpeg2_api23_workaround = 1;
396 ff_AMediaFormat_delete(format);
400 mediacodec_decode_close(avctx);
406 static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
408 MediaCodecH264DecContext *s = avctx->priv_data;
412 /* In delay_flush mode, wait until the user has released or rendered
413 all retained frames. */
414 if (s->delay_flush && ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
415 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
416 return AVERROR(EAGAIN);
420 /* poll for new frame */
421 ret = ff_mediacodec_dec_receive(avctx, s->ctx, frame, false);
422 if (ret != AVERROR(EAGAIN))
427 if (s->ctx->current_input_buffer < 0) {
428 /* poll for input space */
429 index = ff_AMediaCodec_dequeueInputBuffer(s->ctx->codec, 0);
431 /* no space, block for an output frame to appear */
432 return ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
434 s->ctx->current_input_buffer = index;
437 /* try to flush any buffered packet data */
438 if (s->buffered_pkt.size > 0) {
439 ret = ff_mediacodec_dec_send(avctx, s->ctx, &s->buffered_pkt, false);
441 s->buffered_pkt.size -= ret;
442 s->buffered_pkt.data += ret;
443 if (s->buffered_pkt.size <= 0)
444 av_packet_unref(&s->buffered_pkt);
445 } else if (ret < 0 && ret != AVERROR(EAGAIN)) {
449 if (s->amlogic_mpeg2_api23_workaround && s->buffered_pkt.size <= 0) {
450 /* fallthrough to fetch next packet regardless of input buffer space */
452 /* poll for space again */
457 /* fetch new packet or eof */
458 ret = ff_decode_get_packet(avctx, &s->buffered_pkt);
459 if (ret == AVERROR_EOF) {
460 AVPacket null_pkt = { 0 };
461 ret = ff_mediacodec_dec_send(avctx, s->ctx, &null_pkt, true);
464 } else if (ret == AVERROR(EAGAIN) && s->ctx->current_input_buffer < 0) {
465 return ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
466 } else if (ret < 0) {
471 return AVERROR(EAGAIN);
474 static void mediacodec_decode_flush(AVCodecContext *avctx)
476 MediaCodecH264DecContext *s = avctx->priv_data;
478 av_packet_unref(&s->buffered_pkt);
480 ff_mediacodec_dec_flush(avctx, s->ctx);
483 static const AVCodecHWConfigInternal *mediacodec_hw_configs[] = {
484 &(const AVCodecHWConfigInternal) {
486 .pix_fmt = AV_PIX_FMT_MEDIACODEC,
487 .methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
488 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
489 .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
496 #define OFFSET(x) offsetof(MediaCodecH264DecContext, x)
497 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
498 static const AVOption ff_mediacodec_vdec_options[] = {
499 { "delay_flush", "Delay flush until hw output buffers are returned to the decoder",
500 OFFSET(delay_flush), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VD },
504 #define DECLARE_MEDIACODEC_VCLASS(short_name) \
505 static const AVClass ff_##short_name##_mediacodec_dec_class = { \
506 .class_name = #short_name "_mediacodec", \
507 .item_name = av_default_item_name, \
508 .option = ff_mediacodec_vdec_options, \
509 .version = LIBAVUTIL_VERSION_INT, \
512 #define DECLARE_MEDIACODEC_VDEC(short_name, full_name, codec_id, bsf) \
513 DECLARE_MEDIACODEC_VCLASS(short_name) \
514 AVCodec ff_##short_name##_mediacodec_decoder = { \
515 .name = #short_name "_mediacodec", \
516 .long_name = NULL_IF_CONFIG_SMALL(full_name " Android MediaCodec decoder"), \
517 .type = AVMEDIA_TYPE_VIDEO, \
519 .priv_class = &ff_##short_name##_mediacodec_dec_class, \
520 .priv_data_size = sizeof(MediaCodecH264DecContext), \
521 .init = mediacodec_decode_init, \
522 .receive_frame = mediacodec_receive_frame, \
523 .flush = mediacodec_decode_flush, \
524 .close = mediacodec_decode_close, \
525 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
526 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
528 .hw_configs = mediacodec_hw_configs, \
529 .wrapper_name = "mediacodec", \
532 #if CONFIG_H264_MEDIACODEC_DECODER
533 DECLARE_MEDIACODEC_VDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb")
536 #if CONFIG_HEVC_MEDIACODEC_DECODER
537 DECLARE_MEDIACODEC_VDEC(hevc, "H.265", AV_CODEC_ID_HEVC, "hevc_mp4toannexb")
540 #if CONFIG_MPEG2_MEDIACODEC_DECODER
541 DECLARE_MEDIACODEC_VDEC(mpeg2, "MPEG-2", AV_CODEC_ID_MPEG2VIDEO, NULL)
544 #if CONFIG_MPEG4_MEDIACODEC_DECODER
545 DECLARE_MEDIACODEC_VDEC(mpeg4, "MPEG-4", AV_CODEC_ID_MPEG4, NULL)
548 #if CONFIG_VP8_MEDIACODEC_DECODER
549 DECLARE_MEDIACODEC_VDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL)
552 #if CONFIG_VP9_MEDIACODEC_DECODER
553 DECLARE_MEDIACODEC_VDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL)