6 /* The "Check for update" thread */
9 hb_thread_t * update_thread;
11 /* This thread's only purpose is to check other threads'
14 hb_thread_t * main_thread;
17 /* DVD/file scan thread */
18 hb_list_t * list_title;
19 hb_thread_t * scan_thread;
21 /* The thread which processes the jobs. Others threads are launched
22 from this one (see work.c) */
24 hb_job_t * current_job;
26 int job_count_permanent;
27 volatile int work_die;
29 hb_thread_t * work_thread;
33 hb_lock_t * state_lock;
37 hb_lock_t * pause_lock;
38 /* For MacGui active queue
39 increments each time the scan thread completes*/
44 hb_lock_t *hb_avcodec_lock;
45 hb_work_object_t * hb_objects = NULL;
47 static void thread_func( void * );
49 void hb_avcodec_init()
51 hb_avcodec_lock = hb_lock_init();
55 int hb_avcodec_open(AVCodecContext *avctx, AVCodec *codec)
58 hb_lock( hb_avcodec_lock );
59 ret = avcodec_open(avctx, codec);
60 hb_unlock( hb_avcodec_lock );
64 int hb_avcodec_close(AVCodecContext *avctx)
67 hb_lock( hb_avcodec_lock );
68 ret = avcodec_close(avctx);
69 hb_unlock( hb_avcodec_lock );
74 * Registers work objects, by adding the work object to a liked list.
75 * @param w Handle to hb_work_object_t to register.
77 void hb_register( hb_work_object_t * w )
84 * libhb initialization routine.
85 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
86 * @param update_check signals libhb to check for updated version from HandBrake website.
87 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
89 hb_handle_t * hb_init_real( int verbose, int update_check )
91 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
94 /* See hb_deep_log() and hb_log() in common.c */
95 global_verbosity_level = verbose;
97 putenv( "HB_DEBUG=1" );
99 /* Check for an update on the website if asked to */
104 hb_log( "hb_init: checking for updates" );
105 date = hb_get_date();
106 h->update_thread = hb_update_init( &h->build, h->version );
110 if( hb_thread_has_exited( h->update_thread ) )
112 /* Immediate success or failure */
113 hb_thread_close( &h->update_thread );
116 if( hb_get_date() > date + 1000 )
118 /* Still nothing after one second. Connection problem,
119 let the thread die */
120 hb_log( "hb_init: connection problem, not waiting for "
129 * Initialise buffer pool
131 hb_buffer_pool_init();
133 /* CPU count detection */
134 hb_log( "hb_init: checking cpu count" );
135 h->cpu_count = hb_get_cpu_count();
137 h->list_title = hb_list_init();
138 h->jobs = hb_list_init();
140 h->state_lock = hb_lock_init();
141 h->state.state = HB_STATE_IDLE;
143 h->pause_lock = hb_lock_init();
148 /* Start library thread */
149 hb_log( "hb_init: starting libhb thread" );
151 h->main_thread = hb_thread_init( "libhb", thread_func, h,
152 HB_NORMAL_PRIORITY );
156 /* Set the scan count to start at 0 */
161 * libhb initialization routine.
162 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
163 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
164 * @param update_check signals libhb to check for updated version from HandBrake website.
165 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
167 hb_handle_t * hb_init_dl( int verbose, int update_check )
169 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
172 /* See hb_log() in common.c */
173 if( verbose > HB_DEBUG_NONE )
175 putenv( "HB_DEBUG=1" );
178 /* Check for an update on the website if asked to */
183 hb_log( "hb_init: checking for updates" );
184 date = hb_get_date();
185 h->update_thread = hb_update_init( &h->build, h->version );
189 if( hb_thread_has_exited( h->update_thread ) )
191 /* Immediate success or failure */
192 hb_thread_close( &h->update_thread );
195 if( hb_get_date() > date + 1000 )
197 /* Still nothing after one second. Connection problem,
198 let the thread die */
199 hb_log( "hb_init: connection problem, not waiting for "
207 /* CPU count detection */
208 hb_log( "hb_init: checking cpu count" );
209 h->cpu_count = hb_get_cpu_count();
211 h->list_title = hb_list_init();
212 h->jobs = hb_list_init();
213 h->current_job = NULL;
215 h->state_lock = hb_lock_init();
216 h->state.state = HB_STATE_IDLE;
218 h->pause_lock = hb_lock_init();
222 avcodec_register_all();
224 /* Start library thread */
225 hb_log( "hb_init: starting libhb thread" );
227 h->main_thread = hb_thread_init( "libhb", thread_func, h,
228 HB_NORMAL_PRIORITY );
230 hb_register( &hb_sync );
231 hb_register( &hb_decmpeg2 );
232 hb_register( &hb_decsub );
233 hb_register( &hb_render );
234 hb_register( &hb_encavcodec );
235 hb_register( &hb_encxvid );
236 hb_register( &hb_encx264 );
237 hb_register( &hb_enctheora );
238 hb_register( &hb_deca52 );
239 hb_register( &hb_decdca );
240 hb_register( &hb_decavcodec );
241 hb_register( &hb_decavcodecv );
242 hb_register( &hb_decavcodecvi );
243 hb_register( &hb_decavcodecai );
244 hb_register( &hb_declpcm );
245 hb_register( &hb_encfaac );
246 hb_register( &hb_enclame );
247 hb_register( &hb_encvorbis );
254 * Returns current version of libhb.
255 * @param h Handle to hb_handle_t.
256 * @return character array of version number.
258 char * hb_get_version( hb_handle_t * h )
264 * Returns current build of libhb.
265 * @param h Handle to hb_handle_t.
266 * @return character array of build number.
268 int hb_get_build( hb_handle_t * h )
274 * Checks for needed update.
275 * @param h Handle to hb_handle_t.
276 * @param version Pointer to handle where version will be copied.
277 * @return update indicator.
279 int hb_check_update( hb_handle_t * h, char ** version )
281 *version = ( h->build < 0 ) ? NULL : h->version;
286 * Sets the cpu count to the desired value.
287 * @param h Handle to hb_handle_t
288 * @param cpu_count Number of CPUs to use.
290 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
292 cpu_count = MAX( 1, cpu_count );
293 cpu_count = MIN( cpu_count, 8 );
294 h->cpu_count = cpu_count;
298 * Initializes a scan of the by calling hb_scan_init
299 * @param h Handle to hb_handle_t
300 * @param path location of VIDEO_TS folder.
301 * @param title_index Desired title to scan. 0 for all titles.
302 * @param preview_count Number of preview images to generate.
303 * @param store_previews Whether or not to write previews to disk.
305 void hb_scan( hb_handle_t * h, const char * path, int title_index,
306 int preview_count, int store_previews )
310 /* Clean up from previous scan */
311 while( ( title = hb_list_item( h->list_title, 0 ) ) )
313 hb_list_rem( h->list_title, title );
314 hb_title_close( &title );
317 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
318 h->scan_thread = hb_scan_init( h, path, title_index, h->list_title,
319 preview_count, store_previews );
323 * Returns the list of titles found.
324 * @param h Handle to hb_handle_t
325 * @return Handle to hb_list_t of the title list.
327 hb_list_t * hb_get_titles( hb_handle_t * h )
329 return h->list_title;
333 * Create preview image of desired title a index of picture.
334 * @param h Handle to hb_handle_t.
335 * @param title Handle to hb_title_t of desired title.
336 * @param picture Index in title.
337 * @param buffer Handle to buufer were inage will be drawn.
339 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
342 hb_job_t * job = title->job;
345 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
346 uint32_t * p32, swsflags;
347 AVPicture pic_in, pic_preview, pic_deint, pic_crop, pic_scale;
348 struct SwsContext * context;
350 int rgb_width = ((job->width + 7) >> 3) << 3;
353 swsflags = SWS_LANCZOS;
355 swsflags |= SWS_ACCURATE_RND;
356 #endif /* __x86_64__ */
358 buf1 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
359 buf2 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
360 buf3 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, job->width, job->height ) );
361 buf4 = av_malloc( avpicture_get_size( PIX_FMT_RGBA32, rgb_width, job->height ) );
362 avpicture_fill( &pic_in, buf1, PIX_FMT_YUV420P,
363 title->width, title->height );
364 avpicture_fill( &pic_deint, buf2, PIX_FMT_YUV420P,
365 title->width, title->height );
366 avpicture_fill( &pic_scale, buf3, PIX_FMT_YUV420P,
367 job->width, job->height );
368 avpicture_fill( &pic_preview, buf4, PIX_FMT_RGBA32,
369 rgb_width, job->height );
371 // Allocate the AVPicture frames and fill in
373 memset( filename, 0, 1024 );
375 hb_get_tempory_filename( h, filename, "%x%d",
376 (intptr_t) title, picture );
378 file = fopen( filename, "r" );
381 hb_log( "hb_get_preview: fopen failed" );
385 fread( buf1, avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height), 1, file );
388 if( job->deinterlace )
390 // Deinterlace and crop
391 avpicture_deinterlace( &pic_deint, &pic_in, PIX_FMT_YUV420P, title->width, title->height );
392 av_picture_crop( &pic_crop, &pic_deint, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
397 av_picture_crop( &pic_crop, &pic_in, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
400 // Get scaling context
401 context = sws_getContext(title->width - (job->crop[2] + job->crop[3]),
402 title->height - (job->crop[0] + job->crop[1]),
404 job->width, job->height, PIX_FMT_YUV420P,
405 swsflags, NULL, NULL, NULL);
409 pic_crop.data, pic_crop.linesize,
410 0, title->height - (job->crop[0] + job->crop[1]),
411 pic_scale.data, pic_scale.linesize);
414 sws_freeContext( context );
416 // Get preview context
417 context = sws_getContext(rgb_width, job->height, PIX_FMT_YUV420P,
418 rgb_width, job->height, PIX_FMT_RGBA32,
419 swsflags, NULL, NULL, NULL);
423 pic_scale.data, pic_scale.linesize,
425 pic_preview.data, pic_preview.linesize);
428 sws_freeContext( context );
430 /* Gray background */
431 p32 = (uint32_t *) buffer;
432 for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
437 /* Draw the picture, centered, and draw the cropping zone */
438 preview_size = pic_preview.linesize[0];
439 pen = buffer + ( title->height - job->height ) *
440 ( title->width + 2 ) * 2 + ( title->width - job->width ) * 2;
441 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
442 pen += 4 * ( title->width + 2 );
443 for( i = 0; i < job->height; i++ )
446 nextLine = pen + 4 * ( title->width + 2 );
447 memset( pen, 0xFF, 4 );
449 memcpy( pen, buf4 + preview_size * i, 4 * job->width );
450 pen += 4 * job->width;
451 memset( pen, 0xFF, 4 );
454 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
457 avpicture_free( &pic_preview );
458 avpicture_free( &pic_scale );
459 avpicture_free( &pic_deint );
460 avpicture_free( &pic_in );
464 * Analyzes a frame to detect interlacing artifacts
465 * and returns true if interlacing (combing) is found.
467 * Code taken from Thomas Oestreich's 32detect filter
468 * in the Transcode project, with minor formatting changes.
470 * @param buf An hb_buffer structure holding valid frame data
471 * @param width The frame's width in pixels
472 * @param height The frame's height in pixels
473 * @param color_equal Sensitivity for detecting similar colors
474 * @param color_diff Sensitivity for detecting different colors
475 * @param threshold Sensitivity for flagging planes as combed
476 * @param prog_equal Sensitivity for detecting similar colors on progressive frames
477 * @param prog_diff Sensitivity for detecting different colors on progressive frames
478 * @param prog_threshold Sensitivity for flagging progressive frames as combed
480 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 )
482 int j, k, n, off, cc_1, cc_2, cc[3], flag[3] ;
483 uint16_t s1, s2, s3, s4;
488 if ( buf->flags & 16 )
490 /* Frame is progressive, be more discerning. */
491 color_diff = prog_diff;
492 color_equal = prog_equal;
493 threshold = prog_threshold;
496 /* One pas for Y, one pass for Cb, one pass for Cr */
497 for( k = 0; k < 3; k++ )
501 /* Y has already been checked, now offset by Y's dimensions
502 and divide all the other values by 2, since Cr and Cb
503 are half-size compared to Y. */
504 offset = width * height;
510 /* Y and Cb are done, so the offset needs to be bumped
511 so it's width*height + (width / 2) * (height / 2) */
515 for( j = 0; j < width; ++j )
519 for( n = 0; n < ( height - 4 ); n = n + 2 )
521 /* Look at groups of 4 sequential horizontal lines */
522 s1 = ( ( buf->data + offset )[ off + j ] & 0xff );
523 s2 = ( ( buf->data + offset )[ off + j + width ] & 0xff );
524 s3 = ( ( buf->data + offset )[ off + j + 2 * width ] & 0xff );
525 s4 = ( ( buf->data + offset )[ off + j + 3 * width ] & 0xff );
527 /* Note if the 1st and 2nd lines are more different in
528 color than the 1st and 3rd lines are similar in color.*/
529 if ( ( abs( s1 - s3 ) < color_equal ) &&
530 ( abs( s1 - s2 ) > color_diff ) )
533 /* Note if the 2nd and 3rd lines are more different in
534 color than the 2nd and 4th lines are similar in color.*/
535 if ( ( abs( s2 - s4 ) < color_equal ) &&
536 ( abs( s2 - s3 ) > color_diff) )
539 /* Now move down 2 horizontal lines before starting over.*/
545 /* The final cc score for a plane is the percentage of combed pixels it contains.
546 Because sensitivity goes down to hundreths of a percent, multiply by 1000
547 so it will be easy to compare against the threhold value which is an integer. */
548 cc[k] = (int)( ( cc_1 + cc_2 ) * 1000.0 / ( width * height ) );
552 /* HandBrake is all yuv420, so weight the average percentage of all 3 planes accordingly.*/
553 int average_cc = ( 2 * cc[0] + ( cc[1] / 2 ) + ( cc[2] / 2 ) ) / 3;
555 /* Now see if that average percentage of combed pixels surpasses the threshold percentage given by the user.*/
556 if( average_cc > threshold )
559 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" );
565 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" );
568 /* Reaching this point means no combing detected. */
574 * Calculates job width and height for anamorphic content,
576 * @param job Handle to hb_job_t
577 * @param output_width Pointer to returned storage width
578 * @param output_height Pointer to returned storage height
579 * @param output_par_width Pointer to returned pixel width
580 @ param output_par_height Pointer to returned pixel height
582 void hb_set_anamorphic_size( hb_job_t * job,
583 int *output_width, int *output_height,
584 int *output_par_width, int *output_par_height )
586 /* "Loose" anamorphic.
587 - Uses mod16-compliant dimensions,
588 - Allows users to set the width
589 - Handles ITU pixel aspects
592 /* Set up some variables to make the math easier to follow. */
593 hb_title_t * title = job->title;
594 int cropped_width = title->width - job->crop[2] - job->crop[3] ;
595 int cropped_height = title->height - job->crop[0] - job->crop[1] ;
596 double storage_aspect = (double)cropped_width / (double)cropped_height;
597 int width = job->width;
598 int height; // Gets set later, ignore user job->height value
599 int mod = job->modulus;
600 double aspect = title->aspect;
602 /* Gotta handle bounding dimensions differently
603 than for non-anamorphic encodes:
604 If the width is too big, just reset it with no rescaling.
605 Instead of using the aspect-scaled job height,
606 we need to see if the job width divided by the storage aspect
607 is bigger than the max. If so, set it to the max (this is sloppy).
608 If not, set job height to job width divided by storage aspect.
611 if ( job->maxWidth && (job->maxWidth < job->width) )
612 width = job->maxWidth;
614 height = ((double)width / storage_aspect) + 0.5;
615 if ( job->maxHeight && (job->maxHeight < height) )
616 height = job->maxHeight;
618 /* In case the user specified a modulus, use it */
624 /* Time to get picture dimensions that divide cleanly.*/
625 width = MULTIPLE_MOD( width, mod);
626 height = MULTIPLE_MOD( height, mod);
628 /* Verify these new dimensions don't violate max height and width settings */
629 if ( job->maxWidth && (job->maxWidth < job->width) )
630 width = job->maxWidth;
631 if ( job->maxHeight && (job->maxHeight < height) )
632 height = job->maxHeight;
634 int pixel_aspect_width = job->pixel_aspect_width;
635 int pixel_aspect_height = job->pixel_aspect_height;
637 /* If a source was really 704*480 and hard matted with cropping
638 to 720*480, replace the PAR values with the ITU broadcast ones. */
639 if (title->width == 720 && cropped_width <= 706)
641 // convert aspect to a scaled integer so we can test for 16:9 & 4:3
642 // aspect ratios ignoring insignificant differences in the LSBs of
643 // the floating point representation.
644 int iaspect = aspect * 9.;
646 /* Handle ITU PARs */
647 if (title->height == 480)
652 /* It's widescreen */
653 pixel_aspect_width = 40;
654 pixel_aspect_height = 33;
656 else if (iaspect == 12)
659 pixel_aspect_width = 10;
660 pixel_aspect_height = 11;
663 else if (title->height == 576)
668 /* It's widescreen */
669 pixel_aspect_width = 16;
670 pixel_aspect_height = 11;
672 else if (iaspect == 12)
675 pixel_aspect_width = 12;
676 pixel_aspect_height = 11;
681 /* Figure out what dimensions the source would display at. */
682 int source_display_width = cropped_width * (double)pixel_aspect_width /
683 (double)pixel_aspect_height ;
685 /* The film AR is the source's display width / cropped source height.
686 The output display width is the output height * film AR.
687 The output PAR is the output display width / output storage width. */
688 pixel_aspect_width = height * source_display_width / cropped_height;
689 pixel_aspect_height = width;
691 /* Pass the results back to the caller */
692 *output_width = width;
693 *output_height = height;
695 /* While x264 is smart enough to reduce fractions on its own, libavcodec
696 needs some help with the math, so lose superfluous factors. */
697 hb_reduce( output_par_width, output_par_height,
698 pixel_aspect_width, pixel_aspect_height );
702 * Calculates job width, height, and cropping parameters.
703 * @param job Handle to hb_job_t.
704 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
705 * @param pixels Maximum desired pixel count.
707 void hb_set_size( hb_job_t * job, double aspect, int pixels )
709 hb_title_t * title = job->title;
711 int croppedWidth = title->width - title->crop[2] - title->crop[3];
712 int croppedHeight = title->height - title->crop[0] - title->crop[1];
713 double croppedAspect = title->aspect * title->height * croppedWidth /
714 croppedHeight / title->width;
720 /* Keep the best possible aspect ratio */
721 aspect = croppedAspect;
724 /* Crop if necessary to obtain the desired ratio */
725 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
726 if( aspect < croppedAspect )
728 /* Need to crop on the left and right */
729 addCrop = croppedWidth - aspect * croppedHeight * title->width /
730 title->aspect / title->height;
733 addCrop = ( addCrop + 1 ) / 2;
734 job->crop[2] += addCrop;
735 job->crop[3] += addCrop;
737 else if( addCrop & 2 )
740 job->crop[2] += addCrop - 1;
741 job->crop[3] += addCrop + 1;
746 job->crop[2] += addCrop;
747 job->crop[3] += addCrop;
750 else if( aspect > croppedAspect )
752 /* Need to crop on the top and bottom */
753 addCrop = croppedHeight - croppedWidth * title->aspect *
754 title->height / aspect / title->width;
757 addCrop = ( addCrop + 1 ) / 2;
758 job->crop[0] += addCrop;
759 job->crop[1] += addCrop;
761 else if( addCrop & 2 )
764 job->crop[0] += addCrop - 1;
765 job->crop[1] += addCrop + 1;
770 job->crop[0] += addCrop;
771 job->crop[1] += addCrop;
775 /* Compute a resolution from the number of pixels and aspect */
779 h = MULTIPLE_16( (int)( (double)w / aspect ) );
787 job->height = MULTIPLE_16( (int)( (double)job->width / aspect ) );
791 * Returns the number of jobs in the queue.
792 * @param h Handle to hb_handle_t.
793 * @return Number of jobs.
795 int hb_count( hb_handle_t * h )
797 return hb_list_count( h->jobs );
801 * Returns handle to job at index i within the job list.
802 * @param h Handle to hb_handle_t.
803 * @param i Index of job.
804 * @returns Handle to hb_job_t of desired job.
806 hb_job_t * hb_job( hb_handle_t * h, int i )
808 return hb_list_item( h->jobs, i );
811 hb_job_t * hb_current_job( hb_handle_t * h )
813 return( h->current_job );
817 * Adds a job to the job list.
818 * @param h Handle to hb_handle_t.
819 * @param job Handle to hb_job_t.
821 void hb_add( hb_handle_t * h, hb_job_t * job )
824 hb_title_t * title, * title_copy;
825 hb_chapter_t * chapter, * chapter_copy;
827 hb_subtitle_t * subtitle, * subtitle_copy;
833 title_copy = malloc( sizeof( hb_title_t ) );
834 memcpy( title_copy, title, sizeof( hb_title_t ) );
836 title_copy->list_chapter = hb_list_init();
837 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
839 chapter = hb_list_item( title->list_chapter, i );
840 chapter_copy = malloc( sizeof( hb_chapter_t ) );
841 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
842 hb_list_add( title_copy->list_chapter, chapter_copy );
848 if( title->metadata )
850 title_copy->metadata = malloc( sizeof( hb_metadata_t ) );
852 if( title_copy->metadata )
854 memcpy( title_copy->metadata, title->metadata, sizeof( hb_metadata_t ) );
857 * Need to copy the artwork seperatly (TODO).
859 if( title->metadata->coverart )
861 title_copy->metadata->coverart = malloc( title->metadata->coverart_size );
862 if( title_copy->metadata->coverart )
864 memcpy( title_copy->metadata->coverart, title->metadata->coverart,
865 title->metadata->coverart_size );
867 title_copy->metadata->coverart_size = 0;
873 /* Copy the audio track(s) we want */
874 title_copy->list_audio = hb_list_init();
876 for( i = 0; i < hb_list_count(job->list_audio); i++ )
878 if( ( audio = hb_list_item( job->list_audio, i ) ) )
880 hb_list_add( title_copy->list_audio, hb_audio_copy(audio) );
884 title_copy->list_subtitle = hb_list_init();
887 * The following code is confusing, there are three ways in which
888 * we select subtitles and it depends on whether this is single or
891 * subtitle_scan may be enabled, in which case the first pass
892 * scans all subtitles of that language. The second pass does not
893 * select any because they are set at the end of the first pass.
895 * native_language may have a preferred language, in which case we
896 * may be switching the language we want for the subtitles in the
897 * first pass of a single pass, or the second pass of a two pass.
899 * We may have manually selected a subtitle, in which case that is
900 * selected in the first pass of a single pass, or the second of a
903 memset( audio_lang, 0, sizeof( audio_lang ) );
905 if ( job->indepth_scan || job->native_language ) {
908 * Find the first audio language that is being encoded
910 for( i = 0; i < hb_list_count(job->list_audio); i++ )
912 if( ( audio = hb_list_item( job->list_audio, i ) ) )
914 strncpy(audio_lang, audio->config.lang.iso639_2, sizeof(audio_lang));
920 * In all cases switch the language if we need to to our native
923 if( job->native_language )
925 if( strncasecmp( job->native_language, audio_lang,
926 sizeof( audio_lang ) ) != 0 )
931 hb_log( "Enabled subtitles in native language '%s', audio is in '%s'",
932 job->native_language, audio_lang);
935 * The main audio track is not in our native language, so switch
936 * the subtitles to use our native language instead.
938 strncpy( audio_lang, job->native_language, sizeof( audio_lang ) );
941 * native language is irrelevent, free it.
943 free( job->native_language );
944 job->native_language = NULL;
950 * If doing a subtitle scan then add all the matching subtitles for this
953 if ( job->indepth_scan )
955 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
957 subtitle = hb_list_item( title->list_subtitle, i );
958 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
961 * Matched subtitle language with audio language, so
962 * add this to our list to scan.
964 * We will update the subtitle list on the second pass
965 * later after the first pass has completed.
967 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
968 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
969 hb_list_add( title_copy->list_subtitle, subtitle_copy );
970 if ( job->native_language ) {
972 * With native language just select the
973 * first match in our langiage, not all of
974 * them. Subsequent ones are likely to be commentary
982 * Not doing a subtitle scan in this pass, but maybe we are in the
985 if( job->select_subtitle )
988 * Don't add subtitles here, we'll add them via select_subtitle
989 * at the end of the subtitle_scan.
993 * Definitely not doing a subtitle scan.
995 if( job->pass != 1 && job->native_language )
998 * We are not doing a subtitle scan but do want the
999 * native langauge subtitle selected, so select it
1000 * for pass 0 or pass 2 of a two pass.
1002 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
1004 subtitle = hb_list_item( title->list_subtitle, i );
1005 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
1008 * Matched subtitle language with audio language, so
1009 * add this to our list to scan.
1011 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1012 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1013 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1019 * Manually selected subtitle, in which case only
1020 * bother adding them for pass 0 or pass 2 of a two
1023 if( job->pass != 1 )
1025 if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) )
1027 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1028 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1029 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1037 job_copy = calloc( sizeof( hb_job_t ), 1 );
1038 memcpy( job_copy, job, sizeof( hb_job_t ) );
1039 title_copy->job = job_copy;
1040 job_copy->title = title_copy;
1041 job_copy->list_audio = title_copy->list_audio;
1042 job_copy->file = strdup( job->file );
1044 job_copy->pause = h->pause_lock;
1046 /* Copy the job filter list */
1050 int filter_count = hb_list_count( job->filters );
1051 job_copy->filters = hb_list_init();
1052 for( i = 0; i < filter_count; i++ )
1055 * Copy the filters, since the MacGui reuses the global filter objects
1056 * meaning that queued up jobs overwrite the previous filter settings.
1057 * In reality, settings is probably the only field that needs duplicating
1058 * since it's the only value that is ever changed. But name is duplicated
1059 * as well for completeness. Not copying private_data since it gets
1060 * created for each job in renderInit.
1062 hb_filter_object_t * filter = hb_list_item( job->filters, i );
1063 hb_filter_object_t * filter_copy = malloc( sizeof( hb_filter_object_t ) );
1064 memcpy( filter_copy, filter, sizeof( hb_filter_object_t ) );
1066 filter_copy->name = strdup( filter->name );
1067 if( filter->settings )
1068 filter_copy->settings = strdup( filter->settings );
1069 hb_list_add( job_copy->filters, filter_copy );
1073 /* Add the job to the list */
1074 hb_list_add( h->jobs, job_copy );
1075 h->job_count = hb_count(h);
1076 h->job_count_permanent++;
1080 * Removes a job from the job list.
1081 * @param h Handle to hb_handle_t.
1082 * @param job Handle to hb_job_t.
1084 void hb_rem( hb_handle_t * h, hb_job_t * job )
1086 hb_list_rem( h->jobs, job );
1088 h->job_count = hb_count(h);
1089 if (h->job_count_permanent)
1090 h->job_count_permanent--;
1092 /* XXX free everything XXX */
1096 * Starts the conversion process.
1097 * Sets state to HB_STATE_WORKING.
1098 * calls hb_work_init, to launch work thread. Stores handle to work thread.
1099 * @param h Handle to hb_handle_t.
1101 void hb_start( hb_handle_t * h )
1104 h->job_count = hb_list_count( h->jobs );
1105 h->job_count_permanent = h->job_count;
1107 hb_lock( h->state_lock );
1108 h->state.state = HB_STATE_WORKING;
1109 #define p h->state.param.working
1112 p.job_count = h->job_count;
1120 hb_unlock( h->state_lock );
1125 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
1126 &h->work_die, &h->work_error, &h->current_job );
1130 * Pauses the conversion process.
1131 * @param h Handle to hb_handle_t.
1133 void hb_pause( hb_handle_t * h )
1137 hb_lock( h->pause_lock );
1140 hb_lock( h->state_lock );
1141 h->state.state = HB_STATE_PAUSED;
1142 hb_unlock( h->state_lock );
1147 * Resumes the conversion process.
1148 * @param h Handle to hb_handle_t.
1150 void hb_resume( hb_handle_t * h )
1154 hb_unlock( h->pause_lock );
1160 * Stops the conversion process.
1161 * @param h Handle to hb_handle_t.
1163 void hb_stop( hb_handle_t * h )
1167 h->job_count = hb_count(h);
1168 h->job_count_permanent = 0;
1174 * Returns the state of the conversion process.
1175 * @param h Handle to hb_handle_t.
1176 * @param s Handle to hb_state_t which to copy the state data.
1178 void hb_get_state( hb_handle_t * h, hb_state_t * s )
1180 hb_lock( h->state_lock );
1182 memcpy( s, &h->state, sizeof( hb_state_t ) );
1183 if ( h->state.state == HB_STATE_SCANDONE || h->state.state == HB_STATE_WORKDONE )
1184 h->state.state = HB_STATE_IDLE;
1186 hb_unlock( h->state_lock );
1189 void hb_get_state2( hb_handle_t * h, hb_state_t * s )
1191 hb_lock( h->state_lock );
1193 memcpy( s, &h->state, sizeof( hb_state_t ) );
1195 hb_unlock( h->state_lock );
1199 * Called in MacGui in UpdateUI to check
1200 * for a new scan being completed to set a new source
1202 int hb_get_scancount( hb_handle_t * h)
1204 return h->scanCount;
1208 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init_real.
1209 * @param _h Pointer to handle to hb_handle_t.
1211 void hb_close( hb_handle_t ** _h )
1213 hb_handle_t * h = *_h;
1217 hb_thread_close( &h->main_thread );
1219 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1221 hb_list_rem( h->list_title, title );
1222 if( title->job && title->job->filters )
1224 hb_list_close( &title->job->filters );
1227 hb_title_close( &title );
1229 hb_list_close( &h->list_title );
1231 hb_list_close( &h->jobs );
1232 hb_lock_close( &h->state_lock );
1233 hb_lock_close( &h->pause_lock );
1240 * Monitors the state of the update, scan, and work threads.
1241 * Sets scan done state when scan thread exits.
1242 * Sets work done state when work thread exits.
1243 * @param _h Handle to hb_handle_t
1245 static void thread_func( void * _h )
1247 hb_handle_t * h = (hb_handle_t *) _h;
1250 struct dirent * entry;
1254 /* Create folder for temporary files */
1255 memset( dirname, 0, 1024 );
1256 hb_get_tempory_directory( h, dirname );
1258 hb_mkdir( dirname );
1262 /* In case the check_update thread hangs, it'll die sooner or
1263 later. Then, we join it here */
1264 if( h->update_thread &&
1265 hb_thread_has_exited( h->update_thread ) )
1267 hb_thread_close( &h->update_thread );
1270 /* Check if the scan thread is done */
1271 if( h->scan_thread &&
1272 hb_thread_has_exited( h->scan_thread ) )
1274 hb_thread_close( &h->scan_thread );
1276 hb_log( "libhb: scan thread found %d valid title(s)",
1277 hb_list_count( h->list_title ) );
1278 hb_lock( h->state_lock );
1279 h->state.state = HB_STATE_SCANDONE; //originally state.state
1280 hb_unlock( h->state_lock );
1281 /*we increment this sessions scan count by one for the MacGui
1282 to trigger a new source being set */
1286 /* Check if the work thread is done */
1287 if( h->work_thread &&
1288 hb_thread_has_exited( h->work_thread ) )
1290 hb_thread_close( &h->work_thread );
1292 hb_log( "libhb: work result = %d",
1294 hb_lock( h->state_lock );
1295 h->state.state = HB_STATE_WORKDONE;
1296 h->state.param.workdone.error = h->work_error;
1298 h->job_count = hb_count(h);
1299 if (h->job_count < 1)
1300 h->job_count_permanent = 0;
1301 hb_unlock( h->state_lock );
1307 if( h->work_thread )
1310 hb_thread_close( &h->work_thread );
1313 /* Remove temp folder */
1314 dir = opendir( dirname );
1317 while( ( entry = readdir( dir ) ) )
1319 char filename[1024];
1320 if( entry->d_name[0] == '.' )
1324 memset( filename, 0, 1024 );
1325 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
1335 * @param h Handle to hb_handle_t
1337 int hb_get_pid( hb_handle_t * h )
1343 * Sets the current state.
1344 * @param h Handle to hb_handle_t
1345 * @param s Handle to new hb_state_t
1347 void hb_set_state( hb_handle_t * h, hb_state_t * s )
1349 hb_lock( h->pause_lock );
1350 hb_lock( h->state_lock );
1351 memcpy( &h->state, s, sizeof( hb_state_t ) );
1352 if( h->state.state == HB_STATE_WORKING )
1355 if (h->job_count < 1)
1356 h->job_count_permanent = 1;
1358 h->state.param.working.job_cur =
1359 h->job_count_permanent - hb_list_count( h->jobs );
1360 h->state.param.working.job_count = h->job_count_permanent;
1362 // Set which job is being worked on
1364 h->state.param.working.sequence_id = h->current_job->sequence_id;
1366 h->state.param.working.sequence_id = 0;
1368 hb_unlock( h->state_lock );
1369 hb_unlock( h->pause_lock );