yading@10
|
1 /*
|
yading@10
|
2 * Copyright (c) 2006 Paul Richards <paul.richards@gmail.com>
|
yading@10
|
3 *
|
yading@10
|
4 * This file is part of FFmpeg.
|
yading@10
|
5 *
|
yading@10
|
6 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
8 * License as published by the Free Software Foundation; either
|
yading@10
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
10 *
|
yading@10
|
11 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
14 * Lesser General Public License for more details.
|
yading@10
|
15 *
|
yading@10
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
17 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
19 */
|
yading@10
|
20
|
yading@10
|
21 /**
|
yading@10
|
22 * @file
|
yading@10
|
23 * @brief Theora encoder using libtheora.
|
yading@10
|
24 * @author Paul Richards <paul.richards@gmail.com>
|
yading@10
|
25 *
|
yading@10
|
26 * A lot of this is copy / paste from other output codecs in
|
yading@10
|
27 * libavcodec or pure guesswork (or both).
|
yading@10
|
28 *
|
yading@10
|
29 * I have used t_ prefixes on variables which are libtheora types
|
yading@10
|
30 * and o_ prefixes on variables which are libogg types.
|
yading@10
|
31 */
|
yading@10
|
32
|
yading@10
|
33 /* FFmpeg includes */
|
yading@10
|
34 #include "libavutil/common.h"
|
yading@10
|
35 #include "libavutil/intreadwrite.h"
|
yading@10
|
36 #include "libavutil/pixdesc.h"
|
yading@10
|
37 #include "libavutil/log.h"
|
yading@10
|
38 #include "libavutil/base64.h"
|
yading@10
|
39 #include "avcodec.h"
|
yading@10
|
40 #include "internal.h"
|
yading@10
|
41
|
yading@10
|
42 /* libtheora includes */
|
yading@10
|
43 #include <theora/theoraenc.h>
|
yading@10
|
44
|
yading@10
|
45 typedef struct TheoraContext {
|
yading@10
|
46 th_enc_ctx *t_state;
|
yading@10
|
47 uint8_t *stats;
|
yading@10
|
48 int stats_size;
|
yading@10
|
49 int stats_offset;
|
yading@10
|
50 int uv_hshift;
|
yading@10
|
51 int uv_vshift;
|
yading@10
|
52 int keyframe_mask;
|
yading@10
|
53 } TheoraContext;
|
yading@10
|
54
|
yading@10
|
55 /** Concatenate an ogg_packet into the extradata. */
|
yading@10
|
56 static int concatenate_packet(unsigned int* offset,
|
yading@10
|
57 AVCodecContext* avc_context,
|
yading@10
|
58 const ogg_packet* packet)
|
yading@10
|
59 {
|
yading@10
|
60 const char* message = NULL;
|
yading@10
|
61 uint8_t* newdata = NULL;
|
yading@10
|
62 int newsize = avc_context->extradata_size + 2 + packet->bytes;
|
yading@10
|
63 int ret;
|
yading@10
|
64
|
yading@10
|
65 if (packet->bytes < 0) {
|
yading@10
|
66 message = "ogg_packet has negative size";
|
yading@10
|
67 ret = AVERROR_INVALIDDATA;
|
yading@10
|
68 } else if (packet->bytes > 0xffff) {
|
yading@10
|
69 message = "ogg_packet is larger than 65535 bytes";
|
yading@10
|
70 ret = AVERROR_INVALIDDATA;
|
yading@10
|
71 } else if (newsize < avc_context->extradata_size) {
|
yading@10
|
72 message = "extradata_size would overflow";
|
yading@10
|
73 ret = AVERROR_INVALIDDATA;
|
yading@10
|
74 } else {
|
yading@10
|
75 newdata = av_realloc(avc_context->extradata, newsize);
|
yading@10
|
76 if (!newdata)
|
yading@10
|
77 message = "av_realloc failed";
|
yading@10
|
78 ret = AVERROR(ENOMEM);
|
yading@10
|
79 }
|
yading@10
|
80 if (message) {
|
yading@10
|
81 av_log(avc_context, AV_LOG_ERROR, "concatenate_packet failed: %s\n", message);
|
yading@10
|
82 return ret;
|
yading@10
|
83 }
|
yading@10
|
84
|
yading@10
|
85 avc_context->extradata = newdata;
|
yading@10
|
86 avc_context->extradata_size = newsize;
|
yading@10
|
87 AV_WB16(avc_context->extradata + (*offset), packet->bytes);
|
yading@10
|
88 *offset += 2;
|
yading@10
|
89 memcpy(avc_context->extradata + (*offset), packet->packet, packet->bytes);
|
yading@10
|
90 (*offset) += packet->bytes;
|
yading@10
|
91 return 0;
|
yading@10
|
92 }
|
yading@10
|
93
|
yading@10
|
94 static int get_stats(AVCodecContext *avctx, int eos)
|
yading@10
|
95 {
|
yading@10
|
96 #ifdef TH_ENCCTL_2PASS_OUT
|
yading@10
|
97 TheoraContext *h = avctx->priv_data;
|
yading@10
|
98 uint8_t *buf;
|
yading@10
|
99 int bytes;
|
yading@10
|
100
|
yading@10
|
101 bytes = th_encode_ctl(h->t_state, TH_ENCCTL_2PASS_OUT, &buf, sizeof(buf));
|
yading@10
|
102 if (bytes < 0) {
|
yading@10
|
103 av_log(avctx, AV_LOG_ERROR, "Error getting first pass stats\n");
|
yading@10
|
104 return AVERROR_EXTERNAL;
|
yading@10
|
105 }
|
yading@10
|
106 if (!eos) {
|
yading@10
|
107 h->stats = av_fast_realloc(h->stats, &h->stats_size,
|
yading@10
|
108 h->stats_offset + bytes);
|
yading@10
|
109 memcpy(h->stats + h->stats_offset, buf, bytes);
|
yading@10
|
110 h->stats_offset += bytes;
|
yading@10
|
111 } else {
|
yading@10
|
112 int b64_size = AV_BASE64_SIZE(h->stats_offset);
|
yading@10
|
113 // libtheora generates a summary header at the end
|
yading@10
|
114 memcpy(h->stats, buf, bytes);
|
yading@10
|
115 avctx->stats_out = av_malloc(b64_size);
|
yading@10
|
116 av_base64_encode(avctx->stats_out, b64_size, h->stats, h->stats_offset);
|
yading@10
|
117 }
|
yading@10
|
118 return 0;
|
yading@10
|
119 #else
|
yading@10
|
120 av_log(avctx, AV_LOG_ERROR, "libtheora too old to support 2pass\n");
|
yading@10
|
121 return AVERROR(ENOSUP);
|
yading@10
|
122 #endif
|
yading@10
|
123 }
|
yading@10
|
124
|
yading@10
|
125 // libtheora won't read the entire buffer we give it at once, so we have to
|
yading@10
|
126 // repeatedly submit it...
|
yading@10
|
127 static int submit_stats(AVCodecContext *avctx)
|
yading@10
|
128 {
|
yading@10
|
129 #ifdef TH_ENCCTL_2PASS_IN
|
yading@10
|
130 TheoraContext *h = avctx->priv_data;
|
yading@10
|
131 int bytes;
|
yading@10
|
132 if (!h->stats) {
|
yading@10
|
133 if (!avctx->stats_in) {
|
yading@10
|
134 av_log(avctx, AV_LOG_ERROR, "No statsfile for second pass\n");
|
yading@10
|
135 return AVERROR(EINVAL);
|
yading@10
|
136 }
|
yading@10
|
137 h->stats_size = strlen(avctx->stats_in) * 3/4;
|
yading@10
|
138 h->stats = av_malloc(h->stats_size);
|
yading@10
|
139 h->stats_size = av_base64_decode(h->stats, avctx->stats_in, h->stats_size);
|
yading@10
|
140 }
|
yading@10
|
141 while (h->stats_size - h->stats_offset > 0) {
|
yading@10
|
142 bytes = th_encode_ctl(h->t_state, TH_ENCCTL_2PASS_IN,
|
yading@10
|
143 h->stats + h->stats_offset,
|
yading@10
|
144 h->stats_size - h->stats_offset);
|
yading@10
|
145 if (bytes < 0) {
|
yading@10
|
146 av_log(avctx, AV_LOG_ERROR, "Error submitting stats\n");
|
yading@10
|
147 return AVERROR_EXTERNAL;
|
yading@10
|
148 }
|
yading@10
|
149 if (!bytes)
|
yading@10
|
150 return 0;
|
yading@10
|
151 h->stats_offset += bytes;
|
yading@10
|
152 }
|
yading@10
|
153 return 0;
|
yading@10
|
154 #else
|
yading@10
|
155 av_log(avctx, AV_LOG_ERROR, "libtheora too old to support 2pass\n");
|
yading@10
|
156 return AVERROR(ENOSUP);
|
yading@10
|
157 #endif
|
yading@10
|
158 }
|
yading@10
|
159
|
yading@10
|
160 static av_cold int encode_init(AVCodecContext* avc_context)
|
yading@10
|
161 {
|
yading@10
|
162 th_info t_info;
|
yading@10
|
163 th_comment t_comment;
|
yading@10
|
164 ogg_packet o_packet;
|
yading@10
|
165 unsigned int offset;
|
yading@10
|
166 TheoraContext *h = avc_context->priv_data;
|
yading@10
|
167 uint32_t gop_size = avc_context->gop_size;
|
yading@10
|
168 int ret;
|
yading@10
|
169
|
yading@10
|
170 /* Set up the theora_info struct */
|
yading@10
|
171 th_info_init(&t_info);
|
yading@10
|
172 t_info.frame_width = FFALIGN(avc_context->width, 16);
|
yading@10
|
173 t_info.frame_height = FFALIGN(avc_context->height, 16);
|
yading@10
|
174 t_info.pic_width = avc_context->width;
|
yading@10
|
175 t_info.pic_height = avc_context->height;
|
yading@10
|
176 t_info.pic_x = 0;
|
yading@10
|
177 t_info.pic_y = 0;
|
yading@10
|
178 /* Swap numerator and denominator as time_base in AVCodecContext gives the
|
yading@10
|
179 * time period between frames, but theora_info needs the framerate. */
|
yading@10
|
180 t_info.fps_numerator = avc_context->time_base.den;
|
yading@10
|
181 t_info.fps_denominator = avc_context->time_base.num;
|
yading@10
|
182 if (avc_context->sample_aspect_ratio.num) {
|
yading@10
|
183 t_info.aspect_numerator = avc_context->sample_aspect_ratio.num;
|
yading@10
|
184 t_info.aspect_denominator = avc_context->sample_aspect_ratio.den;
|
yading@10
|
185 } else {
|
yading@10
|
186 t_info.aspect_numerator = 1;
|
yading@10
|
187 t_info.aspect_denominator = 1;
|
yading@10
|
188 }
|
yading@10
|
189
|
yading@10
|
190 if (avc_context->color_primaries == AVCOL_PRI_BT470M)
|
yading@10
|
191 t_info.colorspace = TH_CS_ITU_REC_470M;
|
yading@10
|
192 else if (avc_context->color_primaries == AVCOL_PRI_BT470BG)
|
yading@10
|
193 t_info.colorspace = TH_CS_ITU_REC_470BG;
|
yading@10
|
194 else
|
yading@10
|
195 t_info.colorspace = TH_CS_UNSPECIFIED;
|
yading@10
|
196
|
yading@10
|
197 if (avc_context->pix_fmt == AV_PIX_FMT_YUV420P)
|
yading@10
|
198 t_info.pixel_fmt = TH_PF_420;
|
yading@10
|
199 else if (avc_context->pix_fmt == AV_PIX_FMT_YUV422P)
|
yading@10
|
200 t_info.pixel_fmt = TH_PF_422;
|
yading@10
|
201 else if (avc_context->pix_fmt == AV_PIX_FMT_YUV444P)
|
yading@10
|
202 t_info.pixel_fmt = TH_PF_444;
|
yading@10
|
203 else {
|
yading@10
|
204 av_log(avc_context, AV_LOG_ERROR, "Unsupported pix_fmt\n");
|
yading@10
|
205 return AVERROR(EINVAL);
|
yading@10
|
206 }
|
yading@10
|
207 avcodec_get_chroma_sub_sample(avc_context->pix_fmt, &h->uv_hshift, &h->uv_vshift);
|
yading@10
|
208
|
yading@10
|
209 if (avc_context->flags & CODEC_FLAG_QSCALE) {
|
yading@10
|
210 /* Clip global_quality in QP units to the [0 - 10] range
|
yading@10
|
211 to be consistent with the libvorbis implementation.
|
yading@10
|
212 Theora accepts a quality parameter which is an int value in
|
yading@10
|
213 the [0 - 63] range.
|
yading@10
|
214 */
|
yading@10
|
215 t_info.quality = av_clipf(avc_context->global_quality / (float)FF_QP2LAMBDA, 0, 10) * 6.3;
|
yading@10
|
216 t_info.target_bitrate = 0;
|
yading@10
|
217 } else {
|
yading@10
|
218 t_info.target_bitrate = avc_context->bit_rate;
|
yading@10
|
219 t_info.quality = 0;
|
yading@10
|
220 }
|
yading@10
|
221
|
yading@10
|
222 /* Now initialise libtheora */
|
yading@10
|
223 h->t_state = th_encode_alloc(&t_info);
|
yading@10
|
224 if (!h->t_state) {
|
yading@10
|
225 av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n");
|
yading@10
|
226 return AVERROR_EXTERNAL;
|
yading@10
|
227 }
|
yading@10
|
228
|
yading@10
|
229 h->keyframe_mask = (1 << t_info.keyframe_granule_shift) - 1;
|
yading@10
|
230 /* Clear up theora_info struct */
|
yading@10
|
231 th_info_clear(&t_info);
|
yading@10
|
232
|
yading@10
|
233 if (th_encode_ctl(h->t_state, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
|
yading@10
|
234 &gop_size, sizeof(gop_size))) {
|
yading@10
|
235 av_log(avc_context, AV_LOG_ERROR, "Error setting GOP size\n");
|
yading@10
|
236 return AVERROR_EXTERNAL;
|
yading@10
|
237 }
|
yading@10
|
238
|
yading@10
|
239 // need to enable 2 pass (via TH_ENCCTL_2PASS_) before encoding headers
|
yading@10
|
240 if (avc_context->flags & CODEC_FLAG_PASS1) {
|
yading@10
|
241 if ((ret = get_stats(avc_context, 0)) < 0)
|
yading@10
|
242 return ret;
|
yading@10
|
243 } else if (avc_context->flags & CODEC_FLAG_PASS2) {
|
yading@10
|
244 if ((ret = submit_stats(avc_context)) < 0)
|
yading@10
|
245 return ret;
|
yading@10
|
246 }
|
yading@10
|
247
|
yading@10
|
248 /*
|
yading@10
|
249 Output first header packet consisting of theora
|
yading@10
|
250 header, comment, and tables.
|
yading@10
|
251
|
yading@10
|
252 Each one is prefixed with a 16bit size, then they
|
yading@10
|
253 are concatenated together into libavcodec's extradata.
|
yading@10
|
254 */
|
yading@10
|
255 offset = 0;
|
yading@10
|
256
|
yading@10
|
257 /* Headers */
|
yading@10
|
258 th_comment_init(&t_comment);
|
yading@10
|
259
|
yading@10
|
260 while (th_encode_flushheader(h->t_state, &t_comment, &o_packet))
|
yading@10
|
261 if ((ret = concatenate_packet(&offset, avc_context, &o_packet)) < 0)
|
yading@10
|
262 return ret;
|
yading@10
|
263
|
yading@10
|
264 th_comment_clear(&t_comment);
|
yading@10
|
265
|
yading@10
|
266 /* Set up the output AVFrame */
|
yading@10
|
267 avc_context->coded_frame= avcodec_alloc_frame();
|
yading@10
|
268
|
yading@10
|
269 return 0;
|
yading@10
|
270 }
|
yading@10
|
271
|
yading@10
|
272 static int encode_frame(AVCodecContext* avc_context, AVPacket *pkt,
|
yading@10
|
273 const AVFrame *frame, int *got_packet)
|
yading@10
|
274 {
|
yading@10
|
275 th_ycbcr_buffer t_yuv_buffer;
|
yading@10
|
276 TheoraContext *h = avc_context->priv_data;
|
yading@10
|
277 ogg_packet o_packet;
|
yading@10
|
278 int result, i, ret;
|
yading@10
|
279
|
yading@10
|
280 // EOS, finish and get 1st pass stats if applicable
|
yading@10
|
281 if (!frame) {
|
yading@10
|
282 th_encode_packetout(h->t_state, 1, &o_packet);
|
yading@10
|
283 if (avc_context->flags & CODEC_FLAG_PASS1)
|
yading@10
|
284 if ((ret = get_stats(avc_context, 1)) < 0)
|
yading@10
|
285 return ret;
|
yading@10
|
286 return 0;
|
yading@10
|
287 }
|
yading@10
|
288
|
yading@10
|
289 /* Copy planes to the theora yuv_buffer */
|
yading@10
|
290 for (i = 0; i < 3; i++) {
|
yading@10
|
291 t_yuv_buffer[i].width = FFALIGN(avc_context->width, 16) >> (i && h->uv_hshift);
|
yading@10
|
292 t_yuv_buffer[i].height = FFALIGN(avc_context->height, 16) >> (i && h->uv_vshift);
|
yading@10
|
293 t_yuv_buffer[i].stride = frame->linesize[i];
|
yading@10
|
294 t_yuv_buffer[i].data = frame->data[i];
|
yading@10
|
295 }
|
yading@10
|
296
|
yading@10
|
297 if (avc_context->flags & CODEC_FLAG_PASS2)
|
yading@10
|
298 if ((ret = submit_stats(avc_context)) < 0)
|
yading@10
|
299 return ret;
|
yading@10
|
300
|
yading@10
|
301 /* Now call into theora_encode_YUVin */
|
yading@10
|
302 result = th_encode_ycbcr_in(h->t_state, t_yuv_buffer);
|
yading@10
|
303 if (result) {
|
yading@10
|
304 const char* message;
|
yading@10
|
305 switch (result) {
|
yading@10
|
306 case -1:
|
yading@10
|
307 message = "differing frame sizes";
|
yading@10
|
308 break;
|
yading@10
|
309 case TH_EINVAL:
|
yading@10
|
310 message = "encoder is not ready or is finished";
|
yading@10
|
311 break;
|
yading@10
|
312 default:
|
yading@10
|
313 message = "unknown reason";
|
yading@10
|
314 break;
|
yading@10
|
315 }
|
yading@10
|
316 av_log(avc_context, AV_LOG_ERROR, "theora_encode_YUVin failed (%s) [%d]\n", message, result);
|
yading@10
|
317 return AVERROR_EXTERNAL;
|
yading@10
|
318 }
|
yading@10
|
319
|
yading@10
|
320 if (avc_context->flags & CODEC_FLAG_PASS1)
|
yading@10
|
321 if ((ret = get_stats(avc_context, 0)) < 0)
|
yading@10
|
322 return ret;
|
yading@10
|
323
|
yading@10
|
324 /* Pick up returned ogg_packet */
|
yading@10
|
325 result = th_encode_packetout(h->t_state, 0, &o_packet);
|
yading@10
|
326 switch (result) {
|
yading@10
|
327 case 0:
|
yading@10
|
328 /* No packet is ready */
|
yading@10
|
329 return 0;
|
yading@10
|
330 case 1:
|
yading@10
|
331 /* Success, we have a packet */
|
yading@10
|
332 break;
|
yading@10
|
333 default:
|
yading@10
|
334 av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed [%d]\n", result);
|
yading@10
|
335 return AVERROR_EXTERNAL;
|
yading@10
|
336 }
|
yading@10
|
337
|
yading@10
|
338 /* Copy ogg_packet content out to buffer */
|
yading@10
|
339 if ((ret = ff_alloc_packet2(avc_context, pkt, o_packet.bytes)) < 0)
|
yading@10
|
340 return ret;
|
yading@10
|
341 memcpy(pkt->data, o_packet.packet, o_packet.bytes);
|
yading@10
|
342
|
yading@10
|
343 // HACK: assumes no encoder delay, this is true until libtheora becomes
|
yading@10
|
344 // multithreaded (which will be disabled unless explicitly requested)
|
yading@10
|
345 pkt->pts = pkt->dts = frame->pts;
|
yading@10
|
346 avc_context->coded_frame->key_frame = !(o_packet.granulepos & h->keyframe_mask);
|
yading@10
|
347 if (avc_context->coded_frame->key_frame)
|
yading@10
|
348 pkt->flags |= AV_PKT_FLAG_KEY;
|
yading@10
|
349 *got_packet = 1;
|
yading@10
|
350
|
yading@10
|
351 return 0;
|
yading@10
|
352 }
|
yading@10
|
353
|
yading@10
|
354 static av_cold int encode_close(AVCodecContext* avc_context)
|
yading@10
|
355 {
|
yading@10
|
356 TheoraContext *h = avc_context->priv_data;
|
yading@10
|
357
|
yading@10
|
358 th_encode_free(h->t_state);
|
yading@10
|
359 av_freep(&h->stats);
|
yading@10
|
360 av_freep(&avc_context->coded_frame);
|
yading@10
|
361 av_freep(&avc_context->stats_out);
|
yading@10
|
362 av_freep(&avc_context->extradata);
|
yading@10
|
363 avc_context->extradata_size = 0;
|
yading@10
|
364
|
yading@10
|
365 return 0;
|
yading@10
|
366 }
|
yading@10
|
367
|
yading@10
|
368 /** AVCodec struct exposed to libavcodec */
|
yading@10
|
369 AVCodec ff_libtheora_encoder = {
|
yading@10
|
370 .name = "libtheora",
|
yading@10
|
371 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
372 .id = AV_CODEC_ID_THEORA,
|
yading@10
|
373 .priv_data_size = sizeof(TheoraContext),
|
yading@10
|
374 .init = encode_init,
|
yading@10
|
375 .close = encode_close,
|
yading@10
|
376 .encode2 = encode_frame,
|
yading@10
|
377 .capabilities = CODEC_CAP_DELAY, // needed to get the statsfile summary
|
yading@10
|
378 .pix_fmts = (const enum AVPixelFormat[]){
|
yading@10
|
379 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_NONE
|
yading@10
|
380 },
|
yading@10
|
381 .long_name = NULL_IF_CONFIG_SMALL("libtheora Theora"),
|
yading@10
|
382 };
|