2007-01-25 02:16:28 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006 Paul Richards <paul.richards@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2007-07-05 13:40:25 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-01-25 02:16:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2009-01-27 00:51:16 +02:00
|
|
|
* \file libtheoraenc.c
|
2007-01-25 02:16:28 +02:00
|
|
|
* \brief Theora encoder using libtheora.
|
|
|
|
* \author Paul Richards <paul.richards@gmail.com>
|
|
|
|
*
|
|
|
|
* A lot of this is copy / paste from other output codecs in
|
|
|
|
* libavcodec or pure guesswork (or both).
|
|
|
|
*
|
|
|
|
* I have used t_ prefixes on variables which are libtheora types
|
|
|
|
* and o_ prefixes on variables which are libogg types.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* FFmpeg includes */
|
2009-01-12 01:26:52 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2008-05-09 14:56:36 +03:00
|
|
|
#include "libavutil/log.h"
|
2007-01-25 02:16:28 +02:00
|
|
|
#include "avcodec.h"
|
|
|
|
|
|
|
|
/* libtheora includes */
|
|
|
|
#include <theora/theora.h>
|
|
|
|
|
2009-10-19 18:51:34 +03:00
|
|
|
typedef struct TheoraContext {
|
2007-01-25 02:16:28 +02:00
|
|
|
theora_state t_state;
|
|
|
|
} TheoraContext;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Concatenates an ogg_packet into the extradata.
|
|
|
|
*/
|
2009-10-19 18:51:34 +03:00
|
|
|
static int concatenate_packet(unsigned int* offset,
|
|
|
|
AVCodecContext* avc_context,
|
|
|
|
const ogg_packet* packet)
|
2007-01-25 02:16:28 +02:00
|
|
|
{
|
2009-07-13 04:59:48 +03:00
|
|
|
const char* message = NULL;
|
2009-10-19 18:51:34 +03:00
|
|
|
uint8_t* newdata = NULL;
|
2007-01-25 02:16:28 +02:00
|
|
|
int newsize = avc_context->extradata_size + 2 + packet->bytes;
|
|
|
|
|
|
|
|
if (packet->bytes < 0) {
|
|
|
|
message = "ogg_packet has negative size";
|
|
|
|
} else if (packet->bytes > 0xffff) {
|
|
|
|
message = "ogg_packet is larger than 65535 bytes";
|
|
|
|
} else if (newsize < avc_context->extradata_size) {
|
|
|
|
message = "extradata_size would overflow";
|
|
|
|
} else {
|
|
|
|
newdata = av_realloc(avc_context->extradata, newsize);
|
2009-10-20 01:26:52 +03:00
|
|
|
if (!newdata)
|
2007-01-25 02:16:28 +02:00
|
|
|
message = "av_realloc failed";
|
|
|
|
}
|
2009-10-20 01:26:52 +03:00
|
|
|
if (message) {
|
2007-01-25 02:16:28 +02:00
|
|
|
av_log(avc_context, AV_LOG_ERROR, "concatenate_packet failed: %s\n", message);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-10-19 18:51:34 +03:00
|
|
|
avc_context->extradata = newdata;
|
2007-01-25 02:16:28 +02:00
|
|
|
avc_context->extradata_size = newsize;
|
2007-06-02 04:41:07 +03:00
|
|
|
AV_WB16(avc_context->extradata + (*offset), packet->bytes);
|
|
|
|
*offset += 2;
|
2009-10-19 18:51:34 +03:00
|
|
|
memcpy(avc_context->extradata + (*offset), packet->packet, packet->bytes);
|
2007-01-25 02:16:28 +02:00
|
|
|
(*offset) += packet->bytes;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:48:55 +02:00
|
|
|
static av_cold int encode_init(AVCodecContext* avc_context)
|
2007-01-25 02:16:28 +02:00
|
|
|
{
|
|
|
|
theora_info t_info;
|
|
|
|
theora_comment t_comment;
|
|
|
|
ogg_packet o_packet;
|
|
|
|
unsigned int offset;
|
|
|
|
TheoraContext *h = avc_context->priv_data;
|
|
|
|
|
|
|
|
/* Set up the theora_info struct */
|
2009-10-19 18:51:34 +03:00
|
|
|
theora_info_init(&t_info);
|
|
|
|
t_info.width = FFALIGN(avc_context->width, 16);
|
|
|
|
t_info.height = FFALIGN(avc_context->height, 16);
|
|
|
|
t_info.frame_width = avc_context->width;
|
2007-01-25 02:16:28 +02:00
|
|
|
t_info.frame_height = avc_context->height;
|
2009-10-19 18:51:34 +03:00
|
|
|
t_info.offset_x = 0;
|
|
|
|
t_info.offset_y = avc_context->height & 0xf;
|
2007-02-07 16:10:50 +02:00
|
|
|
/* Swap numerator and denominator as time_base in AVCodecContext gives the
|
|
|
|
* time period between frames, but theora_info needs the framerate. */
|
2009-10-19 18:51:34 +03:00
|
|
|
t_info.fps_numerator = avc_context->time_base.den;
|
2007-01-25 02:16:28 +02:00
|
|
|
t_info.fps_denominator = avc_context->time_base.num;
|
2009-10-20 01:26:52 +03:00
|
|
|
if (avc_context->sample_aspect_ratio.num) {
|
2009-10-19 18:51:34 +03:00
|
|
|
t_info.aspect_numerator = avc_context->sample_aspect_ratio.num;
|
2007-01-25 02:16:28 +02:00
|
|
|
t_info.aspect_denominator = avc_context->sample_aspect_ratio.den;
|
|
|
|
} else {
|
2009-10-19 18:51:34 +03:00
|
|
|
t_info.aspect_numerator = 1;
|
2007-01-25 02:16:28 +02:00
|
|
|
t_info.aspect_denominator = 1;
|
|
|
|
}
|
2009-10-19 18:51:34 +03:00
|
|
|
t_info.colorspace = OC_CS_UNSPECIFIED;
|
|
|
|
t_info.pixelformat = OC_PF_420;
|
|
|
|
t_info.keyframe_frequency = avc_context->gop_size;
|
2007-01-25 02:16:28 +02:00
|
|
|
t_info.keyframe_frequency_force = avc_context->gop_size;
|
2009-10-19 18:51:34 +03:00
|
|
|
t_info.keyframe_mindistance = avc_context->keyint_min;
|
2007-01-25 02:16:28 +02:00
|
|
|
|
2009-10-19 18:51:34 +03:00
|
|
|
t_info.quick_p = 1;
|
|
|
|
t_info.dropframes_p = 0;
|
|
|
|
t_info.keyframe_auto_p = 1;
|
2007-01-25 02:16:28 +02:00
|
|
|
t_info.keyframe_data_target_bitrate = t_info.target_bitrate * 1.5;
|
2009-10-19 18:51:34 +03:00
|
|
|
t_info.keyframe_auto_threshold = 80;
|
|
|
|
t_info.noise_sensitivity = 1;
|
|
|
|
t_info.sharpness = 0;
|
2007-01-25 02:16:28 +02:00
|
|
|
|
2009-10-01 01:25:49 +03:00
|
|
|
if (avc_context->flags & CODEC_FLAG_QSCALE) {
|
|
|
|
/* to be constant with the libvorbis implementation, clip global_quality to 0 - 10
|
|
|
|
Theora accepts a quality parameter p, which is:
|
|
|
|
* 0 <= p <=63
|
|
|
|
* an int value
|
|
|
|
*/
|
2009-10-19 18:51:34 +03:00
|
|
|
t_info.quality = av_clip(avc_context->global_quality / (float)FF_QP2LAMBDA, 0, 10) * 6.3;
|
2009-10-01 01:25:49 +03:00
|
|
|
t_info.target_bitrate = 0;
|
|
|
|
} else {
|
|
|
|
t_info.target_bitrate = avc_context->bit_rate;
|
2009-10-19 18:51:34 +03:00
|
|
|
t_info.quality = 0;
|
2009-10-01 01:25:49 +03:00
|
|
|
}
|
|
|
|
|
2007-01-25 02:16:28 +02:00
|
|
|
/* Now initialise libtheora */
|
2009-10-20 01:26:52 +03:00
|
|
|
if (theora_encode_init(&(h->t_state), &t_info)) {
|
2007-01-25 02:16:28 +02:00
|
|
|
av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear up theora_info struct */
|
2009-10-19 18:51:34 +03:00
|
|
|
theora_info_clear(&t_info);
|
2007-01-25 02:16:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Output first header packet consisting of theora
|
|
|
|
header, comment, and tables.
|
|
|
|
|
|
|
|
Each one is prefixed with a 16bit size, then they
|
|
|
|
are concatenated together into ffmpeg's extradata.
|
|
|
|
*/
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
/* Header */
|
2009-10-19 18:51:34 +03:00
|
|
|
theora_encode_header(&(h->t_state), &o_packet);
|
2009-10-20 01:26:52 +03:00
|
|
|
if (concatenate_packet(&offset, avc_context, &o_packet))
|
2007-01-25 02:16:28 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Comment */
|
2009-10-19 18:51:34 +03:00
|
|
|
theora_comment_init(&t_comment);
|
|
|
|
theora_encode_comment(&t_comment, &o_packet);
|
2009-10-20 01:26:52 +03:00
|
|
|
if (concatenate_packet(&offset, avc_context, &o_packet))
|
2007-01-25 02:16:28 +02:00
|
|
|
return -1;
|
2009-07-13 04:59:45 +03:00
|
|
|
/* Clear up theora_comment struct before we reset the packet */
|
2009-10-19 18:51:34 +03:00
|
|
|
theora_comment_clear(&t_comment);
|
2009-07-13 04:59:45 +03:00
|
|
|
/* And despite documentation to the contrary, theora_comment_clear
|
|
|
|
* does not release the packet */
|
|
|
|
ogg_packet_clear(&o_packet);
|
2007-01-25 02:16:28 +02:00
|
|
|
|
|
|
|
/* Tables */
|
2009-10-19 18:51:34 +03:00
|
|
|
theora_encode_tables(&(h->t_state), &o_packet);
|
2009-10-20 01:26:52 +03:00
|
|
|
if (concatenate_packet(&offset, avc_context, &o_packet))
|
2007-01-25 02:16:28 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Set up the output AVFrame */
|
|
|
|
avc_context->coded_frame= avcodec_alloc_frame();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-19 18:51:34 +03:00
|
|
|
static int encode_frame(AVCodecContext* avc_context, uint8_t *outbuf,
|
|
|
|
int buf_size, void *data)
|
2007-01-25 02:16:28 +02:00
|
|
|
{
|
|
|
|
yuv_buffer t_yuv_buffer;
|
|
|
|
TheoraContext *h = avc_context->priv_data;
|
|
|
|
AVFrame *frame = data;
|
|
|
|
ogg_packet o_packet;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
assert(avc_context->pix_fmt == PIX_FMT_YUV420P);
|
|
|
|
|
|
|
|
/* Copy planes to the theora yuv_buffer */
|
|
|
|
if (frame->linesize[1] != frame->linesize[2]) {
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "U and V stride differ\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-10-19 18:51:34 +03:00
|
|
|
t_yuv_buffer.y_width = FFALIGN(avc_context->width, 16);
|
|
|
|
t_yuv_buffer.y_height = FFALIGN(avc_context->height, 16);
|
|
|
|
t_yuv_buffer.y_stride = frame->linesize[0];
|
|
|
|
t_yuv_buffer.uv_width = t_yuv_buffer.y_width / 2;
|
2007-01-25 02:16:28 +02:00
|
|
|
t_yuv_buffer.uv_height = t_yuv_buffer.y_height / 2;
|
|
|
|
t_yuv_buffer.uv_stride = frame->linesize[1];
|
|
|
|
|
|
|
|
t_yuv_buffer.y = frame->data[0];
|
|
|
|
t_yuv_buffer.u = frame->data[1];
|
|
|
|
t_yuv_buffer.v = frame->data[2];
|
|
|
|
|
|
|
|
/* Now call into theora_encode_YUVin */
|
2009-10-19 18:51:34 +03:00
|
|
|
result = theora_encode_YUVin(&(h->t_state), &t_yuv_buffer);
|
2009-10-20 01:26:52 +03:00
|
|
|
if (result) {
|
2007-01-25 02:16:28 +02:00
|
|
|
const char* message;
|
|
|
|
switch (result) {
|
2009-10-19 18:51:34 +03:00
|
|
|
case -1:
|
|
|
|
message = "differing frame sizes";
|
|
|
|
break;
|
|
|
|
case OC_EINVAL:
|
|
|
|
message = "encoder is not ready or is finished";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
message = "unknown reason";
|
|
|
|
break;
|
2007-01-25 02:16:28 +02:00
|
|
|
}
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "theora_encode_YUVin failed (%s) [%d]\n", message, result);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pick up returned ogg_packet */
|
2009-10-19 18:51:34 +03:00
|
|
|
result = theora_encode_packetout(&(h->t_state), 0, &o_packet);
|
2007-01-25 02:16:28 +02:00
|
|
|
switch (result) {
|
2009-10-19 18:51:34 +03:00
|
|
|
case 0:
|
|
|
|
/* No packet is ready */
|
|
|
|
return 0;
|
|
|
|
case 1:
|
|
|
|
/* Success, we have a packet */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed [%d]\n", result);
|
|
|
|
return -1;
|
2007-01-25 02:16:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy ogg_packet content out to buffer */
|
|
|
|
if (buf_size < o_packet.bytes) {
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "encoded frame too large\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(outbuf, o_packet.packet, o_packet.bytes);
|
|
|
|
|
2009-10-10 17:43:31 +03:00
|
|
|
// HACK: does not take codec delay into account (neither does the decoder though)
|
2009-10-19 18:51:34 +03:00
|
|
|
avc_context->coded_frame->pts = frame->pts;
|
2009-10-10 17:43:31 +03:00
|
|
|
|
2007-01-25 02:16:28 +02:00
|
|
|
return o_packet.bytes;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:48:55 +02:00
|
|
|
static av_cold int encode_close(AVCodecContext* avc_context)
|
2007-01-25 02:16:28 +02:00
|
|
|
{
|
|
|
|
ogg_packet o_packet;
|
|
|
|
TheoraContext *h = avc_context->priv_data;
|
|
|
|
int result;
|
|
|
|
const char* message;
|
|
|
|
|
2009-10-19 18:51:34 +03:00
|
|
|
result = theora_encode_packetout(&(h->t_state), 1, &o_packet);
|
|
|
|
theora_clear(&(h->t_state));
|
2009-07-13 04:59:45 +03:00
|
|
|
av_freep(&avc_context->coded_frame);
|
|
|
|
av_freep(&avc_context->extradata);
|
|
|
|
avc_context->extradata_size = 0;
|
|
|
|
|
2007-01-25 02:16:28 +02:00
|
|
|
switch (result) {
|
2009-10-19 18:51:34 +03:00
|
|
|
case 0: /* No packet is ready */
|
|
|
|
case -1: /* Encoding finished */
|
|
|
|
return 0;
|
|
|
|
case 1:
|
|
|
|
/* We have a packet */
|
|
|
|
message = "gave us a packet";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
message = "unknown reason";
|
|
|
|
break;
|
2007-01-25 02:16:28 +02:00
|
|
|
}
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "theora_encode_packetout failed (%s) [%d]\n", message, result);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-05-12 01:28:43 +03:00
|
|
|
static const enum PixelFormat supported_pixel_formats[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
|
2007-01-25 02:16:28 +02:00
|
|
|
|
|
|
|
/*! AVCodec struct exposed to libavcodec */
|
2009-10-19 18:51:34 +03:00
|
|
|
AVCodec libtheora_encoder = {
|
2007-01-25 02:16:28 +02:00
|
|
|
.name = "libtheora",
|
|
|
|
.type = CODEC_TYPE_VIDEO,
|
|
|
|
.id = CODEC_ID_THEORA,
|
|
|
|
.priv_data_size = sizeof(TheoraContext),
|
|
|
|
.init = encode_init,
|
|
|
|
.close = encode_close,
|
|
|
|
.encode = encode_frame,
|
|
|
|
.pix_fmts = supported_pixel_formats,
|
2008-06-13 00:50:13 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("libtheora Theora"),
|
2007-01-25 02:16:28 +02:00
|
|
|
};
|