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));
109 memcpy(h->
stats, buf, bytes);
124 #ifdef TH_ENCCTL_2PASS_IN
137 bytes = th_encode_ctl(h->
t_state, TH_ENCCTL_2PASS_IN,
158 th_comment t_comment;
162 uint32_t gop_size = avc_context->
gop_size;
166 th_info_init(&t_info);
169 t_info.pic_width = avc_context->
width;
170 t_info.pic_height = avc_context->
height;
181 t_info.aspect_numerator = 1;
182 t_info.aspect_denominator = 1;
186 t_info.colorspace = TH_CS_ITU_REC_470M;
188 t_info.colorspace = TH_CS_ITU_REC_470BG;
190 t_info.colorspace = TH_CS_UNSPECIFIED;
193 t_info.pixel_fmt = TH_PF_420;
195 t_info.pixel_fmt = TH_PF_422;
197 t_info.pixel_fmt = TH_PF_444;
211 t_info.target_bitrate = 0;
213 t_info.target_bitrate = avc_context->
bit_rate;
218 h->
t_state = th_encode_alloc(&t_info);
226 th_info_clear(&t_info);
228 if (th_encode_ctl(h->
t_state, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
229 &gop_size,
sizeof(gop_size))) {
236 if ((ret =
get_stats(avc_context, 0)) < 0)
253 th_comment_init(&t_comment);
255 while (th_encode_flushheader(h->
t_state, &t_comment, &o_packet))
259 th_comment_clear(&t_comment);
270 th_ycbcr_buffer t_yuv_buffer;
277 th_encode_packetout(h->
t_state, 1, &o_packet);
279 if ((ret =
get_stats(avc_context, 1)) < 0)
285 for (i = 0; i < 3; i++) {
288 t_yuv_buffer[i].stride = frame->
linesize[i];
289 t_yuv_buffer[i].data = frame->
data[i];
297 result = th_encode_ycbcr_in(h->
t_state, t_yuv_buffer);
302 message =
"differing frame sizes";
305 message =
"encoder is not ready or is finished";
308 message =
"unknown reason";
311 av_log(avc_context,
AV_LOG_ERROR,
"theora_encode_YUVin failed (%s) [%d]\n", message, result);
316 if ((ret =
get_stats(avc_context, 0)) < 0)
320 result = th_encode_packetout(h->
t_state, 0, &o_packet);
336 memcpy(pkt->
data, o_packet.packet, o_packet.bytes);