libtheora  1.1.1
Defines | Variables
theoraenc.h File Reference

The libtheoraenc C encoding API. More...

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

Go to the source code of this file.

Defines

#define _O_THEORA_THEORAENC_H_   (1)
th_encode_ctl() codes

These are the available request codes for th_encode_ctl(). By convention, these are even, to distinguish them from the decoder control codes. Keep any experimental or vendor-specific values above 0x8000.

#define TH_ENCCTL_SET_HUFFMAN_CODES   (0)
 Sets the Huffman tables to use.
#define TH_ENCCTL_SET_QUANT_PARAMS   (2)
 Sets the quantization parameters to use.
#define TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE   (4)
 Sets the maximum distance between key frames.
#define TH_ENCCTL_SET_VP3_COMPATIBLE   (10)
 Disables any encoder features that would prevent lossless transcoding back to VP3.
#define TH_ENCCTL_GET_SPLEVEL_MAX   (12)
 Gets the maximum speed level.
#define TH_ENCCTL_SET_SPLEVEL   (14)
 Sets the speed level.
#define TH_ENCCTL_GET_SPLEVEL   (16)
 Gets the current speed level.
#define TH_ENCCTL_SET_DUP_COUNT   (18)
 Sets the number of duplicates of the next frame to produce.
#define TH_ENCCTL_SET_RATE_FLAGS   (20)
 Modifies the default bitrate management behavior.
#define TH_ENCCTL_SET_RATE_BUFFER   (22)
 Sets the size of the bitrate management bit reservoir as a function of number of frames.
#define TH_ENCCTL_2PASS_OUT   (24)
 Enable pass 1 of two-pass encoding mode and retrieve the first pass metrics.
#define TH_ENCCTL_2PASS_IN   (26)
 Submits two-pass encoding metric data collected the first encoding pass to the second pass.
#define TH_ENCCTL_SET_QUALITY   (28)
 Sets the current encoding quality.
#define TH_ENCCTL_SET_BITRATE   (30)
 Sets the current encoding bitrate.
TH_ENCCTL_SET_RATE_FLAGS flags

These are the flags available for use with TH_ENCCTL_SET_RATE_FLAGS.

#define TH_RATECTL_DROP_FRAMES   (0x1)
 Drop frames to keep within bitrate buffer constraints.
#define TH_RATECTL_CAP_OVERFLOW   (0x2)
 Ignore bitrate buffer overflows.
#define TH_RATECTL_CAP_UNDERFLOW   (0x4)
 Ignore bitrate buffer underflows.

Typedefs

Encoder state

The following data structure is opaque, and its contents are not publicly defined by this API.

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

typedef struct th_enc_ctx th_enc_ctx
 The encoder context.

Functions

Functions for encoding

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

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

th_enc_ctxth_encode_alloc (const th_info *_info)
 Allocates an encoder instance.
int th_encode_ctl (th_enc_ctx *_enc, int _req, void *_buf, size_t _buf_sz)
 Encoder control function.
int th_encode_flushheader (th_enc_ctx *_enc, th_comment *_comments, ogg_packet *_op)
 Outputs the next header packet.
int th_encode_ycbcr_in (th_enc_ctx *_enc, th_ycbcr_buffer _ycbcr)
 Submits an uncompressed frame to the encoder.
int th_encode_packetout (th_enc_ctx *_enc, int _last, ogg_packet *_op)
 Retrieves encoded video data packets.
void th_encode_free (th_enc_ctx *_enc)
 Frees an allocated encoder instance.

Variables

const th_quant_info TH_VP31_QUANT_INFO
 The quantization parameters used by VP3.
const th_huff_code TH_VP31_HUFF_CODES [TH_NHUFFMAN_TABLES][TH_NDCT_TOKENS]
 The Huffman tables used by VP3.

Detailed Description

The libtheoraenc C encoding API.


Define Documentation

#define _O_THEORA_THEORAENC_H_   (1)
#define TH_ENCCTL_2PASS_IN   (26)

Submits two-pass encoding metric data collected the first encoding pass to the second pass.

The first call must be made before the first frame is encoded, and a target bitrate must have already been specified to the encoder. It sets the encoder to pass 2 mode implicitly; this cannot be disabled. The encoder may require reading data from some or all of the frames in advance, depending on, e.g., the reservoir size used in the second pass. You must call this function repeatedly before each frame to provide data until either a) it fails to consume all of the data presented or b) all of the pass 1 data has been consumed. In the first case, you must save the remaining data to be presented after the next frame. You can call this function with a NULL argument to get an upper bound on the number of bytes that will be required before the next frame.

