libtheoraenc
C encoding API. More...
#include <stddef.h>
#include <ogg/ogg.h>
#include "codec.h"
Go to the source code of this file.
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. | |
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 th_enc_ctx | th_enc_ctx |
The encoder context. | |
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_ctx * | th_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. | |
Defines | |
#define | _O_THEORA_THEORAENC_H_ (1) |
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. |
libtheoraenc
C encoding API.
|
|
|
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.
|
|
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.
|
|
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.
|
|
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.).
|
|
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.
|
|
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.
|
|
Sets the Huffman tables to use.
The tables are copied, not stored by reference, so they can be freed after this call.
|
|
Sets the maximum distance between key frames.
This can be changed during an encode, but will be bounded by
|
|
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.
|
|
Sets the quantization parameters to use.
The parameters are copied, not stored by reference, so they can be freed after this call.
|
|
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.
|
|
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
|
|
Sets the speed level. The current speed level may be retrieved using TH_ENCCTL_GET_SPLEVEL.
|
|
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.
|
|
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. |
|
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. |
|
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. |
|
The encoder context.
|
|
Allocates an encoder instance.
|
|
Encoder control function. This is used to provide advanced control the encoding process.
|
|
Outputs the next header packet. This should be called repeatedly after encoder initialization until it returns 0 in order to get all of the header packets, in order, before encoding actual video data.
|
|
Frees an allocated encoder instance.
|
|
Retrieves encoded video data packets. This should be called repeatedly after each frame is submitted to flush any encoded packets, until it returns 0. The encoder will not buffer these packets as subsequent frames are compressed, so a failure to do so will result in lost video data.
|
|
Submits an uncompressed frame to the encoder.
|
|
The Huffman tables used by VP3.
|
|
The quantization parameters used by VP3.
|