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/fifo.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/pixfmt.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 {
44 MediaCodecDecContext *ctx;
48 AVPacket buffered_pkt;
50 } MediaCodecH264DecContext;
52 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
54 MediaCodecH264DecContext *s = avctx->priv_data;
56 ff_mediacodec_dec_close(avctx, s->ctx);
59 av_fifo_free(s->fifo);
61 av_packet_unref(&s->buffered_pkt);
66 #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
67 static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
72 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
74 if (!out || !out_size) {
75 return AVERROR(EINVAL);
78 p = av_malloc(sizeof(nalu_header) + src_size);
80 return AVERROR(ENOMEM);
84 *out_size = sizeof(nalu_header) + src_size;
86 memcpy(p, nalu_header, sizeof(nalu_header));
87 memcpy(p + sizeof(nalu_header), src, src_size);
89 /* Escape 0x00, 0x00, 0x0{0-3} pattern */
90 for (i = 4; i < *out_size; i++) {
91 if (i < *out_size - 3 &&
98 new = av_realloc(*out, *out_size);
100 ret = AVERROR(ENOMEM);
106 memmove(p + i + 1, p + i, *out_size - (i + 1));
120 #if CONFIG_H264_MEDIACODEC_DECODER
121 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
127 const PPS *pps = NULL;
128 const SPS *sps = NULL;
130 int nal_length_size = 0;
132 memset(&ps, 0, sizeof(ps));
134 ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
135 &ps, &is_avc, &nal_length_size, 0, avctx);
140 for (i = 0; i < MAX_PPS_COUNT; i++) {
141 if (ps.pps_list[i]) {
142 pps = (const PPS*)ps.pps_list[i]->data;
148 if (ps.sps_list[pps->sps_id]) {
149 sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
154 uint8_t *data = NULL;
157 if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
160 ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
163 if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
166 ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
169 av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
170 ret = AVERROR_INVALIDDATA;
174 ff_h264_ps_uninit(&ps);
180 #if CONFIG_HEVC_MEDIACODEC_DECODER
181 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
189 const HEVCVPS *vps = NULL;
190 const HEVCPPS *pps = NULL;
191 const HEVCSPS *sps = NULL;
193 int nal_length_size = 0;
195 uint8_t *vps_data = NULL;
196 uint8_t *sps_data = NULL;
197 uint8_t *pps_data = NULL;
198 int vps_data_size = 0;
199 int sps_data_size = 0;
200 int pps_data_size = 0;
202 memset(&ps, 0, sizeof(ps));
203 memset(&sei, 0, sizeof(sei));
205 ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
206 &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
211 for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
212 if (ps.vps_list[i]) {
213 vps = (const HEVCVPS*)ps.vps_list[i]->data;
218 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
219 if (ps.pps_list[i]) {
220 pps = (const HEVCPPS*)ps.pps_list[i]->data;
226 if (ps.sps_list[pps->sps_id]) {
227 sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
231 if (vps && pps && sps) {
235 if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
236 (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
237 (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
241 data_size = vps_data_size + sps_data_size + pps_data_size;
242 data = av_mallocz(data_size);
244 ret = AVERROR(ENOMEM);
248 memcpy(data , vps_data, vps_data_size);
249 memcpy(data + vps_data_size , sps_data, sps_data_size);
250 memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
252 ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
256 av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
257 ret = AVERROR_INVALIDDATA;
269 #if CONFIG_MPEG2_MEDIACODEC_DECODER
270 static int mpeg2_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
274 if (avctx->extradata) {
275 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
282 #if CONFIG_MPEG4_MEDIACODEC_DECODER
283 static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
287 if (avctx->extradata) {
288 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
295 #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
296 static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
300 if (avctx->extradata) {
301 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
308 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
312 const char *codec_mime = NULL;
314 FFAMediaFormat *format = NULL;
315 MediaCodecH264DecContext *s = avctx->priv_data;
317 format = ff_AMediaFormat_new();
319 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
320 ret = AVERROR_EXTERNAL;
324 switch (avctx->codec_id) {
325 #if CONFIG_H264_MEDIACODEC_DECODER
326 case AV_CODEC_ID_H264:
327 codec_mime = "video/avc";
329 ret = h264_set_extradata(avctx, format);
334 #if CONFIG_HEVC_MEDIACODEC_DECODER
335 case AV_CODEC_ID_HEVC:
336 codec_mime = "video/hevc";
338 ret = hevc_set_extradata(avctx, format);
343 #if CONFIG_MPEG2_MEDIACODEC_DECODER
344 case AV_CODEC_ID_MPEG2VIDEO:
345 codec_mime = "video/mpeg2";
347 ret = mpeg2_set_extradata(avctx, format);
352 #if CONFIG_MPEG4_MEDIACODEC_DECODER
353 case AV_CODEC_ID_MPEG4:
354 codec_mime = "video/mp4v-es",
356 ret = mpeg4_set_extradata(avctx, format);
361 #if CONFIG_VP8_MEDIACODEC_DECODER
362 case AV_CODEC_ID_VP8:
363 codec_mime = "video/x-vnd.on2.vp8";
365 ret = vpx_set_extradata(avctx, format);
370 #if CONFIG_VP9_MEDIACODEC_DECODER
371 case AV_CODEC_ID_VP9:
372 codec_mime = "video/x-vnd.on2.vp9";
374 ret = vpx_set_extradata(avctx, format);
383 ff_AMediaFormat_setString(format, "mime", codec_mime);
384 ff_AMediaFormat_setInt32(format, "width", avctx->width);
385 ff_AMediaFormat_setInt32(format, "height", avctx->height);
387 s->ctx = av_mallocz(sizeof(*s->ctx));
389 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
390 ret = AVERROR(ENOMEM);
394 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
399 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
401 s->fifo = av_fifo_alloc(sizeof(AVPacket));
403 ret = AVERROR(ENOMEM);
409 ff_AMediaFormat_delete(format);
413 mediacodec_decode_close(avctx);
420 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
421 int *got_frame, AVPacket *pkt)
423 MediaCodecH264DecContext *s = avctx->priv_data;
425 return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
428 static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
430 MediaCodecH264DecContext *s = avctx->priv_data;
434 AVPacket pkt = { 0 };
437 * MediaCodec.flush() discards both input and output buffers, thus we
438 * need to delay the call to this function until the user has released or
439 * renderered the frames he retains.
441 * After we have buffered an input packet, check if the codec is in the
442 * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
444 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
445 * the codec (because the user retains frames). The codec stays in the
448 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
449 * performed on the codec. The codec leaves the flushing state and can
450 * process again packets.
452 * ff_mediacodec_dec_flush returns a negative value if an error has
456 if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
457 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
458 return AVERROR(EAGAIN);
462 ret = ff_decode_get_packet(avctx, &pkt);
463 if (ret == AVERROR_EOF)
465 else if (ret == AVERROR(EAGAIN))
466 ; /* no input packet, but fallthrough to check for pending frames */
470 /* buffer the input packet */
472 if (av_fifo_space(s->fifo) < sizeof(pkt)) {
473 ret = av_fifo_realloc2(s->fifo,
474 av_fifo_size(s->fifo) + sizeof(pkt));
476 av_packet_unref(&pkt);
480 av_fifo_generic_write(s->fifo, &pkt, sizeof(pkt), NULL);
483 /* process buffered data */
485 /* prepare the input data */
486 if (s->buffered_pkt.size <= 0) {
487 av_packet_unref(&s->buffered_pkt);
490 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
491 AVPacket null_pkt = { 0 };
493 ret = ff_mediacodec_dec_decode(avctx, s->ctx, frame,
494 &got_frame, &null_pkt);
502 return AVERROR(EAGAIN);
505 av_fifo_generic_read(s->fifo, &s->buffered_pkt, sizeof(s->buffered_pkt), NULL);
508 ret = mediacodec_process_data(avctx, frame, &got_frame, &s->buffered_pkt);
512 s->buffered_pkt.size -= ret;
513 s->buffered_pkt.data += ret;
519 static void mediacodec_decode_flush(AVCodecContext *avctx)
521 MediaCodecH264DecContext *s = avctx->priv_data;
523 while (av_fifo_size(s->fifo)) {
525 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
526 av_packet_unref(&pkt);
528 av_fifo_reset(s->fifo);
530 av_packet_unref(&s->buffered_pkt);
532 ff_mediacodec_dec_flush(avctx, s->ctx);
535 static const AVCodecHWConfigInternal *mediacodec_hw_configs[] = {
536 &(const AVCodecHWConfigInternal) {
538 .pix_fmt = AV_PIX_FMT_MEDIACODEC,
539 .methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
540 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
541 .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
548 #if CONFIG_H264_MEDIACODEC_DECODER
549 AVCodec ff_h264_mediacodec_decoder = {
550 .name = "h264_mediacodec",
551 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
552 .type = AVMEDIA_TYPE_VIDEO,
553 .id = AV_CODEC_ID_H264,
554 .priv_data_size = sizeof(MediaCodecH264DecContext),
555 .init = mediacodec_decode_init,
556 .receive_frame = mediacodec_receive_frame,
557 .flush = mediacodec_decode_flush,
558 .close = mediacodec_decode_close,
559 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
560 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
561 .bsfs = "h264_mp4toannexb",
562 .hw_configs = mediacodec_hw_configs,
563 .wrapper_name = "mediacodec",
567 #if CONFIG_HEVC_MEDIACODEC_DECODER
568 AVCodec ff_hevc_mediacodec_decoder = {
569 .name = "hevc_mediacodec",
570 .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
571 .type = AVMEDIA_TYPE_VIDEO,
572 .id = AV_CODEC_ID_HEVC,
573 .priv_data_size = sizeof(MediaCodecH264DecContext),
574 .init = mediacodec_decode_init,
575 .receive_frame = mediacodec_receive_frame,
576 .flush = mediacodec_decode_flush,
577 .close = mediacodec_decode_close,
578 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
579 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
580 .bsfs = "hevc_mp4toannexb",
581 .hw_configs = mediacodec_hw_configs,
582 .wrapper_name = "mediacodec",
586 #if CONFIG_MPEG2_MEDIACODEC_DECODER
587 AVCodec ff_mpeg2_mediacodec_decoder = {
588 .name = "mpeg2_mediacodec",
589 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Android MediaCodec decoder"),
590 .type = AVMEDIA_TYPE_VIDEO,
591 .id = AV_CODEC_ID_MPEG2VIDEO,
592 .priv_data_size = sizeof(MediaCodecH264DecContext),
593 .init = mediacodec_decode_init,
594 .receive_frame = mediacodec_receive_frame,
595 .flush = mediacodec_decode_flush,
596 .close = mediacodec_decode_close,
597 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
598 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
599 .hw_configs = mediacodec_hw_configs,
600 .wrapper_name = "mediacodec",
604 #if CONFIG_MPEG4_MEDIACODEC_DECODER
605 AVCodec ff_mpeg4_mediacodec_decoder = {
606 .name = "mpeg4_mediacodec",
607 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
608 .type = AVMEDIA_TYPE_VIDEO,
609 .id = AV_CODEC_ID_MPEG4,
610 .priv_data_size = sizeof(MediaCodecH264DecContext),
611 .init = mediacodec_decode_init,
612 .receive_frame = mediacodec_receive_frame,
613 .flush = mediacodec_decode_flush,
614 .close = mediacodec_decode_close,
615 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
616 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
617 .hw_configs = mediacodec_hw_configs,
618 .wrapper_name = "mediacodec",
622 #if CONFIG_VP8_MEDIACODEC_DECODER
623 AVCodec ff_vp8_mediacodec_decoder = {
624 .name = "vp8_mediacodec",
625 .long_name = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
626 .type = AVMEDIA_TYPE_VIDEO,
627 .id = AV_CODEC_ID_VP8,
628 .priv_data_size = sizeof(MediaCodecH264DecContext),
629 .init = mediacodec_decode_init,
630 .receive_frame = mediacodec_receive_frame,
631 .flush = mediacodec_decode_flush,
632 .close = mediacodec_decode_close,
633 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
634 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
635 .hw_configs = mediacodec_hw_configs,
636 .wrapper_name = "mediacodec",
640 #if CONFIG_VP9_MEDIACODEC_DECODER
641 AVCodec ff_vp9_mediacodec_decoder = {
642 .name = "vp9_mediacodec",
643 .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
644 .type = AVMEDIA_TYPE_VIDEO,
645 .id = AV_CODEC_ID_VP9,
646 .priv_data_size = sizeof(MediaCodecH264DecContext),
647 .init = mediacodec_decode_init,
648 .receive_frame = mediacodec_receive_frame,
649 .flush = mediacodec_decode_flush,
650 .close = mediacodec_decode_close,
651 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
652 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
653 .hw_configs = mediacodec_hw_configs,
654 .wrapper_name = "mediacodec",