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"
16 hb_list_t * list_title;
22 static void ScanFunc( void * );
23 static int DecodePreviews( hb_scan_t *, hb_title_t * title );
24 static void LookForAC3( hb_title_t * title, hb_buffer_t * b );
25 static int AllAC3OK( hb_title_t * title );
27 hb_thread_t * hb_scan_init( hb_handle_t * handle, const char * path,
28 int title_index, hb_list_t * list_title )
30 hb_scan_t * data = calloc( sizeof( hb_scan_t ), 1 );
33 data->path = strdup( path );
34 data->title_index = title_index;
35 data->list_title = list_title;
37 return hb_thread_init( "scan", ScanFunc, data, HB_NORMAL_PRIORITY );
40 static void ScanFunc( void * _data )
42 hb_scan_t * data = (hb_scan_t *) _data;
46 /* Try to open the path as a DVD. If it fails, try as a file */
47 hb_log( "scan: trying to open with libdvdread" );
48 if( ( data->dvd = hb_dvd_init( data->path ) ) )
50 hb_log( "scan: DVD has %d title(s)",
51 hb_dvd_title_count( data->dvd ) );
52 if( data->title_index )
54 /* Scan this title only */
55 hb_list_add( data->list_title, hb_dvd_title_scan( data->dvd,
56 data->title_index ) );
61 for( i = 0; i < hb_dvd_title_count( data->dvd ); i++ )
63 hb_list_add( data->list_title,
64 hb_dvd_title_scan( data->dvd, i + 1 ) );
70 /* Open as a VOB file */
72 hb_log( "scan: trying to open as VOB file" );
73 file = fopen( data->path, "rb" );
81 hb_log( "scan: fopen failed" );
86 for( i = 0; i < hb_list_count( data->list_title ); )
91 hb_title_t * title_tmp = NULL;
93 title = hb_list_item( data->list_title, i );
95 /* I've seen a DVD with strictly identical titles. Check this
96 here and ignore it if redundant */
97 for( j = 0; j < i; j++ )
99 title_tmp = hb_list_item( data->list_title, j );
100 if( title->vts == title_tmp->vts &&
101 title->block_start == title_tmp->block_start &&
102 title->block_end == title_tmp->block_end &&
103 title->block_count == title_tmp->block_count )
114 hb_log( "scan: title %d is duplicate with title %d",
115 title->index, title_tmp->index );
116 hb_list_rem( data->list_title, title );
117 free( title ); /* This _will_ leak! */
121 #define p state.param.scanning
123 state.state = HB_STATE_SCANNING;
124 p.title_cur = title->index;
125 p.title_count = hb_dvd_title_count( data->dvd );
126 hb_set_state( data->h, &state );
129 /* Decode previews */
130 if( !DecodePreviews( data, title ) )
132 /* TODO: free things */
133 hb_list_rem( data->list_title, title );
137 /* Make sure we found AC3 rates and bitrates */
138 for( j = 0; j < hb_list_count( title->list_audio ); )
140 audio = hb_list_item( title->list_audio, j );
141 if( audio->codec == HB_ACODEC_AC3 &&
144 hb_list_rem( title->list_audio, audio );
151 /* Do we still have audio */
152 if( !hb_list_count( title->list_audio ) )
154 hb_list_rem( data->list_title, title );
162 /* Init jobs templates */
163 for( i = 0; i < hb_list_count( data->list_title ); i++ )
167 title = hb_list_item( data->list_title, i );
168 job = calloc( sizeof( hb_job_t ), 1 );
173 /* Set defaults settings */
174 job->chapter_start = 1;
175 job->chapter_end = hb_list_count( title->list_chapter );
177 /* Autocrop by default. Gnark gnark */
178 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
180 if( title->aspect == 16 )
182 hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
183 16 * title->height, 9 * title->width );
187 hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
188 4 * title->height, 3 * title->width );
191 job->width = title->width - job->crop[2] - job->crop[3];
192 // job->height = title->height - job->crop[0] - job->crop[1];
193 hb_fix_aspect( job, HB_KEEP_WIDTH );
194 if( job->height > title->height - job->crop[0] - job->crop[1] )
196 job->height = title->height - job->crop[0] - job->crop[1];
197 hb_fix_aspect( job, HB_KEEP_HEIGHT );
200 hb_log( "scan: title (%d) job->width:%d, job->height:%d",
201 i,job->width, job->height );
205 job->vcodec = HB_VCODEC_FFMPEG;
206 job->vquality = -1.0;
207 job->vbitrate = 1000;
209 job->vrate = title->rate;
210 job->vrate_base = title->rate_base;
215 job->acodec = HB_ACODEC_FAAC;
221 job->mux = HB_MUX_MP4;
226 hb_dvd_close( &data->dvd );
233 /***********************************************************************
235 ***********************************************************************
236 * Decode 10 pictures for the given title.
237 * It assumes that data->reader and data->vts have successfully been
238 * DVDOpen()ed and ifoOpen()ed.
239 **********************************************************************/
240 static int DecodePreviews( hb_scan_t * data, hb_title_t * title )
243 hb_buffer_t * buf_ps, * buf_es, * buf_raw;
244 hb_list_t * list_es, * list_raw;
245 hb_libmpeg2_t * mpeg2;
247 buf_ps = hb_buffer_init( 2048 );
248 list_es = hb_list_init();
249 list_raw = hb_list_init();
251 hb_log( "scan: decoding previews for title %d", title->index );
253 hb_dvd_start( data->dvd, title->index, 1 );
255 for( i = 0; i < 10; i++ )
261 if( !hb_dvd_seek( data->dvd, (float) ( i + 1 ) / 11.0 ) )
266 hb_log( "scan: preview %d", i + 1 );
268 mpeg2 = hb_libmpeg2_init();
270 for( j = 0; j < 10240 ; j++ )
272 if( !hb_dvd_read( data->dvd, buf_ps ) )
276 hb_demux_ps( buf_ps, list_es );
278 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
280 hb_list_rem( list_es, buf_es );
281 if( buf_es->id == 0xE0 && !hb_list_count( list_raw ) )
283 hb_libmpeg2_decode( mpeg2, buf_es, list_raw );
287 LookForAC3( title, buf_es );
289 hb_buffer_close( &buf_es );
291 if( hb_list_count( list_raw ) &&
292 ( i || AllAC3OK( title ) ) )
294 /* We got a picture */
299 if( hb_list_count( list_raw ) &&
300 ( i || AllAC3OK( title ) ) )
306 if( !hb_list_count( list_raw ) )
308 hb_log( "scan: could not get a decoded picture" );
314 /* Get size and rate infos */
315 title->rate = 27000000;
316 hb_libmpeg2_info( mpeg2, &title->width, &title->height,
318 title->crop[0] = title->crop[1] = title->height / 2;
319 title->crop[2] = title->crop[3] = title->width / 2;
322 hb_libmpeg2_close( &mpeg2 );
324 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
326 hb_list_rem( list_es, buf_es );
327 hb_buffer_close( &buf_es );
330 buf_raw = hb_list_item( list_raw, 0 );
332 hb_get_tempory_filename( data->h, filename, "%x%d",
333 (intptr_t)title, i );
335 file_preview = fopen( filename, "w" );
338 fwrite( buf_raw->data, title->width * title->height * 3 / 2,
340 fclose( file_preview );
344 hb_log( "scan: fopen failed (%s)", filename );
347 #define Y buf_raw->data
350 /* Detect black borders */
352 for( j = 0; j < title->width; j++ )
354 for( k = 0; k < title->crop[0]; k++ )
355 if( Y[ k * title->width + j ] > DARK )
360 for( k = 0; k < title->crop[1]; k++ )
361 if( Y[ ( title->height - k - 1 ) *
362 title->width + j ] > DARK )
368 for( j = 0; j < title->height; j++ )
370 for( k = 0; k < title->crop[2]; k++ )
371 if( Y[ j * title->width + k ] > DARK )
376 for( k = 0; k < title->crop[3]; k++ )
377 if( Y[ j * title->width +
378 title->width - k - 1 ] > DARK )
385 while( ( buf_raw = hb_list_item( list_raw, 0 ) ) )
387 hb_list_rem( list_raw, buf_raw );
388 hb_buffer_close( &buf_raw );
392 title->crop[0] = EVEN( title->crop[0] );
393 title->crop[1] = EVEN( title->crop[1] );
394 title->crop[2] = EVEN( title->crop[2] );
395 title->crop[3] = EVEN( title->crop[3] );
397 hb_log( "scan: %dx%d, %.3f fps, autocrop = %d/%d/%d/%d",
398 title->width, title->height, (float) title->rate /
399 (float) title->rate_base, title->crop[0], title->crop[1],
400 title->crop[2], title->crop[3] );
409 hb_buffer_close( &buf_ps );
410 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
412 hb_list_rem( list_es, buf_es );
413 hb_buffer_close( &buf_es );
415 hb_list_close( &list_es );
416 while( ( buf_raw = hb_list_item( list_raw, 0 ) ) )
418 hb_list_rem( list_raw, buf_raw );
419 hb_buffer_close( &buf_raw );
421 hb_list_close( &list_raw );
422 hb_dvd_stop( data->dvd );
426 static void LookForAC3( hb_title_t * title, hb_buffer_t * b )
433 /* Figure out if this is a AC3 buffer for a known track */
434 hb_audio_t * audio = NULL;
435 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
437 audio = hb_list_item( title->list_audio, i );
438 if( audio->codec == HB_ACODEC_AC3 &&
455 /* Already done for this track */
459 for( i = 0; i < b->size - 7; i++ )
461 if( a52_syncinfo( &b->data[i], &flags, &rate, &bitrate ) )
463 hb_log( "scan: rate=%dHz, bitrate=%d", rate, bitrate );
465 audio->bitrate = bitrate;
466 switch( flags & A52_CHANNEL_MASK )
471 audio->src_discrete_front_channels = 1;
472 audio->src_discrete_rear_channels = 0;
473 audio->src_encoded_front_channels = 1;
474 audio->src_encoded_rear_channels = 0;
478 audio->src_discrete_front_channels = 2;
479 audio->src_discrete_rear_channels = 0;
480 audio->src_encoded_front_channels = 2;
481 audio->src_encoded_rear_channels = 0;
484 audio->src_discrete_front_channels = 2;
485 audio->src_discrete_rear_channels = 0;
486 audio->src_encoded_front_channels = 3;
487 audio->src_encoded_rear_channels = 1;
490 audio->src_discrete_front_channels = 3;
491 audio->src_discrete_rear_channels = 0;
492 audio->src_encoded_front_channels = 3;
493 audio->src_encoded_rear_channels = 0;
496 audio->src_discrete_front_channels = 2;
497 audio->src_discrete_rear_channels = 1;
498 audio->src_encoded_front_channels = 2;
499 audio->src_encoded_rear_channels = 1;
502 audio->src_discrete_front_channels = 3;
503 audio->src_discrete_rear_channels = 1;
504 audio->src_encoded_front_channels = 3;
505 audio->src_encoded_rear_channels = 1;
508 audio->src_discrete_front_channels = 2;
509 audio->src_discrete_rear_channels = 2;
510 audio->src_encoded_front_channels = 2;
511 audio->src_encoded_rear_channels = 2;
514 audio->src_discrete_front_channels = 3;
515 audio->src_discrete_rear_channels = 2;
516 audio->src_encoded_front_channels = 3;
517 audio->src_encoded_rear_channels = 2;
521 if (flags & A52_LFE) {
522 audio->src_discrete_lfe_channels = 1;
524 audio->src_discrete_lfe_channels = 0;
527 /* store the AC3 flags for future reference
528 This enables us to find out if we had a stereo or Dolby source later on */
529 audio->config.a52.ac3flags = flags;
531 /* store the ac3 flags in the public ac3flags property too, so we can access it from the GUI */
532 audio->ac3flags = audio->config.a52.ac3flags;
535 if ( (flags & A52_CHANNEL_MASK) == A52_DOLBY ) {
536 sprintf( audio->lang + strlen( audio->lang ),
537 " (Dolby Surround)" );
539 sprintf( audio->lang + strlen( audio->lang ),
541 audio->src_discrete_front_channels + audio->src_discrete_rear_channels, audio->src_discrete_lfe_channels );
549 static int AllAC3OK( hb_title_t * title )
554 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
556 audio = hb_list_item( title->list_audio, i );
557 if( audio->codec == HB_ACODEC_AC3 &&