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"
17 #ifdef USE_LIBAVCODEC_SO
18 #include <ffmpeg/avcodec.h>
19 #elif defined(USE_LIBAVCODEC)
20 #include "libavcodec/avcodec.h"
22 #define FF_INPUT_BUFFER_PADDING_SIZE 8
26 int vivo_param_version
= -1;
27 char *vivo_param_acodec
= NULL
;
28 int vivo_param_abitrate
= -1;
29 int vivo_param_samplerate
= -1;
30 int vivo_param_bytesperblock
= -1;
31 int vivo_param_width
= -1;
32 int vivo_param_height
= -1;
33 int vivo_param_vformat
= -1;
35 /* VIVO audio standards from vivog723.acm:
40 SamplesPerSec = 8000 - 8khz
42 BlockAlign (bytes per block) = 24
48 SamplesPerSec = 16000 - 16khz
50 BlockAlign (bytes per block) = 40
54 //enum { VIVO_AUDIO_G723, VIVO_AUDIO_SIREN };
56 #define VIVO_AUDIO_G723 1
57 #define VIVO_AUDIO_SIREN 2
78 int audio_bytesperblock
;
81 /* parse all possible extra headers */
82 /* (audio headers are separate - mostly with recordtype=3 or 4) */
83 #define TEXTPARSE_ALL 1
85 static void vivo_parse_text_header(demuxer_t
*demux
, int header_len
)
87 vivo_priv_t
* priv
= demux
->priv
;
92 int parser_in_audio_block
= 0;
96 priv
= malloc(sizeof(vivo_priv_t
));
97 memset(priv
, 0, sizeof(vivo_priv_t
));
102 buf
= malloc(header_len
);
103 opt
= malloc(header_len
);
104 param
= malloc(header_len
);
105 stream_read(demux
->stream
, buf
, header_len
);
107 while(i
<header_len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
109 token
= strtok(buf
, (char *)&("\x0d\x0a"));
110 while (token
&& (header_len
>2))
112 header_len
-= strlen(token
)+2;
113 if (sscanf(token
, "%[^:]:%[^\n]", opt
, param
) != 2)
115 mp_msg(MSGT_DEMUX
, MSGL_V
, "viv_text_header_parser: bad line: '%s' at ~%#"PRIx64
"\n",
116 token
, (int64_t)stream_tell(demux
->stream
));
119 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token: '%s' (%d bytes/%d bytes left)\n",
120 token
, strlen(token
), header_len
);
121 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token => o: '%s', p: '%s'\n",
124 /* checking versions: only v1 or v2 is suitable (or known?:) */
125 if (!strcmp(opt
, "Version"))
127 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Version: %s\n", param
);
128 if (!strncmp(param
, "Vivo/1", 6) || !strncmp(param
, "Vivo/2", 6))
131 /* save major version for fourcc */
132 priv
->version
= param
[5];
137 if (!strcmp(opt
, "FPS"))
139 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "FPS: %f\n", atof(param
));
140 priv
->fps
= atof(param
);
142 if (!strcmp(opt
, "Width"))
144 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Width: %d\n", atoi(param
));
145 priv
->width
= atoi(param
);
147 if (!strcmp(opt
, "Height"))
149 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Height: %d\n", atoi(param
));
150 priv
->height
= atoi(param
);
152 if (!strcmp(opt
, "DisplayWidth"))
154 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Width: %d\n", atoi(param
));
155 priv
->disp_width
= atoi(param
);
157 if (!strcmp(opt
, "DisplayHeight"))
159 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Height: %d\n", atoi(param
));
160 priv
->disp_height
= atoi(param
);
164 if (!strcmp(opt
, "RecordType"))
166 /* no audio recordblock by Vivo/1.00, 3 and 4 by Vivo/2.00 */
167 if ((atoi(param
) == 3) || (atoi(param
) == 4))
168 parser_in_audio_block
= 1;
170 parser_in_audio_block
= 0;
172 if (!strcmp(opt
, "NominalBitrate"))
174 priv
->audio_bitrate
= atoi(param
);
175 if (priv
->audio_bitrate
== 2000)
176 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
177 if (priv
->audio_bitrate
== 800)
178 priv
->audio_codec
= VIVO_AUDIO_G723
;
180 if (!strcmp(opt
, "SamplingFrequency"))
182 priv
->audio_samplerate
= atoi(param
);
183 if (priv
->audio_samplerate
== 16000)
184 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
185 if (priv
->audio_samplerate
== 8000)
186 priv
->audio_codec
= VIVO_AUDIO_G723
;
188 if (!strcmp(opt
, "Length") && (parser_in_audio_block
== 1))
190 priv
->audio_bytesperblock
= atoi(param
); /* 24 or 40 kbps */
191 if (priv
->audio_bytesperblock
== 40)
192 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
193 if (priv
->audio_bytesperblock
== 24)
194 priv
->audio_codec
= VIVO_AUDIO_G723
;
197 /* only for displaying some informations about movie*/
198 if (!strcmp(opt
, "Title"))
200 demux_info_add(demux
, "name", param
);
201 priv
->title
= strdup(param
);
203 if (!strcmp(opt
, "Author"))
205 demux_info_add(demux
, "author", param
);
206 priv
->author
= strdup(param
);
208 if (!strcmp(opt
, "Copyright"))
210 demux_info_add(demux
, "copyright", param
);
211 priv
->copyright
= strdup(param
);
213 if (!strcmp(opt
, "Producer"))
215 demux_info_add(demux
, "encoder", param
);
216 priv
->producer
= strdup(param
);
220 token
= strtok(NULL
, (char *)&("\x0d\x0a"));
231 static int vivo_check_file(demuxer_t
* demuxer
){
235 unsigned char buf
[2048+256];
237 int orig_pos
= stream_tell(demuxer
->stream
);
239 mp_msg(MSGT_DEMUX
,MSGL_V
,"Checking for VIVO\n");
241 c
=stream_read_char(demuxer
->stream
);
242 if(c
==-256) return 0;
244 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
246 if(len
>1024) return 0;
249 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 1 size: %d\n",len
);
250 //stream_skip(demuxer->stream,len);
252 priv
=malloc(sizeof(vivo_priv_t
));
253 memset(priv
,0,sizeof(vivo_priv_t
));
257 vivo_parse_text_header(demuxer
, len
);
258 if (priv
->supported
== 0)
261 /* this is enought for check (for now) */
262 stream_read(demuxer
->stream
,buf
,len
);
264 // printf("VIVO header: '%s'\n",buf);
268 while(i
<len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
269 if(strncmp(buf
+i
,"Version:Vivo/",13)) return 0; // bad version/type!
273 c
=stream_read_char(demuxer
->stream
);
276 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
278 if(len
+len2
>2048) return 0;
281 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 2 size: %d\n",len2
);
282 stream_skip(demuxer
->stream
,len2
);
283 // stream_read(demuxer->stream,buf+len,len2);
286 // c=stream_read_char(demuxer->stream);
287 // printf("first packet: %02X\n",c);
289 stream_seek(demuxer
->stream
, orig_pos
);
291 return DEMUXER_TYPE_VIVO
;
294 static int audio_pos
=0;
295 static int audio_rate
=0;
298 // 0 = EOF or no stream found
299 // 1 = successfully read a packet
300 static int demux_vivo_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
301 demux_stream_t
*ds
=NULL
;
306 demux
->filepos
=stream_tell(demux
->stream
);
308 c
=stream_read_char(demux
->stream
);
309 if (c
== -256) /* EOF */
311 // printf("c=%x,%02X\n",c,c&0xf0);
314 /* ok, this works, but pts calculating from header is required! */
315 #warning "Calculate PTS from picture header!"
317 c
= stream_read_char(demux
->stream
);
318 mp_msg(MSGT_DEMUX
, MSGL_V
, "packet 0x82(pos=%u) chunk=%x\n",
319 (int)stream_tell(demux
->stream
), c
);
322 case 0x00: // header - skip it!
324 len
=stream_read_char(demux
->stream
);
325 if(len
>=0x80) len
=0x80*(len
-0x80)+stream_read_char(demux
->stream
);
326 mp_msg(MSGT_DEMUX
, MSGL_V
, "vivo extra header: %d bytes\n",len
);
330 /* also try to parse all headers */
331 pos
= stream_tell(demux
->stream
);
332 vivo_parse_text_header(demux
, len
);
333 stream_seek(demux
->stream
, pos
);
338 case 0x10: // video packet
340 len
= stream_read_char(demux
->stream
);
345 case 0x20: // video packet
346 len
=stream_read_char(demux
->stream
);
349 case 0x30: // audio packet
351 len
= stream_read_char(demux
->stream
);
357 case 0x40: // audio packet
359 len
= stream_read_char(demux
->stream
);
366 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"VIVO - unknown ID found: %02X at pos %"PRIu64
" contact author!\n",
367 c
, (int64_t)stream_tell(demux
->stream
));
371 // printf("chunk=%x, len=%d\n", c, len);
373 if(!ds
|| ds
->id
<-1){
374 if(len
) stream_skip(demux
->stream
,len
);
381 if(ds
->asf_seq
!=seq
){
382 // closed segment, finalize packet:
383 ds_add_packet(ds
,ds
->asf_packet
);
385 // printf("packet!\n");
387 // append data to it!
388 demux_packet_t
* dp
=ds
->asf_packet
;
389 if(dp
->len
+ len
+ FF_INPUT_BUFFER_PADDING_SIZE
< 0)
391 dp
->buffer
=realloc(dp
->buffer
,dp
->len
+len
+FF_INPUT_BUFFER_PADDING_SIZE
);
392 memset(dp
->buffer
+dp
->len
+len
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
393 //memcpy(dp->buffer+dp->len,data,len);
394 stream_read(demux
->stream
,dp
->buffer
+dp
->len
,len
);
395 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"data appended! %d+%d\n",dp
->len
,len
);
398 if((c
&0xF0)==0x20) --ds
->asf_seq
; // hack!
402 // create new packet:
403 { demux_packet_t
* dp
;
404 dp
=new_demux_packet(len
);
405 //memcpy(dp->buffer,data,len);
406 stream_read(demux
->stream
,dp
->buffer
,len
);
407 dp
->pts
=audio_rate
?((float)audio_pos
/(float)audio_rate
):0;
408 // dp->flags=keyframe;
409 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur);
410 dp
->pos
=demux
->filepos
;
419 static const short h263_format
[8][2] = {
426 { 320, 240 } // ??????? or 240x180 (found in vivo2) ?
429 static unsigned char* buffer
;
432 static unsigned char buf
=0;
433 static int format
, width
, height
;
435 static unsigned int x_get_bits(int n
){
440 buf
=buffer
[bufptr
++];
443 //x=(x<<1)|(buf&1);buf>>=1;
444 x
=(x
<<1)|(buf
>>7);buf
<<=1;
450 #define get_bits(xxx,n) x_get_bits(n)
451 #define get_bits1(xxx) x_get_bits(1)
452 #define skip_bits(xxx,n) x_get_bits(n)
453 #define skip_bits1(xxx) x_get_bits(1)
455 /* most is hardcoded. should extend to handle all h263 streams */
456 static int h263_decode_picture_header(unsigned char *b_ptr
)
460 // for(i=0;i<16;i++) printf(" %02X",b_ptr[i]); printf("\n");
466 if (get_bits(&s
->gb
, 22) != 0x20){
467 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad picture header\n");
470 skip_bits(&s
->gb
, 8); /* picture timestamp */
472 if (get_bits1(&s
->gb
) != 1){
473 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad marker\n");
474 return -1; /* marker */
476 if (get_bits1(&s
->gb
) != 0){
477 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad h263 id\n");
478 return -1; /* h263 id */
480 skip_bits1(&s
->gb
); /* split screen off */
481 skip_bits1(&s
->gb
); /* camera off */
482 skip_bits1(&s
->gb
); /* freeze picture release off */
484 format
= get_bits(&s
->gb
, 3);
487 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 0 format = %d\n", format
);
489 width
= h263_format
[format
][0];
490 height
= h263_format
[format
][1];
491 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
492 // if (!width) return -1;
494 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits1(&s
->gb
));
495 mp_msg(MSGT_DEMUX
, MSGL_V
, "unrestricted_mv=%d\n", get_bits1(&s
->gb
));
497 mp_msg(MSGT_DEMUX
, MSGL_V
, "SAC: %d\n", get_bits1(&s
->gb
));
498 mp_msg(MSGT_DEMUX
, MSGL_V
, "advanced prediction mode: %d\n", get_bits1(&s
->gb
));
499 mp_msg(MSGT_DEMUX
, MSGL_V
, "PB frame: %d\n", get_bits1(&s
->gb
));
501 if (get_bits1(&s
->gb
) != 0)
502 return -1; /* SAC: off */
503 if (get_bits1(&s
->gb
) != 0)
504 return -1; /* advanced prediction mode: off */
505 if (get_bits1(&s
->gb
) != 0)
506 return -1; /* not PB frame */
508 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
509 skip_bits1(&s
->gb
); /* Continuous Presence Multipoint mode: off */
511 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 1\n");
513 if (get_bits(&s
->gb
, 3) != 1){
514 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "H.263v2 A error\n");
517 if (get_bits(&s
->gb
, 3) != 6){ /* custom source format */
518 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "custom source format\n");
521 skip_bits(&s
->gb
, 12);
522 skip_bits(&s
->gb
, 3);
523 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits(&s
->gb
, 3) + 1);
524 // if (s->pict_type != I_TYPE &&
525 // s->pict_type != P_TYPE)
527 skip_bits(&s
->gb
, 7);
528 skip_bits(&s
->gb
, 4); /* aspect ratio */
529 width
= (get_bits(&s
->gb
, 9) + 1) * 4;
531 height
= get_bits(&s
->gb
, 9) * 4;
532 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
535 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
539 while (get_bits1(&s
->gb
) != 0) {
540 skip_bits(&s
->gb
, 8);
544 // s->height = height;
550 static demuxer_t
* demux_open_vivo(demuxer_t
* demuxer
){
551 vivo_priv_t
* priv
=demuxer
->priv
;
553 if(!ds_fill_buffer(demuxer
->video
)){
554 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"VIVO: " MSGTR_MissingVideoStreamBug
);
560 h263_decode_picture_header(demuxer
->video
->buffer
);
562 if (vivo_param_version
!= -1)
563 priv
->version
= '0' + vivo_param_version
;
565 { sh_video_t
* sh
=new_sh_video(demuxer
,0);
567 /* viv1, viv2 (for better codecs.conf) */
568 sh
->format
= mmioFOURCC('v', 'i', 'v', priv
->version
);
576 sh
->frametime
=1.0f
/sh
->fps
;
578 /* XXX: FIXME: can't scale image. */
579 /* hotfix to disable: */
580 priv
->disp_width
= priv
->width
;
581 priv
->disp_height
= priv
->height
;
583 if (vivo_param_width
!= -1)
584 priv
->disp_width
= priv
->width
= vivo_param_width
;
586 if (vivo_param_height
!= -1)
587 priv
->disp_height
= priv
->height
= vivo_param_height
;
589 if (vivo_param_vformat
!= -1)
591 priv
->disp_width
= priv
->width
= h263_format
[vivo_param_vformat
][0];
592 priv
->disp_height
= priv
->height
= h263_format
[vivo_param_vformat
][1];
595 if (priv
->disp_width
)
596 sh
->disp_w
= priv
->disp_width
;
599 if (priv
->disp_height
)
600 sh
->disp_h
= priv
->disp_height
;
604 // emulate BITMAPINFOHEADER:
605 sh
->bih
=malloc(sizeof(BITMAPINFOHEADER
));
606 memset(sh
->bih
,0,sizeof(BITMAPINFOHEADER
));
609 sh
->bih
->biWidth
= priv
->width
;
611 sh
->bih
->biWidth
= width
;
613 sh
->bih
->biHeight
= priv
->height
;
615 sh
->bih
->biHeight
= height
;
617 sh
->bih
->biBitCount
=24;
618 sh
->bih
->biCompression
=sh
->format
;
619 sh
->bih
->biSizeImage
=sh
->bih
->biWidth
*sh
->bih
->biHeight
*3;
621 /* insert as stream */
622 demuxer
->video
->sh
=sh
;
623 sh
->ds
=demuxer
->video
;
624 demuxer
->video
->id
=0;
626 /* disable seeking */
627 demuxer
->seekable
= 0;
629 mp_msg(MSGT_DEMUX
,MSGL_V
,"VIVO Video stream %d size: display: %dx%d, codec: %ux%u\n",
630 demuxer
->video
->id
, sh
->disp_w
, sh
->disp_h
, sh
->bih
->biWidth
,
635 if (demuxer
->audio
->id
>= -1){
636 if(!ds_fill_buffer(demuxer
->audio
)){
637 mp_msg(MSGT_DEMUX
,MSGL_ERR
,"VIVO: " MSGTR_MissingAudioStream
);
639 { sh_audio_t
* sh
=new_sh_audio(demuxer
,1);
641 /* Select audio codec */
642 if (priv
->audio_codec
== 0)
644 if (priv
->version
== '2')
645 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
647 priv
->audio_codec
= VIVO_AUDIO_G723
;
649 if (vivo_param_acodec
!= NULL
)
651 if (!strcasecmp(vivo_param_acodec
, "g723"))
652 priv
->audio_codec
= VIVO_AUDIO_G723
;
653 if (!strcasecmp(vivo_param_acodec
, "siren"))
654 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
657 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
659 else if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
663 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "VIVO: Not support audio codec (%d)\n",
665 free_sh_audio(demuxer
, 1);
669 // Emulate WAVEFORMATEX struct:
670 sh
->wf
=malloc(sizeof(WAVEFORMATEX
));
671 memset(sh
->wf
,0,sizeof(WAVEFORMATEX
));
672 sh
->wf
->wFormatTag
=sh
->format
;
673 sh
->wf
->nChannels
=1; /* 1 channels for both Siren and G.723 */
675 /* Set bits per sample */
676 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
677 sh
->wf
->wBitsPerSample
= 16;
679 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
680 sh
->wf
->wBitsPerSample
= 8;
682 /* Set sampling rate */
683 if (priv
->audio_samplerate
) /* got from header */
684 sh
->wf
->nSamplesPerSec
= priv
->audio_samplerate
;
687 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
688 sh
->wf
->nSamplesPerSec
= 16000;
689 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
690 sh
->wf
->nSamplesPerSec
= 8000;
692 if (vivo_param_samplerate
!= -1)
693 sh
->wf
->nSamplesPerSec
= vivo_param_samplerate
;
695 /* Set audio bitrate */
696 if (priv
->audio_bitrate
) /* got from header */
697 sh
->wf
->nAvgBytesPerSec
= priv
->audio_bitrate
;
700 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
701 sh
->wf
->nAvgBytesPerSec
= 2000;
702 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
703 sh
->wf
->nAvgBytesPerSec
= 800;
705 if (vivo_param_abitrate
!= -1)
706 sh
->wf
->nAvgBytesPerSec
= vivo_param_abitrate
;
707 audio_rate
=sh
->wf
->nAvgBytesPerSec
;
709 if (!priv
->audio_bytesperblock
)
711 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
712 sh
->wf
->nBlockAlign
= 40;
713 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
714 sh
->wf
->nBlockAlign
= 24;
717 sh
->wf
->nBlockAlign
= priv
->audio_bytesperblock
;
718 if (vivo_param_bytesperblock
!= -1)
719 sh
->wf
->nBlockAlign
= vivo_param_bytesperblock
;
722 /* insert as stream */
723 demuxer
->audio
->sh
=sh
;
724 sh
->ds
=demuxer
->audio
;
725 demuxer
->audio
->id
=1;
733 static void demux_close_vivo(demuxer_t
*demuxer
)
735 vivo_priv_t
* priv
=demuxer
->priv
;
743 free(priv
->copyright
);
745 free(priv
->producer
);
752 const demuxer_desc_t demuxer_desc_vivo
= {
756 "A'rpi, Alex Beregszasi",
759 0, // unsafe autodetect
761 demux_vivo_fill_buffer
,