libtheora 1.1.1
Data Structures | Macros
theoradec.h File Reference

The libtheoradec C decoding API. More...

#include <stddef.h>
#include <ogg/ogg.h>
#include "codec.h"

Go to the source code of this file.

Data Structures

struct  th_stripe_callback
 The striped decode callback data to pass to TH_DECCTL_SET_STRIPE_CB. More...
 

Macros

#define _O_THEORA_THEORADEC_H_   (1)
 

Typedefs

Decoder state

The following data structures are opaque, and their contents are not publicly defined by this API.

Referring to their internals directly is unsupported, and may break without warning.

typedef struct th_dec_ctx th_dec_ctx
 The decoder context.
 
typedef struct th_setup_info th_setup_info
 Setup information.
 

Functions

Functions for decoding

You must link to libtheoradec if you use any of the functions in this section.

The functions are listed in the order they are used in a typical decode. The basic steps are:

int th_decode_headerin (th_info *_info, th_comment *_tc, th_setup_info **_setup, ogg_packet *_op)
 Decodes the header packets of a Theora stream.
 
th_dec_ctxth_decode_alloc (const th_info *_info, const th_setup_info *_setup)
 Allocates a decoder instance.
 
void th_setup_free (th_setup_info *_setup)
 Releases all storage used for the decoder setup information.
 
int th_decode_ctl (th_dec_ctx *_dec, int _req, void *_buf, size_t _buf_sz)
 Decoder control function.
 
int th_decode_packetin (th_dec_ctx *_dec, const ogg_packet *_op, ogg_int64_t *_granpos)
 Submits a packet containing encoded video data to the decoder.
 
int th_decode_ycbcr_out (th_dec_ctx *_dec, th_ycbcr_buffer _ycbcr)
 Outputs the next available frame of decoded Y'CbCr data.
 
void th_decode_free (th_dec_ctx *_dec)
 Frees an allocated decoder instance.
 

th_decode_ctl() codes

These are the available request codes for th_decode_ctl().

By convention, these are odd, to distinguish them from the encoder control codes. Keep any experimental or vendor-specific values above 0x8000.

#define TH_DECCTL_GET_PPLEVEL_MAX   (1)
 Gets the maximum post-processing level.
 
#define TH_DECCTL_SET_PPLEVEL   (3)
 Sets the post-processing level.
 
#define TH_DECCTL_SET_GRANPOS   (5)
 Sets the granule position.
 
#define TH_DECCTL_SET_STRIPE_CB   (7)
 Sets the striped decode callback function.
 
#define TH_DECCTL_SET_TELEMETRY_MBMODE   (9)
 Enables telemetry and sets the macroblock display mode.
 
#define TH_DECCTL_SET_TELEMETRY_MV   (11)
 Enables telemetry and sets the motion vector display mode.
 
#define TH_DECCTL_SET_TELEMETRY_QI   (13)
 Enables telemetry and sets the adaptive quantization display mode.
 
#define TH_DECCTL_SET_TELEMETRY_BITS   (15)
 Enables telemetry and sets the bitstream breakdown visualization mode.
 
typedef void(* th_stripe_decoded_func) (void *_ctx, th_ycbcr_buffer _buf, int _yfrag0, int _yfrag_end)
 A callback function for striped decode.
 

Detailed Description

The libtheoradec C decoding API.

Macro Definition Documentation

◆ _O_THEORA_THEORADEC_H_

#define _O_THEORA_THEORADEC_H_   (1)

◆ TH_DECCTL_GET_PPLEVEL_MAX

#define TH_DECCTL_GET_PPLEVEL_MAX   (1)

Gets the maximum post-processing level.

The decoder supports a post-processing filter that can improve the appearance of the decoded images. This returns the highest level setting for this post-processor, corresponding to maximum improvement and computational expense.

Parameters
[out]_bufint: The maximum post-processing level.
Return values
TH_EFAULT_dec_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(int).
TH_EIMPLNot supported by this implementation.

◆ TH_DECCTL_SET_GRANPOS

#define TH_DECCTL_SET_GRANPOS   (5)

Sets the granule position.

Call this after a seek, before decoding the first frame, to ensure that the proper granule position is returned for all subsequent frames. If you track timestamps yourself and do not use the granule position returned by the decoder, then you need not call this function.

Parameters
[in]_bufogg_int64_t: The granule position of the next frame.
Return values
TH_EFAULT_dec_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(ogg_int64_t), or the granule position is negative.

◆ TH_DECCTL_SET_PPLEVEL

#define TH_DECCTL_SET_PPLEVEL   (3)

