46#include <theora/theoraenc.h>
66 .class_name =
"libtheora",
81 if (packet->bytes < 0) {
82 message =
"ogg_packet has negative size";
83 }
else if (packet->bytes > 0xffff) {
84 message =
"ogg_packet is larger than 65535 bytes";
85 }
else if (newsize < avc_context->extradata_size) {
86 message =
"extradata_size would overflow";
101 memcpy(avc_context->
extradata + (*
offset), packet->packet, packet->bytes);
102 (*offset) += packet->bytes;
108#ifdef TH_ENCCTL_2PASS_OUT
113 bytes = th_encode_ctl(
h->t_state, TH_ENCCTL_2PASS_OUT, &buf,
sizeof(buf));
120 h->stats_offset + bytes);
124 memcpy(
h->stats +
h->stats_offset, buf, bytes);
125 h->stats_offset += bytes;
129 memcpy(
h->stats, buf, bytes);
146#ifdef TH_ENCCTL_2PASS_IN
154 h->stats_size = strlen(avctx->
stats_in) * 3/4;
162 while (
h->stats_size -
h->stats_offset > 0) {
163 bytes = th_encode_ctl(
h->t_state, TH_ENCCTL_2PASS_IN,
164 h->stats +
h->stats_offset,
165 h->stats_size -
h->stats_offset);
172 h->stats_offset += bytes;
184 th_comment t_comment;
192 th_info_init(&t_info);
195 t_info.pic_width = avc_context->
width;
196 t_info.pic_height = avc_context->
height;
207 t_info.aspect_numerator = 1;
208 t_info.aspect_denominator = 1;
212 t_info.colorspace = TH_CS_ITU_REC_470M;
214 t_info.colorspace = TH_CS_ITU_REC_470BG;
216 t_info.colorspace = TH_CS_UNSPECIFIED;
219 t_info.pixel_fmt = TH_PF_420;
221 t_info.pixel_fmt = TH_PF_422;
223 t_info.pixel_fmt = TH_PF_444;
239 t_info.target_bitrate = 0;
241 t_info.target_bitrate = avc_context->
bit_rate;
246 h->t_state = th_encode_alloc(&t_info);
254 th_info_clear(&t_info);
256 if (th_encode_ctl(
h->t_state, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
263 if (
h->speed_level != -1) {
265 int speed_level =
h->speed_level;
266 th_encode_ctl(
h->t_state, TH_ENCCTL_GET_SPLEVEL_MAX, &max_speed_level,
sizeof(max_speed_level));
267 speed_level =
FFMIN(speed_level, max_speed_level);
268 th_encode_ctl(
h->t_state, TH_ENCCTL_SET_SPLEVEL, &speed_level,
sizeof(speed_level));
273 if ((ret =
get_stats(avc_context, 0)) < 0)
290 th_comment_init(&t_comment);
292 while (th_encode_flushheader(
h->t_state, &t_comment, &o_packet))
296 th_comment_clear(&t_comment);
304 th_ycbcr_buffer t_yuv_buffer;
311 th_encode_packetout(
h->t_state, 1, &o_packet);
313 if ((ret =
get_stats(avc_context, 1)) < 0)
319 for (
i = 0;
i < 3;
i++) {
320 t_yuv_buffer[
i].width =
FFALIGN(avc_context->
width, 16) >> (
i &&
h->uv_hshift);
321 t_yuv_buffer[
i].height =
FFALIGN(avc_context->
height, 16) >> (
i &&
h->uv_vshift);
322 t_yuv_buffer[
i].stride =
frame->linesize[
i];
323 t_yuv_buffer[
i].data =
frame->data[
i];
331 result = th_encode_ycbcr_in(
h->t_state, t_yuv_buffer);
336 message =
"differing frame sizes";
339 message =
"encoder is not ready or is finished";
350 if ((ret =
get_stats(avc_context, 0)) < 0)
354 result = th_encode_packetout(
h->t_state, 0, &o_packet);
370 memcpy(
pkt->data, o_packet.packet, o_packet.bytes);
381 if (!(o_packet.granulepos &
h->keyframe_mask))
392 th_encode_free(
h->t_state);
402 .p.name =
"libtheora",
418 .p.wrapper_name =
"libtheora",
const FFCodec ff_libtheora_encoder
AVCodec struct exposed to libavcodec.
static av_cold void close(AVCodecParserContext *s)
static av_cold int encode_init(AVCodecContext *avctx)
Libavcodec external API header.
#define i(width, name, range_min, range_max)
#define CODEC_PIXFMTS(...)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
common internal and external API header
static av_cold int encode_close(AVCodecContext *avctx)
int(* init)(AVBSFContext *ctx)
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
int ff_encode_reordered_opaque(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame)
Propagate user opaque values from the frame to avctx/pkt as needed.
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame, AVPacket *pkt)
#define AV_OPT_FLAG_VIDEO_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
@ AV_OPT_TYPE_INT
Underlying C type is int.
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes to a null-terminated string.
int av_base64_decode(uint8_t *out, const char *in_str, int out_size)
Decode a base64-encoded string.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define AVERROR_EXTERNAL
Generic error in an external library.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
#define LIBAVUTIL_VERSION_INT
static av_cold int encode_init(AVCodecContext *avc_context)
static const AVClass theora_class
static int concatenate_packet(unsigned int *offset, AVCodecContext *avc_context, const ogg_packet *packet)
Concatenate an ogg_packet into the extradata.
static int encode_frame(AVCodecContext *avc_context, AVPacket *pkt, const AVFrame *frame, int *got_packet)
static av_cold int encode_close(AVCodecContext *avc_context)
static int submit_stats(AVCodecContext *avctx)
static int get_stats(AVCodecContext *avctx, int eos)
Memory handling functions.
static int ogg_packet(AVFormatContext *s, int *sid, int *dstart, int *dsize, int64_t *fpos)
find the next Ogg packet
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
@ AVCOL_PRI_BT470M
also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Describe the class of an AVClass context structure.
main external API structure.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
char * stats_out
pass1 encoding statistics output buffer
int global_quality
Global quality for codecs which cannot change it per frame.
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
int64_t bit_rate
the average bitrate
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
AVClass * av_class
class for AVOptions