Moves the filters' logging info to work.c, adds parameter info. I also changed the...
[HandBrake.git] / libhb / reader.c
blob335dfb80d48bad61ea7bb26143c66ed7df9c4564
1 /* $Id: reader.c,v 1.21 2005/11/25 15:05:25 titer Exp $
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. */
7 #include "hb.h"
9 typedef struct
11 hb_job_t * job;
12 hb_title_t * title;
13 volatile int * die;
15 hb_dvd_t * dvd;
16 hb_buffer_t * ps;
17 hb_stream_t * stream;
19 } hb_reader_t;
21 /***********************************************************************
22 * Local prototypes
23 **********************************************************************/
24 static void ReaderFunc( void * );
25 static hb_fifo_t * GetFifoForId( hb_job_t * job, int id );
27 /***********************************************************************
28 * hb_reader_init
29 ***********************************************************************
31 **********************************************************************/
32 hb_thread_t * hb_reader_init( hb_job_t * job )
34 hb_reader_t * r;
36 r = calloc( sizeof( hb_reader_t ), 1 );
38 r->job = job;
39 r->title = job->title;
40 r->die = job->die;
42 return hb_thread_init( "reader", ReaderFunc, r,
43 HB_NORMAL_PRIORITY );
46 /***********************************************************************
47 * ReaderFunc
48 ***********************************************************************
50 **********************************************************************/
51 static void ReaderFunc( void * _r )
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;
59 if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
61 if ( !(r->stream = hb_stream_open(r->title->dvd) ) )
63 return;
67 if (r->dvd)
69 if( !hb_dvd_start( r->dvd, r->title->index, r->job->chapter_start ) )
71 hb_dvd_close( &r->dvd );
72 return;
76 if (r->stream)
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]);
84 list = hb_list_init();
85 r->ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
87 while( !*r->die && !r->job->done )
89 if (r->dvd)
90 chapter = hb_dvd_chapter( r->dvd );
91 else if (r->stream)
92 chapter = 1;
94 if( chapter < 0 )
96 hb_log( "reader: end of the title reached" );
97 break;
99 if( chapter > r->job->chapter_end )
101 hb_log( "reader: end of chapter %d reached (%d)",
102 r->job->chapter_end, chapter );
103 break;
106 if (r->dvd)
108 if( !hb_dvd_read( r->dvd, r->ps ) )
110 break;
113 else if (r->stream)
115 if ( !hb_stream_read( r->stream, r->ps ) )
117 break;
121 hb_demux_ps( r->ps, list );
123 while( ( buf = hb_list_item( list, 0 ) ) )
125 hb_list_rem( list, buf );
126 fifo = GetFifoForId( r->job, buf->id );
127 if( fifo )
129 while( !*r->die && !r->job->done &&
130 hb_fifo_is_full( fifo ) )
132 hb_snooze( 50 );
134 hb_fifo_push( fifo, buf );
136 else
138 hb_buffer_close( &buf );
143 hb_list_empty( &list );
144 hb_buffer_close( &r->ps );
145 if (r->dvd)
147 hb_dvd_stop( r->dvd );
148 hb_dvd_close( &r->dvd );
150 else if (r->stream)
152 hb_stream_close(&r->stream);
155 free( r );
156 _r = NULL;
158 hb_log( "reader: done" );
161 /***********************************************************************
162 * GetFifoForId
163 ***********************************************************************
165 **********************************************************************/
166 static hb_fifo_t * GetFifoForId( hb_job_t * job, int id )
168 hb_title_t * title = job->title;
169 hb_audio_t * audio;
170 hb_subtitle_t * subtitle;
171 int i;
173 if( id == 0xE0 )
175 return job->fifo_mpeg2;
178 if (job->subtitle_scan) {
180 * Count the occurances of the subtitles, don't actually return any to encode.
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) {
186 * A hit, count it.
188 subtitle->hits++;
191 } else {
192 if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
193 id == subtitle->id )
195 return subtitle->fifo_in;
198 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
200 audio = hb_list_item( title->list_audio, i );
201 if( id == audio->id )
203 return audio->fifo_in;
207 return NULL;