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_avcodec_close(AVCodecContext *avctx)
88 hb_lock( hb_avcodec_lock );
89 ret = avcodec_close(avctx);
90 hb_unlock( hb_avcodec_lock );
94 int hb_ff_layout_xlat(int64_t ff_channel_layout, int channels)
98 switch (ff_channel_layout)
101 hb_layout = HB_INPUT_CH_LAYOUT_MONO;
103 case CH_LAYOUT_STEREO:
104 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
106 case CH_LAYOUT_SURROUND:
107 hb_layout = HB_INPUT_CH_LAYOUT_3F;
109 case CH_LAYOUT_4POINT0:
110 hb_layout = HB_INPUT_CH_LAYOUT_3F1R;
113 hb_layout = HB_INPUT_CH_LAYOUT_2F2R;
116 hb_layout = HB_INPUT_CH_LAYOUT_2F2R;
118 case CH_LAYOUT_5POINT0:
119 hb_layout = HB_INPUT_CH_LAYOUT_3F2R;
121 case CH_LAYOUT_5POINT1:
122 hb_layout = HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
124 case CH_LAYOUT_5POINT0_BACK:
125 hb_layout = HB_INPUT_CH_LAYOUT_3F2R;
127 case CH_LAYOUT_5POINT1_BACK:
128 hb_layout = HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
130 case CH_LAYOUT_7POINT0:
131 hb_layout = HB_INPUT_CH_LAYOUT_3F4R;
133 case CH_LAYOUT_7POINT1:
134 hb_layout = HB_INPUT_CH_LAYOUT_3F4R|HB_INPUT_CH_LAYOUT_HAS_LFE;
136 case CH_LAYOUT_STEREO_DOWNMIX:
137 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
140 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
143 // Now make sure the chosen layout agrees with the number of channels
144 // ffmpeg tells us there are. It seems ffmpeg is sometimes confused
145 // about this. So we will make a best guess based on the number
147 int chans = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT( hb_layout );
148 if ( chans == channels )
152 hb_log( "Channels reported by ffmpeg (%d) != computed layout channels (%d).", channels, chans );
156 hb_layout = HB_INPUT_CH_LAYOUT_MONO;
159 hb_layout = HB_INPUT_CH_LAYOUT_STEREO;
162 hb_layout = HB_INPUT_CH_LAYOUT_3F;
165 hb_layout = HB_INPUT_CH_LAYOUT_3F1R;
168 hb_layout = HB_INPUT_CH_LAYOUT_3F2R;
171 hb_layout = HB_INPUT_CH_LAYOUT_3F2R|HB_INPUT_CH_LAYOUT_HAS_LFE;
174 hb_layout = HB_INPUT_CH_LAYOUT_3F4R;
177 hb_layout = HB_INPUT_CH_LAYOUT_3F4R|HB_INPUT_CH_LAYOUT_HAS_LFE;
180 hb_log("Unsupported number of audio channels (%d).\n", channels);
188 * Registers work objects, by adding the work object to a liked list.
189 * @param w Handle to hb_work_object_t to register.
191 void hb_register( hb_work_object_t * w )
193 w->next = hb_objects;
198 * Ensures that the process has been initialized.
200 static void process_init()
202 if (!hb_process_initialized)
204 #if defined( SYS_MINGW ) && defined( PTW32_STATIC_LIB )
205 pthread_win32_process_attach_np();
208 #if defined( _WIN32 ) || defined( __MINGW32__ )
209 setvbuf( stdout, NULL, _IONBF, 0 );
210 setvbuf( stderr, NULL, _IONBF, 0 );
212 hb_process_initialized = 1;
217 void (*hb_log_callback)(const char* message);
218 static void redirect_thread_func(void *);
220 #if defined( SYS_MINGW )
221 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
225 * Registers the given function as a logger. All logs will be passed to it.
226 * @param log_cb The function to register as a logger.
228 void hb_register_logger( void (*log_cb)(const char* message) )
232 hb_log_callback = log_cb;
233 hb_thread_init("ioredirect", redirect_thread_func, NULL, HB_NORMAL_PRIORITY);
237 * libhb initialization routine.
238 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
239 * @param update_check signals libhb to check for updated version from HandBrake website.
240 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
242 hb_handle_t * hb_init( int verbose, int update_check )
246 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
249 /* See hb_deep_log() and hb_log() in common.c */
250 global_verbosity_level = verbose;
252 putenv( "HB_DEBUG=1" );
254 h->id = hb_instance_counter++;
256 /* Check for an update on the website if asked to */
261 hb_log( "hb_init: checking for updates" );
262 date = hb_get_date();
263 h->update_thread = hb_update_init( &h->build, h->version );
267 if( hb_thread_has_exited( h->update_thread ) )
269 /* Immediate success or failure */
270 hb_thread_close( &h->update_thread );
273 if( hb_get_date() > date + 1000 )
275 /* Still nothing after one second. Connection problem,
276 let the thread die */
277 hb_log( "hb_init: connection problem, not waiting for "
286 * Initialise buffer pool
288 hb_buffer_pool_init();
290 /* CPU count detection */
291 hb_log( "hb_init: checking cpu count" );
292 h->cpu_count = hb_get_cpu_count();
294 h->list_title = hb_list_init();
295 h->jobs = hb_list_init();
297 h->state_lock = hb_lock_init();
298 h->state.state = HB_STATE_IDLE;
300 h->pause_lock = hb_lock_init();
302 h->interjob = calloc( sizeof( hb_interjob_t ), 1 );
307 /* Start library thread */
308 hb_log( "hb_init: starting libhb thread" );
310 h->main_thread = hb_thread_init( "libhb", thread_func, h,
311 HB_NORMAL_PRIORITY );
312 hb_register( &hb_sync_video );
313 hb_register( &hb_sync_audio );
314 hb_register( &hb_decmpeg2 );
315 hb_register( &hb_decvobsub );
316 hb_register( &hb_encvobsub );
317 hb_register( &hb_deccc608 );
318 hb_register( &hb_decsrtsub );
319 hb_register( &hb_decutf8sub );
320 hb_register( &hb_dectx3gsub );
321 hb_register( &hb_decssasub );
322 hb_register( &hb_render );
323 hb_register( &hb_encavcodec );
324 hb_register( &hb_encx264 );
325 hb_register( &hb_enctheora );
326 hb_register( &hb_deca52 );
327 hb_register( &hb_decdca );
328 hb_register( &hb_decavcodec );
329 hb_register( &hb_decavcodecv );
330 hb_register( &hb_decavcodecvi );
331 hb_register( &hb_decavcodecai );
332 hb_register( &hb_declpcm );
333 hb_register( &hb_encfaac );
334 hb_register( &hb_enclame );
335 hb_register( &hb_encvorbis );
336 hb_register( &hb_muxer );
338 hb_register( &hb_encca_aac );
345 * libhb initialization routine.
346 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
347 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
348 * @param update_check signals libhb to check for updated version from HandBrake website.
349 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
351 hb_handle_t * hb_init_dl( int verbose, int update_check )
353 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
356 /* See hb_log() in common.c */
357 if( verbose > HB_DEBUG_NONE )
359 putenv( "HB_DEBUG=1" );
362 h->id = hb_instance_counter++;
364 /* Check for an update on the website if asked to */
369 hb_log( "hb_init: checking for updates" );
370 date = hb_get_date();
371 h->update_thread = hb_update_init( &h->build, h->version );
375 if( hb_thread_has_exited( h->update_thread ) )
377 /* Immediate success or failure */
378 hb_thread_close( &h->update_thread );
381 if( hb_get_date() > date + 1000 )
383 /* Still nothing after one second. Connection problem,
384 let the thread die */
385 hb_log( "hb_init: connection problem, not waiting for "
393 /* CPU count detection */
394 hb_log( "hb_init: checking cpu count" );
395 h->cpu_count = hb_get_cpu_count();
397 h->list_title = hb_list_init();
398 h->jobs = hb_list_init();
399 h->current_job = NULL;
401 h->state_lock = hb_lock_init();
402 h->state.state = HB_STATE_IDLE;
404 h->pause_lock = hb_lock_init();
408 avcodec_register_all();
410 /* Start library thread */
411 hb_log( "hb_init: starting libhb thread" );
413 h->main_thread = hb_thread_init( "libhb", thread_func, h,
414 HB_NORMAL_PRIORITY );
416 hb_register( &hb_sync_video );
417 hb_register( &hb_sync_audio );
418 hb_register( &hb_decmpeg2 );
419 hb_register( &hb_decvobsub );
420 hb_register( &hb_encvobsub );
421 hb_register( &hb_deccc608 );
422 hb_register( &hb_decsrtsub );
423 hb_register( &hb_decutf8sub );
424 hb_register( &hb_dectx3gsub );
425 hb_register( &hb_decssasub );
426 hb_register( &hb_render );
427 hb_register( &hb_encavcodec );
428 hb_register( &hb_encx264 );
429 hb_register( &hb_enctheora );
430 hb_register( &hb_deca52 );
431 hb_register( &hb_decdca );
432 hb_register( &hb_decavcodec );
433 hb_register( &hb_decavcodecv );
434 hb_register( &hb_decavcodecvi );
435 hb_register( &hb_decavcodecai );
436 hb_register( &hb_declpcm );
437 hb_register( &hb_encfaac );
438 hb_register( &hb_enclame );
439 hb_register( &hb_encvorbis );
440 hb_register( &hb_muxer );
442 hb_register( &hb_encca_aac );
450 * Returns current version of libhb.
451 * @param h Handle to hb_handle_t.
452 * @return character array of version number.
454 char * hb_get_version( hb_handle_t * h )
456 return HB_PROJECT_VERSION;
460 * Returns current build of libhb.
461 * @param h Handle to hb_handle_t.
462 * @return character array of build number.
464 int hb_get_build( hb_handle_t * h )
466 return HB_PROJECT_BUILD;
470 * Checks for needed update.
471 * @param h Handle to hb_handle_t.
472 * @param version Pointer to handle where version will be copied.
473 * @return update indicator.
475 int hb_check_update( hb_handle_t * h, char ** version )
477 *version = ( h->build < 0 ) ? NULL : h->version;
482 * Sets the cpu count to the desired value.
483 * @param h Handle to hb_handle_t
484 * @param cpu_count Number of CPUs to use.
486 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
488 cpu_count = MAX( 1, cpu_count );
489 cpu_count = MIN( cpu_count, 8 );
490 h->cpu_count = cpu_count;
494 * Deletes current previews associated with titles
495 * @param h Handle to hb_handle_t
497 void hb_remove_previews( hb_handle_t * h )
504 struct dirent * entry;
506 memset( dirname, 0, 1024 );
507 hb_get_temporary_directory( dirname );
508 dir = opendir( dirname );
509 if (dir == NULL) return;
511 count = hb_list_count( h->list_title );
512 while( ( entry = readdir( dir ) ) )
514 if( entry->d_name[0] == '.' )
518 for( i = 0; i < count; i++ )
520 title = hb_list_item( h->list_title, i );
521 len = snprintf( filename, 1024, "%d_%d", h->id, title->index );
522 if (strncmp(entry->d_name, filename, len) == 0)
524 snprintf( filename, 1024, "%s/%s", dirname, entry->d_name );
534 * Initializes a scan of the by calling hb_scan_init
535 * @param h Handle to hb_handle_t
536 * @param path location of VIDEO_TS folder.
537 * @param title_index Desired title to scan. 0 for all titles.
538 * @param preview_count Number of preview images to generate.
539 * @param store_previews Whether or not to write previews to disk.
541 void hb_scan( hb_handle_t * h, const char * path, int title_index,
542 int preview_count, int store_previews )
548 /* Clean up from previous scan */
549 hb_remove_previews( h );
550 while( ( title = hb_list_item( h->list_title, 0 ) ) )
552 hb_list_rem( h->list_title, title );
553 hb_title_close( &title );
556 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
557 h->scan_thread = hb_scan_init( h, &h->scan_die, path, title_index,
558 h->list_title, preview_count,
563 * Returns the list of titles found.
564 * @param h Handle to hb_handle_t
565 * @return Handle to hb_list_t of the title list.
567 hb_list_t * hb_get_titles( hb_handle_t * h )
569 return h->list_title;
573 * Create preview image of desired title a index of picture.
574 * @param h Handle to hb_handle_t.
575 * @param title_index Index of the title to get the preview for (1-based).
576 * @param picture Index in title.
577 * @param buffer Handle to buffer were image will be drawn.
579 void hb_get_preview_by_index( hb_handle_t * h, int title_index, int picture, uint8_t * buffer )
583 title = hb_get_title_by_index( h, title_index );
586 hb_get_preview( h, title, picture, buffer );
591 * Create preview image of desired title a index of picture.
592 * @param h Handle to hb_handle_t.
593 * @param title Handle to hb_title_t of desired title.
594 * @param picture Index in title.
595 * @param buffer Handle to buffer were image will be drawn.
597 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
600 hb_job_t * job = title->job;
603 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
605 AVPicture pic_in, pic_preview, pic_deint, pic_crop, pic_scale;
606 struct SwsContext * context;
608 int rgb_width = ((job->width + 7) >> 3) << 3;
611 swsflags = SWS_LANCZOS | SWS_ACCURATE_RND;
613 buf1 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
614 buf2 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
615 buf3 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, rgb_width, job->height ) );
616 buf4 = av_malloc( avpicture_get_size( PIX_FMT_RGB32, rgb_width, job->height ) );
617 avpicture_fill( &pic_in, buf1, PIX_FMT_YUV420P,
618 title->width, title->height );
619 avpicture_fill( &pic_deint, buf2, PIX_FMT_YUV420P,
620 title->width, title->height );
621 avpicture_fill( &pic_scale, buf3, PIX_FMT_YUV420P,
622 rgb_width, job->height );
623 avpicture_fill( &pic_preview, buf4, PIX_FMT_RGB32,
624 rgb_width, job->height );
626 // Allocate the AVPicture frames and fill in
628 memset( filename, 0, 1024 );
630 hb_get_tempory_filename( h, filename, "%d_%d_%d",
631 h->id, title->index, picture );
633 file = fopen( filename, "rb" );
636 hb_log( "hb_get_preview: fopen failed" );
640 fread( buf1, avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height), 1, file );
643 if( job->deinterlace )
645 // Deinterlace and crop
646 avpicture_deinterlace( &pic_deint, &pic_in, PIX_FMT_YUV420P, title->width, title->height );
647 av_picture_crop( &pic_crop, &pic_deint, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
652 av_picture_crop( &pic_crop, &pic_in, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
655 // Get scaling context
656 context = sws_getContext(title->width - (job->crop[2] + job->crop[3]),
657 title->height - (job->crop[0] + job->crop[1]),
659 job->width, job->height, PIX_FMT_YUV420P,
660 swsflags, NULL, NULL, NULL);
664 pic_crop.data, pic_crop.linesize,
665 0, title->height - (job->crop[0] + job->crop[1]),
666 pic_scale.data, pic_scale.linesize);
669 sws_freeContext( context );
671 // Get preview context
672 context = sws_getContext(rgb_width, job->height, PIX_FMT_YUV420P,
673 rgb_width, job->height, PIX_FMT_RGB32,
674 swsflags, NULL, NULL, NULL);
678 pic_scale.data, pic_scale.linesize,
680 pic_preview.data, pic_preview.linesize);
683 sws_freeContext( context );
685 preview_size = pic_preview.linesize[0];
687 for( i = 0; i < job->height; i++ )
689 memcpy( pen, buf4 + preview_size * i, 4 * job->width );
690 pen += 4 * job->width;
694 avpicture_free( &pic_preview );
695 avpicture_free( &pic_scale );
696 avpicture_free( &pic_deint );
697 avpicture_free( &pic_in );
701 * Analyzes a frame to detect interlacing artifacts
702 * and returns true if interlacing (combing) is found.
704 * Code taken from Thomas Oestreich's 32detect filter
705 * in the Transcode project, with minor formatting changes.
707 * @param buf An hb_buffer structure holding valid frame data
708 * @param width The frame's width in pixels
709 * @param height The frame's height in pixels
710 * @param color_equal Sensitivity for detecting similar colors
711 * @param color_diff Sensitivity for detecting different colors
712 * @param threshold Sensitivity for flagging planes as combed
713 * @param prog_equal Sensitivity for detecting similar colors on progressive frames
714 * @param prog_diff Sensitivity for detecting different colors on progressive frames
715 * @param prog_threshold Sensitivity for flagging progressive frames as combed
717 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 )
719 int j, k, n, off, cc_1, cc_2, cc[3];
720 // int flag[3] ; // debugging flag
721 uint16_t s1, s2, s3, s4;
726 if ( buf->flags & 16 )
728 /* Frame is progressive, be more discerning. */
729 color_diff = prog_diff;
730 color_equal = prog_equal;
731 threshold = prog_threshold;
734 /* One pas for Y, one pass for Cb, one pass for Cr */
735 for( k = 0; k < 3; k++ )
739 /* Y has already been checked, now offset by Y's dimensions
740 and divide all the other values by 2, since Cr and Cb
741 are half-size compared to Y. */
742 offset = width * height;
748 /* Y and Cb are done, so the offset needs to be bumped
749 so it's width*height + (width / 2) * (height / 2) */
753 for( j = 0; j < width; ++j )
757 for( n = 0; n < ( height - 4 ); n = n + 2 )
759 /* Look at groups of 4 sequential horizontal lines */
760 s1 = ( ( buf->data + offset )[ off + j ] & 0xff );
761 s2 = ( ( buf->data + offset )[ off + j + width ] & 0xff );
762 s3 = ( ( buf->data + offset )[ off + j + 2 * width ] & 0xff );
763 s4 = ( ( buf->data + offset )[ off + j + 3 * width ] & 0xff );
765 /* Note if the 1st and 2nd lines are more different in
766 color than the 1st and 3rd lines are similar in color.*/
767 if ( ( abs( s1 - s3 ) < color_equal ) &&
768 ( abs( s1 - s2 ) > color_diff ) )
771 /* Note if the 2nd and 3rd lines are more different in
772 color than the 2nd and 4th lines are similar in color.*/
773 if ( ( abs( s2 - s4 ) < color_equal ) &&
774 ( abs( s2 - s3 ) > color_diff) )
777 /* Now move down 2 horizontal lines before starting over.*/
783 /* The final cc score for a plane is the percentage of combed pixels it contains.
784 Because sensitivity goes down to hundreths of a percent, multiply by 1000
785 so it will be easy to compare against the threhold value which is an integer. */
786 cc[k] = (int)( ( cc_1 + cc_2 ) * 1000.0 / ( width * height ) );
790 /* HandBrake is all yuv420, so weight the average percentage of all 3 planes accordingly.*/
791 int average_cc = ( 2 * cc[0] + ( cc[1] / 2 ) + ( cc[2] / 2 ) ) / 3;
793 /* Now see if that average percentage of combed pixels surpasses the threshold percentage given by the user.*/
794 if( average_cc > threshold )
797 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" );
803 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" );
806 /* Reaching this point means no combing detected. */
812 * Calculates job width and height for anamorphic content,
814 * @param h Instance handle
815 * @param title_index Index of the title/job to inspect (1-based).
816 * @param output_width Pointer to returned storage width
817 * @param output_height Pointer to returned storage height
818 * @param output_par_width Pointer to returned pixel width
819 * @param output_par_height Pointer to returned pixel height
821 void hb_set_anamorphic_size_by_index( hb_handle_t * h, int title_index,
822 int *output_width, int *output_height,
823 int *output_par_width, int *output_par_height )
826 title = hb_get_title_by_index( h, title_index );
828 hb_set_anamorphic_size( title->job, output_width, output_height, output_par_width, output_par_height );
832 * Calculates job width and height for anamorphic content,
834 * @param job Handle to hb_job_t
835 * @param output_width Pointer to returned storage width
836 * @param output_height Pointer to returned storage height
837 * @param output_par_width Pointer to returned pixel width
838 * @param output_par_height Pointer to returned pixel height
840 void hb_set_anamorphic_size( hb_job_t * job,
841 int *output_width, int *output_height,
842 int *output_par_width, int *output_par_height )
844 /* Set up some variables to make the math easier to follow. */
845 hb_title_t * title = job->title;
846 int cropped_width = title->width - job->crop[2] - job->crop[3] ;
847 int cropped_height = title->height - job->crop[0] - job->crop[1] ;
848 double storage_aspect = (double)cropped_width / (double)cropped_height;
849 int mod = job->modulus ? job->modulus : 16;
850 double aspect = title->aspect;
852 int pixel_aspect_width = job->anamorphic.par_width;
853 int pixel_aspect_height = job->anamorphic.par_height;
855 /* If a source was really NTSC or PAL and the user specified ITU PAR
856 values, replace the standard PAR values with the ITU broadcast ones. */
857 if( title->width == 720 && job->anamorphic.itu_par )
859 // convert aspect to a scaled integer so we can test for 16:9 & 4:3
860 // aspect ratios ignoring insignificant differences in the LSBs of
861 // the floating point representation.
862 int iaspect = aspect * 9.;
864 /* Handle ITU PARs */
865 if (title->height == 480)
870 /* It's widescreen */
871 pixel_aspect_width = 40;
872 pixel_aspect_height = 33;
874 else if (iaspect == 12)
877 pixel_aspect_width = 10;
878 pixel_aspect_height = 11;
881 else if (title->height == 576)
886 /* It's widescreen */
887 pixel_aspect_width = 16;
888 pixel_aspect_height = 11;
890 else if (iaspect == 12)
893 pixel_aspect_width = 12;
894 pixel_aspect_height = 11;
899 /* Figure out what width the source would display at. */
900 int source_display_width = cropped_width * (double)pixel_aspect_width /
901 (double)pixel_aspect_height ;
904 3 different ways of deciding output dimensions:
905 - 1: Strict anamorphic, preserve source dimensions
906 - 2: Loose anamorphic, round to mod16 and preserve storage aspect ratio
907 - 3: Power user anamorphic, specify everything
910 switch( job->anamorphic.mode )
913 /* Strict anamorphic */
914 *output_width = cropped_width;
915 *output_height = cropped_height;
916 *output_par_width = title->pixel_aspect_width;
917 *output_par_height = title->pixel_aspect_height;
921 /* "Loose" anamorphic.
922 - Uses mod16-compliant dimensions,
923 - Allows users to set the width
926 // height: Gets set later, ignore user job->height value
928 /* Gotta handle bounding dimensions.
929 If the width is too big, just reset it with no rescaling.
930 Instead of using the aspect-scaled job height,
931 we need to see if the job width divided by the storage aspect
932 is bigger than the max. If so, set it to the max (this is sloppy).
933 If not, set job height to job width divided by storage aspect.
936 if ( job->maxWidth && (job->maxWidth < job->width) )
937 width = job->maxWidth;
939 /* Time to get picture width that divide cleanly.*/
940 width = MULTIPLE_MOD( width, mod);
942 /* Verify these new dimensions don't violate max height and width settings */
943 if ( job->maxWidth && (job->maxWidth < job->width) )
944 width = job->maxWidth;
946 height = ((double)width / storage_aspect) + 0.5;
948 if ( job->maxHeight && (job->maxHeight < height) )
949 height = job->maxHeight;
951 /* Time to get picture height that divide cleanly.*/
952 height = MULTIPLE_MOD( height, mod);
954 /* Verify these new dimensions don't violate max height and width settings */
955 if ( job->maxHeight && (job->maxHeight < height) )
956 height = job->maxHeight;
958 /* The film AR is the source's display width / cropped source height.
959 The output display width is the output height * film AR.
960 The output PAR is the output display width / output storage width. */
961 pixel_aspect_width = height * source_display_width / cropped_height;
962 pixel_aspect_height = width;
964 /* Pass the results back to the caller */
965 *output_width = width;
966 *output_height = height;
970 /* Anamorphic 3: Power User Jamboree
971 - Set everything based on specified values */
973 /* Use specified storage dimensions */
975 height = job->height;
977 /* Bind to max dimensions */
978 if( job->maxWidth && width > job->maxWidth )
979 width = job->maxWidth;
980 if( job->maxHeight && height > job->maxHeight )
981 height = job->maxHeight;
983 /* Time to get picture dimensions that divide cleanly.*/
984 width = MULTIPLE_MOD( width, mod);
985 height = MULTIPLE_MOD( height, mod);
987 /* Verify we're still within max dimensions */
988 if( job->maxWidth && width > job->maxWidth )
989 width = job->maxWidth - (mod/2);
990 if( job->maxHeight && height > job->maxHeight )
991 height = job->maxHeight - (mod/2);
993 /* Re-ensure we have picture dimensions that divide cleanly. */
994 width = MULTIPLE_MOD( width, mod );
995 height = MULTIPLE_MOD( height, mod );
997 /* That finishes the storage dimensions. On to display. */
998 if( job->anamorphic.dar_width && job->anamorphic.dar_height )
1000 /* We need to adjust the PAR to produce this aspect. */
1001 pixel_aspect_width = height * job->anamorphic.dar_width / job->anamorphic.dar_height;
1002 pixel_aspect_height = width;
1006 /* If we're doing ana 3 and not specifying a DAR, care needs to be taken.
1007 This indicates a PAR is potentially being set by the interface. But
1008 this is an output PAR, to correct a source, and it should not be assumed
1009 that it properly creates a display aspect ratio when applied to the source,
1010 which could easily be stored in a different resolution. */
1011 if( job->anamorphic.keep_display_aspect )
1013 /* We can ignore the possibility of a PAR change */
1014 pixel_aspect_width = height * ( (double)source_display_width / (double)cropped_height );
1015 pixel_aspect_height = width;
1019 int output_display_width = width * (double)pixel_aspect_width /
1020 (double)pixel_aspect_height;
1021 pixel_aspect_width = output_display_width;
1022 pixel_aspect_height = width;
1026 /* Back to caller */
1027 *output_width = width;
1028 *output_height = height;
1032 /* While x264 is smart enough to reduce fractions on its own, libavcodec
1033 needs some help with the math, so lose superfluous factors. */
1034 hb_reduce( output_par_width, output_par_height,
1035 pixel_aspect_width, pixel_aspect_height );
1039 * Calculates job width, height, and cropping parameters.
1040 * @param job Handle to hb_job_t.
1041 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
1042 * @param pixels Maximum desired pixel count.
1044 void hb_set_size( hb_job_t * job, double aspect, int pixels )
1046 hb_title_t * title = job->title;
1048 int croppedWidth = title->width - title->crop[2] - title->crop[3];
1049 int croppedHeight = title->height - title->crop[0] - title->crop[1];
1050 double croppedAspect = title->aspect * title->height * croppedWidth /
1051 croppedHeight / title->width;
1057 /* Keep the best possible aspect ratio */
1058 aspect = croppedAspect;
1061 /* Crop if necessary to obtain the desired ratio */
1062 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
1063 if( aspect < croppedAspect )
1065 /* Need to crop on the left and right */
1066 addCrop = croppedWidth - aspect * croppedHeight * title->width /
1067 title->aspect / title->height;
1070 addCrop = ( addCrop + 1 ) / 2;
1071 job->crop[2] += addCrop;
1072 job->crop[3] += addCrop;
1074 else if( addCrop & 2 )
1077 job->crop[2] += addCrop - 1;
1078 job->crop[3] += addCrop + 1;
1083 job->crop[2] += addCrop;
1084 job->crop[3] += addCrop;
1087 else if( aspect > croppedAspect )
1089 /* Need to crop on the top and bottom */
1090 addCrop = croppedHeight - croppedWidth * title->aspect *
1091 title->height / aspect / title->width;
1094 addCrop = ( addCrop + 1 ) / 2;
1095 job->crop[0] += addCrop;
1096 job->crop[1] += addCrop;
1098 else if( addCrop & 2 )
1101 job->crop[0] += addCrop - 1;
1102 job->crop[1] += addCrop + 1;
1107 job->crop[0] += addCrop;
1108 job->crop[1] += addCrop;
1112 /* Compute a resolution from the number of pixels and aspect */
1116 h = MULTIPLE_16( (int)( (double)w / aspect ) );
1117 if( w * h > pixels )
1123 job->width = 16 * i;
1124 job->height = MULTIPLE_16( (int)( (double)job->width / aspect ) );
1128 * Returns the number of jobs in the queue.
1129 * @param h Handle to hb_handle_t.
1130 * @return Number of jobs.
1132 int hb_count( hb_handle_t * h )
1134 return hb_list_count( h->jobs );
1138 * Returns handle to job at index i within the job list.
1139 * @param h Handle to hb_handle_t.
1140 * @param i Index of job.
1141 * @returns Handle to hb_job_t of desired job.
1143 hb_job_t * hb_job( hb_handle_t * h, int i )
1145 return hb_list_item( h->jobs, i );
1148 hb_job_t * hb_current_job( hb_handle_t * h )
1150 return( h->current_job );
1154 * Applies information from the given job to the official job instance.
1155 * @param h Handle to hb_handle_t.
1156 * @param title_index Index of the title to apply the chapter name to (1-based).
1157 * @param chapter The chapter to apply the name to (1-based).
1158 * @param job Job information to apply.
1160 void hb_set_chapter_name( hb_handle_t * h, int title_index, int chapter_index, const char * chapter_name )
1163 title = hb_get_title_by_index( h, title_index );
1165 hb_chapter_t * chapter = hb_list_item( title->list_chapter, chapter_index - 1 );
1167 strncpy(chapter->title, chapter_name, 1023);
1168 chapter->title[1023] = '\0';
1172 * Applies information from the given job to the official job instance.
1173 * Currently only applies information needed for anamorphic size calculation and previews.
1174 * @param h Handle to hb_handle_t.
1175 * @param title_index Index of the title to apply the job information to (1-based).
1176 * @param job Job information to apply.
1178 void hb_set_job( hb_handle_t * h, int title_index, hb_job_t * job )
1183 title = hb_get_title_by_index( h, title_index );
1185 hb_job_t * job_target = title->job;
1187 job_target->deinterlace = job->deinterlace;
1188 job_target->width = job->width;
1189 job_target->height = job->height;
1190 job_target->maxWidth = job->maxWidth;
1191 job_target->maxHeight = job->maxHeight;
1192 for (i = 0; i < 4; i++)
1194 job_target->crop[i] = job->crop[i];
1197 job_target->anamorphic = job->anamorphic;
1201 * Adds a job to the job list.
1202 * @param h Handle to hb_handle_t.
1203 * @param job Handle to hb_job_t.
1205 void hb_add( hb_handle_t * h, hb_job_t * job )
1207 hb_job_t * job_copy;
1208 hb_title_t * title, * title_copy;
1209 hb_chapter_t * chapter, * chapter_copy;
1211 hb_subtitle_t * subtitle, * subtitle_copy;
1215 /* Copy the title */
1217 title_copy = malloc( sizeof( hb_title_t ) );
1218 memcpy( title_copy, title, sizeof( hb_title_t ) );
1220 title_copy->list_chapter = hb_list_init();
1221 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
1223 chapter = hb_list_item( title->list_chapter, i );
1224 chapter_copy = malloc( sizeof( hb_chapter_t ) );
1225 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
1226 hb_list_add( title_copy->list_chapter, chapter_copy );
1232 if( title->metadata )
1234 title_copy->metadata = malloc( sizeof( hb_metadata_t ) );
1236 if( title_copy->metadata )
1238 memcpy( title_copy->metadata, title->metadata, sizeof( hb_metadata_t ) );
1241 * Need to copy the artwork seperatly (TODO).
1243 if( title->metadata->coverart )
1245 title_copy->metadata->coverart = malloc( title->metadata->coverart_size );
1246 if( title_copy->metadata->coverart )
1248 memcpy( title_copy->metadata->coverart, title->metadata->coverart,
1249 title->metadata->coverart_size );
1251 title_copy->metadata->coverart_size = 0;
1257 /* Copy the audio track(s) we want */
1258 title_copy->list_audio = hb_list_init();
1260 for( i = 0; i < hb_list_count(job->list_audio); i++ )
1262 if( ( audio = hb_list_item( job->list_audio, i ) ) )
1264 hb_list_add( title_copy->list_audio, hb_audio_copy(audio) );
1268 title_copy->list_subtitle = hb_list_init();
1271 * The following code is confusing, there are two ways in which
1272 * we select subtitles and it depends on whether this is single or
1275 * subtitle_scan may be enabled, in which case the first pass
1276 * scans all subtitles of that language. The second pass does not
1277 * select any because they are set at the end of the first pass.
1279 * We may have manually selected a subtitle, in which case that is
1280 * selected in the first pass of a single pass, or the second of a
1283 memset( audio_lang, 0, sizeof( audio_lang ) );
1285 if ( job->indepth_scan ) {
1288 * Find the first audio language that is being encoded
1290 for( i = 0; i < hb_list_count(job->list_audio); i++ )
1292 if( ( audio = hb_list_item( job->list_audio, i ) ) )
1294 strncpy(audio_lang, audio->config.lang.iso639_2, sizeof(audio_lang));
1301 * If doing a subtitle scan then add all the matching subtitles for this
1304 if ( job->indepth_scan )
1306 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
1308 subtitle = hb_list_item( title->list_subtitle, i );
1309 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 &&
1310 subtitle->source == VOBSUB )
1313 * Matched subtitle language with audio language, so
1314 * add this to our list to scan.
1316 * We will update the subtitle list on the second pass
1317 * later after the first pass has completed.
1319 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1320 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1321 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1326 * Not doing a subtitle scan in this pass, but maybe we are in the
1329 if( job->pass != 1 )
1332 * Copy all of them from the input job, to the title_copy/job_copy.
1334 for( i = 0; i < hb_list_count(job->list_subtitle); i++ ) {
1335 if( ( subtitle = hb_list_item( job->list_subtitle, i ) ) )
1337 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1338 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1339 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1346 job_copy = calloc( sizeof( hb_job_t ), 1 );
1347 memcpy( job_copy, job, sizeof( hb_job_t ) );
1348 title_copy->job = job_copy;
1349 job_copy->title = title_copy;
1350 job_copy->list_audio = title_copy->list_audio;
1351 job_copy->list_subtitle = title_copy->list_subtitle; // sharing list between title and job
1352 job_copy->file = strdup( job->file );
1354 job_copy->pause = h->pause_lock;
1356 /* Copy the job filter list */
1360 int filter_count = hb_list_count( job->filters );
1361 job_copy->filters = hb_list_init();
1362 for( i = 0; i < filter_count; i++ )
1365 * Copy the filters, since the MacGui reuses the global filter objects
1366 * meaning that queued up jobs overwrite the previous filter settings.
1367 * In reality, settings is probably the only field that needs duplicating
1368 * since it's the only value that is ever changed. But name is duplicated
1369 * as well for completeness. Not copying private_data since it gets
1370 * created for each job in renderInit.
1372 hb_filter_object_t * filter = hb_list_item( job->filters, i );
1373 hb_filter_object_t * filter_copy = malloc( sizeof( hb_filter_object_t ) );
1374 memcpy( filter_copy, filter, sizeof( hb_filter_object_t ) );
1376 filter_copy->name = strdup( filter->name );
1377 if( filter->settings )
1378 filter_copy->settings = strdup( filter->settings );
1379 hb_list_add( job_copy->filters, filter_copy );
1383 /* Add the job to the list */
1384 hb_list_add( h->jobs, job_copy );
1385 h->job_count = hb_count(h);
1386 h->job_count_permanent++;
1390 * Removes a job from the job list.
1391 * @param h Handle to hb_handle_t.
1392 * @param job Handle to hb_job_t.
1394 void hb_rem( hb_handle_t * h, hb_job_t * job )
1396 hb_list_rem( h->jobs, job );
1398 h->job_count = hb_count(h);
1399 if (h->job_count_permanent)
1400 h->job_count_permanent--;
1402 /* XXX free everything XXX */
1406 * Starts the conversion process.
1407 * Sets state to HB_STATE_WORKING.
1408 * calls hb_work_init, to launch work thread. Stores handle to work thread.
1409 * @param h Handle to hb_handle_t.
1411 void hb_start( hb_handle_t * h )
1414 h->job_count = hb_list_count( h->jobs );
1415 h->job_count_permanent = h->job_count;
1417 hb_lock( h->state_lock );
1418 h->state.state = HB_STATE_WORKING;
1419 #define p h->state.param.working
1422 p.job_count = h->job_count;
1430 hb_unlock( h->state_lock );
1435 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
1436 &h->work_die, &h->work_error, &h->current_job );
1440 * Pauses the conversion process.
1441 * @param h Handle to hb_handle_t.
1443 void hb_pause( hb_handle_t * h )
1447 hb_lock( h->pause_lock );
1450 hb_current_job( h )->st_pause_date = hb_get_date();
1452 hb_lock( h->state_lock );
1453 h->state.state = HB_STATE_PAUSED;
1454 hb_unlock( h->state_lock );
1459 * Resumes the conversion process.
1460 * @param h Handle to hb_handle_t.
1462 void hb_resume( hb_handle_t * h )
1466 #define job hb_current_job( h )
1467 if( job->st_pause_date != -1 )
1469 job->st_paused += hb_get_date() - job->st_pause_date;
1473 hb_unlock( h->pause_lock );
1479 * Stops the conversion process.
1480 * @param h Handle to hb_handle_t.
1482 void hb_stop( hb_handle_t * h )
1486 h->job_count = hb_count(h);
1487 h->job_count_permanent = 0;
1493 * Stops the conversion process.
1494 * @param h Handle to hb_handle_t.
1496 void hb_scan_stop( hb_handle_t * h )
1500 h->job_count = hb_count(h);
1501 h->job_count_permanent = 0;
1507 * Gets a filter object with the given type and settings.
1508 * @param filter_id The type of filter to get.
1509 * @param settings The filter settings to use.
1510 * @returns The requested filter object.
1512 hb_filter_object_t * hb_get_filter_object(int filter_id, const char * settings)
1514 if (filter_id == HB_FILTER_ROTATE)
1516 hb_filter_rotate.settings = (char*)settings;
1517 return &hb_filter_rotate;
1520 if (filter_id == HB_FILTER_DETELECINE)
1522 hb_filter_detelecine.settings = (char*)settings;
1523 return &hb_filter_detelecine;
1526 if (filter_id == HB_FILTER_DECOMB)
1528 hb_filter_decomb.settings = (char*)settings;
1529 return &hb_filter_decomb;
1532 if (filter_id == HB_FILTER_DEINTERLACE)
1534 hb_filter_deinterlace.settings = (char*)settings;
1535 return &hb_filter_deinterlace;
1538 if (filter_id == HB_FILTER_DEBLOCK)
1540 hb_filter_deblock.settings = (char*)settings;
1541 return &hb_filter_deblock;
1544 if (filter_id == HB_FILTER_DENOISE)
1546 hb_filter_denoise.settings = (char*)settings;
1547 return &hb_filter_denoise;
1553 * Returns the state of the conversion process.
1554 * @param h Handle to hb_handle_t.
1555 * @param s Handle to hb_state_t which to copy the state data.
1557 void hb_get_state( hb_handle_t * h, hb_state_t * s )
1559 hb_lock( h->state_lock );
1561 memcpy( s, &h->state, sizeof( hb_state_t ) );
1562 if ( h->state.state == HB_STATE_SCANDONE || h->state.state == HB_STATE_WORKDONE )
1563 h->state.state = HB_STATE_IDLE;
1565 hb_unlock( h->state_lock );
1568 void hb_get_state2( hb_handle_t * h, hb_state_t * s )
1570 hb_lock( h->state_lock );
1572 memcpy( s, &h->state, sizeof( hb_state_t ) );
1574 hb_unlock( h->state_lock );
1578 * Called in MacGui in UpdateUI to check
1579 * for a new scan being completed to set a new source
1581 int hb_get_scancount( hb_handle_t * h)
1583 return h->scanCount;
1587 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init.
1588 * @param _h Pointer to handle to hb_handle_t.
1590 void hb_close( hb_handle_t ** _h )
1592 hb_handle_t * h = *_h;
1597 hb_thread_close( &h->main_thread );
1599 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1601 hb_list_rem( h->list_title, title );
1602 if( title->job && title->job->filters )
1604 hb_list_close( &title->job->filters );
1607 hb_title_close( &title );
1609 hb_list_close( &h->list_title );
1611 hb_list_close( &h->jobs );
1612 hb_lock_close( &h->state_lock );
1613 hb_lock_close( &h->pause_lock );
1620 * Cleans up libhb at a process level. Call before the app closes. Removes preview directory.
1622 void hb_global_close()
1626 struct dirent * entry;
1628 /* Find and remove temp folder */
1629 memset( dirname, 0, 1024 );
1630 hb_get_temporary_directory( dirname );
1632 dir = opendir( dirname );
1635 while( ( entry = readdir( dir ) ) )
1637 char filename[1024];
1638 if( entry->d_name[0] == '.' )
1642 memset( filename, 0, 1024 );
1643 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
1652 * Monitors the state of the update, scan, and work threads.
1653 * Sets scan done state when scan thread exits.
1654 * Sets work done state when work thread exits.
1655 * @param _h Handle to hb_handle_t
1657 static void thread_func( void * _h )
1659 hb_handle_t * h = (hb_handle_t *) _h;
1664 /* Create folder for temporary files */
1665 memset( dirname, 0, 1024 );
1666 hb_get_temporary_directory( dirname );
1668 hb_mkdir( dirname );
1672 /* In case the check_update thread hangs, it'll die sooner or
1673 later. Then, we join it here */
1674 if( h->update_thread &&
1675 hb_thread_has_exited( h->update_thread ) )
1677 hb_thread_close( &h->update_thread );
1680 /* Check if the scan thread is done */
1681 if( h->scan_thread &&
1682 hb_thread_has_exited( h->scan_thread ) )
1684 hb_thread_close( &h->scan_thread );
1690 hb_remove_previews( h );
1691 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1693 hb_list_rem( h->list_title, title );
1694 hb_title_close( &title );
1697 hb_log( "hb_scan: canceled" );
1701 hb_log( "libhb: scan thread found %d valid title(s)",
1702 hb_list_count( h->list_title ) );
1704 hb_lock( h->state_lock );
1705 h->state.state = HB_STATE_SCANDONE; //originally state.state
1706 hb_unlock( h->state_lock );
1707 /*we increment this sessions scan count by one for the MacGui
1708 to trigger a new source being set */
1712 /* Check if the work thread is done */
1713 if( h->work_thread &&
1714 hb_thread_has_exited( h->work_thread ) )
1716 hb_thread_close( &h->work_thread );
1718 hb_log( "libhb: work result = %d",
1720 hb_lock( h->state_lock );
1721 h->state.state = HB_STATE_WORKDONE;
1722 h->state.param.workdone.error = h->work_error;
1724 h->job_count = hb_count(h);
1725 if (h->job_count < 1)
1726 h->job_count_permanent = 0;
1727 hb_unlock( h->state_lock );
1733 if( h->scan_thread )
1736 hb_thread_close( &h->scan_thread );
1738 if( h->work_thread )
1741 hb_thread_close( &h->work_thread );
1743 hb_remove_previews( h );
1747 * Redirects stderr to the registered callback
1749 * @param _data Unused.
1751 static void redirect_thread_func(void * _data)
1755 #if defined( SYS_MINGW )
1756 // dup2 doesn't work on windows for some stupid reason
1757 stderr->_file = pfd[1];
1759 dup2(pfd[1], /*stderr*/ 2);
1761 FILE * log_f = fdopen(pfd[0], "rb");
1763 char line_buffer[500];
1764 while(fgets(line_buffer, 500, log_f) != NULL)
1766 hb_log_callback(line_buffer);
1772 * @param h Handle to hb_handle_t
1774 int hb_get_pid( hb_handle_t * h )
1780 * Returns the id for the given instance.
1781 * @param h Handle to hb_handle_t
1782 * @returns The ID for the given instance
1784 int hb_get_instance_id( hb_handle_t * h )
1790 * Returns the title with the given title index.
1791 * @param h Handle to hb_handle_t
1792 * @param title_index the index of the title to get
1793 * @returns The requested title
1795 hb_title_t * hb_get_title_by_index( hb_handle_t * h, int title_index )
1799 int count = hb_list_count( h->list_title );
1800 for (i = 0; i < count; i++)
1802 title = hb_list_item( h->list_title, i );
1803 if (title->index == title_index)
1813 * Sets the current state.
1814 * @param h Handle to hb_handle_t
1815 * @param s Handle to new hb_state_t
1817 void hb_set_state( hb_handle_t * h, hb_state_t * s )
1819 hb_lock( h->pause_lock );
1820 hb_lock( h->state_lock );
1821 memcpy( &h->state, s, sizeof( hb_state_t ) );
1822 if( h->state.state == HB_STATE_WORKING ||
1823 h->state.state == HB_STATE_SEARCHING )
1826 if (h->job_count < 1)
1827 h->job_count_permanent = 1;
1829 h->state.param.working.job_cur =
1830 h->job_count_permanent - hb_list_count( h->jobs );
1831 h->state.param.working.job_count = h->job_count_permanent;
1833 // Set which job is being worked on
1835 h->state.param.working.sequence_id = h->current_job->sequence_id;
1837 h->state.param.working.sequence_id = 0;
1839 hb_unlock( h->state_lock );
1840 hb_unlock( h->pause_lock );
1843 /* Passes a pointer to persistent data */
1844 hb_interjob_t * hb_interjob_get( hb_handle_t * h )