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"
34 #ifdef HAVE_BUILTIN_EXPECT
35 #define likely(x) __builtin_expect((x) != 0, 1)
36 #define unlikely(x) __builtin_expect((x) != 0, 0)
39 #define unlikely(x) (x)
42 #define MAX_PACKS 4096
43 #define MAX_PACK_BYTES 0x8000000 // 128 MiB
46 DEMUXER_TYPE_UNKNOWN
= 0,
51 DEMUXER_TYPE_AVI_NINI
,
64 DEMUXER_TYPE_RAWAUDIO
,
70 DEMUXER_TYPE_RAWVIDEO
,
71 DEMUXER_TYPE_MPEG4_ES
,
75 DEMUXER_TYPE_MATROSKA
,
76 DEMUXER_TYPE_REALAUDIO
,
85 DEMUXER_TYPE_MPEG_PES
,
86 DEMUXER_TYPE_MPEG_GXF
,
88 DEMUXER_TYPE_LAVF_PREFERRED
,
89 DEMUXER_TYPE_RTP_NEMESI
,
93 /* Values after this are for internal use and can not be selected
94 * as demuxer type by the user (-demuxer option). */
97 DEMUXER_TYPE_DEMUXERS
,
98 DEMUXER_TYPE_PLAYLIST
,
101 enum timestamp_type
{
107 // DEMUXER control commands/answers
108 #define DEMUXER_CTRL_NOTIMPL -1
109 #define DEMUXER_CTRL_DONTKNOW 0
110 #define DEMUXER_CTRL_OK 1
111 #define DEMUXER_CTRL_GUESS 2
112 #define DEMUXER_CTRL_GET_TIME_LENGTH 10
113 #define DEMUXER_CTRL_GET_PERCENT_POS 11
114 #define DEMUXER_CTRL_SWITCH_AUDIO 12
115 #define DEMUXER_CTRL_RESYNC 13
116 #define DEMUXER_CTRL_SWITCH_VIDEO 14
117 #define DEMUXER_CTRL_IDENTIFY_PROGRAM 15
118 #define DEMUXER_CTRL_CORRECT_PTS 16
120 #define SEEK_ABSOLUTE (1 << 0)
121 #define SEEK_FACTOR (1 << 1)
122 #define SEEK_FORWARD (1 << 2)
123 #define SEEK_BACKWARD (1 << 3)
125 #define MP_INPUT_BUFFER_PADDING_SIZE 64
127 // Holds one packet/frame/whatever
128 typedef struct demux_packet
{
133 off_t pos
; // position in index (AVI) or file (MPG)
134 unsigned char *buffer
;
135 int flags
; // keyframe, etc
136 int refcount
; // counter for the master packet, if 0, buffer can be free()d
137 struct demux_packet
*master
; //in clones, pointer to the master packet
138 struct demux_packet
*next
;
141 typedef struct demux_stream
{
142 int buffer_pos
; // current buffer position
143 int buffer_size
; // current buffer size
144 unsigned char *buffer
; // current buffer, never free() it, always use free_demux_packet(buffer_ref);
145 double pts
; // current buffer's pts
146 int pts_bytes
; // number of bytes read after last pts stamp
147 int eof
; // end of demuxed stream? (true if all buffer empty)
148 off_t pos
; // position in the input stream (file)
149 off_t dpos
; // position in the demuxed stream
150 int pack_no
; // serial number of packet
151 int flags
; // flags of current packet (keyframe etc)
152 int non_interleaved
; // 1 if this stream is not properly interleaved,
153 // so e.g. subtitle handling must do explicit reads.
155 int packs
; // number of packets in buffer
156 int bytes
; // total bytes of packets in buffer
157 demux_packet_t
*first
; // read to current buffer from here
158 demux_packet_t
*last
; // append new packets from input stream to here
159 demux_packet_t
*current
; // needed for refcounting of the buffer
160 int id
; // stream ID (for multiple audio/video streams)
161 struct demuxer
*demuxer
; // parent demuxer structure (stream handler)
163 struct demux_packet
*asf_packet
; // read asf fragments here
166 unsigned int ss_mul
, ss_div
;
167 // ---- stream header ----
171 typedef struct demuxer_info
{
179 #define MAX_A_STREAMS 256
180 #define MAX_V_STREAMS 256
181 #define MAX_S_STREAMS 256
186 * Demuxer description structure
188 typedef struct demuxer_desc
{
189 const char *info
; // What is it (long name and/or description)
190 const char *name
; // Demuxer name, used with -demuxer switch
191 const char *shortdesc
; // Description printed at demuxer detection
192 const char *author
; // Demuxer author(s)
193 const char *comment
; // Comment, printed with -demuxer help
195 enum demuxer_type type
;
196 // If 1 detection is safe and fast, do it before file extension check
199 // Check if can demux the file, return DEMUXER_TYPE_xxx on success
200 // Mandatory if safe_check == 1, else optional
201 int (*check_file
)(struct demuxer
*demuxer
);
202 /// Get packets from file, return 0 on eof. Mandatory
203 int (*fill_buffer
)(struct demuxer
*demuxer
, struct demux_stream
*ds
);
204 /// Open the demuxer, return demuxer on success, NULL on failure
205 struct demuxer
*(*open
)(struct demuxer
*demuxer
); // Optional
206 /// Close the demuxer
207 void (*close
)(struct demuxer
*demuxer
); // Optional
209 void (*seek
)(struct demuxer
*demuxer
, float rel_seek_secs
,
210 float audio_delay
, int flags
);
211 // Various control functions. Optional
212 int (*control
)(struct demuxer
*demuxer
, int cmd
, void *arg
);
215 typedef struct demux_chapter
221 struct matroska_data
{
222 unsigned char segment_uid
[16];
223 // Ordered chapter information if any
224 struct matroska_chapter
{
227 bool has_segment_uid
;
228 unsigned char segment_uid
[16];
231 int num_ordered_chapters
;
234 typedef struct demux_attachment
239 unsigned int data_size
;
240 } demux_attachment_t
;
242 typedef struct demuxer
{
243 const demuxer_desc_t
*desc
; ///< Demuxer description structure
244 char *filetype
; // format name when not identified by demuxer (libavformat)
245 off_t filepos
; // input stream current pos.
248 struct stream
*stream
;
249 double stream_pts
; // current stream pts, if applicable (e.g. dvd)
250 double reference_clock
;
251 char *filename
; // Needed by avs_check_file
252 int synced
; // stream synced (used by mpeg)
253 enum demuxer_type type
;
254 /* Normally the file_format field is just a copy of the type field above.
255 * There are 2 exceptions I noticed. Internal demux_avi may force
256 * ->type to DEMUXER_TYPE_AVI_[NI|NINI] while leaving ->file_format at
257 * DEMUXER_TYPE_AVI. Internal demux_mov may set ->type to
258 * DEMUXER_TYPE_PLAYLIST and also return that from the check function
259 * or not (looks potentially buggy). */
260 enum demuxer_type file_format
;
261 int seekable
; // flag
262 /* Set if using absolute seeks for small movements is OK (no pts resets
263 * that would make pts ambigious, preferably supports back/forward flags */
265 enum timestamp_type timestamp_type
;
267 struct demux_stream
*audio
; // audio buffer/demuxer
268 struct demux_stream
*video
; // video buffer/demuxer
269 struct demux_stream
*sub
; // dvd subtitle buffer/demuxer
272 struct sh_audio
*a_streams
[MAX_A_STREAMS
];
273 struct sh_video
*v_streams
[MAX_V_STREAMS
];
274 struct sh_sub
*s_streams
[MAX_S_STREAMS
];
276 // teletext decoder private data, if demuxer stream contains teletext
279 struct demux_chapter
*chapters
;
282 struct demux_attachment
*attachments
;
285 struct matroska_data matroska_data
;
286 // for trivial demuxers which just read the whole file for codec to use
287 struct bstr file_contents
;
289 void *priv
; // demuxer-specific internal data
290 char **info
; // metadata
295 int progid
; //program id
296 int aid
, vid
, sid
; //audio, video and subtitle id
299 struct demux_packet
*new_demux_packet(size_t len
);
300 void resize_demux_packet(struct demux_packet
*dp
, size_t len
);
301 struct demux_packet
*clone_demux_packet(struct demux_packet
*pack
);
302 void free_demux_packet(struct demux_packet
*dp
);
305 #define SIZE_MAX ((size_t)-1)
308 static inline void *realloc_struct(void *ptr
, size_t nmemb
, size_t size
)
310 if (nmemb
> SIZE_MAX
/ size
) {
314 return realloc(ptr
, nmemb
* size
);
317 struct demuxer
*new_demuxer(struct MPOpts
*opts
, struct stream
*stream
,
318 int type
, int a_id
, int v_id
, int s_id
,
320 void free_demuxer(struct demuxer
*demuxer
);
322 void ds_add_packet(struct demux_stream
*ds
, struct demux_packet
*dp
);
323 void ds_read_packet(struct demux_stream
*ds
, struct stream
*stream
, int len
,
324 double pts
, off_t pos
, int flags
);
326 int demux_fill_buffer(struct demuxer
*demux
, struct demux_stream
*ds
);
327 int ds_fill_buffer(struct demux_stream
*ds
);
329 static inline off_t
ds_tell(struct demux_stream
*ds
)
331 return (ds
->dpos
- ds
->buffer_size
) + ds
->buffer_pos
;
334 static inline int ds_tell_pts(struct demux_stream
*ds
)
336 return (ds
->pts_bytes
- ds
->buffer_size
) + ds
->buffer_pos
;
339 int demux_read_data(struct demux_stream
*ds
, unsigned char *mem
, int len
);
340 int demux_pattern_3(struct demux_stream
*ds
, unsigned char *mem
, int maxlen
,
341 int *read
, uint32_t pattern
);
343 #define demux_peekc(ds) ( \
344 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos] \
345 : ((unlikely(!ds_fill_buffer(ds))) ? (-1) : ds->buffer[ds->buffer_pos]))
346 #define demux_getc(ds) ( \
347 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos++] \
348 : ((unlikely(!ds_fill_buffer(ds))) ? (-1) : ds->buffer[ds->buffer_pos++]))
350 void ds_free_packs(struct demux_stream
*ds
);
351 int ds_get_packet(struct demux_stream
*ds
, unsigned char **start
);
352 int ds_get_packet_pts(struct demux_stream
*ds
, unsigned char **start
,
354 int ds_get_packet_sub(struct demux_stream
*ds
, unsigned char **start
);
355 double ds_get_next_pts(struct demux_stream
*ds
);
356 int ds_parse(struct demux_stream
*sh
, uint8_t **buffer
, int *len
, double pts
,
358 void ds_clear_parser(struct demux_stream
*sh
);
360 static inline int avi_stream_id(unsigned int id
)
366 return 100; // invalid ID
370 struct demuxer
*demux_open(struct MPOpts
*opts
, struct stream
*stream
,
371 int file_format
, int aid
, int vid
, int sid
,
373 void demux_flush(struct demuxer
*demuxer
);
374 int demux_seek(struct demuxer
*demuxer
, float rel_seek_secs
, float audio_delay
,
376 struct demuxer
*new_demuxers_demuxer(struct demuxer
*vd
, struct demuxer
*ad
,
379 // AVI demuxer params:
380 extern int index_mode
; // -1=untouched 0=don't use index 1=use (geneate) index
381 extern char *index_file_save
, *index_file_load
;
383 extern int pts_from_bps
;
385 int demux_info_add(struct demuxer
*demuxer
, const char *opt
, const char *param
);
386 int demux_info_add_bstr(struct demuxer
*demuxer
, struct bstr opt
,
388 char *demux_info_get(struct demuxer
*demuxer
, const char *opt
);
389 int demux_info_print(struct demuxer
*demuxer
);
390 int demux_control(struct demuxer
*demuxer
, int cmd
, void *arg
);
392 int demuxer_switch_audio(struct demuxer
*demuxer
, int index
);
393 int demuxer_switch_video(struct demuxer
*demuxer
, int index
);
395 int demuxer_type_by_filename(char *filename
);
397 void demuxer_help(void);
399 int demuxer_add_attachment(struct demuxer
*demuxer
, struct bstr name
,
400 struct bstr type
, struct bstr data
);
401 int demuxer_add_chapter(struct demuxer
*demuxer
, struct bstr name
,
402 uint64_t start
, uint64_t end
);
403 int demuxer_seek_chapter(struct demuxer
*demuxer
, int chapter
, double *seek_pts
,
404 char **chapter_name
);
406 /// Get current chapter index if available.
407 int demuxer_get_current_chapter(struct demuxer
*demuxer
, double time_now
);
408 /// Get chapter name by index if available.
409 char *demuxer_chapter_name(struct demuxer
*demuxer
, int chapter
);
410 /// Get chapter display name by index.
411 char *demuxer_chapter_display_name(struct demuxer
*demuxer
, int chapter
);
412 /// Get chapter start time and end time by index if available.
413 float demuxer_chapter_time(struct demuxer
*demuxer
, int chapter
, float *end
);
414 /// Get total chapter number.
415 int demuxer_chapter_count(struct demuxer
*demuxer
);
416 /// Get current angle index.
417 int demuxer_get_current_angle(struct demuxer
*demuxer
);
419 int demuxer_set_angle(struct demuxer
*demuxer
, int angle
);
420 /// Get number of angles.
421 int demuxer_angles_count(struct demuxer
*demuxer
);
423 /* Get the index of a track.
424 * lang is a string list, NULL is same as empty list
425 * Sort tracks based on the following criteria:
426 * 1) earlier match in lang list, or last no match
427 * 2) track is marked default (default wins)
428 * 3) track number (lower wins)
429 * For audio, select best track according to these criteria; only return -1
430 * if there are no tracks at all.
431 * For subs, select best track according to the same criteria, but return -1
432 * if all tracks are no-lang-match, not-default.
434 int demuxer_audio_track_by_lang_and_default(struct demuxer
*d
, char **langt
);
435 int demuxer_sub_track_by_lang_and_default(struct demuxer
*d
, char **langt
);
437 #endif /* MPLAYER_DEMUXER_H */