1 // VIVO file parser by A'rpi
2 // VIVO text header parser and audio support by alex
7 #include <string.h> /* strtok */
13 #include "stream/stream.h"
18 int vivo_param_version
= -1;
19 char *vivo_param_acodec
= NULL
;
20 int vivo_param_abitrate
= -1;
21 int vivo_param_samplerate
= -1;
22 int vivo_param_bytesperblock
= -1;
23 int vivo_param_width
= -1;
24 int vivo_param_height
= -1;
25 int vivo_param_vformat
= -1;
27 /* VIVO audio standards from vivog723.acm:
32 SamplesPerSec = 8000 - 8khz
34 BlockAlign (bytes per block) = 24
40 SamplesPerSec = 16000 - 16khz
42 BlockAlign (bytes per block) = 40
46 //enum { VIVO_AUDIO_G723, VIVO_AUDIO_SIREN };
48 #define VIVO_AUDIO_G723 1
49 #define VIVO_AUDIO_SIREN 2
70 int audio_bytesperblock
;
73 /* parse all possible extra headers */
74 /* (audio headers are separate - mostly with recordtype=3 or 4) */
75 #define TEXTPARSE_ALL 1
77 static void vivo_parse_text_header(demuxer_t
*demux
, int header_len
)
79 vivo_priv_t
* priv
= demux
->priv
;
84 int parser_in_audio_block
= 0;
88 priv
= malloc(sizeof(vivo_priv_t
));
89 memset(priv
, 0, sizeof(vivo_priv_t
));
94 buf
= malloc(header_len
);
95 opt
= malloc(header_len
);
96 param
= malloc(header_len
);
97 stream_read(demux
->stream
, buf
, header_len
);
99 while(i
<header_len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
101 token
= strtok(buf
, (char *)&("\x0d\x0a"));
102 while (token
&& (header_len
>2))
104 header_len
-= strlen(token
)+2;
105 if (sscanf(token
, "%[^:]:%[^\n]", opt
, param
) != 2)
107 mp_msg(MSGT_DEMUX
, MSGL_V
, "viv_text_header_parser: bad line: '%s' at ~%#"PRIx64
"\n",
108 token
, (int64_t)stream_tell(demux
->stream
));
111 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token: '%s' (%d bytes/%d bytes left)\n",
112 token
, strlen(token
), header_len
);
113 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token => o: '%s', p: '%s'\n",
116 /* checking versions: only v1 or v2 is suitable (or known?:) */
117 if (!strcmp(opt
, "Version"))
119 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Version: %s\n", param
);
120 if (!strncmp(param
, "Vivo/1", 6) || !strncmp(param
, "Vivo/2", 6))
123 /* save major version for fourcc */
124 priv
->version
= param
[5];
129 if (!strcmp(opt
, "FPS"))
131 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "FPS: %f\n", atof(param
));
132 priv
->fps
= atof(param
);
134 if (!strcmp(opt
, "Width"))
136 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Width: %d\n", atoi(param
));
137 priv
->width
= atoi(param
);
139 if (!strcmp(opt
, "Height"))
141 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Height: %d\n", atoi(param
));
142 priv
->height
= atoi(param
);
144 if (!strcmp(opt
, "DisplayWidth"))
146 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Width: %d\n", atoi(param
));
147 priv
->disp_width
= atoi(param
);
149 if (!strcmp(opt
, "DisplayHeight"))
151 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Height: %d\n", atoi(param
));
152 priv
->disp_height
= atoi(param
);
156 if (!strcmp(opt
, "RecordType"))
158 /* no audio recordblock by Vivo/1.00, 3 and 4 by Vivo/2.00 */
159 if ((atoi(param
) == 3) || (atoi(param
) == 4))
160 parser_in_audio_block
= 1;
162 parser_in_audio_block
= 0;
164 if (!strcmp(opt
, "NominalBitrate"))
166 priv
->audio_bitrate
= atoi(param
);
167 if (priv
->audio_bitrate
== 2000)
168 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
169 if (priv
->audio_bitrate
== 800)
170 priv
->audio_codec
= VIVO_AUDIO_G723
;
172 if (!strcmp(opt
, "SamplingFrequency"))
174 priv
->audio_samplerate
= atoi(param
);
175 if (priv
->audio_samplerate
== 16000)
176 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
177 if (priv
->audio_samplerate
== 8000)
178 priv
->audio_codec
= VIVO_AUDIO_G723
;
180 if (!strcmp(opt
, "Length") && (parser_in_audio_block
== 1))
182 priv
->audio_bytesperblock
= atoi(param
); /* 24 or 40 kbps */
183 if (priv
->audio_bytesperblock
== 40)
184 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
185 if (priv
->audio_bytesperblock
== 24)
186 priv
->audio_codec
= VIVO_AUDIO_G723
;
189 /* only for displaying some informations about movie*/
190 if (!strcmp(opt
, "Title"))
192 demux_info_add(demux
, "name", param
);
193 priv
->title
= strdup(param
);
195 if (!strcmp(opt
, "Author"))
197 demux_info_add(demux
, "author", param
);
198 priv
->author
= strdup(param
);
200 if (!strcmp(opt
, "Copyright"))
202 demux_info_add(demux
, "copyright", param
);
203 priv
->copyright
= strdup(param
);
205 if (!strcmp(opt
, "Producer"))
207 demux_info_add(demux
, "encoder", param
);
208 priv
->producer
= strdup(param
);
212 token
= strtok(NULL
, (char *)&("\x0d\x0a"));
223 static int vivo_check_file(demuxer_t
* demuxer
){
227 unsigned char buf
[2048+256];
229 int orig_pos
= stream_tell(demuxer
->stream
);
231 mp_msg(MSGT_DEMUX
,MSGL_V
,"Checking for VIVO\n");
233 c
=stream_read_char(demuxer
->stream
);
234 if(c
==-256) return 0;
236 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
238 if(len
>1024) return 0;
241 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 1 size: %d\n",len
);
242 //stream_skip(demuxer->stream,len);
244 priv
=malloc(sizeof(vivo_priv_t
));
245 memset(priv
,0,sizeof(vivo_priv_t
));
249 vivo_parse_text_header(demuxer
, len
);
250 if (priv
->supported
== 0)
253 /* this is enought for check (for now) */
254 stream_read(demuxer
->stream
,buf
,len
);
256 // printf("VIVO header: '%s'\n",buf);
260 while(i
<len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
261 if(strncmp(buf
+i
,"Version:Vivo/",13)) return 0; // bad version/type!
265 c
=stream_read_char(demuxer
->stream
);
268 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
270 if(len
+len2
>2048) return 0;
273 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 2 size: %d\n",len2
);
274 stream_skip(demuxer
->stream
,len2
);
275 // stream_read(demuxer->stream,buf+len,len2);
278 // c=stream_read_char(demuxer->stream);
279 // printf("first packet: %02X\n",c);
281 stream_seek(demuxer
->stream
, orig_pos
);
283 return DEMUXER_TYPE_VIVO
;
286 static int audio_pos
=0;
287 static int audio_rate
=0;
290 // 0 = EOF or no stream found
291 // 1 = successfully read a packet
292 static int demux_vivo_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
293 demux_stream_t
*ds
=NULL
;
298 demux
->filepos
=stream_tell(demux
->stream
);
300 c
=stream_read_char(demux
->stream
);
301 if (c
== -256) /* EOF */
303 // printf("c=%x,%02X\n",c,c&0xf0);
306 /* ok, this works, but pts calculating from header is required! */
307 #warning "Calculate PTS from picture header!"
309 c
= stream_read_char(demux
->stream
);
310 mp_msg(MSGT_DEMUX
, MSGL_V
, "packet 0x82(pos=%u) chunk=%x\n",
311 (int)stream_tell(demux
->stream
), c
);
314 case 0x00: // header - skip it!
316 len
=stream_read_char(demux
->stream
);
317 if(len
>=0x80) len
=0x80*(len
-0x80)+stream_read_char(demux
->stream
);
318 mp_msg(MSGT_DEMUX
, MSGL_V
, "vivo extra header: %d bytes\n",len
);
322 /* also try to parse all headers */
323 pos
= stream_tell(demux
->stream
);
324 vivo_parse_text_header(demux
, len
);
325 stream_seek(demux
->stream
, pos
);
330 case 0x10: // video packet
332 len
= stream_read_char(demux
->stream
);
337 case 0x20: // video packet
338 len
=stream_read_char(demux
->stream
);
341 case 0x30: // audio packet
343 len
= stream_read_char(demux
->stream
);
349 case 0x40: // audio packet
351 len
= stream_read_char(demux
->stream
);
358 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"VIVO - unknown ID found: %02X at pos %"PRIu64
" contact author!\n",
359 c
, (int64_t)stream_tell(demux
->stream
));
363 // printf("chunk=%x, len=%d\n", c, len);
365 if(!ds
|| ds
->id
<-1){
366 if(len
) stream_skip(demux
->stream
,len
);
373 if(ds
->asf_seq
!=seq
){
374 // closed segment, finalize packet:
375 ds_add_packet(ds
,ds
->asf_packet
);
377 // printf("packet!\n");
379 // append data to it!
380 demux_packet_t
* dp
=ds
->asf_packet
;
381 if(dp
->len
+ len
+ MP_INPUT_BUFFER_PADDING_SIZE
< 0)
383 dp
->buffer
=realloc(dp
->buffer
,dp
->len
+len
+MP_INPUT_BUFFER_PADDING_SIZE
);
384 memset(dp
->buffer
+dp
->len
+len
, 0, MP_INPUT_BUFFER_PADDING_SIZE
);
385 //memcpy(dp->buffer+dp->len,data,len);
386 stream_read(demux
->stream
,dp
->buffer
+dp
->len
,len
);
387 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"data appended! %d+%d\n",dp
->len
,len
);
390 if((c
&0xF0)==0x20) --ds
->asf_seq
; // hack!
394 // create new packet:
395 { demux_packet_t
* dp
;
396 dp
=new_demux_packet(len
);
397 //memcpy(dp->buffer,data,len);
398 stream_read(demux
->stream
,dp
->buffer
,len
);
399 dp
->pts
=audio_rate
?((float)audio_pos
/(float)audio_rate
):0;
400 // dp->flags=keyframe;
401 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur);
402 dp
->pos
=demux
->filepos
;
411 static const short h263_format
[8][2] = {
418 { 320, 240 } // ??????? or 240x180 (found in vivo2) ?
421 static unsigned char* buffer
;
424 static unsigned char buf
=0;
425 static int format
, width
, height
;
427 static unsigned int x_get_bits(int n
){
432 buf
=buffer
[bufptr
++];
435 //x=(x<<1)|(buf&1);buf>>=1;
436 x
=(x
<<1)|(buf
>>7);buf
<<=1;
442 #define get_bits(xxx,n) x_get_bits(n)
443 #define get_bits1(xxx) x_get_bits(1)
444 #define skip_bits(xxx,n) x_get_bits(n)
445 #define skip_bits1(xxx) x_get_bits(1)
447 /* most is hardcoded. should extend to handle all h263 streams */
448 static int h263_decode_picture_header(unsigned char *b_ptr
)
452 // for(i=0;i<16;i++) printf(" %02X",b_ptr[i]); printf("\n");
458 if (get_bits(&s
->gb
, 22) != 0x20){
459 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad picture header\n");
462 skip_bits(&s
->gb
, 8); /* picture timestamp */
464 if (get_bits1(&s
->gb
) != 1){
465 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad marker\n");
466 return -1; /* marker */
468 if (get_bits1(&s
->gb
) != 0){
469 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad h263 id\n");
470 return -1; /* h263 id */
472 skip_bits1(&s
->gb
); /* split screen off */
473 skip_bits1(&s
->gb
); /* camera off */
474 skip_bits1(&s
->gb
); /* freeze picture release off */
476 format
= get_bits(&s
->gb
, 3);
479 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 0 format = %d\n", format
);
481 width
= h263_format
[format
][0];
482 height
= h263_format
[format
][1];
483 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
484 // if (!width) return -1;
486 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits1(&s
->gb
));
487 mp_msg(MSGT_DEMUX
, MSGL_V
, "unrestricted_mv=%d\n", get_bits1(&s
->gb
));
489 mp_msg(MSGT_DEMUX
, MSGL_V
, "SAC: %d\n", get_bits1(&s
->gb
));
490 mp_msg(MSGT_DEMUX
, MSGL_V
, "advanced prediction mode: %d\n", get_bits1(&s
->gb
));
491 mp_msg(MSGT_DEMUX
, MSGL_V
, "PB frame: %d\n", get_bits1(&s
->gb
));
493 if (get_bits1(&s
->gb
) != 0)
494 return -1; /* SAC: off */
495 if (get_bits1(&s
->gb
) != 0)
496 return -1; /* advanced prediction mode: off */
497 if (get_bits1(&s
->gb
) != 0)
498 return -1; /* not PB frame */
500 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
501 skip_bits1(&s
->gb
); /* Continuous Presence Multipoint mode: off */
503 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 1\n");
505 if (get_bits(&s
->gb
, 3) != 1){
506 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "H.263v2 A error\n");
509 if (get_bits(&s
->gb
, 3) != 6){ /* custom source format */
510 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "custom source format\n");
513 skip_bits(&s
->gb
, 12);
514 skip_bits(&s
->gb
, 3);
515 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits(&s
->gb
, 3) + 1);
516 // if (s->pict_type != I_TYPE &&
517 // s->pict_type != P_TYPE)
519 skip_bits(&s
->gb
, 7);
520 skip_bits(&s
->gb
, 4); /* aspect ratio */
521 width
= (get_bits(&s
->gb
, 9) + 1) * 4;
523 height
= get_bits(&s
->gb
, 9) * 4;
524 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
527 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
531 while (get_bits1(&s
->gb
) != 0) {
532 skip_bits(&s
->gb
, 8);
536 // s->height = height;
542 static demuxer_t
* demux_open_vivo(demuxer_t
* demuxer
){
543 vivo_priv_t
* priv
=demuxer
->priv
;
545 if(!ds_fill_buffer(demuxer
->video
)){
546 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"VIVO: " MSGTR_MissingVideoStreamBug
);
552 h263_decode_picture_header(demuxer
->video
->buffer
);
554 if (vivo_param_version
!= -1)
555 priv
->version
= '0' + vivo_param_version
;
557 { sh_video_t
* sh
=new_sh_video(demuxer
,0);
559 /* viv1, viv2 (for better codecs.conf) */
560 sh
->format
= mmioFOURCC('v', 'i', 'v', priv
->version
);
568 sh
->frametime
=1.0f
/sh
->fps
;
570 /* XXX: FIXME: can't scale image. */
571 /* hotfix to disable: */
572 priv
->disp_width
= priv
->width
;
573 priv
->disp_height
= priv
->height
;
575 if (vivo_param_width
!= -1)
576 priv
->disp_width
= priv
->width
= vivo_param_width
;
578 if (vivo_param_height
!= -1)
579 priv
->disp_height
= priv
->height
= vivo_param_height
;
581 if (vivo_param_vformat
!= -1)
583 priv
->disp_width
= priv
->width
= h263_format
[vivo_param_vformat
][0];
584 priv
->disp_height
= priv
->height
= h263_format
[vivo_param_vformat
][1];
587 if (priv
->disp_width
)
588 sh
->disp_w
= priv
->disp_width
;
591 if (priv
->disp_height
)
592 sh
->disp_h
= priv
->disp_height
;
596 // emulate BITMAPINFOHEADER:
597 sh
->bih
=malloc(sizeof(BITMAPINFOHEADER
));
598 memset(sh
->bih
,0,sizeof(BITMAPINFOHEADER
));
601 sh
->bih
->biWidth
= priv
->width
;
603 sh
->bih
->biWidth
= width
;
605 sh
->bih
->biHeight
= priv
->height
;
607 sh
->bih
->biHeight
= height
;
609 sh
->bih
->biBitCount
=24;
610 sh
->bih
->biCompression
=sh
->format
;
611 sh
->bih
->biSizeImage
=sh
->bih
->biWidth
*sh
->bih
->biHeight
*3;
613 /* insert as stream */
614 demuxer
->video
->sh
=sh
;
615 sh
->ds
=demuxer
->video
;
616 demuxer
->video
->id
=0;
618 /* disable seeking */
619 demuxer
->seekable
= 0;
621 mp_msg(MSGT_DEMUX
,MSGL_V
,"VIVO Video stream %d size: display: %dx%d, codec: %ux%u\n",
622 demuxer
->video
->id
, sh
->disp_w
, sh
->disp_h
, sh
->bih
->biWidth
,
627 if (demuxer
->audio
->id
>= -1){
628 if(!ds_fill_buffer(demuxer
->audio
)){
629 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"VIVO: " MSGTR_MissingAudioStream
);
631 { sh_audio_t
* sh
=new_sh_audio(demuxer
,1);
633 /* Select audio codec */
634 if (priv
->audio_codec
== 0)
636 if (priv
->version
== '2')
637 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
639 priv
->audio_codec
= VIVO_AUDIO_G723
;
641 if (vivo_param_acodec
!= NULL
)
643 if (!strcasecmp(vivo_param_acodec
, "g723"))
644 priv
->audio_codec
= VIVO_AUDIO_G723
;
645 if (!strcasecmp(vivo_param_acodec
, "siren"))
646 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
649 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
651 else if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
655 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "VIVO: Not support audio codec (%d)\n",
657 free_sh_audio(demuxer
, 1);
661 // Emulate WAVEFORMATEX struct:
662 sh
->wf
=malloc(sizeof(WAVEFORMATEX
));
663 memset(sh
->wf
,0,sizeof(WAVEFORMATEX
));
664 sh
->wf
->wFormatTag
=sh
->format
;
665 sh
->wf
->nChannels
=1; /* 1 channels for both Siren and G.723 */
667 /* Set bits per sample */
668 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
669 sh
->wf
->wBitsPerSample
= 16;
671 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
672 sh
->wf
->wBitsPerSample
= 8;
674 /* Set sampling rate */
675 if (priv
->audio_samplerate
) /* got from header */
676 sh
->wf
->nSamplesPerSec
= priv
->audio_samplerate
;
679 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
680 sh
->wf
->nSamplesPerSec
= 16000;
681 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
682 sh
->wf
->nSamplesPerSec
= 8000;
684 if (vivo_param_samplerate
!= -1)
685 sh
->wf
->nSamplesPerSec
= vivo_param_samplerate
;
687 /* Set audio bitrate */
688 if (priv
->audio_bitrate
) /* got from header */
689 sh
->wf
->nAvgBytesPerSec
= priv
->audio_bitrate
;
692 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
693 sh
->wf
->nAvgBytesPerSec
= 2000;
694 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
695 sh
->wf
->nAvgBytesPerSec
= 800;
697 if (vivo_param_abitrate
!= -1)
698 sh
->wf
->nAvgBytesPerSec
= vivo_param_abitrate
;
699 audio_rate
=sh
->wf
->nAvgBytesPerSec
;
701 if (!priv
->audio_bytesperblock
)
703 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
704 sh
->wf
->nBlockAlign
= 40;
705 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
706 sh
->wf
->nBlockAlign
= 24;
709 sh
->wf
->nBlockAlign
= priv
->audio_bytesperblock
;
710 if (vivo_param_bytesperblock
!= -1)
711 sh
->wf
->nBlockAlign
= vivo_param_bytesperblock
;
714 /* insert as stream */
715 demuxer
->audio
->sh
=sh
;
716 sh
->ds
=demuxer
->audio
;
717 demuxer
->audio
->id
=1;
725 static void demux_close_vivo(demuxer_t
*demuxer
)
727 vivo_priv_t
* priv
=demuxer
->priv
;
735 free(priv
->copyright
);
737 free(priv
->producer
);
744 const demuxer_desc_t demuxer_desc_vivo
= {
748 "A'rpi, Alex Beregszasi",
751 0, // unsafe autodetect
753 demux_vivo_fill_buffer
,