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
32
33 /* max bytes to crawl for trying to resync
34 * stupid streaming servers don't start at chunk boundaries...
35 */
36 #define NSV_MAX_RESYNC (500*1024)
37 #define NSV_MAX_RESYNC_TRIES 300
38
39 /*
40 * References:
41 * (1) http://www.multimedia.cx/nsv-format.txt
42 * seems someone came to the same conclusions as me, and updated it:
43 * (2) http://www.stud.ktu.lt/~vitslav/nsv/nsv-format.txt
44 * http://www.stud.ktu.lt/~vitslav/nsv/
45 * official docs
46 * (3) http://ultravox.aol.com/NSVFormat.rtf
47 * Sample files:
48 * (S1) http://www.nullsoft.com/nsv/samples/
49 * http://www.nullsoft.com/nsv/samples/faster.nsv
50 * http://streamripper.sourceforge.net/openbb/read.php?TID=492&page=4
51 */
52
53 /*
54 * notes on the header (Francois Revol):
55 *
56 * It is followed by strings, then a table, but nothing tells
57 * where the table begins according to (1). After checking faster.nsv,
58 * I believe NVSf[16-19] gives the size of the strings data
59 * (that is the offset of the data table after the header).
60 * After checking all samples from (S1) all confirms this.
61 *
62 * Then, about NSVf[12-15], faster.nsf has 179700. When viewing it in VLC,
63 * I noticed there was about 1 NVSs chunk/s, so I ran
64 * strings faster.nsv | grep NSVs | wc -l
65 * which gave me 180. That leads me to think that NSVf[12-15] might be the
66 * file length in milliseconds.
67 * Let's try that:
68 * 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
69 * except for nsvtrailer (which doesn't have an NSVf header), it reports correct time.
70 *
71 * nsvtrailer.nsv (S1) does not have any NSVf header, only NSVs chunks,
72 * so the header seems to not be mandatory. (for streaming).
73 *
74 * index slice duration check (excepts nsvtrailer.nsv):
75 * 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, slite time $(($DUR/$IC))"; done
76 */
77
78 /*
79 * TODO:
80 * - handle timestamps !!!
81 * - use index
82 * - mime-type in probe()
83 * - seek
84 */
85
86 #if 0
87 struct NSVf_header {
88 uint32_t chunk_tag; /* 'NSVf' */
89 uint32_t chunk_size;
90 uint32_t file_size; /* max 4GB ??? no one learns anything it seems :^) */
91 uint32_t file_length; //unknown1; /* what about MSB of file_size ? */
92 uint32_t info_strings_size; /* size of the info strings */ //unknown2;
93 uint32_t table_entries;
94 uint32_t table_entries_used; /* the left ones should be -1 */
95 };
96
97 struct NSVs_header {
98 uint32_t chunk_tag; /* 'NSVs' */
99 uint32_t v4cc; /* or 'NONE' */
100 uint32_t a4cc; /* or 'NONE' */
101 uint16_t vwidth; /* av_assert0(vwidth%16==0) */
102 uint16_t vheight; /* av_assert0(vheight%16==0) */
103 uint8_t
framerate;
/* value = (framerate&0x80)?frtable[frameratex0x7f]:framerate */
104 uint16_t unknown;
105 };
106
107 struct nsv_avchunk_header {
108 uint8_t vchunk_size_lsb;
109 uint16_t vchunk_size_msb; /* value = (vchunk_size_msb << 4) | (vchunk_size_lsb >> 4) */
110 uint16_t achunk_size;
111 };
112
113 struct nsv_pcm_header {
114 uint8_t bits_per_sample;
115 uint8_t channel_count;
116 uint16_t sample_rate;
117 };
118 #endif
119
120 /* variation from avi.h */
121 /*typedef struct CodecTag {
122 int id;
123 unsigned int tag;
124 } CodecTag;*/
125
126 /* tags */
127
128 #define T_NSVF MKTAG('N', 'S', 'V', 'f') /* file header */
129 #define T_NSVS MKTAG('N', 'S', 'V', 's') /* chunk header */
130 #define T_TOC2 MKTAG('T', 'O', 'C', '2') /* extra index marker */
131 #define T_NONE MKTAG('N', 'O', 'N', 'E') /* null a/v 4CC */
132 #define T_SUBT MKTAG('S', 'U', 'B', 'T') /* subtitle aux data */
133 #define T_ASYN MKTAG('A', 'S', 'Y', 'N') /* async a/v aux marker */
134 #define T_KEYF MKTAG('K', 'E', 'Y', 'F') /* video keyframe aux marker (addition) */
135
136 #define TB_NSVF MKBETAG('N', 'S', 'V', 'f')
137 #define TB_NSVS MKBETAG('N', 'S', 'V', 's')
138
139 /* hardcoded stream indexes */
140 #define NSV_ST_VIDEO 0
141 #define NSV_ST_AUDIO 1
142 #define NSV_ST_SUBT 2
143
153 };
154
157 (used to compute the pts) */
162
164 int cum_len;
/* temporary storage (used during seek) */
166
174 /* cached */
183
195 /*
196 { AV_CODEC_ID_VP4, MKTAG('V', 'P', '4', ' ') },
197 { AV_CODEC_ID_VP4, MKTAG('V', 'P', '4', '0') },
198 */
203 };
204
214 };
215
216 //static int nsv_load_index(AVFormatContext *s);
218
219 /* try to find something we recognize, and set the state accordingly */
221 {
224 uint32_t v = 0;
226
231 return -1;
232 }
233 v <<= 8;
237 }
238
239 if ((v & 0x0000ffff) == 0xefbe) { /* BEEF */
242 return 0;
243 }
244 /* we read as big-endian, thus the MK*BE* */
248 return 0;
249 }
250 if (v ==
MKBETAG(
'N',
'S',
'V',
's')) {
/* NSVs */
253 return 0;
254 }
255
256 }
258 return -1;
259 }
260
262 {
268 int strings_size;
269 int table_entries;
270 int table_entries_used;
271
273
276 return 0;
277 }
279
282 return -1;
284
288
291 // XXX: store it in AVStreams
292
297 strings_size, table_entries, table_entries_used);
299 return -1;
300
302
303 if (strings_size > 0) {
304 char *strings; /* last byte will be '0円' to play safe with str*() */
305 char *p, *endp;
307 char quote;
308
309 p = strings =
av_mallocz((
size_t)strings_size + 1);
310 if (!p)
312 endp = strings + strings_size;
314 while (p < endp) {
315 while (*p == ' ')
316 p++; /* strip out spaces */
317 if (p >= endp-2)
318 break;
319 token = p;
320 p = strchr(p, '=');
321 if (!p || p >= endp-2)
322 break;
323 *p++ = '0円';
324 quote = *p++;
326 p = strchr(p, quote);
327 if (!p || p >= endp)
328 break;
329 *p++ = '0円';
332 }
334 }
336 return -1;
337
339
340 if (table_entries_used > 0) {
343 if((unsigned)table_entries_used >= UINT_MAX / sizeof(uint32_t))
344 return -1;
348
349 for(
i=0;
i<table_entries_used;
i++) {
353 }
354
355 if(table_entries > table_entries_used &&
360 for(
i=0;
i<table_entries_used;
i++) {
362 }
363 }
364 }
365
367
369
371 return -1;
373 return 0;
374 }
375
377 {
380 uint32_t vtag, atag;
381 uint16_t vwidth, vheight;
386
392
394 if(
i&0x80) {
/* odd way of giving native framerates from docs */
398
402 }
403
407 }
408 else
410
413
415
416 /* XXX change to ap != NULL ? */
417 if (
s->nb_streams == 0) {
/* streams not yet published, let's do that */
425 if (!st)
427
430 if (!nst)
439
443
448 } else {
451 }
452 }
453 }
456 if (!st)
458
461 if (!nst)
467
468 if (atag ==
MKTAG(
'A',
'A',
'V',
' ')) {
469 static const uint8_t aav_pce[] = {
470 0x12, 0x00, 0x05, 0x08, 0x48, 0x00,
471 0x20, 0x00, 0xC6, 0x40, 0x04, 0x4C,
472 0x61, 0x76, 0x63, 0x56, 0xE5, 0x00,
473 0x00, 0x00,
474 };
476
479
482 }
483
485
486 /* set timebase to common denominator of ms and framerate */
490 }
491 } else {
494 //return -1;
495 }
496 }
497
499 return 0;
501 /* XXX */
503 return -1;
504 }
505
507 {
510
513
516 if (err < 0)
517 return err;
520 if (err < 0)
521 return err;
522 }
523 /* we need the first NSVs also... */
526 if (err < 0)
527 return err;
528 break; /* we just want the first one */
529 }
530 }
531 if (
s->nb_streams < 1)
/* no luck so far */
533
534 /* now read the first chunk, so we can attempt to decode more info */
536
538 return err;
539 }
540
542 {
549 uint8_t auxcount; /* number of aux metadata, also 4 bits of vsize */
550 uint32_t vsize;
551 uint16_t asize;
552 uint16_t auxsize;
554
556 return 0; //-1; /* hey! eat what you've in your plate first! */
557
558 null_chunk_retry:
560 return -1;
561
564 if (err < 0)
565 return err;
568 if (err < 0)
569 return err;
571 return -1;
572
576 vsize = (vsize << 4) | (auxcount >> 4);
577 auxcount &= 0x0f;
578 av_log(
s,
AV_LOG_TRACE,
"NSV CHUNK %d aux, %"PRIu32
" bytes video, %d bytes audio\n", auxcount, vsize, asize);
579 /* skip aux stuff */
580 for (
i = 0;
i < auxcount;
i++) {
585 vsize -= auxsize + sizeof(uint16_t) + sizeof(uint32_t); /* that's becoming brain-dead */
586 }
587
589 return -1;
590 if (!vsize && !asize) {
592 goto null_chunk_retry;
593 }
594
595 /* map back streams to v,a */
596 if (
s->nb_streams > 0)
597 st[
s->streams[0]->id] =
s->streams[0];
598 if (
s->nb_streams > 1)
599 st[
s->streams[1]->id] =
s->streams[1];
600
609 for (
i = 0;
i <
FFMIN(8, vsize);
i++)
611 }
614
618 /* read raw audio specific header on the first audio chunk... */
619 /* on ALL audio chunks ?? seems so! */
620 if (asize >= 4 && st[
NSV_ST_AUDIO]->codecpar->codec_tag ==
MKTAG(
'P',
'C',
'M',
' ')
/* && fill_header*/) {
623 uint16_t samplerate;
629 asize-=4;
631 if (fill_header) {
635 }
639 samplerate /= 4;/* UGH ??? XXX */
644 }
645 }
651 /* on a nsvs frame we have new information on a/v sync */
656 }
658 }
659
661 return 0;
662 }
663
664
666 {
669
670 /* in case we don't already have something to eat ... */
673 if (err < 0)
674 return err;
675
676 /* now pick one of the plates */
677 for (
i = 0;
i < 2;
i++) {
680 return 0;
681 }
682 }
683
684 /* this restaurant is not provisioned :^] */
685 return -1;
686 }
687
689 {
695
698 return -1;
699
701 return -1;
702
705 return 0;
706 }
707
709 {
711
718 return 0;
719 }
720
722 {
724
725 /* check file header */
726 /* streamed files might not have any header */
727 if (p->
buf[0] ==
'N' && p->
buf[1] ==
'S' &&
728 p->
buf[2] ==
'V' && (p->
buf[3] ==
'f' || p->
buf[3] ==
's'))
730 /* XXX: do streamed files always start at chunk boundary ?? */
731 /* or do we need to search NSVs in the byte stream ? */
732 /* seems the servers don't bother starting clean chunks... */
733 /* sometimes even the first header is at 9KB or something :^) */
736 /* Get the chunk size and check if at the end we are getting 0xBEEF */
739 int offset =
i + 23 + asize + vsize + 1;
743 }
744 }
745 /* so we'll have more luck on extension... */
748 /* FIXME: add mime-type check */
749 return score;
750 }
751
762 };