3 * copyright (c) 2001 A'rpi
4 * VIVO text header parser and audio support by alex
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <string.h> /* strtok */
31 #include "stream/stream.h"
36 int vivo_param_version
= -1;
37 char *vivo_param_acodec
= NULL
;
38 int vivo_param_abitrate
= -1;
39 int vivo_param_samplerate
= -1;
40 int vivo_param_bytesperblock
= -1;
41 int vivo_param_width
= -1;
42 int vivo_param_height
= -1;
43 int vivo_param_vformat
= -1;
45 /* VIVO audio standards from vivog723.acm:
50 SamplesPerSec = 8000 - 8khz
52 BlockAlign (bytes per block) = 24
58 SamplesPerSec = 16000 - 16khz
60 BlockAlign (bytes per block) = 40
64 //enum { VIVO_AUDIO_G723, VIVO_AUDIO_SIREN };
66 #define VIVO_AUDIO_G723 1
67 #define VIVO_AUDIO_SIREN 2
88 int audio_bytesperblock
;
91 /* parse all possible extra headers */
92 /* (audio headers are separate - mostly with recordtype=3 or 4) */
93 #define TEXTPARSE_ALL 1
95 static void vivo_parse_text_header(demuxer_t
*demux
, int header_len
)
97 vivo_priv_t
* priv
= demux
->priv
;
102 int parser_in_audio_block
= 0;
106 priv
= malloc(sizeof(vivo_priv_t
));
107 memset(priv
, 0, sizeof(vivo_priv_t
));
112 buf
= malloc(header_len
);
113 opt
= malloc(header_len
);
114 param
= malloc(header_len
);
115 stream_read(demux
->stream
, buf
, header_len
);
117 while(i
<header_len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
119 token
= strtok(buf
, (char *)&("\x0d\x0a"));
120 while (token
&& (header_len
>2))
122 header_len
-= strlen(token
)+2;
123 if (sscanf(token
, "%[^:]:%[^\n]", opt
, param
) != 2)
125 mp_msg(MSGT_DEMUX
, MSGL_V
, "viv_text_header_parser: bad line: '%s' at ~%#"PRIx64
"\n",
126 token
, (int64_t)stream_tell(demux
->stream
));
129 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token: '%s' (%d bytes/%d bytes left)\n",
130 token
, strlen(token
), header_len
);
131 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token => o: '%s', p: '%s'\n",
134 /* checking versions: only v1 or v2 is suitable (or known?:) */
135 if (!strcmp(opt
, "Version"))
137 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Version: %s\n", param
);
138 if (!strncmp(param
, "Vivo/1", 6) || !strncmp(param
, "Vivo/2", 6))
141 /* save major version for fourcc */
142 priv
->version
= param
[5];
147 if (!strcmp(opt
, "FPS"))
149 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "FPS: %f\n", atof(param
));
150 priv
->fps
= atof(param
);
152 if (!strcmp(opt
, "Width"))
154 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Width: %d\n", atoi(param
));
155 priv
->width
= atoi(param
);
157 if (!strcmp(opt
, "Height"))
159 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Height: %d\n", atoi(param
));
160 priv
->height
= atoi(param
);
162 if (!strcmp(opt
, "DisplayWidth"))
164 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Width: %d\n", atoi(param
));
165 priv
->disp_width
= atoi(param
);
167 if (!strcmp(opt
, "DisplayHeight"))
169 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Height: %d\n", atoi(param
));
170 priv
->disp_height
= atoi(param
);
174 if (!strcmp(opt
, "RecordType"))
176 /* no audio recordblock by Vivo/1.00, 3 and 4 by Vivo/2.00 */
177 if ((atoi(param
) == 3) || (atoi(param
) == 4))
178 parser_in_audio_block
= 1;
180 parser_in_audio_block
= 0;
182 if (!strcmp(opt
, "NominalBitrate"))
184 priv
->audio_bitrate
= atoi(param
);
185 if (priv
->audio_bitrate
== 2000)
186 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
187 if (priv
->audio_bitrate
== 800)
188 priv
->audio_codec
= VIVO_AUDIO_G723
;
190 if (!strcmp(opt
, "SamplingFrequency"))
192 priv
->audio_samplerate
= atoi(param
);
193 if (priv
->audio_samplerate
== 16000)
194 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
195 if (priv
->audio_samplerate
== 8000)
196 priv
->audio_codec
= VIVO_AUDIO_G723
;
198 if (!strcmp(opt
, "Length") && (parser_in_audio_block
== 1))
200 priv
->audio_bytesperblock
= atoi(param
); /* 24 or 40 kbps */
201 if (priv
->audio_bytesperblock
== 40)
202 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
203 if (priv
->audio_bytesperblock
== 24)
204 priv
->audio_codec
= VIVO_AUDIO_G723
;
207 /* only for displaying some informations about movie*/
208 if (!strcmp(opt
, "Title"))
210 demux_info_add(demux
, "title", param
);
211 priv
->title
= strdup(param
);
213 if (!strcmp(opt
, "Author"))
215 demux_info_add(demux
, "author", param
);
216 priv
->author
= strdup(param
);
218 if (!strcmp(opt
, "Copyright"))
220 demux_info_add(demux
, "copyright", param
);
221 priv
->copyright
= strdup(param
);
223 if (!strcmp(opt
, "Producer"))
225 demux_info_add(demux
, "encoder", param
);
226 priv
->producer
= strdup(param
);
230 token
= strtok(NULL
, (char *)&("\x0d\x0a"));
241 static int vivo_check_file(demuxer_t
* demuxer
){
245 unsigned char buf
[2048+256];
247 int orig_pos
= stream_tell(demuxer
->stream
);
249 mp_msg(MSGT_DEMUX
,MSGL_V
,"Checking for VIVO\n");
251 c
=stream_read_char(demuxer
->stream
);
252 if(c
==-256) return 0;
254 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
256 if(len
>1024) return 0;
259 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 1 size: %d\n",len
);
260 //stream_skip(demuxer->stream,len);
262 priv
=malloc(sizeof(vivo_priv_t
));
263 memset(priv
,0,sizeof(vivo_priv_t
));
267 vivo_parse_text_header(demuxer
, len
);
268 if (priv
->supported
== 0)
271 /* this is enought for check (for now) */
272 stream_read(demuxer
->stream
,buf
,len
);
274 // printf("VIVO header: '%s'\n",buf);
278 while(i
<len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
279 if(strncmp(buf
+i
,"Version:Vivo/",13)) return 0; // bad version/type!
283 c
=stream_read_char(demuxer
->stream
);
286 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
288 if(len
+len2
>2048) return 0;
291 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 2 size: %d\n",len2
);
292 stream_skip(demuxer
->stream
,len2
);
293 // stream_read(demuxer->stream,buf+len,len2);
296 // c=stream_read_char(demuxer->stream);
297 // printf("first packet: %02X\n",c);
299 stream_seek(demuxer
->stream
, orig_pos
);
301 return DEMUXER_TYPE_VIVO
;
304 static int audio_pos
=0;
305 static int audio_rate
=0;
308 // 0 = EOF or no stream found
309 // 1 = successfully read a packet
310 static int demux_vivo_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
311 demux_stream_t
*ds
=NULL
;
316 demux
->filepos
=stream_tell(demux
->stream
);
318 c
=stream_read_char(demux
->stream
);
319 if (c
== -256) /* EOF */
321 // printf("c=%x,%02X\n",c,c&0xf0);
324 /* ok, this works, but pts calculating from header is required! */
325 #warning "Calculate PTS from picture header!"
327 c
= stream_read_char(demux
->stream
);
328 mp_msg(MSGT_DEMUX
, MSGL_V
, "packet 0x82(pos=%u) chunk=%x\n",
329 (int)stream_tell(demux
->stream
), c
);
332 case 0x00: // header - skip it!
334 len
=stream_read_char(demux
->stream
);
335 if(len
>=0x80) len
=0x80*(len
-0x80)+stream_read_char(demux
->stream
);
336 mp_msg(MSGT_DEMUX
, MSGL_V
, "vivo extra header: %d bytes\n",len
);
340 /* also try to parse all headers */
341 pos
= stream_tell(demux
->stream
);
342 vivo_parse_text_header(demux
, len
);
343 stream_seek(demux
->stream
, pos
);
348 case 0x10: // video packet
350 len
= stream_read_char(demux
->stream
);
355 case 0x20: // video packet
356 len
=stream_read_char(demux
->stream
);
359 case 0x30: // audio packet
361 len
= stream_read_char(demux
->stream
);
367 case 0x40: // audio packet
369 len
= stream_read_char(demux
->stream
);
376 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"VIVO - unknown ID found: %02X at pos %"PRIu64
" contact author!\n",
377 c
, (int64_t)stream_tell(demux
->stream
));
381 // printf("chunk=%x, len=%d\n", c, len);
383 if(!ds
|| ds
->id
<-1){
384 if(len
) stream_skip(demux
->stream
,len
);
391 if(ds
->asf_seq
!=seq
){
392 // closed segment, finalize packet:
393 ds_add_packet(ds
,ds
->asf_packet
);
395 // printf("packet!\n");
397 // append data to it!
398 demux_packet_t
* dp
=ds
->asf_packet
;
399 if(dp
->len
+ len
+ MP_INPUT_BUFFER_PADDING_SIZE
< 0)
401 dp
->buffer
=realloc(dp
->buffer
,dp
->len
+len
+MP_INPUT_BUFFER_PADDING_SIZE
);
402 memset(dp
->buffer
+dp
->len
+len
, 0, MP_INPUT_BUFFER_PADDING_SIZE
);
403 //memcpy(dp->buffer+dp->len,data,len);
404 stream_read(demux
->stream
,dp
->buffer
+dp
->len
,len
);
405 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"data appended! %d+%d\n",dp
->len
,len
);
408 if((c
&0xF0)==0x20) --ds
->asf_seq
; // hack!
412 // create new packet:
413 { demux_packet_t
* dp
;
414 dp
=new_demux_packet(len
);
415 //memcpy(dp->buffer,data,len);
416 stream_read(demux
->stream
,dp
->buffer
,len
);
417 dp
->pts
=audio_rate
?((float)audio_pos
/(float)audio_rate
):0;
418 // dp->flags=keyframe;
419 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur);
420 dp
->pos
=demux
->filepos
;
429 static const short h263_format
[8][2] = {
436 { 320, 240 } // ??????? or 240x180 (found in vivo2) ?
439 static unsigned char* buffer
;
442 static unsigned char buf
=0;
443 static int format
, width
, height
;
445 static unsigned int x_get_bits(int n
){
450 buf
=buffer
[bufptr
++];
453 //x=(x<<1)|(buf&1);buf>>=1;
454 x
=(x
<<1)|(buf
>>7);buf
<<=1;
460 #define get_bits(xxx,n) x_get_bits(n)
461 #define get_bits1(xxx) x_get_bits(1)
462 #define skip_bits(xxx,n) x_get_bits(n)
463 #define skip_bits1(xxx) x_get_bits(1)
465 /* most is hardcoded. should extend to handle all h263 streams */
466 static int h263_decode_picture_header(unsigned char *b_ptr
)
470 // for(i=0;i<16;i++) printf(" %02X",b_ptr[i]); printf("\n");
476 if (get_bits(&s
->gb
, 22) != 0x20){
477 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad picture header\n");
480 skip_bits(&s
->gb
, 8); /* picture timestamp */
482 if (get_bits1(&s
->gb
) != 1){
483 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad marker\n");
484 return -1; /* marker */
486 if (get_bits1(&s
->gb
) != 0){
487 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad h263 id\n");
488 return -1; /* h263 id */
490 skip_bits1(&s
->gb
); /* split screen off */
491 skip_bits1(&s
->gb
); /* camera off */
492 skip_bits1(&s
->gb
); /* freeze picture release off */
494 format
= get_bits(&s
->gb
, 3);
497 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 0 format = %d\n", format
);
499 width
= h263_format
[format
][0];
500 height
= h263_format
[format
][1];
501 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
502 // if (!width) return -1;
504 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits1(&s
->gb
));
505 mp_msg(MSGT_DEMUX
, MSGL_V
, "unrestricted_mv=%d\n", get_bits1(&s
->gb
));
507 mp_msg(MSGT_DEMUX
, MSGL_V
, "SAC: %d\n", get_bits1(&s
->gb
));
508 mp_msg(MSGT_DEMUX
, MSGL_V
, "advanced prediction mode: %d\n", get_bits1(&s
->gb
));
509 mp_msg(MSGT_DEMUX
, MSGL_V
, "PB frame: %d\n", get_bits1(&s
->gb
));
511 if (get_bits1(&s
->gb
) != 0)
512 return -1; /* SAC: off */
513 if (get_bits1(&s
->gb
) != 0)
514 return -1; /* advanced prediction mode: off */
515 if (get_bits1(&s
->gb
) != 0)
516 return -1; /* not PB frame */
518 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
519 skip_bits1(&s
->gb
); /* Continuous Presence Multipoint mode: off */
521 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 1\n");
523 if (get_bits(&s
->gb
, 3) != 1){
524 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "H.263v2 A error\n");
527 if (get_bits(&s
->gb
, 3) != 6){ /* custom source format */
528 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "custom source format\n");
531 skip_bits(&s
->gb
, 12);
532 skip_bits(&s
->gb
, 3);
533 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits(&s
->gb
, 3) + 1);
534 // if (s->pict_type != I_TYPE &&
535 // s->pict_type != P_TYPE)
537 skip_bits(&s
->gb
, 7);
538 skip_bits(&s
->gb
, 4); /* aspect ratio */
539 width
= (get_bits(&s
->gb
, 9) + 1) * 4;
541 height
= get_bits(&s
->gb
, 9) * 4;
542 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
545 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
549 while (get_bits1(&s
->gb
) != 0) {
550 skip_bits(&s
->gb
, 8);
554 // s->height = height;
560 static demuxer_t
* demux_open_vivo(demuxer_t
* demuxer
){
561 vivo_priv_t
* priv
=demuxer
->priv
;
563 if(!ds_fill_buffer(demuxer
->video
)){
564 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "VIVO: %s",
565 mp_gtext("Missing video stream!? Contact the author, it may be a bug :(\n"));
571 h263_decode_picture_header(demuxer
->video
->buffer
);
573 if (vivo_param_version
!= -1)
574 priv
->version
= '0' + vivo_param_version
;
576 { sh_video_t
* sh
=new_sh_video(demuxer
,0);
578 /* viv1, viv2 (for better codecs.conf) */
579 sh
->format
= mmioFOURCC('v', 'i', 'v', priv
->version
);
587 sh
->frametime
=1.0f
/sh
->fps
;
589 /* XXX: FIXME: can't scale image. */
590 /* hotfix to disable: */
591 priv
->disp_width
= priv
->width
;
592 priv
->disp_height
= priv
->height
;
594 if (vivo_param_width
!= -1)
595 priv
->disp_width
= priv
->width
= vivo_param_width
;
597 if (vivo_param_height
!= -1)
598 priv
->disp_height
= priv
->height
= vivo_param_height
;
600 if (vivo_param_vformat
!= -1)
602 priv
->disp_width
= priv
->width
= h263_format
[vivo_param_vformat
][0];
603 priv
->disp_height
= priv
->height
= h263_format
[vivo_param_vformat
][1];
606 if (priv
->disp_width
)
607 sh
->disp_w
= priv
->disp_width
;
610 if (priv
->disp_height
)
611 sh
->disp_h
= priv
->disp_height
;
615 // emulate BITMAPINFOHEADER:
616 sh
->bih
=calloc(1, sizeof(*sh
->bih
));
619 sh
->bih
->biWidth
= priv
->width
;
621 sh
->bih
->biWidth
= width
;
623 sh
->bih
->biHeight
= priv
->height
;
625 sh
->bih
->biHeight
= height
;
627 sh
->bih
->biBitCount
=24;
628 sh
->bih
->biCompression
=sh
->format
;
629 sh
->bih
->biSizeImage
=sh
->bih
->biWidth
*sh
->bih
->biHeight
*3;
631 /* insert as stream */
632 demuxer
->video
->sh
=sh
;
633 sh
->ds
=demuxer
->video
;
634 demuxer
->video
->id
=0;
636 /* disable seeking */
637 demuxer
->seekable
= 0;
639 mp_msg(MSGT_DEMUX
,MSGL_V
,"VIVO Video stream %d size: display: %dx%d, codec: %ux%u\n",
640 demuxer
->video
->id
, sh
->disp_w
, sh
->disp_h
, sh
->bih
->biWidth
,
645 if (demuxer
->audio
->id
>= -1){
646 if(!ds_fill_buffer(demuxer
->audio
)){
647 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "VIVO: %s",
648 mp_gtext("No audio stream found -> no sound.\n"));
650 { sh_audio_t
* sh
=new_sh_audio(demuxer
,1);
652 /* Select audio codec */
653 if (priv
->audio_codec
== 0)
655 if (priv
->version
== '2')
656 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
658 priv
->audio_codec
= VIVO_AUDIO_G723
;
660 if (vivo_param_acodec
!= NULL
)
662 if (!strcasecmp(vivo_param_acodec
, "g723"))
663 priv
->audio_codec
= VIVO_AUDIO_G723
;
664 if (!strcasecmp(vivo_param_acodec
, "siren"))
665 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
668 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
670 else if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
674 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "VIVO: Not support audio codec (%d)\n",
676 free_sh_audio(demuxer
, 1);
680 // Emulate WAVEFORMATEX struct:
681 sh
->wf
=calloc(1, sizeof(*sh
->wf
));
682 sh
->wf
->wFormatTag
=sh
->format
;
683 sh
->wf
->nChannels
=1; /* 1 channels for both Siren and G.723 */
685 /* Set bits per sample */
686 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
687 sh
->wf
->wBitsPerSample
= 16;
689 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
690 sh
->wf
->wBitsPerSample
= 8;
692 /* Set sampling rate */
693 if (priv
->audio_samplerate
) /* got from header */
694 sh
->wf
->nSamplesPerSec
= priv
->audio_samplerate
;
697 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
698 sh
->wf
->nSamplesPerSec
= 16000;
699 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
700 sh
->wf
->nSamplesPerSec
= 8000;
702 if (vivo_param_samplerate
!= -1)
703 sh
->wf
->nSamplesPerSec
= vivo_param_samplerate
;
705 /* Set audio bitrate */
706 if (priv
->audio_bitrate
) /* got from header */
707 sh
->wf
->nAvgBytesPerSec
= priv
->audio_bitrate
;
710 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
711 sh
->wf
->nAvgBytesPerSec
= 2000;
712 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
713 sh
->wf
->nAvgBytesPerSec
= 800;
715 if (vivo_param_abitrate
!= -1)
716 sh
->wf
->nAvgBytesPerSec
= vivo_param_abitrate
;
717 audio_rate
=sh
->wf
->nAvgBytesPerSec
;
719 if (!priv
->audio_bytesperblock
)
721 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
722 sh
->wf
->nBlockAlign
= 40;
723 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
724 sh
->wf
->nBlockAlign
= 24;
727 sh
->wf
->nBlockAlign
= priv
->audio_bytesperblock
;
728 if (vivo_param_bytesperblock
!= -1)
729 sh
->wf
->nBlockAlign
= vivo_param_bytesperblock
;
732 /* insert as stream */
733 demuxer
->audio
->sh
=sh
;
734 sh
->ds
=demuxer
->audio
;
735 demuxer
->audio
->id
=1;
743 static void demux_close_vivo(demuxer_t
*demuxer
)
745 vivo_priv_t
* priv
=demuxer
->priv
;
753 free(priv
->copyright
);
755 free(priv
->producer
);
762 const demuxer_desc_t demuxer_desc_vivo
= {
766 "A'rpi, Alex Beregszasi",
769 0, // unsafe autodetect
771 demux_vivo_fill_buffer
,