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;
261 ff_hevc_ps_uninit(&ps);
271 #if CONFIG_MPEG2_MEDIACODEC_DECODER
272 static int mpeg2_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
276 if (avctx->extradata) {
277 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
284 #if CONFIG_MPEG4_MEDIACODEC_DECODER
285 static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
289 if (avctx->extradata) {
290 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
297 #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
298 static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
302 if (avctx->extradata) {
303 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
310 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
314 const char *codec_mime = NULL;
316 FFAMediaFormat *format = NULL;
317 MediaCodecH264DecContext *s = avctx->priv_data;
319 format = ff_AMediaFormat_new();
321 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
322 ret = AVERROR_EXTERNAL;
326 switch (avctx->codec_id) {
327 #if CONFIG_H264_MEDIACODEC_DECODER
328 case AV_CODEC_ID_H264:
329 codec_mime = "video/avc";
331 ret = h264_set_extradata(avctx, format);
336 #if CONFIG_HEVC_MEDIACODEC_DECODER
337 case AV_CODEC_ID_HEVC:
338 codec_mime = "video/hevc";
340 ret = hevc_set_extradata(avctx, format);
345 #if CONFIG_MPEG2_MEDIACODEC_DECODER
346 case AV_CODEC_ID_MPEG2VIDEO:
347 codec_mime = "video/mpeg2";
349 ret = mpeg2_set_extradata(avctx, format);
354 #if CONFIG_MPEG4_MEDIACODEC_DECODER
355 case AV_CODEC_ID_MPEG4:
356 codec_mime = "video/mp4v-es",
358 ret = mpeg4_set_extradata(avctx, format);
363 #if CONFIG_VP8_MEDIACODEC_DECODER
364 case AV_CODEC_ID_VP8:
365 codec_mime = "video/x-vnd.on2.vp8";
367 ret = vpx_set_extradata(avctx, format);
372 #if CONFIG_VP9_MEDIACODEC_DECODER
373 case AV_CODEC_ID_VP9:
374 codec_mime = "video/x-vnd.on2.vp9";
376 ret = vpx_set_extradata(avctx, format);
385 ff_AMediaFormat_setString(format, "mime", codec_mime);
386 ff_AMediaFormat_setInt32(format, "width", avctx->width);
387 ff_AMediaFormat_setInt32(format, "height", avctx->height);
389 s->ctx = av_mallocz(sizeof(*s->ctx));
391 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
392 ret = AVERROR(ENOMEM);
396 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
401 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
403 s->fifo = av_fifo_alloc(sizeof(AVPacket));
405 ret = AVERROR(ENOMEM);
411 ff_AMediaFormat_delete(format);
415 mediacodec_decode_close(avctx);
421 static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
423 MediaCodecH264DecContext *s = avctx->priv_data;
427 AVPacket pkt = { 0 };
430 * MediaCodec.flush() discards both input and output buffers, thus we
431 * need to delay the call to this function until the user has released or
432 * renderered the frames he retains.
434 * After we have buffered an input packet, check if the codec is in the
435 * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
437 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
438 * the codec (because the user retains frames). The codec stays in the
441 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
442 * performed on the codec. The codec leaves the flushing state and can
443 * process again packets.
445 * ff_mediacodec_dec_flush returns a negative value if an error has
449 if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
450 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
451 return AVERROR(EAGAIN);
455 ret = ff_decode_get_packet(avctx, &pkt);
456 if (ret == AVERROR_EOF)
458 else if (ret == AVERROR(EAGAIN))
459 ; /* no input packet, but fallthrough to check for pending frames */
463 /* buffer the input packet */
465 if (av_fifo_space(s->fifo) < sizeof(pkt)) {
466 ret = av_fifo_realloc2(s->fifo,
467 av_fifo_size(s->fifo) + sizeof(pkt));
469 av_packet_unref(&pkt);
473 av_fifo_generic_write(s->fifo, &pkt, sizeof(pkt), NULL);
476 /* process buffered data */
478 /* prepare the input data */
479 if (s->buffered_pkt.size <= 0) {
480 av_packet_unref(&s->buffered_pkt);
483 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
484 AVPacket null_pkt = { 0 };
486 ret = ff_mediacodec_dec_decode(avctx, s->ctx, frame,
487 &got_frame, &null_pkt);
495 return AVERROR(EAGAIN);
498 av_fifo_generic_read(s->fifo, &s->buffered_pkt, sizeof(s->buffered_pkt), NULL);
501 ret = ff_mediacodec_dec_decode(avctx, s->ctx, frame, &got_frame, &s->buffered_pkt);
505 s->buffered_pkt.size -= ret;
506 s->buffered_pkt.data += ret;
512 static void mediacodec_decode_flush(AVCodecContext *avctx)
514 MediaCodecH264DecContext *s = avctx->priv_data;
516 while (av_fifo_size(s->fifo)) {
518 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
519 av_packet_unref(&pkt);
521 av_fifo_reset(s->fifo);
523 av_packet_unref(&s->buffered_pkt);
525 ff_mediacodec_dec_flush(avctx, s->ctx);
528 static const AVCodecHWConfigInternal *mediacodec_hw_configs[] = {
529 &(const AVCodecHWConfigInternal) {
531 .pix_fmt = AV_PIX_FMT_MEDIACODEC,
532 .methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
533 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
534 .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
541 #if CONFIG_H264_MEDIACODEC_DECODER
542 AVCodec ff_h264_mediacodec_decoder = {
543 .name = "h264_mediacodec",
544 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
545 .type = AVMEDIA_TYPE_VIDEO,
546 .id = AV_CODEC_ID_H264,
547 .priv_data_size = sizeof(MediaCodecH264DecContext),
548 .init = mediacodec_decode_init,
549 .receive_frame = mediacodec_receive_frame,
550 .flush = mediacodec_decode_flush,
551 .close = mediacodec_decode_close,
552 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
553 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
554 .bsfs = "h264_mp4toannexb",
555 .hw_configs = mediacodec_hw_configs,
556 .wrapper_name = "mediacodec",
560 #if CONFIG_HEVC_MEDIACODEC_DECODER
561 AVCodec ff_hevc_mediacodec_decoder = {
562 .name = "hevc_mediacodec",
563 .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
564 .type = AVMEDIA_TYPE_VIDEO,
565 .id = AV_CODEC_ID_HEVC,
566 .priv_data_size = sizeof(MediaCodecH264DecContext),
567 .init = mediacodec_decode_init,
568 .receive_frame = mediacodec_receive_frame,
569 .flush = mediacodec_decode_flush,
570 .close = mediacodec_decode_close,
571 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
572 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
573 .bsfs = "hevc_mp4toannexb",
574 .hw_configs = mediacodec_hw_configs,
575 .wrapper_name = "mediacodec",
579 #if CONFIG_MPEG2_MEDIACODEC_DECODER
580 AVCodec ff_mpeg2_mediacodec_decoder = {
581 .name = "mpeg2_mediacodec",
582 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Android MediaCodec decoder"),
583 .type = AVMEDIA_TYPE_VIDEO,
584 .id = AV_CODEC_ID_MPEG2VIDEO,
585 .priv_data_size = sizeof(MediaCodecH264DecContext),
586 .init = mediacodec_decode_init,
587 .receive_frame = mediacodec_receive_frame,
588 .flush = mediacodec_decode_flush,
589 .close = mediacodec_decode_close,
590 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
591 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
592 .hw_configs = mediacodec_hw_configs,
593 .wrapper_name = "mediacodec",
597 #if CONFIG_MPEG4_MEDIACODEC_DECODER
598 AVCodec ff_mpeg4_mediacodec_decoder = {
599 .name = "mpeg4_mediacodec",
600 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
601 .type = AVMEDIA_TYPE_VIDEO,
602 .id = AV_CODEC_ID_MPEG4,
603 .priv_data_size = sizeof(MediaCodecH264DecContext),
604 .init = mediacodec_decode_init,
605 .receive_frame = mediacodec_receive_frame,
606 .flush = mediacodec_decode_flush,
607 .close = mediacodec_decode_close,
608 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
609 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
610 .hw_configs = mediacodec_hw_configs,
611 .wrapper_name = "mediacodec",
615 #if CONFIG_VP8_MEDIACODEC_DECODER
616 AVCodec ff_vp8_mediacodec_decoder = {
617 .name = "vp8_mediacodec",
618 .long_name = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
619 .type = AVMEDIA_TYPE_VIDEO,
620 .id = AV_CODEC_ID_VP8,
621 .priv_data_size = sizeof(MediaCodecH264DecContext),
622 .init = mediacodec_decode_init,
623 .receive_frame = mediacodec_receive_frame,
624 .flush = mediacodec_decode_flush,
625 .close = mediacodec_decode_close,
626 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
627 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
628 .hw_configs = mediacodec_hw_configs,
629 .wrapper_name = "mediacodec",
633 #if CONFIG_VP9_MEDIACODEC_DECODER
634 AVCodec ff_vp9_mediacodec_decoder = {
635 .name = "vp9_mediacodec",
636 .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
637 .type = AVMEDIA_TYPE_VIDEO,
638 .id = AV_CODEC_ID_VP9,
639 .priv_data_size = sizeof(MediaCodecH264DecContext),
640 .init = mediacodec_decode_init,
641 .receive_frame = mediacodec_receive_frame,
642 .flush = mediacodec_decode_flush,
643 .close = mediacodec_decode_close,
644 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
645 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
646 .hw_configs = mediacodec_hw_configs,
647 .wrapper_name = "mediacodec",