forked from HandBrake/HandBrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
4749 lines (4502 loc) · 167 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* test.c
Copyright (c) 2003-2017 HandBrake Team
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 v2.
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <getopt.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <inttypes.h>
#ifdef SYS_SunOS
#include <strings.h>
#endif
#if defined( __MINGW32__ )
#include <windows.h>
#include <conio.h>
#endif
#if defined( PTW32_STATIC_LIB )
#include <pthread.h>
#endif
#include "hb.h"
#include "lang.h"
#include "parsecsv.h"
#include "openclwrapper.h"
#ifdef USE_QSV
#include "qsv_common.h"
#endif
#if defined( __APPLE_CC__ )
#import <CoreServices/CoreServices.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IODVDMedia.h>
#endif
#define NLMEANS_DEFAULT_PRESET "medium"
#define DEINTERLACE_DEFAULT_PRESET "default"
#define DECOMB_DEFAULT_PRESET "default"
#define DETELECINE_DEFAULT_PRESET "default"
#define COMB_DETECT_DEFAULT_PRESET "default"
#define HQDN3D_DEFAULT_PRESET "medium"
#define ROTATE_DEFAULT "angle=180:hflip=0"
#define DEBLOCK_DEFAULT "qp=5"
/* Options */
static int debug = HB_DEBUG_ALL;
static int dvdnav = 1;
static char * input = NULL;
static char * output = NULL;
static char * format = NULL;
static int titleindex = 1;
static int titlescan = 0;
static int main_feature = 0;
static char * native_language = NULL;
static int native_dub = 0;
static int twoPass = -1;
static int pad_disable = 0;
static char * pad = NULL;
static int deinterlace_disable = 0;
static int deinterlace_custom = 0;
static char * deinterlace = NULL;
static int deblock_disable = 0;
static char * deblock = NULL;
static int hqdn3d_disable = 0;
static int hqdn3d_custom = 0;
static char * hqdn3d = NULL;
static int nlmeans_disable = 0;
static int nlmeans_custom = 0;
static char * nlmeans = NULL;
static char * nlmeans_tune = NULL;
static int detelecine_disable = 0;
static int detelecine_custom = 0;
static char * detelecine = NULL;
static int comb_detect_disable = 0;
static int comb_detect_custom = 0;
static char * comb_detect = NULL;
static int decomb_disable = 0;
static int decomb_custom = 0;
static char * decomb = NULL;
static char * rotate = NULL;
static int grayscale = -1;
static char * vcodec = NULL;
static int audio_all = -1;
static char ** audio_copy_list = NULL;
static char ** audio_lang_list = NULL;
static char ** atracks = NULL;
static char ** acodecs = NULL;
static char ** abitrates = NULL;
static char ** aqualities = NULL;
static char ** arates = NULL;
static char ** mixdowns = NULL;
static char ** normalize_mix_level = NULL;
static char ** audio_dither = NULL;
static char ** dynamic_range_compression = NULL;
static char ** audio_gain = NULL;
static char ** acompressions = NULL;
static char * acodec_fallback = NULL;
static char ** anames = NULL;
static char ** subtitle_lang_list = NULL;
static char ** subtracks = NULL;
static char ** subforce = NULL;
static int subtitle_all = -1;
static int subburn = 0;
static int subburn_native = 0;
static int subdefault = 0;
static char ** srtfile = NULL;
static char ** srtcodeset = NULL;
static char ** srtoffset = NULL;
static char ** srtlang = NULL;
static int srtdefault = -1;
static int srtburn = -1;
static int width = 0;
static int height = 0;
static int crop[4] = { -1,-1,-1,-1 };
static int loose_crop = -1;
static char * vrate = NULL;
static float vquality = HB_INVALID_VIDEO_QUALITY;
static int vbitrate = 0;
static int mux = 0;
static int anamorphic_mode = -1;
static int modulus = 0;
static int par_height = -1;
static int par_width = -1;
static int display_width = -1;
static int keep_display_aspect = 0;
static int itu_par = -1;
static int angle = 0;
static int chapter_start = 0;
static int chapter_end = 0;
static int chapter_markers = -1;
static char * marker_file = NULL;
static char * encoder_preset = NULL;
static char * encoder_tune = NULL;
static char * encoder_profile = NULL;
static char * encoder_level = NULL;
static char * advanced_opts = NULL;
static int maxHeight = 0;
static int maxWidth = 0;
static int fastfirstpass = -1;
static char * preset_export_name = NULL;
static char * preset_export_desc = NULL;
static char * preset_export_file = NULL;
static char * preset_name = NULL;
static char * queue_import_name = NULL;
static int cfr = -1;
static int mp4_optimize = -1;
static int ipod_atom = -1;
static int color_matrix_code = -1;
static int preview_count = 10;
static int store_previews = 0;
static int start_at_preview = 0;
static int64_t start_at_pts = 0;
static int start_at_frame = 0;
static int64_t stop_at_pts = 0;
static int stop_at_frame = 0;
static uint64_t min_title_duration = 10;
static int use_opencl = -1;
#ifdef USE_QSV
static int qsv_async_depth = -1;
static int qsv_decode = -1;
#endif
/* Exit cleanly on Ctrl-C */
static volatile hb_error_code done_error = HB_ERROR_NONE;
static volatile int die = 0;
static volatile int work_done = 0;
static void SigHandler( int );
/* Utils */
static void ShowHelp();
static void ShowCommands()
{
fprintf(stdout, "\nCommands:\n");
fprintf(stdout, " [h]elp Show this message\n");
fprintf(stdout, " [q]uit Exit HandBrakeCLI\n");
fprintf(stdout, " [p]ause Pause encoding\n");
fprintf(stdout, " [r]esume Resume encoding\n");
}
static int ParseOptions( int argc, char ** argv );
static int CheckOptions( int argc, char ** argv );
static int HandleEvents( hb_handle_t * h, hb_dict_t *preset_dict );
static hb_dict_t * PreparePreset( const char *preset_name );
static hb_dict_t * PrepareJob( hb_handle_t *h, hb_title_t *title,
hb_dict_t *preset_dict );
static void print_string_list(FILE *out, const char* const *list, const char *prefix);
#ifdef __APPLE_CC__
static char* bsd_name_for_path(char *path);
static int device_is_dvd(char *device);
static io_service_t get_iokit_service( char *device );
static int is_dvd_service( io_service_t service );
static int is_whole_media_service( io_service_t service );
#endif
/* Only print the "Muxing..." message once */
static int show_mux_warning = 1;
/****************************************************************************
* hb_error_handler
*
* When using the CLI just display using hb_log as we always did in the past
* make sure that we prefix with a nice ERROR message to catch peoples eyes.
****************************************************************************/
static void hb_cli_error_handler ( const char *errmsg )
{
fprintf( stderr, "ERROR: %s\n", errmsg );
}
static int get_argv_utf8(int *argc_ptr, char ***argv_ptr)
{
#if defined( __MINGW32__ )
int ret = 0;
int argc;
char **argv;
wchar_t **argv_utf16 = CommandLineToArgvW(GetCommandLineW(), &argc);
if (argv_utf16)
{
int i;
int offset = (argc+1) * sizeof(char*);
int size = offset;
for(i = 0; i < argc; i++)
size += WideCharToMultiByte(CP_UTF8, 0, argv_utf16[i], -1, NULL, 0, NULL, NULL );
argv = malloc(size);
if (argv != NULL)
{
for (i = 0; i < argc; i++)
{
argv[i] = (char*)argv + offset;
offset += WideCharToMultiByte(CP_UTF8, 0, argv_utf16[i], -1, argv[i], size-offset, NULL, NULL);
}
argv[argc] = NULL;
ret = 1;
}
LocalFree(argv_utf16);
}
if (ret)
{
*argc_ptr = argc;
*argv_ptr = argv;
}
return ret;
#else
// On other systems, assume command line is already utf8
return 1;
#endif
}
static volatile int job_running = 0;
void EventLoop(hb_handle_t *h, hb_dict_t *preset_dict)
{
/* Wait... */
work_done = 0;
while (!die && !work_done)
{
#if defined( __MINGW32__ )
if( _kbhit() ) {
switch( _getch() )
{
case 0x03: /* ctrl-c */
case 'q':
fprintf( stdout, "\nEncoding Quit by user command\n" );
done_error = HB_ERROR_CANCELED;
die = 1;
break;
case 'p':
fprintf(stdout,
"\nEncoding Paused by user command, 'r' to resume\n");
hb_pause(h);
hb_system_sleep_allow(h);
break;
case 'r':
hb_system_sleep_prevent(h);
hb_resume(h);
break;
case 'h':
ShowCommands();
break;
}
}
hb_snooze( 200 );
#elif !defined(SYS_BEOS)
fd_set fds;
struct timeval tv;
int ret;
char buf[257];
tv.tv_sec = 0;
tv.tv_usec = 100000;
FD_ZERO( &fds );
FD_SET( STDIN_FILENO, &fds );
ret = select( STDIN_FILENO + 1, &fds, NULL, NULL, &tv );
if( ret > 0 )
{
int size = 0;
while( size < 256 &&
read( STDIN_FILENO, &buf[size], 1 ) > 0 )
{
if( buf[size] == '\n' )
{
break;
}
size++;
}
if( size >= 256 || buf[size] == '\n' )
{
switch( buf[0] )
{
case 'q':
fprintf( stdout, "\nEncoding Quit by user command\n" );
done_error = HB_ERROR_CANCELED;
die = 1;
break;
case 'p':
fprintf(stdout,
"\nEncoding Paused by user command, 'r' to resume\n");
hb_pause(h);
hb_system_sleep_allow(h);
break;
case 'r':
hb_system_sleep_prevent(h);
hb_resume(h);
break;
case 'h':
ShowCommands();
break;
}
}
}
hb_snooze( 200 );
#else
hb_snooze( 200 );
#endif
HandleEvents( h, preset_dict );
}
job_running = 0;
}
int RunQueueJob(hb_handle_t *h, hb_dict_t *job_dict)
{
if (job_dict == NULL)
{
return -1;
}
char * json_job;
json_job = hb_value_get_json(job_dict);
hb_value_free(&job_dict);
if (json_job == NULL)
{
fprintf(stderr, "Error in setting up job! Aborting.\n");
return -1;
}
hb_add_json(h, json_job);
free(json_job);
job_running = 1;
hb_start( h );
EventLoop(h, NULL);
return 0;
}
int RunQueue(hb_handle_t *h, const char *queue_import_name)
{
hb_value_t * queue = hb_value_read_json(queue_import_name);
if (hb_value_type(queue) == HB_VALUE_TYPE_DICT)
{
return RunQueueJob(h, hb_dict_get(queue, "Job"));
}
else if (hb_value_type(queue) == HB_VALUE_TYPE_ARRAY)
{
int ii, count, result = 0;
count = hb_value_array_len(queue);
for (ii = 0; ii < count; ii++)
{
hb_dict_t * entry = hb_value_array_get(queue, ii);
int ret = RunQueueJob(h, hb_dict_get(entry, "Job"));
if (ret < 0)
{
result = ret;
}
if (die)
{
break;
}
}
return result;
}
else
{
fprintf(stderr, "Error: Invalid queue file %s\n", queue_import_name);
return -1;
}
return 0;
}
int main( int argc, char ** argv )
{
hb_handle_t * h;
hb_global_init();
hb_presets_builtin_update();
hb_presets_cli_default_init();
/* Init libhb */
h = hb_init(4); // Show all logging until debug level is parsed
// Get utf8 command line if windows
get_argv_utf8(&argc, &argv);
/* Parse command line */
if( ParseOptions( argc, argv ) ||
CheckOptions( argc, argv ) )
{
return 1;
}
hb_log_level_set(h, debug);
/* Register our error handler */
hb_register_error_handler(&hb_cli_error_handler);
hb_dvd_set_dvdnav( dvdnav );
if (use_opencl == 1)
{
hb_opencl_set_enable(h, use_opencl);
}
/* Show version */
fprintf( stderr, "%s - %s - %s\n",
HB_PROJECT_TITLE, HB_PROJECT_BUILD_TITLE, HB_PROJECT_URL_WEBSITE );
/* Geeky */
fprintf( stderr, "%d CPU%s detected\n", hb_get_cpu_count(),
hb_get_cpu_count() > 1 ? "s" : "" );
/* Exit ASAP on Ctrl-C */
signal( SIGINT, SigHandler );
if (queue_import_name != NULL)
{
hb_system_sleep_prevent(h);
RunQueue(h, queue_import_name);
}
else
{
// Apply all command line overrides to the preset that are possible.
// Some command line options are applied later to the job
// (e.g. chapter names, explicit audio & subtitle tracks).
hb_dict_t *preset_dict = PreparePreset(preset_name);
if (preset_dict == NULL)
{
// An appropriate error message should have already
// been spilled by PreparePreset.
return 1;
}
if (preset_export_name != NULL)
{
hb_dict_set(preset_dict, "PresetName",
hb_value_string(preset_export_name));
if (preset_export_desc != NULL)
{
hb_dict_set(preset_dict, "PresetDescription",
hb_value_string(preset_export_desc));
}
if (preset_export_file != NULL)
{
hb_presets_write_json(preset_dict, preset_export_file);
}
else
{
char *json;
json = hb_presets_package_json(preset_dict);
fprintf(stdout, "%s\n", json);
}
// If the user requested to export a preset, but not to
// transcode or scan a file, exit here.
if (input == NULL ||
(!titlescan && titleindex != 0 && output == NULL))
{
hb_value_free(&preset_dict);
return 0;
}
}
/* Feed libhb with a DVD to scan */
fprintf( stderr, "Opening %s...\n", input );
if (main_feature) {
/*
* We need to scan for all the titles in order to
* find the main feature
*/
titleindex = 0;
}
hb_system_sleep_prevent(h);
hb_scan(h, input, titleindex, preview_count, store_previews,
min_title_duration * 90000LL);
EventLoop(h, preset_dict);
hb_value_free(&preset_dict);
}
/* Clean up */
hb_close(&h);
hb_global_close();
hb_str_vfree(audio_copy_list);
hb_str_vfree(abitrates);
hb_str_vfree(acompressions);
hb_str_vfree(aqualities);
hb_str_vfree(audio_dither);
hb_str_vfree(acodecs);
hb_str_vfree(arates);
hb_str_vfree(atracks);
hb_str_vfree(audio_lang_list);
hb_str_vfree(audio_gain);
hb_str_vfree(dynamic_range_compression);
hb_str_vfree(mixdowns);
hb_str_vfree(subtitle_lang_list);
hb_str_vfree(subtracks);
free(acodec_fallback);
free(native_language);
free(format);
free(input);
free(output);
free(preset_name);
free(encoder_preset);
free(encoder_tune);
free(advanced_opts);
free(encoder_profile);
free(encoder_level);
free(rotate);
free(deblock);
free(detelecine);
free(deinterlace);
free(decomb);
free(hqdn3d);
free(nlmeans);
free(nlmeans_tune);
free(preset_export_name);
free(preset_export_desc);
free(preset_export_file);
// write a carriage return to stdout
// avoids overlap / line wrapping when stderr is redirected
fprintf(stdout, "\n");
fprintf(stderr, "HandBrake has exited.\n");
return done_error;
}
static void PrintTitleInfo( hb_title_t * title, int feature )
{
int i;
fprintf( stderr, "+ title %d:\n", title->index );
if ( title->index == feature )
{
fprintf( stderr, " + Main Feature\n" );
}
if ( title->type == HB_STREAM_TYPE || title->type == HB_FF_STREAM_TYPE )
{
fprintf( stderr, " + stream: %s\n", title->path );
}
else if ( title->type == HB_DVD_TYPE )
{
fprintf( stderr, " + vts %d, ttn %d, cells %d->%d (%"PRIu64" blocks)\n",
title->vts, title->ttn, title->cell_start, title->cell_end,
title->block_count );
}
else if( title->type == HB_BD_TYPE )
{
fprintf( stderr, " + playlist: %05d.MPLS\n", title->playlist );
}
if (title->angle_count > 1)
fprintf( stderr, " + angle(s) %d\n", title->angle_count );
fprintf( stderr, " + duration: %02d:%02d:%02d\n",
title->hours, title->minutes, title->seconds );
fprintf( stderr, " + size: %dx%d, pixel aspect: %d/%d, display aspect: %.2f, %.3f fps\n",
title->geometry.width, title->geometry.height,
title->geometry.par.num, title->geometry.par.den,
(float)title->dar.num / title->dar.den,
(float)title->vrate.num / title->vrate.den );
fprintf( stderr, " + autocrop: %d/%d/%d/%d\n", title->crop[0],
title->crop[1], title->crop[2], title->crop[3] );
fprintf(stderr, " + support opencl: %s\n", title->opencl_support ? "yes" : "no");
fprintf( stderr, " + chapters:\n" );
for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
{
hb_chapter_t * chapter;
chapter = hb_list_item( title->list_chapter, i );
fprintf( stderr, " + %d: cells %d->%d, %"PRIu64" blocks, duration "
"%02d:%02d:%02d\n", chapter->index,
chapter->cell_start, chapter->cell_end,
chapter->block_count, chapter->hours, chapter->minutes,
chapter->seconds );
}
fprintf( stderr, " + audio tracks:\n" );
for( i = 0; i < hb_list_count( title->list_audio ); i++ )
{
hb_audio_config_t *audio;
audio = hb_list_audio_config_item( title->list_audio, i );
if( ( audio->in.codec == HB_ACODEC_AC3 ) || ( audio->in.codec == HB_ACODEC_DCA) )
{
fprintf( stderr, " + %d, %s (iso639-2: %s), %dHz, %dbps\n",
i + 1,
audio->lang.description,
audio->lang.iso639_2,
audio->in.samplerate,
audio->in.bitrate );
}
else
{
fprintf( stderr, " + %d, %s (iso639-2: %s)\n",
i + 1,
audio->lang.description,
audio->lang.iso639_2 );
}
}
fprintf( stderr, " + subtitle tracks:\n" );
for( i = 0; i < hb_list_count( title->list_subtitle ); i++ )
{
hb_subtitle_t *subtitle;
subtitle = hb_list_item( title->list_subtitle, i );
fprintf( stderr, " + %d, %s (iso639-2: %s) (%s)(%s)\n",
i + 1, subtitle->lang,
subtitle->iso639_2,
(subtitle->format == TEXTSUB) ? "Text" : "Bitmap",
hb_subsource_name(subtitle->source));
}
if(title->detected_interlacing)
{
/* Interlacing was found in half or more of the preview frames */
fprintf( stderr, " + combing detected, may be interlaced or telecined\n");
}
}
static void PrintTitleSetInfo( hb_title_set_t * title_set )
{
int i;
hb_title_t * title;
for( i = 0; i < hb_list_count( title_set->list_title ); i++ )
{
title = hb_list_item( title_set->list_title, i );
PrintTitleInfo( title, title_set->feature );
}
}
static int test_sub_list( char ** list, int pos )
{
int i;
if ( list == NULL || pos == 0 )
return 0;
if ( list[0] == NULL && pos == 1 )
return 1;
for ( i = 0; list[i] != NULL; i++ )
{
int idx = strtol( list[i], NULL, 0 );
if ( idx == pos )
return 1;
}
return 0;
}
void write_chapter_names(hb_dict_t *job_dict, const char *marker_file)
{
if (marker_file == NULL)
return;
hb_csv_file_t * file = hb_open_csv_file(marker_file);
hb_csv_cell_t * cell;
int row = 0;
if (file == NULL)
{
fprintf(stderr, "Cannot open chapter marker file, using defaults\n");
return;
}
fprintf(stderr, "Reading chapter markers from file %s\n", marker_file);
hb_value_array_t *chapter_array;
chapter_array = hb_dict_get(hb_dict_get(job_dict, "Destination"),
"ChapterList");
if (chapter_array == NULL)
return;
/* Parse the cells */
while (NULL != (cell = hb_read_next_cell(file)))
{
/* We have a chapter number */
if (cell->cell_col == 0)
{
row = cell->cell_row;
}
/* We have a chapter name */
if (cell->cell_col == 1 && row == cell->cell_row)
{
/* If we have a valid chapter, add chapter entry */
hb_dict_t *chapter_dict = hb_value_array_get(chapter_array, row);
if (chapter_dict != NULL)
{
hb_dict_set(chapter_dict, "Name",
hb_value_string(cell->cell_text));
}
}
hb_dispose_cell( cell );
}
hb_close_csv_file( file );
}
static void lang_list_remove(hb_value_array_t *list, const char *lang)
{
int count = hb_value_array_len(list);
int ii;
for (ii = count - 1; ii >= 0; ii--)
{
const char *tmp = hb_value_get_string(hb_value_array_get(list, ii));
if (!strncmp(lang, tmp, 4))
hb_value_array_remove(list, ii);
}
}
static int HandleEvents(hb_handle_t * h, hb_dict_t *preset_dict)
{
hb_state_t s;
hb_get_state( h, &s );
switch( s.state )
{
case HB_STATE_IDLE:
/* Nothing to do */
break;
#define p s.param.scanning
case HB_STATE_SCANNING:
/* Show what title is currently being scanned */
if (p.preview_cur)
{
fprintf(stderr, "\rScanning title %d of %d, preview %d, %.2f %%",
p.title_cur, p.title_count, p.preview_cur, 100 * p.progress);
}
else
{
fprintf(stderr, "\rScanning title %d of %d, %.2f %%",
p.title_cur, p.title_count, 100 * p.progress);
}
fflush(stderr);
break;
#undef p
case HB_STATE_SCANDONE:
{
hb_title_set_t * title_set;
hb_title_t * title;
if (job_running)
{
// SCANDONE generated by a scan during execution of the job
break;
}
title_set = hb_get_title_set( h );
if( !title_set || !hb_list_count( title_set->list_title ) )
{
/* No valid title, stop right there */
fprintf( stderr, "No title found.\n" );
done_error = HB_ERROR_WRONG_INPUT;
die = 1;
break;
}
if (main_feature)
{
int i;
int main_feature_idx=0;
int main_feature_pos=-1;
int main_feature_time=0;
int title_time;
fprintf( stderr, "Searching for main feature title...\n" );
for( i = 0; i < hb_list_count( title_set->list_title ); i++ )
{
title = hb_list_item( title_set->list_title, i );
title_time = (title->hours*60*60 ) + (title->minutes *60) + (title->seconds);
fprintf( stderr, " + Title (%d) index %d has length %dsec\n",
i, title->index, title_time );
if( main_feature_time < title_time )
{
main_feature_time = title_time;
main_feature_pos = i;
main_feature_idx = title->index;
}
if( title_set->feature == title->index )
{
main_feature_pos = i;
main_feature_idx = title->index;
break;
}
}
if( main_feature_pos == -1 )
{
fprintf( stderr, "No main feature title found.\n" );
done_error = HB_ERROR_WRONG_INPUT;
die = 1;
break;
}
titleindex = main_feature_idx;
fprintf(stderr, "Found main feature title %d\n",
main_feature_idx);
title = hb_list_item(title_set->list_title, main_feature_pos);
} else {
title = hb_list_item(title_set->list_title, 0);
}
if (!titleindex || titlescan)
{
/* Scan-only mode, print infos and exit */
PrintTitleSetInfo( title_set );
die = 1;
break;
}
fprintf( stderr, "+ Using preset: %s\n",
hb_value_get_string(hb_dict_get(preset_dict, "PresetName")));
PrintTitleInfo(title, title_set->feature);
// All overrides to the preset are complete
// Initialize the job from preset + overrides
// and apply job specific command line overrides
hb_dict_t *job_dict = PrepareJob(h, title, preset_dict);
if (job_dict == NULL)
{
die = 1;
return -1;
}
char * json_job;
json_job = hb_value_get_json(job_dict);
hb_value_free(&job_dict);
if (json_job == NULL)
{
fprintf(stderr, "Error in setting up job! Aborting.\n");
die = 1;
return -1;
}
hb_add_json(h, json_job);
free(json_job);
job_running = 1;
hb_start( h );
break;
}
#define p s.param.working
case HB_STATE_SEARCHING:
fprintf( stdout, "\rEncoding: task %d of %d, Searching for start time, %.2f %%",
p.pass, p.pass_count, 100.0 * p.progress );
if( p.seconds > -1 )
{
fprintf( stdout, " (ETA %02dh%02dm%02ds)",
p.hours, p.minutes, p.seconds );
}
fflush(stdout);
break;
case HB_STATE_WORKING:
fprintf( stdout, "\rEncoding: task %d of %d, %.2f %%",
p.pass, p.pass_count, 100.0 * p.progress );
if( p.seconds > -1 )
{
fprintf( stdout, " (%.2f fps, avg %.2f fps, ETA "
"%02dh%02dm%02ds)", p.rate_cur, p.rate_avg,
p.hours, p.minutes, p.seconds );
}
fflush(stdout);
break;
#undef p
#define p s.param.muxing
case HB_STATE_MUXING:
{
if (show_mux_warning)
{
fprintf( stdout, "\rMuxing: this may take awhile..." );
fflush(stdout);
show_mux_warning = 0;
}
break;
}
#undef p
#define p s.param.workdone
case HB_STATE_WORKDONE:
/* Print error if any, then exit */
switch( p.error )
{
case HB_ERROR_NONE:
fprintf( stderr, "\nEncode done!\n" );
break;
case HB_ERROR_CANCELED:
fprintf( stderr, "\nEncode canceled.\n" );
break;
default:
fprintf( stderr, "\nEncode failed (error %x).\n",
p.error );
}
done_error = p.error;
work_done = 1;
job_running = 0;
break;
#undef p
}
return 0;
}
/****************************************************************************
* SigHandler:
****************************************************************************/
static volatile int64_t i_die_date = 0;
void SigHandler( int i_signal )
{
done_error = HB_ERROR_CANCELED;
if( die == 0 )
{
die = 1;
i_die_date = hb_get_date();
fprintf( stderr, "Signal %d received, terminating - do it "
"again in case it gets stuck\n", i_signal );
}
else if( i_die_date + 500 < hb_get_date() )
{
fprintf( stderr, "Dying badly, files might remain in your /tmp\n" );
exit( done_error );
}
}
/****************************************************************************
* ShowHelp:
****************************************************************************/
static void showFilterPresets(FILE* const out, int filter_id)
{
char ** names = hb_filter_get_presets_short_name(filter_id);
int ii, count = 0;
// Count number of entries we want to display
for (ii = 0; names[ii] != NULL; ii++)
{
if (!strcasecmp(names[ii], "custom") || // skip custom
!strcasecmp(names[ii], "off") || // skip off
!strcasecmp(names[ii], "default")) // skip default
continue;
count++;
}
// If there are no entries, display nothing.
if (count == 0)
{