Mercurial > mplayer.hg
annotate libmpdemux/demux_viv.c @ 3526:cf787373f0aa
vivo A-V sync fixed
author | arpi |
---|---|
date | Sun, 16 Dec 2001 14:51:04 +0000 |
parents | 7aed2edd99ab |
children | a1522fa7728a |
rev | line source |
---|---|
2687 | 1 // VIVO file parser by A'rpi |
3471 | 2 // VIVO text header parser and audio support by alex |
2687 | 3 |
4 #include <stdio.h> | |
5 #include <stdlib.h> | |
6 #include <unistd.h> | |
2928 | 7 #include <string.h> /* strtok */ |
2687 | 8 |
9 #include "config.h" | |
10 #include "mp_msg.h" | |
11 #include "help_mp.h" | |
12 | |
13 #include "stream.h" | |
14 #include "demuxer.h" | |
15 #include "stheader.h" | |
3471 | 16 #include "bswap.h" |
2687 | 17 |
3502 | 18 /* parameters ! */ |
3503 | 19 int vivo_param_version = -1; |
3502 | 20 char *vivo_param_acodec = NULL; |
21 int vivo_param_abitrate = -1; | |
22 int vivo_param_samplerate = -1; | |
23 int vivo_param_bytesperblock = -1; | |
24 int vivo_param_width = -1; | |
25 int vivo_param_height = -1; | |
26 int vivo_param_vformat = -1; | |
27 | |
3471 | 28 /* VIVO audio standards from vivog723.acm: |
29 | |
30 G.723: | |
31 FormatTag = 0x111 | |
32 Channels = 1 - mono | |
33 SamplesPerSec = 8000 - 8khz | |
34 AvgBytesPerSec = 800 | |
35 BlockAlign (bytes per block) = 24 | |
36 BitsPerSample = 8 | |
37 | |
38 Siren: | |
39 FormatTag = 0x112 | |
40 Channels = 1 - mono | |
41 SamplesPerSec = 16000 - 16khz | |
42 AvgBytesPerSec = 2000 | |
43 BlockAlign (bytes per block) = 40 | |
44 BitsPerSample = 8 | |
45 */ | |
46 | |
3502 | 47 //enum { VIVO_AUDIO_G723, VIVO_AUDIO_SIREN }; |
48 | |
3471 | 49 #define VIVO_AUDIO_G723 1 |
50 #define VIVO_AUDIO_SIREN 2 | |
2687 | 51 |
52 typedef struct { | |
2784 | 53 /* generic */ |
3113
c0e6a39d2ab7
changed to generate fourcc's like: viv<version> -> viv1,viv2
alex
parents:
3069
diff
changeset
|
54 char version; |
2784 | 55 int supported; |
56 /* info */ | |
57 char *title; | |
58 char *author; | |
59 char *copyright; | |
60 char *producer; | |
61 /* video */ | |
62 float fps; | |
63 int width; | |
64 int height; | |
65 int disp_width; | |
66 int disp_height; | |
67 /* audio */ | |
3471 | 68 int audio_codec; |
69 int audio_bitrate; | |
70 int audio_samplerate; | |
71 int audio_bytesperblock; | |
2687 | 72 } vivo_priv_t; |
73 | |
2784 | 74 /* parse all possible extra headers */ |
3439
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
75 /* (audio headers are seperate - mostly with recordtype=3 or 4) */ |
2784 | 76 #define TEXTPARSE_ALL 1 |
77 | |
78 static void vivo_parse_text_header(demuxer_t *demux, int header_len) | |
79 { | |
80 vivo_priv_t* priv = demux->priv; | |
81 char *buf; | |
82 int i; | |
83 char *token; | |
84 char *opt, *param; | |
3439
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
85 int parser_in_audio_block = 0; |
2784 | 86 |
87 if (!demux->priv) | |
88 { | |
89 priv = malloc(sizeof(vivo_priv_t)); | |
90 memset(priv, 0, sizeof(vivo_priv_t)); | |
91 demux->priv = priv; | |
92 priv->supported = 0; | |
93 } | |
94 | |
95 buf = malloc(header_len); | |
96 opt = malloc(header_len); | |
97 param = malloc(header_len); | |
98 stream_read(demux->stream, buf, header_len); | |
99 i=0; | |
100 while(i<header_len && buf[i]==0x0D && buf[i+1]==0x0A) i+=2; // skip empty lines | |
101 | |
102 token = strtok(buf, (char *)&("\x0d\x0a")); | |
103 while (token && (header_len>2)) | |
104 { | |
105 header_len -= strlen(token)+2; | |
106 if (sscanf(token, "%[^:]:%[^\n]", opt, param) != 2) | |
107 { | |
108 mp_msg(MSGT_DEMUX, MSGL_V, "viv_text_header_parser: bad line: '%s' at ~%p\n", | |
109 token, stream_tell(demux->stream)); | |
110 break; | |
111 } | |
3471 | 112 mp_dbg(MSGT_DEMUX, MSGL_DBG3, "token: '%s' (%d bytes/%d bytes left)\n", |
2784 | 113 token, strlen(token), header_len); |
3471 | 114 mp_dbg(MSGT_DEMUX, MSGL_DBG3, "token => o: '%s', p: '%s'\n", |
2784 | 115 opt, param); |
116 | |
117 /* checking versions: only v1 or v2 is suitable (or known?:) */ | |
118 if (!strcmp(opt, "Version")) | |
119 { | |
120 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Version: %s\n", param); | |
2928 | 121 if (!strncmp(param, "Vivo/1", 6) || !strncmp(param, "Vivo/2", 6)) |
3113
c0e6a39d2ab7
changed to generate fourcc's like: viv<version> -> viv1,viv2
alex
parents:
3069
diff
changeset
|
122 { |
2784 | 123 priv->supported = 1; |
3439
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
124 /* save major version for fourcc */ |
3113
c0e6a39d2ab7
changed to generate fourcc's like: viv<version> -> viv1,viv2
alex
parents:
3069
diff
changeset
|
125 priv->version = param[5]; |
c0e6a39d2ab7
changed to generate fourcc's like: viv<version> -> viv1,viv2
alex
parents:
3069
diff
changeset
|
126 } |
2784 | 127 } |
128 | |
129 /* video specific */ | |
130 if (!strcmp(opt, "FPS")) | |
131 { | |
132 mp_msg(MSGT_DEMUX, MSGL_DBG2, "FPS: %f\n", atof(param)); | |
133 priv->fps = atof(param); | |
134 } | |
135 if (!strcmp(opt, "Width")) | |
136 { | |
137 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Width: %d\n", atoi(param)); | |
138 priv->width = atoi(param); | |
139 } | |
140 if (!strcmp(opt, "Height")) | |
141 { | |
142 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Height: %d\n", atoi(param)); | |
143 priv->height = atoi(param); | |
144 } | |
145 if (!strcmp(opt, "DisplayWidth")) | |
146 { | |
147 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Display Width: %d\n", atoi(param)); | |
148 priv->disp_width = atoi(param); | |
149 } | |
150 if (!strcmp(opt, "DisplayHeight")) | |
151 { | |
152 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Display Height: %d\n", atoi(param)); | |
153 priv->disp_height = atoi(param); | |
154 } | |
155 | |
156 /* audio specific */ | |
3439
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
157 if (!strcmp(opt, "RecordType")) |
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
158 { |
3471 | 159 /* no audio recordblock by Vivo/1.00, 3 and 4 by Vivo/2.00 */ |
3439
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
160 if ((atoi(param) == 3) || (atoi(param) == 4)) |
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
161 parser_in_audio_block = 1; |
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
162 else |
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
163 parser_in_audio_block = 0; |
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
164 } |
2784 | 165 if (!strcmp(opt, "NominalBitrate")) |
166 { | |
3471 | 167 priv->audio_bitrate = atoi(param); |
168 if (priv->audio_bitrate == 2000) | |
169 priv->audio_codec = VIVO_AUDIO_SIREN; | |
170 if (priv->audio_bitrate == 800) | |
171 priv->audio_codec = VIVO_AUDIO_G723; | |
2784 | 172 } |
173 if (!strcmp(opt, "SamplingFrequency")) | |
174 { | |
3471 | 175 priv->audio_samplerate = atoi(param); |
176 if (priv->audio_samplerate == 16000) | |
177 priv->audio_codec = VIVO_AUDIO_SIREN; | |
178 if (priv->audio_samplerate == 8000) | |
179 priv->audio_codec = VIVO_AUDIO_G723; | |
2784 | 180 } |
3439
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
181 if (!strcmp(opt, "Length") && (parser_in_audio_block == 1)) |
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
182 { |
3471 | 183 priv->audio_bytesperblock = atoi(param); /* 24 or 40 kbps */ |
184 if (priv->audio_bytesperblock == 40) | |
185 priv->audio_codec = VIVO_AUDIO_SIREN; | |
186 if (priv->audio_bytesperblock == 24) | |
187 priv->audio_codec = VIVO_AUDIO_G723; | |
3439
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
188 } |
2784 | 189 |
190 /* only for displaying some informations about movie*/ | |
191 if (!strcmp(opt, "Title")) | |
192 { | |
3068 | 193 demux_info_add(demux, "name", param); |
2784 | 194 priv->title = malloc(strlen(param)); |
195 strcpy(priv->title, param); | |
196 } | |
197 if (!strcmp(opt, "Author")) | |
198 { | |
3068 | 199 demux_info_add(demux, "author", param); |
2784 | 200 priv->author = malloc(strlen(param)); |
201 strcpy(priv->author, param); | |
202 } | |
203 if (!strcmp(opt, "Copyright")) | |
204 { | |
3068 | 205 demux_info_add(demux, "copyright", param); |
2784 | 206 priv->copyright = malloc(strlen(param)); |
207 strcpy(priv->copyright, param); | |
208 } | |
209 if (!strcmp(opt, "Producer")) | |
210 { | |
3069 | 211 demux_info_add(demux, "encoder", param); |
2784 | 212 priv->producer = malloc(strlen(param)); |
213 strcpy(priv->producer, param); | |
214 } | |
215 | |
216 /* get next token */ | |
217 token = strtok(NULL, (char *)&("\x0d\x0a")); | |
218 } | |
2788 | 219 |
220 if (buf) | |
221 free(buf); | |
222 if (opt) | |
223 free(opt); | |
224 if (param) | |
225 free(param); | |
2784 | 226 } |
227 | |
2687 | 228 int vivo_check_file(demuxer_t* demuxer){ |
229 int flags=0; | |
230 int i=0; | |
231 int len; | |
232 int len2; | |
233 int c; | |
234 unsigned char buf[2048+256]; | |
235 vivo_priv_t* priv; | |
2784 | 236 int orig_pos = stream_tell(demuxer->stream); |
2687 | 237 |
238 mp_msg(MSGT_DEMUX,MSGL_V,"Checking for VIVO\n"); | |
239 | |
240 c=stream_read_char(demuxer->stream); | |
2792 | 241 if(c==-256) return 0; |
2687 | 242 len=0; |
243 while((c=stream_read_char(demuxer->stream))>=0x80){ | |
244 len+=0x80*(c-0x80); | |
245 if(len>1024) return 0; | |
246 } | |
247 len+=c; | |
248 printf("header block 1 size: %d\n",len); | |
249 //stream_skip(demuxer->stream,len); | |
2928 | 250 |
251 priv=malloc(sizeof(vivo_priv_t)); | |
252 memset(priv,0,sizeof(vivo_priv_t)); | |
253 demuxer->priv=priv; | |
2784 | 254 |
2805 | 255 #if 0 |
2785 | 256 vivo_parse_text_header(demuxer, len); |
2784 | 257 if (priv->supported == 0) |
258 return 0; | |
259 #else | |
2928 | 260 /* this is enought for check (for now) */ |
261 stream_read(demuxer->stream,buf,len); | |
262 buf[len]=0; | |
263 // printf("VIVO header: '%s'\n",buf); | |
264 | |
2687 | 265 // parse header: |
266 i=0; | |
267 while(i<len && buf[i]==0x0D && buf[i+1]==0x0A) i+=2; // skip empty lines | |
2695 | 268 if(strncmp(buf+i,"Version:Vivo/",13)) return 0; // bad version/type! |
2784 | 269 #endif |
2687 | 270 |
2695 | 271 #if 0 |
2687 | 272 c=stream_read_char(demuxer->stream); |
273 if(c) return 0; | |
274 len2=0; | |
275 while((c=stream_read_char(demuxer->stream))>=0x80){ | |
276 len2+=0x80*(c-0x80); | |
277 if(len+len2>2048) return 0; | |
278 } | |
279 len2+=c; | |
280 printf("header block 2 size: %d\n",len2); | |
281 stream_skip(demuxer->stream,len2); | |
282 // stream_read(demuxer->stream,buf+len,len2); | |
2695 | 283 #endif |
2687 | 284 |
285 // c=stream_read_char(demuxer->stream); | |
286 // printf("first packet: %02X\n",c); | |
287 | |
2784 | 288 stream_seek(demuxer->stream, orig_pos); |
289 | |
2687 | 290 return 1; |
291 } | |
292 | |
3526 | 293 static audio_pos=0; |
294 static audio_rate=0; | |
295 | |
2687 | 296 // return value: |
297 // 0 = EOF or no stream found | |
298 // 1 = successfully read a packet | |
299 int demux_vivo_fill_buffer(demuxer_t *demux){ | |
300 demux_stream_t *ds=NULL; | |
301 int c; | |
302 int len=0; | |
303 int seq; | |
3245
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
304 int prefix=0; |
2687 | 305 demux->filepos=stream_tell(demux->stream); |
306 | |
307 c=stream_read_char(demux->stream); | |
2792 | 308 if (c == -256) /* EOF */ |
309 return 0; | |
3245
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
310 // printf("c=%x,%02X\n",c,c&0xf0); |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
311 if (c == 0x82) |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
312 { |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
313 /* ok, this works, but pts calculating from header is required! */ |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
314 #warning "Calculate PTS from picture header!" |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
315 prefix = 1; |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
316 c = stream_read_char(demux->stream); |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
317 printf("packet 0x82(pos=%lu) chunk=%x\n", |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
318 stream_tell(demux->stream), c); |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
319 } |
2687 | 320 switch(c&0xF0){ |
2695 | 321 case 0x00: // header - skip it! |
2784 | 322 { |
2687 | 323 len=stream_read_char(demux->stream); |
2695 | 324 if(len>=0x80) len=0x80*(len-0x80)+stream_read_char(demux->stream); |
325 printf("vivo extra header: %d bytes\n",len); | |
2784 | 326 #ifdef TEXTPARSE_ALL |
327 { | |
328 int pos; | |
329 /* also try to parse all headers */ | |
330 pos = stream_tell(demux->stream); | |
331 vivo_parse_text_header(demux, len); | |
332 stream_seek(demux->stream, pos); | |
333 } | |
334 #endif | |
2687 | 335 break; |
2784 | 336 } |
2687 | 337 case 0x10: // video packet |
3245
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
338 if (prefix == 1) |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
339 len = stream_read_char(demux->stream); |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
340 else |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
341 len=128; |
2687 | 342 ds=demux->video; |
343 break; | |
344 case 0x20: // video packet | |
345 len=stream_read_char(demux->stream); | |
346 ds=demux->video; | |
347 break; | |
3068 | 348 case 0x30: // audio packet |
3245
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
349 if (prefix == 1) |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
350 len = stream_read_char(demux->stream); |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
351 else |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
352 len=40; /* 40kbps */ |
2695 | 353 ds=demux->audio; |
3526 | 354 audio_pos+=len; |
2695 | 355 break; |
2687 | 356 case 0x40: // audio packet |
3245
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
357 if (prefix == 1) |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
358 len = stream_read_char(demux->stream); |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
359 else |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
360 len=24; /* 24kbps */ |
2687 | 361 ds=demux->audio; |
3526 | 362 audio_pos+=len; |
2687 | 363 break; |
364 default: | |
3245
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
365 mp_msg(MSGT_DEMUX,MSGL_WARN,"VIVO - unknown ID found: %02X at pos %lu contact author!\n", |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
366 c, stream_tell(demux->stream)); |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
367 return 0; |
2687 | 368 } |
3245
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
369 |
1e2a87b7cae8
added support for packet 0x82 (from vivodump.c by arpi)
alex
parents:
3113
diff
changeset
|
370 // printf("chunk=%x, len=%d\n", c, len); |
2687 | 371 |
2695 | 372 if(!ds || ds->id<-1){ |
2687 | 373 if(len) stream_skip(demux->stream,len); |
374 return 1; | |
375 } | |
376 | |
377 seq=c&0x0F; | |
378 | |
379 if(ds->asf_packet){ | |
380 if(ds->asf_seq!=seq){ | |
381 // closed segment, finalize packet: | |
382 ds_add_packet(ds,ds->asf_packet); | |
383 ds->asf_packet=NULL; | |
2695 | 384 // printf("packet!\n"); |
2687 | 385 } else { |
386 // append data to it! | |
387 demux_packet_t* dp=ds->asf_packet; | |
388 dp->buffer=realloc(dp->buffer,dp->len+len); | |
389 //memcpy(dp->buffer+dp->len,data,len); | |
390 stream_read(demux->stream,dp->buffer+dp->len,len); | |
391 mp_dbg(MSGT_DEMUX,MSGL_DBG4,"data appended! %d+%d\n",dp->len,len); | |
392 dp->len+=len; | |
393 // we are ready now. | |
2695 | 394 if((c&0xF0)==0x20) --ds->asf_seq; // hack! |
2687 | 395 return 1; |
396 } | |
397 } | |
398 // create new packet: | |
399 { demux_packet_t* dp; | |
400 dp=new_demux_packet(len); | |
401 //memcpy(dp->buffer,data,len); | |
402 stream_read(demux->stream,dp->buffer,len); | |
3526 | 403 dp->pts=audio_rate?((float)audio_pos/(float)audio_rate):0; |
2687 | 404 // dp->flags=keyframe; |
405 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur); | |
406 dp->pos=demux->filepos; | |
407 ds->asf_packet=dp; | |
408 ds->asf_seq=seq; | |
409 // we are ready now. | |
410 return 1; | |
411 } | |
412 | |
413 } | |
414 | |
415 static const short h263_format[8][2] = { | |
416 { 0, 0 }, | |
417 { 128, 96 }, | |
418 { 176, 144 }, | |
419 { 352, 288 }, | |
420 { 704, 576 }, | |
421 { 1408, 1152 }, | |
2784 | 422 { 320, 240 } // ??????? or 240x180 (found in vivo2) ? |
2687 | 423 }; |
424 | |
425 static unsigned char* buffer; | |
426 static int bufptr=0; | |
427 static int bitcnt=0; | |
428 static unsigned char buf=0; | |
429 static int format, width, height; | |
430 | |
431 static unsigned int x_get_bits(int n){ | |
432 unsigned int x=0; | |
433 while(n-->0){ | |
434 if(!bitcnt){ | |
435 // fill buff | |
436 buf=buffer[bufptr++]; | |
437 bitcnt=8; | |
438 } | |
439 //x=(x<<1)|(buf&1);buf>>=1; | |
440 x=(x<<1)|(buf>>7);buf<<=1; | |
441 --bitcnt; | |
442 } | |
443 return x; | |
444 } | |
445 | |
446 #define get_bits(xxx,n) x_get_bits(n) | |
447 #define get_bits1(xxx) x_get_bits(1) | |
448 #define skip_bits(xxx,n) x_get_bits(n) | |
449 #define skip_bits1(xxx) x_get_bits(1) | |
450 | |
451 /* most is hardcoded. should extend to handle all h263 streams */ | |
452 static int h263_decode_picture_header(unsigned char *b_ptr) | |
453 { | |
2928 | 454 // int i; |
2695 | 455 |
456 // for(i=0;i<16;i++) printf(" %02X",b_ptr[i]); printf("\n"); | |
2687 | 457 |
458 buffer=b_ptr; | |
459 bufptr=bitcnt=buf=0; | |
460 | |
461 /* picture header */ | |
2695 | 462 if (get_bits(&s->gb, 22) != 0x20){ |
463 printf("bad picture header\n"); | |
2687 | 464 return -1; |
2695 | 465 } |
2687 | 466 skip_bits(&s->gb, 8); /* picture timestamp */ |
467 | |
2695 | 468 if (get_bits1(&s->gb) != 1){ |
469 printf("bad marker\n"); | |
2687 | 470 return -1; /* marker */ |
2695 | 471 } |
472 if (get_bits1(&s->gb) != 0){ | |
473 printf("bad h263 id\n"); | |
2687 | 474 return -1; /* h263 id */ |
2695 | 475 } |
2687 | 476 skip_bits1(&s->gb); /* split screen off */ |
477 skip_bits1(&s->gb); /* camera off */ | |
478 skip_bits1(&s->gb); /* freeze picture release off */ | |
479 | |
480 format = get_bits(&s->gb, 3); | |
481 | |
482 if (format != 7) { | |
483 printf("h263_plus = 0 format = %d\n",format); | |
484 /* H.263v1 */ | |
485 width = h263_format[format][0]; | |
486 height = h263_format[format][1]; | |
487 printf("%d x %d\n",width,height); | |
2695 | 488 // if (!width) return -1; |
2687 | 489 |
490 printf("pict_type=%d\n",get_bits1(&s->gb)); | |
491 printf("unrestricted_mv=%d\n",get_bits1(&s->gb)); | |
492 #if 1 | |
493 printf("SAC: %d\n",get_bits1(&s->gb)); | |
494 printf("advanced prediction mode: %d\n",get_bits1(&s->gb)); | |
495 printf("PB frame: %d\n",get_bits1(&s->gb)); | |
496 #else | |
497 if (get_bits1(&s->gb) != 0) | |
498 return -1; /* SAC: off */ | |
499 if (get_bits1(&s->gb) != 0) | |
500 return -1; /* advanced prediction mode: off */ | |
501 if (get_bits1(&s->gb) != 0) | |
502 return -1; /* not PB frame */ | |
503 #endif | |
504 printf("qscale=%d\n",get_bits(&s->gb, 5)); | |
505 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */ | |
506 } else { | |
507 printf("h263_plus = 1\n"); | |
508 /* H.263v2 */ | |
2695 | 509 if (get_bits(&s->gb, 3) != 1){ |
510 printf("H.263v2 A error\n"); | |
2687 | 511 return -1; |
2695 | 512 } |
513 if (get_bits(&s->gb, 3) != 6){ /* custom source format */ | |
514 printf("custom source format\n"); | |
2687 | 515 return -1; |
2695 | 516 } |
2687 | 517 skip_bits(&s->gb, 12); |
518 skip_bits(&s->gb, 3); | |
519 printf("pict_type=%d\n",get_bits(&s->gb, 3) + 1); | |
520 // if (s->pict_type != I_TYPE && | |
521 // s->pict_type != P_TYPE) | |
522 // return -1; | |
523 skip_bits(&s->gb, 7); | |
524 skip_bits(&s->gb, 4); /* aspect ratio */ | |
525 width = (get_bits(&s->gb, 9) + 1) * 4; | |
526 skip_bits1(&s->gb); | |
527 height = get_bits(&s->gb, 9) * 4; | |
528 printf("%d x %d\n",width,height); | |
2695 | 529 //if (height == 0) |
530 // return -1; | |
2687 | 531 printf("qscale=%d\n",get_bits(&s->gb, 5)); |
532 } | |
533 | |
534 /* PEI */ | |
535 while (get_bits1(&s->gb) != 0) { | |
536 skip_bits(&s->gb, 8); | |
537 } | |
538 // s->f_code = 1; | |
539 // s->width = width; | |
540 // s->height = height; | |
541 return 0; | |
542 } | |
543 | |
544 | |
2695 | 545 |
2687 | 546 void demux_open_vivo(demuxer_t* demuxer){ |
547 vivo_priv_t* priv=demuxer->priv; | |
548 | |
549 if(!ds_fill_buffer(demuxer->video)){ | |
550 mp_msg(MSGT_DEMUX,MSGL_ERR,"VIVO: " MSGTR_MissingVideoStreamBug); | |
551 return; | |
552 } | |
3526 | 553 |
554 audio_pos=0; | |
2687 | 555 |
556 h263_decode_picture_header(demuxer->video->buffer); | |
557 | |
3503 | 558 if (vivo_param_version != -1) |
559 priv->version = '0' + vivo_param_version; | |
2687 | 560 |
561 { sh_video_t* sh=new_sh_video(demuxer,0); | |
3113
c0e6a39d2ab7
changed to generate fourcc's like: viv<version> -> viv1,viv2
alex
parents:
3069
diff
changeset
|
562 |
c0e6a39d2ab7
changed to generate fourcc's like: viv<version> -> viv1,viv2
alex
parents:
3069
diff
changeset
|
563 /* viv1, viv2 (for better codecs.conf) */ |
c0e6a39d2ab7
changed to generate fourcc's like: viv<version> -> viv1,viv2
alex
parents:
3069
diff
changeset
|
564 sh->format = mmioFOURCC('v', 'i', 'v', priv->version); |
2784 | 565 if(!sh->fps) |
2928 | 566 { |
2784 | 567 if (priv->fps) |
568 sh->fps=priv->fps; | |
569 else | |
570 sh->fps=15.0f; | |
2928 | 571 } |
2687 | 572 sh->frametime=1.0f/sh->fps; |
2805 | 573 |
3502 | 574 /* XXX: FIXME: can't scale image. */ |
575 /* hotfix to disable: */ | |
2805 | 576 priv->disp_width = priv->width; |
577 priv->disp_height = priv->height; | |
578 | |
3502 | 579 if (vivo_param_width != -1) |
580 priv->disp_width = priv->width = vivo_param_width; | |
581 | |
582 if (vivo_param_height != -1) | |
583 priv->disp_height = priv->height = vivo_param_height; | |
584 | |
585 if (vivo_param_vformat != -1) | |
586 { | |
587 priv->disp_width = priv->width = h263_format[vivo_param_vformat][0]; | |
588 priv->disp_height = priv->height = h263_format[vivo_param_vformat][1]; | |
589 } | |
590 | |
2784 | 591 if (priv->disp_width) |
592 sh->disp_w = priv->disp_width; | |
593 else | |
594 sh->disp_w = width; | |
595 if (priv->disp_height) | |
596 sh->disp_h = priv->disp_height; | |
597 else | |
598 sh->disp_h = height; | |
2687 | 599 |
600 // emulate BITMAPINFOHEADER: | |
601 sh->bih=malloc(sizeof(BITMAPINFOHEADER)); | |
602 memset(sh->bih,0,sizeof(BITMAPINFOHEADER)); | |
603 sh->bih->biSize=40; | |
2784 | 604 if (priv->width) |
605 sh->bih->biWidth = priv->width; | |
606 else | |
607 sh->bih->biWidth = width; | |
608 if (priv->height) | |
609 sh->bih->biHeight = priv->height; | |
610 else | |
611 sh->bih->biHeight = height; | |
2687 | 612 sh->bih->biPlanes=1; |
613 sh->bih->biBitCount=24; | |
614 sh->bih->biCompression=sh->format; | |
615 sh->bih->biSizeImage=sh->bih->biWidth*sh->bih->biHeight*3; | |
3502 | 616 |
617 /* insert as stream */ | |
618 demuxer->video->sh=sh; | |
619 sh->ds=demuxer->video; | |
2687 | 620 demuxer->video->id=0; |
2928 | 621 |
3502 | 622 /* disable seeking */ |
2928 | 623 demuxer->seekable = 0; |
2788 | 624 |
2928 | 625 printf("VIVO Video stream %d size: display: %dx%d, codec: %lux%lu\n", |
2788 | 626 demuxer->video->id, sh->disp_w, sh->disp_h, sh->bih->biWidth, |
627 sh->bih->biHeight); | |
2687 | 628 } |
629 | |
3502 | 630 /* AUDIO init */ |
631 if (demuxer->audio->id >= -1){ | |
2687 | 632 if(!ds_fill_buffer(demuxer->audio)){ |
633 mp_msg(MSGT_DEMUX,MSGL_ERR,"VIVO: " MSGTR_MissingAudioStream); | |
634 } else | |
635 { sh_audio_t* sh=new_sh_audio(demuxer,1); | |
2788 | 636 |
3502 | 637 /* Select audio codec */ |
3471 | 638 if (priv->audio_codec == 0) |
639 { | |
640 if (priv->version == '2') | |
641 priv->audio_codec = VIVO_AUDIO_SIREN; | |
642 else | |
643 priv->audio_codec = VIVO_AUDIO_G723; | |
644 } | |
3502 | 645 if (vivo_param_acodec != NULL) |
646 { | |
647 if (!strcasecmp(vivo_param_acodec, "g723")) | |
648 priv->audio_codec = VIVO_AUDIO_G723; | |
649 if (!strcasecmp(vivo_param_acodec, "siren")) | |
650 priv->audio_codec = VIVO_AUDIO_SIREN; | |
651 } | |
3471 | 652 |
3502 | 653 sh->format = -1; |
654 if (priv->audio_codec == VIVO_AUDIO_G723) | |
655 sh->format = 0x111; | |
3471 | 656 if (priv->audio_codec == VIVO_AUDIO_SIREN) |
3502 | 657 sh->format = 0x112; |
658 if (sh->format == -1) | |
659 { | |
660 mp_msg(MSGT_DEMUX, MSGL_ERR, "VIVO: Not support audio codec (%d)\n", | |
661 priv->audio_codec); | |
662 goto nosound; | |
663 } | |
3439
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
664 |
2687 | 665 // Emulate WAVEFORMATEX struct: |
666 sh->wf=malloc(sizeof(WAVEFORMATEX)); | |
667 memset(sh->wf,0,sizeof(WAVEFORMATEX)); | |
3439
192449c155c7
fixed Waveformatheader emu and added vivo1/2 audio detection
alex
parents:
3245
diff
changeset
|
668 sh->wf->wFormatTag=sh->format; |
3502 | 669 sh->wf->nChannels=1; /* 1 channels for both Siren and G.723 */ |
670 | |
671 /* Set bits per sample */ | |
3471 | 672 if (priv->audio_codec == VIVO_AUDIO_SIREN) |
3502 | 673 sh->wf->wBitsPerSample = 16; |
3471 | 674 else |
3502 | 675 if (priv->audio_codec == VIVO_AUDIO_G723) |
676 sh->wf->wBitsPerSample = 8; | |
677 | |
678 /* Set sampling rate */ | |
679 if (priv->audio_samplerate) /* got from header */ | |
680 sh->wf->nSamplesPerSec = priv->audio_samplerate; | |
3471 | 681 else |
682 { | |
683 if (priv->audio_codec == VIVO_AUDIO_SIREN) | |
3502 | 684 sh->wf->nSamplesPerSec = 16000; |
685 if (priv->audio_codec == VIVO_AUDIO_G723) | |
686 sh->wf->nSamplesPerSec = 8000; | |
3471 | 687 } |
3502 | 688 if (vivo_param_samplerate != -1) |
689 sh->wf->nSamplesPerSec = vivo_param_samplerate; | |
690 | |
691 /* Set audio bitrate */ | |
692 if (priv->audio_bitrate) /* got from header */ | |
693 sh->wf->nAvgBytesPerSec = priv->audio_bitrate; | |
2784 | 694 else |
3471 | 695 { |
696 if (priv->audio_codec == VIVO_AUDIO_SIREN) | |
697 sh->wf->nAvgBytesPerSec = 2000; | |
3502 | 698 if (priv->audio_codec == VIVO_AUDIO_G723) |
3471 | 699 sh->wf->nAvgBytesPerSec = 800; |
700 } | |
3502 | 701 if (vivo_param_abitrate != -1) |
702 sh->wf->nAvgBytesPerSec = vivo_param_abitrate; | |
3526 | 703 audio_rate=sh->wf->nAvgBytesPerSec; |
3502 | 704 |
3471 | 705 if (!priv->audio_bytesperblock) |
706 { | |
707 if (priv->audio_codec == VIVO_AUDIO_SIREN) | |
3502 | 708 sh->wf->nBlockAlign = 40; |
709 if (priv->audio_codec == VIVO_AUDIO_G723) | |
710 sh->wf->nBlockAlign = 24; | |
3471 | 711 } |
712 else | |
3502 | 713 sh->wf->nBlockAlign = priv->audio_bytesperblock; |
714 if (vivo_param_bytesperblock != -1) | |
715 sh->wf->nBlockAlign = vivo_param_bytesperblock; | |
716 | |
717 sound_ok: | |
718 /* insert as stream */ | |
719 demuxer->audio->sh=sh; | |
720 sh->ds=demuxer->audio; | |
2687 | 721 demuxer->audio->id=1; |
3502 | 722 nosound: |
2687 | 723 } |
2695 | 724 } |
2687 | 725 |
726 } | |
727 | |
2788 | 728 void demux_close_vivo(demuxer_t *demuxer) |
729 { | |
730 vivo_priv_t* priv=demuxer->priv; | |
2809 | 731 |
732 if (priv) { | |
733 if (priv->title) | |
734 free(priv->title); | |
735 if (priv->author) | |
736 free(priv->author); | |
737 if (priv->copyright) | |
738 free(priv->copyright); | |
739 if (priv->producer) | |
740 free(priv->producer); | |
2788 | 741 free(priv); |
2809 | 742 } |
2788 | 743 return; |
744 } |