2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #ifndef MPLAYER_DEMUXER_H
20 #define MPLAYER_DEMUXER_H
22 #include <sys/types.h>
28 #include "stream/stream.h"
33 #ifdef HAVE_BUILTIN_EXPECT
34 #define likely(x) __builtin_expect ((x) != 0, 1)
35 #define unlikely(x) __builtin_expect ((x) != 0, 0)
38 #define unlikely(x) (x)
41 #define MAX_PACKS 4096
42 #define MAX_PACK_BYTES 0x8000000 // 128 MiB
44 #define DEMUXER_TYPE_UNKNOWN 0
45 #define DEMUXER_TYPE_MPEG_ES 1
46 #define DEMUXER_TYPE_MPEG_PS 2
47 #define DEMUXER_TYPE_AVI 3
48 #define DEMUXER_TYPE_AVI_NI 4
49 #define DEMUXER_TYPE_AVI_NINI 5
50 #define DEMUXER_TYPE_ASF 6
51 #define DEMUXER_TYPE_MOV 7
52 #define DEMUXER_TYPE_VIVO 8
53 #define DEMUXER_TYPE_TV 9
54 #define DEMUXER_TYPE_FLI 10
55 #define DEMUXER_TYPE_REAL 11
56 #define DEMUXER_TYPE_Y4M 12
57 #define DEMUXER_TYPE_FILM 14
58 #define DEMUXER_TYPE_ROQ 15
59 #define DEMUXER_TYPE_MF 16
60 #define DEMUXER_TYPE_AUDIO 17
61 #define DEMUXER_TYPE_OGG 18
62 #define DEMUXER_TYPE_RAWAUDIO 20
63 #define DEMUXER_TYPE_RTP 21
64 #define DEMUXER_TYPE_RAWDV 22
65 #define DEMUXER_TYPE_PVA 23
66 #define DEMUXER_TYPE_SMJPEG 24
67 #define DEMUXER_TYPE_XMMS 25
68 #define DEMUXER_TYPE_RAWVIDEO 26
69 #define DEMUXER_TYPE_MPEG4_ES 27
70 #define DEMUXER_TYPE_GIF 28
71 #define DEMUXER_TYPE_MPEG_TS 29
72 #define DEMUXER_TYPE_H264_ES 30
73 #define DEMUXER_TYPE_MATROSKA 31
74 #define DEMUXER_TYPE_REALAUDIO 32
75 #define DEMUXER_TYPE_MPEG_TY 33
76 #define DEMUXER_TYPE_LMLM4 34
77 #define DEMUXER_TYPE_LAVF 35
78 #define DEMUXER_TYPE_NSV 36
79 #define DEMUXER_TYPE_VQF 37
80 #define DEMUXER_TYPE_AVS 38
81 #define DEMUXER_TYPE_AAC 39
82 #define DEMUXER_TYPE_MPC 40
83 #define DEMUXER_TYPE_MPEG_PES 41
84 #define DEMUXER_TYPE_MPEG_GXF 42
85 #define DEMUXER_TYPE_NUT 43
86 #define DEMUXER_TYPE_LAVF_PREFERRED 44
87 #define DEMUXER_TYPE_RTP_NEMESI 45
88 #define DEMUXER_TYPE_MNG 46
90 // This should always match the higest demuxer type number.
91 // Unless you want to disallow users to force the demuxer to some types
92 #define DEMUXER_TYPE_MIN 0
93 #define DEMUXER_TYPE_MAX 46
95 #define DEMUXER_TYPE_DEMUXERS (1<<16)
96 // A virtual demuxer type for the network code
97 #define DEMUXER_TYPE_PLAYLIST (2<<16)
100 #define MP_NOPTS_VALUE (-1LL<<63) //both int64_t and double should be able to represent this exactly
103 // DEMUXER control commands/answers
104 #define DEMUXER_CTRL_NOTIMPL -1
105 #define DEMUXER_CTRL_DONTKNOW 0
106 #define DEMUXER_CTRL_OK 1
107 #define DEMUXER_CTRL_GUESS 2
108 #define DEMUXER_CTRL_GET_TIME_LENGTH 10
109 #define DEMUXER_CTRL_GET_PERCENT_POS 11
110 #define DEMUXER_CTRL_SWITCH_AUDIO 12
111 #define DEMUXER_CTRL_RESYNC 13
112 #define DEMUXER_CTRL_SWITCH_VIDEO 14
113 #define DEMUXER_CTRL_IDENTIFY_PROGRAM 15
114 #define DEMUXER_CTRL_CORRECT_PTS 16
116 #define SEEK_ABSOLUTE (1 << 0)
117 #define SEEK_FACTOR (1 << 1)
118 #define SEEK_FORWARD (1 << 2)
119 #define SEEK_BACKWARD (1 << 3)
121 #define MP_INPUT_BUFFER_PADDING_SIZE 64
123 // Holds one packet/frame/whatever
124 typedef struct demux_packet
{
129 off_t pos
; // position in index (AVI) or file (MPG)
130 unsigned char* buffer
;
131 int flags
; // keyframe, etc
132 int refcount
; //refcounter for the master packet, if 0, buffer can be free()d
133 struct demux_packet
*master
; //pointer to the master packet if this one is a cloned one
134 struct demux_packet
*next
;
137 typedef struct demux_stream
{
138 int buffer_pos
; // current buffer position
139 int buffer_size
; // current buffer size
140 unsigned char* buffer
; // current buffer, never free() it, always use free_demux_packet(buffer_ref);
141 double pts
; // current buffer's pts
142 int pts_bytes
; // number of bytes read after last pts stamp
143 int eof
; // end of demuxed stream? (true if all buffer empty)
144 off_t pos
; // position in the input stream (file)
145 off_t dpos
; // position in the demuxed stream
146 int pack_no
; // serial number of packet
147 int flags
; // flags of current packet (keyframe etc)
148 int non_interleaved
; // 1 if this stream is not properly interleaved,
149 // so e.g. subtitle handling must do explicit reads.
151 int packs
; // number of packets in buffer
152 int bytes
; // total bytes of packets in buffer
153 demux_packet_t
*first
; // read to current buffer from here
154 demux_packet_t
*last
; // append new packets from input stream to here
155 demux_packet_t
*current
;// needed for refcounting of the buffer
156 int id
; // stream ID (for multiple audio/video streams)
157 struct demuxer
*demuxer
; // parent demuxer structure (stream handler)
159 demux_packet_t
*asf_packet
; // read asf fragments here
162 unsigned int ss_mul
,ss_div
;
163 // ---- stream header ----
167 typedef struct demuxer_info
{
175 #define MAX_A_STREAMS 256
176 #define MAX_V_STREAMS 256
177 #define MAX_S_STREAMS 256
182 * Demuxer description structure
184 typedef struct demuxer_desc
{
185 const char *info
; ///< What is it (long name and/or description)
186 const char *name
; ///< Demuxer name, used with -demuxer switch
187 const char *shortdesc
; ///< Description printed at demuxer detection
188 const char *author
; ///< Demuxer author(s)
189 const char *comment
; ///< Comment, printed with -demuxer help
191 int type
; ///< DEMUXER_TYPE_xxx
192 int safe_check
; ///< If 1 detection is safe and fast, do it before file extension check
194 /// Check if can demux the file, return DEMUXER_TYPE_xxx on success
195 int (*check_file
)(struct demuxer
*demuxer
); ///< Mandatory if safe_check == 1, else optional
196 /// Get packets from file, return 0 on eof
197 int (*fill_buffer
)(struct demuxer
*demuxer
, demux_stream_t
*ds
); ///< Mandatory
198 /// Open the demuxer, return demuxer on success, NULL on failure
199 struct demuxer
* (*open
)(struct demuxer
*demuxer
); ///< Optional
200 /// Close the demuxer
201 void (*close
)(struct demuxer
*demuxer
); ///< Optional
203 void (*seek
)(struct demuxer
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
); ///< Optional
205 int (*control
)(struct demuxer
*demuxer
, int cmd
, void *arg
); ///< Optional
208 typedef struct demux_chapter
214 struct matroska_data
{
215 unsigned char segment_uid
[16];
216 // Ordered chapter information if any
217 struct matroska_chapter
{
220 bool has_segment_uid
;
221 unsigned char segment_uid
[16];
224 int num_ordered_chapters
;
227 typedef struct demux_attachment
232 unsigned int data_size
;
233 } demux_attachment_t
;
235 typedef struct demuxer
{
236 const demuxer_desc_t
*desc
; ///< Demuxer description structure
237 off_t filepos
; // input stream current pos.
241 double stream_pts
; // current stream pts, if applicable (e.g. dvd)
242 double reference_clock
;
243 char *filename
; ///< Needed by avs_check_file
244 int synced
; // stream synced (used by mpeg)
245 int type
; // demuxer type: mpeg PS, mpeg ES, avi, avi-ni, avi-nini, asf
246 int file_format
; // file format: mpeg/avi/asf
247 int seekable
; // flag
248 /* Set if using absolute seeks for small movements is OK (no pts resets
249 * that would make pts ambigious, preferably supports back/forward flags */
252 demux_stream_t
*audio
; // audio buffer/demuxer
253 demux_stream_t
*video
; // video buffer/demuxer
254 demux_stream_t
*sub
; // dvd subtitle buffer/demuxer
257 struct sh_audio
*a_streams
[MAX_A_STREAMS
];
258 struct sh_video
*v_streams
[MAX_V_STREAMS
];
259 struct sh_sub
*s_streams
[MAX_S_STREAMS
];
261 // pointer to teletext decoder private data, if demuxer stream contains teletext
264 demux_chapter_t
* chapters
;
267 demux_attachment_t
* attachments
;
270 struct matroska_data matroska_data
;
272 void* priv
; // fileformat-dependent data
278 int progid
; //program id
279 int aid
, vid
, sid
; //audio, video and subtitle id
282 static inline demux_packet_t
* new_demux_packet(int len
){
283 demux_packet_t
* dp
=(demux_packet_t
*)malloc(sizeof(demux_packet_t
));
286 dp
->pts
=MP_NOPTS_VALUE
;
287 dp
->endpts
=MP_NOPTS_VALUE
;
288 dp
->stream_pts
= MP_NOPTS_VALUE
;
294 if (len
> 0 && (dp
->buffer
= (unsigned char *)malloc(len
+ MP_INPUT_BUFFER_PADDING_SIZE
)))
295 memset(dp
->buffer
+ len
, 0, 8);
301 static inline void resize_demux_packet(demux_packet_t
* dp
, int len
)
305 dp
->buffer
=(unsigned char *)realloc(dp
->buffer
,len
+8);
309 if(dp
->buffer
) free(dp
->buffer
);
314 memset(dp
->buffer
+ len
, 0, 8);
319 static inline demux_packet_t
* clone_demux_packet(demux_packet_t
* pack
){
320 demux_packet_t
* dp
=(demux_packet_t
*)malloc(sizeof(demux_packet_t
));
321 while(pack
->master
) pack
=pack
->master
; // find the master
322 memcpy(dp
,pack
,sizeof(demux_packet_t
));
330 static inline void free_demux_packet(demux_packet_t
* dp
){
331 if (dp
->master
==NULL
){ //dp is a master packet
333 if (dp
->refcount
==0){
334 if (dp
->buffer
) free(dp
->buffer
);
340 free_demux_packet(dp
->master
);
345 #define SIZE_MAX ((size_t)-1)
348 static inline void *realloc_struct(void *ptr
, size_t nmemb
, size_t size
) {
349 if (nmemb
> SIZE_MAX
/ size
) {
353 return realloc(ptr
, nmemb
* size
);
356 demux_stream_t
* new_demuxer_stream(struct demuxer
*demuxer
,int id
);
357 demuxer_t
* new_demuxer(struct MPOpts
*opts
, stream_t
*stream
,int type
,int a_id
,int v_id
,int s_id
,char *filename
);
358 void free_demuxer_stream(demux_stream_t
*ds
);
359 void free_demuxer(demuxer_t
*demuxer
);
361 void ds_add_packet(demux_stream_t
*ds
,demux_packet_t
* dp
);
362 void ds_read_packet(demux_stream_t
*ds
, stream_t
*stream
, int len
, double pts
, off_t pos
, int flags
);
364 int demux_fill_buffer(demuxer_t
*demux
,demux_stream_t
*ds
);
365 int ds_fill_buffer(demux_stream_t
*ds
);
367 static inline off_t
ds_tell(demux_stream_t
*ds
){
368 return (ds
->dpos
-ds
->buffer_size
)+ds
->buffer_pos
;
371 static inline int ds_tell_pts(demux_stream_t
*ds
){
372 return (ds
->pts_bytes
-ds
->buffer_size
)+ds
->buffer_pos
;
375 int demux_read_data(demux_stream_t
*ds
,unsigned char* mem
,int len
);
376 int demux_pattern_3(demux_stream_t
*ds
, unsigned char *mem
, int maxlen
,
377 int *read
, uint32_t pattern
);
379 #define demux_peekc(ds) (\
380 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos] \
381 :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos] ) )
383 #define demux_getc(ds) (\
384 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos++] \
385 :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos++] ) )
387 static inline int demux_getc(demux_stream_t
*ds
){
388 if(ds
->buffer_pos
>=ds
->buffer_size
){
389 if(!ds_fill_buffer(ds
)){
390 // printf("DEMUX_GETC: EOF reached!\n");
394 // printf("[%02X]",ds->buffer[ds->buffer_pos]);
395 return ds
->buffer
[ds
->buffer_pos
++];
399 void ds_free_packs(demux_stream_t
*ds
);
400 int ds_get_packet(demux_stream_t
*ds
,unsigned char **start
);
401 int ds_get_packet_pts(demux_stream_t
*ds
, unsigned char **start
, double *pts
);
402 int ds_get_packet_sub(demux_stream_t
*ds
,unsigned char **start
);
403 double ds_get_next_pts(demux_stream_t
*ds
);
404 int ds_parse(demux_stream_t
*sh
, uint8_t **buffer
, int *len
, double pts
, off_t pos
);
405 void ds_clear_parser(demux_stream_t
*sh
);
407 // This is defined here because demux_stream_t ins't defined in stream.h
408 stream_t
* new_ds_stream(demux_stream_t
*ds
);
410 static inline int avi_stream_id(unsigned int id
){
414 if(a
>9 || b
>9) return 100; // invalid ID
418 demuxer_t
* demux_open(struct MPOpts
*opts
, stream_t
*stream
,int file_format
,int aid
,int vid
,int sid
,char* filename
);
419 void demux_flush(demuxer_t
*demuxer
);
420 int demux_seek(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
);
421 demuxer_t
* new_demuxers_demuxer(demuxer_t
* vd
, demuxer_t
* ad
, demuxer_t
* sd
);
423 // AVI demuxer params:
424 extern int index_mode
; // -1=untouched 0=don't use index 1=use (geneate) index
425 extern char *index_file_save
, *index_file_load
;
427 extern int pts_from_bps
;
429 extern int extension_parsing
;
431 int demux_info_add(demuxer_t
*demuxer
, const char *opt
, const char *param
);
432 int demux_info_add_bstr(demuxer_t
*demuxer
, struct bstr opt
, struct bstr param
);
433 char* demux_info_get(demuxer_t
*demuxer
, const char *opt
);
434 int demux_info_print(demuxer_t
*demuxer
);
435 int demux_control(demuxer_t
*demuxer
, int cmd
, void *arg
);
437 int demuxer_get_current_time(demuxer_t
*demuxer
);
438 double demuxer_get_time_length(demuxer_t
*demuxer
);
439 int demuxer_get_percent_pos(demuxer_t
*demuxer
);
440 int demuxer_switch_audio(demuxer_t
*demuxer
, int index
);
441 int demuxer_switch_video(demuxer_t
*demuxer
, int index
);
443 int demuxer_type_by_filename(char* filename
);
445 void demuxer_help(void);
446 int get_demuxer_type_from_name(char *demuxer_name
, int *force
);
448 int demuxer_add_attachment(demuxer_t
*demuxer
, struct bstr name
,
449 struct bstr type
, struct bstr data
);
450 int demuxer_add_chapter(demuxer_t
*demuxer
, struct bstr name
,
451 uint64_t start
, uint64_t end
);
452 int demuxer_seek_chapter(demuxer_t
*demuxer
, int chapter
, double *seek_pts
,
453 char **chapter_name
);
455 /// Get current chapter index if available.
456 int demuxer_get_current_chapter(demuxer_t
*demuxer
);
457 /// Get chapter name by index if available.
458 char *demuxer_chapter_name(demuxer_t
*demuxer
, int chapter
);
459 /// Get chapter display name by index.
460 char *demuxer_chapter_display_name(demuxer_t
*demuxer
, int chapter
);
461 /// Get chapter start time and end time by index if available.
462 float demuxer_chapter_time(demuxer_t
*demuxer
, int chapter
, float *end
);
463 /// Get total chapter number.
464 int demuxer_chapter_count(demuxer_t
*demuxer
);
465 /// Get current angle index.
466 int demuxer_get_current_angle(demuxer_t
*demuxer
);
468 int demuxer_set_angle(demuxer_t
*demuxer
, int angle
);
469 /// Get number of angles.
470 int demuxer_angles_count(demuxer_t
*demuxer
);
472 // get the index of a track
473 // lang is a comma-separated list
474 int demuxer_audio_track_by_lang(demuxer_t
* demuxer
, char* lang
);
475 int demuxer_sub_track_by_lang(demuxer_t
* demuxer
, char* lang
);
477 // find the default track
478 // for subtitles, it is the first track with default attribute
479 // for audio, additionally, the first track is selected if no track has default attribute set
480 int demuxer_default_audio_track(demuxer_t
* d
);
481 int demuxer_default_sub_track(demuxer_t
* d
);
483 #endif /* MPLAYER_DEMUXER_H */