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);
129 #ifdef TH_ENCCTL_2PASS_IN
146 bytes = th_encode_ctl(h->
t_state, TH_ENCCTL_2PASS_IN,
167 th_comment t_comment;
171 uint32_t gop_size = avc_context->
gop_size;
175 th_info_init(&t_info);
178 t_info.pic_width = avc_context->
width;
179 t_info.pic_height = avc_context->
height;
190 t_info.aspect_numerator = 1;
191 t_info.aspect_denominator = 1;
195 t_info.colorspace = TH_CS_ITU_REC_470M;
197 t_info.colorspace = TH_CS_ITU_REC_470BG;
199 t_info.colorspace = TH_CS_UNSPECIFIED;
202 t_info.pixel_fmt = TH_PF_420;
204 t_info.pixel_fmt = TH_PF_422;
206 t_info.pixel_fmt = TH_PF_444;
220 t_info.target_bitrate = 0;
222 t_info.target_bitrate = avc_context->
bit_rate;
227 h->
t_state = th_encode_alloc(&t_info);
235 th_info_clear(&t_info);
237 if (th_encode_ctl(h->
t_state, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
238 &gop_size,
sizeof(gop_size))) {
245 if ((ret =
get_stats(avc_context, 0)) < 0)
262 th_comment_init(&t_comment);
264 while (th_encode_flushheader(h->
t_state, &t_comment, &o_packet))
268 th_comment_clear(&t_comment);
279 th_ycbcr_buffer t_yuv_buffer;
286 th_encode_packetout(h->
t_state, 1, &o_packet);
288 if ((ret =
get_stats(avc_context, 1)) < 0)
294 for (i = 0; i < 3; i++) {
297 t_yuv_buffer[i].stride = frame->
linesize[i];
298 t_yuv_buffer[i].data = frame->
data[i];
306 result = th_encode_ycbcr_in(h->
t_state, t_yuv_buffer);
311 message =
"differing frame sizes";
314 message =
"encoder is not ready or is finished";
317 message =
"unknown reason";
320 av_log(avc_context,
AV_LOG_ERROR,
"theora_encode_YUVin failed (%s) [%d]\n", message, result);
325 if ((ret =
get_stats(avc_context, 0)) < 0)
329 result = th_encode_packetout(h->
t_state, 0, &o_packet);
345 memcpy(pkt->
data, o_packet.packet, o_packet.bytes);