3 #include "ffmpeg/avcodec.h"
7 /* The "Check for update" thread */
10 hb_thread_t * update_thread;
12 /* This thread's only purpose is to check other threads'
15 hb_thread_t * main_thread;
18 /* DVD/file scan thread */
19 hb_list_t * list_title;
20 hb_thread_t * scan_thread;
22 /* The thread which processes the jobs. Others threads are launched
23 from this one (see work.c) */
26 volatile int work_die;
28 hb_thread_t * work_thread;
32 hb_lock_t * state_lock;
36 hb_lock_t * pause_lock;
39 hb_work_object_t * hb_objects = NULL;
41 static void thread_func( void * );
44 * Registers work objects, by adding the work object to a liked list.
45 * @param w Handle to hb_work_object_t to register.
47 void hb_register( hb_work_object_t * w )
54 * libhb initialization routine.
55 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
56 * @param update_check signals libhb to check for updated version from HandBrake website.
57 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
59 hb_handle_t * hb_init_real( int verbose, int update_check )
61 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
64 /* See hb_log() in common.c */
65 if( verbose > HB_DEBUG_NONE )
67 putenv( "HB_DEBUG=1" );
68 av_log_set_level(AV_LOG_DEBUG);
71 /* Check for an update on the website if asked to */
76 hb_log( "hb_init: checking for updates" );
78 h->update_thread = hb_update_init( &h->build, h->version );
82 if( hb_thread_has_exited( h->update_thread ) )
84 /* Immediate success or failure */
85 hb_thread_close( &h->update_thread );
88 if( hb_get_date() > date + 1000 )
90 /* Still nothing after one second. Connection problem,
92 hb_log( "hb_init: connection problem, not waiting for "
100 /* CPU count detection */
101 hb_log( "hb_init: checking cpu count" );
102 h->cpu_count = hb_get_cpu_count();
104 h->list_title = hb_list_init();
105 h->jobs = hb_list_init();
107 h->state_lock = hb_lock_init();
108 h->state.state = HB_STATE_IDLE;
110 h->pause_lock = hb_lock_init();
114 register_avcodec( &mpeg4_encoder );
115 register_avcodec( &mp2_decoder );
116 register_avcodec( &ac3_encoder );
118 /* Start library thread */
119 hb_log( "hb_init: starting libhb thread" );
121 h->main_thread = hb_thread_init( "libhb", thread_func, h,
122 HB_NORMAL_PRIORITY );
128 * libhb initialization routine.
129 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
130 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
131 * @param update_check signals libhb to check for updated version from HandBrake website.
132 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
134 hb_handle_t * hb_init_dl( int verbose, int update_check )
136 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
139 /* See hb_log() in common.c */
140 if( verbose > HB_DEBUG_NONE )
142 putenv( "HB_DEBUG=1" );
143 av_log_set_level(AV_LOG_DEBUG);
146 /* Check for an update on the website if asked to */
151 hb_log( "hb_init: checking for updates" );
152 date = hb_get_date();
153 h->update_thread = hb_update_init( &h->build, h->version );
157 if( hb_thread_has_exited( h->update_thread ) )
159 /* Immediate success or failure */
160 hb_thread_close( &h->update_thread );
163 if( hb_get_date() > date + 1000 )
165 /* Still nothing after one second. Connection problem,
166 let the thread die */
167 hb_log( "hb_init: connection problem, not waiting for "
175 /* CPU count detection */
176 hb_log( "hb_init: checking cpu count" );
177 h->cpu_count = hb_get_cpu_count();
179 h->list_title = hb_list_init();
180 h->jobs = hb_list_init();
182 h->state_lock = hb_lock_init();
183 h->state.state = HB_STATE_IDLE;
185 h->pause_lock = hb_lock_init();
189 register_avcodec( &mpeg4_encoder );
190 register_avcodec( &mp2_decoder );
191 register_avcodec( &ac3_encoder );
193 /* Start library thread */
194 hb_log( "hb_init: starting libhb thread" );
196 h->main_thread = hb_thread_init( "libhb", thread_func, h,
197 HB_NORMAL_PRIORITY );
199 hb_register( &hb_sync );
200 hb_register( &hb_decmpeg2 );
201 hb_register( &hb_decsub );
202 hb_register( &hb_render );
203 hb_register( &hb_encavcodec );
204 hb_register( &hb_encxvid );
205 hb_register( &hb_encx264 );
206 hb_register( &hb_deca52 );
207 hb_register( &hb_decavcodec );
208 hb_register( &hb_declpcm );
209 hb_register( &hb_encfaac );
210 hb_register( &hb_enclame );
211 hb_register( &hb_encvorbis );
218 * Returns current version of libhb.
219 * @param h Handle to hb_handle_t.
220 * @return character array of version number.
222 char * hb_get_version( hb_handle_t * h )
228 * Returns current build of libhb.
229 * @param h Handle to hb_handle_t.
230 * @return character array of build number.
232 int hb_get_build( hb_handle_t * h )
238 * Checks for needed update.
239 * @param h Handle to hb_handle_t.
240 * @param version Pointer to handle where version will be copied.
241 * @return update indicator.
243 int hb_check_update( hb_handle_t * h, char ** version )
245 *version = ( h->build < 0 ) ? NULL : h->version;
250 * Sets the cpu count to the desired value.
251 * @param h Handle to hb_handle_t
252 * @param cpu_count Number of CPUs to use.
254 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
256 cpu_count = MAX( 1, cpu_count );
257 cpu_count = MIN( cpu_count, 8 );
258 h->cpu_count = cpu_count;
262 * Initializes a scan of the by calling hb_scan_init
263 * @param h Handle to hb_handle_t
264 * @param path location of VIDEO_TS folder.
265 * @param title_index Desired title to scan. 0 for all titles.
267 void hb_scan( hb_handle_t * h, const char * path, int title_index )
271 /* Clean up from previous scan */
272 while( ( title = hb_list_item( h->list_title, 0 ) ) )
274 hb_list_rem( h->list_title, title );
275 hb_title_close( &title );
278 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
279 h->scan_thread = hb_scan_init( h, path, title_index, h->list_title );
283 * Returns the list of titles found.
284 * @param h Handle to hb_handle_t
285 * @return Handle to hb_list_t of the title list.
287 hb_list_t * hb_get_titles( hb_handle_t * h )
289 return h->list_title;
293 * Create preview image of desired title a index of picture.
294 * @param h Handle to hb_handle_t.
295 * @param title Handle to hb_title_t of desired title.
296 * @param picture Index in title.
297 * @param buffer Handle to buufer were inage will be drawn.
299 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
302 hb_job_t * job = title->job;
305 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
307 AVPicture pic1, pic2, pic3, pic4;
308 ImgReSampleContext * context;
311 buf1 = malloc( title->width * title->height * 3 / 2 );
312 buf2 = malloc( title->width * title->height * 3 / 2 );
313 buf3 = malloc( title->width * title->height * 3 / 2 );
314 buf4 = malloc( title->width * title->height * 4 );
315 avpicture_fill( &pic1, buf1, PIX_FMT_YUV420P,
316 title->width, title->height );
317 avpicture_fill( &pic2, buf2, PIX_FMT_YUV420P,
318 title->width, title->height );
319 avpicture_fill( &pic3, buf3, PIX_FMT_YUV420P,
320 job->width, job->height );
321 avpicture_fill( &pic4, buf4, PIX_FMT_RGBA32,
322 job->width, job->height );
324 memset( filename, 0, 1024 );
326 hb_get_tempory_filename( h, filename, "%x%d",
327 (int) title, picture );
329 file = fopen( filename, "r" );
332 hb_log( "hb_get_preview: fopen failed" );
336 fread( buf1, title->width * title->height * 3 / 2, 1, file );
339 context = img_resample_full_init(
340 job->width, job->height, title->width, title->height,
341 job->crop[0], job->crop[1], job->crop[2], job->crop[3],
344 if( job->deinterlace )
346 avpicture_deinterlace( &pic2, &pic1, PIX_FMT_YUV420P,
347 title->width, title->height );
348 img_resample( context, &pic3, &pic2 );
352 img_resample( context, &pic3, &pic1 );
354 img_convert( &pic4, PIX_FMT_RGBA32, &pic3, PIX_FMT_YUV420P,
355 job->width, job->height );
357 /* Gray background */
358 p32 = (uint32_t *) buffer;
359 for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
364 /* Draw the picture, centered, and draw the cropping zone */
365 pen = buffer + ( title->height - job->height ) *
366 ( title->width + 2 ) * 2 + ( title->width - job->width ) * 2;
367 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
368 pen += 4 * ( title->width + 2 );
369 for( i = 0; i < job->height; i++ )
372 nextLine = pen + 4 * ( title->width + 2 );
373 memset( pen, 0xFF, 4 );
375 memcpy( pen, buf4 + 4 * job->width * i, 4 * job->width );
376 pen += 4 * job->width;
377 memset( pen, 0xFF, 4 );
380 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
389 * Calculates job width, height, and cropping parameters.
390 * @param job Handle to hb_job_t.
391 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
392 * @param pixels Maximum desired pixel count.
394 void hb_set_size( hb_job_t * job, int aspect, int pixels )
396 hb_title_t * title = job->title;
398 int croppedWidth = title->width - title->crop[2] - title->crop[3];
399 int croppedHeight = title->height - title->crop[0] - title->crop[1];
400 int croppedAspect = title->aspect * title->height * croppedWidth /
401 croppedHeight / title->width;
407 /* Keep the best possible aspect ratio */
408 aspect = croppedAspect;
411 /* Crop if necessary to obtain the desired ratio */
412 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
413 if( aspect < croppedAspect )
415 /* Need to crop on the left and right */
416 addCrop = croppedWidth - aspect * croppedHeight * title->width /
417 title->aspect / title->height;
420 addCrop = ( addCrop + 1 ) / 2;
421 job->crop[2] += addCrop;
422 job->crop[3] += addCrop;
424 else if( addCrop & 2 )
427 job->crop[2] += addCrop - 1;
428 job->crop[3] += addCrop + 1;
433 job->crop[2] += addCrop;
434 job->crop[3] += addCrop;
437 else if( aspect > croppedAspect )
439 /* Need to crop on the top and bottom */
440 addCrop = croppedHeight - croppedWidth * title->aspect *
441 title->height / aspect / title->width;
444 addCrop = ( addCrop + 1 ) / 2;
445 job->crop[0] += addCrop;
446 job->crop[1] += addCrop;
448 else if( addCrop & 2 )
451 job->crop[0] += addCrop - 1;
452 job->crop[1] += addCrop + 1;
457 job->crop[0] += addCrop;
458 job->crop[1] += addCrop;
462 /* Compute a resolution from the number of pixels and aspect */
466 h = MULTIPLE_16( w * HB_ASPECT_BASE / aspect );
474 job->height = MULTIPLE_16( 16 * i * HB_ASPECT_BASE / aspect );
478 * Returns the number of jobs in the queue.
479 * @param h Handle to hb_handle_t.
480 * @return Number of jobs.
482 int hb_count( hb_handle_t * h )
484 return hb_list_count( h->jobs );
488 * Returns handle to job at index i within the job list.
489 * @param h Handle to hb_handle_t.
490 * @param i Index of job.
491 * @returns Handle to hb_job_t of desired job.
493 hb_job_t * hb_job( hb_handle_t * h, int i )
495 return hb_list_item( h->jobs, i );
499 * Adds a job to the job list.
500 * @param h Handle to hb_handle_t.
501 * @param job Handle to hb_job_t.
503 void hb_add( hb_handle_t * h, hb_job_t * job )
506 hb_title_t * title, * title_copy;
507 hb_chapter_t * chapter, * chapter_copy;
508 hb_audio_t * audio, * audio_copy;
509 hb_subtitle_t * subtitle, * subtitle_copy;
514 title_copy = malloc( sizeof( hb_title_t ) );
515 memcpy( title_copy, title, sizeof( hb_title_t ) );
517 title_copy->list_chapter = hb_list_init();
518 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
520 chapter = hb_list_item( title->list_chapter, i );
521 chapter_copy = malloc( sizeof( hb_chapter_t ) );
522 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
523 hb_list_add( title_copy->list_chapter, chapter_copy );
526 /* Copy the audio track(s) we want */
527 title_copy->list_audio = hb_list_init();
529 /* Do nothing about audio during first pass */
532 for( i = 0; i < 8; i++ )
534 if( job->audios[i] < 0 )
538 if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
540 audio_copy = malloc( sizeof( hb_audio_t ) );
541 memcpy( audio_copy, audio, sizeof( hb_audio_t ) );
542 hb_list_add( title_copy->list_audio, audio_copy );
547 /* Copy the subtitle we want (or not) */
548 title_copy->list_subtitle = hb_list_init();
549 if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) )
551 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
552 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
553 hb_list_add( title_copy->list_subtitle, subtitle_copy );
557 job_copy = calloc( sizeof( hb_job_t ), 1 );
558 memcpy( job_copy, job, sizeof( hb_job_t ) );
559 job_copy->title = title_copy;
560 job_copy->file = strdup( job->file );
562 job_copy->pause = h->pause_lock;
564 /* Add the job to the list */
565 hb_list_add( h->jobs, job_copy );
569 * Removes a job from the job list.
570 * @param h Handle to hb_handle_t.
571 * @param job Handle to hb_job_t.
573 void hb_rem( hb_handle_t * h, hb_job_t * job )
575 hb_list_rem( h->jobs, job );
577 /* XXX free everything XXX */
581 * Starts the conversion process.
582 * Sets state to HB_STATE_WORKING.
583 * calls hb_work_init, to launch work thread. Stores handle to work thread.
584 * @param h Handle to hb_handle_t.
586 void hb_start( hb_handle_t * h )
589 h->job_count = hb_list_count( h->jobs );
591 hb_lock( h->state_lock );
592 h->state.state = HB_STATE_WORKING;
593 #define p h->state.param.working
596 p.job_count = h->job_count;
603 hb_unlock( h->state_lock );
608 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
609 &h->work_die, &h->work_error );
613 * Pauses the conversion process.
614 * @param h Handle to hb_handle_t.
616 void hb_pause( hb_handle_t * h )
620 hb_lock( h->pause_lock );
623 hb_lock( h->state_lock );
624 h->state.state = HB_STATE_PAUSED;
625 hb_unlock( h->state_lock );
630 * Resumes the conversion process.
631 * @param h Handle to hb_handle_t.
633 void hb_resume( hb_handle_t * h )
637 hb_unlock( h->pause_lock );
643 * Stops the conversion process.
644 * @param h Handle to hb_handle_t.
646 void hb_stop( hb_handle_t * h )
654 * Returns the state of the conversion process.
655 * @param h Handle to hb_handle_t.
656 * @param s Handle to hb_state_t which to copy the state data.
658 void hb_get_state( hb_handle_t * h, hb_state_t * s )
660 hb_lock( h->state_lock );
662 memcpy( s, &h->state, sizeof( hb_state_t ) );
663 h->state.state = HB_STATE_IDLE;
665 hb_unlock( h->state_lock );
669 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init_real.
670 * @param _h Pointer to handle to hb_handle_t.
672 void hb_close( hb_handle_t ** _h )
674 hb_handle_t * h = *_h;
678 hb_thread_close( &h->main_thread );
680 while( ( title = hb_list_item( h->list_title, 0 ) ) )
682 hb_list_rem( h->list_title, title );
683 hb_title_close( &title );
685 hb_list_close( &h->list_title );
687 hb_list_close( &h->jobs );
688 hb_lock_close( &h->state_lock );
689 hb_lock_close( &h->pause_lock );
695 * Monitors the state of the update, scan, and work threads.
696 * Sets scan done state when scan thread exits.
697 * Sets work done state when work thread exits.
698 * @param _h Handle to hb_handle_t
700 static void thread_func( void * _h )
702 hb_handle_t * h = (hb_handle_t *) _h;
705 struct dirent * entry;
709 /* Create folder for temporary files */
710 memset( dirname, 0, 1024 );
711 hb_get_tempory_directory( h, dirname );
717 /* In case the check_update thread hangs, it'll die sooner or
718 later. Then, we join it here */
719 if( h->update_thread &&
720 hb_thread_has_exited( h->update_thread ) )
722 hb_thread_close( &h->update_thread );
725 /* Check if the scan thread is done */
726 if( h->scan_thread &&
727 hb_thread_has_exited( h->scan_thread ) )
729 hb_thread_close( &h->scan_thread );
731 hb_log( "libhb: scan thread found %d valid title(s)",
732 hb_list_count( h->list_title ) );
733 hb_lock( h->state_lock );
734 h->state.state = HB_STATE_SCANDONE;
735 hb_unlock( h->state_lock );
738 /* Check if the work thread is done */
739 if( h->work_thread &&
740 hb_thread_has_exited( h->work_thread ) )
742 hb_thread_close( &h->work_thread );
744 hb_log( "libhb: work result = %d",
746 hb_lock( h->state_lock );
747 h->state.state = HB_STATE_WORKDONE;
748 h->state.param.workdone.error = h->work_error;
749 hb_unlock( h->state_lock );
758 hb_thread_close( &h->work_thread );
761 /* Remove temp folder */
762 dir = opendir( dirname );
763 while( ( entry = readdir( dir ) ) )
766 if( entry->d_name[0] == '.' )
770 memset( filename, 0, 1024 );
771 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
780 * @param h Handle to hb_handle_t
782 int hb_get_pid( hb_handle_t * h )
788 * Sets the current state.
789 * @param h Handle to hb_handle_t
790 * @param s Handle to new hb_state_t
792 void hb_set_state( hb_handle_t * h, hb_state_t * s )
794 hb_lock( h->pause_lock );
795 hb_lock( h->state_lock );
796 memcpy( &h->state, s, sizeof( hb_state_t ) );
797 if( h->state.state == HB_STATE_WORKING )
800 h->state.param.working.job_cur =
801 h->job_count - hb_list_count( h->jobs );
802 h->state.param.working.job_count = h->job_count;
804 hb_unlock( h->state_lock );
805 hb_unlock( h->pause_lock );