transcode with ffmpeg may eat up a lot of memory

688 Views Asked by At

When I transcode this source file with ffmpeg,ffmpeg may eat up a lot of memory about dozens of GB till to be killed,Why?

 the report here:
 ffmpeg started on 2017-12-19 at 10:59:15
 Report written to "ffmpeg-20171219-105915.log"
 Command line:
 ffmpeg -i memory_error_fifo.mp4 -s 854x480 -vcodec libx264 -x264opts
 "keyint=50" -b:v 238k -vf "movie=namei.jpg [watermark]; [vf0] fifo
 [vf1];[vf1][watermark] overlay=main_w-overlay_w-0:0[vf2]" -acodec
 libfdk_aac -ar 44100 -movflags faststart -f mp4 output.mp4 -y -v trace
 -report
 ffmpeg version N-89095-gb3c1172 Copyright (c) 2000-2017 the FFmpeg

 How to reproduce:
 ffmpeg -i "memory_error_fifo.mp4"  -s 854x480  -vcodec libx264 -x264opts
 keyint=50  -b:v 238k -vf "movie=namei.jpg [watermark]; [vf0] fifo
 [vf1];[vf1][watermark] overlay=main_w-overlay_w-0:0[vf2]" -acodec
 libfdk_aac  -ar 44100 -movflags faststart -f mp4 output.mp4 -y -v trace
 -report

When i use -an to disable audio,the result is ok; when i not use "fifo",the result is ok,but my project needed this "fifo" filter.

 n3.5-dev-1292-gce001bb with master commit
 ce001bb8fc6677541c401a614e05e5058d58dde1
 built on linux

ffmpeg report:https://pan.baidu.com/s/1qYNvTes

attached source file: https://pan.baidu.com/s/1pLzbwbp
1

There are 1 best solutions below

0
On

Reason: The fifo-filter won't output frames until it is being requested. And ffmpeg only requests frames which belongs to the stream with a smallest timestamp (See the choose_output() function). So when the media has some abnormal contents, like a short piece of pure image without audio or some audio decoding errors, ffmpeg will remain requesting audio frames and leave thousands of video frames blocked in the fifo-filter eating up your memory.

Solution: I met the same problem and solved it by adding a limitation of samples in the fifo-filter. It will force the next linked filter to request frames when the number of buffered frames exceeds limitation. Code:

libavfilter/fifo.c:

+ #define NB_SAMPLE_MAX 500 // About 1GB memory

typedef struct Buf {
    AVFrame *frame;
    struct Buf        *next;
    AVFrame *out;
    int allocated_samples;      ///< number of samples out was allocated for
+   int buffered_samples;       ///< avoid memory overflow
} FifoContext;


static av_cold int init(AVFilterContext *ctx)
{
    FifoContext *fifo = ctx->priv;
    fifo->last = &fifo->root;
+   fifo->buffered_samples = 0;
    return 0;
}


static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
{
    FifoContext *fifo = inlink->dst->priv;
+   int i;  
    fifo->last->next = av_mallocz(sizeof(Buf));
    if (!fifo->last->next) {
        av_frame_free(&frame);
        return AVERROR(ENOMEM);
    }

    fifo->last = fifo->last->next;
    fifo->last->frame = frame;

+   fifo->buffered_samples++;
+   if (fifo->buffered_samples > NB_SAMPLE_MAX) {
+       av_log(NULL, AV_LOG_DEBUG, "Too many frames buffered in fifo.\n");
+       for (i = 0; i < inlink->dst->nb_outputs; i++) {
+           inlink->dst->outputs[i]->frame_wanted_out = 1; // it will force the next filter to request frames
+       }
+   }
    return 0;
}


static void queue_pop(FifoContext *s)
{
    Buf *tmp = s->root.next->next;
    if (s->last == s->root.next)
        s->last = &s->root;
    av_freep(&s->root.next);
    s->root.next = tmp;
+   if (s->buffered_samples > 0)
+       s->buffered_samples--;
}

I'm not sure if these changes will cause some other problems. If someone has a better solution, let me know please.