]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mediacodecdec.c
vorbisenc: Stop tracking number of samples per frame
[ffmpeg] / libavcodec / mediacodecdec.c
index 6fd0db2fa52939dec86571ff8373313dcb8d2681..6962ce2474f52db7acd300944f89d16e7c4a13b9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Android MediaCodec H.264 / H.265 / MPEG-4 / VP8 / VP9 decoders
+ * Android MediaCodec MPEG-2 / H.264 / H.265 / MPEG-4 / VP8 / VP9 decoders
  *
  * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
  *
@@ -41,11 +41,9 @@ typedef struct MediaCodecH264DecContext {
 
     MediaCodecDecContext *ctx;
 
-    AVBSFContext *bsf;
-
     AVFifoBuffer *fifo;
 
-    AVPacket filtered_pkt;
+    AVPacket buffered_pkt;
 
 } MediaCodecH264DecContext;
 
@@ -58,8 +56,7 @@ static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
 
     av_fifo_free(s->fifo);
 
-    av_bsf_free(&s->bsf);
-    av_packet_unref(&s->filtered_pkt);
+    av_packet_unref(&s->buffered_pkt);
 
     return 0;
 }
@@ -185,6 +182,7 @@ static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
     int ret;
 
     HEVCParamSets ps;
+    HEVCSEIContext sei;
 
     const HEVCVPS *vps = NULL;
     const HEVCPPS *pps = NULL;
@@ -200,9 +198,10 @@ static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
     int pps_data_size = 0;
 
     memset(&ps, 0, sizeof(ps));
+    memset(&sei, 0, sizeof(sei));
 
     ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
-                                   &ps, &is_nalff, &nal_length_size, 0, 1, avctx);
+                                   &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
     if (ret < 0) {
         goto done;
     }
@@ -265,6 +264,19 @@ done:
 }
 #endif
 
+#if CONFIG_MPEG2_MEDIACODEC_DECODER
+static int mpeg2_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
+{
+    int ret = 0;
+
+    if (avctx->extradata) {
+        ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
+    }
+
+    return ret;
+}
+#endif
+
 #if CONFIG_MPEG4_MEDIACODEC_DECODER
 static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
 {
@@ -297,9 +309,6 @@ static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
 
     const char *codec_mime = NULL;
 
-    const char *bsf_name = NULL;
-    const AVBitStreamFilter *bsf = NULL;
-
     FFAMediaFormat *format = NULL;
     MediaCodecH264DecContext *s = avctx->priv_data;
 
@@ -314,7 +323,6 @@ static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
 #if CONFIG_H264_MEDIACODEC_DECODER
     case AV_CODEC_ID_H264:
         codec_mime = "video/avc";
-        bsf_name = "h264_mp4toannexb";
 
         ret = h264_set_extradata(avctx, format);
         if (ret < 0)
@@ -324,13 +332,21 @@ static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
 #if CONFIG_HEVC_MEDIACODEC_DECODER
     case AV_CODEC_ID_HEVC:
         codec_mime = "video/hevc";
-        bsf_name = "hevc_mp4toannexb";
 
         ret = hevc_set_extradata(avctx, format);
         if (ret < 0)
             goto done;
         break;
 #endif
+#if CONFIG_MPEG2_MEDIACODEC_DECODER
+    case AV_CODEC_ID_MPEG2VIDEO:
+        codec_mime = "video/mpeg2";
+
+        ret = mpeg2_set_extradata(avctx, format);
+        if (ret < 0)
+            goto done;
+        break;
+#endif
 #if CONFIG_MPEG4_MEDIACODEC_DECODER
     case AV_CODEC_ID_MPEG4:
         codec_mime = "video/mp4v-es",
@@ -386,25 +402,6 @@ static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
         goto done;
     }
 
-    if (bsf_name) {
-    bsf = av_bsf_get_by_name(bsf_name);
-    if(!bsf) {
-        ret = AVERROR_BSF_NOT_FOUND;
-        goto done;
-    }
-
-    if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
-        goto done;
-    }
-
-    if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
-        ((ret = av_bsf_init(s->bsf)) < 0)) {
-          goto done;
-    }
-    }
-
-    av_init_packet(&s->filtered_pkt);
-
 done:
     if (format) {
         ff_AMediaFormat_delete(format);
@@ -478,11 +475,9 @@ static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
 
     /* process buffered data */
     while (!*got_frame) {
-        /* prepare the input data -- convert to Annex B if needed */
-        if (s->filtered_pkt.size <= 0) {
-            AVPacket input_pkt = { 0 };
-
-            av_packet_unref(&s->filtered_pkt);
+        /* prepare the input data */
+        if (s->buffered_pkt.size <= 0) {
+            av_packet_unref(&s->buffered_pkt);
 
             /* no more data */
             if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
@@ -490,38 +485,17 @@ static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
                     ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
             }
 
-            av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
-
-            if (s->bsf) {
-            ret = av_bsf_send_packet(s->bsf, &input_pkt);
-            if (ret < 0) {
-                return ret;
-            }
-
-            ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
-            if (ret == AVERROR(EAGAIN)) {
-                goto done;
-            }
-            } else {
-                av_packet_move_ref(&s->filtered_pkt, &input_pkt);
-            }
-
-            /* {h264,hevc}_mp4toannexb are used here and do not require flushing */
-            av_assert0(ret != AVERROR_EOF);
-
-            if (ret < 0) {
-                return ret;
-            }
+            av_fifo_generic_read(s->fifo, &s->buffered_pkt, sizeof(s->buffered_pkt), NULL);
         }
 
-        ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
+        ret = mediacodec_process_data(avctx, frame, got_frame, &s->buffered_pkt);
         if (ret < 0)
             return ret;
 
-        s->filtered_pkt.size -= ret;
-        s->filtered_pkt.data += ret;
+        s->buffered_pkt.size -= ret;
+        s->buffered_pkt.data += ret;
     }
-done:
+
     return avpkt->size;
 }
 
@@ -536,7 +510,7 @@ static void mediacodec_decode_flush(AVCodecContext *avctx)
     }
     av_fifo_reset(s->fifo);
 
-    av_packet_unref(&s->filtered_pkt);
+    av_packet_unref(&s->buffered_pkt);
 
     ff_mediacodec_dec_flush(avctx, s->ctx);
 }
@@ -554,6 +528,7 @@ AVCodec ff_h264_mediacodec_decoder = {
     .close          = mediacodec_decode_close,
     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
+    .bsfs           = "h264_mp4toannexb",
 };
 #endif
 
@@ -570,6 +545,23 @@ AVCodec ff_hevc_mediacodec_decoder = {
     .close          = mediacodec_decode_close,
     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
+    .bsfs           = "hevc_mp4toannexb",
+};
+#endif
+
+#if CONFIG_MPEG2_MEDIACODEC_DECODER
+AVCodec ff_mpeg2_mediacodec_decoder = {
+    .name           = "mpeg2_mediacodec",
+    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-2 Android MediaCodec decoder"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_MPEG2VIDEO,
+    .priv_data_size = sizeof(MediaCodecH264DecContext),
+    .init           = mediacodec_decode_init,
+    .decode         = mediacodec_decode_frame,
+    .flush          = mediacodec_decode_flush,
+    .close          = mediacodec_decode_close,
+    .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
+    .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
 };
 #endif