[FFmpeg-devel,1/3] lavc/libmp3lame: send encoder padding in side data of final packet

Submitted by Jon Toohill on Oct. 4, 2016, 12:45 a.m.

Details

Message ID 1475541908-21260-2-git-send-email-jtoohill@google.com
State New
Headers show

Commit Message

Jon Toohill Oct. 4, 2016, 12:45 a.m.
---
 libavcodec/libmp3lame.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
index 5642264..a1bf122 100644
--- a/libavcodec/libmp3lame.c
+++ b/libavcodec/libmp3lame.c
@@ -185,7 +185,7 @@  static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
 {
     LAMEContext *s = avctx->priv_data;
     MPADecodeHeader hdr;
-    int len, ret, ch;
+    int len, ret, ch, discard_padding;
     int lame_result;
     uint32_t h;
 
@@ -269,6 +269,25 @@  static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
                            &avpkt->duration);
 
+        discard_padding = avctx->frame_size - avpkt->duration;
+        // Check if subtraction resulted in an overflow
+        if ((discard_padding < avctx->frame_size) != (avpkt->duration > 0)) {
+            av_packet_unref(avpkt);
+            av_free(avpkt);
+            return AVERROR(EINVAL);
+        }
+        if (discard_padding > 0) {
+            uint8_t* side_data = av_packet_new_side_data(avpkt,
+                                                         AV_PKT_DATA_SKIP_SAMPLES,
+                                                         10);
+            if(!side_data) {
+                av_packet_unref(avpkt);
+                av_free(avpkt);
+                return AVERROR(ENOMEM);
+            }
+            AV_WL32(side_data + 4, discard_padding);
+        }
+
         avpkt->size = len;
         *got_packet_ptr = 1;
     }