OSDN Git Service

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