3 #include "ffmpeg/avcodec.h"
4 #include "ffmpeg/swscale.h"
8 /* The "Check for update" thread */
11 hb_thread_t * update_thread;
13 /* This thread's only purpose is to check other threads'
16 hb_thread_t * main_thread;
19 /* DVD/file scan thread */
20 hb_list_t * list_title;
21 hb_thread_t * scan_thread;
23 /* The thread which processes the jobs. Others threads are launched
24 from this one (see work.c) */
26 hb_job_t * current_job;
28 int job_count_permanent;
29 volatile int work_die;
31 hb_thread_t * work_thread;
35 hb_lock_t * state_lock;
39 hb_lock_t * pause_lock;
40 /* For MacGui active queue
41 increments each time the scan thread completes*/
46 hb_work_object_t * hb_objects = NULL;
48 static void thread_func( void * );
51 * Registers work objects, by adding the work object to a liked list.
52 * @param w Handle to hb_work_object_t to register.
54 void hb_register( hb_work_object_t * w )
61 * libhb initialization routine.
62 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
63 * @param update_check signals libhb to check for updated version from HandBrake website.
64 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
66 hb_handle_t * hb_init_real( int verbose, int update_check )
68 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
71 /* See hb_log() in common.c */
72 if( verbose > HB_DEBUG_NONE )
74 putenv( "HB_DEBUG=1" );
75 av_log_set_level(AV_LOG_DEBUG);
78 /* Check for an update on the website if asked to */
83 hb_log( "hb_init: checking for updates" );
85 h->update_thread = hb_update_init( &h->build, h->version );
89 if( hb_thread_has_exited( h->update_thread ) )
91 /* Immediate success or failure */
92 hb_thread_close( &h->update_thread );
95 if( hb_get_date() > date + 1000 )
97 /* Still nothing after one second. Connection problem,
99 hb_log( "hb_init: connection problem, not waiting for "
108 * Initialise buffer pool
110 hb_buffer_pool_init();
112 /* CPU count detection */
113 hb_log( "hb_init: checking cpu count" );
114 h->cpu_count = hb_get_cpu_count();
116 h->list_title = hb_list_init();
117 h->jobs = hb_list_init();
119 h->state_lock = hb_lock_init();
120 h->state.state = HB_STATE_IDLE;
122 h->pause_lock = hb_lock_init();
126 avcodec_register_all();
127 av_register_codec_parser( &mpegaudio_parser);
129 /* Start library thread */
130 hb_log( "hb_init: starting libhb thread" );
132 h->main_thread = hb_thread_init( "libhb", thread_func, h,
133 HB_NORMAL_PRIORITY );
137 /* Set the scan count to start at 0 */
142 * libhb initialization routine.
143 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
144 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
145 * @param update_check signals libhb to check for updated version from HandBrake website.
146 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
148 hb_handle_t * hb_init_dl( int verbose, int update_check )
150 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
153 /* See hb_log() in common.c */
154 if( verbose > HB_DEBUG_NONE )
156 putenv( "HB_DEBUG=1" );
157 av_log_set_level(AV_LOG_DEBUG);
160 /* Check for an update on the website if asked to */
165 hb_log( "hb_init: checking for updates" );
166 date = hb_get_date();
167 h->update_thread = hb_update_init( &h->build, h->version );
171 if( hb_thread_has_exited( h->update_thread ) )
173 /* Immediate success or failure */
174 hb_thread_close( &h->update_thread );
177 if( hb_get_date() > date + 1000 )
179 /* Still nothing after one second. Connection problem,
180 let the thread die */
181 hb_log( "hb_init: connection problem, not waiting for "
189 /* CPU count detection */
190 hb_log( "hb_init: checking cpu count" );
191 h->cpu_count = hb_get_cpu_count();
193 h->list_title = hb_list_init();
194 h->jobs = hb_list_init();
195 h->current_job = NULL;
197 h->state_lock = hb_lock_init();
198 h->state.state = HB_STATE_IDLE;
200 h->pause_lock = hb_lock_init();
204 avcodec_register_all();
206 /* Start library thread */
207 hb_log( "hb_init: starting libhb thread" );
209 h->main_thread = hb_thread_init( "libhb", thread_func, h,
210 HB_NORMAL_PRIORITY );
212 hb_register( &hb_sync );
213 hb_register( &hb_decmpeg2 );
214 hb_register( &hb_decsub );
215 hb_register( &hb_render );
216 hb_register( &hb_encavcodec );
217 hb_register( &hb_encxvid );
218 hb_register( &hb_encx264 );
219 hb_register( &hb_deca52 );
220 hb_register( &hb_decdca );
221 hb_register( &hb_decavcodec );
222 hb_register( &hb_declpcm );
223 hb_register( &hb_encfaac );
224 hb_register( &hb_enclame );
225 hb_register( &hb_encvorbis );
232 * Returns current version of libhb.
233 * @param h Handle to hb_handle_t.
234 * @return character array of version number.
236 char * hb_get_version( hb_handle_t * h )
242 * Returns current build of libhb.
243 * @param h Handle to hb_handle_t.
244 * @return character array of build number.
246 int hb_get_build( hb_handle_t * h )
252 * Checks for needed update.
253 * @param h Handle to hb_handle_t.
254 * @param version Pointer to handle where version will be copied.
255 * @return update indicator.
257 int hb_check_update( hb_handle_t * h, char ** version )
259 *version = ( h->build < 0 ) ? NULL : h->version;
264 * Sets the cpu count to the desired value.
265 * @param h Handle to hb_handle_t
266 * @param cpu_count Number of CPUs to use.
268 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
270 cpu_count = MAX( 1, cpu_count );
271 cpu_count = MIN( cpu_count, 8 );
272 h->cpu_count = cpu_count;
276 * Initializes a scan of the by calling hb_scan_init
277 * @param h Handle to hb_handle_t
278 * @param path location of VIDEO_TS folder.
279 * @param title_index Desired title to scan. 0 for all titles.
281 void hb_scan( hb_handle_t * h, const char * path, int title_index )
285 /* Clean up from previous scan */
286 while( ( title = hb_list_item( h->list_title, 0 ) ) )
288 hb_list_rem( h->list_title, title );
289 hb_title_close( &title );
292 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
293 h->scan_thread = hb_scan_init( h, path, title_index, h->list_title );
297 * Returns the list of titles found.
298 * @param h Handle to hb_handle_t
299 * @return Handle to hb_list_t of the title list.
301 hb_list_t * hb_get_titles( hb_handle_t * h )
303 return h->list_title;
307 * Create preview image of desired title a index of picture.
308 * @param h Handle to hb_handle_t.
309 * @param title Handle to hb_title_t of desired title.
310 * @param picture Index in title.
311 * @param buffer Handle to buufer were inage will be drawn.
313 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
316 hb_job_t * job = title->job;
319 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
321 AVPicture pic_in, pic_preview, pic_deint, pic_crop, pic_scale;
322 struct SwsContext * context;
325 buf1 = malloc( title->width * title->height * 3 / 2 );
326 buf2 = malloc( title->width * title->height * 3 / 2 );
327 buf3 = malloc( title->width * title->height * 3 / 2 );
328 buf4 = malloc( title->width * title->height * 4 );
329 avpicture_fill( &pic_in, buf1, PIX_FMT_YUV420P,
330 title->width, title->height );
331 avpicture_fill( &pic_deint, buf2, PIX_FMT_YUV420P,
332 title->width, title->height );
333 avpicture_fill( &pic_scale, buf3, PIX_FMT_YUV420P,
334 job->width, job->height );
335 avpicture_fill( &pic_preview, buf4, PIX_FMT_RGBA32,
336 job->width, job->height );
338 // Allocate the AVPicture frames and fill in
340 memset( filename, 0, 1024 );
342 hb_get_tempory_filename( h, filename, "%x%d",
343 (intptr_t) title, picture );
345 file = fopen( filename, "r" );
348 hb_log( "hb_get_preview: fopen failed" );
352 fread( buf1, title->width * title->height * 3 / 2, 1, file );
355 if( job->deinterlace )
357 // Deinterlace and crop
358 avpicture_deinterlace( &pic_deint, &pic_in, PIX_FMT_YUV420P, title->width, title->height );
359 av_picture_crop( &pic_crop, &pic_deint, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
364 av_picture_crop( &pic_crop, &pic_in, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
367 // Get scaling context
368 context = sws_getContext(title->width - (job->crop[2] + job->crop[3]),
369 title->height - (job->crop[0] + job->crop[1]),
371 job->width, job->height, PIX_FMT_YUV420P,
372 (uint16_t)(SWS_LANCZOS|SWS_ACCURATE_RND), NULL, NULL, NULL);
376 pic_crop.data, pic_crop.linesize,
377 0, title->height - (job->crop[0] + job->crop[1]),
378 pic_scale.data, pic_scale.linesize);
381 sws_freeContext( context );
383 // Get preview context
384 context = sws_getContext(job->width, job->height, PIX_FMT_YUV420P,
385 job->width, job->height, PIX_FMT_RGBA32,
386 (uint16_t)(SWS_LANCZOS|SWS_ACCURATE_RND), NULL, NULL, NULL);
390 pic_scale.data, pic_scale.linesize,
392 pic_preview.data, pic_preview.linesize);
395 sws_freeContext( context );
397 /* Gray background */
398 p32 = (uint32_t *) buffer;
399 for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
404 /* Draw the picture, centered, and draw the cropping zone */
405 pen = buffer + ( title->height - job->height ) *
406 ( title->width + 2 ) * 2 + ( title->width - job->width ) * 2;
407 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
408 pen += 4 * ( title->width + 2 );
409 for( i = 0; i < job->height; i++ )
412 nextLine = pen + 4 * ( title->width + 2 );
413 memset( pen, 0xFF, 4 );
415 memcpy( pen, buf4 + 4 * job->width * i, 4 * job->width );
416 pen += 4 * job->width;
417 memset( pen, 0xFF, 4 );
420 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
423 avpicture_free( &pic_preview );
424 avpicture_free( &pic_scale );
425 avpicture_free( &pic_deint );
426 avpicture_free( &pic_in );
430 * Calculates job width, height, and cropping parameters.
431 * @param job Handle to hb_job_t.
432 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
433 * @param pixels Maximum desired pixel count.
434 * @param par If true, skip the cropping and keep-aspect-ratio stuff.
436 void hb_set_size( hb_job_t * job, int aspect, int pixels, int par )
438 hb_title_t * title = job->title;
440 int croppedWidth = title->width - title->crop[2] - title->crop[3];
441 int croppedHeight = title->height - title->crop[0] - title->crop[1];
446 /* That means we're setting size for video with non-square pixels */
447 croppedAspect = croppedWidth * HB_ASPECT_BASE / croppedHeight;
448 aspect = croppedAspect;
452 /* We're free to change cropping and maintain display aspect ratio */
453 croppedAspect = title->aspect * title->height * croppedWidth /
454 croppedHeight / title->width;
460 /* Keep the best possible aspect ratio */
461 aspect = croppedAspect;
464 /* Crop if necessary to obtain the desired ratio */
465 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
466 if( aspect < croppedAspect )
468 /* Need to crop on the left and right */
469 addCrop = croppedWidth - aspect * croppedHeight * title->width /
470 title->aspect / title->height;
473 addCrop = ( addCrop + 1 ) / 2;
474 job->crop[2] += addCrop;
475 job->crop[3] += addCrop;
477 else if( addCrop & 2 )
480 job->crop[2] += addCrop - 1;
481 job->crop[3] += addCrop + 1;
486 job->crop[2] += addCrop;
487 job->crop[3] += addCrop;
490 else if( aspect > croppedAspect )
492 /* Need to crop on the top and bottom */
493 addCrop = croppedHeight - croppedWidth * title->aspect *
494 title->height / aspect / title->width;
497 addCrop = ( addCrop + 1 ) / 2;
498 job->crop[0] += addCrop;
499 job->crop[1] += addCrop;
501 else if( addCrop & 2 )
504 job->crop[0] += addCrop - 1;
505 job->crop[1] += addCrop + 1;
510 job->crop[0] += addCrop;
511 job->crop[1] += addCrop;
518 /* Compute a resolution from the number of pixels and aspect */
522 h = MULTIPLE_16( w * HB_ASPECT_BASE / aspect );
530 job->height = MULTIPLE_16( 16 * i * HB_ASPECT_BASE / aspect );
534 * Returns the number of jobs in the queue.
535 * @param h Handle to hb_handle_t.
536 * @return Number of jobs.
538 int hb_count( hb_handle_t * h )
540 return hb_list_count( h->jobs );
544 * Returns handle to job at index i within the job list.
545 * @param h Handle to hb_handle_t.
546 * @param i Index of job.
547 * @returns Handle to hb_job_t of desired job.
549 hb_job_t * hb_job( hb_handle_t * h, int i )
551 return hb_list_item( h->jobs, i );
554 hb_job_t * hb_current_job( hb_handle_t * h )
556 return( h->current_job );
560 * Adds a job to the job list.
561 * @param h Handle to hb_handle_t.
562 * @param job Handle to hb_job_t.
564 void hb_add( hb_handle_t * h, hb_job_t * job )
567 hb_title_t * title, * title_copy;
568 hb_chapter_t * chapter, * chapter_copy;
569 hb_audio_t * audio, * audio_copy;
570 hb_subtitle_t * subtitle, * subtitle_copy;
576 title_copy = malloc( sizeof( hb_title_t ) );
577 memcpy( title_copy, title, sizeof( hb_title_t ) );
579 title_copy->list_chapter = hb_list_init();
580 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
582 chapter = hb_list_item( title->list_chapter, i );
583 chapter_copy = malloc( sizeof( hb_chapter_t ) );
584 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
585 hb_list_add( title_copy->list_chapter, chapter_copy );
588 /* Copy the audio track(s) we want */
589 title_copy->list_audio = hb_list_init();
591 /* Do nothing about audio during first pass */
592 if( job->pass == 0 || job->pass == 2 )
594 for( i = 0; i < 8; i++ )
596 if( job->audios[i] < 0 )
600 if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
602 audio_copy = malloc( sizeof( hb_audio_t ) );
603 memcpy( audio_copy, audio, sizeof( hb_audio_t ) );
604 hb_list_add( title_copy->list_audio, audio_copy );
609 title_copy->list_subtitle = hb_list_init();
612 * The following code is confusing, there are three ways in which
613 * we select subtitles and it depends on whether this is single or
616 * subtitle_scan may be enabled, in which case the first pass
617 * scans all subtitles of that language. The second pass does not
618 * select any because they are set at the end of the first pass.
620 * native_language may have a preferred language, in which case we
621 * may be switching the language we want for the subtitles in the
622 * first pass of a single pass, or the second pass of a two pass.
624 * We may have manually selected a subtitle, in which case that is
625 * selected in the first pass of a single pass, or the second of a
628 memset( audio_lang, 0, sizeof( audio_lang ) );
630 if ( job->indepth_scan || job->native_language ) {
633 * Find the first audio language that is being encoded
635 for( i = 0; i < 8; i++ )
637 if( job->audios[i] < 0 )
641 if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
643 strncpy(audio_lang, audio->iso639_2, sizeof(audio_lang));
649 * In all cases switch the language if we need to to our native
652 if( job->native_language )
654 if( strncasecmp( job->native_language, audio_lang,
655 sizeof( audio_lang ) ) != 0 )
660 hb_log( "Enabled subtitles in native language '%s', audio is in '%s'",
661 job->native_language, audio_lang);
664 * The main audio track is not in our native language, so switch
665 * the subtitles to use our native language instead.
667 strncpy( audio_lang, job->native_language, sizeof( audio_lang ) );
670 * native language is irrelevent, free it.
672 free( job->native_language );
673 job->native_language = NULL;
679 * If doing a subtitle scan then add all the matching subtitles for this
682 if ( job->indepth_scan )
684 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
686 subtitle = hb_list_item( title->list_subtitle, i );
687 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
690 * Matched subtitle language with audio language, so
691 * add this to our list to scan.
693 * We will update the subtitle list on the second pass
694 * later after the first pass has completed.
696 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
697 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
698 hb_list_add( title_copy->list_subtitle, subtitle_copy );
699 if ( job->native_language ) {
701 * With native language just select the
702 * first match in our langiage, not all of
703 * them. Subsequent ones are likely to be commentary
711 * Not doing a subtitle scan in this pass, but maybe we are in the
714 if( job->select_subtitle )
717 * Don't add subtitles here, we'll add them via select_subtitle
718 * at the end of the subtitle_scan.
722 * Definitely not doing a subtitle scan.
724 if( job->pass != 1 && job->native_language )
727 * We are not doing a subtitle scan but do want the
728 * native langauge subtitle selected, so select it
729 * for pass 0 or pass 2 of a two pass.
731 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
733 subtitle = hb_list_item( title->list_subtitle, i );
734 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
737 * Matched subtitle language with audio language, so
738 * add this to our list to scan.
740 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
741 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
742 hb_list_add( title_copy->list_subtitle, subtitle_copy );
748 * Manually selected subtitle, in which case only
749 * bother adding them for pass 0 or pass 2 of a two
754 if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) )
756 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
757 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
758 hb_list_add( title_copy->list_subtitle, subtitle_copy );
766 job_copy = calloc( sizeof( hb_job_t ), 1 );
767 memcpy( job_copy, job, sizeof( hb_job_t ) );
768 title_copy->job = job_copy;
769 job_copy->title = title_copy;
770 job_copy->file = strdup( job->file );
772 job_copy->pause = h->pause_lock;
774 /* Copy the job filter list */
778 int filter_count = hb_list_count( job->filters );
779 job_copy->filters = hb_list_init();
780 for( i = 0; i < filter_count; i++ )
782 hb_filter_object_t * filter = hb_list_item( job->filters, i );
783 hb_list_add( job_copy->filters, filter );
787 /* Add the job to the list */
788 hb_list_add( h->jobs, job_copy );
789 h->job_count = hb_count(h);
790 h->job_count_permanent++;
794 * Removes a job from the job list.
795 * @param h Handle to hb_handle_t.
796 * @param job Handle to hb_job_t.
798 void hb_rem( hb_handle_t * h, hb_job_t * job )
800 hb_list_rem( h->jobs, job );
802 h->job_count = hb_count(h);
803 if (h->job_count_permanent)
804 h->job_count_permanent--;
806 /* XXX free everything XXX */
810 * Starts the conversion process.
811 * Sets state to HB_STATE_WORKING.
812 * calls hb_work_init, to launch work thread. Stores handle to work thread.
813 * @param h Handle to hb_handle_t.
815 void hb_start( hb_handle_t * h )
818 h->job_count = hb_list_count( h->jobs );
819 h->job_count_permanent = h->job_count;
821 hb_lock( h->state_lock );
822 h->state.state = HB_STATE_WORKING;
823 #define p h->state.param.working
826 p.job_count = h->job_count;
833 hb_unlock( h->state_lock );
838 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
839 &h->work_die, &h->work_error, &h->current_job );
843 * Pauses the conversion process.
844 * @param h Handle to hb_handle_t.
846 void hb_pause( hb_handle_t * h )
850 hb_lock( h->pause_lock );
853 hb_lock( h->state_lock );
854 h->state.state = HB_STATE_PAUSED;
855 hb_unlock( h->state_lock );
860 * Resumes the conversion process.
861 * @param h Handle to hb_handle_t.
863 void hb_resume( hb_handle_t * h )
867 hb_unlock( h->pause_lock );
873 * Stops the conversion process.
874 * @param h Handle to hb_handle_t.
876 void hb_stop( hb_handle_t * h )
880 h->job_count = hb_count(h);
881 h->job_count_permanent = 0;
887 * Returns the state of the conversion process.
888 * @param h Handle to hb_handle_t.
889 * @param s Handle to hb_state_t which to copy the state data.
891 void hb_get_state( hb_handle_t * h, hb_state_t * s )
893 hb_lock( h->state_lock );
895 memcpy( s, &h->state, sizeof( hb_state_t ) );
896 if ( h->state.state == HB_STATE_SCANDONE || h->state.state == HB_STATE_WORKDONE )
897 h->state.state = HB_STATE_IDLE;
899 hb_unlock( h->state_lock );
902 void hb_get_state2( hb_handle_t * h, hb_state_t * s )
904 hb_lock( h->state_lock );
906 memcpy( s, &h->state, sizeof( hb_state_t ) );
908 hb_unlock( h->state_lock );
912 * Called in MacGui in UpdateUI to check
913 * for a new scan being completed to set a new source
915 int hb_get_scancount( hb_handle_t * h)
921 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init_real.
922 * @param _h Pointer to handle to hb_handle_t.
924 void hb_close( hb_handle_t ** _h )
926 hb_handle_t * h = *_h;
930 hb_thread_close( &h->main_thread );
932 while( ( title = hb_list_item( h->list_title, 0 ) ) )
934 hb_list_rem( h->list_title, title );
935 if( title->job && title->job->filters )
937 hb_list_close( &title->job->filters );
940 hb_title_close( &title );
942 hb_list_close( &h->list_title );
944 hb_list_close( &h->jobs );
945 hb_lock_close( &h->state_lock );
946 hb_lock_close( &h->pause_lock );
953 * Monitors the state of the update, scan, and work threads.
954 * Sets scan done state when scan thread exits.
955 * Sets work done state when work thread exits.
956 * @param _h Handle to hb_handle_t
958 static void thread_func( void * _h )
960 hb_handle_t * h = (hb_handle_t *) _h;
963 struct dirent * entry;
967 /* Create folder for temporary files */
968 memset( dirname, 0, 1024 );
969 hb_get_tempory_directory( h, dirname );
975 /* In case the check_update thread hangs, it'll die sooner or
976 later. Then, we join it here */
977 if( h->update_thread &&
978 hb_thread_has_exited( h->update_thread ) )
980 hb_thread_close( &h->update_thread );
983 /* Check if the scan thread is done */
984 if( h->scan_thread &&
985 hb_thread_has_exited( h->scan_thread ) )
987 hb_thread_close( &h->scan_thread );
989 hb_log( "libhb: scan thread found %d valid title(s)",
990 hb_list_count( h->list_title ) );
991 hb_lock( h->state_lock );
992 h->state.state = HB_STATE_SCANDONE; //originally state.state
993 hb_unlock( h->state_lock );
994 /*we increment this sessions scan count by one for the MacGui
995 to trigger a new source being set */
999 /* Check if the work thread is done */
1000 if( h->work_thread &&
1001 hb_thread_has_exited( h->work_thread ) )
1003 hb_thread_close( &h->work_thread );
1005 hb_log( "libhb: work result = %d",
1007 hb_lock( h->state_lock );
1008 h->state.state = HB_STATE_WORKDONE;
1009 h->state.param.workdone.error = h->work_error;
1011 h->job_count = hb_count(h);
1012 if (h->job_count < 1)
1013 h->job_count_permanent = 0;
1014 hb_unlock( h->state_lock );
1020 if( h->work_thread )
1023 hb_thread_close( &h->work_thread );
1026 /* Remove temp folder */
1027 dir = opendir( dirname );
1028 while( ( entry = readdir( dir ) ) )
1030 char filename[1024];
1031 if( entry->d_name[0] == '.' )
1035 memset( filename, 0, 1024 );
1036 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
1045 * @param h Handle to hb_handle_t
1047 int hb_get_pid( hb_handle_t * h )
1053 * Sets the current state.
1054 * @param h Handle to hb_handle_t
1055 * @param s Handle to new hb_state_t
1057 void hb_set_state( hb_handle_t * h, hb_state_t * s )
1059 hb_lock( h->pause_lock );
1060 hb_lock( h->state_lock );
1061 memcpy( &h->state, s, sizeof( hb_state_t ) );
1062 if( h->state.state == HB_STATE_WORKING )
1065 if (h->job_count < 1)
1066 h->job_count_permanent = 1;
1068 h->state.param.working.job_cur =
1069 h->job_count_permanent - hb_list_count( h->jobs );
1070 h->state.param.working.job_count = h->job_count_permanent;
1072 hb_unlock( h->state_lock );
1073 hb_unlock( h->pause_lock );