OSDN Git Service

Fix potential crash in libbluray
[handbrake-jp/handbrake-jp-git.git] / libhb / decavcodec.c
index 91949fb..abf7ed1 100644 (file)
@@ -210,8 +210,6 @@ static int decavcodecInit( hb_work_object_t * w, hb_job_t * job )
         pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout, 
                                       w->audio->config.out.mixdown);
         hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, &hb_qt_chan_map );
-        if ( w->audio->config.out.codec == HB_ACODEC_LAME )
-            hb_downmix_adjust_level( pv->downmix );
     }
 
     return 0;
@@ -535,7 +533,12 @@ static int reget_frame_buf( AVCodecContext *context, AVFrame *frame )
 
 static void log_chapter( hb_work_private_t *pv, int chap_num, int64_t pts )
 {
-    hb_chapter_t *c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
+    hb_chapter_t *c;
+
+    if ( !pv->job )
+        return;
+
+    c = hb_list_item( pv->job->title->list_chapter, chap_num - 1 );
     if ( c && c->title )
     {
         hb_log( "%s: \"%s\" (%d) at frame %u time %"PRId64,
@@ -563,7 +566,20 @@ static void flushDelayQueue( hb_work_private_t *pv )
     }
 }
 
-static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
+/*
+ * Decodes a video frame from the specified raw packet data ('data', 'size', 'sequence').
+ * The output of this function is stored in 'pv->list', which contains a list
+ * of zero or more decoded packets.
+ * 
+ * The returned packets are guaranteed to have their timestamps in the correct order,
+ * even if the original packets decoded by libavcodec have misordered timestamps,
+ * due to the use of 'packed B-frames'.
+ * 
+ * Internally the set of decoded packets may be buffered in 'pv->delayq'
+ * until enough packets have been decoded so that the timestamps can be
+ * correctly rewritten, if this is necessary.
+ */
+static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size, int sequence )
 {
     int got_picture, oldlevel = 0;
     AVFrame frame;
@@ -636,6 +652,18 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
         {
             buf = copy_frame( pv, &frame );
             buf->start = pts;
+            buf->sequence = sequence;
+            if ( pv->new_chap && buf->start >= pv->chap_time )
+            {
+                buf->new_chap = pv->new_chap;
+                pv->new_chap = 0;
+                pv->chap_time = 0;
+                log_chapter( pv, buf->new_chap, buf->start );
+            }
+            else if ( pv->nframes == 0 && pv->job )
+            {
+                log_chapter( pv, pv->job->chapter_start, buf->start );
+            }
             hb_list_add( pv->list, buf );
             ++pv->nframes;
             return got_picture;
@@ -675,7 +703,7 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
                 pv->chap_time = 0;
                 log_chapter( pv, buf->new_chap, buf->start );
             }
-            else if ( pv->nframes == 0 )
+            else if ( pv->nframes == 0 && pv->job )
             {
                 log_chapter( pv, pv->job->chapter_start, buf->start );
             }
@@ -683,7 +711,9 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
         }
 
         // add the new frame to the delayq & push its timestamp on the heap
-        pv->delayq[slot] = copy_frame( pv, &frame );
+        buf = copy_frame( pv, &frame );
+        buf->sequence = sequence;
+        pv->delayq[slot] = buf;
         heap_push( &pv->pts_heap, pts );
 
         ++pv->nframes;
@@ -692,7 +722,7 @@ static int decodeFrame( hb_work_private_t *pv, uint8_t *data, int size )
     return got_picture;
 }
 
-static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size,
+static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size, int sequence,
                          int64_t pts, int64_t dts )
 {
     /*
@@ -712,20 +742,24 @@ static void decodeVideo( hb_work_private_t *pv, uint8_t *data, int size,
         if ( pout_len > 0 )
         {
             pv->pts = pv->parser->pts;
-            decodeFrame( pv, pout, pout_len );
+            decodeFrame( pv, pout, pout_len, sequence );
         }
     } while ( pos < size );
 
     /* the stuff above flushed the parser, now flush the decoder */
     if ( size <= 0 )
     {
-        while ( decodeFrame( pv, NULL, 0 ) )
+        while ( decodeFrame( pv, NULL, 0, sequence ) )
         {
         }
         flushDelayQueue( pv );
     }
 }
 
