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);