Joakim Plate <[email protected]> added the comment:

I've been able to repro something similar when using libavformat in xbmc. I can 
reproduce it on my sample file in 
ffplay only if I change it so that non selected stream are not 
AVDISCARD_ALL'ed. Ie:


********************************************

diff --git a/ffplay.c b/ffplay.c
index 60b65a4..6c5ae49 100644
--- a/ffplay.c
+++ b/ffplay.c
@@ -2365,7 +2365,7 @@ static void stream_component_close(VideoState *is, int 
stream_index)
         break;
     }
 
-    ic->streams[stream_index]->discard = AVDISCARD_ALL;
+    //ic->streams[stream_index]->discard = AVDISCARD_ALL;
     avcodec_close(avctx);
     switch(avctx->codec_type) {
     case AVMEDIA_TYPE_AUDIO:
@@ -2470,7 +2470,7 @@ static int decode_thread(void *arg)
     for(i = 0; i < ic->nb_streams; i++) {
         AVStream *st= ic->streams[i];
         AVCodecContext *avctx = st->codec;
-        ic->streams[i]->discard = AVDISCARD_ALL;
+        //ic->streams[i]->discard = AVDISCARD_ALL;
         if(avctx->codec_type >= (unsigned)AVMEDIA_TYPE_NB)
             continue;
         if(st_count[avctx->codec_type]++ != wanted_stream[avctx->codec_type] 
&& wanted_stream[avctx->codec_type] 
>= 0)



*********************************************


Reason for the above requirement is that the mpegts demuxer will discard deep 
inside it's code since quite 
recently. The above change should not create a memleak by itself nontheless


*************** VALGRIND (omit frame pointers) ******************

==12174== 
==12174== HEAP SUMMARY:
==12174==     in use at exit: 171,517,575 bytes in 2,201 blocks
==12174==   total heap usage: 27,221 allocs, 25,020 frees, 782,372,757 bytes 
allocated
==12174== 
==12174== 18 bytes in 2 blocks are definitely lost in loss record 24 of 171
==12174==    at 0x4024F20: malloc (vg_replace_malloc.c:236)
==12174==    by 0x44315B4: ??? (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x4430451: _XimEncodeLocalICAttr (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x4431845: _XimSetICValueData (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x442A4B1: _XimLocalCreateIC (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x440CB38: XCreateIC (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x40CB5D4: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12174==    by 0x40CC7D3: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12174==    by 0x40BAAAA: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.3)
==12174==    by 0x40908B3: SDL_InitSubSystem (in /usr/lib/libSDL-1.2.so.0.11.3)
==12174==    by 0x4090916: SDL_Init (in /usr/lib/libSDL-1.2.so.0.11.3)
==12174==    by 0x807B027: main (ffplay.c:3146)
==12174== 
==12174== 112 (8 direct, 104 indirect) bytes in 1 blocks are definitely lost in 
loss record 130 of 171
==12174==    at 0x4025016: realloc (vg_replace_malloc.c:525)
==12174==    by 0x4412B3E: ??? (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x4413E2D: ??? (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x4415101: ??? (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x4415864: _XlcCreateLC (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x4435BAA: _XlcDefaultLoader (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x441D813: _XOpenLC (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x441D982: _XlcCurrentLC (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x441DE21: XSetLocaleModifiers (in /usr/lib/libX11.so.6.3.0)
==12174==    by 0x40C228D: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12174==    by 0x40CB8FF: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12174==    by 0x40CC7D3: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12174== 
==12174== 584 bytes in 1 blocks are definitely lost in loss record 149 of 171
==12174==    at 0x4024106: memalign (vg_replace_malloc.c:581)
==12174==    by 0x4024163: posix_memalign (vg_replace_malloc.c:709)
==12174==    by 0x85AF120: av_malloc (mem.c:83)
==12174==    by 0x4B053EF: ???
==12174== 
==12174== 24,372,152 bytes in 119 blocks are definitely lost in loss record 170 
of 171
==12174==    at 0x4024106: memalign (vg_replace_malloc.c:581)
==12174==    by 0x4024163: posix_memalign (vg_replace_malloc.c:709)
==12174==    by 0x85AF120: av_malloc (mem.c:83)
==12174==    by 0x7FFFFFFF: ???
==12174== 
==12174== 147,052,144 bytes in 718 blocks are possibly lost in loss record 171 
of 171
==12174==    at 0x4024106: memalign (vg_replace_malloc.c:581)
==12174==    by 0x4024163: posix_memalign (vg_replace_malloc.c:709)
==12174==    by 0x85AF120: av_malloc (mem.c:83)
==12174==    by 0x7FFFFFFF: ???
==12174== 
==12174== LEAK SUMMARY:
==12174==    definitely lost: 24,372,762 bytes in 123 blocks
==12174==    indirectly lost: 104 bytes in 4 blocks
==12174==      possibly lost: 147,052,144 bytes in 718 blocks
==12174==    still reachable: 92,565 bytes in 1,356 blocks
==12174==         suppressed: 0 bytes in 0 blocks
==12174== Reachable blocks (those to which a pointer was found) are not shown.
==12174== To see them, rerun with: --leak-check=full --show-reachable=yes
==12174== 
==12174== For counts of detected and suppressed errors, rerun with: -v
==12174== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 86 from 11)

****************************************************************************

************* VALGRIND hacked config.mak to enable frame pointers **********

joa...@htpc:~/Sandbox/ffmpeg.git$ valgrind --tool=memcheck --leak-check=full 
./ffplay_g /home/joakim/brokenCut.ts
==12395== Memcheck, a memory error detector
==12395== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==12395== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for 
copyright info
==12395== Command: ./ffplay_g /home/joakim/brokenCut.ts
==12395== 
FFplay version git-svn-r25688, Copyright (c) 2003-2010 the FFmpeg developers
  built on Nov 27 2010 13:42:29 with gcc 4.4.3
  configuration: --enable-static --enable-muxers --disable-decoder=mpeg_xvmc 
--disable-devices --disable-shared --
enable-postproc --enable-gpl --enable-protocol=http --enable-pthreads 
--enable-runtime-cpudetect
  libavutil     50.32. 6 / 50.32. 6
  libavcore      0.12. 0 /  0.12. 0
  libavcodec    52.94. 3 / 52.94. 3
  libavformat   52.84. 0 / 52.84. 0
  libavdevice   52. 2. 2 / 52. 2. 2
  libavfilter    1.56. 0 /  1.56. 0
  libswscale     0.11. 0 /  0.11. 0
  libpostproc   51. 2. 0 / 51. 2. 0
[mpeg2video @ 0x4b23f90] mpeg_decode_postinit() failure
    Last message repeated 15 times
[mpegts @ 0x4b053f0] max_analyze_duration reached
[NULL @ 0x4b45860] start time is not set in av_estimate_timings_from_pts
Input #0, mpegts, from '/home/joakim/brokenCut.ts':
  Duration: 00:00:30.64, start: 40354.429344, bitrate: 5346 kb/s
  Program 17502 
    Stream #0.0[0x2ff]: Video: mpeg2video, yuv420p, 720x576 [PAR 16:15 DAR 
4:3], 15000 kb/s, 28.04 fps, 25 tbr, 90k 
tbn, 50 tbc
    Stream #0.1[0x300](deu): Audio: mp2, 48000 Hz, 2 channels, s16, 192 kb/s
  No Program
    Stream #0.2[0x666]: Data: [0][0][0][0] / 0x0000
[mpeg2video @ 0x4b23f90] warning: first frame is no keyframe   0B f=0/0   
40361.50 A-V:-24.402 s:36.0 aq=  599KB vq=  147KB sq=    0B f=0/1   =0/0   
==12395== 
==12395== HEAP SUMMARY:
==12395==     in use at exit: 203,467,623 bytes in 2,357 blocks
==12395==   total heap usage: 29,378 allocs, 27,021 frees, 941,896,838 bytes 
allocated
==12395== 
==12395== 18 bytes in 2 blocks are definitely lost in loss record 24 of 176
==12395==    at 0x4024F20: malloc (vg_replace_malloc.c:236)
==12395==    by 0x44315B4: ??? (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x4430451: _XimEncodeLocalICAttr (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x4431845: _XimSetICValueData (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x442A4B1: _XimLocalCreateIC (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x440CB38: XCreateIC (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x40CB5D4: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395==    by 0x40CC7D3: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395==    by 0x40BAAAA: SDL_VideoInit (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395==    by 0x40908B3: SDL_InitSubSystem (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395==    by 0x4090916: SDL_Init (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395==    by 0x807B027: main (ffplay.c:3146)
==12395== 
==12395== 112 (8 direct, 104 indirect) bytes in 1 blocks are definitely lost in 
loss record 130 of 176
==12395==    at 0x4025016: realloc (vg_replace_malloc.c:525)
==12395==    by 0x4412B3E: ??? (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x4413E2D: ??? (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x4415101: ??? (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x4415864: _XlcCreateLC (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x4435BAA: _XlcDefaultLoader (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x441D813: _XOpenLC (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x441D982: _XlcCurrentLC (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x441DE21: XSetLocaleModifiers (in /usr/lib/libX11.so.6.3.0)
==12395==    by 0x40C228D: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395==    by 0x40CB8FF: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395==    by 0x40CC7D3: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395== 
==12395== 204,808 bytes in 1 blocks are definitely lost in loss record 169 of 
176
==12395==    at 0x4024106: memalign (vg_replace_malloc.c:581)
==12395==    by 0x4024163: posix_memalign (vg_replace_malloc.c:709)
==12395==    by 0x85AEBC0: av_malloc (mem.c:83)
==12395==    by 0x80DBBD4: handle_packet (mpegts.c:1219)
==12395==    by 0x80DE310: mpegts_read_packet (mpegts.c:1293)
==12395==    by 0x811EB6E: av_read_packet (utils.c:686)
==12395==    by 0x8124043: av_find_stream_info (utils.c:1929)
==12395==    by 0x807A30F: decode_thread (ffplay.c:2443)
==12395==    by 0x40E425C: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395==    by 0x411F96D: start_thread (pthread_create.c:300)
==12395==    by 0x4200A4D: clone (clone.S:130)
==12395== 
==12395== 410,200 (584 direct, 409,616 indirect) bytes in 1 blocks are 
definitely lost in loss record 171 of 176
==12395==    at 0x4024106: memalign (vg_replace_malloc.c:581)
==12395==    by 0x4024163: posix_memalign (vg_replace_malloc.c:709)
==12395==    by 0x85AEBC0: av_malloc (mem.c:83)
==12395==    by 0x4B053EF: ???
==12395== 
==12395== 2,457,696 bytes in 12 blocks are possibly lost in loss record 172 of 
176
==12395==    at 0x4024106: memalign (vg_replace_malloc.c:581)
==12395==    by 0x4024163: posix_memalign (vg_replace_malloc.c:709)
==12395==    by 0x85AEBC0: av_malloc (mem.c:83)
==12395==    by 0x80DBBD4: handle_packet (mpegts.c:1219)
==12395==    by 0x80DE310: mpegts_read_packet (mpegts.c:1293)
==12395==    by 0x811EB6E: av_read_packet (utils.c:686)
==12395==    by 0x8124043: av_find_stream_info (utils.c:1929)
==12395==    by 0x807A30F: decode_thread (ffplay.c:2443)
==12395==    by 0x40E425C: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395==    by 0x411F96D: start_thread (pthread_create.c:300)
==12395==    by 0x4200A4D: clone (clone.S:130)
==12395== 
==12395== 3,891,352 bytes in 19 blocks are definitely lost in loss record 173 
of 176
==12395==    at 0x4024106: memalign (vg_replace_malloc.c:581)
==12395==    by 0x4024163: posix_memalign (vg_replace_malloc.c:709)
==12395==    by 0x85AEBC0: av_malloc (mem.c:83)
==12395==    by 0x80DBBD4: handle_packet (mpegts.c:1219)
==12395==    by 0x80DE310: mpegts_read_packet (mpegts.c:1293)
==12395==    by 0x811EB6E: av_read_packet (utils.c:686)
==12395==    by 0x8121222: av_read_frame_internal (utils.c:1126)
==12395==    by 0x8122599: av_find_stream_info (utils.c:2265)
==12395==    by 0x807A30F: decode_thread (ffplay.c:2443)
==12395==    by 0x40E425C: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395==    by 0x411F96D: start_thread (pthread_create.c:300)
==12395==    by 0x4200A4D: clone (clone.S:130)
==12395== 
==12395== 26,010,616 bytes in 127 blocks are possibly lost in loss record 174 
of 176
==12395==    at 0x4024106: memalign (vg_replace_malloc.c:581)
==12395==    by 0x4024163: posix_memalign (vg_replace_malloc.c:709)
==12395==    by 0x85AEBC0: av_malloc (mem.c:83)
==12395==    by 0x80DBBD4: handle_packet (mpegts.c:1219)
==12395==    by 0x80DE310: mpegts_read_packet (mpegts.c:1293)
==12395==    by 0x811EB6E: av_read_packet (utils.c:686)
==12395==    by 0x8121222: av_read_frame_internal (utils.c:1126)
==12395==    by 0x8122599: av_find_stream_info (utils.c:2265)
==12395==    by 0x807A30F: decode_thread (ffplay.c:2443)
==12395==    by 0x40E425C: ??? (in /usr/lib/libSDL-1.2.so.0.11.3)
==12395==    by 0x411F96D: start_thread (pthread_create.c:300)
==12395==    by 0x4200A4D: clone (clone.S:130)
==12395== 
==12395== 26,010,616 bytes in 127 blocks are definitely lost in loss record 175 
of 176
==12395==    at 0x4024106: memalign (vg_replace_malloc.c:581)
==12395==    by 0x4024163: posix_memalign (vg_replace_malloc.c:709)
==12395==    by 0x85AEBC0: av_malloc (mem.c:83)
==12395==    by 0x80DBBD4: handle_packet (mpegts.c:1219)
==12395==    by 0x80DE310: mpegts_read_packet (mpegts.c:1293)
==12395==    by 0x811EB6E: av_read_packet (utils.c:686)
==12395==    by 0x8121222: av_read_frame_internal (utils.c:1126)
==12395==    by 0x807AAD9: decode_thread (ffplay.c:2600)
==12395==    by 0x4B053EF: ???
==12395== 
==12395== 144,389,640 bytes in 705 blocks are possibly lost in loss record 176 
of 176
==12395==    at 0x4024106: memalign (vg_replace_malloc.c:581)
==12395==    by 0x4024163: posix_memalign (vg_replace_malloc.c:709)
==12395==    by 0x85AEBC0: av_malloc (mem.c:83)
==12395==    by 0x80DBBD4: handle_packet (mpegts.c:1219)
==12395==    by 0x80DE310: mpegts_read_packet (mpegts.c:1293)
==12395==    by 0x811EB6E: av_read_packet (utils.c:686)
==12395==    by 0x8121222: av_read_frame_internal (utils.c:1126)
==12395==    by 0x807AAD9: decode_thread (ffplay.c:2600)
==12395==    by 0x4B053EF: ???
==12395== 
==12395== LEAK SUMMARY:
==12395==    definitely lost: 30,107,386 bytes in 151 blocks
==12395==    indirectly lost: 409,720 bytes in 6 blocks
==12395==      possibly lost: 172,857,952 bytes in 844 blocks
==12395==    still reachable: 92,565 bytes in 1,356 blocks
==12395==         suppressed: 0 bytes in 0 blocks
==12395== Reachable blocks (those to which a pointer was found) are not shown.
==12395== To see them, rerun with: --leak-check=full --show-reachable=yes
==12395== 
==12395== For counts of detected and suppressed errors, rerun with: -v
==12395== ERROR SUMMARY: 9 errors from 9 contexts (suppressed: 86 from 11)

*************************************************************************


The "reason" for the leak is that mpegts.c doesn't check if it has already 
allocated an output packet before 
writing a new one to the pkt buffer. I've hacked up this solution in xbmc to 
atleast avoid the memleak.

*************************************************************************


Modified: trunk/xbmc/cores/dvdplayer/Codecs/ffmpeg/libavformat/mpegts.c
===================================================================
--- trunk/xbmc/cores/dvdplayer/Codecs/ffmpeg/libavformat/mpegts.c       
2010-11-26 20:56:23 UTC (rev 35474)
+++ trunk/xbmc/cores/dvdplayer/Codecs/ffmpeg/libavformat/mpegts.c       
2010-11-26 20:56:48 UTC (rev 35475)
@@ -625,6 +625,10 @@
 
 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
 {
+    if(pkt->data) {
+      av_log(pes->stream, AV_LOG_ERROR, "ignoring previously allocated packet 
on stream %d\n", pkt->stream_index);
+      av_free_packet(pkt);
+    }
     av_init_packet(pkt);
 
     pkt->destruct = av_destruct_packet;
@@ -1572,6 +1576,8 @@
     }
 
     ts->pkt = pkt;
+    ts->pkt->data = NULL;
+
     ret = handle_packets(ts, 0);
     if (ret < 0) {
         /* flush pes data left */


*****************************************************************************


The above code doesn't really fix the issue with the files, it just avoids 
ffmpeg leaking memory on them (at the 
expense of log spew).

________________________________________________
FFmpeg issue tracker <[email protected]>
<https://roundup.ffmpeg.org/issue899>
________________________________________________

Reply via email to