43 #include <theora/theoraenc.h>
60 const char* message =
NULL;
64 if (packet->bytes < 0) {
65 message =
"ogg_packet has negative size";
66 }
else if (packet->bytes > 0xffff) {
67 message =
"ogg_packet is larger than 65535 bytes";
68 }
else if (newsize < avc_context->extradata_size) {
69 message =
"extradata_size would overflow";
73 message =
"av_realloc failed";
84 memcpy(avc_context->
extradata + (*offset), packet->packet, packet->bytes);
85 (*offset) += packet->bytes;
91 #ifdef TH_ENCCTL_2PASS_OUT
96 bytes = th_encode_ctl(h->
t_state, TH_ENCCTL_2PASS_OUT, &buf,
sizeof(buf));
112 memcpy(h->
stats, buf, bytes);
127 #ifdef TH_ENCCTL_2PASS_IN
144 bytes = th_encode_ctl(h->
t_state, TH_ENCCTL_2PASS_IN,
165 th_comment t_comment;
169 uint32_t gop_size = avc_context->
gop_size;
173 th_info_init(&t_info);
176 t_info.pic_width = avc_context->
width;
177 t_info.pic_height = avc_context->
height;
188 t_info.aspect_numerator = 1;
189 t_info.aspect_denominator = 1;
193 t_info.colorspace = TH_CS_ITU_REC_470M;
195 t_info.colorspace = TH_CS_ITU_REC_470BG;
197 t_info.colorspace = TH_CS_UNSPECIFIED;
200 t_info.pixel_fmt = TH_PF_420;
202 t_info.pixel_fmt = TH_PF_422;
204 t_info.pixel_fmt = TH_PF_444;
218 t_info.target_bitrate = 0;
220 t_info.target_bitrate = avc_context->
bit_rate;
225 h->
t_state = th_encode_alloc(&t_info);
233 th_info_clear(&t_info);
235 if (th_encode_ctl(h->
t_state, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
236 &gop_size,
sizeof(gop_size))) {
243 if ((ret =
get_stats(avc_context, 0)) < 0)
260 th_comment_init(&t_comment);
262 while (th_encode_flushheader(h->
t_state, &t_comment, &o_packet))
266 th_comment_clear(&t_comment);
277 th_ycbcr_buffer t_yuv_buffer;
284 th_encode_packetout(h->
t_state, 1, &o_packet);
286 if ((ret =
get_stats(avc_context, 1)) < 0)
292 for (i = 0; i < 3; i++) {
295 t_yuv_buffer[i].stride = frame->
linesize[i];
296 t_yuv_buffer[i].data = frame->
data[i];
304 result = th_encode_ycbcr_in(h->
t_state, t_yuv_buffer);
309 message =
"differing frame sizes";
312 message =
"encoder is not ready or is finished";
315 message =
"unknown reason";
318 av_log(avc_context,
AV_LOG_ERROR,
"theora_encode_YUVin failed (%s) [%d]\n", message, result);
323 if ((ret =
get_stats(avc_context, 0)) < 0)
327 result = th_encode_packetout(h->
t_state, 0, &o_packet);
343 memcpy(pkt->
data, o_packet.packet, o_packet.bytes);