OSDN Git Service

Don't crash during scan when source not recognized (bug found & fixed by John Stebbins)
[handbrake-jp/handbrake-jp-git.git] / libhb / reader.c
index db1c7af..9ad2867 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id: reader.c,v 1.21 2005/11/25 15:05:25 titer Exp $
 
    This file is part of the HandBrake source code.
-   Homepage: <http://handbrake.m0k.org/>.
+   Homepage: <http://handbrake.fr/>.
    It may be used under the terms of the GNU General Public License. */
 
 #include "hb.h"
@@ -16,12 +16,9 @@ typedef struct
     hb_buffer_t  * ps;
     hb_stream_t  * stream;
 
+    hb_psdemux_t   demux;
     uint           sequence;
     int            saw_video;
-    int64_t        scr_offset;
-    int64_t        last_scr;
-    int            scr_changes;
-
 } hb_reader_t;
 
 /***********************************************************************
@@ -50,6 +47,19 @@ hb_thread_t * hb_reader_init( hb_job_t * job )
                            HB_NORMAL_PRIORITY );
 }
 
+static void push_buf( hb_reader_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
+{
+    while( !*r->die && !r->job->done && hb_fifo_is_full( fifo ) )
+    {
+        /*
+         * Loop until the incoming fifo is reaqdy to receive
+         * this buffer.
+         */
+        hb_snooze( 50 );
+    }
+    hb_fifo_push( fifo, buf );
+}
+
 /***********************************************************************
  * ReaderFunc
  ***********************************************************************
@@ -60,7 +70,6 @@ static void ReaderFunc( void * _r )
     hb_reader_t  * r = _r;
     hb_fifo_t   ** fifos;
     hb_buffer_t  * buf;
-    hb_buffer_t  * buf_old;
     hb_list_t    * list;
     int            n;
     int            chapter = -1;
@@ -68,7 +77,7 @@ static void ReaderFunc( void * _r )
 
     if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
     {
-        if ( !( r->stream = hb_stream_open( r->title->dvd, 1 ) ) )
+        if ( !( r->stream = hb_stream_open( r->title->dvd, r->title ) ) )
         {
           return;
         }
@@ -161,7 +170,14 @@ static void ReaderFunc( void * _r )
             hb_set_state( r->job->h, &state );
         }
 
-        hb_demux_ps( r->ps, list );
+        if ( r->title->demuxer == HB_NULL_DEMUXER )
+        {
+            hb_demux_null( r->ps, list, &r->demux );
+        }
+        else
+        {
+            hb_demux_ps( r->ps, list, &r->demux );
+        }
 
         while( ( buf = hb_list_item( list, 0 ) ) )
         {
@@ -172,13 +188,12 @@ static void ReaderFunc( void * _r )
             {
                 /* The first video packet defines 'time zero' so discard
                    data until we get a video packet with a PTS */
