OSDN Git Service

MacGui: Make Debug window non editable.
[handbrake-jp/handbrake-jp-git.git] / libhb / scan.c
1 /* $Id: scan.c,v 1.52 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 #include "a52dec/a52.h"
9 #include "dca.h"
10
11 typedef struct
12 {
13     hb_handle_t * h;
14     
15     char        * path;
16     int           title_index;
17     hb_list_t   * list_title;
18     
19     hb_dvd_t    * dvd;
20
21 } hb_scan_t;
22
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 );
27
28 hb_thread_t * hb_scan_init( hb_handle_t * handle, const char * path,
29                             int title_index, hb_list_t * list_title )
30 {
31     hb_scan_t * data = calloc( sizeof( hb_scan_t ), 1 );
32
33     data->h            = handle;
34     data->path         = strdup( path );
35     data->title_index  = title_index;
36     data->list_title   = list_title;
37
38     return hb_thread_init( "scan", ScanFunc, data, HB_NORMAL_PRIORITY );
39 }
40
41 static void ScanFunc( void * _data )
42 {
43     hb_scan_t  * data = (hb_scan_t *) _data;
44     hb_title_t * title;
45     int          i;
46
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 ) ) )
50     {
51         hb_log( "scan: DVD has %d title(s)",
52                 hb_dvd_title_count( data->dvd ) );
53         if( data->title_index )
54         {
55             /* Scan this title only */
56             hb_list_add( data->list_title, hb_dvd_title_scan( data->dvd,
57                             data->title_index ) );
58         }
59         else
60         {
61             /* Scan all titles */
62             for( i = 0; i < hb_dvd_title_count( data->dvd ); i++ )
63             {
64                 hb_list_add( data->list_title,
65                              hb_dvd_title_scan( data->dvd, i + 1 ) );
66             }
67         }
68     }
69     else
70     {
71         /* Open as a VOB file */
72         FILE * file;
73         hb_log( "scan: trying to open as VOB file" );
74         file = fopen( data->path, "rb" );
75         if( file )
76         {
77             /* XXX */
78             fclose( file );
79         }
80         else
81         {
82             hb_log( "scan: fopen failed" );
83             return;
84         }
85     }
86
87     for( i = 0; i < hb_list_count( data->list_title ); )
88     {
89         int j;
90         hb_state_t state;
91         hb_audio_t * audio;
92         hb_title_t * title_tmp = NULL;
93
94         title = hb_list_item( data->list_title, i );
95
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++ )
99         {
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 )
105             {
106                 break;
107             }
108             else
109             {
110                 title_tmp = NULL;
111             }
112         }
113         if( title_tmp )
114         {
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! */
119             continue;
120         }
121
122 #define p state.param.scanning
123         /* Update the UI */
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 );
128 #undef p
129
130         /* Decode previews */
131         /* this will also detect more AC3 / DTS information */
132         if( !DecodePreviews( data, title ) )
133         {
134             /* TODO: free things */
135             hb_list_rem( data->list_title, title );
136             continue;
137         }
138
139         /* Make sure we found AC3 / DCA rates and bitrates */
140         for( j = 0; j < hb_list_count( title->list_audio ); )
141         {
142             audio = hb_list_item( title->list_audio, j );
143             if( ( audio->codec == HB_ACODEC_AC3 || audio->codec == HB_ACODEC_DCA ) &&
144                 !audio->bitrate )
145             {
146                 hb_log( "scan: removing audio with codec of 0x%x because of no bitrate",
147                     audio->codec );
148                 hb_list_rem( title->list_audio, audio );
149                 free( audio );
150                 continue;
151             }
152             j++;
153         }
154
155         /* Do we still have audio */
156         if( !hb_list_count( title->list_audio ) )
157         {
158             hb_list_rem( data->list_title, title );
159             free( title );
160             continue;
161         }
162
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++ )
167         {
168             audio = hb_list_item( title->list_audio, j );
169             if( audio->codec == HB_ACODEC_LPCM || audio->codec == HB_ACODEC_MPGA )
170             {
171                 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
172             }
173         }
174         
175         i++;
176     }
177
178     /* Init jobs templates */
179     for( i = 0; i < hb_list_count( data->list_title ); i++ )
180     {
181         hb_job_t * job;
182
183         title      = hb_list_item( data->list_title, i );
184         job        = calloc( sizeof( hb_job_t ), 1 );
185         title->job = job;
186
187         job->title = title;
188
189         /* Set defaults settings */
190         job->chapter_start = 1;
191         job->chapter_end   = hb_list_count( title->list_chapter );
192
193         /* Autocrop by default. Gnark gnark */
194         memcpy( job->crop, title->crop, 4 * sizeof( int ) );
195
196         if( title->aspect == 16 )
197         {
198             hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
199                        16 * title->height, 9 * title->width );
200         }
201         else
202         {
203             hb_reduce( &job->pixel_aspect_width, &job->pixel_aspect_height,
204                        4 * title->height, 3 * title->width );
205         }
206
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] )
211         {
212             job->height = title->height - job->crop[0] - job->crop[1];
213             hb_fix_aspect( job, HB_KEEP_HEIGHT );
214         }
215
216     hb_log( "scan: title (%d) job->width:%d, job->height:%d",
217             i,job->width, job->height );
218
219         job->keep_ratio = 1;
220
221         job->vcodec     = HB_VCODEC_FFMPEG;
222         job->vquality   = -1.0;
223         job->vbitrate   = 1000;
224         job->pass       = 0;
225         job->vrate      = title->rate;
226         job->vrate_base = title->rate_base;
227
228         job->audios[0] = 0;
229         job->audios[1] = -1;
230
231         job->acodec   = HB_ACODEC_FAAC;
232         job->abitrate = 128;
233         job->arate    = 44100;
234
235         job->subtitle = -1;
236
237         job->mux = HB_MUX_MP4;
238     }
239
240     if( data->dvd )
241     {
242         hb_dvd_close( &data->dvd );
243     }
244     free( data->path );
245     free( data );
246     _data = NULL;
247 }
248
249 /***********************************************************************
250  * DecodePreviews
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 )
257 {
258     int             i, ret;
259     hb_buffer_t   * buf_ps, * buf_es, * buf_raw;
260     hb_list_t     * list_es, * list_raw;
261     hb_libmpeg2_t * mpeg2;
262
263     buf_ps   = hb_buffer_init( 2048 );
264     list_es  = hb_list_init();
265     list_raw = hb_list_init();
266
267     hb_log( "scan: decoding previews for title %d", title->index );
268
269     hb_dvd_start( data->dvd, title->index, 1 );
270
271     for( i = 0; i < 10; i++ )
272     {
273         int j, k;
274         FILE * file_preview;
275         char   filename[1024];
276
277         if( !hb_dvd_seek( data->dvd, (float) ( i + 1 ) / 11.0 ) )
278         {
279             goto error;
280         }
281
282         hb_log( "scan: preview %d", i + 1 );
283
284         mpeg2 = hb_libmpeg2_init();
285
286         for( j = 0; j < 10240 ; j++ )
287         {
288             if( !hb_dvd_read( data->dvd, buf_ps ) )
289             {
290                 goto error;
291             }
292             hb_demux_ps( buf_ps, list_es );
293
294             while( ( buf_es = hb_list_item( list_es, 0 ) ) )
295             {
296                 hb_list_rem( list_es, buf_es );
297                 if( buf_es->id == 0xE0 && !hb_list_count( list_raw ) )
298                 {
299                     hb_libmpeg2_decode( mpeg2, buf_es, list_raw );
300                 }
301                 else if( !i )
302                 {
303                     LookForAC3AndDCA( title, buf_es );
304                 }
305                 hb_buffer_close( &buf_es );
306
307                 if( hb_list_count( list_raw ) &&
308                     ( i || AllAC3AndDCAOK( title ) ) )
309                 {
310                     /* We got a picture */
311                     break;
312                 }
313             }
314
315             if( hb_list_count( list_raw ) &&
316                 ( i || AllAC3AndDCAOK( title ) ) )
317             {
318                 break;
319             }
320         }
321
322         if( !hb_list_count( list_raw ) )
323         {
324             hb_log( "scan: could not get a decoded picture" );
325             goto error;
326         }
327
328         if( !i )
329         {
330             /* Get size and rate infos */
331             title->rate = 27000000;
332             hb_libmpeg2_info( mpeg2, &title->width, &title->height,
333                               &title->rate_base );
334             title->crop[0] = title->crop[1] = title->height / 2;
335             title->crop[2] = title->crop[3] = title->width / 2;
336         }
337
338         hb_libmpeg2_close( &mpeg2 );
339
340         while( ( buf_es = hb_list_item( list_es, 0 ) ) )
341         {
342             hb_list_rem( list_es, buf_es );
343             hb_buffer_close( &buf_es );
344         }
345
346         buf_raw = hb_list_item( list_raw, 0 );
347
348         hb_get_tempory_filename( data->h, filename, "%x%d",
349                                  (intptr_t)title, i );
350
351         file_preview = fopen( filename, "w" );
352         if( file_preview )
353         {
354             fwrite( buf_raw->data, title->width * title->height * 3 / 2,
355                     1, file_preview );
356             fclose( file_preview );
357         }
358         else
359         {
360             hb_log( "scan: fopen failed (%s)", filename );
361         }
362
363 #define Y    buf_raw->data
364 #define DARK 64
365         
366         /* Detect black borders */
367         
368         for( j = 0; j < title->width; j++ )
369         {
370             for( k = 0; k < title->crop[0]; k++ )
371                 if( Y[ k * title->width + j ] > DARK )
372                 {
373                     title->crop[0] = k;
374                     break;
375                 }
376             for( k = 0; k < title->crop[1]; k++ )
377                 if( Y[ ( title->height - k - 1 ) *
378                        title->width + j ] > DARK )
379                 {
380                     title->crop[1] = k;
381                     break;
382                 }
383         }
384         for( j = 0; j < title->height; j++ )
385         {
386             for( k = 0; k < title->crop[2]; k++ ) 
387                 if( Y[ j * title->width + k ] > DARK )
388                 {
389                     title->crop[2] = k;
390                     break;
391                 }
392             for( k = 0; k < title->crop[3]; k++ )
393                 if( Y[ j * title->width +
394                         title->width - k - 1 ] > DARK )
395                 {
396                     title->crop[3] = k;
397                     break;
398                 }
399         }
400
401         while( ( buf_raw = hb_list_item( list_raw, 0 ) ) )
402         {
403             hb_list_rem( list_raw, buf_raw );
404             hb_buffer_close( &buf_raw );
405         }
406     }
407
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] );
412
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] );
417
418     ret = 1;
419     goto cleanup;
420
421 error:
422     ret = 0;
423
424 cleanup:
425     hb_buffer_close( &buf_ps );
426     while( ( buf_es = hb_list_item( list_es, 0 ) ) )
427     {
428         hb_list_rem( list_es, buf_es );
429         hb_buffer_close( &buf_es );
430     }
431     hb_list_close( &list_es );
432     while( ( buf_raw = hb_list_item( list_raw, 0 ) ) )
433     {
434         hb_list_rem( list_raw, buf_raw );
435         hb_buffer_close( &buf_raw );
436     }
437     hb_list_close( &list_raw );
438     hb_dvd_stop( data->dvd );
439     return ret;
440 }
441
442 static void LookForAC3AndDCA( hb_title_t * title, hb_buffer_t * b ) 
443 {
444     int i;
445     int flags;
446     int rate;
447     int bitrate;
448     int frame_length;
449     dca_state_t * state;
450
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++ )
454     {
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 ) &&
458             audio->id    == b->id )
459         {
460             break;
461         }
462         else
463         {
464             audio = NULL;
465         }
466     }
467     if( !audio )
468     {
469         return;
470     }
471
472     if( audio->bitrate )
473     {
474         /* Already done for this track */
475         return;
476     }
477
478     for( i = 0; i < b->size - 7; i++ )
479     {
480
481         if ( audio->codec == HB_ACODEC_AC3 )
482         {
483
484             /* check for a52 */
485             if( a52_syncinfo( &b->data[i], &flags, &rate, &bitrate ) )
486             {
487                 hb_log( "scan: AC3, rate=%dHz, bitrate=%d", rate, bitrate );
488                 audio->rate    = rate;
489                 audio->bitrate = bitrate;
490                 switch( flags & A52_CHANNEL_MASK )
491                 {
492                     /* mono sources */
493                     case A52_MONO:
494                     case A52_CHANNEL1:
495                     case A52_CHANNEL2:
496                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_MONO;
497                         break;
498                     /* stereo input */
499                     case A52_CHANNEL:
500                     case A52_STEREO:
501                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
502                         break;
503                     /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
504                     case A52_DOLBY:
505                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
506                         break;
507                     /* 3F/2R input */
508                     case A52_3F2R:
509                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
510                         break;
511                     /* 3F/1R input */
512                     case A52_3F1R:
513                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
514                         break;
515                     /* other inputs */
516                     case A52_3F:
517                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F;
518                         break;
519                     case A52_2F1R:
520                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
521                         break;
522                     case A52_2F2R:
523                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
524                         break;
525                     /* unknown */
526                     default:
527                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
528                 }
529                 
530                 /* add in our own LFE flag if the source has LFE */
531                 if (flags & A52_LFE)
532                 {
533                     audio->input_channel_layout = audio->input_channel_layout | HB_INPUT_CH_LAYOUT_HAS_LFE;
534                 }
535
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;
539
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;
542
543                 /* XXX */
544                 if ( (flags & A52_CHANNEL_MASK) == A52_DOLBY ) {
545                     sprintf( audio->lang + strlen( audio->lang ),
546                          " (Dolby Surround)" );
547                 } else {
548                     sprintf( audio->lang + strlen( audio->lang ),
549                          " (%d.%d ch)",
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));
553                 }
554
555                 break;
556             
557             }
558
559         }
560         else if ( audio->codec == HB_ACODEC_DCA )
561         {
562
563             hb_log( "scan: checking for DCA syncinfo" );
564
565             /* check for dca */
566             state = dca_init( 0 );
567             if( dca_syncinfo( state, &b->data[i], &flags, &rate, &bitrate, &frame_length ) )
568             {
569                 hb_log( "scan: DCA, rate=%dHz, bitrate=%d", rate, bitrate );
570                 audio->rate    = rate;
571                 audio->bitrate = bitrate;
572                 switch( flags & DCA_CHANNEL_MASK )
573                 {
574                     /* mono sources */
575                     case DCA_MONO:
576                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_MONO;
577                         break;
578                     /* stereo input */
579                     case DCA_CHANNEL:
580                     case DCA_STEREO:
581                     case DCA_STEREO_SUMDIFF:
582                     case DCA_STEREO_TOTAL:
583                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
584                         break;
585                     /* 3F/2R input */
586                     case DCA_3F2R:
587                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
588                         break;
589                     /* 3F/1R input */
590                     case DCA_3F1R:
591                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
592                         break;
593                     /* other inputs */
594                     case DCA_3F:
595                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F;
596                         break;
597                     case DCA_2F1R:
598                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
599                         break;
600                     case DCA_2F2R:
601                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
602                         break;
603                     case DCA_4F2R:
604                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_4F2R;
605                         break;
606                     /* unknown */
607                     default:
608                         audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
609                 }
610
611                 /* add in our own LFE flag if the source has LFE */
612                 if (flags & DCA_LFE)
613                 {
614                     audio->input_channel_layout = audio->input_channel_layout | HB_INPUT_CH_LAYOUT_HAS_LFE;
615                 }
616
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;
620
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;
623
624                 /* XXX */
625                 if ( (flags & DCA_CHANNEL_MASK) == DCA_DOLBY ) {
626                     sprintf( audio->lang + strlen( audio->lang ),
627                          " (Dolby Surround)" );
628                 } else {
629                     sprintf( audio->lang + strlen( audio->lang ),
630                          " (%d.%d ch)",
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));
634                 }
635
636                 break;
637             }
638         }
639     }
640
641 }
642
643 static int  AllAC3AndDCAOK( hb_title_t * title )
644 {
645     int i;
646     hb_audio_t * audio;
647
648     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
649     {
650         audio = hb_list_item( title->list_audio, i );
651         if( ( audio->codec == HB_ACODEC_AC3 || audio->codec == HB_ACODEC_DCA ) &&
652             !audio->bitrate )
653         {
654             return 0;
655         }
656     }
657
658     return 1;
659 }