OSDN Git Service

Implement transport and program stream support. With these changes it's now possible...
[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         hb_demux_ps( r->ps, list );
122
123         while( ( buf = hb_list_item( list, 0 ) ) )
124         {
125             hb_list_rem( list, buf );
126             fifo = GetFifoForId( r->job, buf->id );
127             if( fifo )
128             {
129                 while( !*r->die && !r->job->done &&
130                        hb_fifo_is_full( fifo ) )
131                 {
132                     hb_snooze( 50 );
133                 }
134                 hb_fifo_push( fifo, buf );
135             }
136             else
137             {
138                 hb_buffer_close( &buf );
139             }
140         }
141     }
142
143     hb_list_empty( &list );
144     hb_buffer_close( &r->ps );
145     if (r->dvd)
146     {
147       hb_dvd_stop( r->dvd );
148       hb_dvd_close( &r->dvd );
149     }
150     else if (r->stream)
151     {
152       hb_stream_close(&r->stream);
153     }
154     
155     free( r );
156     _r = NULL;
157
158     hb_log( "reader: done" );
159 }
160
161 /***********************************************************************
162  * GetFifoForId
163  ***********************************************************************
164  *
165  **********************************************************************/
166 static hb_fifo_t * GetFifoForId( hb_job_t * job, int id )
167 {
168     hb_title_t    * title = job->title;
169     hb_audio_t    * audio;
170     hb_subtitle_t * subtitle;
171     int             i;
172
173     if( id == 0xE0 )
174     {
175         return job->fifo_mpeg2;
176     }
177
178     if (job->subtitle_scan) {
179         /*
180          * Count the occurances of the subtitles, don't actually return any to encode.
181          */
182         for (i=0; i < hb_list_count(title->list_subtitle); i++) {
183             subtitle =  hb_list_item( title->list_subtitle, i);
184             if (id == subtitle->id) {
185                 /*
186                  * A hit, count it.
187                  */
188                 subtitle->hits++;
189             }
190         }
191     } else {
192         if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
193             id == subtitle->id )
194         {
195             return subtitle->fifo_in;
196         }
197     }
198     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
199     {
200         audio = hb_list_item( title->list_audio, i );
201         if( id == audio->id )
202         {
203             return audio->fifo_in;
204         }
205     }
206
207     return NULL;
208 }
209