OSDN Git Service

Don't drop subtitles when crossing PTS discontinuities by using buffer sequence numbe...
[handbrake-jp/handbrake-jp-git.git] / libhb / reader.c
1 /* $Id: reader.c,v 1.21 2005/11/25 15:05:25 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "hb.h"
8
9 typedef struct
10 {
11     hb_job_t     * job;
12     hb_title_t   * title;
13     volatile int * die;
14
15     hb_dvd_t     * dvd;
16     hb_buffer_t  * ps;
17     hb_stream_t  * stream;
18     
19     uint           sequence;
20
21 } hb_reader_t;
22
23 /***********************************************************************
24  * Local prototypes
25  **********************************************************************/
26 static void        ReaderFunc( void * );
27 static hb_fifo_t * GetFifoForId( hb_job_t * job, int id );
28
29 /***********************************************************************
30  * hb_reader_init
31  ***********************************************************************
32  *
33  **********************************************************************/
34 hb_thread_t * hb_reader_init( hb_job_t * job )
35 {
36     hb_reader_t * r;
37
38     r = calloc( sizeof( hb_reader_t ), 1 );
39
40     r->job   = job;
41     r->title = job->title;
42     r->die   = job->die;
43     r->sequence = 0;
44     
45     return hb_thread_init( "reader", ReaderFunc, r,
46                            HB_NORMAL_PRIORITY );
47 }
48
49 /***********************************************************************
50  * ReaderFunc
51  ***********************************************************************
52  *
53  **********************************************************************/
54 static void ReaderFunc( void * _r )
55 {
56     hb_reader_t  * r = _r;
57     hb_fifo_t    * fifo;
58     hb_buffer_t  * buf;
59     hb_list_t    * list;
60     int            chapter = -1;
61
62     if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
63     {
64         if ( !(r->stream = hb_stream_open(r->title->dvd) ) )
65         {
66           return;
67         }
68     }
69
70     if (r->dvd)
71     {
72       if( !hb_dvd_start( r->dvd, r->title->index, r->job->chapter_start ) )
73       {
74           hb_dvd_close( &r->dvd );
75           return;
76       }
77     }
78     
79         if (r->stream)
80         {
81                 // At this point r->audios[0] gives us the index of the selected audio track for output track 0
82                 // we cannot effectively demux multiple PID's into the seperate output tracks unfortunately
83                 // so we'll just specifiy things here for a single track.
84                 hb_stream_set_selected_audio_pid_index(r->stream, r->job->audios[0]);
85         }
86         
87     list  = hb_list_init();
88     r->ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
89
90     while( !*r->die && !r->job->done )
91     {
92         if (r->dvd)
93           chapter = hb_dvd_chapter( r->dvd );
94         else if (r->stream)
95           chapter = 1;
96           
97         if( chapter < 0 )
98         {
99             hb_log( "reader: end of the title reached" );
100             break;
101         }
102         if( chapter > r->job->chapter_end )
103         {
104             hb_log( "reader: end of chapter %d reached (%d)",
105                     r->job->chapter_end, chapter );
106             break;
107         }
108
109         if (r->dvd)
110         {
111           if( !hb_dvd_read( r->dvd, r->ps ) )
112           {
113               break;
114           }
115         }
116         else if (r->stream)
117         {
118           if ( !hb_stream_read( r->stream, r->ps ) )
119           {
120             break;
121           }
122         }
123
124         if( r->job->indepth_scan )
125         {
126             /*
127              * Need to update the progress during a subtitle scan
128              */
129             hb_state_t state;
130
131 #define p state.param.working
132
133             state.state = HB_STATE_WORKING;
134             p.progress = (float)chapter / (float)r->job->chapter_end;
135             if( p.progress > 1.0 )
136             {
137                 p.progress = 1.0;
138             } 
139             p.rate_avg = 0.0;
140             p.hours    = -1;
141             p.minutes  = -1;
142             p.seconds  = -1;
143             hb_set_state( r->job->h, &state );
144         }
145
146         hb_demux_ps( r->ps, list );
147
148         while( ( buf = hb_list_item( list, 0 ) ) )
149         {
150             hb_list_rem( list, buf );
151             fifo = GetFifoForId( r->job, buf->id );
152             if( fifo )
153             {
154                 while( !*r->die && !r->job->done &&
155                        hb_fifo_is_full( fifo ) )
156                 {
157                     hb_snooze( 50 );
158                 }
159                 buf->sequence = r->sequence++;
160                 hb_fifo_push( fifo, buf );
161             }
162             else
163             {
164                 hb_buffer_close( &buf );
165             }
166         }
167     }
168
169     hb_list_empty( &list );
170     hb_buffer_close( &r->ps );
171     if (r->dvd)
172     {
173       hb_dvd_stop( r->dvd );
174       hb_dvd_close( &r->dvd );
175     }
176     else if (r->stream)
177     {
178       hb_stream_close(&r->stream);
179     }
180     
181     free( r );
182     _r = NULL;
183
184     hb_log( "reader: done" );
185 }
186
187 /***********************************************************************
188  * GetFifoForId
189  ***********************************************************************
190  *
191  **********************************************************************/
192 static hb_fifo_t * GetFifoForId( hb_job_t * job, int id )
193 {
194     hb_title_t    * title = job->title;
195     hb_audio_t    * audio;
196     hb_subtitle_t * subtitle;
197     int             i;
198
199     if( id == 0xE0 )
200     {
201         if( job->indepth_scan ) 
202         {
203             /*
204              * Ditch the video here during the indepth scan until
205              * we can improve the MPEG2 decode performance.
206              */
207             return NULL;
208         } 
209         else 
210         {
211             return job->fifo_mpeg2;
212         }
213     }
214
215     if( job->indepth_scan ) {
216         /*
217          * Count the occurances of the subtitles, don't actually
218          * return any to encode unless we are looking fro forced
219          * subtitles in which case we need to look in the sub picture
220          * to see if it has the forced flag enabled.
221          */
222         for (i=0; i < hb_list_count(title->list_subtitle); i++) {
223             subtitle =  hb_list_item( title->list_subtitle, i);
224             if (id == subtitle->id) {
225                 /*
226                  * A hit, count it.
227                  */
228                 subtitle->hits++;
229                 if( job->subtitle_force )
230                 {
231                     return subtitle->fifo_in;
232                 }
233                 break;
234             }
235         }
236     } else {
237         if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
238             id == subtitle->id )
239         {
240             return subtitle->fifo_in;
241         }
242     }
243     if( !job->indepth_scan ) 
244     {
245         for( i = 0; i < hb_list_count( title->list_audio ); i++ )
246         {
247             audio = hb_list_item( title->list_audio, i );
248             if( id == audio->id )
249             {
250                 return audio->fifo_in;
251             }
252         }
253     }
254
255     return NULL;
256 }
257