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
|
|
|
*/
|
|
|
|
|
2010-04-20 17:23:00 +03:00
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2010-04-20 17:23:00 +03:00
|
|
|
* @brief Theora encoder using libtheora.
|
|
|
|
* @author Paul Richards <paul.richards@gmail.com>
|
2007-01-25 02:16:28 +02: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-18 00:57:50 +03:00
|
|
|
/* FFmpeg includes */
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/common.h"
|
2009-01-12 01:26:52 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2012-11-13 19:42:13 +03:00
|
|
|
#include "libavutil/pixdesc.h"
|
2008-05-09 14:56:36 +03:00
|
|
|
#include "libavutil/log.h"
|
2009-11-22 23:08:40 +02:00
|
|
|
#include "libavutil/base64.h"
|
2007-01-25 02:16:28 +02:00
|
|
|
#include "avcodec.h"
|
2012-02-12 11:32:40 +03:00
|
|
|
#include "internal.h"
|
2007-01-25 02:16:28 +02:00
|
|
|
|
|
|
|
/* libtheora includes */
|
2009-11-22 23:08:37 +02:00
|
|
|
#include <theora/theoraenc.h>
|
2007-01-25 02:16:28 +02:00
|
|
|
|
2009-10-19 18:51:34 +03:00
|
|
|
typedef struct TheoraContext {
|
2009-11-22 23:08:37 +02:00
|
|
|
th_enc_ctx *t_state;
|
2009-11-22 23:08:40 +02:00
|
|
|
uint8_t *stats;
|
|
|
|
int stats_size;
|
|
|
|
int stats_offset;
|
2009-11-22 23:08:43 +02:00
|
|
|
int uv_hshift;
|
|
|
|
int uv_vshift;
|
2009-12-12 22:18:46 +02:00
|
|
|
int keyframe_mask;
|
2007-01-25 02:16:28 +02:00
|
|
|
} TheoraContext;
|
|
|
|
|
2010-06-30 18:38:06 +03:00
|
|
|
/** Concatenate 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;
|
2007-01-25 02:16:28 +02:00
|
|
|
int newsize = avc_context->extradata_size + 2 + packet->bytes;
|
2013-12-06 15:44:17 +03:00
|
|
|
int err = AVERROR_INVALIDDATA;
|
2007-01-25 02:16:28 +02: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 15:44:17 +03:00
|
|
|
if ((err = av_reallocp(&avc_context->extradata, newsize)) < 0) {
|
|
|
|
avc_context->extradata_size = 0;
|
2007-01-25 02:16:28 +02:00
|
|
|
message = "av_realloc failed";
|
2013-12-06 15:44:17 +03:00
|
|
|
}
|
2007-01-25 02:16:28 +02:00
|
|
|
}
|
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);
|
2013-12-06 15:44:17 +03:00
|
|
|
return err;
|
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-11-22 23:08:40 +02:00
|
|
|
static int get_stats(AVCodecContext *avctx, int eos)
|
|
|
|
{
|
2009-11-25 18:00:14 +02:00
|
|
|
#ifdef TH_ENCCTL_2PASS_OUT
|
2009-11-22 23:08:40 +02: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 13:32:41 +03:00
|
|
|
return AVERROR_EXTERNAL;
|
2009-11-22 23:08:40 +02:00
|
|
|
}
|
|
|
|
if (!eos) {
|
2015-02-22 03:15:37 +02:00
|
|
|
void *tmp = av_fast_realloc(h->stats, &h->stats_size,
|
2009-11-22 23:08:40 +02:00
|
|
|
h->stats_offset + bytes);
|
2015-02-22 03:15:37 +02:00
|
|
|
if (!tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
h->stats = tmp;
|
2009-11-22 23:08:40 +02:00
|
|
|
memcpy(h->stats + h->stats_offset, buf, bytes);
|
|
|
|
h->stats_offset += bytes;
|
|
|
|
} else {
|
2010-06-04 04:15:41 +03:00
|
|
|
int b64_size = AV_BASE64_SIZE(h->stats_offset);
|
2009-11-22 23:08:40 +02:00
|
|
|
// libtheora generates a summary header at the end
|
|
|
|
memcpy(h->stats, buf, bytes);
|
|
|
|
avctx->stats_out = av_malloc(b64_size);
|
2015-05-15 22:02:12 +02:00
|
|
|
if (!avctx->stats_out)
|
|
|
|
return AVERROR(ENOMEM);
|
2009-11-22 23:08:40 +02:00
|
|
|
av_base64_encode(avctx->stats_out, b64_size, h->stats, h->stats_offset);
|
|
|
|
}
|
|
|
|
return 0;
|
2009-11-25 18:00:14 +02:00
|
|
|
#else
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "libtheora too old to support 2pass\n");
|
2012-11-11 13:32:41 +03:00
|
|
|
return AVERROR(ENOSUP);
|
2009-11-25 18:00:14 +02:00
|
|
|
#endif
|
2009-11-22 23:08:40 +02: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 18:00:14 +02:00
|
|
|
#ifdef TH_ENCCTL_2PASS_IN
|
2009-11-22 23:08:40 +02: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 13:32:41 +03:00
|
|
|
return AVERROR(EINVAL);
|
2009-11-22 23:08:40 +02:00
|
|
|
}
|
|
|
|
h->stats_size = strlen(avctx->stats_in) * 3/4;
|
|
|
|
h->stats = av_malloc(h->stats_size);
|
2015-02-22 03:11:27 +02:00
|
|
|
if (!h->stats) {
|
|
|
|
h->stats_size = 0;
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2009-11-22 23:08:40 +02:00
|
|
|
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 13:32:41 +03:00
|
|
|
return AVERROR_EXTERNAL;
|
2009-11-22 23:08:40 +02:00
|
|
|
}
|
|
|
|
if (!bytes)
|
|
|
|
return 0;
|
|
|
|
h->stats_offset += bytes;
|
|
|
|
}
|
|
|
|
return 0;
|
2009-11-25 18:00:14 +02:00
|
|
|
#else
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "libtheora too old to support 2pass\n");
|
2012-11-11 13:32:41 +03:00
|
|
|
return AVERROR(ENOSUP);
|
2009-11-25 18:00:14 +02:00
|
|
|
#endif
|
2009-11-22 23:08:40 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2009-11-22 23:08:37 +02:00
|
|
|
th_info t_info;
|
|
|
|
th_comment t_comment;
|
2007-01-25 02:16:28 +02:00
|
|
|
ogg_packet o_packet;
|
|
|
|
unsigned int offset;
|
|
|
|
TheoraContext *h = avc_context->priv_data;
|
2009-11-22 23:08:37 +02:00
|
|
|
uint32_t gop_size = avc_context->gop_size;
|
2012-11-11 13:32:41 +03:00
|
|
|
int ret;
|
2007-01-25 02:16:28 +02:00
|
|
|
|
|
|
|
/* Set up the theora_info struct */
|
2009-11-22 23:08:37 +02: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 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-11-22 23:08:46 +02: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 23:08:43 +02:00
|
|
|
|
2012-10-06 13:10:34 +03:00
|
|
|
if (avc_context->pix_fmt == AV_PIX_FMT_YUV420P)
|
2009-11-22 23:08:43 +02:00
|
|
|
t_info.pixel_fmt = TH_PF_420;
|
2012-10-06 13:10:34 +03:00
|
|
|
else if (avc_context->pix_fmt == AV_PIX_FMT_YUV422P)
|
2009-11-22 23:08:43 +02:00
|
|
|
t_info.pixel_fmt = TH_PF_422;
|
2012-10-06 13:10:34 +03:00
|
|
|
else if (avc_context->pix_fmt == AV_PIX_FMT_YUV444P)
|
2009-11-22 23:08:43 +02:00
|
|
|
t_info.pixel_fmt = TH_PF_444;
|
|
|
|
else {
|
|
|
|
av_log(avc_context, AV_LOG_ERROR, "Unsupported pix_fmt\n");
|
2012-11-11 13:32:41 +03:00
|
|
|
return AVERROR(EINVAL);
|
2009-11-22 23:08:43 +02:00
|
|
|
}
|
|
|
|
avcodec_get_chroma_sub_sample(avc_context->pix_fmt, &h->uv_hshift, &h->uv_vshift);
|
2007-01-25 02:16:28 +02:00
|
|
|
|
2015-06-29 21:59:37 +02:00
|
|
|
if (avc_context->flags & AV_CODEC_FLAG_QSCALE) {
|
2013-03-19 15:19:12 +03: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 18:56:00 +03:00
|
|
|
t_info.quality = av_clipf(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-11-22 23:08:37 +02:00
|
|
|
h->t_state = th_encode_alloc(&t_info);
|
|
|
|
if (!h->t_state) {
|
2007-01-25 02:16:28 +02:00
|
|
|
av_log(avc_context, AV_LOG_ERROR, "theora_encode_init failed\n");
|
2012-11-11 13:32:41 +03:00
|
|
|
return AVERROR_EXTERNAL;
|
2007-01-25 02:16:28 +02:00
|
|
|
}
|
|
|
|
|
2009-12-12 22:18:46 +02:00
|
|
|
h->keyframe_mask = (1 << t_info.keyframe_granule_shift) - 1;
|
2007-01-25 02:16:28 +02:00
|
|
|
/* Clear up theora_info struct */
|
2009-11-22 23:08:37 +02: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 13:32:41 +03:00
|
|
|
return AVERROR_EXTERNAL;
|
2009-11-22 23:08:37 +02:00
|
|
|
}
|
2007-01-25 02:16:28 +02:00
|
|
|
|
2009-11-22 23:08:40 +02:00
|
|
|
// need to enable 2 pass (via TH_ENCCTL_2PASS_) before encoding headers
|
2015-06-29 21:59:37 +02:00
|
|
|
if (avc_context->flags & AV_CODEC_FLAG_PASS1) {
|
2012-11-11 13:32:41 +03:00
|
|
|
if ((ret = get_stats(avc_context, 0)) < 0)
|
|
|
|
return ret;
|
2015-06-29 21:59:37 +02:00
|
|
|
} else if (avc_context->flags & AV_CODEC_FLAG_PASS2) {
|
2012-11-11 13:32:41 +03:00
|
|
|
if ((ret = submit_stats(avc_context)) < 0)
|
|
|
|
return ret;
|
2009-11-22 23:08:40 +02:00
|
|
|
}
|
|
|
|
|
2007-01-25 02:16:28 +02:00
|
|
|
/*
|
|
|
|
Output first header packet consisting of theora
|
|
|
|
header, comment, and tables.
|
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
Each one is prefixed with a 16-bit size, then they
|
2011-10-30 20:02:42 +03:00
|
|
|
are concatenated together into libavcodec's extradata.
|
2007-01-25 02:16:28 +02:00
|
|
|
*/
|
|
|
|
offset = 0;
|
|
|
|
|
2009-11-22 23:08:37 +02:00
|
|
|
/* Headers */
|
|
|
|
th_comment_init(&t_comment);
|
2007-01-25 02:16:28 +02:00
|
|
|
|
2009-11-22 23:08:37 +02:00
|
|
|
while (th_encode_flushheader(h->t_state, &t_comment, &o_packet))
|
2012-11-11 13:32:41 +03:00
|
|
|
if ((ret = concatenate_packet(&offset, avc_context, &o_packet)) < 0)
|
|
|
|
return ret;
|
2009-11-22 23:08:37 +02:00
|
|
|
|
|
|
|
th_comment_clear(&t_comment);
|
2007-01-25 02:16:28 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-12 11:32:40 +03:00
|
|
|
static int encode_frame(AVCodecContext* avc_context, AVPacket *pkt,
|
|
|
|
const AVFrame *frame, int *got_packet)
|
2007-01-25 02:16:28 +02:00
|
|
|
{
|
2009-11-22 23:08:37 +02:00
|
|
|
th_ycbcr_buffer t_yuv_buffer;
|
2007-01-25 02:16:28 +02:00
|
|
|
TheoraContext *h = avc_context->priv_data;
|
|
|
|
ogg_packet o_packet;
|
2012-02-12 11:32:40 +03:00
|
|
|
int result, i, ret;
|
2007-01-25 02:16:28 +02:00
|
|
|
|
2009-11-22 23:08:40 +02:00
|
|
|
// EOS, finish and get 1st pass stats if applicable
|
|
|
|
if (!frame) {
|
|
|
|
th_encode_packetout(h->t_state, 1, &o_packet);
|
2015-06-29 21:59:37 +02:00
|
|
|
if (avc_context->flags & AV_CODEC_FLAG_PASS1)
|
2012-11-11 13:32:41 +03:00
|
|
|
if ((ret = get_stats(avc_context, 1)) < 0)
|
|
|
|
return ret;
|
2009-11-22 23:08:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-25 02:16:28 +02:00
|
|
|
/* Copy planes to the theora yuv_buffer */
|
2009-11-22 23:08:37 +02:00
|
|
|
for (i = 0; i < 3; i++) {
|
2009-11-22 23:08:43 +02: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 23:08:37 +02:00
|
|
|
t_yuv_buffer[i].stride = frame->linesize[i];
|
|
|
|
t_yuv_buffer[i].data = frame->data[i];
|
2007-01-25 02:16:28 +02:00
|
|
|
}
|
|
|
|
|
2015-06-29 21:59:37 +02:00
|
|
|
if (avc_context->flags & AV_CODEC_FLAG_PASS2)
|
2012-11-11 13:32:41 +03:00
|
|
|
if ((ret = submit_stats(avc_context)) < 0)
|
|
|
|
return ret;
|
2009-11-22 23:08:40 +02:00
|
|
|
|
2007-01-25 02:16:28 +02:00
|
|
|
/* Now call into theora_encode_YUVin */
|
2009-11-22 23:08:37 +02:00
|
|
|
result = th_encode_ycbcr_in(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;
|
2009-11-22 23:08:37 +02:00
|
|
|
case TH_EINVAL:
|
2009-10-19 18:51:34 +03:00
|
|
|
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);
|
2012-11-11 13:32:41 +03:00
|
|
|
return AVERROR_EXTERNAL;
|
2007-01-25 02:16:28 +02:00
|
|
|
}
|
|
|
|
|
2015-06-29 21:59:37 +02:00
|
|
|
if (avc_context->flags & AV_CODEC_FLAG_PASS1)
|
2012-11-11 13:32:41 +03:00
|
|
|
if ((ret = get_stats(avc_context, 0)) < 0)
|
|
|
|
return ret;
|
2009-11-22 23:08:40 +02:00
|
|
|
|
2007-01-25 02:16:28 +02:00
|
|
|
/* Pick up returned ogg_packet */
|
2009-11-22 23:08:37 +02:00
|
|
|
result = th_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);
|
2012-11-11 13:32:41 +03:00
|
|
|
return AVERROR_EXTERNAL;
|
2007-01-25 02:16:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy ogg_packet content out to buffer */
|
2015-07-27 19:36:16 +02:00
|
|
|
if ((ret = ff_alloc_packet2(avc_context, pkt, o_packet.bytes, 0)) < 0)
|
2012-02-12 11:32:40 +03:00
|
|
|
return ret;
|
|
|
|
memcpy(pkt->data, o_packet.packet, o_packet.bytes);
|
2007-01-25 02:16:28 +02:00
|
|
|
|
2009-12-12 22:18:49 +02:00
|
|
|
// HACK: assumes no encoder delay, this is true until libtheora becomes
|
2012-12-19 20:48:21 +03:00
|
|
|
// multithreaded (which will be disabled unless explicitly requested)
|
2012-02-12 11:32:40 +03:00
|
|
|
pkt->pts = pkt->dts = frame->pts;
|
2015-07-15 19:41:22 +02:00
|
|
|
#if FF_API_CODED_FRAME
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2009-12-12 22:18:46 +02:00
|
|
|
avc_context->coded_frame->key_frame = !(o_packet.granulepos & h->keyframe_mask);
|
2015-07-15 19:41:22 +02:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2015-07-15 19:41:15 +02:00
|
|
|
if (!(o_packet.granulepos & h->keyframe_mask))
|
2012-02-12 11:32:40 +03:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
*got_packet = 1;
|
2009-10-10 17:43:31 +03:00
|
|
|
|
2012-02-12 11:32:40 +03:00
|
|
|
return 0;
|
2007-01-25 02:16:28 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
TheoraContext *h = avc_context->priv_data;
|
|
|
|
|
2009-11-22 23:08:37 +02:00
|
|
|
th_encode_free(h->t_state);
|
2009-11-22 23:08:40 +02:00
|
|
|
av_freep(&h->stats);
|
|
|
|
av_freep(&avc_context->stats_out);
|
2009-07-13 04:59:45 +03:00
|
|
|
av_freep(&avc_context->extradata);
|
|
|
|
avc_context->extradata_size = 0;
|
|
|
|
|
2009-11-22 23:08:40 +02:00
|
|
|
return 0;
|
2007-01-25 02:16:28 +02:00
|
|
|
}
|
|
|
|
|
2010-04-20 17:23:00 +03:00
|
|
|
/** AVCodec struct exposed to libavcodec */
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_libtheora_encoder = {
|
2012-04-06 19:19:39 +03:00
|
|
|
.name = "libtheora",
|
2013-10-03 23:57:53 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("libtheora Theora"),
|
2012-04-06 19:19:39 +03:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_THEORA,
|
2007-01-25 02:16:28 +02:00
|
|
|
.priv_data_size = sizeof(TheoraContext),
|
2012-04-06 19:19:39 +03:00
|
|
|
.init = encode_init,
|
|
|
|
.close = encode_close,
|
|
|
|
.encode2 = encode_frame,
|
2015-07-07 02:41:27 +02:00
|
|
|
.capabilities = AV_CODEC_CAP_DELAY, // needed to get the statsfile summary
|
2012-10-06 13:10:34 +03: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 19:19:39 +03:00
|
|
|
},
|
2007-01-25 02:16:28 +02:00
|
|
|
};
|