Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /******************************************************************** |
michael@0 | 2 | * * |
michael@0 | 3 | * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. * |
michael@0 | 4 | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
michael@0 | 5 | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
michael@0 | 6 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
michael@0 | 7 | * * |
michael@0 | 8 | * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009 * |
michael@0 | 9 | * by the Xiph.Org Foundation http://www.xiph.org/ * |
michael@0 | 10 | * * |
michael@0 | 11 | ******************************************************************** |
michael@0 | 12 | |
michael@0 | 13 | function: |
michael@0 | 14 | last mod: $Id: theora.h,v 1.8 2004/03/15 22:17:32 derf Exp $ |
michael@0 | 15 | |
michael@0 | 16 | ********************************************************************/ |
michael@0 | 17 | |
michael@0 | 18 | /**\mainpage |
michael@0 | 19 | * |
michael@0 | 20 | * \section intro Introduction |
michael@0 | 21 | * |
michael@0 | 22 | * This is the documentation for <tt>libtheora</tt> C API. |
michael@0 | 23 | * The current reference |
michael@0 | 24 | * implementation for <a href="http://www.theora.org/">Theora</a>, a free, |
michael@0 | 25 | * patent-unencumbered video codec. |
michael@0 | 26 | * Theora is derived from On2's VP3 codec with additional features and |
michael@0 | 27 | * integration with Ogg multimedia formats by |
michael@0 | 28 | * <a href="http://www.xiph.org/">the Xiph.Org Foundation</a>. |
michael@0 | 29 | * Complete documentation of the format itself is available in |
michael@0 | 30 | * <a href="http://www.theora.org/doc/Theora.pdf">the Theora |
michael@0 | 31 | * specification</a>. |
michael@0 | 32 | * |
michael@0 | 33 | * \subsection Organization |
michael@0 | 34 | * |
michael@0 | 35 | * The functions documented here are actually subdivided into three |
michael@0 | 36 | * separate libraries: |
michael@0 | 37 | * - <tt>libtheoraenc</tt> contains the encoder interface, |
michael@0 | 38 | * described in \ref encfuncs. |
michael@0 | 39 | * - <tt>libtheoradec</tt> contains the decoder interface and |
michael@0 | 40 | * routines shared with the encoder. |
michael@0 | 41 | * You must also link to this if you link to <tt>libtheoraenc</tt>. |
michael@0 | 42 | * The routines in this library are described in \ref decfuncs and |
michael@0 | 43 | * \ref basefuncs. |
michael@0 | 44 | * - <tt>libtheora</tt> contains the \ref oldfuncs. |
michael@0 | 45 | * |
michael@0 | 46 | * New code should link to <tt>libtheoradec</tt> and, if using encoder |
michael@0 | 47 | * features, <tt>libtheoraenc</tt>. Together these two export both |
michael@0 | 48 | * the standard and the legacy API, so this is all that is needed by |
michael@0 | 49 | * any code. The older <tt>libtheora</tt> library is provided just for |
michael@0 | 50 | * compatibility with older build configurations. |
michael@0 | 51 | * |
michael@0 | 52 | * In general the recommended 1.x API symbols can be distinguished |
michael@0 | 53 | * by their <tt>th_</tt> or <tt>TH_</tt> namespace prefix. |
michael@0 | 54 | * The older, legacy API uses <tt>theora_</tt> or <tt>OC_</tt> |
michael@0 | 55 | * prefixes instead. |
michael@0 | 56 | */ |
michael@0 | 57 | |
michael@0 | 58 | /**\file |
michael@0 | 59 | * The shared <tt>libtheoradec</tt> and <tt>libtheoraenc</tt> C API. |
michael@0 | 60 | * You don't need to include this directly.*/ |
michael@0 | 61 | |
michael@0 | 62 | #if !defined(_O_THEORA_CODEC_H_) |
michael@0 | 63 | # define _O_THEORA_CODEC_H_ (1) |
michael@0 | 64 | # include <ogg/ogg.h> |
michael@0 | 65 | |
michael@0 | 66 | #if defined(__cplusplus) |
michael@0 | 67 | extern "C" { |
michael@0 | 68 | #endif |
michael@0 | 69 | |
michael@0 | 70 | |
michael@0 | 71 | |
michael@0 | 72 | /**\name Return codes*/ |
michael@0 | 73 | /*@{*/ |
michael@0 | 74 | /**An invalid pointer was provided.*/ |
michael@0 | 75 | #define TH_EFAULT (-1) |
michael@0 | 76 | /**An invalid argument was provided.*/ |
michael@0 | 77 | #define TH_EINVAL (-10) |
michael@0 | 78 | /**The contents of the header were incomplete, invalid, or unexpected.*/ |
michael@0 | 79 | #define TH_EBADHEADER (-20) |
michael@0 | 80 | /**The header does not belong to a Theora stream.*/ |
michael@0 | 81 | #define TH_ENOTFORMAT (-21) |
michael@0 | 82 | /**The bitstream version is too high.*/ |
michael@0 | 83 | #define TH_EVERSION (-22) |
michael@0 | 84 | /**The specified function is not implemented.*/ |
michael@0 | 85 | #define TH_EIMPL (-23) |
michael@0 | 86 | /**There were errors in the video data packet.*/ |
michael@0 | 87 | #define TH_EBADPACKET (-24) |
michael@0 | 88 | /**The decoded packet represented a dropped frame. |
michael@0 | 89 | The player can continue to display the current frame, as the contents of the |
michael@0 | 90 | decoded frame buffer have not changed.*/ |
michael@0 | 91 | #define TH_DUPFRAME (1) |
michael@0 | 92 | /*@}*/ |
michael@0 | 93 | |
michael@0 | 94 | /**The currently defined color space tags. |
michael@0 | 95 | * See <a href="http://www.theora.org/doc/Theora.pdf">the Theora |
michael@0 | 96 | * specification</a>, Chapter 4, for exact details on the meaning |
michael@0 | 97 | * of each of these color spaces.*/ |
michael@0 | 98 | typedef enum{ |
michael@0 | 99 | /**The color space was not specified at the encoder. |
michael@0 | 100 | It may be conveyed by an external means.*/ |
michael@0 | 101 | TH_CS_UNSPECIFIED, |
michael@0 | 102 | /**A color space designed for NTSC content.*/ |
michael@0 | 103 | TH_CS_ITU_REC_470M, |
michael@0 | 104 | /**A color space designed for PAL/SECAM content.*/ |
michael@0 | 105 | TH_CS_ITU_REC_470BG, |
michael@0 | 106 | /**The total number of currently defined color spaces.*/ |
michael@0 | 107 | TH_CS_NSPACES |
michael@0 | 108 | }th_colorspace; |
michael@0 | 109 | |
michael@0 | 110 | /**The currently defined pixel format tags. |
michael@0 | 111 | * See <a href="http://www.theora.org/doc/Theora.pdf">the Theora |
michael@0 | 112 | * specification</a>, Section 4.4, for details on the precise sample |
michael@0 | 113 | * locations.*/ |
michael@0 | 114 | typedef enum{ |
michael@0 | 115 | /**Chroma decimation by 2 in both the X and Y directions (4:2:0). |
michael@0 | 116 | The Cb and Cr chroma planes are half the width and half the |
michael@0 | 117 | height of the luma plane.*/ |
michael@0 | 118 | TH_PF_420, |
michael@0 | 119 | /**Currently reserved.*/ |
michael@0 | 120 | TH_PF_RSVD, |
michael@0 | 121 | /**Chroma decimation by 2 in the X direction (4:2:2). |
michael@0 | 122 | The Cb and Cr chroma planes are half the width of the luma plane, but full |
michael@0 | 123 | height.*/ |
michael@0 | 124 | TH_PF_422, |
michael@0 | 125 | /**No chroma decimation (4:4:4). |
michael@0 | 126 | The Cb and Cr chroma planes are full width and full height.*/ |
michael@0 | 127 | TH_PF_444, |
michael@0 | 128 | /**The total number of currently defined pixel formats.*/ |
michael@0 | 129 | TH_PF_NFORMATS |
michael@0 | 130 | }th_pixel_fmt; |
michael@0 | 131 | |
michael@0 | 132 | |
michael@0 | 133 | |
michael@0 | 134 | /**A buffer for a single color plane in an uncompressed image. |
michael@0 | 135 | * This contains the image data in a left-to-right, top-down format. |
michael@0 | 136 | * Each row of pixels is stored contiguously in memory, but successive |
michael@0 | 137 | * rows need not be. |
michael@0 | 138 | * Use \a stride to compute the offset of the next row. |
michael@0 | 139 | * The encoder accepts both positive \a stride values (top-down in memory) |
michael@0 | 140 | * and negative (bottom-up in memory). |
michael@0 | 141 | * The decoder currently always generates images with positive strides.*/ |
michael@0 | 142 | typedef struct{ |
michael@0 | 143 | /**The width of this plane.*/ |
michael@0 | 144 | int width; |
michael@0 | 145 | /**The height of this plane.*/ |
michael@0 | 146 | int height; |
michael@0 | 147 | /**The offset in bytes between successive rows.*/ |
michael@0 | 148 | int stride; |
michael@0 | 149 | /**A pointer to the beginning of the first row.*/ |
michael@0 | 150 | unsigned char *data; |
michael@0 | 151 | }th_img_plane; |
michael@0 | 152 | |
michael@0 | 153 | /**A complete image buffer for an uncompressed frame. |
michael@0 | 154 | * The chroma planes may be decimated by a factor of two in either |
michael@0 | 155 | * direction, as indicated by th_info#pixel_fmt. |
michael@0 | 156 | * The width and height of the Y' plane must be multiples of 16. |
michael@0 | 157 | * They may need to be cropped for display, using the rectangle |
michael@0 | 158 | * specified by th_info#pic_x, th_info#pic_y, th_info#pic_width, |
michael@0 | 159 | * and th_info#pic_height. |
michael@0 | 160 | * All samples are 8 bits. |
michael@0 | 161 | * \note The term YUV often used to describe a colorspace is ambiguous. |
michael@0 | 162 | * The exact parameters of the RGB to YUV conversion process aside, in |
michael@0 | 163 | * many contexts the U and V channels actually have opposite meanings. |
michael@0 | 164 | * To avoid this confusion, we are explicit: the name of the color |
michael@0 | 165 | * channels are Y'CbCr, and they appear in that order, always. |
michael@0 | 166 | * The prime symbol denotes that the Y channel is non-linear. |
michael@0 | 167 | * Cb and Cr stand for "Chroma blue" and "Chroma red", respectively.*/ |
michael@0 | 168 | typedef th_img_plane th_ycbcr_buffer[3]; |
michael@0 | 169 | |
michael@0 | 170 | /**Theora bitstream information. |
michael@0 | 171 | * This contains the basic playback parameters for a stream, and corresponds to |
michael@0 | 172 | * the initial 'info' header packet. |
michael@0 | 173 | * To initialize an encoder, the application fills in this structure and |
michael@0 | 174 | * passes it to th_encode_alloc(). |
michael@0 | 175 | * A default encoding mode is chosen based on the values of the #quality and |
michael@0 | 176 | * #target_bitrate fields. |
michael@0 | 177 | * On decode, it is filled in by th_decode_headerin(), and then passed to |
michael@0 | 178 | * th_decode_alloc(). |
michael@0 | 179 | * |
michael@0 | 180 | * Encoded Theora frames must be a multiple of 16 in size; |
michael@0 | 181 | * this is what the #frame_width and #frame_height members represent. |
michael@0 | 182 | * To handle arbitrary picture sizes, a crop rectangle is specified in the |
michael@0 | 183 | * #pic_x, #pic_y, #pic_width and #pic_height members. |
michael@0 | 184 | * |
michael@0 | 185 | * All frame buffers contain pointers to the full, padded frame. |
michael@0 | 186 | * However, the current encoder <em>will not</em> reference pixels outside of |
michael@0 | 187 | * the cropped picture region, and the application does not need to fill them |
michael@0 | 188 | * in. |
michael@0 | 189 | * The decoder <em>will</em> allocate storage for a full frame, but the |
michael@0 | 190 | * application <em>should not</em> rely on the padding containing sensible |
michael@0 | 191 | * data. |
michael@0 | 192 | * |
michael@0 | 193 | * It is also generally recommended that the offsets and sizes should still be |
michael@0 | 194 | * multiples of 2 to avoid chroma sampling shifts when chroma is sub-sampled. |
michael@0 | 195 | * See <a href="http://www.theora.org/doc/Theora.pdf">the Theora |
michael@0 | 196 | * specification</a>, Section 4.4, for more details. |
michael@0 | 197 | * |
michael@0 | 198 | * Frame rate, in frames per second, is stored as a rational fraction, as is |
michael@0 | 199 | * the pixel aspect ratio. |
michael@0 | 200 | * Note that this refers to the aspect ratio of the individual pixels, not of |
michael@0 | 201 | * the overall frame itself. |
michael@0 | 202 | * The frame aspect ratio can be computed from pixel aspect ratio using the |
michael@0 | 203 | * image dimensions.*/ |
michael@0 | 204 | typedef struct{ |
michael@0 | 205 | /**\name Theora version |
michael@0 | 206 | * Bitstream version information.*/ |
michael@0 | 207 | /*@{*/ |
michael@0 | 208 | unsigned char version_major; |
michael@0 | 209 | unsigned char version_minor; |
michael@0 | 210 | unsigned char version_subminor; |
michael@0 | 211 | /*@}*/ |
michael@0 | 212 | /**The encoded frame width. |
michael@0 | 213 | * This must be a multiple of 16, and less than 1048576.*/ |
michael@0 | 214 | ogg_uint32_t frame_width; |
michael@0 | 215 | /**The encoded frame height. |
michael@0 | 216 | * This must be a multiple of 16, and less than 1048576.*/ |
michael@0 | 217 | ogg_uint32_t frame_height; |
michael@0 | 218 | /**The displayed picture width. |
michael@0 | 219 | * This must be no larger than width.*/ |
michael@0 | 220 | ogg_uint32_t pic_width; |
michael@0 | 221 | /**The displayed picture height. |
michael@0 | 222 | * This must be no larger than height.*/ |
michael@0 | 223 | ogg_uint32_t pic_height; |
michael@0 | 224 | /**The X offset of the displayed picture. |
michael@0 | 225 | * This must be no larger than #frame_width-#pic_width or 255, whichever is |
michael@0 | 226 | * smaller.*/ |
michael@0 | 227 | ogg_uint32_t pic_x; |
michael@0 | 228 | /**The Y offset of the displayed picture. |
michael@0 | 229 | * This must be no larger than #frame_height-#pic_height, and |
michael@0 | 230 | * #frame_height-#pic_height-#pic_y must be no larger than 255. |
michael@0 | 231 | * This slightly funny restriction is due to the fact that the offset is |
michael@0 | 232 | * specified from the top of the image for consistency with the standard |
michael@0 | 233 | * graphics left-handed coordinate system used throughout this API, while |
michael@0 | 234 | * it is stored in the encoded stream as an offset from the bottom.*/ |
michael@0 | 235 | ogg_uint32_t pic_y; |
michael@0 | 236 | /**\name Frame rate |
michael@0 | 237 | * The frame rate, as a fraction. |
michael@0 | 238 | * If either is 0, the frame rate is undefined.*/ |
michael@0 | 239 | /*@{*/ |
michael@0 | 240 | ogg_uint32_t fps_numerator; |
michael@0 | 241 | ogg_uint32_t fps_denominator; |
michael@0 | 242 | /*@}*/ |
michael@0 | 243 | /**\name Aspect ratio |
michael@0 | 244 | * The aspect ratio of the pixels. |
michael@0 | 245 | * If either value is zero, the aspect ratio is undefined. |
michael@0 | 246 | * If not specified by any external means, 1:1 should be assumed. |
michael@0 | 247 | * The aspect ratio of the full picture can be computed as |
michael@0 | 248 | * \code |
michael@0 | 249 | * aspect_numerator*pic_width/(aspect_denominator*pic_height). |
michael@0 | 250 | * \endcode */ |
michael@0 | 251 | /*@{*/ |
michael@0 | 252 | ogg_uint32_t aspect_numerator; |
michael@0 | 253 | ogg_uint32_t aspect_denominator; |
michael@0 | 254 | /*@}*/ |
michael@0 | 255 | /**The color space.*/ |
michael@0 | 256 | th_colorspace colorspace; |
michael@0 | 257 | /**The pixel format.*/ |
michael@0 | 258 | th_pixel_fmt pixel_fmt; |
michael@0 | 259 | /**The target bit-rate in bits per second. |
michael@0 | 260 | If initializing an encoder with this struct, set this field to a non-zero |
michael@0 | 261 | value to activate CBR encoding by default.*/ |
michael@0 | 262 | int target_bitrate; |
michael@0 | 263 | /**The target quality level. |
michael@0 | 264 | Valid values range from 0 to 63, inclusive, with higher values giving |
michael@0 | 265 | higher quality. |
michael@0 | 266 | If initializing an encoder with this struct, and #target_bitrate is set |
michael@0 | 267 | to zero, VBR encoding at this quality will be activated by default.*/ |
michael@0 | 268 | /*Currently this is set so that a qi of 0 corresponds to distortions of 24 |
michael@0 | 269 | times the JND, and each increase by 16 halves that value. |
michael@0 | 270 | This gives us fine discrimination at low qualities, yet effective rate |
michael@0 | 271 | control at high qualities. |
michael@0 | 272 | The qi value 63 is special, however. |
michael@0 | 273 | For this, the highest quality, we use one half of a JND for our threshold. |
michael@0 | 274 | Due to the lower bounds placed on allowable quantizers in Theora, we will |
michael@0 | 275 | not actually be able to achieve quality this good, but this should |
michael@0 | 276 | provide as close to visually lossless quality as Theora is capable of. |
michael@0 | 277 | We could lift the quantizer restrictions without breaking VP3.1 |
michael@0 | 278 | compatibility, but this would result in quantized coefficients that are |
michael@0 | 279 | too large for the current bitstream to be able to store. |
michael@0 | 280 | We'd have to redesign the token syntax to store these large coefficients, |
michael@0 | 281 | which would make transcoding complex.*/ |
michael@0 | 282 | int quality; |
michael@0 | 283 | /**The amount to shift to extract the last keyframe number from the granule |
michael@0 | 284 | * position. |
michael@0 | 285 | * This can be at most 31. |
michael@0 | 286 | * th_info_init() will set this to a default value (currently <tt>6</tt>, |
michael@0 | 287 | * which is good for streaming applications), but you can set it to 0 to |
michael@0 | 288 | * make every frame a keyframe. |
michael@0 | 289 | * The maximum distance between key frames is |
michael@0 | 290 | * <tt>1<<#keyframe_granule_shift</tt>. |
michael@0 | 291 | * The keyframe frequency can be more finely controlled with |
michael@0 | 292 | * #TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE, which can also be adjusted |
michael@0 | 293 | * during encoding (for example, to force the next frame to be a keyframe), |
michael@0 | 294 | * but it cannot be set larger than the amount permitted by this field after |
michael@0 | 295 | * the headers have been output.*/ |
michael@0 | 296 | int keyframe_granule_shift; |
michael@0 | 297 | }th_info; |
michael@0 | 298 | |
michael@0 | 299 | /**The comment information. |
michael@0 | 300 | * |
michael@0 | 301 | * This structure holds the in-stream metadata corresponding to |
michael@0 | 302 | * the 'comment' header packet. |
michael@0 | 303 | * The comment header is meant to be used much like someone jotting a quick |
michael@0 | 304 | * note on the label of a video. |
michael@0 | 305 | * It should be a short, to the point text note that can be more than a couple |
michael@0 | 306 | * words, but not more than a short paragraph. |
michael@0 | 307 | * |
michael@0 | 308 | * The metadata is stored as a series of (tag, value) pairs, in |
michael@0 | 309 | * length-encoded string vectors. |
michael@0 | 310 | * The first occurrence of the '=' character delimits the tag and value. |
michael@0 | 311 | * A particular tag may occur more than once, and order is significant. |
michael@0 | 312 | * The character set encoding for the strings is always UTF-8, but the tag |
michael@0 | 313 | * names are limited to ASCII, and treated as case-insensitive. |
michael@0 | 314 | * See <a href="http://www.theora.org/doc/Theora.pdf">the Theora |
michael@0 | 315 | * specification</a>, Section 6.3.3 for details. |
michael@0 | 316 | * |
michael@0 | 317 | * In filling in this structure, th_decode_headerin() will null-terminate |
michael@0 | 318 | * the user_comment strings for safety. |
michael@0 | 319 | * However, the bitstream format itself treats them as 8-bit clean vectors, |
michael@0 | 320 | * possibly containing null characters, and so the length array should be |
michael@0 | 321 | * treated as their authoritative length. |
michael@0 | 322 | */ |
michael@0 | 323 | typedef struct th_comment{ |
michael@0 | 324 | /**The array of comment string vectors.*/ |
michael@0 | 325 | char **user_comments; |
michael@0 | 326 | /**An array of the corresponding length of each vector, in bytes.*/ |
michael@0 | 327 | int *comment_lengths; |
michael@0 | 328 | /**The total number of comment strings.*/ |
michael@0 | 329 | int comments; |
michael@0 | 330 | /**The null-terminated vendor string. |
michael@0 | 331 | This identifies the software used to encode the stream.*/ |
michael@0 | 332 | char *vendor; |
michael@0 | 333 | }th_comment; |
michael@0 | 334 | |
michael@0 | 335 | |
michael@0 | 336 | |
michael@0 | 337 | /**A single base matrix.*/ |
michael@0 | 338 | typedef unsigned char th_quant_base[64]; |
michael@0 | 339 | |
michael@0 | 340 | /**A set of \a qi ranges.*/ |
michael@0 | 341 | typedef struct{ |
michael@0 | 342 | /**The number of ranges in the set.*/ |
michael@0 | 343 | int nranges; |
michael@0 | 344 | /**The size of each of the #nranges ranges. |
michael@0 | 345 | These must sum to 63.*/ |
michael@0 | 346 | const int *sizes; |
michael@0 | 347 | /**#nranges <tt>+1</tt> base matrices. |
michael@0 | 348 | Matrices \a i and <tt>i+1</tt> form the endpoints of range \a i.*/ |
michael@0 | 349 | const th_quant_base *base_matrices; |
michael@0 | 350 | }th_quant_ranges; |
michael@0 | 351 | |
michael@0 | 352 | /**A complete set of quantization parameters. |
michael@0 | 353 | The quantizer for each coefficient is calculated as: |
michael@0 | 354 | \code |
michael@0 | 355 | Q=MAX(MIN(qmin[qti][ci!=0],scale[ci!=0][qi]*base[qti][pli][qi][ci]/100), |
michael@0 | 356 | 1024). |
michael@0 | 357 | \endcode |
michael@0 | 358 | |
michael@0 | 359 | \a qti is the quantization type index: 0 for intra, 1 for inter. |
michael@0 | 360 | <tt>ci!=0</tt> is 0 for the DC coefficient and 1 for AC coefficients. |
michael@0 | 361 | \a qi is the quality index, ranging between 0 (low quality) and 63 (high |
michael@0 | 362 | quality). |
michael@0 | 363 | \a pli is the color plane index: 0 for Y', 1 for Cb, 2 for Cr. |
michael@0 | 364 | \a ci is the DCT coefficient index. |
michael@0 | 365 | Coefficient indices correspond to the normal 2D DCT block |
michael@0 | 366 | ordering--row-major with low frequencies first--\em not zig-zag order. |
michael@0 | 367 | |
michael@0 | 368 | Minimum quantizers are constant, and are given by: |
michael@0 | 369 | \code |
michael@0 | 370 | qmin[2][2]={{4,2},{8,4}}. |
michael@0 | 371 | \endcode |
michael@0 | 372 | |
michael@0 | 373 | Parameters that can be stored in the bitstream are as follows: |
michael@0 | 374 | - The two scale matrices ac_scale and dc_scale. |
michael@0 | 375 | \code |
michael@0 | 376 | scale[2][64]={dc_scale,ac_scale}. |
michael@0 | 377 | \endcode |
michael@0 | 378 | - The base matrices for each \a qi, \a qti and \a pli (up to 384 in all). |
michael@0 | 379 | In order to avoid storing a full 384 base matrices, only a sparse set of |
michael@0 | 380 | matrices are stored, and the rest are linearly interpolated. |
michael@0 | 381 | This is done as follows. |
michael@0 | 382 | For each \a qti and \a pli, a series of \a n \a qi ranges is defined. |
michael@0 | 383 | The size of each \a qi range can vary arbitrarily, but they must sum to |
michael@0 | 384 | 63. |
michael@0 | 385 | Then, <tt>n+1</tt> matrices are specified, one for each endpoint of the |
michael@0 | 386 | ranges. |
michael@0 | 387 | For interpolation purposes, each range's endpoints are the first \a qi |
michael@0 | 388 | value it contains and one past the last \a qi value it contains. |
michael@0 | 389 | Fractional values are rounded to the nearest integer, with ties rounded |
michael@0 | 390 | away from zero. |
michael@0 | 391 | |
michael@0 | 392 | Base matrices are stored by reference, so if the same matrices are used |
michael@0 | 393 | multiple times, they will only appear once in the bitstream. |
michael@0 | 394 | The bitstream is also capable of omitting an entire set of ranges and |
michael@0 | 395 | its associated matrices if they are the same as either the previous |
michael@0 | 396 | set (indexed in row-major order) or if the inter set is the same as the |
michael@0 | 397 | intra set. |
michael@0 | 398 | |
michael@0 | 399 | - Loop filter limit values. |
michael@0 | 400 | The same limits are used for the loop filter in all color planes, despite |
michael@0 | 401 | potentially differing levels of quantization in each. |
michael@0 | 402 | |
michael@0 | 403 | For the current encoder, <tt>scale[ci!=0][qi]</tt> must be no greater |
michael@0 | 404 | than <tt>scale[ci!=0][qi-1]</tt> and <tt>base[qti][pli][qi][ci]</tt> must |
michael@0 | 405 | be no greater than <tt>base[qti][pli][qi-1][ci]</tt>. |
michael@0 | 406 | These two conditions ensure that the actual quantizer for a given \a qti, |
michael@0 | 407 | \a pli, and \a ci does not increase as \a qi increases. |
michael@0 | 408 | This is not required by the decoder.*/ |
michael@0 | 409 | typedef struct{ |
michael@0 | 410 | /**The DC scaling factors.*/ |
michael@0 | 411 | ogg_uint16_t dc_scale[64]; |
michael@0 | 412 | /**The AC scaling factors.*/ |
michael@0 | 413 | ogg_uint16_t ac_scale[64]; |
michael@0 | 414 | /**The loop filter limit values.*/ |
michael@0 | 415 | unsigned char loop_filter_limits[64]; |
michael@0 | 416 | /**The \a qi ranges for each \a ci and \a pli.*/ |
michael@0 | 417 | th_quant_ranges qi_ranges[2][3]; |
michael@0 | 418 | }th_quant_info; |
michael@0 | 419 | |
michael@0 | 420 | |
michael@0 | 421 | |
michael@0 | 422 | /**The number of Huffman tables used by Theora.*/ |
michael@0 | 423 | #define TH_NHUFFMAN_TABLES (80) |
michael@0 | 424 | /**The number of DCT token values in each table.*/ |
michael@0 | 425 | #define TH_NDCT_TOKENS (32) |
michael@0 | 426 | |
michael@0 | 427 | /**A Huffman code for a Theora DCT token. |
michael@0 | 428 | * Each set of Huffman codes in a given table must form a complete, prefix-free |
michael@0 | 429 | * code. |
michael@0 | 430 | * There is no requirement that all the tokens in a table have a valid code, |
michael@0 | 431 | * but the current encoder is not optimized to take advantage of this. |
michael@0 | 432 | * If each of the five grouops of 16 tables does not contain at least one table |
michael@0 | 433 | * with a code for every token, then the encoder may fail to encode certain |
michael@0 | 434 | * frames. |
michael@0 | 435 | * The complete table in the first group of 16 does not have to be in the same |
michael@0 | 436 | * place as the complete table in the other groups, but the complete tables in |
michael@0 | 437 | * the remaining four groups must all be in the same place.*/ |
michael@0 | 438 | typedef struct{ |
michael@0 | 439 | /**The bit pattern for the code, with the LSbit of the pattern aligned in |
michael@0 | 440 | * the LSbit of the word.*/ |
michael@0 | 441 | ogg_uint32_t pattern; |
michael@0 | 442 | /**The number of bits in the code. |
michael@0 | 443 | * This must be between 0 and 32, inclusive.*/ |
michael@0 | 444 | int nbits; |
michael@0 | 445 | }th_huff_code; |
michael@0 | 446 | |
michael@0 | 447 | |
michael@0 | 448 | |
michael@0 | 449 | /**\defgroup basefuncs Functions Shared by Encode and Decode*/ |
michael@0 | 450 | /*@{*/ |
michael@0 | 451 | /**\name Basic shared functions*/ |
michael@0 | 452 | /*@{*/ |
michael@0 | 453 | /**Retrieves a human-readable string to identify the library vendor and |
michael@0 | 454 | * version. |
michael@0 | 455 | * \return the version string.*/ |
michael@0 | 456 | extern const char *th_version_string(void); |
michael@0 | 457 | /**Retrieves the library version number. |
michael@0 | 458 | * This is the highest bitstream version that the encoder library will produce, |
michael@0 | 459 | * or that the decoder library can decode. |
michael@0 | 460 | * This number is composed of a 16-bit major version, 8-bit minor version |
michael@0 | 461 | * and 8 bit sub-version, composed as follows: |
michael@0 | 462 | * \code |
michael@0 | 463 | * (VERSION_MAJOR<<16)+(VERSION_MINOR<<8)+(VERSION_SUBMINOR) |
michael@0 | 464 | * \endcode |
michael@0 | 465 | * \return the version number.*/ |
michael@0 | 466 | extern ogg_uint32_t th_version_number(void); |
michael@0 | 467 | /**Converts a granule position to an absolute frame index, starting at |
michael@0 | 468 | * <tt>0</tt>. |
michael@0 | 469 | * The granule position is interpreted in the context of a given |
michael@0 | 470 | * #th_enc_ctx or #th_dec_ctx handle (either will suffice). |
michael@0 | 471 | * \param _encdec A previously allocated #th_enc_ctx or #th_dec_ctx |
michael@0 | 472 | * handle. |
michael@0 | 473 | * \param _granpos The granule position to convert. |
michael@0 | 474 | * \returns The absolute frame index corresponding to \a _granpos. |
michael@0 | 475 | * \retval -1 The given granule position was invalid (i.e. negative).*/ |
michael@0 | 476 | extern ogg_int64_t th_granule_frame(void *_encdec,ogg_int64_t _granpos); |
michael@0 | 477 | /**Converts a granule position to an absolute time in seconds. |
michael@0 | 478 | * The granule position is interpreted in the context of a given |
michael@0 | 479 | * #th_enc_ctx or #th_dec_ctx handle (either will suffice). |
michael@0 | 480 | * \param _encdec A previously allocated #th_enc_ctx or #th_dec_ctx |
michael@0 | 481 | * handle. |
michael@0 | 482 | * \param _granpos The granule position to convert. |
michael@0 | 483 | * \return The absolute time in seconds corresponding to \a _granpos. |
michael@0 | 484 | * This is the "end time" for the frame, or the latest time it should |
michael@0 | 485 | * be displayed. |
michael@0 | 486 | * It is not the presentation time. |
michael@0 | 487 | * \retval -1 The given granule position was invalid (i.e. negative).*/ |
michael@0 | 488 | extern double th_granule_time(void *_encdec,ogg_int64_t _granpos); |
michael@0 | 489 | /**Determines whether a Theora packet is a header or not. |
michael@0 | 490 | * This function does no verification beyond checking the packet type bit, so |
michael@0 | 491 | * it should not be used for bitstream identification; use |
michael@0 | 492 | * th_decode_headerin() for that. |
michael@0 | 493 | * As per the Theora specification, an empty (0-byte) packet is treated as a |
michael@0 | 494 | * data packet (a delta frame with no coded blocks). |
michael@0 | 495 | * \param _op An <tt>ogg_packet</tt> containing encoded Theora data. |
michael@0 | 496 | * \retval 1 The packet is a header packet |
michael@0 | 497 | * \retval 0 The packet is a video data packet.*/ |
michael@0 | 498 | extern int th_packet_isheader(ogg_packet *_op); |
michael@0 | 499 | /**Determines whether a theora packet is a key frame or not. |
michael@0 | 500 | * This function does no verification beyond checking the packet type and |
michael@0 | 501 | * key frame bits, so it should not be used for bitstream identification; use |
michael@0 | 502 | * th_decode_headerin() for that. |
michael@0 | 503 | * As per the Theora specification, an empty (0-byte) packet is treated as a |
michael@0 | 504 | * delta frame (with no coded blocks). |
michael@0 | 505 | * \param _op An <tt>ogg_packet</tt> containing encoded Theora data. |
michael@0 | 506 | * \retval 1 The packet contains a key frame. |
michael@0 | 507 | * \retval 0 The packet contains a delta frame. |
michael@0 | 508 | * \retval -1 The packet is not a video data packet.*/ |
michael@0 | 509 | extern int th_packet_iskeyframe(ogg_packet *_op); |
michael@0 | 510 | /*@}*/ |
michael@0 | 511 | |
michael@0 | 512 | |
michael@0 | 513 | /**\name Functions for manipulating header data*/ |
michael@0 | 514 | /*@{*/ |
michael@0 | 515 | /**Initializes a th_info structure. |
michael@0 | 516 | * This should be called on a freshly allocated #th_info structure before |
michael@0 | 517 | * attempting to use it. |
michael@0 | 518 | * \param _info The #th_info struct to initialize.*/ |
michael@0 | 519 | extern void th_info_init(th_info *_info); |
michael@0 | 520 | /**Clears a #th_info structure. |
michael@0 | 521 | * This should be called on a #th_info structure after it is no longer |
michael@0 | 522 | * needed. |
michael@0 | 523 | * \param _info The #th_info struct to clear.*/ |
michael@0 | 524 | extern void th_info_clear(th_info *_info); |
michael@0 | 525 | |
michael@0 | 526 | /**Initialize a #th_comment structure. |
michael@0 | 527 | * This should be called on a freshly allocated #th_comment structure |
michael@0 | 528 | * before attempting to use it. |
michael@0 | 529 | * \param _tc The #th_comment struct to initialize.*/ |
michael@0 | 530 | extern void th_comment_init(th_comment *_tc); |
michael@0 | 531 | /**Add a comment to an initialized #th_comment structure. |
michael@0 | 532 | * \note Neither th_comment_add() nor th_comment_add_tag() support |
michael@0 | 533 | * comments containing null values, although the bitstream format does |
michael@0 | 534 | * support them. |
michael@0 | 535 | * To add such comments you will need to manipulate the #th_comment |
michael@0 | 536 | * structure directly. |
michael@0 | 537 | * \param _tc The #th_comment struct to add the comment to. |
michael@0 | 538 | * \param _comment Must be a null-terminated UTF-8 string containing the |
michael@0 | 539 | * comment in "TAG=the value" form.*/ |
michael@0 | 540 | extern void th_comment_add(th_comment *_tc, char *_comment); |
michael@0 | 541 | /**Add a comment to an initialized #th_comment structure. |
michael@0 | 542 | * \note Neither th_comment_add() nor th_comment_add_tag() support |
michael@0 | 543 | * comments containing null values, although the bitstream format does |
michael@0 | 544 | * support them. |
michael@0 | 545 | * To add such comments you will need to manipulate the #th_comment |
michael@0 | 546 | * structure directly. |
michael@0 | 547 | * \param _tc The #th_comment struct to add the comment to. |
michael@0 | 548 | * \param _tag A null-terminated string containing the tag associated with |
michael@0 | 549 | * the comment. |
michael@0 | 550 | * \param _val The corresponding value as a null-terminated string.*/ |
michael@0 | 551 | extern void th_comment_add_tag(th_comment *_tc,char *_tag,char *_val); |
michael@0 | 552 | /**Look up a comment value by its tag. |
michael@0 | 553 | * \param _tc An initialized #th_comment structure. |
michael@0 | 554 | * \param _tag The tag to look up. |
michael@0 | 555 | * \param _count The instance of the tag. |
michael@0 | 556 | * The same tag can appear multiple times, each with a distinct |
michael@0 | 557 | * value, so an index is required to retrieve them all. |
michael@0 | 558 | * The order in which these values appear is significant and |
michael@0 | 559 | * should be preserved. |
michael@0 | 560 | * Use th_comment_query_count() to get the legal range for |
michael@0 | 561 | * the \a _count parameter. |
michael@0 | 562 | * \return A pointer to the queried tag's value. |
michael@0 | 563 | * This points directly to data in the #th_comment structure. |
michael@0 | 564 | * It should not be modified or freed by the application, and |
michael@0 | 565 | * modifications to the structure may invalidate the pointer. |
michael@0 | 566 | * \retval NULL If no matching tag is found.*/ |
michael@0 | 567 | extern char *th_comment_query(th_comment *_tc,char *_tag,int _count); |
michael@0 | 568 | /**Look up the number of instances of a tag. |
michael@0 | 569 | * Call this first when querying for a specific tag and then iterate over the |
michael@0 | 570 | * number of instances with separate calls to th_comment_query() to |
michael@0 | 571 | * retrieve all the values for that tag in order. |
michael@0 | 572 | * \param _tc An initialized #th_comment structure. |
michael@0 | 573 | * \param _tag The tag to look up. |
michael@0 | 574 | * \return The number on instances of this particular tag.*/ |
michael@0 | 575 | extern int th_comment_query_count(th_comment *_tc,char *_tag); |
michael@0 | 576 | /**Clears a #th_comment structure. |
michael@0 | 577 | * This should be called on a #th_comment structure after it is no longer |
michael@0 | 578 | * needed. |
michael@0 | 579 | * It will free all memory used by the structure members. |
michael@0 | 580 | * \param _tc The #th_comment struct to clear.*/ |
michael@0 | 581 | extern void th_comment_clear(th_comment *_tc); |
michael@0 | 582 | /*@}*/ |
michael@0 | 583 | /*@}*/ |
michael@0 | 584 | |
michael@0 | 585 | |
michael@0 | 586 | |
michael@0 | 587 | #if defined(__cplusplus) |
michael@0 | 588 | } |
michael@0 | 589 | #endif |
michael@0 | 590 | |
michael@0 | 591 | #endif |