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 );
166 /* Init jobs templates */
167 for( i = 0; i < hb_list_count( data->list_title ); i++ )
171 title = hb_list_item( data->list_title, i );
172 job = calloc( sizeof( hb_job_t ), 1 );
177 /* Set defaults settings */
178 job->chapter_start = 1;
179 job->chapter_end = hb_list_count( title->list_chapter );
181 /* Autocrop by default. Gnark gnark */
182 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
184 if( title->aspect == 16 )
186 hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
187 16 * title->height, 9 * title->width );
191 hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
192 4 * title->height, 3 * title->width );
195 job->width = title->width - job->crop[2] - job->crop[3];
196 // job->height = title->height - job->crop[0] - job->crop[1];
197 hb_fix_aspect( job, HB_KEEP_WIDTH );
198 if( job->height > title->height - job->crop[0] - job->crop[1] )
200 job->height = title->height - job->crop[0] - job->crop[1];
201 hb_fix_aspect( job, HB_KEEP_HEIGHT );
204 hb_log( "scan: title (%d) job->width:%d, job->height:%d",
205 i,job->width, job->height );
209 job->vcodec = HB_VCODEC_FFMPEG;
210 job->vquality = -1.0;
211 job->vbitrate = 1000;
213 job->vrate = title->rate;
214 job->vrate_base = title->rate_base;
219 job->acodec = HB_ACODEC_FAAC;
225 job->mux = HB_MUX_MP4;
230 hb_dvd_close( &data->dvd );
237 /***********************************************************************
239 ***********************************************************************
240 * Decode 10 pictures for the given title.
241 * It assumes that data->reader and data->vts have successfully been
242 * DVDOpen()ed and ifoOpen()ed.
243 **********************************************************************/
244 static int DecodePreviews( hb_scan_t * data, hb_title_t * title )
247 hb_buffer_t * buf_ps, * buf_es, * buf_raw;
248 hb_list_t * list_es, * list_raw;
249 hb_libmpeg2_t * mpeg2;
251 buf_ps = hb_buffer_init( 2048 );
252 list_es = hb_list_init();
253 list_raw = hb_list_init();
255 hb_log( "scan: decoding previews for title %d", title->index );
257 hb_dvd_start( data->dvd, title->index, 1 );
259 for( i = 0; i < 10; i++ )
265 if( !hb_dvd_seek( data->dvd, (float) ( i + 1 ) / 11.0 ) )
270 hb_log( "scan: preview %d", i + 1 );
272 mpeg2 = hb_libmpeg2_init();
274 for( j = 0; j < 10240 ; j++ )
276 if( !hb_dvd_read( data->dvd, buf_ps ) )
280 hb_demux_ps( buf_ps, list_es );
282 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
284 hb_list_rem( list_es, buf_es );
285 if( buf_es->id == 0xE0 && !hb_list_count( list_raw ) )
287 hb_libmpeg2_decode( mpeg2, buf_es, list_raw );
291 LookForAC3AndDCA( title, buf_es );
293 hb_buffer_close( &buf_es );
295 if( hb_list_count( list_raw ) &&
296 ( i || AllAC3AndDCAOK( title ) ) )
298 /* We got a picture */
303 if( hb_list_count( list_raw ) &&
304 ( i || AllAC3AndDCAOK( title ) ) )
310 if( !hb_list_count( list_raw ) )
312 hb_log( "scan: could not get a decoded picture" );
318 /* Get size and rate infos */
319 title->rate = 27000000;
320 hb_libmpeg2_info( mpeg2, &title->width, &title->height,
322 title->crop[0] = title->crop[1] = title->height / 2;
323 title->crop[2] = title->crop[3] = title->width / 2;
326 hb_libmpeg2_close( &mpeg2 );
328 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
330 hb_list_rem( list_es, buf_es );
331 hb_buffer_close( &buf_es );
334 buf_raw = hb_list_item( list_raw, 0 );
336 hb_get_tempory_filename( data->h, filename, "%x%d",
337 (intptr_t)title, i );
339 file_preview = fopen( filename, "w" );
342 fwrite( buf_raw->data, title->width * title->height * 3 / 2,
344 fclose( file_preview );
348 hb_log( "scan: fopen failed (%s)", filename );
351 #define Y buf_raw->data
354 /* Detect black borders */
356 for( j = 0; j < title->width; j++ )
358 for( k = 0; k < title->crop[0]; k++ )
359 if( Y[ k * title->width + j ] > DARK )
364 for( k = 0; k < title->crop[1]; k++ )
365 if( Y[ ( title->height - k - 1 ) *
366 title->width + j ] > DARK )
372 for( j = 0; j < title->height; j++ )
374 for( k = 0; k < title->crop[2]; k++ )
375 if( Y[ j * title->width + k ] > DARK )
380 for( k = 0; k < title->crop[3]; k++ )
381 if( Y[ j * title->width +
382 title->width - k - 1 ] > DARK )
389 while( ( buf_raw = hb_list_item( list_raw, 0 ) ) )
391 hb_list_rem( list_raw, buf_raw );
392 hb_buffer_close( &buf_raw );
396 title->crop[0] = EVEN( title->crop[0] );
397 title->crop[1] = EVEN( title->crop[1] );
398 title->crop[2] = EVEN( title->crop[2] );
399 title->crop[3] = EVEN( title->crop[3] );
401 hb_log( "scan: %dx%d, %.3f fps, autocrop = %d/%d/%d/%d",
402 title->width, title->height, (float) title->rate /
403 (float) title->rate_base, title->crop[0], title->crop[1],
404 title->crop[2], title->crop[3] );
413 hb_buffer_close( &buf_ps );
414 while( ( buf_es = hb_list_item( list_es, 0 ) ) )
416 hb_list_rem( list_es, buf_es );
417 hb_buffer_close( &buf_es );
419 hb_list_close( &list_es );
420 while( ( buf_raw = hb_list_item( list_raw, 0 ) ) )
422 hb_list_rem( list_raw, buf_raw );
423 hb_buffer_close( &buf_raw );
425 hb_list_close( &list_raw );
426 hb_dvd_stop( data->dvd );
430 static void LookForAC3AndDCA( hb_title_t * title, hb_buffer_t * b )
439 /* Figure out if this is a AC3 or DCA buffer for a known track */
440 hb_audio_t * audio = NULL;
441 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
443 audio = hb_list_item( title->list_audio, i );
444 if( ( audio->codec == HB_ACODEC_AC3 || audio->codec == HB_ACODEC_DCA ) &&
461 /* Already done for this track */
465 for( i = 0; i < b->size - 7; i++ )
468 if ( audio->codec == HB_ACODEC_AC3 )
472 if( a52_syncinfo( &b->data[i], &flags, &rate, &bitrate ) )
474 hb_log( "scan: AC3, rate=%dHz, bitrate=%d", rate, bitrate );
476 audio->bitrate = bitrate;
477 switch( flags & A52_CHANNEL_MASK )
482 audio->src_discrete_front_channels = 1;
483 audio->src_discrete_rear_channels = 0;
484 audio->src_encoded_front_channels = 1;
485 audio->src_encoded_rear_channels = 0;
489 audio->src_discrete_front_channels = 2;
490 audio->src_discrete_rear_channels = 0;
491 audio->src_encoded_front_channels = 2;
492 audio->src_encoded_rear_channels = 0;
495 audio->src_discrete_front_channels = 2;
496 audio->src_discrete_rear_channels = 0;
497 audio->src_encoded_front_channels = 3;
498 audio->src_encoded_rear_channels = 1;
501 audio->src_discrete_front_channels = 3;
502 audio->src_discrete_rear_channels = 0;
503 audio->src_encoded_front_channels = 3;
504 audio->src_encoded_rear_channels = 0;
507 audio->src_discrete_front_channels = 2;
508 audio->src_discrete_rear_channels = 1;
509 audio->src_encoded_front_channels = 2;
510 audio->src_encoded_rear_channels = 1;
513 audio->src_discrete_front_channels = 3;
514 audio->src_discrete_rear_channels = 1;
515 audio->src_encoded_front_channels = 3;
516 audio->src_encoded_rear_channels = 1;
519 audio->src_discrete_front_channels = 2;
520 audio->src_discrete_rear_channels = 2;
521 audio->src_encoded_front_channels = 2;
522 audio->src_encoded_rear_channels = 2;
525 audio->src_discrete_front_channels = 3;
526 audio->src_discrete_rear_channels = 2;
527 audio->src_encoded_front_channels = 3;
528 audio->src_encoded_rear_channels = 2;
532 if (flags & A52_LFE) {
533 audio->src_discrete_lfe_channels = 1;
535 audio->src_discrete_lfe_channels = 0;
538 /* store the AC3 flags for future reference
539 This enables us to find out if we had a stereo or Dolby source later on */
540 audio->config.a52.ac3flags = flags;
542 /* store the ac3 flags in the public ac3flags property too, so we can access it from the GUI */
543 audio->ac3flags = audio->config.a52.ac3flags;
546 if ( (flags & A52_CHANNEL_MASK) == A52_DOLBY ) {
547 sprintf( audio->lang + strlen( audio->lang ),
548 " (Dolby Surround)" );
550 sprintf( audio->lang + strlen( audio->lang ),
552 audio->src_discrete_front_channels + audio->src_discrete_rear_channels, audio->src_discrete_lfe_channels );
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 )
575 audio->src_discrete_front_channels = 1;
576 audio->src_discrete_rear_channels = 0;
577 audio->src_encoded_front_channels = 1;
578 audio->src_encoded_rear_channels = 0;
582 case DCA_STEREO_SUMDIFF:
583 case DCA_STEREO_TOTAL:
584 audio->src_discrete_front_channels = 2;
585 audio->src_discrete_rear_channels = 0;
586 audio->src_encoded_front_channels = 2;
587 audio->src_encoded_rear_channels = 0;
590 audio->src_discrete_front_channels = 2;
591 audio->src_discrete_rear_channels = 0;
592 audio->src_encoded_front_channels = 3;
593 audio->src_encoded_rear_channels = 1;
596 audio->src_discrete_front_channels = 3;
597 audio->src_discrete_rear_channels = 0;
598 audio->src_encoded_front_channels = 3;
599 audio->src_encoded_rear_channels = 0;
602 audio->src_discrete_front_channels = 2;
603 audio->src_discrete_rear_channels = 1;
604 audio->src_encoded_front_channels = 2;
605 audio->src_encoded_rear_channels = 1;
608 audio->src_discrete_front_channels = 3;
609 audio->src_discrete_rear_channels = 1;
610 audio->src_encoded_front_channels = 3;
611 audio->src_encoded_rear_channels = 1;
614 audio->src_discrete_front_channels = 2;
615 audio->src_discrete_rear_channels = 2;
616 audio->src_encoded_front_channels = 2;
617 audio->src_encoded_rear_channels = 2;
620 audio->src_discrete_front_channels = 3;
621 audio->src_discrete_rear_channels = 2;
622 audio->src_encoded_front_channels = 3;
623 audio->src_encoded_rear_channels = 2;
626 audio->src_discrete_front_channels = 4;
627 audio->src_discrete_rear_channels = 2;
628 audio->src_encoded_front_channels = 4;
629 audio->src_encoded_rear_channels = 2;
633 if (flags & DCA_LFE) {
634 audio->src_discrete_lfe_channels = 1;
636 audio->src_discrete_lfe_channels = 0;
639 /* store the DCA flags for future reference
640 This enables us to find out if we had a stereo or Dolby source later on */
641 audio->config.dca.dcaflags = flags;
643 /* store the dca flags in the public dcaflags property too, so we can access it from the GUI */
644 audio->dcaflags = audio->config.dca.dcaflags;
647 if ( (flags & DCA_CHANNEL_MASK) == DCA_DOLBY ) {
648 sprintf( audio->lang + strlen( audio->lang ),
649 " (Dolby Surround)" );
651 sprintf( audio->lang + strlen( audio->lang ),
653 audio->src_discrete_front_channels + audio->src_discrete_rear_channels, audio->src_discrete_lfe_channels );
662 static int AllAC3AndDCAOK( hb_title_t * title )
667 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
669 audio = hb_list_item( title->list_audio, i );
670 if( ( audio->codec == HB_ACODEC_AC3 || audio->codec == HB_ACODEC_DCA ) &&