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_lock_t *hb_avcodec_lock;
48 hb_work_object_t * hb_objects = NULL;
50 static void thread_func( void * );
52 void hb_avcodec_init()
54 hb_avcodec_lock = hb_lock_init();
58 int hb_avcodec_open(AVCodecContext *avctx, AVCodec *codec)
61 hb_lock( hb_avcodec_lock );
62 ret = avcodec_open(avctx, codec);
63 hb_unlock( hb_avcodec_lock );
67 int hb_avcodec_close(AVCodecContext *avctx)
70 hb_lock( hb_avcodec_lock );
71 ret = avcodec_close(avctx);
72 hb_unlock( hb_avcodec_lock );
77 * Registers work objects, by adding the work object to a liked list.
78 * @param w Handle to hb_work_object_t to register.
80 void hb_register( hb_work_object_t * w )
87 * libhb initialization routine.
88 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
89 * @param update_check signals libhb to check for updated version from HandBrake website.
90 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
92 hb_handle_t * hb_init_real( int verbose, int update_check )
94 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
97 /* See hb_deep_log() and hb_log() in common.c */
98 global_verbosity_level = verbose;
100 putenv( "HB_DEBUG=1" );
102 /* Check for an update on the website if asked to */
107 hb_log( "hb_init: checking for updates" );
108 date = hb_get_date();
109 h->update_thread = hb_update_init( &h->build, h->version );
113 if( hb_thread_has_exited( h->update_thread ) )
115 /* Immediate success or failure */
116 hb_thread_close( &h->update_thread );
119 if( hb_get_date() > date + 1000 )
121 /* Still nothing after one second. Connection problem,
122 let the thread die */
123 hb_log( "hb_init: connection problem, not waiting for "
132 * Initialise buffer pool
134 hb_buffer_pool_init();
136 /* CPU count detection */
137 hb_log( "hb_init: checking cpu count" );
138 h->cpu_count = hb_get_cpu_count();
140 h->list_title = hb_list_init();
141 h->jobs = hb_list_init();
143 h->state_lock = hb_lock_init();
144 h->state.state = HB_STATE_IDLE;
146 h->pause_lock = hb_lock_init();
151 /* Start library thread */
152 hb_log( "hb_init: starting libhb thread" );
154 h->main_thread = hb_thread_init( "libhb", thread_func, h,
155 HB_NORMAL_PRIORITY );
159 /* Set the scan count to start at 0 */
164 * libhb initialization routine.
165 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
166 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
167 * @param update_check signals libhb to check for updated version from HandBrake website.
168 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
170 hb_handle_t * hb_init_dl( int verbose, int update_check )
172 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
175 /* See hb_log() in common.c */
176 if( verbose > HB_DEBUG_NONE )
178 putenv( "HB_DEBUG=1" );
181 /* Check for an update on the website if asked to */
186 hb_log( "hb_init: checking for updates" );
187 date = hb_get_date();
188 h->update_thread = hb_update_init( &h->build, h->version );
192 if( hb_thread_has_exited( h->update_thread ) )
194 /* Immediate success or failure */
195 hb_thread_close( &h->update_thread );
198 if( hb_get_date() > date + 1000 )
200 /* Still nothing after one second. Connection problem,
201 let the thread die */
202 hb_log( "hb_init: connection problem, not waiting for "
210 /* CPU count detection */
211 hb_log( "hb_init: checking cpu count" );
212 h->cpu_count = hb_get_cpu_count();
214 h->list_title = hb_list_init();
215 h->jobs = hb_list_init();
216 h->current_job = NULL;
218 h->state_lock = hb_lock_init();
219 h->state.state = HB_STATE_IDLE;
221 h->pause_lock = hb_lock_init();
225 avcodec_register_all();
227 /* Start library thread */
228 hb_log( "hb_init: starting libhb thread" );
230 h->main_thread = hb_thread_init( "libhb", thread_func, h,
231 HB_NORMAL_PRIORITY );
233 hb_register( &hb_sync );
234 hb_register( &hb_decmpeg2 );
235 hb_register( &hb_decsub );
236 hb_register( &hb_render );
237 hb_register( &hb_encavcodec );
238 hb_register( &hb_encxvid );
239 hb_register( &hb_encx264 );
240 hb_register( &hb_enctheora );
241 hb_register( &hb_deca52 );
242 hb_register( &hb_decdca );
243 hb_register( &hb_decavcodec );
244 hb_register( &hb_decavcodecv );
245 hb_register( &hb_decavcodecvi );
246 hb_register( &hb_decavcodecai );
247 hb_register( &hb_declpcm );
248 hb_register( &hb_encfaac );
249 hb_register( &hb_enclame );
250 hb_register( &hb_encvorbis );
257 * Returns current version of libhb.
258 * @param h Handle to hb_handle_t.
259 * @return character array of version number.
261 char * hb_get_version( hb_handle_t * h )
267 * Returns current build of libhb.
268 * @param h Handle to hb_handle_t.
269 * @return character array of build number.
271 int hb_get_build( hb_handle_t * h )
277 * Checks for needed update.
278 * @param h Handle to hb_handle_t.
279 * @param version Pointer to handle where version will be copied.
280 * @return update indicator.
282 int hb_check_update( hb_handle_t * h, char ** version )
284 *version = ( h->build < 0 ) ? NULL : h->version;
289 * Sets the cpu count to the desired value.
290 * @param h Handle to hb_handle_t
291 * @param cpu_count Number of CPUs to use.
293 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
295 cpu_count = MAX( 1, cpu_count );
296 cpu_count = MIN( cpu_count, 8 );
297 h->cpu_count = cpu_count;
301 * Initializes a scan of the by calling hb_scan_init
302 * @param h Handle to hb_handle_t
303 * @param path location of VIDEO_TS folder.
304 * @param title_index Desired title to scan. 0 for all titles.
305 * @param preview_count Number of preview images to generate.
306 * @param store_previews Whether or not to write previews to disk.
308 void hb_scan( hb_handle_t * h, const char * path, int title_index,
309 int preview_count, int store_previews )
313 /* Clean up from previous scan */
314 while( ( title = hb_list_item( h->list_title, 0 ) ) )
316 hb_list_rem( h->list_title, title );
317 hb_title_close( &title );
320 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
321 h->scan_thread = hb_scan_init( h, path, title_index, h->list_title,
322 preview_count, store_previews );
326 * Returns the list of titles found.
327 * @param h Handle to hb_handle_t
328 * @return Handle to hb_list_t of the title list.
330 hb_list_t * hb_get_titles( hb_handle_t * h )
332 return h->list_title;
336 * Create preview image of desired title a index of picture.
337 * @param h Handle to hb_handle_t.
338 * @param title Handle to hb_title_t of desired title.
339 * @param picture Index in title.
340 * @param buffer Handle to buufer were inage will be drawn.
342 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
345 hb_job_t * job = title->job;
348 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
349 uint32_t * p32, swsflags;
350 AVPicture pic_in, pic_preview, pic_deint, pic_crop, pic_scale;
351 struct SwsContext * context;
353 int rgb_width = ((job->width + 7) >> 3) << 3;
356 swsflags = SWS_LANCZOS;
358 swsflags |= SWS_ACCURATE_RND;
359 #endif /* __x86_64__ */
361 buf1 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
362 buf2 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height ) );
363 buf3 = av_malloc( avpicture_get_size( PIX_FMT_YUV420P, job->width, job->height ) );
364 buf4 = av_malloc( avpicture_get_size( PIX_FMT_RGBA32, rgb_width, job->height ) );
365 avpicture_fill( &pic_in, buf1, PIX_FMT_YUV420P,
366 title->width, title->height );
367 avpicture_fill( &pic_deint, buf2, PIX_FMT_YUV420P,
368 title->width, title->height );
369 avpicture_fill( &pic_scale, buf3, PIX_FMT_YUV420P,
370 job->width, job->height );
371 avpicture_fill( &pic_preview, buf4, PIX_FMT_RGBA32,
372 rgb_width, job->height );
374 // Allocate the AVPicture frames and fill in
376 memset( filename, 0, 1024 );
378 hb_get_tempory_filename( h, filename, "%x%d",
379 (intptr_t) title, picture );
381 file = fopen( filename, "r" );
384 hb_log( "hb_get_preview: fopen failed" );
388 fread( buf1, avpicture_get_size( PIX_FMT_YUV420P, title->width, title->height), 1, file );
391 if( job->deinterlace )
393 // Deinterlace and crop
394 avpicture_deinterlace( &pic_deint, &pic_in, PIX_FMT_YUV420P, title->width, title->height );
395 av_picture_crop( &pic_crop, &pic_deint, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
400 av_picture_crop( &pic_crop, &pic_in, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
403 // Get scaling context
404 context = sws_getContext(title->width - (job->crop[2] + job->crop[3]),
405 title->height - (job->crop[0] + job->crop[1]),
407 job->width, job->height, PIX_FMT_YUV420P,
408 swsflags, NULL, NULL, NULL);
412 pic_crop.data, pic_crop.linesize,
413 0, title->height - (job->crop[0] + job->crop[1]),
414 pic_scale.data, pic_scale.linesize);
417 sws_freeContext( context );
419 // Get preview context
420 context = sws_getContext(rgb_width, job->height, PIX_FMT_YUV420P,
421 rgb_width, job->height, PIX_FMT_RGBA32,
422 swsflags, NULL, NULL, NULL);
426 pic_scale.data, pic_scale.linesize,
428 pic_preview.data, pic_preview.linesize);
431 sws_freeContext( context );
433 /* Gray background */
434 p32 = (uint32_t *) buffer;
435 for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
440 /* Draw the picture, centered, and draw the cropping zone */
441 preview_size = pic_preview.linesize[0];
442 pen = buffer + ( title->height - job->height ) *
443 ( title->width + 2 ) * 2 + ( title->width - job->width ) * 2;
444 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
445 pen += 4 * ( title->width + 2 );
446 for( i = 0; i < job->height; i++ )
449 nextLine = pen + 4 * ( title->width + 2 );
450 memset( pen, 0xFF, 4 );
452 memcpy( pen, buf4 + preview_size * i, 4 * job->width );
453 pen += 4 * job->width;
454 memset( pen, 0xFF, 4 );
457 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
460 avpicture_free( &pic_preview );
461 avpicture_free( &pic_scale );
462 avpicture_free( &pic_deint );
463 avpicture_free( &pic_in );
467 * Analyzes a frame to detect interlacing artifacts
468 * and returns true if interlacing (combing) is found.
470 * Code taken from Thomas Oestreich's 32detect filter
471 * in the Transcode project, with minor formatting changes.
473 * @param buf An hb_buffer structure holding valid frame data
474 * @param width The frame's width in pixels
475 * @param height The frame's height in pixels
476 * @param color_equal Sensitivity for detecting similar colors
477 * @param color_diff Sensitivity for detecting different colors
478 * @param threshold Sensitivity for flagging planes as combed
479 * @param prog_equal Sensitivity for detecting similar colors on progressive frames
480 * @param prog_diff Sensitivity for detecting different colors on progressive frames
481 * @param prog_threshold Sensitivity for flagging progressive frames as combed
483 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 )
485 int j, k, n, off, cc_1, cc_2, cc[3], flag[3] ;
486 uint16_t s1, s2, s3, s4;
491 if ( buf->flags & 16 )
493 /* Frame is progressive, be more discerning. */
494 color_diff = prog_diff;
495 color_equal = prog_equal;
496 threshold = prog_threshold;
499 /* One pas for Y, one pass for Cb, one pass for Cr */
500 for( k = 0; k < 3; k++ )
504 /* Y has already been checked, now offset by Y's dimensions
505 and divide all the other values by 2, since Cr and Cb
506 are half-size compared to Y. */
507 offset = width * height;
513 /* Y and Cb are done, so the offset needs to be bumped
514 so it's width*height + (width / 2) * (height / 2) */
518 for( j = 0; j < width; ++j )
522 for( n = 0; n < ( height - 4 ); n = n + 2 )
524 /* Look at groups of 4 sequential horizontal lines */
525 s1 = ( ( buf->data + offset )[ off + j ] & 0xff );
526 s2 = ( ( buf->data + offset )[ off + j + width ] & 0xff );
527 s3 = ( ( buf->data + offset )[ off + j + 2 * width ] & 0xff );
528 s4 = ( ( buf->data + offset )[ off + j + 3 * width ] & 0xff );
530 /* Note if the 1st and 2nd lines are more different in
531 color than the 1st and 3rd lines are similar in color.*/
532 if ( ( abs( s1 - s3 ) < color_equal ) &&
533 ( abs( s1 - s2 ) > color_diff ) )
536 /* Note if the 2nd and 3rd lines are more different in
537 color than the 2nd and 4th lines are similar in color.*/
538 if ( ( abs( s2 - s4 ) < color_equal ) &&
539 ( abs( s2 - s3 ) > color_diff) )
542 /* Now move down 2 horizontal lines before starting over.*/
548 /* The final cc score for a plane is the percentage of combed pixels it contains.
549 Because sensitivity goes down to hundreths of a percent, multiply by 1000
550 so it will be easy to compare against the threhold value which is an integer. */
551 cc[k] = (int)( ( cc_1 + cc_2 ) * 1000.0 / ( width * height ) );
555 /* HandBrake is all yuv420, so weight the average percentage of all 3 planes accordingly.*/
556 int average_cc = ( 2 * cc[0] + ( cc[1] / 2 ) + ( cc[2] / 2 ) ) / 3;
558 /* Now see if that average percentage of combed pixels surpasses the threshold percentage given by the user.*/
559 if( average_cc > threshold )
562 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" );
568 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" );
571 /* Reaching this point means no combing detected. */
577 * Calculates job width and height for anamorphic content,
579 * @param job Handle to hb_job_t
580 * @param output_width Pointer to returned storage width
581 * @param output_height Pointer to returned storage height
582 * @param output_par_width Pointer to returned pixel width
583 @ param output_par_height Pointer to returned pixel height
585 void hb_set_anamorphic_size( hb_job_t * job,
586 int *output_width, int *output_height,
587 int *output_par_width, int *output_par_height )
589 /* "Loose" anamorphic.
590 - Uses mod16-compliant dimensions,
591 - Allows users to set the width
592 - Handles ITU pixel aspects
595 /* Set up some variables to make the math easier to follow. */
596 hb_title_t * title = job->title;
597 int cropped_width = title->width - job->crop[2] - job->crop[3] ;
598 int cropped_height = title->height - job->crop[0] - job->crop[1] ;
599 double storage_aspect = (double)cropped_width / (double)cropped_height;
600 int width = job->width;
601 int height; // Gets set later, ignore user job->height value
602 int mod = job->modulus;
603 double aspect = title->aspect;
605 /* Gotta handle bounding dimensions differently
606 than for non-anamorphic encodes:
607 If the width is too big, just reset it with no rescaling.
608 Instead of using the aspect-scaled job height,
609 we need to see if the job width divided by the storage aspect
610 is bigger than the max. If so, set it to the max (this is sloppy).
611 If not, set job height to job width divided by storage aspect.
614 if ( job->maxWidth && (job->maxWidth < job->width) )
615 width = job->maxWidth;
617 height = ((double)width / storage_aspect) + 0.5;
618 if ( job->maxHeight && (job->maxHeight < height) )
619 height = job->maxHeight;
621 /* In case the user specified a modulus, use it */
627 /* Time to get picture dimensions that divide cleanly.*/
628 width = MULTIPLE_MOD( width, mod);
629 height = MULTIPLE_MOD( height, mod);
631 /* Verify these new dimensions don't violate max height and width settings */
632 if ( job->maxWidth && (job->maxWidth < job->width) )
633 width = job->maxWidth;
634 if ( job->maxHeight && (job->maxHeight < height) )
635 height = job->maxHeight;
637 int pixel_aspect_width = job->pixel_aspect_width;
638 int pixel_aspect_height = job->pixel_aspect_height;
640 /* If a source was really 704*480 and hard matted with cropping
641 to 720*480, replace the PAR values with the ITU broadcast ones. */
642 if (title->width == 720 && cropped_width <= 706)
644 // convert aspect to a scaled integer so we can test for 16:9 & 4:3
645 // aspect ratios ignoring insignificant differences in the LSBs of
646 // the floating point representation.
647 int iaspect = aspect * 9.;
649 /* Handle ITU PARs */
650 if (title->height == 480)
655 /* It's widescreen */
656 pixel_aspect_width = 40;
657 pixel_aspect_height = 33;
659 else if (iaspect == 12)
662 pixel_aspect_width = 10;
663 pixel_aspect_height = 11;
666 else if (title->height == 576)
671 /* It's widescreen */
672 pixel_aspect_width = 16;
673 pixel_aspect_height = 11;
675 else if (iaspect == 12)
678 pixel_aspect_width = 12;
679 pixel_aspect_height = 11;
684 /* Figure out what dimensions the source would display at. */
685 int source_display_width = cropped_width * (double)pixel_aspect_width /
686 (double)pixel_aspect_height ;
688 /* The film AR is the source's display width / cropped source height.
689 The output display width is the output height * film AR.
690 The output PAR is the output display width / output storage width. */
691 pixel_aspect_width = height * source_display_width / cropped_height;
692 pixel_aspect_height = width;
694 /* Pass the results back to the caller */
695 *output_width = width;
696 *output_height = height;
698 /* While x264 is smart enough to reduce fractions on its own, libavcodec
699 needs some help with the math, so lose superfluous factors. */
700 hb_reduce( output_par_width, output_par_height,
701 pixel_aspect_width, pixel_aspect_height );
705 * Calculates job width, height, and cropping parameters.
706 * @param job Handle to hb_job_t.
707 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
708 * @param pixels Maximum desired pixel count.
710 void hb_set_size( hb_job_t * job, double aspect, int pixels )
712 hb_title_t * title = job->title;
714 int croppedWidth = title->width - title->crop[2] - title->crop[3];
715 int croppedHeight = title->height - title->crop[0] - title->crop[1];
716 double croppedAspect = title->aspect * title->height * croppedWidth /
717 croppedHeight / title->width;
723 /* Keep the best possible aspect ratio */
724 aspect = croppedAspect;
727 /* Crop if necessary to obtain the desired ratio */
728 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
729 if( aspect < croppedAspect )
731 /* Need to crop on the left and right */
732 addCrop = croppedWidth - aspect * croppedHeight * title->width /
733 title->aspect / title->height;
736 addCrop = ( addCrop + 1 ) / 2;
737 job->crop[2] += addCrop;
738 job->crop[3] += addCrop;
740 else if( addCrop & 2 )
743 job->crop[2] += addCrop - 1;
744 job->crop[3] += addCrop + 1;
749 job->crop[2] += addCrop;
750 job->crop[3] += addCrop;
753 else if( aspect > croppedAspect )
755 /* Need to crop on the top and bottom */
756 addCrop = croppedHeight - croppedWidth * title->aspect *
757 title->height / aspect / title->width;
760 addCrop = ( addCrop + 1 ) / 2;
761 job->crop[0] += addCrop;
762 job->crop[1] += addCrop;
764 else if( addCrop & 2 )
767 job->crop[0] += addCrop - 1;
768 job->crop[1] += addCrop + 1;
773 job->crop[0] += addCrop;
774 job->crop[1] += addCrop;
778 /* Compute a resolution from the number of pixels and aspect */
782 h = MULTIPLE_16( (int)( (double)w / aspect ) );
790 job->height = MULTIPLE_16( (int)( (double)job->width / aspect ) );
794 * Returns the number of jobs in the queue.
795 * @param h Handle to hb_handle_t.
796 * @return Number of jobs.
798 int hb_count( hb_handle_t * h )
800 return hb_list_count( h->jobs );
804 * Returns handle to job at index i within the job list.
805 * @param h Handle to hb_handle_t.
806 * @param i Index of job.
807 * @returns Handle to hb_job_t of desired job.
809 hb_job_t * hb_job( hb_handle_t * h, int i )
811 return hb_list_item( h->jobs, i );
814 hb_job_t * hb_current_job( hb_handle_t * h )
816 return( h->current_job );
820 * Adds a job to the job list.
821 * @param h Handle to hb_handle_t.
822 * @param job Handle to hb_job_t.
824 void hb_add( hb_handle_t * h, hb_job_t * job )
827 hb_title_t * title, * title_copy;
828 hb_chapter_t * chapter, * chapter_copy;
830 hb_subtitle_t * subtitle, * subtitle_copy;
836 title_copy = malloc( sizeof( hb_title_t ) );
837 memcpy( title_copy, title, sizeof( hb_title_t ) );
839 title_copy->list_chapter = hb_list_init();
840 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
842 chapter = hb_list_item( title->list_chapter, i );
843 chapter_copy = malloc( sizeof( hb_chapter_t ) );
844 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
845 hb_list_add( title_copy->list_chapter, chapter_copy );
851 if( title->metadata )
853 title_copy->metadata = malloc( sizeof( hb_metadata_t ) );
855 if( title_copy->metadata )
857 memcpy( title_copy->metadata, title->metadata, sizeof( hb_metadata_t ) );
860 * Need to copy the artwork seperatly (TODO).
862 if( title->metadata->coverart )
864 title_copy->metadata->coverart = malloc( title->metadata->coverart_size );
865 if( title_copy->metadata->coverart )
867 memcpy( title_copy->metadata->coverart, title->metadata->coverart,
868 title->metadata->coverart_size );
870 title_copy->metadata->coverart_size = 0;
876 /* Copy the audio track(s) we want */
877 title_copy->list_audio = hb_list_init();
879 for( i = 0; i < hb_list_count(job->list_audio); i++ )
881 if( ( audio = hb_list_item( job->list_audio, i ) ) )
883 hb_list_add( title_copy->list_audio, hb_audio_copy(audio) );
887 title_copy->list_subtitle = hb_list_init();
890 * The following code is confusing, there are three ways in which
891 * we select subtitles and it depends on whether this is single or
894 * subtitle_scan may be enabled, in which case the first pass
895 * scans all subtitles of that language. The second pass does not
896 * select any because they are set at the end of the first pass.
898 * native_language may have a preferred language, in which case we
899 * may be switching the language we want for the subtitles in the
900 * first pass of a single pass, or the second pass of a two pass.
902 * We may have manually selected a subtitle, in which case that is
903 * selected in the first pass of a single pass, or the second of a
906 memset( audio_lang, 0, sizeof( audio_lang ) );
908 if ( job->indepth_scan || job->native_language ) {
911 * Find the first audio language that is being encoded
913 for( i = 0; i < hb_list_count(job->list_audio); i++ )
915 if( ( audio = hb_list_item( job->list_audio, i ) ) )
917 strncpy(audio_lang, audio->config.lang.iso639_2, sizeof(audio_lang));
923 * In all cases switch the language if we need to to our native
926 if( job->native_language )
928 if( strncasecmp( job->native_language, audio_lang,
929 sizeof( audio_lang ) ) != 0 )
934 hb_log( "Enabled subtitles in native language '%s', audio is in '%s'",
935 job->native_language, audio_lang);
938 * The main audio track is not in our native language, so switch
939 * the subtitles to use our native language instead.
941 strncpy( audio_lang, job->native_language, sizeof( audio_lang ) );
944 * native language is irrelevent, free it.
946 free( job->native_language );
947 job->native_language = NULL;
953 * If doing a subtitle scan then add all the matching subtitles for this
956 if ( job->indepth_scan )
958 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
960 subtitle = hb_list_item( title->list_subtitle, i );
961 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
964 * Matched subtitle language with audio language, so
965 * add this to our list to scan.
967 * We will update the subtitle list on the second pass
968 * later after the first pass has completed.
970 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
971 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
972 hb_list_add( title_copy->list_subtitle, subtitle_copy );
973 if ( job->native_language ) {
975 * With native language just select the
976 * first match in our langiage, not all of
977 * them. Subsequent ones are likely to be commentary
985 * Not doing a subtitle scan in this pass, but maybe we are in the
988 if( job->select_subtitle )
991 * Don't add subtitles here, we'll add them via select_subtitle
992 * at the end of the subtitle_scan.
996 * Definitely not doing a subtitle scan.
998 if( job->pass != 1 && job->native_language )
1001 * We are not doing a subtitle scan but do want the
1002 * native langauge subtitle selected, so select it
1003 * for pass 0 or pass 2 of a two pass.
1005 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
1007 subtitle = hb_list_item( title->list_subtitle, i );
1008 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
1011 * Matched subtitle language with audio language, so
1012 * add this to our list to scan.
1014 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1015 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1016 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1022 * Manually selected subtitle, in which case only
1023 * bother adding them for pass 0 or pass 2 of a two
1026 if( job->pass != 1 )
1028 if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) )
1030 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1031 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1032 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1040 job_copy = calloc( sizeof( hb_job_t ), 1 );
1041 memcpy( job_copy, job, sizeof( hb_job_t ) );
1042 title_copy->job = job_copy;
1043 job_copy->title = title_copy;
1044 job_copy->list_audio = title_copy->list_audio;
1045 job_copy->file = strdup( job->file );
1047 job_copy->pause = h->pause_lock;
1049 /* Copy the job filter list */
1053 int filter_count = hb_list_count( job->filters );
1054 job_copy->filters = hb_list_init();
1055 for( i = 0; i < filter_count; i++ )
1058 * Copy the filters, since the MacGui reuses the global filter objects
1059 * meaning that queued up jobs overwrite the previous filter settings.
1060 * In reality, settings is probably the only field that needs duplicating
1061 * since it's the only value that is ever changed. But name is duplicated
1062 * as well for completeness. Not copying private_data since it gets
1063 * created for each job in renderInit.
1065 hb_filter_object_t * filter = hb_list_item( job->filters, i );
1066 hb_filter_object_t * filter_copy = malloc( sizeof( hb_filter_object_t ) );
1067 memcpy( filter_copy, filter, sizeof( hb_filter_object_t ) );
1069 filter_copy->name = strdup( filter->name );
1070 if( filter->settings )
1071 filter_copy->settings = strdup( filter->settings );
1072 hb_list_add( job_copy->filters, filter_copy );
1076 /* Add the job to the list */
1077 hb_list_add( h->jobs, job_copy );
1078 h->job_count = hb_count(h);
1079 h->job_count_permanent++;
1083 * Removes a job from the job list.
1084 * @param h Handle to hb_handle_t.
1085 * @param job Handle to hb_job_t.
1087 void hb_rem( hb_handle_t * h, hb_job_t * job )
1089 hb_list_rem( h->jobs, job );
1091 h->job_count = hb_count(h);
1092 if (h->job_count_permanent)
1093 h->job_count_permanent--;
1095 /* XXX free everything XXX */
1099 * Starts the conversion process.
1100 * Sets state to HB_STATE_WORKING.
1101 * calls hb_work_init, to launch work thread. Stores handle to work thread.
1102 * @param h Handle to hb_handle_t.
1104 void hb_start( hb_handle_t * h )
1107 h->job_count = hb_list_count( h->jobs );
1108 h->job_count_permanent = h->job_count;
1110 hb_lock( h->state_lock );
1111 h->state.state = HB_STATE_WORKING;
1112 #define p h->state.param.working
1115 p.job_count = h->job_count;
1123 hb_unlock( h->state_lock );
1128 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
1129 &h->work_die, &h->work_error, &h->current_job );
1133 * Pauses the conversion process.
1134 * @param h Handle to hb_handle_t.
1136 void hb_pause( hb_handle_t * h )
1140 hb_lock( h->pause_lock );
1143 hb_lock( h->state_lock );
1144 h->state.state = HB_STATE_PAUSED;
1145 hb_unlock( h->state_lock );
1150 * Resumes the conversion process.
1151 * @param h Handle to hb_handle_t.
1153 void hb_resume( hb_handle_t * h )
1157 hb_unlock( h->pause_lock );
1163 * Stops the conversion process.
1164 * @param h Handle to hb_handle_t.
1166 void hb_stop( hb_handle_t * h )
1170 h->job_count = hb_count(h);
1171 h->job_count_permanent = 0;
1177 * Returns the state of the conversion process.
1178 * @param h Handle to hb_handle_t.
1179 * @param s Handle to hb_state_t which to copy the state data.
1181 void hb_get_state( hb_handle_t * h, hb_state_t * s )
1183 hb_lock( h->state_lock );
1185 memcpy( s, &h->state, sizeof( hb_state_t ) );
1186 if ( h->state.state == HB_STATE_SCANDONE || h->state.state == HB_STATE_WORKDONE )
1187 h->state.state = HB_STATE_IDLE;
1189 hb_unlock( h->state_lock );
1192 void hb_get_state2( hb_handle_t * h, hb_state_t * s )
1194 hb_lock( h->state_lock );
1196 memcpy( s, &h->state, sizeof( hb_state_t ) );
1198 hb_unlock( h->state_lock );
1202 * Called in MacGui in UpdateUI to check
1203 * for a new scan being completed to set a new source
1205 int hb_get_scancount( hb_handle_t * h)
1207 return h->scanCount;
1211 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init_real.
1212 * @param _h Pointer to handle to hb_handle_t.
1214 void hb_close( hb_handle_t ** _h )
1216 hb_handle_t * h = *_h;
1220 hb_thread_close( &h->main_thread );
1222 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1224 hb_list_rem( h->list_title, title );
1225 if( title->job && title->job->filters )
1227 hb_list_close( &title->job->filters );
1230 hb_title_close( &title );
1232 hb_list_close( &h->list_title );
1234 hb_list_close( &h->jobs );
1235 hb_lock_close( &h->state_lock );
1236 hb_lock_close( &h->pause_lock );
1243 * Monitors the state of the update, scan, and work threads.
1244 * Sets scan done state when scan thread exits.
1245 * Sets work done state when work thread exits.
1246 * @param _h Handle to hb_handle_t
1248 static void thread_func( void * _h )
1250 hb_handle_t * h = (hb_handle_t *) _h;
1253 struct dirent * entry;
1257 /* Create folder for temporary files */
1258 memset( dirname, 0, 1024 );
1259 hb_get_tempory_directory( h, dirname );
1261 hb_mkdir( dirname );
1265 /* In case the check_update thread hangs, it'll die sooner or
1266 later. Then, we join it here */
1267 if( h->update_thread &&
1268 hb_thread_has_exited( h->update_thread ) )
1270 hb_thread_close( &h->update_thread );
1273 /* Check if the scan thread is done */
1274 if( h->scan_thread &&
1275 hb_thread_has_exited( h->scan_thread ) )
1277 hb_thread_close( &h->scan_thread );
1279 hb_log( "libhb: scan thread found %d valid title(s)",
1280 hb_list_count( h->list_title ) );
1281 hb_lock( h->state_lock );
1282 h->state.state = HB_STATE_SCANDONE; //originally state.state
1283 hb_unlock( h->state_lock );
1284 /*we increment this sessions scan count by one for the MacGui
1285 to trigger a new source being set */
1289 /* Check if the work thread is done */
1290 if( h->work_thread &&
1291 hb_thread_has_exited( h->work_thread ) )
1293 hb_thread_close( &h->work_thread );
1295 hb_log( "libhb: work result = %d",
1297 hb_lock( h->state_lock );
1298 h->state.state = HB_STATE_WORKDONE;
1299 h->state.param.workdone.error = h->work_error;
1301 h->job_count = hb_count(h);
1302 if (h->job_count < 1)
1303 h->job_count_permanent = 0;
1304 hb_unlock( h->state_lock );
1310 if( h->work_thread )
1313 hb_thread_close( &h->work_thread );
1316 /* Remove temp folder */
1317 dir = opendir( dirname );
1320 while( ( entry = readdir( dir ) ) )
1322 char filename[1024];
1323 if( entry->d_name[0] == '.' )
1327 memset( filename, 0, 1024 );
1328 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
1338 * @param h Handle to hb_handle_t
1340 int hb_get_pid( hb_handle_t * h )
1346 * Sets the current state.
1347 * @param h Handle to hb_handle_t
1348 * @param s Handle to new hb_state_t
1350 void hb_set_state( hb_handle_t * h, hb_state_t * s )
1352 hb_lock( h->pause_lock );
1353 hb_lock( h->state_lock );
1354 memcpy( &h->state, s, sizeof( hb_state_t ) );
1355 if( h->state.state == HB_STATE_WORKING )
1358 if (h->job_count < 1)
1359 h->job_count_permanent = 1;
1361 h->state.param.working.job_cur =
1362 h->job_count_permanent - hb_list_count( h->jobs );
1363 h->state.param.working.job_count = h->job_count_permanent;
1365 // Set which job is being worked on
1367 h->state.param.working.sequence_id = h->current_job->sequence_id;
1369 h->state.param.working.sequence_id = 0;
1371 hb_unlock( h->state_lock );
1372 hb_unlock( h->pause_lock );