You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

769 lines
25KB

  1. /*
  2. * NSV demuxer
  3. * Copyright (c) 2004 The Libav Project
  4. *
  5. * first version by Francois Revol <revol@free.fr>
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/attributes.h"
  24. #include "libavutil/mathematics.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "libavutil/dict.h"
  28. /* max bytes to crawl for trying to resync
  29. * stupid streaming servers don't start at chunk boundaries...
  30. */
  31. #define NSV_MAX_RESYNC (500*1024)
  32. #define NSV_MAX_RESYNC_TRIES 300
  33. /*
  34. * References:
  35. * (1) http://www.multimedia.cx/nsv-format.txt
  36. * seems someone came to the same conclusions as me, and updated it:
  37. * (2) http://www.stud.ktu.lt/~vitslav/nsv/nsv-format.txt
  38. * http://www.stud.ktu.lt/~vitslav/nsv/
  39. * official docs
  40. * (3) http://ultravox.aol.com/NSVFormat.rtf
  41. * Sample files:
  42. * (S1) http://www.nullsoft.com/nsv/samples/
  43. * http://www.nullsoft.com/nsv/samples/faster.nsv
  44. * http://streamripper.sourceforge.net/openbb/read.php?TID=492&page=4
  45. */
  46. /*
  47. * notes on the header (Francois Revol):
  48. *
  49. * It is followed by strings, then a table, but nothing tells
  50. * where the table begins according to (1). After checking faster.nsv,
  51. * I believe NVSf[16-19] gives the size of the strings data
  52. * (that is the offset of the data table after the header).
  53. * After checking all samples from (S1) all confirms this.
  54. *
  55. * Then, about NSVf[12-15], faster.nsf has 179700. When veiwing it in VLC,
  56. * I noticed there was about 1 NVSs chunk/s, so I ran
  57. * strings faster.nsv | grep NSVs | wc -l
  58. * which gave me 180. That leads me to think that NSVf[12-15] might be the
  59. * file length in milliseconds.
  60. * Let's try that:
  61. * 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
  62. * except for nstrailer (which doesn't have an NSVf header), it repports correct time.
  63. *
  64. * nsvtrailer.nsv (S1) does not have any NSVf header, only NSVs chunks,
  65. * so the header seems to not be mandatory. (for streaming).
  66. *
  67. * index slice duration check (excepts nsvtrailer.nsv):
  68. * for f in [^n]*.nsv; do
  69. * DUR="$(avconv -i "$f" 2> /dev/null | grep 'NSVf duration' | cut -d ' ' -f 4)"
  70. * IC="$(avconv -i "$f" 2> /dev/null | grep 'INDEX ENTRIES' | cut -d ' ' -f 2)"
  71. * echo "duration $DUR, slite time $(($DUR/$IC))"
  72. * done
  73. */
  74. /*
  75. * TODO:
  76. * - handle timestamps !!!
  77. * - use index
  78. * - mime-type in probe()
  79. * - seek
  80. */
  81. #if 0
  82. struct NSVf_header {
  83. uint32_t chunk_tag; /* 'NSVf' */
  84. uint32_t chunk_size;
  85. uint32_t file_size; /* max 4GB ??? no one learns anything it seems :^) */
  86. uint32_t file_length; //unknown1; /* what about MSB of file_size ? */
  87. uint32_t info_strings_size; /* size of the info strings */ //unknown2;
  88. uint32_t table_entries;
  89. uint32_t table_entries_used; /* the left ones should be -1 */
  90. };
  91. struct NSVs_header {
  92. uint32_t chunk_tag; /* 'NSVs' */
  93. uint32_t v4cc; /* or 'NONE' */
  94. uint32_t a4cc; /* or 'NONE' */
  95. uint16_t vwidth; /* assert(vwidth%16==0) */
  96. uint16_t vheight; /* assert(vheight%16==0) */
  97. uint8_t framerate; /* value = (framerate&0x80)?frtable[frameratex0x7f]:framerate */
  98. uint16_t unknown;
  99. };
  100. struct nsv_avchunk_header {
  101. uint8_t vchunk_size_lsb;
  102. uint16_t vchunk_size_msb; /* value = (vchunk_size_msb << 4) | (vchunk_size_lsb >> 4) */
  103. uint16_t achunk_size;
  104. };
  105. struct nsv_pcm_header {
  106. uint8_t bits_per_sample;
  107. uint8_t channel_count;
  108. uint16_t sample_rate;
  109. };
  110. #endif
  111. /* variation from avi.h */
  112. /*typedef struct CodecTag {
  113. int id;
  114. unsigned int tag;
  115. } CodecTag;*/
  116. /* tags */
  117. #define T_NSVF MKTAG('N', 'S', 'V', 'f') /* file header */
  118. #define T_NSVS MKTAG('N', 'S', 'V', 's') /* chunk header */
  119. #define T_TOC2 MKTAG('T', 'O', 'C', '2') /* extra index marker */
  120. #define T_NONE MKTAG('N', 'O', 'N', 'E') /* null a/v 4CC */
  121. #define T_SUBT MKTAG('S', 'U', 'B', 'T') /* subtitle aux data */
  122. #define T_ASYN MKTAG('A', 'S', 'Y', 'N') /* async a/v aux marker */
  123. #define T_KEYF MKTAG('K', 'E', 'Y', 'F') /* video keyframe aux marker (addition) */
  124. #define TB_NSVF MKBETAG('N', 'S', 'V', 'f')
  125. #define TB_NSVS MKBETAG('N', 'S', 'V', 's')
  126. /* hardcoded stream indexes */
  127. #define NSV_ST_VIDEO 0
  128. #define NSV_ST_AUDIO 1
  129. #define NSV_ST_SUBT 2
  130. enum NSVStatus {
  131. NSV_UNSYNC,
  132. NSV_FOUND_NSVF,
  133. NSV_HAS_READ_NSVF,
  134. NSV_FOUND_NSVS,
  135. NSV_HAS_READ_NSVS,
  136. NSV_FOUND_BEEF,
  137. NSV_GOT_VIDEO,
  138. NSV_GOT_AUDIO,
  139. };
  140. typedef struct NSVStream {
  141. int frame_offset; /* current frame (video) or byte (audio) counter
  142. (used to compute the pts) */
  143. int scale;
  144. int rate;
  145. int sample_size; /* audio only data */
  146. int start;
  147. int new_frame_offset; /* temporary storage (used during seek) */
  148. int cum_len; /* temporary storage (used during seek) */
  149. } NSVStream;
  150. typedef struct NSVContext {
  151. int base_offset;
  152. int NSVf_end;
  153. uint32_t *nsvs_file_offset;
  154. int index_entries;
  155. enum NSVStatus state;
  156. AVPacket ahead[2]; /* [v, a] if .data is !NULL there is something */
  157. /* cached */
  158. int64_t duration;
  159. uint32_t vtag, atag;
  160. uint16_t vwidth, vheight;
  161. int16_t avsync;
  162. AVRational framerate;
  163. uint32_t *nsvs_timestamps;
  164. //DVDemuxContext* dv_demux;
  165. } NSVContext;
  166. static const AVCodecTag nsv_codec_video_tags[] = {
  167. { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', ' ') },
  168. { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '0') },
  169. { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') },
  170. { AV_CODEC_ID_VP5, MKTAG('V', 'P', '5', ' ') },
  171. { AV_CODEC_ID_VP5, MKTAG('V', 'P', '5', '0') },
  172. { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', ' ') },
  173. { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '0') },
  174. { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '1') },
  175. { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '2') },
  176. /*
  177. { AV_CODEC_ID_VP4, MKTAG('V', 'P', '4', ' ') },
  178. { AV_CODEC_ID_VP4, MKTAG('V', 'P', '4', '0') },
  179. */
  180. { AV_CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') }, /* cf sample xvid decoder from nsv_codec_sdk.zip */
  181. { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', '3') },
  182. { AV_CODEC_ID_NONE, 0 },
  183. };
  184. static const AVCodecTag nsv_codec_audio_tags[] = {
  185. { AV_CODEC_ID_MP3, MKTAG('M', 'P', '3', ' ') },
  186. { AV_CODEC_ID_AAC, MKTAG('A', 'A', 'C', ' ') },
  187. { AV_CODEC_ID_AAC, MKTAG('A', 'A', 'C', 'P') },
  188. { AV_CODEC_ID_SPEEX, MKTAG('S', 'P', 'X', ' ') },
  189. { AV_CODEC_ID_PCM_U16LE, MKTAG('P', 'C', 'M', ' ') },
  190. { AV_CODEC_ID_NONE, 0 },
  191. };
  192. //static int nsv_load_index(AVFormatContext *s);
  193. static int nsv_read_chunk(AVFormatContext *s, int fill_header);
  194. #define print_tag(str, tag, size) \
  195. av_dlog(NULL, "%s: tag=%c%c%c%c\n", \
  196. str, tag & 0xff, \
  197. (tag >> 8) & 0xff, \
  198. (tag >> 16) & 0xff, \
  199. (tag >> 24) & 0xff);
  200. /* try to find something we recognize, and set the state accordingly */
  201. static int nsv_resync(AVFormatContext *s)
  202. {
  203. NSVContext *nsv = s->priv_data;
  204. AVIOContext *pb = s->pb;
  205. uint32_t v = 0;
  206. int i;
  207. av_dlog(s, "%s(), offset = %"PRId64", state = %d\n", __FUNCTION__, avio_tell(pb), nsv->state);
  208. //nsv->state = NSV_UNSYNC;
  209. for (i = 0; i < NSV_MAX_RESYNC; i++) {
  210. if (pb->eof_reached) {
  211. av_dlog(s, "NSV EOF\n");
  212. nsv->state = NSV_UNSYNC;
  213. return -1;
  214. }
  215. v <<= 8;
  216. v |= avio_r8(pb);
  217. if (i < 8) {
  218. av_dlog(s, "NSV resync: [%d] = %02x\n", i, v & 0x0FF);
  219. }
  220. if ((v & 0x0000ffff) == 0xefbe) { /* BEEF */
  221. av_dlog(s, "NSV resynced on BEEF after %d bytes\n", i+1);
  222. nsv->state = NSV_FOUND_BEEF;
  223. return 0;
  224. }
  225. /* we read as big-endian, thus the MK*BE* */
  226. if (v == TB_NSVF) { /* NSVf */
  227. av_dlog(s, "NSV resynced on NSVf after %d bytes\n", i+1);
  228. nsv->state = NSV_FOUND_NSVF;
  229. return 0;
  230. }
  231. if (v == MKBETAG('N', 'S', 'V', 's')) { /* NSVs */
  232. av_dlog(s, "NSV resynced on NSVs after %d bytes\n", i+1);
  233. nsv->state = NSV_FOUND_NSVS;
  234. return 0;
  235. }
  236. }
  237. av_dlog(s, "NSV sync lost\n");
  238. return -1;
  239. }
  240. static int nsv_parse_NSVf_header(AVFormatContext *s)
  241. {
  242. NSVContext *nsv = s->priv_data;
  243. AVIOContext *pb = s->pb;
  244. unsigned int av_unused file_size;
  245. unsigned int size;
  246. int64_t duration;
  247. int strings_size;
  248. int table_entries;
  249. int table_entries_used;
  250. av_dlog(s, "%s()\n", __FUNCTION__);
  251. nsv->state = NSV_UNSYNC; /* in case we fail */
  252. size = avio_rl32(pb);
  253. if (size < 28)
  254. return -1;
  255. nsv->NSVf_end = size;
  256. //s->file_size = (uint32_t)avio_rl32(pb);
  257. file_size = (uint32_t)avio_rl32(pb);
  258. av_dlog(s, "NSV NSVf chunk_size %u\n", size);
  259. av_dlog(s, "NSV NSVf file_size %u\n", file_size);
  260. nsv->duration = duration = avio_rl32(pb); /* in ms */
  261. av_dlog(s, "NSV NSVf duration %"PRId64" ms\n", duration);
  262. // XXX: store it in AVStreams
  263. strings_size = avio_rl32(pb);
  264. table_entries = avio_rl32(pb);
  265. table_entries_used = avio_rl32(pb);
  266. av_dlog(s, "NSV NSVf info-strings size: %d, table entries: %d, bis %d\n",
  267. strings_size, table_entries, table_entries_used);
  268. if (pb->eof_reached)
  269. return -1;
  270. av_dlog(s, "NSV got header; filepos %"PRId64"\n", avio_tell(pb));
  271. if (strings_size > 0) {
  272. char *strings; /* last byte will be '\0' to play safe with str*() */
  273. char *p, *endp;
  274. char *token, *value;
  275. char quote;
  276. p = strings = av_mallocz((size_t)strings_size + 1);
  277. if (!p)
  278. return AVERROR(ENOMEM);
  279. endp = strings + strings_size;
  280. avio_read(pb, strings, strings_size);
  281. while (p < endp) {
  282. while (*p == ' ')
  283. p++; /* strip out spaces */
  284. if (p >= endp-2)
  285. break;
  286. token = p;
  287. p = strchr(p, '=');
  288. if (!p || p >= endp-2)
  289. break;
  290. *p++ = '\0';
  291. quote = *p++;
  292. value = p;
  293. p = strchr(p, quote);
  294. if (!p || p >= endp)
  295. break;
  296. *p++ = '\0';
  297. av_dlog(s, "NSV NSVf INFO: %s='%s'\n", token, value);
  298. av_dict_set(&s->metadata, token, value, 0);
  299. }
  300. av_free(strings);
  301. }
  302. if (pb->eof_reached)
  303. return -1;
  304. av_dlog(s, "NSV got infos; filepos %"PRId64"\n", avio_tell(pb));
  305. if (table_entries_used > 0) {
  306. int i;
  307. nsv->index_entries = table_entries_used;
  308. if((unsigned)table_entries_used >= UINT_MAX / sizeof(uint32_t))
  309. return -1;
  310. nsv->nsvs_file_offset = av_malloc((unsigned)table_entries_used * sizeof(uint32_t));
  311. if (!nsv->nsvs_file_offset)
  312. return AVERROR(ENOMEM);
  313. for(i=0;i<table_entries_used;i++)
  314. nsv->nsvs_file_offset[i] = avio_rl32(pb) + size;
  315. if(table_entries > table_entries_used &&
  316. avio_rl32(pb) == MKTAG('T','O','C','2')) {
  317. nsv->nsvs_timestamps = av_malloc((unsigned)table_entries_used*sizeof(uint32_t));
  318. if (!nsv->nsvs_timestamps)
  319. return AVERROR(ENOMEM);
  320. for(i=0;i<table_entries_used;i++) {
  321. nsv->nsvs_timestamps[i] = avio_rl32(pb);
  322. }
  323. }
  324. }
  325. av_dlog(s, "NSV got index; filepos %"PRId64"\n", avio_tell(pb));
  326. avio_seek(pb, nsv->base_offset + size, SEEK_SET); /* required for dumbdriving-271.nsv (2 extra bytes) */
  327. if (pb->eof_reached)
  328. return -1;
  329. nsv->state = NSV_HAS_READ_NSVF;
  330. return 0;
  331. }
  332. static int nsv_parse_NSVs_header(AVFormatContext *s)
  333. {
  334. NSVContext *nsv = s->priv_data;
  335. AVIOContext *pb = s->pb;
  336. uint32_t vtag, atag;
  337. uint16_t vwidth, vheight;
  338. AVRational framerate;
  339. int i;
  340. AVStream *st;
  341. NSVStream *nst;
  342. av_dlog(s, "%s()\n", __FUNCTION__);
  343. vtag = avio_rl32(pb);
  344. atag = avio_rl32(pb);
  345. vwidth = avio_rl16(pb);
  346. vheight = avio_rl16(pb);
  347. i = avio_r8(pb);
  348. av_dlog(s, "NSV NSVs framerate code %2x\n", i);
  349. if(i&0x80) { /* odd way of giving native framerates from docs */
  350. int t=(i & 0x7F)>>2;
  351. if(t<16) framerate = (AVRational){1, t+1};
  352. else framerate = (AVRational){t-15, 1};
  353. if(i&1){
  354. framerate.num *= 1000;
  355. framerate.den *= 1001;
  356. }
  357. if((i&3)==3) framerate.num *= 24;
  358. else if((i&3)==2) framerate.num *= 25;
  359. else framerate.num *= 30;
  360. }
  361. else
  362. framerate= (AVRational){i, 1};
  363. nsv->avsync = avio_rl16(pb);
  364. nsv->framerate = framerate;
  365. print_tag("NSV NSVs vtag", vtag, 0);
  366. print_tag("NSV NSVs atag", atag, 0);
  367. av_dlog(s, "NSV NSVs vsize %dx%d\n", vwidth, vheight);
  368. /* XXX change to ap != NULL ? */
  369. if (s->nb_streams == 0) { /* streams not yet published, let's do that */
  370. nsv->vtag = vtag;
  371. nsv->atag = atag;
  372. nsv->vwidth = vwidth;
  373. nsv->vheight = vwidth;
  374. if (vtag != T_NONE) {
  375. int i;
  376. st = avformat_new_stream(s, NULL);
  377. if (!st)
  378. goto fail;
  379. st->id = NSV_ST_VIDEO;
  380. nst = av_mallocz(sizeof(NSVStream));
  381. if (!nst)
  382. goto fail;
  383. st->priv_data = nst;
  384. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  385. st->codec->codec_tag = vtag;
  386. st->codec->codec_id = ff_codec_get_id(nsv_codec_video_tags, vtag);
  387. st->codec->width = vwidth;
  388. st->codec->height = vheight;
  389. st->codec->bits_per_coded_sample = 24; /* depth XXX */
  390. avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
  391. st->start_time = 0;
  392. st->duration = av_rescale(nsv->duration, framerate.num, 1000*framerate.den);
  393. for(i=0;i<nsv->index_entries;i++) {
  394. if(nsv->nsvs_timestamps) {
  395. av_add_index_entry(st, nsv->nsvs_file_offset[i], nsv->nsvs_timestamps[i],
  396. 0, 0, AVINDEX_KEYFRAME);
  397. } else {
  398. int64_t ts = av_rescale(i*nsv->duration/nsv->index_entries, framerate.num, 1000*framerate.den);
  399. av_add_index_entry(st, nsv->nsvs_file_offset[i], ts, 0, 0, AVINDEX_KEYFRAME);
  400. }
  401. }
  402. }
  403. if (atag != T_NONE) {
  404. st = avformat_new_stream(s, NULL);
  405. if (!st)
  406. goto fail;
  407. st->id = NSV_ST_AUDIO;
  408. nst = av_mallocz(sizeof(NSVStream));
  409. if (!nst)
  410. goto fail;
  411. st->priv_data = nst;
  412. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  413. st->codec->codec_tag = atag;
  414. st->codec->codec_id = ff_codec_get_id(nsv_codec_audio_tags, atag);
  415. st->need_parsing = AVSTREAM_PARSE_FULL; /* for PCM we will read a chunk later and put correct info */
  416. /* set timebase to common denominator of ms and framerate */
  417. avpriv_set_pts_info(st, 64, 1, framerate.num*1000);
  418. st->start_time = 0;
  419. st->duration = (int64_t)nsv->duration * framerate.num;
  420. }
  421. } else {
  422. if (nsv->vtag != vtag || nsv->atag != atag || nsv->vwidth != vwidth || nsv->vheight != vwidth) {
  423. av_dlog(s, "NSV NSVs header values differ from the first one!!!\n");
  424. //return -1;
  425. }
  426. }
  427. nsv->state = NSV_HAS_READ_NSVS;
  428. return 0;
  429. fail:
  430. /* XXX */
  431. nsv->state = NSV_UNSYNC;
  432. return -1;
  433. }
  434. static int nsv_read_header(AVFormatContext *s)
  435. {
  436. NSVContext *nsv = s->priv_data;
  437. int i, err;
  438. av_dlog(s, "%s()\n", __FUNCTION__);
  439. av_dlog(s, "filename '%s'\n", s->filename);
  440. nsv->state = NSV_UNSYNC;
  441. nsv->ahead[0].data = nsv->ahead[1].data = NULL;
  442. for (i = 0; i < NSV_MAX_RESYNC_TRIES; i++) {
  443. if (nsv_resync(s) < 0)
  444. return -1;
  445. if (nsv->state == NSV_FOUND_NSVF) {
  446. err = nsv_parse_NSVf_header(s);
  447. if (err < 0)
  448. return err;
  449. }
  450. /* we need the first NSVs also... */
  451. if (nsv->state == NSV_FOUND_NSVS) {
  452. err = nsv_parse_NSVs_header(s);
  453. if (err < 0)
  454. return err;
  455. break; /* we just want the first one */
  456. }
  457. }
  458. if (s->nb_streams < 1) /* no luck so far */
  459. return -1;
  460. /* now read the first chunk, so we can attempt to decode more info */
  461. err = nsv_read_chunk(s, 1);
  462. av_dlog(s, "parsed header\n");
  463. return err;
  464. }
  465. static int nsv_read_chunk(AVFormatContext *s, int fill_header)
  466. {
  467. NSVContext *nsv = s->priv_data;
  468. AVIOContext *pb = s->pb;
  469. AVStream *st[2] = {NULL, NULL};
  470. NSVStream *nst;
  471. AVPacket *pkt;
  472. int i, err = 0;
  473. uint8_t auxcount; /* number of aux metadata, also 4 bits of vsize */
  474. uint32_t vsize;
  475. uint16_t asize;
  476. uint16_t auxsize;
  477. av_dlog(s, "%s(%d)\n", __FUNCTION__, fill_header);
  478. if (nsv->ahead[0].data || nsv->ahead[1].data)
  479. return 0; //-1; /* hey! eat what you've in your plate first! */
  480. null_chunk_retry:
  481. if (pb->eof_reached)
  482. return -1;
  483. for (i = 0; i < NSV_MAX_RESYNC_TRIES && nsv->state < NSV_FOUND_NSVS && !err; i++)
  484. err = nsv_resync(s);
  485. if (err < 0)
  486. return err;
  487. if (nsv->state == NSV_FOUND_NSVS)
  488. err = nsv_parse_NSVs_header(s);
  489. if (err < 0)
  490. return err;
  491. if (nsv->state != NSV_HAS_READ_NSVS && nsv->state != NSV_FOUND_BEEF)
  492. return -1;
  493. auxcount = avio_r8(pb);
  494. vsize = avio_rl16(pb);
  495. asize = avio_rl16(pb);
  496. vsize = (vsize << 4) | (auxcount >> 4);
  497. auxcount &= 0x0f;
  498. av_dlog(s, "NSV CHUNK %d aux, %u bytes video, %d bytes audio\n", auxcount, vsize, asize);
  499. /* skip aux stuff */
  500. for (i = 0; i < auxcount; i++) {
  501. uint32_t av_unused auxtag;
  502. auxsize = avio_rl16(pb);
  503. auxtag = avio_rl32(pb);
  504. av_dlog(s, "NSV aux data: '%c%c%c%c', %d bytes\n",
  505. (auxtag & 0x0ff),
  506. ((auxtag >> 8) & 0x0ff),
  507. ((auxtag >> 16) & 0x0ff),
  508. ((auxtag >> 24) & 0x0ff),
  509. auxsize);
  510. avio_skip(pb, auxsize);
  511. vsize -= auxsize + sizeof(uint16_t) + sizeof(uint32_t); /* that's becoming braindead */
  512. }
  513. if (pb->eof_reached)
  514. return -1;
  515. if (!vsize && !asize) {
  516. nsv->state = NSV_UNSYNC;
  517. goto null_chunk_retry;
  518. }
  519. /* map back streams to v,a */
  520. if (s->nb_streams > 0)
  521. st[s->streams[0]->id] = s->streams[0];
  522. if (s->nb_streams > 1)
  523. st[s->streams[1]->id] = s->streams[1];
  524. if (vsize && st[NSV_ST_VIDEO]) {
  525. nst = st[NSV_ST_VIDEO]->priv_data;
  526. pkt = &nsv->ahead[NSV_ST_VIDEO];
  527. av_get_packet(pb, pkt, vsize);
  528. pkt->stream_index = st[NSV_ST_VIDEO]->index;//NSV_ST_VIDEO;
  529. pkt->dts = nst->frame_offset;
  530. pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */
  531. for (i = 0; i < FFMIN(8, vsize); i++)
  532. av_dlog(s, "NSV video: [%d] = %02x\n", i, pkt->data[i]);
  533. }
  534. if(st[NSV_ST_VIDEO])
  535. ((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset++;
  536. if (asize && st[NSV_ST_AUDIO]) {
  537. nst = st[NSV_ST_AUDIO]->priv_data;
  538. pkt = &nsv->ahead[NSV_ST_AUDIO];
  539. /* read raw audio specific header on the first audio chunk... */
  540. /* on ALL audio chunks ?? seems so! */
  541. if (asize && st[NSV_ST_AUDIO]->codec->codec_tag == MKTAG('P', 'C', 'M', ' ')/* && fill_header*/) {
  542. uint8_t bps;
  543. uint8_t channels;
  544. uint16_t samplerate;
  545. bps = avio_r8(pb);
  546. channels = avio_r8(pb);
  547. samplerate = avio_rl16(pb);
  548. if (!channels || !samplerate)
  549. return AVERROR_INVALIDDATA;
  550. asize-=4;
  551. av_dlog(s, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);
  552. if (fill_header) {
  553. st[NSV_ST_AUDIO]->need_parsing = AVSTREAM_PARSE_NONE; /* we know everything */
  554. if (bps != 16) {
  555. av_dlog(s, "NSV AUDIO bit/sample != 16 (%d)!!!\n", bps);
  556. }
  557. bps /= channels; // ???
  558. if (bps == 8)
  559. st[NSV_ST_AUDIO]->codec->codec_id = AV_CODEC_ID_PCM_U8;
  560. samplerate /= 4;/* UGH ??? XXX */
  561. channels = 1;
  562. st[NSV_ST_AUDIO]->codec->channels = channels;
  563. st[NSV_ST_AUDIO]->codec->sample_rate = samplerate;
  564. av_dlog(s, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);
  565. }
  566. }
  567. av_get_packet(pb, pkt, asize);
  568. pkt->stream_index = st[NSV_ST_AUDIO]->index;//NSV_ST_AUDIO;
  569. pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */
  570. if( nsv->state == NSV_HAS_READ_NSVS && st[NSV_ST_VIDEO] ) {
  571. /* on a nsvs frame we have new information on a/v sync */
  572. pkt->dts = (((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset-1);
  573. pkt->dts *= (int64_t)1000 * nsv->framerate.den;
  574. pkt->dts += (int64_t)nsv->avsync * nsv->framerate.num;
  575. av_dlog(s, "NSV AUDIO: sync:%d, dts:%"PRId64, nsv->avsync, pkt->dts);
  576. }
  577. nst->frame_offset++;
  578. }
  579. nsv->state = NSV_UNSYNC;
  580. return 0;
  581. }
  582. static int nsv_read_packet(AVFormatContext *s, AVPacket *pkt)
  583. {
  584. NSVContext *nsv = s->priv_data;
  585. int i, err = 0;
  586. av_dlog(s, "%s()\n", __FUNCTION__);
  587. /* in case we don't already have something to eat ... */
  588. if (!nsv->ahead[0].data && !nsv->ahead[1].data)
  589. err = nsv_read_chunk(s, 0);
  590. if (err < 0)
  591. return err;
  592. /* now pick one of the plates */
  593. for (i = 0; i < 2; i++) {
  594. if (nsv->ahead[i].data) {
  595. av_dlog(s, "%s: using cached packet[%d]\n", __FUNCTION__, i);
  596. /* avoid the cost of new_packet + memcpy(->data) */
  597. memcpy(pkt, &nsv->ahead[i], sizeof(AVPacket));
  598. nsv->ahead[i].data = NULL; /* we ate that one */
  599. return pkt->size;
  600. }
  601. }
  602. /* this restaurant is not approvisionned :^] */
  603. return -1;
  604. }
  605. static int nsv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  606. {
  607. NSVContext *nsv = s->priv_data;
  608. AVStream *st = s->streams[stream_index];
  609. NSVStream *nst = st->priv_data;
  610. int index;
  611. index = av_index_search_timestamp(st, timestamp, flags);
  612. if(index < 0)
  613. return -1;
  614. if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
  615. return -1;
  616. nst->frame_offset = st->index_entries[index].timestamp;
  617. nsv->state = NSV_UNSYNC;
  618. return 0;
  619. }
  620. static int nsv_read_close(AVFormatContext *s)
  621. {
  622. NSVContext *nsv = s->priv_data;
  623. av_freep(&nsv->nsvs_file_offset);
  624. av_freep(&nsv->nsvs_timestamps);
  625. if (nsv->ahead[0].data)
  626. av_free_packet(&nsv->ahead[0]);
  627. if (nsv->ahead[1].data)
  628. av_free_packet(&nsv->ahead[1]);
  629. return 0;
  630. }
  631. static int nsv_probe(AVProbeData *p)
  632. {
  633. int i;
  634. int score;
  635. int vsize, asize, auxcount;
  636. score = 0;
  637. av_dlog(NULL, "nsv_probe(), buf_size %d\n", p->buf_size);
  638. /* check file header */
  639. /* streamed files might not have any header */
  640. if (p->buf[0] == 'N' && p->buf[1] == 'S' &&
  641. p->buf[2] == 'V' && (p->buf[3] == 'f' || p->buf[3] == 's'))
  642. return AVPROBE_SCORE_MAX;
  643. /* XXX: do streamed files always start at chunk boundary ?? */
  644. /* or do we need to search NSVs in the byte stream ? */
  645. /* seems the servers don't bother starting clean chunks... */
  646. /* sometimes even the first header is at 9KB or something :^) */
  647. for (i = 1; i < p->buf_size - 3; i++) {
  648. if (p->buf[i+0] == 'N' && p->buf[i+1] == 'S' &&
  649. p->buf[i+2] == 'V' && p->buf[i+3] == 's') {
  650. score = AVPROBE_SCORE_MAX/5;
  651. /* Get the chunk size and check if at the end we are getting 0xBEEF */
  652. auxcount = p->buf[i+19];
  653. vsize = p->buf[i+20] | p->buf[i+21] << 8;
  654. asize = p->buf[i+22] | p->buf[i+23] << 8;
  655. vsize = (vsize << 4) | (auxcount >> 4);
  656. if ((asize + vsize + i + 23) < p->buf_size - 2) {
  657. if (p->buf[i+23+asize+vsize+1] == 0xEF &&
  658. p->buf[i+23+asize+vsize+2] == 0xBE)
  659. return AVPROBE_SCORE_MAX-20;
  660. }
  661. }
  662. }
  663. /* so we'll have more luck on extension... */
  664. if (av_match_ext(p->filename, "nsv"))
  665. return AVPROBE_SCORE_EXTENSION;
  666. /* FIXME: add mime-type check */
  667. return score;
  668. }
  669. AVInputFormat ff_nsv_demuxer = {
  670. .name = "nsv",
  671. .long_name = NULL_IF_CONFIG_SMALL("Nullsoft Streaming Video"),
  672. .priv_data_size = sizeof(NSVContext),
  673. .read_probe = nsv_probe,
  674. .read_header = nsv_read_header,
  675. .read_packet = nsv_read_packet,
  676. .read_close = nsv_read_close,
  677. .read_seek = nsv_read_seek,
  678. };