3 #include "libavcodec/avcodec.h"
4 #include "libavformat/avformat.h"
5 #include "libswscale/swscale.h"
9 /* The "Check for update" thread */
12 hb_thread_t * update_thread;
14 /* This thread's only purpose is to check other threads'
17 hb_thread_t * main_thread;
20 /* DVD/file scan thread */
21 hb_list_t * list_title;
22 hb_thread_t * scan_thread;
24 /* The thread which processes the jobs. Others threads are launched
25 from this one (see work.c) */
27 hb_job_t * current_job;
29 int job_count_permanent;
30 volatile int work_die;
32 hb_thread_t * work_thread;
36 hb_lock_t * state_lock;
40 hb_lock_t * pause_lock;
41 /* For MacGui active queue
42 increments each time the scan thread completes*/
47 hb_work_object_t * hb_objects = NULL;
49 static void thread_func( void * );
52 * Registers work objects, by adding the work object to a liked list.
53 * @param w Handle to hb_work_object_t to register.
55 void hb_register( hb_work_object_t * w )
62 * libhb initialization routine.
63 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
64 * @param update_check signals libhb to check for updated version from HandBrake website.
65 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
67 hb_handle_t * hb_init_real( int verbose, int update_check )
69 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
72 /* See hb_deep_log() and hb_log() in common.c */
73 global_verbosity_level = verbose;
75 putenv( "HB_DEBUG=1" );
77 /* Check for an update on the website if asked to */
82 hb_log( "hb_init: checking for updates" );
84 h->update_thread = hb_update_init( &h->build, h->version );
88 if( hb_thread_has_exited( h->update_thread ) )
90 /* Immediate success or failure */
91 hb_thread_close( &h->update_thread );
94 if( hb_get_date() > date + 1000 )
96 /* Still nothing after one second. Connection problem,
98 hb_log( "hb_init: connection problem, not waiting for "
107 * Initialise buffer pool
109 hb_buffer_pool_init();
111 /* CPU count detection */
112 hb_log( "hb_init: checking cpu count" );
113 h->cpu_count = hb_get_cpu_count();
115 h->list_title = hb_list_init();
116 h->jobs = hb_list_init();
118 h->state_lock = hb_lock_init();
119 h->state.state = HB_STATE_IDLE;
121 h->pause_lock = hb_lock_init();
126 /* Start library thread */
127 hb_log( "hb_init: starting libhb thread" );
129 h->main_thread = hb_thread_init( "libhb", thread_func, h,
130 HB_NORMAL_PRIORITY );
134 /* Set the scan count to start at 0 */
139 * libhb initialization routine.
140 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
141 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
142 * @param update_check signals libhb to check for updated version from HandBrake website.
143 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
145 hb_handle_t * hb_init_dl( int verbose, int update_check )
147 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
150 /* See hb_log() in common.c */
151 if( verbose > HB_DEBUG_NONE )
153 putenv( "HB_DEBUG=1" );
156 /* Check for an update on the website if asked to */
161 hb_log( "hb_init: checking for updates" );
162 date = hb_get_date();
163 h->update_thread = hb_update_init( &h->build, h->version );
167 if( hb_thread_has_exited( h->update_thread ) )
169 /* Immediate success or failure */
170 hb_thread_close( &h->update_thread );
173 if( hb_get_date() > date + 1000 )
175 /* Still nothing after one second. Connection problem,
176 let the thread die */
177 hb_log( "hb_init: connection problem, not waiting for "
185 /* CPU count detection */
186 hb_log( "hb_init: checking cpu count" );
187 h->cpu_count = hb_get_cpu_count();
189 h->list_title = hb_list_init();
190 h->jobs = hb_list_init();
191 h->current_job = NULL;
193 h->state_lock = hb_lock_init();
194 h->state.state = HB_STATE_IDLE;
196 h->pause_lock = hb_lock_init();
200 avcodec_register_all();
202 /* Start library thread */
203 hb_log( "hb_init: starting libhb thread" );
205 h->main_thread = hb_thread_init( "libhb", thread_func, h,
206 HB_NORMAL_PRIORITY );
208 hb_register( &hb_sync );
209 hb_register( &hb_decmpeg2 );
210 hb_register( &hb_decsub );
211 hb_register( &hb_render );
212 hb_register( &hb_encavcodec );
213 hb_register( &hb_encxvid );
214 hb_register( &hb_encx264 );
215 hb_register( &hb_enctheora );
216 hb_register( &hb_deca52 );
217 hb_register( &hb_decdca );
218 hb_register( &hb_decavcodec );
219 hb_register( &hb_decavcodecv );
220 hb_register( &hb_decavcodecvi );
221 hb_register( &hb_decavcodecai );
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;
320 uint32_t * p32, swsflags;
321 AVPicture pic_in, pic_preview, pic_deint, pic_crop, pic_scale;
322 struct SwsContext * context;
324 int rgb_width = ((job->width + 7) >> 3) << 3;
327 swsflags = SWS_LANCZOS;
329 swsflags |= SWS_ACCURATE_RND;
330 #endif /* __x86_64__ */
332 buf1 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
333 buf2 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
334 buf3 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, job->width, job->height ) );
335 buf4 = av_malloc( avpicture_get_size( PIX_FMT_RGBA32, rgb_width, job->height ) );
336 avpicture_fill( &pic_in, buf1, PIX_FMT_YUV420P,
337 title->width, title->height );
338 avpicture_fill( &pic_deint, buf2, PIX_FMT_YUV420P,
339 title->width, title->height );
340 avpicture_fill( &pic_scale, buf3, PIX_FMT_YUV420P,
341 job->width, job->height );
342 avpicture_fill( &pic_preview, buf4, PIX_FMT_RGBA32,
343 rgb_width, job->height );
345 // Allocate the AVPicture frames and fill in
347 memset( filename, 0, 1024 );
349 hb_get_tempory_filename( h, filename, "%x%d",
350 (intptr_t) title, picture );
352 file = fopen( filename, "r" );
355 hb_log( "hb_get_preview: fopen failed" );
359 fread( buf1, avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height), 1, file );
362 if( job->deinterlace )
364 // Deinterlace and crop
365 avpicture_deinterlace( &pic_deint, &pic_in, PIX_FMT_YUV420P, title->width, title->height );
366 av_picture_crop( &pic_crop, &pic_deint, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
371 av_picture_crop( &pic_crop, &pic_in, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
374 // Get scaling context
375 context = sws_getContext(title->width - (job->crop[2] + job->crop[3]),
376 title->height - (job->crop[0] + job->crop[1]),
378 job->width, job->height, PIX_FMT_YUV420P,
379 swsflags, NULL, NULL, NULL);
383 pic_crop.data, pic_crop.linesize,
384 0, title->height - (job->crop[0] + job->crop[1]),
385 pic_scale.data, pic_scale.linesize);
388 sws_freeContext( context );
390 // Get preview context
391 context = sws_getContext(rgb_width, job->height, PIX_FMT_YUV420P,
392 rgb_width, job->height, PIX_FMT_RGBA32,
393 swsflags, NULL, NULL, NULL);
397 pic_scale.data, pic_scale.linesize,
399 pic_preview.data, pic_preview.linesize);
402 sws_freeContext( context );
404 /* Gray background */
405 p32 = (uint32_t *) buffer;
406 for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
411 /* Draw the picture, centered, and draw the cropping zone */
412 preview_size = pic_preview.linesize[0];
413 pen = buffer + ( title->height - job->height ) *
414 ( title->width + 2 ) * 2 + ( title->width - job->width ) * 2;
415 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
416 pen += 4 * ( title->width + 2 );
417 for( i = 0; i < job->height; i++ )
420 nextLine = pen + 4 * ( title->width + 2 );
421 memset( pen, 0xFF, 4 );
423 memcpy( pen, buf4 + preview_size * i, 4 * job->width );
424 pen += 4 * job->width;
425 memset( pen, 0xFF, 4 );
428 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
431 avpicture_free( &pic_preview );
432 avpicture_free( &pic_scale );
433 avpicture_free( &pic_deint );
434 avpicture_free( &pic_in );
438 * Analyzes a frame to detect interlacing artifacts
439 * and returns true if interlacing (combing) is found.
441 * Code taken from Thomas Oestreich's 32detect filter
442 * in the Transcode project, with minor formatting changes.
444 * @param buf An hb_buffer structure holding valid frame data
445 * @param width The frame's width in pixels
446 * @param height The frame's height in pixels
447 * @param color_equal Sensitivity for detecting similar colors
448 * @param color_diff Sensitivity for detecting different colors
449 * @param threshold Sensitivity for flagging planes as combed
450 * @param prog_equal Sensitivity for detecting similar colors on progressive frames
451 * @param prog_diff Sensitivity for detecting different colors on progressive frames
452 * @param prog_threshold Sensitivity for flagging progressive frames as combed
454 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 )
456 int j, k, n, off, cc_1, cc_2, cc[3], flag[3] ;
457 uint16_t s1, s2, s3, s4;
462 if ( buf->flags & 16 )
464 /* Frame is progressive, be more discerning. */
465 color_diff = prog_diff;
466 color_equal = prog_equal;
467 threshold = prog_threshold;
470 /* One pas for Y, one pass for Cb, one pass for Cr */
471 for( k = 0; k < 3; k++ )
475 /* Y has already been checked, now offset by Y's dimensions
476 and divide all the other values by 2, since Cr and Cb
477 are half-size compared to Y. */
478 offset = width * height;
484 /* Y and Cb are done, so the offset needs to be bumped
485 so it's width*height + (width / 2) * (height / 2) */
489 for( j = 0; j < width; ++j )
493 for( n = 0; n < ( height - 4 ); n = n + 2 )
495 /* Look at groups of 4 sequential horizontal lines */
496 s1 = ( ( buf->data + offset )[ off + j ] & 0xff );
497 s2 = ( ( buf->data + offset )[ off + j + width ] & 0xff );
498 s3 = ( ( buf->data + offset )[ off + j + 2 * width ] & 0xff );
499 s4 = ( ( buf->data + offset )[ off + j + 3 * width ] & 0xff );
501 /* Note if the 1st and 2nd lines are more different in
502 color than the 1st and 3rd lines are similar in color.*/
503 if ( ( abs( s1 - s3 ) < color_equal ) &&
504 ( abs( s1 - s2 ) > color_diff ) )
507 /* Note if the 2nd and 3rd lines are more different in
508 color than the 2nd and 4th lines are similar in color.*/
509 if ( ( abs( s2 - s4 ) < color_equal ) &&
510 ( abs( s2 - s3 ) > color_diff) )
513 /* Now move down 2 horizontal lines before starting over.*/
519 /* The final cc score for a plane is the percentage of combed pixels it contains.
520 Because sensitivity goes down to hundreths of a percent, multiply by 1000
521 so it will be easy to compare against the threhold value which is an integer. */
522 cc[k] = (int)( ( cc_1 + cc_2 ) * 1000.0 / ( width * height ) );
526 /* HandBrake is all yuv420, so weight the average percentage of all 3 planes accordingly.*/
527 int average_cc = ( 2 * cc[0] + ( cc[1] / 2 ) + ( cc[2] / 2 ) ) / 3;
529 /* Now see if that average percentage of combed pixels surpasses the threshold percentage given by the user.*/
530 if( average_cc > threshold )
533 hb_log("Average %i combed (Threshold %i) %i/%i/%i | PTS: %lld (%fs) %s", average_cc, threshold, cc[0], cc[1], cc[2], buf->start, (float)buf->start / 90000, (buf->flags & 16) ? "Film" : "Video" );
539 hb_log("SKIPPED Average %i combed (Threshold %i) %i/%i/%i | PTS: %lld (%fs) %s", average_cc, threshold, cc[0], cc[1], cc[2], buf->start, (float)buf->start / 90000, (buf->flags & 16) ? "Film" : "Video" );
542 /* Reaching this point means no combing detected. */
548 * Calculates job width and height for anamorphic content,
550 * @param job Handle to hb_job_t
551 * @param output_width Pointer to returned storage width
552 * @param output_height Pointer to returned storage height
553 * @param output_par_width Pointer to returned pixel width
554 @ param output_par_height Pointer to returned pixel height
556 void hb_set_anamorphic_size( hb_job_t * job,
557 int *output_width, int *output_height,
558 int *output_par_width, int *output_par_height )
560 /* "Loose" anamorphic.
561 - Uses mod16-compliant dimensions,
562 - Allows users to set the width
563 - Handles ITU pixel aspects
566 /* Set up some variables to make the math easier to follow. */
567 hb_title_t * title = job->title;
568 int cropped_width = title->width - job->crop[2] - job->crop[3] ;
569 int cropped_height = title->height - job->crop[0] - job->crop[1] ;
570 double storage_aspect = (double)cropped_width / (double)cropped_height;
571 int width = job->width;
572 int height; // Gets set later, ignore user job->height value
573 int mod = job->modulus;
574 double aspect = title->aspect;
576 /* Gotta handle bounding dimensions differently
577 than for non-anamorphic encodes:
578 If the width is too big, just reset it with no rescaling.
579 Instead of using the aspect-scaled job height,
580 we need to see if the job width divided by the storage aspect
581 is bigger than the max. If so, set it to the max (this is sloppy).
582 If not, set job height to job width divided by storage aspect.
585 if ( job->maxWidth && (job->maxWidth < job->width) )
586 width = job->maxWidth;
588 height = (double)width / storage_aspect;
589 if ( job->maxHeight && (job->maxHeight < height) )
590 height = job->maxHeight;
592 /* In case the user specified a modulus, use it */
598 /* Time to get picture dimensions that divide cleanly.*/
599 width = MULTIPLE_MOD( width, mod);
600 height = MULTIPLE_MOD( height, mod);
602 /* Verify these new dimensions don't violate max height and width settings */
603 if ( job->maxWidth && (job->maxWidth < job->width) )
604 width = job->maxWidth;
605 if ( job->maxHeight && (job->maxHeight < height) )
606 height = job->maxHeight;
608 int pixel_aspect_width = job->pixel_aspect_width;
609 int pixel_aspect_height = job->pixel_aspect_height;
611 /* If a source was really 704*480 and hard matted with cropping
612 to 720*480, replace the PAR values with the ITU broadcast ones. */
613 if (title->width == 720 && cropped_width <= 706)
615 // convert aspect to a scaled integer so we can test for 16:9 & 4:3
616 // aspect ratios ignoring insignificant differences in the LSBs of
617 // the floating point representation.
618 int iaspect = aspect * 9.;
620 /* Handle ITU PARs */
621 if (title->height == 480)
626 /* It's widescreen */
627 pixel_aspect_width = 40;
628 pixel_aspect_height = 33;
630 else if (iaspect == 12)
633 pixel_aspect_width = 10;
634 pixel_aspect_height = 11;
637 else if (title->height == 576)
642 /* It's widescreen */
643 pixel_aspect_width = 16;
644 pixel_aspect_height = 11;
646 else if (iaspect == 12)
649 pixel_aspect_width = 12;
650 pixel_aspect_height = 11;
655 /* Figure out what dimensions the source would display at. */
656 int source_display_width = cropped_width * (double)pixel_aspect_width /
657 (double)pixel_aspect_height ;
659 /* The film AR is the source's display width / cropped source height.
660 The output display width is the output height * film AR.
661 The output PAR is the output display width / output storage width. */
662 pixel_aspect_width = height * source_display_width / cropped_height;
663 pixel_aspect_height = width;
665 /* Pass the results back to the caller */
666 *output_width = width;
667 *output_height = height;
669 /* While x264 is smart enough to reduce fractions on its own, libavcodec
670 needs some help with the math, so lose superfluous factors. */
671 hb_reduce( output_par_width, output_par_height,
672 pixel_aspect_width, pixel_aspect_height );
676 * Calculates job width, height, and cropping parameters.
677 * @param job Handle to hb_job_t.
678 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
679 * @param pixels Maximum desired pixel count.
681 void hb_set_size( hb_job_t * job, double aspect, int pixels )
683 hb_title_t * title = job->title;
685 int croppedWidth = title->width - title->crop[2] - title->crop[3];
686 int croppedHeight = title->height - title->crop[0] - title->crop[1];
687 double croppedAspect = title->aspect * title->height * croppedWidth /
688 croppedHeight / title->width;
694 /* Keep the best possible aspect ratio */
695 aspect = croppedAspect;
698 /* Crop if necessary to obtain the desired ratio */
699 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
700 if( aspect < croppedAspect )
702 /* Need to crop on the left and right */
703 addCrop = croppedWidth - aspect * croppedHeight * title->width /
704 title->aspect / title->height;
707 addCrop = ( addCrop + 1 ) / 2;
708 job->crop[2] += addCrop;
709 job->crop[3] += addCrop;
711 else if( addCrop & 2 )
714 job->crop[2] += addCrop - 1;
715 job->crop[3] += addCrop + 1;
720 job->crop[2] += addCrop;
721 job->crop[3] += addCrop;
724 else if( aspect > croppedAspect )
726 /* Need to crop on the top and bottom */
727 addCrop = croppedHeight - croppedWidth * title->aspect *
728 title->height / aspect / title->width;
731 addCrop = ( addCrop + 1 ) / 2;
732 job->crop[0] += addCrop;
733 job->crop[1] += addCrop;
735 else if( addCrop & 2 )
738 job->crop[0] += addCrop - 1;
739 job->crop[1] += addCrop + 1;
744 job->crop[0] += addCrop;
745 job->crop[1] += addCrop;
749 /* Compute a resolution from the number of pixels and aspect */
753 h = MULTIPLE_16( (int)( (double)w / aspect ) );
761 job->height = MULTIPLE_16( (int)( (double)job->width / aspect ) );
765 * Returns the number of jobs in the queue.
766 * @param h Handle to hb_handle_t.
767 * @return Number of jobs.
769 int hb_count( hb_handle_t * h )
771 return hb_list_count( h->jobs );
775 * Returns handle to job at index i within the job list.
776 * @param h Handle to hb_handle_t.
777 * @param i Index of job.
778 * @returns Handle to hb_job_t of desired job.
780 hb_job_t * hb_job( hb_handle_t * h, int i )
782 return hb_list_item( h->jobs, i );
785 hb_job_t * hb_current_job( hb_handle_t * h )
787 return( h->current_job );
791 * Adds a job to the job list.
792 * @param h Handle to hb_handle_t.
793 * @param job Handle to hb_job_t.
795 void hb_add( hb_handle_t * h, hb_job_t * job )
798 hb_title_t * title, * title_copy;
799 hb_chapter_t * chapter, * chapter_copy;
801 hb_subtitle_t * subtitle, * subtitle_copy;
807 title_copy = malloc( sizeof( hb_title_t ) );
808 memcpy( title_copy, title, sizeof( hb_title_t ) );
810 title_copy->list_chapter = hb_list_init();
811 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
813 chapter = hb_list_item( title->list_chapter, i );
814 chapter_copy = malloc( sizeof( hb_chapter_t ) );
815 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
816 hb_list_add( title_copy->list_chapter, chapter_copy );
819 /* Copy the audio track(s) we want */
820 title_copy->list_audio = hb_list_init();
822 for( i = 0; i < hb_list_count(job->list_audio); i++ )
824 if( ( audio = hb_list_item( job->list_audio, i ) ) )
826 hb_list_add( title_copy->list_audio, hb_audio_copy(audio) );
830 title_copy->list_subtitle = hb_list_init();
833 * The following code is confusing, there are three ways in which
834 * we select subtitles and it depends on whether this is single or
837 * subtitle_scan may be enabled, in which case the first pass
838 * scans all subtitles of that language. The second pass does not
839 * select any because they are set at the end of the first pass.
841 * native_language may have a preferred language, in which case we
842 * may be switching the language we want for the subtitles in the
843 * first pass of a single pass, or the second pass of a two pass.
845 * We may have manually selected a subtitle, in which case that is
846 * selected in the first pass of a single pass, or the second of a
849 memset( audio_lang, 0, sizeof( audio_lang ) );
851 if ( job->indepth_scan || job->native_language ) {
854 * Find the first audio language that is being encoded
856 for( i = 0; i < hb_list_count(job->list_audio); i++ )
858 if( ( audio = hb_list_item( job->list_audio, i ) ) )
860 strncpy(audio_lang, audio->config.lang.iso639_2, sizeof(audio_lang));
866 * In all cases switch the language if we need to to our native
869 if( job->native_language )
871 if( strncasecmp( job->native_language, audio_lang,
872 sizeof( audio_lang ) ) != 0 )
877 hb_log( "Enabled subtitles in native language '%s', audio is in '%s'",
878 job->native_language, audio_lang);
881 * The main audio track is not in our native language, so switch
882 * the subtitles to use our native language instead.
884 strncpy( audio_lang, job->native_language, sizeof( audio_lang ) );
887 * native language is irrelevent, free it.
889 free( job->native_language );
890 job->native_language = NULL;
896 * If doing a subtitle scan then add all the matching subtitles for this
899 if ( job->indepth_scan )
901 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
903 subtitle = hb_list_item( title->list_subtitle, i );
904 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
907 * Matched subtitle language with audio language, so
908 * add this to our list to scan.
910 * We will update the subtitle list on the second pass
911 * later after the first pass has completed.
913 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
914 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
915 hb_list_add( title_copy->list_subtitle, subtitle_copy );
916 if ( job->native_language ) {
918 * With native language just select the
919 * first match in our langiage, not all of
920 * them. Subsequent ones are likely to be commentary
928 * Not doing a subtitle scan in this pass, but maybe we are in the
931 if( job->select_subtitle )
934 * Don't add subtitles here, we'll add them via select_subtitle
935 * at the end of the subtitle_scan.
939 * Definitely not doing a subtitle scan.
941 if( job->pass != 1 && job->native_language )
944 * We are not doing a subtitle scan but do want the
945 * native langauge subtitle selected, so select it
946 * for pass 0 or pass 2 of a two pass.
948 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
950 subtitle = hb_list_item( title->list_subtitle, i );
951 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
954 * Matched subtitle language with audio language, so
955 * add this to our list to scan.
957 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
958 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
959 hb_list_add( title_copy->list_subtitle, subtitle_copy );
965 * Manually selected subtitle, in which case only
966 * bother adding them for pass 0 or pass 2 of a two
971 if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) )
973 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
974 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
975 hb_list_add( title_copy->list_subtitle, subtitle_copy );
983 job_copy = calloc( sizeof( hb_job_t ), 1 );
984 memcpy( job_copy, job, sizeof( hb_job_t ) );
985 title_copy->job = job_copy;
986 job_copy->title = title_copy;
987 job_copy->list_audio = title_copy->list_audio;
988 job_copy->file = strdup( job->file );
990 job_copy->pause = h->pause_lock;
992 /* Copy the job filter list */
996 int filter_count = hb_list_count( job->filters );
997 job_copy->filters = hb_list_init();
998 for( i = 0; i < filter_count; i++ )
1001 * Copy the filters, since the MacGui reuses the global filter objects
1002 * meaning that queued up jobs overwrite the previous filter settings.
1003 * In reality, settings is probably the only field that needs duplicating
1004 * since it's the only value that is ever changed. But name is duplicated
1005 * as well for completeness. Not copying private_data since it gets
1006 * created for each job in renderInit.
1008 hb_filter_object_t * filter = hb_list_item( job->filters, i );
1009 hb_filter_object_t * filter_copy = malloc( sizeof( hb_filter_object_t ) );
1010 memcpy( filter_copy, filter, sizeof( hb_filter_object_t ) );
1012 filter_copy->name = strdup( filter->name );
1013 if( filter->settings )
1014 filter_copy->settings = strdup( filter->settings );
1015 hb_list_add( job_copy->filters, filter_copy );
1019 /* Add the job to the list */
1020 hb_list_add( h->jobs, job_copy );
1021 h->job_count = hb_count(h);
1022 h->job_count_permanent++;
1026 * Removes a job from the job list.
1027 * @param h Handle to hb_handle_t.
1028 * @param job Handle to hb_job_t.
1030 void hb_rem( hb_handle_t * h, hb_job_t * job )
1032 hb_list_rem( h->jobs, job );
1034 h->job_count = hb_count(h);
1035 if (h->job_count_permanent)
1036 h->job_count_permanent--;
1038 /* XXX free everything XXX */
1042 * Starts the conversion process.
1043 * Sets state to HB_STATE_WORKING.
1044 * calls hb_work_init, to launch work thread. Stores handle to work thread.
1045 * @param h Handle to hb_handle_t.
1047 void hb_start( hb_handle_t * h )
1050 h->job_count = hb_list_count( h->jobs );
1051 h->job_count_permanent = h->job_count;
1053 hb_lock( h->state_lock );
1054 h->state.state = HB_STATE_WORKING;
1055 #define p h->state.param.working
1058 p.job_count = h->job_count;
1066 hb_unlock( h->state_lock );
1071 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
1072 &h->work_die, &h->work_error, &h->current_job );
1076 * Pauses the conversion process.
1077 * @param h Handle to hb_handle_t.
1079 void hb_pause( hb_handle_t * h )
1083 hb_lock( h->pause_lock );
1086 hb_lock( h->state_lock );
1087 h->state.state = HB_STATE_PAUSED;
1088 hb_unlock( h->state_lock );
1093 * Resumes the conversion process.
1094 * @param h Handle to hb_handle_t.
1096 void hb_resume( hb_handle_t * h )
1100 hb_unlock( h->pause_lock );
1106 * Stops the conversion process.
1107 * @param h Handle to hb_handle_t.
1109 void hb_stop( hb_handle_t * h )
1113 h->job_count = hb_count(h);
1114 h->job_count_permanent = 0;
1120 * Returns the state of the conversion process.
1121 * @param h Handle to hb_handle_t.
1122 * @param s Handle to hb_state_t which to copy the state data.
1124 void hb_get_state( hb_handle_t * h, hb_state_t * s )
1126 hb_lock( h->state_lock );
1128 memcpy( s, &h->state, sizeof( hb_state_t ) );
1129 if ( h->state.state == HB_STATE_SCANDONE || h->state.state == HB_STATE_WORKDONE )
1130 h->state.state = HB_STATE_IDLE;
1132 hb_unlock( h->state_lock );
1135 void hb_get_state2( hb_handle_t * h, hb_state_t * s )
1137 hb_lock( h->state_lock );
1139 memcpy( s, &h->state, sizeof( hb_state_t ) );
1141 hb_unlock( h->state_lock );
1145 * Called in MacGui in UpdateUI to check
1146 * for a new scan being completed to set a new source
1148 int hb_get_scancount( hb_handle_t * h)
1150 return h->scanCount;
1154 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init_real.
1155 * @param _h Pointer to handle to hb_handle_t.
1157 void hb_close( hb_handle_t ** _h )
1159 hb_handle_t * h = *_h;
1163 hb_thread_close( &h->main_thread );
1165 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1167 hb_list_rem( h->list_title, title );
1168 if( title->job && title->job->filters )
1170 hb_list_close( &title->job->filters );
1173 hb_title_close( &title );
1175 hb_list_close( &h->list_title );
1177 hb_list_close( &h->jobs );
1178 hb_lock_close( &h->state_lock );
1179 hb_lock_close( &h->pause_lock );
1186 * Monitors the state of the update, scan, and work threads.
1187 * Sets scan done state when scan thread exits.
1188 * Sets work done state when work thread exits.
1189 * @param _h Handle to hb_handle_t
1191 static void thread_func( void * _h )
1193 hb_handle_t * h = (hb_handle_t *) _h;
1196 struct dirent * entry;
1200 /* Create folder for temporary files */
1201 memset( dirname, 0, 1024 );
1202 hb_get_tempory_directory( h, dirname );
1204 hb_mkdir( dirname );
1208 /* In case the check_update thread hangs, it'll die sooner or
1209 later. Then, we join it here */
1210 if( h->update_thread &&
1211 hb_thread_has_exited( h->update_thread ) )
1213 hb_thread_close( &h->update_thread );
1216 /* Check if the scan thread is done */
1217 if( h->scan_thread &&
1218 hb_thread_has_exited( h->scan_thread ) )
1220 hb_thread_close( &h->scan_thread );
1222 hb_log( "libhb: scan thread found %d valid title(s)",
1223 hb_list_count( h->list_title ) );
1224 hb_lock( h->state_lock );
1225 h->state.state = HB_STATE_SCANDONE; //originally state.state
1226 hb_unlock( h->state_lock );
1227 /*we increment this sessions scan count by one for the MacGui
1228 to trigger a new source being set */
1232 /* Check if the work thread is done */
1233 if( h->work_thread &&
1234 hb_thread_has_exited( h->work_thread ) )
1236 hb_thread_close( &h->work_thread );
1238 hb_log( "libhb: work result = %d",
1240 hb_lock( h->state_lock );
1241 h->state.state = HB_STATE_WORKDONE;
1242 h->state.param.workdone.error = h->work_error;
1244 h->job_count = hb_count(h);
1245 if (h->job_count < 1)
1246 h->job_count_permanent = 0;
1247 hb_unlock( h->state_lock );
1253 if( h->work_thread )
1256 hb_thread_close( &h->work_thread );
1259 /* Remove temp folder */
1260 dir = opendir( dirname );
1263 while( ( entry = readdir( dir ) ) )
1265 char filename[1024];
1266 if( entry->d_name[0] == '.' )
1270 memset( filename, 0, 1024 );
1271 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
1281 * @param h Handle to hb_handle_t
1283 int hb_get_pid( hb_handle_t * h )
1289 * Sets the current state.
1290 * @param h Handle to hb_handle_t
1291 * @param s Handle to new hb_state_t
1293 void hb_set_state( hb_handle_t * h, hb_state_t * s )
1295 hb_lock( h->pause_lock );
1296 hb_lock( h->state_lock );
1297 memcpy( &h->state, s, sizeof( hb_state_t ) );
1298 if( h->state.state == HB_STATE_WORKING )
1301 if (h->job_count < 1)
1302 h->job_count_permanent = 1;
1304 h->state.param.working.job_cur =
1305 h->job_count_permanent - hb_list_count( h->jobs );
1306 h->state.param.working.job_count = h->job_count_permanent;
1308 // Set which job is being worked on
1310 h->state.param.working.sequence_id = h->current_job->sequence_id;
1312 h->state.param.working.sequence_id = 0;
1314 hb_unlock( h->state_lock );
1315 hb_unlock( h->pause_lock );