1 /* $Id: hb.c,v 1.43 2005/04/27 21:05:24 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
9 #include "ffmpeg/avcodec.h"
13 /* The "Check for update" thread */
16 hb_thread_t * update_thread;
18 /* This thread's only purpose is to check other threads'
21 hb_thread_t * main_thread;
24 /* DVD/file scan thread */
25 hb_list_t * list_title;
26 hb_thread_t * scan_thread;
28 /* The thread which processes the jobs. Others threads are launched
29 from this one (see work.c) */
32 volatile int work_die;
34 hb_thread_t * work_thread;
38 hb_lock_t * state_lock;
42 hb_lock_t * pause_lock;
45 hb_work_object_t * hb_objects = NULL;
47 static void thread_func( void * );
50 * Registers work objects, by adding the work object to a liked list.
51 * @param w Handle to hb_work_object_t to register.
53 void hb_register( hb_work_object_t * w )
60 * libhb initialization routine.
61 * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
62 * @param update_check signals libhb to check for updated version from HandBrake website.
63 * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
65 hb_handle_t * hb_init_real( int verbose, int update_check )
67 hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
70 /* See hb_log() in common.c */
71 if( verbose > HB_DEBUG_NONE )
73 putenv( "HB_DEBUG=1" );
76 /* Check for an update on the website if asked to */
81 hb_log( "hb_init: checking for updates" );
83 h->update_thread = hb_update_init( &h->build, h->version );
87 if( hb_thread_has_exited( h->update_thread ) )
89 /* Immediate success or failure */
90 hb_thread_close( &h->update_thread );
93 if( hb_get_date() > date + 1000 )
95 /* Still nothing after one second. Connection problem,
97 hb_log( "hb_init: connection problem, not waiting for "
105 /* CPU count detection */
106 hb_log( "hb_init: checking cpu count" );
107 h->cpu_count = hb_get_cpu_count();
109 h->list_title = hb_list_init();
110 h->jobs = hb_list_init();
112 h->state_lock = hb_lock_init();
113 h->state.state = HB_STATE_IDLE;
115 h->pause_lock = hb_lock_init();
119 register_avcodec( &mpeg4_encoder );
120 register_avcodec( &mp2_decoder );
121 register_avcodec( &ac3_encoder );
123 /* Start library thread */
124 hb_log( "hb_init: starting libhb thread" );
126 h->main_thread = hb_thread_init( "libhb", thread_func, h,
127 HB_NORMAL_PRIORITY );
133 * Returns current version of libhb.
134 * @param h Handle to hb_handle_t.
135 * @return character array of version number.
137 char * hb_get_version( hb_handle_t * h )
143 * Returns current build of libhb.
144 * @param h Handle to hb_handle_t.
145 * @return character array of build number.
147 int hb_get_build( hb_handle_t * h )
153 * Checks for needed update.
154 * @param h Handle to hb_handle_t.
155 * @param version Pointer to handle where version will be copied.
156 * @return update indicator.
158 int hb_check_update( hb_handle_t * h, char ** version )
160 *version = ( h->build < 0 ) ? NULL : h->version;
165 * Sets the cpu count to the desired value.
166 * @param h Handle to hb_handle_t
167 * @param cpu_count Number of CPUs to use.
169 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
171 cpu_count = MAX( 1, cpu_count );
172 cpu_count = MIN( cpu_count, 8 );
173 h->cpu_count = cpu_count;
177 * Initializes a scan of the by calling hb_scan_init
178 * @param h Handle to hb_handle_t
179 * @param path location of VIDEO_TS folder.
180 * @param title_index Desired title to scan. 0 for all titles.
182 void hb_scan( hb_handle_t * h, const char * path, int title_index )
186 /* Clean up from previous scan */
187 while( ( title = hb_list_item( h->list_title, 0 ) ) )
189 hb_list_rem( h->list_title, title );
190 hb_title_close( &title );
193 hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
194 h->scan_thread = hb_scan_init( h, path, title_index, h->list_title );
198 * Returns the list of titles found.
199 * @param h Handle to hb_handle_t
200 * @return Handle to hb_list_t of the title list.
202 hb_list_t * hb_get_titles( hb_handle_t * h )
204 return h->list_title;
208 * Create preview image of desired title a index of picture.
209 * @param h Handle to hb_handle_t.
210 * @param title Handle to hb_title_t of desired title.
211 * @param picture Index in title.
212 * @param buffer Handle to buufer were inage will be drawn.
214 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
217 hb_job_t * job = title->job;
220 uint8_t * buf1, * buf2, * buf3, * buf4, * pen;
222 AVPicture pic1, pic2, pic3, pic4;
223 ImgReSampleContext * context;
226 buf1 = malloc( title->width * title->height * 3 / 2 );
227 buf2 = malloc( title->width * title->height * 3 / 2 );
228 buf3 = malloc( title->width * title->height * 3 / 2 );
229 buf4 = malloc( title->width * title->height * 4 );
230 avpicture_fill( &pic1, buf1, PIX_FMT_YUV420P,
231 title->width, title->height );
232 avpicture_fill( &pic2, buf2, PIX_FMT_YUV420P,
233 title->width, title->height );
234 avpicture_fill( &pic3, buf3, PIX_FMT_YUV420P,
235 job->width, job->height );
236 avpicture_fill( &pic4, buf4, PIX_FMT_RGBA32,
237 job->width, job->height );
239 memset( filename, 0, 1024 );
241 hb_get_tempory_filename( h, filename, "%x%d",
242 (int) title, picture );
244 file = fopen( filename, "r" );
247 hb_log( "hb_get_preview: fopen failed" );
251 fread( buf1, title->width * title->height * 3 / 2, 1, file );
254 context = img_resample_full_init(
255 job->width, job->height, title->width, title->height,
256 job->crop[0], job->crop[1], job->crop[2], job->crop[3],
259 if( job->deinterlace )
261 avpicture_deinterlace( &pic2, &pic1, PIX_FMT_YUV420P,
262 title->width, title->height );
263 img_resample( context, &pic3, &pic2 );
267 img_resample( context, &pic3, &pic1 );
269 img_convert( &pic4, PIX_FMT_RGBA32, &pic3, PIX_FMT_YUV420P,
270 job->width, job->height );
272 /* Gray background */
273 p32 = (uint32_t *) buffer;
274 for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
279 /* Draw the picture, centered, and draw the cropping zone */
280 pen = buffer + ( title->height - job->height ) *
281 ( title->width + 2 ) * 2 + ( title->width - job->width ) * 2;
282 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
283 pen += 4 * ( title->width + 2 );
284 for( i = 0; i < job->height; i++ )
287 nextLine = pen + 4 * ( title->width + 2 );
288 memset( pen, 0xFF, 4 );
290 memcpy( pen, buf4 + 4 * job->width * i, 4 * job->width );
291 pen += 4 * job->width;
292 memset( pen, 0xFF, 4 );
295 memset( pen, 0xFF, 4 * ( job->width + 2 ) );
304 * Calculates job width, height, and cropping parameters.
305 * @param job Handle to hb_job_t.
306 * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
307 * @param pixels Maximum desired pixel count.
309 void hb_set_size( hb_job_t * job, int aspect, int pixels )
311 hb_title_t * title = job->title;
313 int croppedWidth = title->width - title->crop[2] - title->crop[3];
314 int croppedHeight = title->height - title->crop[0] - title->crop[1];
315 int croppedAspect = title->aspect * title->height * croppedWidth /
316 croppedHeight / title->width;
322 /* Keep the best possible aspect ratio */
323 aspect = croppedAspect;
326 /* Crop if necessary to obtain the desired ratio */
327 memcpy( job->crop, title->crop, 4 * sizeof( int ) );
328 if( aspect < croppedAspect )
330 /* Need to crop on the left and right */
331 addCrop = croppedWidth - aspect * croppedHeight * title->width /
332 title->aspect / title->height;
335 addCrop = ( addCrop + 1 ) / 2;
336 job->crop[2] += addCrop;
337 job->crop[3] += addCrop;
339 else if( addCrop & 2 )
342 job->crop[2] += addCrop - 1;
343 job->crop[3] += addCrop + 1;
348 job->crop[2] += addCrop;
349 job->crop[3] += addCrop;
352 else if( aspect > croppedAspect )
354 /* Need to crop on the top and bottom */
355 addCrop = croppedHeight - croppedWidth * title->aspect *
356 title->height / aspect / title->width;
359 addCrop = ( addCrop + 1 ) / 2;
360 job->crop[0] += addCrop;
361 job->crop[1] += addCrop;
363 else if( addCrop & 2 )
366 job->crop[0] += addCrop - 1;
367 job->crop[1] += addCrop + 1;
372 job->crop[0] += addCrop;
373 job->crop[1] += addCrop;
377 /* Compute a resolution from the number of pixels and aspect */
381 h = MULTIPLE_16( w * HB_ASPECT_BASE / aspect );
389 job->height = MULTIPLE_16( 16 * i * HB_ASPECT_BASE / aspect );
393 * Returns the number of jobs in the queue.
394 * @param h Handle to hb_handle_t.
395 * @return Number of jobs.
397 int hb_count( hb_handle_t * h )
399 return hb_list_count( h->jobs );
403 * Returns handle to job at index i within the job list.
404 * @param h Handle to hb_handle_t.
405 * @param i Index of job.
406 * @returns Handle to hb_job_t of desired job.
408 hb_job_t * hb_job( hb_handle_t * h, int i )
410 return hb_list_item( h->jobs, i );
414 * Adds a job to the job list.
415 * @param h Handle to hb_handle_t.
416 * @param job Handle to hb_job_t.
418 void hb_add( hb_handle_t * h, hb_job_t * job )
421 hb_title_t * title, * title_copy;
422 hb_chapter_t * chapter, * chapter_copy;
423 hb_audio_t * audio, * audio_copy;
424 hb_subtitle_t * subtitle, * subtitle_copy;
429 title_copy = malloc( sizeof( hb_title_t ) );
430 memcpy( title_copy, title, sizeof( hb_title_t ) );
432 title_copy->list_chapter = hb_list_init();
433 for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
435 chapter = hb_list_item( title->list_chapter, i );
436 chapter_copy = malloc( sizeof( hb_chapter_t ) );
437 memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
438 hb_list_add( title_copy->list_chapter, chapter_copy );
441 /* Copy the audio track(s) we want */
442 title_copy->list_audio = hb_list_init();
444 /* Do nothing about audio during first pass */
447 for( i = 0; i < 8; i++ )
449 if( job->audios[i] < 0 )
453 if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
455 audio_copy = malloc( sizeof( hb_audio_t ) );
456 memcpy( audio_copy, audio, sizeof( hb_audio_t ) );
457 hb_list_add( title_copy->list_audio, audio_copy );
462 /* Copy the subtitle we want (or not) */
463 title_copy->list_subtitle = hb_list_init();
464 if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) )
466 subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
467 memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
468 hb_list_add( title_copy->list_subtitle, subtitle_copy );
472 job_copy = calloc( sizeof( hb_job_t ), 1 );
473 memcpy( job_copy, job, sizeof( hb_job_t ) );
474 job_copy->title = title_copy;
475 job_copy->file = strdup( job->file );
477 job_copy->pause = h->pause_lock;
479 /* Add the job to the list */
480 hb_list_add( h->jobs, job_copy );
484 * Removes a job from the job list.
485 * @param h Handle to hb_handle_t.
486 * @param job Handle to hb_job_t.
488 void hb_rem( hb_handle_t * h, hb_job_t * job )
490 hb_list_rem( h->jobs, job );
492 /* XXX free everything XXX */
496 * Starts the conversion process.
497 * Sets state to HB_STATE_WORKING.
498 * calls hb_work_init, to launch work thread. Stores handle to work thread.
499 * @param h Handle to hb_handle_t.
501 void hb_start( hb_handle_t * h )
504 h->job_count = hb_list_count( h->jobs );
506 hb_lock( h->state_lock );
507 h->state.state = HB_STATE_WORKING;
508 #define p h->state.param.working
511 p.job_count = h->job_count;
518 hb_unlock( h->state_lock );
523 h->work_thread = hb_work_init( h->jobs, h->cpu_count,
524 &h->work_die, &h->work_error );
528 * Pauses the conversion process.
529 * @param h Handle to hb_handle_t.
531 void hb_pause( hb_handle_t * h )
535 hb_lock( h->pause_lock );
538 hb_lock( h->state_lock );
539 h->state.state = HB_STATE_PAUSED;
540 hb_unlock( h->state_lock );
545 * Resumes the conversion process.
546 * @param h Handle to hb_handle_t.
548 void hb_resume( hb_handle_t * h )
552 hb_unlock( h->pause_lock );
558 * Stops the conversion process.
559 * @param h Handle to hb_handle_t.
561 void hb_stop( hb_handle_t * h )
569 * Returns the state of the conversion process.
570 * @param h Handle to hb_handle_t.
571 * @param s Handle to hb_state_t which to copy the state data.
573 void hb_get_state( hb_handle_t * h, hb_state_t * s )
575 hb_lock( h->state_lock );
577 memcpy( s, &h->state, sizeof( hb_state_t ) );
578 h->state.state = HB_STATE_IDLE;
580 hb_unlock( h->state_lock );
584 * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init_real.
585 * @param _h Pointer to handle to hb_handle_t.
587 void hb_close( hb_handle_t ** _h )
589 hb_handle_t * h = *_h;
593 hb_thread_close( &h->main_thread );
595 while( ( title = hb_list_item( h->list_title, 0 ) ) )
597 hb_list_rem( h->list_title, title );
598 hb_title_close( &title );
600 hb_list_close( &h->list_title );
602 hb_list_close( &h->jobs );
603 hb_lock_close( &h->state_lock );
604 hb_lock_close( &h->pause_lock );
610 * Monitors the state of the update, scan, and work threads.
611 * Sets scan done state when scan thread exits.
612 * Sets work done state when work thread exits.
613 * @param _h Handle to hb_handle_t
615 static void thread_func( void * _h )
617 hb_handle_t * h = (hb_handle_t *) _h;
620 struct dirent * entry;
624 /* Create folder for temporary files */
625 memset( dirname, 0, 1024 );
626 hb_get_tempory_directory( h, dirname );
632 /* In case the check_update thread hangs, it'll die sooner or
633 later. Then, we join it here */
634 if( h->update_thread &&
635 hb_thread_has_exited( h->update_thread ) )
637 hb_thread_close( &h->update_thread );
640 /* Check if the scan thread is done */
641 if( h->scan_thread &&
642 hb_thread_has_exited( h->scan_thread ) )
644 hb_thread_close( &h->scan_thread );
646 hb_log( "libhb: scan thread found %d valid title(s)",
647 hb_list_count( h->list_title ) );
648 hb_lock( h->state_lock );
649 h->state.state = HB_STATE_SCANDONE;
650 hb_unlock( h->state_lock );
653 /* Check if the work thread is done */
654 if( h->work_thread &&
655 hb_thread_has_exited( h->work_thread ) )
657 hb_thread_close( &h->work_thread );
659 hb_log( "libhb: work result = %d",
661 hb_lock( h->state_lock );
662 h->state.state = HB_STATE_WORKDONE;
663 h->state.param.workdone.error = h->work_error;
664 hb_unlock( h->state_lock );
673 hb_thread_close( &h->work_thread );
676 /* Remove temp folder */
677 dir = opendir( dirname );
678 while( ( entry = readdir( dir ) ) )
681 if( entry->d_name[0] == '.' )
685 memset( filename, 0, 1024 );
686 snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
695 * @param h Handle to hb_handle_t
697 int hb_get_pid( hb_handle_t * h )
703 * Sets the current state.
704 * @param h Handle to hb_handle_t
705 * @param s Handle to new hb_state_t
707 void hb_set_state( hb_handle_t * h, hb_state_t * s )
709 hb_lock( h->pause_lock );
710 hb_lock( h->state_lock );
711 memcpy( &h->state, s, sizeof( hb_state_t ) );
712 if( h->state.state == HB_STATE_WORKING )
715 h->state.param.working.job_cur =
716 h->job_count - hb_list_count( h->jobs );
717 h->state.param.working.job_count = h->job_count;
719 hb_unlock( h->state_lock );
720 hb_unlock( h->pause_lock );