1 #ifndef MPLAYER_DEMUXER_H
2 #define MPLAYER_DEMUXER_H
9 #include "stream/stream.h"
11 #include "libass/ass_types.h"
14 #ifdef HAVE_BUILTIN_EXPECT
15 #define likely(x) __builtin_expect ((x) != 0, 1)
16 #define unlikely(x) __builtin_expect ((x) != 0, 0)
19 #define unlikely(x) (x)
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_FILM 14
39 #define DEMUXER_TYPE_ROQ 15
40 #define DEMUXER_TYPE_MF 16
41 #define DEMUXER_TYPE_AUDIO 17
42 #define DEMUXER_TYPE_OGG 18
43 #define DEMUXER_TYPE_RAWAUDIO 20
44 #define DEMUXER_TYPE_RTP 21
45 #define DEMUXER_TYPE_RAWDV 22
46 #define DEMUXER_TYPE_PVA 23
47 #define DEMUXER_TYPE_SMJPEG 24
48 #define DEMUXER_TYPE_XMMS 25
49 #define DEMUXER_TYPE_RAWVIDEO 26
50 #define DEMUXER_TYPE_MPEG4_ES 27
51 #define DEMUXER_TYPE_GIF 28
52 #define DEMUXER_TYPE_MPEG_TS 29
53 #define DEMUXER_TYPE_H264_ES 30
54 #define DEMUXER_TYPE_MATROSKA 31
55 #define DEMUXER_TYPE_REALAUDIO 32
56 #define DEMUXER_TYPE_MPEG_TY 33
57 #define DEMUXER_TYPE_LMLM4 34
58 #define DEMUXER_TYPE_LAVF 35
59 #define DEMUXER_TYPE_NSV 36
60 #define DEMUXER_TYPE_VQF 37
61 #define DEMUXER_TYPE_AVS 38
62 #define DEMUXER_TYPE_AAC 39
63 #define DEMUXER_TYPE_MPC 40
64 #define DEMUXER_TYPE_MPEG_PES 41
65 #define DEMUXER_TYPE_MPEG_GXF 42
66 #define DEMUXER_TYPE_NUT 43
67 #define DEMUXER_TYPE_LAVF_PREFERRED 44
68 #define DEMUXER_TYPE_RTP_NEMESI 45
69 #define DEMUXER_TYPE_MNG 46
71 // This should always match the higest demuxer type number.
72 // Unless you want to disallow users to force the demuxer to some types
73 #define DEMUXER_TYPE_MIN 0
74 #define DEMUXER_TYPE_MAX 46
76 #define DEMUXER_TYPE_DEMUXERS (1<<16)
77 // A virtual demuxer type for the network code
78 #define DEMUXER_TYPE_PLAYLIST (2<<16)
81 #define MP_NOPTS_VALUE (-1LL<<63) //both int64_t and double should be able to represent this exactly
84 // DEMUXER control commands/answers
85 #define DEMUXER_CTRL_NOTIMPL -1
86 #define DEMUXER_CTRL_DONTKNOW 0
87 #define DEMUXER_CTRL_OK 1
88 #define DEMUXER_CTRL_GUESS 2
89 #define DEMUXER_CTRL_GET_TIME_LENGTH 10
90 #define DEMUXER_CTRL_GET_PERCENT_POS 11
91 #define DEMUXER_CTRL_SWITCH_AUDIO 12
92 #define DEMUXER_CTRL_RESYNC 13
93 #define DEMUXER_CTRL_SWITCH_VIDEO 14
94 #define DEMUXER_CTRL_IDENTIFY_PROGRAM 15
95 #define DEMUXER_CTRL_CORRECT_PTS 16
97 #define SEEK_ABSOLUTE (1 << 0)
98 #define SEEK_FACTOR (1 << 1)
100 #define MP_INPUT_BUFFER_PADDING_SIZE 8
102 // Holds one packet/frame/whatever
103 typedef struct demux_packet_st
{
108 off_t pos
; // position in index (AVI) or file (MPG)
109 unsigned char* buffer
;
110 int flags
; // keyframe, etc
111 int refcount
; //refcounter for the master packet, if 0, buffer can be free()d
112 struct demux_packet_st
* master
; //pointer to the master packet if this one is a cloned one
113 struct demux_packet_st
* next
;
117 int buffer_pos
; // current buffer position
118 int buffer_size
; // current buffer size
119 unsigned char* buffer
; // current buffer, never free() it, always use free_demux_packet(buffer_ref);
120 double pts
; // current buffer's pts
121 int pts_bytes
; // number of bytes read after last pts stamp
122 int eof
; // end of demuxed stream? (true if all buffer empty)
123 off_t pos
; // position in the input stream (file)
124 off_t dpos
; // position in the demuxed stream
125 int pack_no
; // serial number of packet
126 int flags
; // flags of current packet (keyframe etc)
128 int packs
; // number of packets in buffer
129 int bytes
; // total bytes of packets in buffer
130 demux_packet_t
*first
; // read to current buffer from here
131 demux_packet_t
*last
; // append new packets from input stream to here
132 demux_packet_t
*current
;// needed for refcounting of the buffer
133 int id
; // stream ID (for multiple audio/video streams)
134 struct demuxer_st
*demuxer
; // parent demuxer structure (stream handler)
136 demux_packet_t
*asf_packet
; // read asf fragments here
139 unsigned int ss_mul
,ss_div
;
140 // ---- stream header ----
144 typedef struct demuxer_info_st
{
152 #define MAX_A_STREAMS 256
153 #define MAX_V_STREAMS 256
154 #define MAX_S_STREAMS 256
158 extern int correct_pts
;
159 extern int user_correct_pts
;
162 * Demuxer description structure
164 typedef struct demuxers_desc_st
{
165 const char *info
; ///< What is it (long name and/or description)
166 const char *name
; ///< Demuxer name, used with -demuxer switch
167 const char *shortdesc
; ///< Description printed at demuxer detection
168 const char *author
; ///< Demuxer author(s)
169 const char *comment
; ///< Comment, printed with -demuxer help
171 int type
; ///< DEMUXER_TYPE_xxx
172 int safe_check
; ///< If 1 detection is safe and fast, do it before file extension check
174 /// Check if can demux the file, return DEMUXER_TYPE_xxx on success
175 int (*check_file
)(struct demuxer_st
*demuxer
); ///< Mandatory if safe_check == 1, else optional
176 /// Get packets from file, return 0 on eof
177 int (*fill_buffer
)(struct demuxer_st
*demuxer
, demux_stream_t
*ds
); ///< Mandatory
178 /// Open the demuxer, return demuxer on success, NULL on failure
179 struct demuxer_st
* (*open
)(struct demuxer_st
*demuxer
); ///< Optional
180 /// Close the demuxer
181 void (*close
)(struct demuxer_st
*demuxer
); ///< Optional
183 void (*seek
)(struct demuxer_st
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
); ///< Optional
185 int (*control
)(struct demuxer_st
*demuxer
, int cmd
, void *arg
); ///< Optional
188 typedef struct demux_chapter_s
194 typedef struct demux_attachment_s
199 unsigned int data_size
;
200 } demux_attachment_t
;
202 typedef struct demuxer_st
{
203 const demuxer_desc_t
*desc
; ///< Demuxer description structure
204 off_t filepos
; // input stream current pos.
208 double stream_pts
; // current stream pts, if applicable (e.g. dvd)
209 double reference_clock
;
210 char *filename
; ///< Needed by avs_check_file
211 int synced
; // stream synced (used by mpeg)
212 int type
; // demuxer type: mpeg PS, mpeg ES, avi, avi-ni, avi-nini, asf
213 int file_format
; // file format: mpeg/avi/asf
214 int seekable
; // flag
216 demux_stream_t
*audio
; // audio buffer/demuxer
217 demux_stream_t
*video
; // video buffer/demuxer
218 demux_stream_t
*sub
; // dvd subtitle buffer/demuxer
221 void* a_streams
[MAX_A_STREAMS
]; // audio streams (sh_audio_t)
222 void* v_streams
[MAX_V_STREAMS
]; // video sterams (sh_video_t)
223 void *s_streams
[MAX_S_STREAMS
]; // dvd subtitles (flag)
225 demux_chapter_t
* chapters
;
228 demux_attachment_t
* attachments
;
231 void* priv
; // fileformat-dependent data
236 int progid
; //program id
237 int aid
, vid
, sid
; //audio, video and subtitle id
240 static inline demux_packet_t
* new_demux_packet(int len
){
241 demux_packet_t
* dp
=(demux_packet_t
*)malloc(sizeof(demux_packet_t
));
244 dp
->pts
=MP_NOPTS_VALUE
;
245 dp
->endpts
=MP_NOPTS_VALUE
;
246 dp
->stream_pts
= MP_NOPTS_VALUE
;
252 if (len
> 0 && (dp
->buffer
= (unsigned char *)malloc(len
+ MP_INPUT_BUFFER_PADDING_SIZE
)))
253 memset(dp
->buffer
+ len
, 0, 8);
259 static inline void resize_demux_packet(demux_packet_t
* dp
, int len
)
263 dp
->buffer
=(unsigned char *)realloc(dp
->buffer
,len
+8);
267 if(dp
->buffer
) free(dp
->buffer
);
272 memset(dp
->buffer
+ len
, 0, 8);
277 static inline demux_packet_t
* clone_demux_packet(demux_packet_t
* pack
){
278 demux_packet_t
* dp
=(demux_packet_t
*)malloc(sizeof(demux_packet_t
));
279 while(pack
->master
) pack
=pack
->master
; // find the master
280 memcpy(dp
,pack
,sizeof(demux_packet_t
));
288 static inline void free_demux_packet(demux_packet_t
* dp
){
289 if (dp
->master
==NULL
){ //dp is a master packet
291 if (dp
->refcount
==0){
292 if (dp
->buffer
) free(dp
->buffer
);
298 free_demux_packet(dp
->master
);
303 #define SIZE_MAX ((size_t)-1)
306 static inline void *realloc_struct(void *ptr
, size_t nmemb
, size_t size
) {
307 if (nmemb
> SIZE_MAX
/ size
) {
311 return realloc(ptr
, nmemb
* size
);
314 demux_stream_t
* new_demuxer_stream(struct demuxer_st
*demuxer
,int id
);
315 demuxer_t
* new_demuxer(stream_t
*stream
,int type
,int a_id
,int v_id
,int s_id
,char *filename
);
316 void free_demuxer_stream(demux_stream_t
*ds
);
317 void free_demuxer(demuxer_t
*demuxer
);
319 void ds_add_packet(demux_stream_t
*ds
,demux_packet_t
* dp
);
320 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
, double pts
, off_t pos
, int flags
);
322 int demux_fill_buffer(demuxer_t
*demux
,demux_stream_t
*ds
);
323 int ds_fill_buffer(demux_stream_t
*ds
);
325 static inline off_t
ds_tell(demux_stream_t
*ds
){
326 return (ds
->dpos
-ds
->buffer_size
)+ds
->buffer_pos
;
329 static inline int ds_tell_pts(demux_stream_t
*ds
){
330 return (ds
->pts_bytes
-ds
->buffer_size
)+ds
->buffer_pos
;
333 int demux_read_data(demux_stream_t
*ds
,unsigned char* mem
,int len
);
334 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
335 int *read
, uint32_t pattern
);
337 #define demux_peekc(ds) (\
338 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos] \
339 :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos] ) )
341 #define demux_getc(ds) (\
342 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos++] \
343 :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos++] ) )
345 static inline int demux_getc(demux_stream_t
*ds
){
346 if(ds
->buffer_pos
>=ds
->buffer_size
){
347 if(!ds_fill_buffer(ds
)){
348 // printf("DEMUX_GETC: EOF reached!\n");
352 // printf("[%02X]",ds->buffer[ds->buffer_pos]);
353 return ds
->buffer
[ds
->buffer_pos
++];
357 void ds_free_packs(demux_stream_t
*ds
);
358 int ds_get_packet(demux_stream_t
*ds
,unsigned char **start
);
359 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
);
360 int ds_get_packet_sub(demux_stream_t
*ds
,unsigned char **start
);
361 double ds_get_next_pts(demux_stream_t
*ds
);
363 // This is defined here because demux_stream_t ins't defined in stream.h
364 stream_t
* new_ds_stream(demux_stream_t
*ds
);
366 static inline int avi_stream_id(unsigned int id
){
367 unsigned char *p
=(unsigned char *)&id
;
369 #ifdef WORDS_BIGENDIAN
370 a
=p
[3]-'0'; b
=p
[2]-'0';
372 a
=p
[0]-'0'; b
=p
[1]-'0';
374 if(a
>9 || b
>9) return 100; // invalid ID
378 demuxer_t
* demux_open(stream_t
*stream
,int file_format
,int aid
,int vid
,int sid
,char* filename
);
379 void demux_flush(demuxer_t
*demuxer
);
380 int demux_seek(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
);
381 demuxer_t
* new_demuxers_demuxer(demuxer_t
* vd
, demuxer_t
* ad
, demuxer_t
* sd
);
383 // AVI demuxer params:
384 extern int index_mode
; // -1=untouched 0=don't use index 1=use (geneate) index
385 extern char *index_file_save
, *index_file_load
;
387 extern int pts_from_bps
;
389 extern int extension_parsing
;
391 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
);
392 char* demux_info_get(demuxer_t
*demuxer
, const char *opt
);
393 int demux_info_print(demuxer_t
*demuxer
);
394 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
);
396 #ifdef CONFIG_OGGVORBIS
397 /* Found in demux_ogg.c */
398 int demux_ogg_num_subs(demuxer_t
*demuxer
);
399 int demux_ogg_sub_id(demuxer_t
*demuxer
, int index
);
402 int demuxer_get_current_time(demuxer_t
*demuxer
);
403 double demuxer_get_time_length(demuxer_t
*demuxer
);
404 int demuxer_get_percent_pos(demuxer_t
*demuxer
);
405 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
);
406 int demuxer_switch_video(demuxer_t
*demuxer
, int index
);
408 int demuxer_type_by_filename(char* filename
);
410 void demuxer_help(void);
411 int get_demuxer_type_from_name(char *demuxer_name
, int *force
);
413 int demuxer_add_attachment(demuxer_t
* demuxer
, const char* name
,
414 const char* type
, const void* data
, size_t size
);
416 int demuxer_add_chapter(demuxer_t
* demuxer
, const char* name
, uint64_t start
, uint64_t end
);
417 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, int mode
, float *seek_pts
, int *num_chapters
, char **chapter_name
);
419 /// Get current chapter index if available.
420 int demuxer_get_current_chapter(demuxer_t
*demuxer
);
421 /// Get chapter name by index if available.
422 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
);
423 /// Get chapter display name by index.
424 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
);
425 /// Get chapter start time and end time by index if available.
426 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
);
427 /// Get total chapter number.
428 int demuxer_chapter_count(demuxer_t
*demuxer
);
429 /// Get current angle index.
430 int demuxer_get_current_angle(demuxer_t
*demuxer
);
432 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
);
433 /// Get number of angles.
434 int demuxer_angles_count(demuxer_t
*demuxer
);
436 // get the index of a track
437 // lang is a comma-separated list
438 int demuxer_audio_track_by_lang(demuxer_t
* demuxer
, char* lang
);
439 int demuxer_sub_track_by_lang(demuxer_t
* demuxer
, char* lang
);
441 // find the default track
442 // for subtitles, it is the first track with default attribute
443 // for audio, additionally, the first track is selected if no track has default attribute set
444 int demuxer_default_audio_track(demuxer_t
* d
);
445 int demuxer_default_sub_track(demuxer_t
* d
);
447 #endif /* MPLAYER_DEMUXER_H */