]> git.sesse.net Git - vlc/blob - modules/demux/mp4/mp4.h
* ./modules/demux/mp4: DRMS support.
[vlc] / modules / demux / mp4 / mp4.h
1 /*****************************************************************************
2  * mp4.h : MP4 file input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: mp4.h,v 1.10 2004/01/05 12:37:52 jlj Exp $
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23
24 /*****************************************************************************
25  * Contain all information about a chunk
26  *****************************************************************************/
27 typedef struct chunk_data_mp4_s
28 {
29     uint64_t     i_offset; /* absolute position of this chunk in the file */
30     uint32_t     i_sample_description_index; /* index for SampleEntry to use */
31     uint32_t     i_sample_count; /* how many samples in this chunk */
32     uint32_t     i_sample_first; /* index of the first sample in this chunk */
33
34     /* now provide way to calculate pts, dts, and offset without to
35         much memory and with fast acces */
36
37     /* with this we can calculate dts/pts without waste memory */
38     uint64_t     i_first_dts;
39     uint32_t     *p_sample_count_dts;
40     uint32_t     *p_sample_delta_dts; /* dts delta */
41
42     /* TODO if needed add pts
43         but quickly *add* support for edts and seeking */
44
45 } chunk_data_mp4_t;
46
47
48 /*****************************************************************************
49  * Contain all needed information for read all track with vlc
50  *****************************************************************************/
51 typedef struct track_data_mp4_s
52 {
53     int i_track_ID;     /* this should be unique */
54
55     int b_ok;           /* The track is usable */
56     int b_enable;       /* is the trak enable by default */
57     vlc_bool_t b_selected;     /* is the trak being played */
58
59     es_format_t fmt;
60     es_out_id_t *p_es;
61
62     /* display size only ! */
63     int i_width;
64     int i_height;
65
66     /* more internal data */
67     uint64_t         i_timescale;  /* time scale for this track only */
68
69     /* give the next sample to read, i_chunk is to find quickly where
70       the sample is located */
71     uint32_t         i_sample;       /* next sample to read */
72     uint32_t         i_chunk;        /* chunk where next sample is stored */
73     /* total count of chunk and sample */
74     uint32_t         i_chunk_count;
75     uint32_t         i_sample_count;
76
77     chunk_data_mp4_t    *chunk; /* always defined  for each chunk */
78
79     /* sample size, p_sample_size defined only if i_sample_size == 0
80         else i_sample_size is size for all sample */
81     uint32_t         i_sample_size;
82     uint32_t         *p_sample_size; /* XXX perhaps add file offset if take
83                                     too much time to do sumations each time*/
84
85     MP4_Box_t *p_stbl;  /* will contain all timing information */
86     MP4_Box_t *p_stsd;  /* will contain all data to initialize decoder */
87     MP4_Box_t *p_sample;/* point on actual sdsd */
88
89     void *p_drms;
90
91 } track_data_mp4_t;
92
93
94 /*****************************************************************************
95  *
96  *****************************************************************************/
97 struct demux_sys_t
98 {
99     MP4_Box_t    *p_root;      /* container for the whole file */
100
101     mtime_t      i_pcr;
102
103     uint64_t     i_time;        /* time position of the presentation
104                                  * in movie timescale */
105     uint64_t     i_timescale;   /* movie time scale */
106     uint64_t     i_duration;    /* movie duration */
107     unsigned int i_tracks;      /* number of tracks */
108     track_data_mp4_t *track;    /* array of track */
109 };
110
111 #if 0
112 static inline uint64_t MP4_GetTrackPos( track_data_mp4_t *p_track )
113 {
114     unsigned int i_sample;
115     uint64_t i_pos;
116
117
118     i_pos = p_track->chunk[p_track->i_chunk].i_offset;
119
120     if( p_track->i_sample_size )
121     {
122         i_pos += ( p_track->i_sample -
123                         p_track->chunk[p_track->i_chunk].i_sample_first ) *
124                                 p_track->i_sample_size;
125     }
126     else
127     {
128         for( i_sample = p_track->chunk[p_track->i_chunk].i_sample_first;
129                 i_sample < p_track->i_sample; i_sample++ )
130         {
131             i_pos += p_track->p_sample_size[i_sample];
132         }
133
134     }
135     return( i_pos );
136 }
137 #endif
138
139 /* Return time in µs of a track */
140 static inline mtime_t MP4_GetTrackPTS( track_data_mp4_t *p_track )
141 {
142     unsigned int i_sample;
143     unsigned int i_index;
144     uint64_t i_dts;
145
146     i_sample = p_track->i_sample - p_track->chunk[p_track->i_chunk].i_sample_first;
147     i_dts = p_track->chunk[p_track->i_chunk].i_first_dts;
148     i_index = 0;
149     while( i_sample > 0 )
150     {
151         if( i_sample > p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] )
152         {
153             i_dts += p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index] *
154                         p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
155             i_sample -= p_track->chunk[p_track->i_chunk].p_sample_count_dts[i_index];
156             i_index++;
157         }
158         else
159         {
160             i_dts += i_sample *
161                         p_track->chunk[p_track->i_chunk].p_sample_delta_dts[i_index];
162             i_sample = 0;
163             break;
164         }
165     }
166     return( (mtime_t)(
167                 (mtime_t)1000000 *
168                 (mtime_t)i_dts /
169                 (mtime_t)p_track->i_timescale ) );
170 }
171
172 static inline mtime_t MP4_GetMoviePTS(demux_sys_t *p_demux )
173 {
174     return( (mtime_t)(
175                 (mtime_t)1000000 *
176                 (mtime_t)p_demux->i_time /
177                 (mtime_t)p_demux->i_timescale )
178           );
179 }