1 /* $Id: scan.c,v 1.52 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. */
8 #include "a52dec/a52.h"
17 hb_list_t * list_title;
23 static void ScanFunc( void * );
24 static int DecodePreviews( hb_scan_t *, hb_title_t * title );
25 static void LookForAC3AndDCA( hb_title_t * title, hb_buffer_t * b );
26 static int AllAC3AndDCAOK( hb_title_t * title );
28 hb_thread_t * hb_scan_init( hb_handle_t * handle, const char * path,
29 int title_index, hb_list_t * list_title )
31 hb_scan_t * data = calloc( sizeof( hb_scan_t ), 1 );
34 data->path = strdup( path );
35 data->title_index = title_index;
36 data->list_title = list_title;
38 return hb_thread_init( "scan", ScanFunc, data, HB_NORMAL_PRIORITY );
41 static void ScanFunc( void * _data )
43 hb_scan_t * data = (hb_scan_t *) _data;
47 /* Try to open the path as a DVD. If it fails, try as a file */
48 hb_log( "scan: trying to open with libdvdread" );
49 if( ( data->dvd = hb_dvd_init( data->path ) ) )
51 hb_log( "scan: DVD has %d title(s)",
52 hb_dvd_title_count( data->dvd ) );
53 if( data->title_index )
55 /* Scan this title only */
56 hb_list_add( data->list_title, hb_dvd_title_scan( data->dvd,
57 data->title_index ) );
62 for( i = 0; i < hb_dvd_title_count( data->dvd ); i++ )
64 hb_list_add( data->list_title,
65 hb_dvd_title_scan( data->dvd, i + 1 ) );
71 /* Open as a VOB file */
73 hb_log( "scan: trying to open as VOB file" );
74 file = fopen( data->path, "rb" );
82 hb_log( "scan: fopen failed" );
87 for( i = 0; i < hb_list_count( data->list_title ); )
92 hb_title_t * title_tmp = NULL;
94 title = hb_list_item( data->list_title, i );
96 /* I've seen a DVD with strictly identical titles. Check this
97 here and ignore it if redundant */
98 for( j = 0; j < i; j++ )
100 title_tmp = hb_list_item( data->list_title, j );
101 if( title->vts == title_tmp->vts &&
102 title->block_start == title_tmp->block_start &&
103 title->block_end == title_tmp->block_end &&
104 title->block_count == title_tmp->block_count )
115 hb_log( "scan: title %d is duplicate with title %d",
116 title->index, title_tmp->index );
117 hb_list_rem( data->list_title, title );
118 free( title ); /* This _will_ leak! */
122 #define p state.param.scanning
124 state.state = HB_STATE_SCANNING;
125 p.title_cur = title->index;
126 p.title_count = hb_dvd_title_count( data->dvd );
127 hb_set_state( data->h, &state );
130 /* Decode previews */
131 /* this will also detect more AC3 / DTS information */
132 if( !DecodePreviews( data, title ) )
134 /* TODO: free things */
135 hb_list_rem( data->list_title, title );
139 /* Make sure we found AC3 / DCA rates and bitrates */
140 for( j = 0; j < hb_list_count( title->list_audio ); )
142 audio = hb_list_item( title->list_audio, j );
143 if( ( audio->codec == HB_ACODEC_AC3 || audio->codec == HB_ACODEC_DCA ) &&
146 hb_log( "scan: removing audio with codec of 0x%x because of no bitrate",
148 hb_list_rem( title->list_audio, audio );
155 /* Do we still have audio */
156 if( !hb_list_count( title->list_audio ) )
158 hb_list_rem( data->list_title, title );
163 /* set a default input channel layout of stereo for LPCM or MPEG2 audio */
164 /* AC3 and DCA will already have had their layout set via DecodePreviews above, */
165 /* which calls LookForAC3AndDCA */
166 for( j = 0; j < hb_list_count( title->list_audio ); j++ )
168 audio = hb_list_item( title->list_audio, j );
169 if( audio->codec == HB_ACODEC_LPCM || audio->codec == HB_ACODEC_MPGA )
171 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
178 /* Init jobs templates */
179 for( i = 0; i < hb_list_count( data->list_title ); i++ )
183 title = hb_list_item( data->list_title, i );
184 job = calloc( sizeof( hb_job_t ), 1 );
189 /* Set defaults settings */
190 job->chapter_start = 1;
191 job->chapter_end = hb_list_count( title->list_chapter );
193 /* Autocrop by default. Gnark gnark */
194 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
196 if( title->aspect == 16 )
198 hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
199 16 * title->height, 9 * title->width );
203 hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
204 4 * title->height, 3 * title->width );
207 job->width = title->width - job->crop[2] - job->crop[3];
208 // job->height = title->height - job->crop[0] - job->crop[1];
209 hb_fix_aspect( job, HB_KEEP_WIDTH );
210 if( job->height > title->height - job->crop[0] - job->crop[1] )
212 job->height = title->height - job->crop[0] - job->crop[1];
213 hb_fix_aspect( job, HB_KEEP_HEIGHT );
216 hb_log( "scan: title (%d) job->width:%d, job->height:%d",
217 i,job->width, job->height );
221 job->vcodec = HB_VCODEC_FFMPEG;
222 job->vquality = -1.0;
223 job->vbitrate = 1000;
225 job->vrate = title->rate;
226 job->vrate_base = title->rate_base;
231 job->acodec = HB_ACODEC_FAAC;
237 job->mux = HB_MUX_MP4;
242 hb_dvd_close( &data->dvd );
249 /***********************************************************************
251 ***********************************************************************
252 * Decode 10 pictures for the given title.
253 * It assumes that data->reader and data->vts have successfully been
254 * DVDOpen()ed and ifoOpen()ed.
255 **********************************************************************/
256 static int DecodePreviews( hb_scan_t * data, hb_title_t * title )
259 hb_buffer_t * buf_ps, * buf_es, * buf_raw;
260 hb_list_t * list_es, * list_raw;
261 hb_libmpeg2_t * mpeg2;
263 buf_ps = hb_buffer_init( 2048 );
264 list_es = hb_list_init();
265 list_raw = hb_list_init();
267 hb_log( "scan: decoding previews for title %d", title->index );
269 hb_dvd_start( data->dvd, title->index, 1 );
271 for( i = 0; i < 10; i++ )
277 if( !hb_dvd_seek( data->dvd, (float) ( i + 1 ) / 11.0 ) )
282 hb_log( "scan: preview %d", i + 1 );
284 mpeg2 = hb_libmpeg2_init();
286 for( j = 0; j < 10240 ; j++ )
288 if( !hb_dvd_read( data->dvd, buf_ps ) )
292 hb_demux_ps( buf_ps, list_es );
294 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
296 hb_list_rem( list_es, buf_es );
297 if( buf_es->id == 0xE0 && !hb_list_count( list_raw ) )
299 hb_libmpeg2_decode( mpeg2, buf_es, list_raw );
303 LookForAC3AndDCA( title, buf_es );
305 hb_buffer_close( &buf_es );
307 if( hb_list_count( list_raw ) &&
308 ( i || AllAC3AndDCAOK( title ) ) )
310 /* We got a picture */
315 if( hb_list_count( list_raw ) &&
316 ( i || AllAC3AndDCAOK( title ) ) )
322 if( !hb_list_count( list_raw ) )
324 hb_log( "scan: could not get a decoded picture" );
330 /* Get size and rate infos */
331 title->rate = 27000000;
332 hb_libmpeg2_info( mpeg2, &title->width, &title->height,
334 title->crop[0] = title->crop[1] = title->height / 2;
335 title->crop[2] = title->crop[3] = title->width / 2;
338 hb_libmpeg2_close( &mpeg2 );
340 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
342 hb_list_rem( list_es, buf_es );
343 hb_buffer_close( &buf_es );
346 buf_raw = hb_list_item( list_raw, 0 );
348 hb_get_tempory_filename( data->h, filename, "%x%d",
349 (intptr_t)title, i );
351 file_preview = fopen( filename, "w" );
354 fwrite( buf_raw->data, title->width * title->height * 3 / 2,
356 fclose( file_preview );
360 hb_log( "scan: fopen failed (%s)", filename );
363 #define Y buf_raw->data
366 /* Detect black borders */
368 for( j = 0; j < title->width; j++ )
370 for( k = 0; k < title->crop[0]; k++ )
371 if( Y[ k * title->width + j ] > DARK )
376 for( k = 0; k < title->crop[1]; k++ )
377 if( Y[ ( title->height - k - 1 ) *
378 title->width + j ] > DARK )
384 for( j = 0; j < title->height; j++ )
386 for( k = 0; k < title->crop[2]; k++ )
387 if( Y[ j * title->width + k ] > DARK )
392 for( k = 0; k < title->crop[3]; k++ )
393 if( Y[ j * title->width +
394 title->width - k - 1 ] > DARK )
401 while( ( buf_raw = hb_list_item( list_raw, 0 ) ) )
403 hb_list_rem( list_raw, buf_raw );
404 hb_buffer_close( &buf_raw );
408 title->crop[0] = EVEN( title->crop[0] );
409 title->crop[1] = EVEN( title->crop[1] );
410 title->crop[2] = EVEN( title->crop[2] );
411 title->crop[3] = EVEN( title->crop[3] );
413 hb_log( "scan: %dx%d, %.3f fps, autocrop = %d/%d/%d/%d",
414 title->width, title->height, (float) title->rate /
415 (float) title->rate_base, title->crop[0], title->crop[1],
416 title->crop[2], title->crop[3] );
425 hb_buffer_close( &buf_ps );
426 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
428 hb_list_rem( list_es, buf_es );
429 hb_buffer_close( &buf_es );
431 hb_list_close( &list_es );
432 while( ( buf_raw = hb_list_item( list_raw, 0 ) ) )
434 hb_list_rem( list_raw, buf_raw );
435 hb_buffer_close( &buf_raw );
437 hb_list_close( &list_raw );
438 hb_dvd_stop( data->dvd );
442 static void LookForAC3AndDCA( hb_title_t * title, hb_buffer_t * b )
451 /* Figure out if this is a AC3 or DCA buffer for a known track */
452 hb_audio_t * audio = NULL;
453 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
455 audio = hb_list_item( title->list_audio, i );
456 /* check if we have an AC3 or DCA which we recognise */
457 if( ( audio->codec == HB_ACODEC_AC3 || audio->codec == HB_ACODEC_DCA ) &&
474 /* Already done for this track */
478 for( i = 0; i < b->size - 7; i++ )
481 if ( audio->codec == HB_ACODEC_AC3 )
485 if( a52_syncinfo( &b->data[i], &flags, &rate, &bitrate ) )
487 hb_log( "scan: AC3, rate=%dHz, bitrate=%d", rate, bitrate );
489 audio->bitrate = bitrate;
490 switch( flags & A52_CHANNEL_MASK )
496 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_MONO;
501 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
503 /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
505 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
509 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
513 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
517 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F;
520 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
523 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
527 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
530 /* add in our own LFE flag if the source has LFE */
533 audio->input_channel_layout = audio->input_channel_layout | HB_INPUT_CH_LAYOUT_HAS_LFE;
536 /* store the AC3 flags for future reference
537 This enables us to find out if we had a stereo or Dolby source later on */
538 audio->config.a52.ac3flags = flags;
540 /* store the ac3 flags in the public ac3flags property too, so we can access it from the GUI */
541 audio->ac3flags = audio->config.a52.ac3flags;
544 if ( (flags & A52_CHANNEL_MASK) == A52_DOLBY ) {
545 sprintf( audio->lang + strlen( audio->lang ),
546 " (Dolby Surround)" );
548 sprintf( audio->lang + strlen( audio->lang ),
550 HB_INPUT_CH_LAYOUT_GET_DISCRETE_FRONT_COUNT(audio->input_channel_layout) +
551 HB_INPUT_CH_LAYOUT_GET_DISCRETE_REAR_COUNT(audio->input_channel_layout),
552 HB_INPUT_CH_LAYOUT_GET_DISCRETE_LFE_COUNT(audio->input_channel_layout));
560 else if ( audio->codec == HB_ACODEC_DCA )
563 hb_log( "scan: checking for DCA syncinfo" );
566 state = dca_init( 0 );
567 if( dca_syncinfo( state, &b->data[i], &flags, &rate, &bitrate, &frame_length ) )
569 hb_log( "scan: DCA, rate=%dHz, bitrate=%d", rate, bitrate );
571 audio->bitrate = bitrate;
572 switch( flags & DCA_CHANNEL_MASK )
576 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_MONO;
581 case DCA_STEREO_SUMDIFF:
582 case DCA_STEREO_TOTAL:
583 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
587 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
591 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
595 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F;
598 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
601 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
604 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_4F2R;
608 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
611 /* add in our own LFE flag if the source has LFE */
614 audio->input_channel_layout = audio->input_channel_layout | HB_INPUT_CH_LAYOUT_HAS_LFE;
617 /* store the DCA flags for future reference
618 This enables us to find out if we had a stereo or Dolby source later on */
619 audio->config.dca.dcaflags = flags;
621 /* store the dca flags in the public dcaflags property too, so we can access it from the GUI */
622 audio->dcaflags = audio->config.dca.dcaflags;
625 if ( (flags & DCA_CHANNEL_MASK) == DCA_DOLBY ) {
626 sprintf( audio->lang + strlen( audio->lang ),
627 " (Dolby Surround)" );
629 sprintf( audio->lang + strlen( audio->lang ),
631 HB_INPUT_CH_LAYOUT_GET_DISCRETE_FRONT_COUNT(audio->input_channel_layout) +
632 HB_INPUT_CH_LAYOUT_GET_DISCRETE_REAR_COUNT(audio->input_channel_layout),
633 HB_INPUT_CH_LAYOUT_GET_DISCRETE_LFE_COUNT(audio->input_channel_layout));
643 static int AllAC3AndDCAOK( hb_title_t * title )
648 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
650 audio = hb_list_item( title->list_audio, i );
651 if( ( audio->codec == HB_ACODEC_AC3 || audio->codec == HB_ACODEC_DCA ) &&