Replace a mention of MPlayer by MEncoder in the MEncoder section.
[mplayer/glamo.git] / libmpdemux / demuxer.h
blob5e4f0858e8ab725eec3056f150300fae81ae4a4b
1 #ifndef MPLAYER_DEMUXER_H
2 #define MPLAYER_DEMUXER_H
4 #include <sys/types.h>
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #include "stream/stream.h"
10 #ifdef CONFIG_ASS
11 #include "libass/ass_types.h"
12 #endif
14 #ifdef HAVE_BUILTIN_EXPECT
15 #define likely(x) __builtin_expect ((x) != 0, 1)
16 #define unlikely(x) __builtin_expect ((x) != 0, 0)
17 #else
18 #define likely(x) (x)
19 #define unlikely(x) (x)
20 #endif
22 #define MAX_PACKS 4096
23 #define MAX_PACK_BYTES 0x2000000
25 #define DEMUXER_TYPE_UNKNOWN 0
26 #define DEMUXER_TYPE_MPEG_ES 1
27 #define DEMUXER_TYPE_MPEG_PS 2
28 #define DEMUXER_TYPE_AVI 3
29 #define DEMUXER_TYPE_AVI_NI 4
30 #define DEMUXER_TYPE_AVI_NINI 5
31 #define DEMUXER_TYPE_ASF 6
32 #define DEMUXER_TYPE_MOV 7
33 #define DEMUXER_TYPE_VIVO 8
34 #define DEMUXER_TYPE_TV 9
35 #define DEMUXER_TYPE_FLI 10
36 #define DEMUXER_TYPE_REAL 11
37 #define DEMUXER_TYPE_Y4M 12
38 #define DEMUXER_TYPE_NUV 13
39 #define DEMUXER_TYPE_FILM 14
40 #define DEMUXER_TYPE_ROQ 15
41 #define DEMUXER_TYPE_MF 16
42 #define DEMUXER_TYPE_AUDIO 17
43 #define DEMUXER_TYPE_OGG 18
44 #define DEMUXER_TYPE_RAWAUDIO 20
45 #define DEMUXER_TYPE_RTP 21
46 #define DEMUXER_TYPE_RAWDV 22
47 #define DEMUXER_TYPE_PVA 23
48 #define DEMUXER_TYPE_SMJPEG 24
49 #define DEMUXER_TYPE_XMMS 25
50 #define DEMUXER_TYPE_RAWVIDEO 26
51 #define DEMUXER_TYPE_MPEG4_ES 27
52 #define DEMUXER_TYPE_GIF 28
53 #define DEMUXER_TYPE_MPEG_TS 29
54 #define DEMUXER_TYPE_H264_ES 30
55 #define DEMUXER_TYPE_MATROSKA 31
56 #define DEMUXER_TYPE_REALAUDIO 32
57 #define DEMUXER_TYPE_MPEG_TY 33
58 #define DEMUXER_TYPE_LMLM4 34
59 #define DEMUXER_TYPE_LAVF 35
60 #define DEMUXER_TYPE_NSV 36
61 #define DEMUXER_TYPE_VQF 37
62 #define DEMUXER_TYPE_AVS 38
63 #define DEMUXER_TYPE_AAC 39
64 #define DEMUXER_TYPE_MPC 40
65 #define DEMUXER_TYPE_MPEG_PES 41
66 #define DEMUXER_TYPE_MPEG_GXF 42
67 #define DEMUXER_TYPE_NUT 43
68 #define DEMUXER_TYPE_LAVF_PREFERRED 44
69 #define DEMUXER_TYPE_RTP_NEMESI 45
70 #define DEMUXER_TYPE_MNG 46
72 // This should always match the higest demuxer type number.
73 // Unless you want to disallow users to force the demuxer to some types
74 #define DEMUXER_TYPE_MIN 0
75 #define DEMUXER_TYPE_MAX 46
77 #define DEMUXER_TYPE_DEMUXERS (1<<16)
78 // A virtual demuxer type for the network code
79 #define DEMUXER_TYPE_PLAYLIST (2<<16)
82 #define MP_NOPTS_VALUE (-1LL<<63) //both int64_t and double should be able to represent this exactly
85 // DEMUXER control commands/answers
86 #define DEMUXER_CTRL_NOTIMPL -1
87 #define DEMUXER_CTRL_DONTKNOW 0
88 #define DEMUXER_CTRL_OK 1
89 #define DEMUXER_CTRL_GUESS 2
90 #define DEMUXER_CTRL_GET_TIME_LENGTH 10
91 #define DEMUXER_CTRL_GET_PERCENT_POS 11
92 #define DEMUXER_CTRL_SWITCH_AUDIO 12
93 #define DEMUXER_CTRL_RESYNC 13
94 #define DEMUXER_CTRL_SWITCH_VIDEO 14
95 #define DEMUXER_CTRL_IDENTIFY_PROGRAM 15
96 #define DEMUXER_CTRL_CORRECT_PTS 16
98 #define SEEK_ABSOLUTE (1 << 0)
99 #define SEEK_FACTOR (1 << 1)
101 #define MP_INPUT_BUFFER_PADDING_SIZE 8
103 // Holds one packet/frame/whatever
104 typedef struct demux_packet_st {
105 int len;
106 double pts;
107 double endpts;
108 double stream_pts;
109 off_t pos; // position in index (AVI) or file (MPG)
110 unsigned char* buffer;
111 int flags; // keyframe, etc
112 int refcount; //refcounter for the master packet, if 0, buffer can be free()d
113 struct demux_packet_st* master; //pointer to the master packet if this one is a cloned one
114 struct demux_packet_st* next;
115 } demux_packet_t;
117 typedef struct {
118 int buffer_pos; // current buffer position
119 int buffer_size; // current buffer size
120 unsigned char* buffer; // current buffer, never free() it, always use free_demux_packet(buffer_ref);
121 double pts; // current buffer's pts
122 int pts_bytes; // number of bytes read after last pts stamp
123 int eof; // end of demuxed stream? (true if all buffer empty)
124 off_t pos; // position in the input stream (file)
125 off_t dpos; // position in the demuxed stream
126 int pack_no; // serial number of packet
127 int flags; // flags of current packet (keyframe etc)
128 //---------------
129 int packs; // number of packets in buffer
130 int bytes; // total bytes of packets in buffer
131 demux_packet_t *first; // read to current buffer from here
132 demux_packet_t *last; // append new packets from input stream to here
133 demux_packet_t *current;// needed for refcounting of the buffer
134 int id; // stream ID (for multiple audio/video streams)
135 struct demuxer_st *demuxer; // parent demuxer structure (stream handler)
136 // ---- asf -----
137 demux_packet_t *asf_packet; // read asf fragments here
138 int asf_seq;
139 // ---- mov -----
140 unsigned int ss_mul,ss_div;
141 // ---- stream header ----
142 void* sh;
143 } demux_stream_t;
145 typedef struct demuxer_info_st {
146 char *name;
147 char *author;
148 char *encoder;
149 char *comments;
150 char *copyright;
151 } demuxer_info_t;
153 #define MAX_A_STREAMS 256
154 #define MAX_V_STREAMS 256
155 #define MAX_S_STREAMS 256
157 struct demuxer_st;
159 extern int correct_pts;
160 extern int user_correct_pts;
163 * Demuxer description structure
165 typedef struct demuxers_desc_st {
166 const char *info; ///< What is it (long name and/or description)
167 const char *name; ///< Demuxer name, used with -demuxer switch
168 const char *shortdesc; ///< Description printed at demuxer detection
169 const char *author; ///< Demuxer author(s)
170 const char *comment; ///< Comment, printed with -demuxer help
172 int type; ///< DEMUXER_TYPE_xxx
173 int safe_check; ///< If 1 detection is safe and fast, do it before file extension check
175 /// Check if can demux the file, return DEMUXER_TYPE_xxx on success
176 int (*check_file)(struct demuxer_st *demuxer); ///< Mandatory if safe_check == 1, else optional
177 /// Get packets from file, return 0 on eof
178 int (*fill_buffer)(struct demuxer_st *demuxer, demux_stream_t *ds); ///< Mandatory
179 /// Open the demuxer, return demuxer on success, NULL on failure
180 struct demuxer_st* (*open)(struct demuxer_st *demuxer); ///< Optional
181 /// Close the demuxer
182 void (*close)(struct demuxer_st *demuxer); ///< Optional
183 // Seek
184 void (*seek)(struct demuxer_st *demuxer, float rel_seek_secs, float audio_delay, int flags); ///< Optional
185 // Control
186 int (*control)(struct demuxer_st *demuxer, int cmd, void *arg); ///< Optional
187 } demuxer_desc_t;
189 typedef struct demux_chapter_s
191 uint64_t start, end;
192 char* name;
193 } demux_chapter_t;
195 typedef struct demux_attachment_s
197 char* name;
198 char* type;
199 void* data;
200 unsigned int data_size;
201 } demux_attachment_t;
203 typedef struct demuxer_st {
204 const demuxer_desc_t *desc; ///< Demuxer description structure
205 off_t filepos; // input stream current pos.
206 off_t movi_start;
207 off_t movi_end;
208 stream_t *stream;
209 double stream_pts; // current stream pts, if applicable (e.g. dvd)
210 double reference_clock;
211 char *filename; ///< Needed by avs_check_file
212 int synced; // stream synced (used by mpeg)
213 int type; // demuxer type: mpeg PS, mpeg ES, avi, avi-ni, avi-nini, asf
214 int file_format; // file format: mpeg/avi/asf
215 int seekable; // flag
217 demux_stream_t *audio; // audio buffer/demuxer
218 demux_stream_t *video; // video buffer/demuxer
219 demux_stream_t *sub; // dvd subtitle buffer/demuxer
221 // stream headers:
222 void* a_streams[MAX_A_STREAMS]; // audio streams (sh_audio_t)
223 void* v_streams[MAX_V_STREAMS]; // video sterams (sh_video_t)
224 void *s_streams[MAX_S_STREAMS]; // dvd subtitles (flag)
226 demux_chapter_t* chapters;
227 int num_chapters;
229 demux_attachment_t* attachments;
230 int num_attachments;
232 void* priv; // fileformat-dependent data
233 char** info;
234 } demuxer_t;
236 typedef struct {
237 int progid; //program id
238 int aid, vid, sid; //audio, video and subtitle id
239 } demux_program_t;
241 static inline demux_packet_t* new_demux_packet(int len){
242 demux_packet_t* dp=(demux_packet_t*)malloc(sizeof(demux_packet_t));
243 dp->len=len;
244 dp->next=NULL;
245 dp->pts=MP_NOPTS_VALUE;
246 dp->endpts=MP_NOPTS_VALUE;
247 dp->stream_pts = MP_NOPTS_VALUE;
248 dp->pos=0;
249 dp->flags=0;
250 dp->refcount=1;
251 dp->master=NULL;
252 dp->buffer=NULL;
253 if (len > 0 && (dp->buffer = (unsigned char *)malloc(len + MP_INPUT_BUFFER_PADDING_SIZE)))
254 memset(dp->buffer + len, 0, 8);
255 else
256 dp->len = 0;
257 return dp;
260 static inline void resize_demux_packet(demux_packet_t* dp, int len)
262 if(len > 0)
264 dp->buffer=(unsigned char *)realloc(dp->buffer,len+8);
266 else
268 if(dp->buffer) free(dp->buffer);
269 dp->buffer=NULL;
271 dp->len=len;
272 if (dp->buffer)
273 memset(dp->buffer + len, 0, 8);
274 else
275 dp->len = 0;
278 static inline demux_packet_t* clone_demux_packet(demux_packet_t* pack){
279 demux_packet_t* dp=(demux_packet_t*)malloc(sizeof(demux_packet_t));
280 while(pack->master) pack=pack->master; // find the master
281 memcpy(dp,pack,sizeof(demux_packet_t));
282 dp->next=NULL;
283 dp->refcount=0;
284 dp->master=pack;
285 pack->refcount++;
286 return dp;
289 static inline void free_demux_packet(demux_packet_t* dp){
290 if (dp->master==NULL){ //dp is a master packet
291 dp->refcount--;
292 if (dp->refcount==0){
293 if (dp->buffer) free(dp->buffer);
294 free(dp);
296 return;
298 // dp is a clone:
299 free_demux_packet(dp->master);
300 free(dp);
303 #ifndef SIZE_MAX
304 #define SIZE_MAX ((size_t)-1)
305 #endif
307 static inline void *realloc_struct(void *ptr, size_t nmemb, size_t size) {
308 if (nmemb > SIZE_MAX / size) {
309 free(ptr);
310 return NULL;
312 return realloc(ptr, nmemb * size);
315 demux_stream_t* new_demuxer_stream(struct demuxer_st *demuxer,int id);
316 demuxer_t* new_demuxer(stream_t *stream,int type,int a_id,int v_id,int s_id,char *filename);
317 void free_demuxer_stream(demux_stream_t *ds);
318 void free_demuxer(demuxer_t *demuxer);
320 void ds_add_packet(demux_stream_t *ds,demux_packet_t* dp);
321 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len, double pts, off_t pos, int flags);
323 int demux_fill_buffer(demuxer_t *demux,demux_stream_t *ds);
324 int ds_fill_buffer(demux_stream_t *ds);
326 static inline off_t ds_tell(demux_stream_t *ds){
327 return (ds->dpos-ds->buffer_size)+ds->buffer_pos;
330 static inline int ds_tell_pts(demux_stream_t *ds){
331 return (ds->pts_bytes-ds->buffer_size)+ds->buffer_pos;
334 int demux_read_data(demux_stream_t *ds,unsigned char* mem,int len);
335 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
336 int *read, uint32_t pattern);
338 #define demux_peekc(ds) (\
339 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos] \
340 :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos] ) )
341 #if 1
342 #define demux_getc(ds) (\
343 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos++] \
344 :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos++] ) )
345 #else
346 static inline int demux_getc(demux_stream_t *ds){
347 if(ds->buffer_pos>=ds->buffer_size){
348 if(!ds_fill_buffer(ds)){
349 // printf("DEMUX_GETC: EOF reached!\n");
350 return -1; // EOF
353 // printf("[%02X]",ds->buffer[ds->buffer_pos]);
354 return ds->buffer[ds->buffer_pos++];
356 #endif
358 void ds_free_packs(demux_stream_t *ds);
359 int ds_get_packet(demux_stream_t *ds,unsigned char **start);
360 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts);
361 int ds_get_packet_sub(demux_stream_t *ds,unsigned char **start);
362 double ds_get_next_pts(demux_stream_t *ds);
364 // This is defined here because demux_stream_t ins't defined in stream.h
365 stream_t* new_ds_stream(demux_stream_t *ds);
367 static inline int avi_stream_id(unsigned int id){
368 unsigned char *p=(unsigned char *)&id;
369 unsigned char a,b;
370 #ifdef WORDS_BIGENDIAN
371 a=p[3]-'0'; b=p[2]-'0';
372 #else
373 a=p[0]-'0'; b=p[1]-'0';
374 #endif
375 if(a>9 || b>9) return 100; // invalid ID
376 return a*10+b;
379 demuxer_t* demux_open(stream_t *stream,int file_format,int aid,int vid,int sid,char* filename);
380 void demux_flush(demuxer_t *demuxer);
381 int demux_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags);
382 demuxer_t* new_demuxers_demuxer(demuxer_t* vd, demuxer_t* ad, demuxer_t* sd);
384 // AVI demuxer params:
385 extern int index_mode; // -1=untouched 0=don't use index 1=use (geneate) index
386 extern char *index_file_save, *index_file_load;
387 extern int force_ni;
388 extern int pts_from_bps;
390 extern int extension_parsing;
392 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param);
393 char* demux_info_get(demuxer_t *demuxer, const char *opt);
394 int demux_info_print(demuxer_t *demuxer);
395 int demux_control(demuxer_t *demuxer, int cmd, void *arg);
397 #ifdef CONFIG_OGGVORBIS
398 /* Found in demux_ogg.c */
399 int demux_ogg_num_subs(demuxer_t *demuxer);
400 int demux_ogg_sub_id(demuxer_t *demuxer, int index);
401 #endif
403 int demuxer_get_current_time(demuxer_t *demuxer);
404 double demuxer_get_time_length(demuxer_t *demuxer);
405 int demuxer_get_percent_pos(demuxer_t *demuxer);
406 int demuxer_switch_audio(demuxer_t *demuxer, int index);
407 int demuxer_switch_video(demuxer_t *demuxer, int index);
409 int demuxer_type_by_filename(char* filename);
411 void demuxer_help(void);
412 int get_demuxer_type_from_name(char *demuxer_name, int *force);
414 int demuxer_add_attachment(demuxer_t* demuxer, const char* name,
415 const char* type, const void* data, size_t size);
417 int demuxer_add_chapter(demuxer_t* demuxer, const char* name, uint64_t start, uint64_t end);
418 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, int mode, float *seek_pts, int *num_chapters, char **chapter_name);
420 /// Get current chapter index if available.
421 int demuxer_get_current_chapter(demuxer_t *demuxer);
422 /// Get chapter name by index if available.
423 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter);
424 /// Get chapter display name by index.
425 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter);
426 /// Get chapter start time and end time by index if available.
427 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end);
428 /// Get total chapter number.
429 int demuxer_chapter_count(demuxer_t *demuxer);
430 /// Get current angle index.
431 int demuxer_get_current_angle(demuxer_t *demuxer);
432 /// Set angle.
433 int demuxer_set_angle(demuxer_t *demuxer, int angle);
434 /// Get number of angles.
435 int demuxer_angles_count(demuxer_t *demuxer);
437 // get the index of a track
438 // lang is a comma-separated list
439 int demuxer_audio_track_by_lang(demuxer_t* demuxer, char* lang);
440 int demuxer_sub_track_by_lang(demuxer_t* demuxer, char* lang);
442 // find the default track
443 // for subtitles, it is the first track with default attribute
444 // for audio, additionally, the first track is selected if no track has default attribute set
445 int demuxer_default_audio_track(demuxer_t* d);
446 int demuxer_default_sub_track(demuxer_t* d);
448 #endif /* MPLAYER_DEMUXER_H */