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_log() in common.c */
73 if( verbose > HB_DEBUG_NONE )
75 putenv( "HB_DEBUG=1" );
76 av_log_set_level(AV_LOG_DEBUG);
79 /* Check for an update on the website if asked to */
84 hb_log( "hb_init: checking for updates" );
86 h->update_thread = hb_update_init( &h->build, h->version );
90 if( hb_thread_has_exited( h->update_thread ) )
92 /* Immediate success or failure */
93 hb_thread_close( &h->update_thread );
96 if( hb_get_date() > date + 1000 )
98 /* Still nothing after one second. Connection problem,
100 hb_log( "hb_init: connection problem, not waiting for "
109 * Initialise buffer pool
111 hb_buffer_pool_init();
113 /* CPU count detection */
114 hb_log( "hb_init: checking cpu count" );
115 h->cpu_count = hb_get_cpu_count();
117 h->list_title = hb_list_init();
118 h->jobs = hb_list_init();
120 h->state_lock = hb_lock_init();
121 h->state.state = HB_STATE_IDLE;
123 h->pause_lock = hb_lock_init();
128 /* Start library thread */
129 hb_log( "hb_init: starting libhb thread" );
131 h->main_thread = hb_thread_init( "libhb", thread_func, h,
132 HB_NORMAL_PRIORITY );
136 /* Set the scan count to start at 0 */
141 * libhb initialization routine.
142 * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
143 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
144 * @param update_check signals libhb to check for updated version from HandBrake website.
145 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
147 hb_handle_t * hb_init_dl( int verbose, int update_check )
149 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
152 /* See hb_log() in common.c */
153 if( verbose > HB_DEBUG_NONE )
155 putenv( "HB_DEBUG=1" );
156 av_log_set_level(AV_LOG_DEBUG);
159 /* Check for an update on the website if asked to */
164 hb_log( "hb_init: checking for updates" );
165 date = hb_get_date();
166 h->update_thread = hb_update_init( &h->build, h->version );
170 if( hb_thread_has_exited( h->update_thread ) )
172 /* Immediate success or failure */
173 hb_thread_close( &h->update_thread );
176 if( hb_get_date() > date + 1000 )
178 /* Still nothing after one second. Connection problem,
179 let the thread die */
180 hb_log( "hb_init: connection problem, not waiting for "
188 /* CPU count detection */
189 hb_log( "hb_init: checking cpu count" );
190 h->cpu_count = hb_get_cpu_count();
192 h->list_title = hb_list_init();
193 h->jobs = hb_list_init();
194 h->current_job = NULL;
196 h->state_lock = hb_lock_init();
197 h->state.state = HB_STATE_IDLE;
199 h->pause_lock = hb_lock_init();
203 avcodec_register_all();
205 /* Start library thread */
206 hb_log( "hb_init: starting libhb thread" );
208 h->main_thread = hb_thread_init( "libhb", thread_func, h,
209 HB_NORMAL_PRIORITY );
211 hb_register( &hb_sync );
212 hb_register( &hb_decmpeg2 );
213 hb_register( &hb_decsub );
214 hb_register( &hb_render );
215 hb_register( &hb_encavcodec );
216 hb_register( &hb_encxvid );
217 hb_register( &hb_encx264 );
218 hb_register( &hb_enctheora );
219 hb_register( &hb_deca52 );
220 hb_register( &hb_decdca );
221 hb_register( &hb_decavcodec );
222 hb_register( &hb_decavcodecv );
223 hb_register( &hb_decavcodecvi );
224 hb_register( &hb_decavcodecai );
225 hb_register( &hb_declpcm );
226 hb_register( &hb_encfaac );
227 hb_register( &hb_enclame );
228 hb_register( &hb_encvorbis );
235 * Returns current version of libhb.
236 * @param h Handle to hb_handle_t.
237 * @return character array of version number.
239 char * hb_get_version( hb_handle_t * h )
245 * Returns current build of libhb.
246 * @param h Handle to hb_handle_t.
247 * @return character array of build number.
249 int hb_get_build( hb_handle_t * h )
255 * Checks for needed update.
256 * @param h Handle to hb_handle_t.
257 * @param version Pointer to handle where version will be copied.
258 * @return update indicator.
260 int hb_check_update( hb_handle_t * h, char ** version )
262 *version = ( h->build < 0 ) ? NULL : h->version;
267 * Sets the cpu count to the desired value.
268 * @param h Handle to hb_handle_t
269 * @param cpu_count Number of CPUs to use.
271 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
273 cpu_count = MAX( 1, cpu_count );
274 cpu_count = MIN( cpu_count, 8 );
275 h->cpu_count = cpu_count;
279 * Initializes a scan of the by calling hb_scan_init
280 * @param h Handle to hb_handle_t
281 * @param path location of VIDEO_TS folder.
282 * @param title_index Desired title to scan. 0 for all titles.
284 void hb_scan( hb_handle_t * h, const char * path, int title_index )
288 /* Clean up from previous scan */
289 while( ( title = hb_list_item( h->list_title, 0 ) ) )
291 hb_list_rem( h->list_title, title );
292 hb_title_close( &title );
295 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
296 h->scan_thread = hb_scan_init( h, path, title_index, h->list_title );
300 * Returns the list of titles found.
301 * @param h Handle to hb_handle_t
302 * @return Handle to hb_list_t of the title list.
304 hb_list_t * hb_get_titles( hb_handle_t * h )
306 return h->list_title;
310 * Create preview image of desired title a index of picture.
311 * @param h Handle to hb_handle_t.
312 * @param title Handle to hb_title_t of desired title.
313 * @param picture Index in title.
314 * @param buffer Handle to buufer were inage will be drawn.
316 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
319 hb_job_t * job = title->job;
322 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
323 uint32_t * p32, swsflags;
324 AVPicture pic_in, pic_preview, pic_deint, pic_crop, pic_scale;
325 struct SwsContext * context;
328 swsflags = SWS_LANCZOS;
330 swsflags |= SWS_ACCURATE_RND;
331 #endif /* __x86_64__ */
333 buf1 = malloc( title->width * title->height * 3 / 2 );
334 buf2 = malloc( title->width * title->height * 3 / 2 );
335 buf3 = malloc( title->width * title->height * 3 / 2 );
336 buf4 = malloc( title->width * title->height * 4 );
337 avpicture_fill( &pic_in, buf1, PIX_FMT_YUV420P,
338 title->width, title->height );
339 avpicture_fill( &pic_deint, buf2, PIX_FMT_YUV420P,
340 title->width, title->height );
341 avpicture_fill( &pic_scale, buf3, PIX_FMT_YUV420P,
342 job->width, job->height );
343 avpicture_fill( &pic_preview, buf4, PIX_FMT_RGBA32,
344 job->width, job->height );
346 // Allocate the AVPicture frames and fill in
348 memset( filename, 0, 1024 );
350 hb_get_tempory_filename( h, filename, "%x%d",
351 (intptr_t) title, picture );
353 file = fopen( filename, "r" );
356 hb_log( "hb_get_preview: fopen failed" );
360 fread( buf1, title->width * title->height * 3 / 2, 1, file );
363 if( job->deinterlace )
365 // Deinterlace and crop
366 avpicture_deinterlace( &pic_deint, &pic_in, PIX_FMT_YUV420P, title->width, title->height );
367 av_picture_crop( &pic_crop, &pic_deint, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
372 av_picture_crop( &pic_crop, &pic_in, PIX_FMT_YUV420P, job->crop[0], job->crop[2] );
375 // Get scaling context
376 context = sws_getContext(title->width - (job->crop[2] + job->crop[3]),
377 title->height - (job->crop[0] + job->crop[1]),
379 job->width, job->height, PIX_FMT_YUV420P,
380 swsflags, NULL, NULL, NULL);
384 pic_crop.data, pic_crop.linesize,
385 0, title->height - (job->crop[0] + job->crop[1]),
386 pic_scale.data, pic_scale.linesize);
389 sws_freeContext( context );
391 // Get preview context
392 context = sws_getContext(job->width, job->height, PIX_FMT_YUV420P,
393 job->width, job->height, PIX_FMT_RGBA32,
394 swsflags, NULL, NULL, NULL);
398 pic_scale.data, pic_scale.linesize,
400 pic_preview.data, pic_preview.linesize);
403 sws_freeContext( context );
405 /* Gray background */
406 p32 = (uint32_t *) buffer;
407 for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
412 /* Draw the picture, centered, and draw the cropping zone */
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 + 4 * job->width * 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 int storage_aspect = cropped_width * 10000 / cropped_height;
571 int width = job->width;
572 int height; // Gets set later, ignore user value
573 int mod = job->modulus;
574 int 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 if ( job->maxHeight && (job->maxHeight < (width / storage_aspect * 10000)) )
590 height = job->maxHeight;
594 height = width * 10000 / storage_aspect;
598 /* Time to get picture dimensions that divide cleanly.
599 These variables will store temporary dimensions as we iterate. */
602 /* In case the user specified a modulus, use it */
608 /* Iterate through multiples of mod to find one close to job->width. */
615 if ( ( width - w ) <= ( mod / 2 ) )
616 /* We'll take a width that's
617 smaller, but close enough. */
621 /* Mod 16 dimensions, how nice! */
625 if ( ( w - width ) < (mod/2) )
626 /* We'll take a width that's bigger, if we have to. */
632 /* Now do the same for a mod-friendly value near job->height. */
639 if ( ( height - h ) <= ( mod / 2 ))
640 /* Go with a smaller height,
641 if it's close enough. */
645 /* Mod 16 dimensions, how nice! */
650 if ( ( h - height ) < ( mod / 2 ))
651 /* Use a taller height if necessary */
657 int pixel_aspect_width = job->pixel_aspect_width;
658 int pixel_aspect_height = job->pixel_aspect_height;
660 /* If a source was really 704*480 and hard matted with cropping
661 to 720*480, replace the PAR values with the ITU broadcast ones. */
662 if (cropped_width <= 706)
664 /* Handle ITU PARs */
665 if (title->height == 480)
670 /* It's widescreen */
671 pixel_aspect_width = 40;
672 pixel_aspect_height = 33;
674 else if (aspect == 12)
677 pixel_aspect_width = 10;
678 pixel_aspect_height = 11;
681 else if (title->height == 576)
686 /* It's widescreen */
687 pixel_aspect_width = 16;
688 pixel_aspect_height = 11;
690 else if (aspect == 12)
693 pixel_aspect_width = 12;
694 pixel_aspect_height = 11;
699 /* Figure out what dimensions the source would display at. */
700 int source_display_width = cropped_width * ((float)pixel_aspect_width / (float)pixel_aspect_height) ;
702 /* The film AR is the source's display width / cropped source height.
703 The output display width is the output height * film AR.
704 The output PAR is the output display width / output storage width. */
705 pixel_aspect_width = height * source_display_width / cropped_height;
706 pixel_aspect_height = width;
708 /* While x264 is smart enough to reduce fractions on its own, libavcodec
709 needs some help with the math, so lose superfluous factors. */
710 hb_reduce( &pixel_aspect_width, &pixel_aspect_height,
711 pixel_aspect_width, pixel_aspect_height );
713 /* Pass the results back to the caller */
714 *output_width = width;
715 *output_height = height;
716 *output_par_width = pixel_aspect_width;
717 *output_par_height = pixel_aspect_height;
721 * Calculates job width, height, and cropping parameters.
722 * @param job Handle to hb_job_t.
723 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
724 * @param pixels Maximum desired pixel count.
726 void hb_set_size( hb_job_t * job, int aspect, int pixels )
728 hb_title_t * title = job->title;
730 int croppedWidth = title->width - title->crop[2] - title->crop[3];
731 int croppedHeight = title->height - title->crop[0] - title->crop[1];
732 int croppedAspect = title->aspect * title->height * croppedWidth /
733 croppedHeight / title->width;
739 /* Keep the best possible aspect ratio */
740 aspect = croppedAspect;
743 /* Crop if necessary to obtain the desired ratio */
744 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
745 if( aspect < croppedAspect )
747 /* Need to crop on the left and right */
748 addCrop = croppedWidth - aspect * croppedHeight * title->width /
749 title->aspect / title->height;
752 addCrop = ( addCrop + 1 ) / 2;
753 job->crop[2] += addCrop;
754 job->crop[3] += addCrop;
756 else if( addCrop & 2 )
759 job->crop[2] += addCrop - 1;
760 job->crop[3] += addCrop + 1;
765 job->crop[2] += addCrop;
766 job->crop[3] += addCrop;
769 else if( aspect > croppedAspect )
771 /* Need to crop on the top and bottom */
772 addCrop = croppedHeight - croppedWidth * title->aspect *
773 title->height / aspect / title->width;
776 addCrop = ( addCrop + 1 ) / 2;
777 job->crop[0] += addCrop;
778 job->crop[1] += addCrop;
780 else if( addCrop & 2 )
783 job->crop[0] += addCrop - 1;
784 job->crop[1] += addCrop + 1;
789 job->crop[0] += addCrop;
790 job->crop[1] += addCrop;
794 /* Compute a resolution from the number of pixels and aspect */
798 h = MULTIPLE_16( w * HB_ASPECT_BASE / aspect );
806 job->height = MULTIPLE_16( 16 * i * HB_ASPECT_BASE / aspect );
810 * Returns the number of jobs in the queue.
811 * @param h Handle to hb_handle_t.
812 * @return Number of jobs.
814 int hb_count( hb_handle_t * h )
816 return hb_list_count( h->jobs );
820 * Returns handle to job at index i within the job list.
821 * @param h Handle to hb_handle_t.
822 * @param i Index of job.
823 * @returns Handle to hb_job_t of desired job.
825 hb_job_t * hb_job( hb_handle_t * h, int i )
827 return hb_list_item( h->jobs, i );
830 hb_job_t * hb_current_job( hb_handle_t * h )
832 return( h->current_job );
836 * Adds a job to the job list.
837 * @param h Handle to hb_handle_t.
838 * @param job Handle to hb_job_t.
840 void hb_add( hb_handle_t * h, hb_job_t * job )
843 hb_title_t * title, * title_copy;
844 hb_chapter_t * chapter, * chapter_copy;
846 hb_subtitle_t * subtitle, * subtitle_copy;
852 title_copy = malloc( sizeof( hb_title_t ) );
853 memcpy( title_copy, title, sizeof( hb_title_t ) );
855 title_copy->list_chapter = hb_list_init();
856 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
858 chapter = hb_list_item( title->list_chapter, i );
859 chapter_copy = malloc( sizeof( hb_chapter_t ) );
860 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
861 hb_list_add( title_copy->list_chapter, chapter_copy );
864 /* Copy the audio track(s) we want */
865 title_copy->list_audio = hb_list_init();
867 /* Do nothing about audio during first pass */
868 if( job->pass == 0 || job->pass == 2 )
870 for( i = 0; i < hb_list_count(job->list_audio); i++ )
872 if( ( audio = hb_list_item( job->list_audio, i ) ) )
874 hb_list_add( title_copy->list_audio, hb_audio_copy(audio) );
879 title_copy->list_subtitle = hb_list_init();
882 * The following code is confusing, there are three ways in which
883 * we select subtitles and it depends on whether this is single or
886 * subtitle_scan may be enabled, in which case the first pass
887 * scans all subtitles of that language. The second pass does not
888 * select any because they are set at the end of the first pass.
890 * native_language may have a preferred language, in which case we
891 * may be switching the language we want for the subtitles in the
892 * first pass of a single pass, or the second pass of a two pass.
894 * We may have manually selected a subtitle, in which case that is
895 * selected in the first pass of a single pass, or the second of a
898 memset( audio_lang, 0, sizeof( audio_lang ) );
900 if ( job->indepth_scan || job->native_language ) {
903 * Find the first audio language that is being encoded
905 for( i = 0; i < hb_list_count(job->list_audio); i++ )
907 if( ( audio = hb_list_item( job->list_audio, i ) ) )
909 strncpy(audio_lang, audio->config.lang.iso639_2, sizeof(audio_lang));
915 * In all cases switch the language if we need to to our native
918 if( job->native_language )
920 if( strncasecmp( job->native_language, audio_lang,
921 sizeof( audio_lang ) ) != 0 )
926 hb_log( "Enabled subtitles in native language '%s', audio is in '%s'",
927 job->native_language, audio_lang);
930 * The main audio track is not in our native language, so switch
931 * the subtitles to use our native language instead.
933 strncpy( audio_lang, job->native_language, sizeof( audio_lang ) );
936 * native language is irrelevent, free it.
938 free( job->native_language );
939 job->native_language = NULL;
945 * If doing a subtitle scan then add all the matching subtitles for this
948 if ( job->indepth_scan )
950 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
952 subtitle = hb_list_item( title->list_subtitle, i );
953 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
956 * Matched subtitle language with audio language, so
957 * add this to our list to scan.
959 * We will update the subtitle list on the second pass
960 * later after the first pass has completed.
962 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
963 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
964 hb_list_add( title_copy->list_subtitle, subtitle_copy );
965 if ( job->native_language ) {
967 * With native language just select the
968 * first match in our langiage, not all of
969 * them. Subsequent ones are likely to be commentary
977 * Not doing a subtitle scan in this pass, but maybe we are in the
980 if( job->select_subtitle )
983 * Don't add subtitles here, we'll add them via select_subtitle
984 * at the end of the subtitle_scan.
988 * Definitely not doing a subtitle scan.
990 if( job->pass != 1 && job->native_language )
993 * We are not doing a subtitle scan but do want the
994 * native langauge subtitle selected, so select it
995 * for pass 0 or pass 2 of a two pass.
997 for( i=0; i < hb_list_count( title->list_subtitle ); i++ )
999 subtitle = hb_list_item( title->list_subtitle, i );
1000 if( strcmp( subtitle->iso639_2, audio_lang ) == 0 )
1003 * Matched subtitle language with audio language, so
1004 * add this to our list to scan.
1006 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1007 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1008 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1014 * Manually selected subtitle, in which case only
1015 * bother adding them for pass 0 or pass 2 of a two
1018 if( job->pass != 1 )
1020 if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) )
1022 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
1023 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
1024 hb_list_add( title_copy->list_subtitle, subtitle_copy );
1032 job_copy = calloc( sizeof( hb_job_t ), 1 );
1033 memcpy( job_copy, job, sizeof( hb_job_t ) );
1034 title_copy->job = job_copy;
1035 job_copy->title = title_copy;
1036 job_copy->list_audio = title_copy->list_audio;
1037 job_copy->file = strdup( job->file );
1039 job_copy->pause = h->pause_lock;
1041 /* Copy the job filter list */
1045 int filter_count = hb_list_count( job->filters );
1046 job_copy->filters = hb_list_init();
1047 for( i = 0; i < filter_count; i++ )
1050 * Copy the filters, since the MacGui reuses the global filter objects
1051 * meaning that queued up jobs overwrite the previous filter settings.
1052 * In reality, settings is probably the only field that needs duplicating
1053 * since it's the only value that is ever changed. But name is duplicated
1054 * as well for completeness. Not copying private_data since it gets
1055 * created for each job in renderInit.
1057 hb_filter_object_t * filter = hb_list_item( job->filters, i );
1058 hb_filter_object_t * filter_copy = malloc( sizeof( hb_filter_object_t ) );
1059 memcpy( filter_copy, filter, sizeof( hb_filter_object_t ) );
1061 filter_copy->name = strdup( filter->name );
1062 if( filter->settings )
1063 filter_copy->settings = strdup( filter->settings );
1064 hb_list_add( job_copy->filters, filter_copy );
1068 /* Add the job to the list */
1069 hb_list_add( h->jobs, job_copy );
1070 h->job_count = hb_count(h);
1071 h->job_count_permanent++;
1075 * Removes a job from the job list.
1076 * @param h Handle to hb_handle_t.
1077 * @param job Handle to hb_job_t.
1079 void hb_rem( hb_handle_t * h, hb_job_t * job )
1081 hb_list_rem( h->jobs, job );
1083 h->job_count = hb_count(h);
1084 if (h->job_count_permanent)
1085 h->job_count_permanent--;
1087 /* XXX free everything XXX */
1091 * Starts the conversion process.
1092 * Sets state to HB_STATE_WORKING.
1093 * calls hb_work_init, to launch work thread. Stores handle to work thread.
1094 * @param h Handle to hb_handle_t.
1096 void hb_start( hb_handle_t * h )
1099 h->job_count = hb_list_count( h->jobs );
1100 h->job_count_permanent = h->job_count;
1102 hb_lock( h->state_lock );
1103 h->state.state = HB_STATE_WORKING;
1104 #define p h->state.param.working
1107 p.job_count = h->job_count;
1115 hb_unlock( h->state_lock );
1120 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
1121 &h->work_die, &h->work_error, &h->current_job );
1125 * Pauses the conversion process.
1126 * @param h Handle to hb_handle_t.
1128 void hb_pause( hb_handle_t * h )
1132 hb_lock( h->pause_lock );
1135 hb_lock( h->state_lock );
1136 h->state.state = HB_STATE_PAUSED;
1137 hb_unlock( h->state_lock );
1142 * Resumes the conversion process.
1143 * @param h Handle to hb_handle_t.
1145 void hb_resume( hb_handle_t * h )
1149 hb_unlock( h->pause_lock );
1155 * Stops the conversion process.
1156 * @param h Handle to hb_handle_t.
1158 void hb_stop( hb_handle_t * h )
1162 h->job_count = hb_count(h);
1163 h->job_count_permanent = 0;
1169 * Returns the state of the conversion process.
1170 * @param h Handle to hb_handle_t.
1171 * @param s Handle to hb_state_t which to copy the state data.
1173 void hb_get_state( hb_handle_t * h, hb_state_t * s )
1175 hb_lock( h->state_lock );
1177 memcpy( s, &h->state, sizeof( hb_state_t ) );
1178 if ( h->state.state == HB_STATE_SCANDONE || h->state.state == HB_STATE_WORKDONE )
1179 h->state.state = HB_STATE_IDLE;
1181 hb_unlock( h->state_lock );
1184 void hb_get_state2( hb_handle_t * h, hb_state_t * s )
1186 hb_lock( h->state_lock );
1188 memcpy( s, &h->state, sizeof( hb_state_t ) );
1190 hb_unlock( h->state_lock );
1194 * Called in MacGui in UpdateUI to check
1195 * for a new scan being completed to set a new source
1197 int hb_get_scancount( hb_handle_t * h)
1199 return h->scanCount;
1203 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init_real.
1204 * @param _h Pointer to handle to hb_handle_t.
1206 void hb_close( hb_handle_t ** _h )
1208 hb_handle_t * h = *_h;
1212 hb_thread_close( &h->main_thread );
1214 while( ( title = hb_list_item( h->list_title, 0 ) ) )
1216 hb_list_rem( h->list_title, title );
1217 if( title->job && title->job->filters )
1219 hb_list_close( &title->job->filters );
1222 hb_title_close( &title );
1224 hb_list_close( &h->list_title );
1226 hb_list_close( &h->jobs );
1227 hb_lock_close( &h->state_lock );
1228 hb_lock_close( &h->pause_lock );
1235 * Monitors the state of the update, scan, and work threads.
1236 * Sets scan done state when scan thread exits.
1237 * Sets work done state when work thread exits.
1238 * @param _h Handle to hb_handle_t
1240 static void thread_func( void * _h )
1242 hb_handle_t * h = (hb_handle_t *) _h;
1245 struct dirent * entry;
1249 /* Create folder for temporary files */
1250 memset( dirname, 0, 1024 );
1251 hb_get_tempory_directory( h, dirname );
1253 hb_mkdir( dirname );
1257 /* In case the check_update thread hangs, it'll die sooner or
1258 later. Then, we join it here */
1259 if( h->update_thread &&
1260 hb_thread_has_exited( h->update_thread ) )
1262 hb_thread_close( &h->update_thread );
1265 /* Check if the scan thread is done */
1266 if( h->scan_thread &&
1267 hb_thread_has_exited( h->scan_thread ) )
1269 hb_thread_close( &h->scan_thread );
1271 hb_log( "libhb: scan thread found %d valid title(s)",
1272 hb_list_count( h->list_title ) );
1273 hb_lock( h->state_lock );
1274 h->state.state = HB_STATE_SCANDONE; //originally state.state
1275 hb_unlock( h->state_lock );
1276 /*we increment this sessions scan count by one for the MacGui
1277 to trigger a new source being set */
1281 /* Check if the work thread is done */
1282 if( h->work_thread &&
1283 hb_thread_has_exited( h->work_thread ) )
1285 hb_thread_close( &h->work_thread );
1287 hb_log( "libhb: work result = %d",
1289 hb_lock( h->state_lock );
1290 h->state.state = HB_STATE_WORKDONE;
1291 h->state.param.workdone.error = h->work_error;
1293 h->job_count = hb_count(h);
1294 if (h->job_count < 1)
1295 h->job_count_permanent = 0;
1296 hb_unlock( h->state_lock );
1302 if( h->work_thread )
1305 hb_thread_close( &h->work_thread );
1308 /* Remove temp folder */
1309 dir = opendir( dirname );
1310 while( ( entry = readdir( dir ) ) )
1312 char filename[1024];
1313 if( entry->d_name[0] == '.' )
1317 memset( filename, 0, 1024 );
1318 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
1327 * @param h Handle to hb_handle_t
1329 int hb_get_pid( hb_handle_t * h )
1335 * Sets the current state.
1336 * @param h Handle to hb_handle_t
1337 * @param s Handle to new hb_state_t
1339 void hb_set_state( hb_handle_t * h, hb_state_t * s )
1341 hb_lock( h->pause_lock );
1342 hb_lock( h->state_lock );
1343 memcpy( &h->state, s, sizeof( hb_state_t ) );
1344 if( h->state.state == HB_STATE_WORKING )
1347 if (h->job_count < 1)
1348 h->job_count_permanent = 1;
1350 h->state.param.working.job_cur =
1351 h->job_count_permanent - hb_list_count( h->jobs );
1352 h->state.param.working.job_count = h->job_count_permanent;
1354 // Set which job is being worked on
1356 h->state.param.working.sequence_id = h->current_job->sequence_id;
1358 h->state.param.working.sequence_id = 0;
1360 hb_unlock( h->state_lock );
1361 hb_unlock( h->pause_lock );