summaryrefslogtreecommitdiffstats
path: root/libhb
diff options
context:
space:
mode:
authorjstebbins <[email protected]>2009-11-25 21:14:44 +0000
committerjstebbins <[email protected]>2009-11-25 21:14:44 +0000
commit009238a822971a933d0b7642fd04c15bf8e404a2 (patch)
tree0979d88ba5116f4ad7e41f55bc7f93a1a82040b9 /libhb
parent08483929dd5352012eb8b1f1143f138e3d5f19f0 (diff)
batch file scanning and scan cancel
When a directory is specified as the source, first we attempt to open as a dvd, then if that fails, we attempt to open each file in the directory as a stream source. Since opening a large directory of files can take a really long time, you can also now cancel a scan. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@2980 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'libhb')
-rw-r--r--libhb/batch.c128
-rw-r--r--libhb/common.c46
-rw-r--r--libhb/common.h5
-rw-r--r--libhb/decmetadata.c2
-rw-r--r--libhb/dvd.c2
-rw-r--r--libhb/dvdnav.c1
-rw-r--r--libhb/hb.c42
-rw-r--r--libhb/hb.h1
-rw-r--r--libhb/internal.h17
-rw-r--r--libhb/ports.h6
-rw-r--r--libhb/reader.c18
-rw-r--r--libhb/scan.c69
-rw-r--r--libhb/stream.c2
-rw-r--r--libhb/work.c2
14 files changed, 310 insertions, 31 deletions
diff --git a/libhb/batch.c b/libhb/batch.c
new file mode 100644
index 000000000..f627df555
--- /dev/null
+++ b/libhb/batch.c
@@ -0,0 +1,128 @@
+/* $Id: dvd.c,v 1.12 2005/11/25 15:05:25 titer Exp $
+
+ This file is part of the HandBrake source code.
+ Homepage: <http://handbrake.fr/>.
+ It may be used under the terms of the GNU General Public License. */
+
+#include "hb.h"
+#include "lang.h"
+
+struct hb_batch_s
+{
+ char * path;
+ hb_list_t * list_file;
+};
+
+/***********************************************************************
+ * hb_batch_init
+ ***********************************************************************
+ *
+ **********************************************************************/
+hb_batch_t * hb_batch_init( char * path )
+{
+ hb_batch_t * d;
+ struct stat sb;
+ DIR * dir;
+ struct dirent * entry;
+ char * filename;
+
+ if ( stat( path, &sb ) )
+ return NULL;
+
+ if ( !S_ISDIR( sb.st_mode ) )
+ return NULL;
+
+ dir = opendir( path );
+ if ( dir == NULL )
+ return NULL;
+
+ d = calloc( sizeof( hb_batch_t ), 1 );
+ d->list_file = hb_list_init();
+
+ while ( (entry = readdir( dir ) ) )
+ {
+ filename = hb_strdup_printf( "%s" DIR_SEP_STR "%s", path, entry->d_name );
+ if ( stat( filename, &sb ) )
+ {
+ free( filename );
+ continue;
+ }
+
+ if ( !S_ISREG( sb.st_mode ) )
+ {
+ free( filename );
+ continue;
+ }
+
+ hb_list_add( d->list_file, filename );
+ }
+
+ if ( hb_list_count( d->list_file ) == 0 )
+ {
+ hb_list_close( &d->list_file );
+ free( d );
+ return NULL;
+ }
+
+ d->path = strdup( path );
+
+ return d;
+}
+
+/***********************************************************************
+ * hb_batch_title_count
+ **********************************************************************/
+int hb_batch_title_count( hb_batch_t * d )
+{
+ return hb_list_count( d->list_file );
+}
+
+/***********************************************************************
+ * hb_batch_title_scan
+ **********************************************************************/
+hb_title_t * hb_batch_title_scan( hb_batch_t * d, int t )
+{
+
+ hb_title_t * title;
+ char * filename;
+ hb_stream_t * stream;
+
+ if ( t < 1 )
+ return NULL;
+
+ filename = hb_list_item( d->list_file, t-1 );
+ if ( filename == NULL )
+ return NULL;
+
+ stream = hb_stream_open( filename, 0 );
+ if ( stream == NULL )
+ return NULL;
+
+ title = hb_stream_title_scan( stream );
+ title->index = t;
+ hb_stream_close( &stream );
+
+ return title;
+}
+
+/***********************************************************************
+ * hb_batch_close
+ ***********************************************************************
+ * Closes and frees everything
+ **********************************************************************/
+void hb_batch_close( hb_batch_t ** _d )
+{
+ hb_batch_t * d = *_d;
+ char * filename;
+
+ while ( ( filename = hb_list_item( d->list_file, 0 ) ) )
+ {
+ hb_list_rem( d->list_file, filename );
+ free( filename );
+ }
+ hb_list_close( &d->list_file );
+ free( d->path );
+ free( d );
+ *_d = NULL;
+}
+
diff --git a/libhb/common.c b/libhb/common.c
index 357625528..ce983987f 100644
--- a/libhb/common.c
+++ b/libhb/common.c
@@ -714,7 +714,7 @@ void hb_register_error_handler( hb_error_handler_t * handler )
**********************************************************************
*
*********************************************************************/
-hb_title_t * hb_title_init( char * dvd, int index )
+hb_title_t * hb_title_init( char * path, int index )
{
hb_title_t * t;
@@ -724,7 +724,7 @@ hb_title_t * hb_title_init( char * dvd, int index )
t->list_audio = hb_list_init();
t->list_chapter = hb_list_init();
t->list_subtitle = hb_list_init();
- strcat( t->dvd, dvd );
+ strcat( t->path, path );
// default to decoding mpeg2
t->video_id = 0xE0;
t->video_codec = WORK_DECMPEG2;
@@ -973,3 +973,45 @@ int hb_srt_add( const hb_job_t * job,
}
return retval;
}
+
+char * hb_strdup_printf( char * fmt, ... )
+{
+ int len;
+ va_list ap;
+ int size = 256;
+ char * str;
+ char * tmp;
+
+ str = malloc( size );
+ if ( str == NULL )
+ return NULL;
+
+ while (1)
+ {
+ /* Try to print in the allocated space. */
+ va_start( ap, fmt );
+ len = vsnprintf( str, size, fmt, ap );
+ va_end( ap );
+
+ /* If that worked, return the string. */
+ if ( len > -1 && len < size )
+ {
+ return str;
+ }
+
+ /* Else try again with more space. */
+ if ( len > -1 ) /* glibc 2.1 */
+ size = len + 1; /* precisely what is needed */
+ else /* glibc 2.0 */
+ size *= 2; /* twice the old size */
+ tmp = realloc( str, size );
+ if ( tmp == NULL )
+ {
+ free( str );
+ return NULL;
+ }
+ else
+ str = tmp;
+ }
+}
+
diff --git a/libhb/common.h b/libhb/common.h
index b6c5e5714..a85beda01 100644
--- a/libhb/common.h
+++ b/libhb/common.h
@@ -501,7 +501,8 @@ struct hb_metadata_s
struct hb_title_s
{
- char dvd[1024];
+ enum { HB_DVD_TYPE, HB_STREAM_TYPE } type;
+ char path[1024];
char name[1024];
int index;
int vts;
@@ -735,4 +736,6 @@ typedef void hb_error_handler_t( const char *errmsg );
extern void hb_register_error_handler( hb_error_handler_t * handler );
+char * hb_strdup_printf( char * fmt, ... );
+
#endif
diff --git a/libhb/decmetadata.c b/libhb/decmetadata.c
index e377f8c25..bd848a95a 100644
--- a/libhb/decmetadata.c
+++ b/libhb/decmetadata.c
@@ -13,7 +13,7 @@ static void decmp4metadata( hb_title_t *title )
MP4FileHandle input_file;
hb_deep_log( 2, "Got an MP4 input, read the metadata");
- input_file = MP4Read( title->dvd, 0 );
+ input_file = MP4Read( title->path, 0 );
if( input_file != MP4_INVALID_FILE_HANDLE )
{
diff --git a/libhb/dvd.c b/libhb/dvd.c
index fa5373e79..fbac84de4 100644
--- a/libhb/dvd.c
+++ b/libhb/dvd.c
@@ -158,6 +158,8 @@ static hb_title_t * hb_dvdread_title_scan( hb_dvd_t * e, int t )
hb_log( "scan: scanning title %d", t );
title = hb_title_init( d->path, t );
+ title->type = HB_DVD_TYPE;
+
if( DVDUDFVolumeInfo( d->reader, title->name, sizeof( title->name ),
unused, sizeof( unused ) ) )
{
diff --git a/libhb/dvdnav.c b/libhb/dvdnav.c
index 2f1ac93cc..84895caca 100644
--- a/libhb/dvdnav.c
+++ b/libhb/dvdnav.c
@@ -307,6 +307,7 @@ static hb_title_t * hb_dvdnav_title_scan( hb_dvd_t * e, int t )
hb_log( "scan: scanning title %d", t );
title = hb_title_init( d->path, t );
+ title->type = HB_DVD_TYPE;
if (dvdnav_get_title_string(d->dvdnav, &name) == DVDNAV_STATUS_OK)
{
strncpy( title->name, name, sizeof( title->name ) );
diff --git a/libhb/hb.c b/libhb/hb.c
index 34fe53b72..1b899e39c 100644
--- a/libhb/hb.c
+++ b/libhb/hb.c
@@ -38,6 +38,7 @@ struct hb_handle_s
/* For MacGui active queue
increments each time the scan thread completes*/
int scanCount;
+ volatile int scan_die;
/* Stash of persistent data between jobs, for stuff
like correcting frame count and framerate estimates
@@ -379,6 +380,8 @@ void hb_scan( hb_handle_t * h, const char * path, int title_index,
{
hb_title_t * title;
+ h->scan_die = 0;
+
/* Clean up from previous scan */
hb_remove_previews( h );
while( ( title = hb_list_item( h->list_title, 0 ) ) )
@@ -388,8 +391,9 @@ void hb_scan( hb_handle_t * h, const char * path, int title_index,
}
hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
- h->scan_thread = hb_scan_init( h, path, title_index, h->list_title,
- preview_count, store_previews );
+ h->scan_thread = hb_scan_init( h, &h->scan_die, path, title_index,
+ h->list_title, preview_count,
+ store_previews );
}
/**
@@ -1238,6 +1242,20 @@ void hb_stop( hb_handle_t * h )
}
/**
+ * Stops the conversion process.
+ * @param h Handle to hb_handle_t.
+ */
+void hb_scan_stop( hb_handle_t * h )
+{
+ h->scan_die = 1;
+
+ h->job_count = hb_count(h);
+ h->job_count_permanent = 0;
+
+ hb_resume( h );
+}
+
+/**
* Returns the state of the conversion process.
* @param h Handle to hb_handle_t.
* @param s Handle to hb_state_t which to copy the state data.
@@ -1340,8 +1358,24 @@ static void thread_func( void * _h )
{
hb_thread_close( &h->scan_thread );
- hb_log( "libhb: scan thread found %d valid title(s)",
- hb_list_count( h->list_title ) );
+ if ( h->scan_die )
+ {
+ hb_title_t * title;
+
+ hb_remove_previews( h );
+ while( ( title = hb_list_item( h->list_title, 0 ) ) )
+ {
+ hb_list_rem( h->list_title, title );
+ hb_title_close( &title );
+ }
+
+ hb_log( "hb_scan: canceled" );
+ }
+ else
+ {
+ hb_log( "libhb: scan thread found %d valid title(s)",
+ hb_list_count( h->list_title ) );
+ }
hb_lock( h->state_lock );
h->state.state = HB_STATE_SCANDONE; //originally state.state
hb_unlock( h->state_lock );
diff --git a/libhb/hb.h b/libhb/hb.h
index 234d4b502..04d87b2be 100644
--- a/libhb/hb.h
+++ b/libhb/hb.h
@@ -42,6 +42,7 @@ void hb_dvd_set_dvdnav( int enable );
void hb_scan( hb_handle_t *, const char * path,
int title_index, int preview_count,
int store_previews );
+void hb_scan_stop( hb_handle_t * );
/* hb_get_titles()
Returns the list of valid titles detected by the latest scan. */
diff --git a/libhb/internal.h b/libhb/internal.h
index 9a03074bf..88c8ef983 100644
--- a/libhb/internal.h
+++ b/libhb/internal.h
@@ -131,9 +131,10 @@ static inline void hb_buffer_swap_copy( hb_buffer_t *src, hb_buffer_t *dst )
* Threads: update.c, scan.c, work.c, reader.c, muxcommon.c
**********************************************************************/
hb_thread_t * hb_update_init( int * build, char * version );
-hb_thread_t * hb_scan_init( hb_handle_t *, const char * path,
- int title_index, hb_list_t * list_title,
- int preview_count, int store_previews );
+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,
+ int store_previews );
hb_thread_t * hb_work_init( hb_list_t * jobs, int cpu_count,
volatile int * die, int * error, hb_job_t ** job );
hb_thread_t * hb_reader_init( hb_job_t * );
@@ -166,6 +167,16 @@ extern const hb_muxer_t hb_demux[];
extern void decmetadata( hb_title_t *title );
/***********************************************************************
+ * batch.c
+ **********************************************************************/
+typedef struct hb_batch_s hb_batch_t;
+
+hb_batch_t * hb_batch_init( char * path );
+void hb_batch_close( hb_batch_t ** _d );
+int hb_batch_title_count( hb_batch_t * d );
+hb_title_t * hb_batch_title_scan( hb_batch_t * d, int t );
+
+/***********************************************************************
* dvd.c
**********************************************************************/
typedef union hb_dvd_s hb_dvd_t;
diff --git a/libhb/ports.h b/libhb/ports.h
index 0f3697f0d..47be2d76e 100644
--- a/libhb/ports.h
+++ b/libhb/ports.h
@@ -7,6 +7,12 @@
#ifndef HB_PORTS_H
#define HB_PORTS_H
+#if defined(_WIN32)
+#define DIR_SEP_STR "\\"
+#else
+#define DIR_SEP_STR "/"
+#endif
+
/************************************************************************
* Utils
***********************************************************************/
diff --git a/libhb/reader.c b/libhb/reader.c
index 1dda826d0..94ee77efb 100644
--- a/libhb/reader.c
+++ b/libhb/reader.c
@@ -194,12 +194,20 @@ static void ReaderFunc( void * _r )
int chapter = -1;
int chapter_end = r->job->chapter_end;
- if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
+ if ( r->title->type == HB_DVD_TYPE )
{
- if ( !( r->stream = hb_stream_open( r->title->dvd, r->title ) ) )
- {
- return;
- }
+ if ( !( r->dvd = hb_dvd_init( r->title->path ) ) )
+ return;
+ }
+ else if ( r->title->type == HB_STREAM_TYPE )
+ {
+ if ( !( r->stream = hb_stream_open( r->title->path, r->title ) ) )
+ return;
+ }
+ else
+ {
+ // Unknown type, should never happen
+ return;
}
if (r->dvd)
diff --git a/libhb/scan.c b/libhb/scan.c
index 74cc52adb..ea15fc512 100644
--- a/libhb/scan.c
+++ b/libhb/scan.c
@@ -12,17 +12,19 @@
typedef struct
{
- hb_handle_t * h;
+ hb_handle_t * h;
+ volatile int * die;
- char * path;
- int title_index;
- hb_list_t * list_title;
+ char * path;
+ int title_index;
+ hb_list_t * list_title;
- hb_dvd_t * dvd;
- hb_stream_t * stream;
+ hb_dvd_t * dvd;
+ hb_stream_t * stream;
+ hb_batch_t * batch;
- int preview_count;
- int store_previews;
+ int preview_count;
+ int store_previews;
} hb_scan_t;
@@ -43,13 +45,15 @@ static const char *aspect_to_string( double aspect )
return arstr;
}
-hb_thread_t * hb_scan_init( hb_handle_t * handle, const char * path,
- int title_index, hb_list_t * list_title,
- int preview_count, int store_previews )
+hb_thread_t * hb_scan_init( hb_handle_t * handle, volatile int * die,
+ const char * path, int title_index,
+ hb_list_t * list_title, int preview_count,
+ int store_previews )
{
hb_scan_t * data = calloc( sizeof( hb_scan_t ), 1 );
data->h = handle;
+ data->die = die;
data->path = strdup( path );
data->title_index = title_index;
data->list_title = list_title;
@@ -91,6 +95,20 @@ static void ScanFunc( void * _data )
}
}
}
+ else if ( ( data->batch = hb_batch_init( data->path ) ) )
+ {
+ /* Scan all titles */
+ for( i = 0; i < hb_batch_title_count( data->batch ); i++ )
+ {
+ hb_title_t * title;
+
+ title = hb_batch_title_scan( data->batch, i + 1 );
+ if ( title != NULL )
+ {
+ hb_list_add( data->list_title, title );
+ }
+ }
+ }
else if ( (data->stream = hb_stream_open( data->path, 0 ) ) != NULL )
{
hb_list_add( data->list_title, hb_stream_title_scan( data->stream ) );
@@ -107,6 +125,10 @@ static void ScanFunc( void * _data )
hb_state_t state;
hb_audio_t * audio;
+ if ( *data->die )
+ {
+ goto finish;
+ }
title = hb_list_item( data->list_title, i );
#define p state.param.scanning
@@ -202,6 +224,8 @@ static void ScanFunc( void * _data )
job->mux = HB_MUX_MP4;
}
+finish:
+
if( data->dvd )
{
hb_dvd_close( &data->dvd );
@@ -210,6 +234,10 @@ static void ScanFunc( void * _data )
{
hb_stream_close(&data->stream);
}
+ if( data->batch )
+ {
+ hb_batch_close( &data->batch );
+ }
free( data->path );
free( data );
_data = NULL;
@@ -379,9 +407,13 @@ static int DecodePreviews( hb_scan_t * data, hb_title_t * title )
if (data->dvd)
{
- hb_dvd_start( data->dvd, title, 1 );
- title->angle_count = hb_dvd_angle_count( data->dvd );
- hb_log( "scan: title angle(s) %d", title->angle_count );
+ hb_dvd_start( data->dvd, title, 1 );
+ title->angle_count = hb_dvd_angle_count( data->dvd );
+ hb_log( "scan: title angle(s) %d", title->angle_count );
+ }
+ else if (data->batch)
+ {
+ data->stream = hb_stream_open( title->path, title );
}
for( i = 0; i < data->preview_count; i++ )
@@ -390,6 +422,10 @@ static int DecodePreviews( hb_scan_t * data, hb_title_t * title )
FILE * file_preview;
char filename[1024];
+ if ( *data->die )
+ {
+ return 0;
+ }
if (data->dvd)
{
if( !hb_dvd_seek( data->dvd, (float) ( i + 1 ) / ( data->preview_count + 1.0 ) ) )
@@ -663,6 +699,11 @@ skip_preview:
hb_buffer_close( &vid_buf );
}
+ if ( data->batch && data->stream )
+ {
+ hb_stream_close( &data->stream );
+ }
+
if ( npreviews )
{
// use the most common frame info for our final title dimensions
diff --git a/libhb/stream.c b/libhb/stream.c
index ff593b2c7..da2e5791d 100644
--- a/libhb/stream.c
+++ b/libhb/stream.c
@@ -652,6 +652,7 @@ hb_title_t * hb_stream_title_scan(hb_stream_t *stream)
// 'Barebones Title'
hb_title_t *aTitle = hb_title_init( stream->path, 0 );
+ aTitle->type = HB_STREAM_TYPE;
aTitle->index = 1;
// Copy part of the stream path to the title name
@@ -2702,6 +2703,7 @@ static hb_title_t *ffmpeg_title_scan( hb_stream_t *stream )
// 'Barebones Title'
hb_title_t *title = hb_title_init( stream->path, 0 );
+ title->type = HB_STREAM_TYPE;
title->index = 1;
// Copy part of the stream path to the title name
diff --git a/libhb/work.c b/libhb/work.c
index 2209b15f2..6bd9700f7 100644
--- a/libhb/work.c
+++ b/libhb/work.c
@@ -125,7 +125,7 @@ void hb_display_job_info( hb_job_t * job )
hb_log("job configuration:");
hb_log( " * source");
- hb_log( " + %s", title->dvd );
+ hb_log( " + %s", title->path );
hb_log( " + title %d, chapter(s) %d to %d", title->index,
job->chapter_start, job->chapter_end );