2007-01-25 00:16:28 +00: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 10:40:25 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-01-25 00:16:28 +00:00
|
|
|
*/
|
|
|
|
|
2010-04-20 14:23:00 +00:00
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2010-04-20 14:23:00 +00:00
|
|
|
* @brief Theora encoder using libtheora.
|
|
|
|
* @author Paul Richards <paul.richards@gmail.com>
|
2007-01-25 00:16:28 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-04-17 21:57:50 +00:00
|
|
|
/* FFmpeg includes */
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/common.h"
|
2009-01-11 23:26:52 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2012-11-13 16:42:13 +00:00
|
|
|
#include "libavutil/pixdesc.h"
|
2008-05-09 11:56:36 +00:00
|
|
|
#include "libavutil/log.h"
|
2009-11-22 21:08:40 +00:00
|
|
|
#include "libavutil/base64.h"
|
2007-01-25 00:16:28 +00:00
|
|
|
#include "avcodec.h"
|
2012-02-12 08:32:40 +00:00
|
|
|
#include "internal.h"
|
2007-01-25 00:16:28 +00:00
|
|
|
|
|
|
|
/* libtheora includes */
|
2009-11-22 21:08:37 +00:00
|
|
|
#include <theora/theoraenc.h>
|
2007-01-25 00:16:28 +00:00
|
|
|
|
2009-10-19 15:51:34 +00:00
|
|
|
typedef struct TheoraContext {
|
2009-11-22 21:08:37 +00:00
|
|
|
th_enc_ctx *t_state;
|
2009-11-22 21:08:40 +00:00
|
|
|
uint8_t *stats;
|
|
|
|
int stats_size;
|
|
|
|
int stats_offset;
|
2009-11-22 21:08:43 +00:00
|
|
|
int uv_hshift;
|
|
|
|
int uv_vshift;
|
2009-12-12 20:18:46 +00:00
|
|
|
int keyframe_mask;
|
2007-01-25 00:16:28 +00:00
|
|
|
} TheoraContext;
|
|
|
|
|
2010-06-30 15:38:06 +00:00
|
|
|
/** Concatenate an ogg_packet into the extradata. */
|
2009-10-19 15:51:34 +00:00
|
|
|
static int concatenate_packet(unsigned int* offset,
|
|
|
|
AVCodecContext* avc_context,
|
|
|
|
const ogg_packet* packet)
|
2007-01-25 00:16:28 +00:00
|
|
|
{
|
2009-07-13 01:59:48 +00:00
|
|
|
const char* message = NULL;
|
2007-01-25 00:16:28 +00:00
|
|
|
int newsize = avc_context->extradata_size + 2 + packet->bytes;
|
2013-12-06 12:44:17 +00:00
|
|
|
int err = AVERROR_INVALIDDATA;
|
2007-01-25 00:16:28 +00:00
|
|
|
|
|
|
|
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 {
|
2013-12-06 12:44:17 +00:00
|
|
|
if ((err = av_reallocp(&avc_context->extradata, newsize)) < 0) {
|
|
|
|
avc_context->extradata_size = 0;
|
2007-01-25 00:16:28 +00:00
|
|
|
message = "av_realloc failed";
|
2013-12-06 12:44:17 +00:00
|
|
|
}
|
2007-01-25 00:16:28 +00:00
|
|
|
}
|
2009-10-19 22:26:52 +00:00
|
|
|
if (message) {
|
2007-01-25 00:16:28 +00:00
|
|
|
av_log(avc_context, AV_LOG_ERROR, "concatenate_packet failed: %s\n", message);
|
2013-12-06 12:44:17 +00:00
|
|
|
return err;
|
2007-01-25 00:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
avc_context->extradata_size = newsize;
|
2007-06-02 01:41:07 +00:00
|
|
|
AV_WB16(avc_context->extradata + (*offset), packet->bytes);
|
|
|
|
*offset += 2;
|
2009-10-19 15:51:34 +00:00
|
|
|
memcpy(avc_context->extradata + (*offset), packet->packet, packet->bytes);
|
2007-01-25 00:16:28 +00:00
|
|
|
(*offset) += packet->bytes;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-22 21:08:40 +00:00
|
|
|
static int get_stats(AVCodecContext *avctx, int eos)
|
|
|
|
{
|
2009-11-25 16:00:14 +00:00
|
|
|
#ifdef TH_ENCCTL_2PASS_OUT
|
2009-11-22 21:08:40 +00:00
|
|
|
TheoraContext *h = avctx->priv_data;
|
|
|
|
uint8_t *buf;
|
|
|
|
int bytes;
|
|
|
|
|
|
|
|
bytes = th_encode_ctl(h->t_state, TH_ENCCTL_2PASS_OUT, &buf, sizeof(buf));
|
|
|
|
if (bytes < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error getting first pass stats\n");
|
2012-11-11 10:32:41 +00:00
|
|
|
return AVERROR_EXTERNAL;
|
2009-11-22 21:08:40 +00:00
|
|
|
}
|
|
|
|
if (!eos) {
|
|
|
|
h->stats = av_fast_realloc(h->stats, &h->stats_size,
|
|
|
|
h->stats_offset + bytes);
|
|
|
|
memcpy(h->stats + h->stats_offset, buf, bytes);
|
|
|
|
h->stats_offset += bytes;
|
|
|
|
} else {
|
2010-06-04 01:15:41 +00:00
|
|
|
int b64_size = AV_BASE64_SIZE(h->stats_offset);
|
2009-11-22 21:08:40 +00:00
|
|
|
// libtheora generates a summary header at the end
|
|
|
|
memcpy(h->stats, buf, bytes);
|
|
|
|
avctx->stats_out = av_malloc(b64_size);
|
2015-05-15 20:02:12 +00:00
|
|
|
if (!avctx->stats_out)
|
|
|
|
return AVERROR(ENOMEM);
|
2009-11-22 21:08:40 +00:00
|
|
|
av_base64_encode(avctx->stats_out, b64_size, h->stats, h->stats_offset);
|
|
|
|
}
|
|
|
|
return 0;
|
2009-11-25 16:00:14 +00:00
|
|
|
#else
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "libtheora too old to support 2pass\n");
|
2012-11-11 10:32:41 +00:00
|
|
|
return AVERROR(ENOSUP);
|
2009-11-25 16:00:14 +00:00
|
|
|
#endif
|
2009-11-22 21:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// libtheora won't read the entire buffer we give it at once, so we have to
|
|
|
|
// repeatedly submit it...
|
|
|
|
static int submit_stats(AVCodecContext *avctx)
|
|
|
|
{
|
2009-11-25 16:00:14 +00:00
|
|
|
#ifdef TH_ENCCTL_2PASS_IN
|
2009-11-22 21:08:40 +00:00
|
|
|
TheoraContext *h = avctx->priv_data;
|
|
|
|
int bytes;
|
|
|
|
if (!h->stats) {
|
|
|
|
if (!avctx->stats_in) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "No statsfile for second pass\n");
|
2012-11-11 10:32:41 +00:00
|
|
|
return AVERROR(EINVAL);
|
2009-11-22 21:08:40 +00:00
|
|
|
}
|
|
|
|
h->stats_size = strlen(avctx->stats_in) * 3/4;
|
|
|
|
h->stats = av_malloc(h->stats_size);
|
|
|
|
h->stats_size = av_base64_decode(h->stats, avctx->stats_in, h->stats_size);
|
|
|
|
}
|
|
|
|
while (h->stats_size - h->stats_offset > 0) {
|
|
|
|
bytes = th_encode_ctl(h->t_state, TH_ENCCTL_2PASS_IN,
|
|
|
|
h->stats + h->stats_offset,
|
|
|
|
h->stats_size - h->stats_offset);
|
|
|
|
if (bytes < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error submitting stats\n");
|
2012-11-11 10:32:41 +00:00
|
|
|
return AVERROR_EXTERNAL;
|
2009-11-22 21:08:40 +00:00
|
|
|
}
|
|
|
|
if (!bytes)
|
|
|
|
return 0;
|
|
|
|
h->stats_offset += bytes;
|
|
|
|
}
|
|
|
|
return 0;
|
2009-11-25 16:00:14 +00:00
|
|
|
#else
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "libtheora too old to support 2pass\n");
|
2012-11-11 10:32:41 +00:00
|
|
|
return AVERROR(ENOSUP);
|
2009-11-25 16:00:14 +00:00
|
|
|
#endif
|
2009-11-22 21:08:40 +00:00
|
|
|
}
|
|
|
|
|
2009-02-22 13:48:55 +00:00
|
|
|
static av_cold int encode_init(AVCodecContext* avc_context)
|
2007-01-25 00:16:28 +00:00
|
|
|
{
|
2009-11-22 21:08:37 +00:00
|
|
|
th_info t_info;
|
|
|
|
th_comment t_comment;
|
2007-01-25 00:16:28 +00:00
|
|
|
ogg_packet o_packet;
|
|
|
|
unsigned int offset;
|
|
|
|
TheoraContext *h = avc_context->priv_data;
|
2009-11-22 21:08:37 +00:00
|
|
|
uint32_t gop_size = avc_context->gop_size;
|
2012-11-11 10:32:41 +00:00
|
|
|
int ret;
|
2007-01-25 00:16:28 +00:00
|
|
|
|
|
|
|
/* Set up the theora_info struct */
|
2009-11-22 21:08:37 +00:00
|
|
|
th_info_init(&t_info);
|
|
|
|
t_info.frame_width = FFALIGN(avc_context->width, 16);
|
|
|
|
t_info.frame_height = FFALIGN(avc_context->height, 16);
|
|
|
|
t_info.pic_width = avc_context->width;
|
|
|
|
t_info.pic_height = avc_context->height;
|
|
|
|
t_info.pic_x = 0;
|
|
|
|
t_info.pic_y = 0;
|
2007-02-07 14:10:50 +00: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 15:51:34 +00:00
|
|
|
t_info.fps_numerator = avc_context->time_base.den;
|
2007-01-25 00:16:28 +00:00
|
|
|
t_info.fps_denominator = avc_context->time_base.num;
|
2009-10-19 22:26:52 +00:00
|
|
|
if (avc_context->sample_aspect_ratio.num) {
|
2009-10-19 15:51:34 +00:00
|
|
|
t_info.aspect_numerator = avc_context->sample_aspect_ratio.num;
|
2007-01-25 00:16:28 +00:00
|
|
|
t_info.aspect_denominator = avc_context->sample_aspect_ratio.den;
|
|
|
|
} else {
|
2009-10-19 15:51:34 +00:00
|
|
|
t_info.aspect_numerator = 1;
|
2007-01-25 00:16:28 +00:00
|
|
|
t_info.aspect_denominator = 1;
|
|
|
|
}
|
2009-11-22 21:08:46 +00:00
|
|
|
|
|
|
|
if (avc_context->color_primaries == AVCOL_PRI_BT470M)
|
|
|
|
t_info.colorspace = TH_CS_ITU_REC_470M;
|
|
|
|
else if (avc_context->color_primaries == AVCOL_PRI_BT470BG)
|
|
|
|
t_info.colorspace = TH_CS_ITU_REC_470BG;
|
|
|
|
else
|
|
|
|
t_info.colorspace = TH_CS_UNSPECIFIED;
|
2009-11-22 21:08:43 +00:00
|
|
|
|
2012-10-06 10:10:34 +00:00
|
|
|
if (avc_context->pix_fmt == AV_PIX_FMT_YUV420P)
|
2009-11-22 21:08:43 +00:00
|
|
|
t_info.pixel_fmt = TH_PF_420;
|
2012-10-06 10:10:34 +00:00
|
|
|
else if (avc_context->pix_fmt == AV_PIX_FMT_YUV422P)
|
2009-11-22 21:08:43 +00:00
|
|
|
t_info.pixel_fmt = TH_PF_422;
|
2012-10-06 10:10:34 +00:00
|
|
|
else if (avc_context->pix_fmt == AV_PIX_FMT_YUV444P)
|
2009-11-22 21:08:43 +00:00
|
|
|
t_info.pixel_fmt = TH_PF_444;
|
|
|
|
else {
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "Unsupported pix_fmt\n");
|
2012-11-11 10:32:41 +00:00
|
|
|
return AVERROR(EINVAL);
|
2009-11-22 21:08:43 +00:00
|
|
|
}
|
|
|
|
avcodec_get_chroma_sub_sample(avc_context->pix_fmt, &h->uv_hshift, &h->uv_vshift);
|
2007-01-25 00:16:28 +00:00
|
|
|
|
2009-09-30 22:25:49 +00:00
|
|
|
if (avc_context->flags & CODEC_FLAG_QSCALE) {
|
2013-03-19 12:19:12 +00:00
|
|
|
/* Clip global_quality in QP units to the [0 - 10] range
|
|
|
|
to be consistent with the libvorbis implementation.
|
|
|
|
Theora accepts a quality parameter which is an int value in
|
|
|
|
the [0 - 63] range.
|
|
|
|
*/
|
2013-01-04 15:56:00 +00:00
|
|
|
t_info.quality = av_clipf(avc_context->global_quality / (float)FF_QP2LAMBDA, 0, 10) * 6.3;
|
2009-09-30 22:25:49 +00:00
|
|
|
t_info.target_bitrate = 0;
|
|
|
|
} else {
|
|
|
|
t_info.target_bitrate = avc_context->bit_rate;
|
2009-10-19 15:51:34 +00:00
|
|
|
t_info.quality = 0;
|
2009-09-30 22:25:49 +00:00
|
|
|
}
|
|
|
|
|
2007-01-25 00:16:28 +00:00
|
|
|
/* Now initialise libtheora */
|
2009-11-22 21:08:37 +00:00
|
|
|
h->t_state = th_encode_alloc(&t_info);
|
|
|
|
if (!h->t_state) {
|
2007-01-25 00:16:28 +00:00
|
|
|
av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n");
|
2012-11-11 10:32:41 +00:00
|
|
|
return AVERROR_EXTERNAL;
|
2007-01-25 00:16:28 +00:00
|
|
|
}
|
|
|
|
|
2009-12-12 20:18:46 +00:00
|
|
|
h->keyframe_mask = (1 << t_info.keyframe_granule_shift) - 1;
|
2007-01-25 00:16:28 +00:00
|
|
|
/* Clear up theora_info struct */
|
2009-11-22 21:08:37 +00:00
|
|
|
th_info_clear(&t_info);
|
|
|
|
|
|
|
|
if (th_encode_ctl(h->t_state, TH_ENCCTL_SET_KEYFRAME_FREQUENCY_FORCE,
|
|
|
|
&gop_size, sizeof(gop_size))) {
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "Error setting GOP size\n");
|
2012-11-11 10:32:41 +00:00
|
|
|
return AVERROR_EXTERNAL;
|
2009-11-22 21:08:37 +00:00
|
|
|
}
|
2007-01-25 00:16:28 +00:00
|
|
|
|
2009-11-22 21:08:40 +00:00
|
|
|
// need to enable 2 pass (via TH_ENCCTL_2PASS_) before encoding headers
|
|
|
|
if (avc_context->flags & CODEC_FLAG_PASS1) {
|
2012-11-11 10:32:41 +00:00
|
|
|
if ((ret = get_stats(avc_context, 0)) < 0)
|
|
|
|
return ret;
|
2009-11-22 21:08:40 +00:00
|
|
|
} else if (avc_context->flags & CODEC_FLAG_PASS2) {
|
2012-11-11 10:32:41 +00:00
|
|
|
if ((ret = submit_stats(avc_context)) < 0)
|
|
|
|
return ret;
|
2009-11-22 21:08:40 +00:00
|
|
|
}
|
|
|
|
|
2007-01-25 00:16:28 +00:00
|
|
|
/*
|
|
|
|
Output first header packet consisting of theora
|
|
|
|
header, comment, and tables.
|
|
|
|
|
|
|
|
Each one is prefixed with a 16bit size, then they
|
2011-10-30 17:02:42 +00:00
|
|
|
are concatenated together into libavcodec's extradata.
|
2007-01-25 00:16:28 +00:00
|
|
|
*/
|
|
|
|
offset = 0;
|
|
|
|
|
2009-11-22 21:08:37 +00:00
|
|
|
/* Headers */
|
|
|
|
th_comment_init(&t_comment);
|
2007-01-25 00:16:28 +00:00
|
|
|
|
2009-11-22 21:08:37 +00:00
|
|
|
while (th_encode_flushheader(h->t_state, &t_comment, &o_packet))
|
2012-11-11 10:32:41 +00:00
|
|
|
if ((ret = concatenate_packet(&offset, avc_context, &o_packet)) < 0)
|
|
|
|
return ret;
|
2009-11-22 21:08:37 +00:00
|
|
|
|
|
|
|
th_comment_clear(&t_comment);
|
2007-01-25 00:16:28 +00:00
|
|
|
|
|
|
|
/* Set up the output AVFrame */
|
2013-11-09 09:05:22 +00:00
|
|
|
avc_context->coded_frame = av_frame_alloc();
|
2007-01-25 00:16:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-12 08:32:40 +00:00
|
|
|
static int encode_frame(AVCodecContext* avc_context, AVPacket *pkt,
|
|
|
|
const AVFrame *frame, int *got_packet)
|
2007-01-25 00:16:28 +00:00
|
|
|
{
|
2009-11-22 21:08:37 +00:00
|
|
|
th_ycbcr_buffer t_yuv_buffer;
|
2007-01-25 00:16:28 +00:00
|
|
|
TheoraContext *h = avc_context->priv_data;
|
|
|
|
ogg_packet o_packet;
|
2012-02-12 08:32:40 +00:00
|
|
|
int result, i, ret;
|
2007-01-25 00:16:28 +00:00
|
|
|
|
2009-11-22 21:08:40 +00:00
|
|
|
// EOS, finish and get 1st pass stats if applicable
|
|
|
|
if (!frame) {
|
|
|
|
th_encode_packetout(h->t_state, 1, &o_packet);
|
|
|
|
if (avc_context->flags & CODEC_FLAG_PASS1)
|
2012-11-11 10:32:41 +00:00
|
|
|
if ((ret = get_stats(avc_context, 1)) < 0)
|
|
|
|
return ret;
|
2009-11-22 21:08:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-25 00:16:28 +00:00
|
|
|
/* Copy planes to the theora yuv_buffer */
|
2009-11-22 21:08:37 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
2009-11-22 21:08:43 +00:00
|
|
|
t_yuv_buffer[i].width = FFALIGN(avc_context->width, 16) >> (i && h->uv_hshift);
|
|
|
|
t_yuv_buffer[i].height = FFALIGN(avc_context->height, 16) >> (i && h->uv_vshift);
|
2009-11-22 21:08:37 +00:00
|
|
|
t_yuv_buffer[i].stride = frame->linesize[i];
|
|
|
|
t_yuv_buffer[i].data = frame->data[i];
|
2007-01-25 00:16:28 +00:00
|
|
|
}
|
|
|
|
|
2009-11-22 21:08:40 +00:00
|
|
|
if (avc_context->flags & CODEC_FLAG_PASS2)
|
2012-11-11 10:32:41 +00:00
|
|
|
if ((ret = submit_stats(avc_context)) < 0)
|
|
|
|
return ret;
|
2009-11-22 21:08:40 +00:00
|
|
|
|
2007-01-25 00:16:28 +00:00
|
|
|
/* Now call into theora_encode_YUVin */
|
2009-11-22 21:08:37 +00:00
|
|
|
result = th_encode_ycbcr_in(h->t_state, t_yuv_buffer);
|
2009-10-19 22:26:52 +00:00
|
|
|
if (result) {
|
2007-01-25 00:16:28 +00:00
|
|
|
const char* message;
|
|
|
|
switch (result) {
|
2009-10-19 15:51:34 +00:00
|
|
|
case -1:
|
|
|
|
message = "differing frame sizes";
|
|
|
|
break;
|
2009-11-22 21:08:37 +00:00
|
|
|
case TH_EINVAL:
|
2009-10-19 15:51:34 +00:00
|
|
|
message = "encoder is not ready or is finished";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
message = "unknown reason";
|
|
|
|
break;
|
2007-01-25 00:16:28 +00:00
|
|
|
}
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "theora_encode_YUVin failed (%s) [%d]\n", message, result);
|
2012-11-11 10:32:41 +00:00
|
|
|
return AVERROR_EXTERNAL;
|
2007-01-25 00:16:28 +00:00
|
|
|
}
|
|
|
|
|
2009-11-22 21:08:40 +00:00
|
|
|
if (avc_context->flags & CODEC_FLAG_PASS1)
|
2012-11-11 10:32:41 +00:00
|
|
|
if ((ret = get_stats(avc_context, 0)) < 0)
|
|
|
|
return ret;
|
2009-11-22 21:08:40 +00:00
|
|
|
|
2007-01-25 00:16:28 +00:00
|
|
|
/* Pick up returned ogg_packet */
|
2009-11-22 21:08:37 +00:00
|
|
|
result = th_encode_packetout(h->t_state, 0, &o_packet);
|
2007-01-25 00:16:28 +00:00
|
|
|
switch (result) {
|
2009-10-19 15:51:34 +00: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);
|
2012-11-11 10:32:41 +00:00
|
|
|
return AVERROR_EXTERNAL;
|
2007-01-25 00:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy ogg_packet content out to buffer */
|
2012-03-22 20:22:39 +00:00
|
|
|
if ((ret = ff_alloc_packet2(avc_context, pkt, o_packet.bytes)) < 0)
|
2012-02-12 08:32:40 +00:00
|
|
|
return ret;
|
|
|
|
memcpy(pkt->data, o_packet.packet, o_packet.bytes);
|
2007-01-25 00:16:28 +00:00
|
|
|
|
2009-12-12 20:18:49 +00:00
|
|
|
// HACK: assumes no encoder delay, this is true until libtheora becomes
|
2012-12-19 17:48:21 +00:00
|
|
|
// multithreaded (which will be disabled unless explicitly requested)
|
2012-02-12 08:32:40 +00:00
|
|
|
pkt->pts = pkt->dts = frame->pts;
|
2009-12-12 20:18:46 +00:00
|
|
|
avc_context->coded_frame->key_frame = !(o_packet.granulepos & h->keyframe_mask);
|
2012-02-12 08:32:40 +00:00
|
|
|
if (avc_context->coded_frame->key_frame)
|
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
*got_packet = 1;
|
2009-10-10 14:43:31 +00:00
|
|
|
|
2012-02-12 08:32:40 +00:00
|
|
|
return 0;
|
2007-01-25 00:16:28 +00:00
|
|
|
}
|
|
|
|
|
2009-02-22 13:48:55 +00:00
|
|
|
static av_cold int encode_close(AVCodecContext* avc_context)
|
2007-01-25 00:16:28 +00:00
|
|
|
{
|
|
|
|
TheoraContext *h = avc_context->priv_data;
|
|
|
|
|
2009-11-22 21:08:37 +00:00
|
|
|
th_encode_free(h->t_state);
|
2009-11-22 21:08:40 +00:00
|
|
|
av_freep(&h->stats);
|
2009-07-13 01:59:45 +00:00
|
|
|
av_freep(&avc_context->coded_frame);
|
2009-11-22 21:08:40 +00:00
|
|
|
av_freep(&avc_context->stats_out);
|
2009-07-13 01:59:45 +00:00
|
|
|
av_freep(&avc_context->extradata);
|
|
|
|
avc_context->extradata_size = 0;
|
|
|
|
|
2009-11-22 21:08:40 +00:00
|
|
|
return 0;
|
2007-01-25 00:16:28 +00:00
|
|
|
}
|
|
|
|
|
2010-04-20 14:23:00 +00:00
|
|
|
/** AVCodec struct exposed to libavcodec */
|
2011-01-25 21:40:11 +00:00
|
|
|
AVCodec ff_libtheora_encoder = {
|
2012-04-06 16:19:39 +00:00
|
|
|
.name = "libtheora",
|
2013-10-03 20:57:53 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("libtheora Theora"),
|
2012-04-06 16:19:39 +00:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 09:11:04 +00:00
|
|
|
.id = AV_CODEC_ID_THEORA,
|
2007-01-25 00:16:28 +00:00
|
|
|
.priv_data_size = sizeof(TheoraContext),
|
2012-04-06 16:19:39 +00:00
|
|
|
.init = encode_init,
|
|
|
|
.close = encode_close,
|
|
|
|
.encode2 = encode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DELAY, // needed to get the statsfile summary
|
2012-10-06 10:10:34 +00:00
|
|
|
.pix_fmts = (const enum AVPixelFormat[]){
|
|
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_NONE
|
2012-04-06 16:19:39 +00:00
|
|
|
},
|
2007-01-25 00:16:28 +00:00
|
|
|
};
|