comparison libmpdemux/demux_nsv.c @ 12175:da8e39bfe121

nsv (Nullsoft streaming video) demuxer Patch by Reza Jelveh <reza (dot) jelveh (at) tu-harburg (dot) de>
author rtognimp
date Mon, 12 Apr 2004 14:19:12 +0000
parents
children aeb377ccb110
comparison
equal deleted inserted replaced
12174:84a3e29c9ec5 12175:da8e39bfe121
1
2 /*
3 * Nullsoft Streaming Video demuxer
4 * for MPlayer
5 * by Reza Jelveh <reza.jelveh@tuhh.de>
6 * seeking and PCM audio not yet supported
7 * PCM needs extra audio chunk "miniheader" parsing
8 * Based on a'rpis g2 work
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14
15 #include "config.h"
16 #include "mp_msg.h"
17 #include "help_mp.h"
18 #include "stream.h"
19 #include "demuxer.h"
20 #include "stheader.h"
21
22 typedef struct {
23 float v_pts;
24 int video_pack_no;
25 unsigned int a_format;
26 unsigned int v_format;
27 unsigned char fps;
28 } nsv_priv_t;
29
30 /**
31 * Seeking still to be implemented
32 */
33 void demux_seek_nsv ( demuxer_t *demuxer, float rel_seek_secs, int flags )
34 {
35 // seeking is not yet implemented
36 }
37
38
39 int demux_nsv_fill_buffer ( demuxer_t *demuxer )
40 {
41 unsigned char hdr[17];
42 // for the extra data
43 unsigned char aux[6];
44 int i_aux = 0;
45 // videolen = audio chunk length, audiolen = video chunk length
46 int videolen,audiolen;
47
48 sh_video_t *sh_video = NULL;
49 // sh_audio_t *sh_audio = NULL;
50
51 nsv_priv_t * priv = demuxer->priv;
52
53 sh_video = demuxer->video->sh ;
54
55 // if the audio/video chunk has no new header the first 2 bytes will be discarded 0xBEEF
56 // or rather 0xEF 0xBE
57 stream_read(demuxer->stream,hdr,7);
58 if(stream_eof(demuxer->stream)) return 0;
59 // sometimes instead of 0xBEEF as described for the next audio/video chunk we get
60 // a whole new header
61
62 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"demux_nsv: %08X %08X\n",hdr[0]<<8|hdr[1],stream_tell(demuxer->stream));
63 switch(hdr[0]<<8|hdr[1]) {
64 case 0x4E53:
65 if(hdr[2]==0x56 && hdr[3]==0x73){
66 // NSVs
67 // get the header since there is no more metaheader after the first one
68 // there is no more need to skip that
69 stream_read(demuxer->stream,hdr+7,17-7);
70 stream_read(demuxer->stream,hdr,7);
71 }
72 break;
73
74 case 0xEFBE:
75 break;
76
77 default:
78 break;
79 }
80
81 sh_video->pts = priv->v_pts =demuxer->video->pts= priv->video_pack_no *
82 (float)sh_video->frametime;
83
84 demuxer->filepos=stream_tell(demuxer->stream);
85
86
87 mp_dbg(MSGT_DEMUX,MSGL_DBG2,"demux_nsv: %08X: %02X %02X | %02X %02X %02X | %02X %02X \n",
88 (int)demuxer->filepos, hdr[0],hdr[1],hdr[2],hdr[3],hdr[4],hdr[5],hdr[6]);
89
90 // read video:
91 videolen=(hdr[2]>>4)|(hdr[3]<<4)|(hdr[4]<<0xC);
92 //check if we got extra data like subtitles here
93 if( (hdr[2]&0x0f) != 0x0 ) {
94 stream_read( demuxer->stream, aux, 6);
95
96 i_aux = aux[0]|aux[1]<<8;
97 // We skip this extra data
98 stream_skip( demuxer->stream, i_aux );
99 i_aux+=6;
100 videolen -= i_aux;
101 }
102
103
104 // we need to return an empty packet when the
105 // video frame is empty otherwise the stream will fasten up
106 if(demuxer->video){
107 if( (hdr[2]&0x0f) != 0x0 )
108 ds_read_packet(demuxer->video,demuxer->stream,videolen,priv->v_pts,demuxer->filepos-i_aux,0);
109 else
110 ds_read_packet(demuxer->video,demuxer->stream,videolen,priv->v_pts,demuxer->filepos,0);
111 }
112 else
113 stream_skip(demuxer->stream,videolen);
114
115 // read audio:
116 audiolen=(hdr[5])|(hdr[6]<<8);
117 // we need to return an empty packet when the
118 // audio frame is empty otherwise the stream will fasten up
119 if(demuxer->audio){
120 ds_read_packet(demuxer->audio,demuxer->stream,audiolen,priv->v_pts,demuxer->filepos+videolen,0);
121 }
122 else
123 stream_skip(demuxer->stream,audiolen);
124
125 ++priv->video_pack_no;
126
127 return 1;
128
129 }
130
131
132 demuxer_t* demux_open_nsv ( demuxer_t* demuxer )
133 {
134 // last 2 bytes 17 and 18 are unknown but right after that comes the length
135 unsigned char hdr[17];
136 int videolen,audiolen;
137 sh_video_t *sh_video = NULL;
138 sh_audio_t *sh_audio = NULL;
139
140
141 // vp6x magic keyframe ee if no keyframe f0 if keyframe correct if theres a mistake
142 // unsigned char magic;
143
144 nsv_priv_t * priv = malloc(sizeof(nsv_priv_t));
145 demuxer->priv=priv;
146 priv->video_pack_no=0;
147 /* Create a new video stream header */
148 sh_video = new_sh_video ( demuxer, 0 );
149
150 /* Make sure the demuxer knows about the new video stream header
151 * (even though new_sh_video() ought to take care of it)
152 */
153 demuxer->video->sh = sh_video;
154
155 /* Make sure that the video demuxer stream header knows about its
156 * parent video demuxer stream (this is getting wacky), or else
157 * video_read_properties() will choke
158 */
159 sh_video->ds = demuxer->video;
160
161 /* disable seeking yet to be fixed*/
162 demuxer->seekable = 0;
163
164 stream_read(demuxer->stream,hdr,4);
165 if(stream_eof(demuxer->stream)) return 0;
166
167 /*** if we detected the file to be nsv and there was neither eof nor a header
168 **** that means that its most likely a shoutcast stream so we will need to seek
169 **** to the first occurance of the NSVs header ****/
170 if(!(hdr[0]==0x4E && hdr[1]==0x53 && hdr[2]==0x56)){
171 // todo: replace this with a decent string search algo
172 while(1){
173 stream_read(demuxer->stream,hdr,1);
174 if(stream_eof(demuxer->stream))
175 return 0;
176 if(hdr[0]!=0x4E)
177 continue;
178
179 stream_read(demuxer->stream,hdr+1,1);
180
181 if(stream_eof(demuxer->stream))
182 return 0;
183 if(hdr[1]!=0x53)
184 continue;
185
186 stream_read(demuxer->stream,hdr+2,1);
187
188 if(stream_eof(demuxer->stream))
189 return 0;
190 if(hdr[2]!=0x56)
191 continue;
192
193 stream_read(demuxer->stream,hdr+3,1);
194
195 if(stream_eof(demuxer->stream))
196 return 0;
197 if(hdr[3]!=0x73)
198 continue;
199
200 break;
201 }
202 }
203 if(hdr[0]==0x4E && hdr[1]==0x53 && hdr[2]==0x56){
204 // NSV header!
205 if(hdr[3]==0x73){
206 // NSVs
207 stream_read(demuxer->stream,hdr+4,17-4);
208 }
209
210 if(hdr[3]==0x66){
211 // NSVf
212 int len=stream_read_dword_le(demuxer->stream);
213 // TODO: parse out metadata!!!!
214 stream_skip(demuxer->stream,len-8);
215
216 // NSVs
217 stream_read(demuxer->stream,hdr,17);
218 }
219
220 // dummy debug message
221 mp_msg(MSGT_DEMUX,MSGL_V,"demux_nsv: Header: %.12s\n",hdr);
222
223 // bytes 8-11 audio codec fourcc
224 // PCM fourcc needs extra parsing for every audio chunk, yet to implement
225 if( strncmp(hdr+8,"NONE", 4)){//&&strncmp(hdr+8,"VLB ", 4)){
226 sh_audio = new_sh_audio ( demuxer, 0 );
227 demuxer->audio->sh = sh_audio;
228 sh_audio->format=mmioFOURCC(hdr[8],hdr[9],hdr[10],hdr[11]);
229 sh_audio->ds = demuxer->audio;
230 }
231 priv->a_format=mmioFOURCC(hdr[8],hdr[9],hdr[10],hdr[11]);
232
233 // !!!!!!!!!!!!!!!!!!!!
234 // RemoveMe!!! This is just to avoid lot of bugreports!
235 // !!!!!!!!!!!!!!!!!!!!
236 if(priv->a_format==mmioFOURCC('V','L','B',' '))
237 mp_msg(MSGT_DEMUX,MSGL_WARN,"demux_nsv: VLB audio does not work yet. Expect problems.\n");
238
239
240 // store hdr fps
241 priv->fps=hdr[16];
242
243 if (strncmp(hdr+4,"NONE", 4)) {
244 // bytes 4-7 video codec fourcc
245 priv->v_format = sh_video->format=mmioFOURCC(hdr[4],hdr[5],hdr[6],hdr[7]);
246
247 // new video stream! parse header
248 sh_video->disp_w=hdr[12]|(hdr[13]<<8);
249 sh_video->disp_h=hdr[14]|(hdr[15]<<8);
250 sh_video->bih=(BITMAPINFOHEADER*)calloc(1,sizeof(BITMAPINFOHEADER));
251 sh_video->bih->biSize=sizeof(BITMAPINFOHEADER);
252 sh_video->bih->biPlanes=1;
253 sh_video->bih->biBitCount=24;
254 sh_video->bih->biWidth=hdr[12]|(hdr[13]<<8);
255 sh_video->bih->biHeight=hdr[14]|(hdr[15]<<8);
256 memcpy(&sh_video->bih->biCompression,hdr+4,4);
257 sh_video->bih->biSizeImage=sh_video->bih->biWidth*sh_video->bih->biHeight*3;
258
259 // !!!!!!!!!!!!!!!!!!!!
260 // RemoveMe!!! This is just to avoid lot of bugreports!
261 // !!!!!!!!!!!!!!!!!!!!
262 if(priv->v_format==mmioFOURCC('V','P','5','0'))
263 mp_msg(MSGT_DEMUX,MSGL_WARN,"demux_nsv: VP50 video does not work yet. Expect problems.\n");
264
265 // here we search for the correct keyframe
266 // vp6 keyframe is when the 2nd byte of the vp6 header is 0x36
267 if(priv->v_format==mmioFOURCC('V','P','6','1')){
268 stream_read(demuxer->stream,hdr,9);
269 videolen=(hdr[2]>>4)|(hdr[3]<<4)|(hdr[4]<<0xC);
270 audiolen=(hdr[5])|(hdr[6]<<8);
271 mp_msg(MSGT_DEMUX,MSGL_V,"demux_nsv: Header: %08X\n",*hdr);
272 stream_skip(demuxer->stream, videolen+audiolen-2);
273 stream_read(demuxer->stream,hdr,9);
274
275 if (hdr[8]!=0x36) {
276 while(hdr[8]!=0x36){
277 videolen=(hdr[2]>>4)|(hdr[3]<<4)|(hdr[4]<<0xC);
278 audiolen=(hdr[5])|(hdr[6]<<8);
279 stream_skip(demuxer->stream, videolen+audiolen-2);
280 stream_read(demuxer->stream,hdr,9);
281 if(hdr[0]==0x4E){
282 if(stream_eof(demuxer->stream)) return 0;
283 stream_skip(demuxer->stream,8);
284 stream_read(demuxer->stream,hdr,9);
285 }
286 }
287 }
288
289
290 stream_seek(demuxer->stream,stream_tell(demuxer->stream)-9);
291 }
292 }
293
294
295 switch(priv->fps){
296 case 0x80:
297 sh_video->fps=30;
298 break;
299 case 0x81:
300 sh_video->fps=(float)30000.0/1001.0;
301 break;
302 case 0x82:
303 sh_video->fps=25;
304 break;
305 case 0x83:
306 sh_video->fps=(float)24000.0/1001.0;
307 break;
308 case 0x85:
309 sh_video->fps=(float)15000.0/1001.0;
310 break;
311 default:
312 sh_video->fps = (float)priv->fps;
313 }
314 sh_video->frametime = (float)1.0 / (float)sh_video->fps;
315 }
316
317
318
319 return demuxer;
320 }
321
322 int nsv_check_file ( demuxer_t* demuxer )
323 {
324 unsigned int id;
325
326 /* Store original position */
327 // off_t orig_pos = stream_tell(demuxer->stream);
328
329 mp_msg ( MSGT_DEMUX, MSGL_V, "Checking for Nullsoft Streaming Video\n" );
330
331 //---- check NSVx header:
332 id=stream_read_dword_le(demuxer->stream);
333 if(id!=mmioFOURCC('N','S','V','f') && id!=mmioFOURCC('N','S','V','s'))
334 return 0; // not an NSV file
335
336 stream_reset(demuxer->stream); // clear EOF
337 stream_seek(demuxer->stream,demuxer->stream->start_pos);
338
339
340 return 1;
341 }
342
343 void demux_close_nsv(demuxer_t* demuxer) {
344 nsv_priv_t* priv = demuxer->priv;
345
346 if(!priv)
347 return;
348 free(priv);
349
350 }