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 */
32 #include "stream/stream.h"
37 int vivo_param_version
= -1;
38 char *vivo_param_acodec
= NULL
;
39 int vivo_param_abitrate
= -1;
40 int vivo_param_samplerate
= -1;
41 int vivo_param_bytesperblock
= -1;
42 int vivo_param_width
= -1;
43 int vivo_param_height
= -1;
44 int vivo_param_vformat
= -1;
46 /* VIVO audio standards from vivog723.acm:
51 SamplesPerSec = 8000 - 8khz
53 BlockAlign (bytes per block) = 24
59 SamplesPerSec = 16000 - 16khz
61 BlockAlign (bytes per block) = 40
65 //enum { VIVO_AUDIO_G723, VIVO_AUDIO_SIREN };
67 #define VIVO_AUDIO_G723 1
68 #define VIVO_AUDIO_SIREN 2
89 int audio_bytesperblock
;
92 /* parse all possible extra headers */
93 /* (audio headers are separate - mostly with recordtype=3 or 4) */
94 #define TEXTPARSE_ALL 1
96 static void vivo_parse_text_header(demuxer_t
*demux
, int header_len
)
98 vivo_priv_t
* priv
= demux
->priv
;
103 int parser_in_audio_block
= 0;
107 priv
= malloc(sizeof(vivo_priv_t
));
108 memset(priv
, 0, sizeof(vivo_priv_t
));
113 buf
= malloc(header_len
);
114 opt
= malloc(header_len
);
115 param
= malloc(header_len
);
116 stream_read(demux
->stream
, buf
, header_len
);
118 while(i
<header_len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
120 token
= strtok(buf
, (char *)&("\x0d\x0a"));
121 while (token
&& (header_len
>2))
123 header_len
-= strlen(token
)+2;
124 if (sscanf(token
, "%[^:]:%[^\n]", opt
, param
) != 2)
126 mp_msg(MSGT_DEMUX
, MSGL_V
, "viv_text_header_parser: bad line: '%s' at ~%#"PRIx64
"\n",
127 token
, (int64_t)stream_tell(demux
->stream
));
130 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token: '%s' (%d bytes/%d bytes left)\n",
131 token
, strlen(token
), header_len
);
132 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token => o: '%s', p: '%s'\n",
135 /* checking versions: only v1 or v2 is suitable (or known?:) */
136 if (!strcmp(opt
, "Version"))
138 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Version: %s\n", param
);
139 if (!strncmp(param
, "Vivo/1", 6) || !strncmp(param
, "Vivo/2", 6))
142 /* save major version for fourcc */
143 priv
->version
= param
[5];
148 if (!strcmp(opt
, "FPS"))
150 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "FPS: %f\n", atof(param
));
151 priv
->fps
= atof(param
);
153 if (!strcmp(opt
, "Width"))
155 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Width: %d\n", atoi(param
));
156 priv
->width
= atoi(param
);
158 if (!strcmp(opt
, "Height"))
160 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Height: %d\n", atoi(param
));
161 priv
->height
= atoi(param
);
163 if (!strcmp(opt
, "DisplayWidth"))
165 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Width: %d\n", atoi(param
));
166 priv
->disp_width
= atoi(param
);
168 if (!strcmp(opt
, "DisplayHeight"))
170 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Height: %d\n", atoi(param
));
171 priv
->disp_height
= atoi(param
);
175 if (!strcmp(opt
, "RecordType"))
177 /* no audio recordblock by Vivo/1.00, 3 and 4 by Vivo/2.00 */
178 if ((atoi(param
) == 3) || (atoi(param
) == 4))
179 parser_in_audio_block
= 1;
181 parser_in_audio_block
= 0;
183 if (!strcmp(opt
, "NominalBitrate"))
185 priv
->audio_bitrate
= atoi(param
);
186 if (priv
->audio_bitrate
== 2000)
187 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
188 if (priv
->audio_bitrate
== 800)
189 priv
->audio_codec
= VIVO_AUDIO_G723
;
191 if (!strcmp(opt
, "SamplingFrequency"))
193 priv
->audio_samplerate
= atoi(param
);
194 if (priv
->audio_samplerate
== 16000)
195 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
196 if (priv
->audio_samplerate
== 8000)
197 priv
->audio_codec
= VIVO_AUDIO_G723
;
199 if (!strcmp(opt
, "Length") && (parser_in_audio_block
== 1))
201 priv
->audio_bytesperblock
= atoi(param
); /* 24 or 40 kbps */
202 if (priv
->audio_bytesperblock
== 40)
203 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
204 if (priv
->audio_bytesperblock
== 24)
205 priv
->audio_codec
= VIVO_AUDIO_G723
;
208 /* only for displaying some informations about movie*/
209 if (!strcmp(opt
, "Title"))
211 demux_info_add(demux
, "name", param
);
212 priv
->title
= strdup(param
);
214 if (!strcmp(opt
, "Author"))
216 demux_info_add(demux
, "author", param
);
217 priv
->author
= strdup(param
);
219 if (!strcmp(opt
, "Copyright"))
221 demux_info_add(demux
, "copyright", param
);
222 priv
->copyright
= strdup(param
);
224 if (!strcmp(opt
, "Producer"))
226 demux_info_add(demux
, "encoder", param
);
227 priv
->producer
= strdup(param
);
231 token
= strtok(NULL
, (char *)&("\x0d\x0a"));
242 static int vivo_check_file(demuxer_t
* demuxer
){
246 unsigned char buf
[2048+256];
248 int orig_pos
= stream_tell(demuxer
->stream
);
250 mp_msg(MSGT_DEMUX
,MSGL_V
,"Checking for VIVO\n");
252 c
=stream_read_char(demuxer
->stream
);
253 if(c
==-256) return 0;
255 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
257 if(len
>1024) return 0;
260 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 1 size: %d\n",len
);
261 //stream_skip(demuxer->stream,len);
263 priv
=malloc(sizeof(vivo_priv_t
));
264 memset(priv
,0,sizeof(vivo_priv_t
));
268 vivo_parse_text_header(demuxer
, len
);
269 if (priv
->supported
== 0)
272 /* this is enought for check (for now) */
273 stream_read(demuxer
->stream
,buf
,len
);
275 // printf("VIVO header: '%s'\n",buf);
279 while(i
<len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
280 if(strncmp(buf
+i
,"Version:Vivo/",13)) return 0; // bad version/type!
284 c
=stream_read_char(demuxer
->stream
);
287 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
289 if(len
+len2
>2048) return 0;
292 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 2 size: %d\n",len2
);
293 stream_skip(demuxer
->stream
,len2
);
294 // stream_read(demuxer->stream,buf+len,len2);
297 // c=stream_read_char(demuxer->stream);
298 // printf("first packet: %02X\n",c);
300 stream_seek(demuxer
->stream
, orig_pos
);
302 return DEMUXER_TYPE_VIVO
;
305 static int audio_pos
=0;
306 static int audio_rate
=0;
309 // 0 = EOF or no stream found
310 // 1 = successfully read a packet
311 static int demux_vivo_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
312 demux_stream_t
*ds
=NULL
;
317 demux
->filepos
=stream_tell(demux
->stream
);
319 c
=stream_read_char(demux
->stream
);
320 if (c
== -256) /* EOF */
322 // printf("c=%x,%02X\n",c,c&0xf0);
325 /* ok, this works, but pts calculating from header is required! */
326 #warning "Calculate PTS from picture header!"
328 c
= stream_read_char(demux
->stream
);
329 mp_msg(MSGT_DEMUX
, MSGL_V
, "packet 0x82(pos=%u) chunk=%x\n",
330 (int)stream_tell(demux
->stream
), c
);
333 case 0x00: // header - skip it!
335 len
=stream_read_char(demux
->stream
);
336 if(len
>=0x80) len
=0x80*(len
-0x80)+stream_read_char(demux
->stream
);
337 mp_msg(MSGT_DEMUX
, MSGL_V
, "vivo extra header: %d bytes\n",len
);
341 /* also try to parse all headers */
342 pos
= stream_tell(demux
->stream
);
343 vivo_parse_text_header(demux
, len
);
344 stream_seek(demux
->stream
, pos
);
349 case 0x10: // video packet
351 len
= stream_read_char(demux
->stream
);
356 case 0x20: // video packet
357 len
=stream_read_char(demux
->stream
);
360 case 0x30: // audio packet
362 len
= stream_read_char(demux
->stream
);
368 case 0x40: // audio packet
370 len
= stream_read_char(demux
->stream
);
377 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"VIVO - unknown ID found: %02X at pos %"PRIu64
" contact author!\n",
378 c
, (int64_t)stream_tell(demux
->stream
));
382 // printf("chunk=%x, len=%d\n", c, len);
384 if(!ds
|| ds
->id
<-1){
385 if(len
) stream_skip(demux
->stream
,len
);
392 if(ds
->asf_seq
!=seq
){
393 // closed segment, finalize packet:
394 ds_add_packet(ds
,ds
->asf_packet
);
396 // printf("packet!\n");
398 // append data to it!
399 demux_packet_t
* dp
=ds
->asf_packet
;
400 if(dp
->len
+ len
+ MP_INPUT_BUFFER_PADDING_SIZE
< 0)
402 dp
->buffer
=realloc(dp
->buffer
,dp
->len
+len
+MP_INPUT_BUFFER_PADDING_SIZE
);
403 memset(dp
->buffer
+dp
->len
+len
, 0, MP_INPUT_BUFFER_PADDING_SIZE
);
404 //memcpy(dp->buffer+dp->len,data,len);
405 stream_read(demux
->stream
,dp
->buffer
+dp
->len
,len
);
406 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"data appended! %d+%d\n",dp
->len
,len
);
409 if((c
&0xF0)==0x20) --ds
->asf_seq
; // hack!
413 // create new packet:
414 { demux_packet_t
* dp
;
415 dp
=new_demux_packet(len
);
416 //memcpy(dp->buffer,data,len);
417 stream_read(demux
->stream
,dp
->buffer
,len
);
418 dp
->pts
=audio_rate
?((float)audio_pos
/(float)audio_rate
):0;
419 // dp->flags=keyframe;
420 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur);
421 dp
->pos
=demux
->filepos
;
430 static const short h263_format
[8][2] = {
437 { 320, 240 } // ??????? or 240x180 (found in vivo2) ?
440 static unsigned char* buffer
;
443 static unsigned char buf
=0;
444 static int format
, width
, height
;
446 static unsigned int x_get_bits(int n
){
451 buf
=buffer
[bufptr
++];
454 //x=(x<<1)|(buf&1);buf>>=1;
455 x
=(x
<<1)|(buf
>>7);buf
<<=1;
461 #define get_bits(xxx,n) x_get_bits(n)
462 #define get_bits1(xxx) x_get_bits(1)
463 #define skip_bits(xxx,n) x_get_bits(n)
464 #define skip_bits1(xxx) x_get_bits(1)
466 /* most is hardcoded. should extend to handle all h263 streams */
467 static int h263_decode_picture_header(unsigned char *b_ptr
)
471 // for(i=0;i<16;i++) printf(" %02X",b_ptr[i]); printf("\n");
477 if (get_bits(&s
->gb
, 22) != 0x20){
478 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad picture header\n");
481 skip_bits(&s
->gb
, 8); /* picture timestamp */
483 if (get_bits1(&s
->gb
) != 1){
484 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad marker\n");
485 return -1; /* marker */
487 if (get_bits1(&s
->gb
) != 0){
488 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad h263 id\n");
489 return -1; /* h263 id */
491 skip_bits1(&s
->gb
); /* split screen off */
492 skip_bits1(&s
->gb
); /* camera off */
493 skip_bits1(&s
->gb
); /* freeze picture release off */
495 format
= get_bits(&s
->gb
, 3);
498 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 0 format = %d\n", format
);
500 width
= h263_format
[format
][0];
501 height
= h263_format
[format
][1];
502 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
503 // if (!width) return -1;
505 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits1(&s
->gb
));
506 mp_msg(MSGT_DEMUX
, MSGL_V
, "unrestricted_mv=%d\n", get_bits1(&s
->gb
));
508 mp_msg(MSGT_DEMUX
, MSGL_V
, "SAC: %d\n", get_bits1(&s
->gb
));
509 mp_msg(MSGT_DEMUX
, MSGL_V
, "advanced prediction mode: %d\n", get_bits1(&s
->gb
));
510 mp_msg(MSGT_DEMUX
, MSGL_V
, "PB frame: %d\n", get_bits1(&s
->gb
));
512 if (get_bits1(&s
->gb
) != 0)
513 return -1; /* SAC: off */
514 if (get_bits1(&s
->gb
) != 0)
515 return -1; /* advanced prediction mode: off */
516 if (get_bits1(&s
->gb
) != 0)
517 return -1; /* not PB frame */
519 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
520 skip_bits1(&s
->gb
); /* Continuous Presence Multipoint mode: off */
522 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 1\n");
524 if (get_bits(&s
->gb
, 3) != 1){
525 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "H.263v2 A error\n");
528 if (get_bits(&s
->gb
, 3) != 6){ /* custom source format */
529 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "custom source format\n");
532 skip_bits(&s
->gb
, 12);
533 skip_bits(&s
->gb
, 3);
534 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits(&s
->gb
, 3) + 1);
535 // if (s->pict_type != I_TYPE &&
536 // s->pict_type != P_TYPE)
538 skip_bits(&s
->gb
, 7);
539 skip_bits(&s
->gb
, 4); /* aspect ratio */
540 width
= (get_bits(&s
->gb
, 9) + 1) * 4;
542 height
= get_bits(&s
->gb
, 9) * 4;
543 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
546 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
550 while (get_bits1(&s
->gb
) != 0) {
551 skip_bits(&s
->gb
, 8);
555 // s->height = height;
561 static demuxer_t
* demux_open_vivo(demuxer_t
* demuxer
){
562 vivo_priv_t
* priv
=demuxer
->priv
;
564 if(!ds_fill_buffer(demuxer
->video
)){
565 mp_tmsg(MSGT_DEMUX
,MSGL_ERR
,"VIVO: " "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
=malloc(sizeof(BITMAPINFOHEADER
));
617 memset(sh
->bih
,0,sizeof(BITMAPINFOHEADER
));
620 sh
->bih
->biWidth
= priv
->width
;
622 sh
->bih
->biWidth
= width
;
624 sh
->bih
->biHeight
= priv
->height
;
626 sh
->bih
->biHeight
= height
;
628 sh
->bih
->biBitCount
=24;
629 sh
->bih
->biCompression
=sh
->format
;
630 sh
->bih
->biSizeImage
=sh
->bih
->biWidth
*sh
->bih
->biHeight
*3;
632 /* insert as stream */
633 demuxer
->video
->sh
=sh
;
634 sh
->ds
=demuxer
->video
;
635 demuxer
->video
->id
=0;
637 /* disable seeking */
638 demuxer
->seekable
= 0;
640 mp_msg(MSGT_DEMUX
,MSGL_V
,"VIVO Video stream %d size: display: %dx%d, codec: %ux%u\n",
641 demuxer
->video
->id
, sh
->disp_w
, sh
->disp_h
, sh
->bih
->biWidth
,
646 if (demuxer
->audio
->id
>= -1){
647 if(!ds_fill_buffer(demuxer
->audio
)){
648 mp_tmsg(MSGT_DEMUX
,MSGL_ERR
,"VIVO: " "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
=malloc(sizeof(WAVEFORMATEX
));
682 memset(sh
->wf
,0,sizeof(WAVEFORMATEX
));
683 sh
->wf
->wFormatTag
=sh
->format
;
684 sh
->wf
->nChannels
=1; /* 1 channels for both Siren and G.723 */
686 /* Set bits per sample */
687 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
688 sh
->wf
->wBitsPerSample
= 16;
690 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
691 sh
->wf
->wBitsPerSample
= 8;
693 /* Set sampling rate */
694 if (priv
->audio_samplerate
) /* got from header */
695 sh
->wf
->nSamplesPerSec
= priv
->audio_samplerate
;
698 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
699 sh
->wf
->nSamplesPerSec
= 16000;
700 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
701 sh
->wf
->nSamplesPerSec
= 8000;
703 if (vivo_param_samplerate
!= -1)
704 sh
->wf
->nSamplesPerSec
= vivo_param_samplerate
;
706 /* Set audio bitrate */
707 if (priv
->audio_bitrate
) /* got from header */
708 sh
->wf
->nAvgBytesPerSec
= priv
->audio_bitrate
;
711 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
712 sh
->wf
->nAvgBytesPerSec
= 2000;
713 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
714 sh
->wf
->nAvgBytesPerSec
= 800;
716 if (vivo_param_abitrate
!= -1)
717 sh
->wf
->nAvgBytesPerSec
= vivo_param_abitrate
;
718 audio_rate
=sh
->wf
->nAvgBytesPerSec
;
720 if (!priv
->audio_bytesperblock
)
722 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
723 sh
->wf
->nBlockAlign
= 40;
724 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
725 sh
->wf
->nBlockAlign
= 24;
728 sh
->wf
->nBlockAlign
= priv
->audio_bytesperblock
;
729 if (vivo_param_bytesperblock
!= -1)
730 sh
->wf
->nBlockAlign
= vivo_param_bytesperblock
;
733 /* insert as stream */
734 demuxer
->audio
->sh
=sh
;
735 sh
->ds
=demuxer
->audio
;
736 demuxer
->audio
->id
=1;
744 static void demux_close_vivo(demuxer_t
*demuxer
)
746 vivo_priv_t
* priv
=demuxer
->priv
;
754 free(priv
->copyright
);
756 free(priv
->producer
);
763 const demuxer_desc_t demuxer_desc_vivo
= {
767 "A'rpi, Alex Beregszasi",
770 0, // unsafe autodetect
772 demux_vivo_fill_buffer
,