40 #include <theora/theoraenc.h>
57 const char* message =
NULL;
58 uint8_t* newdata =
NULL;
61 if (packet->bytes < 0) {
62 message =
"ogg_packet has negative size";
63 }
else if (packet->bytes > 0xffff) {
64 message =
"ogg_packet is larger than 65535 bytes";
65 }
else if (newsize < avc_context->extradata_size) {
66 message =
"extradata_size would overflow";
70 message =
"av_realloc failed";
81 memcpy(avc_context->
extradata + (*offset), packet->packet, packet->bytes);
82 (*offset) += packet->bytes;
88 #ifdef TH_ENCCTL_2PASS_OUT
93 bytes = th_encode_ctl(h->
t_state, TH_ENCCTL_2PASS_OUT, &buf,
sizeof(buf));
106 memcpy(h->
stats, buf, bytes);
121 #ifdef TH_ENCCTL_2PASS_IN
134 bytes = th_encode_ctl(h->
t_state, TH_ENCCTL_2PASS_IN,
155 th_comment t_comment;
159 uint32_t gop_size = avc_context->
gop_size;
162 th_info_init(&t_info);
165 t_info.pic_width = avc_context->
width;
166 t_info.pic_height = avc_context->
height;
177 t_info.aspect_numerator = 1;
178 t_info.aspect_denominator = 1;
182 t_info.colorspace = TH_CS_ITU_REC_470M;
184 t_info.colorspace = TH_CS_ITU_REC_470BG;
186 t_info.colorspace = TH_CS_UNSPECIFIED;
189 t_info.pixel_fmt = TH_PF_420;
191 t_info.pixel_fmt = TH_PF_422;
193 t_info.pixel_fmt = TH_PF_444;
207 t_info.target_bitrate = 0;
209 t_info.target_bitrate = avc_context->
bit_rate;
214 h->
t_state = th_encode_alloc(&t_info);
222 th_info_clear(&t_info);
224 if (th_encode_ctl(h->
t_state, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
225 &gop_size,
sizeof(gop_size))) {
249 th_comment_init(&t_comment);
251 while (th_encode_flushheader(h->
t_state, &t_comment, &o_packet))
255 th_comment_clear(&t_comment);
264 int buf_size,
void *
data)
266 th_ycbcr_buffer t_yuv_buffer;
274 th_encode_packetout(h->
t_state, 1, &o_packet);
282 for (i = 0; i < 3; i++) {
285 t_yuv_buffer[i].stride = frame->
linesize[i];
286 t_yuv_buffer[i].data = frame->
data[i];
294 result = th_encode_ycbcr_in(h->
t_state, t_yuv_buffer);
299 message =
"differing frame sizes";
302 message =
"encoder is not ready or is finished";
305 message =
"unknown reason";
308 av_log(avc_context,
AV_LOG_ERROR,
"theora_encode_YUVin failed (%s) [%d]\n", message, result);
317 result = th_encode_packetout(h->
t_state, 0, &o_packet);
331 if (buf_size < o_packet.bytes) {
335 memcpy(outbuf, o_packet.packet, o_packet.bytes);
342 return o_packet.bytes;
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
AVFrame * coded_frame
the picture in the bitstream
static int concatenate_packet(unsigned int *offset, AVCodecContext *avc_context, const ogg_packet *packet)
Concatenate an ogg_packet into the extradata.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
static int submit_stats(AVCodecContext *avctx)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
static int encode_frame(AVCodecContext *avc_context, uint8_t *outbuf, int buf_size, void *data)
int64_t pts
presentation timestamp in time_base units (time when frame should be shown to user) If AV_NOPTS_VALUE...
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
char * stats_out
pass1 encoding statistics output buffer
static int init(AVCodecParserContext *s)
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
static int get_stats(AVCodecContext *avctx, int eos)
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
int bit_rate
the average bitrate
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes.
struct TheoraContext TheoraContext
int width
picture width / height.
static av_cold int encode_init(AVCodecContext *avc_context)
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
main external API structure.
static void close(AVCodecParserContext *s)
#define CODEC_FLAG_QSCALE
Use fixed qscale.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_cold int encode_close(AVCodecContext *avc_context)
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int global_quality
Global quality for codecs which cannot change it per frame.
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
static int ogg_packet(AVFormatContext *s, int *str, int *dstart, int *dsize, int64_t *fpos)
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
AVCodec ff_libtheora_encoder
AVCodec struct exposed to libavcodec.
int key_frame
1 -> keyframe, 0-> not
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
int av_base64_decode(uint8_t *out, const char *in, int out_size)
Decode a base64-encoded string.
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...