[FFmpeg-cvslog] avcodec/mlpenc: improve handling of last samples

Paul B Mahol git at videolan.org
Tue Apr 12 20:30:23 EEST 2022


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Tue Apr 12 17:57:58 2022 +0200| [cbad385f040959fca32568e9ba29e6f05ddd8730] | committer: Paul B Mahol

avcodec/mlpenc: improve handling of last samples

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

 libavcodec/mlpenc.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/libavcodec/mlpenc.c b/libavcodec/mlpenc.c
index da36818169..25adf6f998 100644
--- a/libavcodec/mlpenc.c
+++ b/libavcodec/mlpenc.c
@@ -1165,6 +1165,7 @@ static int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf,
  *  lossless_check_data that will be written to the restart header.
  */
 static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples,
+                                int nb_samples,
                                 int is24)
 {
     int32_t *lossless_check_data = ctx->lossless_check_data;
@@ -1177,7 +1178,7 @@ static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples,
 
     lossless_check_data += ctx->frame_index;
 
-    for (int i = 0; i < ctx->avctx->frame_size; i++) {
+    for (int i = 0; i < nb_samples; i++) {
         for (unsigned int channel = 0; channel <= rh->max_channel; channel++) {
             uint32_t abs_sample;
             int32_t sample;
@@ -1202,12 +1203,12 @@ static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples,
 }
 
 /** Wrapper function for inputting data in two different bit-depths. */
-static void input_data(MLPEncodeContext *ctx, void *samples)
+static void input_data(MLPEncodeContext *ctx, void *samples, int nb_samples)
 {
     if (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
-        input_data_internal(ctx, samples, 1);
+        input_data_internal(ctx, samples, nb_samples, 1);
     else
-        input_data_internal(ctx, samples, 0);
+        input_data_internal(ctx, samples, nb_samples, 0);
 }
 
 static void input_to_sample_buffer(MLPEncodeContext *ctx)
@@ -2074,6 +2075,9 @@ static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
     int restart_frame, ret;
     uint8_t *data;
 
+    if (!frame && !ctx->last_frames)
+        ctx->last_frames = (ctx->afq.remaining_samples + avctx->frame_size - 1) / avctx->frame_size;
+
     if (!frame && !ctx->last_frames--)
         return 0;
 
@@ -2084,7 +2088,6 @@ static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
         /* add current frame to queue */
         if ((ret = ff_af_queue_add(&ctx->afq, frame)) < 0)
             return ret;
-        ctx->last_frames = ctx->max_restart_interval;
     }
 
     data = frame ? frame->data[0] : NULL;
@@ -2128,7 +2131,7 @@ input_and_return:
     ctx->next_major_frame_size += avctx->frame_size;
     ctx->next_major_number_of_frames++;
     if (data)
-        input_data(ctx, data);
+        input_data(ctx, data, frame->nb_samples);
 
     restart_frame = (ctx->frame_index + 1) % ctx->min_restart_interval;
 
@@ -2171,7 +2174,9 @@ input_and_return:
         avctx->frame_number++;
 
     if (bytes_written > 0) {
-        ff_af_queue_remove(&ctx->afq, avctx->frame_size, &avpkt->pts,
+        ff_af_queue_remove(&ctx->afq,
+                           FFMIN(avctx->frame_size, ctx->afq.remaining_samples),
+                           &avpkt->pts,
                            &avpkt->duration);
 
         av_shrink_packet(avpkt, bytes_written);



More information about the ffmpeg-cvslog mailing list