1 | #ifndef __DEMUXER_H
|
---|
2 | #define __DEMUXER_H 1
|
---|
3 |
|
---|
4 | #ifndef __cplusplus
|
---|
5 | #define inline __inline
|
---|
6 | #endif
|
---|
7 |
|
---|
8 | #ifdef HAVE_BUILTIN_EXPECT
|
---|
9 | #define likely(x) __builtin_expect ((x) != 0, 1)
|
---|
10 | #define unlikely(x) __builtin_expect ((x) != 0, 0)
|
---|
11 | #else
|
---|
12 | #define likely(x) (x)
|
---|
13 | #define unlikely(x) (x)
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #define MAX_PACKS 4096
|
---|
17 | #ifdef HAVE_TV_BSDBT848
|
---|
18 | #define MAX_PACK_BYTES 0x2000000
|
---|
19 | #else
|
---|
20 | //#define MAX_PACK_BYTES 0x800000
|
---|
21 | #define MAX_PACK_BYTES 0xFFFFFFFF
|
---|
22 | #endif
|
---|
23 | #define DEMUXER_MAX_WAIT 60000L //ms
|
---|
24 |
|
---|
25 | #define HAVE_OGGVORBIS
|
---|
26 | #define USE_LIBAVFORMAT
|
---|
27 |
|
---|
28 |
|
---|
29 | #define DEMUXER_TYPE_UNKNOWN 0
|
---|
30 | #define DEMUXER_TYPE_MPEG_ES 1
|
---|
31 | #define DEMUXER_TYPE_MPEG_PS 2
|
---|
32 | #define DEMUXER_TYPE_AVI 3
|
---|
33 | #define DEMUXER_TYPE_AVI_NI 4
|
---|
34 | #define DEMUXER_TYPE_AVI_NINI 5
|
---|
35 | #define DEMUXER_TYPE_ASF 6
|
---|
36 | #define DEMUXER_TYPE_MOV 7
|
---|
37 | #define DEMUXER_TYPE_VIVO 8
|
---|
38 | #define DEMUXER_TYPE_TV 9
|
---|
39 | #define DEMUXER_TYPE_FLI 10
|
---|
40 | #define DEMUXER_TYPE_REAL 11
|
---|
41 | #define DEMUXER_TYPE_Y4M 12
|
---|
42 | #define DEMUXER_TYPE_NUV 13
|
---|
43 | #define DEMUXER_TYPE_FILM 14
|
---|
44 | #define DEMUXER_TYPE_ROQ 15
|
---|
45 | #define DEMUXER_TYPE_MF 16
|
---|
46 | #define DEMUXER_TYPE_AUDIO 17
|
---|
47 | #define DEMUXER_TYPE_OGG 18
|
---|
48 | #define DEMUXER_TYPE_BMP 19
|
---|
49 | #define DEMUXER_TYPE_RAWAUDIO 20
|
---|
50 | #define DEMUXER_TYPE_RTP 21
|
---|
51 | #define DEMUXER_TYPE_RAWDV 22
|
---|
52 | #define DEMUXER_TYPE_PVA 23
|
---|
53 | #define DEMUXER_TYPE_SMJPEG 24
|
---|
54 | #define DEMUXER_TYPE_XMMS 25
|
---|
55 | #define DEMUXER_TYPE_RAWVIDEO 26
|
---|
56 | #define DEMUXER_TYPE_MPEG4_ES 27
|
---|
57 | #define DEMUXER_TYPE_GIF 28
|
---|
58 | #define DEMUXER_TYPE_MPEG_TS 29
|
---|
59 | #define DEMUXER_TYPE_H264_ES 30
|
---|
60 | #define DEMUXER_TYPE_MATROSKA 31
|
---|
61 | #define DEMUXER_TYPE_REALAUDIO 32
|
---|
62 | #define DEMUXER_TYPE_AC3 33
|
---|
63 | #define DEMUXER_TYPE_MPEG_TY 34
|
---|
64 | #define DEMUXER_TYPE_LMLM4 35
|
---|
65 | #define DEMUXER_TYPE_LAVF 36
|
---|
66 | #define DEMUXER_TYPE_NSV 37
|
---|
67 | #define DEMUXER_TYPE_VQF 38
|
---|
68 | #define DEMUXER_TYPE_MPEG_PES 39
|
---|
69 | #define DEMUXER_TYPE_MPEG_GXF 40
|
---|
70 |
|
---|
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 40
|
---|
75 |
|
---|
76 | #define DEMUXER_TYPE_DEMUXERS (1<<16)
|
---|
77 | // A virtual demuxer type for the network code
|
---|
78 | #define DEMUXER_TYPE_PLAYLIST (2<<16)
|
---|
79 |
|
---|
80 | #define WV_NOPTS_VALUE (-1LL<<63) //both int64_t and double should be able to represent this exactly
|
---|
81 |
|
---|
82 | #define DEMUXER_TIME_NONE 0
|
---|
83 | #define DEMUXER_TIME_PTS 1
|
---|
84 | #define DEMUXER_TIME_FILE 2
|
---|
85 | #define DEMUXER_TIME_BPS 3
|
---|
86 |
|
---|
87 | // DEMUXER control commands/answers
|
---|
88 | #define DEMUXER_CTRL_NOTIMPL -1
|
---|
89 | #define DEMUXER_CTRL_DONTKNOW 0
|
---|
90 | #define DEMUXER_CTRL_OK 1
|
---|
91 | #define DEMUXER_CTRL_GUESS 2
|
---|
92 | #define DEMUXER_CTRL_GET_TIME_LENGTH 10
|
---|
93 | #define DEMUXER_CTRL_GET_PERCENT_POS 11
|
---|
94 | #define DEMUXER_CTRL_SWITCH_AUDIO 12
|
---|
95 | #define DEMUXER_CTRL_RESYNC 13
|
---|
96 | #define DEMUXER_CTRL_SWITCH_VIDEO 14
|
---|
97 | #define DEMUXER_CTRL_IDENTIFY_PROGRAM 15
|
---|
98 |
|
---|
99 | //#define off_t long long
|
---|
100 |
|
---|
101 | #pragma pack(1)
|
---|
102 | // Holds one packet/frame/whatever
|
---|
103 | typedef struct demux_packet_st {
|
---|
104 | unsigned char* buffer;
|
---|
105 | int len;
|
---|
106 | double pts;
|
---|
107 | off_t pos; // position in index (AVI) or file (MPG)
|
---|
108 | int flags; // keyframe, etc
|
---|
109 | int refcount; //refcounter for the master packet, if 0, buffer can be free()d
|
---|
110 | struct demux_packet_st* master; //pointer to the master packet if this one is a cloned one
|
---|
111 | struct demux_packet_st* next;
|
---|
112 | } demux_packet_t;
|
---|
113 |
|
---|
114 | typedef struct {
|
---|
115 | unsigned char* buffer; // current buffer, never free() it, always use free_demux_packet(buffer_ref);
|
---|
116 | int buffer_pos; // current buffer position
|
---|
117 | int buffer_size; // current buffer size
|
---|
118 | double pts;
|
---|
119 | int pts_bytes; // number of bytes read after last pts stamp
|
---|
120 | int eof; // end of demuxed stream? (true if all buffer empty)
|
---|
121 | off_t pos; // position in the input stream (file)
|
---|
122 | off_t dpos; // position in the demuxed stream
|
---|
123 | int pack_no; // serial number of packet
|
---|
124 | int flags; // flags of current packet (keyframe etc)
|
---|
125 | //---------------
|
---|
126 | int packs; // number of packets in buffer
|
---|
127 | int bytes; // total bytes of packets in buffer
|
---|
128 | demux_packet_t *first; // read to current buffer from here
|
---|
129 | demux_packet_t *last; // append new packets from input stream to here
|
---|
130 | demux_packet_t *current;// needed for refcounting of the buffer
|
---|
131 | int id; // stream ID (for multiple audio/video streams)
|
---|
132 | struct demuxer_st *demuxer; // parent demuxer structure (stream handler)
|
---|
133 | // ---- asf -----
|
---|
134 | demux_packet_t *asf_packet; // read asf fragments here
|
---|
135 | int asf_seq;
|
---|
136 | // ---- mov -----
|
---|
137 | unsigned int ss_mul,ss_div;
|
---|
138 | // ---- avi -----
|
---|
139 | unsigned int block_size;
|
---|
140 | // ---- stream header ----
|
---|
141 | void* sh;
|
---|
142 | } demux_stream_t;
|
---|
143 |
|
---|
144 | typedef struct demuxer_info_st {
|
---|
145 | char *name;
|
---|
146 | char *author;
|
---|
147 | char *encoder;
|
---|
148 | char *comments;
|
---|
149 | char *copyright;
|
---|
150 | } demuxer_info_t;
|
---|
151 |
|
---|
152 | #define MAX_A_STREAMS 256
|
---|
153 | #define MAX_V_STREAMS 256
|
---|
154 | #define MAX_S_STREAMS 32
|
---|
155 |
|
---|
156 | struct demuxer_st;
|
---|
157 |
|
---|
158 | extern int correct_pts;
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Demuxer description structure
|
---|
162 | */
|
---|
163 | typedef struct demuxers_desc_st {
|
---|
164 | const char *info; ///< What is it (long name and/or description)
|
---|
165 | const char *name; ///< Demuxer name, used with -demuxer switch
|
---|
166 | const char *shortdesc; ///< Description printed at demuxer detection
|
---|
167 | const char *author; ///< Demuxer author(s)
|
---|
168 | const char *comment; ///< Comment, printed with -demuxer help
|
---|
169 |
|
---|
170 | int type; ///< DEMUXER_TYPE_xxx
|
---|
171 | int safe_check; ///< If 1 detection is safe and fast, do it before file extension check
|
---|
172 |
|
---|
173 | /// Check if can demux the file, return DEMUXER_TYPE_xxx on success
|
---|
174 | int (*check_file)(struct demuxer_st *demuxer); ///< Mandatory if safe_check == 1, else optional
|
---|
175 | /// Get packets from file, return 0 on eof
|
---|
176 | int (*fill_buffer)(struct demuxer_st *demuxer, demux_stream_t *ds); ///< Mandatory
|
---|
177 | /// Open the demuxer, return demuxer on success, NULL on failure
|
---|
178 | struct demuxer_st* (*open)(struct demuxer_st *demuxer); ///< Optional
|
---|
179 | /// Close the demuxer
|
---|
180 | void (*close)(struct demuxer_st *demuxer); ///< Optional
|
---|
181 | // Seek
|
---|
182 | void (*seek)(struct demuxer_st *demuxer, float rel_seek_secs, int flags); ///< Optional
|
---|
183 | // Control
|
---|
184 | int (*control)(struct demuxer_st *demuxer, int cmd, void *arg); ///< Optional
|
---|
185 | } demuxer_desc_t;
|
---|
186 |
|
---|
187 | typedef struct demux_chapter_s
|
---|
188 | {
|
---|
189 | uint64_t start, end;
|
---|
190 | char* name;
|
---|
191 | } demux_chapter_t;
|
---|
192 |
|
---|
193 | typedef struct {
|
---|
194 | int progid; //program id
|
---|
195 | int aid, vid, sid; //audio, video and subtitle id
|
---|
196 | } demux_program_t;
|
---|
197 |
|
---|
198 | typedef struct demuxer_st {
|
---|
199 | demuxer_desc_t *desc; ///< Demuxer description structure
|
---|
200 | off_t filepos; // input stream current pos.
|
---|
201 | off_t movi_start;
|
---|
202 | off_t movi_end;
|
---|
203 | stream_t *stream;
|
---|
204 | char *filename; ///< Needed by avs_check_file
|
---|
205 | int synced; // stream synced (used by mpeg)
|
---|
206 | int type; // demuxer type: mpeg PS, mpeg ES, avi, avi-ni, avi-nini, asf
|
---|
207 | int file_format; // file format: mpeg/avi/asf
|
---|
208 | int seekable; // flag
|
---|
209 | //
|
---|
210 | demux_stream_t *audio; // audio buffer/demuxer
|
---|
211 | demux_stream_t *video; // video buffer/demuxer
|
---|
212 | demux_stream_t *sub; // dvd subtitle buffer/demuxer
|
---|
213 | char** info;
|
---|
214 |
|
---|
215 | // stream headers:
|
---|
216 | void* a_streams[MAX_A_STREAMS]; // audio streams (sh_audio_t)
|
---|
217 | void* v_streams[MAX_V_STREAMS]; // video sterams (sh_video_t)
|
---|
218 | void *s_streams[MAX_S_STREAMS]; // dvd subtitles (flag)
|
---|
219 | demux_chapter_t* chapters;
|
---|
220 | int num_chapters;
|
---|
221 | void* priv; // fileformat-dependent data
|
---|
222 | } demuxer_t;
|
---|
223 |
|
---|
224 | #pragma pack()
|
---|
225 |
|
---|
226 | inline static demux_packet_t* new_demux_packet(int len){
|
---|
227 | demux_packet_t* dp;
|
---|
228 | dp=(demux_packet_t *)malloc(sizeof(demux_packet_t));
|
---|
229 | dp->len=len;
|
---|
230 | dp->next=NULL;
|
---|
231 | dp->pts=0;
|
---|
232 | dp->pos=0;
|
---|
233 | dp->flags=0;
|
---|
234 | dp->refcount=1;
|
---|
235 | dp->master=NULL;
|
---|
236 | dp->buffer=NULL;
|
---|
237 | if (len > 0 && (dp->buffer = (unsigned char*)malloc(len + 8)))
|
---|
238 | memset(dp->buffer + len, 0, 8);
|
---|
239 | else
|
---|
240 | dp->len = 0;
|
---|
241 | return dp;
|
---|
242 | }
|
---|
243 |
|
---|
244 | inline static void resize_demux_packet(demux_packet_t* dp, int len)
|
---|
245 | {
|
---|
246 | if(len > 0)
|
---|
247 | {
|
---|
248 | dp->buffer=(unsigned char *)realloc(dp->buffer,len+8);
|
---|
249 | }
|
---|
250 | else
|
---|
251 | {
|
---|
252 | if(dp->buffer) free(dp->buffer);
|
---|
253 | dp->buffer=NULL;
|
---|
254 | }
|
---|
255 | dp->len=len;
|
---|
256 | if (dp->buffer)
|
---|
257 | memset(dp->buffer + len, 0, 8);
|
---|
258 | else
|
---|
259 | dp->len = 0;
|
---|
260 | }
|
---|
261 |
|
---|
262 | inline static demux_packet_t* clone_demux_packet(demux_packet_t* pack){
|
---|
263 | demux_packet_t* dp=(demux_packet_t *)malloc(sizeof(demux_packet_t));
|
---|
264 | while(pack->master) pack=pack->master; // find the master
|
---|
265 | memcpy(dp,pack,sizeof(demux_packet_t));
|
---|
266 | dp->next=NULL;
|
---|
267 | dp->refcount=0;
|
---|
268 | dp->master=pack;
|
---|
269 | pack->refcount++;
|
---|
270 | return dp;
|
---|
271 | }
|
---|
272 |
|
---|
273 | inline static void free_demux_packet(demux_packet_t* dp){
|
---|
274 | if (dp->master==NULL){ //dp is a master packet
|
---|
275 | dp->refcount--;
|
---|
276 | if (dp->refcount==0){
|
---|
277 | if (dp->buffer)
|
---|
278 | {
|
---|
279 | free(dp->buffer);
|
---|
280 | }
|
---|
281 | free(dp);
|
---|
282 | dp=NULL;
|
---|
283 | }
|
---|
284 | return;
|
---|
285 | }
|
---|
286 | // dp is a clone:
|
---|
287 | free_demux_packet(dp->master);
|
---|
288 | free(dp);
|
---|
289 | dp=NULL;
|
---|
290 | }
|
---|
291 |
|
---|
292 | demux_stream_t* new_demuxer_stream(struct demuxer_st *demuxer,int id);
|
---|
293 | demuxer_t* new_demuxer(stream_t *stream,int type,int a_id,int v_id,int s_id,char *filename);
|
---|
294 | void free_demuxer_stream(demux_stream_t *ds);
|
---|
295 | void free_demuxer(demuxer_t *demuxer);
|
---|
296 |
|
---|
297 | void ds_add_packet(demux_stream_t *ds,demux_packet_t* dp);
|
---|
298 | void ds_read_packet(demux_stream_t *ds, stream_t *stream, int len, double pts, off_t pos, int flags);
|
---|
299 |
|
---|
300 | int demux_fill_buffer(demuxer_t *demux,demux_stream_t *ds);
|
---|
301 | int ds_fill_buffer(demux_stream_t *ds);
|
---|
302 |
|
---|
303 | inline static off_t ds_tell(demux_stream_t *ds){
|
---|
304 | return (ds->dpos-ds->buffer_size)+ds->buffer_pos;
|
---|
305 | }
|
---|
306 |
|
---|
307 | inline static int ds_tell_pts(demux_stream_t *ds){
|
---|
308 | return (ds->pts_bytes-ds->buffer_size)+ds->buffer_pos;
|
---|
309 | }
|
---|
310 |
|
---|
311 | int demux_read_data(demux_stream_t *ds,unsigned char* mem,int len);
|
---|
312 | int demux_read_data_pack(demux_stream_t *ds,unsigned char* mem,int len);
|
---|
313 | int demux_pattern_3(demux_stream_t *ds, unsigned char *mem, int maxlen,
|
---|
314 | int *read, uint32_t pattern);
|
---|
315 |
|
---|
316 | #define demux_peekc(ds) (\
|
---|
317 | (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos] \
|
---|
318 | :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos] ) )
|
---|
319 | #if 1
|
---|
320 | #define demux_getc(ds) (\
|
---|
321 | (likely(ds->buffer_pos<ds->buffer_size)) ? ds->buffer[ds->buffer_pos++] \
|
---|
322 | :((unlikely(!ds_fill_buffer(ds)))? (-1) : ds->buffer[ds->buffer_pos++] ) )
|
---|
323 | #else
|
---|
324 | inline static int demux_getc(demux_stream_t *ds){
|
---|
325 | if(ds->buffer_pos>=ds->buffer_size){
|
---|
326 | if(!ds_fill_buffer(ds)){
|
---|
327 | // printf("DEMUX_GETC: EOF reached!\n");
|
---|
328 | return -1; // EOF
|
---|
329 | }
|
---|
330 | }
|
---|
331 | // printf("[%02X]",ds->buffer[ds->buffer_pos]);
|
---|
332 | return ds->buffer[ds->buffer_pos++];
|
---|
333 | }
|
---|
334 | #endif
|
---|
335 |
|
---|
336 | void ds_free_packs(demux_stream_t *ds);
|
---|
337 | int ds_get_packet(demux_stream_t *ds,unsigned char **start);
|
---|
338 | int ds_get_packet_pts(demux_stream_t *ds, unsigned char **start, double *pts);
|
---|
339 | int ds_get_packet_sub(demux_stream_t *ds,unsigned char **start);
|
---|
340 | double ds_get_next_pts(demux_stream_t *ds);
|
---|
341 |
|
---|
342 | // This is defined here because demux_stream_t ins't defined in stream.h
|
---|
343 | stream_t* new_ds_stream(demux_stream_t *ds);
|
---|
344 |
|
---|
345 | static inline int avi_stream_id(unsigned int id){
|
---|
346 | unsigned char *p=(unsigned char *)&id;
|
---|
347 | unsigned char a,b;
|
---|
348 | #if WORDS_BIGENDIAN
|
---|
349 | a=p[3]-'0'; b=p[2]-'0';
|
---|
350 | #else
|
---|
351 | a=p[0]-'0'; b=p[1]-'0';
|
---|
352 | #endif
|
---|
353 | if(a>9 || b>9) return 100; // invalid ID
|
---|
354 | return a*10+b;
|
---|
355 | }
|
---|
356 |
|
---|
357 | demuxer_t* demux_open(stream_t *stream,int file_format,int aid,int vid,int sid,char* filename);
|
---|
358 | int demux_seek(demuxer_t *demuxer,float rel_seek_secs,int flags);
|
---|
359 | demuxer_t* new_demuxers_demuxer(demuxer_t* vd, demuxer_t* ad, demuxer_t* sd);
|
---|
360 |
|
---|
361 | // AVI demuxer params:
|
---|
362 | extern int index_mode; // -1=untouched 0=don't use index 1=use (geneate) index
|
---|
363 | extern int force_ni;
|
---|
364 | extern int pts_from_bps;
|
---|
365 |
|
---|
366 | int demux_info_add(demuxer_t *demuxer, char *opt, char *param);
|
---|
367 | char* demux_info_get(demuxer_t *demuxer, char *opt);
|
---|
368 | int demux_info_print(demuxer_t *demuxer);
|
---|
369 | int demux_control(demuxer_t *demuxer, int cmd, void *arg);
|
---|
370 | #endif
|
---|
371 | extern int demuxer_get_current_time(demuxer_t *demuxer);
|
---|
372 | extern double demuxer_get_time_length(demuxer_t *demuxer);
|
---|
373 | extern int demuxer_get_percent_pos(demuxer_t *demuxer);
|
---|
374 | extern int demuxer_switch_audio(demuxer_t *demuxer, int index);
|
---|
375 | extern int demuxer_switch_video(demuxer_t *demuxer, int index);
|
---|
376 | float demux_mov_len(demuxer_t *demuxer);
|
---|
377 | float demux_mpg_len(demuxer_t *d);
|
---|
378 | int demux_ogg_control(demuxer_t *demuxer,int cmd, void *arg);
|
---|
379 |
|
---|
380 | extern void demuxer_help(void);
|
---|
381 | extern int identify;
|
---|
382 | extern int get_demuxer_type_from_name(char *demuxer_name, int *force);
|
---|
383 | int demuxer_add_chapter(demuxer_t* demuxer, const char* name, uint64_t start, uint64_t end);
|
---|
384 | int demuxer_seek_chapter(demuxer_t *demuxer, int chapter, int mode, float *seek_pts, int *num_chapters, char **chapter_name);
|
---|