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.

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