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;