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.

752 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 viewing 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 nsvtrailer (which doesn't have an NSVf header), it reports 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. } NSVContext;
  165. static const AVCodecTag nsv_codec_video_tags[] = {
  166. { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', ' ') },
  167. { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '0') },
  168. { AV_CODEC_ID_VP3, MKTAG('V', 'P', '3', '1') },
  169. { AV_CODEC_ID_VP5, MKTAG('V', 'P', '5', ' ') },
  170. { AV_CODEC_ID_VP5, MKTAG('V', 'P', '5', '0') },
  171. { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', ' ') },
  172. { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '0') },
  173. { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '1') },
  174. { AV_CODEC_ID_VP6, MKTAG('V', 'P', '6', '2') },
  175. /*
  176. { AV_CODEC_ID_VP4, MKTAG('V', 'P', '4', ' ') },
  177. { AV_CODEC_ID_VP4, MKTAG('V', 'P', '4', '0') },
  178. */
  179. { AV_CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') }, /* cf sample xvid decoder from nsv_codec_sdk.zip */
  180. { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', '3') },
  181. { AV_CODEC_ID_NONE, 0 },
  182. };
  183. static const AVCodecTag nsv_codec_audio_tags[] = {
  184. { AV_CODEC_ID_MP3, MKTAG('M', 'P', '3', ' ') },
  185. { AV_CODEC_ID_AAC, MKTAG('A', 'A', 'C', ' ') },
  186. { AV_CODEC_ID_AAC, MKTAG('A', 'A', 'C', 'P') },
  187. { AV_CODEC_ID_SPEEX, MKTAG('S', 'P', 'X', ' ') },
  188. { AV_CODEC_ID_PCM_U16LE, MKTAG('P', 'C', 'M', ' ') },
  189. { AV_CODEC_ID_NONE, 0 },
  190. };
  191. //static int nsv_load_index(AVFormatContext *s);
  192. static int nsv_read_chunk(AVFormatContext *s, int fill_header);
  193. #define print_tag(str, tag, size) \
  194. av_log(NULL, AV_LOG_TRACE, "%s: tag=%c%c%c%c\n", \
  195. str, tag & 0xff, \
  196. (tag >> 8) & 0xff, \
  197. (tag >> 16) & 0xff, \
  198. (tag >> 24) & 0xff);
  199. /* try to find something we recognize, and set the state accordingly */
  200. static int nsv_resync(AVFormatContext *s)
  201. {
  202. NSVContext *nsv = s->priv_data;
  203. AVIOContext *pb = s->pb;
  204. uint32_t v = 0;
  205. int i;
  206. for (i = 0; i < NSV_MAX_RESYNC; i++) {
  207. if (pb->eof_reached) {
  208. av_log(s, AV_LOG_TRACE, "NSV EOF\n");
  209. nsv->state = NSV_UNSYNC;
  210. return -1;
  211. }
  212. v <<= 8;
  213. v |= avio_r8(pb);
  214. if (i < 8) {
  215. av_log(s, AV_LOG_TRACE, "NSV resync: [%d] = %02x\n", i, v & 0x0FF);
  216. }
  217. if ((v & 0x0000ffff) == 0xefbe) { /* BEEF */
  218. av_log(s, AV_LOG_TRACE, "NSV resynced on BEEF after %d bytes\n", i+1);
  219. nsv->state = NSV_FOUND_BEEF;
  220. return 0;
  221. }
  222. /* we read as big-endian, thus the MK*BE* */
  223. if (v == TB_NSVF) { /* NSVf */
  224. av_log(s, AV_LOG_TRACE, "NSV resynced on NSVf after %d bytes\n", i+1);
  225. nsv->state = NSV_FOUND_NSVF;
  226. return 0;
  227. }
  228. if (v == MKBETAG('N', 'S', 'V', 's')) { /* NSVs */
  229. av_log(s, AV_LOG_TRACE, "NSV resynced on NSVs after %d bytes\n", i+1);
  230. nsv->state = NSV_FOUND_NSVS;
  231. return 0;
  232. }
  233. }
  234. av_log(s, AV_LOG_TRACE, "NSV sync lost\n");
  235. return -1;
  236. }
  237. static int nsv_parse_NSVf_header(AVFormatContext *s)
  238. {
  239. NSVContext *nsv = s->priv_data;
  240. AVIOContext *pb = s->pb;
  241. unsigned int av_unused file_size;
  242. unsigned int size;
  243. int64_t duration;
  244. int strings_size;
  245. int table_entries;
  246. int table_entries_used;
  247. nsv->state = NSV_UNSYNC; /* in case we fail */
  248. size = avio_rl32(pb);
  249. if (size < 28)
  250. return -1;
  251. nsv->NSVf_end = size;
  252. file_size = (uint32_t)avio_rl32(pb);
  253. av_log(s, AV_LOG_TRACE, "NSV NSVf chunk_size %u\n", size);
  254. av_log(s, AV_LOG_TRACE, "NSV NSVf file_size %u\n", file_size);
  255. nsv->duration = duration = avio_rl32(pb); /* in ms */
  256. av_log(s, AV_LOG_TRACE, "NSV NSVf duration %"PRId64" ms\n", duration);
  257. // XXX: store it in AVStreams
  258. strings_size = avio_rl32(pb);
  259. table_entries = avio_rl32(pb);
  260. table_entries_used = avio_rl32(pb);
  261. av_log(s, AV_LOG_TRACE, "NSV NSVf info-strings size: %d, table entries: %d, bis %d\n",
  262. strings_size, table_entries, table_entries_used);
  263. if (pb->eof_reached)
  264. return -1;
  265. av_log(s, AV_LOG_TRACE, "NSV got header; filepos %"PRId64"\n", avio_tell(pb));
  266. if (strings_size > 0) {
  267. char *strings; /* last byte will be '\0' to play safe with str*() */
  268. char *p, *endp;
  269. char *token, *value;
  270. char quote;
  271. p = strings = av_mallocz((size_t)strings_size + 1);
  272. if (!p)
  273. return AVERROR(ENOMEM);
  274. endp = strings + strings_size;
  275. avio_read(pb, strings, strings_size);
  276. while (p < endp) {
  277. while (*p == ' ')
  278. p++; /* strip out spaces */
  279. if (p >= endp-2)
  280. break;
  281. token = p;
  282. p = strchr(p, '=');
  283. if (!p || p >= endp-2)
  284. break;
  285. *p++ = '\0';
  286. quote = *p++;
  287. value = p;
  288. p = strchr(p, quote);
  289. if (!p || p >= endp)
  290. break;
  291. *p++ = '\0';
  292. av_log(s, AV_LOG_TRACE, "NSV NSVf INFO: %s='%s'\n", token, value);
  293. av_dict_set(&s->metadata, token, value, 0);
  294. }
  295. av_free(strings);
  296. }
  297. if (pb->eof_reached)
  298. return -1;
  299. av_log(s, AV_LOG_TRACE, "NSV got infos; filepos %"PRId64"\n", avio_tell(pb));
  300. if (table_entries_used > 0) {
  301. int i;
  302. nsv->index_entries = table_entries_used;
  303. if((unsigned)table_entries_used >= UINT_MAX / sizeof(uint32_t))
  304. return -1;
  305. nsv->nsvs_file_offset = av_malloc((unsigned)table_entries_used * sizeof(uint32_t));
  306. if (!nsv->nsvs_file_offset)
  307. return AVERROR(ENOMEM);
  308. for(i=0;i<table_entries_used;i++)
  309. nsv->nsvs_file_offset[i] = avio_rl32(pb) + size;
  310. if(table_entries > table_entries_used &&
  311. avio_rl32(pb) == MKTAG('T','O','C','2')) {
  312. nsv->nsvs_timestamps = av_malloc((unsigned)table_entries_used*sizeof(uint32_t));
  313. if (!nsv->nsvs_timestamps)
  314. return AVERROR(ENOMEM);
  315. for(i=0;i<table_entries_used;i++) {
  316. nsv->nsvs_timestamps[i] = avio_rl32(pb);
  317. }
  318. }
  319. }
  320. av_log(s, AV_LOG_TRACE, "NSV got index; filepos %"PRId64"\n", avio_tell(pb));
  321. avio_seek(pb, nsv->base_offset + size, SEEK_SET); /* required for dumbdriving-271.nsv (2 extra bytes) */
  322. if (pb->eof_reached)
  323. return -1;
  324. nsv->state = NSV_HAS_READ_NSVF;
  325. return 0;
  326. }
  327. static int nsv_parse_NSVs_header(AVFormatContext *s)
  328. {
  329. NSVContext *nsv = s->priv_data;
  330. AVIOContext *pb = s->pb;
  331. uint32_t vtag, atag;
  332. uint16_t vwidth, vheight;
  333. AVRational framerate;
  334. int i;
  335. AVStream *st;
  336. NSVStream *nst;
  337. vtag = avio_rl32(pb);
  338. atag = avio_rl32(pb);
  339. vwidth = avio_rl16(pb);
  340. vheight = avio_rl16(pb);
  341. i = avio_r8(pb);
  342. av_log(s, AV_LOG_TRACE, "NSV NSVs framerate code %2x\n", i);
  343. if(i&0x80) { /* odd way of giving native framerates from docs */
  344. int t=(i & 0x7F)>>2;
  345. if(t<16) framerate = (AVRational){1, t+1};
  346. else framerate = (AVRational){t-15, 1};
  347. if(i&1){
  348. framerate.num *= 1000;
  349. framerate.den *= 1001;
  350. }
  351. if((i&3)==3) framerate.num *= 24;
  352. else if((i&3)==2) framerate.num *= 25;
  353. else framerate.num *= 30;
  354. }
  355. else
  356. framerate= (AVRational){i, 1};
  357. nsv->avsync = avio_rl16(pb);
  358. nsv->framerate = framerate;
  359. print_tag("NSV NSVs vtag", vtag, 0);
  360. print_tag("NSV NSVs atag", atag, 0);
  361. av_log(s, AV_LOG_TRACE, "NSV NSVs vsize %dx%d\n", vwidth, vheight);
  362. /* XXX change to ap != NULL ? */
  363. if (s->nb_streams == 0) { /* streams not yet published, let's do that */
  364. nsv->vtag = vtag;
  365. nsv->atag = atag;
  366. nsv->vwidth = vwidth;
  367. nsv->vheight = vwidth;
  368. if (vtag != T_NONE) {
  369. int i;
  370. st = avformat_new_stream(s, NULL);
  371. if (!st)
  372. goto fail;
  373. st->id = NSV_ST_VIDEO;
  374. nst = av_mallocz(sizeof(NSVStream));
  375. if (!nst)
  376. goto fail;
  377. st->priv_data = nst;
  378. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  379. st->codecpar->codec_tag = vtag;
  380. st->codecpar->codec_id = ff_codec_get_id(nsv_codec_video_tags, vtag);
  381. st->codecpar->width = vwidth;
  382. st->codecpar->height = vheight;
  383. st->codecpar->bits_per_coded_sample = 24; /* depth XXX */
  384. avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
  385. st->start_time = 0;
  386. st->duration = av_rescale(nsv->duration, framerate.num, 1000*framerate.den);
  387. for(i=0;i<nsv->index_entries;i++) {
  388. if(nsv->nsvs_timestamps) {
  389. av_add_index_entry(st, nsv->nsvs_file_offset[i], nsv->nsvs_timestamps[i],
  390. 0, 0, AVINDEX_KEYFRAME);
  391. } else {
  392. int64_t ts = av_rescale(i*nsv->duration/nsv->index_entries, framerate.num, 1000*framerate.den);
  393. av_add_index_entry(st, nsv->nsvs_file_offset[i], ts, 0, 0, AVINDEX_KEYFRAME);
  394. }
  395. }
  396. }
  397. if (atag != T_NONE) {
  398. st = avformat_new_stream(s, NULL);
  399. if (!st)
  400. goto fail;
  401. st->id = NSV_ST_AUDIO;
  402. nst = av_mallocz(sizeof(NSVStream));
  403. if (!nst)
  404. goto fail;
  405. st->priv_data = nst;
  406. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  407. st->codecpar->codec_tag = atag;
  408. st->codecpar->codec_id = ff_codec_get_id(nsv_codec_audio_tags, atag);
  409. st->need_parsing = AVSTREAM_PARSE_FULL; /* for PCM we will read a chunk later and put correct info */
  410. /* set timebase to common denominator of ms and framerate */
  411. avpriv_set_pts_info(st, 64, 1, framerate.num*1000);
  412. st->start_time = 0;
  413. st->duration = (int64_t)nsv->duration * framerate.num;
  414. }
  415. } else {
  416. if (nsv->vtag != vtag || nsv->atag != atag || nsv->vwidth != vwidth || nsv->vheight != vwidth) {
  417. av_log(s, AV_LOG_TRACE, "NSV NSVs header values differ from the first one!!!\n");
  418. //return -1;
  419. }
  420. }
  421. nsv->state = NSV_HAS_READ_NSVS;
  422. return 0;
  423. fail:
  424. /* XXX */
  425. nsv->state = NSV_UNSYNC;
  426. return -1;
  427. }
  428. static int nsv_read_header(AVFormatContext *s)
  429. {
  430. NSVContext *nsv = s->priv_data;
  431. int i, err;
  432. nsv->state = NSV_UNSYNC;
  433. nsv->ahead[0].data = nsv->ahead[1].data = NULL;
  434. for (i = 0; i < NSV_MAX_RESYNC_TRIES; i++) {
  435. if (nsv_resync(s) < 0)
  436. return -1;
  437. if (nsv->state == NSV_FOUND_NSVF) {
  438. err = nsv_parse_NSVf_header(s);
  439. if (err < 0)
  440. return err;
  441. }
  442. /* we need the first NSVs also... */
  443. if (nsv->state == NSV_FOUND_NSVS) {
  444. err = nsv_parse_NSVs_header(s);
  445. if (err < 0)
  446. return err;
  447. break; /* we just want the first one */
  448. }
  449. }
  450. if (s->nb_streams < 1) /* no luck so far */
  451. return -1;
  452. /* now read the first chunk, so we can attempt to decode more info */
  453. err = nsv_read_chunk(s, 1);
  454. av_log(s, AV_LOG_TRACE, "parsed header\n");
  455. return err;
  456. }
  457. static int nsv_read_chunk(AVFormatContext *s, int fill_header)
  458. {
  459. NSVContext *nsv = s->priv_data;
  460. AVIOContext *pb = s->pb;
  461. AVStream *st[2] = {NULL, NULL};
  462. NSVStream *nst;
  463. AVPacket *pkt;
  464. int i, err = 0;
  465. uint8_t auxcount; /* number of aux metadata, also 4 bits of vsize */
  466. uint32_t vsize;
  467. uint16_t asize;
  468. uint16_t auxsize;
  469. if (nsv->ahead[0].data || nsv->ahead[1].data)
  470. return 0; //-1; /* hey! eat what you've in your plate first! */
  471. null_chunk_retry:
  472. if (pb->eof_reached)
  473. return -1;
  474. for (i = 0; i < NSV_MAX_RESYNC_TRIES && nsv->state < NSV_FOUND_NSVS && !err; i++)
  475. err = nsv_resync(s);
  476. if (err < 0)
  477. return err;
  478. if (nsv->state == NSV_FOUND_NSVS)
  479. err = nsv_parse_NSVs_header(s);
  480. if (err < 0)
  481. return err;
  482. if (nsv->state != NSV_HAS_READ_NSVS && nsv->state != NSV_FOUND_BEEF)
  483. return -1;
  484. auxcount = avio_r8(pb);
  485. vsize = avio_rl16(pb);
  486. asize = avio_rl16(pb);
  487. vsize = (vsize << 4) | (auxcount >> 4);
  488. auxcount &= 0x0f;
  489. av_log(s, AV_LOG_TRACE, "NSV CHUNK %d aux, %u bytes video, %d bytes audio\n", auxcount, vsize, asize);
  490. /* skip aux stuff */
  491. for (i = 0; i < auxcount; i++) {
  492. uint32_t av_unused auxtag;
  493. auxsize = avio_rl16(pb);
  494. auxtag = avio_rl32(pb);
  495. av_log(s, AV_LOG_TRACE, "NSV aux data: '%c%c%c%c', %d bytes\n",
  496. (auxtag & 0x0ff),
  497. ((auxtag >> 8) & 0x0ff),
  498. ((auxtag >> 16) & 0x0ff),
  499. ((auxtag >> 24) & 0x0ff),
  500. auxsize);
  501. avio_skip(pb, auxsize);
  502. vsize -= auxsize + sizeof(uint16_t) + sizeof(uint32_t); /* that's becoming brain-dead */
  503. }
  504. if (pb->eof_reached)
  505. return -1;
  506. if (!vsize && !asize) {
  507. nsv->state = NSV_UNSYNC;
  508. goto null_chunk_retry;
  509. }
  510. /* map back streams to v,a */
  511. if (s->nb_streams > 0)
  512. st[s->streams[0]->id] = s->streams[0];
  513. if (s->nb_streams > 1)
  514. st[s->streams[1]->id] = s->streams[1];
  515. if (vsize && st[NSV_ST_VIDEO]) {
  516. nst = st[NSV_ST_VIDEO]->priv_data;
  517. pkt = &nsv->ahead[NSV_ST_VIDEO];
  518. av_get_packet(pb, pkt, vsize);
  519. pkt->stream_index = st[NSV_ST_VIDEO]->index;//NSV_ST_VIDEO;
  520. pkt->dts = nst->frame_offset;
  521. pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */
  522. for (i = 0; i < FFMIN(8, vsize); i++)
  523. av_log(s, AV_LOG_TRACE, "NSV video: [%d] = %02x\n", i, pkt->data[i]);
  524. }
  525. if(st[NSV_ST_VIDEO])
  526. ((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset++;
  527. if (asize && st[NSV_ST_AUDIO]) {
  528. nst = st[NSV_ST_AUDIO]->priv_data;
  529. pkt = &nsv->ahead[NSV_ST_AUDIO];
  530. /* read raw audio specific header on the first audio chunk... */
  531. /* on ALL audio chunks ?? seems so! */
  532. if (asize && st[NSV_ST_AUDIO]->codecpar->codec_tag == MKTAG('P', 'C', 'M', ' ')/* && fill_header*/) {
  533. uint8_t bps;
  534. uint8_t channels;
  535. uint16_t samplerate;
  536. bps = avio_r8(pb);
  537. channels = avio_r8(pb);
  538. samplerate = avio_rl16(pb);
  539. if (!channels || !samplerate)
  540. return AVERROR_INVALIDDATA;
  541. asize-=4;
  542. av_log(s, AV_LOG_TRACE, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);
  543. if (fill_header) {
  544. st[NSV_ST_AUDIO]->need_parsing = AVSTREAM_PARSE_NONE; /* we know everything */
  545. if (bps != 16) {
  546. av_log(s, AV_LOG_TRACE, "NSV AUDIO bit/sample != 16 (%d)!!!\n", bps);
  547. }
  548. bps /= channels; // ???
  549. if (bps == 8)
  550. st[NSV_ST_AUDIO]->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  551. samplerate /= 4;/* UGH ??? XXX */
  552. channels = 1;
  553. st[NSV_ST_AUDIO]->codecpar->channels = channels;
  554. st[NSV_ST_AUDIO]->codecpar->sample_rate = samplerate;
  555. av_log(s, AV_LOG_TRACE, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);
  556. }
  557. }
  558. av_get_packet(pb, pkt, asize);
  559. pkt->stream_index = st[NSV_ST_AUDIO]->index;//NSV_ST_AUDIO;
  560. pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */
  561. if( nsv->state == NSV_HAS_READ_NSVS && st[NSV_ST_VIDEO] ) {
  562. /* on a nsvs frame we have new information on a/v sync */
  563. pkt->dts = (((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset-1);
  564. pkt->dts *= (int64_t)1000 * nsv->framerate.den;
  565. pkt->dts += (int64_t)nsv->avsync * nsv->framerate.num;
  566. av_log(s, AV_LOG_TRACE, "NSV AUDIO: sync:%d, dts:%"PRId64, nsv->avsync, pkt->dts);
  567. }
  568. nst->frame_offset++;
  569. }
  570. nsv->state = NSV_UNSYNC;
  571. return 0;
  572. }
  573. static int nsv_read_packet(AVFormatContext *s, AVPacket *pkt)
  574. {
  575. NSVContext *nsv = s->priv_data;
  576. int i, err = 0;
  577. /* in case we don't already have something to eat ... */
  578. if (!nsv->ahead[0].data && !nsv->ahead[1].data)
  579. err = nsv_read_chunk(s, 0);
  580. if (err < 0)
  581. return err;
  582. /* now pick one of the plates */
  583. for (i = 0; i < 2; i++) {
  584. if (nsv->ahead[i].data) {
  585. /* avoid the cost of new_packet + memcpy(->data) */
  586. memcpy(pkt, &nsv->ahead[i], sizeof(AVPacket));
  587. nsv->ahead[i].data = NULL; /* we ate that one */
  588. return pkt->size;
  589. }
  590. }
  591. /* this restaurant is not provisioned :^] */
  592. return -1;
  593. }
  594. static int nsv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  595. {
  596. NSVContext *nsv = s->priv_data;
  597. AVStream *st = s->streams[stream_index];
  598. NSVStream *nst = st->priv_data;
  599. int index;
  600. index = av_index_search_timestamp(st, timestamp, flags);
  601. if(index < 0)
  602. return -1;
  603. if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
  604. return -1;
  605. nst->frame_offset = st->index_entries[index].timestamp;
  606. nsv->state = NSV_UNSYNC;
  607. return 0;
  608. }
  609. static int nsv_read_close(AVFormatContext *s)
  610. {
  611. NSVContext *nsv = s->priv_data;
  612. av_freep(&nsv->nsvs_file_offset);
  613. av_freep(&nsv->nsvs_timestamps);
  614. if (nsv->ahead[0].data)
  615. av_packet_unref(&nsv->ahead[0]);
  616. if (nsv->ahead[1].data)
  617. av_packet_unref(&nsv->ahead[1]);
  618. return 0;
  619. }
  620. static int nsv_probe(AVProbeData *p)
  621. {
  622. int i;
  623. int score;
  624. int vsize, asize, auxcount;
  625. score = 0;
  626. av_log(NULL, AV_LOG_TRACE, "nsv_probe(), buf_size %d\n", p->buf_size);
  627. /* check file header */
  628. /* streamed files might not have any header */
  629. if (p->buf[0] == 'N' && p->buf[1] == 'S' &&
  630. p->buf[2] == 'V' && (p->buf[3] == 'f' || p->buf[3] == 's'))
  631. return AVPROBE_SCORE_MAX;
  632. /* XXX: do streamed files always start at chunk boundary ?? */
  633. /* or do we need to search NSVs in the byte stream ? */
  634. /* seems the servers don't bother starting clean chunks... */
  635. /* sometimes even the first header is at 9KB or something :^) */
  636. for (i = 1; i < p->buf_size - 3; i++) {
  637. if (p->buf[i+0] == 'N' && p->buf[i+1] == 'S' &&
  638. p->buf[i+2] == 'V' && p->buf[i+3] == 's') {
  639. score = AVPROBE_SCORE_MAX/5;
  640. /* Get the chunk size and check if at the end we are getting 0xBEEF */
  641. auxcount = p->buf[i+19];
  642. vsize = p->buf[i+20] | p->buf[i+21] << 8;
  643. asize = p->buf[i+22] | p->buf[i+23] << 8;
  644. vsize = (vsize << 4) | (auxcount >> 4);
  645. if ((asize + vsize + i + 23) < p->buf_size - 2) {
  646. if (p->buf[i+23+asize+vsize+1] == 0xEF &&
  647. p->buf[i+23+asize+vsize+2] == 0xBE)
  648. return AVPROBE_SCORE_MAX-20;
  649. }
  650. }
  651. }
  652. /* so we'll have more luck on extension... */
  653. if (av_match_ext(p->filename, "nsv"))
  654. return AVPROBE_SCORE_EXTENSION;
  655. /* FIXME: add mime-type check */
  656. return score;
  657. }
  658. AVInputFormat ff_nsv_demuxer = {
  659. .name = "nsv",
  660. .long_name = NULL_IF_CONFIG_SMALL("Nullsoft Streaming Video"),
  661. .priv_data_size = sizeof(NSVContext),
  662. .read_probe = nsv_probe,
  663. .read_header = nsv_read_header,
  664. .read_packet = nsv_read_packet,
  665. .read_close = nsv_read_close,
  666. .read_seek = nsv_read_seek,
  667. };