Sets the post-processing level.

By default, post-processing is disabled.

Sets the level of post-processing to use when decoding the compressed stream. This must be a value between zero (off) and the maximum returned by TH_DECCTL_GET_PPLEVEL_MAX.

Parameters
[in]_bufint: The new post-processing level. 0 to disable; larger values use more CPU.
Return values
TH_EFAULT_dec_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(int), or the post-processing level is out of bounds. The maximum post-processing level may be implementation-specific, and can be obtained via TH_DECCTL_GET_PPLEVEL_MAX.
TH_EIMPLNot supported by this implementation.

◆ TH_DECCTL_SET_STRIPE_CB

#define TH_DECCTL_SET_STRIPE_CB   (7)

Sets the striped decode callback function.

If set, this function will be called as each piece of a frame is fully decoded in th_decode_packetin(). You can pass in a th_stripe_callback with th_stripe_callback::stripe_decoded set to NULL to disable the callbacks at any point. Enabling striped decode does not prevent you from calling th_decode_ycbcr_out() after the frame is fully decoded.

Parameters
[in]_bufth_stripe_callback: The callback parameters.
Return values
TH_EFAULT_dec_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(th_stripe_callback).

◆ TH_DECCTL_SET_TELEMETRY_BITS

#define TH_DECCTL_SET_TELEMETRY_BITS   (15)

Enables telemetry and sets the bitstream breakdown visualization mode.

◆ TH_DECCTL_SET_TELEMETRY_MBMODE

#define TH_DECCTL_SET_TELEMETRY_MBMODE   (9)

Enables telemetry and sets the macroblock display mode.

◆ TH_DECCTL_SET_TELEMETRY_MV

#define TH_DECCTL_SET_TELEMETRY_MV   (11)

Enables telemetry and sets the motion vector display mode.

◆ TH_DECCTL_SET_TELEMETRY_QI

#define TH_DECCTL_SET_TELEMETRY_QI   (13)

Enables telemetry and sets the adaptive quantization display mode.

Typedef Documentation

◆ th_dec_ctx

typedef struct th_dec_ctx th_dec_ctx

The decoder context.

◆ th_setup_info

typedef struct th_setup_info th_setup_info

Setup information.

This contains auxiliary information (Huffman tables and quantization parameters) decoded from the setup header by th_decode_headerin() to be passed to th_decode_alloc(). It can be re-used to initialize any number of decoders, and can be freed via th_setup_free() at any time.

◆ th_stripe_decoded_func

typedef void(* th_stripe_decoded_func) (void *_ctx, th_ycbcr_buffer _buf, int _yfrag0, int _yfrag_end)

A callback function for striped decode.

This is a function pointer to an application-provided function that will be called each time a section of the image is fully decoded in th_decode_packetin(). This allows the application to process the section immediately, while it is still in cache. Note that the frame is decoded bottom to top, so _yfrag0 will steadily decrease with each call until it reaches 0, at which point the full frame is decoded. The number of fragment rows made available in each call depends on the pixel format and the number of post-processing filters enabled, and may not even be constant for the entire frame. If a non-NULL _granpos pointer is passed to th_decode_packetin(), the granule position for the frame will be stored in it before the first callback is made. If an entire frame is dropped (a 0-byte packet), then no callbacks will be made at all for that frame.

Parameters
_ctxAn application-provided context pointer.
_bufThe image buffer for the decoded frame.
_yfrag0The Y coordinate of the first row of 8x8 fragments decoded. Multiply this by 8 to obtain the pixel row number in the luma plane. If the chroma planes are subsampled in the Y direction, this will always be divisible by two.
_yfrag_endThe Y coordinate of the first row of 8x8 fragments past the newly decoded section. If the chroma planes are subsampled in the Y direction, this will always be divisible by two. I.e., this section contains fragment rows _yfrag0 ..._yfrag_end -1.

Function Documentation

◆ th_decode_alloc()

th_dec_ctx * th_decode_alloc ( const th_info _info,
const th_setup_info _setup 
)

Allocates a decoder instance.

Security Warning: The Theora format supports very large frame sizes, potentially even larger than the address space of a 32-bit machine, and creating a decoder context allocates the space for several frames of data. If the allocation fails here, your program will crash, possibly at some future point because the OS kernel returned a valid memory range and will only fail when it tries to map the pages in it the first time they are used. Even if it succeeds, you may experience a denial of service if the frame size is large enough to cause excessive paging. If you are integrating libtheora in a larger application where such things are undesirable, it is highly recommended that you check the frame size in _info before calling this function and refuse to decode streams where it is larger than some reasonable maximum. libtheora will not check this for you, because there may be machines that can handle such streams and applications that wish to.

