summaryrefslogtreecommitdiffstats
path: root/libhb/hb.c
diff options
context:
space:
mode:
authorjstebbins <[email protected]>2012-11-12 08:22:07 +0000
committerjstebbins <[email protected]>2012-11-12 08:22:07 +0000
commitab5445d6f279a3477c08f069ff3329b696cb9ac8 (patch)
tree28b1f1aa52db4f2f0d35a35950f6486955703426 /libhb/hb.c
parentcf1f034f914348d0f0bfcd5f8902f0a3fb519273 (diff)
Improve management of titles and jobs
Cleans up several several unavoidable memory leaks caused by old api. Clearly separates titles from jobs. Titles are set during scan and never modified now. Since titles are immutable, this lead to API some changes. For example, We were setting chapter names in the title from the front ends. Now these get set in the job. These new APIs allow us to start moving away from our use of title->job. Eventually, I would like to eliminate title->job completely, but the mac ui is too tightly tied to using this field to allow removing it at this time. So there is temporarily a convenience function used only by the mac ui that allows it to continue using title->job and also use the new APIs. New APIs: typedef struct hb_title_set_s hb_title_set_t; struct hb_title_set_s { hb_list_t * list_title; int feature; // Detected DVD feature title }; hb_title_set_t * hb_get_title_set( hb_handle_t * ); This is just something I added to clean up how "feature title" info is passed. hb_job_t * hb_job_init( hb_title_t * title ); Initializes a new job with default settings from the title. hb_job_t * hb_job_init_by_index( hb_handle_t *h, int title_index ); Same as hb_job_init(). For use by win Interop lib. void hb_job_reset( hb_job_t * job ); Convenience function for the MacUi. Clears audio, subtitle, and filter lists. The macui still uses title->job because it is so intricately tied to it. So I created this convenience function that it can call after adding a job. void hb_job_close( hb_job_t ** job ); Releases the job an all resources it contains. void hb_job_set_advanced_opts( hb_job_t *job, const char *advanced_opts ); Makes a copy of "advanced_opts" and stores in job. Freed by hb_job_close(). void hb_job_set_file( hb_job_t *job, const char *file ); Makes a copy of "file" and stores in job. Freed by hb_job_close(). void hb_chapter_set_title(hb_chapter_t *chapter, const char *title); Makes a copy of "title" and stores in chapter. Freed by hb_chapter_close(). Recommended usage (cli and lingui are updated to do this): job = hb_job_init( title ); // set job settings ... hb_add(h, job); hb_job_close( &job ); I have also added new APIs for managing metadata. These are used to add metadata to a job. void hb_metadata_set_name( hb_metadata_t *metadata, const char *name ); void hb_metadata_set_artist( hb_metadata_t *metadata, const char *artist ); void hb_metadata_set_composer( hb_metadata_t *metadata, const char *composer ); void hb_metadata_set_release_date( hb_metadata_t *metadata, const char *release_date ); void hb_metadata_set_comment( hb_metadata_t *metadata, const char *comment ); void hb_metadata_set_genre( hb_metadata_t *metadata, const char *genre ); void hb_metadata_set_album( hb_metadata_t *metadata, const char *album ); void hb_metadata_set_coverart( hb_metadata_t *metadata, const uint8_t *coverart, int size ); Example: job = hb_job_init( &job ); // set job settings ... hb_metadata_set_artist( job->metadata, "Danny Elfman" ); hb_add(h, job); hb_job_close( &job ); Some APIs have changed in order to avoid using title incorrectly and use the new hb_title_set_t. -void hb_autopassthru_apply_settings( hb_job_t * job, hb_title_t * title ); +void hb_autopassthru_apply_settings( hb_job_t * job ); -void hb_get_preview( hb_handle_t *, hb_title_t *, int, uint8_t * ); +void hb_get_preview( hb_handle_t *, hb_job_t *, int, uint8_t * ); hb_thread_t * hb_scan_init( hb_handle_t *, volatile int * die, const char * path, int title_index, - hb_list_t * list_title, int preview_count, + hb_title_set_t * title_set, int preview_count, int store_previews, uint64_t min_duration ); These APIs have been removed. Win Interop will need some changes. I think what I've provided will be suffecient, but let me know if it's not. -void hb_get_preview_by_index( hb_handle_t *, int, int, uint8_t * ); -void hb_set_anamorphic_size_by_index( hb_handle_t *, int, - int *output_width, int *output_height, - int *output_par_width, int *output_par_height ); git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@5058 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'libhb/hb.c')
-rw-r--r--libhb/hb.c326
1 files changed, 55 insertions, 271 deletions
diff --git a/libhb/hb.c b/libhb/hb.c
index 52ddc206b..8a1ec3b1e 100644
--- a/libhb/hb.c
+++ b/libhb/hb.c
@@ -36,7 +36,7 @@ struct hb_handle_s
int pid;
/* DVD/file scan thread */
- hb_list_t * list_title;
+ hb_title_set_t title_set;
hb_thread_t * scan_thread;
/* The thread which processes the jobs. Others threads are launched
@@ -71,7 +71,6 @@ int hb_instance_counter = 0;
int hb_process_initialized = 0;
static void thread_func( void * );
-hb_title_t * hb_get_title_by_index( hb_handle_t *, int );
static int ff_lockmgr_cb(void **mutex, enum AVLockOp op)
{
@@ -432,7 +431,7 @@ hb_handle_t * hb_init( int verbose, int update_check )
*/
hb_buffer_pool_init();
- h->list_title = hb_list_init();
+ h->title_set.list_title = hb_list_init();
h->jobs = hb_list_init();
h->state_lock = hb_lock_init();
@@ -531,7 +530,7 @@ hb_handle_t * hb_init_dl( int verbose, int update_check )
}
}
- h->list_title = hb_list_init();
+ h->title_set.list_title = hb_list_init();
h->jobs = hb_list_init();
h->current_job = NULL;
@@ -631,7 +630,7 @@ void hb_remove_previews( hb_handle_t * h )
dir = opendir( dirname );
if (dir == NULL) return;
- count = hb_list_count( h->list_title );
+ count = hb_list_count( h->title_set.list_title );
while( ( entry = readdir( dir ) ) )
{
if( entry->d_name[0] == '.' )
@@ -640,7 +639,7 @@ void hb_remove_previews( hb_handle_t * h )
}
for( i = 0; i < count; i++ )
{
- title = hb_list_item( h->list_title, i );
+ title = hb_list_item( h->title_set.list_title, i );
len = snprintf( filename, 1024, "%d_%d", h->id, title->index );
if (strncmp(entry->d_name, filename, len) == 0)
{
@@ -670,15 +669,15 @@ void hb_scan( hb_handle_t * h, const char * path, int title_index,
/* Clean up from previous scan */
hb_remove_previews( h );
- while( ( title = hb_list_item( h->list_title, 0 ) ) )
+ while( ( title = hb_list_item( h->title_set.list_title, 0 ) ) )
{
- hb_list_rem( h->list_title, title );
+ hb_list_rem( h->title_set.list_title, title );
hb_title_close( &title );
}
hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
h->scan_thread = hb_scan_init( h, &h->scan_die, path, title_index,
- h->list_title, preview_count,
+ &h->title_set, preview_count,
store_previews, min_duration );
}
@@ -689,25 +688,12 @@ void hb_scan( hb_handle_t * h, const char * path, int title_index,
*/
hb_list_t * hb_get_titles( hb_handle_t * h )
{
- return h->list_title;
+ return h->title_set.list_title;
}
-/**
- * Create preview image of desired title a index of picture.
- * @param h Handle to hb_handle_t.
- * @param title_index Index of the title to get the preview for (1-based).
- * @param picture Index in title.
- * @param buffer Handle to buffer were image will be drawn.
- */
-void hb_get_preview_by_index( hb_handle_t * h, int title_index, int picture, uint8_t * buffer )
+hb_title_set_t * hb_get_title_set( hb_handle_t * h )
{
- hb_title_t * title;
-
- title = hb_get_title_by_index( h, title_index );
- if ( title != NULL )
- {
- hb_get_preview( h, title, picture, buffer );
- }
+ return &h->title_set;
}
int hb_save_preview( hb_handle_t * h, int title, int preview, hb_buffer_t *buf )
@@ -747,12 +733,16 @@ hb_buffer_t * hb_read_preview( hb_handle_t * h, int title_idx, int preview )
{
FILE * file;
char filename[1024];
+ hb_title_set_t *title_set;
+
hb_title_t * title = NULL;
+ title_set = hb_get_title_set(h);
+
int ii;
- for (ii = 0; ii < hb_list_count(h->list_title); ii++)
+ for (ii = 0; ii < hb_list_count(title_set->list_title); ii++)
{
- title = hb_list_item( h->list_title, ii);
+ title = hb_list_item( title_set->list_title, ii);
if (title != NULL && title->index == title_idx)
{
break;
@@ -804,10 +794,10 @@ hb_buffer_t * hb_read_preview( hb_handle_t * h, int title_idx, int preview )
* @param picture Index in title.
* @param buffer Handle to buffer were image will be drawn.
*/
-void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
+void hb_get_preview( hb_handle_t * h, hb_job_t * job, int picture,
uint8_t * buffer )
{
- hb_job_t * job = title->job;
+ hb_title_t * title = job->title;
char filename[1024];
hb_buffer_t * in_buf, * deint_buf = NULL, * preview_buf;
uint8_t * pen;
@@ -991,26 +981,6 @@ int hb_detect_comb( hb_buffer_t * buf, int color_equal, int color_diff, int thre
/**
* Calculates job width and height for anamorphic content,
*
- * @param h Instance handle
- * @param title_index Index of the title/job to inspect (1-based).
- * @param output_width Pointer to returned storage width
- * @param output_height Pointer to returned storage height
- * @param output_par_width Pointer to returned pixel width
- * @param output_par_height Pointer to returned pixel height
- */
-void hb_set_anamorphic_size_by_index( hb_handle_t * h, int title_index,
- int *output_width, int *output_height,
- int *output_par_width, int *output_par_height )
-{
- hb_title_t * title;
- title = hb_get_title_by_index( h, title_index );
-
- hb_set_anamorphic_size( title->job, output_width, output_height, output_par_width, output_par_height );
-}
-
-/**
- * Calculates job width and height for anamorphic content,
- *
* @param job Handle to hb_job_t
* @param output_width Pointer to returned storage width
* @param output_height Pointer to returned storage height
@@ -1423,53 +1393,6 @@ hb_job_t * hb_current_job( hb_handle_t * h )
}
/**
- * Applies information from the given job to the official job instance.
- * @param h Handle to hb_handle_t.
- * @param title_index Index of the title to apply the chapter name to (1-based).
- * @param chapter The chapter to apply the name to (1-based).
- * @param job Job information to apply.
- */
-void hb_set_chapter_name( hb_handle_t * h, int title_index, int chapter_index, const char * chapter_name )
-{
- hb_title_t * title;
- title = hb_get_title_by_index( h, title_index );
-
- hb_chapter_t * chapter = hb_list_item( title->list_chapter, chapter_index - 1 );
-
- strncpy(chapter->title, chapter_name, 1023);
- chapter->title[1023] = '\0';
-}
-
-/**
- * Applies information from the given job to the official job instance.
- * Currently only applies information needed for anamorphic size calculation and previews.
- * @param h Handle to hb_handle_t.
- * @param title_index Index of the title to apply the job information to (1-based).
- * @param job Job information to apply.
- */
-void hb_set_job( hb_handle_t * h, int title_index, hb_job_t * job )
-{
- int i;
-
- hb_title_t * title;
- title = hb_get_title_by_index( h, title_index );
-
- hb_job_t * job_target = title->job;
-
- job_target->deinterlace = job->deinterlace;
- job_target->width = job->width;
- job_target->height = job->height;
- job_target->maxWidth = job->maxWidth;
- job_target->maxHeight = job->maxHeight;
- for (i = 0; i < 4; i++)
- {
- job_target->crop[i] = job->crop[i];
- }
-
- job_target->anamorphic = job->anamorphic;
-}
-
-/**
* Adds a job to the job list.
* @param h Handle to hb_handle_t.
* @param job Handle to hb_job_t.
@@ -1477,85 +1400,21 @@ void hb_set_job( hb_handle_t * h, int title_index, hb_job_t * job )
void hb_add( hb_handle_t * h, hb_job_t * job )
{
hb_job_t * job_copy;
- hb_title_t * title, * title_copy;
- hb_chapter_t * chapter, * chapter_copy;
hb_audio_t * audio;
hb_subtitle_t * subtitle;
- hb_attachment_t * attachment;
int i;
char audio_lang[4];
- /* Copy the title */
- title = job->title;
- title_copy = malloc( sizeof( hb_title_t ) );
- memcpy( title_copy, title, sizeof( hb_title_t ) );
-
- title_copy->list_chapter = hb_list_init();
- for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
- {
- chapter = hb_list_item( title->list_chapter, i );
- chapter_copy = malloc( sizeof( hb_chapter_t ) );
- memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
- hb_list_add( title_copy->list_chapter, chapter_copy );
- }
-
- /*
- * Copy the metadata
- */
- if( title->metadata )
- {
- title_copy->metadata = malloc( sizeof( hb_metadata_t ) );
-
- if( title_copy->metadata )
- {
- memcpy( title_copy->metadata, title->metadata, sizeof( hb_metadata_t ) );
-
- /*
- * Need to copy the artwork seperatly (TODO).
- */
- if( title->metadata->coverart )
- {
- title_copy->metadata->coverart = malloc( title->metadata->coverart_size );
- if( title_copy->metadata->coverart )
- {
- memcpy( title_copy->metadata->coverart, title->metadata->coverart,
- title->metadata->coverart_size );
- } else {
- title_copy->metadata->coverart_size = 0;
- }
- }
- }
- }
-
- /* Copy the audio track(s) we want */
- title_copy->list_audio = hb_list_init();
- for( i = 0; i < hb_list_count(job->list_audio); i++ )
- {
- if( ( audio = hb_list_item( job->list_audio, i ) ) )
- {
- hb_list_add( title_copy->list_audio, hb_audio_copy(audio) );
- }
- }
-
- /* Initialize subtitle list - filled out further below */
- title_copy->list_subtitle = hb_list_init();
-
- /* Copy all the attachments */
- title_copy->list_attachment = hb_list_init();
- for( i = 0; i < hb_list_count(title->list_attachment); i++ )
- {
- if( ( attachment = hb_list_item( title->list_attachment, i ) ) )
- {
- hb_list_add( title_copy->list_attachment, hb_attachment_copy(attachment) );
- }
- }
- title_copy->video_codec_name = strdup( title->video_codec_name );
+ /* Copy the job */
+ job_copy = calloc( sizeof( hb_job_t ), 1 );
+ memcpy( job_copy, job, sizeof( hb_job_t ) );
- /* If we're doing Foreign Audio Search, copy all subtitles matching the first
- * audio track language we find in the audio list.
+ /* If we're doing Foreign Audio Search, copy all subtitles matching the
+ * first audio track language we find in the audio list.
*
- * Otherwise, copy all subtitles found in the input job (which can be manually
- * selected by the user, or added after the Foreign Audio Search pass). */
+ * Otherwise, copy all subtitles found in the input job (which can be
+ * manually selected by the user, or added after the Foreign Audio
+ * Search pass). */
memset( audio_lang, 0, sizeof( audio_lang ) );
if( job->indepth_scan )
@@ -1571,9 +1430,16 @@ void hb_add( hb_handle_t * h, hb_job_t * job )
break;
}
}
- for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
+
+ /*
+ * If doing a subtitle scan then add all the matching subtitles for this
+ * language.
+ */
+ job_copy->list_subtitle = hb_list_init();
+
+ for( i = 0; i < hb_list_count( job->title->list_subtitle ); i++ )
{
- subtitle = hb_list_item( title->list_subtitle, i );
+ subtitle = hb_list_item( job->title->list_subtitle, i );
if( strcmp( subtitle->iso639_2, audio_lang ) == 0 &&
hb_subtitle_can_force( subtitle->source ) )
{
@@ -1582,54 +1448,31 @@ void hb_add( hb_handle_t * h, hb_job_t * job )
*
* We will update the subtitle list on the next pass later, after
* the subtitle scan pass has completed. */
- hb_list_add( title_copy->list_subtitle, hb_subtitle_copy( subtitle ) );
+ hb_list_add( job_copy->list_subtitle,
+ hb_subtitle_copy( subtitle ) );
}
}
}
else
{
/* Copy all subtitles from the input job to title_copy/job_copy. */
- for( i = 0; i < hb_list_count( job->list_subtitle ); i++ )
- {
- if( ( subtitle = hb_list_item( job->list_subtitle, i ) ) )
- {
- hb_list_add( title_copy->list_subtitle, hb_subtitle_copy( subtitle ) );
- }
- }
+ job_copy->list_subtitle = hb_subtitle_list_copy( job->list_subtitle );
}
- /* Copy the job */
- job_copy = calloc( sizeof( hb_job_t ), 1 );
- memcpy( job_copy, job, sizeof( hb_job_t ) );
- title_copy->job = job_copy;
- job_copy->title = title_copy;
- job_copy->list_audio = title_copy->list_audio;
- job_copy->list_subtitle = title_copy->list_subtitle; // sharing list between title and job
- job_copy->file = strdup( job->file );
+ job_copy->list_chapter = hb_chapter_list_copy( job->list_chapter );
+ job_copy->list_audio = hb_audio_list_copy( job->list_audio );
+ job_copy->list_attachment = hb_attachment_list_copy( job->list_attachment );
+ job_copy->metadata = hb_metadata_copy( job->metadata );
+
+ if ( job->file )
+ job_copy->file = strdup( job->file );
+ if ( job->advanced_opts )
+ job_copy->advanced_opts = strdup( job->advanced_opts );
job_copy->h = h;
job_copy->pause = h->pause_lock;
/* Copy the job filter list */
- if( job->list_filter )
- {
- int i;
- int filter_count = hb_list_count( job->list_filter );
- job_copy->list_filter = hb_list_init();
- for( i = 0; i < filter_count; i++ )
- {
- /*
- * Copy the filters, since the MacGui reuses the global filter objects
- * meaning that queued up jobs overwrite the previous filter settings.
- * In reality, settings is probably the only field that needs duplicating
- * since it's the only value that is ever changed. But name is duplicated
- * as well for completeness. Not copying private_data since it gets
- * created for each job in renderInit.
- */
- hb_filter_object_t * filter = hb_list_item( job->list_filter, i );
- hb_filter_object_t * filter_copy = hb_filter_copy( filter );
- hb_list_add( job_copy->list_filter, filter_copy );
- }
- }
+ job_copy->list_filter = hb_filter_list_copy( job->list_filter );
/* Add the job to the list */
hb_list_add( h->jobs, job_copy );
@@ -1638,37 +1481,6 @@ void hb_add( hb_handle_t * h, hb_job_t * job )
}
/**
- * Clean up the job structure so that is is ready for setting up a new job.
- * Should be called by front-ends after hb_add().
- */
-void hb_reset_job( hb_job_t * job )
-{
- hb_audio_t *audio;
- hb_subtitle_t *subtitle;
- hb_filter_object_t *filter;
-
- // clean up audio list
- while( ( audio = hb_list_item( job->list_audio, 0 ) ) )
- {
- hb_list_rem( job->list_audio, audio );
- free( audio );
- }
- // clean up subtitle list
- while( ( subtitle = hb_list_item( job->list_subtitle, 0 ) ) )
- {
- hb_list_rem( job->list_subtitle, subtitle );
- free( subtitle );
- }
- // clean up filter list
- while( ( filter = hb_list_item( job->list_filter, 0 ) ) )
- {
- hb_list_rem( job->list_filter, filter );
- free( filter->settings );
- free( filter );
- }
-}
-
-/**
* Removes a job from the job list.
* @param h Handle to hb_handle_t.
* @param job Handle to hb_job_t.
@@ -1831,17 +1643,12 @@ void hb_close( hb_handle_t ** _h )
hb_thread_close( &h->main_thread );
- while( ( title = hb_list_item( h->list_title, 0 ) ) )
+ while( ( title = hb_list_item( h->title_set.list_title, 0 ) ) )
{
- hb_list_rem( h->list_title, title );
- if( title->job )
- {
- hb_reset_job( title->job );
- }
- free( title->job );
+ hb_list_rem( h->title_set.list_title, title );
hb_title_close( &title );
}
- hb_list_close( &h->list_title );
+ hb_list_close( &h->title_set.list_title );
hb_list_close( &h->jobs );
hb_lock_close( &h->state_lock );
@@ -1925,9 +1732,9 @@ static void thread_func( void * _h )
hb_title_t * title;
hb_remove_previews( h );
- while( ( title = hb_list_item( h->list_title, 0 ) ) )
+ while( ( title = hb_list_item( h->title_set.list_title, 0 ) ) )
{
- hb_list_rem( h->list_title, title );
+ hb_list_rem( h->title_set.list_title, title );
hb_title_close( &title );
}
@@ -1936,7 +1743,7 @@ static void thread_func( void * _h )
else
{
hb_log( "libhb: scan thread found %d valid title(s)",
- hb_list_count( h->list_title ) );
+ hb_list_count( h->title_set.list_title ) );
}
hb_lock( h->state_lock );
h->state.state = HB_STATE_SCANDONE; //originally state.state
@@ -2024,29 +1831,6 @@ int hb_get_instance_id( hb_handle_t * h )
}
/**
- * Returns the title with the given title index.
- * @param h Handle to hb_handle_t
- * @param title_index the index of the title to get
- * @returns The requested title
- */
-hb_title_t * hb_get_title_by_index( hb_handle_t * h, int title_index )
-{
- hb_title_t * title;
- int i;
- int count = hb_list_count( h->list_title );
- for (i = 0; i < count; i++)
- {
- title = hb_list_item( h->list_title, i );
- if (title->index == title_index)
- {
- return title;
- }
- }
-
- return NULL;
-}
-
-/**
* Sets the current state.
* @param h Handle to hb_handle_t
* @param s Handle to new hb_state_t