OSDN Git Service

Fix to the chapter merging to ensure that when reading from the media we take into...
[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     int            chapter_end = r->job->chapter_end;
62
63     if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
64     {
65         if ( !(r->stream = hb_stream_open(r->title->dvd) ) )
66         {
67           return;
68         }
69     }
70
71     if (r->dvd)
72     {
73       /*
74        * XXX this code is a temporary hack that should go away if/when
75        *     chapter merging goes away in libhb/dvd.c
76        * map the start and end chapter numbers to on-media chapter
77        * numbers since chapter merging could cause the handbrake numbers
78        * to diverge from the media numbers and, if our chapter_end is after
79        * a media chapter that got merged, we'll stop ripping too early.
80        */
81       int start = r->job->chapter_start;
82       hb_chapter_t * chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
83
84       chapter_end = chap->index;
85       if (start > 1)
86       {
87          chap = hb_list_item( r->title->list_chapter, start - 1 );
88          start = chap->index;
89       }
90       /* end chapter mapping XXX */
91
92       if( !hb_dvd_start( r->dvd, r->title->index, start ) )
93       {
94           hb_dvd_close( &r->dvd );
95           return;
96       }
97     }
98     
99         if (r->stream)
100         {
101                 // At this point r->audios[0] gives us the index of the selected audio track for output track 0
102                 // we cannot effectively demux multiple PID's into the seperate output tracks unfortunately
103                 // so we'll just specifiy things here for a single track.
104                 hb_stream_set_selected_audio_pid_index(r->stream, r->job->audios[0]);
105         }
106         
107     list  = hb_list_init();
108     r->ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
109
110     while( !*r->die && !r->job->done )
111     {
112         if (r->dvd)
113           chapter = hb_dvd_chapter( r->dvd );
114         else if (r->stream)
115           chapter = 1;
116           
117         if( chapter < 0 )
118         {
119             hb_log( "reader: end of the title reached" );
120             break;
121         }
122         if( chapter > chapter_end )
123         {
124             hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
125                     r->job->chapter_end, chapter_end, chapter );
126             break;
127         }
128
129         if (r->dvd)
130         {
131           if( !hb_dvd_read( r->dvd, r->ps ) )
132           {
133               break;
134           }
135         }
136         else if (r->stream)
137         {
138           if ( !hb_stream_read( r->stream, r->ps ) )
139           {
140             break;
141           }
142         }
143
144         if( r->job->indepth_scan )
145         {
146             /*
147              * Need to update the progress during a subtitle scan
148              */
149             hb_state_t state;
150
151 #define p state.param.working
152
153             state.state = HB_STATE_WORKING;
154             p.progress = (float)chapter / (float)r->job->chapter_end;
155             if( p.progress > 1.0 )
156             {
157                 p.progress = 1.0;
158             } 
159             p.rate_avg = 0.0;
160             p.hours    = -1;
161             p.minutes  = -1;
162             p.seconds  = -1;
163             hb_set_state( r->job->h, &state );
164         }
165
166         hb_demux_ps( r->ps, list );
167
168         while( ( buf = hb_list_item( list, 0 ) ) )
169         {
170             hb_list_rem( list, buf );
171             fifo = GetFifoForId( r->job, buf->id );
172             if( fifo )
173             {
174                 while( !*r->die && !r->job->done &&
175                        hb_fifo_is_full( fifo ) )
176                 {
177                     hb_snooze( 50 );
178                 }
179                 buf->sequence = r->sequence++;
180                 hb_fifo_push( fifo, buf );
181             }
182             else
183             {
184                 hb_buffer_close( &buf );
185             }
186         }
187     }
188
189     hb_list_empty( &list );
190     hb_buffer_close( &r->ps );
191     if (r->dvd)
192     {
193       hb_dvd_stop( r->dvd );
194       hb_dvd_close( &r->dvd );
195     }
196     else if (r->stream)
197     {
198       hb_stream_close(&r->stream);
199     }
200     
201     free( r );
202     _r = NULL;
203
204     hb_log( "reader: done" );
205 }
206
207 /***********************************************************************
208  * GetFifoForId
209  ***********************************************************************
210  *
211  **********************************************************************/
212 static hb_fifo_t * GetFifoForId( hb_job_t * job, int id )
213 {
214     hb_title_t    * title = job->title;
215     hb_audio_t    * audio;
216     hb_subtitle_t * subtitle;
217     int             i;
218
219     if( id == 0xE0 )
220     {
221         if( job->indepth_scan ) 
222         {
223             /*
224              * Ditch the video here during the indepth scan until
225              * we can improve the MPEG2 decode performance.
226              */
227             return NULL;
228         } 
229         else 
230         {
231             return job->fifo_mpeg2;
232         }
233     }
234
235     if( job->indepth_scan ) {
236         /*
237          * Count the occurances of the subtitles, don't actually
238          * return any to encode unless we are looking fro forced
239          * subtitles in which case we need to look in the sub picture
240          * to see if it has the forced flag enabled.
241          */
242         for (i=0; i < hb_list_count(title->list_subtitle); i++) {
243             subtitle =  hb_list_item( title->list_subtitle, i);
244             if (id == subtitle->id) {
245                 /*
246                  * A hit, count it.
247                  */
248                 subtitle->hits++;
249                 if( job->subtitle_force )
250                 {
251                     return subtitle->fifo_in;
252                 }
253                 break;
254             }
255         }
256     } else {
257         if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
258             id == subtitle->id )
259         {
260             return subtitle->fifo_in;
261         }
262     }
263     if( !job->indepth_scan ) 
264     {
265         for( i = 0; i < hb_list_count( title->list_audio ); i++ )
266         {
267             audio = hb_list_item( title->list_audio, i );
268             if( id == audio->id )
269             {
270                 return audio->fifo_in;
271             }
272         }
273     }
274
275     return NULL;
276 }
277