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 int job_count_permanent;
27 volatile int work_die;
29 hb_thread_t * work_thread;
33 hb_lock_t * state_lock;
37 hb_lock_t * pause_lock;
38 /* For MacGui active queue
39 increments each time the scan thread completes*/
44 hb_work_object_t * hb_objects = NULL;
46 static void thread_func( void * );
49 * Registers work objects, by adding the work object to a liked list.
50 * @param w Handle to hb_work_object_t to register.
52 void hb_register( hb_work_object_t * w )
59 * libhb initialization routine.
60 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
61 * @param update_check signals libhb to check for updated version from HandBrake website.
62 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
64 hb_handle_t * hb_init_real( int verbose, int update_check )
66 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
69 /* See hb_log() in common.c */
70 if( verbose > HB_DEBUG_NONE )
72 putenv( "HB_DEBUG=1" );
73 av_log_set_level(AV_LOG_DEBUG);
76 /* Check for an update on the website if asked to */
81 hb_log( "hb_init: checking for updates" );
83 h->update_thread = hb_update_init( &h->build, h->version );
87 if( hb_thread_has_exited( h->update_thread ) )
89 /* Immediate success or failure */
90 hb_thread_close( &h->update_thread );
93 if( hb_get_date() > date + 1000 )
95 /* Still nothing after one second. Connection problem,
97 hb_log( "hb_init: connection problem, not waiting for "
105 /* CPU count detection */
106 hb_log( "hb_init: checking cpu count" );
107 h->cpu_count = hb_get_cpu_count();
109 h->list_title = hb_list_init();
110 h->jobs = hb_list_init();
112 h->state_lock = hb_lock_init();
113 h->state.state = HB_STATE_IDLE;
115 h->pause_lock = hb_lock_init();
119 register_avcodec( &mpeg4_encoder );
120 register_avcodec( &mp2_decoder );
121 register_avcodec( &ac3_encoder );
122 av_register_codec_parser( &mpegaudio_parser);
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 );
132 /* Set the scan count to start at 0 */
137 * libhb initialization routine.
138 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
139 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
140 * @param update_check signals libhb to check for updated version from HandBrake website.
141 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
143 hb_handle_t * hb_init_dl( int verbose, int update_check )
145 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
148 /* See hb_log() in common.c */
149 if( verbose > HB_DEBUG_NONE )
151 putenv( "HB_DEBUG=1" );
152 av_log_set_level(AV_LOG_DEBUG);
155 /* Check for an update on the website if asked to */
160 hb_log( "hb_init: checking for updates" );
161 date = hb_get_date();
162 h->update_thread = hb_update_init( &h->build, h->version );
166 if( hb_thread_has_exited( h->update_thread ) )
168 /* Immediate success or failure */
169 hb_thread_close( &h->update_thread );
172 if( hb_get_date() > date + 1000 )
174 /* Still nothing after one second. Connection problem,
175 let the thread die */
176 hb_log( "hb_init: connection problem, not waiting for "
184 /* CPU count detection */
185 hb_log( "hb_init: checking cpu count" );
186 h->cpu_count = hb_get_cpu_count();
188 h->list_title = hb_list_init();
189 h->jobs = hb_list_init();
191 h->state_lock = hb_lock_init();
192 h->state.state = HB_STATE_IDLE;
194 h->pause_lock = hb_lock_init();
198 register_avcodec( &mpeg4_encoder );
199 register_avcodec( &mp2_decoder );
200 register_avcodec( &ac3_encoder );
202 /* Start library thread */
203 hb_log( "hb_init: starting libhb thread" );
205 h->main_thread = hb_thread_init( "libhb", thread_func, h,
206 HB_NORMAL_PRIORITY );
208 hb_register( &hb_sync );
209 hb_register( &hb_decmpeg2 );
210 hb_register( &hb_decsub );
211 hb_register( &hb_render );
212 hb_register( &hb_encavcodec );
213 hb_register( &hb_encxvid );
214 hb_register( &hb_encx264 );
215 hb_register( &hb_deca52 );
216 hb_register( &hb_decdca );
217 hb_register( &hb_decavcodec );
218 hb_register( &hb_declpcm );
219 hb_register( &hb_encfaac );
220 hb_register( &hb_enclame );
221 hb_register( &hb_encvorbis );
228 * Returns current version of libhb.
229 * @param h Handle to hb_handle_t.
230 * @return character array of version number.
232 char * hb_get_version( hb_handle_t * h )
238 * Returns current build of libhb.
239 * @param h Handle to hb_handle_t.
240 * @return character array of build number.
242 int hb_get_build( hb_handle_t * h )
248 * Checks for needed update.
249 * @param h Handle to hb_handle_t.
250 * @param version Pointer to handle where version will be copied.
251 * @return update indicator.
253 int hb_check_update( hb_handle_t * h, char ** version )
255 *version = ( h->build < 0 ) ? NULL : h->version;
260 * Sets the cpu count to the desired value.
261 * @param h Handle to hb_handle_t
262 * @param cpu_count Number of CPUs to use.
264 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
266 cpu_count = MAX( 1, cpu_count );
267 cpu_count = MIN( cpu_count, 8 );
268 h->cpu_count = cpu_count;
272 * Initializes a scan of the by calling hb_scan_init
273 * @param h Handle to hb_handle_t
274 * @param path location of VIDEO_TS folder.
275 * @param title_index Desired title to scan. 0 for all titles.
277 void hb_scan( hb_handle_t * h, const char * path, int title_index )
281 /* Clean up from previous scan */
282 while( ( title = hb_list_item( h->list_title, 0 ) ) )
284 hb_list_rem( h->list_title, title );
285 hb_title_close( &title );
288 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
289 h->scan_thread = hb_scan_init( h, path, title_index, h->list_title );
293 * Returns the list of titles found.
294 * @param h Handle to hb_handle_t
295 * @return Handle to hb_list_t of the title list.
297 hb_list_t * hb_get_titles( hb_handle_t * h )
299 return h->list_title;
303 * Create preview image of desired title a index of picture.
304 * @param h Handle to hb_handle_t.
305 * @param title Handle to hb_title_t of desired title.
306 * @param picture Index in title.
307 * @param buffer Handle to buufer were inage will be drawn.
309 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
312 hb_job_t * job = title->job;
315 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
317 AVPicture pic1, pic2, pic3, pic4;
318 ImgReSampleContext * context;
321 buf1 = malloc( title->width * title->height * 3 / 2 );
322 buf2 = malloc( title->width * title->height * 3 / 2 );
323 buf3 = malloc( title->width * title->height * 3 / 2 );
324 buf4 = malloc( title->width * title->height * 4 );
325 avpicture_fill( &pic1, buf1, PIX_FMT_YUV420P,
326 title->width, title->height );
327 avpicture_fill( &pic2, buf2, PIX_FMT_YUV420P,
328 title->width, title->height );
329 avpicture_fill( &pic3, buf3, PIX_FMT_YUV420P,
330 job->width, job->height );
331 avpicture_fill( &pic4, buf4, PIX_FMT_RGBA32,
332 job->width, job->height );
334 memset( filename, 0, 1024 );
336 hb_get_tempory_filename( h, filename, "%x%d",
337 (intptr_t) title, picture );
339 file = fopen( filename, "r" );
342 hb_log( "hb_get_preview: fopen failed" );
346 fread( buf1, title->width * title->height * 3 / 2, 1, file );
349 context = img_resample_full_init(
350 job->width, job->height, title->width, title->height,
351 job->crop[0], job->crop[1], job->crop[2], job->crop[3],
354 if( job->deinterlace )
356 avpicture_deinterlace( &pic2, &pic1, PIX_FMT_YUV420P,
357 title->width, title->height );
358 img_resample( context, &pic3, &pic2 );
362 img_resample( context, &pic3, &pic1 );
364 img_convert( &pic4, PIX_FMT_RGBA32, &pic3, PIX_FMT_YUV420P,
365 job->width, job->height );
367 /* Gray background */
368 p32 = (uint32_t *) buffer;
369 for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
374 /* Draw the picture, centered, and draw the cropping zone */
375 pen = buffer + ( title->height - job->height ) *
376 ( title->width + 2 ) * 2 + ( title->width - job->width ) * 2;
377 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
378 pen += 4 * ( title->width + 2 );
379 for( i = 0; i < job->height; i++ )
382 nextLine = pen + 4 * ( title->width + 2 );
383 memset( pen, 0xFF, 4 );
385 memcpy( pen, buf4 + 4 * job->width * i, 4 * job->width );
386 pen += 4 * job->width;
387 memset( pen, 0xFF, 4 );
390 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
399 * Calculates job width, height, and cropping parameters.
400 * @param job Handle to hb_job_t.
401 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
402 * @param pixels Maximum desired pixel count.
404 void hb_set_size( hb_job_t * job, int aspect, int pixels )
406 hb_title_t * title = job->title;
408 int croppedWidth = title->width - title->crop[2] - title->crop[3];
409 int croppedHeight = title->height - title->crop[0] - title->crop[1];
410 int croppedAspect = title->aspect * title->height * croppedWidth /
411 croppedHeight / title->width;
417 /* Keep the best possible aspect ratio */
418 aspect = croppedAspect;
421 /* Crop if necessary to obtain the desired ratio */
422 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
423 if( aspect < croppedAspect )
425 /* Need to crop on the left and right */
426 addCrop = croppedWidth - aspect * croppedHeight * title->width /
427 title->aspect / title->height;
430 addCrop = ( addCrop + 1 ) / 2;
431 job->crop[2] += addCrop;
432 job->crop[3] += addCrop;
434 else if( addCrop & 2 )
437 job->crop[2] += addCrop - 1;
438 job->crop[3] += addCrop + 1;
443 job->crop[2] += addCrop;
444 job->crop[3] += addCrop;
447 else if( aspect > croppedAspect )
449 /* Need to crop on the top and bottom */
450 addCrop = croppedHeight - croppedWidth * title->aspect *
451 title->height / aspect / title->width;
454 addCrop = ( addCrop + 1 ) / 2;
455 job->crop[0] += addCrop;
456 job->crop[1] += addCrop;
458 else if( addCrop & 2 )
461 job->crop[0] += addCrop - 1;
462 job->crop[1] += addCrop + 1;
467 job->crop[0] += addCrop;
468 job->crop[1] += addCrop;
472 /* Compute a resolution from the number of pixels and aspect */
476 h = MULTIPLE_16( w * HB_ASPECT_BASE / aspect );
484 job->height = MULTIPLE_16( 16 * i * HB_ASPECT_BASE / aspect );
488 * Returns the number of jobs in the queue.
489 * @param h Handle to hb_handle_t.
490 * @return Number of jobs.
492 int hb_count( hb_handle_t * h )
494 return hb_list_count( h->jobs );
498 * Returns handle to job at index i within the job list.
499 * @param h Handle to hb_handle_t.
500 * @param i Index of job.
501 * @returns Handle to hb_job_t of desired job.
503 hb_job_t * hb_job( hb_handle_t * h, int i )
505 return hb_list_item( h->jobs, i );
509 * Adds a job to the job list.
510 * @param h Handle to hb_handle_t.
511 * @param job Handle to hb_job_t.
513 void hb_add( hb_handle_t * h, hb_job_t * job )
516 hb_title_t * title, * title_copy;
517 hb_chapter_t * chapter, * chapter_copy;
518 hb_audio_t * audio, * audio_copy;
519 hb_subtitle_t * subtitle, * subtitle_copy;
525 title_copy = malloc( sizeof( hb_title_t ) );
526 memcpy( title_copy, title, sizeof( hb_title_t ) );
528 title_copy->list_chapter = hb_list_init();
529 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
531 chapter = hb_list_item( title->list_chapter, i );
532 chapter_copy = malloc( sizeof( hb_chapter_t ) );
533 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
534 hb_list_add( title_copy->list_chapter, chapter_copy );
537 /* Copy the audio track(s) we want */
538 title_copy->list_audio = hb_list_init();
540 /* Do nothing about audio during first pass */
543 for( i = 0; i < 8; i++ )
545 if( job->audios[i] < 0 )
549 if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
551 audio_copy = malloc( sizeof( hb_audio_t ) );
552 memcpy( audio_copy, audio, sizeof( hb_audio_t ) );
553 hb_list_add( title_copy->list_audio, audio_copy );
558 title_copy->list_subtitle = hb_list_init();
561 * The following code is confusing, there are three ways in which we select subtitles
562 * and it depends on whether this is single or two pass mode.
564 * subtitle_scan may be enabled, in which case the first pass scans all subtitles
565 * of that language. The second pass does not select any because they are set at the
566 * end of the first pass.
568 * native_language may have a preferred language, in which case we may be switching
569 * the language we want for the subtitles in the first pass of a single pass, or the
570 * second pass of a two pass.
572 * We may have manually selected a subtitle, in which case that is selected in the
573 * first pass of a single pass, or the second of a two pass.
575 memset( audio_lang, 0, sizeof( audio_lang ) );
577 if ( job->subtitle_scan || job->native_language ) {
580 * Find the first audio language that is being encoded
582 for( i = 0; i < 8; i++ )
584 if( job->audios[i] < 0 )
588 if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
590 strncpy(audio_lang, audio->iso639_2, sizeof(audio_lang));
596 * In all cases switch the language if we need to to our native
599 if( job->native_language )
601 if( strncasecmp( job->native_language, audio_lang,
602 sizeof( audio_lang ) ) != 0 )
607 hb_log( "Enabled subtitles in native language '%s', audio is in '%s'",
608 job->native_language, audio_lang);
611 * The main audio track is not in our native language, so switch
612 * the subtitles to use our native language instead.
614 strncpy( audio_lang, job->native_language, sizeof( audio_lang ) );
617 * native language is irrelevent, free it.
619 free( job->native_language );
620 job->native_language = NULL;
626 * If doing a subtitle scan then add all the matching subtitles for this
629 if ( job->subtitle_scan )
631 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
633 subtitle = hb_list_item( title->list_subtitle, i );
634 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
637 * Matched subtitle language with audio language, so
638 * add this to our list to scan.
640 * We will update the subtitle list on the second pass
641 * later after the first pass has completed.
643 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
644 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
645 hb_list_add( title_copy->list_subtitle, subtitle_copy );
646 if ( job->native_language ) {
648 * With native language just select the
649 * first match in our langiage, not all of
650 * them. Subsequent ones are likely to be commentary
658 * Not doing a subtitle scan in this pass, but maybe we are in the
661 if( job->select_subtitle )
664 * Don't add subtitles here, we'll add them via select_subtitle
665 * at the end of pass 1
669 * Definitely not doing a subtitle scan.
671 if( job->pass != 1 && job->native_language )
674 * We are not doing a subtitle scan but do want the
675 * native langauge subtitle selected, so select it
676 * for pass 0 or pass 2 of a two pass.
678 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
680 subtitle = hb_list_item( title->list_subtitle, i );
681 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
684 * Matched subtitle language with audio language, so
685 * add this to our list to scan.
687 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
688 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
689 hb_list_add( title_copy->list_subtitle, subtitle_copy );
695 * Manually selected subtitle, in which case only
696 * bother adding them for pass 0 or pass 2 of a two
701 if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) )
703 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
704 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
705 hb_list_add( title_copy->list_subtitle, subtitle_copy );
713 job_copy = calloc( sizeof( hb_job_t ), 1 );
714 memcpy( job_copy, job, sizeof( hb_job_t ) );
715 title_copy->job = job_copy;
716 job_copy->title = title_copy;
717 job_copy->file = strdup( job->file );
719 job_copy->pause = h->pause_lock;
721 /* Add the job to the list */
722 hb_list_add( h->jobs, job_copy );
723 h->job_count = hb_count(h);
724 h->job_count_permanent++;
728 * Removes a job from the job list.
729 * @param h Handle to hb_handle_t.
730 * @param job Handle to hb_job_t.
732 void hb_rem( hb_handle_t * h, hb_job_t * job )
734 hb_list_rem( h->jobs, job );
736 h->job_count = hb_count(h);
737 if (h->job_count_permanent)
738 h->job_count_permanent--;
740 /* XXX free everything XXX */
744 * Starts the conversion process.
745 * Sets state to HB_STATE_WORKING.
746 * calls hb_work_init, to launch work thread. Stores handle to work thread.
747 * @param h Handle to hb_handle_t.
749 void hb_start( hb_handle_t * h )
752 h->job_count = hb_list_count( h->jobs );
754 hb_lock( h->state_lock );
755 h->state.state = HB_STATE_WORKING;
756 #define p h->state.param.working
759 p.job_count = h->job_count;
766 hb_unlock( h->state_lock );
771 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
772 &h->work_die, &h->work_error );
776 * Pauses the conversion process.
777 * @param h Handle to hb_handle_t.
779 void hb_pause( hb_handle_t * h )
783 hb_lock( h->pause_lock );
786 hb_lock( h->state_lock );
787 h->state.state = HB_STATE_PAUSED;
788 hb_unlock( h->state_lock );
793 * Resumes the conversion process.
794 * @param h Handle to hb_handle_t.
796 void hb_resume( hb_handle_t * h )
800 hb_unlock( h->pause_lock );
806 * Stops the conversion process.
807 * @param h Handle to hb_handle_t.
809 void hb_stop( hb_handle_t * h )
813 h->job_count = hb_count(h);
814 h->job_count_permanent = 0;
820 * Returns the state of the conversion process.
821 * @param h Handle to hb_handle_t.
822 * @param s Handle to hb_state_t which to copy the state data.
824 void hb_get_state( hb_handle_t * h, hb_state_t * s )
826 hb_lock( h->state_lock );
828 memcpy( s, &h->state, sizeof( hb_state_t ) );
829 h->state.state = HB_STATE_IDLE;
831 hb_unlock( h->state_lock );
835 * Called in MacGui in UpdateUI to check
836 * for a new scan being completed to set a new source
838 int hb_get_scancount( hb_handle_t * h)
844 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init_real.
845 * @param _h Pointer to handle to hb_handle_t.
847 void hb_close( hb_handle_t ** _h )
849 hb_handle_t * h = *_h;
853 hb_thread_close( &h->main_thread );
855 while( ( title = hb_list_item( h->list_title, 0 ) ) )
857 hb_list_rem( h->list_title, title );
859 hb_title_close( &title );
861 hb_list_close( &h->list_title );
863 hb_list_close( &h->jobs );
864 hb_lock_close( &h->state_lock );
865 hb_lock_close( &h->pause_lock );
871 * Monitors the state of the update, scan, and work threads.
872 * Sets scan done state when scan thread exits.
873 * Sets work done state when work thread exits.
874 * @param _h Handle to hb_handle_t
876 static void thread_func( void * _h )
878 hb_handle_t * h = (hb_handle_t *) _h;
881 struct dirent * entry;
885 /* Create folder for temporary files */
886 memset( dirname, 0, 1024 );
887 hb_get_tempory_directory( h, dirname );
893 /* In case the check_update thread hangs, it'll die sooner or
894 later. Then, we join it here */
895 if( h->update_thread &&
896 hb_thread_has_exited( h->update_thread ) )
898 hb_thread_close( &h->update_thread );
901 /* Check if the scan thread is done */
902 if( h->scan_thread &&
903 hb_thread_has_exited( h->scan_thread ) )
905 hb_thread_close( &h->scan_thread );
907 hb_log( "libhb: scan thread found %d valid title(s)",
908 hb_list_count( h->list_title ) );
909 hb_lock( h->state_lock );
910 h->state.state = HB_STATE_SCANDONE; //originally state.state
911 hb_unlock( h->state_lock );
912 /*we increment this sessions scan count by one for the MacGui
913 to trigger a new source being set */
917 /* Check if the work thread is done */
918 if( h->work_thread &&
919 hb_thread_has_exited( h->work_thread ) )
921 hb_thread_close( &h->work_thread );
923 hb_log( "libhb: work result = %d",
925 hb_lock( h->state_lock );
926 h->state.state = HB_STATE_WORKDONE;
927 h->state.param.workdone.error = h->work_error;
929 h->job_count = hb_count(h);
930 if (h->job_count < 1)
931 h->job_count_permanent = 0;
932 hb_unlock( h->state_lock );
941 hb_thread_close( &h->work_thread );
944 /* Remove temp folder */
945 dir = opendir( dirname );
946 while( ( entry = readdir( dir ) ) )
949 if( entry->d_name[0] == '.' )
953 memset( filename, 0, 1024 );
954 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
963 * @param h Handle to hb_handle_t
965 int hb_get_pid( hb_handle_t * h )
971 * Sets the current state.
972 * @param h Handle to hb_handle_t
973 * @param s Handle to new hb_state_t
975 void hb_set_state( hb_handle_t * h, hb_state_t * s )
977 hb_lock( h->pause_lock );
978 hb_lock( h->state_lock );
979 memcpy( &h->state, s, sizeof( hb_state_t ) );
980 if( h->state.state == HB_STATE_WORKING )
983 if (h->job_count < 1)
984 h->job_count_permanent = 1;
986 h->state.param.working.job_cur =
987 h->job_count_permanent - hb_list_count( h->jobs );
988 h->state.param.working.job_count = h->job_count_permanent;
990 hb_unlock( h->state_lock );
991 hb_unlock( h->pause_lock );