print: Avoid Variable Length Arrays.
[L-SMASH.git] / cli / boxdumper.c
blobcd8f8a1304ab9232c2b8a0a673e46e44df450dbe
1 /*****************************************************************************
2 * boxdumper.c:
3 *****************************************************************************
4 * Copyright (C) 2010-2014 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 <strings.h>
28 #include <stdlib.h>
29 #include <inttypes.h>
30 #ifdef _WIN32
31 #include <io.h>
32 #include <fcntl.h>
33 #endif
35 #include "config.h"
37 #define eprintf( ... ) fprintf( stderr, __VA_ARGS__ )
39 static void display_version( void )
41 eprintf( "\n"
42 "L-SMASH isom/mov structual analyzer rev%s %s\n"
43 "Built on %s %s\n"
44 "Copyright (C) 2010-2014 L-SMASH project\n",
45 LSMASH_REV, LSMASH_GIT_HASH, __DATE__, __TIME__ );
48 static void display_help( void )
50 display_version();
51 eprintf( "\n"
52 "Usage: boxdumper [option] input\n"
53 " options:\n"
54 " --help Display help\n"
55 " --version Display version information\n"
56 " --box Dump box structure\n"
57 " --chapter Extract chapter list\n"
58 " --timestamp Dump media timestamps\n" );
61 static int boxdumper_error
63 lsmash_root_t *root,
64 lsmash_file_parameters_t *file_param,
65 const char *message
68 lsmash_close_file( file_param );
69 lsmash_destroy_root( root );
70 eprintf( "%s", message );
71 return -1;
74 #define BOXDUMPER_ERR( message ) boxdumper_error( root, &file_param, message )
75 #define DO_NOTHING
77 int main( int argc, char *argv[] )
79 if ( argc < 2 )
81 display_help();
82 return -1;
84 else if( !strcasecmp( argv[1], "-h" ) || !strcasecmp( argv[1], "--help" ) )
86 display_help();
87 return 0;
89 else if( !strcasecmp( argv[1], "-v" ) || !strcasecmp( argv[1], "--version" ) )
91 display_version();
92 return 0;
94 int dump_box = 1;
95 int chapter = 0;
96 char *filename;
97 lsmash_get_mainargs( &argc, &argv );
98 if( argc > 2 )
100 if( !strcasecmp( argv[1], "--box" ) )
101 DO_NOTHING;
102 else if( !strcasecmp( argv[1], "--chapter" ) )
103 chapter = 1;
104 else if( !strcasecmp( argv[1], "--timestamp" ) )
105 dump_box = 0;
106 else
108 display_help();
109 return -1;
111 filename = argv[2];
113 else
115 filename = argv[1];
117 /* Open the input file. */
118 lsmash_root_t *root = lsmash_create_root();
119 if( !root )
121 fprintf( stderr, "Failed to create a ROOT.\n" );
122 return -1;
124 lsmash_file_parameters_t file_param = { 0 };
125 if( lsmash_open_file( filename, 1, &file_param ) < 0 )
126 return BOXDUMPER_ERR( "Failed to open an input file.\n" );
127 if( dump_box )
128 file_param.mode |= LSMASH_FILE_MODE_DUMP;
129 lsmash_file_t *file = lsmash_set_file( root, &file_param );
130 if( !file )
131 return BOXDUMPER_ERR( "Failed to add a file into a ROOT.\n" );
132 if( lsmash_read_file( file, &file_param ) < 0 )
133 return BOXDUMPER_ERR( "Failed to read a file\n" );
134 /* Dump the input file. */
135 if( chapter )
137 if( lsmash_print_chapter_list( root ) )
138 return BOXDUMPER_ERR( "Failed to extract chapter.\n" );
140 else if( dump_box )
142 if( lsmash_print_movie( root, "-" ) )
143 return BOXDUMPER_ERR( "Failed to dump box structure.\n" );
145 else
147 lsmash_movie_parameters_t movie_param;
148 lsmash_initialize_movie_parameters( &movie_param );
149 lsmash_get_movie_parameters( root, &movie_param );
150 uint32_t num_tracks = movie_param.number_of_tracks;
151 for( uint32_t track_number = 1; track_number <= num_tracks; track_number++ )
153 uint32_t track_ID = lsmash_get_track_ID( root, track_number );
154 if( !track_ID )
155 return BOXDUMPER_ERR( "Failed to get track_ID.\n" );
156 lsmash_media_parameters_t media_param;
157 lsmash_initialize_media_parameters( &media_param );
158 if( lsmash_get_media_parameters( root, track_ID, &media_param ) )
159 return BOXDUMPER_ERR( "Failed to get media parameters.\n" );
160 if( lsmash_construct_timeline( root, track_ID ) )
161 return BOXDUMPER_ERR( "Failed to construct timeline.\n" );
162 uint32_t timeline_shift;
163 if( lsmash_get_composition_to_decode_shift_from_media_timeline( root, track_ID, &timeline_shift ) )
164 return BOXDUMPER_ERR( "Failed to get timestamps.\n" );
165 lsmash_media_ts_list_t ts_list;
166 if( lsmash_get_media_timestamps( root, track_ID, &ts_list ) )
167 return BOXDUMPER_ERR( "Failed to get timestamps.\n" );
168 fprintf( stdout, "track_ID: %"PRIu32"\n", track_ID );
169 fprintf( stdout, "Media timescale: %"PRIu32"\n", media_param.timescale );
170 lsmash_media_ts_t *ts_array = ts_list.timestamp;
171 if( !ts_array )
173 fprintf( stdout, "\n" );
174 continue;
176 for( uint32_t i = 0; i < ts_list.sample_count; i++ )
177 fprintf( stdout, "DTS = %"PRIu64", CTS = %"PRIu64"\n", ts_array[i].dts, ts_array[i].cts + timeline_shift );
178 lsmash_free( ts_array );
179 fprintf( stdout, "\n" );
182 lsmash_destroy_root( root );
183 return 0;