1 // VIVO file parser by A'rpi
2 // VIVO text header parser and audio support by alex
7 #include <string.h> /* strtok */
18 #ifdef USE_LIBAVCODEC_SO
19 #include <ffmpeg/avcodec.h>
20 #elif defined(USE_LIBAVCODEC)
21 #include "libavcodec/avcodec.h"
23 #define FF_INPUT_BUFFER_PADDING_SIZE 8
27 int vivo_param_version
= -1;
28 char *vivo_param_acodec
= NULL
;
29 int vivo_param_abitrate
= -1;
30 int vivo_param_samplerate
= -1;
31 int vivo_param_bytesperblock
= -1;
32 int vivo_param_width
= -1;
33 int vivo_param_height
= -1;
34 int vivo_param_vformat
= -1;
36 /* VIVO audio standards from vivog723.acm:
41 SamplesPerSec = 8000 - 8khz
43 BlockAlign (bytes per block) = 24
49 SamplesPerSec = 16000 - 16khz
51 BlockAlign (bytes per block) = 40
55 //enum { VIVO_AUDIO_G723, VIVO_AUDIO_SIREN };
57 #define VIVO_AUDIO_G723 1
58 #define VIVO_AUDIO_SIREN 2
79 int audio_bytesperblock
;
82 /* parse all possible extra headers */
83 /* (audio headers are seperate - mostly with recordtype=3 or 4) */
84 #define TEXTPARSE_ALL 1
86 static void vivo_parse_text_header(demuxer_t
*demux
, int header_len
)
88 vivo_priv_t
* priv
= demux
->priv
;
93 int parser_in_audio_block
= 0;
97 priv
= malloc(sizeof(vivo_priv_t
));
98 memset(priv
, 0, sizeof(vivo_priv_t
));
103 buf
= malloc(header_len
);
104 opt
= malloc(header_len
);
105 param
= malloc(header_len
);
106 stream_read(demux
->stream
, buf
, header_len
);
108 while(i
<header_len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
110 token
= strtok(buf
, (char *)&("\x0d\x0a"));
111 while (token
&& (header_len
>2))
113 header_len
-= strlen(token
)+2;
114 if (sscanf(token
, "%[^:]:%[^\n]", opt
, param
) != 2)
116 mp_msg(MSGT_DEMUX
, MSGL_V
, "viv_text_header_parser: bad line: '%s' at ~%#"PRIx64
"\n",
117 token
, (int64_t)stream_tell(demux
->stream
));
120 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token: '%s' (%d bytes/%d bytes left)\n",
121 token
, strlen(token
), header_len
);
122 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token => o: '%s', p: '%s'\n",
125 /* checking versions: only v1 or v2 is suitable (or known?:) */
126 if (!strcmp(opt
, "Version"))
128 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Version: %s\n", param
);
129 if (!strncmp(param
, "Vivo/1", 6) || !strncmp(param
, "Vivo/2", 6))
132 /* save major version for fourcc */
133 priv
->version
= param
[5];
138 if (!strcmp(opt
, "FPS"))
140 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "FPS: %f\n", atof(param
));
141 priv
->fps
= atof(param
);
143 if (!strcmp(opt
, "Width"))
145 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Width: %d\n", atoi(param
));
146 priv
->width
= atoi(param
);
148 if (!strcmp(opt
, "Height"))
150 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Height: %d\n", atoi(param
));
151 priv
->height
= atoi(param
);
153 if (!strcmp(opt
, "DisplayWidth"))
155 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Width: %d\n", atoi(param
));
156 priv
->disp_width
= atoi(param
);
158 if (!strcmp(opt
, "DisplayHeight"))
160 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Height: %d\n", atoi(param
));
161 priv
->disp_height
= atoi(param
);
165 if (!strcmp(opt
, "RecordType"))
167 /* no audio recordblock by Vivo/1.00, 3 and 4 by Vivo/2.00 */
168 if ((atoi(param
) == 3) || (atoi(param
) == 4))
169 parser_in_audio_block
= 1;
171 parser_in_audio_block
= 0;
173 if (!strcmp(opt
, "NominalBitrate"))
175 priv
->audio_bitrate
= atoi(param
);
176 if (priv
->audio_bitrate
== 2000)
177 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
178 if (priv
->audio_bitrate
== 800)
179 priv
->audio_codec
= VIVO_AUDIO_G723
;
181 if (!strcmp(opt
, "SamplingFrequency"))
183 priv
->audio_samplerate
= atoi(param
);
184 if (priv
->audio_samplerate
== 16000)
185 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
186 if (priv
->audio_samplerate
== 8000)
187 priv
->audio_codec
= VIVO_AUDIO_G723
;
189 if (!strcmp(opt
, "Length") && (parser_in_audio_block
== 1))
191 priv
->audio_bytesperblock
= atoi(param
); /* 24 or 40 kbps */
192 if (priv
->audio_bytesperblock
== 40)
193 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
194 if (priv
->audio_bytesperblock
== 24)
195 priv
->audio_codec
= VIVO_AUDIO_G723
;
198 /* only for displaying some informations about movie*/
199 if (!strcmp(opt
, "Title"))
201 demux_info_add(demux
, "name", param
);
202 priv
->title
= strdup(param
);
204 if (!strcmp(opt
, "Author"))
206 demux_info_add(demux
, "author", param
);
207 priv
->author
= strdup(param
);
209 if (!strcmp(opt
, "Copyright"))
211 demux_info_add(demux
, "copyright", param
);
212 priv
->copyright
= strdup(param
);
214 if (!strcmp(opt
, "Producer"))
216 demux_info_add(demux
, "encoder", param
);
217 priv
->producer
= strdup(param
);
221 token
= strtok(NULL
, (char *)&("\x0d\x0a"));
232 static int vivo_check_file(demuxer_t
* demuxer
){
236 unsigned char buf
[2048+256];
238 int orig_pos
= stream_tell(demuxer
->stream
);
240 mp_msg(MSGT_DEMUX
,MSGL_V
,"Checking for VIVO\n");
242 c
=stream_read_char(demuxer
->stream
);
243 if(c
==-256) return 0;
245 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
247 if(len
>1024) return 0;
250 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 1 size: %d\n",len
);
251 //stream_skip(demuxer->stream,len);
253 priv
=malloc(sizeof(vivo_priv_t
));
254 memset(priv
,0,sizeof(vivo_priv_t
));
258 vivo_parse_text_header(demuxer
, len
);
259 if (priv
->supported
== 0)
262 /* this is enought for check (for now) */
263 stream_read(demuxer
->stream
,buf
,len
);
265 // printf("VIVO header: '%s'\n",buf);
269 while(i
<len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
270 if(strncmp(buf
+i
,"Version:Vivo/",13)) return 0; // bad version/type!
274 c
=stream_read_char(demuxer
->stream
);
277 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
279 if(len
+len2
>2048) return 0;
282 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 2 size: %d\n",len2
);
283 stream_skip(demuxer
->stream
,len2
);
284 // stream_read(demuxer->stream,buf+len,len2);
287 // c=stream_read_char(demuxer->stream);
288 // printf("first packet: %02X\n",c);
290 stream_seek(demuxer
->stream
, orig_pos
);
292 return DEMUXER_TYPE_VIVO
;
295 static int audio_pos
=0;
296 static int audio_rate
=0;
299 // 0 = EOF or no stream found
300 // 1 = successfully read a packet
301 static int demux_vivo_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
302 demux_stream_t
*ds
=NULL
;
307 demux
->filepos
=stream_tell(demux
->stream
);
309 c
=stream_read_char(demux
->stream
);
310 if (c
== -256) /* EOF */
312 // printf("c=%x,%02X\n",c,c&0xf0);
315 /* ok, this works, but pts calculating from header is required! */
316 #warning "Calculate PTS from picture header!"
318 c
= stream_read_char(demux
->stream
);
319 printf("packet 0x82(pos=%u) chunk=%x\n",
320 (int)stream_tell(demux
->stream
), c
);
323 case 0x00: // header - skip it!
325 len
=stream_read_char(demux
->stream
);
326 if(len
>=0x80) len
=0x80*(len
-0x80)+stream_read_char(demux
->stream
);
327 printf("vivo extra header: %d bytes\n",len
);
331 /* also try to parse all headers */
332 pos
= stream_tell(demux
->stream
);
333 vivo_parse_text_header(demux
, len
);
334 stream_seek(demux
->stream
, pos
);
339 case 0x10: // video packet
341 len
= stream_read_char(demux
->stream
);
346 case 0x20: // video packet
347 len
=stream_read_char(demux
->stream
);
350 case 0x30: // audio packet
352 len
= stream_read_char(demux
->stream
);
358 case 0x40: // audio packet
360 len
= stream_read_char(demux
->stream
);
367 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"VIVO - unknown ID found: %02X at pos %"PRIu64
" contact author!\n",
368 c
, (int64_t)stream_tell(demux
->stream
));
372 // printf("chunk=%x, len=%d\n", c, len);
374 if(!ds
|| ds
->id
<-1){
375 if(len
) stream_skip(demux
->stream
,len
);
382 if(ds
->asf_seq
!=seq
){
383 // closed segment, finalize packet:
384 ds_add_packet(ds
,ds
->asf_packet
);
386 // printf("packet!\n");
388 // append data to it!
389 demux_packet_t
* dp
=ds
->asf_packet
;
390 dp
->buffer
=realloc(dp
->buffer
,dp
->len
+len
+FF_INPUT_BUFFER_PADDING_SIZE
);
391 memset(dp
->buffer
+dp
->len
+len
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
392 //memcpy(dp->buffer+dp->len,data,len);
393 stream_read(demux
->stream
,dp
->buffer
+dp
->len
,len
);
394 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"data appended! %d+%d\n",dp
->len
,len
);
397 if((c
&0xF0)==0x20) --ds
->asf_seq
; // hack!
401 // create new packet:
402 { demux_packet_t
* dp
;
403 dp
=new_demux_packet(len
);
404 //memcpy(dp->buffer,data,len);
405 stream_read(demux
->stream
,dp
->buffer
,len
);
406 dp
->pts
=audio_rate
?((float)audio_pos
/(float)audio_rate
):0;
407 // dp->flags=keyframe;
408 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur);
409 dp
->pos
=demux
->filepos
;
418 static const short h263_format
[8][2] = {
425 { 320, 240 } // ??????? or 240x180 (found in vivo2) ?
428 static unsigned char* buffer
;
431 static unsigned char buf
=0;
432 static int format
, width
, height
;
434 static unsigned int x_get_bits(int n
){
439 buf
=buffer
[bufptr
++];
442 //x=(x<<1)|(buf&1);buf>>=1;
443 x
=(x
<<1)|(buf
>>7);buf
<<=1;
449 #define get_bits(xxx,n) x_get_bits(n)
450 #define get_bits1(xxx) x_get_bits(1)
451 #define skip_bits(xxx,n) x_get_bits(n)
452 #define skip_bits1(xxx) x_get_bits(1)
454 /* most is hardcoded. should extend to handle all h263 streams */
455 static int h263_decode_picture_header(unsigned char *b_ptr
)
459 // for(i=0;i<16;i++) printf(" %02X",b_ptr[i]); printf("\n");
465 if (get_bits(&s
->gb
, 22) != 0x20){
466 printf("bad picture header\n");
469 skip_bits(&s
->gb
, 8); /* picture timestamp */
471 if (get_bits1(&s
->gb
) != 1){
472 printf("bad marker\n");
473 return -1; /* marker */
475 if (get_bits1(&s
->gb
) != 0){
476 printf("bad h263 id\n");
477 return -1; /* h263 id */
479 skip_bits1(&s
->gb
); /* split screen off */
480 skip_bits1(&s
->gb
); /* camera off */
481 skip_bits1(&s
->gb
); /* freeze picture release off */
483 format
= get_bits(&s
->gb
, 3);
486 printf("h263_plus = 0 format = %d\n",format
);
488 width
= h263_format
[format
][0];
489 height
= h263_format
[format
][1];
490 printf("%d x %d\n",width
,height
);
491 // if (!width) return -1;
493 printf("pict_type=%d\n",get_bits1(&s
->gb
));
494 printf("unrestricted_mv=%d\n",get_bits1(&s
->gb
));
496 printf("SAC: %d\n",get_bits1(&s
->gb
));
497 printf("advanced prediction mode: %d\n",get_bits1(&s
->gb
));
498 printf("PB frame: %d\n",get_bits1(&s
->gb
));
500 if (get_bits1(&s
->gb
) != 0)
501 return -1; /* SAC: off */
502 if (get_bits1(&s
->gb
) != 0)
503 return -1; /* advanced prediction mode: off */
504 if (get_bits1(&s
->gb
) != 0)
505 return -1; /* not PB frame */
507 printf("qscale=%d\n",get_bits(&s
->gb
, 5));
508 skip_bits1(&s
->gb
); /* Continuous Presence Multipoint mode: off */
510 printf("h263_plus = 1\n");
512 if (get_bits(&s
->gb
, 3) != 1){
513 printf("H.263v2 A error\n");
516 if (get_bits(&s
->gb
, 3) != 6){ /* custom source format */
517 printf("custom source format\n");
520 skip_bits(&s
->gb
, 12);
521 skip_bits(&s
->gb
, 3);
522 printf("pict_type=%d\n",get_bits(&s
->gb
, 3) + 1);
523 // if (s->pict_type != I_TYPE &&
524 // s->pict_type != P_TYPE)
526 skip_bits(&s
->gb
, 7);
527 skip_bits(&s
->gb
, 4); /* aspect ratio */
528 width
= (get_bits(&s
->gb
, 9) + 1) * 4;
530 height
= get_bits(&s
->gb
, 9) * 4;
531 printf("%d x %d\n",width
,height
);
534 printf("qscale=%d\n",get_bits(&s
->gb
, 5));
538 while (get_bits1(&s
->gb
) != 0) {
539 skip_bits(&s
->gb
, 8);
543 // s->height = height;
549 static demuxer_t
* demux_open_vivo(demuxer_t
* demuxer
){
550 vivo_priv_t
* priv
=demuxer
->priv
;
552 if(!ds_fill_buffer(demuxer
->video
)){
553 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"VIVO: " MSGTR_MissingVideoStreamBug
);
559 h263_decode_picture_header(demuxer
->video
->buffer
);
561 if (vivo_param_version
!= -1)
562 priv
->version
= '0' + vivo_param_version
;
564 { sh_video_t
* sh
=new_sh_video(demuxer
,0);
566 /* viv1, viv2 (for better codecs.conf) */
567 sh
->format
= mmioFOURCC('v', 'i', 'v', priv
->version
);
575 sh
->frametime
=1.0f
/sh
->fps
;
577 /* XXX: FIXME: can't scale image. */
578 /* hotfix to disable: */
579 priv
->disp_width
= priv
->width
;
580 priv
->disp_height
= priv
->height
;
582 if (vivo_param_width
!= -1)
583 priv
->disp_width
= priv
->width
= vivo_param_width
;
585 if (vivo_param_height
!= -1)
586 priv
->disp_height
= priv
->height
= vivo_param_height
;
588 if (vivo_param_vformat
!= -1)
590 priv
->disp_width
= priv
->width
= h263_format
[vivo_param_vformat
][0];
591 priv
->disp_height
= priv
->height
= h263_format
[vivo_param_vformat
][1];
594 if (priv
->disp_width
)
595 sh
->disp_w
= priv
->disp_width
;
598 if (priv
->disp_height
)
599 sh
->disp_h
= priv
->disp_height
;
603 // emulate BITMAPINFOHEADER:
604 sh
->bih
=malloc(sizeof(BITMAPINFOHEADER
));
605 memset(sh
->bih
,0,sizeof(BITMAPINFOHEADER
));
608 sh
->bih
->biWidth
= priv
->width
;
610 sh
->bih
->biWidth
= width
;
612 sh
->bih
->biHeight
= priv
->height
;
614 sh
->bih
->biHeight
= height
;
616 sh
->bih
->biBitCount
=24;
617 sh
->bih
->biCompression
=sh
->format
;
618 sh
->bih
->biSizeImage
=sh
->bih
->biWidth
*sh
->bih
->biHeight
*3;
620 /* insert as stream */
621 demuxer
->video
->sh
=sh
;
622 sh
->ds
=demuxer
->video
;
623 demuxer
->video
->id
=0;
625 /* disable seeking */
626 demuxer
->seekable
= 0;
628 mp_msg(MSGT_DEMUX
,MSGL_STATUS
,"VIVO Video stream %d size: display: %dx%d, codec: %ux%u\n",
629 demuxer
->video
->id
, sh
->disp_w
, sh
->disp_h
, sh
->bih
->biWidth
,
634 if (demuxer
->audio
->id
>= -1){
635 if(!ds_fill_buffer(demuxer
->audio
)){
636 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"VIVO: " MSGTR_MissingAudioStream
);
638 { sh_audio_t
* sh
=new_sh_audio(demuxer
,1);
640 /* Select audio codec */
641 if (priv
->audio_codec
== 0)
643 if (priv
->version
== '2')
644 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
646 priv
->audio_codec
= VIVO_AUDIO_G723
;
648 if (vivo_param_acodec
!= NULL
)
650 if (!strcasecmp(vivo_param_acodec
, "g723"))
651 priv
->audio_codec
= VIVO_AUDIO_G723
;
652 if (!strcasecmp(vivo_param_acodec
, "siren"))
653 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
656 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
658 else if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
662 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "VIVO: Not support audio codec (%d)\n",
668 // Emulate WAVEFORMATEX struct:
669 sh
->wf
=malloc(sizeof(WAVEFORMATEX
));
670 memset(sh
->wf
,0,sizeof(WAVEFORMATEX
));
671 sh
->wf
->wFormatTag
=sh
->format
;
672 sh
->wf
->nChannels
=1; /* 1 channels for both Siren and G.723 */
674 /* Set bits per sample */
675 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
676 sh
->wf
->wBitsPerSample
= 16;
678 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
679 sh
->wf
->wBitsPerSample
= 8;
681 /* Set sampling rate */
682 if (priv
->audio_samplerate
) /* got from header */
683 sh
->wf
->nSamplesPerSec
= priv
->audio_samplerate
;
686 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
687 sh
->wf
->nSamplesPerSec
= 16000;
688 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
689 sh
->wf
->nSamplesPerSec
= 8000;
691 if (vivo_param_samplerate
!= -1)
692 sh
->wf
->nSamplesPerSec
= vivo_param_samplerate
;
694 /* Set audio bitrate */
695 if (priv
->audio_bitrate
) /* got from header */
696 sh
->wf
->nAvgBytesPerSec
= priv
->audio_bitrate
;
699 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
700 sh
->wf
->nAvgBytesPerSec
= 2000;
701 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
702 sh
->wf
->nAvgBytesPerSec
= 800;
704 if (vivo_param_abitrate
!= -1)
705 sh
->wf
->nAvgBytesPerSec
= vivo_param_abitrate
;
706 audio_rate
=sh
->wf
->nAvgBytesPerSec
;
708 if (!priv
->audio_bytesperblock
)
710 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
711 sh
->wf
->nBlockAlign
= 40;
712 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
713 sh
->wf
->nBlockAlign
= 24;
716 sh
->wf
->nBlockAlign
= priv
->audio_bytesperblock
;
717 if (vivo_param_bytesperblock
!= -1)
718 sh
->wf
->nBlockAlign
= vivo_param_bytesperblock
;
721 /* insert as stream */
722 demuxer
->audio
->sh
=sh
;
723 sh
->ds
=demuxer
->audio
;
724 demuxer
->audio
->id
=1;
732 static void demux_close_vivo(demuxer_t
*demuxer
)
734 vivo_priv_t
* priv
=demuxer
->priv
;
742 free(priv
->copyright
);
744 free(priv
->producer
);
751 demuxer_desc_t demuxer_desc_vivo
= {
755 "A'rpi, Alex Beregszasi",
758 0, // unsafe autodetect
760 demux_vivo_fill_buffer
,