1 /* $Id: hb.c,v 1.43 2005/04/27 21:05:24 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. */
9 #include "ffmpeg/avcodec.h"
13 /* The "Check for update" thread */
16 hb_thread_t * update_thread;
18 /* This thread's only purpose is to check other threads'
21 hb_thread_t * main_thread;
24 /* DVD/file scan thread */
25 hb_list_t * list_title;
26 hb_thread_t * scan_thread;
28 /* The thread which processes the jobs. Others threads are launched
29 from this one (see work.c) */
32 volatile int work_die;
34 hb_thread_t * work_thread;
38 hb_lock_t * state_lock;
42 hb_lock_t * pause_lock;
45 hb_work_object_t * hb_objects = NULL;
47 static void thread_func( void * );
50 * Registers work objects, by adding the work object to a liked list.
51 * @param w Handle to hb_work_object_t to register.
53 void hb_register( hb_work_object_t * w )
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.
65 hb_handle_t * hb_init_real( int verbose, int update_check )
67 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
70 /* See hb_log() in common.c */
71 if( verbose > HB_DEBUG_NONE )
73 putenv( "HB_DEBUG=1" );
74 av_log_set_level(AV_LOG_DEBUG);
77 /* Check for an update on the website if asked to */
82 hb_log( "hb_init: checking for updates" );
84 h->update_thread = hb_update_init( &h->build, h->version );
88 if( hb_thread_has_exited( h->update_thread ) )
90 /* Immediate success or failure */
91 hb_thread_close( &h->update_thread );
94 if( hb_get_date() > date + 1000 )
96 /* Still nothing after one second. Connection problem,
98 hb_log( "hb_init: connection problem, not waiting for "
106 /* CPU count detection */
107 hb_log( "hb_init: checking cpu count" );
108 h->cpu_count = hb_get_cpu_count();
110 h->list_title = hb_list_init();
111 h->jobs = hb_list_init();
113 h->state_lock = hb_lock_init();
114 h->state.state = HB_STATE_IDLE;
116 h->pause_lock = hb_lock_init();
120 register_avcodec( &mpeg4_encoder );
121 register_avcodec( &mp2_decoder );
122 register_avcodec( &ac3_encoder );
124 /* Start library thread */
125 hb_log( "hb_init: starting libhb thread" );
127 h->main_thread = hb_thread_init( "libhb", thread_func, h,
128 HB_NORMAL_PRIORITY );
134 * Returns current version of libhb.
135 * @param h Handle to hb_handle_t.
136 * @return character array of version number.
138 char * hb_get_version( hb_handle_t * h )
144 * Returns current build of libhb.
145 * @param h Handle to hb_handle_t.
146 * @return character array of build number.
148 int hb_get_build( hb_handle_t * h )
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.
159 int hb_check_update( hb_handle_t * h, char ** version )
161 *version = ( h->build < 0 ) ? NULL : h->version;
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.
170 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
172 cpu_count = MAX( 1, cpu_count );
173 cpu_count = MIN( cpu_count, 8 );
174 h->cpu_count = cpu_count;
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.
183 void hb_scan( hb_handle_t * h, const char * path, int title_index )
187 /* Clean up from previous scan */
188 while( ( title = hb_list_item( h->list_title, 0 ) ) )
190 hb_list_rem( h->list_title, title );
191 hb_title_close( &title );
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 );
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.
203 hb_list_t * hb_get_titles( hb_handle_t * h )
205 return h->list_title;
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.
215 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
218 hb_job_t * job = title->job;
221 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
223 AVPicture pic1, pic2, pic3, pic4;
224 ImgReSampleContext * context;
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 );
240 memset( filename, 0, 1024 );
242 hb_get_tempory_filename( h, filename, "%x%d",
243 (int) title, picture );
245 file = fopen( filename, "r" );
248 hb_log( "hb_get_preview: fopen failed" );
252 fread( buf1, title->width * title->height * 3 / 2, 1, file );
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],
260 if( job->deinterlace )
262 avpicture_deinterlace( &pic2, &pic1, PIX_FMT_YUV420P,
263 title->width, title->height );
264 img_resample( context, &pic3, &pic2 );
268 img_resample( context, &pic3, &pic1 );
270 img_convert( &pic4, PIX_FMT_RGBA32, &pic3, PIX_FMT_YUV420P,
271 job->width, job->height );
273 /* Gray background */
274 p32 = (uint32_t *) buffer;
275 for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
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++ )
288 nextLine = pen + 4 * ( title->width + 2 );
289 memset( pen, 0xFF, 4 );
291 memcpy( pen, buf4 + 4 * job->width * i, 4 * job->width );
292 pen += 4 * job->width;
293 memset( pen, 0xFF, 4 );
296 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
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.
310 void hb_set_size( hb_job_t * job, int aspect, int pixels )
312 hb_title_t * title = job->title;
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;
323 /* Keep the best possible aspect ratio */
324 aspect = croppedAspect;
327 /* Crop if necessary to obtain the desired ratio */
328 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
329 if( aspect < croppedAspect )
331 /* Need to crop on the left and right */
332 addCrop = croppedWidth - aspect * croppedHeight * title->width /
333 title->aspect / title->height;
336 addCrop = ( addCrop + 1 ) / 2;
337 job->crop[2] += addCrop;
338 job->crop[3] += addCrop;
340 else if( addCrop & 2 )
343 job->crop[2] += addCrop - 1;
344 job->crop[3] += addCrop + 1;
349 job->crop[2] += addCrop;
350 job->crop[3] += addCrop;
353 else if( aspect > croppedAspect )
355 /* Need to crop on the top and bottom */
356 addCrop = croppedHeight - croppedWidth * title->aspect *
357 title->height / aspect / title->width;
360 addCrop = ( addCrop + 1 ) / 2;
361 job->crop[0] += addCrop;
362 job->crop[1] += addCrop;
364 else if( addCrop & 2 )
367 job->crop[0] += addCrop - 1;
368 job->crop[1] += addCrop + 1;
373 job->crop[0] += addCrop;
374 job->crop[1] += addCrop;
378 /* Compute a resolution from the number of pixels and aspect */
382 h = MULTIPLE_16( w * HB_ASPECT_BASE / aspect );
390 job->height = MULTIPLE_16( 16 * i * HB_ASPECT_BASE / aspect );
394 * Returns the number of jobs in the queue.
395 * @param h Handle to hb_handle_t.
396 * @return Number of jobs.
398 int hb_count( hb_handle_t * h )
400 return hb_list_count( h->jobs );
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.
409 hb_job_t * hb_job( hb_handle_t * h, int i )
411 return hb_list_item( h->jobs, i );
415 * Adds a job to the job list.
416 * @param h Handle to hb_handle_t.
417 * @param job Handle to hb_job_t.
419 void hb_add( hb_handle_t * h, hb_job_t * job )
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;
430 title_copy = malloc( sizeof( hb_title_t ) );
431 memcpy( title_copy, title, sizeof( hb_title_t ) );
433 title_copy->list_chapter = hb_list_init();
434 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
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 );
442 /* Copy the audio track(s) we want */
443 title_copy->list_audio = hb_list_init();
445 /* Do nothing about audio during first pass */
448 for( i = 0; i < 8; i++ )
450 if( job->audios[i] < 0 )
454 if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
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 );
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 ) ) )
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 );
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 );
478 job_copy->pause = h->pause_lock;
480 /* Add the job to the list */
481 hb_list_add( h->jobs, job_copy );
485 * Removes a job from the job list.
486 * @param h Handle to hb_handle_t.
487 * @param job Handle to hb_job_t.
489 void hb_rem( hb_handle_t * h, hb_job_t * job )
491 hb_list_rem( h->jobs, job );
493 /* XXX free everything XXX */
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.
502 void hb_start( hb_handle_t * h )
505 h->job_count = hb_list_count( h->jobs );
507 hb_lock( h->state_lock );
508 h->state.state = HB_STATE_WORKING;
509 #define p h->state.param.working
512 p.job_count = h->job_count;
519 hb_unlock( h->state_lock );
524 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
525 &h->work_die, &h->work_error );
529 * Pauses the conversion process.
530 * @param h Handle to hb_handle_t.
532 void hb_pause( hb_handle_t * h )
536 hb_lock( h->pause_lock );
539 hb_lock( h->state_lock );
540 h->state.state = HB_STATE_PAUSED;
541 hb_unlock( h->state_lock );
546 * Resumes the conversion process.
547 * @param h Handle to hb_handle_t.
549 void hb_resume( hb_handle_t * h )
553 hb_unlock( h->pause_lock );
559 * Stops the conversion process.
560 * @param h Handle to hb_handle_t.
562 void hb_stop( hb_handle_t * h )
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.
574 void hb_get_state( hb_handle_t * h, hb_state_t * s )
576 hb_lock( h->state_lock );
578 memcpy( s, &h->state, sizeof( hb_state_t ) );
579 h->state.state = HB_STATE_IDLE;
581 hb_unlock( h->state_lock );
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.
588 void hb_close( hb_handle_t ** _h )
590 hb_handle_t * h = *_h;
594 hb_thread_close( &h->main_thread );
596 while( ( title = hb_list_item( h->list_title, 0 ) ) )
598 hb_list_rem( h->list_title, title );
599 hb_title_close( &title );
601 hb_list_close( &h->list_title );
603 hb_list_close( &h->jobs );
604 hb_lock_close( &h->state_lock );
605 hb_lock_close( &h->pause_lock );
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
616 static void thread_func( void * _h )
618 hb_handle_t * h = (hb_handle_t *) _h;
621 struct dirent * entry;
625 /* Create folder for temporary files */
626 memset( dirname, 0, 1024 );
627 hb_get_tempory_directory( h, dirname );
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 ) )
638 hb_thread_close( &h->update_thread );
641 /* Check if the scan thread is done */
642 if( h->scan_thread &&
643 hb_thread_has_exited( h->scan_thread ) )
645 hb_thread_close( &h->scan_thread );
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 );
654 /* Check if the work thread is done */
655 if( h->work_thread &&
656 hb_thread_has_exited( h->work_thread ) )
658 hb_thread_close( &h->work_thread );
660 hb_log( "libhb: work result = %d",
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 );
674 hb_thread_close( &h->work_thread );
677 /* Remove temp folder */
678 dir = opendir( dirname );
679 while( ( entry = readdir( dir ) ) )
682 if( entry->d_name[0] == '.' )
686 memset( filename, 0, 1024 );
687 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
696 * @param h Handle to hb_handle_t
698 int hb_get_pid( hb_handle_t * h )
704 * Sets the current state.
705 * @param h Handle to hb_handle_t
706 * @param s Handle to new hb_state_t
708 void hb_set_state( hb_handle_t * h, hb_state_t * s )
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 )
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;
720 hb_unlock( h->state_lock );
721 hb_unlock( h->pause_lock );