FFmpeg
Loading...
Searching...
No Matches
nsvdec.c
Go to the documentation of this file.
1/*
2 * NSV demuxer
3 * Copyright (c) 2004 The FFmpeg Project
4 *
5 * first version by Francois Revol <revol@free.fr>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
25#include "libavutil/bprint.h"
27#include "libavutil/mem.h"
28#include "avformat.h"
29#include "demux.h"
30#include "internal.h"
31#include "libavutil/dict.h"
33
34/* max bytes to crawl for trying to resync
35 * stupid streaming servers don't start at chunk boundaries...
36 */
37#define NSV_MAX_RESYNC (500*1024)
38#define NSV_MAX_RESYNC_TRIES 300
39
40/*
41 * References:
42 * (1) http://www.multimedia.cx/nsv-format.txt
43 * seems someone came to the same conclusions as me, and updated it:
44 * (2) http://www.stud.ktu.lt/~vitslav/nsv/nsv-format.txt
45 * http://www.stud.ktu.lt/~vitslav/nsv/
46 * official docs
47 * (3) http://ultravox.aol.com/NSVFormat.rtf
48 * Sample files:
49 * (S1) http://www.nullsoft.com/nsv/samples/
50 * http://www.nullsoft.com/nsv/samples/faster.nsv
51 * http://streamripper.sourceforge.net/openbb/read.php?TID=492&page=4
52 */
53
54/*
55 * notes on the header (Francois Revol):
56 *
57 * It is followed by strings, then a table, but nothing tells
58 * where the table begins according to (1). After checking faster.nsv,
59 * I believe NVSf[16-19] gives the size of the strings data
60 * (that is the offset of the data table after the header).
61 * After checking all samples from (S1) all confirms this.
62 *
63 * Then, about NSVf[12-15], faster.nsf has 179700. When viewing it in VLC,
64 * I noticed there was about 1 NVSs chunk/s, so I ran
65 * strings faster.nsv | grep NSVs | wc -l
66 * which gave me 180. That leads me to think that NSVf[12-15] might be the
67 * file length in milliseconds.
68 * Let's try that:
69 * for f in *.nsv; do HTIME="$(od -t x4 "$f" | head -1 | sed 's/.* //')"; echo "'$f' $((0x$HTIME))s = $((0x$HTIME/1000/60)):$((0x$HTIME/1000%60))"; done
70 * except for nsvtrailer (which doesn't have an NSVf header), it reports correct time.
71 *
72 * nsvtrailer.nsv (S1) does not have any NSVf header, only NSVs chunks,
73 * so the header seems to not be mandatory. (for streaming).
74 *
75 * index slice duration check (excepts nsvtrailer.nsv):
76 * for f in [^n]*.nsv; do DUR="$(ffmpeg -i "$f" 2>/dev/null | grep 'NSVf duration' | cut -d ' ' -f 4)"; IC="$(ffmpeg -i "$f" 2>/dev/null | grep 'INDEX ENTRIES' | cut -d ' ' -f 2)"; echo "duration $DUR, slice time $(($DUR/$IC))"; done
77 */
78
79/*
80 * TODO:
81 * - handle timestamps !!!
82 * - use index
83 * - mime-type in probe()
84 * - seek
85 */
86
87#if 0
88struct NSVf_header {
89 uint32_t chunk_tag; /* 'NSVf' */
90 uint32_t chunk_size;
91 uint32_t file_size; /* max 4GB ??? no one learns anything it seems :^) */
92 uint32_t file_length; //unknown1; /* what about MSB of file_size ? */
93 uint32_t info_strings_size; /* size of the info strings */ //unknown2;
94 uint32_t table_entries;
95 uint32_t table_entries_used; /* the left ones should be -1 */
96};
97
98struct NSVs_header {
99 uint32_t chunk_tag; /* 'NSVs' */
100 uint32_t v4cc; /* or 'NONE' */
101 uint32_t a4cc; /* or 'NONE' */
102 uint16_t vwidth; /* av_assert0(vwidth%16==0) */
103 uint16_t vheight; /* av_assert0(vheight%16==0) */
104 uint8_t framerate; /* value = (framerate&0x80)?frtable[frameratex0x7f]:framerate */
105 uint16_t unknown;
106};
107
108struct nsv_avchunk_header {
109 uint8_t vchunk_size_lsb;
110 uint16_t vchunk_size_msb; /* value = (vchunk_size_msb << 4) | (vchunk_size_lsb >> 4) */
111 uint16_t achunk_size;
112};
113
114struct nsv_pcm_header {
115 uint8_t bits_per_sample;
116 uint8_t channel_count;
117 uint16_t sample_rate;
118};
119#endif
120
121/* variation from avi.h */
122/*typedef struct CodecTag {
123 int id;
124 unsigned int tag;
125} CodecTag;*/
126
127/* tags */
128
129#define T_NSVF MKTAG('N', 'S', 'V', 'f') /* file header */
130#define T_NSVS MKTAG('N', 'S', 'V', 's') /* chunk header */
131#define T_TOC2 MKTAG('T', 'O', 'C', '2') /* extra index marker */
132#define T_NONE MKTAG('N', 'O', 'N', 'E') /* null a/v 4CC */
133#define T_SUBT MKTAG('S', 'U', 'B', 'T') /* subtitle aux data */
134#define T_ASYN MKTAG('A', 'S', 'Y', 'N') /* async a/v aux marker */
135#define T_KEYF MKTAG('K', 'E', 'Y', 'F') /* video keyframe aux marker (addition) */
136
137#define TB_NSVF MKBETAG('N', 'S', 'V', 'f')
138#define TB_NSVS MKBETAG('N', 'S', 'V', 's')
139
140/* hardcoded stream indexes */
141#define NSV_ST_VIDEO 0
142#define NSV_ST_AUDIO 1
143#define NSV_ST_SUBT 2
144
155
156typedef struct NSVStream {
157 int frame_offset; /* current frame (video) or byte (audio) counter
158 (used to compute the pts) */
159 int scale;
160 int rate;
161 int sample_size; /* audio only data */
162 int start;
163
164 int new_frame_offset; /* temporary storage (used during seek) */
165 int cum_len; /* temporary storage (used during seek) */
166} NSVStream;
167
168typedef struct NSVContext {
174 AVPacket ahead[2]; /* [v, a] if .data is !NULL there is something */
175 /* cached */
177 uint32_t vtag, atag;
178 uint16_t vwidth, vheight;
179 int16_t avsync;
182 int nsvf;
183} NSVContext;
184
186 { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', ' ') },
187 { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '0') },
188 { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') },
189 { AV_CODEC_ID_VP5, MKTAG('V', 'P', '5', ' ') },
190 { AV_CODEC_ID_VP5, MKTAG('V', 'P', '5', '0') },
191 { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', ' ') },
192 { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '0') },
193 { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '1') },
194 { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '2') },
195 { AV_CODEC_ID_VP8, MKTAG('V', 'P', '8', '0') },
196/*
197 { AV_CODEC_ID_VP4, MKTAG('V', 'P', '4', ' ') },
198 { AV_CODEC_ID_VP4, MKTAG('V', 'P', '4', '0') },
199*/
200 { AV_CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') }, /* cf sample xvid decoder from nsv_codec_sdk.zip */
201 { AV_CODEC_ID_H264, MKTAG('H', '2', '6', '4') },
202 { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', '3') },
203 { AV_CODEC_ID_NONE, 0 },
204};
205
207 { AV_CODEC_ID_MP3, MKTAG('M', 'P', '3', ' ') },
208 { AV_CODEC_ID_AAC, MKTAG('A', 'A', 'C', ' ') },
209 { AV_CODEC_ID_AAC, MKTAG('A', 'A', 'C', 'P') },
210 { AV_CODEC_ID_AAC, MKTAG('A', 'A', 'V', ' ') },
211 { AV_CODEC_ID_AAC, MKTAG('V', 'L', 'B', ' ') },
212 { AV_CODEC_ID_SPEEX, MKTAG('S', 'P', 'X', ' ') },
213 { AV_CODEC_ID_PCM_U16LE, MKTAG('P', 'C', 'M', ' ') },
214 { AV_CODEC_ID_NONE, 0 },
215};
216
217//static int nsv_load_index(AVFormatContext *s);
218static int nsv_read_chunk(AVFormatContext *s, int fill_header);
219
220/* try to find something we recognize, and set the state accordingly */
222{
223 NSVContext *nsv = s->priv_data;
224 AVIOContext *pb = s->pb;
225 uint32_t v = 0;
226 int i;
227
228 for (i = 0; i < NSV_MAX_RESYNC; i++) {
229 if (avio_feof(pb)) {
230 av_log(s, AV_LOG_TRACE, "NSV EOF\n");
231 nsv->state = NSV_UNSYNC;
232 return -1;
233 }
234 v <<= 8;
235 v |= avio_r8(pb);
236 if (i < 8) {
237 av_log(s, AV_LOG_TRACE, "NSV resync: [%d] = %02"PRIx32"\n", i, v & 0x0FF);
238 }
239
240 if ((v & 0x0000ffff) == 0xefbe) { /* BEEF */
241 av_log(s, AV_LOG_TRACE, "NSV resynced on BEEF after %d bytes\n", i+1);
242 nsv->state = NSV_FOUND_BEEF;
243 return 0;
244 }
245 /* we read as big-endian, thus the MK*BE* */
246 if (v == TB_NSVF) { /* NSVf */
247 av_log(s, AV_LOG_TRACE, "NSV resynced on NSVf after %d bytes\n", i+1);
248 nsv->state = NSV_FOUND_NSVF;
249 return 0;
250 }
251 if (v == MKBETAG('N', 'S', 'V', 's')) { /* NSVs */
252 av_log(s, AV_LOG_TRACE, "NSV resynced on NSVs after %d bytes\n", i+1);
253 nsv->state = NSV_FOUND_NSVS;
254 return 0;
255 }
256
257 }
258 av_log(s, AV_LOG_TRACE, "NSV sync lost\n");
259 return -1;
260}
261
263{
264 NSVContext *nsv = s->priv_data;
265 AVIOContext *pb = s->pb;
266 av_unused unsigned int file_size;
267 unsigned int size;
269 int strings_size;
270 int table_entries;
271 int table_entries_used;
272
273 nsv->state = NSV_UNSYNC; /* in case we fail */
274
275 if (nsv->nsvf) {
276 av_log(s, AV_LOG_TRACE, "Multiple NSVf\n");
277 return 0;
278 }
279 nsv->nsvf = 1;
280
281 size = avio_rl32(pb);
282 if (size < 28)
283 return -1;
284 nsv->NSVf_end = size;
285
286 file_size = (uint32_t)avio_rl32(pb);
287 av_log(s, AV_LOG_TRACE, "NSV NSVf chunk_size %u\n", size);
288 av_log(s, AV_LOG_TRACE, "NSV NSVf file_size %u\n", file_size);
289
290 nsv->duration = duration = avio_rl32(pb); /* in ms */
291 av_log(s, AV_LOG_TRACE, "NSV NSVf duration %"PRId64" ms\n", duration);
292 // XXX: store it in AVStreams
293
294 strings_size = avio_rl32(pb);
295 table_entries = avio_rl32(pb);
296 table_entries_used = avio_rl32(pb);
297 av_log(s, AV_LOG_TRACE, "NSV NSVf info-strings size: %d, table entries: %d, bis %d\n",
298 strings_size, table_entries, table_entries_used);
299 if (avio_feof(pb))
300 return -1;
301
302 av_log(s, AV_LOG_TRACE, "NSV got header; filepos %"PRId64"\n", avio_tell(pb));
303
304 if (strings_size > 0) {
305 AVBPrint strings;
306 char *p, *endp;
307 char *token, *value;
308 char quote;
309 int ret;
310
312 ret = avio_read_to_bprint(pb, &strings, strings_size);
313 if (ret < 0) {
314 av_bprint_finalize(&strings, NULL);
315 return ret;
316 }
317 p = strings.str;
318 endp = strings.str + strings.len;
319 while (p < endp) {
320 while (*p == ' ')
321 p++; /* strip out spaces */
322 if (p >= endp-2)
323 break;
324 token = p;
325 p = strchr(p, '=');
326 if (!p || p >= endp-2)
327 break;
328 *p++ = '\0';
329 quote = *p++;
330 value = p;
331 p = strchr(p, quote);
332 if (!p || p >= endp)
333 break;
334 *p++ = '\0';
335 av_log(s, AV_LOG_TRACE, "NSV NSVf INFO: %s='%s'\n", token, value);
336 av_dict_set(&s->metadata, token, value, 0);
337 }
338 av_bprint_finalize(&strings, NULL);
339 }
340 if (avio_feof(pb))
341 return -1;
342
343 av_log(s, AV_LOG_TRACE, "NSV got infos; filepos %"PRId64"\n", avio_tell(pb));
344
345 if (table_entries_used > 0) {
346 int i;
347 nsv->index_entries = table_entries_used;
348 if((unsigned)table_entries_used >= UINT_MAX / sizeof(uint32_t))
349 return -1;
350 nsv->nsvs_file_offset = av_malloc_array((unsigned)table_entries_used, sizeof(uint32_t));
351 if (!nsv->nsvs_file_offset)
352 return AVERROR(ENOMEM);
353
354 for(i=0;i<table_entries_used;i++) {
355 if (avio_feof(pb))
356 return AVERROR_INVALIDDATA;
357 nsv->nsvs_file_offset[i] = avio_rl32(pb) + size;
358 }
359
360 if(table_entries > table_entries_used &&
361 avio_rl32(pb) == MKTAG('T','O','C','2')) {
362 nsv->nsvs_timestamps = av_malloc_array((unsigned)table_entries_used, sizeof(uint32_t));
363 if (!nsv->nsvs_timestamps)
364 return AVERROR(ENOMEM);
365 for(i=0;i<table_entries_used;i++) {
366 nsv->nsvs_timestamps[i] = avio_rl32(pb);
367 }
368 }
369 }
370
371 av_log(s, AV_LOG_TRACE, "NSV got index; filepos %"PRId64"\n", avio_tell(pb));
372
373 avio_seek(pb, nsv->base_offset + size, SEEK_SET); /* required for dumbdriving-271.nsv (2 extra bytes) */
374
375 if (avio_feof(pb))
376 return -1;
378 return 0;
379}
380
382{
383 NSVContext *nsv = s->priv_data;
384 AVIOContext *pb = s->pb;
385 uint32_t vtag, atag;
386 uint16_t vwidth, vheight;
388 int i;
389 AVStream *st;
390 NSVStream *nst;
391
392 vtag = avio_rl32(pb);
393 atag = avio_rl32(pb);
394 vwidth = avio_rl16(pb);
395 vheight = avio_rl16(pb);
396 i = avio_r8(pb);
397
398 av_log(s, AV_LOG_TRACE, "NSV NSVs framerate code %2x\n", i);
399 if(i&0x80) { /* odd way of giving native framerates from docs */
400 int t=(i & 0x7F)>>2;
401 if(t<16) framerate = (AVRational){1, t+1};
402 else framerate = (AVRational){t-15, 1};
403
404 if(i&1){
405 framerate.num *= 1000;
406 framerate.den *= 1001;
407 }
408
409 if((i&3)==3) framerate.num *= 24;
410 else if((i&3)==2) framerate.num *= 25;
411 else framerate.num *= 30;
412 }
413 else
414 framerate= (AVRational){i, 1};
415
416 nsv->avsync = avio_rl16(pb);
417 nsv->framerate = framerate;
418
419 av_log(s, AV_LOG_TRACE, "NSV NSVs vsize %dx%d\n", vwidth, vheight);
420
421 /* XXX change to ap != NULL ? */
422 if (s->nb_streams == 0) { /* streams not yet published, let's do that */
423 nsv->vtag = vtag;
424 nsv->atag = atag;
425 nsv->vwidth = vwidth;
426 nsv->vheight = vwidth;
427 if (vtag != T_NONE) {
428 int i;
430 if (!st)
431 goto fail;
432
433 st->id = NSV_ST_VIDEO;
434 nst = av_mallocz(sizeof(NSVStream));
435 if (!nst)
436 goto fail;
437 st->priv_data = nst;
439 st->codecpar->codec_tag = vtag;
441 st->codecpar->width = vwidth;
442 st->codecpar->height = vheight;
443 st->codecpar->bits_per_coded_sample = 24; /* depth XXX */
444
445 avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
446 st->start_time = 0;
447 st->duration = av_rescale(nsv->duration, framerate.num, 1000*framerate.den);
448
449 for(i=0;i<nsv->index_entries;i++) {
450 if(nsv->nsvs_timestamps) {
452 0, 0, AVINDEX_KEYFRAME);
453 } else {
454 int64_t ts = av_rescale(i*nsv->duration/nsv->index_entries, framerate.num, 1000*framerate.den);
456 }
457 }
458 }
459 if (atag != T_NONE) {
461 if (!st)
462 goto fail;
463
464 st->id = NSV_ST_AUDIO;
465 nst = av_mallocz(sizeof(NSVStream));
466 if (!nst)
467 goto fail;
468 st->priv_data = nst;
470 st->codecpar->codec_tag = atag;
472
473 if (atag == MKTAG('A', 'A', 'V', ' ')) {
474 static const uint8_t aav_pce[] = {
475 0x12, 0x00, 0x05, 0x08, 0x48, 0x00,
476 0x20, 0x00, 0xC6, 0x40, 0x04, 0x4C,
477 0x61, 0x76, 0x63, 0x56, 0xE5, 0x00,
478 0x00, 0x00,
479 };
480 int ret;
481
482 if ((ret = ff_alloc_extradata(st->codecpar, sizeof(aav_pce))) < 0)
483 return ret;
484
485 st->codecpar->sample_rate = 44100;
486 memcpy(st->codecpar->extradata, aav_pce, sizeof(aav_pce));
487 }
488
489 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL; /* for PCM we will read a chunk later and put correct info */
490
491 /* set timebase to common denominator of ms and framerate */
492 avpriv_set_pts_info(st, 64, 1, framerate.num*1000);
493 st->start_time = 0;
494 st->duration = (int64_t)nsv->duration * framerate.num;
495 }
496 } else {
497 if (nsv->vtag != vtag || nsv->atag != atag || nsv->vwidth != vwidth || nsv->vheight != vwidth) {
498 av_log(s, AV_LOG_TRACE, "NSV NSVs header values differ from the first one!!!\n");
499 //return -1;
500 }
501 }
502
504 return 0;
505fail:
506 /* XXX */
507 nsv->state = NSV_UNSYNC;
508 return -1;
509}
510
512{
513 NSVContext *nsv = s->priv_data;
514 int i, err;
515
516 nsv->state = NSV_UNSYNC;
517 nsv->ahead[0].data = nsv->ahead[1].data = NULL;
518
519 for (i = 0; i < NSV_MAX_RESYNC_TRIES; i++) {
520 err = nsv_resync(s);
521 if (err < 0)
522 return err;
523 if (nsv->state == NSV_FOUND_NSVF) {
525 if (err < 0)
526 return err;
527 }
528 /* we need the first NSVs also... */
529 if (nsv->state == NSV_FOUND_NSVS) {
531 if (err < 0)
532 return err;
533 break; /* we just want the first one */
534 }
535 }
536 if (s->nb_streams < 1) /* no luck so far */
537 return AVERROR_INVALIDDATA;
538
539 /* now read the first chunk, so we can attempt to decode more info */
540 err = nsv_read_chunk(s, 1);
541
542 av_log(s, AV_LOG_TRACE, "parsed header\n");
543 return err;
544}
545
546static int nsv_read_chunk(AVFormatContext *s, int fill_header)
547{
548 NSVContext *nsv = s->priv_data;
549 AVIOContext *pb = s->pb;
550 AVStream *st[2] = {NULL, NULL};
551 NSVStream *nst;
552 AVPacket *pkt;
553 int i, err = 0;
554 uint8_t auxcount; /* number of aux metadata, also 4 bits of vsize */
555 uint32_t vsize;
556 uint16_t asize;
557 uint16_t auxsize;
558 int ret;
559
560 if (nsv->ahead[0].data || nsv->ahead[1].data)
561 return 0; //-1; /* hey! eat what you've in your plate first! */
562
563null_chunk_retry:
564 if (avio_feof(pb))
565 return -1;
566
567 for (i = 0; i < NSV_MAX_RESYNC_TRIES && nsv->state < NSV_FOUND_NSVS && !err; i++)
568 err = nsv_resync(s);
569 if (err < 0)
570 return err;
571 if (nsv->state == NSV_FOUND_NSVS)
573 if (err < 0)
574 return err;
575 if (nsv->state != NSV_HAS_READ_NSVS && nsv->state != NSV_FOUND_BEEF)
576 return -1;
577
578 auxcount = avio_r8(pb);
579 vsize = avio_rl16(pb);
580 asize = avio_rl16(pb);
581 vsize = (vsize << 4) | (auxcount >> 4);
582 auxcount &= 0x0f;
583 av_log(s, AV_LOG_TRACE, "NSV CHUNK %d aux, %"PRIu32" bytes video, %d bytes audio\n", auxcount, vsize, asize);
584 /* skip aux stuff */
585 for (i = 0; i < auxcount; i++) {
586 av_unused uint32_t auxtag;
587 auxsize = avio_rl16(pb);
588 auxtag = avio_rl32(pb);
589 avio_skip(pb, auxsize);
590 if (vsize < auxsize + sizeof(uint16_t) + sizeof(uint32_t))
591 return AVERROR_INVALIDDATA;
592 vsize -= auxsize + sizeof(uint16_t) + sizeof(uint32_t); /* that's becoming brain-dead */
593 }
594
595 if (avio_feof(pb))
596 return -1;
597 if (!vsize && !asize) {
598 nsv->state = NSV_UNSYNC;
599 goto null_chunk_retry;
600 }
601
602 /* map back streams to v,a */
603 if (s->nb_streams > 0)
604 st[s->streams[0]->id] = s->streams[0];
605 if (s->nb_streams > 1)
606 st[s->streams[1]->id] = s->streams[1];
607
608 if (vsize && st[NSV_ST_VIDEO]) {
609 nst = st[NSV_ST_VIDEO]->priv_data;
610 pkt = &nsv->ahead[NSV_ST_VIDEO];
611 if ((ret = av_get_packet(pb, pkt, vsize)) < 0)
612 return ret;
613 pkt->stream_index = st[NSV_ST_VIDEO]->index;//NSV_ST_VIDEO;
614 pkt->dts = nst->frame_offset;
615 pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */
616 for (i = 0; i < FFMIN(8, vsize); i++)
617 av_log(s, AV_LOG_TRACE, "NSV video: [%d] = %02x\n", i, pkt->data[i]);
618 }
619 if(st[NSV_ST_VIDEO])
620 ((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset++;
621
622 if (asize && st[NSV_ST_AUDIO]) {
623 nst = st[NSV_ST_AUDIO]->priv_data;
624 pkt = &nsv->ahead[NSV_ST_AUDIO];
625 /* read raw audio specific header on the first audio chunk... */
626 /* on ALL audio chunks ?? seems so! */
627 if (asize >= 4 && st[NSV_ST_AUDIO]->codecpar->codec_tag == MKTAG('P', 'C', 'M', ' ')/* && fill_header*/) {
628 uint8_t bps;
629 uint8_t channels;
630 uint16_t samplerate;
631 bps = avio_r8(pb);
632 channels = avio_r8(pb);
633 samplerate = avio_rl16(pb);
634 if (!channels || !samplerate)
635 return AVERROR_INVALIDDATA;
636 asize-=4;
637 av_log(s, AV_LOG_TRACE, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);
638 if (fill_header) {
639 ffstream(st[NSV_ST_AUDIO])->need_parsing = AVSTREAM_PARSE_NONE; /* we know everything */
640 if (bps != 16) {
641 av_log(s, AV_LOG_TRACE, "NSV AUDIO bit/sample != 16 (%d)!!!\n", bps);
642 }
643 bps /= channels; // ???
644 if (bps == 8)
646 samplerate /= 4;/* UGH ??? XXX */
647 channels = 1;
649 st[NSV_ST_AUDIO]->codecpar->sample_rate = samplerate;
650 av_log(s, AV_LOG_TRACE, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);
651 }
652 }
653 if ((ret = av_get_packet(pb, pkt, asize)) < 0)
654 return ret;
655 pkt->stream_index = st[NSV_ST_AUDIO]->index;//NSV_ST_AUDIO;
656 pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */
657 if( nsv->state == NSV_HAS_READ_NSVS && st[NSV_ST_VIDEO] ) {
658 /* on a nsvs frame we have new information on a/v sync */
659 pkt->dts = (((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset-1);
660 pkt->dts *= (int64_t)1000 * nsv->framerate.den;
661 pkt->dts += (int64_t)nsv->avsync * nsv->framerate.num;
662 av_log(s, AV_LOG_TRACE, "NSV AUDIO: sync:%d, dts:%"PRId64, nsv->avsync, pkt->dts);
663 }
664 nst->frame_offset++;
665 }
666
667 nsv->state = NSV_UNSYNC;
668 return 0;
669}
670
671
673{
674 NSVContext *nsv = s->priv_data;
675 int i, err = 0;
676
677 /* in case we don't already have something to eat ... */
678 if (!nsv->ahead[0].data && !nsv->ahead[1].data)
679 err = nsv_read_chunk(s, 0);
680 if (err < 0)
681 return err;
682
683 /* now pick one of the plates */
684 for (i = 0; i < 2; i++) {
685 if (nsv->ahead[i].data) {
686 av_packet_move_ref(pkt, &nsv->ahead[i]);
687 return 0;
688 }
689 }
690
691 /* this restaurant is not provisioned :^] */
692 return -1;
693}
694
695static int nsv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
696{
697 NSVContext *nsv = s->priv_data;
698 AVStream *st = s->streams[stream_index];
699 FFStream *const sti = ffstream(st);
700 NSVStream *nst = st->priv_data;
701 int index;
702
703 index = av_index_search_timestamp(st, timestamp, flags);
704 if(index < 0)
705 return -1;
706
707 if (avio_seek(s->pb, sti->index_entries[index].pos, SEEK_SET) < 0)
708 return -1;
709
711 nsv->state = NSV_UNSYNC;
712 return 0;
713}
714
716{
717 NSVContext *nsv = s->priv_data;
718
721 if (nsv->ahead[0].data)
722 av_packet_unref(&nsv->ahead[0]);
723 if (nsv->ahead[1].data)
724 av_packet_unref(&nsv->ahead[1]);
725 return 0;
726}
727
728static int nsv_probe(const AVProbeData *p)
729{
730 int i, score = 0;
731
732 /* check file header */
733 /* streamed files might not have any header */
734 if (p->buf[0] == 'N' && p->buf[1] == 'S' &&
735 p->buf[2] == 'V' && (p->buf[3] == 'f' || p->buf[3] == 's'))
736 return AVPROBE_SCORE_MAX;
737 /* XXX: do streamed files always start at chunk boundary ?? */
738 /* or do we need to search NSVs in the byte stream ? */
739 /* seems the servers don't bother starting clean chunks... */
740 /* sometimes even the first header is at 9KB or something :^) */
741 for (i = 1; i < p->buf_size - 3; i++) {
742 if (AV_RL32(p->buf + i) == AV_RL32("NSVs")) {
743 /* Get the chunk size and check if at the end we are getting 0xBEEF */
744 int vsize = AV_RL24(p->buf+i+19) >> 4;
745 int asize = AV_RL16(p->buf+i+22);
746 int offset = i + 23 + asize + vsize + 1;
747 if (offset <= p->buf_size - 2 && AV_RL16(p->buf + offset) == 0xBEEF)
748 return 4*AVPROBE_SCORE_MAX/5;
749 score = AVPROBE_SCORE_MAX/5;
750 }
751 }
752 /* so we'll have more luck on extension... */
753 if (av_match_ext(p->filename, "nsv"))
755 /* FIXME: add mime-type check */
756 return score;
757}
758
760 .p.name = "nsv",
761 .p.long_name = NULL_IF_CONFIG_SMALL("Nullsoft Streaming Video"),
762 .priv_data_size = sizeof(NSVContext),
763 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
769};
const FFInputFormat ff_nsv_demuxer
Definition nsvdec.c:759
channels
Definition aptx.h:31
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:865
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:630
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:485
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition avformat.h:613
@ AVSTREAM_PARSE_NONE
Definition avformat.h:612
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:507
unsigned int avio_rl16(AVIOContext *s)
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size)
Read contents of h into print buffer, up to max_size bytes, or up to EOF.
Definition aviobuf.c:1222
int avio_r8(AVIOContext *s)
Definition aviobuf.c:611
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:68
AVBPrint public header.
#define AV_BPRINT_SIZE_UNLIMITED
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
Public dictionary API.
double value
Definition eval.c:102
static int64_t duration
Definition ffplay.c:359
int ret
Definition ffplay.c:391
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:580
#define fail
Definition test.h:479
@ AV_CODEC_ID_VP6
Definition codec_id.h:141
@ AV_CODEC_ID_VP3
Definition codec_id.h:79
@ AV_CODEC_ID_RAWVIDEO
Definition codec_id.h:63
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:338
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_VP8
Definition codec_id.h:190
@ AV_CODEC_ID_AAC
Definition codec_id.h:461
@ AV_CODEC_ID_VP5
Definition codec_id.h:140
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_SPEEX
Definition codec_id.h:494
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:460
@ AV_CODEC_ID_PCM_U16LE
Definition codec_id.h:335
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:435
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:492
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition format.c:41
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition seek.c:245
#define AV_CHANNEL_LAYOUT_MONO
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:234
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:87
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int index
Definition gxfenc.c:90
#define AV_RL24(x)
#define AV_RL32(p)
#define AV_RL16(p)
unsigned offset
Definition libaomenc.c:763
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:384
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition utils.c:143
Macro definitions for various function/variable attributes.
#define av_unused
Definition attributes.h:164
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define MKBETAG(a, b, c, d)
Definition macros.h:56
Memory handling functions.
unsigned bps
Definition movenc.c:2129
#define TB_NSVF
Definition nsvdec.c:137
#define NSV_ST_AUDIO
Definition nsvdec.c:142
static int nsv_read_close(AVFormatContext *s)
Definition nsvdec.c:715
static int nsv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition nsvdec.c:672
#define T_NONE
Definition nsvdec.c:132
#define NSV_MAX_RESYNC_TRIES
Definition nsvdec.c:38
#define NSV_ST_VIDEO
Definition nsvdec.c:141
static const AVCodecTag nsv_codec_video_tags[]
Definition nsvdec.c:185
static int nsv_resync(AVFormatContext *s)
Definition nsvdec.c:221
static int nsv_read_header(AVFormatContext *s)
Definition nsvdec.c:511
static int nsv_parse_NSVs_header(AVFormatContext *s)
Definition nsvdec.c:381
static int nsv_probe(const AVProbeData *p)
Definition nsvdec.c:728
static int nsv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition nsvdec.c:695
static int nsv_read_chunk(AVFormatContext *s, int fill_header)
Definition nsvdec.c:546
static const AVCodecTag nsv_codec_audio_tags[]
Definition nsvdec.c:206
#define NSV_MAX_RESYNC
Definition nsvdec.c:37
static int nsv_parse_NSVf_header(AVFormatContext *s)
Definition nsvdec.c:262
NSVStatus
Definition nsvdec.c:145
@ NSV_GOT_AUDIO
Definition nsvdec.c:153
@ NSV_FOUND_NSVS
Definition nsvdec.c:149
@ NSV_FOUND_BEEF
Definition nsvdec.c:151
@ NSV_GOT_VIDEO
Definition nsvdec.c:152
@ NSV_HAS_READ_NSVS
Definition nsvdec.c:150
@ NSV_UNSYNC
Definition nsvdec.c:146
@ NSV_FOUND_NSVF
Definition nsvdec.c:147
@ NSV_HAS_READ_NSVF
Definition nsvdec.c:148
An AVChannelLayout holds information about the channel layout of audio data.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1401
Bytestream IO Context.
Definition avio.h:160
int64_t pos
Definition avformat.h:623
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition avformat.h:624
This structure stores compressed data.
Definition packet.h:587
uint8_t * data
Definition packet.h:610
This structure contains the data a format has to probe a file.
Definition avformat.h:473
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:827
void * priv_data
Definition avformat.h:793
int id
Format-specific stream ID.
Definition avformat.h:780
int index
stream index in AVFormatContext
Definition avformat.h:774
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:817
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition internal.h:196
enum AVStreamParseType need_parsing
Definition internal.h:340
int16_t avsync
Definition nsvdec.c:179
AVRational framerate
Definition nsvdec.c:180
uint32_t * nsvs_timestamps
Definition nsvdec.c:181
int NSVf_end
Definition nsvdec.c:170
int index_entries
Definition nsvdec.c:172
enum NSVStatus state
Definition nsvdec.c:173
uint16_t vwidth
Definition nsvdec.c:178
int64_t duration
Definition nsvdec.c:176
AVPacket ahead[2]
Definition nsvdec.c:174
uint32_t atag
Definition nsvdec.c:177
uint16_t vheight
Definition nsvdec.c:178
int base_offset
Definition nsvdec.c:169
uint32_t vtag
Definition nsvdec.c:177
uint32_t * nsvs_file_offset
Definition nsvdec.c:171
int nsvf
Definition nsvdec.c:182
int rate
Definition nsvdec.c:160
int new_frame_offset
Definition nsvdec.c:164
int frame_offset
Definition nsvdec.c:157
int start
Definition nsvdec.c:162
int cum_len
Definition nsvdec.c:165
int scale
Definition nsvdec.c:159
int sample_size
Definition nsvdec.c:161
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
float framerate
Definition av1_levels.c:29
int size