When pass 2 is first enabled, the default bit reservoir is set to the entire file; this gives maximum flexibility but can lead to very high peak rates. You can subsequently set it to another value with TH_ENCCTL_SET_RATE_BUFFER (e.g., to set it to the keyframe interval for non-live streaming), however, you may then need to provide more data before the next frame.

Parameters:
[in]_bufchar[]: A buffer containing the data returned by TH_ENCCTL_2PASS_OUT in pass 1. You may pass NULL for _buf to return an upper bound on the number of additional bytes needed before the next frame. The summary data returned at the end of pass 1 must be at the head of the buffer on the first call with a non-NULL _buf, and the placeholder data returned at the start of pass 1 should be omitted. After each call you should advance this buffer by the number of bytes consumed.
Return values:
>0The number of bytes of metric data required/consumed.
0No more data is required before the next frame.
TH_EFAULT_enc_ctx is NULL.
TH_EINVALNo target bitrate has been set, or the first call was made after the first frame was submitted for encoding.
TH_ENOTFORMATThe data did not appear to be pass 1 from a compatible implementation of this library.
TH_EBADHEADERThe data was invalid; this may be returned when attempting to read an aborted pass 1 file that still has the placeholder data in place of the summary data.
TH_EIMPLNot supported by this implementation.
#define TH_ENCCTL_2PASS_OUT   (24)

Enable pass 1 of two-pass encoding mode and retrieve the first pass metrics.

Pass 1 mode must be enabled before the first frame is encoded, and a target bitrate must have already been specified to the encoder. Although this does not have to be the exact rate that will be used in the second pass, closer values may produce better results. The first call returns the size of the two-pass header data, along with some placeholder content, and sets the encoder into pass 1 mode implicitly. This call sets the encoder to pass 1 mode implicitly. Then, a subsequent call must be made after each call to th_encode_ycbcr_in() to retrieve the metrics for that frame. An additional, final call must be made to retrieve the summary data, containing such information as the total number of frames, etc. This must be stored in place of the placeholder data that was returned in the first call, before the frame metrics data. All of this data must be presented back to the encoder during pass 2 using TH_ENCCTL_2PASS_IN.

Parameters:
[out]<tt>char*_buf: Returns a pointer to internal storage containing the two pass metrics data. This storage is only valid until the next call, or until the encoder context is freed, and must be copied by the application.
Return values:
>=0The number of bytes of metric data available in the returned buffer.
TH_EFAULT_enc_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(char *), no target bitrate has been set, or the first call was made after the first frame was submitted for encoding.
TH_EIMPLNot supported by this implementation.
#define TH_ENCCTL_GET_SPLEVEL   (16)

Gets the current speed level.

The default speed level may vary according to encoder implementation, but if this control code is not supported (it returns TH_EIMPL), the default may be assumed to be the slowest available speed (0). The maximum encoding speed level may be implementation- and encoding mode-specific, and can be obtained via TH_ENCCTL_GET_SPLEVEL_MAX.

Parameters:
[out]_bufint: The current encoding speed level. 0 is slowest, larger values use less CPU.
Return values:
TH_EFAULT_enc_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(int).
TH_EIMPLNot supported by this implementation in the current encoding mode.
#define TH_ENCCTL_GET_SPLEVEL_MAX   (12)

Gets the maximum speed level.

Higher speed levels favor quicker encoding over better quality per bit. Depending on the encoding mode, and the internal algorithms used, quality may actually improve, but in this case bitrate will also likely increase. In any case, overall rate/distortion performance will probably decrease. The maximum value, and the meaning of each value, may change depending on the current encoding mode (VBR vs. constant quality, etc.).

Parameters:
[out]_bufint: The maximum encoding speed level.
Return values:
TH_EFAULT_enc_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(int).
TH_EIMPLNot supported by this implementation in the current encoding mode.
#define TH_ENCCTL_SET_BITRATE   (30)

Sets the current encoding bitrate.

