1 /*****************************************************************************
2 * mp4.h : MP4 file input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mp4.h,v 1.5 2002/12/06 16:34:06 sam Exp $
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
25 /*****************************************************************************
26 * Structure needed for decoder
27 *****************************************************************************/
28 typedef struct bitmapinfoheader_s
30 uint32_t i_size; /* size of header 40 + size of data follwoing this header */
35 uint32_t i_compression;
37 uint32_t i_xpelspermeter;
38 uint32_t i_ypelspermeter;
40 uint32_t i_clrimportant;
43 typedef struct waveformatex_s
47 uint32_t i_samplepersec;
48 uint32_t i_avgbytespersec;
49 uint16_t i_blockalign;
50 uint16_t i_bitspersample;
51 uint16_t i_size; /* This give size of data
52 imediatly following this header. */
55 /*****************************************************************************
56 * Contain all information about a chunk
57 *****************************************************************************/
58 typedef struct chunk_data_mp4_s
60 uint64_t i_offset; /* absolute position of this chunk in the file */
61 uint32_t i_sample_description_index; /* index for SampleEntry to use */
62 uint32_t i_sample_count; /* how many samples in this chunk */
63 uint32_t i_sample_first; /* index of the first sample in this chunk */
65 /* now provide way to calculate pts, dts, and offset without to
66 much memory and with fast acces */
68 /* with this we can calculate dts/pts without waste memory */
70 uint32_t *p_sample_count_dts;
71 uint32_t *p_sample_delta_dts; /* dts delta */
73 /* TODO if needed add pts
74 but quickly *add* support for edts and seeking */
79 /*****************************************************************************
80 * Contain all needed information for read all track with vlc
81 *****************************************************************************/
82 typedef struct track_data_mp4_s
84 int b_ok; /* The track is usable */
85 int i_track_ID; /* this should be unique */
86 int b_enable; /* is the trak enable by default */
87 int b_selected; /* is the trak being played */
88 int i_cat; /* Type of the track, VIDEO_ES, AUDIO_ES, UNKNOWN_ES ... */
91 /* display size only ! */
95 /* more internal data */
96 uint64_t i_timescale; /* time scale for this track only */
98 /* give the next sample to read, i_chunk is to find quickly where
99 the sample is located */
100 uint32_t i_sample; /* next sample to read */
101 uint32_t i_chunk; /* chunk where next sample is stored */
102 /* total count of chunk and sample */
103 uint32_t i_chunk_count;
104 uint32_t i_sample_count;
106 chunk_data_mp4_t *chunk; /* always defined for each chunk */
108 /* sample size, p_sample_size defined only if i_sample_size == 0
109 else i_sample_size is size for all sample */
110 uint32_t i_sample_size;
111 uint32_t *p_sample_size; /* XXX perhaps add file offset if take
112 too much time to do sumations each time*/
114 es_descriptor_t *p_es; /* vlc es for this track */
116 MP4_Box_t *p_stbl; /* will contain all timing information */
117 MP4_Box_t *p_stsd; /* will contain all data to initialize decoder */
119 MP4_Box_t *p_sample; /* actual SampleEntry to make life simpler */
123 /*****************************************************************************
125 *****************************************************************************/
128 MP4_Box_t box_root; /* container for the whole file */
132 uint64_t i_time; /* time position of the presentation
133 * in movie timescale */
134 uint64_t i_timescale; /* movie time scale */
135 uint64_t i_duration; /* movie duration */
136 unsigned int i_tracks; /* number of tracks */
137 track_data_mp4_t *track; /* array of track */
140 static inline uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
142 unsigned int i_sample;
146 i_pos = p_track->chunk[p_track->i_chunk].i_offset;
148 if( p_track->i_sample_size )
150 i_pos += ( p_track->i_sample -
151 p_track->chunk[p_track->i_chunk].i_sample_first ) *
152 p_track->i_sample_size;
156 for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
157 i_sample < p_track->i_sample; i_sample++ )
159 i_pos += p_track->p_sample_size[i_sample];
166 /* Return time in µs of a track */
167 static inline mtime_t MP4_GetTrackPTS( track_data_mp4_t *p_track )
169 unsigned int i_sample;
170 unsigned int i_index;
173 i_sample = p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first;
174 i_dts = p_track->chunk[p_track->i_chunk].i_first_dts;
176 while( i_sample > 0 )
178 if( i_sample > p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] )
180 i_dts += p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] *
181 p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
182 i_sample -= p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index];
188 p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
196 (mtime_t)p_track->i_timescale ) );
199 static inline mtime_t MP4_GetMoviePTS(demux_sys_t *p_demux )
203 (mtime_t)p_demux->i_time /
204 (mtime_t)p_demux->i_timescale )