8 /* The "Check for update" thread */
11 hb_thread_t * update_thread;
13 /* This thread's only purpose is to check other threads'
16 hb_thread_t * main_thread;
19 /* DVD/file scan thread */
20 hb_list_t * list_title;
21 hb_thread_t * scan_thread;
23 /* The thread which processes the jobs. Others threads are launched
24 from this one (see work.c) */
26 hb_job_t * current_job;
28 int job_count_permanent;
29 volatile int work_die;
31 hb_thread_t * work_thread;
35 hb_lock_t * state_lock;
39 hb_lock_t * pause_lock;
40 /* For MacGui active queue
41 increments each time the scan thread completes*/
43 volatile int scan_die;
45 /* Stash of persistent data between jobs, for stuff
46 like correcting frame count and framerate estimates
47 on multi-pass encodes where frames get dropped. */
48 hb_interjob_t * interjob;
52 hb_lock_t *hb_avcodec_lock;
53 hb_work_object_t * hb_objects = NULL;
54 int hb_instance_counter = 0;
55 int hb_process_initialized = 0;
57 static void thread_func( void * );
59 void hb_avcodec_init()
61 hb_avcodec_lock = hb_lock_init();
65 int hb_avcodec_open(AVCodecContext *avctx, AVCodec *codec)
68 hb_lock( hb_avcodec_lock );
69 ret = avcodec_open(avctx, codec);
70 hb_unlock( hb_avcodec_lock );
74 int hb_avcodec_close(AVCodecContext *avctx)
77 hb_lock( hb_avcodec_lock );
78 ret = avcodec_close(avctx);
79 hb_unlock( hb_avcodec_lock );
84 * Registers work objects, by adding the work object to a liked list.
85 * @param w Handle to hb_work_object_t to register.
87 void hb_register( hb_work_object_t * w )
94 * libhb initialization routine.
95 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
96 * @param update_check signals libhb to check for updated version from HandBrake website.
97 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
99 hb_handle_t * hb_init( int verbose, int update_check )
101 if (!hb_process_initialized)
104 #if defined( _WIN32 ) || defined( __MINGW32__ )
105 pthread_win32_process_attach_np();
108 hb_process_initialized =1;
111 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
114 /* See hb_deep_log() and hb_log() in common.c */
115 global_verbosity_level = verbose;
117 putenv( "HB_DEBUG=1" );
119 h->id = hb_instance_counter++;
121 /* Check for an update on the website if asked to */
126 hb_log( "hb_init: checking for updates" );
127 date = hb_get_date();
128 h->update_thread = hb_update_init( &h->build, h->version );
132 if( hb_thread_has_exited( h->update_thread ) )
134 /* Immediate success or failure */
135 hb_thread_close( &h->update_thread );
138 if( hb_get_date() > date + 1000 )
140 /* Still nothing after one second. Connection problem,
141 let the thread die */
142 hb_log( "hb_init: connection problem, not waiting for "
151 * Initialise buffer pool
153 hb_buffer_pool_init();
155 /* CPU count detection */
156 hb_log( "hb_init: checking cpu count" );
157 h->cpu_count = hb_get_cpu_count();
159 h->list_title = hb_list_init();
160 h->jobs = hb_list_init();
162 h->state_lock = hb_lock_init();
163 h->state.state = HB_STATE_IDLE;
165 h->pause_lock = hb_lock_init();
167 h->interjob = calloc( sizeof( hb_interjob_t ), 1 );
172 /* Start library thread */
173 hb_log( "hb_init: starting libhb thread" );
175 h->main_thread = hb_thread_init( "libhb", thread_func, h,
176 HB_NORMAL_PRIORITY );
177 hb_register( &hb_sync_video );
178 hb_register( &hb_sync_audio );
179 hb_register( &hb_decmpeg2 );
180 hb_register( &hb_decvobsub );
181 hb_register( &hb_encvobsub );
182 hb_register( &hb_deccc608 );
183 hb_register( &hb_decsrtsub );
184 hb_register( &hb_render );
185 hb_register( &hb_encavcodec );
186 hb_register( &hb_encx264 );
187 hb_register( &hb_enctheora );
188 hb_register( &hb_deca52 );
189 hb_register( &hb_decdca );
190 hb_register( &hb_decavcodec );
191 hb_register( &hb_decavcodecv );
192 hb_register( &hb_decavcodecvi );
193 hb_register( &hb_decavcodecai );
194 hb_register( &hb_declpcm );
195 hb_register( &hb_encfaac );
196 hb_register( &hb_enclame );
197 hb_register( &hb_encvorbis );
198 hb_register( &hb_muxer );
200 hb_register( &hb_encca_aac );
207 * libhb initialization routine.
208 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
209 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
210 * @param update_check signals libhb to check for updated version from HandBrake website.
211 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
213 hb_handle_t * hb_init_dl( int verbose, int update_check )
215 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
218 /* See hb_log() in common.c */
219 if( verbose > HB_DEBUG_NONE )
221 putenv( "HB_DEBUG=1" );
224 h->id = hb_instance_counter++;
226 /* Check for an update on the website if asked to */
231 hb_log( "hb_init: checking for updates" );
232 date = hb_get_date();
233 h->update_thread = hb_update_init( &h->build, h->version );
237 if( hb_thread_has_exited( h->update_thread ) )
239 /* Immediate success or failure */
240 hb_thread_close( &h->update_thread );
243 if( hb_get_date() > date + 1000 )
245 /* Still nothing after one second. Connection problem,
246 let the thread die */
247 hb_log( "hb_init: connection problem, not waiting for "
255 /* CPU count detection */
256 hb_log( "hb_init: checking cpu count" );
257 h->cpu_count = hb_get_cpu_count();
259 h->list_title = hb_list_init();
260 h->jobs = hb_list_init();
261 h->current_job = NULL;
263 h->state_lock = hb_lock_init();
264 h->state.state = HB_STATE_IDLE;
266 h->pause_lock = hb_lock_init();
270 avcodec_register_all();
272 /* Start library thread */
273 hb_log( "hb_init: starting libhb thread" );
275 h->main_thread = hb_thread_init( "libhb", thread_func, h,
276 HB_NORMAL_PRIORITY );
278 hb_register( &hb_sync_video );
279 hb_register( &hb_sync_audio );
280 hb_register( &hb_decmpeg2 );
281 hb_register( &hb_decvobsub );
282 hb_register( &hb_encvobsub );
283 hb_register( &hb_deccc608 );
284 hb_register( &hb_decsrtsub );
285 hb_register( &hb_render );
286 hb_register( &hb_encavcodec );
287 hb_register( &hb_encx264 );
288 hb_register( &hb_enctheora );
289 hb_register( &hb_deca52 );
290 hb_register( &hb_decdca );
291 hb_register( &hb_decavcodec );
292 hb_register( &hb_decavcodecv );
293 hb_register( &hb_decavcodecvi );
294 hb_register( &hb_decavcodecai );
295 hb_register( &hb_declpcm );
296 hb_register( &hb_encfaac );
297 hb_register( &hb_enclame );
298 hb_register( &hb_encvorbis );
299 hb_register( &hb_muxer );
301 hb_register( &hb_encca_aac );
309 * Returns current version of libhb.
310 * @param h Handle to hb_handle_t.
311 * @return character array of version number.
313 char * hb_get_version( hb_handle_t * h )
315 return HB_PROJECT_VERSION;
319 * Returns current build of libhb.
320 * @param h Handle to hb_handle_t.
321 * @return character array of build number.
323 int hb_get_build( hb_handle_t * h )
325 return HB_PROJECT_BUILD;
329 * Checks for needed update.
330 * @param h Handle to hb_handle_t.
331 * @param version Pointer to handle where version will be copied.
332 * @return update indicator.
334 int hb_check_update( hb_handle_t * h, char ** version )
336 *version = ( h->build < 0 ) ? NULL : h->version;
341 * Sets the cpu count to the desired value.
342 * @param h Handle to hb_handle_t
343 * @param cpu_count Number of CPUs to use.
345 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
347 cpu_count = MAX( 1, cpu_count );
348 cpu_count = MIN( cpu_count, 8 );
349 h->cpu_count = cpu_count;
353 * Deletes current previews associated with titles
354 * @param h Handle to hb_handle_t
356 void hb_remove_previews( hb_handle_t * h )
363 struct dirent * entry;
365 memset( dirname, 0, 1024 );
366 hb_get_temporary_directory( dirname );
367 dir = opendir( dirname );
368 if (dir == NULL) return;
370 count = hb_list_count( h->list_title );
371 while( ( entry = readdir( dir ) ) )
373 if( entry->d_name[0] == '.' )
377 for( i = 0; i < count; i++ )
379 title = hb_list_item( h->list_title, i );
380 len = snprintf( filename, 1024, "%d_%d", h->id, title->index );
381 if (strncmp(entry->d_name, filename, len) == 0)
383 snprintf( filename, 1024, "%s/%s", dirname, entry->d_name );
393 * Initializes a scan of the by calling hb_scan_init
394 * @param h Handle to hb_handle_t
395 * @param path location of VIDEO_TS folder.
396 * @param title_index Desired title to scan. 0 for all titles.
397 * @param preview_count Number of preview images to generate.
398 * @param store_previews Whether or not to write previews to disk.
400 void hb_scan( hb_handle_t * h, const char * path, int title_index,
401 int preview_count, int store_previews )
407 /* Clean up from previous scan */
408 hb_remove_previews( h );
409 while( ( title = hb_list_item( h->list_title, 0 ) ) )
411 hb_list_rem( h->list_title, title );
412 hb_title_close( &title );
415 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
416 h->scan_thread = hb_scan_init( h, &h->scan_die, path, title_index,
417 h->list_title, preview_count,
422 * Returns the list of titles found.
423 * @param h Handle to hb_handle_t
424 * @return Handle to hb_list_t of the title list.
426 hb_list_t * hb_get_titles( hb_handle_t * h )
428 return h->list_title;
432 * Create preview image of desired title a index of picture.
433 * @param h Handle to hb_handle_t.
434 * @param title Handle to hb_title_t of desired title.
435 * @param picture Index in title.
436 * @param buffer Handle to buufer were inage will be drawn.
438 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
441 hb_job_t * job = title->job;
444 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
446 AVPicture pic_in, pic_preview, pic_deint, pic_crop, pic_scale;
447 struct SwsContext * context;
449 int rgb_width = ((job->width + 7) >> 3) << 3;
452 swsflags = SWS_LANCZOS | SWS_ACCURATE_RND;
454 buf1 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
455 buf2 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
456 buf3 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, rgb_width, job->height ) );
457 buf4 = av_malloc( avpicture_get_size( PIX_FMT_RGB32, rgb_width, job->height ) );
458 avpicture_fill( &pic_in, buf1, PIX_FMT_YUV420P,
459 title->width, title->height );
460 avpicture_fill( &pic_deint, buf2, PIX_FMT_YUV420P,
461 title->width, title->height );
462 avpicture_fill( &pic_scale, buf3, PIX_FMT_YUV420P,
463 rgb_width, job->height );
464 avpicture_fill( &pic_preview, buf4, PIX_FMT_RGB32,
465 rgb_width, job->height );
467 // Allocate the AVPicture frames and fill in
469 memset( filename, 0, 1024 );
471 hb_get_tempory_filename( h, filename, "%d_%d_%d",
472 h->id, title->index, picture );
474 file = fopen( filename, "rb" );
477 hb_log( "hb_get_preview: fopen failed" );
481 fread( buf1, avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height), 1, file );
484 if( job->deinterlace )
486 // Deinterlace and crop
487 avpicture_deinterlace( &pic_deint, &pic_in, PIX_FMT_YUV420P, title->width, title->height );
488 av_picture_crop( &pic_crop, &pic_deint, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
493 av_picture_crop( &pic_crop, &pic_in, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
496 // Get scaling context
497 context = sws_getContext(title->width - (job->crop[2] + job->crop[3]),
498 title->height - (job->crop[0] + job->crop[1]),
500 job->width, job->height, PIX_FMT_YUV420P,
501 swsflags, NULL, NULL, NULL);
505 pic_crop.data, pic_crop.linesize,
506 0, title->height - (job->crop[0] + job->crop[1]),
507 pic_scale.data, pic_scale.linesize);
510 sws_freeContext( context );
512 // Get preview context
513 context = sws_getContext(rgb_width, job->height, PIX_FMT_YUV420P,
514 rgb_width, job->height, PIX_FMT_RGB32,
515 swsflags, NULL, NULL, NULL);
519 pic_scale.data, pic_scale.linesize,
521 pic_preview.data, pic_preview.linesize);
524 sws_freeContext( context );
526 preview_size = pic_preview.linesize[0];
528 for( i = 0; i < job->height; i++ )
530 memcpy( pen, buf4 + preview_size * i, 4 * job->width );
531 pen += 4 * job->width;
535 avpicture_free( &pic_preview );
536 avpicture_free( &pic_scale );
537 avpicture_free( &pic_deint );
538 avpicture_free( &pic_in );
542 * Analyzes a frame to detect interlacing artifacts
543 * and returns true if interlacing (combing) is found.
545 * Code taken from Thomas Oestreich's 32detect filter
546 * in the Transcode project, with minor formatting changes.
548 * @param buf An hb_buffer structure holding valid frame data
549 * @param width The frame's width in pixels
550 * @param height The frame's height in pixels
551 * @param color_equal Sensitivity for detecting similar colors
552 * @param color_diff Sensitivity for detecting different colors
553 * @param threshold Sensitivity for flagging planes as combed
554 * @param prog_equal Sensitivity for detecting similar colors on progressive frames
555 * @param prog_diff Sensitivity for detecting different colors on progressive frames
556 * @param prog_threshold Sensitivity for flagging progressive frames as combed
558 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 )
560 int j, k, n, off, cc_1, cc_2, cc[3];
561 // int flag[3] ; // debugging flag
562 uint16_t s1, s2, s3, s4;
567 if ( buf->flags & 16 )
569 /* Frame is progressive, be more discerning. */
570 color_diff = prog_diff;
571 color_equal = prog_equal;
572 threshold = prog_threshold;
575 /* One pas for Y, one pass for Cb, one pass for Cr */
576 for( k = 0; k < 3; k++ )
580 /* Y has already been checked, now offset by Y's dimensions
581 and divide all the other values by 2, since Cr and Cb
582 are half-size compared to Y. */
583 offset = width * height;
589 /* Y and Cb are done, so the offset needs to be bumped
590 so it's width*height + (width / 2) * (height / 2) */
594 for( j = 0; j < width; ++j )
598 for( n = 0; n < ( height - 4 ); n = n + 2 )
600 /* Look at groups of 4 sequential horizontal lines */
601 s1 = ( ( buf->data + offset )[ off + j ] & 0xff );
602 s2 = ( ( buf->data + offset )[ off + j + width ] & 0xff );
603 s3 = ( ( buf->data + offset )[ off + j + 2 * width ] & 0xff );
604 s4 = ( ( buf->data + offset )[ off + j + 3 * width ] & 0xff );
606 /* Note if the 1st and 2nd lines are more different in
607 color than the 1st and 3rd lines are similar in color.*/
608 if ( ( abs( s1 - s3 ) < color_equal ) &&
609 ( abs( s1 - s2 ) > color_diff ) )
612 /* Note if the 2nd and 3rd lines are more different in
613 color than the 2nd and 4th lines are similar in color.*/
614 if ( ( abs( s2 - s4 ) < color_equal ) &&
615 ( abs( s2 - s3 ) > color_diff) )
618 /* Now move down 2 horizontal lines before starting over.*/
624 /* The final cc score for a plane is the percentage of combed pixels it contains.
625 Because sensitivity goes down to hundreths of a percent, multiply by 1000
626 so it will be easy to compare against the threhold value which is an integer. */
627 cc[k] = (int)( ( cc_1 + cc_2 ) * 1000.0 / ( width * height ) );
631 /* HandBrake is all yuv420, so weight the average percentage of all 3 planes accordingly.*/
632 int average_cc = ( 2 * cc[0] + ( cc[1] / 2 ) + ( cc[2] / 2 ) ) / 3;
634 /* Now see if that average percentage of combed pixels surpasses the threshold percentage given by the user.*/
635 if( average_cc > threshold )
638 hb_log("Average %i combed (Threshold %i) %i/%i/%i | PTS: %lld (%fs) %s", average_cc, threshold, cc[0], cc[1], cc[2], buf->start, (float)buf->start / 90000, (buf->flags & 16) ? "Film" : "Video" );
644 hb_log("SKIPPED Average %i combed (Threshold %i) %i/%i/%i | PTS: %lld (%fs) %s", average_cc, threshold, cc[0], cc[1], cc[2], buf->start, (float)buf->start / 90000, (buf->flags & 16) ? "Film" : "Video" );
647 /* Reaching this point means no combing detected. */
653 * Calculates job width and height for anamorphic content,
655 * @param job Handle to hb_job_t
656 * @param output_width Pointer to returned storage width
657 * @param output_height Pointer to returned storage height
658 * @param output_par_width Pointer to returned pixel width
659 @ param output_par_height Pointer to returned pixel height
661 void hb_set_anamorphic_size( hb_job_t * job,
662 int *output_width, int *output_height,
663 int *output_par_width, int *output_par_height )
665 /* Set up some variables to make the math easier to follow. */
666 hb_title_t * title = job->title;
667 int cropped_width = title->width - job->crop[2] - job->crop[3] ;
668 int cropped_height = title->height - job->crop[0] - job->crop[1] ;
669 double storage_aspect = (double)cropped_width / (double)cropped_height;
670 int mod = job->modulus ? job->modulus : 16;
671 double aspect = title->aspect;
673 int pixel_aspect_width = job->anamorphic.par_width;
674 int pixel_aspect_height = job->anamorphic.par_height;
676 /* If a source was really NTSC or PAL and the user specified ITU PAR
677 values, replace the standard PAR values with the ITU broadcast ones. */
678 if( title->width == 720 && job->anamorphic.itu_par )
680 // convert aspect to a scaled integer so we can test for 16:9 & 4:3
681 // aspect ratios ignoring insignificant differences in the LSBs of
682 // the floating point representation.
683 int iaspect = aspect * 9.;
685 /* Handle ITU PARs */
686 if (title->height == 480)
691 /* It's widescreen */
692 pixel_aspect_width = 40;
693 pixel_aspect_height = 33;
695 else if (iaspect == 12)
698 pixel_aspect_width = 10;
699 pixel_aspect_height = 11;
702 else if (title->height == 576)
707 /* It's widescreen */
708 pixel_aspect_width = 16;
709 pixel_aspect_height = 11;
711 else if (iaspect == 12)
714 pixel_aspect_width = 12;
715 pixel_aspect_height = 11;
720 /* Figure out what width the source would display at. */
721 int source_display_width = cropped_width * (double)pixel_aspect_width /
722 (double)pixel_aspect_height ;
725 3 different ways of deciding output dimensions:
726 - 1: Strict anamorphic, preserve source dimensions
727 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
728 - 3: Power user anamorphic, specify everything
731 switch( job->anamorphic.mode )
734 /* Strict anamorphic */
735 *output_width = cropped_width;
736 *output_height = cropped_height;
737 *output_par_width = title->pixel_aspect_width;
738 *output_par_height = title->pixel_aspect_height;
742 /* "Loose" anamorphic.
743 - Uses mod16-compliant dimensions,
744 - Allows users to set the width
747 // height: Gets set later, ignore user job->height value
749 /* Gotta handle bounding dimensions.
750 If the width is too big, just reset it with no rescaling.
751 Instead of using the aspect-scaled job height,
752 we need to see if the job width divided by the storage aspect
753 is bigger than the max. If so, set it to the max (this is sloppy).
754 If not, set job height to job width divided by storage aspect.
757 if ( job->maxWidth && (job->maxWidth < job->width) )
758 width = job->maxWidth;
760 /* Time to get picture width that divide cleanly.*/
761 width = MULTIPLE_MOD( width, mod);
763 /* Verify these new dimensions don't violate max height and width settings */
764 if ( job->maxWidth && (job->maxWidth < job->width) )
765 width = job->maxWidth;
767 height = ((double)width / storage_aspect) + 0.5;
769 if ( job->maxHeight && (job->maxHeight < height) )
770 height = job->maxHeight;
772 /* Time to get picture height that divide cleanly.*/
773 height = MULTIPLE_MOD( height, mod);
775 /* Verify these new dimensions don't violate max height and width settings */
776 if ( job->maxHeight && (job->maxHeight < height) )
777 height = job->maxHeight;
779 /* The film AR is the source's display width / cropped source height.
780 The output display width is the output height * film AR.
781 The output PAR is the output display width / output storage width. */
782 pixel_aspect_width = height * source_display_width / cropped_height;
783 pixel_aspect_height = width;
785 /* Pass the results back to the caller */
786 *output_width = width;
787 *output_height = height;
791 /* Anamorphic 3: Power User Jamboree
792 - Set everything based on specified values */
794 /* Use specified storage dimensions */
796 height = job->height;
798 /* Bind to max dimensions */
799 if( job->maxWidth && width > job->maxWidth )
800 width = job->maxWidth;
801 if( job->maxHeight && height > job->maxHeight )
802 height = job->maxHeight;
804 /* Time to get picture dimensions that divide cleanly.*/
805 width = MULTIPLE_MOD( width, mod);
806 height = MULTIPLE_MOD( height, mod);
808 /* Verify we're still within max dimensions */
809 if( job->maxWidth && width > job->maxWidth )
810 width = job->maxWidth - (mod/2);
811 if( job->maxHeight && height > job->maxHeight )
812 height = job->maxHeight - (mod/2);
814 /* Re-ensure we have picture dimensions that divide cleanly. */
815 width = MULTIPLE_MOD( width, mod );
816 height = MULTIPLE_MOD( height, mod );
818 /* That finishes the storage dimensions. On to display. */
819 if( job->anamorphic.dar_width && job->anamorphic.dar_height )
821 /* We need to adjust the PAR to produce this aspect. */
822 pixel_aspect_width = height * job->anamorphic.dar_width / job->anamorphic.dar_height;
823 pixel_aspect_height = width;
827 /* If we're doing ana 3 and not specifying a DAR, care needs to be taken.
828 This indicates a PAR is potentially being set by the interface. But
829 this is an output PAR, to correct a source, and it should not be assumed
830 that it properly creates a display aspect ratio when applied to the source,
831 which could easily be stored in a different resolution. */
832 if( job->anamorphic.keep_display_aspect )
834 /* We can ignore the possibility of a PAR change */
835 pixel_aspect_width = height * ( (double)source_display_width / (double)cropped_height );
836 pixel_aspect_height = width;
840 int output_display_width = width * (double)pixel_aspect_width /
841 (double)pixel_aspect_height;
842 pixel_aspect_width = output_display_width;
843 pixel_aspect_height = width;
848 *output_width = width;
849 *output_height = height;
853 /* While x264 is smart enough to reduce fractions on its own, libavcodec
854 needs some help with the math, so lose superfluous factors. */
855 hb_reduce( output_par_width, output_par_height,
856 pixel_aspect_width, pixel_aspect_height );
860 * Calculates job width, height, and cropping parameters.
861 * @param job Handle to hb_job_t.
862 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
863 * @param pixels Maximum desired pixel count.
865 void hb_set_size( hb_job_t * job, double aspect, int pixels )
867 hb_title_t * title = job->title;
869 int croppedWidth = title->width - title->crop[2] - title->crop[3];
870 int croppedHeight = title->height - title->crop[0] - title->crop[1];
871 double croppedAspect = title->aspect * title->height * croppedWidth /
872 croppedHeight / title->width;
878 /* Keep the best possible aspect ratio */
879 aspect = croppedAspect;
882 /* Crop if necessary to obtain the desired ratio */
883 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
884 if( aspect < croppedAspect )
886 /* Need to crop on the left and right */
887 addCrop = croppedWidth - aspect * croppedHeight * title->width /
888 title->aspect / title->height;
891 addCrop = ( addCrop + 1 ) / 2;
892 job->crop[2] += addCrop;
893 job->crop[3] += addCrop;
895 else if( addCrop & 2 )
898 job->crop[2] += addCrop - 1;
899 job->crop[3] += addCrop + 1;
904 job->crop[2] += addCrop;
905 job->crop[3] += addCrop;
908 else if( aspect > croppedAspect )
910 /* Need to crop on the top and bottom */
911 addCrop = croppedHeight - croppedWidth * title->aspect *
912 title->height / aspect / title->width;
915 addCrop = ( addCrop + 1 ) / 2;
916 job->crop[0] += addCrop;
917 job->crop[1] += addCrop;
919 else if( addCrop & 2 )
922 job->crop[0] += addCrop - 1;
923 job->crop[1] += addCrop + 1;
928 job->crop[0] += addCrop;
929 job->crop[1] += addCrop;
933 /* Compute a resolution from the number of pixels and aspect */
937 h = MULTIPLE_16( (int)( (double)w / aspect ) );
945 job->height = MULTIPLE_16( (int)( (double)job->width / aspect ) );
949 * Returns the number of jobs in the queue.
950 * @param h Handle to hb_handle_t.
951 * @return Number of jobs.
953 int hb_count( hb_handle_t * h )
955 return hb_list_count( h->jobs );
959 * Returns handle to job at index i within the job list.
960 * @param h Handle to hb_handle_t.
961 * @param i Index of job.
962 * @returns Handle to hb_job_t of desired job.
964 hb_job_t * hb_job( hb_handle_t * h, int i )
966 return hb_list_item( h->jobs, i );
969 hb_job_t * hb_current_job( hb_handle_t * h )
971 return( h->current_job );
975 * Adds a job to the job list.
976 * @param h Handle to hb_handle_t.
977 * @param job Handle to hb_job_t.
979 void hb_add( hb_handle_t * h, hb_job_t * job )
982 hb_title_t * title, * title_copy;
983 hb_chapter_t * chapter, * chapter_copy;
985 hb_subtitle_t * subtitle, * subtitle_copy;
991 title_copy = malloc( sizeof( hb_title_t ) );
992 memcpy( title_copy, title, sizeof( hb_title_t ) );
994 title_copy->list_chapter = hb_list_init();
995 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
997 chapter = hb_list_item( title->list_chapter, i );
998 chapter_copy = malloc( sizeof( hb_chapter_t ) );
999 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
1000 hb_list_add( title_copy->list_chapter, chapter_copy );
1006 if( title->metadata )
1008 title_copy->metadata = malloc( sizeof( hb_metadata_t ) );
1010 if( title_copy->metadata )
1012 memcpy( title_copy->metadata, title->metadata, sizeof( hb_metadata_t ) );
1015 * Need to copy the artwork seperatly (TODO).
1017 if( title->metadata->coverart )
1019 title_copy->metadata->coverart = malloc( title->metadata->coverart_size );
1020 if( title_copy->metadata->coverart )
1022 memcpy( title_copy->metadata->coverart, title->metadata->coverart,
1023 title->metadata->coverart_size );
1025 title_copy->metadata->coverart_size = 0;
1031 /* Copy the audio track(s) we want */
1032 title_copy->list_audio = hb_list_init();
1034 for( i = 0; i < hb_list_count(job->list_audio); i++ )
1036 if( ( audio = hb_list_item( job->list_audio, i ) ) )
1038 hb_list_add( title_copy->list_audio, hb_audio_copy(audio) );
1042 title_copy->list_subtitle = hb_list_init();
1045 * The following code is confusing, there are two ways in which
1046 * we select subtitles and it depends on whether this is single or
1049 * subtitle_scan may be enabled, in which case the first pass
1050 * scans all subtitles of that language. The second pass does not
1051 * select any because they are set at the end of the first pass.
1053 * We may have manually selected a subtitle, in which case that is
1054 * selected in the first pass of a single pass, or the second of a
1057 memset( audio_lang, 0, sizeof( audio_lang ) );
1059 if ( job->indepth_scan ) {
1062 * Find the first audio language that is being encoded
1064 for( i = 0; i < hb_list_count(job->list_audio); i++ )
1066 if( ( audio = hb_list_item( job->list_audio, i ) ) )
1068 strncpy(audio_lang, audio->config.lang.iso639_2, sizeof(audio_lang));
1075 * If doing a subtitle scan then add all the matching subtitles for this
1078 if ( job->indepth_scan )
1080 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
1082 subtitle = hb_list_item( title->list_subtitle, i );
1083 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 &&
1084 subtitle->source == VOBSUB )
1087 * Matched subtitle language with audio language, so
1088 * add this to our list to scan.
1090 * We will update the subtitle list on the second pass
1091 * later after the first pass has completed.
1093 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1094 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1095 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1100 * Not doing a subtitle scan in this pass, but maybe we are in the
1103 if( job->pass != 1 )
1106 * Copy all of them from the input job, to the title_copy/job_copy.
1108 for( i = 0; i < hb_list_count(job->list_subtitle); i++ ) {
1109 if( ( subtitle = hb_list_item( job->list_subtitle, i ) ) )
1111 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1112 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1113 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1120 job_copy = calloc( sizeof( hb_job_t ), 1 );
1121 memcpy( job_copy, job, sizeof( hb_job_t ) );
1122 title_copy->job = job_copy;
1123 job_copy->title = title_copy;
1124 job_copy->list_audio = title_copy->list_audio;
1125 job_copy->list_subtitle = title_copy->list_subtitle; // sharing list between title and job
1126 job_copy->file = strdup( job->file );
1128 job_copy->pause = h->pause_lock;
1130 /* Copy the job filter list */
1134 int filter_count = hb_list_count( job->filters );
1135 job_copy->filters = hb_list_init();
1136 for( i = 0; i < filter_count; i++ )
1139 * Copy the filters, since the MacGui reuses the global filter objects
1140 * meaning that queued up jobs overwrite the previous filter settings.
1141 * In reality, settings is probably the only field that needs duplicating
1142 * since it's the only value that is ever changed. But name is duplicated
1143 * as well for completeness. Not copying private_data since it gets
1144 * created for each job in renderInit.
1146 hb_filter_object_t * filter = hb_list_item( job->filters, i );
1147 hb_filter_object_t * filter_copy = malloc( sizeof( hb_filter_object_t ) );
1148 memcpy( filter_copy, filter, sizeof( hb_filter_object_t ) );
1150 filter_copy->name = strdup( filter->name );
1151 if( filter->settings )
1152 filter_copy->settings = strdup( filter->settings );
1153 hb_list_add( job_copy->filters, filter_copy );
1157 /* Add the job to the list */
1158 hb_list_add( h->jobs, job_copy );
1159 h->job_count = hb_count(h);
1160 h->job_count_permanent++;
1164 * Removes a job from the job list.
1165 * @param h Handle to hb_handle_t.
1166 * @param job Handle to hb_job_t.
1168 void hb_rem( hb_handle_t * h, hb_job_t * job )
1170 hb_list_rem( h->jobs, job );
1172 h->job_count = hb_count(h);
1173 if (h->job_count_permanent)
1174 h->job_count_permanent--;
1176 /* XXX free everything XXX */
1180 * Starts the conversion process.
1181 * Sets state to HB_STATE_WORKING.
1182 * calls hb_work_init, to launch work thread. Stores handle to work thread.
1183 * @param h Handle to hb_handle_t.
1185 void hb_start( hb_handle_t * h )
1188 h->job_count = hb_list_count( h->jobs );
1189 h->job_count_permanent = h->job_count;
1191 hb_lock( h->state_lock );
1192 h->state.state = HB_STATE_WORKING;
1193 #define p h->state.param.working
1196 p.job_count = h->job_count;
1204 hb_unlock( h->state_lock );
1209 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
1210 &h->work_die, &h->work_error, &h->current_job );
1214 * Pauses the conversion process.
1215 * @param h Handle to hb_handle_t.
1217 void hb_pause( hb_handle_t * h )
1221 hb_lock( h->pause_lock );
1224 hb_current_job( h )->st_pause_date = hb_get_date();
1226 hb_lock( h->state_lock );
1227 h->state.state = HB_STATE_PAUSED;
1228 hb_unlock( h->state_lock );
1233 * Resumes the conversion process.
1234 * @param h Handle to hb_handle_t.
1236 void hb_resume( hb_handle_t * h )
1240 #define job hb_current_job( h )
1241 if( job->st_pause_date != -1 )
1243 job->st_paused += hb_get_date() - job->st_pause_date;
1247 hb_unlock( h->pause_lock );
1253 * Stops the conversion process.
1254 * @param h Handle to hb_handle_t.
1256 void hb_stop( hb_handle_t * h )
1260 h->job_count = hb_count(h);
1261 h->job_count_permanent = 0;
1267 * Stops the conversion process.
1268 * @param h Handle to hb_handle_t.
1270 void hb_scan_stop( hb_handle_t * h )
1274 h->job_count = hb_count(h);
1275 h->job_count_permanent = 0;
1281 * Returns the state of the conversion process.
1282 * @param h Handle to hb_handle_t.
1283 * @param s Handle to hb_state_t which to copy the state data.
1285 void hb_get_state( hb_handle_t * h, hb_state_t * s )
1287 hb_lock( h->state_lock );
1289 memcpy( s, &h->state, sizeof( hb_state_t ) );
1290 if ( h->state.state == HB_STATE_SCANDONE || h->state.state == HB_STATE_WORKDONE )
1291 h->state.state = HB_STATE_IDLE;
1293 hb_unlock( h->state_lock );
1296 void hb_get_state2( hb_handle_t * h, hb_state_t * s )
1298 hb_lock( h->state_lock );
1300 memcpy( s, &h->state, sizeof( hb_state_t ) );
1302 hb_unlock( h->state_lock );
1306 * Called in MacGui in UpdateUI to check
1307 * for a new scan being completed to set a new source
1309 int hb_get_scancount( hb_handle_t * h)
1311 return h->scanCount;
1315 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init.
1316 * @param _h Pointer to handle to hb_handle_t.
1318 void hb_close( hb_handle_t ** _h )
1320 hb_handle_t * h = *_h;
1325 hb_thread_close( &h->main_thread );
1327 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1329 hb_list_rem( h->list_title, title );
1330 if( title->job && title->job->filters )
1332 hb_list_close( &title->job->filters );
1335 hb_title_close( &title );
1337 hb_list_close( &h->list_title );
1339 hb_list_close( &h->jobs );
1340 hb_lock_close( &h->state_lock );
1341 hb_lock_close( &h->pause_lock );
1348 * Cleans up libhb at a process level. Call before the app closes. Removes preview directory.
1350 void hb_global_close()
1354 struct dirent * entry;
1356 /* Find and remove temp folder */
1357 memset( dirname, 0, 1024 );
1358 hb_get_temporary_directory( dirname );
1360 dir = opendir( dirname );
1363 while( ( entry = readdir( dir ) ) )
1365 char filename[1024];
1366 if( entry->d_name[0] == '.' )
1370 memset( filename, 0, 1024 );
1371 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
1380 * Monitors the state of the update, scan, and work threads.
1381 * Sets scan done state when scan thread exits.
1382 * Sets work done state when work thread exits.
1383 * @param _h Handle to hb_handle_t
1385 static void thread_func( void * _h )
1387 hb_handle_t * h = (hb_handle_t *) _h;
1392 /* Create folder for temporary files */
1393 memset( dirname, 0, 1024 );
1394 hb_get_temporary_directory( dirname );
1396 hb_mkdir( dirname );
1400 /* In case the check_update thread hangs, it'll die sooner or
1401 later. Then, we join it here */
1402 if( h->update_thread &&
1403 hb_thread_has_exited( h->update_thread ) )
1405 hb_thread_close( &h->update_thread );
1408 /* Check if the scan thread is done */
1409 if( h->scan_thread &&
1410 hb_thread_has_exited( h->scan_thread ) )
1412 hb_thread_close( &h->scan_thread );
1418 hb_remove_previews( h );
1419 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1421 hb_list_rem( h->list_title, title );
1422 hb_title_close( &title );
1425 hb_log( "hb_scan: canceled" );
1429 hb_log( "libhb: scan thread found %d valid title(s)",
1430 hb_list_count( h->list_title ) );
1432 hb_lock( h->state_lock );
1433 h->state.state = HB_STATE_SCANDONE; //originally state.state
1434 hb_unlock( h->state_lock );
1435 /*we increment this sessions scan count by one for the MacGui
1436 to trigger a new source being set */
1440 /* Check if the work thread is done */
1441 if( h->work_thread &&
1442 hb_thread_has_exited( h->work_thread ) )
1444 hb_thread_close( &h->work_thread );
1446 hb_log( "libhb: work result = %d",
1448 hb_lock( h->state_lock );
1449 h->state.state = HB_STATE_WORKDONE;
1450 h->state.param.workdone.error = h->work_error;
1452 h->job_count = hb_count(h);
1453 if (h->job_count < 1)
1454 h->job_count_permanent = 0;
1455 hb_unlock( h->state_lock );
1461 if( h->scan_thread )
1464 hb_thread_close( &h->scan_thread );
1466 if( h->work_thread )
1469 hb_thread_close( &h->work_thread );
1471 hb_remove_previews( h );
1476 * @param h Handle to hb_handle_t
1478 int hb_get_pid( hb_handle_t * h )
1484 * Returns the id for the given instance.
1485 * @param h Handle to hb_handle_t
1487 int hb_get_instance_id( hb_handle_t * h )
1493 * Sets the current state.
1494 * @param h Handle to hb_handle_t
1495 * @param s Handle to new hb_state_t
1497 void hb_set_state( hb_handle_t * h, hb_state_t * s )
1499 hb_lock( h->pause_lock );
1500 hb_lock( h->state_lock );
1501 memcpy( &h->state, s, sizeof( hb_state_t ) );
1502 if( h->state.state == HB_STATE_WORKING ||
1503 h->state.state == HB_STATE_SEARCHING )
1506 if (h->job_count < 1)
1507 h->job_count_permanent = 1;
1509 h->state.param.working.job_cur =
1510 h->job_count_permanent - hb_list_count( h->jobs );
1511 h->state.param.working.job_count = h->job_count_permanent;
1513 // Set which job is being worked on
1515 h->state.param.working.sequence_id = h->current_job->sequence_id;
1517 h->state.param.working.sequence_id = 0;
1519 hb_unlock( h->state_lock );
1520 hb_unlock( h->pause_lock );
1523 /* Passes a pointer to persistent data */
1524 hb_interjob_t * hb_interjob_get( hb_handle_t * h )