]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mmaldec.c
Merge commit 'e94e651c762f90ac5fd2dc9bd3ba1336a77d5b5c'
[ffmpeg] / libavcodec / mmaldec.c
index a70c51b8e3336b20b61d3d5de0841b972c369870..ecac9246893253b1ed880d51bda13a60346b3ebd 100644 (file)
@@ -67,8 +67,6 @@ typedef struct MMALDecodeContext {
     AVClass *av_class;
     int extra_buffers;
 
-    AVBitStreamFilterContext *bsfc;
-
     MMAL_COMPONENT_T *decoder;
     MMAL_QUEUE_T *queue_decoded_frames;
     MMAL_POOL_T *pool_in;
@@ -86,6 +84,7 @@ typedef struct MMALDecodeContext {
     int64_t frames_output;
     int eos_received;
     int eos_sent;
+    int extradata_sent;
 } MMALDecodeContext;
 
 // Assume decoder is guaranteed to produce output after at least this many
@@ -165,7 +164,7 @@ static void ffmmal_stop_decoder(AVCodecContext *avctx)
     }
     ctx->waiting_buffers_tail = NULL;
 
-    ctx->frames_output = ctx->eos_received = ctx->eos_sent = ctx->packets_sent = 0;
+    ctx->frames_output = ctx->eos_received = ctx->eos_sent = ctx->packets_sent = ctx->extradata_sent = 0;
 }
 
 static av_cold int ffmmal_close_decoder(AVCodecContext *avctx)
@@ -181,9 +180,6 @@ static av_cold int ffmmal_close_decoder(AVCodecContext *avctx)
     mmal_pool_destroy(ctx->pool_in);
     ffmmal_poolref_unref(ctx->pool_out);
 
-    if (ctx->bsfc)
-        av_bitstream_filter_close(ctx->bsfc);
-
     mmal_vc_deinit();
 
     return 0;
@@ -356,23 +352,6 @@ static av_cold int ffmmal_init_decoder(AVCodecContext *avctx)
     format_in->es->video.par.den = avctx->sample_aspect_ratio.den;
     format_in->flags = MMAL_ES_FORMAT_FLAG_FRAMED;
 
-    if (avctx->codec->id == AV_CODEC_ID_H264 && avctx->extradata && avctx->extradata[0] == 1) {
-        uint8_t *dummy_p;
-        int dummy_int;
-        ctx->bsfc = av_bitstream_filter_init("h264_mp4toannexb");
-        if (!ctx->bsfc) {
-            av_log(avctx, AV_LOG_ERROR, "Cannot open the h264_mp4toannexb BSF!\n");
-            ret = AVERROR(ENOSYS);
-            goto fail;
-        }
-        av_bitstream_filter_filter(ctx->bsfc, avctx, "private_spspps_buf", &dummy_p, &dummy_int, NULL, 0, 0);
-    } else if (avctx->extradata_size) {
-        if ((status = mmal_format_extradata_alloc(format_in, avctx->extradata_size)))
-            goto fail;
-        format_in->extradata_size = avctx->extradata_size;
-        memcpy(format_in->extradata, avctx->extradata, format_in->extradata_size);
-    }
-
     if ((status = mmal_port_format_commit(decoder->input[0])))
         goto fail;
 
@@ -440,7 +419,9 @@ fail:
 // (due to us not reading/returning enough output buffers) and won't accept
 // new input. (This wouldn't be an issue if MMAL input buffers always were
 // complete frames - then the input buffer just would have to be big enough.)
-static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt)
+// If is_extradata is set, send it as MMAL_BUFFER_HEADER_FLAG_CONFIG.
+static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt,
+                             int is_extradata)
 {
     MMALDecodeContext *ctx = avctx->priv_data;
     AVBufferRef *buf = NULL;
@@ -450,31 +431,24 @@ static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt)
     int ret = 0;
 
     if (avpkt->size) {
-        if (ctx->bsfc) {
-            uint8_t *tmp_data;
-            int tmp_size;
-            if ((ret = av_bitstream_filter_filter(ctx->bsfc, avctx, "private_spspps_buf",
-                                                  &tmp_data, &tmp_size,
-                                                  avpkt->data, avpkt->size,
-                                                  avpkt->flags & AV_PKT_FLAG_KEY)) < 0)
-                goto done;
-            buf = av_buffer_create(tmp_data, tmp_size, NULL, NULL, 0);
+        if (avpkt->buf) {
+            buf = av_buffer_ref(avpkt->buf);
+            size = avpkt->size;
+            data = avpkt->data;
         } else {
-            if (avpkt->buf) {
-                buf = av_buffer_ref(avpkt->buf);
-            } else {
-                buf = av_buffer_alloc(avpkt->size);
-                if (buf)
-                    memcpy(buf->data, avpkt->data, avpkt->size);
+            buf = av_buffer_alloc(avpkt->size);
+            if (buf) {
+                memcpy(buf->data, avpkt->data, avpkt->size);
+                size = buf->size;
+                data = buf->data;
             }
         }
         if (!buf) {
             ret = AVERROR(ENOMEM);
             goto done;
         }
-        size = buf->size;
-        data = buf->data;
-        ctx->packets_sent++;
+        if (!is_extradata)
+            ctx->packets_sent++;
     } else {
         if (!ctx->packets_sent) {
             // Short-cut the flush logic to avoid upsetting MMAL.
@@ -496,6 +470,9 @@ static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt)
         buffer->data = data;
         buffer->length = FFMIN(size, ctx->decoder->input[0]->buffer_size);
 
+        if (is_extradata)
+            buffer->flags |= MMAL_BUFFER_HEADER_FLAG_CONFIG;
+
         if (data == start)
             buffer->flags |= MMAL_BUFFER_HEADER_FLAG_FRAME_START;
 
@@ -619,10 +596,8 @@ static int ffmal_copy_frame(AVCodecContext *avctx,  AVFrame *frame,
         }
     }
 
-    if (buffer->pts != MMAL_TIME_UNKNOWN) {
-        frame->pkt_pts = buffer->pts;
-        frame->pts = buffer->pts;
-    }
+    frame->pkt_pts = buffer->pts == MMAL_TIME_UNKNOWN ? AV_NOPTS_VALUE : buffer->pts;
+    frame->pkt_dts = AV_NOPTS_VALUE;
 
 done:
     return ret;
@@ -733,10 +708,21 @@ done:
 static int ffmmal_decode(AVCodecContext *avctx, void *data, int *got_frame,
                          AVPacket *avpkt)
 {
+    MMALDecodeContext *ctx = avctx->priv_data;
     AVFrame *frame = data;
     int ret = 0;
 
-    if ((ret = ffmmal_add_packet(avctx, avpkt)) < 0)
+    if (avctx->extradata_size && !ctx->extradata_sent) {
+        AVPacket pkt = {0};
+        av_init_packet(&pkt);
+        pkt.data = avctx->extradata;
+        pkt.size = avctx->extradata_size;
+        ctx->extradata_sent = 1;
+        if ((ret = ffmmal_add_packet(avctx, &pkt, 1)) < 0)
+            return ret;
+    }
+
+    if ((ret = ffmmal_add_packet(avctx, avpkt, 0)) < 0)
         return ret;
 
     if ((ret = ffmmal_fill_input_port(avctx)) < 0)