[FFmpeg-cvslog] cbs: Add buffer padding when splitting fragments

Mark Thompson git at videolan.org
Sat Nov 11 19:07:51 EET 2017


ffmpeg | branch: master | Mark Thompson <sw at jkqxz.net> | Tue Sep 12 22:11:47 2017 +0100| [e7f64191b27bcf37cbf7006606f0f439c6cdc24f] | committer: Mark Thompson

cbs: Add buffer padding when splitting fragments

Remove any trailing zeroes from H.26[45] NAL units at the same time.

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=e7f64191b27bcf37cbf7006606f0f439c6cdc24f
---

 libavcodec/cbs_h2645.c | 11 +++++++++--
 libavcodec/cbs_mpeg2.c |  3 ++-
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index c8a13f1679..50a227da78 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -479,12 +479,19 @@ static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
 
     for (i = 0; i < packet->nb_nals; i++) {
         const H2645NAL *nal = &packet->nals[i];
+        size_t size = nal->size;
         uint8_t *data;
 
-        data = av_malloc(nal->size);
+        // Remove trailing zeroes.
+        while (size > 0 && nal->data[size - 1] == 0)
+            --size;
+        av_assert0(size > 0);
+
+        data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
         if (!data)
             return AVERROR(ENOMEM);
-        memcpy(data, nal->data, nal->size);
+        memcpy(data, nal->data, size);
+        memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
 
         err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
                                       data, nal->size);
diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c
index cbee42e905..3c09377df3 100644
--- a/libavcodec/cbs_mpeg2.c
+++ b/libavcodec/cbs_mpeg2.c
@@ -131,10 +131,11 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
             unit_size = (end - 4) - (start - 1);
         }
 
-        unit_data = av_malloc(unit_size);
+        unit_data = av_malloc(unit_size + AV_INPUT_BUFFER_PADDING_SIZE);
         if (!unit_data)
             return AVERROR(ENOMEM);
         memcpy(unit_data, start - 1, unit_size);
+        memset(unit_data + unit_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
 
         err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
                                       unit_data, unit_size);



More information about the ffmpeg-cvslog mailing list