OSDN Git Service

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