box: Initialize predefined children by non-existing box constants whenever
[L-SMASH.git] / cli / boxdumper.c
bloba8e2a375aac862784f72a01de9236ac4fc1f89e3
1 /*****************************************************************************
2 * boxdumper.c
3 *****************************************************************************
4 * Copyright (C) 2010-2017 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #include "cli.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <inttypes.h>
29 #ifdef _WIN32
30 #include <io.h>
31 #include <fcntl.h>
32 #endif
34 #define eprintf( ... ) fprintf( stderr, __VA_ARGS__ )
36 static void display_version( void )
38 eprintf( "\n"
39 "L-SMASH isom/mov structual analyzer rev%s %s\n"
40 "Built on %s %s\n"
41 "Copyright (C) 2010-2017 L-SMASH project\n",
42 LSMASH_REV, LSMASH_GIT_HASH, __DATE__, __TIME__ );
45 static void display_help( void )
47 display_version();
48 eprintf( "\n"
49 "Usage: boxdumper [option] input\n"
50 " options:\n"
51 " --help Display help\n"
52 " --version Display version information\n"
53 " --box Dump box structure\n"
54 " --chapter Extract chapter list\n"
55 " --timestamp Dump media timestamps\n" );
58 static int boxdumper_error
60 lsmash_root_t *root,
61 lsmash_file_parameters_t *file_param,
62 const char *message
65 lsmash_close_file( file_param );
66 lsmash_destroy_root( root );
67 eprintf( "%s", message );
68 return -1;
71 #define BOXDUMPER_ERR( message ) boxdumper_error( root, &file_param, message )
72 #define DO_NOTHING
74 int main( int argc, char *argv[] )
76 if ( argc < 2 )
78 display_help();
79 return -1;
81 else if( !strcasecmp( argv[1], "-h" ) || !strcasecmp( argv[1], "--help" ) )
83 display_help();
84 return 0;
86 else if( !strcasecmp( argv[1], "-v" ) || !strcasecmp( argv[1], "--version" ) )
88 display_version();
89 return 0;
91 int dump_box = 1;
92 int chapter = 0;
93 char *filename;
94 lsmash_get_mainargs( &argc, &argv );
95 if( argc > 2 )
97 if( !strcasecmp( argv[1], "--box" ) )
98 DO_NOTHING;
99 else if( !strcasecmp( argv[1], "--chapter" ) )
100 chapter = 1;
101 else if( !strcasecmp( argv[1], "--timestamp" ) )
102 dump_box = 0;
103 else
105 display_help();
106 return -1;
108 filename = argv[2];
110 else
112 filename = argv[1];
114 /* Open the input file. */
115 lsmash_root_t *root = lsmash_create_root();
116 if( !root )
118 fprintf( stderr, "Failed to create a ROOT.\n" );
119 return -1;
121 lsmash_file_parameters_t file_param = { 0 };
122 if( lsmash_open_file( filename, 1, &file_param ) < 0 )
123 return BOXDUMPER_ERR( "Failed to open an input file.\n" );
124 if( dump_box )
125 file_param.mode |= LSMASH_FILE_MODE_DUMP;
126 lsmash_file_t *file = lsmash_set_file( root, &file_param );
127 if( !file )
128 return BOXDUMPER_ERR( "Failed to add a file into a ROOT.\n" );
129 if( lsmash_read_file( file, &file_param ) < 0 )
130 return BOXDUMPER_ERR( "Failed to read a file\n" );
131 /* Dump the input file. */
132 if( chapter )
134 if( lsmash_print_chapter_list( root ) )
135 return BOXDUMPER_ERR( "Failed to extract chapter.\n" );
137 else if( dump_box )
139 if( lsmash_print_movie( root, "-" ) )
140 return BOXDUMPER_ERR( "Failed to dump box structure.\n" );
142 else
144 lsmash_movie_parameters_t movie_param;
145 lsmash_initialize_movie_parameters( &movie_param );
146 lsmash_get_movie_parameters( root, &movie_param );
147 uint32_t num_tracks = movie_param.number_of_tracks;
148 for( uint32_t track_number = 1; track_number <= num_tracks; track_number++ )
150 uint32_t track_ID = lsmash_get_track_ID( root, track_number );
151 if( !track_ID )
152 return BOXDUMPER_ERR( "Failed to get track_ID.\n" );
153 lsmash_media_parameters_t media_param;
154 lsmash_initialize_media_parameters( &media_param );
155 if( lsmash_get_media_parameters( root, track_ID, &media_param ) )
156 return BOXDUMPER_ERR( "Failed to get media parameters.\n" );
157 if( lsmash_construct_timeline( root, track_ID ) )
158 return BOXDUMPER_ERR( "Failed to construct timeline.\n" );
159 uint32_t timeline_shift;
160 if( lsmash_get_composition_to_decode_shift_from_media_timeline( root, track_ID, &timeline_shift ) )
161 return BOXDUMPER_ERR( "Failed to get timestamps.\n" );
162 lsmash_media_ts_list_t ts_list;
163 if( lsmash_get_media_timestamps( root, track_ID, &ts_list ) )
164 return BOXDUMPER_ERR( "Failed to get timestamps.\n" );
165 fprintf( stdout, "track_ID: %"PRIu32"\n", track_ID );
166 fprintf( stdout, "Media timescale: %"PRIu32"\n", media_param.timescale );
167 lsmash_media_ts_t *ts_array = ts_list.timestamp;
168 if( !ts_array )
170 fprintf( stdout, "\n" );
171 continue;
173 for( uint32_t i = 0; i < ts_list.sample_count; i++ )
174 fprintf( stdout, "DTS = %"PRIu64", CTS = %"PRIu64"\n", ts_array[i].dts, ts_array[i].cts + timeline_shift );
175 lsmash_free( ts_array );
176 fprintf( stdout, "\n" );
179 lsmash_destroy_root( root );
180 return 0;