Once a bitrate is set, the encoder must use a rate-controlled mode for all future frames (this restriction may be relaxed in a future version). If it is set before the headers are emitted, the target bitrate encoded in them will be updated. Due to the buffer delay, the exact bitrate of each section of the encode is not guaranteed. The encoder may have already used more bits than allowed for the frames it has encoded, expecting to make them up in future frames, or it may have used fewer, holding the excess in reserve. The exact transition between the two bitrates is not well-defined by this API, but may be affected by flags set with TH_ENCCTL_SET_RATE_FLAGS. After a number of frames equal to the buffer delay, one may expect further output to average at the target bitrate.

Parameters:
[in]_buflong: The new target bitrate, in bits per second.
Return values:
0Success.
TH_EFAULT_enc_ctx or _buf is NULL.
TH_EINVALThe target bitrate was not positive.
TH_EIMPLNot supported by this implementation.
#define TH_ENCCTL_SET_DUP_COUNT   (18)

Sets the number of duplicates of the next frame to produce.

Although libtheora can encode duplicate frames very cheaply, it costs some amount of CPU to detect them, and a run of duplicates cannot span a keyframe boundary. This control code tells the encoder to produce the specified number of extra duplicates of the next frame. This allows the encoder to make smarter keyframe placement decisions and rate control decisions, and reduces CPU usage as well, when compared to just submitting the same frame for encoding multiple times. This setting only applies to the next frame submitted for encoding. You MUST call th_encode_packetout() repeatedly until it returns 0, or the extra duplicate frames will be lost.

Parameters:
[in]_bufint: The number of duplicates to produce. If this is negative or zero, no duplicates will be produced.
Return values:
TH_EFAULT_enc_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(int), or the number of duplicates is greater than or equal to the maximum keyframe interval. In the latter case, NO duplicate frames will be produced. You must ensure that the maximum keyframe interval is set larger than the maximum number of duplicates you will ever wish to insert prior to encoding.
TH_EIMPLNot supported by this implementation in the current encoding mode.
#define TH_ENCCTL_SET_HUFFMAN_CODES   (0)

Sets the Huffman tables to use.

The tables are copied, not stored by reference, so they can be freed after this call. NULL may be specified to revert to the default tables.

Parameters:
[in]_bufth_huff_code[TH_NHUFFMAN_TABLES][TH_NDCT_TOKENS]
Return values:
TH_EFAULT_enc_ctx is NULL.
TH_EINVALEncoding has already begun or one or more of the given tables is not full or prefix-free, _buf is NULL and _buf_sz is not zero, or _buf is non-NULL and _buf_sz is not sizeof(th_huff_code)*TH_NHUFFMAN_TABLES*TH_NDCT_TOKENS.
TH_EIMPLNot supported by this implementation.

Sets the maximum distance between key frames.

This can be changed during an encode, but will be bounded by 1<<th_info::keyframe_granule_shift. If it is set before encoding begins, th_info::keyframe_granule_shift will be enlarged appropriately.

Parameters:
[in]_bufogg_uint32_t: The maximum distance between key frames.
[out]_bufogg_uint32_t: The actual maximum distance set.
Return values:
TH_EFAULT_enc_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(ogg_uint32_t).
TH_EIMPLNot supported by this implementation.
#define TH_ENCCTL_SET_QUALITY   (28)

Sets the current encoding quality.

This is only valid so long as no bitrate has been specified, either through the th_info struct used to initialize the encoder or through TH_ENCCTL_SET_BITRATE (this restriction may be relaxed in a future version). If it is set before the headers are emitted, the target quality encoded in them will be updated.

Parameters:
[in]_bufint: The new target quality, in the range 0...63, inclusive.
Return values:
0Success.
TH_EFAULT_enc_ctx or _buf is NULL.
TH_EINVALA target bitrate has already been specified, or the quality index was not in the range 0...63.
TH_EIMPLNot supported by this implementation.
#define TH_ENCCTL_SET_QUANT_PARAMS   (2)

Sets the quantization parameters to use.

The parameters are copied, not stored by reference, so they can be freed after this call. NULL may be specified to revert to the default parameters.

Parameters:
[in]_bufth_quant_info
Return values:
TH_EFAULT_enc_ctx is NULL.
TH_EINVALEncoding has already begun, _buf is NULL and _buf_sz is not zero, or _buf is non-NULL and _buf_sz is not sizeof(th_quant_info).
TH_EIMPLNot supported by this implementation.
#define TH_ENCCTL_SET_RATE_BUFFER   (22)

Sets the size of the bitrate management bit reservoir as a function of number of frames.

