[FFmpeg-trac] #9349(avformat:new): avformat_find_stream_info with avio+mpeg may causes an abnormal increase in memory

FFmpeg trac at avcodec.org
Tue Jul 27 05:21:19 EEST 2021


#9349: avformat_find_stream_info with avio+mpeg may causes an abnormal increase in
memory
----------------------------------+--------------------------------------
             Reporter:  潘雨诗    |                     Type:  defect
               Status:  new       |                 Priority:  normal
            Component:  avformat  |                  Version:  git-master
             Keywords:            |               Blocked By:
             Blocking:            |  Reproduced by developer:  0
Analyzed by developer:  0         |
----------------------------------+--------------------------------------
 The startDemoLoop function is completely copied from
 ffmpeg/doc/examples/avio_reading.c. By calling it repeatedly, I found that
 the memory will increase slowly and endlessly. The audio data packets are
 some mpeg-encapsulated pcma and hevc data. The following is the code that
 can be easily executed And observe the memory at each loop.

 #include <libavutil/timestamp.h>
 #include <libavformat/avformat.h>
 #include <stdatomic.h>
 int fileIndex = 0;
 char *fileName = NULL;
 int bufSize = 0;
 void resetAll(){
     if (fileName == NULL) {
         fileName = malloc(1024);
     }
     fileIndex = 0;
 }
 int read_packet(void *opaque, uint8_t *buf, int buf_size) {
     sprintf(fileName, "/Users/panys/Documents/99/test/data%i",
 fileIndex++);
     FILE *fp = fopen((const char *) fileName, "r");
     if (!fp) {
         return AVERROR_EOF;
     }
     fseek(fp, 0, SEEK_END);
     bufSize = ftell(fp);
     rewind(fp);
     fread(buf, 1, bufSize, fp);
     fclose(fp);
     return bufSize;
 }
 int startDemoLoop(){
     AVFormatContext *fmt_ctx = NULL;
     AVIOContext *avio_ctx = NULL;
     uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;
     size_t buffer_size, avio_ctx_buffer_size = 4096;
     char *input_filename = NULL;
     int ret = 0;
     if (!(fmt_ctx = avformat_alloc_context())) {
         ret = AVERROR(ENOMEM);
         goto end;
     }

     avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);
     if (!avio_ctx_buffer) {
         ret = AVERROR(ENOMEM);
         goto end;
     }
     avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size,
                                   0, (void*)12345, &read_packet, NULL,
 NULL);
     if (!avio_ctx) {
         ret = AVERROR(ENOMEM);
         goto end;
     }
     fmt_ctx->pb = avio_ctx;

     ret = avformat_open_input(&fmt_ctx, NULL, NULL, NULL);
     if (ret < 0) {
         fprintf(stderr, "Could not open input\n");
         goto end;
     }

     ret = avformat_find_stream_info(fmt_ctx, NULL);
     if (ret < 0) {
         fprintf(stderr, "Could not find stream information\n");
         goto end;
     }

     av_dump_format(fmt_ctx, 0, input_filename, 0);

     end:
     avformat_close_input(&fmt_ctx);

     /* note: the internal buffer could have changed, and be !=
 avio_ctx_buffer */
     if (avio_ctx)
         av_freep(&avio_ctx->buffer);
     avio_context_free(&avio_ctx);
     if (ret < 0) {
         fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
         return 1;
     }
     return 0;
 }
 int main(int argc, char **argv) {
     while (1){
         resetAll();
         startDemoLoop();
     }
 //    av_log_set_level(-8);
 //    setvbuf(stdout, NULL, _IONBF, 0);
 //    startLoop();
 }
-- 
Ticket URL: <https://trac.ffmpeg.org/ticket/9349>
FFmpeg <https://ffmpeg.org>
FFmpeg issue tracker


More information about the FFmpeg-trac mailing list