nsvdec.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * NSV demuxer
  3. * Copyright (c) 2004 The FFmpeg Project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/attributes.h"
  22. #include "libavutil/mathematics.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #include "riff.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/intreadwrite.h"
  28. //#define DEBUG_DUMP_INDEX // XXX dumbdriving-271.nsv breaks with it commented!!
  29. #define CHECK_SUBSEQUENT_NSVS
  30. //#define DISABLE_AUDIO
  31. /* max bytes to crawl for trying to resync
  32. * stupid streaming servers don't start at chunk boundaries...
  33. */
  34. #define NSV_MAX_RESYNC (500*1024)
  35. #define NSV_MAX_RESYNC_TRIES 300
  36. /*
  37. * First version by Francois Revol - revol@free.fr
  38. * References:
  39. * (1) http://www.multimedia.cx/nsv-format.txt
  40. * seems someone came to the same conclusions as me, and updated it:
  41. * (2) http://www.stud.ktu.lt/~vitslav/nsv/nsv-format.txt
  42. * http://www.stud.ktu.lt/~vitslav/nsv/
  43. * official docs
  44. * (3) http://ultravox.aol.com/NSVFormat.rtf
  45. * Sample files:
  46. * (S1) http://www.nullsoft.com/nsv/samples/
  47. * http://www.nullsoft.com/nsv/samples/faster.nsv
  48. * http://streamripper.sourceforge.net/openbb/read.php?TID=492&page=4
  49. */
  50. /*
  51. * notes on the header (Francois Revol):
  52. *
  53. * It is followed by strings, then a table, but nothing tells
  54. * where the table begins according to (1). After checking faster.nsv,
  55. * I believe NVSf[16-19] gives the size of the strings data
  56. * (that is the offset of the data table after the header).
  57. * After checking all samples from (S1) all confirms this.
  58. *
  59. * Then, about NSVf[12-15], faster.nsf has 179700. When veiwing it in VLC,
  60. * I noticed there was about 1 NVSs chunk/s, so I ran
  61. * strings faster.nsv | grep NSVs | wc -l
  62. * which gave me 180. That leads me to think that NSVf[12-15] might be the
  63. * file length in milliseconds.
  64. * Let's try that:
  65. * 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
  66. * except for nstrailer (which doesn't have an NSVf header), it repports correct time.
  67. *
  68. * nsvtrailer.nsv (S1) does not have any NSVf header, only NSVs chunks,
  69. * so the header seems to not be mandatory. (for streaming).
  70. *
  71. * index slice duration check (excepts nsvtrailer.nsv):
  72. * 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
  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; /* av_assert(vwidth%16==0) */
  96. uint16_t vheight; /* av_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 {
  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. { AV_CODEC_ID_VP8, MKTAG('V', 'P', '8', '0') },
  177. /*
  178. { AV_CODEC_ID_VP4, MKTAG('V', 'P', '4', ' ') },
  179. { AV_CODEC_ID_VP4, MKTAG('V', 'P', '4', '0') },
  180. */
  181. { AV_CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D') }, /* cf sample xvid decoder from nsv_codec_sdk.zip */
  182. { AV_CODEC_ID_RAWVIDEO, MKTAG('R', 'G', 'B', '3') },
  183. { AV_CODEC_ID_NONE, 0 },
  184. };
  185. static const AVCodecTag nsv_codec_audio_tags[] = {
  186. { AV_CODEC_ID_MP3, MKTAG('M', 'P', '3', ' ') },
  187. { AV_CODEC_ID_AAC, MKTAG('A', 'A', 'C', ' ') },
  188. { AV_CODEC_ID_AAC, MKTAG('A', 'A', 'C', 'P') },
  189. { AV_CODEC_ID_AAC, MKTAG('V', 'L', 'B', ' ') },
  190. { AV_CODEC_ID_SPEEX, MKTAG('S', 'P', 'X', ' ') },
  191. { AV_CODEC_ID_PCM_U16LE, MKTAG('P', 'C', 'M', ' ') },
  192. { AV_CODEC_ID_NONE, 0 },
  193. };
  194. //static int nsv_load_index(AVFormatContext *s);
  195. static int nsv_read_chunk(AVFormatContext *s, int fill_header);
  196. #define print_tag(str, tag, size) \
  197. av_dlog(NULL, "%s: tag=%c%c%c%c\n", \
  198. str, tag & 0xff, \
  199. (tag >> 8) & 0xff, \
  200. (tag >> 16) & 0xff, \
  201. (tag >> 24) & 0xff);
  202. /* try to find something we recognize, and set the state accordingly */
  203. static int nsv_resync(AVFormatContext *s)
  204. {
  205. NSVContext *nsv = s->priv_data;
  206. AVIOContext *pb = s->pb;
  207. uint32_t v = 0;
  208. int i;
  209. av_dlog(s, "%s(), offset = %"PRId64", state = %d\n", __FUNCTION__, avio_tell(pb), nsv->state);
  210. //nsv->state = NSV_UNSYNC;
  211. for (i = 0; i < NSV_MAX_RESYNC; i++) {
  212. if (url_feof(pb)) {
  213. av_dlog(s, "NSV EOF\n");
  214. nsv->state = NSV_UNSYNC;
  215. return -1;
  216. }
  217. v <<= 8;
  218. v |= avio_r8(pb);
  219. if (i < 8) {
  220. av_dlog(s, "NSV resync: [%d] = %02x\n", i, v & 0x0FF);
  221. }
  222. if ((v & 0x0000ffff) == 0xefbe) { /* BEEF */
  223. av_dlog(s, "NSV resynced on BEEF after %d bytes\n", i+1);
  224. nsv->state = NSV_FOUND_BEEF;
  225. return 0;
  226. }
  227. /* we read as big endian, thus the MK*BE* */
  228. if (v == TB_NSVF) { /* NSVf */
  229. av_dlog(s, "NSV resynced on NSVf after %d bytes\n", i+1);
  230. nsv->state = NSV_FOUND_NSVF;
  231. return 0;
  232. }
  233. if (v == MKBETAG('N', 'S', 'V', 's')) { /* NSVs */
  234. av_dlog(s, "NSV resynced on NSVs after %d bytes\n", i+1);
  235. nsv->state = NSV_FOUND_NSVS;
  236. return 0;
  237. }
  238. }
  239. av_dlog(s, "NSV sync lost\n");
  240. return -1;
  241. }
  242. static int nsv_parse_NSVf_header(AVFormatContext *s)
  243. {
  244. NSVContext *nsv = s->priv_data;
  245. AVIOContext *pb = s->pb;
  246. unsigned int av_unused file_size;
  247. unsigned int size;
  248. int64_t duration;
  249. int strings_size;
  250. int table_entries;
  251. int table_entries_used;
  252. av_dlog(s, "%s()\n", __FUNCTION__);
  253. nsv->state = NSV_UNSYNC; /* in case we fail */
  254. size = avio_rl32(pb);
  255. if (size < 28)
  256. return -1;
  257. nsv->NSVf_end = size;
  258. //s->file_size = (uint32_t)avio_rl32(pb);
  259. file_size = (uint32_t)avio_rl32(pb);
  260. av_dlog(s, "NSV NSVf chunk_size %u\n", size);
  261. av_dlog(s, "NSV NSVf file_size %u\n", file_size);
  262. nsv->duration = duration = avio_rl32(pb); /* in ms */
  263. av_dlog(s, "NSV NSVf duration %"PRId64" ms\n", duration);
  264. // XXX: store it in AVStreams
  265. strings_size = avio_rl32(pb);
  266. table_entries = avio_rl32(pb);
  267. table_entries_used = avio_rl32(pb);
  268. av_dlog(s, "NSV NSVf info-strings size: %d, table entries: %d, bis %d\n",
  269. strings_size, table_entries, table_entries_used);
  270. if (url_feof(pb))
  271. return -1;
  272. av_dlog(s, "NSV got header; filepos %"PRId64"\n", avio_tell(pb));
  273. if (strings_size > 0) {
  274. char *strings; /* last byte will be '\0' to play safe with str*() */
  275. char *p, *endp;
  276. char *token, *value;
  277. char quote;
  278. p = strings = av_mallocz((size_t)strings_size + 1);
  279. if (!p)
  280. return AVERROR(ENOMEM);
  281. endp = strings + strings_size;
  282. avio_read(pb, strings, strings_size);
  283. while (p < endp) {
  284. while (*p == ' ')
  285. p++; /* strip out spaces */
  286. if (p >= endp-2)
  287. break;
  288. token = p;
  289. p = strchr(p, '=');
  290. if (!p || p >= endp-2)
  291. break;
  292. *p++ = '\0';
  293. quote = *p++;
  294. value = p;
  295. p = strchr(p, quote);
  296. if (!p || p >= endp)
  297. break;
  298. *p++ = '\0';
  299. av_dlog(s, "NSV NSVf INFO: %s='%s'\n", token, value);
  300. av_dict_set(&s->metadata, token, value, 0);
  301. }
  302. av_free(strings);
  303. }
  304. if (url_feof(pb))
  305. return -1;
  306. av_dlog(s, "NSV got infos; filepos %"PRId64"\n", avio_tell(pb));
  307. if (table_entries_used > 0) {
  308. int i;
  309. nsv->index_entries = table_entries_used;
  310. if((unsigned)table_entries_used >= UINT_MAX / sizeof(uint32_t))
  311. return -1;
  312. nsv->nsvs_file_offset = av_malloc((unsigned)table_entries_used * sizeof(uint32_t));
  313. if (!nsv->nsvs_file_offset)
  314. return AVERROR(ENOMEM);
  315. for(i=0;i<table_entries_used;i++)
  316. nsv->nsvs_file_offset[i] = avio_rl32(pb) + size;
  317. if(table_entries > table_entries_used &&
  318. avio_rl32(pb) == MKTAG('T','O','C','2')) {
  319. nsv->nsvs_timestamps = av_malloc((unsigned)table_entries_used*sizeof(uint32_t));
  320. if (!nsv->nsvs_timestamps)
  321. return AVERROR(ENOMEM);
  322. for(i=0;i<table_entries_used;i++) {
  323. nsv->nsvs_timestamps[i] = avio_rl32(pb);
  324. }
  325. }
  326. }
  327. av_dlog(s, "NSV got index; filepos %"PRId64"\n", avio_tell(pb));
  328. #ifdef DEBUG_DUMP_INDEX
  329. #define V(v) ((v<0x20 || v > 127)?'.':v)
  330. /* dump index */
  331. av_dlog(s, "NSV %d INDEX ENTRIES:\n", table_entries);
  332. av_dlog(s, "NSV [dataoffset][fileoffset]\n", table_entries);
  333. for (i = 0; i < table_entries; i++) {
  334. unsigned char b[8];
  335. avio_seek(pb, size + nsv->nsvs_file_offset[i], SEEK_SET);
  336. avio_read(pb, b, 8);
  337. av_dlog(s, "NSV [0x%08lx][0x%08lx]: %02x %02x %02x %02x %02x %02x %02x %02x"
  338. "%c%c%c%c%c%c%c%c\n",
  339. nsv->nsvs_file_offset[i], size + nsv->nsvs_file_offset[i],
  340. b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
  341. V(b[0]), V(b[1]), V(b[2]), V(b[3]), V(b[4]), V(b[5]), V(b[6]), V(b[7]) );
  342. }
  343. //avio_seek(pb, size, SEEK_SET); /* go back to end of header */
  344. #undef V
  345. #endif
  346. avio_seek(pb, nsv->base_offset + size, SEEK_SET); /* required for dumbdriving-271.nsv (2 extra bytes) */
  347. if (url_feof(pb))
  348. return -1;
  349. nsv->state = NSV_HAS_READ_NSVF;
  350. return 0;
  351. }
  352. static int nsv_parse_NSVs_header(AVFormatContext *s)
  353. {
  354. NSVContext *nsv = s->priv_data;
  355. AVIOContext *pb = s->pb;
  356. uint32_t vtag, atag;
  357. uint16_t vwidth, vheight;
  358. AVRational framerate;
  359. int i;
  360. AVStream *st;
  361. NSVStream *nst;
  362. av_dlog(s, "%s()\n", __FUNCTION__);
  363. vtag = avio_rl32(pb);
  364. atag = avio_rl32(pb);
  365. vwidth = avio_rl16(pb);
  366. vheight = avio_rl16(pb);
  367. i = avio_r8(pb);
  368. av_dlog(s, "NSV NSVs framerate code %2x\n", i);
  369. if(i&0x80) { /* odd way of giving native framerates from docs */
  370. int t=(i & 0x7F)>>2;
  371. if(t<16) framerate = (AVRational){1, t+1};
  372. else framerate = (AVRational){t-15, 1};
  373. if(i&1){
  374. framerate.num *= 1000;
  375. framerate.den *= 1001;
  376. }
  377. if((i&3)==3) framerate.num *= 24;
  378. else if((i&3)==2) framerate.num *= 25;
  379. else framerate.num *= 30;
  380. }
  381. else
  382. framerate= (AVRational){i, 1};
  383. nsv->avsync = avio_rl16(pb);
  384. nsv->framerate = framerate;
  385. print_tag("NSV NSVs vtag", vtag, 0);
  386. print_tag("NSV NSVs atag", atag, 0);
  387. av_dlog(s, "NSV NSVs vsize %dx%d\n", vwidth, vheight);
  388. /* XXX change to ap != NULL ? */
  389. if (s->nb_streams == 0) { /* streams not yet published, let's do that */
  390. nsv->vtag = vtag;
  391. nsv->atag = atag;
  392. nsv->vwidth = vwidth;
  393. nsv->vheight = vwidth;
  394. if (vtag != T_NONE) {
  395. int i;
  396. st = avformat_new_stream(s, NULL);
  397. if (!st)
  398. goto fail;
  399. st->id = NSV_ST_VIDEO;
  400. nst = av_mallocz(sizeof(NSVStream));
  401. if (!nst)
  402. goto fail;
  403. st->priv_data = nst;
  404. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  405. st->codec->codec_tag = vtag;
  406. st->codec->codec_id = ff_codec_get_id(nsv_codec_video_tags, vtag);
  407. st->codec->width = vwidth;
  408. st->codec->height = vheight;
  409. st->codec->bits_per_coded_sample = 24; /* depth XXX */
  410. avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
  411. st->start_time = 0;
  412. st->duration = av_rescale(nsv->duration, framerate.num, 1000*framerate.den);
  413. for(i=0;i<nsv->index_entries;i++) {
  414. if(nsv->nsvs_timestamps) {
  415. av_add_index_entry(st, nsv->nsvs_file_offset[i], nsv->nsvs_timestamps[i],
  416. 0, 0, AVINDEX_KEYFRAME);
  417. } else {
  418. int64_t ts = av_rescale(i*nsv->duration/nsv->index_entries, framerate.num, 1000*framerate.den);
  419. av_add_index_entry(st, nsv->nsvs_file_offset[i], ts, 0, 0, AVINDEX_KEYFRAME);
  420. }
  421. }
  422. }
  423. if (atag != T_NONE) {
  424. #ifndef DISABLE_AUDIO
  425. st = avformat_new_stream(s, NULL);
  426. if (!st)
  427. goto fail;
  428. st->id = NSV_ST_AUDIO;
  429. nst = av_mallocz(sizeof(NSVStream));
  430. if (!nst)
  431. goto fail;
  432. st->priv_data = nst;
  433. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  434. st->codec->codec_tag = atag;
  435. st->codec->codec_id = ff_codec_get_id(nsv_codec_audio_tags, atag);
  436. st->need_parsing = AVSTREAM_PARSE_FULL; /* for PCM we will read a chunk later and put correct info */
  437. /* set timebase to common denominator of ms and framerate */
  438. avpriv_set_pts_info(st, 64, 1, framerate.num*1000);
  439. st->start_time = 0;
  440. st->duration = (int64_t)nsv->duration * framerate.num;
  441. #endif
  442. }
  443. #ifdef CHECK_SUBSEQUENT_NSVS
  444. } else {
  445. if (nsv->vtag != vtag || nsv->atag != atag || nsv->vwidth != vwidth || nsv->vheight != vwidth) {
  446. av_dlog(s, "NSV NSVs header values differ from the first one!!!\n");
  447. //return -1;
  448. }
  449. #endif /* CHECK_SUBSEQUENT_NSVS */
  450. }
  451. nsv->state = NSV_HAS_READ_NSVS;
  452. return 0;
  453. fail:
  454. /* XXX */
  455. nsv->state = NSV_UNSYNC;
  456. return -1;
  457. }
  458. static int nsv_read_header(AVFormatContext *s)
  459. {
  460. NSVContext *nsv = s->priv_data;
  461. int i, err;
  462. av_dlog(s, "%s()\n", __FUNCTION__);
  463. av_dlog(s, "filename '%s'\n", s->filename);
  464. nsv->state = NSV_UNSYNC;
  465. nsv->ahead[0].data = nsv->ahead[1].data = NULL;
  466. for (i = 0; i < NSV_MAX_RESYNC_TRIES; i++) {
  467. if (nsv_resync(s) < 0)
  468. return -1;
  469. if (nsv->state == NSV_FOUND_NSVF) {
  470. err = nsv_parse_NSVf_header(s);
  471. if (err < 0)
  472. return err;
  473. }
  474. /* we need the first NSVs also... */
  475. if (nsv->state == NSV_FOUND_NSVS) {
  476. err = nsv_parse_NSVs_header(s);
  477. if (err < 0)
  478. return err;
  479. break; /* we just want the first one */
  480. }
  481. }
  482. if (s->nb_streams < 1) /* no luck so far */
  483. return -1;
  484. /* now read the first chunk, so we can attempt to decode more info */
  485. err = nsv_read_chunk(s, 1);
  486. av_dlog(s, "parsed header\n");
  487. return err;
  488. }
  489. static int nsv_read_chunk(AVFormatContext *s, int fill_header)
  490. {
  491. NSVContext *nsv = s->priv_data;
  492. AVIOContext *pb = s->pb;
  493. AVStream *st[2] = {NULL, NULL};
  494. NSVStream *nst;
  495. AVPacket *pkt;
  496. int i, err = 0;
  497. uint8_t auxcount; /* number of aux metadata, also 4 bits of vsize */
  498. uint32_t vsize;
  499. uint16_t asize;
  500. uint16_t auxsize;
  501. av_dlog(s, "%s(%d)\n", __FUNCTION__, fill_header);
  502. if (nsv->ahead[0].data || nsv->ahead[1].data)
  503. return 0; //-1; /* hey! eat what you've in your plate first! */
  504. null_chunk_retry:
  505. if (url_feof(pb))
  506. return -1;
  507. for (i = 0; i < NSV_MAX_RESYNC_TRIES && nsv->state < NSV_FOUND_NSVS && !err; i++)
  508. err = nsv_resync(s);
  509. if (err < 0)
  510. return err;
  511. if (nsv->state == NSV_FOUND_NSVS)
  512. err = nsv_parse_NSVs_header(s);
  513. if (err < 0)
  514. return err;
  515. if (nsv->state != NSV_HAS_READ_NSVS && nsv->state != NSV_FOUND_BEEF)
  516. return -1;
  517. auxcount = avio_r8(pb);
  518. vsize = avio_rl16(pb);
  519. asize = avio_rl16(pb);
  520. vsize = (vsize << 4) | (auxcount >> 4);
  521. auxcount &= 0x0f;
  522. av_dlog(s, "NSV CHUNK %d aux, %u bytes video, %d bytes audio\n", auxcount, vsize, asize);
  523. /* skip aux stuff */
  524. for (i = 0; i < auxcount; i++) {
  525. uint32_t av_unused auxtag;
  526. auxsize = avio_rl16(pb);
  527. auxtag = avio_rl32(pb);
  528. av_dlog(s, "NSV aux data: '%c%c%c%c', %d bytes\n",
  529. (auxtag & 0x0ff),
  530. ((auxtag >> 8) & 0x0ff),
  531. ((auxtag >> 16) & 0x0ff),
  532. ((auxtag >> 24) & 0x0ff),
  533. auxsize);
  534. avio_skip(pb, auxsize);
  535. vsize -= auxsize + sizeof(uint16_t) + sizeof(uint32_t); /* that's becoming braindead */
  536. }
  537. if (url_feof(pb))
  538. return -1;
  539. if (!vsize && !asize) {
  540. nsv->state = NSV_UNSYNC;
  541. goto null_chunk_retry;
  542. }
  543. /* map back streams to v,a */
  544. if (s->nb_streams > 0)
  545. st[s->streams[0]->id] = s->streams[0];
  546. if (s->nb_streams > 1)
  547. st[s->streams[1]->id] = s->streams[1];
  548. if (vsize && st[NSV_ST_VIDEO]) {
  549. nst = st[NSV_ST_VIDEO]->priv_data;
  550. pkt = &nsv->ahead[NSV_ST_VIDEO];
  551. av_get_packet(pb, pkt, vsize);
  552. pkt->stream_index = st[NSV_ST_VIDEO]->index;//NSV_ST_VIDEO;
  553. pkt->dts = nst->frame_offset;
  554. pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */
  555. for (i = 0; i < FFMIN(8, vsize); i++)
  556. av_dlog(s, "NSV video: [%d] = %02x\n", i, pkt->data[i]);
  557. }
  558. if(st[NSV_ST_VIDEO])
  559. ((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset++;
  560. if (asize && st[NSV_ST_AUDIO]) {
  561. nst = st[NSV_ST_AUDIO]->priv_data;
  562. pkt = &nsv->ahead[NSV_ST_AUDIO];
  563. /* read raw audio specific header on the first audio chunk... */
  564. /* on ALL audio chunks ?? seems so! */
  565. if (asize && st[NSV_ST_AUDIO]->codec->codec_tag == MKTAG('P', 'C', 'M', ' ')/* && fill_header*/) {
  566. uint8_t bps;
  567. uint8_t channels;
  568. uint16_t samplerate;
  569. bps = avio_r8(pb);
  570. channels = avio_r8(pb);
  571. samplerate = avio_rl16(pb);
  572. asize-=4;
  573. av_dlog(s, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);
  574. if (fill_header) {
  575. st[NSV_ST_AUDIO]->need_parsing = AVSTREAM_PARSE_NONE; /* we know everything */
  576. if (bps != 16) {
  577. av_dlog(s, "NSV AUDIO bit/sample != 16 (%d)!!!\n", bps);
  578. }
  579. bps /= channels; // ???
  580. if (bps == 8)
  581. st[NSV_ST_AUDIO]->codec->codec_id = AV_CODEC_ID_PCM_U8;
  582. samplerate /= 4;/* UGH ??? XXX */
  583. channels = 1;
  584. st[NSV_ST_AUDIO]->codec->channels = channels;
  585. st[NSV_ST_AUDIO]->codec->sample_rate = samplerate;
  586. av_dlog(s, "NSV RAWAUDIO: bps %d, nchan %d, srate %d\n", bps, channels, samplerate);
  587. }
  588. }
  589. av_get_packet(pb, pkt, asize);
  590. pkt->stream_index = st[NSV_ST_AUDIO]->index;//NSV_ST_AUDIO;
  591. pkt->flags |= nsv->state == NSV_HAS_READ_NSVS ? AV_PKT_FLAG_KEY : 0; /* keyframe only likely on a sync frame */
  592. if( nsv->state == NSV_HAS_READ_NSVS && st[NSV_ST_VIDEO] ) {
  593. /* on a nsvs frame we have new information on a/v sync */
  594. pkt->dts = (((NSVStream*)st[NSV_ST_VIDEO]->priv_data)->frame_offset-1);
  595. pkt->dts *= (int64_t)1000 * nsv->framerate.den;
  596. pkt->dts += (int64_t)nsv->avsync * nsv->framerate.num;
  597. av_dlog(s, "NSV AUDIO: sync:%d, dts:%"PRId64, nsv->avsync, pkt->dts);
  598. }
  599. nst->frame_offset++;
  600. }
  601. nsv->state = NSV_UNSYNC;
  602. return 0;
  603. }
  604. static int nsv_read_packet(AVFormatContext *s, AVPacket *pkt)
  605. {
  606. NSVContext *nsv = s->priv_data;
  607. int i, err = 0;
  608. av_dlog(s, "%s()\n", __FUNCTION__);
  609. /* in case we don't already have something to eat ... */
  610. if (nsv->ahead[0].data == NULL && nsv->ahead[1].data == NULL)
  611. err = nsv_read_chunk(s, 0);
  612. if (err < 0)
  613. return err;
  614. /* now pick one of the plates */
  615. for (i = 0; i < 2; i++) {
  616. if (nsv->ahead[i].data) {
  617. av_dlog(s, "%s: using cached packet[%d]\n", __FUNCTION__, i);
  618. /* avoid the cost of new_packet + memcpy(->data) */
  619. memcpy(pkt, &nsv->ahead[i], sizeof(AVPacket));
  620. nsv->ahead[i].data = NULL; /* we ate that one */
  621. return pkt->size;
  622. }
  623. }
  624. /* this restaurant is not approvisionned :^] */
  625. return -1;
  626. }
  627. static int nsv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  628. {
  629. NSVContext *nsv = s->priv_data;
  630. AVStream *st = s->streams[stream_index];
  631. NSVStream *nst = st->priv_data;
  632. int index;
  633. index = av_index_search_timestamp(st, timestamp, flags);
  634. if(index < 0)
  635. return -1;
  636. if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
  637. return -1;
  638. nst->frame_offset = st->index_entries[index].timestamp;
  639. nsv->state = NSV_UNSYNC;
  640. return 0;
  641. }
  642. static int nsv_read_close(AVFormatContext *s)
  643. {
  644. /* int i; */
  645. NSVContext *nsv = s->priv_data;
  646. av_freep(&nsv->nsvs_file_offset);
  647. av_freep(&nsv->nsvs_timestamps);
  648. if (nsv->ahead[0].data)
  649. av_free_packet(&nsv->ahead[0]);
  650. if (nsv->ahead[1].data)
  651. av_free_packet(&nsv->ahead[1]);
  652. #if 0
  653. for(i=0;i<s->nb_streams;i++) {
  654. AVStream *st = s->streams[i];
  655. NSVStream *ast = st->priv_data;
  656. if(ast){
  657. av_free(ast->index_entries);
  658. av_free(ast);
  659. }
  660. av_free(st->codec->palctrl);
  661. }
  662. #endif
  663. return 0;
  664. }
  665. static int nsv_probe(AVProbeData *p)
  666. {
  667. int i, score = 0;
  668. av_dlog(NULL, "nsv_probe(), buf_size %d\n", p->buf_size);
  669. /* check file header */
  670. /* streamed files might not have any header */
  671. if (p->buf[0] == 'N' && p->buf[1] == 'S' &&
  672. p->buf[2] == 'V' && (p->buf[3] == 'f' || p->buf[3] == 's'))
  673. return AVPROBE_SCORE_MAX;
  674. /* XXX: do streamed files always start at chunk boundary ?? */
  675. /* or do we need to search NSVs in the byte stream ? */
  676. /* seems the servers don't bother starting clean chunks... */
  677. /* sometimes even the first header is at 9KB or something :^) */
  678. for (i = 1; i < p->buf_size - 3; i++) {
  679. if (AV_RL32(p->buf + i) == AV_RL32("NSVs")) {
  680. /* Get the chunk size and check if at the end we are getting 0xBEEF */
  681. int vsize = AV_RL24(p->buf+i+19) >> 4;
  682. int asize = AV_RL16(p->buf+i+22);
  683. int offset = i + 23 + asize + vsize + 1;
  684. if (offset <= p->buf_size - 2 && AV_RL16(p->buf + offset) == 0xBEEF)
  685. return 4*AVPROBE_SCORE_MAX/5;
  686. score = AVPROBE_SCORE_MAX/5;
  687. }
  688. }
  689. /* so we'll have more luck on extension... */
  690. if (av_match_ext(p->filename, "nsv"))
  691. return AVPROBE_SCORE_MAX/2;
  692. /* FIXME: add mime-type check */
  693. return score;
  694. }
  695. AVInputFormat ff_nsv_demuxer = {
  696. .name = "nsv",
  697. .long_name = NULL_IF_CONFIG_SMALL("Nullsoft Streaming Video"),
  698. .priv_data_size = sizeof(NSVContext),
  699. .read_probe = nsv_probe,
  700. .read_header = nsv_read_header,
  701. .read_packet = nsv_read_packet,
  702. .read_close = nsv_read_close,
  703. .read_seek = nsv_read_seek,
  704. };