diff mbox

[FFmpeg-devel,1/2] lavc/libmp3lame: send encoder delay/padding in packet side data

Message ID 1475696257-12390-2-git-send-email-jtoohill@google.com
State Superseded
Headers show

Commit Message

Jon Toohill Oct. 5, 2016, 7:37 p.m. UTC
---
 libavcodec/libmp3lame.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

Comments

Andreas Cadhalpun Oct. 15, 2016, 9:54 a.m. UTC | #1
On 05.10.2016 21:37, Jon Toohill wrote:
> ---
>  libavcodec/libmp3lame.c | 26 +++++++++++++++++++++++++-
>  1 file changed, 25 insertions(+), 1 deletion(-)
> 
> diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
> index 5642264..b3ba0d8 100644
> --- a/libavcodec/libmp3lame.c
> +++ b/libavcodec/libmp3lame.c
[...]
> @@ -269,6 +270,29 @@ 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);

It would be nice to have an av_log here reporting the problem.
Otherwise patch LGTM.

Best regards,
Andreas
diff mbox

Patch

diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c
index 5642264..b3ba0d8 100644
--- a/libavcodec/libmp3lame.c
+++ b/libavcodec/libmp3lame.c
@@ -50,6 +50,7 @@  typedef struct LAMEContext {
     int reservoir;
     int joint_stereo;
     int abr;
+    int delay_sent;
     float *samples_flt[2];
     AudioFrameQueue afq;
     AVFloatDSPContext *fdsp;
@@ -185,7 +186,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 +270,29 @@  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 ((!s->delay_sent && avctx->initial_padding > 0) || 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);
+            }
+            if (!s->delay_sent) {
+                AV_WL32(side_data, avctx->initial_padding);
+                s->delay_sent = 1;
+            }
+            AV_WL32(side_data + 4, discard_padding);
+        }
+
         avpkt->size = len;
         *got_packet_ptr = 1;
     }