The reservoir size affects how quickly bitrate management reacts to instantaneous changes in the video complexity. Larger reservoirs react more slowly, and provide better overall quality, but require more buffering by a client, adding more latency to live streams. By default, libtheora sets the reservoir to the maximum distance between keyframes, subject to a minimum and maximum limit. This call may be used to increase or decrease the reservoir, increasing or decreasing the allowed temporary variance in bitrate. An implementation may impose some limits on the size of a reservoir it can handle, in which case the actual reservoir size may not be exactly what was requested. The actual value set will be returned.

Parameters:
[in]_bufint: Requested size of the reservoir measured in frames.
[out]_bufint: The actual size of the reservoir set.
Return values:
TH_EFAULT_enc_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(int), or rate control is not enabled. The buffer has an implementation defined minimum and maximum size and the value in _buf will be adjusted to match the actual value set.
TH_EIMPLNot supported by this implementation in the current encoding mode.
#define TH_ENCCTL_SET_RATE_FLAGS   (20)

Modifies the default bitrate management behavior.

Use to allow or disallow frame dropping, and to enable or disable capping bit reservoir overflows and underflows. See the list of available flags. The flags are set by default to TH_RATECTL_DROP_FRAMES|TH_RATECTL_CAP_OVERFLOW.

Parameters:
[in]_bufint: Any combination of the available flags:
Return values:
TH_EFAULT_enc_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(int) or rate control is not enabled.
TH_EIMPLNot supported by this implementation in the current encoding mode.
#define TH_ENCCTL_SET_SPLEVEL   (14)

Sets the speed level.

The current speed level may be retrieved using TH_ENCCTL_GET_SPLEVEL.

Parameters:
[in]_bufint: The new encoding speed level. 0 is slowest, larger values use less CPU.
Return values:
TH_EFAULT_enc_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(int), or the encoding speed level is out of bounds. The maximum encoding speed level may be implementation- and encoding mode-specific, and can be obtained via TH_ENCCTL_GET_SPLEVEL_MAX.
TH_EIMPLNot supported by this implementation in the current encoding mode.
#define TH_ENCCTL_SET_VP3_COMPATIBLE   (10)

Disables any encoder features that would prevent lossless transcoding back to VP3.

This primarily means disabling block-adaptive quantization and always coding all four luma blocks in a macro block when 4MV is used. It also includes using the VP3 quantization tables and Huffman codes; if you set them explicitly after calling this function, the resulting stream will not be VP3-compatible. If you enable VP3-compatibility when encoding 4:2:2 or 4:4:4 source material, or when using a picture region smaller than the full frame (e.g. a non-multiple-of-16 width or height), then non-VP3 bitstream features will still be disabled, but the stream will still not be VP3-compatible, as VP3 was not capable of encoding such formats. If you call this after encoding has already begun, then the quantization tables and codebooks cannot be changed, but the frame-level features will be enabled or disabled as requested.

Parameters:
[in]_bufint: a non-zero value to enable VP3 compatibility, or 0 to disable it (the default).
[out]_bufint: 1 if all bitstream features required for VP3-compatibility could be set, and 0 otherwise. The latter will be returned if the pixel format is not 4:2:0, the picture region is smaller than the full frame, or if encoding has begun, preventing the quantization tables and codebooks from being set.
Return values:
TH_EFAULT_enc_ctx or _buf is NULL.
TH_EINVAL_buf_sz is not sizeof(int).
TH_EIMPLNot supported by this implementation.
#define TH_RATECTL_CAP_OVERFLOW   (0x2)

Ignore bitrate buffer overflows.

If the encoder uses so few bits that the reservoir of available bits overflows, ignore the excess. The encoder will not try to use these extra bits in future frames. At high rates this may cause the result to be undersized, but allows a client to play the stream using a finite buffer; it should normally be enabled.

#define TH_RATECTL_CAP_UNDERFLOW   (0x4)

Ignore bitrate buffer underflows.

If the encoder uses so many bits that the reservoir of available bits underflows, ignore the deficit. The encoder will not try to make up these extra bits in future frames. At low rates this may cause the result to be oversized; it should normally be disabled.

#define TH_RATECTL_DROP_FRAMES   (0x1)

Drop frames to keep within bitrate buffer constraints.

This can have a severe impact on quality, but is the only way to ensure that bitrate targets are met at low rates during sudden bursts of activity.


Typedef Documentation

typedef struct th_enc_ctx th_enc_ctx

The encoder context.


Variable Documentation

The Huffman tables used by VP3.

The quantization parameters used by VP3.