# 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:

- Parse the header packets by repeatedly calling th_decode_headerin().
- Allocate a th_dec_ctx handle with th_decode_alloc().
- Call th_setup_free() to free any memory used for codec setup information.
- Perform any additional decoder configuration with th_decode_ctl().
- For each video data packet:
  - Submit the packet to the decoder via th_decode_packetin().
  - Retrieve the uncompressed video data via th_decode_ycbcr_out().
- Call th_decode_free() to release all decoder memory.

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_ctx * | th_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.

## Function Documentation

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:

  | _info | A th_info struct filled via th_decode_headerin().
  | _setup | A th_setup_info handle returned via th_decode_headerin().

Returns:
  The initialized th_dec_ctx handle.

Return values:

  | NULL | If the decoding parameters were invalid.

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:

  | _dec | A th_dec_ctx handle.
  | _req | The control code to process. See the list of available control codes for details.
  | _buf | The parameters for this control code.
  | _buf_sz | The size of the parameter buffer.

void th_decode_free | ( | th_dec_ctx * | _dec | ) |

Frees an allocated decoder instance.

Parameters:

  | _dec | A th_dec_ctx handle.

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:

  | _info | A 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.
  | _tc | A 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.
  | _setup | Returns 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.
  | _op | An `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:

  | 0 | The 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_EFAULT | One 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_EVERSION | The packet data was a Theora info header, but for a bitstream version not decodable with this version of `libtheoradec`.
  | TH_ENOTFORMAT | The packet was not a Theora header.

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:

  | _dec | A th_dec_ctx handle.
  | _op | An `ogg_packet` containing encoded video data.
  | _granpos | Returns 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:

  | 0 | Success. A new decoded frame can be retrieved by calling th_decode_ycbcr_out().
  | TH_DUPFRAME | The 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_EIMPL | The video data uses bitstream features which this library does not support.

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:

  | _dec | A th_dec_ctx handle.
  | _ycbcr | A 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:

  | 0 | Success
  | TH_EFAULT | _dec or _ycbcr was `NULL`.

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:

  | _setup | The setup information to free. This can safely be `NULL`.