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 #include "libavcodec/avcodec.h"
20 #define FF_INPUT_BUFFER_PADDING_SIZE 8
24 int vivo_param_version
= -1;
25 char *vivo_param_acodec
= NULL
;
26 int vivo_param_abitrate
= -1;
27 int vivo_param_samplerate
= -1;
28 int vivo_param_bytesperblock
= -1;
29 int vivo_param_width
= -1;
30 int vivo_param_height
= -1;
31 int vivo_param_vformat
= -1;
33 /* VIVO audio standards from vivog723.acm:
38 SamplesPerSec = 8000 - 8khz
40 BlockAlign (bytes per block) = 24
46 SamplesPerSec = 16000 - 16khz
48 BlockAlign (bytes per block) = 40
52 //enum { VIVO_AUDIO_G723, VIVO_AUDIO_SIREN };
54 #define VIVO_AUDIO_G723 1
55 #define VIVO_AUDIO_SIREN 2
76 int audio_bytesperblock
;
79 /* parse all possible extra headers */
80 /* (audio headers are separate - mostly with recordtype=3 or 4) */
81 #define TEXTPARSE_ALL 1
83 static void vivo_parse_text_header(demuxer_t
*demux
, int header_len
)
85 vivo_priv_t
* priv
= demux
->priv
;
90 int parser_in_audio_block
= 0;
94 priv
= malloc(sizeof(vivo_priv_t
));
95 memset(priv
, 0, sizeof(vivo_priv_t
));
100 buf
= malloc(header_len
);
101 opt
= malloc(header_len
);
102 param
= malloc(header_len
);
103 stream_read(demux
->stream
, buf
, header_len
);
105 while(i
<header_len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
107 token
= strtok(buf
, (char *)&("\x0d\x0a"));
108 while (token
&& (header_len
>2))
110 header_len
-= strlen(token
)+2;
111 if (sscanf(token
, "%[^:]:%[^\n]", opt
, param
) != 2)
113 mp_msg(MSGT_DEMUX
, MSGL_V
, "viv_text_header_parser: bad line: '%s' at ~%#"PRIx64
"\n",
114 token
, (int64_t)stream_tell(demux
->stream
));
117 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token: '%s' (%d bytes/%d bytes left)\n",
118 token
, strlen(token
), header_len
);
119 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token => o: '%s', p: '%s'\n",
122 /* checking versions: only v1 or v2 is suitable (or known?:) */
123 if (!strcmp(opt
, "Version"))
125 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Version: %s\n", param
);
126 if (!strncmp(param
, "Vivo/1", 6) || !strncmp(param
, "Vivo/2", 6))
129 /* save major version for fourcc */
130 priv
->version
= param
[5];
135 if (!strcmp(opt
, "FPS"))
137 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "FPS: %f\n", atof(param
));
138 priv
->fps
= atof(param
);
140 if (!strcmp(opt
, "Width"))
142 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Width: %d\n", atoi(param
));
143 priv
->width
= atoi(param
);
145 if (!strcmp(opt
, "Height"))
147 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Height: %d\n", atoi(param
));
148 priv
->height
= atoi(param
);
150 if (!strcmp(opt
, "DisplayWidth"))
152 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Width: %d\n", atoi(param
));
153 priv
->disp_width
= atoi(param
);
155 if (!strcmp(opt
, "DisplayHeight"))
157 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Height: %d\n", atoi(param
));
158 priv
->disp_height
= atoi(param
);
162 if (!strcmp(opt
, "RecordType"))
164 /* no audio recordblock by Vivo/1.00, 3 and 4 by Vivo/2.00 */
165 if ((atoi(param
) == 3) || (atoi(param
) == 4))
166 parser_in_audio_block
= 1;
168 parser_in_audio_block
= 0;
170 if (!strcmp(opt
, "NominalBitrate"))
172 priv
->audio_bitrate
= atoi(param
);
173 if (priv
->audio_bitrate
== 2000)
174 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
175 if (priv
->audio_bitrate
== 800)
176 priv
->audio_codec
= VIVO_AUDIO_G723
;
178 if (!strcmp(opt
, "SamplingFrequency"))
180 priv
->audio_samplerate
= atoi(param
);
181 if (priv
->audio_samplerate
== 16000)
182 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
183 if (priv
->audio_samplerate
== 8000)
184 priv
->audio_codec
= VIVO_AUDIO_G723
;
186 if (!strcmp(opt
, "Length") && (parser_in_audio_block
== 1))
188 priv
->audio_bytesperblock
= atoi(param
); /* 24 or 40 kbps */
189 if (priv
->audio_bytesperblock
== 40)
190 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
191 if (priv
->audio_bytesperblock
== 24)
192 priv
->audio_codec
= VIVO_AUDIO_G723
;
195 /* only for displaying some informations about movie*/
196 if (!strcmp(opt
, "Title"))
198 demux_info_add(demux
, "name", param
);
199 priv
->title
= strdup(param
);
201 if (!strcmp(opt
, "Author"))
203 demux_info_add(demux
, "author", param
);
204 priv
->author
= strdup(param
);
206 if (!strcmp(opt
, "Copyright"))
208 demux_info_add(demux
, "copyright", param
);
209 priv
->copyright
= strdup(param
);
211 if (!strcmp(opt
, "Producer"))
213 demux_info_add(demux
, "encoder", param
);
214 priv
->producer
= strdup(param
);
218 token
= strtok(NULL
, (char *)&("\x0d\x0a"));
229 static int vivo_check_file(demuxer_t
* demuxer
){
233 unsigned char buf
[2048+256];
235 int orig_pos
= stream_tell(demuxer
->stream
);
237 mp_msg(MSGT_DEMUX
,MSGL_V
,"Checking for VIVO\n");
239 c
=stream_read_char(demuxer
->stream
);
240 if(c
==-256) return 0;
242 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
244 if(len
>1024) return 0;
247 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 1 size: %d\n",len
);
248 //stream_skip(demuxer->stream,len);
250 priv
=malloc(sizeof(vivo_priv_t
));
251 memset(priv
,0,sizeof(vivo_priv_t
));
255 vivo_parse_text_header(demuxer
, len
);
256 if (priv
->supported
== 0)
259 /* this is enought for check (for now) */
260 stream_read(demuxer
->stream
,buf
,len
);
262 // printf("VIVO header: '%s'\n",buf);
266 while(i
<len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
267 if(strncmp(buf
+i
,"Version:Vivo/",13)) return 0; // bad version/type!
271 c
=stream_read_char(demuxer
->stream
);
274 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
276 if(len
+len2
>2048) return 0;
279 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 2 size: %d\n",len2
);
280 stream_skip(demuxer
->stream
,len2
);
281 // stream_read(demuxer->stream,buf+len,len2);
284 // c=stream_read_char(demuxer->stream);
285 // printf("first packet: %02X\n",c);
287 stream_seek(demuxer
->stream
, orig_pos
);
289 return DEMUXER_TYPE_VIVO
;
292 static int audio_pos
=0;
293 static int audio_rate
=0;
296 // 0 = EOF or no stream found
297 // 1 = successfully read a packet
298 static int demux_vivo_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
299 demux_stream_t
*ds
=NULL
;
304 demux
->filepos
=stream_tell(demux
->stream
);
306 c
=stream_read_char(demux
->stream
);
307 if (c
== -256) /* EOF */
309 // printf("c=%x,%02X\n",c,c&0xf0);
312 /* ok, this works, but pts calculating from header is required! */
313 #warning "Calculate PTS from picture header!"
315 c
= stream_read_char(demux
->stream
);
316 mp_msg(MSGT_DEMUX
, MSGL_V
, "packet 0x82(pos=%u) chunk=%x\n",
317 (int)stream_tell(demux
->stream
), c
);
320 case 0x00: // header - skip it!
322 len
=stream_read_char(demux
->stream
);
323 if(len
>=0x80) len
=0x80*(len
-0x80)+stream_read_char(demux
->stream
);
324 mp_msg(MSGT_DEMUX
, MSGL_V
, "vivo extra header: %d bytes\n",len
);
328 /* also try to parse all headers */
329 pos
= stream_tell(demux
->stream
);
330 vivo_parse_text_header(demux
, len
);
331 stream_seek(demux
->stream
, pos
);
336 case 0x10: // video packet
338 len
= stream_read_char(demux
->stream
);
343 case 0x20: // video packet
344 len
=stream_read_char(demux
->stream
);
347 case 0x30: // audio packet
349 len
= stream_read_char(demux
->stream
);
355 case 0x40: // audio packet
357 len
= stream_read_char(demux
->stream
);
364 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"VIVO - unknown ID found: %02X at pos %"PRIu64
" contact author!\n",
365 c
, (int64_t)stream_tell(demux
->stream
));
369 // printf("chunk=%x, len=%d\n", c, len);
371 if(!ds
|| ds
->id
<-1){
372 if(len
) stream_skip(demux
->stream
,len
);
379 if(ds
->asf_seq
!=seq
){
380 // closed segment, finalize packet:
381 ds_add_packet(ds
,ds
->asf_packet
);
383 // printf("packet!\n");
385 // append data to it!
386 demux_packet_t
* dp
=ds
->asf_packet
;
387 if(dp
->len
+ len
+ FF_INPUT_BUFFER_PADDING_SIZE
< 0)
389 dp
->buffer
=realloc(dp
->buffer
,dp
->len
+len
+FF_INPUT_BUFFER_PADDING_SIZE
);
390 memset(dp
->buffer
+dp
->len
+len
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
391 //memcpy(dp->buffer+dp->len,data,len);
392 stream_read(demux
->stream
,dp
->buffer
+dp
->len
,len
);
393 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"data appended! %d+%d\n",dp
->len
,len
);
396 if((c
&0xF0)==0x20) --ds
->asf_seq
; // hack!
400 // create new packet:
401 { demux_packet_t
* dp
;
402 dp
=new_demux_packet(len
);
403 //memcpy(dp->buffer,data,len);
404 stream_read(demux
->stream
,dp
->buffer
,len
);
405 dp
->pts
=audio_rate
?((float)audio_pos
/(float)audio_rate
):0;
406 // dp->flags=keyframe;
407 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur);
408 dp
->pos
=demux
->filepos
;
417 static const short h263_format
[8][2] = {
424 { 320, 240 } // ??????? or 240x180 (found in vivo2) ?
427 static unsigned char* buffer
;
430 static unsigned char buf
=0;
431 static int format
, width
, height
;
433 static unsigned int x_get_bits(int n
){
438 buf
=buffer
[bufptr
++];
441 //x=(x<<1)|(buf&1);buf>>=1;
442 x
=(x
<<1)|(buf
>>7);buf
<<=1;
448 #define get_bits(xxx,n) x_get_bits(n)
449 #define get_bits1(xxx) x_get_bits(1)
450 #define skip_bits(xxx,n) x_get_bits(n)
451 #define skip_bits1(xxx) x_get_bits(1)
453 /* most is hardcoded. should extend to handle all h263 streams */
454 static int h263_decode_picture_header(unsigned char *b_ptr
)
458 // for(i=0;i<16;i++) printf(" %02X",b_ptr[i]); printf("\n");
464 if (get_bits(&s
->gb
, 22) != 0x20){
465 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad picture header\n");
468 skip_bits(&s
->gb
, 8); /* picture timestamp */
470 if (get_bits1(&s
->gb
) != 1){
471 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad marker\n");
472 return -1; /* marker */
474 if (get_bits1(&s
->gb
) != 0){
475 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad h263 id\n");
476 return -1; /* h263 id */
478 skip_bits1(&s
->gb
); /* split screen off */
479 skip_bits1(&s
->gb
); /* camera off */
480 skip_bits1(&s
->gb
); /* freeze picture release off */
482 format
= get_bits(&s
->gb
, 3);
485 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 0 format = %d\n", format
);
487 width
= h263_format
[format
][0];
488 height
= h263_format
[format
][1];
489 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
490 // if (!width) return -1;
492 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits1(&s
->gb
));
493 mp_msg(MSGT_DEMUX
, MSGL_V
, "unrestricted_mv=%d\n", get_bits1(&s
->gb
));
495 mp_msg(MSGT_DEMUX
, MSGL_V
, "SAC: %d\n", get_bits1(&s
->gb
));
496 mp_msg(MSGT_DEMUX
, MSGL_V
, "advanced prediction mode: %d\n", get_bits1(&s
->gb
));
497 mp_msg(MSGT_DEMUX
, MSGL_V
, "PB frame: %d\n", get_bits1(&s
->gb
));
499 if (get_bits1(&s
->gb
) != 0)
500 return -1; /* SAC: off */
501 if (get_bits1(&s
->gb
) != 0)
502 return -1; /* advanced prediction mode: off */
503 if (get_bits1(&s
->gb
) != 0)
504 return -1; /* not PB frame */
506 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
507 skip_bits1(&s
->gb
); /* Continuous Presence Multipoint mode: off */
509 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 1\n");
511 if (get_bits(&s
->gb
, 3) != 1){
512 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "H.263v2 A error\n");
515 if (get_bits(&s
->gb
, 3) != 6){ /* custom source format */
516 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "custom source format\n");
519 skip_bits(&s
->gb
, 12);
520 skip_bits(&s
->gb
, 3);
521 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits(&s
->gb
, 3) + 1);
522 // if (s->pict_type != I_TYPE &&
523 // s->pict_type != P_TYPE)
525 skip_bits(&s
->gb
, 7);
526 skip_bits(&s
->gb
, 4); /* aspect ratio */
527 width
= (get_bits(&s
->gb
, 9) + 1) * 4;
529 height
= get_bits(&s
->gb
, 9) * 4;
530 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
533 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
537 while (get_bits1(&s
->gb
) != 0) {
538 skip_bits(&s
->gb
, 8);
542 // s->height = height;
548 static demuxer_t
* demux_open_vivo(demuxer_t
* demuxer
){
549 vivo_priv_t
* priv
=demuxer
->priv
;
551 if(!ds_fill_buffer(demuxer
->video
)){
552 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"VIVO: " MSGTR_MissingVideoStreamBug
);
558 h263_decode_picture_header(demuxer
->video
->buffer
);
560 if (vivo_param_version
!= -1)
561 priv
->version
= '0' + vivo_param_version
;
563 { sh_video_t
* sh
=new_sh_video(demuxer
,0);
565 /* viv1, viv2 (for better codecs.conf) */
566 sh
->format
= mmioFOURCC('v', 'i', 'v', priv
->version
);
574 sh
->frametime
=1.0f
/sh
->fps
;
576 /* XXX: FIXME: can't scale image. */
577 /* hotfix to disable: */
578 priv
->disp_width
= priv
->width
;
579 priv
->disp_height
= priv
->height
;
581 if (vivo_param_width
!= -1)
582 priv
->disp_width
= priv
->width
= vivo_param_width
;
584 if (vivo_param_height
!= -1)
585 priv
->disp_height
= priv
->height
= vivo_param_height
;
587 if (vivo_param_vformat
!= -1)
589 priv
->disp_width
= priv
->width
= h263_format
[vivo_param_vformat
][0];
590 priv
->disp_height
= priv
->height
= h263_format
[vivo_param_vformat
][1];
593 if (priv
->disp_width
)
594 sh
->disp_w
= priv
->disp_width
;
597 if (priv
->disp_height
)
598 sh
->disp_h
= priv
->disp_height
;
602 // emulate BITMAPINFOHEADER:
603 sh
->bih
=malloc(sizeof(BITMAPINFOHEADER
));
604 memset(sh
->bih
,0,sizeof(BITMAPINFOHEADER
));
607 sh
->bih
->biWidth
= priv
->width
;
609 sh
->bih
->biWidth
= width
;
611 sh
->bih
->biHeight
= priv
->height
;
613 sh
->bih
->biHeight
= height
;
615 sh
->bih
->biBitCount
=24;
616 sh
->bih
->biCompression
=sh
->format
;
617 sh
->bih
->biSizeImage
=sh
->bih
->biWidth
*sh
->bih
->biHeight
*3;
619 /* insert as stream */
620 demuxer
->video
->sh
=sh
;
621 sh
->ds
=demuxer
->video
;
622 demuxer
->video
->id
=0;
624 /* disable seeking */
625 demuxer
->seekable
= 0;
627 mp_msg(MSGT_DEMUX
,MSGL_V
,"VIVO Video stream %d size: display: %dx%d, codec: %ux%u\n",
628 demuxer
->video
->id
, sh
->disp_w
, sh
->disp_h
, sh
->bih
->biWidth
,
633 if (demuxer
->audio
->id
>= -1){
634 if(!ds_fill_buffer(demuxer
->audio
)){
635 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"VIVO: " MSGTR_MissingAudioStream
);
637 { sh_audio_t
* sh
=new_sh_audio(demuxer
,1);
639 /* Select audio codec */
640 if (priv
->audio_codec
== 0)
642 if (priv
->version
== '2')
643 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
645 priv
->audio_codec
= VIVO_AUDIO_G723
;
647 if (vivo_param_acodec
!= NULL
)
649 if (!strcasecmp(vivo_param_acodec
, "g723"))
650 priv
->audio_codec
= VIVO_AUDIO_G723
;
651 if (!strcasecmp(vivo_param_acodec
, "siren"))
652 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
655 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
657 else if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
661 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "VIVO: Not support audio codec (%d)\n",
663 free_sh_audio(demuxer
, 1);
667 // Emulate WAVEFORMATEX struct:
668 sh
->wf
=malloc(sizeof(WAVEFORMATEX
));
669 memset(sh
->wf
,0,sizeof(WAVEFORMATEX
));
670 sh
->wf
->wFormatTag
=sh
->format
;
671 sh
->wf
->nChannels
=1; /* 1 channels for both Siren and G.723 */
673 /* Set bits per sample */
674 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
675 sh
->wf
->wBitsPerSample
= 16;
677 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
678 sh
->wf
->wBitsPerSample
= 8;
680 /* Set sampling rate */
681 if (priv
->audio_samplerate
) /* got from header */
682 sh
->wf
->nSamplesPerSec
= priv
->audio_samplerate
;
685 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
686 sh
->wf
->nSamplesPerSec
= 16000;
687 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
688 sh
->wf
->nSamplesPerSec
= 8000;
690 if (vivo_param_samplerate
!= -1)
691 sh
->wf
->nSamplesPerSec
= vivo_param_samplerate
;
693 /* Set audio bitrate */
694 if (priv
->audio_bitrate
) /* got from header */
695 sh
->wf
->nAvgBytesPerSec
= priv
->audio_bitrate
;
698 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
699 sh
->wf
->nAvgBytesPerSec
= 2000;
700 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
701 sh
->wf
->nAvgBytesPerSec
= 800;
703 if (vivo_param_abitrate
!= -1)
704 sh
->wf
->nAvgBytesPerSec
= vivo_param_abitrate
;
705 audio_rate
=sh
->wf
->nAvgBytesPerSec
;
707 if (!priv
->audio_bytesperblock
)
709 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
710 sh
->wf
->nBlockAlign
= 40;
711 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
712 sh
->wf
->nBlockAlign
= 24;
715 sh
->wf
->nBlockAlign
= priv
->audio_bytesperblock
;
716 if (vivo_param_bytesperblock
!= -1)
717 sh
->wf
->nBlockAlign
= vivo_param_bytesperblock
;
720 /* insert as stream */
721 demuxer
->audio
->sh
=sh
;
722 sh
->ds
=demuxer
->audio
;
723 demuxer
->audio
->id
=1;
731 static void demux_close_vivo(demuxer_t
*demuxer
)
733 vivo_priv_t
* priv
=demuxer
->priv
;
741 free(priv
->copyright
);
743 free(priv
->producer
);
750 const demuxer_desc_t demuxer_desc_vivo
= {
754 "A'rpi, Alex Beregszasi",
757 0, // unsafe autodetect
759 demux_vivo_fill_buffer
,