OSDN Git Service

added call to av_log_set_level in hb_init_real based on value of verbose param.
[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 /**
50  * Registers work objects, by adding the work object to a liked list.
51  * @param w Handle to hb_work_object_t to register.
52  */
53 void hb_register( hb_work_object_t * w )
54 {
55     w->next    = hb_objects;
56     hb_objects = w;
57 }
58
59 /**
60  * libhb initialization routine.
61  * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
62  * @param update_check signals libhb to check for updated version from HandBrake website.
63  * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
64  */
65 hb_handle_t * hb_init_real( int verbose, int update_check )
66 {
67     hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
68     uint64_t      date;
69
70     /* See hb_log() in common.c */
71     if( verbose > HB_DEBUG_NONE )
72     {
73         putenv( "HB_DEBUG=1" );
74                 av_log_set_level(AV_LOG_DEBUG);
75     }
76
77     /* Check for an update on the website if asked to */
78     h->build = -1;
79
80     if( update_check )
81     {
82         hb_log( "hb_init: checking for updates" );
83         date             = hb_get_date();
84         h->update_thread = hb_update_init( &h->build, h->version );
85
86         for( ;; )
87         {
88             if( hb_thread_has_exited( h->update_thread ) )
89             {
90                 /* Immediate success or failure */
91                 hb_thread_close( &h->update_thread );
92                 break;
93             }
94             if( hb_get_date() > date + 1000 )
95             {
96                 /* Still nothing after one second. Connection problem,
97                    let the thread die */
98                 hb_log( "hb_init: connection problem, not waiting for "
99                         "update_thread" );
100                 break;
101             }
102             hb_snooze( 500 );
103         }
104     }
105
106     /* CPU count detection */
107     hb_log( "hb_init: checking cpu count" );
108     h->cpu_count = hb_get_cpu_count();
109
110     h->list_title = hb_list_init();
111     h->jobs       = hb_list_init();
112
113     h->state_lock  = hb_lock_init();
114     h->state.state = HB_STATE_IDLE;
115
116     h->pause_lock = hb_lock_init();
117
118     /* libavcodec */
119     avcodec_init();
120     register_avcodec( &mpeg4_encoder );
121     register_avcodec( &mp2_decoder );
122     register_avcodec( &ac3_encoder );
123
124     /* Start library thread */
125     hb_log( "hb_init: starting libhb thread" );
126     h->die         = 0;
127     h->main_thread = hb_thread_init( "libhb", thread_func, h,
128                                      HB_NORMAL_PRIORITY );
129
130     return h;
131 }
132
133 /**
134  * Returns current version of libhb.
135  * @param h Handle to hb_handle_t.
136  * @return character array of version number.
137  */
138 char * hb_get_version( hb_handle_t * h )
139 {
140     return HB_VERSION;
141 }
142
143 /**
144  * Returns current build of libhb.
145  * @param h Handle to hb_handle_t.
146  * @return character array of build number.
147  */
148 int hb_get_build( hb_handle_t * h )
149 {
150     return HB_BUILD;
151 }
152
153 /**
154  * Checks for needed update.
155  * @param h Handle to hb_handle_t.
156  * @param version Pointer to handle where version will be copied.
157  * @return update indicator.
158  */
159 int hb_check_update( hb_handle_t * h, char ** version )
160 {
161     *version = ( h->build < 0 ) ? NULL : h->version;
162     return h->build;
163 }
164
165 /**
166  * Sets the cpu count to the desired value.
167  * @param h Handle to hb_handle_t
168  * @param cpu_count Number of CPUs to use.
169  */
170 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
171 {
172     cpu_count    = MAX( 1, cpu_count );
173     cpu_count    = MIN( cpu_count, 8 );
174     h->cpu_count = cpu_count;
175 }
176
177 /**
178  * Initializes a scan of the by calling hb_scan_init
179  * @param h Handle to hb_handle_t
180  * @param path location of VIDEO_TS folder.
181  * @param title_index Desired title to scan.  0 for all titles.
182  */
183 void hb_scan( hb_handle_t * h, const char * path, int title_index )
184 {
185     hb_title_t * title;
186
187     /* Clean up from previous scan */
188     while( ( title = hb_list_item( h->list_title, 0 ) ) )
189     {
190         hb_list_rem( h->list_title, title );
191         hb_title_close( &title );
192     }
193     
194     hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
195     h->scan_thread = hb_scan_init( h, path, title_index, h->list_title );
196 }
197
198 /**
199  * Returns the list of titles found.
200  * @param h Handle to hb_handle_t
201  * @return Handle to hb_list_t of the title list.
202  */
203 hb_list_t * hb_get_titles( hb_handle_t * h )
204 {
205     return h->list_title;
206 }
207
208 /**
209  * Create preview image of desired title a index of picture.
210  * @param h Handle to hb_handle_t.
211  * @param title Handle to hb_title_t of desired title.
212  * @param picture Index in title.
213  * @param buffer Handle to buufer were inage will be drawn.
214  */
215 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
216                      uint8_t * buffer )
217 {
218     hb_job_t           * job = title->job;
219     char                 filename[1024];
220     FILE               * file;
221     uint8_t            * buf1, * buf2, * buf3, * buf4, * pen;
222     uint32_t           * p32;
223     AVPicture            pic1, pic2, pic3, pic4;
224     ImgReSampleContext * context;
225     int                  i;
226
227     buf1 = malloc( title->width * title->height * 3 / 2 );
228     buf2 = malloc( title->width * title->height * 3 / 2 );
229     buf3 = malloc( title->width * title->height * 3 / 2 );
230     buf4 = malloc( title->width * title->height * 4 );
231     avpicture_fill( &pic1, buf1, PIX_FMT_YUV420P,
232                     title->width, title->height );
233     avpicture_fill( &pic2, buf2, PIX_FMT_YUV420P,
234                     title->width, title->height );
235     avpicture_fill( &pic3, buf3, PIX_FMT_YUV420P,
236                     job->width, job->height );
237     avpicture_fill( &pic4, buf4, PIX_FMT_RGBA32,
238                     job->width, job->height );
239     
240     memset( filename, 0, 1024 );
241
242     hb_get_tempory_filename( h, filename, "%x%d",
243                              (int) title, picture );
244
245     file = fopen( filename, "r" );
246     if( !file )
247     {
248         hb_log( "hb_get_preview: fopen failed" );
249         return;
250     }
251
252     fread( buf1, title->width * title->height * 3 / 2, 1, file );
253     fclose( file );
254
255     context = img_resample_full_init(
256             job->width, job->height, title->width, title->height,
257             job->crop[0], job->crop[1], job->crop[2], job->crop[3],
258             0, 0, 0, 0 );
259
260     if( job->deinterlace )
261     {
262         avpicture_deinterlace( &pic2, &pic1, PIX_FMT_YUV420P,
263                                title->width, title->height );
264         img_resample( context, &pic3, &pic2 );
265     }
266     else
267     {
268         img_resample( context, &pic3, &pic1 );
269     }
270     img_convert( &pic4, PIX_FMT_RGBA32, &pic3, PIX_FMT_YUV420P,
271                  job->width, job->height );
272
273     /* Gray background */
274     p32 = (uint32_t *) buffer;
275     for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
276     {
277         p32[i] = 0xFF808080;
278     }
279
280     /* Draw the picture, centered, and draw the cropping zone */
281     pen = buffer + ( title->height - job->height ) *
282         ( title->width + 2 ) * 2 + ( title->width - job->width ) * 2;
283     memset( pen, 0xFF, 4 * ( job->width + 2 ) );
284     pen += 4 * ( title->width + 2 );
285     for( i = 0; i < job->height; i++ )
286     {
287         uint8_t * nextLine;
288         nextLine = pen + 4 * ( title->width + 2 );
289         memset( pen, 0xFF, 4 );
290         pen += 4;
291         memcpy( pen, buf4 + 4 * job->width * i, 4 * job->width );
292         pen += 4 * job->width;
293         memset( pen, 0xFF, 4 );
294         pen = nextLine;
295     }
296     memset( pen, 0xFF, 4 * ( job->width + 2 ) );
297
298     free( buf1 );
299     free( buf2 );
300     free( buf3 );
301     free( buf4 );
302 }
303
304 /**
305  * Calculates job width, height, and cropping parameters.
306  * @param job Handle to hb_job_t.
307  * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
308  * @param pixels Maximum desired pixel count.
309  */
310 void hb_set_size( hb_job_t * job, int aspect, int pixels )
311 {
312     hb_title_t * title = job->title;
313
314     int croppedWidth  = title->width - title->crop[2] - title->crop[3];
315     int croppedHeight = title->height - title->crop[0] - title->crop[1];
316     int croppedAspect = title->aspect * title->height * croppedWidth /
317                             croppedHeight / title->width;
318     int addCrop;
319     int i, w, h;
320
321     if( aspect <= 0 )
322     {
323         /* Keep the best possible aspect ratio */
324         aspect = croppedAspect;
325     }
326
327     /* Crop if necessary to obtain the desired ratio */
328     memcpy( job->crop, title->crop, 4 * sizeof( int ) );
329     if( aspect < croppedAspect )
330     {
331         /* Need to crop on the left and right */
332         addCrop = croppedWidth - aspect * croppedHeight * title->width /
333                     title->aspect / title->height;
334         if( addCrop & 3 )
335         {
336             addCrop = ( addCrop + 1 ) / 2;
337             job->crop[2] += addCrop;
338             job->crop[3] += addCrop;
339         }
340         else if( addCrop & 2 )
341         {
342             addCrop /= 2;
343             job->crop[2] += addCrop - 1;
344             job->crop[3] += addCrop + 1;
345         }
346         else
347         {
348             addCrop /= 2;
349             job->crop[2] += addCrop;
350             job->crop[3] += addCrop;
351         }
352     }
353     else if( aspect > croppedAspect )
354     {
355         /* Need to crop on the top and bottom */
356         addCrop = croppedHeight - croppedWidth * title->aspect *
357             title->height / aspect / title->width;
358         if( addCrop & 3 )
359         {
360             addCrop = ( addCrop + 1 ) / 2;
361             job->crop[0] += addCrop;
362             job->crop[1] += addCrop;
363         }
364         else if( addCrop & 2 )
365         {
366             addCrop /= 2;
367             job->crop[0] += addCrop - 1;
368             job->crop[1] += addCrop + 1;
369         }
370         else
371         {
372             addCrop /= 2;
373             job->crop[0] += addCrop;
374             job->crop[1] += addCrop;
375         }
376     }
377
378     /* Compute a resolution from the number of pixels and aspect */
379     for( i = 0;; i++ )
380     {
381         w = 16 * i;
382         h = MULTIPLE_16( w * HB_ASPECT_BASE / aspect );
383         if( w * h > pixels )
384         {
385             break;
386         }
387     }
388     i--;
389     job->width  = 16 * i;
390     job->height = MULTIPLE_16( 16 * i * HB_ASPECT_BASE / aspect );
391 }
392
393 /**
394  * Returns the number of jobs in the queue.
395  * @param h Handle to hb_handle_t.
396  * @return Number of jobs.
397  */
398 int hb_count( hb_handle_t * h )
399 {
400     return hb_list_count( h->jobs );
401 }
402
403 /**
404  * Returns handle to job at index i within the job list.
405  * @param h Handle to hb_handle_t.
406  * @param i Index of job.
407  * @returns Handle to hb_job_t of desired job.
408  */
409 hb_job_t * hb_job( hb_handle_t * h, int i )
410 {
411     return hb_list_item( h->jobs, i );
412 }
413
414 /**
415  * Adds a job to the job list.
416  * @param h Handle to hb_handle_t.
417  * @param job Handle to hb_job_t.
418  */
419 void hb_add( hb_handle_t * h, hb_job_t * job )
420 {
421     hb_job_t      * job_copy;
422     hb_title_t    * title,    * title_copy;
423     hb_chapter_t  * chapter,  * chapter_copy;
424     hb_audio_t    * audio,    * audio_copy;
425     hb_subtitle_t * subtitle, * subtitle_copy;
426     int             i;
427
428     /* Copy the title */
429     title      = job->title;
430     title_copy = malloc( sizeof( hb_title_t ) );
431     memcpy( title_copy, title, sizeof( hb_title_t ) );
432
433     title_copy->list_chapter = hb_list_init();
434     for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
435     {
436         chapter      = hb_list_item( title->list_chapter, i );
437         chapter_copy = malloc( sizeof( hb_chapter_t ) );
438         memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
439         hb_list_add( title_copy->list_chapter, chapter_copy );
440     }
441
442     /* Copy the audio track(s) we want */
443     title_copy->list_audio = hb_list_init();
444
445     /* Do nothing about audio during first pass */
446     if( job->pass != 1 )
447     {
448         for( i = 0; i < 8; i++ )
449         {
450             if( job->audios[i] < 0 )
451             {
452                 break;
453             }
454             if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
455             {
456                 audio_copy = malloc( sizeof( hb_audio_t ) );
457                 memcpy( audio_copy, audio, sizeof( hb_audio_t ) );
458                 hb_list_add( title_copy->list_audio, audio_copy );
459             }
460         }
461     }
462
463     /* Copy the subtitle we want (or not) */
464     title_copy->list_subtitle = hb_list_init();
465     if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) )
466     {
467         subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
468         memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
469         hb_list_add( title_copy->list_subtitle, subtitle_copy );
470     }
471
472     /* Copy the job */
473     job_copy        = calloc( sizeof( hb_job_t ), 1 );
474     memcpy( job_copy, job, sizeof( hb_job_t ) );
475     job_copy->title = title_copy;
476     job_copy->file  = strdup( job->file );
477     job_copy->h     = h;
478     job_copy->pause = h->pause_lock;
479
480     /* Add the job to the list */
481     hb_list_add( h->jobs, job_copy );
482 }
483
484 /**
485  * Removes a job from the job list.
486  * @param h Handle to hb_handle_t.
487  * @param job Handle to hb_job_t.
488  */
489 void hb_rem( hb_handle_t * h, hb_job_t * job )
490 {
491     hb_list_rem( h->jobs, job );
492
493     /* XXX free everything XXX */
494 }
495
496 /**
497  * Starts the conversion process.
498  * Sets state to HB_STATE_WORKING.
499  * calls hb_work_init, to launch work thread. Stores handle to work thread.
500  * @param h Handle to hb_handle_t.
501  */
502 void hb_start( hb_handle_t * h )
503 {
504     /* XXX Hack */
505     h->job_count = hb_list_count( h->jobs );
506
507     hb_lock( h->state_lock );
508     h->state.state = HB_STATE_WORKING;
509 #define p h->state.param.working
510     p.progress  = 0.0;
511     p.job_cur   = 1;
512     p.job_count = h->job_count;
513     p.rate_cur  = 0.0;
514     p.rate_avg  = 0.0;
515     p.hours     = -1;
516     p.minutes   = -1;
517     p.seconds   = -1;
518 #undef p
519     hb_unlock( h->state_lock );
520
521     h->paused = 0;
522
523     h->work_die    = 0;
524     h->work_thread = hb_work_init( h->jobs, h->cpu_count,
525                                    &h->work_die, &h->work_error );
526 }
527
528 /**
529  * Pauses the conversion process.
530  * @param h Handle to hb_handle_t.
531  */
532 void hb_pause( hb_handle_t * h )
533 {
534     if( !h->paused )
535     {
536         hb_lock( h->pause_lock );
537         h->paused = 1;
538
539         hb_lock( h->state_lock );
540         h->state.state = HB_STATE_PAUSED;
541         hb_unlock( h->state_lock );
542     }
543 }
544
545 /**
546  * Resumes the conversion process.
547  * @param h Handle to hb_handle_t.
548  */
549 void hb_resume( hb_handle_t * h )
550 {
551     if( h->paused )
552     {
553         hb_unlock( h->pause_lock );
554         h->paused = 0;
555     }
556 }
557
558 /**
559  * Stops the conversion process.
560  * @param h Handle to hb_handle_t.
561  */
562 void hb_stop( hb_handle_t * h )
563 {
564     h->work_die = 1;
565
566     hb_resume( h );
567 }
568
569 /**
570  * Returns the state of the conversion process.
571  * @param h Handle to hb_handle_t.
572  * @param s Handle to hb_state_t which to copy the state data.
573  */
574 void hb_get_state( hb_handle_t * h, hb_state_t * s )
575 {
576     hb_lock( h->state_lock );
577
578     memcpy( s, &h->state, sizeof( hb_state_t ) );
579     h->state.state = HB_STATE_IDLE;
580
581     hb_unlock( h->state_lock );
582 }
583
584 /**
585  * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init_real.
586  * @param _h Pointer to handle to hb_handle_t.
587  */
588 void hb_close( hb_handle_t ** _h )
589 {
590     hb_handle_t * h = *_h;
591     hb_title_t * title;
592
593     h->die = 1;
594     hb_thread_close( &h->main_thread );
595
596     while( ( title = hb_list_item( h->list_title, 0 ) ) )
597     {
598         hb_list_rem( h->list_title, title );
599         hb_title_close( &title );
600     }
601     hb_list_close( &h->list_title );
602
603     hb_list_close( &h->jobs );
604     hb_lock_close( &h->state_lock );
605     hb_lock_close( &h->pause_lock );
606     free( h );
607     *_h = NULL;
608 }
609
610 /**
611  * Monitors the state of the update, scan, and work threads.
612  * Sets scan done state when scan thread exits.
613  * Sets work done state when work thread exits.
614  * @param _h Handle to hb_handle_t
615  */
616 static void thread_func( void * _h )
617 {
618     hb_handle_t * h = (hb_handle_t *) _h;
619     char dirname[1024];
620     DIR * dir;
621     struct dirent * entry;
622
623     h->pid = getpid();
624
625     /* Create folder for temporary files */
626     memset( dirname, 0, 1024 );
627     hb_get_tempory_directory( h, dirname );
628
629     hb_mkdir( dirname );
630
631     while( !h->die )
632     {
633         /* In case the check_update thread hangs, it'll die sooner or
634            later. Then, we join it here */
635         if( h->update_thread &&
636             hb_thread_has_exited( h->update_thread ) )
637         {
638             hb_thread_close( &h->update_thread );
639         }
640
641         /* Check if the scan thread is done */
642         if( h->scan_thread &&
643             hb_thread_has_exited( h->scan_thread ) )
644         {
645             hb_thread_close( &h->scan_thread );
646
647             hb_log( "libhb: scan thread found %d valid title(s)",
648                     hb_list_count( h->list_title ) );
649             hb_lock( h->state_lock );
650             h->state.state = HB_STATE_SCANDONE;
651             hb_unlock( h->state_lock );
652         }
653
654         /* Check if the work thread is done */
655         if( h->work_thread &&
656             hb_thread_has_exited( h->work_thread ) )
657         {
658             hb_thread_close( &h->work_thread );
659
660             hb_log( "libhb: work result = %d",
661                     h->work_error );
662             hb_lock( h->state_lock );
663             h->state.state                = HB_STATE_WORKDONE;
664             h->state.param.workdone.error = h->work_error;
665             hb_unlock( h->state_lock );
666         }
667
668         hb_snooze( 50 );
669     }
670
671     if( h->work_thread )
672     {
673         hb_stop( h );
674         hb_thread_close( &h->work_thread );
675     }
676
677     /* Remove temp folder */
678     dir = opendir( dirname );
679     while( ( entry = readdir( dir ) ) )
680     {
681         char filename[1024];
682         if( entry->d_name[0] == '.' )
683         {
684             continue;
685         }
686         memset( filename, 0, 1024 );
687         snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
688         unlink( filename );
689     }
690     closedir( dir );
691     rmdir( dirname );
692 }
693
694 /**
695  * Returns the PID.
696  * @param h Handle to hb_handle_t
697  */
698 int hb_get_pid( hb_handle_t * h )
699 {
700     return h->pid;
701 }
702
703 /**
704  * Sets the current state.
705  * @param h Handle to hb_handle_t
706  * @param s Handle to new hb_state_t
707  */
708 void hb_set_state( hb_handle_t * h, hb_state_t * s )
709 {
710     hb_lock( h->pause_lock );
711     hb_lock( h->state_lock );
712     memcpy( &h->state, s, sizeof( hb_state_t ) );
713     if( h->state.state == HB_STATE_WORKING )
714     {
715         /* XXX Hack */
716         h->state.param.working.job_cur =
717             h->job_count - hb_list_count( h->jobs );
718         h->state.param.working.job_count = h->job_count;
719     }
720     hb_unlock( h->state_lock );
721     hb_unlock( h->pause_lock );
722 }