timelineeditor: Support importing timecode format v1 and automatic timescale generation.
[L-SMASH.git] / boxdumper.c
blob648402bb81598f7c7bf073f2761fa6905f6e71e6
1 /*****************************************************************************
2 * boxdumper.c:
3 *****************************************************************************
4 * Copyright (C) 2010 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 "lsmash.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 int print_help( int ret )
38 eprintf( "Usage: boxdumper [option] input\n"
39 " options:\n"
40 " --box Dump box structure\n"
41 " --timestamp Dump media timestamps\n" );
42 return ret;
45 static int boxdumper_error( lsmash_root_t *root, char* message )
47 lsmash_destroy_root( root );
48 eprintf( message );
49 return -1;
52 #define BOXDUMPER_ERR( message ) boxdumper_error( root, message )
53 #define DO_NOTHING
55 int main( int argc, char *argv[] )
57 if( argc < 2 || argc > 3 )
58 return print_help( -1 );
59 int dump_box = 1;
60 char *filename;
61 if( argc > 2 )
63 if( !strcasecmp( argv[1], "--box" ) )
64 DO_NOTHING;
65 else if( !strcasecmp( argv[1], "--timestamp" ) )
66 dump_box = 0;
67 else
68 return print_help( -1 );
69 filename = argv[2];
71 else
73 if( !strcasecmp( argv[1], "-h" ) || !strcasecmp( argv[1], "--help" ) )
74 return print_help( 0 );
75 filename = argv[1];
77 #ifdef _WIN32
78 _setmode( _fileno(stdin), _O_BINARY );
79 #endif
80 lsmash_file_mode mode = dump_box ? (LSMASH_FILE_MODE_READ | LSMASH_FILE_MODE_DUMP) : LSMASH_FILE_MODE_READ;
81 lsmash_root_t *root = lsmash_open_movie( filename, mode );
82 if( !root )
84 fprintf( stderr, "Failed to open input file.\n" );
85 return -1;
87 if( dump_box )
89 if( lsmash_print_movie( root ) )
90 return BOXDUMPER_ERR( "Failed to dump box structure.\n" );
92 else
94 lsmash_movie_parameters_t movie_param;
95 lsmash_initialize_movie_parameters( &movie_param );
96 lsmash_get_movie_parameters( root, &movie_param );
97 uint32_t num_tracks = movie_param.number_of_tracks;
98 for( uint32_t track_number = 1; track_number <= num_tracks; track_number++ )
100 uint32_t track_ID = lsmash_get_track_ID( root, track_number );
101 if( !track_ID )
102 return BOXDUMPER_ERR( "Failed to get track_ID.\n" );
103 lsmash_media_parameters_t media_param;
104 lsmash_initialize_media_parameters( &media_param );
105 if( lsmash_get_media_parameters( root, track_ID, &media_param ) )
106 return BOXDUMPER_ERR( "Failed to get media parameters.\n" );
107 if( lsmash_construct_timeline( root, track_ID ) )
108 return BOXDUMPER_ERR( "Failed to construct timeline.\n" );
109 int32_t timeline_shift;
110 if( lsmash_get_media_timeline_shift( root, track_ID, &timeline_shift ) )
111 return BOXDUMPER_ERR( "Failed to get timestamps.\n" );
112 lsmash_media_ts_list_t ts_list;
113 if( lsmash_get_media_timestamps( root, track_ID, &ts_list ) )
114 return BOXDUMPER_ERR( "Failed to get timestamps.\n" );
115 fprintf( stdout, "track_ID: %"PRIu32"\n", track_ID );
116 fprintf( stdout, "Media timescale: %"PRIu32"\n", media_param.timescale );
117 lsmash_media_ts_t *ts_array = ts_list.timestamp;
118 if( !ts_array )
120 fprintf( stdout, "\n" );
121 continue;
123 for( uint32_t i = 0; i < ts_list.sample_count; i++ )
124 fprintf( stdout, "DTS = %"PRIu64", CTS = %"PRIu64"\n", ts_array[i].dts, ts_array[i].cts + timeline_shift );
125 free( ts_array );
126 fprintf( stdout, "\n" );
129 lsmash_destroy_root( root );
130 return 0;