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
;
101 int parser_in_audio_block
= 0;
105 priv
= malloc(sizeof(vivo_priv_t
));
106 memset(priv
, 0, sizeof(vivo_priv_t
));
111 buf
= malloc(header_len
+ 1);
112 opt
= malloc(header_len
+ 1);
113 param
= malloc(header_len
+ 1);
114 stream_read(demux
->stream
, buf
, header_len
);
117 token
= strtok(buf
, (char *)&("\x0d\x0a"));
120 if (sscanf(token
, "%[^:]:%[^\n]", opt
, param
) != 2)
122 mp_msg(MSGT_DEMUX
, MSGL_V
, "viv_text_header_parser: bad line: '%s' at ~%#"PRIx64
"\n",
123 token
, (int64_t)stream_tell(demux
->stream
));
126 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token: '%s' (%zd bytes)\n",
127 token
, strlen(token
));
128 mp_dbg(MSGT_DEMUX
, MSGL_DBG3
, "token => o: '%s', p: '%s'\n",
131 /* checking versions: only v1 or v2 is suitable (or known?:) */
132 if (!strcmp(opt
, "Version"))
134 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Version: %s\n", param
);
135 if (!strncmp(param
, "Vivo/1", 6) || !strncmp(param
, "Vivo/2", 6))
138 /* save major version for fourcc */
139 priv
->version
= param
[5];
144 if (!strcmp(opt
, "FPS"))
146 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "FPS: %f\n", atof(param
));
147 priv
->fps
= atof(param
);
149 if (!strcmp(opt
, "Width"))
151 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Width: %d\n", atoi(param
));
152 priv
->width
= atoi(param
);
154 if (!strcmp(opt
, "Height"))
156 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Height: %d\n", atoi(param
));
157 priv
->height
= atoi(param
);
159 if (!strcmp(opt
, "DisplayWidth"))
161 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Width: %d\n", atoi(param
));
162 priv
->disp_width
= atoi(param
);
164 if (!strcmp(opt
, "DisplayHeight"))
166 mp_msg(MSGT_DEMUX
, MSGL_DBG2
, "Display Height: %d\n", atoi(param
));
167 priv
->disp_height
= atoi(param
);
171 if (!strcmp(opt
, "RecordType"))
173 /* no audio recordblock by Vivo/1.00, 3 and 4 by Vivo/2.00 */
174 if ((atoi(param
) == 3) || (atoi(param
) == 4))
175 parser_in_audio_block
= 1;
177 parser_in_audio_block
= 0;
179 if (!strcmp(opt
, "NominalBitrate"))
181 priv
->audio_bitrate
= atoi(param
);
182 if (priv
->audio_bitrate
== 2000)
183 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
184 if (priv
->audio_bitrate
== 800)
185 priv
->audio_codec
= VIVO_AUDIO_G723
;
187 if (!strcmp(opt
, "SamplingFrequency"))
189 priv
->audio_samplerate
= atoi(param
);
190 if (priv
->audio_samplerate
== 16000)
191 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
192 if (priv
->audio_samplerate
== 8000)
193 priv
->audio_codec
= VIVO_AUDIO_G723
;
195 if (!strcmp(opt
, "Length") && (parser_in_audio_block
== 1))
197 priv
->audio_bytesperblock
= atoi(param
); /* 24 or 40 kbps */
198 if (priv
->audio_bytesperblock
== 40)
199 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
200 if (priv
->audio_bytesperblock
== 24)
201 priv
->audio_codec
= VIVO_AUDIO_G723
;
204 /* only for displaying some informations about movie*/
205 if (!strcmp(opt
, "Title"))
207 demux_info_add(demux
, "title", param
);
208 priv
->title
= strdup(param
);
210 if (!strcmp(opt
, "Author"))
212 demux_info_add(demux
, "author", param
);
213 priv
->author
= strdup(param
);
215 if (!strcmp(opt
, "Copyright"))
217 demux_info_add(demux
, "copyright", param
);
218 priv
->copyright
= strdup(param
);
220 if (!strcmp(opt
, "Producer"))
222 demux_info_add(demux
, "encoder", param
);
223 priv
->producer
= strdup(param
);
227 token
= strtok(NULL
, (char *)&("\x0d\x0a"));
235 static int vivo_check_file(demuxer_t
* demuxer
){
239 unsigned char buf
[2048+256];
241 int orig_pos
= stream_tell(demuxer
->stream
);
243 mp_msg(MSGT_DEMUX
,MSGL_V
,"Checking for VIVO\n");
245 c
=stream_read_char(demuxer
->stream
);
246 if(c
==-256) return 0;
248 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
250 if(len
>1024) return 0;
253 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 1 size: %d\n",len
);
254 //stream_skip(demuxer->stream,len);
256 priv
=malloc(sizeof(vivo_priv_t
));
257 memset(priv
,0,sizeof(vivo_priv_t
));
261 vivo_parse_text_header(demuxer
, len
);
262 if (priv
->supported
== 0)
265 /* this is enought for check (for now) */
266 stream_read(demuxer
->stream
,buf
,len
);
268 // printf("VIVO header: '%s'\n",buf);
272 while(i
<len
&& buf
[i
]==0x0D && buf
[i
+1]==0x0A) i
+=2; // skip empty lines
273 if(strncmp(buf
+i
,"Version:Vivo/",13)) return 0; // bad version/type!
277 c
=stream_read_char(demuxer
->stream
);
280 while((c
=stream_read_char(demuxer
->stream
))>=0x80){
282 if(len
+len2
>2048) return 0;
285 mp_msg(MSGT_DEMUX
,MSGL_V
,"header block 2 size: %d\n",len2
);
286 stream_skip(demuxer
->stream
,len2
);
287 // stream_read(demuxer->stream,buf+len,len2);
290 // c=stream_read_char(demuxer->stream);
291 // printf("first packet: %02X\n",c);
293 stream_seek(demuxer
->stream
, orig_pos
);
295 return DEMUXER_TYPE_VIVO
;
298 static int audio_pos
=0;
299 static int audio_rate
=0;
302 // 0 = EOF or no stream found
303 // 1 = successfully read a packet
304 static int demux_vivo_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
){
305 demux_stream_t
*ds
=NULL
;
310 demux
->filepos
=stream_tell(demux
->stream
);
312 c
=stream_read_char(demux
->stream
);
313 if (c
== -256) /* EOF */
315 // printf("c=%x,%02X\n",c,c&0xf0);
318 /* ok, this works, but pts calculating from header is required! */
319 /* FIXME: "Calculate PTS from picture header!" */
321 c
= stream_read_char(demux
->stream
);
322 mp_msg(MSGT_DEMUX
, MSGL_V
, "packet 0x82(pos=%u) chunk=%x\n",
323 (int)stream_tell(demux
->stream
), c
);
326 case 0x00: // header - skip it!
328 len
=stream_read_char(demux
->stream
);
329 if(len
>=0x80) len
=0x80*(len
-0x80)+stream_read_char(demux
->stream
);
330 mp_msg(MSGT_DEMUX
, MSGL_V
, "vivo extra header: %d bytes\n",len
);
334 /* also try to parse all headers */
335 pos
= stream_tell(demux
->stream
);
336 vivo_parse_text_header(demux
, len
);
337 stream_seek(demux
->stream
, pos
);
342 case 0x10: // video packet
344 len
= stream_read_char(demux
->stream
);
349 case 0x20: // video packet
350 len
=stream_read_char(demux
->stream
);
353 case 0x30: // audio packet
355 len
= stream_read_char(demux
->stream
);
361 case 0x40: // audio packet
363 len
= stream_read_char(demux
->stream
);
370 mp_msg(MSGT_DEMUX
,MSGL_WARN
,"VIVO - unknown ID found: %02X at pos %"PRIu64
" contact author!\n",
371 c
, (int64_t)stream_tell(demux
->stream
));
375 // printf("chunk=%x, len=%d\n", c, len);
377 if(!ds
|| ds
->id
<-1){
378 if(len
) stream_skip(demux
->stream
,len
);
385 if(ds
->asf_seq
!=seq
){
386 // closed segment, finalize packet:
387 ds_add_packet(ds
,ds
->asf_packet
);
389 // printf("packet!\n");
391 // append data to it!
392 demux_packet_t
* dp
=ds
->asf_packet
;
393 if(dp
->len
+ len
+ MP_INPUT_BUFFER_PADDING_SIZE
< 0)
395 dp
->buffer
=realloc(dp
->buffer
,dp
->len
+len
+MP_INPUT_BUFFER_PADDING_SIZE
);
396 memset(dp
->buffer
+dp
->len
+len
, 0, MP_INPUT_BUFFER_PADDING_SIZE
);
397 //memcpy(dp->buffer+dp->len,data,len);
398 stream_read(demux
->stream
,dp
->buffer
+dp
->len
,len
);
399 mp_dbg(MSGT_DEMUX
,MSGL_DBG4
,"data appended! %d+%d\n",dp
->len
,len
);
402 if((c
&0xF0)==0x20) --ds
->asf_seq
; // hack!
406 // create new packet:
407 { demux_packet_t
* dp
;
408 dp
=new_demux_packet(len
);
409 //memcpy(dp->buffer,data,len);
410 stream_read(demux
->stream
,dp
->buffer
,len
);
411 dp
->pts
=audio_rate
?((float)audio_pos
/(float)audio_rate
):0;
412 // dp->flags=keyframe;
413 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur);
414 dp
->pos
=demux
->filepos
;
423 static const short h263_format
[8][2] = {
430 { 320, 240 } // ??????? or 240x180 (found in vivo2) ?
433 static unsigned char* buffer
;
436 static unsigned char buf
=0;
437 static int format
, width
, height
;
439 static unsigned int x_get_bits(int n
){
444 buf
=buffer
[bufptr
++];
447 //x=(x<<1)|(buf&1);buf>>=1;
448 x
=(x
<<1)|(buf
>>7);buf
<<=1;
454 #define get_bits(xxx,n) x_get_bits(n)
455 #define get_bits1(xxx) x_get_bits(1)
456 #define skip_bits(xxx,n) x_get_bits(n)
457 #define skip_bits1(xxx) x_get_bits(1)
459 /* most is hardcoded. should extend to handle all h263 streams */
460 static int h263_decode_picture_header(unsigned char *b_ptr
)
464 // for(i=0;i<16;i++) printf(" %02X",b_ptr[i]); printf("\n");
470 if (get_bits(&s
->gb
, 22) != 0x20){
471 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad picture header\n");
474 skip_bits(&s
->gb
, 8); /* picture timestamp */
476 if (get_bits1(&s
->gb
) != 1){
477 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad marker\n");
478 return -1; /* marker */
480 if (get_bits1(&s
->gb
) != 0){
481 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "bad h263 id\n");
482 return -1; /* h263 id */
484 skip_bits1(&s
->gb
); /* split screen off */
485 skip_bits1(&s
->gb
); /* camera off */
486 skip_bits1(&s
->gb
); /* freeze picture release off */
488 format
= get_bits(&s
->gb
, 3);
491 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 0 format = %d\n", format
);
493 width
= h263_format
[format
][0];
494 height
= h263_format
[format
][1];
495 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
496 // if (!width) return -1;
498 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits1(&s
->gb
));
499 mp_msg(MSGT_DEMUX
, MSGL_V
, "unrestricted_mv=%d\n", get_bits1(&s
->gb
));
501 mp_msg(MSGT_DEMUX
, MSGL_V
, "SAC: %d\n", get_bits1(&s
->gb
));
502 mp_msg(MSGT_DEMUX
, MSGL_V
, "advanced prediction mode: %d\n", get_bits1(&s
->gb
));
503 mp_msg(MSGT_DEMUX
, MSGL_V
, "PB frame: %d\n", get_bits1(&s
->gb
));
505 if (get_bits1(&s
->gb
) != 0)
506 return -1; /* SAC: off */
507 if (get_bits1(&s
->gb
) != 0)
508 return -1; /* advanced prediction mode: off */
509 if (get_bits1(&s
->gb
) != 0)
510 return -1; /* not PB frame */
512 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
513 skip_bits1(&s
->gb
); /* Continuous Presence Multipoint mode: off */
515 mp_msg(MSGT_DEMUX
, MSGL_V
, "h263_plus = 1\n");
517 if (get_bits(&s
->gb
, 3) != 1){
518 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "H.263v2 A error\n");
521 if (get_bits(&s
->gb
, 3) != 6){ /* custom source format */
522 mp_msg(MSGT_DEMUX
, MSGL_FATAL
, "custom source format\n");
525 skip_bits(&s
->gb
, 12);
526 skip_bits(&s
->gb
, 3);
527 mp_msg(MSGT_DEMUX
, MSGL_V
, "pict_type=%d\n", get_bits(&s
->gb
, 3) + 1);
528 // if (s->pict_type != I_TYPE &&
529 // s->pict_type != P_TYPE)
531 skip_bits(&s
->gb
, 7);
532 skip_bits(&s
->gb
, 4); /* aspect ratio */
533 width
= (get_bits(&s
->gb
, 9) + 1) * 4;
535 height
= get_bits(&s
->gb
, 9) * 4;
536 mp_msg(MSGT_DEMUX
, MSGL_V
, "%d x %d\n", width
, height
);
539 mp_msg(MSGT_DEMUX
, MSGL_V
, "qscale=%d\n", get_bits(&s
->gb
, 5));
543 while (get_bits1(&s
->gb
) != 0) {
544 skip_bits(&s
->gb
, 8);
548 // s->height = height;
554 static demuxer_t
* demux_open_vivo(demuxer_t
* demuxer
){
555 vivo_priv_t
* priv
=demuxer
->priv
;
557 if(!ds_fill_buffer(demuxer
->video
)){
558 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "VIVO: %s",
559 mp_gtext("Missing video stream!? Contact the author, it may be a bug :(\n"));
565 h263_decode_picture_header(demuxer
->video
->buffer
);
567 if (vivo_param_version
!= -1)
568 priv
->version
= '0' + vivo_param_version
;
570 { sh_video_t
* sh
=new_sh_video(demuxer
,0);
572 /* viv1, viv2 (for better codecs.conf) */
573 sh
->format
= mmioFOURCC('v', 'i', 'v', priv
->version
);
581 sh
->frametime
=1.0f
/sh
->fps
;
583 /* XXX: FIXME: can't scale image. */
584 /* hotfix to disable: */
585 priv
->disp_width
= priv
->width
;
586 priv
->disp_height
= priv
->height
;
588 if (vivo_param_width
!= -1)
589 priv
->disp_width
= priv
->width
= vivo_param_width
;
591 if (vivo_param_height
!= -1)
592 priv
->disp_height
= priv
->height
= vivo_param_height
;
594 if (vivo_param_vformat
!= -1)
596 priv
->disp_width
= priv
->width
= h263_format
[vivo_param_vformat
][0];
597 priv
->disp_height
= priv
->height
= h263_format
[vivo_param_vformat
][1];
600 if (priv
->disp_width
)
601 sh
->disp_w
= priv
->disp_width
;
604 if (priv
->disp_height
)
605 sh
->disp_h
= priv
->disp_height
;
609 // emulate BITMAPINFOHEADER:
610 sh
->bih
=calloc(1, sizeof(*sh
->bih
));
613 sh
->bih
->biWidth
= priv
->width
;
615 sh
->bih
->biWidth
= width
;
617 sh
->bih
->biHeight
= priv
->height
;
619 sh
->bih
->biHeight
= height
;
621 sh
->bih
->biBitCount
=24;
622 sh
->bih
->biCompression
=sh
->format
;
623 sh
->bih
->biSizeImage
=sh
->bih
->biWidth
*sh
->bih
->biHeight
*3;
625 /* insert as stream */
626 demuxer
->video
->sh
=sh
;
627 sh
->ds
=demuxer
->video
;
628 demuxer
->video
->id
=0;
630 /* disable seeking */
631 demuxer
->seekable
= 0;
633 mp_msg(MSGT_DEMUX
,MSGL_V
,"VIVO Video stream %d size: display: %dx%d, codec: %ux%u\n",
634 demuxer
->video
->id
, sh
->disp_w
, sh
->disp_h
, sh
->bih
->biWidth
,
639 if (demuxer
->audio
->id
>= -1){
640 if(!ds_fill_buffer(demuxer
->audio
)){
641 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "VIVO: %s",
642 mp_gtext("No audio stream found -> no sound.\n"));
644 { sh_audio_t
* sh
=new_sh_audio(demuxer
,1);
646 /* Select audio codec */
647 if (priv
->audio_codec
== 0)
649 if (priv
->version
== '2')
650 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
652 priv
->audio_codec
= VIVO_AUDIO_G723
;
654 if (vivo_param_acodec
!= NULL
)
656 if (!strcasecmp(vivo_param_acodec
, "g723"))
657 priv
->audio_codec
= VIVO_AUDIO_G723
;
658 if (!strcasecmp(vivo_param_acodec
, "siren"))
659 priv
->audio_codec
= VIVO_AUDIO_SIREN
;
662 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
664 else if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
668 mp_msg(MSGT_DEMUX
, MSGL_ERR
, "VIVO: Not support audio codec (%d)\n",
670 free_sh_audio(demuxer
, 1);
674 // Emulate WAVEFORMATEX struct:
675 sh
->wf
=calloc(1, sizeof(*sh
->wf
));
676 sh
->wf
->wFormatTag
=sh
->format
;
677 sh
->wf
->nChannels
=1; /* 1 channels for both Siren and G.723 */
679 /* Set bits per sample */
680 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
681 sh
->wf
->wBitsPerSample
= 16;
683 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
684 sh
->wf
->wBitsPerSample
= 8;
686 /* Set sampling rate */
687 if (priv
->audio_samplerate
) /* got from header */
688 sh
->wf
->nSamplesPerSec
= priv
->audio_samplerate
;
691 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
692 sh
->wf
->nSamplesPerSec
= 16000;
693 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
694 sh
->wf
->nSamplesPerSec
= 8000;
696 if (vivo_param_samplerate
!= -1)
697 sh
->wf
->nSamplesPerSec
= vivo_param_samplerate
;
699 /* Set audio bitrate */
700 if (priv
->audio_bitrate
) /* got from header */
701 sh
->wf
->nAvgBytesPerSec
= priv
->audio_bitrate
;
704 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
705 sh
->wf
->nAvgBytesPerSec
= 2000;
706 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
707 sh
->wf
->nAvgBytesPerSec
= 800;
709 if (vivo_param_abitrate
!= -1)
710 sh
->wf
->nAvgBytesPerSec
= vivo_param_abitrate
;
711 audio_rate
=sh
->wf
->nAvgBytesPerSec
;
713 if (!priv
->audio_bytesperblock
)
715 if (priv
->audio_codec
== VIVO_AUDIO_SIREN
)
716 sh
->wf
->nBlockAlign
= 40;
717 if (priv
->audio_codec
== VIVO_AUDIO_G723
)
718 sh
->wf
->nBlockAlign
= 24;
721 sh
->wf
->nBlockAlign
= priv
->audio_bytesperblock
;
722 if (vivo_param_bytesperblock
!= -1)
723 sh
->wf
->nBlockAlign
= vivo_param_bytesperblock
;
726 /* insert as stream */
727 demuxer
->audio
->sh
=sh
;
728 sh
->ds
=demuxer
->audio
;
729 demuxer
->audio
->id
=1;
737 static void demux_close_vivo(demuxer_t
*demuxer
)
739 vivo_priv_t
* priv
=demuxer
->priv
;
744 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
,