OSDN Git Service

- patch a reference picture leak in ffmpeg/libavcodec/mpegvideo.c that caused aborts...
[handbrake-jp/handbrake-jp-git.git] / contrib / patch-ffmpeg-mpegleak.patch
1 Index: libavcodec/h264.c
2 ===================================================================
3 --- libavcodec/h264.c   (revision 14820)
4 +++ libavcodec/h264.c   (working copy)
5 @@ -3355,7 +3355,7 @@
6           * stream. Need to discard one frame. Prevents overrun of the
7           * short_ref and long_ref buffers.
8           */
9 -        av_log(h->s.avctx, AV_LOG_ERROR,
10 +        av_log(h->s.avctx, AV_LOG_DEBUG,
11                 "number of reference frames exceeds max (probably "
12                 "corrupt input), discarding one\n");
13  
14 @@ -7557,7 +7557,7 @@
15  
16      if(!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr){
17          if (avctx->skip_frame >= AVDISCARD_NONREF || s->hurry_up) return 0;
18 -        av_log(avctx, AV_LOG_ERROR, "no frame!\n");
19 +        av_log(avctx, AV_LOG_DEBUG, "no frame!\n");
20          return -1;
21      }
22  
23 Index: libavcodec/mpegvideo.c
24 ===================================================================
25 --- libavcodec/mpegvideo.c      (revision 14820)
26 +++ libavcodec/mpegvideo.c      (working copy)
27 @@ -772,6 +772,15 @@
28      }else{
29          for(i=0; i<MAX_PICTURE_COUNT; i++){
30              if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) return i; //FIXME
31 +            /* XXX there seems to be a leak caused by h264 in mpeg transport
32 +             * streams: Over-the-air streams have a lot of errors. A picture
33 +             * may be marked as referenced but the actual references get lost
34 +             * so it never gets released. We take care of that here by purging
35 +             * pictures that are impossibly old.
36 +             */
37 +            if (s->coded_picture_number - s->picture[i].coded_picture_number >
38 +                32*MAX_PICTURE_COUNT)
39 +                s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]);
40          }
41          for(i=0; i<MAX_PICTURE_COUNT; i++){
42              if(s->picture[i].data[0]==NULL) return i;
43 @@ -779,19 +788,15 @@
44      }
45  
46      av_log(s->avctx, AV_LOG_FATAL, "Internal error, picture buffer overflow\n");
47 -    /* We could return -1, but the codec would crash trying to draw into a
48 -     * non-existing frame anyway. This is safer than waiting for a random crash.
49 -     * Also the return of this is never useful, an encoder must only allocate
50 -     * as much as allowed in the specification. This has no relationship to how
51 -     * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
52 -     * enough for such valid streams).
53 -     * Plus, a decoder has to check stream validity and remove frames if too
54 -     * many reference frames are around. Waiting for "OOM" is not correct at
55 -     * all. Similarly, missing reference frames have to be replaced by
56 -     * interpolated/MC frames, anything else is a bug in the codec ...
57 -     */
58 -    abort();
59 -    return -1;
60 +    /* assume that we don't have a picture because of the leak described above.
61 +     * just release the oldest we have & reuse its slot. */
62 +    int oldest=0;
63 +    for(i=0; i<MAX_PICTURE_COUNT; i++){
64 +        if (s->picture[i].coded_picture_number < s->picture[oldest].coded_picture_number)
65 +            oldest = i;
66 +    }
67 +    s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[oldest]);
68 +    return oldest;
69  }
70  
71  static void update_noise_reduction(MpegEncContext *s){