Parameters
_infoA th_info struct filled via th_decode_headerin().
_setupA th_setup_info handle returned via th_decode_headerin().
Returns
The initialized th_dec_ctx handle.
Return values
NULLIf the decoding parameters were invalid.

◆ th_decode_ctl()

int th_decode_ctl ( th_dec_ctx _dec,
int  _req,
void *  _buf,
size_t  _buf_sz 
)

Decoder control function.

This is used to provide advanced control of the decoding process.

Parameters
_decA th_dec_ctx handle.
_reqThe control code to process. See the list of available control codes for details.
_bufThe parameters for this control code.
_buf_szThe size of the parameter buffer.

◆ th_decode_free()

void th_decode_free ( th_dec_ctx _dec)

Frees an allocated decoder instance.

Parameters
_decA th_dec_ctx handle.

◆ th_decode_headerin()

int th_decode_headerin ( th_info _info,
th_comment _tc,
th_setup_info **  _setup,
ogg_packet *  _op 
)

Decodes the header packets of a Theora stream.

This should be called on the initial packets of the stream, in succession, until it returns 0, indicating that all headers have been processed, or an error is encountered. At least three header packets are required, and additional optional header packets may follow. This can be used on the first packet of any logical stream to determine if that stream is a Theora stream.

Parameters
_infoA th_info structure to fill in. This must have been previously initialized with th_info_init(). The application may immediately begin using the contents of this structure after the first header is decoded, though it must continue to be passed in on all subsequent calls.
_tcA th_comment structure to fill in. The application may immediately begin using the contents of this structure after the second header is decoded, though it must continue to be passed in on all subsequent calls.
_setupReturns a pointer to additional, private setup information needed by the decoder. The contents of this pointer must be initialized to NULL on the first call, and the returned value must continue to be passed in on all subsequent calls.
_opAn ogg_packet structure which contains one of the initial packets of an Ogg logical stream.
Returns
A positive value indicates that a Theora header was successfully processed.
Return values
0The first video data packet was encountered after all required header packets were parsed. The packet just passed in on this call should be saved and fed to th_decode_packetin() to begin decoding video data.
TH_EFAULTOne of _info, _tc, or _setup was NULL.
TH_EBADHEADER_op was NULL, the packet was not the next header packet in the expected sequence, or the format of the header data was invalid.
TH_EVERSIONThe packet data was a Theora info header, but for a bitstream version not decodable with this version of libtheoradec.
TH_ENOTFORMATThe packet was not a Theora header.

◆ th_decode_packetin()

int th_decode_packetin ( th_dec_ctx _dec,
const ogg_packet *  _op,
ogg_int64_t *  _granpos 
)

Submits a packet containing encoded video data to the decoder.

Parameters
_decA th_dec_ctx handle.
_opAn ogg_packet containing encoded video data.
_granposReturns the granule position of the decoded packet. If non-NULL, the granule position for this specific packet is stored in this location. This is computed incrementally from previously decoded packets. After a seek, the correct granule position must be set via TH_DECCTL_SET_GRANPOS for this to work properly.
Return values
0Success. A new decoded frame can be retrieved by calling th_decode_ycbcr_out().
TH_DUPFRAMEThe packet represented a dropped (0-byte) frame. The player can skip the call to th_decode_ycbcr_out(), as the contents of the decoded frame buffer have not changed.
TH_EFAULT_dec or _op was NULL.
TH_EBADPACKET_op does not contain encoded video data.
TH_EIMPLThe video data uses bitstream features which this library does not support.

◆ th_decode_ycbcr_out()

int th_decode_ycbcr_out ( th_dec_ctx _dec,
th_ycbcr_buffer  _ycbcr 
)

Outputs the next available frame of decoded Y'CbCr data.

If a striped decode callback has been set with TH_DECCTL_SET_STRIPE_CB, then the application does not need to call this function.

Parameters
_decA th_dec_ctx handle.
_ycbcrA video buffer structure to fill in. libtheoradec will fill in all the members of this structure, including the pointers to the uncompressed video data. The memory for this video data is owned by libtheoradec. It may be freed or overwritten without notification when subsequent frames are decoded.
Return values
0Success
TH_EFAULT_dec or _ycbcr was NULL.

◆ th_setup_free()

void th_setup_free ( th_setup_info _setup)

Releases all storage used for the decoder setup information.

This should be called after you no longer want to create any decoders for a stream whose headers you have parsed with th_decode_headerin().

Parameters
_setupThe setup information to free. This can safely be NULL.