34 #include "libavutil/common.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/log.h"
38 #include "libavutil/base64.h"
43 #include <theora/theoraenc.h>
60 const char* message = NULL;
65 if (packet->bytes < 0) {
66 message =
"ogg_packet has negative size";
68 }
else if (packet->bytes > 0xffff) {
69 message =
"ogg_packet is larger than 65535 bytes";
71 }
else if (newsize < avc_context->extradata_size) {
72 message =
"extradata_size would overflow";
77 message =
"av_realloc failed";
89 memcpy(avc_context->
extradata + (*offset), packet->packet, packet->bytes);
90 (*offset) += packet->bytes;
96 #ifdef TH_ENCCTL_2PASS_OUT
101 bytes = th_encode_ctl(h->
t_state, TH_ENCCTL_2PASS_OUT, &buf,
sizeof(buf));
114 memcpy(h->
stats, buf, bytes);
129 #ifdef TH_ENCCTL_2PASS_IN
142 bytes = th_encode_ctl(h->
t_state, TH_ENCCTL_2PASS_IN,
163 th_comment t_comment;
167 uint32_t gop_size = avc_context->
gop_size;
171 th_info_init(&t_info);
174 t_info.pic_width = avc_context->
width;
175 t_info.pic_height = avc_context->
height;
186 t_info.aspect_numerator = 1;
187 t_info.aspect_denominator = 1;
191 t_info.colorspace = TH_CS_ITU_REC_470M;
193 t_info.colorspace = TH_CS_ITU_REC_470BG;
195 t_info.colorspace = TH_CS_UNSPECIFIED;
198 t_info.pixel_fmt = TH_PF_420;
200 t_info.pixel_fmt = TH_PF_422;
202 t_info.pixel_fmt = TH_PF_444;
216 t_info.target_bitrate = 0;
218 t_info.target_bitrate = avc_context->
bit_rate;
223 h->
t_state = th_encode_alloc(&t_info);
231 th_info_clear(&t_info);
233 if (th_encode_ctl(h->
t_state, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
234 &gop_size,
sizeof(gop_size))) {
241 if ((ret =
get_stats(avc_context, 0)) < 0)
258 th_comment_init(&t_comment);
260 while (th_encode_flushheader(h->
t_state, &t_comment, &o_packet))
264 th_comment_clear(&t_comment);
275 th_ycbcr_buffer t_yuv_buffer;
282 th_encode_packetout(h->
t_state, 1, &o_packet);
284 if ((ret =
get_stats(avc_context, 1)) < 0)
290 for (i = 0; i < 3; i++) {
293 t_yuv_buffer[i].stride = frame->
linesize[i];
294 t_yuv_buffer[i].data = frame->
data[i];
302 result = th_encode_ycbcr_in(h->
t_state, t_yuv_buffer);
307 message =
"differing frame sizes";
310 message =
"encoder is not ready or is finished";
313 message =
"unknown reason";
316 av_log(avc_context,
AV_LOG_ERROR,
"theora_encode_YUVin failed (%s) [%d]\n", message, result);
321 if ((ret =
get_stats(avc_context, 0)) < 0)
325 result = th_encode_packetout(h->
t_state, 0, &o_packet);
341 memcpy(pkt->
data, o_packet.packet, o_packet.bytes);
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
This structure describes decoded (raw) audio or video data.
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
static av_cold int init(AVCodecContext *avctx)
static int concatenate_packet(unsigned int *offset, AVCodecContext *avc_context, const ogg_packet *packet)
Concatenate an ogg_packet into the extradata.
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
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 *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static const uint8_t offset[511][2]
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
const char * name
Name of the codec implementation.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define CODEC_FLAG_QSCALE
Use fixed qscale.
Libavcodec external API header.
AVPixelFormat
Pixel format.
int flags
A combination of AV_PKT_FLAG values.
static int get_stats(AVCodecContext *avctx, int eos)
int bit_rate
the average bitrate
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes to a null-terminated string.
void * av_realloc(void *ptr, size_t size) 1(2)
Allocate or reallocate a block of memory.
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
int width
picture width / height.
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static av_cold int encode_init(AVCodecContext *avc_context)
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
char * stats_out
pass1 encoding statistics output buffer
void avcodec_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.
int AC3_NAME() encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
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.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
static int ogg_packet(AVFormatContext *s, int *sid, int *dstart, int *dsize, int64_t *fpos)
find the next Ogg packet
main external API structure.
static void close(AVCodecParserContext *s)
static av_cold int encode_close(AVCodecContext *avc_context)
AVFrame * coded_frame
the picture in the bitstream
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
int global_quality
Global quality for codecs which cannot change it per frame.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
common internal api header.
#define AVERROR_INVALIDDATA
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.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
This structure stores compressed data.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...