-                if ( buf->id == 0xE0 && buf->start != -1 )
+                if ( buf->id == r->title->video_id && buf->start != -1 )
                 {
                     r->saw_video = 1;
-                    r->scr_offset = buf->start;
-                    r->last_scr = buf->stop;
+                    r->demux.scr_offset = buf->renderOffset;
                     hb_log( "reader: first SCR %llu scr_offset %llu",
-                            r->last_scr, r->scr_offset );
+                            r->demux.last_scr, r->demux.scr_offset );
                 }
                 else
                 {
@@ -187,80 +202,28 @@ static void ReaderFunc( void * _r )
             }
             if( fifos )
             {
-                /*
-                 * This section of code implements the timing model of
-                 * the "Standard Target Decoder" (STD) of the MPEG2 standard
-                 * (specified in ISO 13818-1 sections 2.4.2, 2.5.2 & Annex D).
-                 * The STD removes and corrects for clock discontinuities so
-                 * that the time stamps on the video, audio & other media
-                 * streams can be used for cross-media synchronization. To do
-                 * this the STD has its own timestamp value, the System Clock
-                 * Reference or SCR, in the PACK header. Clock discontinuities
-                 * are detected using the SCR & and the adjustment needed
-                 * to correct post-discontinuity timestamps to be contiguous
-                 * with pre-discontinuity timestamps is computed from pre- and
-                 * post-discontinuity values of the SCR. Then this adjustment
-                 * is applied to every media timestamp (PTS).
-                 *
-                 * hb_demux_ps left the SCR for this pack in buf->stop.
-                 * ISO 13818-1 says there must be an SCR at least every 700ms
-                 * (100ms for Transport Streams) so if the difference between
-                 * this SCR & the previous is >700ms it's a discontinuity.
-                 * If the difference is negative it's non-physical (time doesn't
-                 * go backward) and must also be a discontinuity. When we find a
-                 * discontinuity we adjust the scr_offset so that the SCR of the
-                 * new packet lines up with that of the previous packet.
-                 */
-                int64_t scr_delta = buf->stop - r->last_scr;
-                if ( scr_delta > (90*700) || scr_delta < -90 )
-                {
-                    ++r->scr_changes;
-                    r->scr_offset += scr_delta - 1;
-                }
-                r->last_scr = buf->stop;
-                buf->stop = -1;
-
-                /*
-                 * The last section detected discontinuites and computed the
-                 * appropriate correction to remove them. The next couple of
-                 * lines apply the correction to the media timestamps so the
-                 * code downstream of us sees only timestamps relative to the
-                 * same, continuous clock with time zero on that clock being
-                 * the time of the first video packet.
-                 */
                 if ( buf->start != -1 )
                 {
                     /* this packet has a PTS - correct it for the initial
-                       video time offset & any timing discontinuities. */
-                    buf->start -= r->scr_offset;
+                       video time offset & any timing discontinuities so that
+                       everything after this sees a continuous clock with 0
+                       being the time of the first video packet. */
+                    buf->start -= r->demux.scr_offset;
+                    buf->renderOffset -= r->demux.scr_offset;
                 }
                 buf->sequence = r->sequence++;
-                for( n = 0; fifos[n] != NULL; n++)
+                /* if there are mutiple output fifos, send a copy of the
+                 * buffer down all but the first (we have to not ship the
+                 * original buffer or we'll race with the thread that's
+                 * consuming the buffer & inject garbage into the data stream). */
+                for( n = 1; fifos[n] != NULL; n++)
                 {
-                    if( n != 0 )
-                    {
-                        /*
-                         * Replace the buffer with a new copy of itself for when
-                         * it is being sent down multiple fifos.
-                         */
-                        buf_old = buf;
-                        buf = hb_buffer_init(buf_old->size);
-                        memcpy( buf->data, buf_old->data, buf->size );
-                        hb_buffer_copy_settings( buf, buf_old );
-                    }
-
-                    while( !*r->die && !r->job->done &&
-                           hb_fifo_is_full( fifos[n] ) )
-                    {
-                        /*
-                         * Loop until the incoming fifo is reaqdy to receive
-                         * this buffer.
-                         */
-                        hb_snooze( 50 );
-                    }
-
-                    hb_fifo_push( fifos[n], buf );
+                    hb_buffer_t *buf_copy = hb_buffer_init( buf->size );
+                    hb_buffer_copy_settings( buf_copy, buf );
+                    memcpy( buf_copy->data, buf->data, buf->size );
+                    push_buf( r, fifos[n], buf_copy );
                 }
+                push_buf( r, fifos[0], buf );
             }
             else
             {
@@ -269,6 +232,9 @@ static void ReaderFunc( void * _r )
         }
     }
 
+    /* send an empty buffer upstream to signal we're done */
+    hb_fifo_push( r->job->fifo_mpeg2, hb_buffer_init(0) );
+
     hb_list_empty( &list );
     hb_buffer_close( &r->ps );
     if (r->dvd)
@@ -281,7 +247,7 @@ static void ReaderFunc( void * _r )
       hb_stream_close(&r->stream);
     }
 
-    hb_log( "reader: done. %d scr changes", r->scr_changes );
+    hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
 
     free( r );
     _r = NULL;
@@ -302,7 +268,7 @@ static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
 
     memset(fifos, 0, sizeof(fifos));
 
-    if( id == 0xE0 )
+    if( id == title->video_id )
     {
         if( job->indepth_scan )
         {
@@ -358,7 +324,7 @@ static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
             audio = hb_list_item( title->list_audio, i );
             if( id == audio->id )
             {
-                fifos[n++] = audio->fifo_in;
+                fifos[n++] = audio->priv.fifo_in;
             }
         }