+/*
+ * Removes all packets from 'pv->list', links them together into
+ * a linked-list, and returns the first packet in the list.
+ */
 static hb_buffer_t *link_buf_list( hb_work_private_t *pv )
 {
     hb_buffer_t *head = hb_list_item( pv->list, 0 );
@@ -865,7 +899,7 @@ static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     /* if we got an empty buffer signaling end-of-stream send it downstream */
     if ( in->size == 0 )
     {
-        decodeVideo( pv, in->data, in->size, pts, dts );
+        decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
         hb_list_add( pv->list, in );
         *buf_out = link_buf_list( pv );
         return HB_WORK_DONE;
@@ -902,7 +936,7 @@ static int decavcodecvWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         pv->new_chap = in->new_chap;
         pv->chap_time = pts >= 0? pts : pv->pts_next;
     }
-    decodeVideo( pv, in->data, in->size, pts, dts );
+    decodeVideo( pv, in->data, in->size, in->sequence, pts, dts );
     hb_buffer_close( &in );
     *buf_out = link_buf_list( pv );
     return HB_WORK_OK;
@@ -943,10 +977,11 @@ static int decavcodecvInfo( hb_work_object_t *w, hb_work_info_t *info )
         if ( info->pixel_aspect_width == 0 ||
              info->pixel_aspect_height == 0 )
         {
+            // There will not be an ffmpeg stream if the file is TS
             AVStream *st = hb_ffmpeg_avstream( w->codec_param );
-            info->pixel_aspect_width = st->sample_aspect_ratio.num ?
-                                        st->sample_aspect_ratio.num : 1;
-            info->pixel_aspect_height = st->sample_aspect_ratio.den ?
+            info->pixel_aspect_width = st && st->sample_aspect_ratio.num ?
+                                       st->sample_aspect_ratio.num : 1;
+            info->pixel_aspect_height = st && st->sample_aspect_ratio.den ?
                                         st->sample_aspect_ratio.den : 1;
         }
         /* ffmpeg returns the Pixel Aspect Ratio (PAR). Handbrake wants the
@@ -1027,8 +1062,14 @@ static void init_ffmpeg_context( hb_work_object_t *w )
         // Because the time bases are so screwed up, we only take values
         // in the range 8fps - 64fps.
         AVRational tb;
-        if ( st->time_base.num * 64 > st->time_base.den &&
-             st->time_base.den > st->time_base.num * 8 )
+        if ( st->avg_frame_rate.den * 64 > st->avg_frame_rate.num &&
+             st->avg_frame_rate.num > st->avg_frame_rate.den * 8 )
+        {
+            tb.num = st->avg_frame_rate.den;
+            tb.den = st->avg_frame_rate.num;
+        }
+        else if ( st->time_base.num * 64 > st->time_base.den &&
+                  st->time_base.den > st->time_base.num * 8 )
         {
             tb = st->time_base;
         }
@@ -1095,8 +1136,6 @@ static int decavcodecviInit( hb_work_object_t * w, hb_job_t * job )
         pv->downmix = hb_downmix_init(w->audio->config.in.channel_layout, 
                                       w->audio->config.out.mixdown);
         hb_downmix_set_chan_map( pv->downmix, &hb_smpte_chan_map, &hb_qt_chan_map );
-        if ( w->audio->config.out.codec == HB_ACODEC_LAME )
-            hb_downmix_adjust_level( pv->downmix );
     }
 
     return 0;
@@ -1113,7 +1152,7 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     if ( in->size == 0 )
     {
         /* flush any frames left in the decoder */
-        while ( pv->context && decodeFrame( pv, NULL, 0 ) )
+        while ( pv->context && decodeFrame( pv, NULL, 0, in->sequence ) )
         {
         }
         flushDelayQueue( pv );
@@ -1144,7 +1183,7 @@ static int decavcodecviWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         pv->chap_time = pts >= 0? pts : pv->pts_next;
     }
     prepare_ffmpeg_buffer( in );
-    decodeFrame( pv, in->data, in->size );
+    decodeFrame( pv, in->data, in->size, in->sequence );
     hb_buffer_close( &in );
     *buf_out = link_buf_list( pv );
     return HB_WORK_OK;
@@ -1172,6 +1211,7 @@ static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *dat
 {
     AVCodecContext *context = pv->context;
     int pos = 0;
+    int loop_limit = 256;
 
     while ( pos < size )
     {
@@ -1190,10 +1230,18 @@ static void decodeAudio( hb_audio_t * audio, hb_work_private_t *pv, uint8_t *dat
         int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
         int nsamples;
         int len = avcodec_decode_audio3( context, buffer, &out_size, &avp );
-        if ( len <= 0 )
+        if ( len < 0 )
         {
             return;
         }
+        if ( len == 0 )
+        {
+            if ( !(loop_limit--) )
+                return;
+        }
+        else
+            loop_limit = 256;
+
         pos += len;
         if( out_size > 0 )
         {