dts: Support DTS:X muxing.
[L-SMASH.git] / cli / muxer.c
blob85d7c5f4f4fd544077f43c4e521a0b0bab4cc53a
1 /*****************************************************************************
2 * muxer.c:
3 *****************************************************************************
4 * Copyright (C) 2010-2015 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 "cli.h"
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <inttypes.h>
32 #include "importer/importer.h"
34 #define MAX_NUM_OF_BRANDS 50
35 #define MAX_NUM_OF_INPUTS 10
36 #define MAX_NUM_OF_TRACKS 1
38 typedef struct
40 char *album_name;
41 char *artist;
42 char *comment;
43 char *release_date;
44 char *encoder;
45 char *genre;
46 char *lyrics;
47 char *title;
48 char *composer;
49 char *album_artist;
50 char *copyright;
51 char *description;
52 char *grouping;
53 uint32_t beats_per_minute;
54 } itunes_metadata_t;
56 typedef struct
58 int help;
59 int version;
60 int isom;
61 int isom_version;
62 int itunes_movie;
63 int qtff;
64 int brand_3gx;
65 int optimize_pd;
66 int timeline_shift;
67 int compact_size_table;
68 uint32_t interleave;
69 uint32_t num_of_brands;
70 uint32_t brands[MAX_NUM_OF_BRANDS];
71 uint32_t major_brand;
72 uint32_t minor_version;
73 uint32_t num_of_inputs;
74 uint32_t chap_track;
75 char *chap_file;
76 int add_bom_to_chpl;
77 char *copyright_notice;
78 uint16_t copyright_language;
79 itunes_metadata_t itunes_metadata;
80 uint16_t default_language;
81 } option_t;
83 typedef struct
85 char *raws;
86 int disable;
87 int sbr;
88 int user_fps;
89 uint32_t fps_num;
90 uint32_t fps_den;
91 uint32_t encoder_delay;
92 int16_t alternate_group;
93 uint16_t ISO_language;
94 uint16_t copyright_language;
95 char *copyright_notice;
96 char *handler_name;
97 uint32_t par_h;
98 uint32_t par_v;
99 } input_track_option_t;
101 typedef struct
103 lsmash_summary_t *summary;
104 input_track_option_t opt;
105 int active;
106 int lpcm;
107 } input_track_t;
109 typedef struct
111 char *whole_track_option;
112 int num_of_track_delimiters;
113 } input_option_t;
115 typedef struct
117 input_option_t opt;
118 char *file_name;
119 importer_t *importer;
120 input_track_t track[MAX_NUM_OF_TRACKS];
121 uint32_t num_of_tracks;
122 uint32_t num_of_active_tracks;
123 uint32_t current_track_number;
124 } input_t;
126 typedef struct
128 lsmash_summary_t *summary;
129 lsmash_sample_t *sample;
130 int active;
131 uint32_t track_ID;
132 uint32_t timescale;
133 uint32_t timebase;
134 uint32_t sample_entry;
135 uint32_t current_sample_number;
136 uint32_t ctd_shift;
137 uint32_t priming_samples;
138 uint32_t last_delta;
139 uint64_t prev_dts;
140 int64_t start_offset;
141 double dts;
142 int lpcm;
143 } output_track_t;
145 typedef struct
147 output_track_t *track;
148 uint32_t num_of_tracks;
149 uint32_t current_track_number;
150 } output_movie_t;
152 typedef struct
154 char *name;
155 lsmash_file_t *fh;
156 lsmash_file_parameters_t param;
157 output_movie_t movie;
158 } output_file_t;
160 typedef struct
162 lsmash_root_t *root;
163 output_file_t file;
164 } output_t;
166 typedef struct
168 option_t opt;
169 output_t output;
170 input_t input[MAX_NUM_OF_INPUTS];
171 uint32_t num_of_inputs;
172 } muxer_t;
174 static void cleanup_muxer( muxer_t *muxer )
176 if( !muxer )
177 return;
178 output_t *output = &muxer->output;
179 lsmash_close_file( &output->file.param );
180 lsmash_destroy_root( output->root );
181 if( output->file.movie.track )
183 for( uint32_t i = 0; i < output->file.movie.num_of_tracks; i++ )
185 output_track_t *out_track = &output->file.movie.track[i];
186 lsmash_delete_sample( out_track->sample );
188 lsmash_free( output->file.movie.track );
190 for( uint32_t i = 0; i < muxer->num_of_inputs; i++ )
192 input_t *input = &muxer->input[i];
193 lsmash_importer_close( input->importer );
194 for( uint32_t j = 0; j < input->num_of_tracks; j++ )
195 lsmash_cleanup_summary( input->track[j].summary );
199 #define eprintf( ... ) fprintf( stderr, __VA_ARGS__ )
200 #define REFRESH_CONSOLE eprintf( " \r" )
202 static int muxer_error( muxer_t *muxer, const char *message, ... )
204 cleanup_muxer( muxer );
205 REFRESH_CONSOLE;
206 eprintf( "Error: " );
207 va_list args;
208 va_start( args, message );
209 vfprintf( stderr, message, args );
210 va_end( args );
211 return -1;
214 static int error_message( const char *message, ... )
216 REFRESH_CONSOLE;
217 eprintf( "Error: " );
218 va_list args;
219 va_start( args, message );
220 vfprintf( stderr, message, args );
221 va_end( args );
222 return -1;
225 static void display_version( void )
227 eprintf( "\n"
228 "L-SMASH isom/mov multiplexer rev%s %s\n"
229 "Built on %s %s\n"
230 "Copyright (C) 2010-2015 L-SMASH project\n",
231 LSMASH_REV, LSMASH_GIT_HASH, __DATE__, __TIME__ );
234 static void display_help( void )
236 display_version();
237 eprintf( "\n"
238 "Usage: muxer [global_options] -i input1 [-i input2 -i input3 ...] -o output\n"
239 "Global options:\n"
240 " --help Display help\n"
241 " --version Display version information\n"
242 " --optimize-pd Optimize for progressive download\n"
243 " --interleave <integer> Specify time interval for media interleaving in milliseconds\n"
244 " --file-format <string> Specify output file format\n"
245 " Multiple file format can be specified by comma separators\n"
246 " The first is applied as the best used one\n"
247 " --isom-version <integer> Specify maximum compatible ISO Base Media version\n"
248 " --shift-timeline Enable composition to decode timeline shift\n"
249 " --chapter <string> Set chapters from the file.\n"
250 " --chpl-with-bom Add UTF-8 BOM to the chapter strings\n"
251 " in the chapter list. (experimental)\n"
252 " --chapter-track <integer> Set which track the chapter applies to.\n"
253 " This option takes effect only when reference\n"
254 " chapter is available.\n"
255 " If this option is not used, it defaults to 1.\n"
256 " --copyright-notice <arg> Specify copyright notice with or without language (latter string)\n"
257 " <arg> is <string> or <string>/<string>\n"
258 " --language <string> Specify the default language for all the output tracks.\n"
259 " This option is overridden by the track options.\n"
260 " --compact-size-table Compress sample size tables if possible.\n"
261 "Output file formats:\n"
262 " mp4, mov, 3gp, 3g2, m4a, m4v\n"
263 "\n"
264 "Track options:\n"
265 " disable Disable this track\n"
266 " fps=<arg> Specify video framerate\n"
267 " <arg> is <integer> or <integer>/<integer>\n"
268 " language=<string> Specify media language\n"
269 " alternate-group=<integer> Specify alternate group\n"
270 " encoder-delay=<integer> Represent audio encoder delay (priming samples) explicitly\n"
271 " copyright=<arg> Specify copyright notice with or without language (latter string)\n"
272 " <arg> is <string> or <string>/<string>\n"
273 " handler=<string> Set media handler name\n"
274 " sbr Enable backward-compatible SBR explicit signaling mode\n"
275 " par=<integer>:<integer> Specify pixel aspect ratio of the first sequence\n"
276 " And never change ones in the media stream.\n"
277 "How to use track options:\n"
278 " -i input?[track_option1],[track_option2]...\n"
279 "\n"
280 "iTunes Metadata:\n"
281 " --album-name <string> Album name\n"
282 " --artist <string> Artist\n"
283 " --comment <string> User comment\n"
284 " --release-date <string> Release date (YYYY-MM-DD)\n"
285 " --encoder <string> Person or company that encoded the recording\n"
286 " --genre <string> Genre\n"
287 " --lyrics <string> Lyrics\n"
288 " --title <string> Title or song name\n"
289 " --composer <string> Composer\n"
290 " --album-artist <string> Artist for the whole album (if different than the individual tracks)\n"
291 " --copyright <string> Copyright\n"
292 " --description <string> Description\n"
293 " --grouping <string> Grouping\n"
294 " --tempo <integer> Beats per minute\n" );
297 static int muxer_usage_error( void )
299 display_help();
300 return -1;
303 #define MUXER_ERR( ... ) muxer_error( &muxer, __VA_ARGS__ )
304 #define ERROR_MSG( ... ) error_message( __VA_ARGS__ )
305 #define MUXER_USAGE_ERR() muxer_usage_error();
307 static int add_brand( option_t *opt, uint32_t brand )
309 if( opt->num_of_brands >= MAX_NUM_OF_BRANDS )
310 return -1;
311 /* Avoid duplication. */
312 for( uint32_t i = 0; i < opt->num_of_brands; i++ )
313 if( opt->brands[i] == brand )
314 return -2;
315 opt->brands[opt->num_of_brands ++] = brand;
316 return 0;
319 static int setup_isom_version( option_t *opt )
321 add_brand( opt, ISOM_BRAND_TYPE_ISOM );
322 if( opt->isom_version > 6 )
323 return ERROR_MSG( "unknown ISO Base Media version.\n" );
324 #define SET_ISOM_VERSION( version ) \
325 if( opt->isom_version >= version ) \
326 add_brand( opt, ISOM_BRAND_TYPE_ISO##version )
327 SET_ISOM_VERSION( 2 );
328 SET_ISOM_VERSION( 3 );
329 SET_ISOM_VERSION( 4 );
330 SET_ISOM_VERSION( 5 );
331 SET_ISOM_VERSION( 6 );
332 #undef SET_ISOM_VERSION
333 return 0;
336 static int decide_brands( option_t *opt )
338 if( opt->num_of_brands == 0 )
340 /* default file format */
341 opt->major_brand = ISOM_BRAND_TYPE_MP42;
342 opt->minor_version = 0x00000000;
343 add_brand( opt, ISOM_BRAND_TYPE_MP42 );
344 add_brand( opt, ISOM_BRAND_TYPE_MP41 );
345 add_brand( opt, ISOM_BRAND_TYPE_ISOM );
346 opt->isom = 1;
347 eprintf( "MP4 muxing mode\n" );
348 return setup_isom_version( opt );
350 opt->major_brand = opt->brands[0]; /* Pick the first brand as major brand. */
351 for( uint32_t i = 0; i < opt->num_of_brands; i++ )
353 switch( opt->brands[i] )
355 case ISOM_BRAND_TYPE_3GP6 :
356 /* When being compatible with 3gp6, also compatible with 3g2a. */
357 add_brand( opt, ISOM_BRAND_TYPE_3G2A );
358 opt->brand_3gx = 1;
359 break;
360 case ISOM_BRAND_TYPE_3G2A :
361 opt->brand_3gx = 2;
362 break;
363 case ISOM_BRAND_TYPE_QT :
364 opt->qtff = 1;
365 break;
366 case ISOM_BRAND_TYPE_M4A :
367 case ISOM_BRAND_TYPE_M4V :
368 opt->itunes_movie = 1;
369 /* fall-through */
370 case ISOM_BRAND_TYPE_MP42 :
371 add_brand( opt, ISOM_BRAND_TYPE_MP42 );
372 add_brand( opt, ISOM_BRAND_TYPE_MP41 );
373 break;
374 default :
375 break;
377 if( opt->brands[i] != ISOM_BRAND_TYPE_QT )
378 opt->isom = 1;
380 switch( opt->major_brand )
382 case ISOM_BRAND_TYPE_MP42 :
383 opt->minor_version = 0x00000000;
384 eprintf( "MP4 muxing mode\n" );
385 break;
386 case ISOM_BRAND_TYPE_M4A :
387 case ISOM_BRAND_TYPE_M4V :
388 opt->minor_version = 0x00000000;
389 eprintf( "iTunes MP4 muxing mode\n" );
390 break;
391 case ISOM_BRAND_TYPE_3GP6 :
392 opt->minor_version = 0x00000000; /* means, 3gp(3gp6) 6.0.0 : "6" is not included in minor_version. */
393 eprintf( "3GPP muxing mode\n" );
394 break;
395 case ISOM_BRAND_TYPE_3G2A :
396 opt->minor_version = 0x00010000; /* means, 3g2(3g2a) 1.0.0 : a == 1 */
397 eprintf( "3GPP2 muxing mode\n" );
398 break;
399 case ISOM_BRAND_TYPE_QT :
400 opt->minor_version = 0x00000000; /* We don't know exact version of the spec to use QTFF features. */
401 eprintf( "QuickTime file format muxing mode\n" );
402 break;
403 default :
404 break;
406 /* Set up ISO Base Media version. */
407 if( opt->isom )
408 setup_isom_version( opt );
409 if( opt->num_of_brands > MAX_NUM_OF_BRANDS )
410 return ERROR_MSG( "exceed the maximum number of brands we can deal with.\n" );
411 return 0;
414 static int parse_global_options( int argc, char **argv, muxer_t *muxer )
416 if ( argc < 2 )
417 return -1;
418 else if( !strcasecmp( argv[1], "-h" ) || !strcasecmp( argv[1], "--help" ) )
420 muxer->opt.help = 1;
421 return 0;
423 else if( !strcasecmp( argv[1], "-v" ) || !strcasecmp( argv[1], "--version" ) )
425 muxer->opt.version = 1;
426 return 0;
428 else if( argc < 5 )
429 return -1;
430 uint32_t i = 1;
431 option_t *opt = &muxer->opt;
432 opt->chap_track = 1;
433 opt->add_bom_to_chpl = 0;
434 while( argc > i && *argv[i] == '-' )
436 #define CHECK_NEXT_ARG if( argc == ++i ) return -1
437 if( !strcasecmp( argv[i], "-i" ) || !strcasecmp( argv[i], "--input" ) )
439 CHECK_NEXT_ARG;
440 if( opt->num_of_inputs + 1 > MAX_NUM_OF_INPUTS )
441 return ERROR_MSG( "exceed the maximum number of input files.\n" );
442 input_t *input = &muxer->input[opt->num_of_inputs];
443 input_option_t *input_movie_opt = &input->opt;
444 char *p = argv[i];
445 while( *p )
446 input_movie_opt->num_of_track_delimiters += (*p++ == '?');
447 if( input_movie_opt->num_of_track_delimiters > MAX_NUM_OF_TRACKS )
448 return ERROR_MSG( "you specified options to exceed the maximum number of tracks per input files.\n" );
449 input->file_name = strtok( argv[i], "?" );
450 input_movie_opt->whole_track_option = strtok( NULL, "" );
451 if( input_movie_opt->num_of_track_delimiters )
453 input_track_option_t *track_opt = &input->track[0].opt;
454 track_opt->raws = strtok( input_movie_opt->whole_track_option, "?" );
455 #if (MAX_NUM_OF_TRACKS - 1)
456 for( uint32_t j = 1; j < input_movie_opt->num_of_track_delimiters; j++ )
458 track_opt = &input->track[j].opt;
459 track_opt->raws = strtok( NULL, "?" );
461 #endif
463 ++ opt->num_of_inputs;
465 else if( !strcasecmp( argv[i], "-o" ) || !strcasecmp( argv[i], "--output" ) )
467 CHECK_NEXT_ARG;
468 muxer->output.file.name = argv[i];
470 else if( !strcasecmp( argv[i], "--optimize-pd" ) )
471 opt->optimize_pd = 1;
472 else if( !strcasecmp( argv[i], "--interleave" ) )
474 CHECK_NEXT_ARG;
475 if( opt->interleave )
476 return ERROR_MSG( "you specified --interleave twice.\n" );
477 opt->interleave = atoi( argv[i] );
479 else if( !strcasecmp( argv[i], "--file-format" ) )
481 CHECK_NEXT_ARG;
482 static const struct
484 uint32_t brand_4cc;
485 char *file_format;
486 } file_format_list[]
488 { ISOM_BRAND_TYPE_MP42, "mp4" },
489 { ISOM_BRAND_TYPE_QT, "mov" },
490 { ISOM_BRAND_TYPE_3GP6, "3gp" },
491 { ISOM_BRAND_TYPE_3G2A, "3g2" },
492 { ISOM_BRAND_TYPE_M4A, "m4a" },
493 { ISOM_BRAND_TYPE_M4V, "m4v" },
494 { 0, NULL }
496 char *file_format = NULL;
497 while( (file_format = strtok( file_format ? NULL : argv[i], "," )) != NULL )
499 int j;
500 for( j = 0; file_format_list[j].file_format; j++ )
501 if( !strcmp( file_format, file_format_list[j].file_format ) )
503 int ret = add_brand( opt, file_format_list[j].brand_4cc );
504 if( ret == -2 )
505 return ERROR_MSG( "you specified same output file format twice.\n" );
506 else if( ret == -1 )
507 return ERROR_MSG( "exceed the maximum number of brands we can deal with.\n" );
508 break;
510 if( !file_format_list[j].file_format )
511 return MUXER_USAGE_ERR();
514 else if( !strcasecmp( argv[i], "--isom-version" ) )
516 CHECK_NEXT_ARG;
517 if( opt->isom_version )
518 return ERROR_MSG( "you specified --isom-version twice.\n" );
519 opt->isom_version = atoi( argv[i] );
521 else if( !strcasecmp( argv[i], "--shift-timeline" ) )
522 opt->timeline_shift = 1;
523 else if( !strcasecmp( argv[i], "compact-size-table" ) )
524 opt->compact_size_table = 1;
525 else if( !strcasecmp( argv[i], "--chapter" ) )
527 CHECK_NEXT_ARG;
528 opt->chap_file = argv[i];
530 else if( !strcasecmp( argv[i], "--chapter-track" ) )
532 CHECK_NEXT_ARG;
533 opt->chap_track = atoi( argv[i] );
534 if( !opt->chap_track )
535 return ERROR_MSG( "%s is an invalid track number.\n", argv[i] );
537 else if( !strcasecmp( argv[i], "--chpl-with-bom" ) )
538 opt->add_bom_to_chpl = 1;
539 else if( !strcasecmp( argv[i], "--copyright-notice" ) )
541 CHECK_NEXT_ARG;
542 if( opt->copyright_notice )
543 return ERROR_MSG( "you specified --copyright-notice twice.\n" );
544 opt->copyright_notice = argv[i];
545 char *language = opt->copyright_notice;
546 while( *language )
548 if( *language == '/' )
550 *language++ = '\0';
551 break;
553 ++language;
555 opt->copyright_language = language ? lsmash_pack_iso_language( language ) : ISOM_LANGUAGE_CODE_UNDEFINED;
557 /* iTunes metadata */
558 #define CHECK_ITUNES_METADATA_ARG_STRING( argument, value ) \
559 else if( !strcasecmp( argv[i], "--"#argument ) ) \
561 CHECK_NEXT_ARG; \
562 if( opt->itunes_metadata.value ) \
563 return ERROR_MSG( "you specified --"#argument" twice.\n" ); \
564 opt->itunes_metadata.value = argv[i]; \
566 CHECK_ITUNES_METADATA_ARG_STRING( album-name, album_name )
567 CHECK_ITUNES_METADATA_ARG_STRING( artist, artist )
568 CHECK_ITUNES_METADATA_ARG_STRING( comment, comment )
569 CHECK_ITUNES_METADATA_ARG_STRING( release-date, release_date )
570 CHECK_ITUNES_METADATA_ARG_STRING( encoder, encoder )
571 CHECK_ITUNES_METADATA_ARG_STRING( genre, genre )
572 CHECK_ITUNES_METADATA_ARG_STRING( lyrics, lyrics )
573 CHECK_ITUNES_METADATA_ARG_STRING( title, title )
574 CHECK_ITUNES_METADATA_ARG_STRING( composer, composer )
575 CHECK_ITUNES_METADATA_ARG_STRING( album-artist, album_artist )
576 CHECK_ITUNES_METADATA_ARG_STRING( copyright, copyright )
577 CHECK_ITUNES_METADATA_ARG_STRING( description, description )
578 CHECK_ITUNES_METADATA_ARG_STRING( grouping, grouping )
579 #undef CHECK_ITUNES_METADATA_ARG_STRING
580 else if( !strcasecmp( argv[i], "--tempo" ) )
582 CHECK_NEXT_ARG;
583 if( opt->itunes_metadata.beats_per_minute )
584 return ERROR_MSG( "you specified --tempo twice.\n" );
585 opt->itunes_metadata.beats_per_minute = atoi( argv[i] );
587 else if( !strcasecmp( argv[i], "--language" ) )
589 CHECK_NEXT_ARG;
590 opt->default_language = lsmash_pack_iso_language( argv[i] );
592 #undef CHECK_NEXT_ARG
593 else
594 return ERROR_MSG( "you specified invalid option: %s.\n", argv[i] );
595 ++i;
597 if( !muxer->output.file.name )
598 return ERROR_MSG( "output file name is not specified.\n" );
599 if( decide_brands( opt ) )
600 return ERROR_MSG( "failed to set up output file format.\n" );
601 if( opt->timeline_shift && !opt->qtff && opt->isom_version < 4 )
602 return ERROR_MSG( "timeline shift requires --file-format mov, or --isom-version 4 or later.\n" );
603 muxer->num_of_inputs = opt->num_of_inputs;
604 return 0;
607 static int parse_track_options( input_t *input )
609 for( input->current_track_number = 1;
610 input->current_track_number <= input->num_of_tracks;
611 input->current_track_number ++ )
613 input_track_t *in_track = &input->track[input->current_track_number - 1];
614 input_track_option_t *track_opt = &in_track->opt;
615 if( track_opt->raws == NULL )
616 break;
617 #if 0
618 if( !strchr( track_opt->raws, ':' )
619 || strchr( track_opt->raws, ':' ) == track_opt->raws )
620 return ERROR_MSG( "track number is not specified in %s\n", track_opt->raws );
621 if( strchr( track_opt->raws, ':' ) != strrchr( track_opt->raws, ':' ) )
622 return ERROR_MSG( "multiple colons inside one track option in %s.\n", track_opt->raws );
623 uint32_t track_number = atoi( strtok( track_opt->raws, ":" ) );
624 if( track_number == 0 || track_number > MAX_NUM_OF_TRACKS )
625 return ERROR_MSG( "%s is an invalid track number %"PRIu32".\n", strtok( track_opt->raws, ":" ), track_number );
626 in_track = &input->track[track_number - 1];
627 track_opt = &in_track->opt;
628 char *track_option;
629 while( (track_option = strtok( NULL, "," )) != NULL )
630 #else
631 char *track_option = NULL;
632 while( (track_option = strtok( track_option ? NULL : track_opt->raws, "," )) != NULL )
633 #endif
635 if( strchr( track_option, '=' ) != strrchr( track_option, '=' ) )
636 return ERROR_MSG( "multiple equal signs inside one track option in %s\n", track_option );
637 if( strstr( track_option, "disable" ) )
638 track_opt->disable = 1;
639 else if( strstr( track_option, "alternate-group=" ) )
641 char *track_parameter = strchr( track_option, '=' ) + 1;
642 track_opt->alternate_group = atoi( track_parameter );
644 else if( strstr( track_option, "encoder-delay=" ) )
646 char *track_parameter = strchr( track_option, '=' ) + 1;
647 track_opt->encoder_delay = atoi( track_parameter );
649 else if( strstr( track_option, "language=" ) )
651 char *track_parameter = strchr( track_option, '=' ) + 1;
652 track_opt->ISO_language = lsmash_pack_iso_language( track_parameter );
654 else if( strstr( track_option, "fps=" ) )
656 char *track_parameter = strchr( track_option, '=' ) + 1;
657 if( sscanf( track_parameter, "%"SCNu32"/%"SCNu32, &track_opt->fps_num, &track_opt->fps_den ) == 1 )
659 track_opt->fps_num = atoi( track_parameter );
660 track_opt->fps_den = 1;
662 track_opt->user_fps = 1;
664 else if( strstr( track_option, "copyright=" ) )
666 char *track_parameter = strchr( track_option, '=' ) + 1;
667 track_opt->copyright_notice = track_parameter;
668 while( *track_parameter )
670 if( *track_parameter == '/' )
672 *track_parameter++ = '\0';
673 break;
675 ++track_parameter;
677 track_opt->copyright_language = lsmash_pack_iso_language( track_parameter );
679 else if( strstr( track_option, "handler=" ) )
681 char *track_parameter = strchr( track_option, '=' ) + 1;
682 track_opt->handler_name = track_parameter;
684 else if( strstr( track_option, "sbr" ) )
685 track_opt->sbr = 1;
686 else if( strstr( track_option, "par=" ) )
688 char *track_parameter = strchr( track_option, '=' ) + 1;
689 if( sscanf( track_parameter, "%"SCNu32":%"SCNu32, &track_opt->par_h, &track_opt->par_v ) != 2 )
691 track_opt->par_h = 0;
692 track_opt->par_v = 0;
695 else
696 return ERROR_MSG( "unknown track option %s\n", track_option );
699 return 0;
702 static void display_codec_name( lsmash_codec_type_t codec_type, uint32_t track_number )
704 #define DISPLAY_CODEC_NAME( codec, codec_name ) \
705 else if( lsmash_check_codec_type_identical( codec_type, codec ) ) \
706 eprintf( "Track %"PRIu32": "#codec_name"\n", track_number )
707 if( 0 );
708 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_AVC1_VIDEO, H.264 Advanced Video Coding );
709 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_HVC1_VIDEO, H.265 High Efficiency Video Coding );
710 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_VC_1_VIDEO, SMPTE VC-1 Advanced Profile );
711 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_MP4A_AUDIO, MPEG-4 Audio );
712 DISPLAY_CODEC_NAME( QT_CODEC_TYPE_MP4A_AUDIO, MPEG-4 Audio );
713 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_AC_3_AUDIO, AC-3 );
714 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_EC_3_AUDIO, Enhanced AC-3 );
715 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_DTSC_AUDIO, DTS );
716 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_DTSE_AUDIO, DTS LBR );
717 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_DTSH_AUDIO, DTS-HD );
718 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_DTSL_AUDIO, DTS-HD Lossless );
719 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_DTSX_AUDIO, DTS:X );
720 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_SAWB_AUDIO, Wideband AMR voice );
721 DISPLAY_CODEC_NAME( ISOM_CODEC_TYPE_SAMR_AUDIO, Narrowband AMR voice );
722 DISPLAY_CODEC_NAME( QT_CODEC_TYPE_LPCM_AUDIO, Uncompressed Audio );
723 #undef DISPLAY_CODEC_NAME
726 static int open_input_files( muxer_t *muxer )
728 output_movie_t *out_movie = &muxer->output.file.movie;
729 option_t *opt = &muxer->opt;
730 for( uint32_t current_input_number = 1; current_input_number <= muxer->num_of_inputs; current_input_number++ )
732 input_t *input = &muxer->input[current_input_number - 1];
733 /* Initialize importer framework. */
734 input->importer = lsmash_importer_open( input->file_name, "auto" );
735 if( !input->importer )
736 return ERROR_MSG( "failed to open input file.\n" );
737 input->num_of_tracks = lsmash_importer_get_track_count( input->importer );
738 if( input->num_of_tracks == 0 )
739 return ERROR_MSG( "there is no valid track in input file.\n" );
740 if( opt->default_language )
741 for( int i = 0; i < input->num_of_tracks; i ++ )
742 input->track[i].opt.ISO_language = opt->default_language;
743 /* Parse track options */
744 if( parse_track_options( input ) )
745 return ERROR_MSG( "failed to parse track options.\n" );
746 /* Activate tracks by CODEC type. */
747 for( input->current_track_number = 1;
748 input->current_track_number <= input->num_of_tracks;
749 input->current_track_number ++ )
751 input_track_t *in_track = &input->track[input->current_track_number - 1];
752 int err = lsmash_importer_construct_timeline( input->importer, input->current_track_number );
753 if( err < 0 && err != LSMASH_ERR_PATCH_WELCOME )
755 in_track->active = 0;
756 continue;
758 in_track->summary = lsmash_duplicate_summary( input->importer, input->current_track_number );
759 if( !in_track->summary )
760 return ERROR_MSG( "failed to get input summary.\n" );
761 /* Check codec type. */
762 lsmash_codec_type_t codec_type = in_track->summary->sample_type;
763 in_track->active = 1;
764 if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_AVC1_VIDEO ) )
766 if( opt->isom )
767 add_brand( opt, ISOM_BRAND_TYPE_AVC1 );
769 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_HVC1_VIDEO ) )
771 if( !opt->isom && opt->qtff )
772 return ERROR_MSG( "the input seems HEVC, at present available only for ISO Base Media file format.\n" );
774 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_VC_1_VIDEO ) )
776 if( !opt->isom && opt->qtff )
777 return ERROR_MSG( "the input seems VC-1, at present available only for ISO Base Media file format.\n" );
779 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_MP4A_AUDIO )
780 || lsmash_check_codec_type_identical( codec_type, QT_CODEC_TYPE_MP4A_AUDIO ) )
781 /* Do nothing. */;
782 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_AC_3_AUDIO )
783 || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_EC_3_AUDIO ) )
785 if( !opt->isom && opt->qtff )
786 return ERROR_MSG( "the input seems (Enhanced) AC-3, at present available only for ISO Base Media file format.\n" );
787 add_brand( opt, ISOM_BRAND_TYPE_DBY1 );
789 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSC_AUDIO )
790 || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSE_AUDIO )
791 || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSH_AUDIO )
792 || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSL_AUDIO )
793 || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSX_AUDIO ) )
795 if( !opt->isom && opt->qtff )
796 return ERROR_MSG( "the input seems DTS(-HD) Audio, at present available only for ISO Base Media file format.\n" );
798 else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_SAWB_AUDIO )
799 || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_SAMR_AUDIO ) )
801 if( !opt->brand_3gx )
802 return ERROR_MSG( "the input seems AMR-NB/WB, available for 3GPP(2) file format.\n" );
804 else if( lsmash_check_codec_type_identical( codec_type, QT_CODEC_TYPE_LPCM_AUDIO ) )
806 if( opt->isom && !opt->qtff )
807 return ERROR_MSG( "the input seems Uncompressed Audio, at present available only for QuickTime file format.\n" );
808 in_track->lpcm = 1;
810 else
812 lsmash_cleanup_summary( in_track->summary );
813 in_track->summary = NULL;
814 in_track->active = 0;
816 if( in_track->active )
818 ++ input->num_of_active_tracks;
819 display_codec_name( codec_type, out_movie->num_of_tracks + input->num_of_active_tracks );
822 out_movie->num_of_tracks += input->num_of_active_tracks;
824 if( out_movie->num_of_tracks == 0 )
825 return ERROR_MSG( "there is no media that can be stored in output movie.\n" );
826 return 0;
829 static int set_itunes_metadata( output_t *output, option_t *opt )
831 if( !opt->itunes_movie )
832 return 0;
833 itunes_metadata_t *metadata = &opt->itunes_metadata;
834 #define SET_ITUNES_METADATA( item, type, value ) \
835 if( value \
836 && lsmash_set_itunes_metadata( output->root, (lsmash_itunes_metadata_t){ item, ITUNES_METADATA_TYPE_NONE, { .type = value }, NULL, NULL } ) ) \
837 return -1
838 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_ENCODING_TOOL, string, "L-SMASH" );
839 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_ALBUM_NAME, string, metadata->album_name );
840 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_ARTIST, string, metadata->artist );
841 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_USER_COMMENT, string, metadata->comment );
842 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_RELEASE_DATE, string, metadata->release_date );
843 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_ENCODED_BY, string, metadata->encoder );
844 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_USER_GENRE, string, metadata->genre );
845 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_LYRICS, string, metadata->lyrics );
846 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_TITLE, string, metadata->title );
847 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_COMPOSER, string, metadata->composer );
848 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_ALBUM_ARTIST, string, metadata->album_artist );
849 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_COPYRIGHT, string, metadata->copyright );
850 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_DESCRIPTION, string, metadata->description );
851 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_GROUPING, string, metadata->grouping );
852 SET_ITUNES_METADATA( ITUNES_METADATA_ITEM_BEATS_PER_MINUTE, integer, metadata->beats_per_minute );
853 #undef SET_ITUNES_METADATA
854 return 0;
857 static int prepare_output( muxer_t *muxer )
859 option_t *opt = &muxer->opt;
860 output_t *output = &muxer->output;
861 output_file_t *out_file = &output->file;
862 output_movie_t *out_movie = &out_file->movie;
863 /* Allocate output tracks. */
864 out_movie->track = lsmash_malloc( out_movie->num_of_tracks * sizeof(output_track_t) );
865 if( !out_movie->track )
866 return ERROR_MSG( "failed to allocate output tracks.\n" );
867 memset( out_movie->track, 0, out_movie->num_of_tracks * sizeof(output_track_t) );
868 /* Initialize L-SMASH muxer */
869 output->root = lsmash_create_root();
870 if( !output->root )
871 return ERROR_MSG( "failed to create a ROOT.\n" );
872 lsmash_file_parameters_t *file_param = &out_file->param;
873 if( lsmash_open_file( out_file->name, 0, file_param ) < 0 )
874 return ERROR_MSG( "failed to open an output file.\n" );
875 file_param->major_brand = opt->major_brand;
876 file_param->brands = opt->brands;
877 file_param->brand_count = opt->num_of_brands;
878 file_param->minor_version = opt->minor_version;
879 if( opt->interleave )
880 file_param->max_chunk_duration = opt->interleave * 1e-3;
881 out_file->fh = lsmash_set_file( output->root, file_param );
882 if( !out_file->fh )
883 return ERROR_MSG( "failed to add an output file into a ROOT.\n" );
884 /* Initialize movie */
885 lsmash_movie_parameters_t movie_param;
886 lsmash_initialize_movie_parameters( &movie_param );
887 if( lsmash_set_movie_parameters( output->root, &movie_param ) )
888 return ERROR_MSG( "failed to set movie parameters.\n" );
889 if( opt->copyright_notice
890 && lsmash_set_copyright( output->root, 0, opt->copyright_language, opt->copyright_notice ) )
891 return ERROR_MSG( "failed to set a copyright notice for the entire movie.\n" );
892 if( set_itunes_metadata( output, opt ) )
893 return ERROR_MSG( "failed to set iTunes metadata.\n" );
894 out_movie->current_track_number = 1;
895 for( uint32_t current_input_number = 1; current_input_number <= muxer->num_of_inputs; current_input_number++ )
897 input_t *input = &muxer->input[current_input_number - 1];
898 for( input->current_track_number = 1;
899 input->current_track_number <= input->num_of_tracks;
900 input->current_track_number ++ )
902 input_track_t *in_track = &input->track[ input->current_track_number - 1 ];
903 if( !in_track->active )
904 continue;
905 input_track_option_t *track_opt = &in_track->opt;
906 output_track_t *out_track = &out_movie->track[ out_movie->current_track_number - 1 ];
907 /* Set up track parameters. */
908 lsmash_track_parameters_t track_param;
909 lsmash_initialize_track_parameters( &track_param );
910 track_param.mode = ISOM_TRACK_IN_MOVIE | ISOM_TRACK_IN_PREVIEW;
911 if( !track_opt->disable )
912 track_param.mode |= ISOM_TRACK_ENABLED;
913 if( opt->qtff )
914 track_param.mode |= QT_TRACK_IN_POSTER;
915 track_param.alternate_group = track_opt->alternate_group;
916 lsmash_media_parameters_t media_param;
917 lsmash_initialize_media_parameters( &media_param );
918 media_param.ISO_language = track_opt->ISO_language;
919 media_param.compact_sample_size_table = opt->compact_size_table;
920 switch( in_track->summary->summary_type )
922 case LSMASH_SUMMARY_TYPE_VIDEO :
924 out_track->track_ID = lsmash_create_track( output->root, ISOM_MEDIA_HANDLER_TYPE_VIDEO_TRACK );
925 if( !out_track->track_ID )
926 return ERROR_MSG( "failed to create a track.\n" );
927 lsmash_video_summary_t *summary = (lsmash_video_summary_t *)in_track->summary;
928 uint64_t display_width = (uint64_t)summary->width << 16;
929 uint64_t display_height = (uint64_t)summary->height << 16;
930 if( track_opt->par_h && track_opt-> par_v )
932 summary->par_h = track_opt->par_h;
933 summary->par_v = track_opt->par_v;
935 if( summary->par_h && summary->par_v )
937 double sar = (double)summary->par_h / summary->par_v;
938 if( sar > 1.0 )
939 display_width *= sar;
940 else
941 display_height /= sar;
943 track_param.display_width = display_width <= UINT32_MAX ? display_width : UINT32_MAX;
944 track_param.display_height = display_height <= UINT32_MAX ? display_height : UINT32_MAX;
945 /* Initialize media */
946 uint32_t timescale = 25; /* default value */
947 uint32_t timebase = 1; /* default value */
948 if( track_opt->user_fps )
950 timescale = track_opt->fps_num << (!!summary->sample_per_field);
951 timebase = track_opt->fps_den;
953 else if( !summary->vfr )
955 if( lsmash_check_codec_type_identical( summary->sample_type, ISOM_CODEC_TYPE_AVC1_VIDEO )
956 || lsmash_check_codec_type_identical( summary->sample_type, ISOM_CODEC_TYPE_HVC1_VIDEO ) )
958 uint32_t compare_timebase = summary->timebase;
959 uint32_t compare_timescale = summary->timescale;
960 static const struct
962 uint32_t timescale;
963 uint32_t timebase;
964 } well_known_fps[]
966 { 24000, 1001 }, { 30000, 1001 }, { 60000, 1001 }, { 120000, 1001 }, { 72000, 1001 },
967 { 25, 1 }, { 50, 1 }, { 24, 1 }, { 30, 1 }, { 60, 1 }, { 120, 1 }, { 72, 1 }, { 0, 0 }
969 for( int i = 0; well_known_fps[i].timescale; i++ )
970 if( well_known_fps[i].timescale == compare_timescale
971 && well_known_fps[i].timebase == compare_timebase )
973 timescale = well_known_fps[i].timescale;
974 timebase = well_known_fps[i].timebase;
975 break;
977 lsmash_codec_specific_t *bitrate = lsmash_create_codec_specific_data( LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264_BITRATE,
978 LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
979 if( bitrate )
980 lsmash_add_codec_specific_data( in_track->summary, bitrate );
981 lsmash_destroy_codec_specific_data( bitrate );
983 else
985 timescale = summary->timescale;
986 timebase = summary->timebase;
989 media_param.timescale = timescale;
990 media_param.media_handler_name = track_opt->handler_name ? track_opt->handler_name : "L-SMASH Video Handler";
991 media_param.roll_grouping = 1;
992 media_param.rap_grouping = opt->isom_version >= 6;
993 out_track->timescale = timescale;
994 out_track->timebase = timebase;
995 break;
997 case LSMASH_SUMMARY_TYPE_AUDIO :
999 out_track->track_ID = lsmash_create_track( output->root, ISOM_MEDIA_HANDLER_TYPE_AUDIO_TRACK );
1000 if( !out_track->track_ID )
1001 return ERROR_MSG( "failed to create a track.\n" );
1002 lsmash_audio_summary_t *summary = (lsmash_audio_summary_t *)in_track->summary;
1003 if( track_opt->sbr )
1005 /* Check if explicit SBR is valid or not. */
1006 if( lsmash_mp4sys_get_object_type_indication( (lsmash_summary_t *)summary ) != MP4SYS_OBJECT_TYPE_Audio_ISO_14496_3 )
1007 return ERROR_MSG( "--sbr is only valid with MPEG-4 Audio.\n" );
1008 summary->sbr_mode = MP4A_AAC_SBR_BACKWARD_COMPATIBLE;
1009 if( lsmash_setup_AudioSpecificConfig( summary ) )
1010 return ERROR_MSG( "failed to set SBR mode.\n" );
1012 media_param.timescale = summary->frequency;
1013 media_param.media_handler_name = track_opt->handler_name ? track_opt->handler_name : "L-SMASH Audio Handler";
1014 media_param.roll_grouping = (opt->isom_version >= 2 || (opt->qtff && !in_track->lpcm));
1015 out_track->priming_samples = track_opt->encoder_delay;
1016 out_track->timescale = summary->frequency;
1017 out_track->timebase = 1;
1018 out_track->lpcm = in_track->lpcm;
1019 break;
1021 default :
1022 return ERROR_MSG( "not supported stream type.\n" );
1024 /* Reset the movie timescale in order to match the media timescale if only one track is there. */
1025 if( muxer->num_of_inputs == 1
1026 && current_input_number == 1
1027 && input->current_track_number == 1 )
1029 movie_param.timescale = media_param.timescale;
1030 if( lsmash_set_movie_parameters( output->root, &movie_param ) )
1031 return ERROR_MSG( "failed to set movie parameters.\n" );
1033 /* Set copyright information. */
1034 if( track_opt->copyright_notice
1035 && lsmash_set_copyright( output->root, out_track->track_ID, track_opt->copyright_language, track_opt->copyright_notice ) )
1036 return ERROR_MSG( "failed to set a copyright notice.\n" );
1037 /* Set track parameters. */
1038 if( lsmash_set_track_parameters( output->root, out_track->track_ID, &track_param ) )
1039 return ERROR_MSG( "failed to set track parameters.\n" );
1040 /* Set media parameters. */
1041 if( lsmash_set_media_parameters( output->root, out_track->track_ID, &media_param ) )
1042 return ERROR_MSG( "failed to set media parameters.\n" );
1043 out_track->summary = in_track->summary;
1044 out_track->sample_entry = lsmash_add_sample_entry( output->root, out_track->track_ID, out_track->summary );
1045 if( !out_track->sample_entry )
1046 return ERROR_MSG( "failed to add sample description entry.\n" );
1047 out_track->active = 1;
1048 ++ out_movie->current_track_number;
1050 input->current_track_number = 1;
1052 out_movie->current_track_number = 1;
1053 return 0;
1056 static void set_reference_chapter_track( output_t *output, option_t *opt )
1058 if( !opt->chap_file || (!opt->qtff && !opt->itunes_movie) || (opt->brand_3gx == 1) )
1059 return;
1060 lsmash_create_reference_chapter_track( output->root, opt->chap_track, opt->chap_file );
1063 static int do_mux( muxer_t *muxer )
1065 #define LSMASH_MAX( a, b ) ((a) > (b) ? (a) : (b))
1066 option_t *opt = &muxer->opt;
1067 output_t *output = &muxer->output;
1068 output_movie_t *out_movie = &output->file.movie;
1069 set_reference_chapter_track( output, opt );
1070 double largest_dts = 0;
1071 uint32_t current_input_number = 1;
1072 uint32_t num_consecutive_sample_skip = 0;
1073 uint32_t num_active_input_tracks = out_movie->num_of_tracks;
1074 uint64_t total_media_size = 0;
1075 uint32_t progress_pos = 0;
1076 while( 1 )
1078 input_t *input = &muxer->input[current_input_number - 1];
1079 output_track_t *out_track = &out_movie->track[ out_movie->current_track_number - 1 ];
1080 if( out_track->active )
1082 lsmash_sample_t *sample = out_track->sample;
1083 /* Get a new sample data if the track doesn't hold any one. */
1084 if( !sample )
1086 /* lsmash_importer_get_access_unit() returns 1 if there're any changes in stream's properties. */
1087 int ret = lsmash_importer_get_access_unit( input->importer, input->current_track_number, &sample );
1088 if( ret == LSMASH_ERR_MEMORY_ALLOC )
1089 return ERROR_MSG( "failed to alloc memory for buffer.\n" );
1090 else if( ret <= -1 )
1092 lsmash_delete_sample( sample );
1093 ERROR_MSG( "failed to get a frame from input file. Maybe corrupted.\n"
1094 "Aborting muxing operation and trying to let output be valid file.\n" );
1095 break;
1097 else if( ret == 1 ) /* a change of stream's properties */
1099 input_track_t *in_track = &input->track[input->current_track_number - 1];
1100 lsmash_cleanup_summary( in_track->summary );
1101 in_track->summary = lsmash_duplicate_summary( input->importer, input->current_track_number );
1102 out_track->summary = in_track->summary;
1103 out_track->sample_entry = lsmash_add_sample_entry( output->root, out_track->track_ID, out_track->summary );
1104 if( !out_track->sample_entry )
1106 ERROR_MSG( "failed to add sample description entry.\n" );
1107 break;
1110 else if( ret == 2 ) /* EOF */
1112 /* No more appendable samples in this track. */
1113 lsmash_delete_sample( sample );
1114 sample = NULL;
1115 out_track->active = 0;
1116 out_track->last_delta = lsmash_importer_get_last_delta( input->importer, input->current_track_number );
1117 if( out_track->last_delta == 0 )
1118 ERROR_MSG( "failed to get the last sample delta.\n" );
1119 out_track->last_delta *= out_track->timebase;
1120 if( --num_active_input_tracks == 0 )
1121 break; /* Reached the end of whole tracks. */
1123 if( sample )
1125 sample->index = out_track->sample_entry;
1126 sample->dts *= out_track->timebase;
1127 sample->cts *= out_track->timebase;
1128 if( opt->timeline_shift )
1130 if( out_track->current_sample_number == 0 )
1131 out_track->ctd_shift = sample->cts;
1132 sample->cts -= out_track->ctd_shift;
1134 out_track->dts = (double)sample->dts / out_track->timescale;
1135 out_track->sample = sample;
1138 if( sample )
1140 /* Append a sample if meeting a condition. */
1141 if( out_track->dts <= largest_dts || num_consecutive_sample_skip == num_active_input_tracks )
1143 uint64_t sample_size = sample->length; /* sample might be deleted internally after appending. */
1144 uint64_t sample_dts = sample->dts; /* same as above */
1145 uint64_t sample_cts = sample->cts; /* same as above */
1146 if( lsmash_append_sample( output->root, out_track->track_ID, sample ) )
1147 return ERROR_MSG( "failed to append a sample.\n" );
1148 if( out_track->current_sample_number == 0 )
1149 out_track->start_offset = sample_cts;
1150 else
1151 out_track->last_delta = sample_dts - out_track->prev_dts; /* for any changes in stream's properties */
1152 out_track->prev_dts = sample_dts;
1153 out_track->sample = NULL;
1154 largest_dts = LSMASH_MAX( largest_dts, out_track->dts );
1155 total_media_size += sample_size;
1156 ++ out_track->current_sample_number;
1157 num_consecutive_sample_skip = 0;
1158 /* Print, per 4 megabytes, total size of imported media. */
1159 if( (total_media_size >> 22) > progress_pos )
1161 progress_pos = total_media_size >> 22;
1162 eprintf( "Importing: %"PRIu64" bytes\r", total_media_size );
1165 else
1166 ++num_consecutive_sample_skip; /* Skip appendig sample. */
1169 if( ++ out_movie->current_track_number > out_movie->num_of_tracks )
1170 out_movie->current_track_number = 1; /* Back the first output track. */
1171 /* Move the next track. */
1172 if( ++ input->current_track_number > input->num_of_tracks )
1174 /* Move the next input movie. */
1175 input->current_track_number = 1;
1176 if( ++ current_input_number > muxer->num_of_inputs )
1177 current_input_number = 1; /* Back the first input movie. */
1180 for( out_movie->current_track_number = 1;
1181 out_movie->current_track_number <= out_movie->num_of_tracks;
1182 out_movie->current_track_number ++ )
1184 /* Close track. */
1185 output_track_t *out_track = &out_movie->track[ out_movie->current_track_number - 1 ];
1186 uint32_t last_sample_delta = out_track->lpcm ? 1 : out_track->last_delta;
1187 if( lsmash_flush_pooled_samples( output->root, out_track->track_ID, last_sample_delta ) )
1188 ERROR_MSG( "failed to flush the rest of samples.\n" );
1189 /* Create edit list.
1190 * Don't trust media duration basically. It's just duration of media, not duration of track presentation. */
1191 uint64_t actual_duration = out_track->lpcm
1192 ? lsmash_get_media_duration( output->root, out_track->track_ID )
1193 : out_track->prev_dts + last_sample_delta;
1194 actual_duration -= out_track->priming_samples;
1195 lsmash_edit_t edit;
1196 edit.duration = actual_duration * ((double)lsmash_get_movie_timescale( output->root ) / out_track->timescale);
1197 edit.start_time = out_track->priming_samples + out_track->start_offset;
1198 edit.rate = ISOM_EDIT_MODE_NORMAL;
1199 if( lsmash_create_explicit_timeline_map( output->root, out_track->track_ID, edit ) )
1200 ERROR_MSG( "failed to set timeline map.\n" );
1202 return 0;
1203 #undef LSMASH_MAX
1206 static int moov_to_front_callback( void *param, uint64_t written_movie_size, uint64_t total_movie_size )
1208 static uint32_t progress_pos = 0;
1209 if ( (written_movie_size >> 24) <= progress_pos )
1210 return 0;
1211 REFRESH_CONSOLE;
1212 eprintf( "Finalizing: [%5.2lf%%]\r", total_movie_size ? ((double)written_movie_size / total_movie_size) * 100.0 : 0 );
1213 /* Print, per 16 megabytes */
1214 progress_pos = written_movie_size >> 24;
1215 return 0;
1218 static int finish_movie( output_t *output, option_t *opt )
1220 /* Set chapter list. */
1221 if( opt->chap_file )
1222 lsmash_set_tyrant_chapter( output->root, opt->chap_file, opt->add_bom_to_chpl );
1223 /* Close movie. */
1224 REFRESH_CONSOLE;
1225 if( opt->optimize_pd )
1227 lsmash_adhoc_remux_t moov_to_front;
1228 moov_to_front.func = moov_to_front_callback;
1229 moov_to_front.buffer_size = 4*1024*1024; /* 4MiB */
1230 moov_to_front.param = NULL;
1231 return lsmash_finish_movie( output->root, &moov_to_front );
1233 if( lsmash_finish_movie( output->root, NULL ) )
1234 return -1;
1235 return lsmash_write_lsmash_indicator( output->root );
1238 int main( int argc, char *argv[] )
1240 muxer_t muxer = { { 0 } };
1241 lsmash_get_mainargs( &argc, &argv );
1242 if( parse_global_options( argc, argv, &muxer ) )
1243 return MUXER_USAGE_ERR();
1244 if( muxer.opt.help )
1246 display_help();
1247 cleanup_muxer( &muxer );
1248 return 0;
1250 else if( muxer.opt.version )
1252 display_version();
1253 cleanup_muxer( &muxer );
1254 return 0;
1256 if( open_input_files( &muxer ) )
1257 return MUXER_ERR( "failed to open input files.\n" );
1258 if( prepare_output( &muxer ) )
1259 return MUXER_ERR( "failed to set up preparation for output.\n" );
1260 if( do_mux( &muxer ) )
1261 return MUXER_ERR( "failed to do muxing.\n" );
1262 if( finish_movie( &muxer.output, &muxer.opt ) )
1263 return MUXER_ERR( "failed to finish movie.\n" );
1264 REFRESH_CONSOLE;
1265 eprintf( "Muxing completed!\n" );
1266 cleanup_muxer( &muxer ); /* including lsmash_destroy_root() */
1267 return 0;