2687
|
1 // VIVO file parser by A'rpi
|
|
2
|
|
3 #include <stdio.h>
|
|
4 #include <stdlib.h>
|
|
5 #include <unistd.h>
|
|
6
|
|
7 #include "config.h"
|
|
8 #include "mp_msg.h"
|
|
9 #include "help_mp.h"
|
|
10
|
|
11 #include "stream.h"
|
|
12 #include "demuxer.h"
|
|
13 #include "stheader.h"
|
|
14
|
|
15 #include "bswap.h"
|
|
16
|
|
17 typedef struct {
|
|
18 float fps;
|
|
19 } vivo_priv_t;
|
|
20
|
|
21 int vivo_check_file(demuxer_t* demuxer){
|
|
22 int flags=0;
|
|
23 int i=0;
|
|
24 int len;
|
|
25 int len2;
|
|
26 int c;
|
|
27 unsigned char buf[2048+256];
|
|
28 vivo_priv_t* priv;
|
|
29
|
|
30 mp_msg(MSGT_DEMUX,MSGL_V,"Checking for VIVO\n");
|
|
31
|
|
32 c=stream_read_char(demuxer->stream);
|
|
33 if(c) return 0;
|
|
34 len=0;
|
|
35 while((c=stream_read_char(demuxer->stream))>=0x80){
|
|
36 len+=0x80*(c-0x80);
|
|
37 if(len>1024) return 0;
|
|
38 }
|
|
39 len+=c;
|
|
40 printf("header block 1 size: %d\n",len);
|
|
41 //stream_skip(demuxer->stream,len);
|
|
42 stream_read(demuxer->stream,buf,len);
|
|
43 buf[len]=0;
|
|
44 // printf("VIVO header: '%s'\n",buf);
|
|
45
|
|
46 // parse header:
|
|
47 i=0;
|
|
48 while(i<len && buf[i]==0x0D && buf[i+1]==0x0A) i+=2; // skip empty lines
|
2695
|
49 if(strncmp(buf+i,"Version:Vivo/",13)) return 0; // bad version/type!
|
2687
|
50
|
|
51 priv=malloc(sizeof(vivo_priv_t));
|
|
52 memset(priv,0,sizeof(vivo_priv_t));
|
|
53 demuxer->priv=priv;
|
|
54
|
|
55 // TODO: parse FPS and other info (display title/author etc)
|
|
56 priv->fps=10.0; // FIXME (parse from header)
|
|
57
|
2695
|
58 #if 0
|
2687
|
59 c=stream_read_char(demuxer->stream);
|
|
60 if(c) return 0;
|
|
61 len2=0;
|
|
62 while((c=stream_read_char(demuxer->stream))>=0x80){
|
|
63 len2+=0x80*(c-0x80);
|
|
64 if(len+len2>2048) return 0;
|
|
65 }
|
|
66 len2+=c;
|
|
67 printf("header block 2 size: %d\n",len2);
|
|
68 stream_skip(demuxer->stream,len2);
|
|
69 // stream_read(demuxer->stream,buf+len,len2);
|
2695
|
70 #endif
|
2687
|
71
|
|
72 // c=stream_read_char(demuxer->stream);
|
|
73 // printf("first packet: %02X\n",c);
|
|
74
|
|
75 return 1;
|
|
76 }
|
|
77
|
|
78
|
|
79 // return value:
|
|
80 // 0 = EOF or no stream found
|
|
81 // 1 = successfully read a packet
|
|
82 int demux_vivo_fill_buffer(demuxer_t *demux){
|
|
83 demux_stream_t *ds=NULL;
|
|
84 int c;
|
|
85 int len=0;
|
|
86 int seq;
|
|
87 demux->filepos=stream_tell(demux->stream);
|
|
88
|
|
89 c=stream_read_char(demux->stream);
|
2695
|
90 // printf("c=%02X\n",c);
|
2687
|
91 switch(c&0xF0){
|
2695
|
92 case 0x00: // header - skip it!
|
2687
|
93 len=stream_read_char(demux->stream);
|
2695
|
94 // printf("header: %02X\n",len);
|
|
95 if(len>=0x80) len=0x80*(len-0x80)+stream_read_char(demux->stream);
|
|
96 printf("vivo extra header: %d bytes\n",len);
|
2687
|
97 break;
|
|
98 case 0x10: // video packet
|
|
99 len=128;
|
|
100 ds=demux->video;
|
|
101 break;
|
|
102 case 0x20: // video packet
|
|
103 len=stream_read_char(demux->stream);
|
|
104 ds=demux->video;
|
|
105 break;
|
2695
|
106 case 0x30: // audio ?
|
|
107 len=0x28;
|
|
108 ds=demux->audio;
|
|
109 break;
|
2687
|
110 case 0x40: // audio packet
|
|
111 len=24;
|
|
112 ds=demux->audio;
|
|
113 break;
|
|
114 default:
|
|
115 mp_msg(MSGT_DEMUX,MSGL_WARN,"VIVO - unknown ID found: %02X contact author!\n",c);
|
|
116 }
|
|
117
|
2695
|
118 if(!ds || ds->id<-1){
|
2687
|
119 if(len) stream_skip(demux->stream,len);
|
|
120 return 1;
|
|
121 }
|
|
122
|
|
123 seq=c&0x0F;
|
|
124
|
|
125 if(ds->asf_packet){
|
|
126 if(ds->asf_seq!=seq){
|
|
127 // closed segment, finalize packet:
|
|
128 ds_add_packet(ds,ds->asf_packet);
|
|
129 ds->asf_packet=NULL;
|
2695
|
130 // printf("packet!\n");
|
2687
|
131 } else {
|
|
132 // append data to it!
|
|
133 demux_packet_t* dp=ds->asf_packet;
|
|
134 dp->buffer=realloc(dp->buffer,dp->len+len);
|
|
135 //memcpy(dp->buffer+dp->len,data,len);
|
|
136 stream_read(demux->stream,dp->buffer+dp->len,len);
|
|
137 mp_dbg(MSGT_DEMUX,MSGL_DBG4,"data appended! %d+%d\n",dp->len,len);
|
|
138 dp->len+=len;
|
|
139 // we are ready now.
|
2695
|
140 if((c&0xF0)==0x20) --ds->asf_seq; // hack!
|
2687
|
141 return 1;
|
|
142 }
|
|
143 }
|
|
144 // create new packet:
|
|
145 { demux_packet_t* dp;
|
|
146 dp=new_demux_packet(len);
|
|
147 //memcpy(dp->buffer,data,len);
|
|
148 stream_read(demux->stream,dp->buffer,len);
|
|
149 // dp->pts=time*0.001f;
|
|
150 // dp->flags=keyframe;
|
|
151 // if(ds==demux->video) printf("ASF time: %8d dur: %5d \n",time,dur);
|
|
152 dp->pos=demux->filepos;
|
|
153 ds->asf_packet=dp;
|
|
154 ds->asf_seq=seq;
|
|
155 // we are ready now.
|
|
156 return 1;
|
|
157 }
|
|
158
|
|
159 }
|
|
160
|
|
161 static const short h263_format[8][2] = {
|
|
162 { 0, 0 },
|
|
163 { 128, 96 },
|
|
164 { 176, 144 },
|
|
165 { 352, 288 },
|
|
166 { 704, 576 },
|
|
167 { 1408, 1152 },
|
2695
|
168 { 320, 240 } // ???????
|
2687
|
169 };
|
|
170
|
|
171 static unsigned char* buffer;
|
|
172 static int bufptr=0;
|
|
173 static int bitcnt=0;
|
|
174 static unsigned char buf=0;
|
|
175 static int format, width, height;
|
|
176
|
|
177 static unsigned int x_get_bits(int n){
|
|
178 unsigned int x=0;
|
|
179 while(n-->0){
|
|
180 if(!bitcnt){
|
|
181 // fill buff
|
|
182 buf=buffer[bufptr++];
|
|
183 bitcnt=8;
|
|
184 }
|
|
185 //x=(x<<1)|(buf&1);buf>>=1;
|
|
186 x=(x<<1)|(buf>>7);buf<<=1;
|
|
187 --bitcnt;
|
|
188 }
|
|
189 return x;
|
|
190 }
|
|
191
|
|
192 #define get_bits(xxx,n) x_get_bits(n)
|
|
193 #define get_bits1(xxx) x_get_bits(1)
|
|
194 #define skip_bits(xxx,n) x_get_bits(n)
|
|
195 #define skip_bits1(xxx) x_get_bits(1)
|
|
196
|
|
197 /* most is hardcoded. should extend to handle all h263 streams */
|
|
198 static int h263_decode_picture_header(unsigned char *b_ptr)
|
|
199 {
|
2695
|
200 int i;
|
|
201
|
|
202 // for(i=0;i<16;i++) printf(" %02X",b_ptr[i]); printf("\n");
|
2687
|
203
|
|
204 buffer=b_ptr;
|
|
205 bufptr=bitcnt=buf=0;
|
|
206
|
|
207 /* picture header */
|
2695
|
208 if (get_bits(&s->gb, 22) != 0x20){
|
|
209 printf("bad picture header\n");
|
2687
|
210 return -1;
|
2695
|
211 }
|
2687
|
212 skip_bits(&s->gb, 8); /* picture timestamp */
|
|
213
|
2695
|
214 if (get_bits1(&s->gb) != 1){
|
|
215 printf("bad marker\n");
|
2687
|
216 return -1; /* marker */
|
2695
|
217 }
|
|
218 if (get_bits1(&s->gb) != 0){
|
|
219 printf("bad h263 id\n");
|
2687
|
220 return -1; /* h263 id */
|
2695
|
221 }
|
2687
|
222 skip_bits1(&s->gb); /* split screen off */
|
|
223 skip_bits1(&s->gb); /* camera off */
|
|
224 skip_bits1(&s->gb); /* freeze picture release off */
|
|
225
|
|
226 format = get_bits(&s->gb, 3);
|
|
227
|
|
228 if (format != 7) {
|
|
229 printf("h263_plus = 0 format = %d\n",format);
|
|
230 /* H.263v1 */
|
|
231 width = h263_format[format][0];
|
|
232 height = h263_format[format][1];
|
|
233 printf("%d x %d\n",width,height);
|
2695
|
234 // if (!width) return -1;
|
2687
|
235
|
|
236 printf("pict_type=%d\n",get_bits1(&s->gb));
|
|
237 printf("unrestricted_mv=%d\n",get_bits1(&s->gb));
|
|
238 #if 1
|
|
239 printf("SAC: %d\n",get_bits1(&s->gb));
|
|
240 printf("advanced prediction mode: %d\n",get_bits1(&s->gb));
|
|
241 printf("PB frame: %d\n",get_bits1(&s->gb));
|
|
242 #else
|
|
243 if (get_bits1(&s->gb) != 0)
|
|
244 return -1; /* SAC: off */
|
|
245 if (get_bits1(&s->gb) != 0)
|
|
246 return -1; /* advanced prediction mode: off */
|
|
247 if (get_bits1(&s->gb) != 0)
|
|
248 return -1; /* not PB frame */
|
|
249 #endif
|
|
250 printf("qscale=%d\n",get_bits(&s->gb, 5));
|
|
251 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
|
|
252 } else {
|
|
253 printf("h263_plus = 1\n");
|
|
254 /* H.263v2 */
|
2695
|
255 if (get_bits(&s->gb, 3) != 1){
|
|
256 printf("H.263v2 A error\n");
|
2687
|
257 return -1;
|
2695
|
258 }
|
|
259 if (get_bits(&s->gb, 3) != 6){ /* custom source format */
|
|
260 printf("custom source format\n");
|
2687
|
261 return -1;
|
2695
|
262 }
|
2687
|
263 skip_bits(&s->gb, 12);
|
|
264 skip_bits(&s->gb, 3);
|
|
265 printf("pict_type=%d\n",get_bits(&s->gb, 3) + 1);
|
|
266 // if (s->pict_type != I_TYPE &&
|
|
267 // s->pict_type != P_TYPE)
|
|
268 // return -1;
|
|
269 skip_bits(&s->gb, 7);
|
|
270 skip_bits(&s->gb, 4); /* aspect ratio */
|
|
271 width = (get_bits(&s->gb, 9) + 1) * 4;
|
|
272 skip_bits1(&s->gb);
|
|
273 height = get_bits(&s->gb, 9) * 4;
|
|
274 printf("%d x %d\n",width,height);
|
2695
|
275 //if (height == 0)
|
|
276 // return -1;
|
2687
|
277 printf("qscale=%d\n",get_bits(&s->gb, 5));
|
|
278 }
|
|
279
|
|
280 /* PEI */
|
|
281 while (get_bits1(&s->gb) != 0) {
|
|
282 skip_bits(&s->gb, 8);
|
|
283 }
|
|
284 // s->f_code = 1;
|
|
285 // s->width = width;
|
|
286 // s->height = height;
|
|
287 return 0;
|
|
288 }
|
|
289
|
|
290
|
2695
|
291
|
2687
|
292 void demux_open_vivo(demuxer_t* demuxer){
|
|
293 vivo_priv_t* priv=demuxer->priv;
|
|
294
|
|
295 if(!ds_fill_buffer(demuxer->video)){
|
|
296 mp_msg(MSGT_DEMUX,MSGL_ERR,"VIVO: " MSGTR_MissingVideoStreamBug);
|
|
297 return;
|
|
298 }
|
|
299
|
|
300 h263_decode_picture_header(demuxer->video->buffer);
|
|
301
|
|
302
|
|
303 { sh_video_t* sh=new_sh_video(demuxer,0);
|
|
304
|
|
305 sh->format=0x6f766976; // "vivo"
|
|
306 if(!sh->fps) sh->fps=priv->fps;
|
|
307 sh->frametime=1.0f/sh->fps;
|
|
308 sh->disp_w=width; // FIXME
|
|
309 sh->disp_h=height;
|
|
310
|
|
311 // emulate BITMAPINFOHEADER:
|
|
312 sh->bih=malloc(sizeof(BITMAPINFOHEADER));
|
|
313 memset(sh->bih,0,sizeof(BITMAPINFOHEADER));
|
|
314 sh->bih->biSize=40;
|
|
315 sh->bih->biWidth=sh->disp_w;
|
|
316 sh->bih->biHeight=sh->disp_h;
|
|
317 sh->bih->biPlanes=1;
|
|
318 sh->bih->biBitCount=24;
|
|
319 sh->bih->biCompression=sh->format;
|
|
320 sh->bih->biSizeImage=sh->bih->biWidth*sh->bih->biHeight*3;
|
|
321 demuxer->video->sh=sh; sh->ds=demuxer->video;
|
|
322 demuxer->video->id=0;
|
|
323 }
|
|
324
|
2695
|
325 if(demuxer->audio->id>=-1){
|
2687
|
326 if(!ds_fill_buffer(demuxer->audio)){
|
|
327 mp_msg(MSGT_DEMUX,MSGL_ERR,"VIVO: " MSGTR_MissingAudioStream);
|
|
328 } else
|
|
329 { sh_audio_t* sh=new_sh_audio(demuxer,1);
|
|
330 sh->format=0x111; // 0x112
|
|
331 // Emulate WAVEFORMATEX struct:
|
|
332 sh->wf=malloc(sizeof(WAVEFORMATEX));
|
|
333 memset(sh->wf,0,sizeof(WAVEFORMATEX));
|
|
334 sh->wf->nChannels=1;
|
|
335 sh->wf->wBitsPerSample=16;
|
|
336 sh->wf->nSamplesPerSec=22050;
|
|
337 sh->wf->nAvgBytesPerSec=sh->wf->nChannels*sh->wf->wBitsPerSample*sh->wf->nSamplesPerSec/8;
|
|
338 demuxer->audio->sh=sh; sh->ds=demuxer->audio;
|
|
339 demuxer->audio->id=1;
|
|
340 }
|
2695
|
341 }
|
2687
|
342
|
|
343 }
|
|
344
|