mf demuxer only works with mf:// urls, so check for that.
[mplayer/glamo.git] / libmpdemux / demuxer.h
blob4aa4a5da1d8c65faaa4c0482c919bad43c5360f6
1 #ifndef __DEMUXER_H
2 #define __DEMUXER_H 1
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
59 // This should always match the higest demuxer type number.
60 // Unless you want to disallow users to force the demuxer to some types
61 #define DEMUXER_TYPE_MIN 0
62 #define DEMUXER_TYPE_MAX 43
64 #define DEMUXER_TYPE_DEMUXERS (1<<16)
65 // A virtual demuxer type for the network code
66 #define DEMUXER_TYPE_PLAYLIST (2<<16)
69 #define MP_NOPTS_VALUE (-1LL<<63) //both int64_t and double should be able to represent this exactly
72 // DEMUXER control commands/answers
73 #define DEMUXER_CTRL_NOTIMPL -1
74 #define DEMUXER_CTRL_DONTKNOW 0
75 #define DEMUXER_CTRL_OK 1
76 #define DEMUXER_CTRL_GUESS 2
77 #define DEMUXER_CTRL_GET_TIME_LENGTH 10
78 #define DEMUXER_CTRL_GET_PERCENT_POS 11
79 #define DEMUXER_CTRL_SWITCH_AUDIO 12
80 #define DEMUXER_CTRL_RESYNC 13
81 #define DEMUXER_CTRL_SWITCH_VIDEO 14
82 #define DEMUXER_CTRL_IDENTIFY_PROGRAM 15
84 // Holds one packet/frame/whatever
85 typedef struct demux_packet_st {
86 int len;
87 double pts;
88 double stream_pts;
89 off_t pos; // position in index (AVI) or file (MPG)
90 unsigned char* buffer;
91 int flags; // keyframe, etc
92 int refcount; //refcounter for the master packet, if 0, buffer can be free()d
93 struct demux_packet_st* master; //pointer to the master packet if this one is a cloned one
94 struct demux_packet_st* next;
95 } demux_packet_t;
97 typedef struct {
98 int buffer_pos; // current buffer position
99 int buffer_size; // current buffer size
100 unsigned char* buffer; // current buffer, never free() it, always use free_demux_packet(buffer_ref);
101 double pts; // current buffer's pts
102 int pts_bytes; // number of bytes read after last pts stamp
103 int eof; // end of demuxed stream? (true if all buffer empty)
104 off_t pos; // position in the input stream (file)
105 off_t dpos; // position in the demuxed stream
106 int pack_no; // serial number of packet
107 int flags; // flags of current packet (keyframe etc)
108 //---------------
109 int packs; // number of packets in buffer
110 int bytes; // total bytes of packets in buffer
111 demux_packet_t *first; // read to current buffer from here
112 demux_packet_t *last; // append new packets from input stream to here
113 demux_packet_t *current;// needed for refcounting of the buffer
114 int id; // stream ID (for multiple audio/video streams)
115 struct demuxer_st *demuxer; // parent demuxer structure (stream handler)
116 // ---- asf -----
117 demux_packet_t *asf_packet; // read asf fragments here
118 int asf_seq;
119 // ---- mov -----
120 unsigned int ss_mul,ss_div;
121 // ---- stream header ----
122 void* sh;
123 } demux_stream_t;
125 typedef struct demuxer_info_st {
126 char *name;
127 char *author;
128 char *encoder;
129 char *comments;
130 char *copyright;
131 } demuxer_info_t;
133 #define MAX_A_STREAMS 256
134 #define MAX_V_STREAMS 256
135 #define MAX_S_STREAMS 32
137 struct demuxer_st;
139 extern int correct_pts;
142 * Demuxer description structure
144 typedef struct demuxers_desc_st {
145 const char *info; ///< What is it (long name and/or description)
146 const char *name; ///< Demuxer name, used with -demuxer switch
147 const char *shortdesc; ///< Description printed at demuxer detection
148 const char *author; ///< Demuxer author(s)
149 const char *comment; ///< Comment, printed with -demuxer help
151 int type; ///< DEMUXER_TYPE_xxx
152 int safe_check; ///< If 1 detection is safe and fast, do it before file extension check
154 /// Check if can demux the file, return DEMUXER_TYPE_xxx on success
155 int (*check_file)(struct demuxer_st *demuxer); ///< Mandatory if safe_check == 1, else optional
156 /// Get packets from file, return 0 on eof
157 int (*fill_buffer)(struct demuxer_st *demuxer, demux_stream_t *ds); ///< Mandatory
158 /// Open the demuxer, return demuxer on success, NULL on failure
159 struct demuxer_st* (*open)(struct demuxer_st *demuxer); ///< Optional
160 /// Close the demuxer
161 void (*close)(struct demuxer_st *demuxer); ///< Optional
162 // Seek
163 void (*seek)(struct demuxer_st *demuxer, float rel_seek_secs, float audio_delay, int flags); ///< Optional
164 // Control
165 int (*control)(struct demuxer_st *demuxer, int cmd, void *arg); ///< Optional
166 } demuxer_desc_t;
168 typedef struct demux_chapter_s
170 uint64_t start, end;
171 char* name;
172 } demux_chapter_t;
174 typedef struct demuxer_st {
175 demuxer_desc_t *desc; ///< Demuxer description structure
176 off_t filepos; // input stream current pos.
177 off_t movi_start;
178 off_t movi_end;
179 stream_t *stream;
180 double stream_pts; // current stream pts, if applicable (e.g. dvd)
181 char *filename; ///< Needed by avs_check_file
182 int synced; // stream synced (used by mpeg)
183 int type; // demuxer type: mpeg PS, mpeg ES, avi, avi-ni, avi-nini, asf
184 int file_format; // file format: mpeg/avi/asf
185 int seekable; // flag
187 demux_stream_t *audio; // audio buffer/demuxer
188 demux_stream_t *video; // video buffer/demuxer
189 demux_stream_t *sub; // dvd subtitle buffer/demuxer
191 // stream headers:
192 void* a_streams[MAX_A_STREAMS]; // audio streams (sh_audio_t)
193 void* v_streams[MAX_V_STREAMS]; // video sterams (sh_video_t)
194 void *s_streams[MAX_S_STREAMS]; // dvd subtitles (flag)
196 demux_chapter_t* chapters;
197 int num_chapters;
199 void* priv; // fileformat-dependent data
200 char** info;
201 } demuxer_t;
203 typedef struct {
204 int progid; //program id
205 int aid, vid, sid; //audio, video and subtitle id
206 } demux_program_t;
208 inline static demux_packet_t* new_demux_packet(int len){
209 demux_packet_t* dp=(demux_packet_t*)malloc(sizeof(demux_packet_t));
210 dp->len=len;
211 dp->next=NULL;
212 // still using 0 by default in case there is some code that uses 0 for both
213 // unknown and a valid pts value
214 dp->pts=correct_pts ? MP_NOPTS_VALUE : 0;
215 dp->stream_pts = MP_NOPTS_VALUE;
216 dp->pos=0;
217 dp->flags=0;
218 dp->refcount=1;
219 dp->master=NULL;
220 dp->buffer=NULL;
221 if (len > 0 && (dp->buffer = (unsigned char *)malloc(len + 8)))
222 memset(dp->buffer + len, 0, 8);
223 else
224 dp->len = 0;
225 return dp;
228 inline static void resize_demux_packet(demux_packet_t* dp, int len)
230 if(len > 0)
232 dp->buffer=(unsigned char *)realloc(dp->buffer,len+8);
234 else
236 if(dp->buffer) free(dp->buffer);
237 dp->buffer=NULL;
239 dp->len=len;
240 if (dp->buffer)
241 memset(dp->buffer + len, 0, 8);
242 else
243 dp->len = 0;
246 inline static demux_packet_t* clone_demux_packet(demux_packet_t* pack){
247 demux_packet_t* dp=(demux_packet_t*)malloc(sizeof(demux_packet_t));
248 while(pack->master) pack=pack->master; // find the master
249 memcpy(dp,pack,sizeof(demux_packet_t));
250 dp->next=NULL;
251 dp->refcount=0;
252 dp->master=pack;
253 pack->refcount++;
254 return dp;
257 inline static void free_demux_packet(demux_packet_t* dp){
258 if (dp->master==NULL){ //dp is a master packet
259 dp->refcount--;
260 if (dp->refcount==0){
261 if (dp->buffer) free(dp->buffer);
262 free(dp);
264 return;
266 // dp is a clone:
267 free_demux_packet(dp->master);
268 free(dp);
271 #ifndef SIZE_MAX
272 #define SIZE_MAX ((size_t)-1)
273 #endif
275 static void *realloc_struct(void *ptr, size_t nmemb, size_t size) {
276 if (nmemb > SIZE_MAX / size) {
277 free(ptr);
278 return NULL;
280 return realloc(ptr, nmemb * size);
283 demux_stream_t* new_demuxer_stream(struct demuxer_st *demuxer,int id);
284 demuxer_t* new_demuxer(stream_t *stream,int type,int a_id,int v_id,int s_id,char *filename);
285 void free_demuxer_stream(demux_stream_t *ds);
286 void free_demuxer(demuxer_t *demuxer);
288 void ds_add_packet(demux_stream_t *ds,demux_packet_t* dp);
289 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len, double pts, off_t pos, int flags);
291 int demux_fill_buffer(demuxer_t *demux,demux_stream_t *ds);
292 int ds_fill_buffer(demux_stream_t *ds);
294 inline static off_t ds_tell(demux_stream_t *ds){
295 return (ds->dpos-ds->buffer_size)+ds->buffer_pos;
298 inline static int ds_tell_pts(demux_stream_t *ds){
299 return (ds->pts_bytes-ds->buffer_size)+ds->buffer_pos;
302 int demux_read_data(demux_stream_t *ds,unsigned char* mem,int len);
303 int demux_read_data_pack(demux_stream_t *ds,unsigned char* mem,int len);
304 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
305 int *read, uint32_t pattern);
307 #define demux_peekc(ds) (\
308 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos] \
309 :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos] ) )
310 #if 1
311 #define demux_getc(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 #else
315 inline static int demux_getc(demux_stream_t *ds){
316 if(ds->buffer_pos>=ds->buffer_size){
317 if(!ds_fill_buffer(ds)){
318 // printf("DEMUX_GETC: EOF reached!\n");
319 return -1; // EOF
322 // printf("[%02X]",ds->buffer[ds->buffer_pos]);
323 return ds->buffer[ds->buffer_pos++];
325 #endif
327 void ds_free_packs(demux_stream_t *ds);
328 int ds_get_packet(demux_stream_t *ds,unsigned char **start);
329 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts);
330 int ds_get_packet_sub(demux_stream_t *ds,unsigned char **start);
331 double ds_get_next_pts(demux_stream_t *ds);
333 // This is defined here because demux_stream_t ins't defined in stream.h
334 stream_t* new_ds_stream(demux_stream_t *ds);
336 static inline int avi_stream_id(unsigned int id){
337 unsigned char *p=(unsigned char *)&id;
338 unsigned char a,b;
339 #if WORDS_BIGENDIAN
340 a=p[3]-'0'; b=p[2]-'0';
341 #else
342 a=p[0]-'0'; b=p[1]-'0';
343 #endif
344 if(a>9 || b>9) return 100; // invalid ID
345 return a*10+b;
348 demuxer_t* demux_open(stream_t *stream,int file_format,int aid,int vid,int sid,char* filename);
349 int demux_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags);
350 demuxer_t* new_demuxers_demuxer(demuxer_t* vd, demuxer_t* ad, demuxer_t* sd);
352 // AVI demuxer params:
353 extern int index_mode; // -1=untouched 0=don't use index 1=use (geneate) index
354 extern char *index_file_save, *index_file_load;
355 extern int force_ni;
356 extern int pts_from_bps;
358 extern int extension_parsing;
360 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param);
361 char* demux_info_get(demuxer_t *demuxer, char *opt);
362 int demux_info_print(demuxer_t *demuxer);
363 int demux_control(demuxer_t *demuxer, int cmd, void *arg);
365 #ifdef HAVE_OGGVORBIS
366 /* Found in demux_ogg.c */
367 int demux_ogg_num_subs(demuxer_t *demuxer);
368 int demux_ogg_sub_id(demuxer_t *demuxer, int index);
369 char *demux_ogg_sub_lang(demuxer_t *demuxer, int index);
370 #endif
372 #endif
374 extern int demuxer_get_current_time(demuxer_t *demuxer);
375 extern double demuxer_get_time_length(demuxer_t *demuxer);
376 extern int demuxer_get_percent_pos(demuxer_t *demuxer);
377 extern int demuxer_switch_audio(demuxer_t *demuxer, int index);
378 extern int demuxer_switch_video(demuxer_t *demuxer, int index);
380 extern int demuxer_type_by_filename(char* filename);
382 extern void demuxer_help(void);
383 extern int get_demuxer_type_from_name(char *demuxer_name, int *force);
385 int demuxer_add_chapter(demuxer_t* demuxer, const char* name, uint64_t start, uint64_t end);
386 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, int mode, float *seek_pts, int *num_chapters, char **chapter_name);