Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / libmpdemux / demuxer.h
blobe5a7049dee940d33f3362a5c962874c2ec516c9a
1 #ifndef DEMUXER_H
2 #define DEMUXER_H
4 #ifdef USE_ASS
5 #include "libass/ass_types.h"
6 #endif
8 #define MAX_PACKS 4096
9 #ifdef HAVE_TV_BSDBT848
10 #define MAX_PACK_BYTES 0x2000000
11 #else
12 #define MAX_PACK_BYTES 0x800000
13 #endif
15 #define DEMUXER_TYPE_UNKNOWN 0
16 #define DEMUXER_TYPE_MPEG_ES 1
17 #define DEMUXER_TYPE_MPEG_PS 2
18 #define DEMUXER_TYPE_AVI 3
19 #define DEMUXER_TYPE_AVI_NI 4
20 #define DEMUXER_TYPE_AVI_NINI 5
21 #define DEMUXER_TYPE_ASF 6
22 #define DEMUXER_TYPE_MOV 7
23 #define DEMUXER_TYPE_VIVO 8
24 #define DEMUXER_TYPE_TV 9
25 #define DEMUXER_TYPE_FLI 10
26 #define DEMUXER_TYPE_REAL 11
27 #define DEMUXER_TYPE_Y4M 12
28 #define DEMUXER_TYPE_NUV 13
29 #define DEMUXER_TYPE_FILM 14
30 #define DEMUXER_TYPE_ROQ 15
31 #define DEMUXER_TYPE_MF 16
32 #define DEMUXER_TYPE_AUDIO 17
33 #define DEMUXER_TYPE_OGG 18
34 #define DEMUXER_TYPE_RAWAUDIO 20
35 #define DEMUXER_TYPE_RTP 21
36 #define DEMUXER_TYPE_RAWDV 22
37 #define DEMUXER_TYPE_PVA 23
38 #define DEMUXER_TYPE_SMJPEG 24
39 #define DEMUXER_TYPE_XMMS 25
40 #define DEMUXER_TYPE_RAWVIDEO 26
41 #define DEMUXER_TYPE_MPEG4_ES 27
42 #define DEMUXER_TYPE_GIF 28
43 #define DEMUXER_TYPE_MPEG_TS 29
44 #define DEMUXER_TYPE_H264_ES 30
45 #define DEMUXER_TYPE_MATROSKA 31
46 #define DEMUXER_TYPE_REALAUDIO 32
47 #define DEMUXER_TYPE_MPEG_TY 33
48 #define DEMUXER_TYPE_LMLM4 34
49 #define DEMUXER_TYPE_LAVF 35
50 #define DEMUXER_TYPE_NSV 36
51 #define DEMUXER_TYPE_VQF 37
52 #define DEMUXER_TYPE_AVS 38
53 #define DEMUXER_TYPE_AAC 39
54 #define DEMUXER_TYPE_MPC 40
55 #define DEMUXER_TYPE_MPEG_PES 41
56 #define DEMUXER_TYPE_MPEG_GXF 42
57 #define DEMUXER_TYPE_NUT 43
58 #define DEMUXER_TYPE_LAVF_PREFERRED 44
59 #define DEMUXER_TYPE_RTP_NEMESI 45
61 // This should always match the higest demuxer type number.
62 // Unless you want to disallow users to force the demuxer to some types
63 #define DEMUXER_TYPE_MIN 0
64 #define DEMUXER_TYPE_MAX 45
66 #define DEMUXER_TYPE_DEMUXERS (1<<16)
67 // A virtual demuxer type for the network code
68 #define DEMUXER_TYPE_PLAYLIST (2<<16)
71 #define MP_NOPTS_VALUE (-1LL<<63) //both int64_t and double should be able to represent this exactly
74 // DEMUXER control commands/answers
75 #define DEMUXER_CTRL_NOTIMPL -1
76 #define DEMUXER_CTRL_DONTKNOW 0
77 #define DEMUXER_CTRL_OK 1
78 #define DEMUXER_CTRL_GUESS 2
79 #define DEMUXER_CTRL_GET_TIME_LENGTH 10
80 #define DEMUXER_CTRL_GET_PERCENT_POS 11
81 #define DEMUXER_CTRL_SWITCH_AUDIO 12
82 #define DEMUXER_CTRL_RESYNC 13
83 #define DEMUXER_CTRL_SWITCH_VIDEO 14
84 #define DEMUXER_CTRL_IDENTIFY_PROGRAM 15
86 // Holds one packet/frame/whatever
87 typedef struct demux_packet_st {
88 int len;
89 double pts;
90 double endpts;
91 double stream_pts;
92 off_t pos; // position in index (AVI) or file (MPG)
93 unsigned char* buffer;
94 int flags; // keyframe, etc
95 int refcount; //refcounter for the master packet, if 0, buffer can be free()d
96 struct demux_packet_st* master; //pointer to the master packet if this one is a cloned one
97 struct demux_packet_st* next;
98 } demux_packet_t;
100 typedef struct {
101 int buffer_pos; // current buffer position
102 int buffer_size; // current buffer size
103 unsigned char* buffer; // current buffer, never free() it, always use free_demux_packet(buffer_ref);
104 double pts; // current buffer's pts
105 int pts_bytes; // number of bytes read after last pts stamp
106 int eof; // end of demuxed stream? (true if all buffer empty)
107 off_t pos; // position in the input stream (file)
108 off_t dpos; // position in the demuxed stream
109 int pack_no; // serial number of packet
110 int flags; // flags of current packet (keyframe etc)
111 //---------------
112 int packs; // number of packets in buffer
113 int bytes; // total bytes of packets in buffer
114 demux_packet_t *first; // read to current buffer from here
115 demux_packet_t *last; // append new packets from input stream to here
116 demux_packet_t *current;// needed for refcounting of the buffer
117 int id; // stream ID (for multiple audio/video streams)
118 struct demuxer_st *demuxer; // parent demuxer structure (stream handler)
119 // ---- asf -----
120 demux_packet_t *asf_packet; // read asf fragments here
121 int asf_seq;
122 // ---- mov -----
123 unsigned int ss_mul,ss_div;
124 // ---- stream header ----
125 void* sh;
126 } demux_stream_t;
128 typedef struct demuxer_info_st {
129 char *name;
130 char *author;
131 char *encoder;
132 char *comments;
133 char *copyright;
134 } demuxer_info_t;
136 #define MAX_A_STREAMS 256
137 #define MAX_V_STREAMS 256
138 #define MAX_S_STREAMS 32
140 struct demuxer_st;
142 extern int correct_pts;
145 * Demuxer description structure
147 typedef struct demuxers_desc_st {
148 const char *info; ///< What is it (long name and/or description)
149 const char *name; ///< Demuxer name, used with -demuxer switch
150 const char *shortdesc; ///< Description printed at demuxer detection
151 const char *author; ///< Demuxer author(s)
152 const char *comment; ///< Comment, printed with -demuxer help
154 int type; ///< DEMUXER_TYPE_xxx
155 int safe_check; ///< If 1 detection is safe and fast, do it before file extension check
157 /// Check if can demux the file, return DEMUXER_TYPE_xxx on success
158 int (*check_file)(struct demuxer_st *demuxer); ///< Mandatory if safe_check == 1, else optional
159 /// Get packets from file, return 0 on eof
160 int (*fill_buffer)(struct demuxer_st *demuxer, demux_stream_t *ds); ///< Mandatory
161 /// Open the demuxer, return demuxer on success, NULL on failure
162 struct demuxer_st* (*open)(struct demuxer_st *demuxer); ///< Optional
163 /// Close the demuxer
164 void (*close)(struct demuxer_st *demuxer); ///< Optional
165 // Seek
166 void (*seek)(struct demuxer_st *demuxer, float rel_seek_secs, float audio_delay, int flags); ///< Optional
167 // Control
168 int (*control)(struct demuxer_st *demuxer, int cmd, void *arg); ///< Optional
169 } demuxer_desc_t;
171 typedef struct demux_chapter_s
173 uint64_t start, end;
174 char* name;
175 } demux_chapter_t;
177 typedef struct demuxer_st {
178 demuxer_desc_t *desc; ///< Demuxer description structure
179 off_t filepos; // input stream current pos.
180 off_t movi_start;
181 off_t movi_end;
182 stream_t *stream;
183 double stream_pts; // current stream pts, if applicable (e.g. dvd)
184 char *filename; ///< Needed by avs_check_file
185 int synced; // stream synced (used by mpeg)
186 int type; // demuxer type: mpeg PS, mpeg ES, avi, avi-ni, avi-nini, asf
187 int file_format; // file format: mpeg/avi/asf
188 int seekable; // flag
190 demux_stream_t *audio; // audio buffer/demuxer
191 demux_stream_t *video; // video buffer/demuxer
192 demux_stream_t *sub; // dvd subtitle buffer/demuxer
194 // stream headers:
195 void* a_streams[MAX_A_STREAMS]; // audio streams (sh_audio_t)
196 void* v_streams[MAX_V_STREAMS]; // video sterams (sh_video_t)
197 void *s_streams[MAX_S_STREAMS]; // dvd subtitles (flag)
199 demux_chapter_t* chapters;
200 int num_chapters;
202 void* priv; // fileformat-dependent data
203 char** info;
204 } demuxer_t;
206 typedef struct {
207 int progid; //program id
208 int aid, vid, sid; //audio, video and subtitle id
209 } demux_program_t;
211 static inline demux_packet_t* new_demux_packet(int len){
212 demux_packet_t* dp=(demux_packet_t*)malloc(sizeof(demux_packet_t));
213 dp->len=len;
214 dp->next=NULL;
215 // still using 0 by default in case there is some code that uses 0 for both
216 // unknown and a valid pts value
217 dp->pts=correct_pts ? MP_NOPTS_VALUE : 0;
218 dp->endpts=MP_NOPTS_VALUE;
219 dp->stream_pts = MP_NOPTS_VALUE;
220 dp->pos=0;
221 dp->flags=0;
222 dp->refcount=1;
223 dp->master=NULL;
224 dp->buffer=NULL;
225 if (len > 0 && (dp->buffer = (unsigned char *)malloc(len + 8)))
226 memset(dp->buffer + len, 0, 8);
227 else
228 dp->len = 0;
229 return dp;
232 static inline void resize_demux_packet(demux_packet_t* dp, int len)
234 if(len > 0)
236 dp->buffer=(unsigned char *)realloc(dp->buffer,len+8);
238 else
240 if(dp->buffer) free(dp->buffer);
241 dp->buffer=NULL;
243 dp->len=len;
244 if (dp->buffer)
245 memset(dp->buffer + len, 0, 8);
246 else
247 dp->len = 0;
250 static inline demux_packet_t* clone_demux_packet(demux_packet_t* pack){
251 demux_packet_t* dp=(demux_packet_t*)malloc(sizeof(demux_packet_t));
252 while(pack->master) pack=pack->master; // find the master
253 memcpy(dp,pack,sizeof(demux_packet_t));
254 dp->next=NULL;
255 dp->refcount=0;
256 dp->master=pack;
257 pack->refcount++;
258 return dp;
261 static inline void free_demux_packet(demux_packet_t* dp){
262 if (dp->master==NULL){ //dp is a master packet
263 dp->refcount--;
264 if (dp->refcount==0){
265 if (dp->buffer) free(dp->buffer);
266 free(dp);
268 return;
270 // dp is a clone:
271 free_demux_packet(dp->master);
272 free(dp);
275 #ifndef SIZE_MAX
276 #define SIZE_MAX ((size_t)-1)
277 #endif
279 static inline void *realloc_struct(void *ptr, size_t nmemb, size_t size) {
280 if (nmemb > SIZE_MAX / size) {
281 free(ptr);
282 return NULL;
284 return realloc(ptr, nmemb * size);
287 demux_stream_t* new_demuxer_stream(struct demuxer_st *demuxer,int id);
288 demuxer_t* new_demuxer(stream_t *stream,int type,int a_id,int v_id,int s_id,char *filename);
289 void free_demuxer_stream(demux_stream_t *ds);
290 void free_demuxer(demuxer_t *demuxer);
292 void ds_add_packet(demux_stream_t *ds,demux_packet_t* dp);
293 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len, double pts, off_t pos, int flags);
295 int demux_fill_buffer(demuxer_t *demux,demux_stream_t *ds);
296 int ds_fill_buffer(demux_stream_t *ds);
298 static inline off_t ds_tell(demux_stream_t *ds){
299 return (ds->dpos-ds->buffer_size)+ds->buffer_pos;
302 static inline int ds_tell_pts(demux_stream_t *ds){
303 return (ds->pts_bytes-ds->buffer_size)+ds->buffer_pos;
306 int demux_read_data(demux_stream_t *ds,unsigned char* mem,int len);
307 int demux_read_data_pack(demux_stream_t *ds,unsigned char* mem,int len);
308 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
309 int *read, uint32_t pattern);
311 #define demux_peekc(ds) (\
312 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos] \
313 :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos] ) )
314 #if 1
315 #define demux_getc(ds) (\
316 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos++] \
317 :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos++] ) )
318 #else
319 static inline int demux_getc(demux_stream_t *ds){
320 if(ds->buffer_pos>=ds->buffer_size){
321 if(!ds_fill_buffer(ds)){
322 // printf("DEMUX_GETC: EOF reached!\n");
323 return -1; // EOF
326 // printf("[%02X]",ds->buffer[ds->buffer_pos]);
327 return ds->buffer[ds->buffer_pos++];
329 #endif
331 void ds_free_packs(demux_stream_t *ds);
332 int ds_get_packet(demux_stream_t *ds,unsigned char **start);
333 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts);
334 int ds_get_packet_sub(demux_stream_t *ds,unsigned char **start);
335 double ds_get_next_pts(demux_stream_t *ds);
337 // This is defined here because demux_stream_t ins't defined in stream.h
338 stream_t* new_ds_stream(demux_stream_t *ds);
340 static inline int avi_stream_id(unsigned int id){
341 unsigned char *p=(unsigned char *)&id;
342 unsigned char a,b;
343 #if WORDS_BIGENDIAN
344 a=p[3]-'0'; b=p[2]-'0';
345 #else
346 a=p[0]-'0'; b=p[1]-'0';
347 #endif
348 if(a>9 || b>9) return 100; // invalid ID
349 return a*10+b;
352 demuxer_t* demux_open(stream_t *stream,int file_format,int aid,int vid,int sid,char* filename);
353 int demux_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags);
354 demuxer_t* new_demuxers_demuxer(demuxer_t* vd, demuxer_t* ad, demuxer_t* sd);
356 // AVI demuxer params:
357 extern int index_mode; // -1=untouched 0=don't use index 1=use (geneate) index
358 extern char *index_file_save, *index_file_load;
359 extern int force_ni;
360 extern int pts_from_bps;
362 extern int extension_parsing;
364 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param);
365 char* demux_info_get(demuxer_t *demuxer, const char *opt);
366 int demux_info_print(demuxer_t *demuxer);
367 int demux_control(demuxer_t *demuxer, int cmd, void *arg);
369 #ifdef HAVE_OGGVORBIS
370 /* Found in demux_ogg.c */
371 int demux_ogg_num_subs(demuxer_t *demuxer);
372 int demux_ogg_sub_id(demuxer_t *demuxer, int index);
373 const char *demux_ogg_sub_lang(demuxer_t *demuxer, int index);
374 #endif
376 extern int demuxer_get_current_time(demuxer_t *demuxer);
377 extern double demuxer_get_time_length(demuxer_t *demuxer);
378 extern int demuxer_get_percent_pos(demuxer_t *demuxer);
379 extern int demuxer_switch_audio(demuxer_t *demuxer, int index);
380 extern int demuxer_switch_video(demuxer_t *demuxer, int index);
382 extern int demuxer_type_by_filename(char* filename);
384 extern void demuxer_help(void);
385 extern int get_demuxer_type_from_name(char *demuxer_name, int *force);
387 int demuxer_add_chapter(demuxer_t* demuxer, const char* name, uint64_t start, uint64_t end);
388 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, int mode, float *seek_pts, int *num_chapters, char **chapter_name);
390 /// Get current chapter index if available.
391 int demuxer_get_current_chapter(demuxer_t *demuxer);
392 /// Get chapter name by index if available.
393 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter);
394 /// Get chapter display name by index.
395 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter);
396 /// Get chapter start time and end time by index if available.
397 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end);
398 /// Get total chapter number.
399 int demuxer_chapter_count(demuxer_t *demuxer);
401 #endif /* DEMUXER_H */