nsvlib.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. ** nsvlib.h - NSV file/bitstream reading/writing interface
  3. **
  4. ** Copyright (C) 2001-2002 Nullsoft, Inc.
  5. **
  6. ** Confidential Subject to NDA
  7. */
  8. #ifndef _NSVLIB_H_
  9. #define _NSVLIB_H_
  10. /*********************************************************************
  11. ** bitstream classes
  12. */
  13. #include "nsvbs.h"
  14. /*********************************************************************
  15. ** NSV packeting limits
  16. */
  17. #define NSV_MAX_AUDIO_LEN 0x8000 // 32kb
  18. #define NSV_MAX_VIDEO_LEN 0x80000 // 512kb
  19. #define NSV_MAX_AUX_LEN 0x8000 // 32kb for each aux stream
  20. #define NSV_MAX_AUXSTREAMS 15 // 15 aux streams maximum
  21. /*********************************************************************
  22. ** Constants for setting certain metadata items using addHdrMetaData()
  23. */
  24. #define METADATANAME_AUTHOR "Author"
  25. #define METADATANAME_TITLE "Title"
  26. #define METADATANAME_COPYRIGHT "Copyright"
  27. #define METADATANAME_COMMENT "Comment"
  28. #define METADATANAME_PROFILE "Profile"
  29. #define METADATANAME_FILEID "File ID"
  30. /*********************************************************************
  31. ** NSV type utility functions/macros
  32. */
  33. /*
  34. ** Use NSV_MAKETYPE() to quickly make NSV audio/video/aux types.
  35. ** ex: NSV_MAKETYPE('R','G','B','A')
  36. */
  37. #define NSV_MAKETYPE(A,B,C,D) ((A) | ((B)<<8) | ((C)<<16) | ((D)<<24))
  38. /*
  39. ** These functions convert types to and from strings.
  40. */
  41. /* nsv_type_to_string() converts an NSV type to a string.
  42. * out must be at least 5 bytes long. If 't' is not a valid type,
  43. * then out will be set to an empty string
  44. * ex:
  45. * char out[5];
  46. * nsv_type_to_string(NSV_MAKETYPE('R','G','B','A'),out);
  47. * strcmp(out,"RGBA") == 0
  48. */
  49. void nsv_type_to_string(unsigned int t, char *out);
  50. /* nsv_string_to_type() converts a string to an NSV type.
  51. * Returns 0 if the type is not valid.
  52. * ex: nsv_string_to_type("RGBA") == NSV_MAKETYPE('R','G','B','A')
  53. */
  54. unsigned int nsv_string_to_type(char *in);
  55. /*********************************************************************
  56. ** NSV bitstream packeting/unpacketing classes
  57. */
  58. /* nsv_Packeter is used to packet audio/video/auxiliary data into
  59. * a bitstream.
  60. *
  61. * ex:
  62. * nsv_Packeter p;
  63. * nsv_OutBS bs;
  64. * p.setVidFmt(NSV_MAKETYPE('R','G','B','A'),320,240,30.0);
  65. * p.setAudFmt(NSV_MAKETYPE('P','C','M',' '));
  66. * for (;;) {
  67. * doEncodeAudioAndVideo();
  68. * p.setSyncFrame(is_keyframe);
  69. * p.setSyncOffset(av_sync_offset);
  70. * p.setAudio(audio_data,audio_len);
  71. * p.setVideo(video_data,video_len);
  72. * p.clearAuxChannels(); // you can add aux channels if you want
  73. * if (p.packet(bs)) error();
  74. * int outbuflen;
  75. * void *outbuf=bs.get(&outbuflen);
  76. * fwrite(outbuf,outbuflen,1,fp); // write output
  77. * bs.clear(); // clear bitstream
  78. * }
  79. *
  80. */
  81. class nsv_Packeter {
  82. public:
  83. nsv_Packeter();
  84. ~nsv_Packeter();
  85. // init (per file) calls
  86. void setVidFmt(unsigned int vfmt, unsigned int w, unsigned int h, double frt);
  87. void setAudFmt(unsigned int afmt) { audfmt=afmt; }
  88. // per frame calls
  89. void setSyncFrame(int is_syncframe) { is_sync_frame=is_syncframe; }
  90. void setSyncOffset(int syncoffs) { syncoffset_cur=syncoffs; }
  91. void setAudio(void *a, int a_len) { audio=a; audio_len=a_len; }
  92. void setVideo(void *v, int v_len) { video=v; video_len=v_len; }
  93. int addAuxChannel(unsigned int fmt, void *data, int data_len) // 0 on success
  94. {
  95. if (aux_used >= NSV_MAX_AUXSTREAMS) return -1;
  96. aux[aux_used]=data;
  97. aux_len[aux_used]=data_len;
  98. aux_types[aux_used]=fmt;
  99. aux_used++;
  100. return 0;
  101. }
  102. void clearAuxChannels() { aux_used=0; }
  103. int packet(nsv_OutBS &bs); // returns 0 on success
  104. // some utility getting functions
  105. unsigned int getAudFmt() { return audfmt; }
  106. unsigned int getVidFmt() { return vidfmt; }
  107. unsigned int getWidth() { return width; }
  108. unsigned int getHeight() { return height; }
  109. double getFrameRate() { return framerate; }
  110. private:
  111. unsigned char framerate_idx;
  112. unsigned int vidfmt;
  113. unsigned int audfmt;
  114. unsigned int width;
  115. unsigned int height;
  116. double framerate;
  117. int syncoffset_cur;
  118. int aux_used;
  119. void *aux[NSV_MAX_AUXSTREAMS];
  120. int aux_len[NSV_MAX_AUXSTREAMS];
  121. unsigned int aux_types[NSV_MAX_AUXSTREAMS];
  122. int is_sync_frame;
  123. void *audio;
  124. int audio_len;
  125. void *video;
  126. int video_len;
  127. };
  128. /* nsv_Unpacketer is used to unpacket a bitstream into audio/video/auxiliary data
  129. * to decode, use an nsv_InBS object with data, and call unpacket().
  130. * ex:
  131. * nsv_Unpacketer up;
  132. * nsv_InBS in;
  133. * nsv_InBS videoout, audioout;
  134. * up.setVideoOut(&videoout);
  135. * up.setAudioOut(&audioout);
  136. * for (;;) {
  137. * int ret=up.unpacket(in);
  138. * if (ret < 0) break; // eof
  139. * if (ret > 0) add_data_to_bitstream(&in,ret);
  140. * if (!ret) { // got frame
  141. * int vl=videoout.getbits(32);
  142. * int al=videoout.getbits(32);
  143. * char *vd=(char*)videoout.getcurbyteptr();
  144. * char *ad=(char*)audioout.getcurbyteptr();
  145. * doDecode(vd,vl,ad,al);
  146. * videoout.seek(vl*8);
  147. * audioout.seek(al*8);
  148. * videoout.compact(); // free memory up
  149. * audioout.compact(); // free memory up
  150. * in.compact(); // free memory up
  151. * }
  152. * }
  153. */
  154. class nsv_Unpacketer {
  155. public:
  156. nsv_Unpacketer() { reset(); }
  157. ~nsv_Unpacketer() { }
  158. void reset(int full=1); // if full, full reset is done.
  159. // if not, then it is a partial reset (ie for seeking)
  160. // when EOF is set, the unpacketer will fail instead of requesting more data at the
  161. // end; it will also not require that the next frame be available for sync
  162. // (normally it looks ahead to verify data)
  163. void setEof(int eof=1) { m_eof=eof; }
  164. int getEof() { return m_eof; }
  165. // use these to set where the unpacketer writes the output of each stream
  166. // set to NULL to ignore output of that stream
  167. void setAudioOut(nsv_InBS *output=NULL) { m_audiobs=output; }
  168. // the format of the audio data written to the output is:
  169. // 32 bits: length of frame
  170. // ? bytes: audio data
  171. // (to read):
  172. // int l=output->getbits(32);
  173. // decode_audio(output->getcurbyteptr(),l);
  174. // output->seek(l*8);
  175. void setVideoOut(nsv_InBS *output=NULL) { m_videobs=output; }
  176. // the format of the video data written to the output is:
  177. // 32 bits: length of frame
  178. // ? bytes: video data
  179. // (to read):
  180. // int l=output->getbits(32);
  181. // decode_video(output->getcurbyteptr(),l);
  182. // output->seek(l*8);
  183. void setAuxOut(nsv_InBS *output=NULL) { m_auxbs=output; }
  184. // the format of the aux data written to the output is:
  185. // 32 bits: length of frame
  186. // 32 bits: type of aux data
  187. // ? bytes: aux data
  188. // (to read):
  189. // int l=output->getbits(32);
  190. // int type=output->getbits(32);
  191. // decode_aux(output->getcurbyteptr(),l);
  192. // output->seek(l*8);
  193. // aux is different than audio/video in that it includes a 32 bit
  194. // type value that is not included in the length.
  195. // returns 0 on success, >0 on needs (at least X bytes) more data,
  196. // -1 on error (eof and no header found)
  197. int unpacket(nsv_InBS &bs);
  198. // do we have enough sync to determine formats/widths/heights/framerates
  199. int isValid() { return valid; }
  200. // are we fully synched?
  201. int isSynched() { return synched; }
  202. // get sync offset from when we first synched up
  203. signed int getSyncOffset() { return (signed int) syncoffset; }
  204. // get sync offset from current frame (not usually used)
  205. signed int getCurSyncOffset() { return (signed int) syncoffset_cur; }
  206. // get video, audio, width, height, framerate formats.
  207. unsigned int getVidFmt() { return vidfmt; }
  208. unsigned int getAudFmt() { return audfmt; }
  209. unsigned int getWidth() { return width; }
  210. unsigned int getHeight() { return height; }
  211. double getFrameRate() { return framerate; }
  212. unsigned char getFrameRateIdx() { return framerate_idx; }
  213. // is current frame a sync frame?
  214. int isSynchFrame() { return is_sync_frame; }
  215. private:
  216. nsv_InBS *m_audiobs, *m_videobs, *m_auxbs;
  217. int valid; // contents of stream info are valid for syncing
  218. int synched; // turns off anal packet checking
  219. unsigned int vidfmt;
  220. unsigned int audfmt;
  221. unsigned int width;
  222. unsigned int height;
  223. double framerate;
  224. int is_sync_frame;
  225. unsigned char framerate_idx;
  226. int syncoffset;
  227. int syncoffset_cur;
  228. int m_eof;
  229. };
  230. /*********************************************************************
  231. ** NSV file header reading/writing functions
  232. */
  233. typedef struct {
  234. // header_size is the size of NSV header. nsv_writeheader() and nsv_readheader()
  235. // will set this automatically
  236. unsigned int header_size;
  237. // file_lenbytes is the size of the NSV bitstream (not including the header size)
  238. // this can be 0xFFFFFFFF to signify unknown length
  239. unsigned int file_lenbytes;
  240. // file_lenms is the length of the NSV bitstream in milliseconds.
  241. // this can be 0xFFFFFFFF to signify unknown length
  242. unsigned int file_lenms;
  243. // metadata_len describes the length of the metadata.
  244. unsigned int metadata_len;
  245. // toc_alloc describes the allocated length of the TOC (in entries).
  246. // set this to zero to use toc_size (recommended).
  247. unsigned int toc_alloc;
  248. // toc_size describes the used size of the TOC (in entries)
  249. // set this to zero to disable the TOC. When using toc_ex,
  250. // this must be < toc_alloc/2 (if using nsv_writeheader, and
  251. // toc_size is too big, toc_alloc will be grown automatically.
  252. unsigned int toc_size;
  253. // buffer which contains the TOC. this will be automatically
  254. // allocated when using nsv_readheader(), but you should allocate
  255. // this yourself when using nsv_writeheader()
  256. unsigned int *toc;
  257. // if used, contains time pairs (in frames) for the offset. this will be
  258. // automatically allocated when using nsv_readheader(), but you should allocate
  259. // this yourself when using nsv_writeheader()
  260. // DO NOT FREE THIS VALUE IF IT WAS ALLOCATED FROM NSV_READHEADER. :)
  261. // (it is just an extension of toc, which should be freed with free())
  262. unsigned int *toc_ex;
  263. // buffer which contains metadata. allocated when using nsv_readheader(),
  264. // but you should allocate this yourself when using nsv_writeheader()
  265. // note that nsv_readheader() will NULL terminate this buffer.
  266. void *metadata;
  267. } nsv_fileHeader;
  268. // nsv_writeheader() writes the NSV file header to the bitstream bs.
  269. // the NSV file header will be at LEAST padto bytes long (usually
  270. // you will leave padto to 0)
  271. void nsv_writeheader(nsv_OutBS &bs, nsv_fileHeader *hdr, unsigned int padto);
  272. // nsv_readheader() reads an NSV file header from a bitstream bs.
  273. // if the return value is less than zero, then there is no NSV
  274. // file header in bs. if the return value is zero, the NSV file
  275. // header was succesfully read. if the return value is positive,
  276. // then at least that many more bytes are needed to decode the
  277. // header.
  278. // ex:
  279. // nsv_InBS bs;
  280. // nsv_fileHeader hdr;
  281. // for (;;) {
  282. // int ret=nsv_readheader(bs,&hdr);
  283. // if (ret<=0) break;
  284. // addBytesToBs(bs,ret);
  285. // }
  286. // if (hdr.header_size) { we_got_valid_header(&hdr); }
  287. //
  288. int nsv_readheader(nsv_InBS &bs, nsv_fileHeader *hdr);
  289. // nsv_getmetadata() retrieves a metadata item from the metadata
  290. // block. if that item is not found, NULL is returned.
  291. // Note that the value returned by nsv_getmetadata() has been
  292. // malloc()'d, and you must free() it when you are done.
  293. // ex:
  294. // char *v=nsv_getmetadata(hdr.metadata,"TITLE");
  295. // if (v) printf("title=%s\n",v);
  296. // free(v);
  297. //
  298. char *nsv_getmetadata(void *metadata, char *name);
  299. #endif//_NSVLIB_H_