box: Utilize meaningful error values.
[L-SMASH.git] / cli / muxer.c
blob3895ed3c8d0589267fc564d45f09fcd3645bc75f
1 /*****************************************************************************
2 * muxer.c:
3 *****************************************************************************
4 * Copyright (C) 2010-2014 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
7 * Takashi Hirata <silverfilain@gmail.com>
9 * Permission to use, copy, modify, and/or distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *****************************************************************************/
22 /* This file is available under an ISC license. */
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <strings.h>
28 #include <stdlib.h>
29 #include <inttypes.h>
31 #include "lsmash.h"
32 #include "importer.h"
33 #include "cli.h"
35 #include "config.h"
37 #define MAX_NUM_OF_BRANDS 50
38 #define MAX_NUM_OF_INPUTS 10
39 #define MAX_NUM_OF_TRACKS 1
41 typedef struct
43 char *album_name;
44 char *artist;
45 char *comment;
46 char *release_date;
47 char *encoder;
48 char *genre;
49 char *lyrics;
50 char *title;
51 char *composer;
52 char *album_artist;
53 char *copyright;
54 char *description;
55 char *grouping;
56 uint32_t beats_per_minute;
57 } itunes_metadata_t;
59 typedef struct
61 int help;
62 int version;
63 int isom;
64 int isom_version;
65 int itunes_movie;
66 int qtff;
67 int brand_3gx;
68 int optimize_pd;
69 int timeline_shift;
70 uint32_t interleave;
71 uint32_t num_of_brands;
72 uint32_t brands[MAX_NUM_OF_BRANDS];
73 uint32_t major_brand;
74 uint32_t minor_version;
75 uint32_t num_of_inputs;
76 uint32_t chap_track;
77 char *chap_file;
78 int add_bom_to_chpl;
79 char *copyright_notice;
80 uint16_t copyright_language;
81 itunes_metadata_t itunes_metadata;
82 uint16_t default_language;
83 } option_t;
85 typedef struct
87 char *raws;
88 int disable;
89 int sbr;
90 int user_fps;
91 uint32_t fps_num;
92 uint32_t fps_den;
93 uint32_t encoder_delay;
94 int16_t alternate_group;
95 uint16_t ISO_language;
96 uint16_t copyright_language;
97 char *copyright_notice;
98 char *handler_name;
99 } input_track_option_t;
101 typedef struct
103 lsmash_summary_t *summary;
104 input_track_option_t opt;
105 int active;
106 } input_track_t;
108 typedef struct
110 char *whole_track_option;
111 int num_of_track_delimiters;
112 } input_option_t;
114 typedef struct
116 input_option_t opt;
117 char *file_name;
118 importer_t *importer;
119 input_track_t track[MAX_NUM_OF_TRACKS];
120 uint32_t num_of_tracks;
121 uint32_t num_of_active_tracks;
122 uint32_t current_track_number;
123 } input_t;
125 typedef struct
127 lsmash_summary_t *summary;
128 lsmash_sample_t *sample;
129 int active;
130 uint32_t track_ID;
131 uint32_t timescale;
132 uint32_t timebase;
133 uint32_t sample_entry;
134 uint32_t current_sample_number;
135 uint32_t ctd_shift;
136 uint32_t priming_samples;
137 uint32_t last_delta;
138 uint64_t prev_dts;
139 int64_t start_offset;
140 double dts;
141 } output_track_t;
143 typedef struct
145 output_track_t *track;
146 uint32_t num_of_tracks;
147 uint32_t current_track_number;
148 } output_movie_t;
150 typedef struct
152 char *name;
153 lsmash_file_t *fh;
154 lsmash_file_parameters_t param;
155 output_movie_t movie;
156 } output_file_t;
158 typedef struct
160 lsmash_root_t *root;
161 output_file_t file;
162 } output_t;
164 typedef struct
166 option_t opt;
167 output_t output;
168 input_t input[MAX_NUM_OF_INPUTS];
169 uint32_t num_of_inputs;
170 } muxer_t;
172 static void cleanup_muxer( muxer_t *muxer )
174 if( !muxer )
175 return;
176 output_t *output = &muxer->output;
177 lsmash_close_file( &output->file.param );
178 lsmash_destroy_root( output->root );
179 if( output->file.movie.track )
181 for( uint32_t i = 0; i < output->file.movie.num_of_tracks; i++ )
183 output_track_t *out_track = &output->file.movie.track[i];
184 lsmash_delete_sample( out_track->sample );
186 lsmash_free( output->file.movie.track );
188 for( uint32_t i = 0; i < muxer->num_of_inputs; i++ )
190 input_t *input = &muxer->input[i];
191 lsmash_importer_close( input->importer );
192 for( uint32_t j = 0; j < input->num_of_tracks; j++ )
193 lsmash_cleanup_summary( input->track[j].summary );
197 #define eprintf( ... ) fprintf( stderr, __VA_ARGS__ )
198 #define REFRESH_CONSOLE eprintf( " \r" )
200 static int muxer_error( muxer_t *muxer, const char *message, ... )
202 cleanup_muxer( muxer );
203 REFRESH_CONSOLE;
204 eprintf( "Error: " );
205 va_list args;
206 va_start( args, message );
207 vfprintf( stderr, message, args );
208 va_end( args );
209 return -1;
212 static int error_message( const char *message, ... )
214 REFRESH_CONSOLE;
215 eprintf( "Error: " );
216 va_list args;
217 va_start( args, message );
218 vfprintf( stderr, message, args );
219 va_end( args );
220 return -1;
223 static void display_version( void )
225 eprintf( "\n"
226 "L-SMASH isom/mov multiplexer rev%s %s\n"
227 "Built on %s %s\n"
228 "Copyright (C) 2010-2014 L-SMASH project\n",
229 LSMASH_REV, LSMASH_GIT_HASH, __DATE__, __TIME__ );
232 static void display_help( void )
234 display_version();
235 eprintf( "\n"
236 "Usage: muxer [global_options] -i input1 [-i input2 -i input3 ...] -o output\n"
237 "Global options:\n"
238 " --help Display help\n"
239 " --version Display version information\n"
240 " --optimize-pd Optimize for progressive download\n"
241 " --interleave <integer> Specify time interval for media interleaving in milliseconds\n"
242 " --file-format <string> Specify output file format\n"
243 " Multiple file format can be specified by comma separators\n"
244 " The first is applied as the best used one\n"
245 " --isom-version <integer> Specify maximum compatible ISO Base Media version\n"
246 " --shift-timeline Enable composition to decode timeline shift\n"
247 " --chapter <string> Set chapters from the file.\n"
248 " --chpl-with-bom Add UTF-8 BOM to the chapter strings\n"
249 " in the chapter list. (experimental)\n"
250 " --chapter-track <integer> Set which track the chapter applies to.\n"
251 " This option takes effect only when reference\n"
252 " chapter is available.\n"
253 " If this option is not used, it defaults to 1.\n"
254 " --copyright-notice <arg> Specify copyright notice with or without language (latter string)\n"
255 " <arg> is <string> or <string>/<string>\n"
256 " --language <string> Specify the default language for all the output tracks.\n"
257 " This option is overridden by the track options.\n"
258 "Output file formats:\n"
259 " mp4, mov, 3gp, 3g2, m4a, m4v\n"
260 "\n"
261 "Track options:\n"
262 " disable Disable this track\n"
263 " fps=<arg> Specify video framerate\n"
264 " <arg> is <integer> or <integer>/<integer>\n"
265 " language=<string> Specify media language\n"
266 " alternate-group=<integer> Specify alternate group\n"
267 " encoder-delay=<integer> Represent audio encoder delay (priming samples) explicitly\n"
268 " copyright=<arg> Specify copyright notice with or without language (latter string)\n"
269 " <arg> is <string> or <string>/<string>\n"
270 " handler=<string> Set media handler name\n"
271 " sbr Enable backward-compatible SBR explicit signaling mode\n"
272 "How to use track options:\n"
273 " -i input?[track_option1],[track_option2]...\n"
274 "\n"
275 "iTunes Metadata:\n"
276 " --album-name <string> Album name\n"
277 " --artist <string> Artist\n"
278 " --comment <string> User comment\n"
279 " --release-date <string> Release date (YYYY-MM-DD)\n"
280 " --encoder <string> Person or company that encoded the recording\n"
281 " --genre <string> Genre\n"
282 " --lyrics <string> Lyrics\n"
283 " --title <string> Title or song name\n"
284 " --composer <string> Composer\n"
285 " --album-artist <string> Artist for the whole album (if different than the individual tracks)\n"
286 " --copyright <string> Copyright\n"
287 " --description <string> Description\n"
288 " --grouping <string> Grouping\n"
289 " --tempo <integer> Beats per minute\n" );
292 static int muxer_usage_error( void )
294 display_help();
295 return -1;
298 #define MUXER_ERR( ... ) muxer_error( &muxer, __VA_ARGS__ )
299 #define ERROR_MSG( ... ) error_message( __VA_ARGS__ )
300 #define MUXER_USAGE_ERR() muxer_usage_error();
302 static int add_brand( option_t *opt, uint32_t brand )
304 if( opt->num_of_brands > MAX_NUM_OF_BRANDS )
305 return -1;
306 /* Avoid duplication. */
307 for( uint32_t i = 0; i < opt->num_of_brands; i++ )
308 if( opt->brands[i] == brand )
309 return -2;
310 opt->brands[opt->num_of_brands ++] = brand;
311 return 0;
314 static int setup_isom_version( option_t *opt )
316 add_brand( opt, ISOM_BRAND_TYPE_ISOM );
317 if( opt->isom_version > 6 )
318 return ERROR_MSG( "unknown ISO Base Media version.\n" );
319 #define SET_ISOM_VERSION( version ) \
320 if( opt->isom_version >= version ) \
321 add_brand( opt, ISOM_BRAND_TYPE_ISO##version )
322 SET_ISOM_VERSION( 2 );
323 SET_ISOM_VERSION( 3 );
324 SET_ISOM_VERSION( 4 );
325 SET_ISOM_VERSION( 5 );
326 SET_ISOM_VERSION( 6 );
327 #undef SET_ISOM_VERSION
328 return 0;
331 static int decide_brands( option_t *opt )
333 if( opt->num_of_brands == 0 )
335 /* default file format */
336 opt->major_brand = ISOM_BRAND_TYPE_MP42;
337 opt->minor_version = 0x00000000;
338 add_brand( opt, ISOM_BRAND_TYPE_MP42 );
339 add_brand( opt, ISOM_BRAND_TYPE_MP41 );
340 add_brand( opt, ISOM_BRAND_TYPE_ISOM );
341 opt->isom = 1;
342 eprintf( "MP4 muxing mode\n" );
343 return setup_isom_version( opt );
345 opt->major_brand = opt->brands[0]; /* Pick the first brand as major brand. */
346 for( uint32_t i = 0; i < opt->num_of_brands; i++ )
348 switch( opt->brands[i] )
350 case ISOM_BRAND_TYPE_3GP6 :
351 /* When being compatible with 3gp6, also compatible with 3g2a. */
352 add_brand( opt, ISOM_BRAND_TYPE_3G2A );
353 opt->brand_3gx = 1;
354 break;
355 case ISOM_BRAND_TYPE_3G2A :
356 opt->brand_3gx = 2;
357 break;
358 case ISOM_BRAND_TYPE_QT :
359 opt->qtff = 1;
360 break;
361 case ISOM_BRAND_TYPE_M4A :
362 case ISOM_BRAND_TYPE_M4V :
363 opt->itunes_movie = 1;
364 case ISOM_BRAND_TYPE_MP42 :
365 add_brand( opt, ISOM_BRAND_TYPE_MP42 );
366 add_brand( opt, ISOM_BRAND_TYPE_MP41 );
367 break;
368 default :
369 break;
371 if( opt->brands[i] != ISOM_BRAND_TYPE_QT )
372 opt->isom = 1;
374 switch( opt->major_brand )
376 case ISOM_BRAND_TYPE_MP42 :
377 opt->minor_version = 0x00000000;
378 eprintf( "MP4 muxing mode\n" );
379 break;
380 case ISOM_BRAND_TYPE_M4A :
381 case ISOM_BRAND_TYPE_M4V :
382 opt->minor_version = 0x00000000;
383 eprintf( "iTunes MP4 muxing mode\n" );
384 break;
385 case ISOM_BRAND_TYPE_3GP6 :
386 opt->minor_version = 0x00000000; /* means, 3gp(3gp6) 6.0.0 : "6" is not included in minor_version. */
387 eprintf( "3GPP muxing mode\n" );
388 break;
389 case ISOM_BRAND_TYPE_3G2A :
390 opt->minor_version = 0x00010000; /* means, 3g2(3g2a) 1.0.0 : a == 1 */
391 eprintf( "3GPP2 muxing mode\n" );
392 break;
393 case ISOM_BRAND_TYPE_QT :
394 opt->minor_version = 0x00000000; /* We don't know exact version of the spec to use QTFF features. */
395 eprintf( "QuickTime file format muxing mode\n" );
396 break;
397 default :
398 break;
400 /* Set up ISO Base Media version. */
401 if( opt->isom )
402 setup_isom_version( opt );
403 if( opt->num_of_brands > MAX_NUM_OF_BRANDS )
404 return ERROR_MSG( "exceed the maximum number of brands we can deal with.\n" );
405 return 0;
408 static int parse_global_options( int argc, char **argv, muxer_t *muxer )
410 if ( argc < 2 )
411 return -1;
412 else if( !strcasecmp( argv[1], "-h" ) || !strcasecmp( argv[1], "--help" ) )
414 muxer->opt.help = 1;
415 return 0;
417 else if( !strcasecmp( argv[1], "-v" ) || !strcasecmp( argv[1], "--version" ) )
419 muxer->opt.version = 1;
420 return 0;
422 else if( argc < 5 )
423 return -1;
424 uint32_t i = 1;
425 option_t *opt = &muxer->opt;
426 opt->chap_track = 1;
427 opt->add_bom_to_chpl = 0;
428 while( argc > i && *argv[i] == '-' )
430 #define CHECK_NEXT_ARG if( argc == ++i ) return -1
431 if( !strcasecmp( argv[i], "-i" ) || !strcasecmp( argv[i], "--input" ) )
433 CHECK_NEXT_ARG;
434 if( opt->num_of_inputs + 1 > MAX_NUM_OF_INPUTS )
435 return ERROR_MSG( "exceed the maximum number of input files.\n" );
436 input_t *input = &muxer->input[opt->num_of_inputs];
437 input_option_t *input_movie_opt = &input->opt;
438 char *p = argv[i];
439 while( *p )
440 input_movie_opt->num_of_track_delimiters += (*p++ == '?');
441 if( input_movie_opt->num_of_track_delimiters > MAX_NUM_OF_TRACKS )
442 return ERROR_MSG( "you specified options to exceed the maximum number of tracks per input files.\n" );
443 input->file_name = strtok( argv[i], "?" );
444 input_movie_opt->whole_track_option = strtok( NULL, "" );
445 if( input_movie_opt->num_of_track_delimiters )
447 input_track_option_t *track_opt = &input->track[0].opt;
448 track_opt->raws = strtok( input_movie_opt->whole_track_option, "?" );
449 #if (MAX_NUM_OF_TRACKS - 1)
450 for( uint32_t j = 1; j < input_movie_opt->num_of_track_delimiters; j++ )
452 track_opt = &input->track[j].opt;
453 track_opt->raws = strtok( NULL, "?" );
455 #endif
457 ++ opt->num_of_inputs;
459 else if( !strcasecmp( argv[i], "-o" ) || !strcasecmp( argv[i], "--output" ) )
461 CHECK_NEXT_ARG;
462 muxer->output.file.name = argv[i];
464 else if( !strcasecmp( argv[i], "--optimize-pd" ) )
465 opt->optimize_pd = 1;
466 else if( !strcasecmp( argv[i], "--interleave" ) )
468 CHECK_NEXT_ARG;
469 if( opt->interleave )
470 return ERROR_MSG( "you specified --interleave twice.\n" );
471 opt->interleave = atoi( argv[i] );
473 else if( !strcasecmp( argv[i], "--file-format" ) )
475 CHECK_NEXT_ARG;
476 static const struct
478 uint32_t brand_4cc;
479 char *file_format;
480 } file_format_list[]
482 { ISOM_BRAND_TYPE_MP42, "mp4" },
483 { ISOM_BRAND_TYPE_QT, "mov" },
484 { ISOM_BRAND_TYPE_3GP6, "3gp" },
485 { ISOM_BRAND_TYPE_3G2A, "3g2" },
486 { ISOM_BRAND_TYPE_M4A, "m4a" },
487 { ISOM_BRAND_TYPE_M4V, "m4v" },
488 { 0, NULL }
490 char *file_format = NULL;
491 while( (file_format = strtok( file_format ? NULL : argv[i], "," )) != NULL )
493 int j;
494 for( j = 0; file_format_list[j].file_format; j++ )
495 if( !strcmp( file_format, file_format_list[j].file_format ) )
497 int ret = add_brand( opt, file_format_list[j].brand_4cc );
498 if( ret == -2 )
499 return ERROR_MSG( "you specified same output file format twice.\n" );
500 else if( ret == -1 )
501 return ERROR_MSG( "exceed the maximum number of brands we can deal with.\n" );
502 break;
504 if( !file_format_list[j].file_format )
505 return MUXER_USAGE_ERR();
508 else if( !strcasecmp( argv[i], "--isom-version" ) )
510 CHECK_NEXT_ARG;
511 if( opt->isom_version )
512 return ERROR_MSG( "you specified --isom-version twice.\n" );
513 opt->isom_version = atoi( argv[i] );
515 else if( !strcasecmp( argv[i], "--shift-timeline" ) )
516 opt->timeline_shift = 1;
517 else if( !strcasecmp( argv[i], "--chapter" ) )
519 CHECK_NEXT_ARG;
520 opt->chap_file = argv[i];
522 else if( !strcasecmp( argv[i], "--chapter-track" ) )
524 CHECK_NEXT_ARG;
525 opt->chap_track = atoi( argv[i] );
526 if( !opt->chap_track )
527 return ERROR_MSG( "%s is an invalid track number.\n", argv[i] );
529 else if( !strcasecmp( argv[i], "--chpl-with-bom" ) )
530 opt->add_bom_to_chpl = 1;
531 else if( !strcasecmp( argv[i], "--copyright-notice" ) )
533 CHECK_NEXT_ARG;
534 if( opt->copyright_notice )
535 return ERROR_MSG( "you specified --copyright-notice twice.\n" );
536 opt->copyright_notice = argv[i];
537 char *language = opt->copyright_notice;
538 while( *language )
540 if( *language == '/' )
542 *language++ = '\0';
543 break;
545 ++language;
547 opt->copyright_language = language ? lsmash_pack_iso_language( language ) : ISOM_LANGUAGE_CODE_UNDEFINED;
549 /* iTunes metadata */
550 #define CHECK_ITUNES_METADATA_ARG_STRING( argument, value ) \
551 else if( !strcasecmp( argv[i], "--"#argument ) ) \
553 CHECK_NEXT_ARG; \
554 if( opt->itunes_metadata.value ) \
555 return ERROR_MSG( "you specified --"#argument" twice.\n" ); \
556 opt->itunes_metadata.value = argv[i]; \
558 CHECK_ITUNES_METADATA_ARG_STRING( album-name, album_name )
559 CHECK_ITUNES_METADATA_ARG_STRING( artist, artist )
560 CHECK_ITUNES_METADATA_ARG_STRING( comment, comment )
561 CHECK_ITUNES_METADATA_ARG_STRING( release-date, release_date )
562 CHECK_ITUNES_METADATA_ARG_STRING( encoder, encoder )
563 CHECK_ITUNES_METADATA_ARG_STRING( genre, genre )
564 CHECK_ITUNES_METADATA_ARG_STRING( lyrics, lyrics )
565 CHECK_ITUNES_METADATA_ARG_STRING( title, title )
566 CHECK_ITUNES_METADATA_ARG_STRING( composer, composer )
567 CHECK_ITUNES_METADATA_ARG_STRING( album-artist, album_artist )
568 CHECK_ITUNES_METADATA_ARG_STRING( copyright, copyright )
569 CHECK_ITUNES_METADATA_ARG_STRING( description, description )
570 CHECK_ITUNES_METADATA_ARG_STRING( grouping, grouping )
571 #undef CHECK_ITUNES_METADATA_ARG_STRING
572 else if( !strcasecmp( argv[i], "--tempo" ) )
574 CHECK_NEXT_ARG;
575 if( opt->itunes_metadata.beats_per_minute )
576 return ERROR_MSG( "you specified --tempo twice.\n" );
577 opt->itunes_metadata.beats_per_minute = atoi( argv[i] );
579 else if( !strcasecmp( argv[i], "--language" ) )
581 CHECK_NEXT_ARG;
582 opt->default_language = lsmash_pack_iso_language( argv[i] );
584 #undef CHECK_NEXT_ARG
585 else
586 return ERROR_MSG( "you specified invalid option: %s.\n", argv[i] );
587 ++i;
589 if( !muxer->output.file.name )
590 return ERROR_MSG( "output file name is not specified.\n" );
591 if( decide_brands( opt ) )
592 return ERROR_MSG( "failed to set up output file format.\n" );
593 if( opt->timeline_shift && !opt->qtff && opt->isom_version < 4 )
594 return ERROR_MSG( "timeline shift requires --file-format mov, or --isom-version 4 or later.\n" );
595 muxer->num_of_inputs = opt->num_of_inputs;
596 return 0;
599 static int parse_track_options( input_t *input )
601 for( input->current_track_number = 1;
602 input->current_track_number <= input->num_of_tracks;
603 input->current_track_number ++ )
605 input_track_t *in_track = &input->track[input->current_track_number - 1];
606 input_track_option_t *track_opt = &in_track->opt;
607 if( track_opt->raws == NULL )
608 break;
609 #if 0
610 if( !strchr( track_opt->raws, ':' )
611 || strchr( track_opt->raws, ':' ) == track_opt->raws )
612 return ERROR_MSG( "track number is not specified in %s\n", track_opt->raws );
613 if( strchr( track_opt->raws, ':' ) != strrchr( track_opt->raws, ':' ) )
614 return ERROR_MSG( "multiple colons inside one track option in %s.\n", track_opt->raws );
615 uint32_t track_number = atoi( strtok( track_opt->raws, ":" ) );
616 if( track_number == 0 || track_number > MAX_NUM_OF_TRACKS )
617 return ERROR_MSG( "%s is an invalid track number %"PRIu32".\n", strtok( track_opt->raws, ":" ), track_number );
618 in_track = &input->track[track_number - 1];
619 track_opt = &in_track->opt;
620 char *track_option;
621 while( (track_option = strtok( NULL, "," )) != NULL )
622 #else
623 char *track_option = NULL;
624 while( (track_option = strtok( track_option ? NULL : track_opt->raws, "," )) != NULL )
625 #endif
627 if( strchr( track_option, '=' ) != strrchr( track_option, '=' ) )
628 return ERROR_MSG( "multiple equal signs inside one track option in %s\n", track_option );
629 if( strstr( track_option, "disable" ) )
630 track_opt->disable = 1;
631 else if( strstr( track_option, "alternate-group=" ) )
633 char *track_parameter = strchr( track_option, '=' ) + 1;
634 track_opt->alternate_group = atoi( track_parameter );
636 else if( strstr( track_option, "encoder-delay=" ) )
638 char *track_parameter = strchr( track_option, '=' ) + 1;
639 track_opt->encoder_delay = atoi( track_parameter );
641 else if( strstr( track_option, "language=" ) )
643 char *track_parameter = strchr( track_option, '=' ) + 1;
644 track_opt->ISO_language = lsmash_pack_iso_language( track_parameter );
646 else if( strstr( track_option, "fps=" ) )
648 char *track_parameter = strchr( track_option, '=' ) + 1;
649 if( sscanf( track_parameter, "%"SCNu32"/%"SCNu32, &track_opt->fps_num, &track_opt->fps_den ) == 1 )
651 track_opt->fps_num = atoi( track_parameter );
652 track_opt->fps_den = 1;
654 track_opt->user_fps = 1;
656 else if( strstr( track_option, "copyright=" ) )
658 char *track_parameter = strchr( track_option, '=' ) + 1;
659 track_opt->copyright_notice = track_parameter;
660 while( *track_parameter )
662 if( *track_parameter == '/' )
664 *track_parameter++ = '\0';
665 break;
667 ++track_parameter;
669 track_opt->copyright_language = track_parameter ? lsmash_pack_iso_language( track_parameter ) : ISOM_LANGUAGE_CODE_UNDEFINED;
671 else if( strstr( track_option, "handler=" ) )
673 char *track_parameter = strchr( track_option, '=' ) + 1;
674 track_opt->handler_name = track_parameter;
676 else if( strstr( track_option, "sbr" ) )
677 track_opt->sbr = 1;
678 else
679 return ERROR_MSG( "unknown track option %s\n", track_option );
682 return 0;
685 static void display_codec_name( lsmash_codec_type_t codec_type, uint32_t track_number )
687 #define DISPLAY_CODEC_NAME( codec, codec_name ) \
688 else if( lsmash_check_codec_type_identical( codec_type, codec ) ) \
689 eprintf( "Track %"PRIu32": "#codec_name"\n", track_number )
690 if( 0 );
691 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_AVC1_VIDEO, H.264 Advanced Video Coding );
692 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_HVC1_VIDEO, H.265 High Efficiency Video Coding );
693 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_VC_1_VIDEO, SMPTE VC-1 Advanced Profile );
694 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_MP4A_AUDIO, MPEG-4 Audio );
695 DISPLAY_CODEC_NAME( QT_CODEC_TYPE_MP4A_AUDIO, MPEG-4 Audio );
696 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_AC_3_AUDIO, AC-3 );
697 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_EC_3_AUDIO, Enhanced AC-3 );
698 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_DTSC_AUDIO, DTS );
699 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_DTSE_AUDIO, DTS LBR );
700 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_DTSH_AUDIO, DTS-HD );
701 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_DTSL_AUDIO, DTS-HD Lossless );
702 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_SAWB_AUDIO, Wideband AMR voice );
703 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_SAMR_AUDIO, Narrowband AMR voice );
704 #undef DISPLAY_CODEC_NAME
707 static int open_input_files( muxer_t *muxer )
709 output_movie_t *out_movie = &muxer->output.file.movie;
710 option_t *opt = &muxer->opt;
711 for( uint32_t current_input_number = 1; current_input_number <= muxer->num_of_inputs; current_input_number++ )
713 input_t *input = &muxer->input[current_input_number - 1];
714 /* Initialize importer framework. */
715 input->importer = lsmash_importer_open( input->file_name, "auto" );
716 if( !input->importer )
717 return ERROR_MSG( "failed to open input file.\n" );
718 input->num_of_tracks = lsmash_importer_get_track_count( input->importer );
719 if( input->num_of_tracks == 0 )
720 return ERROR_MSG( "there is no valid track in input file.\n" );
721 if( opt->default_language )
722 for( int i = 0; i < input->num_of_tracks; i ++ )
723 input->track[i].opt.ISO_language = opt->default_language;
724 /* Parse track options */
725 if( parse_track_options( input ) )
726 return ERROR_MSG( "failed to parse track options.\n" );
727 /* Activate tracks by CODEC type. */
728 for( input->current_track_number = 1;
729 input->current_track_number <= input->num_of_tracks;
730 input->current_track_number ++ )
732 input_track_t *in_track = &input->track[input->current_track_number - 1];
733 in_track->summary = lsmash_duplicate_summary( input->importer, input->current_track_number );
734 if( !in_track->summary )
735 return ERROR_MSG( "failed to get input summary.\n" );
736 /* Check codec type. */
737 lsmash_codec_type_t codec_type = in_track->summary->sample_type;
738 in_track->active = 1;
739 if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_AVC1_VIDEO ) )
741 if( opt->isom )
742 add_brand( opt, ISOM_BRAND_TYPE_AVC1 );
744 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_HVC1_VIDEO ) )
746 if( !opt->isom && opt->qtff )
747 return ERROR_MSG( "the input seems HEVC, at present available only for ISO Base Media file format.\n" );
749 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_VC_1_VIDEO ) )
751 if( !opt->isom && opt->qtff )
752 return ERROR_MSG( "the input seems VC-1, at present available only for ISO Base Media file format.\n" );
754 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_MP4A_AUDIO )
755 || lsmash_check_codec_type_identical( codec_type, QT_CODEC_TYPE_MP4A_AUDIO ) )
756 /* Do nothing. */;
757 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_AC_3_AUDIO )
758 || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_EC_3_AUDIO ) )
760 if( !opt->isom && opt->qtff )
761 return ERROR_MSG( "the input seems (Enhanced) AC-3, at present available only for ISO Base Media file format.\n" );
762 add_brand( opt, ISOM_BRAND_TYPE_DBY1 );
764 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSC_AUDIO )
765 || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSE_AUDIO )
766 || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSH_AUDIO )
767 || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSL_AUDIO ) )
769 if( !opt->isom && opt->qtff )
770 return ERROR_MSG( "the input seems DTS(-HD) Audio, at present available only for ISO Base Media file format.\n" );
772 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_SAWB_AUDIO )
773 || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_SAMR_AUDIO ) )
775 if( !opt->brand_3gx )
776 return ERROR_MSG( "the input seems AMR-NB/WB, available for 3GPP(2) file format.\n" );
778 else
780 lsmash_cleanup_summary( in_track->summary );
781 in_track->summary = NULL;
782 in_track->active = 0;
784 if( in_track->active )
786 ++ input->num_of_active_tracks;
787 display_codec_name( codec_type, out_movie->num_of_tracks + input->num_of_active_tracks );
790 out_movie->num_of_tracks += input->num_of_active_tracks;
792 if( out_movie->num_of_tracks == 0 )
793 return ERROR_MSG( "there is no media that can be stored in output movie.\n" );
794 return 0;
797 static int set_itunes_metadata( output_t *output, option_t *opt )
799 if( !opt->itunes_movie )
800 return 0;
801 itunes_metadata_t *metadata = &opt->itunes_metadata;
802 #define SET_ITUNES_METADATA( item, type, value ) \
803 if( value \
804 && lsmash_set_itunes_metadata( output->root, (lsmash_itunes_metadata_t){ item, ITUNES_METADATA_TYPE_NONE, { .type = value }, NULL, NULL } ) ) \
805 return -1
806 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_ENCODING_TOOL, string, "L-SMASH" );
807 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_ALBUM_NAME, string, metadata->album_name );
808 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_ARTIST, string, metadata->artist );
809 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_USER_COMMENT, string, metadata->comment );
810 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_RELEASE_DATE, string, metadata->release_date );
811 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_ENCODED_BY, string, metadata->encoder );
812 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_USER_GENRE, string, metadata->genre );
813 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_LYRICS, string, metadata->lyrics );
814 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_TITLE, string, metadata->title );
815 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_COMPOSER, string, metadata->composer );
816 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_ALBUM_ARTIST, string, metadata->album_artist );
817 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_COPYRIGHT, string, metadata->copyright );
818 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_DESCRIPTION, string, metadata->description );
819 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_GROUPING, string, metadata->grouping );
820 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_BEATS_PER_MINUTE, integer, metadata->beats_per_minute );
821 #undef SET_ITUNES_METADATA
822 return 0;
825 static int prepare_output( muxer_t *muxer )
827 option_t *opt = &muxer->opt;
828 output_t *output = &muxer->output;
829 output_file_t *out_file = &output->file;
830 output_movie_t *out_movie = &out_file->movie;
831 /* Allocate output tracks. */
832 out_movie->track = lsmash_malloc( out_movie->num_of_tracks * sizeof(output_track_t) );
833 if( !out_movie->track )
834 return ERROR_MSG( "failed to allocate output tracks.\n" );
835 memset( out_movie->track, 0, out_movie->num_of_tracks * sizeof(output_track_t) );
836 /* Initialize L-SMASH muxer */
837 output->root = lsmash_create_root();
838 if( !output->root )
839 return ERROR_MSG( "failed to create a ROOT.\n" );
840 lsmash_file_parameters_t *file_param = &out_file->param;
841 if( lsmash_open_file( out_file->name, 0, file_param ) < 0 )
842 return ERROR_MSG( "failed to open an output file.\n" );
843 file_param->major_brand = opt->major_brand;
844 file_param->brands = opt->brands;
845 file_param->brand_count = opt->num_of_brands;
846 file_param->minor_version = opt->minor_version;
847 if( opt->interleave )
848 file_param->max_chunk_duration = opt->interleave * 1e-3;
849 out_file->fh = lsmash_set_file( output->root, file_param );
850 if( !out_file->fh )
851 return ERROR_MSG( "failed to add an output file into a ROOT.\n" );
852 /* Initialize movie */
853 lsmash_movie_parameters_t movie_param;
854 lsmash_initialize_movie_parameters( &movie_param );
855 if( lsmash_set_movie_parameters( output->root, &movie_param ) )
856 return ERROR_MSG( "failed to set movie parameters.\n" );
857 if( opt->copyright_notice
858 && lsmash_set_copyright( output->root, 0, opt->copyright_language, opt->copyright_notice ) )
859 return ERROR_MSG( "failed to set a copyright notice for the entire movie.\n" );
860 if( set_itunes_metadata( output, opt ) )
861 return ERROR_MSG( "failed to set iTunes metadata.\n" );
862 out_movie->current_track_number = 1;
863 for( uint32_t current_input_number = 1; current_input_number <= muxer->num_of_inputs; current_input_number++ )
865 input_t *input = &muxer->input[current_input_number - 1];
866 for( input->current_track_number = 1;
867 input->current_track_number <= input->num_of_tracks;
868 input->current_track_number ++ )
870 input_track_t *in_track = &input->track[ input->current_track_number - 1 ];
871 if( !in_track->active )
872 continue;
873 input_track_option_t *track_opt = &in_track->opt;
874 output_track_t *out_track = &out_movie->track[ out_movie->current_track_number - 1 ];
875 /* Set up track parameters. */
876 lsmash_track_parameters_t track_param;
877 lsmash_initialize_track_parameters( &track_param );
878 track_param.mode = ISOM_TRACK_IN_MOVIE | ISOM_TRACK_IN_PREVIEW;
879 if( !track_opt->disable )
880 track_param.mode |= ISOM_TRACK_ENABLED;
881 if( opt->qtff )
882 track_param.mode |= QT_TRACK_IN_POSTER;
883 track_param.alternate_group = track_opt->alternate_group;
884 lsmash_media_parameters_t media_param;
885 lsmash_initialize_media_parameters( &media_param );
886 media_param.ISO_language = track_opt->ISO_language;
887 switch( in_track->summary->summary_type )
889 case LSMASH_SUMMARY_TYPE_VIDEO :
891 out_track->track_ID = lsmash_create_track( output->root, ISOM_MEDIA_HANDLER_TYPE_VIDEO_TRACK );
892 if( !out_track->track_ID )
893 return ERROR_MSG( "failed to create a track.\n" );
894 lsmash_video_summary_t *summary = (lsmash_video_summary_t *)in_track->summary;
895 uint64_t display_width = summary->width << 16;
896 uint64_t display_height = summary->height << 16;
897 if( summary->par_h && summary->par_v )
899 double sar = (double)summary->par_h / summary->par_v;
900 if( sar > 1.0 )
901 display_width *= sar;
902 else
903 display_height /= sar;
905 track_param.display_width = display_width;
906 track_param.display_height = display_height;
907 /* Initialize media */
908 uint32_t timescale = 25; /* default value */
909 uint32_t timebase = 1; /* default value */
910 if( track_opt->user_fps )
912 timescale = track_opt->fps_num << (!!summary->sample_per_field);
913 timebase = track_opt->fps_den;
915 else if( !summary->vfr )
917 if( lsmash_check_codec_type_identical( summary->sample_type, ISOM_CODEC_TYPE_AVC1_VIDEO )
918 || lsmash_check_codec_type_identical( summary->sample_type, ISOM_CODEC_TYPE_HVC1_VIDEO ) )
920 uint32_t compare_timebase = summary->timebase;
921 uint32_t compare_timescale = summary->timescale;
922 static const struct
924 uint32_t timescale;
925 uint32_t timebase;
926 } well_known_fps[]
928 { 24000, 1001 }, { 30000, 1001 }, { 60000, 1001 }, { 120000, 1001 }, { 72000, 1001 },
929 { 25, 1 }, { 50, 1 }, { 24, 1 }, { 30, 1 }, { 60, 1 }, { 120, 1 }, { 72, 1 }, { 0, 0 }
931 for( int i = 0; well_known_fps[i].timescale; i++ )
932 if( well_known_fps[i].timescale == compare_timescale
933 && well_known_fps[i].timebase == compare_timebase )
935 timescale = well_known_fps[i].timescale;
936 timebase = well_known_fps[i].timebase;
937 break;
939 lsmash_codec_specific_t *bitrate = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264_BITRATE,
940 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
941 if( bitrate )
942 lsmash_add_codec_specific_data( in_track->summary, bitrate );
943 lsmash_destroy_codec_specific_data( bitrate );
945 else
947 timescale = summary->timescale;
948 timebase = summary->timebase;
951 media_param.timescale = timescale;
952 media_param.media_handler_name = track_opt->handler_name ? track_opt->handler_name : "L-SMASH Video Handler";
953 media_param.roll_grouping = 1;
954 media_param.rap_grouping = opt->isom_version >= 6;
955 out_track->timescale = timescale;
956 out_track->timebase = timebase;
957 break;
959 case LSMASH_SUMMARY_TYPE_AUDIO :
961 out_track->track_ID = lsmash_create_track( output->root, ISOM_MEDIA_HANDLER_TYPE_AUDIO_TRACK );
962 if( !out_track->track_ID )
963 return ERROR_MSG( "failed to create a track.\n" );
964 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)in_track->summary;
965 if( track_opt->sbr )
967 /* Check if explicit SBR is valid or not. */
968 if( lsmash_mp4sys_get_object_type_indication( (lsmash_summary_t *)summary ) != MP4SYS_OBJECT_TYPE_Audio_ISO_14496_3 )
969 return ERROR_MSG( "--sbr is only valid with MPEG-4 Audio.\n" );
970 summary->sbr_mode = MP4A_AAC_SBR_BACKWARD_COMPATIBLE;
971 if( lsmash_setup_AudioSpecificConfig( summary ) )
972 return ERROR_MSG( "failed to set SBR mode.\n" );
974 media_param.timescale = summary->frequency;
975 media_param.media_handler_name = track_opt->handler_name ? track_opt->handler_name : "L-SMASH Audio Handler";
976 media_param.roll_grouping = (opt->isom_version >= 2 || opt->qtff);
977 out_track->priming_samples = track_opt->encoder_delay;
978 out_track->timescale = summary->frequency;
979 out_track->timebase = 1;
980 break;
982 default :
983 return ERROR_MSG( "not supported stream type.\n" );
985 /* Reset the movie timescale in order to match the media timescale if only one track is there. */
986 if( muxer->num_of_inputs == 1
987 && current_input_number == 1
988 && input->current_track_number == 1 )
990 movie_param.timescale = media_param.timescale;
991 if( lsmash_set_movie_parameters( output->root, &movie_param ) )
992 return ERROR_MSG( "failed to set movie parameters.\n" );
994 /* Set copyright information. */
995 if( track_opt->copyright_notice
996 && lsmash_set_copyright( output->root, out_track->track_ID, track_opt->copyright_language, track_opt->copyright_notice ) )
997 return ERROR_MSG( "failed to set a copyright notice.\n" );
998 /* Set track parameters. */
999 if( lsmash_set_track_parameters( output->root, out_track->track_ID, &track_param ) )
1000 return ERROR_MSG( "failed to set track parameters.\n" );
1001 /* Set media parameters. */
1002 if( lsmash_set_media_parameters( output->root, out_track->track_ID, &media_param ) )
1003 return ERROR_MSG( "failed to set media parameters.\n" );
1004 out_track->summary = in_track->summary;
1005 out_track->sample_entry = lsmash_add_sample_entry( output->root, out_track->track_ID, out_track->summary );
1006 if( !out_track->sample_entry )
1007 return ERROR_MSG( "failed to add sample description entry.\n" );
1008 out_track->active = 1;
1009 ++ out_movie->current_track_number;
1011 input->current_track_number = 1;
1013 out_movie->current_track_number = 1;
1014 return 0;
1017 static void set_reference_chapter_track( output_t *output, option_t *opt )
1019 if( !opt->chap_file || (!opt->qtff && !opt->itunes_movie) || (opt->brand_3gx == 1) )
1020 return;
1021 lsmash_create_reference_chapter_track( output->root, opt->chap_track, opt->chap_file );
1024 static int do_mux( muxer_t *muxer )
1026 #define LSMASH_MAX( a, b ) ((a) > (b) ? (a) : (b))
1027 option_t *opt = &muxer->opt;
1028 output_t *output = &muxer->output;
1029 output_movie_t *out_movie = &output->file.movie;
1030 set_reference_chapter_track( output, opt );
1031 double largest_dts = 0;
1032 uint32_t current_input_number = 1;
1033 uint32_t num_consecutive_sample_skip = 0;
1034 uint32_t num_active_input_tracks = out_movie->num_of_tracks;
1035 uint64_t total_media_size = 0;
1036 uint8_t sample_count = 0;
1037 while( 1 )
1039 input_t *input = &muxer->input[current_input_number - 1];
1040 output_track_t *out_track = &out_movie->track[ out_movie->current_track_number - 1 ];
1041 if( out_track->active )
1043 lsmash_sample_t *sample = out_track->sample;
1044 /* Get a new sample data if the track doesn't hold any one. */
1045 if( !sample )
1047 /* Allocate sample buffer. */
1048 sample = lsmash_create_sample( out_track->summary->max_au_length );
1049 if( !sample )
1050 return ERROR_MSG( "failed to alloc memory for buffer.\n" );
1051 /* lsmash_importer_get_access_unit() returns 1 if there're any changes in stream's properties. */
1052 int ret = lsmash_importer_get_access_unit( input->importer, input->current_track_number, sample );
1053 if( ret == -1 )
1055 lsmash_delete_sample( sample );
1056 ERROR_MSG( "failed to get a frame from input file. Maybe corrupted.\n"
1057 "Aborting muxing operation and trying to let output be valid file.\n" );
1058 break;
1060 else if( ret == 1 )
1062 input_track_t *in_track = &input->track[input->current_track_number - 1];
1063 lsmash_cleanup_summary( in_track->summary );
1064 in_track->summary = lsmash_duplicate_summary( input->importer, input->current_track_number );
1065 out_track->summary = in_track->summary;
1066 out_track->sample_entry = lsmash_add_sample_entry( output->root, out_track->track_ID, out_track->summary );
1067 if( !out_track->sample_entry )
1069 ERROR_MSG( "failed to add sample description entry.\n" );
1070 break;
1073 if( sample->length == 0 )
1075 /* No more appendable samples in this track. */
1076 lsmash_delete_sample( sample );
1077 sample = NULL;
1078 out_track->active = 0;
1079 out_track->last_delta = lsmash_importer_get_last_delta( input->importer, input->current_track_number );
1080 if( out_track->last_delta == 0 )
1081 ERROR_MSG( "failed to get the last sample delta.\n" );
1082 out_track->last_delta *= out_track->timebase;
1083 if( --num_active_input_tracks == 0 )
1084 break; /* Reached the end of whole tracks. */
1086 else
1088 sample->index = out_track->sample_entry;
1089 sample->dts *= out_track->timebase;
1090 sample->cts *= out_track->timebase;
1091 if( opt->timeline_shift )
1093 if( out_track->current_sample_number == 0 )
1094 out_track->ctd_shift = sample->cts;
1095 sample->cts -= out_track->ctd_shift;
1097 out_track->dts = (double)sample->dts / out_track->timescale;
1098 out_track->sample = sample;
1101 if( sample )
1103 /* Append a sample if meeting a condition. */
1104 if( out_track->dts <= largest_dts || num_consecutive_sample_skip == num_active_input_tracks )
1106 uint64_t sample_size = sample->length; /* sample might be deleted internally after appending. */
1107 uint64_t sample_dts = sample->dts; /* same as above */
1108 uint64_t sample_cts = sample->cts; /* same as above */
1109 if( lsmash_append_sample( output->root, out_track->track_ID, sample ) )
1110 return ERROR_MSG( "failed to append a sample.\n" );
1111 if( out_track->current_sample_number == 0 )
1112 out_track->start_offset = sample_cts;
1113 else
1114 out_track->last_delta = sample_dts - out_track->prev_dts; /* for any changes in stream's properties */
1115 out_track->prev_dts = sample_dts;
1116 out_track->sample = NULL;
1117 largest_dts = LSMASH_MAX( largest_dts, out_track->dts );
1118 total_media_size += sample_size;
1119 ++ out_track->current_sample_number;
1120 num_consecutive_sample_skip = 0;
1121 /* Print, per 256 samples, total size of imported media. */
1122 if( ++sample_count == 0 )
1124 REFRESH_CONSOLE;
1125 eprintf( "Importing: %"PRIu64" bytes\r", total_media_size );
1128 else
1129 ++num_consecutive_sample_skip; /* Skip appendig sample. */
1132 if( ++ out_movie->current_track_number > out_movie->num_of_tracks )
1133 out_movie->current_track_number = 1; /* Back the first output track. */
1134 /* Move the next track. */
1135 if( ++ input->current_track_number > input->num_of_tracks )
1137 /* Move the next input movie. */
1138 input->current_track_number = 1;
1139 if( ++ current_input_number > muxer->num_of_inputs )
1140 current_input_number = 1; /* Back the first input movie. */
1143 for( out_movie->current_track_number = 1;
1144 out_movie->current_track_number <= out_movie->num_of_tracks;
1145 out_movie->current_track_number ++ )
1147 /* Close track. */
1148 output_track_t *out_track = &out_movie->track[ out_movie->current_track_number - 1 ];
1149 if( lsmash_flush_pooled_samples( output->root, out_track->track_ID, out_track->last_delta ) )
1150 ERROR_MSG( "failed to flush the rest of samples.\n" );
1151 /* Create edit list.
1152 * Don't trust media duration. It's just duration of media, not duration of track presentation.
1153 * Calculation of presentation duration by DTS is reliable since this muxer handles CFR only. */
1154 uint64_t actual_duration = out_track->prev_dts + out_track->last_delta - out_track->priming_samples;
1155 lsmash_edit_t edit;
1156 edit.duration = actual_duration * ((double)lsmash_get_movie_timescale( output->root ) / out_track->timescale);
1157 edit.start_time = out_track->priming_samples + out_track->start_offset;
1158 edit.rate = ISOM_EDIT_MODE_NORMAL;
1159 if( lsmash_create_explicit_timeline_map( output->root, out_track->track_ID, edit ) )
1160 ERROR_MSG( "failed to set timeline map.\n" );
1162 return 0;
1163 #undef LSMASH_MAX
1166 static int moov_to_front_callback( void *param, uint64_t written_movie_size, uint64_t total_movie_size )
1168 REFRESH_CONSOLE;
1169 eprintf( "Finalizing: [%5.2lf%%]\r", total_movie_size ? ((double)written_movie_size / total_movie_size) * 100.0 : 0 );
1170 return 0;
1173 static int finish_movie( output_t *output, option_t *opt )
1175 /* Set chapter list. */
1176 if( opt->chap_file )
1177 lsmash_set_tyrant_chapter( output->root, opt->chap_file, opt->add_bom_to_chpl );
1178 /* Close movie. */
1179 REFRESH_CONSOLE;
1180 if( opt->optimize_pd )
1182 lsmash_adhoc_remux_t moov_to_front;
1183 moov_to_front.func = moov_to_front_callback;
1184 moov_to_front.buffer_size = 4*1024*1024; /* 4MiB */
1185 moov_to_front.param = NULL;
1186 return lsmash_finish_movie( output->root, &moov_to_front );
1188 if( lsmash_finish_movie( output->root, NULL ) )
1189 return -1;
1190 return lsmash_write_lsmash_indicator( output->root );
1193 int main( int argc, char *argv[] )
1195 muxer_t muxer = { { 0 } };
1196 lsmash_get_mainargs( &argc, &argv );
1197 if( parse_global_options( argc, argv, &muxer ) )
1198 return MUXER_USAGE_ERR();
1199 if( muxer.opt.help )
1201 display_help();
1202 cleanup_muxer( &muxer );
1203 return 0;
1205 else if( muxer.opt.version )
1207 display_version();
1208 cleanup_muxer( &muxer );
1209 return 0;
1211 if( open_input_files( &muxer ) )
1212 return MUXER_ERR( "failed to open input files.\n" );
1213 if( prepare_output( &muxer ) )
1214 return MUXER_ERR( "failed to set up preparation for output.\n" );
1215 if( do_mux( &muxer ) )
1216 return MUXER_ERR( "failed to do muxing.\n" );
1217 if( finish_movie( &muxer.output, &muxer.opt ) )
1218 return MUXER_ERR( "failed to finish movie.\n" );
1219 REFRESH_CONSOLE;
1220 eprintf( "Muxing completed!\n" );
1221 cleanup_muxer( &muxer ); /* including lsmash_destroy_root() */
1222 return 0;