7 #if defined( SYS_MINGW )
9 #if defined( PTW32_STATIC_LIB )
18 /* The "Check for update" thread */
21 hb_thread_t * update_thread;
23 /* This thread's only purpose is to check other threads'
26 hb_thread_t * main_thread;
29 /* DVD/file scan thread */
30 hb_list_t * list_title;
31 hb_thread_t * scan_thread;
33 /* The thread which processes the jobs. Others threads are launched
34 from this one (see work.c) */
36 hb_job_t * current_job;
38 int job_count_permanent;
39 volatile int work_die;
41 hb_thread_t * work_thread;
45 hb_lock_t * state_lock;
49 hb_lock_t * pause_lock;
50 /* For MacGui active queue
51 increments each time the scan thread completes*/
53 volatile int scan_die;
55 /* Stash of persistent data between jobs, for stuff
56 like correcting frame count and framerate estimates
57 on multi-pass encodes where frames get dropped. */
58 hb_interjob_t * interjob;
62 hb_lock_t *hb_avcodec_lock;
63 hb_work_object_t * hb_objects = NULL;
64 int hb_instance_counter = 0;
65 int hb_process_initialized = 0;
67 static void thread_func( void * );
68 hb_title_t * hb_get_title_by_index( hb_handle_t *, int );
70 void hb_avcodec_init()
72 hb_avcodec_lock = hb_lock_init();
76 int hb_avcodec_open(AVCodecContext *avctx, AVCodec *codec)
79 hb_lock( hb_avcodec_lock );
80 ret = avcodec_open(avctx, codec);
81 hb_unlock( hb_avcodec_lock );
85 int hb_av_find_stream_info(AVFormatContext *ic)
88 hb_lock( hb_avcodec_lock );
89 ret = av_find_stream_info( ic );
90 hb_unlock( hb_avcodec_lock );
95 hb_sws_get_context(int srcW, int srcH, enum PixelFormat srcFormat,
96 int dstW, int dstH, enum PixelFormat dstFormat,
99 struct SwsContext * ctx;
102 // sws_getContext is being depricated. But it appears that
103 // the new method isn't quite wrung out yet. So when it is
104 // this code should be fixed up and enabled.
105 ctx = sws_alloc_context();
108 av_set_int(ctx, "srcw", srcW);
109 av_set_int(ctx, "srch", srcH);
110 av_set_int(ctx, "src_format", srcFormat);
111 av_set_int(ctx, "dstw", dstW);
112 av_set_int(ctx, "dsth", dstH);
113 av_set_int(ctx, "dst_format", dstFormat);
114 av_set_int(ctx, "sws_flags", flags);
116 if (sws_init_context(ctx, NULL, NULL) < 0) {
117 fprintf(stderr, "Cannot initialize resampling context\n");
118 sws_freeContext(ctx);
123 ctx = sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat,
124 flags, NULL, NULL, NULL);
129 int hb_avcodec_close(AVCodecContext *avctx)
132 hb_lock( hb_avcodec_lock );
133 ret = avcodec_close(avctx);
134 hb_unlock( hb_avcodec_lock );
138 int hb_ff_layout_xlat(int64_t ff_channel_layout, int channels)
142 switch (ff_channel_layout)
145 hb_layout = HB_INPUT_CH_LAYOUT_MONO;
147 case CH_LAYOUT_STEREO:
148 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
150 case CH_LAYOUT_SURROUND:
151 hb_layout = HB_INPUT_CH_LAYOUT_3F;
153 case CH_LAYOUT_4POINT0:
154 hb_layout = HB_INPUT_CH_LAYOUT_3F1R;
157 hb_layout = HB_INPUT_CH_LAYOUT_2F2R;
160 hb_layout = HB_INPUT_CH_LAYOUT_2F2R;
162 case CH_LAYOUT_5POINT0:
163 hb_layout = HB_INPUT_CH_LAYOUT_3F2R;
165 case CH_LAYOUT_5POINT1:
166 hb_layout = HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
168 case CH_LAYOUT_5POINT0_BACK:
169 hb_layout = HB_INPUT_CH_LAYOUT_3F2R;
171 case CH_LAYOUT_5POINT1_BACK:
172 hb_layout = HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
174 case CH_LAYOUT_7POINT0:
175 hb_layout = HB_INPUT_CH_LAYOUT_3F4R;
177 case CH_LAYOUT_7POINT1:
178 hb_layout = HB_INPUT_CH_LAYOUT_3F4R|HB_INPUT_CH_LAYOUT_HAS_LFE;
180 case CH_LAYOUT_STEREO_DOWNMIX:
181 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
184 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
187 // Now make sure the chosen layout agrees with the number of channels
188 // ffmpeg tells us there are. It seems ffmpeg is sometimes confused
189 // about this. So we will make a best guess based on the number
191 int chans = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT( hb_layout );
192 if ( chans == channels )
196 hb_log( "Channels reported by ffmpeg (%d) != computed layout channels (%d).", channels, chans );
200 hb_layout = HB_INPUT_CH_LAYOUT_MONO;
203 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
206 hb_layout = HB_INPUT_CH_LAYOUT_3F;
209 hb_layout = HB_INPUT_CH_LAYOUT_3F1R;
212 hb_layout = HB_INPUT_CH_LAYOUT_3F2R;
215 hb_layout = HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
218 hb_layout = HB_INPUT_CH_LAYOUT_3F4R;
221 hb_layout = HB_INPUT_CH_LAYOUT_3F4R|HB_INPUT_CH_LAYOUT_HAS_LFE;
224 hb_log("Unsupported number of audio channels (%d).\n", channels);
232 * Registers work objects, by adding the work object to a liked list.
233 * @param w Handle to hb_work_object_t to register.
235 void hb_register( hb_work_object_t * w )
237 w->next = hb_objects;
242 * Ensures that the process has been initialized.
244 static void process_init()
246 if (!hb_process_initialized)
248 #if defined( SYS_MINGW ) && defined( PTW32_STATIC_LIB )
249 pthread_win32_process_attach_np();
252 #if defined( _WIN32 ) || defined( __MINGW32__ )
253 setvbuf( stdout, NULL, _IONBF, 0 );
254 setvbuf( stderr, NULL, _IONBF, 0 );
256 hb_process_initialized = 1;
261 void (*hb_log_callback)(const char* message);
262 static void redirect_thread_func(void *);
264 #if defined( SYS_MINGW )
265 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
269 * Registers the given function as a logger. All logs will be passed to it.
270 * @param log_cb The function to register as a logger.
272 void hb_register_logger( void (*log_cb)(const char* message) )
276 hb_log_callback = log_cb;
277 hb_thread_init("ioredirect", redirect_thread_func, NULL, HB_NORMAL_PRIORITY);
281 * libhb initialization routine.
282 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
283 * @param update_check signals libhb to check for updated version from HandBrake website.
284 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
286 hb_handle_t * hb_init( int verbose, int update_check )
290 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
293 /* See hb_deep_log() and hb_log() in common.c */
294 global_verbosity_level = verbose;
296 putenv( "HB_DEBUG=1" );
298 h->id = hb_instance_counter++;
300 /* Check for an update on the website if asked to */
305 hb_log( "hb_init: checking for updates" );
306 date = hb_get_date();
307 h->update_thread = hb_update_init( &h->build, h->version );
311 if( hb_thread_has_exited( h->update_thread ) )
313 /* Immediate success or failure */
314 hb_thread_close( &h->update_thread );
317 if( hb_get_date() > date + 1000 )
319 /* Still nothing after one second. Connection problem,
320 let the thread die */
321 hb_log( "hb_init: connection problem, not waiting for "
330 * Initialise buffer pool
332 hb_buffer_pool_init();
334 /* CPU count detection */
335 hb_log( "hb_init: checking cpu count" );
336 h->cpu_count = hb_get_cpu_count();
338 h->list_title = hb_list_init();
339 h->jobs = hb_list_init();
341 h->state_lock = hb_lock_init();
342 h->state.state = HB_STATE_IDLE;
344 h->pause_lock = hb_lock_init();
346 h->interjob = calloc( sizeof( hb_interjob_t ), 1 );
351 /* Start library thread */
352 hb_log( "hb_init: starting libhb thread" );
354 h->main_thread = hb_thread_init( "libhb", thread_func, h,
355 HB_NORMAL_PRIORITY );
356 hb_register( &hb_sync_video );
357 hb_register( &hb_sync_audio );
358 hb_register( &hb_decmpeg2 );
359 hb_register( &hb_decvobsub );
360 hb_register( &hb_encvobsub );
361 hb_register( &hb_deccc608 );
362 hb_register( &hb_decsrtsub );
363 hb_register( &hb_decutf8sub );
364 hb_register( &hb_dectx3gsub );
365 hb_register( &hb_decssasub );
366 hb_register( &hb_render );
367 hb_register( &hb_encavcodec );
368 hb_register( &hb_encx264 );
369 hb_register( &hb_enctheora );
370 hb_register( &hb_deca52 );
371 hb_register( &hb_decdca );
372 hb_register( &hb_decavcodec );
373 hb_register( &hb_decavcodecv );
374 hb_register( &hb_decavcodecvi );
375 hb_register( &hb_decavcodecai );
376 hb_register( &hb_declpcm );
377 hb_register( &hb_encfaac );
378 hb_register( &hb_enclame );
379 hb_register( &hb_encvorbis );
380 hb_register( &hb_muxer );
382 hb_register( &hb_encca_aac );
384 hb_register( &hb_encac3 );
390 * libhb initialization routine.
391 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
392 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
393 * @param update_check signals libhb to check for updated version from HandBrake website.
394 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
396 hb_handle_t * hb_init_dl( int verbose, int update_check )
398 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
401 /* See hb_log() in common.c */
402 if( verbose > HB_DEBUG_NONE )
404 putenv( "HB_DEBUG=1" );
407 h->id = hb_instance_counter++;
409 /* Check for an update on the website if asked to */
414 hb_log( "hb_init: checking for updates" );
415 date = hb_get_date();
416 h->update_thread = hb_update_init( &h->build, h->version );
420 if( hb_thread_has_exited( h->update_thread ) )
422 /* Immediate success or failure */
423 hb_thread_close( &h->update_thread );
426 if( hb_get_date() > date + 1000 )
428 /* Still nothing after one second. Connection problem,
429 let the thread die */
430 hb_log( "hb_init: connection problem, not waiting for "
438 /* CPU count detection */
439 hb_log( "hb_init: checking cpu count" );
440 h->cpu_count = hb_get_cpu_count();
442 h->list_title = hb_list_init();
443 h->jobs = hb_list_init();
444 h->current_job = NULL;
446 h->state_lock = hb_lock_init();
447 h->state.state = HB_STATE_IDLE;
449 h->pause_lock = hb_lock_init();
453 avcodec_register_all();
455 /* Start library thread */
456 hb_log( "hb_init: starting libhb thread" );
458 h->main_thread = hb_thread_init( "libhb", thread_func, h,
459 HB_NORMAL_PRIORITY );
461 hb_register( &hb_sync_video );
462 hb_register( &hb_sync_audio );
463 hb_register( &hb_decmpeg2 );
464 hb_register( &hb_decvobsub );
465 hb_register( &hb_encvobsub );
466 hb_register( &hb_deccc608 );
467 hb_register( &hb_decsrtsub );
468 hb_register( &hb_decutf8sub );
469 hb_register( &hb_dectx3gsub );
470 hb_register( &hb_decssasub );
471 hb_register( &hb_render );
472 hb_register( &hb_encavcodec );
473 hb_register( &hb_encx264 );
474 hb_register( &hb_enctheora );
475 hb_register( &hb_deca52 );
476 hb_register( &hb_decdca );
477 hb_register( &hb_decavcodec );
478 hb_register( &hb_decavcodecv );
479 hb_register( &hb_decavcodecvi );
480 hb_register( &hb_decavcodecai );
481 hb_register( &hb_declpcm );
482 hb_register( &hb_encfaac );
483 hb_register( &hb_enclame );
484 hb_register( &hb_encvorbis );
485 hb_register( &hb_muxer );
487 hb_register( &hb_encca_aac );
489 hb_register( &hb_encac3 );
496 * Returns current version of libhb.
497 * @param h Handle to hb_handle_t.
498 * @return character array of version number.
500 char * hb_get_version( hb_handle_t * h )
502 return HB_PROJECT_VERSION;
506 * Returns current build of libhb.
507 * @param h Handle to hb_handle_t.
508 * @return character array of build number.
510 int hb_get_build( hb_handle_t * h )
512 return HB_PROJECT_BUILD;
516 * Checks for needed update.
517 * @param h Handle to hb_handle_t.
518 * @param version Pointer to handle where version will be copied.
519 * @return update indicator.
521 int hb_check_update( hb_handle_t * h, char ** version )
523 *version = ( h->build < 0 ) ? NULL : h->version;
528 * Sets the cpu count to the desired value.
529 * @param h Handle to hb_handle_t
530 * @param cpu_count Number of CPUs to use.
532 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
534 cpu_count = MAX( 1, cpu_count );
535 cpu_count = MIN( cpu_count, 64 );
536 h->cpu_count = cpu_count;
540 * Deletes current previews associated with titles
541 * @param h Handle to hb_handle_t
543 void hb_remove_previews( hb_handle_t * h )
550 struct dirent * entry;
552 memset( dirname, 0, 1024 );
553 hb_get_temporary_directory( dirname );
554 dir = opendir( dirname );
555 if (dir == NULL) return;
557 count = hb_list_count( h->list_title );
558 while( ( entry = readdir( dir ) ) )
560 if( entry->d_name[0] == '.' )
564 for( i = 0; i < count; i++ )
566 title = hb_list_item( h->list_title, i );
567 len = snprintf( filename, 1024, "%d_%d", h->id, title->index );
568 if (strncmp(entry->d_name, filename, len) == 0)
570 snprintf( filename, 1024, "%s/%s", dirname, entry->d_name );
580 * Initializes a scan of the by calling hb_scan_init
581 * @param h Handle to hb_handle_t
582 * @param path location of VIDEO_TS folder.
583 * @param title_index Desired title to scan. 0 for all titles.
584 * @param preview_count Number of preview images to generate.
585 * @param store_previews Whether or not to write previews to disk.
587 void hb_scan( hb_handle_t * h, const char * path, int title_index,
588 int preview_count, int store_previews, uint64_t min_duration )
594 /* Clean up from previous scan */
595 hb_remove_previews( h );
596 while( ( title = hb_list_item( h->list_title, 0 ) ) )
598 hb_list_rem( h->list_title, title );
599 hb_title_close( &title );
602 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
603 h->scan_thread = hb_scan_init( h, &h->scan_die, path, title_index,
604 h->list_title, preview_count,
605 store_previews, min_duration );
609 * Returns the list of titles found.
610 * @param h Handle to hb_handle_t
611 * @return Handle to hb_list_t of the title list.
613 hb_list_t * hb_get_titles( hb_handle_t * h )
615 return h->list_title;
619 * Create preview image of desired title a index of picture.
620 * @param h Handle to hb_handle_t.
621 * @param title_index Index of the title to get the preview for (1-based).
622 * @param picture Index in title.
623 * @param buffer Handle to buffer were image will be drawn.
625 void hb_get_preview_by_index( hb_handle_t * h, int title_index, int picture, uint8_t * buffer )
629 title = hb_get_title_by_index( h, title_index );
632 hb_get_preview( h, title, picture, buffer );
637 * Create preview image of desired title a index of picture.
638 * @param h Handle to hb_handle_t.
639 * @param title Handle to hb_title_t of desired title.
640 * @param picture Index in title.
641 * @param buffer Handle to buffer were image will be drawn.
643 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
646 hb_job_t * job = title->job;
649 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
651 AVPicture pic_in, pic_preview, pic_deint, pic_crop, pic_scale;
652 struct SwsContext * context;
654 int deint_width = ((title->width + 7) >> 3) << 3;
655 int rgb_width = ((job->width + 7) >> 3) << 3;
658 swsflags = SWS_LANCZOS | SWS_ACCURATE_RND;
660 buf1 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
661 buf2 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, deint_width, title->height ) );
662 buf3 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, rgb_width, job->height ) );
663 buf4 = av_malloc( avpicture_get_size( PIX_FMT_RGB32, rgb_width, job->height ) );
664 avpicture_fill( &pic_in, buf1, PIX_FMT_YUV420P,
665 title->width, title->height );
666 avpicture_fill( &pic_deint, buf2, PIX_FMT_YUV420P,
667 deint_width, title->height );
668 avpicture_fill( &pic_scale, buf3, PIX_FMT_YUV420P,
669 rgb_width, job->height );
670 avpicture_fill( &pic_preview, buf4, PIX_FMT_RGB32,
671 rgb_width, job->height );
673 // Allocate the AVPicture frames and fill in
675 memset( filename, 0, 1024 );
677 hb_get_tempory_filename( h, filename, "%d_%d_%d",
678 h->id, title->index, picture );
680 file = fopen( filename, "rb" );
683 hb_log( "hb_get_preview: fopen failed" );
687 fread( buf1, avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height), 1, file );
690 if( job->deinterlace )
692 // Deinterlace and crop
693 avpicture_deinterlace( &pic_deint, &pic_in, PIX_FMT_YUV420P, title->width, title->height );
694 av_picture_crop( &pic_crop, &pic_deint, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
699 av_picture_crop( &pic_crop, &pic_in, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
702 // Get scaling context
703 context = hb_sws_get_context(title->width - (job->crop[2] + job->crop[3]),
704 title->height - (job->crop[0] + job->crop[1]),
706 job->width, job->height, PIX_FMT_YUV420P,
711 pic_crop.data, pic_crop.linesize,
712 0, title->height - (job->crop[0] + job->crop[1]),
713 pic_scale.data, pic_scale.linesize);
716 sws_freeContext( context );
718 // Get preview context
719 context = hb_sws_get_context(rgb_width, job->height, PIX_FMT_YUV420P,
720 rgb_width, job->height, PIX_FMT_RGB32,
725 pic_scale.data, pic_scale.linesize,
727 pic_preview.data, pic_preview.linesize);
730 sws_freeContext( context );
732 preview_size = pic_preview.linesize[0];
734 for( i = 0; i < job->height; i++ )
736 memcpy( pen, buf4 + preview_size * i, 4 * job->width );
737 pen += 4 * job->width;
741 avpicture_free( &pic_preview );
742 avpicture_free( &pic_scale );
743 avpicture_free( &pic_deint );
744 avpicture_free( &pic_in );
748 * Analyzes a frame to detect interlacing artifacts
749 * and returns true if interlacing (combing) is found.
751 * Code taken from Thomas Oestreich's 32detect filter
752 * in the Transcode project, with minor formatting changes.
754 * @param buf An hb_buffer structure holding valid frame data
755 * @param width The frame's width in pixels
756 * @param height The frame's height in pixels
757 * @param color_equal Sensitivity for detecting similar colors
758 * @param color_diff Sensitivity for detecting different colors
759 * @param threshold Sensitivity for flagging planes as combed
760 * @param prog_equal Sensitivity for detecting similar colors on progressive frames
761 * @param prog_diff Sensitivity for detecting different colors on progressive frames
762 * @param prog_threshold Sensitivity for flagging progressive frames as combed
764 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 )
766 int j, k, n, off, cc_1, cc_2, cc[3];
767 // int flag[3] ; // debugging flag
768 uint16_t s1, s2, s3, s4;
773 if ( buf->flags & 16 )
775 /* Frame is progressive, be more discerning. */
776 color_diff = prog_diff;
777 color_equal = prog_equal;
778 threshold = prog_threshold;
781 /* One pas for Y, one pass for Cb, one pass for Cr */
782 for( k = 0; k < 3; k++ )
786 /* Y has already been checked, now offset by Y's dimensions
787 and divide all the other values by 2, since Cr and Cb
788 are half-size compared to Y. */
789 offset = width * height;
795 /* Y and Cb are done, so the offset needs to be bumped
796 so it's width*height + (width / 2) * (height / 2) */
800 for( j = 0; j < width; ++j )
804 for( n = 0; n < ( height - 4 ); n = n + 2 )
806 /* Look at groups of 4 sequential horizontal lines */
807 s1 = ( ( buf->data + offset )[ off + j ] & 0xff );
808 s2 = ( ( buf->data + offset )[ off + j + width ] & 0xff );
809 s3 = ( ( buf->data + offset )[ off + j + 2 * width ] & 0xff );
810 s4 = ( ( buf->data + offset )[ off + j + 3 * width ] & 0xff );
812 /* Note if the 1st and 2nd lines are more different in
813 color than the 1st and 3rd lines are similar in color.*/
814 if ( ( abs( s1 - s3 ) < color_equal ) &&
815 ( abs( s1 - s2 ) > color_diff ) )
818 /* Note if the 2nd and 3rd lines are more different in
819 color than the 2nd and 4th lines are similar in color.*/
820 if ( ( abs( s2 - s4 ) < color_equal ) &&
821 ( abs( s2 - s3 ) > color_diff) )
824 /* Now move down 2 horizontal lines before starting over.*/
830 /* The final cc score for a plane is the percentage of combed pixels it contains.
831 Because sensitivity goes down to hundreths of a percent, multiply by 1000
832 so it will be easy to compare against the threhold value which is an integer. */
833 cc[k] = (int)( ( cc_1 + cc_2 ) * 1000.0 / ( width * height ) );
837 /* HandBrake is all yuv420, so weight the average percentage of all 3 planes accordingly.*/
838 int average_cc = ( 2 * cc[0] + ( cc[1] / 2 ) + ( cc[2] / 2 ) ) / 3;
840 /* Now see if that average percentage of combed pixels surpasses the threshold percentage given by the user.*/
841 if( average_cc > threshold )
844 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" );
850 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" );
853 /* Reaching this point means no combing detected. */
859 * Calculates job width and height for anamorphic content,
861 * @param h Instance handle
862 * @param title_index Index of the title/job to inspect (1-based).
863 * @param output_width Pointer to returned storage width
864 * @param output_height Pointer to returned storage height
865 * @param output_par_width Pointer to returned pixel width
866 * @param output_par_height Pointer to returned pixel height
868 void hb_set_anamorphic_size_by_index( hb_handle_t * h, int title_index,
869 int *output_width, int *output_height,
870 int *output_par_width, int *output_par_height )
873 title = hb_get_title_by_index( h, title_index );
875 hb_set_anamorphic_size( title->job, output_width, output_height, output_par_width, output_par_height );
879 * Calculates job width and height for anamorphic content,
881 * @param job Handle to hb_job_t
882 * @param output_width Pointer to returned storage width
883 * @param output_height Pointer to returned storage height
884 * @param output_par_width Pointer to returned pixel width
885 * @param output_par_height Pointer to returned pixel height
887 void hb_set_anamorphic_size( hb_job_t * job,
888 int *output_width, int *output_height,
889 int *output_par_width, int *output_par_height )
891 /* Set up some variables to make the math easier to follow. */
892 hb_title_t * title = job->title;
893 int cropped_width = title->width - job->crop[2] - job->crop[3] ;
894 int cropped_height = title->height - job->crop[0] - job->crop[1] ;
895 double storage_aspect = (double)cropped_width / (double)cropped_height;
896 int mod = job->modulus ? job->modulus : 16;
897 double aspect = title->aspect;
899 int pixel_aspect_width = job->anamorphic.par_width;
900 int pixel_aspect_height = job->anamorphic.par_height;
902 /* If a source was really NTSC or PAL and the user specified ITU PAR
903 values, replace the standard PAR values with the ITU broadcast ones. */
904 if( title->width == 720 && job->anamorphic.itu_par )
906 // convert aspect to a scaled integer so we can test for 16:9 & 4:3
907 // aspect ratios ignoring insignificant differences in the LSBs of
908 // the floating point representation.
909 int iaspect = aspect * 9.;
911 /* Handle ITU PARs */
912 if (title->height == 480)
917 /* It's widescreen */
918 pixel_aspect_width = 40;
919 pixel_aspect_height = 33;
921 else if (iaspect == 12)
924 pixel_aspect_width = 10;
925 pixel_aspect_height = 11;
928 else if (title->height == 576)
933 /* It's widescreen */
934 pixel_aspect_width = 16;
935 pixel_aspect_height = 11;
937 else if (iaspect == 12)
940 pixel_aspect_width = 12;
941 pixel_aspect_height = 11;
946 /* Figure out what width the source would display at. */
947 int source_display_width = cropped_width * (double)pixel_aspect_width /
948 (double)pixel_aspect_height ;
951 3 different ways of deciding output dimensions:
952 - 1: Strict anamorphic, preserve source dimensions
953 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
954 - 3: Power user anamorphic, specify everything
957 int maxWidth, maxHeight;
959 maxWidth = MULTIPLE_MOD_DOWN( job->maxWidth, mod );
960 maxHeight = MULTIPLE_MOD_DOWN( job->maxHeight, mod );
962 switch( job->anamorphic.mode )
965 /* Strict anamorphic */
966 *output_width = MULTIPLE_MOD( cropped_width, 2 );
967 *output_height = MULTIPLE_MOD( cropped_height, 2 );
968 // adjust the source PAR for new width/height
969 // new PAR = source PAR * ( old width / new_width ) * ( new_height / old_height )
970 pixel_aspect_width = title->pixel_aspect_width * cropped_width * (*output_height);
971 pixel_aspect_height = title->pixel_aspect_height * (*output_width) * cropped_height;
975 /* "Loose" anamorphic.
976 - Uses mod16-compliant dimensions,
977 - Allows users to set the width
980 // height: Gets set later, ignore user job->height value
982 /* Gotta handle bounding dimensions.
983 If the width is too big, just reset it with no rescaling.
984 Instead of using the aspect-scaled job height,
985 we need to see if the job width divided by the storage aspect
986 is bigger than the max. If so, set it to the max (this is sloppy).
987 If not, set job height to job width divided by storage aspect.
990 /* Time to get picture width that divide cleanly.*/
991 width = MULTIPLE_MOD( width, mod);
993 if ( maxWidth && (maxWidth < job->width) )
996 /* Verify these new dimensions don't violate max height and width settings */
997 height = ((double)width / storage_aspect) + 0.5;
999 /* Time to get picture height that divide cleanly.*/
1000 height = MULTIPLE_MOD( height, mod);
1002 if ( maxHeight && (maxHeight < height) )
1005 width = ((double)height * storage_aspect) + 0.5;
1006 width = MULTIPLE_MOD( width, mod);
1009 /* The film AR is the source's display width / cropped source height.
1010 The output display width is the output height * film AR.
1011 The output PAR is the output display width / output storage width. */
1012 pixel_aspect_width = height * cropped_width * pixel_aspect_width;
1013 pixel_aspect_height = width * cropped_height * pixel_aspect_height;
1015 /* Pass the results back to the caller */
1016 *output_width = width;
1017 *output_height = height;
1021 /* Anamorphic 3: Power User Jamboree
1022 - Set everything based on specified values */
1024 /* Use specified storage dimensions */
1025 storage_aspect = (double)job->width / (double)job->height;
1027 height = job->height;
1029 /* Time to get picture dimensions that divide cleanly.*/
1030 width = MULTIPLE_MOD( width, mod);
1031 height = MULTIPLE_MOD( height, mod);
1033 /* Bind to max dimensions */
1034 if( maxWidth && width > maxWidth )
1037 // If we are keeping the display aspect, then we are going
1038 // to be modifying the PAR anyway. So it's preferred
1039 // to let the width/height stray some from the original
1040 // requested storage aspect.
1042 // But otherwise, PAR and DAR will change the least
1043 // if we stay as close as possible to the requested
1045 if ( !job->anamorphic.keep_display_aspect )
1047 height = ((double)width / storage_aspect) + 0.5;
1048 height = MULTIPLE_MOD( height, mod);
1051 if( maxHeight && height > maxHeight )
1054 // Ditto, see comment above
1055 if ( !job->anamorphic.keep_display_aspect )
1057 width = ((double)height * storage_aspect) + 0.5;
1058 width = MULTIPLE_MOD( width, mod);
1062 /* That finishes the storage dimensions. On to display. */
1063 if( job->anamorphic.dar_width && job->anamorphic.dar_height )
1065 /* We need to adjust the PAR to produce this aspect. */
1066 pixel_aspect_width = height * job->anamorphic.dar_width / job->anamorphic.dar_height;
1067 pixel_aspect_height = width;
1071 /* If we're doing ana 3 and not specifying a DAR, care needs to be taken.
1072 This indicates a PAR is potentially being set by the interface. But
1073 this is an output PAR, to correct a source, and it should not be assumed
1074 that it properly creates a display aspect ratio when applied to the source,
1075 which could easily be stored in a different resolution. */
1076 if( job->anamorphic.keep_display_aspect )
1078 /* We can ignore the possibility of a PAR change */
1079 pixel_aspect_width = height * ( (double)source_display_width / (double)cropped_height );
1080 pixel_aspect_height = width;
1084 int output_display_width = width * (double)pixel_aspect_width /
1085 (double)pixel_aspect_height;
1086 pixel_aspect_width = output_display_width;
1087 pixel_aspect_height = width;
1091 /* Back to caller */
1092 *output_width = width;
1093 *output_height = height;
1097 /* While x264 is smart enough to reduce fractions on its own, libavcodec
1098 needs some help with the math, so lose superfluous factors. */
1099 hb_reduce( output_par_width, output_par_height,
1100 pixel_aspect_width, pixel_aspect_height );
1104 * Calculates job width, height, and cropping parameters.
1105 * @param job Handle to hb_job_t.
1106 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
1107 * @param pixels Maximum desired pixel count.
1109 void hb_set_size( hb_job_t * job, double aspect, int pixels )
1111 hb_title_t * title = job->title;
1113 int croppedWidth = title->width - title->crop[2] - title->crop[3];
1114 int croppedHeight = title->height - title->crop[0] - title->crop[1];
1115 double croppedAspect = title->aspect * title->height * croppedWidth /
1116 croppedHeight / title->width;
1122 /* Keep the best possible aspect ratio */
1123 aspect = croppedAspect;
1126 /* Crop if necessary to obtain the desired ratio */
1127 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
1128 if( aspect < croppedAspect )
1130 /* Need to crop on the left and right */
1131 addCrop = croppedWidth - aspect * croppedHeight * title->width /
1132 title->aspect / title->height;
1135 addCrop = ( addCrop + 1 ) / 2;
1136 job->crop[2] += addCrop;
1137 job->crop[3] += addCrop;
1139 else if( addCrop & 2 )
1142 job->crop[2] += addCrop - 1;
1143 job->crop[3] += addCrop + 1;
1148 job->crop[2] += addCrop;
1149 job->crop[3] += addCrop;
1152 else if( aspect > croppedAspect )
1154 /* Need to crop on the top and bottom */
1155 addCrop = croppedHeight - croppedWidth * title->aspect *
1156 title->height / aspect / title->width;
1159 addCrop = ( addCrop + 1 ) / 2;
1160 job->crop[0] += addCrop;
1161 job->crop[1] += addCrop;
1163 else if( addCrop & 2 )
1166 job->crop[0] += addCrop - 1;
1167 job->crop[1] += addCrop + 1;
1172 job->crop[0] += addCrop;
1173 job->crop[1] += addCrop;
1177 /* Compute a resolution from the number of pixels and aspect */
1181 h = MULTIPLE_16( (int)( (double)w / aspect ) );
1182 if( w * h > pixels )
1188 job->width = 16 * i;
1189 job->height = MULTIPLE_16( (int)( (double)job->width / aspect ) );
1193 * Returns the number of jobs in the queue.
1194 * @param h Handle to hb_handle_t.
1195 * @return Number of jobs.
1197 int hb_count( hb_handle_t * h )
1199 return hb_list_count( h->jobs );
1203 * Returns handle to job at index i within the job list.
1204 * @param h Handle to hb_handle_t.
1205 * @param i Index of job.
1206 * @returns Handle to hb_job_t of desired job.
1208 hb_job_t * hb_job( hb_handle_t * h, int i )
1210 return hb_list_item( h->jobs, i );
1213 hb_job_t * hb_current_job( hb_handle_t * h )
1215 return( h->current_job );
1219 * Applies information from the given job to the official job instance.
1220 * @param h Handle to hb_handle_t.
1221 * @param title_index Index of the title to apply the chapter name to (1-based).
1222 * @param chapter The chapter to apply the name to (1-based).
1223 * @param job Job information to apply.
1225 void hb_set_chapter_name( hb_handle_t * h, int title_index, int chapter_index, const char * chapter_name )
1228 title = hb_get_title_by_index( h, title_index );
1230 hb_chapter_t * chapter = hb_list_item( title->list_chapter, chapter_index - 1 );
1232 strncpy(chapter->title, chapter_name, 1023);
1233 chapter->title[1023] = '\0';
1237 * Applies information from the given job to the official job instance.
1238 * Currently only applies information needed for anamorphic size calculation and previews.
1239 * @param h Handle to hb_handle_t.
1240 * @param title_index Index of the title to apply the job information to (1-based).
1241 * @param job Job information to apply.
1243 void hb_set_job( hb_handle_t * h, int title_index, hb_job_t * job )
1248 title = hb_get_title_by_index( h, title_index );
1250 hb_job_t * job_target = title->job;
1252 job_target->deinterlace = job->deinterlace;
1253 job_target->width = job->width;
1254 job_target->height = job->height;
1255 job_target->maxWidth = job->maxWidth;
1256 job_target->maxHeight = job->maxHeight;
1257 for (i = 0; i < 4; i++)
1259 job_target->crop[i] = job->crop[i];
1262 job_target->anamorphic = job->anamorphic;
1266 * Adds a job to the job list.
1267 * @param h Handle to hb_handle_t.
1268 * @param job Handle to hb_job_t.
1270 void hb_add( hb_handle_t * h, hb_job_t * job )
1272 hb_job_t * job_copy;
1273 hb_title_t * title, * title_copy;
1274 hb_chapter_t * chapter, * chapter_copy;
1276 hb_subtitle_t * subtitle, * subtitle_copy;
1277 hb_attachment_t * attachment;
1281 /* Copy the title */
1283 title_copy = malloc( sizeof( hb_title_t ) );
1284 memcpy( title_copy, title, sizeof( hb_title_t ) );
1286 title_copy->list_chapter = hb_list_init();
1287 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
1289 chapter = hb_list_item( title->list_chapter, i );
1290 chapter_copy = malloc( sizeof( hb_chapter_t ) );
1291 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
1292 hb_list_add( title_copy->list_chapter, chapter_copy );
1298 if( title->metadata )
1300 title_copy->metadata = malloc( sizeof( hb_metadata_t ) );
1302 if( title_copy->metadata )
1304 memcpy( title_copy->metadata, title->metadata, sizeof( hb_metadata_t ) );
1307 * Need to copy the artwork seperatly (TODO).
1309 if( title->metadata->coverart )
1311 title_copy->metadata->coverart = malloc( title->metadata->coverart_size );
1312 if( title_copy->metadata->coverart )
1314 memcpy( title_copy->metadata->coverart, title->metadata->coverart,
1315 title->metadata->coverart_size );
1317 title_copy->metadata->coverart_size = 0;
1323 /* Copy the audio track(s) we want */
1324 title_copy->list_audio = hb_list_init();
1325 for( i = 0; i < hb_list_count(job->list_audio); i++ )
1327 if( ( audio = hb_list_item( job->list_audio, i ) ) )
1329 hb_list_add( title_copy->list_audio, hb_audio_copy(audio) );
1333 /* Initialize subtitle list - filled out further below */
1334 title_copy->list_subtitle = hb_list_init();
1336 /* Copy all the attachments */
1337 title_copy->list_attachment = hb_list_init();
1338 for( i = 0; i < hb_list_count(title->list_attachment); i++ )
1340 if( ( attachment = hb_list_item( title->list_attachment, i ) ) )
1342 hb_list_add( title_copy->list_attachment, hb_attachment_copy(attachment) );
1347 * The following code is confusing, there are two ways in which
1348 * we select subtitles and it depends on whether this is single or
1351 * subtitle_scan may be enabled, in which case the first pass
1352 * scans all subtitles of that language. The second pass does not
1353 * select any because they are set at the end of the first pass.
1355 * We may have manually selected a subtitle, in which case that is
1356 * selected in the first pass of a single pass, or the second of a
1359 memset( audio_lang, 0, sizeof( audio_lang ) );
1361 if ( job->indepth_scan ) {
1364 * Find the first audio language that is being encoded
1366 for( i = 0; i < hb_list_count(job->list_audio); i++ )
1368 if( ( audio = hb_list_item( job->list_audio, i ) ) )
1370 strncpy(audio_lang, audio->config.lang.iso639_2, sizeof(audio_lang));
1377 * If doing a subtitle scan then add all the matching subtitles for this
1380 if ( job->indepth_scan )
1382 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
1384 subtitle = hb_list_item( title->list_subtitle, i );
1385 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 &&
1386 subtitle->source == VOBSUB )
1389 * Matched subtitle language with audio language, so
1390 * add this to our list to scan.
1392 * We will update the subtitle list on the second pass
1393 * later after the first pass has completed.
1395 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1396 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1397 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1402 * Not doing a subtitle scan in this pass, but maybe we are in the
1405 if( job->pass != 1 )
1408 * Copy all of them from the input job, to the title_copy/job_copy.
1410 for( i = 0; i < hb_list_count(job->list_subtitle); i++ ) {
1411 if( ( subtitle = hb_list_item( job->list_subtitle, i ) ) )
1413 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1414 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1415 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1422 job_copy = calloc( sizeof( hb_job_t ), 1 );
1423 memcpy( job_copy, job, sizeof( hb_job_t ) );
1424 title_copy->job = job_copy;
1425 job_copy->title = title_copy;
1426 job_copy->list_audio = title_copy->list_audio;
1427 job_copy->list_subtitle = title_copy->list_subtitle; // sharing list between title and job
1428 job_copy->file = strdup( job->file );
1430 job_copy->pause = h->pause_lock;
1432 /* Copy the job filter list */
1436 int filter_count = hb_list_count( job->filters );
1437 job_copy->filters = hb_list_init();
1438 for( i = 0; i < filter_count; i++ )
1441 * Copy the filters, since the MacGui reuses the global filter objects
1442 * meaning that queued up jobs overwrite the previous filter settings.
1443 * In reality, settings is probably the only field that needs duplicating
1444 * since it's the only value that is ever changed. But name is duplicated
1445 * as well for completeness. Not copying private_data since it gets
1446 * created for each job in renderInit.
1448 hb_filter_object_t * filter = hb_list_item( job->filters, i );
1449 hb_filter_object_t * filter_copy = malloc( sizeof( hb_filter_object_t ) );
1450 memcpy( filter_copy, filter, sizeof( hb_filter_object_t ) );
1452 filter_copy->name = strdup( filter->name );
1453 if( filter->settings )
1454 filter_copy->settings = strdup( filter->settings );
1455 hb_list_add( job_copy->filters, filter_copy );
1459 /* Add the job to the list */
1460 hb_list_add( h->jobs, job_copy );
1461 h->job_count = hb_count(h);
1462 h->job_count_permanent++;
1466 * Removes a job from the job list.
1467 * @param h Handle to hb_handle_t.
1468 * @param job Handle to hb_job_t.
1470 void hb_rem( hb_handle_t * h, hb_job_t * job )
1472 hb_list_rem( h->jobs, job );
1474 h->job_count = hb_count(h);
1475 if (h->job_count_permanent)
1476 h->job_count_permanent--;
1478 /* XXX free everything XXX */
1482 * Starts the conversion process.
1483 * Sets state to HB_STATE_WORKING.
1484 * calls hb_work_init, to launch work thread. Stores handle to work thread.
1485 * @param h Handle to hb_handle_t.
1487 void hb_start( hb_handle_t * h )
1490 h->job_count = hb_list_count( h->jobs );
1491 h->job_count_permanent = h->job_count;
1493 hb_lock( h->state_lock );
1494 h->state.state = HB_STATE_WORKING;
1495 #define p h->state.param.working
1498 p.job_count = h->job_count;
1506 hb_unlock( h->state_lock );
1511 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
1512 &h->work_die, &h->work_error, &h->current_job );
1516 * Pauses the conversion process.
1517 * @param h Handle to hb_handle_t.
1519 void hb_pause( hb_handle_t * h )
1523 hb_lock( h->pause_lock );
1526 hb_current_job( h )->st_pause_date = hb_get_date();
1528 hb_lock( h->state_lock );
1529 h->state.state = HB_STATE_PAUSED;
1530 hb_unlock( h->state_lock );
1535 * Resumes the conversion process.
1536 * @param h Handle to hb_handle_t.
1538 void hb_resume( hb_handle_t * h )
1542 #define job hb_current_job( h )
1543 if( job->st_pause_date != -1 )
1545 job->st_paused += hb_get_date() - job->st_pause_date;
1549 hb_unlock( h->pause_lock );
1555 * Stops the conversion process.
1556 * @param h Handle to hb_handle_t.
1558 void hb_stop( hb_handle_t * h )
1562 h->job_count = hb_count(h);
1563 h->job_count_permanent = 0;
1569 * Stops the conversion process.
1570 * @param h Handle to hb_handle_t.
1572 void hb_scan_stop( hb_handle_t * h )
1576 h->job_count = hb_count(h);
1577 h->job_count_permanent = 0;
1583 * Gets a filter object with the given type and settings.
1584 * @param filter_id The type of filter to get.
1585 * @param settings The filter settings to use.
1586 * @returns The requested filter object.
1588 hb_filter_object_t * hb_get_filter_object(int filter_id, const char * settings)
1590 if (filter_id == HB_FILTER_ROTATE)
1592 hb_filter_rotate.settings = (char*)settings;
1593 return &hb_filter_rotate;
1596 if (filter_id == HB_FILTER_DETELECINE)
1598 hb_filter_detelecine.settings = (char*)settings;
1599 return &hb_filter_detelecine;
1602 if (filter_id == HB_FILTER_DECOMB)
1604 hb_filter_decomb.settings = (char*)settings;
1605 return &hb_filter_decomb;
1608 if (filter_id == HB_FILTER_DEINTERLACE)
1610 hb_filter_deinterlace.settings = (char*)settings;
1611 return &hb_filter_deinterlace;
1614 if (filter_id == HB_FILTER_DEBLOCK)
1616 hb_filter_deblock.settings = (char*)settings;
1617 return &hb_filter_deblock;
1620 if (filter_id == HB_FILTER_DENOISE)
1622 hb_filter_denoise.settings = (char*)settings;
1623 return &hb_filter_denoise;
1629 * Returns the state of the conversion process.
1630 * @param h Handle to hb_handle_t.
1631 * @param s Handle to hb_state_t which to copy the state data.
1633 void hb_get_state( hb_handle_t * h, hb_state_t * s )
1635 hb_lock( h->state_lock );
1637 memcpy( s, &h->state, sizeof( hb_state_t ) );
1638 if ( h->state.state == HB_STATE_SCANDONE || h->state.state == HB_STATE_WORKDONE )
1639 h->state.state = HB_STATE_IDLE;
1641 hb_unlock( h->state_lock );
1644 void hb_get_state2( hb_handle_t * h, hb_state_t * s )
1646 hb_lock( h->state_lock );
1648 memcpy( s, &h->state, sizeof( hb_state_t ) );
1650 hb_unlock( h->state_lock );
1654 * Called in MacGui in UpdateUI to check
1655 * for a new scan being completed to set a new source
1657 int hb_get_scancount( hb_handle_t * h)
1659 return h->scanCount;
1663 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init.
1664 * @param _h Pointer to handle to hb_handle_t.
1666 void hb_close( hb_handle_t ** _h )
1668 hb_handle_t * h = *_h;
1673 hb_thread_close( &h->main_thread );
1675 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1677 hb_list_rem( h->list_title, title );
1678 if( title->job && title->job->filters )
1680 hb_list_close( &title->job->filters );
1683 hb_title_close( &title );
1685 hb_list_close( &h->list_title );
1687 hb_list_close( &h->jobs );
1688 hb_lock_close( &h->state_lock );
1689 hb_lock_close( &h->pause_lock );
1696 * Cleans up libhb at a process level. Call before the app closes. Removes preview directory.
1698 void hb_global_close()
1702 struct dirent * entry;
1704 /* Find and remove temp folder */
1705 memset( dirname, 0, 1024 );
1706 hb_get_temporary_directory( dirname );
1708 dir = opendir( dirname );
1711 while( ( entry = readdir( dir ) ) )
1713 char filename[1024];
1714 if( entry->d_name[0] == '.' )
1718 memset( filename, 0, 1024 );
1719 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
1728 * Monitors the state of the update, scan, and work threads.
1729 * Sets scan done state when scan thread exits.
1730 * Sets work done state when work thread exits.
1731 * @param _h Handle to hb_handle_t
1733 static void thread_func( void * _h )
1735 hb_handle_t * h = (hb_handle_t *) _h;
1740 /* Create folder for temporary files */
1741 memset( dirname, 0, 1024 );
1742 hb_get_temporary_directory( dirname );
1744 hb_mkdir( dirname );
1748 /* In case the check_update thread hangs, it'll die sooner or
1749 later. Then, we join it here */
1750 if( h->update_thread &&
1751 hb_thread_has_exited( h->update_thread ) )
1753 hb_thread_close( &h->update_thread );
1756 /* Check if the scan thread is done */
1757 if( h->scan_thread &&
1758 hb_thread_has_exited( h->scan_thread ) )
1760 hb_thread_close( &h->scan_thread );
1766 hb_remove_previews( h );
1767 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1769 hb_list_rem( h->list_title, title );
1770 hb_title_close( &title );
1773 hb_log( "hb_scan: canceled" );
1777 hb_log( "libhb: scan thread found %d valid title(s)",
1778 hb_list_count( h->list_title ) );
1780 hb_lock( h->state_lock );
1781 h->state.state = HB_STATE_SCANDONE; //originally state.state
1782 hb_unlock( h->state_lock );
1783 /*we increment this sessions scan count by one for the MacGui
1784 to trigger a new source being set */
1788 /* Check if the work thread is done */
1789 if( h->work_thread &&
1790 hb_thread_has_exited( h->work_thread ) )
1792 hb_thread_close( &h->work_thread );
1794 hb_log( "libhb: work result = %d",
1796 hb_lock( h->state_lock );
1797 h->state.state = HB_STATE_WORKDONE;
1798 h->state.param.workdone.error = h->work_error;
1800 h->job_count = hb_count(h);
1801 if (h->job_count < 1)
1802 h->job_count_permanent = 0;
1803 hb_unlock( h->state_lock );
1809 if( h->scan_thread )
1812 hb_thread_close( &h->scan_thread );
1814 if( h->work_thread )
1817 hb_thread_close( &h->work_thread );
1819 hb_remove_previews( h );
1823 * Redirects stderr to the registered callback
1825 * @param _data Unused.
1827 static void redirect_thread_func(void * _data)
1831 #if defined( SYS_MINGW )
1832 // dup2 doesn't work on windows for some stupid reason
1833 stderr->_file = pfd[1];
1835 dup2(pfd[1], /*stderr*/ 2);
1837 FILE * log_f = fdopen(pfd[0], "rb");
1839 char line_buffer[500];
1840 while(fgets(line_buffer, 500, log_f) != NULL)
1842 hb_log_callback(line_buffer);
1848 * @param h Handle to hb_handle_t
1850 int hb_get_pid( hb_handle_t * h )
1856 * Returns the id for the given instance.
1857 * @param h Handle to hb_handle_t
1858 * @returns The ID for the given instance
1860 int hb_get_instance_id( hb_handle_t * h )
1866 * Returns the title with the given title index.
1867 * @param h Handle to hb_handle_t
1868 * @param title_index the index of the title to get
1869 * @returns The requested title
1871 hb_title_t * hb_get_title_by_index( hb_handle_t * h, int title_index )
1875 int count = hb_list_count( h->list_title );
1876 for (i = 0; i < count; i++)
1878 title = hb_list_item( h->list_title, i );
1879 if (title->index == title_index)
1889 * Sets the current state.
1890 * @param h Handle to hb_handle_t
1891 * @param s Handle to new hb_state_t
1893 void hb_set_state( hb_handle_t * h, hb_state_t * s )
1895 hb_lock( h->pause_lock );
1896 hb_lock( h->state_lock );
1897 memcpy( &h->state, s, sizeof( hb_state_t ) );
1898 if( h->state.state == HB_STATE_WORKING ||
1899 h->state.state == HB_STATE_SEARCHING )
1902 if (h->job_count < 1)
1903 h->job_count_permanent = 1;
1905 h->state.param.working.job_cur =
1906 h->job_count_permanent - hb_list_count( h->jobs );
1907 h->state.param.working.job_count = h->job_count_permanent;
1909 // Set which job is being worked on
1911 h->state.param.working.sequence_id = h->current_job->sequence_id;
1913 h->state.param.working.sequence_id = 0;
1915 hb_unlock( h->state_lock );
1916 hb_unlock( h->pause_lock );
1919 /* Passes a pointer to persistent data */
1920 hb_interjob_t * hb_interjob_get( hb_handle_t * h )