4 #if defined( SYS_MINGW ) && defined( PTW32_STATIC_LIB )
12 /* The "Check for update" thread */
15 hb_thread_t * update_thread;
17 /* This thread's only purpose is to check other threads'
20 hb_thread_t * main_thread;
23 /* DVD/file scan thread */
24 hb_list_t * list_title;
25 hb_thread_t * scan_thread;
27 /* The thread which processes the jobs. Others threads are launched
28 from this one (see work.c) */
30 hb_job_t * current_job;
32 int job_count_permanent;
33 volatile int work_die;
35 hb_thread_t * work_thread;
39 hb_lock_t * state_lock;
43 hb_lock_t * pause_lock;
44 /* For MacGui active queue
45 increments each time the scan thread completes*/
47 volatile int scan_die;
49 /* Stash of persistent data between jobs, for stuff
50 like correcting frame count and framerate estimates
51 on multi-pass encodes where frames get dropped. */
52 hb_interjob_t * interjob;
56 hb_lock_t *hb_avcodec_lock;
57 hb_work_object_t * hb_objects = NULL;
58 int hb_instance_counter = 0;
59 int hb_process_initialized = 0;
61 static void thread_func( void * );
63 void hb_avcodec_init()
65 hb_avcodec_lock = hb_lock_init();
69 int hb_avcodec_open(AVCodecContext *avctx, AVCodec *codec)
72 hb_lock( hb_avcodec_lock );
73 ret = avcodec_open(avctx, codec);
74 hb_unlock( hb_avcodec_lock );
78 int hb_avcodec_close(AVCodecContext *avctx)
81 hb_lock( hb_avcodec_lock );
82 ret = avcodec_close(avctx);
83 hb_unlock( hb_avcodec_lock );
87 int hb_ff_layout_xlat(int64_t ff_channel_layout, int channels)
89 switch (ff_channel_layout)
92 return HB_INPUT_CH_LAYOUT_MONO;
93 case CH_LAYOUT_STEREO:
94 return HB_INPUT_CH_LAYOUT_STEREO;
95 case CH_LAYOUT_SURROUND:
96 return HB_INPUT_CH_LAYOUT_3F;
97 case CH_LAYOUT_4POINT0:
98 return HB_INPUT_CH_LAYOUT_3F1R;
100 return HB_INPUT_CH_LAYOUT_2F2R;
102 return HB_INPUT_CH_LAYOUT_2F2R;
103 case CH_LAYOUT_5POINT0:
104 // ffmpeg like to neglect to signal LFE
106 return HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
107 return HB_INPUT_CH_LAYOUT_3F2R;
108 case CH_LAYOUT_5POINT1:
109 return HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
110 case CH_LAYOUT_5POINT0_BACK:
111 // ffmpeg like to neglect to signal LFE
113 return HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
114 return HB_INPUT_CH_LAYOUT_3F2R;
115 case CH_LAYOUT_5POINT1_BACK:
116 return HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
117 case CH_LAYOUT_7POINT0:
118 // ffmpeg like to neglect to signal LFE
120 return HB_INPUT_CH_LAYOUT_3F4R|HB_INPUT_CH_LAYOUT_HAS_LFE;
121 return HB_INPUT_CH_LAYOUT_3F4R;
122 case CH_LAYOUT_7POINT1:
123 return HB_INPUT_CH_LAYOUT_3F4R|HB_INPUT_CH_LAYOUT_HAS_LFE;
124 case CH_LAYOUT_STEREO_DOWNMIX:
125 return HB_INPUT_CH_LAYOUT_STEREO;
127 return HB_INPUT_CH_LAYOUT_STEREO;
129 return HB_INPUT_CH_LAYOUT_STEREO;
133 * Registers work objects, by adding the work object to a liked list.
134 * @param w Handle to hb_work_object_t to register.
136 void hb_register( hb_work_object_t * w )
138 w->next = hb_objects;
143 * libhb initialization routine.
144 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
145 * @param update_check signals libhb to check for updated version from HandBrake website.
146 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
148 hb_handle_t * hb_init( int verbose, int update_check )
150 if (!hb_process_initialized)
152 #if defined( SYS_MINGW ) && defined( PTW32_STATIC_LIB )
153 pthread_win32_process_attach_np();
155 hb_process_initialized =1;
158 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
161 /* See hb_deep_log() and hb_log() in common.c */
162 global_verbosity_level = verbose;
164 putenv( "HB_DEBUG=1" );
166 h->id = hb_instance_counter++;
168 /* Check for an update on the website if asked to */
173 hb_log( "hb_init: checking for updates" );
174 date = hb_get_date();
175 h->update_thread = hb_update_init( &h->build, h->version );
179 if( hb_thread_has_exited( h->update_thread ) )
181 /* Immediate success or failure */
182 hb_thread_close( &h->update_thread );
185 if( hb_get_date() > date + 1000 )
187 /* Still nothing after one second. Connection problem,
188 let the thread die */
189 hb_log( "hb_init: connection problem, not waiting for "
198 * Initialise buffer pool
200 hb_buffer_pool_init();
202 /* CPU count detection */
203 hb_log( "hb_init: checking cpu count" );
204 h->cpu_count = hb_get_cpu_count();
206 h->list_title = hb_list_init();
207 h->jobs = hb_list_init();
209 h->state_lock = hb_lock_init();
210 h->state.state = HB_STATE_IDLE;
212 h->pause_lock = hb_lock_init();
214 h->interjob = calloc( sizeof( hb_interjob_t ), 1 );
219 /* Start library thread */
220 hb_log( "hb_init: starting libhb thread" );
222 h->main_thread = hb_thread_init( "libhb", thread_func, h,
223 HB_NORMAL_PRIORITY );
224 hb_register( &hb_sync_video );
225 hb_register( &hb_sync_audio );
226 hb_register( &hb_decmpeg2 );
227 hb_register( &hb_decvobsub );
228 hb_register( &hb_encvobsub );
229 hb_register( &hb_deccc608 );
230 hb_register( &hb_decsrtsub );
231 hb_register( &hb_render );
232 hb_register( &hb_encavcodec );
233 hb_register( &hb_encx264 );
234 hb_register( &hb_enctheora );
235 hb_register( &hb_deca52 );
236 hb_register( &hb_decdca );
237 hb_register( &hb_decavcodec );
238 hb_register( &hb_decavcodecv );
239 hb_register( &hb_decavcodecvi );
240 hb_register( &hb_decavcodecai );
241 hb_register( &hb_declpcm );
242 hb_register( &hb_encfaac );
243 hb_register( &hb_enclame );
244 hb_register( &hb_encvorbis );
245 hb_register( &hb_muxer );
247 hb_register( &hb_encca_aac );
254 * libhb initialization routine.
255 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
256 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
257 * @param update_check signals libhb to check for updated version from HandBrake website.
258 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
260 hb_handle_t * hb_init_dl( int verbose, int update_check )
262 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
265 /* See hb_log() in common.c */
266 if( verbose > HB_DEBUG_NONE )
268 putenv( "HB_DEBUG=1" );
271 h->id = hb_instance_counter++;
273 /* Check for an update on the website if asked to */
278 hb_log( "hb_init: checking for updates" );
279 date = hb_get_date();
280 h->update_thread = hb_update_init( &h->build, h->version );
284 if( hb_thread_has_exited( h->update_thread ) )
286 /* Immediate success or failure */
287 hb_thread_close( &h->update_thread );
290 if( hb_get_date() > date + 1000 )
292 /* Still nothing after one second. Connection problem,
293 let the thread die */
294 hb_log( "hb_init: connection problem, not waiting for "
302 /* CPU count detection */
303 hb_log( "hb_init: checking cpu count" );
304 h->cpu_count = hb_get_cpu_count();
306 h->list_title = hb_list_init();
307 h->jobs = hb_list_init();
308 h->current_job = NULL;
310 h->state_lock = hb_lock_init();
311 h->state.state = HB_STATE_IDLE;
313 h->pause_lock = hb_lock_init();
317 avcodec_register_all();
319 /* Start library thread */
320 hb_log( "hb_init: starting libhb thread" );
322 h->main_thread = hb_thread_init( "libhb", thread_func, h,
323 HB_NORMAL_PRIORITY );
325 hb_register( &hb_sync_video );
326 hb_register( &hb_sync_audio );
327 hb_register( &hb_decmpeg2 );
328 hb_register( &hb_decvobsub );
329 hb_register( &hb_encvobsub );
330 hb_register( &hb_deccc608 );
331 hb_register( &hb_decsrtsub );
332 hb_register( &hb_render );
333 hb_register( &hb_encavcodec );
334 hb_register( &hb_encx264 );
335 hb_register( &hb_enctheora );
336 hb_register( &hb_deca52 );
337 hb_register( &hb_decdca );
338 hb_register( &hb_decavcodec );
339 hb_register( &hb_decavcodecv );
340 hb_register( &hb_decavcodecvi );
341 hb_register( &hb_decavcodecai );
342 hb_register( &hb_declpcm );
343 hb_register( &hb_encfaac );
344 hb_register( &hb_enclame );
345 hb_register( &hb_encvorbis );
346 hb_register( &hb_muxer );
348 hb_register( &hb_encca_aac );
356 * Returns current version of libhb.
357 * @param h Handle to hb_handle_t.
358 * @return character array of version number.
360 char * hb_get_version( hb_handle_t * h )
362 return HB_PROJECT_VERSION;
366 * Returns current build of libhb.
367 * @param h Handle to hb_handle_t.
368 * @return character array of build number.
370 int hb_get_build( hb_handle_t * h )
372 return HB_PROJECT_BUILD;
376 * Checks for needed update.
377 * @param h Handle to hb_handle_t.
378 * @param version Pointer to handle where version will be copied.
379 * @return update indicator.
381 int hb_check_update( hb_handle_t * h, char ** version )
383 *version = ( h->build < 0 ) ? NULL : h->version;
388 * Sets the cpu count to the desired value.
389 * @param h Handle to hb_handle_t
390 * @param cpu_count Number of CPUs to use.
392 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
394 cpu_count = MAX( 1, cpu_count );
395 cpu_count = MIN( cpu_count, 8 );
396 h->cpu_count = cpu_count;
400 * Deletes current previews associated with titles
401 * @param h Handle to hb_handle_t
403 void hb_remove_previews( hb_handle_t * h )
410 struct dirent * entry;
412 memset( dirname, 0, 1024 );
413 hb_get_temporary_directory( dirname );
414 dir = opendir( dirname );
415 if (dir == NULL) return;
417 count = hb_list_count( h->list_title );
418 while( ( entry = readdir( dir ) ) )
420 if( entry->d_name[0] == '.' )
424 for( i = 0; i < count; i++ )
426 title = hb_list_item( h->list_title, i );
427 len = snprintf( filename, 1024, "%d_%d", h->id, title->index );
428 if (strncmp(entry->d_name, filename, len) == 0)
430 snprintf( filename, 1024, "%s/%s", dirname, entry->d_name );
440 * Initializes a scan of the by calling hb_scan_init
441 * @param h Handle to hb_handle_t
442 * @param path location of VIDEO_TS folder.
443 * @param title_index Desired title to scan. 0 for all titles.
444 * @param preview_count Number of preview images to generate.
445 * @param store_previews Whether or not to write previews to disk.
447 void hb_scan( hb_handle_t * h, const char * path, int title_index,
448 int preview_count, int store_previews )
454 /* Clean up from previous scan */
455 hb_remove_previews( h );
456 while( ( title = hb_list_item( h->list_title, 0 ) ) )
458 hb_list_rem( h->list_title, title );
459 hb_title_close( &title );
462 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
463 h->scan_thread = hb_scan_init( h, &h->scan_die, path, title_index,
464 h->list_title, preview_count,
469 * Returns the list of titles found.
470 * @param h Handle to hb_handle_t
471 * @return Handle to hb_list_t of the title list.
473 hb_list_t * hb_get_titles( hb_handle_t * h )
475 return h->list_title;
479 * Create preview image of desired title a index of picture.
480 * @param h Handle to hb_handle_t.
481 * @param title Handle to hb_title_t of desired title.
482 * @param picture Index in title.
483 * @param buffer Handle to buufer were inage will be drawn.
485 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
488 hb_job_t * job = title->job;
491 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
493 AVPicture pic_in, pic_preview, pic_deint, pic_crop, pic_scale;
494 struct SwsContext * context;
496 int rgb_width = ((job->width + 7) >> 3) << 3;
499 swsflags = SWS_LANCZOS | SWS_ACCURATE_RND;
501 buf1 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
502 buf2 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
503 buf3 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, rgb_width, job->height ) );
504 buf4 = av_malloc( avpicture_get_size( PIX_FMT_RGB32, rgb_width, job->height ) );
505 avpicture_fill( &pic_in, buf1, PIX_FMT_YUV420P,
506 title->width, title->height );
507 avpicture_fill( &pic_deint, buf2, PIX_FMT_YUV420P,
508 title->width, title->height );
509 avpicture_fill( &pic_scale, buf3, PIX_FMT_YUV420P,
510 rgb_width, job->height );
511 avpicture_fill( &pic_preview, buf4, PIX_FMT_RGB32,
512 rgb_width, job->height );
514 // Allocate the AVPicture frames and fill in
516 memset( filename, 0, 1024 );
518 hb_get_tempory_filename( h, filename, "%d_%d_%d",
519 h->id, title->index, picture );
521 file = fopen( filename, "rb" );
524 hb_log( "hb_get_preview: fopen failed" );
528 fread( buf1, avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height), 1, file );
531 if( job->deinterlace )
533 // Deinterlace and crop
534 avpicture_deinterlace( &pic_deint, &pic_in, PIX_FMT_YUV420P, title->width, title->height );
535 av_picture_crop( &pic_crop, &pic_deint, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
540 av_picture_crop( &pic_crop, &pic_in, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
543 // Get scaling context
544 context = sws_getContext(title->width - (job->crop[2] + job->crop[3]),
545 title->height - (job->crop[0] + job->crop[1]),
547 job->width, job->height, PIX_FMT_YUV420P,
548 swsflags, NULL, NULL, NULL);
552 pic_crop.data, pic_crop.linesize,
553 0, title->height - (job->crop[0] + job->crop[1]),
554 pic_scale.data, pic_scale.linesize);
557 sws_freeContext( context );
559 // Get preview context
560 context = sws_getContext(rgb_width, job->height, PIX_FMT_YUV420P,
561 rgb_width, job->height, PIX_FMT_RGB32,
562 swsflags, NULL, NULL, NULL);
566 pic_scale.data, pic_scale.linesize,
568 pic_preview.data, pic_preview.linesize);
571 sws_freeContext( context );
573 preview_size = pic_preview.linesize[0];
575 for( i = 0; i < job->height; i++ )
577 memcpy( pen, buf4 + preview_size * i, 4 * job->width );
578 pen += 4 * job->width;
582 avpicture_free( &pic_preview );
583 avpicture_free( &pic_scale );
584 avpicture_free( &pic_deint );
585 avpicture_free( &pic_in );
589 * Analyzes a frame to detect interlacing artifacts
590 * and returns true if interlacing (combing) is found.
592 * Code taken from Thomas Oestreich's 32detect filter
593 * in the Transcode project, with minor formatting changes.
595 * @param buf An hb_buffer structure holding valid frame data
596 * @param width The frame's width in pixels
597 * @param height The frame's height in pixels
598 * @param color_equal Sensitivity for detecting similar colors
599 * @param color_diff Sensitivity for detecting different colors
600 * @param threshold Sensitivity for flagging planes as combed
601 * @param prog_equal Sensitivity for detecting similar colors on progressive frames
602 * @param prog_diff Sensitivity for detecting different colors on progressive frames
603 * @param prog_threshold Sensitivity for flagging progressive frames as combed
605 int hb_detect_comb( hb_buffer_t * buf, int width, int height, int color_equal, int color_diff, int threshold, int prog_equal, int prog_diff, int prog_threshold )
607 int j, k, n, off, cc_1, cc_2, cc[3];
608 // int flag[3] ; // debugging flag
609 uint16_t s1, s2, s3, s4;
614 if ( buf->flags & 16 )
616 /* Frame is progressive, be more discerning. */
617 color_diff = prog_diff;
618 color_equal = prog_equal;
619 threshold = prog_threshold;
622 /* One pas for Y, one pass for Cb, one pass for Cr */
623 for( k = 0; k < 3; k++ )
627 /* Y has already been checked, now offset by Y's dimensions
628 and divide all the other values by 2, since Cr and Cb
629 are half-size compared to Y. */
630 offset = width * height;
636 /* Y and Cb are done, so the offset needs to be bumped
637 so it's width*height + (width / 2) * (height / 2) */
641 for( j = 0; j < width; ++j )
645 for( n = 0; n < ( height - 4 ); n = n + 2 )
647 /* Look at groups of 4 sequential horizontal lines */
648 s1 = ( ( buf->data + offset )[ off + j ] & 0xff );
649 s2 = ( ( buf->data + offset )[ off + j + width ] & 0xff );
650 s3 = ( ( buf->data + offset )[ off + j + 2 * width ] & 0xff );
651 s4 = ( ( buf->data + offset )[ off + j + 3 * width ] & 0xff );
653 /* Note if the 1st and 2nd lines are more different in
654 color than the 1st and 3rd lines are similar in color.*/
655 if ( ( abs( s1 - s3 ) < color_equal ) &&
656 ( abs( s1 - s2 ) > color_diff ) )
659 /* Note if the 2nd and 3rd lines are more different in
660 color than the 2nd and 4th lines are similar in color.*/
661 if ( ( abs( s2 - s4 ) < color_equal ) &&
662 ( abs( s2 - s3 ) > color_diff) )
665 /* Now move down 2 horizontal lines before starting over.*/
671 /* The final cc score for a plane is the percentage of combed pixels it contains.
672 Because sensitivity goes down to hundreths of a percent, multiply by 1000
673 so it will be easy to compare against the threhold value which is an integer. */
674 cc[k] = (int)( ( cc_1 + cc_2 ) * 1000.0 / ( width * height ) );
678 /* HandBrake is all yuv420, so weight the average percentage of all 3 planes accordingly.*/
679 int average_cc = ( 2 * cc[0] + ( cc[1] / 2 ) + ( cc[2] / 2 ) ) / 3;
681 /* Now see if that average percentage of combed pixels surpasses the threshold percentage given by the user.*/
682 if( average_cc > threshold )
685 hb_log("Average %i combed (Threshold %i) %i/%i/%i | PTS: %"PRId64" (%fs) %s", average_cc, threshold, cc[0], cc[1], cc[2], buf->start, (float)buf->start / 90000, (buf->flags & 16) ? "Film" : "Video" );
691 hb_log("SKIPPED Average %i combed (Threshold %i) %i/%i/%i | PTS: %"PRId64" (%fs) %s", average_cc, threshold, cc[0], cc[1], cc[2], buf->start, (float)buf->start / 90000, (buf->flags & 16) ? "Film" : "Video" );
694 /* Reaching this point means no combing detected. */
700 * Calculates job width and height for anamorphic content,
702 * @param job Handle to hb_job_t
703 * @param output_width Pointer to returned storage width
704 * @param output_height Pointer to returned storage height
705 * @param output_par_width Pointer to returned pixel width
706 @ param output_par_height Pointer to returned pixel height
708 void hb_set_anamorphic_size( hb_job_t * job,
709 int *output_width, int *output_height,
710 int *output_par_width, int *output_par_height )
712 /* Set up some variables to make the math easier to follow. */
713 hb_title_t * title = job->title;
714 int cropped_width = title->width - job->crop[2] - job->crop[3] ;
715 int cropped_height = title->height - job->crop[0] - job->crop[1] ;
716 double storage_aspect = (double)cropped_width / (double)cropped_height;
717 int mod = job->modulus ? job->modulus : 16;
718 double aspect = title->aspect;
720 int pixel_aspect_width = job->anamorphic.par_width;
721 int pixel_aspect_height = job->anamorphic.par_height;
723 /* If a source was really NTSC or PAL and the user specified ITU PAR
724 values, replace the standard PAR values with the ITU broadcast ones. */
725 if( title->width == 720 && job->anamorphic.itu_par )
727 // convert aspect to a scaled integer so we can test for 16:9 & 4:3
728 // aspect ratios ignoring insignificant differences in the LSBs of
729 // the floating point representation.
730 int iaspect = aspect * 9.;
732 /* Handle ITU PARs */
733 if (title->height == 480)
738 /* It's widescreen */
739 pixel_aspect_width = 40;
740 pixel_aspect_height = 33;
742 else if (iaspect == 12)
745 pixel_aspect_width = 10;
746 pixel_aspect_height = 11;
749 else if (title->height == 576)
754 /* It's widescreen */
755 pixel_aspect_width = 16;
756 pixel_aspect_height = 11;
758 else if (iaspect == 12)
761 pixel_aspect_width = 12;
762 pixel_aspect_height = 11;
767 /* Figure out what width the source would display at. */
768 int source_display_width = cropped_width * (double)pixel_aspect_width /
769 (double)pixel_aspect_height ;
772 3 different ways of deciding output dimensions:
773 - 1: Strict anamorphic, preserve source dimensions
774 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
775 - 3: Power user anamorphic, specify everything
778 switch( job->anamorphic.mode )
781 /* Strict anamorphic */
782 *output_width = cropped_width;
783 *output_height = cropped_height;
784 *output_par_width = title->pixel_aspect_width;
785 *output_par_height = title->pixel_aspect_height;
789 /* "Loose" anamorphic.
790 - Uses mod16-compliant dimensions,
791 - Allows users to set the width
794 // height: Gets set later, ignore user job->height value
796 /* Gotta handle bounding dimensions.
797 If the width is too big, just reset it with no rescaling.
798 Instead of using the aspect-scaled job height,
799 we need to see if the job width divided by the storage aspect
800 is bigger than the max. If so, set it to the max (this is sloppy).
801 If not, set job height to job width divided by storage aspect.
804 if ( job->maxWidth && (job->maxWidth < job->width) )
805 width = job->maxWidth;
807 /* Time to get picture width that divide cleanly.*/
808 width = MULTIPLE_MOD( width, mod);
810 /* Verify these new dimensions don't violate max height and width settings */
811 if ( job->maxWidth && (job->maxWidth < job->width) )
812 width = job->maxWidth;
814 height = ((double)width / storage_aspect) + 0.5;
816 if ( job->maxHeight && (job->maxHeight < height) )
817 height = job->maxHeight;
819 /* Time to get picture height that divide cleanly.*/
820 height = MULTIPLE_MOD( height, mod);
822 /* Verify these new dimensions don't violate max height and width settings */
823 if ( job->maxHeight && (job->maxHeight < height) )
824 height = job->maxHeight;
826 /* The film AR is the source's display width / cropped source height.
827 The output display width is the output height * film AR.
828 The output PAR is the output display width / output storage width. */
829 pixel_aspect_width = height * source_display_width / cropped_height;
830 pixel_aspect_height = width;
832 /* Pass the results back to the caller */
833 *output_width = width;
834 *output_height = height;
838 /* Anamorphic 3: Power User Jamboree
839 - Set everything based on specified values */
841 /* Use specified storage dimensions */
843 height = job->height;
845 /* Bind to max dimensions */
846 if( job->maxWidth && width > job->maxWidth )
847 width = job->maxWidth;
848 if( job->maxHeight && height > job->maxHeight )
849 height = job->maxHeight;
851 /* Time to get picture dimensions that divide cleanly.*/
852 width = MULTIPLE_MOD( width, mod);
853 height = MULTIPLE_MOD( height, mod);
855 /* Verify we're still within max dimensions */
856 if( job->maxWidth && width > job->maxWidth )
857 width = job->maxWidth - (mod/2);
858 if( job->maxHeight && height > job->maxHeight )
859 height = job->maxHeight - (mod/2);
861 /* Re-ensure we have picture dimensions that divide cleanly. */
862 width = MULTIPLE_MOD( width, mod );
863 height = MULTIPLE_MOD( height, mod );
865 /* That finishes the storage dimensions. On to display. */
866 if( job->anamorphic.dar_width && job->anamorphic.dar_height )
868 /* We need to adjust the PAR to produce this aspect. */
869 pixel_aspect_width = height * job->anamorphic.dar_width / job->anamorphic.dar_height;
870 pixel_aspect_height = width;
874 /* If we're doing ana 3 and not specifying a DAR, care needs to be taken.
875 This indicates a PAR is potentially being set by the interface. But
876 this is an output PAR, to correct a source, and it should not be assumed
877 that it properly creates a display aspect ratio when applied to the source,
878 which could easily be stored in a different resolution. */
879 if( job->anamorphic.keep_display_aspect )
881 /* We can ignore the possibility of a PAR change */
882 pixel_aspect_width = height * ( (double)source_display_width / (double)cropped_height );
883 pixel_aspect_height = width;
887 int output_display_width = width * (double)pixel_aspect_width /
888 (double)pixel_aspect_height;
889 pixel_aspect_width = output_display_width;
890 pixel_aspect_height = width;
895 *output_width = width;
896 *output_height = height;
900 /* While x264 is smart enough to reduce fractions on its own, libavcodec
901 needs some help with the math, so lose superfluous factors. */
902 hb_reduce( output_par_width, output_par_height,
903 pixel_aspect_width, pixel_aspect_height );
907 * Calculates job width, height, and cropping parameters.
908 * @param job Handle to hb_job_t.
909 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
910 * @param pixels Maximum desired pixel count.
912 void hb_set_size( hb_job_t * job, double aspect, int pixels )
914 hb_title_t * title = job->title;
916 int croppedWidth = title->width - title->crop[2] - title->crop[3];
917 int croppedHeight = title->height - title->crop[0] - title->crop[1];
918 double croppedAspect = title->aspect * title->height * croppedWidth /
919 croppedHeight / title->width;
925 /* Keep the best possible aspect ratio */
926 aspect = croppedAspect;
929 /* Crop if necessary to obtain the desired ratio */
930 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
931 if( aspect < croppedAspect )
933 /* Need to crop on the left and right */
934 addCrop = croppedWidth - aspect * croppedHeight * title->width /
935 title->aspect / title->height;
938 addCrop = ( addCrop + 1 ) / 2;
939 job->crop[2] += addCrop;
940 job->crop[3] += addCrop;
942 else if( addCrop & 2 )
945 job->crop[2] += addCrop - 1;
946 job->crop[3] += addCrop + 1;
951 job->crop[2] += addCrop;
952 job->crop[3] += addCrop;
955 else if( aspect > croppedAspect )
957 /* Need to crop on the top and bottom */
958 addCrop = croppedHeight - croppedWidth * title->aspect *
959 title->height / aspect / title->width;
962 addCrop = ( addCrop + 1 ) / 2;
963 job->crop[0] += addCrop;
964 job->crop[1] += addCrop;
966 else if( addCrop & 2 )
969 job->crop[0] += addCrop - 1;
970 job->crop[1] += addCrop + 1;
975 job->crop[0] += addCrop;
976 job->crop[1] += addCrop;
980 /* Compute a resolution from the number of pixels and aspect */
984 h = MULTIPLE_16( (int)( (double)w / aspect ) );
992 job->height = MULTIPLE_16( (int)( (double)job->width / aspect ) );
996 * Returns the number of jobs in the queue.
997 * @param h Handle to hb_handle_t.
998 * @return Number of jobs.
1000 int hb_count( hb_handle_t * h )
1002 return hb_list_count( h->jobs );
1006 * Returns handle to job at index i within the job list.
1007 * @param h Handle to hb_handle_t.
1008 * @param i Index of job.
1009 * @returns Handle to hb_job_t of desired job.
1011 hb_job_t * hb_job( hb_handle_t * h, int i )
1013 return hb_list_item( h->jobs, i );
1016 hb_job_t * hb_current_job( hb_handle_t * h )
1018 return( h->current_job );
1022 * Adds a job to the job list.
1023 * @param h Handle to hb_handle_t.
1024 * @param job Handle to hb_job_t.
1026 void hb_add( hb_handle_t * h, hb_job_t * job )
1028 hb_job_t * job_copy;
1029 hb_title_t * title, * title_copy;
1030 hb_chapter_t * chapter, * chapter_copy;
1032 hb_subtitle_t * subtitle, * subtitle_copy;
1036 /* Copy the title */
1038 title_copy = malloc( sizeof( hb_title_t ) );
1039 memcpy( title_copy, title, sizeof( hb_title_t ) );
1041 title_copy->list_chapter = hb_list_init();
1042 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
1044 chapter = hb_list_item( title->list_chapter, i );
1045 chapter_copy = malloc( sizeof( hb_chapter_t ) );
1046 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
1047 hb_list_add( title_copy->list_chapter, chapter_copy );
1053 if( title->metadata )
1055 title_copy->metadata = malloc( sizeof( hb_metadata_t ) );
1057 if( title_copy->metadata )
1059 memcpy( title_copy->metadata, title->metadata, sizeof( hb_metadata_t ) );
1062 * Need to copy the artwork seperatly (TODO).
1064 if( title->metadata->coverart )
1066 title_copy->metadata->coverart = malloc( title->metadata->coverart_size );
1067 if( title_copy->metadata->coverart )
1069 memcpy( title_copy->metadata->coverart, title->metadata->coverart,
1070 title->metadata->coverart_size );
1072 title_copy->metadata->coverart_size = 0;
1078 /* Copy the audio track(s) we want */
1079 title_copy->list_audio = hb_list_init();
1081 for( i = 0; i < hb_list_count(job->list_audio); i++ )
1083 if( ( audio = hb_list_item( job->list_audio, i ) ) )
1085 hb_list_add( title_copy->list_audio, hb_audio_copy(audio) );
1089 title_copy->list_subtitle = hb_list_init();
1092 * The following code is confusing, there are two ways in which
1093 * we select subtitles and it depends on whether this is single or
1096 * subtitle_scan may be enabled, in which case the first pass
1097 * scans all subtitles of that language. The second pass does not
1098 * select any because they are set at the end of the first pass.
1100 * We may have manually selected a subtitle, in which case that is
1101 * selected in the first pass of a single pass, or the second of a
1104 memset( audio_lang, 0, sizeof( audio_lang ) );
1106 if ( job->indepth_scan ) {
1109 * Find the first audio language that is being encoded
1111 for( i = 0; i < hb_list_count(job->list_audio); i++ )
1113 if( ( audio = hb_list_item( job->list_audio, i ) ) )
1115 strncpy(audio_lang, audio->config.lang.iso639_2, sizeof(audio_lang));
1122 * If doing a subtitle scan then add all the matching subtitles for this
1125 if ( job->indepth_scan )
1127 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
1129 subtitle = hb_list_item( title->list_subtitle, i );
1130 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 &&
1131 subtitle->source == VOBSUB )
1134 * Matched subtitle language with audio language, so
1135 * add this to our list to scan.
1137 * We will update the subtitle list on the second pass
1138 * later after the first pass has completed.
1140 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1141 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1142 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1147 * Not doing a subtitle scan in this pass, but maybe we are in the
1150 if( job->pass != 1 )
1153 * Copy all of them from the input job, to the title_copy/job_copy.
1155 for( i = 0; i < hb_list_count(job->list_subtitle); i++ ) {
1156 if( ( subtitle = hb_list_item( job->list_subtitle, i ) ) )
1158 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1159 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1160 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1167 job_copy = calloc( sizeof( hb_job_t ), 1 );
1168 memcpy( job_copy, job, sizeof( hb_job_t ) );
1169 title_copy->job = job_copy;
1170 job_copy->title = title_copy;
1171 job_copy->list_audio = title_copy->list_audio;
1172 job_copy->list_subtitle = title_copy->list_subtitle; // sharing list between title and job
1173 job_copy->file = strdup( job->file );
1175 job_copy->pause = h->pause_lock;
1177 /* Copy the job filter list */
1181 int filter_count = hb_list_count( job->filters );
1182 job_copy->filters = hb_list_init();
1183 for( i = 0; i < filter_count; i++ )
1186 * Copy the filters, since the MacGui reuses the global filter objects
1187 * meaning that queued up jobs overwrite the previous filter settings.
1188 * In reality, settings is probably the only field that needs duplicating
1189 * since it's the only value that is ever changed. But name is duplicated
1190 * as well for completeness. Not copying private_data since it gets
1191 * created for each job in renderInit.
1193 hb_filter_object_t * filter = hb_list_item( job->filters, i );
1194 hb_filter_object_t * filter_copy = malloc( sizeof( hb_filter_object_t ) );
1195 memcpy( filter_copy, filter, sizeof( hb_filter_object_t ) );
1197 filter_copy->name = strdup( filter->name );
1198 if( filter->settings )
1199 filter_copy->settings = strdup( filter->settings );
1200 hb_list_add( job_copy->filters, filter_copy );
1204 /* Add the job to the list */
1205 hb_list_add( h->jobs, job_copy );
1206 h->job_count = hb_count(h);
1207 h->job_count_permanent++;
1211 * Removes a job from the job list.
1212 * @param h Handle to hb_handle_t.
1213 * @param job Handle to hb_job_t.
1215 void hb_rem( hb_handle_t * h, hb_job_t * job )
1217 hb_list_rem( h->jobs, job );
1219 h->job_count = hb_count(h);
1220 if (h->job_count_permanent)
1221 h->job_count_permanent--;
1223 /* XXX free everything XXX */
1227 * Starts the conversion process.
1228 * Sets state to HB_STATE_WORKING.
1229 * calls hb_work_init, to launch work thread. Stores handle to work thread.
1230 * @param h Handle to hb_handle_t.
1232 void hb_start( hb_handle_t * h )
1235 h->job_count = hb_list_count( h->jobs );
1236 h->job_count_permanent = h->job_count;
1238 hb_lock( h->state_lock );
1239 h->state.state = HB_STATE_WORKING;
1240 #define p h->state.param.working
1243 p.job_count = h->job_count;
1251 hb_unlock( h->state_lock );
1256 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
1257 &h->work_die, &h->work_error, &h->current_job );
1261 * Pauses the conversion process.
1262 * @param h Handle to hb_handle_t.
1264 void hb_pause( hb_handle_t * h )
1268 hb_lock( h->pause_lock );
1271 hb_current_job( h )->st_pause_date = hb_get_date();
1273 hb_lock( h->state_lock );
1274 h->state.state = HB_STATE_PAUSED;
1275 hb_unlock( h->state_lock );
1280 * Resumes the conversion process.
1281 * @param h Handle to hb_handle_t.
1283 void hb_resume( hb_handle_t * h )
1287 #define job hb_current_job( h )
1288 if( job->st_pause_date != -1 )
1290 job->st_paused += hb_get_date() - job->st_pause_date;
1294 hb_unlock( h->pause_lock );
1300 * Stops the conversion process.
1301 * @param h Handle to hb_handle_t.
1303 void hb_stop( hb_handle_t * h )
1307 h->job_count = hb_count(h);
1308 h->job_count_permanent = 0;
1314 * Stops the conversion process.
1315 * @param h Handle to hb_handle_t.
1317 void hb_scan_stop( hb_handle_t * h )
1321 h->job_count = hb_count(h);
1322 h->job_count_permanent = 0;
1328 * Returns the state of the conversion process.
1329 * @param h Handle to hb_handle_t.
1330 * @param s Handle to hb_state_t which to copy the state data.
1332 void hb_get_state( hb_handle_t * h, hb_state_t * s )
1334 hb_lock( h->state_lock );
1336 memcpy( s, &h->state, sizeof( hb_state_t ) );
1337 if ( h->state.state == HB_STATE_SCANDONE || h->state.state == HB_STATE_WORKDONE )
1338 h->state.state = HB_STATE_IDLE;
1340 hb_unlock( h->state_lock );
1343 void hb_get_state2( hb_handle_t * h, hb_state_t * s )
1345 hb_lock( h->state_lock );
1347 memcpy( s, &h->state, sizeof( hb_state_t ) );
1349 hb_unlock( h->state_lock );
1353 * Called in MacGui in UpdateUI to check
1354 * for a new scan being completed to set a new source
1356 int hb_get_scancount( hb_handle_t * h)
1358 return h->scanCount;
1362 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init.
1363 * @param _h Pointer to handle to hb_handle_t.
1365 void hb_close( hb_handle_t ** _h )
1367 hb_handle_t * h = *_h;
1372 hb_thread_close( &h->main_thread );
1374 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1376 hb_list_rem( h->list_title, title );
1377 if( title->job && title->job->filters )
1379 hb_list_close( &title->job->filters );
1382 hb_title_close( &title );
1384 hb_list_close( &h->list_title );
1386 hb_list_close( &h->jobs );
1387 hb_lock_close( &h->state_lock );
1388 hb_lock_close( &h->pause_lock );
1395 * Cleans up libhb at a process level. Call before the app closes. Removes preview directory.
1397 void hb_global_close()
1401 struct dirent * entry;
1403 /* Find and remove temp folder */
1404 memset( dirname, 0, 1024 );
1405 hb_get_temporary_directory( dirname );
1407 dir = opendir( dirname );
1410 while( ( entry = readdir( dir ) ) )
1412 char filename[1024];
1413 if( entry->d_name[0] == '.' )
1417 memset( filename, 0, 1024 );
1418 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
1427 * Monitors the state of the update, scan, and work threads.
1428 * Sets scan done state when scan thread exits.
1429 * Sets work done state when work thread exits.
1430 * @param _h Handle to hb_handle_t
1432 static void thread_func( void * _h )
1434 hb_handle_t * h = (hb_handle_t *) _h;
1439 /* Create folder for temporary files */
1440 memset( dirname, 0, 1024 );
1441 hb_get_temporary_directory( dirname );
1443 hb_mkdir( dirname );
1447 /* In case the check_update thread hangs, it'll die sooner or
1448 later. Then, we join it here */
1449 if( h->update_thread &&
1450 hb_thread_has_exited( h->update_thread ) )
1452 hb_thread_close( &h->update_thread );
1455 /* Check if the scan thread is done */
1456 if( h->scan_thread &&
1457 hb_thread_has_exited( h->scan_thread ) )
1459 hb_thread_close( &h->scan_thread );
1465 hb_remove_previews( h );
1466 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1468 hb_list_rem( h->list_title, title );
1469 hb_title_close( &title );
1472 hb_log( "hb_scan: canceled" );
1476 hb_log( "libhb: scan thread found %d valid title(s)",
1477 hb_list_count( h->list_title ) );
1479 hb_lock( h->state_lock );
1480 h->state.state = HB_STATE_SCANDONE; //originally state.state
1481 hb_unlock( h->state_lock );
1482 /*we increment this sessions scan count by one for the MacGui
1483 to trigger a new source being set */
1487 /* Check if the work thread is done */
1488 if( h->work_thread &&
1489 hb_thread_has_exited( h->work_thread ) )
1491 hb_thread_close( &h->work_thread );
1493 hb_log( "libhb: work result = %d",
1495 hb_lock( h->state_lock );
1496 h->state.state = HB_STATE_WORKDONE;
1497 h->state.param.workdone.error = h->work_error;
1499 h->job_count = hb_count(h);
1500 if (h->job_count < 1)
1501 h->job_count_permanent = 0;
1502 hb_unlock( h->state_lock );
1508 if( h->scan_thread )
1511 hb_thread_close( &h->scan_thread );
1513 if( h->work_thread )
1516 hb_thread_close( &h->work_thread );
1518 hb_remove_previews( h );
1523 * @param h Handle to hb_handle_t
1525 int hb_get_pid( hb_handle_t * h )
1531 * Returns the id for the given instance.
1532 * @param h Handle to hb_handle_t
1534 int hb_get_instance_id( hb_handle_t * h )
1540 * Sets the current state.
1541 * @param h Handle to hb_handle_t
1542 * @param s Handle to new hb_state_t
1544 void hb_set_state( hb_handle_t * h, hb_state_t * s )
1546 hb_lock( h->pause_lock );
1547 hb_lock( h->state_lock );
1548 memcpy( &h->state, s, sizeof( hb_state_t ) );
1549 if( h->state.state == HB_STATE_WORKING ||
1550 h->state.state == HB_STATE_SEARCHING )
1553 if (h->job_count < 1)
1554 h->job_count_permanent = 1;
1556 h->state.param.working.job_cur =
1557 h->job_count_permanent - hb_list_count( h->jobs );
1558 h->state.param.working.job_count = h->job_count_permanent;
1560 // Set which job is being worked on
1562 h->state.param.working.sequence_id = h->current_job->sequence_id;
1564 h->state.param.working.sequence_id = 0;
1566 hb_unlock( h->state_lock );
1567 hb_unlock( h->pause_lock );
1570 /* Passes a pointer to persistent data */
1571 hb_interjob_t * hb_interjob_get( hb_handle_t * h )