ao_pcm, core: use new API in ao_pcm, change timing with it
[mplayer.git] / libmpdemux / demuxer.h
blobed2d651a8c671cb9b5020fb0ea7c783925df0ef5
1 /*
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>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdbool.h>
28 #include "stream/stream.h"
29 #include "bstr.h"
30 #include "mpcommon.h"
32 struct MPOpts;
34 #ifdef HAVE_BUILTIN_EXPECT
35 #define likely(x) __builtin_expect((x) != 0, 1)
36 #define unlikely(x) __builtin_expect((x) != 0, 0)
37 #else
38 #define likely(x) (x)
39 #define unlikely(x) (x)
40 #endif
42 #define MAX_PACKS 4096
43 #define MAX_PACK_BYTES 0x8000000 // 128 MiB
45 enum demuxer_type {
46 DEMUXER_TYPE_UNKNOWN = 0,
47 DEMUXER_TYPE_MPEG_ES,
48 DEMUXER_TYPE_MPEG_PS,
49 DEMUXER_TYPE_AVI,
50 DEMUXER_TYPE_AVI_NI,
51 DEMUXER_TYPE_AVI_NINI,
52 DEMUXER_TYPE_ASF,
53 DEMUXER_TYPE_MOV,
54 DEMUXER_TYPE_VIVO,
55 DEMUXER_TYPE_TV,
56 DEMUXER_TYPE_FLI,
57 DEMUXER_TYPE_REAL,
58 DEMUXER_TYPE_Y4M,
59 DEMUXER_TYPE_FILM,
60 DEMUXER_TYPE_ROQ,
61 DEMUXER_TYPE_MF,
62 DEMUXER_TYPE_AUDIO,
63 DEMUXER_TYPE_OGG,
64 DEMUXER_TYPE_RAWAUDIO,
65 DEMUXER_TYPE_RTP,
66 DEMUXER_TYPE_RAWDV,
67 DEMUXER_TYPE_PVA,
68 DEMUXER_TYPE_SMJPEG,
69 DEMUXER_TYPE_XMMS,
70 DEMUXER_TYPE_RAWVIDEO,
71 DEMUXER_TYPE_MPEG4_ES,
72 DEMUXER_TYPE_GIF,
73 DEMUXER_TYPE_MPEG_TS,
74 DEMUXER_TYPE_H264_ES,
75 DEMUXER_TYPE_MATROSKA,
76 DEMUXER_TYPE_REALAUDIO,
77 DEMUXER_TYPE_MPEG_TY,
78 DEMUXER_TYPE_LMLM4,
79 DEMUXER_TYPE_LAVF,
80 DEMUXER_TYPE_NSV,
81 DEMUXER_TYPE_VQF,
82 DEMUXER_TYPE_AVS,
83 DEMUXER_TYPE_AAC,
84 DEMUXER_TYPE_MPC,
85 DEMUXER_TYPE_MPEG_PES,
86 DEMUXER_TYPE_MPEG_GXF,
87 DEMUXER_TYPE_NUT,
88 DEMUXER_TYPE_LAVF_PREFERRED,
89 DEMUXER_TYPE_RTP_NEMESI,
90 DEMUXER_TYPE_MNG,
91 DEMUXER_TYPE_EDL,
93 /* Values after this are for internal use and can not be selected
94 * as demuxer type by the user (-demuxer option). */
95 DEMUXER_TYPE_END,
97 DEMUXER_TYPE_DEMUXERS,
98 DEMUXER_TYPE_PLAYLIST,
101 enum timestamp_type {
102 TIMESTAMP_TYPE_PTS,
103 TIMESTAMP_TYPE_SORT,
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 {
129 int len;
130 double pts;
131 double duration;
132 double stream_pts;
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;
139 } demux_packet_t;
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.
154 //---------------
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)
162 // ---- asf -----
163 struct demux_packet *asf_packet; // read asf fragments here
164 int asf_seq;
165 // ---- mov -----
166 unsigned int ss_mul, ss_div;
167 // ---- stream header ----
168 void *sh;
169 } demux_stream_t;
171 typedef struct demuxer_info {
172 char *name;
173 char *author;
174 char *encoder;
175 char *comments;
176 char *copyright;
177 } demuxer_info_t;
179 #define MAX_A_STREAMS 256
180 #define MAX_V_STREAMS 256
181 #define MAX_S_STREAMS 256
183 struct demuxer;
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
197 int safe_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
208 // Seek. 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);
213 } demuxer_desc_t;
215 typedef struct demux_chapter
217 uint64_t start, end;
218 char *name;
219 } demux_chapter_t;
221 struct matroska_data {
222 unsigned char segment_uid[16];
223 // Ordered chapter information if any
224 struct matroska_chapter {
225 uint64_t start;
226 uint64_t end;
227 bool has_segment_uid;
228 unsigned char segment_uid[16];
229 char *name;
230 } *ordered_chapters;
231 int num_ordered_chapters;
234 typedef struct demux_attachment
236 char *name;
237 char *type;
238 void *data;
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.
246 off_t movi_start;
247 off_t movi_end;
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 */
264 bool accurate_seek;
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
271 // stream headers:
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
277 void *teletext;
279 struct demux_chapter *chapters;
280 int num_chapters;
282 struct demux_attachment *attachments;
283 int num_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
291 struct MPOpts *opts;
292 } demuxer_t;
294 typedef struct {
295 int progid; //program id
296 int aid, vid, sid; //audio, video and subtitle id
297 } demux_program_t;
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);
304 #ifndef SIZE_MAX
305 #define SIZE_MAX ((size_t)-1)
306 #endif
308 static inline void *realloc_struct(void *ptr, size_t nmemb, size_t size)
310 if (nmemb > SIZE_MAX / size) {
311 free(ptr);
312 return NULL;
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,
319 char *filename);
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,
353 double *pts);
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,
357 off_t pos);
358 void ds_clear_parser(struct demux_stream *sh);
360 static inline int avi_stream_id(unsigned int id)
362 unsigned char a, b;
363 a = id - '0';
364 b = (id >> 8) - '0';
365 if (a>9 || b>9)
366 return 100; // invalid ID
367 return a * 10 + b;
370 struct demuxer *demux_open(struct MPOpts *opts, struct stream *stream,
371 int file_format, int aid, int vid, int sid,
372 char *filename);
373 void demux_flush(struct demuxer *demuxer);
374 int demux_seek(struct demuxer *demuxer, float rel_seek_secs, float audio_delay,
375 int flags);
376 struct demuxer *new_demuxers_demuxer(struct demuxer *vd, struct demuxer *ad,
377 struct demuxer *sd);
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;
382 extern int force_ni;
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,
387 struct bstr param);
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);
418 /// Set angle.
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 */