theoradec.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  5. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 *
  9. * by the Xiph.Org Foundation http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function:
  13. last mod: $Id: theora.h,v 1.8 2004/03/15 22:17:32 derf Exp $
  14. ********************************************************************/
  15. /**\file
  16. * The <tt>libtheoradec</tt> C decoding API.*/
  17. #if !defined(_O_THEORA_THEORADEC_H_)
  18. # define _O_THEORA_THEORADEC_H_ (1)
  19. # include <stddef.h>
  20. # include <ogg/ogg.h>
  21. # include "codec.h"
  22. #if defined(__cplusplus)
  23. extern "C" {
  24. #endif
  25. /**\name th_decode_ctl() codes
  26. * \anchor decctlcodes
  27. * These are the available request codes for th_decode_ctl().
  28. * By convention, these are odd, to distinguish them from the
  29. * \ref encctlcodes "encoder control codes".
  30. * Keep any experimental or vendor-specific values above \c 0x8000.*/
  31. /*@{*/
  32. /**Gets the maximum post-processing level.
  33. * The decoder supports a post-processing filter that can improve
  34. * the appearance of the decoded images. This returns the highest
  35. * level setting for this post-processor, corresponding to maximum
  36. * improvement and computational expense.
  37. *
  38. * \param[out] _buf int: The maximum post-processing level.
  39. * \retval TH_EFAULT \a _dec_ctx or \a _buf is <tt>NULL</tt>.
  40. * \retval TH_EINVAL \a _buf_sz is not <tt>sizeof(int)</tt>.
  41. * \retval TH_EIMPL Not supported by this implementation.*/
  42. #define TH_DECCTL_GET_PPLEVEL_MAX (1)
  43. /**Sets the post-processing level.
  44. * By default, post-processing is disabled.
  45. *
  46. * Sets the level of post-processing to use when decoding the
  47. * compressed stream. This must be a value between zero (off)
  48. * and the maximum returned by TH_DECCTL_GET_PPLEVEL_MAX.
  49. *
  50. * \param[in] _buf int: The new post-processing level.
  51. * 0 to disable; larger values use more CPU.
  52. * \retval TH_EFAULT \a _dec_ctx or \a _buf is <tt>NULL</tt>.
  53. * \retval TH_EINVAL \a _buf_sz is not <tt>sizeof(int)</tt>, or the
  54. * post-processing level is out of bounds.
  55. * The maximum post-processing level may be
  56. * implementation-specific, and can be obtained via
  57. * #TH_DECCTL_GET_PPLEVEL_MAX.
  58. * \retval TH_EIMPL Not supported by this implementation.*/
  59. #define TH_DECCTL_SET_PPLEVEL (3)
  60. /**Sets the granule position.
  61. * Call this after a seek, before decoding the first frame, to ensure that the
  62. * proper granule position is returned for all subsequent frames.
  63. * If you track timestamps yourself and do not use the granule position
  64. * returned by the decoder, then you need not call this function.
  65. *
  66. * \param[in] _buf <tt>ogg_int64_t</tt>: The granule position of the next
  67. * frame.
  68. * \retval TH_EFAULT \a _dec_ctx or \a _buf is <tt>NULL</tt>.
  69. * \retval TH_EINVAL \a _buf_sz is not <tt>sizeof(ogg_int64_t)</tt>, or the
  70. * granule position is negative.*/
  71. #define TH_DECCTL_SET_GRANPOS (5)
  72. /**Sets the striped decode callback function.
  73. * If set, this function will be called as each piece of a frame is fully
  74. * decoded in th_decode_packetin().
  75. * You can pass in a #th_stripe_callback with
  76. * th_stripe_callback#stripe_decoded set to <tt>NULL</tt> to disable the
  77. * callbacks at any point.
  78. * Enabling striped decode does not prevent you from calling
  79. * th_decode_ycbcr_out() after the frame is fully decoded.
  80. *
  81. * \param[in] _buf #th_stripe_callback: The callback parameters.
  82. * \retval TH_EFAULT \a _dec_ctx or \a _buf is <tt>NULL</tt>.
  83. * \retval TH_EINVAL \a _buf_sz is not
  84. * <tt>sizeof(th_stripe_callback)</tt>.*/
  85. #define TH_DECCTL_SET_STRIPE_CB (7)
  86. /**Sets the macroblock display mode. Set to 0 to disable displaying
  87. * macroblocks.*/
  88. #define TH_DECCTL_SET_TELEMETRY_MBMODE (9)
  89. /**Sets the motion vector display mode. Set to 0 to disable displaying motion
  90. * vectors.*/
  91. #define TH_DECCTL_SET_TELEMETRY_MV (11)
  92. /**Sets the adaptive quantization display mode. Set to 0 to disable displaying
  93. * adaptive quantization. */
  94. #define TH_DECCTL_SET_TELEMETRY_QI (13)
  95. /**Sets the bitstream breakdown visualization mode. Set to 0 to disable
  96. * displaying bitstream breakdown.*/
  97. #define TH_DECCTL_SET_TELEMETRY_BITS (15)
  98. /*@}*/
  99. /**A callback function for striped decode.
  100. * This is a function pointer to an application-provided function that will be
  101. * called each time a section of the image is fully decoded in
  102. * th_decode_packetin().
  103. * This allows the application to process the section immediately, while it is
  104. * still in cache.
  105. * Note that the frame is decoded bottom to top, so \a _yfrag0 will steadily
  106. * decrease with each call until it reaches 0, at which point the full frame
  107. * is decoded.
  108. * The number of fragment rows made available in each call depends on the pixel
  109. * format and the number of post-processing filters enabled, and may not even
  110. * be constant for the entire frame.
  111. * If a non-<tt>NULL</tt> \a _granpos pointer is passed to
  112. * th_decode_packetin(), the granule position for the frame will be stored
  113. * in it before the first callback is made.
  114. * If an entire frame is dropped (a 0-byte packet), then no callbacks will be
  115. * made at all for that frame.
  116. * \param _ctx An application-provided context pointer.
  117. * \param _buf The image buffer for the decoded frame.
  118. * \param _yfrag0 The Y coordinate of the first row of 8x8 fragments
  119. * decoded.
  120. * Multiply this by 8 to obtain the pixel row number in the
  121. * luma plane.
  122. * If the chroma planes are subsampled in the Y direction,
  123. * this will always be divisible by two.
  124. * \param _yfrag_end The Y coordinate of the first row of 8x8 fragments past
  125. * the newly decoded section.
  126. * If the chroma planes are subsampled in the Y direction,
  127. * this will always be divisible by two.
  128. * I.e., this section contains fragment rows
  129. * <tt>\a _yfrag0 ...\a _yfrag_end -1</tt>.*/
  130. typedef void (*th_stripe_decoded_func)(void *_ctx,th_ycbcr_buffer _buf,
  131. int _yfrag0,int _yfrag_end);
  132. /**The striped decode callback data to pass to #TH_DECCTL_SET_STRIPE_CB.*/
  133. typedef struct{
  134. /**An application-provided context pointer.
  135. * This will be passed back verbatim to the application.*/
  136. void *ctx;
  137. /**The callback function pointer.*/
  138. th_stripe_decoded_func stripe_decoded;
  139. }th_stripe_callback;
  140. /**\name Decoder state
  141. The following data structures are opaque, and their contents are not
  142. publicly defined by this API.
  143. Referring to their internals directly is unsupported, and may break without
  144. warning.*/
  145. /*@{*/
  146. /**The decoder context.*/
  147. typedef struct th_dec_ctx th_dec_ctx;
  148. /**Setup information.
  149. This contains auxiliary information (Huffman tables and quantization
  150. parameters) decoded from the setup header by th_decode_headerin() to be
  151. passed to th_decode_alloc().
  152. It can be re-used to initialize any number of decoders, and can be freed
  153. via th_setup_free() at any time.*/
  154. typedef struct th_setup_info th_setup_info;
  155. /*@}*/
  156. /**\defgroup decfuncs Functions for Decoding*/
  157. /*@{*/
  158. /**\name Functions for decoding
  159. * You must link to <tt>libtheoradec</tt> if you use any of the
  160. * functions in this section.
  161. *
  162. * The functions are listed in the order they are used in a typical decode.
  163. * The basic steps are:
  164. * - Parse the header packets by repeatedly calling th_decode_headerin().
  165. * - Allocate a #th_dec_ctx handle with th_decode_alloc().
  166. * - Call th_setup_free() to free any memory used for codec setup
  167. * information.
  168. * - Perform any additional decoder configuration with th_decode_ctl().
  169. * - For each video data packet:
  170. * - Submit the packet to the decoder via th_decode_packetin().
  171. * - Retrieve the uncompressed video data via th_decode_ycbcr_out().
  172. * - Call th_decode_free() to release all decoder memory.*/
  173. /*@{*/
  174. /**Decodes the header packets of a Theora stream.
  175. * This should be called on the initial packets of the stream, in succession,
  176. * until it returns <tt>0</tt>, indicating that all headers have been
  177. * processed, or an error is encountered.
  178. * At least three header packets are required, and additional optional header
  179. * packets may follow.
  180. * This can be used on the first packet of any logical stream to determine if
  181. * that stream is a Theora stream.
  182. * \param _info A #th_info structure to fill in.
  183. * This must have been previously initialized with
  184. * th_info_init().
  185. * The application may immediately begin using the contents of
  186. * this structure after the first header is decoded, though it
  187. * must continue to be passed in on all subsequent calls.
  188. * \param _tc A #th_comment structure to fill in.
  189. * The application may immediately begin using the contents of
  190. * this structure after the second header is decoded, though it
  191. * must continue to be passed in on all subsequent calls.
  192. * \param _setup Returns a pointer to additional, private setup information
  193. * needed by the decoder.
  194. * The contents of this pointer must be initialized to
  195. * <tt>NULL</tt> on the first call, and the returned value must
  196. * continue to be passed in on all subsequent calls.
  197. * \param _op An <tt>ogg_packet</tt> structure which contains one of the
  198. * initial packets of an Ogg logical stream.
  199. * \return A positive value indicates that a Theora header was successfully
  200. * processed.
  201. * \retval 0 The first video data packet was encountered after all
  202. * required header packets were parsed.
  203. * The packet just passed in on this call should be saved
  204. * and fed to th_decode_packetin() to begin decoding
  205. * video data.
  206. * \retval TH_EFAULT One of \a _info, \a _tc, or \a _setup was
  207. * <tt>NULL</tt>.
  208. * \retval TH_EBADHEADER \a _op was <tt>NULL</tt>, the packet was not the next
  209. * header packet in the expected sequence, or the format
  210. * of the header data was invalid.
  211. * \retval TH_EVERSION The packet data was a Theora info header, but for a
  212. * bitstream version not decodable with this version of
  213. * <tt>libtheoradec</tt>.
  214. * \retval TH_ENOTFORMAT The packet was not a Theora header.
  215. */
  216. extern int th_decode_headerin(th_info *_info,th_comment *_tc,
  217. th_setup_info **_setup,ogg_packet *_op);
  218. /**Allocates a decoder instance.
  219. *
  220. * <b>Security Warning:</b> The Theora format supports very large frame sizes,
  221. * potentially even larger than the address space of a 32-bit machine, and
  222. * creating a decoder context allocates the space for several frames of data.
  223. * If the allocation fails here, your program will crash, possibly at some
  224. * future point because the OS kernel returned a valid memory range and will
  225. * only fail when it tries to map the pages in it the first time they are
  226. * used.
  227. * Even if it succeeds, you may experience a denial of service if the frame
  228. * size is large enough to cause excessive paging.
  229. * If you are integrating libtheora in a larger application where such things
  230. * are undesirable, it is highly recommended that you check the frame size in
  231. * \a _info before calling this function and refuse to decode streams where it
  232. * is larger than some reasonable maximum.
  233. * libtheora will not check this for you, because there may be machines that
  234. * can handle such streams and applications that wish to.
  235. * \param _info A #th_info struct filled via th_decode_headerin().
  236. * \param _setup A #th_setup_info handle returned via
  237. * th_decode_headerin().
  238. * \return The initialized #th_dec_ctx handle.
  239. * \retval NULL If the decoding parameters were invalid.*/
  240. extern th_dec_ctx *th_decode_alloc(const th_info *_info,
  241. const th_setup_info *_setup);
  242. /**Releases all storage used for the decoder setup information.
  243. * This should be called after you no longer want to create any decoders for
  244. * a stream whose headers you have parsed with th_decode_headerin().
  245. * \param _setup The setup information to free.
  246. * This can safely be <tt>NULL</tt>.*/
  247. extern void th_setup_free(th_setup_info *_setup);
  248. /**Decoder control function.
  249. * This is used to provide advanced control of the decoding process.
  250. * \param _dec A #th_dec_ctx handle.
  251. * \param _req The control code to process.
  252. * See \ref decctlcodes "the list of available control codes"
  253. * for details.
  254. * \param _buf The parameters for this control code.
  255. * \param _buf_sz The size of the parameter buffer.
  256. * \return Possible return values depend on the control code used.
  257. * See \ref decctlcodes "the list of control codes" for
  258. * specific values. Generally 0 indicates success.*/
  259. extern int th_decode_ctl(th_dec_ctx *_dec,int _req,void *_buf,
  260. size_t _buf_sz);
  261. /**Submits a packet containing encoded video data to the decoder.
  262. * \param _dec A #th_dec_ctx handle.
  263. * \param _op An <tt>ogg_packet</tt> containing encoded video data.
  264. * \param _granpos Returns the granule position of the decoded packet.
  265. * If non-<tt>NULL</tt>, the granule position for this specific
  266. * packet is stored in this location.
  267. * This is computed incrementally from previously decoded
  268. * packets.
  269. * After a seek, the correct granule position must be set via
  270. * #TH_DECCTL_SET_GRANPOS for this to work properly.
  271. * \retval 0 Success.
  272. * A new decoded frame can be retrieved by calling
  273. * th_decode_ycbcr_out().
  274. * \retval TH_DUPFRAME The packet represented a dropped frame (either a
  275. * 0-byte frame or an INTER frame with no coded blocks).
  276. * The player can skip the call to th_decode_ycbcr_out(),
  277. * as the contents of the decoded frame buffer have not
  278. * changed.
  279. * \retval TH_EFAULT \a _dec or \a _op was <tt>NULL</tt>.
  280. * \retval TH_EBADPACKET \a _op does not contain encoded video data.
  281. * \retval TH_EIMPL The video data uses bitstream features which this
  282. * library does not support.*/
  283. extern int th_decode_packetin(th_dec_ctx *_dec,const ogg_packet *_op,
  284. ogg_int64_t *_granpos);
  285. /**Outputs the next available frame of decoded Y'CbCr data.
  286. * If a striped decode callback has been set with #TH_DECCTL_SET_STRIPE_CB,
  287. * then the application does not need to call this function.
  288. * \param _dec A #th_dec_ctx handle.
  289. * \param _ycbcr A video buffer structure to fill in.
  290. * <tt>libtheoradec</tt> will fill in all the members of this
  291. * structure, including the pointers to the uncompressed video
  292. * data.
  293. * The memory for this video data is owned by
  294. * <tt>libtheoradec</tt>.
  295. * It may be freed or overwritten without notification when
  296. * subsequent frames are decoded.
  297. * \retval 0 Success
  298. * \retval TH_EFAULT \a _dec or \a _ycbcr was <tt>NULL</tt>.
  299. */
  300. extern int th_decode_ycbcr_out(th_dec_ctx *_dec,
  301. th_ycbcr_buffer _ycbcr);
  302. /**Frees an allocated decoder instance.
  303. * \param _dec A #th_dec_ctx handle.*/
  304. extern void th_decode_free(th_dec_ctx *_dec);
  305. /*@}*/
  306. /*@}*/
  307. #if defined(__cplusplus)
  308. }
  309. #endif
  310. #endif