OSDN Git Service

added Doxyfile, config file for doxygen
[handbrake-jp/handbrake-jp-git.git] / libhb / hb.c
1 /* $Id: hb.c,v 1.43 2005/04/27 21:05:24 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
9 #include "ffmpeg/avcodec.h"
10
11 struct hb_handle_s
12 {
13     /* The "Check for update" thread */
14     int            build;
15     char           version[16];
16     hb_thread_t  * update_thread;
17
18     /* This thread's only purpose is to check other threads'
19        states */
20     volatile int   die;
21     hb_thread_t  * main_thread;
22     int            pid;
23
24     /* DVD/file scan thread */
25     hb_list_t    * list_title;
26     hb_thread_t  * scan_thread;
27
28     /* The thread which processes the jobs. Others threads are launched
29        from this one (see work.c) */
30     hb_list_t    * jobs;
31     int            job_count;
32     volatile int   work_die;
33     int            work_error;
34     hb_thread_t  * work_thread;
35
36     int            cpu_count;
37
38     hb_lock_t    * state_lock;
39     hb_state_t     state;
40
41     int            paused;
42     hb_lock_t    * pause_lock;
43 };
44
45 hb_work_object_t * hb_objects = NULL;
46
47 static void thread_func( void * );
48
49 void hb_register( hb_work_object_t * w )
50 {
51     w->next    = hb_objects;
52     hb_objects = w;
53 }
54
55 hb_handle_t * hb_init_real( int verbose, int update_check )
56 {
57     hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
58     uint64_t      date;
59
60     /* See hb_log() in common.c */
61     if( verbose > HB_DEBUG_NONE )
62     {
63         putenv( "HB_DEBUG=1" );
64     }
65
66     /* Check for an update on the website if asked to */
67     h->build = -1;
68
69     if( update_check )
70     {
71         hb_log( "hb_init: checking for updates" );
72         date             = hb_get_date();
73         h->update_thread = hb_update_init( &h->build, h->version );
74
75         for( ;; )
76         {
77             if( hb_thread_has_exited( h->update_thread ) )
78             {
79                 /* Immediate success or failure */
80                 hb_thread_close( &h->update_thread );
81                 break;
82             }
83             if( hb_get_date() > date + 1000 )
84             {
85                 /* Still nothing after one second. Connection problem,
86                    let the thread die */
87                 hb_log( "hb_init: connection problem, not waiting for "
88                         "update_thread" );
89                 break;
90             }
91             hb_snooze( 50 );
92         }
93     }
94
95     /* CPU count detection */
96     hb_log( "hb_init: checking cpu count" );
97     h->cpu_count = hb_get_cpu_count();
98
99     h->list_title = hb_list_init();
100     h->jobs       = hb_list_init();
101
102     h->state_lock  = hb_lock_init();
103     h->state.state = HB_STATE_IDLE;
104
105     h->pause_lock = hb_lock_init();
106
107     /* libavcodec */
108     avcodec_init();
109     register_avcodec( &mpeg4_encoder );
110     register_avcodec( &mp2_decoder );
111     register_avcodec( &ac3_encoder );
112
113     /* Start library thread */
114     hb_log( "hb_init: starting libhb thread" );
115     h->die         = 0;
116     h->main_thread = hb_thread_init( "libhb", thread_func, h,
117                                      HB_NORMAL_PRIORITY );
118
119     return h;
120 }
121
122 char * hb_get_version( hb_handle_t * h )
123 {
124     return "0.7.1a2";//HB_VERSION;
125 }
126
127 int hb_get_build( hb_handle_t * h )
128 {
129     return HB_BUILD;
130 }
131
132 int hb_check_update( hb_handle_t * h, char ** version )
133 {
134     *version = ( h->build < 0 ) ? NULL : h->version;
135     return h->build;
136 }
137
138 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
139 {
140     cpu_count    = MAX( 1, cpu_count );
141     cpu_count    = MIN( cpu_count, 8 );
142     h->cpu_count = cpu_count;
143 }
144
145 void hb_scan( hb_handle_t * h, const char * path, int title_index )
146 {
147     hb_title_t * title;
148
149     /* Clean up from previous scan */
150     while( ( title = hb_list_item( h->list_title, 0 ) ) )
151     {
152         hb_list_rem( h->list_title, title );
153         hb_title_close( &title );
154     }
155     
156     hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
157     h->scan_thread = hb_scan_init( h, path, title_index, h->list_title );
158 }
159
160 hb_list_t * hb_get_titles( hb_handle_t * h )
161 {
162     return h->list_title;
163 }
164
165 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
166                      uint8_t * buffer )
167 {
168     hb_job_t           * job = title->job;
169     char                 filename[1024];
170     FILE               * file;
171     uint8_t            * buf1, * buf2, * buf3, * buf4, * pen;
172     uint32_t           * p32;
173     AVPicture            pic1, pic2, pic3, pic4;
174     ImgReSampleContext * context;
175     int                  i;
176
177     buf1 = malloc( title->width * title->height * 3 / 2 );
178     buf2 = malloc( title->width * title->height * 3 / 2 );
179     buf3 = malloc( title->width * title->height * 3 / 2 );
180     buf4 = malloc( title->width * title->height * 4 );
181     avpicture_fill( &pic1, buf1, PIX_FMT_YUV420P,
182                     title->width, title->height );
183     avpicture_fill( &pic2, buf2, PIX_FMT_YUV420P,
184                     title->width, title->height );
185     avpicture_fill( &pic3, buf3, PIX_FMT_YUV420P,
186                     job->width, job->height );
187     avpicture_fill( &pic4, buf4, PIX_FMT_RGBA32,
188                     job->width, job->height );
189     
190     memset( filename, 0, 1024 );
191
192     hb_get_tempory_filename( h, filename, "%x%d",
193                              (int) title, picture );
194
195     file = fopen( filename, "r" );
196     if( !file )
197     {
198         hb_log( "hb_get_preview: fopen failed" );
199         return;
200     }
201
202     fread( buf1, title->width * title->height * 3 / 2, 1, file );
203     fclose( file );
204
205     context = img_resample_full_init(
206             job->width, job->height, title->width, title->height,
207             job->crop[0], job->crop[1], job->crop[2], job->crop[3],
208             0, 0, 0, 0 );
209
210     if( job->deinterlace )
211     {
212         avpicture_deinterlace( &pic2, &pic1, PIX_FMT_YUV420P,
213                                title->width, title->height );
214         img_resample( context, &pic3, &pic2 );
215     }
216     else
217     {
218         img_resample( context, &pic3, &pic1 );
219     }
220     img_convert( &pic4, PIX_FMT_RGBA32, &pic3, PIX_FMT_YUV420P,
221                  job->width, job->height );
222
223     /* Gray background */
224     p32 = (uint32_t *) buffer;
225     for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
226     {
227         p32[i] = 0xFF808080;
228     }
229
230     /* Draw the picture, centered, and draw the cropping zone */
231     pen = buffer + ( title->height - job->height ) *
232         ( title->width + 2 ) * 2 + ( title->width - job->width ) * 2;
233     memset( pen, 0xFF, 4 * ( job->width + 2 ) );
234     pen += 4 * ( title->width + 2 );
235     for( i = 0; i < job->height; i++ )
236     {
237         uint8_t * nextLine;
238         nextLine = pen + 4 * ( title->width + 2 );
239         memset( pen, 0xFF, 4 );
240         pen += 4;
241         memcpy( pen, buf4 + 4 * job->width * i, 4 * job->width );
242         pen += 4 * job->width;
243         memset( pen, 0xFF, 4 );
244         pen = nextLine;
245     }
246     memset( pen, 0xFF, 4 * ( job->width + 2 ) );
247
248     free( buf1 );
249     free( buf2 );
250     free( buf3 );
251     free( buf4 );
252 }
253
254 void hb_set_size( hb_job_t * job, int aspect, int pixels )
255 {
256     hb_title_t * title = job->title;
257
258     int croppedWidth  = title->width - title->crop[2] - title->crop[3];
259     int croppedHeight = title->height - title->crop[0] - title->crop[1];
260     int croppedAspect = title->aspect * title->height * croppedWidth /
261                             croppedHeight / title->width;
262     int addCrop;
263     int i, w, h;
264
265     if( aspect <= 0 )
266     {
267         /* Keep the best possible aspect ratio */
268         aspect = croppedAspect;
269     }
270
271     /* Crop if necessary to obtain the desired ratio */
272     memcpy( job->crop, title->crop, 4 * sizeof( int ) );
273     if( aspect < croppedAspect )
274     {
275         /* Need to crop on the left and right */
276         addCrop = croppedWidth - aspect * croppedHeight * title->width /
277                     title->aspect / title->height;
278         if( addCrop & 3 )
279         {
280             addCrop = ( addCrop + 1 ) / 2;
281             job->crop[2] += addCrop;
282             job->crop[3] += addCrop;
283         }
284         else if( addCrop & 2 )
285         {
286             addCrop /= 2;
287             job->crop[2] += addCrop - 1;
288             job->crop[3] += addCrop + 1;
289         }
290         else
291         {
292             addCrop /= 2;
293             job->crop[2] += addCrop;
294             job->crop[3] += addCrop;
295         }
296     }
297     else if( aspect > croppedAspect )
298     {
299         /* Need to crop on the top and bottom */
300         addCrop = croppedHeight - croppedWidth * title->aspect *
301             title->height / aspect / title->width;
302         if( addCrop & 3 )
303         {
304             addCrop = ( addCrop + 1 ) / 2;
305             job->crop[0] += addCrop;
306             job->crop[1] += addCrop;
307         }
308         else if( addCrop & 2 )
309         {
310             addCrop /= 2;
311             job->crop[0] += addCrop - 1;
312             job->crop[1] += addCrop + 1;
313         }
314         else
315         {
316             addCrop /= 2;
317             job->crop[0] += addCrop;
318             job->crop[1] += addCrop;
319         }
320     }
321
322     /* Compute a resolution from the number of pixels and aspect */
323     for( i = 0;; i++ )
324     {
325         w = 16 * i;
326         h = MULTIPLE_16( w * HB_ASPECT_BASE / aspect );
327         if( w * h > pixels )
328         {
329             break;
330         }
331     }
332     i--;
333     job->width  = 16 * i;
334     job->height = MULTIPLE_16( 16 * i * HB_ASPECT_BASE / aspect );
335 }
336
337 int hb_count( hb_handle_t * h )
338 {
339     return hb_list_count( h->jobs );
340 }
341
342 hb_job_t * hb_job( hb_handle_t * h, int i )
343 {
344     return hb_list_item( h->jobs, i );
345 }
346
347 /* hb_add: memcpy() party. That's ugly, for if someone has a better
348    idea... */
349 void hb_add( hb_handle_t * h, hb_job_t * job )
350 {
351     hb_job_t      * job_copy;
352     hb_title_t    * title,    * title_copy;
353     hb_chapter_t  * chapter,  * chapter_copy;
354     hb_audio_t    * audio,    * audio_copy;
355     hb_subtitle_t * subtitle, * subtitle_copy;
356     int             i;
357
358     /* Copy the title */
359     title      = job->title;
360     title_copy = malloc( sizeof( hb_title_t ) );
361     memcpy( title_copy, title, sizeof( hb_title_t ) );
362
363     title_copy->list_chapter = hb_list_init();
364     for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
365     {
366         chapter      = hb_list_item( title->list_chapter, i );
367         chapter_copy = malloc( sizeof( hb_chapter_t ) );
368         memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
369         hb_list_add( title_copy->list_chapter, chapter_copy );
370     }
371
372     /* Copy the audio track(s) we want */
373     title_copy->list_audio = hb_list_init();
374
375     /* Do nothing about audio during first pass */
376     if( job->pass != 1 )
377     {
378         for( i = 0; i < 8; i++ )
379         {
380             if( job->audios[i] < 0 )
381             {
382                 break;
383             }
384             if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
385             {
386                 audio_copy = malloc( sizeof( hb_audio_t ) );
387                 memcpy( audio_copy, audio, sizeof( hb_audio_t ) );
388                 hb_list_add( title_copy->list_audio, audio_copy );
389             }
390         }
391     }
392
393     /* Copy the subtitle we want (or not) */
394     title_copy->list_subtitle = hb_list_init();
395     if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) )
396     {
397         subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
398         memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
399         hb_list_add( title_copy->list_subtitle, subtitle_copy );
400     }
401
402     /* Copy the job */
403     job_copy        = calloc( sizeof( hb_job_t ), 1 );
404     memcpy( job_copy, job, sizeof( hb_job_t ) );
405     job_copy->title = title_copy;
406     job_copy->file  = strdup( job->file );
407     job_copy->h     = h;
408     job_copy->pause = h->pause_lock;
409
410     /* Add the job to the list */
411     hb_list_add( h->jobs, job_copy );
412 }
413
414 void hb_rem( hb_handle_t * h, hb_job_t * job )
415 {
416     hb_list_rem( h->jobs, job );
417
418     /* XXX free everything XXX */
419 }
420
421 void hb_start( hb_handle_t * h )
422 {
423     /* XXX Hack */
424     h->job_count = hb_list_count( h->jobs );
425
426     hb_lock( h->state_lock );
427     h->state.state = HB_STATE_WORKING;
428 #define p h->state.param.working
429     p.progress  = 0.0;
430     p.job_cur   = 1;
431     p.job_count = h->job_count;
432     p.rate_cur  = 0.0;
433     p.rate_avg  = 0.0;
434     p.hours     = -1;
435     p.minutes   = -1;
436     p.seconds   = -1;
437 #undef p
438     hb_unlock( h->state_lock );
439
440     h->paused = 0;
441
442     h->work_die    = 0;
443     h->work_thread = hb_work_init( h->jobs, h->cpu_count,
444                                    &h->work_die, &h->work_error );
445 }
446
447 void hb_pause( hb_handle_t * h )
448 {
449     if( !h->paused )
450     {
451         hb_lock( h->pause_lock );
452         h->paused = 1;
453
454         hb_lock( h->state_lock );
455         h->state.state = HB_STATE_PAUSED;
456         hb_unlock( h->state_lock );
457     }
458 }
459
460 void hb_resume( hb_handle_t * h )
461 {
462     if( h->paused )
463     {
464         hb_unlock( h->pause_lock );
465         h->paused = 0;
466     }
467 }
468
469 void hb_stop( hb_handle_t * h )
470 {
471     h->work_die = 1;
472
473     hb_resume( h );
474 }
475
476 void hb_get_state( hb_handle_t * h, hb_state_t * s )
477 {
478     hb_lock( h->state_lock );
479
480     memcpy( s, &h->state, sizeof( hb_state_t ) );
481     h->state.state = HB_STATE_IDLE;
482
483     hb_unlock( h->state_lock );
484 }
485
486 void hb_close( hb_handle_t ** _h )
487 {
488     hb_handle_t * h = *_h;
489     hb_title_t * title;
490
491     h->die = 1;
492     hb_thread_close( &h->main_thread );
493
494     while( ( title = hb_list_item( h->list_title, 0 ) ) )
495     {
496         hb_list_rem( h->list_title, title );
497         hb_title_close( &title );
498     }
499     hb_list_close( &h->list_title );
500
501     hb_list_close( &h->jobs );
502     hb_lock_close( &h->state_lock );
503     hb_lock_close( &h->pause_lock );
504     free( h );
505     *_h = NULL;
506 }
507
508 static void thread_func( void * _h )
509 {
510     hb_handle_t * h = (hb_handle_t *) _h;
511     char dirname[1024];
512     DIR * dir;
513     struct dirent * entry;
514
515     h->pid = getpid();
516
517     /* Create folder for temporary files */
518     memset( dirname, 0, 1024 );
519     hb_get_tempory_directory( h, dirname );
520
521     hb_mkdir( dirname );
522
523     while( !h->die )
524     {
525         /* In case the check_update thread hangs, it'll die sooner or
526            later. Then, we join it here */
527         if( h->update_thread &&
528             hb_thread_has_exited( h->update_thread ) )
529         {
530             hb_thread_close( &h->update_thread );
531         }
532
533         /* Check if the scan thread is done */
534         if( h->scan_thread &&
535             hb_thread_has_exited( h->scan_thread ) )
536         {
537             hb_thread_close( &h->scan_thread );
538
539             hb_log( "libhb: scan thread found %d valid title(s)",
540                     hb_list_count( h->list_title ) );
541             hb_lock( h->state_lock );
542             h->state.state = HB_STATE_SCANDONE;
543             hb_unlock( h->state_lock );
544         }
545
546         /* Check if the work thread is done */
547         if( h->work_thread &&
548             hb_thread_has_exited( h->work_thread ) )
549         {
550             hb_thread_close( &h->work_thread );
551
552             hb_log( "libhb: work result = %d",
553                     h->work_error );
554             hb_lock( h->state_lock );
555             h->state.state                = HB_STATE_WORKDONE;
556             h->state.param.workdone.error = h->work_error;
557             hb_unlock( h->state_lock );
558         }
559
560         hb_snooze( 50 );
561     }
562
563     if( h->work_thread )
564     {
565         hb_stop( h );
566         hb_thread_close( &h->work_thread );
567     }
568
569     /* Remove temp folder */
570     dir = opendir( dirname );
571     while( ( entry = readdir( dir ) ) )
572     {
573         char filename[1024];
574         if( entry->d_name[0] == '.' )
575         {
576             continue;
577         }
578         memset( filename, 0, 1024 );
579         snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
580         unlink( filename );
581     }
582     closedir( dir );
583     rmdir( dirname );
584 }
585
586 int hb_get_pid( hb_handle_t * h )
587 {
588     return h->pid;
589 }
590
591 void hb_set_state( hb_handle_t * h, hb_state_t * s )
592 {
593     hb_lock( h->pause_lock );
594     hb_lock( h->state_lock );
595     memcpy( &h->state, s, sizeof( hb_state_t ) );
596     if( h->state.state == HB_STATE_WORKING )
597     {
598         /* XXX Hack */
599         h->state.param.working.job_cur =
600             h->job_count - hb_list_count( h->jobs );
601         h->state.param.working.job_count = h->job_count;
602     }
603     hb_unlock( h->state_lock );
604     hb_unlock( h->pause_lock );
605 }