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;
43 hb_lock_t * state_lock;
47 hb_lock_t * pause_lock;
48 /* For MacGui active queue
49 increments each time the scan thread completes*/
51 volatile int scan_die;
53 /* Stash of persistent data between jobs, for stuff
54 like correcting frame count and framerate estimates
55 on multi-pass encodes where frames get dropped. */
56 hb_interjob_t * interjob;
60 hb_lock_t *hb_avcodec_lock;
61 hb_work_object_t * hb_objects = NULL;
62 int hb_instance_counter = 0;
63 int hb_process_initialized = 0;
65 static void thread_func( void * );
66 hb_title_t * hb_get_title_by_index( hb_handle_t *, int );
68 void hb_avcodec_init()
70 hb_avcodec_lock = hb_lock_init();
74 int hb_avcodec_open(AVCodecContext *avctx, AVCodec *codec)
77 hb_lock( hb_avcodec_lock );
78 ret = avcodec_open(avctx, codec);
79 hb_unlock( hb_avcodec_lock );
83 int hb_av_find_stream_info(AVFormatContext *ic)
86 hb_lock( hb_avcodec_lock );
87 ret = av_find_stream_info( ic );
88 hb_unlock( hb_avcodec_lock );
93 hb_sws_get_context(int srcW, int srcH, enum PixelFormat srcFormat,
94 int dstW, int dstH, enum PixelFormat dstFormat,
97 struct SwsContext * ctx;
100 // sws_getContext is being depricated. But it appears that
101 // the new method isn't quite wrung out yet. So when it is
102 // this code should be fixed up and enabled.
103 ctx = sws_alloc_context();
106 av_set_int(ctx, "srcw", srcW);
107 av_set_int(ctx, "srch", srcH);
108 av_set_int(ctx, "src_format", srcFormat);
109 av_set_int(ctx, "dstw", dstW);
110 av_set_int(ctx, "dsth", dstH);
111 av_set_int(ctx, "dst_format", dstFormat);
112 av_set_int(ctx, "sws_flags", flags);
114 if (sws_init_context(ctx, NULL, NULL) < 0) {
115 fprintf(stderr, "Cannot initialize resampling context\n");
116 sws_freeContext(ctx);
121 ctx = sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat,
122 flags, NULL, NULL, NULL);
127 int hb_avcodec_close(AVCodecContext *avctx)
130 hb_lock( hb_avcodec_lock );
131 ret = avcodec_close(avctx);
132 hb_unlock( hb_avcodec_lock );
136 int hb_ff_layout_xlat(int64_t ff_channel_layout, int channels)
140 switch (ff_channel_layout)
143 hb_layout = HB_INPUT_CH_LAYOUT_MONO;
145 case CH_LAYOUT_STEREO:
146 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
148 case CH_LAYOUT_SURROUND:
149 hb_layout = HB_INPUT_CH_LAYOUT_3F;
151 case CH_LAYOUT_4POINT0:
152 hb_layout = HB_INPUT_CH_LAYOUT_3F1R;
155 hb_layout = HB_INPUT_CH_LAYOUT_2F2R;
158 hb_layout = HB_INPUT_CH_LAYOUT_2F2R;
160 case CH_LAYOUT_5POINT0:
161 hb_layout = HB_INPUT_CH_LAYOUT_3F2R;
163 case CH_LAYOUT_5POINT1:
164 hb_layout = HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
166 case CH_LAYOUT_5POINT0_BACK:
167 hb_layout = HB_INPUT_CH_LAYOUT_3F2R;
169 case CH_LAYOUT_5POINT1_BACK:
170 hb_layout = HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
172 case CH_LAYOUT_7POINT0:
173 hb_layout = HB_INPUT_CH_LAYOUT_3F4R;
175 case CH_LAYOUT_7POINT1:
176 hb_layout = HB_INPUT_CH_LAYOUT_3F4R|HB_INPUT_CH_LAYOUT_HAS_LFE;
178 case CH_LAYOUT_STEREO_DOWNMIX:
179 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
182 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
185 // Now make sure the chosen layout agrees with the number of channels
186 // ffmpeg tells us there are. It seems ffmpeg is sometimes confused
187 // about this. So we will make a best guess based on the number
189 int chans = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT( hb_layout );
190 if ( chans == channels )
194 hb_log( "Channels reported by ffmpeg (%d) != computed layout channels (%d).", channels, chans );
198 hb_layout = HB_INPUT_CH_LAYOUT_MONO;
201 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
204 hb_layout = HB_INPUT_CH_LAYOUT_3F;
207 hb_layout = HB_INPUT_CH_LAYOUT_3F1R;
210 hb_layout = HB_INPUT_CH_LAYOUT_3F2R;
213 hb_layout = HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
216 hb_layout = HB_INPUT_CH_LAYOUT_3F4R;
219 hb_layout = HB_INPUT_CH_LAYOUT_3F4R|HB_INPUT_CH_LAYOUT_HAS_LFE;
222 hb_log("Unsupported number of audio channels (%d).\n", channels);
230 * Registers work objects, by adding the work object to a liked list.
231 * @param w Handle to hb_work_object_t to register.
233 void hb_register( hb_work_object_t * w )
235 w->next = hb_objects;
240 * Ensures that the process has been initialized.
242 static void process_init()
244 if (!hb_process_initialized)
246 #if defined( SYS_MINGW ) && defined( PTW32_STATIC_LIB )
247 pthread_win32_process_attach_np();
250 #if defined( _WIN32 ) || defined( __MINGW32__ )
251 setvbuf( stdout, NULL, _IONBF, 0 );
252 setvbuf( stderr, NULL, _IONBF, 0 );
254 hb_process_initialized = 1;
259 void (*hb_log_callback)(const char* message);
260 static void redirect_thread_func(void *);
262 #if defined( SYS_MINGW )
263 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
267 * Registers the given function as a logger. All logs will be passed to it.
268 * @param log_cb The function to register as a logger.
270 void hb_register_logger( void (*log_cb)(const char* message) )
274 hb_log_callback = log_cb;
275 hb_thread_init("ioredirect", redirect_thread_func, NULL, HB_NORMAL_PRIORITY);
279 * libhb initialization routine.
280 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
281 * @param update_check signals libhb to check for updated version from HandBrake website.
282 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
284 hb_handle_t * hb_init( int verbose, int update_check )
288 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
291 /* See hb_deep_log() and hb_log() in common.c */
292 global_verbosity_level = verbose;
294 putenv( "HB_DEBUG=1" );
296 h->id = hb_instance_counter++;
298 /* Check for an update on the website if asked to */
303 hb_log( "hb_init: checking for updates" );
304 date = hb_get_date();
305 h->update_thread = hb_update_init( &h->build, h->version );
309 if( hb_thread_has_exited( h->update_thread ) )
311 /* Immediate success or failure */
312 hb_thread_close( &h->update_thread );
315 if( hb_get_date() > date + 1000 )
317 /* Still nothing after one second. Connection problem,
318 let the thread die */
319 hb_log( "hb_init: connection problem, not waiting for "
328 * Initialise buffer pool
330 hb_buffer_pool_init();
332 h->list_title = hb_list_init();
333 h->jobs = hb_list_init();
335 h->state_lock = hb_lock_init();
336 h->state.state = HB_STATE_IDLE;
338 h->pause_lock = hb_lock_init();
340 h->interjob = calloc( sizeof( hb_interjob_t ), 1 );
345 /* Start library thread */
346 hb_log( "hb_init: starting libhb thread" );
348 h->main_thread = hb_thread_init( "libhb", thread_func, h,
349 HB_NORMAL_PRIORITY );
350 hb_register( &hb_sync_video );
351 hb_register( &hb_sync_audio );
352 hb_register( &hb_decmpeg2 );
353 hb_register( &hb_decvobsub );
354 hb_register( &hb_encvobsub );
355 hb_register( &hb_deccc608 );
356 hb_register( &hb_decsrtsub );
357 hb_register( &hb_decutf8sub );
358 hb_register( &hb_dectx3gsub );
359 hb_register( &hb_decssasub );
360 hb_register( &hb_render );
361 hb_register( &hb_encavcodec );
362 hb_register( &hb_encx264 );
363 hb_register( &hb_enctheora );
364 hb_register( &hb_deca52 );
365 hb_register( &hb_decdca );
366 hb_register( &hb_decavcodec );
367 hb_register( &hb_decavcodecv );
368 hb_register( &hb_decavcodecvi );
369 hb_register( &hb_decavcodecai );
370 hb_register( &hb_declpcm );
371 hb_register( &hb_encfaac );
372 hb_register( &hb_enclame );
373 hb_register( &hb_encvorbis );
374 hb_register( &hb_muxer );
376 hb_register( &hb_encca_aac );
378 hb_register( &hb_encac3 );
384 * libhb initialization routine.
385 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
386 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
387 * @param update_check signals libhb to check for updated version from HandBrake website.
388 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
390 hb_handle_t * hb_init_dl( int verbose, int update_check )
392 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
395 /* See hb_log() in common.c */
396 if( verbose > HB_DEBUG_NONE )
398 putenv( "HB_DEBUG=1" );
401 h->id = hb_instance_counter++;
403 /* Check for an update on the website if asked to */
408 hb_log( "hb_init: checking for updates" );
409 date = hb_get_date();
410 h->update_thread = hb_update_init( &h->build, h->version );
414 if( hb_thread_has_exited( h->update_thread ) )
416 /* Immediate success or failure */
417 hb_thread_close( &h->update_thread );
420 if( hb_get_date() > date + 1000 )
422 /* Still nothing after one second. Connection problem,
423 let the thread die */
424 hb_log( "hb_init: connection problem, not waiting for "
432 h->list_title = hb_list_init();
433 h->jobs = hb_list_init();
434 h->current_job = NULL;
436 h->state_lock = hb_lock_init();
437 h->state.state = HB_STATE_IDLE;
439 h->pause_lock = hb_lock_init();
443 avcodec_register_all();
445 /* Start library thread */
446 hb_log( "hb_init: starting libhb thread" );
448 h->main_thread = hb_thread_init( "libhb", thread_func, h,
449 HB_NORMAL_PRIORITY );
451 hb_register( &hb_sync_video );
452 hb_register( &hb_sync_audio );
453 hb_register( &hb_decmpeg2 );
454 hb_register( &hb_decvobsub );
455 hb_register( &hb_encvobsub );
456 hb_register( &hb_deccc608 );
457 hb_register( &hb_decsrtsub );
458 hb_register( &hb_decutf8sub );
459 hb_register( &hb_dectx3gsub );
460 hb_register( &hb_decssasub );
461 hb_register( &hb_render );
462 hb_register( &hb_encavcodec );
463 hb_register( &hb_encx264 );
464 hb_register( &hb_enctheora );
465 hb_register( &hb_deca52 );
466 hb_register( &hb_decdca );
467 hb_register( &hb_decavcodec );
468 hb_register( &hb_decavcodecv );
469 hb_register( &hb_decavcodecvi );
470 hb_register( &hb_decavcodecai );
471 hb_register( &hb_declpcm );
472 hb_register( &hb_encfaac );
473 hb_register( &hb_enclame );
474 hb_register( &hb_encvorbis );
475 hb_register( &hb_muxer );
477 hb_register( &hb_encca_aac );
479 hb_register( &hb_encac3 );
486 * Returns current version of libhb.
487 * @param h Handle to hb_handle_t.
488 * @return character array of version number.
490 char * hb_get_version( hb_handle_t * h )
492 return HB_PROJECT_VERSION;
496 * Returns current build of libhb.
497 * @param h Handle to hb_handle_t.
498 * @return character array of build number.
500 int hb_get_build( hb_handle_t * h )
502 return HB_PROJECT_BUILD;
506 * Checks for needed update.
507 * @param h Handle to hb_handle_t.
508 * @param version Pointer to handle where version will be copied.
509 * @return update indicator.
511 int hb_check_update( hb_handle_t * h, char ** version )
513 *version = ( h->build < 0 ) ? NULL : h->version;
518 * Deletes current previews associated with titles
519 * @param h Handle to hb_handle_t
521 void hb_remove_previews( hb_handle_t * h )
528 struct dirent * entry;
530 memset( dirname, 0, 1024 );
531 hb_get_temporary_directory( dirname );
532 dir = opendir( dirname );
533 if (dir == NULL) return;
535 count = hb_list_count( h->list_title );
536 while( ( entry = readdir( dir ) ) )
538 if( entry->d_name[0] == '.' )
542 for( i = 0; i < count; i++ )
544 title = hb_list_item( h->list_title, i );
545 len = snprintf( filename, 1024, "%d_%d", h->id, title->index );
546 if (strncmp(entry->d_name, filename, len) == 0)
548 snprintf( filename, 1024, "%s/%s", dirname, entry->d_name );
558 * Initializes a scan of the by calling hb_scan_init
559 * @param h Handle to hb_handle_t
560 * @param path location of VIDEO_TS folder.
561 * @param title_index Desired title to scan. 0 for all titles.
562 * @param preview_count Number of preview images to generate.
563 * @param store_previews Whether or not to write previews to disk.
565 void hb_scan( hb_handle_t * h, const char * path, int title_index,
566 int preview_count, int store_previews, uint64_t min_duration )
572 /* Clean up from previous scan */
573 hb_remove_previews( h );
574 while( ( title = hb_list_item( h->list_title, 0 ) ) )
576 hb_list_rem( h->list_title, title );
577 hb_title_close( &title );
580 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
581 h->scan_thread = hb_scan_init( h, &h->scan_die, path, title_index,
582 h->list_title, preview_count,
583 store_previews, min_duration );
587 * Returns the list of titles found.
588 * @param h Handle to hb_handle_t
589 * @return Handle to hb_list_t of the title list.
591 hb_list_t * hb_get_titles( hb_handle_t * h )
593 return h->list_title;
597 * Create preview image of desired title a index of picture.
598 * @param h Handle to hb_handle_t.
599 * @param title_index Index of the title to get the preview for (1-based).
600 * @param picture Index in title.
601 * @param buffer Handle to buffer were image will be drawn.
603 void hb_get_preview_by_index( hb_handle_t * h, int title_index, int picture, uint8_t * buffer )
607 title = hb_get_title_by_index( h, title_index );
610 hb_get_preview( h, title, picture, buffer );
615 * Create preview image of desired title a index of picture.
616 * @param h Handle to hb_handle_t.
617 * @param title Handle to hb_title_t of desired title.
618 * @param picture Index in title.
619 * @param buffer Handle to buffer were image will be drawn.
621 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
624 hb_job_t * job = title->job;
627 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
629 AVPicture pic_in, pic_preview, pic_deint, pic_crop, pic_scale;
630 struct SwsContext * context;
632 int deint_width = ((title->width + 7) >> 3) << 3;
633 int rgb_width = ((job->width + 7) >> 3) << 3;
636 swsflags = SWS_LANCZOS | SWS_ACCURATE_RND;
638 buf1 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
639 buf2 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, deint_width, title->height ) );
640 buf3 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, rgb_width, job->height ) );
641 buf4 = av_malloc( avpicture_get_size( PIX_FMT_RGB32, rgb_width, job->height ) );
642 avpicture_fill( &pic_in, buf1, PIX_FMT_YUV420P,
643 title->width, title->height );
644 avpicture_fill( &pic_deint, buf2, PIX_FMT_YUV420P,
645 deint_width, title->height );
646 avpicture_fill( &pic_scale, buf3, PIX_FMT_YUV420P,
647 rgb_width, job->height );
648 avpicture_fill( &pic_preview, buf4, PIX_FMT_RGB32,
649 rgb_width, job->height );
651 // Allocate the AVPicture frames and fill in
653 memset( filename, 0, 1024 );
655 hb_get_tempory_filename( h, filename, "%d_%d_%d",
656 h->id, title->index, picture );
658 file = fopen( filename, "rb" );
661 hb_log( "hb_get_preview: fopen failed" );
665 fread( buf1, avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height), 1, file );
668 if( job->deinterlace )
670 // Deinterlace and crop
671 avpicture_deinterlace( &pic_deint, &pic_in, PIX_FMT_YUV420P, title->width, title->height );
672 av_picture_crop( &pic_crop, &pic_deint, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
677 av_picture_crop( &pic_crop, &pic_in, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
680 // Get scaling context
681 context = hb_sws_get_context(title->width - (job->crop[2] + job->crop[3]),
682 title->height - (job->crop[0] + job->crop[1]),
684 job->width, job->height, PIX_FMT_YUV420P,
689 pic_crop.data, pic_crop.linesize,
690 0, title->height - (job->crop[0] + job->crop[1]),
691 pic_scale.data, pic_scale.linesize);
694 sws_freeContext( context );
696 // Get preview context
697 context = hb_sws_get_context(rgb_width, job->height, PIX_FMT_YUV420P,
698 rgb_width, job->height, PIX_FMT_RGB32,
703 pic_scale.data, pic_scale.linesize,
705 pic_preview.data, pic_preview.linesize);
708 sws_freeContext( context );
710 preview_size = pic_preview.linesize[0];
712 for( i = 0; i < job->height; i++ )
714 memcpy( pen, buf4 + preview_size * i, 4 * job->width );
715 pen += 4 * job->width;
719 avpicture_free( &pic_preview );
720 avpicture_free( &pic_scale );
721 avpicture_free( &pic_deint );
722 avpicture_free( &pic_in );
726 * Analyzes a frame to detect interlacing artifacts
727 * and returns true if interlacing (combing) is found.
729 * Code taken from Thomas Oestreich's 32detect filter
730 * in the Transcode project, with minor formatting changes.
732 * @param buf An hb_buffer structure holding valid frame data
733 * @param width The frame's width in pixels
734 * @param height The frame's height in pixels
735 * @param color_equal Sensitivity for detecting similar colors
736 * @param color_diff Sensitivity for detecting different colors
737 * @param threshold Sensitivity for flagging planes as combed
738 * @param prog_equal Sensitivity for detecting similar colors on progressive frames
739 * @param prog_diff Sensitivity for detecting different colors on progressive frames
740 * @param prog_threshold Sensitivity for flagging progressive frames as combed
742 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 )
744 int j, k, n, off, cc_1, cc_2, cc[3];
745 // int flag[3] ; // debugging flag
746 uint16_t s1, s2, s3, s4;
751 if ( buf->flags & 16 )
753 /* Frame is progressive, be more discerning. */
754 color_diff = prog_diff;
755 color_equal = prog_equal;
756 threshold = prog_threshold;
759 /* One pas for Y, one pass for Cb, one pass for Cr */
760 for( k = 0; k < 3; k++ )
764 /* Y has already been checked, now offset by Y's dimensions
765 and divide all the other values by 2, since Cr and Cb
766 are half-size compared to Y. */
767 offset = width * height;
773 /* Y and Cb are done, so the offset needs to be bumped
774 so it's width*height + (width / 2) * (height / 2) */
778 for( j = 0; j < width; ++j )
782 for( n = 0; n < ( height - 4 ); n = n + 2 )
784 /* Look at groups of 4 sequential horizontal lines */
785 s1 = ( ( buf->data + offset )[ off + j ] & 0xff );
786 s2 = ( ( buf->data + offset )[ off + j + width ] & 0xff );
787 s3 = ( ( buf->data + offset )[ off + j + 2 * width ] & 0xff );
788 s4 = ( ( buf->data + offset )[ off + j + 3 * width ] & 0xff );
790 /* Note if the 1st and 2nd lines are more different in
791 color than the 1st and 3rd lines are similar in color.*/
792 if ( ( abs( s1 - s3 ) < color_equal ) &&
793 ( abs( s1 - s2 ) > color_diff ) )
796 /* Note if the 2nd and 3rd lines are more different in
797 color than the 2nd and 4th lines are similar in color.*/
798 if ( ( abs( s2 - s4 ) < color_equal ) &&
799 ( abs( s2 - s3 ) > color_diff) )
802 /* Now move down 2 horizontal lines before starting over.*/
808 /* The final cc score for a plane is the percentage of combed pixels it contains.
809 Because sensitivity goes down to hundreths of a percent, multiply by 1000
810 so it will be easy to compare against the threhold value which is an integer. */
811 cc[k] = (int)( ( cc_1 + cc_2 ) * 1000.0 / ( width * height ) );
815 /* HandBrake is all yuv420, so weight the average percentage of all 3 planes accordingly.*/
816 int average_cc = ( 2 * cc[0] + ( cc[1] / 2 ) + ( cc[2] / 2 ) ) / 3;
818 /* Now see if that average percentage of combed pixels surpasses the threshold percentage given by the user.*/
819 if( average_cc > threshold )
822 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" );
828 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" );
831 /* Reaching this point means no combing detected. */
837 * Calculates job width and height for anamorphic content,
839 * @param h Instance handle
840 * @param title_index Index of the title/job to inspect (1-based).
841 * @param output_width Pointer to returned storage width
842 * @param output_height Pointer to returned storage height
843 * @param output_par_width Pointer to returned pixel width
844 * @param output_par_height Pointer to returned pixel height
846 void hb_set_anamorphic_size_by_index( hb_handle_t * h, int title_index,
847 int *output_width, int *output_height,
848 int *output_par_width, int *output_par_height )
851 title = hb_get_title_by_index( h, title_index );
853 hb_set_anamorphic_size( title->job, output_width, output_height, output_par_width, output_par_height );
857 * Calculates job width and height for anamorphic content,
859 * @param job Handle to hb_job_t
860 * @param output_width Pointer to returned storage width
861 * @param output_height Pointer to returned storage height
862 * @param output_par_width Pointer to returned pixel width
863 * @param output_par_height Pointer to returned pixel height
865 void hb_set_anamorphic_size( hb_job_t * job,
866 int *output_width, int *output_height,
867 int *output_par_width, int *output_par_height )
869 /* Set up some variables to make the math easier to follow. */
870 hb_title_t * title = job->title;
871 int cropped_width = title->width - job->crop[2] - job->crop[3] ;
872 int cropped_height = title->height - job->crop[0] - job->crop[1] ;
873 double storage_aspect = (double)cropped_width / (double)cropped_height;
874 int mod = job->modulus ? job->modulus : 16;
875 double aspect = title->aspect;
877 int pixel_aspect_width = job->anamorphic.par_width;
878 int pixel_aspect_height = job->anamorphic.par_height;
880 /* If a source was really NTSC or PAL and the user specified ITU PAR
881 values, replace the standard PAR values with the ITU broadcast ones. */
882 if( title->width == 720 && job->anamorphic.itu_par )
884 // convert aspect to a scaled integer so we can test for 16:9 & 4:3
885 // aspect ratios ignoring insignificant differences in the LSBs of
886 // the floating point representation.
887 int iaspect = aspect * 9.;
889 /* Handle ITU PARs */
890 if (title->height == 480)
895 /* It's widescreen */
896 pixel_aspect_width = 40;
897 pixel_aspect_height = 33;
899 else if (iaspect == 12)
902 pixel_aspect_width = 10;
903 pixel_aspect_height = 11;
906 else if (title->height == 576)
911 /* It's widescreen */
912 pixel_aspect_width = 16;
913 pixel_aspect_height = 11;
915 else if (iaspect == 12)
918 pixel_aspect_width = 12;
919 pixel_aspect_height = 11;
924 /* Figure out what width the source would display at. */
925 int source_display_width = cropped_width * (double)pixel_aspect_width /
926 (double)pixel_aspect_height ;
929 3 different ways of deciding output dimensions:
930 - 1: Strict anamorphic, preserve source dimensions
931 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
932 - 3: Power user anamorphic, specify everything
935 int maxWidth, maxHeight;
937 maxWidth = MULTIPLE_MOD_DOWN( job->maxWidth, mod );
938 maxHeight = MULTIPLE_MOD_DOWN( job->maxHeight, mod );
940 switch( job->anamorphic.mode )
943 /* Strict anamorphic */
944 *output_width = MULTIPLE_MOD( cropped_width, 2 );
945 *output_height = MULTIPLE_MOD( cropped_height, 2 );
946 // adjust the source PAR for new width/height
947 // new PAR = source PAR * ( old width / new_width ) * ( new_height / old_height )
948 pixel_aspect_width = title->pixel_aspect_width * cropped_width * (*output_height);
949 pixel_aspect_height = title->pixel_aspect_height * (*output_width) * cropped_height;
953 /* "Loose" anamorphic.
954 - Uses mod16-compliant dimensions,
955 - Allows users to set the width
958 // height: Gets set later, ignore user job->height value
960 /* Gotta handle bounding dimensions.
961 If the width is too big, just reset it with no rescaling.
962 Instead of using the aspect-scaled job height,
963 we need to see if the job width divided by the storage aspect
964 is bigger than the max. If so, set it to the max (this is sloppy).
965 If not, set job height to job width divided by storage aspect.
968 /* Time to get picture width that divide cleanly.*/
969 width = MULTIPLE_MOD( width, mod);
971 if ( maxWidth && (maxWidth < job->width) )
974 /* Verify these new dimensions don't violate max height and width settings */
975 height = ((double)width / storage_aspect) + 0.5;
977 /* Time to get picture height that divide cleanly.*/
978 height = MULTIPLE_MOD( height, mod);
980 if ( maxHeight && (maxHeight < height) )
983 width = ((double)height * storage_aspect) + 0.5;
984 width = MULTIPLE_MOD( width, mod);
987 /* The film AR is the source's display width / cropped source height.
988 The output display width is the output height * film AR.
989 The output PAR is the output display width / output storage width. */
990 pixel_aspect_width = height * cropped_width * pixel_aspect_width;
991 pixel_aspect_height = width * cropped_height * pixel_aspect_height;
993 /* Pass the results back to the caller */
994 *output_width = width;
995 *output_height = height;
999 /* Anamorphic 3: Power User Jamboree
1000 - Set everything based on specified values */
1002 /* Use specified storage dimensions */
1003 storage_aspect = (double)job->width / (double)job->height;
1005 height = job->height;
1007 /* Time to get picture dimensions that divide cleanly.*/
1008 width = MULTIPLE_MOD( width, mod);
1009 height = MULTIPLE_MOD( height, mod);
1011 /* Bind to max dimensions */
1012 if( maxWidth && width > maxWidth )
1015 // If we are keeping the display aspect, then we are going
1016 // to be modifying the PAR anyway. So it's preferred
1017 // to let the width/height stray some from the original
1018 // requested storage aspect.
1020 // But otherwise, PAR and DAR will change the least
1021 // if we stay as close as possible to the requested
1023 if ( !job->anamorphic.keep_display_aspect )
1025 height = ((double)width / storage_aspect) + 0.5;
1026 height = MULTIPLE_MOD( height, mod);
1029 if( maxHeight && height > maxHeight )
1032 // Ditto, see comment above
1033 if ( !job->anamorphic.keep_display_aspect )
1035 width = ((double)height * storage_aspect) + 0.5;
1036 width = MULTIPLE_MOD( width, mod);
1040 /* That finishes the storage dimensions. On to display. */
1041 if( job->anamorphic.dar_width && job->anamorphic.dar_height )
1043 /* We need to adjust the PAR to produce this aspect. */
1044 pixel_aspect_width = height * job->anamorphic.dar_width / job->anamorphic.dar_height;
1045 pixel_aspect_height = width;
1049 /* If we're doing ana 3 and not specifying a DAR, care needs to be taken.
1050 This indicates a PAR is potentially being set by the interface. But
1051 this is an output PAR, to correct a source, and it should not be assumed
1052 that it properly creates a display aspect ratio when applied to the source,
1053 which could easily be stored in a different resolution. */
1054 if( job->anamorphic.keep_display_aspect )
1056 /* We can ignore the possibility of a PAR change */
1057 pixel_aspect_width = height * ( (double)source_display_width / (double)cropped_height );
1058 pixel_aspect_height = width;
1062 int output_display_width = width * (double)pixel_aspect_width /
1063 (double)pixel_aspect_height;
1064 pixel_aspect_width = output_display_width;
1065 pixel_aspect_height = width;
1069 /* Back to caller */
1070 *output_width = width;
1071 *output_height = height;
1075 /* While x264 is smart enough to reduce fractions on its own, libavcodec
1076 needs some help with the math, so lose superfluous factors. */
1077 hb_reduce( output_par_width, output_par_height,
1078 pixel_aspect_width, pixel_aspect_height );
1082 * Calculates job width, height, and cropping parameters.
1083 * @param job Handle to hb_job_t.
1084 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
1085 * @param pixels Maximum desired pixel count.
1087 void hb_set_size( hb_job_t * job, double aspect, int pixels )
1089 hb_title_t * title = job->title;
1091 int croppedWidth = title->width - title->crop[2] - title->crop[3];
1092 int croppedHeight = title->height - title->crop[0] - title->crop[1];
1093 double croppedAspect = title->aspect * title->height * croppedWidth /
1094 croppedHeight / title->width;
1100 /* Keep the best possible aspect ratio */
1101 aspect = croppedAspect;
1104 /* Crop if necessary to obtain the desired ratio */
1105 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
1106 if( aspect < croppedAspect )
1108 /* Need to crop on the left and right */
1109 addCrop = croppedWidth - aspect * croppedHeight * title->width /
1110 title->aspect / title->height;
1113 addCrop = ( addCrop + 1 ) / 2;
1114 job->crop[2] += addCrop;
1115 job->crop[3] += addCrop;
1117 else if( addCrop & 2 )
1120 job->crop[2] += addCrop - 1;
1121 job->crop[3] += addCrop + 1;
1126 job->crop[2] += addCrop;
1127 job->crop[3] += addCrop;
1130 else if( aspect > croppedAspect )
1132 /* Need to crop on the top and bottom */
1133 addCrop = croppedHeight - croppedWidth * title->aspect *
1134 title->height / aspect / title->width;
1137 addCrop = ( addCrop + 1 ) / 2;
1138 job->crop[0] += addCrop;
1139 job->crop[1] += addCrop;
1141 else if( addCrop & 2 )
1144 job->crop[0] += addCrop - 1;
1145 job->crop[1] += addCrop + 1;
1150 job->crop[0] += addCrop;
1151 job->crop[1] += addCrop;
1155 /* Compute a resolution from the number of pixels and aspect */
1159 h = MULTIPLE_16( (int)( (double)w / aspect ) );
1160 if( w * h > pixels )
1166 job->width = 16 * i;
1167 job->height = MULTIPLE_16( (int)( (double)job->width / aspect ) );
1171 * Returns the number of jobs in the queue.
1172 * @param h Handle to hb_handle_t.
1173 * @return Number of jobs.
1175 int hb_count( hb_handle_t * h )
1177 return hb_list_count( h->jobs );
1181 * Returns handle to job at index i within the job list.
1182 * @param h Handle to hb_handle_t.
1183 * @param i Index of job.
1184 * @returns Handle to hb_job_t of desired job.
1186 hb_job_t * hb_job( hb_handle_t * h, int i )
1188 return hb_list_item( h->jobs, i );
1191 hb_job_t * hb_current_job( hb_handle_t * h )
1193 return( h->current_job );
1197 * Applies information from the given job to the official job instance.
1198 * @param h Handle to hb_handle_t.
1199 * @param title_index Index of the title to apply the chapter name to (1-based).
1200 * @param chapter The chapter to apply the name to (1-based).
1201 * @param job Job information to apply.
1203 void hb_set_chapter_name( hb_handle_t * h, int title_index, int chapter_index, const char * chapter_name )
1206 title = hb_get_title_by_index( h, title_index );
1208 hb_chapter_t * chapter = hb_list_item( title->list_chapter, chapter_index - 1 );
1210 strncpy(chapter->title, chapter_name, 1023);
1211 chapter->title[1023] = '\0';
1215 * Applies information from the given job to the official job instance.
1216 * Currently only applies information needed for anamorphic size calculation and previews.
1217 * @param h Handle to hb_handle_t.
1218 * @param title_index Index of the title to apply the job information to (1-based).
1219 * @param job Job information to apply.
1221 void hb_set_job( hb_handle_t * h, int title_index, hb_job_t * job )
1226 title = hb_get_title_by_index( h, title_index );
1228 hb_job_t * job_target = title->job;
1230 job_target->deinterlace = job->deinterlace;
1231 job_target->width = job->width;
1232 job_target->height = job->height;
1233 job_target->maxWidth = job->maxWidth;
1234 job_target->maxHeight = job->maxHeight;
1235 for (i = 0; i < 4; i++)
1237 job_target->crop[i] = job->crop[i];
1240 job_target->anamorphic = job->anamorphic;
1244 * Adds a job to the job list.
1245 * @param h Handle to hb_handle_t.
1246 * @param job Handle to hb_job_t.
1248 void hb_add( hb_handle_t * h, hb_job_t * job )
1250 hb_job_t * job_copy;
1251 hb_title_t * title, * title_copy;
1252 hb_chapter_t * chapter, * chapter_copy;
1254 hb_subtitle_t * subtitle, * subtitle_copy;
1255 hb_attachment_t * attachment;
1259 /* Copy the title */
1261 title_copy = malloc( sizeof( hb_title_t ) );
1262 memcpy( title_copy, title, sizeof( hb_title_t ) );
1264 title_copy->list_chapter = hb_list_init();
1265 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
1267 chapter = hb_list_item( title->list_chapter, i );
1268 chapter_copy = malloc( sizeof( hb_chapter_t ) );
1269 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
1270 hb_list_add( title_copy->list_chapter, chapter_copy );
1276 if( title->metadata )
1278 title_copy->metadata = malloc( sizeof( hb_metadata_t ) );
1280 if( title_copy->metadata )
1282 memcpy( title_copy->metadata, title->metadata, sizeof( hb_metadata_t ) );
1285 * Need to copy the artwork seperatly (TODO).
1287 if( title->metadata->coverart )
1289 title_copy->metadata->coverart = malloc( title->metadata->coverart_size );
1290 if( title_copy->metadata->coverart )
1292 memcpy( title_copy->metadata->coverart, title->metadata->coverart,
1293 title->metadata->coverart_size );
1295 title_copy->metadata->coverart_size = 0;
1301 /* Copy the audio track(s) we want */
1302 title_copy->list_audio = hb_list_init();
1303 for( i = 0; i < hb_list_count(job->list_audio); i++ )
1305 if( ( audio = hb_list_item( job->list_audio, i ) ) )
1307 hb_list_add( title_copy->list_audio, hb_audio_copy(audio) );
1311 /* Initialize subtitle list - filled out further below */
1312 title_copy->list_subtitle = hb_list_init();
1314 /* Copy all the attachments */
1315 title_copy->list_attachment = hb_list_init();
1316 for( i = 0; i < hb_list_count(title->list_attachment); i++ )
1318 if( ( attachment = hb_list_item( title->list_attachment, i ) ) )
1320 hb_list_add( title_copy->list_attachment, hb_attachment_copy(attachment) );
1325 * The following code is confusing, there are two ways in which
1326 * we select subtitles and it depends on whether this is single or
1329 * subtitle_scan may be enabled, in which case the first pass
1330 * scans all subtitles of that language. The second pass does not
1331 * select any because they are set at the end of the first pass.
1333 * We may have manually selected a subtitle, in which case that is
1334 * selected in the first pass of a single pass, or the second of a
1337 memset( audio_lang, 0, sizeof( audio_lang ) );
1339 if ( job->indepth_scan ) {
1342 * Find the first audio language that is being encoded
1344 for( i = 0; i < hb_list_count(job->list_audio); i++ )
1346 if( ( audio = hb_list_item( job->list_audio, i ) ) )
1348 strncpy(audio_lang, audio->config.lang.iso639_2, sizeof(audio_lang));
1355 * If doing a subtitle scan then add all the matching subtitles for this
1358 if ( job->indepth_scan )
1360 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
1362 subtitle = hb_list_item( title->list_subtitle, i );
1363 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 &&
1364 subtitle->source == VOBSUB )
1367 * Matched subtitle language with audio language, so
1368 * add this to our list to scan.
1370 * We will update the subtitle list on the second pass
1371 * later after the first pass has completed.
1373 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1374 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1375 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1380 * Not doing a subtitle scan in this pass, but maybe we are in the
1383 if( job->pass != 1 )
1386 * Copy all of them from the input job, to the title_copy/job_copy.
1388 for( i = 0; i < hb_list_count(job->list_subtitle); i++ ) {
1389 if( ( subtitle = hb_list_item( job->list_subtitle, i ) ) )
1391 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1392 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1393 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1400 job_copy = calloc( sizeof( hb_job_t ), 1 );
1401 memcpy( job_copy, job, sizeof( hb_job_t ) );
1402 title_copy->job = job_copy;
1403 job_copy->title = title_copy;
1404 job_copy->list_audio = title_copy->list_audio;
1405 job_copy->list_subtitle = title_copy->list_subtitle; // sharing list between title and job
1406 job_copy->file = strdup( job->file );
1408 job_copy->pause = h->pause_lock;
1410 /* Copy the job filter list */
1414 int filter_count = hb_list_count( job->filters );
1415 job_copy->filters = hb_list_init();
1416 for( i = 0; i < filter_count; i++ )
1419 * Copy the filters, since the MacGui reuses the global filter objects
1420 * meaning that queued up jobs overwrite the previous filter settings.
1421 * In reality, settings is probably the only field that needs duplicating
1422 * since it's the only value that is ever changed. But name is duplicated
1423 * as well for completeness. Not copying private_data since it gets
1424 * created for each job in renderInit.
1426 hb_filter_object_t * filter = hb_list_item( job->filters, i );
1427 hb_filter_object_t * filter_copy = malloc( sizeof( hb_filter_object_t ) );
1428 memcpy( filter_copy, filter, sizeof( hb_filter_object_t ) );
1430 filter_copy->name = strdup( filter->name );
1431 if( filter->settings )
1432 filter_copy->settings = strdup( filter->settings );
1433 hb_list_add( job_copy->filters, filter_copy );
1437 /* Add the job to the list */
1438 hb_list_add( h->jobs, job_copy );
1439 h->job_count = hb_count(h);
1440 h->job_count_permanent++;
1444 * Removes a job from the job list.
1445 * @param h Handle to hb_handle_t.
1446 * @param job Handle to hb_job_t.
1448 void hb_rem( hb_handle_t * h, hb_job_t * job )
1450 hb_list_rem( h->jobs, job );
1452 h->job_count = hb_count(h);
1453 if (h->job_count_permanent)
1454 h->job_count_permanent--;
1456 /* XXX free everything XXX */
1460 * Starts the conversion process.
1461 * Sets state to HB_STATE_WORKING.
1462 * calls hb_work_init, to launch work thread. Stores handle to work thread.
1463 * @param h Handle to hb_handle_t.
1465 void hb_start( hb_handle_t * h )
1468 h->job_count = hb_list_count( h->jobs );
1469 h->job_count_permanent = h->job_count;
1471 hb_lock( h->state_lock );
1472 h->state.state = HB_STATE_WORKING;
1473 #define p h->state.param.working
1476 p.job_count = h->job_count;
1484 hb_unlock( h->state_lock );
1489 h->work_thread = hb_work_init( h->jobs, &h->work_die, &h->work_error, &h->current_job );
1493 * Pauses the conversion process.
1494 * @param h Handle to hb_handle_t.
1496 void hb_pause( hb_handle_t * h )
1500 hb_lock( h->pause_lock );
1503 hb_current_job( h )->st_pause_date = hb_get_date();
1505 hb_lock( h->state_lock );
1506 h->state.state = HB_STATE_PAUSED;
1507 hb_unlock( h->state_lock );
1512 * Resumes the conversion process.
1513 * @param h Handle to hb_handle_t.
1515 void hb_resume( hb_handle_t * h )
1519 #define job hb_current_job( h )
1520 if( job->st_pause_date != -1 )
1522 job->st_paused += hb_get_date() - job->st_pause_date;
1526 hb_unlock( h->pause_lock );
1532 * Stops the conversion process.
1533 * @param h Handle to hb_handle_t.
1535 void hb_stop( hb_handle_t * h )
1539 h->job_count = hb_count(h);
1540 h->job_count_permanent = 0;
1546 * Stops the conversion process.
1547 * @param h Handle to hb_handle_t.
1549 void hb_scan_stop( hb_handle_t * h )
1553 h->job_count = hb_count(h);
1554 h->job_count_permanent = 0;
1560 * Gets a filter object with the given type and settings.
1561 * @param filter_id The type of filter to get.
1562 * @param settings The filter settings to use.
1563 * @returns The requested filter object.
1565 hb_filter_object_t * hb_get_filter_object(int filter_id, const char * settings)
1567 if (filter_id == HB_FILTER_ROTATE)
1569 hb_filter_rotate.settings = (char*)settings;
1570 return &hb_filter_rotate;
1573 if (filter_id == HB_FILTER_DETELECINE)
1575 hb_filter_detelecine.settings = (char*)settings;
1576 return &hb_filter_detelecine;
1579 if (filter_id == HB_FILTER_DECOMB)
1581 hb_filter_decomb.settings = (char*)settings;
1582 return &hb_filter_decomb;
1585 if (filter_id == HB_FILTER_DEINTERLACE)
1587 hb_filter_deinterlace.settings = (char*)settings;
1588 return &hb_filter_deinterlace;
1591 if (filter_id == HB_FILTER_DEBLOCK)
1593 hb_filter_deblock.settings = (char*)settings;
1594 return &hb_filter_deblock;
1597 if (filter_id == HB_FILTER_DENOISE)
1599 hb_filter_denoise.settings = (char*)settings;
1600 return &hb_filter_denoise;
1606 * Returns the state of the conversion process.
1607 * @param h Handle to hb_handle_t.
1608 * @param s Handle to hb_state_t which to copy the state data.
1610 void hb_get_state( hb_handle_t * h, hb_state_t * s )
1612 hb_lock( h->state_lock );
1614 memcpy( s, &h->state, sizeof( hb_state_t ) );
1615 if ( h->state.state == HB_STATE_SCANDONE || h->state.state == HB_STATE_WORKDONE )
1616 h->state.state = HB_STATE_IDLE;
1618 hb_unlock( h->state_lock );
1621 void hb_get_state2( hb_handle_t * h, hb_state_t * s )
1623 hb_lock( h->state_lock );
1625 memcpy( s, &h->state, sizeof( hb_state_t ) );
1627 hb_unlock( h->state_lock );
1631 * Called in MacGui in UpdateUI to check
1632 * for a new scan being completed to set a new source
1634 int hb_get_scancount( hb_handle_t * h)
1636 return h->scanCount;
1640 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init.
1641 * @param _h Pointer to handle to hb_handle_t.
1643 void hb_close( hb_handle_t ** _h )
1645 hb_handle_t * h = *_h;
1650 hb_thread_close( &h->main_thread );
1652 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1654 hb_list_rem( h->list_title, title );
1655 if( title->job && title->job->filters )
1657 hb_list_close( &title->job->filters );
1660 hb_title_close( &title );
1662 hb_list_close( &h->list_title );
1664 hb_list_close( &h->jobs );
1665 hb_lock_close( &h->state_lock );
1666 hb_lock_close( &h->pause_lock );
1673 * Cleans up libhb at a process level. Call before the app closes. Removes preview directory.
1675 void hb_global_close()
1679 struct dirent * entry;
1681 /* Find and remove temp folder */
1682 memset( dirname, 0, 1024 );
1683 hb_get_temporary_directory( dirname );
1685 dir = opendir( dirname );
1688 while( ( entry = readdir( dir ) ) )
1690 char filename[1024];
1691 if( entry->d_name[0] == '.' )
1695 memset( filename, 0, 1024 );
1696 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
1705 * Monitors the state of the update, scan, and work threads.
1706 * Sets scan done state when scan thread exits.
1707 * Sets work done state when work thread exits.
1708 * @param _h Handle to hb_handle_t
1710 static void thread_func( void * _h )
1712 hb_handle_t * h = (hb_handle_t *) _h;
1717 /* Create folder for temporary files */
1718 memset( dirname, 0, 1024 );
1719 hb_get_temporary_directory( dirname );
1721 hb_mkdir( dirname );
1725 /* In case the check_update thread hangs, it'll die sooner or
1726 later. Then, we join it here */
1727 if( h->update_thread &&
1728 hb_thread_has_exited( h->update_thread ) )
1730 hb_thread_close( &h->update_thread );
1733 /* Check if the scan thread is done */
1734 if( h->scan_thread &&
1735 hb_thread_has_exited( h->scan_thread ) )
1737 hb_thread_close( &h->scan_thread );
1743 hb_remove_previews( h );
1744 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1746 hb_list_rem( h->list_title, title );
1747 hb_title_close( &title );
1750 hb_log( "hb_scan: canceled" );
1754 hb_log( "libhb: scan thread found %d valid title(s)",
1755 hb_list_count( h->list_title ) );
1757 hb_lock( h->state_lock );
1758 h->state.state = HB_STATE_SCANDONE; //originally state.state
1759 hb_unlock( h->state_lock );
1760 /*we increment this sessions scan count by one for the MacGui
1761 to trigger a new source being set */
1765 /* Check if the work thread is done */
1766 if( h->work_thread &&
1767 hb_thread_has_exited( h->work_thread ) )
1769 hb_thread_close( &h->work_thread );
1771 hb_log( "libhb: work result = %d",
1773 hb_lock( h->state_lock );
1774 h->state.state = HB_STATE_WORKDONE;
1775 h->state.param.workdone.error = h->work_error;
1777 h->job_count = hb_count(h);
1778 if (h->job_count < 1)
1779 h->job_count_permanent = 0;
1780 hb_unlock( h->state_lock );
1786 if( h->scan_thread )
1789 hb_thread_close( &h->scan_thread );
1791 if( h->work_thread )
1794 hb_thread_close( &h->work_thread );
1796 hb_remove_previews( h );
1800 * Redirects stderr to the registered callback
1802 * @param _data Unused.
1804 static void redirect_thread_func(void * _data)
1808 #if defined( SYS_MINGW )
1809 // dup2 doesn't work on windows for some stupid reason
1810 stderr->_file = pfd[1];
1812 dup2(pfd[1], /*stderr*/ 2);
1814 FILE * log_f = fdopen(pfd[0], "rb");
1816 char line_buffer[500];
1817 while(fgets(line_buffer, 500, log_f) != NULL)
1819 hb_log_callback(line_buffer);
1825 * @param h Handle to hb_handle_t
1827 int hb_get_pid( hb_handle_t * h )
1833 * Returns the id for the given instance.
1834 * @param h Handle to hb_handle_t
1835 * @returns The ID for the given instance
1837 int hb_get_instance_id( hb_handle_t * h )
1843 * Returns the title with the given title index.
1844 * @param h Handle to hb_handle_t
1845 * @param title_index the index of the title to get
1846 * @returns The requested title
1848 hb_title_t * hb_get_title_by_index( hb_handle_t * h, int title_index )
1852 int count = hb_list_count( h->list_title );
1853 for (i = 0; i < count; i++)
1855 title = hb_list_item( h->list_title, i );
1856 if (title->index == title_index)
1866 * Sets the current state.
1867 * @param h Handle to hb_handle_t
1868 * @param s Handle to new hb_state_t
1870 void hb_set_state( hb_handle_t * h, hb_state_t * s )
1872 hb_lock( h->pause_lock );
1873 hb_lock( h->state_lock );
1874 memcpy( &h->state, s, sizeof( hb_state_t ) );
1875 if( h->state.state == HB_STATE_WORKING ||
1876 h->state.state == HB_STATE_SEARCHING )
1879 if (h->job_count < 1)
1880 h->job_count_permanent = 1;
1882 h->state.param.working.job_cur =
1883 h->job_count_permanent - hb_list_count( h->jobs );
1884 h->state.param.working.job_count = h->job_count_permanent;
1886 // Set which job is being worked on
1888 h->state.param.working.sequence_id = h->current_job->sequence_id;
1890 h->state.param.working.sequence_id = 0;
1892 hb_unlock( h->state_lock );
1893 hb_unlock( h->pause_lock );
1896 /* Passes a pointer to persistent data */
1897 hb_interjob_t * hb_interjob_get( hb_handle_t * h )