demux: take chapter/attachment name strings without 0-termination
[mplayer/glamo.git] / libmpdemux / demuxer.h
blob375c0d0ccf08f405e0103181b5e6ff3d99bf19be
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"
30 struct MPOpts;
32 #ifdef HAVE_BUILTIN_EXPECT
33 #define likely(x) __builtin_expect ((x) != 0, 1)
34 #define unlikely(x) __builtin_expect ((x) != 0, 0)
35 #else
36 #define likely(x) (x)
37 #define unlikely(x) (x)
38 #endif
40 #define MAX_PACKS 4096
41 #define MAX_PACK_BYTES 0x8000000 // 128 MiB
43 #define DEMUXER_TYPE_UNKNOWN 0
44 #define DEMUXER_TYPE_MPEG_ES 1
45 #define DEMUXER_TYPE_MPEG_PS 2
46 #define DEMUXER_TYPE_AVI 3
47 #define DEMUXER_TYPE_AVI_NI 4
48 #define DEMUXER_TYPE_AVI_NINI 5
49 #define DEMUXER_TYPE_ASF 6
50 #define DEMUXER_TYPE_MOV 7
51 #define DEMUXER_TYPE_VIVO 8
52 #define DEMUXER_TYPE_TV 9
53 #define DEMUXER_TYPE_FLI 10
54 #define DEMUXER_TYPE_REAL 11
55 #define DEMUXER_TYPE_Y4M 12
56 #define DEMUXER_TYPE_FILM 14
57 #define DEMUXER_TYPE_ROQ 15
58 #define DEMUXER_TYPE_MF 16
59 #define DEMUXER_TYPE_AUDIO 17
60 #define DEMUXER_TYPE_OGG 18
61 #define DEMUXER_TYPE_RAWAUDIO 20
62 #define DEMUXER_TYPE_RTP 21
63 #define DEMUXER_TYPE_RAWDV 22
64 #define DEMUXER_TYPE_PVA 23
65 #define DEMUXER_TYPE_SMJPEG 24
66 #define DEMUXER_TYPE_XMMS 25
67 #define DEMUXER_TYPE_RAWVIDEO 26
68 #define DEMUXER_TYPE_MPEG4_ES 27
69 #define DEMUXER_TYPE_GIF 28
70 #define DEMUXER_TYPE_MPEG_TS 29
71 #define DEMUXER_TYPE_H264_ES 30
72 #define DEMUXER_TYPE_MATROSKA 31
73 #define DEMUXER_TYPE_REALAUDIO 32
74 #define DEMUXER_TYPE_MPEG_TY 33
75 #define DEMUXER_TYPE_LMLM4 34
76 #define DEMUXER_TYPE_LAVF 35
77 #define DEMUXER_TYPE_NSV 36
78 #define DEMUXER_TYPE_VQF 37
79 #define DEMUXER_TYPE_AVS 38
80 #define DEMUXER_TYPE_AAC 39
81 #define DEMUXER_TYPE_MPC 40
82 #define DEMUXER_TYPE_MPEG_PES 41
83 #define DEMUXER_TYPE_MPEG_GXF 42
84 #define DEMUXER_TYPE_NUT 43
85 #define DEMUXER_TYPE_LAVF_PREFERRED 44
86 #define DEMUXER_TYPE_RTP_NEMESI 45
87 #define DEMUXER_TYPE_MNG 46
89 // This should always match the higest demuxer type number.
90 // Unless you want to disallow users to force the demuxer to some types
91 #define DEMUXER_TYPE_MIN 0
92 #define DEMUXER_TYPE_MAX 46
94 #define DEMUXER_TYPE_DEMUXERS (1<<16)
95 // A virtual demuxer type for the network code
96 #define DEMUXER_TYPE_PLAYLIST (2<<16)
99 #define MP_NOPTS_VALUE (-1LL<<63) //both int64_t and double should be able to represent this exactly
102 // DEMUXER control commands/answers
103 #define DEMUXER_CTRL_NOTIMPL -1
104 #define DEMUXER_CTRL_DONTKNOW 0
105 #define DEMUXER_CTRL_OK 1
106 #define DEMUXER_CTRL_GUESS 2
107 #define DEMUXER_CTRL_GET_TIME_LENGTH 10
108 #define DEMUXER_CTRL_GET_PERCENT_POS 11
109 #define DEMUXER_CTRL_SWITCH_AUDIO 12
110 #define DEMUXER_CTRL_RESYNC 13
111 #define DEMUXER_CTRL_SWITCH_VIDEO 14
112 #define DEMUXER_CTRL_IDENTIFY_PROGRAM 15
113 #define DEMUXER_CTRL_CORRECT_PTS 16
115 #define SEEK_ABSOLUTE (1 << 0)
116 #define SEEK_FACTOR (1 << 1)
117 #define SEEK_FORWARD (1 << 2)
118 #define SEEK_BACKWARD (1 << 3)
120 #define MP_INPUT_BUFFER_PADDING_SIZE 8
122 // Holds one packet/frame/whatever
123 typedef struct demux_packet {
124 int len;
125 double pts;
126 double endpts;
127 double stream_pts;
128 off_t pos; // position in index (AVI) or file (MPG)
129 unsigned char* buffer;
130 int flags; // keyframe, etc
131 int refcount; //refcounter for the master packet, if 0, buffer can be free()d
132 struct demux_packet *master; //pointer to the master packet if this one is a cloned one
133 struct demux_packet *next;
134 } demux_packet_t;
136 typedef struct demux_stream {
137 int buffer_pos; // current buffer position
138 int buffer_size; // current buffer size
139 unsigned char* buffer; // current buffer, never free() it, always use free_demux_packet(buffer_ref);
140 double pts; // current buffer's pts
141 int pts_bytes; // number of bytes read after last pts stamp
142 int eof; // end of demuxed stream? (true if all buffer empty)
143 off_t pos; // position in the input stream (file)
144 off_t dpos; // position in the demuxed stream
145 int pack_no; // serial number of packet
146 int flags; // flags of current packet (keyframe etc)
147 int non_interleaved; // 1 if this stream is not properly interleaved,
148 // so e.g. subtitle handling must do explicit reads.
149 //---------------
150 int packs; // number of packets in buffer
151 int bytes; // total bytes of packets in buffer
152 demux_packet_t *first; // read to current buffer from here
153 demux_packet_t *last; // append new packets from input stream to here
154 demux_packet_t *current;// needed for refcounting of the buffer
155 int id; // stream ID (for multiple audio/video streams)
156 struct demuxer *demuxer; // parent demuxer structure (stream handler)
157 // ---- asf -----
158 demux_packet_t *asf_packet; // read asf fragments here
159 int asf_seq;
160 // ---- mov -----
161 unsigned int ss_mul,ss_div;
162 // ---- stream header ----
163 void* sh;
164 } demux_stream_t;
166 typedef struct demuxer_info {
167 char *name;
168 char *author;
169 char *encoder;
170 char *comments;
171 char *copyright;
172 } demuxer_info_t;
174 #define MAX_A_STREAMS 256
175 #define MAX_V_STREAMS 256
176 #define MAX_S_STREAMS 256
178 struct demuxer;
181 * Demuxer description structure
183 typedef struct demuxer_desc {
184 const char *info; ///< What is it (long name and/or description)
185 const char *name; ///< Demuxer name, used with -demuxer switch
186 const char *shortdesc; ///< Description printed at demuxer detection
187 const char *author; ///< Demuxer author(s)
188 const char *comment; ///< Comment, printed with -demuxer help
190 int type; ///< DEMUXER_TYPE_xxx
191 int safe_check; ///< If 1 detection is safe and fast, do it before file extension check
193 /// Check if can demux the file, return DEMUXER_TYPE_xxx on success
194 int (*check_file)(struct demuxer *demuxer); ///< Mandatory if safe_check == 1, else optional
195 /// Get packets from file, return 0 on eof
196 int (*fill_buffer)(struct demuxer *demuxer, demux_stream_t *ds); ///< Mandatory
197 /// Open the demuxer, return demuxer on success, NULL on failure
198 struct demuxer* (*open)(struct demuxer *demuxer); ///< Optional
199 /// Close the demuxer
200 void (*close)(struct demuxer *demuxer); ///< Optional
201 // Seek
202 void (*seek)(struct demuxer *demuxer, float rel_seek_secs, float audio_delay, int flags); ///< Optional
203 // Control
204 int (*control)(struct demuxer *demuxer, int cmd, void *arg); ///< Optional
205 } demuxer_desc_t;
207 typedef struct demux_chapter
209 uint64_t start, end;
210 char* name;
211 } demux_chapter_t;
213 struct matroska_data {
214 unsigned char segment_uid[16];
215 // Ordered chapter information if any
216 struct matroska_chapter {
217 uint64_t start;
218 uint64_t end;
219 bool has_segment_uid;
220 unsigned char segment_uid[16];
221 char *name;
222 } *ordered_chapters;
223 int num_ordered_chapters;
226 typedef struct demux_attachment
228 char* name;
229 char* type;
230 void* data;
231 unsigned int data_size;
232 } demux_attachment_t;
234 typedef struct demuxer {
235 const demuxer_desc_t *desc; ///< Demuxer description structure
236 off_t filepos; // input stream current pos.
237 off_t movi_start;
238 off_t movi_end;
239 stream_t *stream;
240 double stream_pts; // current stream pts, if applicable (e.g. dvd)
241 double reference_clock;
242 char *filename; ///< Needed by avs_check_file
243 int synced; // stream synced (used by mpeg)
244 int type; // demuxer type: mpeg PS, mpeg ES, avi, avi-ni, avi-nini, asf
245 int file_format; // file format: mpeg/avi/asf
246 int seekable; // flag
247 /* Set if using absolute seeks for small movements is OK (no pts resets
248 * that would make pts ambigious, preferably supports back/forward flags */
249 bool accurate_seek;
251 demux_stream_t *audio; // audio buffer/demuxer
252 demux_stream_t *video; // video buffer/demuxer
253 demux_stream_t *sub; // dvd subtitle buffer/demuxer
255 // stream headers:
256 struct sh_audio *a_streams[MAX_A_STREAMS];
257 struct sh_video *v_streams[MAX_V_STREAMS];
258 struct sh_sub *s_streams[MAX_S_STREAMS];
260 // pointer to teletext decoder private data, if demuxer stream contains teletext
261 void *teletext;
263 demux_chapter_t* chapters;
264 int num_chapters;
266 demux_attachment_t* attachments;
267 int num_attachments;
269 struct matroska_data matroska_data;
271 void* priv; // fileformat-dependent data
272 char** info;
273 struct MPOpts *opts;
274 } demuxer_t;
276 typedef struct {
277 int progid; //program id
278 int aid, vid, sid; //audio, video and subtitle id
279 } demux_program_t;
281 static inline demux_packet_t* new_demux_packet(int len){
282 demux_packet_t* dp=(demux_packet_t*)malloc(sizeof(demux_packet_t));
283 dp->len=len;
284 dp->next=NULL;
285 dp->pts=MP_NOPTS_VALUE;
286 dp->endpts=MP_NOPTS_VALUE;
287 dp->stream_pts = MP_NOPTS_VALUE;
288 dp->pos=0;
289 dp->flags=0;
290 dp->refcount=1;
291 dp->master=NULL;
292 dp->buffer=NULL;
293 if (len > 0 && (dp->buffer = (unsigned char *)malloc(len + MP_INPUT_BUFFER_PADDING_SIZE)))
294 memset(dp->buffer + len, 0, 8);
295 else
296 dp->len = 0;
297 return dp;
300 static inline void resize_demux_packet(demux_packet_t* dp, int len)
302 if(len > 0)
304 dp->buffer=(unsigned char *)realloc(dp->buffer,len+8);
306 else
308 if(dp->buffer) free(dp->buffer);
309 dp->buffer=NULL;
311 dp->len=len;
312 if (dp->buffer)
313 memset(dp->buffer + len, 0, 8);
314 else
315 dp->len = 0;
318 static inline demux_packet_t* clone_demux_packet(demux_packet_t* pack){
319 demux_packet_t* dp=(demux_packet_t*)malloc(sizeof(demux_packet_t));
320 while(pack->master) pack=pack->master; // find the master
321 memcpy(dp,pack,sizeof(demux_packet_t));
322 dp->next=NULL;
323 dp->refcount=0;
324 dp->master=pack;
325 pack->refcount++;
326 return dp;
329 static inline void free_demux_packet(demux_packet_t* dp){
330 if (dp->master==NULL){ //dp is a master packet
331 dp->refcount--;
332 if (dp->refcount==0){
333 if (dp->buffer) free(dp->buffer);
334 free(dp);
336 return;
338 // dp is a clone:
339 free_demux_packet(dp->master);
340 free(dp);
343 #ifndef SIZE_MAX
344 #define SIZE_MAX ((size_t)-1)
345 #endif
347 static inline void *realloc_struct(void *ptr, size_t nmemb, size_t size) {
348 if (nmemb > SIZE_MAX / size) {
349 free(ptr);
350 return NULL;
352 return realloc(ptr, nmemb * size);
355 demux_stream_t* new_demuxer_stream(struct demuxer *demuxer,int id);
356 demuxer_t* new_demuxer(struct MPOpts *opts, stream_t *stream,int type,int a_id,int v_id,int s_id,char *filename);
357 void free_demuxer_stream(demux_stream_t *ds);
358 void free_demuxer(demuxer_t *demuxer);
360 void ds_add_packet(demux_stream_t *ds,demux_packet_t* dp);
361 void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len, double pts, off_t pos, int flags);
363 int demux_fill_buffer(demuxer_t *demux,demux_stream_t *ds);
364 int ds_fill_buffer(demux_stream_t *ds);
366 static inline off_t ds_tell(demux_stream_t *ds){
367 return (ds->dpos-ds->buffer_size)+ds->buffer_pos;
370 static inline int ds_tell_pts(demux_stream_t *ds){
371 return (ds->pts_bytes-ds->buffer_size)+ds->buffer_pos;
374 int demux_read_data(demux_stream_t *ds,unsigned char* mem,int len);
375 int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
376 int *read, uint32_t pattern);
378 #define demux_peekc(ds) (\
379 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos] \
380 :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos] ) )
381 #if 1
382 #define demux_getc(ds) (\
383 (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos++] \
384 :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos++] ) )
385 #else
386 static inline int demux_getc(demux_stream_t *ds){
387 if(ds->buffer_pos>=ds->buffer_size){
388 if(!ds_fill_buffer(ds)){
389 // printf("DEMUX_GETC: EOF reached!\n");
390 return -1; // EOF
393 // printf("[%02X]",ds->buffer[ds->buffer_pos]);
394 return ds->buffer[ds->buffer_pos++];
396 #endif
398 void ds_free_packs(demux_stream_t *ds);
399 int ds_get_packet(demux_stream_t *ds,unsigned char **start);
400 int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts);
401 int ds_get_packet_sub(demux_stream_t *ds,unsigned char **start);
402 double ds_get_next_pts(demux_stream_t *ds);
403 int ds_parse(demux_stream_t *sh, uint8_t **buffer, int *len, double pts, off_t pos);
405 // This is defined here because demux_stream_t ins't defined in stream.h
406 stream_t* new_ds_stream(demux_stream_t *ds);
408 static inline int avi_stream_id(unsigned int id){
409 unsigned char *p=(unsigned char *)&id;
410 unsigned char a,b;
411 #if HAVE_BIGENDIAN
412 a=p[3]-'0'; b=p[2]-'0';
413 #else
414 a=p[0]-'0'; b=p[1]-'0';
415 #endif
416 if(a>9 || b>9) return 100; // invalid ID
417 return a*10+b;
420 demuxer_t* demux_open(struct MPOpts *opts, stream_t *stream,int file_format,int aid,int vid,int sid,char* filename);
421 void demux_flush(demuxer_t *demuxer);
422 int demux_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags);
423 demuxer_t* new_demuxers_demuxer(demuxer_t* vd, demuxer_t* ad, demuxer_t* sd);
425 // AVI demuxer params:
426 extern int index_mode; // -1=untouched 0=don't use index 1=use (geneate) index
427 extern char *index_file_save, *index_file_load;
428 extern int force_ni;
429 extern int pts_from_bps;
431 extern int extension_parsing;
433 int demux_info_add(demuxer_t *demuxer, const char *opt, const char *param);
434 char* demux_info_get(demuxer_t *demuxer, const char *opt);
435 int demux_info_print(demuxer_t *demuxer);
436 int demux_control(demuxer_t *demuxer, int cmd, void *arg);
438 #ifdef CONFIG_OGGVORBIS
439 /* Found in demux_ogg.c */
440 int demux_ogg_num_subs(demuxer_t *demuxer);
441 int demux_ogg_sub_id(demuxer_t *demuxer, int index);
442 #endif
444 int demuxer_get_current_time(demuxer_t *demuxer);
445 double demuxer_get_time_length(demuxer_t *demuxer);
446 int demuxer_get_percent_pos(demuxer_t *demuxer);
447 int demuxer_switch_audio(demuxer_t *demuxer, int index);
448 int demuxer_switch_video(demuxer_t *demuxer, int index);
450 int demuxer_type_by_filename(char* filename);
452 void demuxer_help(void);
453 int get_demuxer_type_from_name(char *demuxer_name, int *force);
455 int demuxer_add_attachment(demuxer_t *demuxer, const char *name,
456 int name_maxlen, const char *type, int type_maxlen,
457 const void *data, size_t size);
458 int demuxer_add_chapter(demuxer_t *demuxer, const char *name, int name_maxlen,
459 uint64_t start, uint64_t end);
460 int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, double *seek_pts,
461 char **chapter_name);
463 /// Get current chapter index if available.
464 int demuxer_get_current_chapter(demuxer_t *demuxer);
465 /// Get chapter name by index if available.
466 char *demuxer_chapter_name(demuxer_t *demuxer, int chapter);
467 /// Get chapter display name by index.
468 char *demuxer_chapter_display_name(demuxer_t *demuxer, int chapter);
469 /// Get chapter start time and end time by index if available.
470 float demuxer_chapter_time(demuxer_t *demuxer, int chapter, float *end);
471 /// Get total chapter number.
472 int demuxer_chapter_count(demuxer_t *demuxer);
473 /// Get current angle index.
474 int demuxer_get_current_angle(demuxer_t *demuxer);
475 /// Set angle.
476 int demuxer_set_angle(demuxer_t *demuxer, int angle);
477 /// Get number of angles.
478 int demuxer_angles_count(demuxer_t *demuxer);
480 // get the index of a track
481 // lang is a comma-separated list
482 int demuxer_audio_track_by_lang(demuxer_t* demuxer, char* lang);
483 int demuxer_sub_track_by_lang(demuxer_t* demuxer, char* lang);
485 // find the default track
486 // for subtitles, it is the first track with default attribute
487 // for audio, additionally, the first track is selected if no track has default attribute set
488 int demuxer_default_audio_track(demuxer_t* d);
489 int demuxer_default_sub_track(demuxer_t* d);
491 #endif /* MPLAYER_DEMUXER_H */