[FFmpeg-devel] [PATCH 2/6] lavf: add API to apply a list of bsfs to a packet

Rodger Combs rodger.combs at gmail.com
Thu Oct 8 04:50:03 CEST 2015


---
 libavformat/avformat.h |  8 ++++++++
 libavformat/utils.c    | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+)

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index e2a27d4..5226b0a 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -2728,6 +2728,14 @@ int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st,
 
 int avformat_queue_attached_pictures(AVFormatContext *s);
 
+/**
+ * Apply a list of bitstream filters to a packet.
+ *
+ * @return  >=0 on success;
+ *          AVERROR code on failure
+ */
+int av_apply_bitstream_filters(AVFormatContext *s, AVPacket *pkt,
+                               AVBitStreamFilterContext *bsfc);
 
 /**
  * @}
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 689473e..d4f8f12 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -4596,3 +4596,51 @@ uint8_t *ff_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type,
     sd->size = size;
     return data;
 }
+
+int av_apply_bitstream_filters(AVFormatContext *s, AVPacket *pkt,
+                               AVBitStreamFilterContext *bsfc)
+{
+    int ret = 0;
+    AVStream *st = s->streams[pkt->stream_index];
+    while (bsfc) {
+        AVPacket new_pkt = *pkt;
+        int a = av_bitstream_filter_filter(bsfc, st->codec, NULL,
+                                           &new_pkt.data, &new_pkt.size,
+                                           pkt->data, pkt->size,
+                                           pkt->flags & AV_PKT_FLAG_KEY);
+        if(a == 0 && new_pkt.data != pkt->data) {
+            uint8_t *t = av_malloc(new_pkt.size + AV_INPUT_BUFFER_PADDING_SIZE); //the new should be a subset of the old so cannot overflow
+            if (t) {
+                memcpy(t, new_pkt.data, new_pkt.size);
+                memset(t + new_pkt.size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
+                new_pkt.data = t;
+                new_pkt.buf = NULL;
+                a = 1;
+            } else {
+                a = AVERROR(ENOMEM);
+            }
+        }
+        if (a > 0) {
+            new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
+                                           av_buffer_default_free, NULL, 0);
+            if (new_pkt.buf) {
+                pkt->side_data = NULL;
+                pkt->side_data_elems = 0;
+                av_free_packet(pkt);
+            } else {
+                a = AVERROR(ENOMEM);
+            }
+        }
+        if (a < 0) {
+            av_log(s, AV_LOG_ERROR, "Failed to open bitstream filter %s for stream %d with codec %s",
+                   bsfc->filter->name, pkt->stream_index,
+                   st->codec->codec ? st->codec->codec->name : "copy");
+            ret = a;
+            break;
+        }
+        *pkt = new_pkt;
+
+        bsfc = bsfc->next;
+    }
+    return ret;
+}
-- 
2.6.0



More information about the ffmpeg-devel mailing list