Opened 3 years ago

Closed 3 years ago

#9349 closed defect (worksforme)

avformat_find_stream_info with avio+mpeg may causes an abnormal increase in memory

Reported by: 潘雨诗 Owned by:
Priority: normal Component: avformat
Version: git-master Keywords:
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

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();
}

Attachments (1)

test data.zip (295.7 KB ) - added by 潘雨诗 3 years ago.

Download all attachments as: .zip

Change History (2)

by 潘雨诗, 3 years ago

Attachment: test data.zip added

comment:1 by mkver, 3 years ago

Resolution: worksforme
Status: newclosed

Your code as-is doesn't work: A startLoop function is called but not defined. I presume this while (1) loop is supposed to be the function body of said startLoop? If so, then I cannot reproduce your findings at all. I can't detect any leak or incrementing memory usage. So I am closing this as worksforme, but feel free to reopen it if you can provide more info.

PS: Your read function is buggy: It might read more than buf_size.

Note: See TracTickets for help on using tickets.