2011-04-16 15:45:30 +00:00
|
|
|
/*
|
2011-10-25 08:14:34 +00:00
|
|
|
* Xiph CELT decoder using libcelt
|
2011-04-16 15:45:30 +00:00
|
|
|
* Copyright (c) 2011 Nicolas George
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <celt/celt.h>
|
|
|
|
#include <celt/celt_header.h>
|
|
|
|
#include "avcodec.h"
|
2012-12-09 02:43:03 +00:00
|
|
|
#include "internal.h"
|
2011-04-16 15:45:30 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
|
|
|
|
struct libcelt_context {
|
|
|
|
CELTMode *mode;
|
|
|
|
CELTDecoder *dec;
|
|
|
|
int discard;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ff_celt_error_to_averror(int err)
|
|
|
|
{
|
2011-10-25 07:38:08 +00:00
|
|
|
switch (err) {
|
2011-04-16 15:45:30 +00:00
|
|
|
case CELT_BAD_ARG: return AVERROR(EINVAL);
|
|
|
|
#ifdef CELT_BUFFER_TOO_SMALL
|
|
|
|
case CELT_BUFFER_TOO_SMALL: return AVERROR(ENOBUFS);
|
|
|
|
#endif
|
|
|
|
case CELT_INTERNAL_ERROR: return AVERROR(EFAULT);
|
|
|
|
case CELT_CORRUPTED_DATA: return AVERROR_INVALIDDATA;
|
2011-12-20 18:08:04 +00:00
|
|
|
case CELT_UNIMPLEMENTED: return AVERROR(ENOSYS);
|
2011-04-23 08:09:11 +00:00
|
|
|
#ifdef ENOTRECOVERABLE
|
2011-04-16 15:45:30 +00:00
|
|
|
case CELT_INVALID_STATE: return AVERROR(ENOTRECOVERABLE);
|
2011-04-23 08:09:11 +00:00
|
|
|
#endif
|
2011-04-16 15:45:30 +00:00
|
|
|
case CELT_ALLOC_FAIL: return AVERROR(ENOMEM);
|
2011-04-23 08:06:39 +00:00
|
|
|
default: return AVERROR(EINVAL);
|
2011-04-16 15:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ff_celt_bitstream_version_hack(CELTMode *mode)
|
|
|
|
{
|
|
|
|
CELTHeader header = { .version_id = 0 };
|
|
|
|
celt_header_init(&header, mode, 960, 2);
|
|
|
|
return header.version_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int libcelt_dec_init(AVCodecContext *c)
|
|
|
|
{
|
|
|
|
struct libcelt_context *celt = c->priv_data;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!c->channels || !c->frame_size ||
|
|
|
|
c->frame_size > INT_MAX / sizeof(int16_t) / c->channels)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
celt->mode = celt_mode_create(c->sample_rate, c->frame_size, &err);
|
|
|
|
if (!celt->mode)
|
|
|
|
return ff_celt_error_to_averror(err);
|
|
|
|
celt->dec = celt_decoder_create_custom(celt->mode, c->channels, &err);
|
|
|
|
if (!celt->dec) {
|
|
|
|
celt_mode_destroy(celt->mode);
|
|
|
|
return ff_celt_error_to_averror(err);
|
|
|
|
}
|
|
|
|
if (c->extradata_size >= 4) {
|
|
|
|
celt->discard = AV_RL32(c->extradata);
|
|
|
|
if (celt->discard < 0 || celt->discard >= c->frame_size) {
|
|
|
|
av_log(c, AV_LOG_WARNING,
|
|
|
|
"Invalid overlap (%d), ignored.\n", celt->discard);
|
|
|
|
celt->discard = 0;
|
|
|
|
}
|
|
|
|
}
|
2011-10-25 07:38:08 +00:00
|
|
|
if (c->extradata_size >= 8) {
|
2011-04-16 15:45:30 +00:00
|
|
|
unsigned version = AV_RL32(c->extradata + 4);
|
|
|
|
unsigned lib_version = ff_celt_bitstream_version_hack(celt->mode);
|
|
|
|
if (version != lib_version)
|
|
|
|
av_log(c, AV_LOG_WARNING,
|
|
|
|
"CELT bitstream version 0x%x may be "
|
|
|
|
"improperly decoded by libcelt for version 0x%x.\n",
|
|
|
|
version, lib_version);
|
|
|
|
}
|
2011-10-24 17:55:49 +00:00
|
|
|
c->sample_fmt = AV_SAMPLE_FMT_S16;
|
2011-04-16 15:45:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int libcelt_dec_close(AVCodecContext *c)
|
|
|
|
{
|
|
|
|
struct libcelt_context *celt = c->priv_data;
|
|
|
|
|
|
|
|
celt_decoder_destroy(celt->dec);
|
|
|
|
celt_mode_destroy(celt->mode);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-13 13:32:27 +00:00
|
|
|
static int libcelt_dec_decode(AVCodecContext *c, void *data,
|
2011-12-03 10:52:48 +00:00
|
|
|
int *got_frame_ptr, AVPacket *pkt)
|
2011-04-16 15:45:30 +00:00
|
|
|
{
|
|
|
|
struct libcelt_context *celt = c->priv_data;
|
2013-02-13 13:32:27 +00:00
|
|
|
AVFrame *frame = data;
|
2011-04-16 15:45:30 +00:00
|
|
|
int err;
|
2011-12-03 10:52:48 +00:00
|
|
|
int16_t *pcm;
|
2011-04-16 15:45:30 +00:00
|
|
|
|
2013-02-13 13:32:27 +00:00
|
|
|
frame->nb_samples = c->frame_size;
|
lavc: factorize ff_{thread_,re,}get_buffer error messages.
Coccinelle profile used:
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_get_buffer(ctx, f, flags)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str;
@@
-if ((r = ff_reget_buffer(ctx, f)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_reget_buffer(ctx, f)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0)
+ return r;
...along with some manual patches for the remaining ones.
2013-03-12 07:41:53 +00:00
|
|
|
if ((err = ff_get_buffer(c, frame, 0)) < 0)
|
2011-12-03 10:52:48 +00:00
|
|
|
return err;
|
2013-02-13 13:32:27 +00:00
|
|
|
pcm = (int16_t *)frame->data[0];
|
2011-04-16 15:45:30 +00:00
|
|
|
err = celt_decode(celt->dec, pkt->data, pkt->size, pcm, c->frame_size);
|
|
|
|
if (err < 0)
|
|
|
|
return ff_celt_error_to_averror(err);
|
|
|
|
if (celt->discard) {
|
2013-02-13 13:32:27 +00:00
|
|
|
frame->nb_samples -= celt->discard;
|
2011-12-03 10:52:48 +00:00
|
|
|
memmove(pcm, pcm + celt->discard * c->channels,
|
2013-02-13 13:32:27 +00:00
|
|
|
frame->nb_samples * c->channels * sizeof(int16_t));
|
2011-04-16 15:45:30 +00:00
|
|
|
celt->discard = 0;
|
|
|
|
}
|
2011-12-03 10:52:48 +00:00
|
|
|
*got_frame_ptr = 1;
|
2011-04-16 15:45:30 +00:00
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCodec ff_libcelt_decoder = {
|
|
|
|
.name = "libcelt",
|
2013-10-04 13:33:20 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Xiph CELT decoder using libcelt"),
|
2011-04-16 15:45:30 +00:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2012-08-07 20:45:46 +00:00
|
|
|
.id = AV_CODEC_ID_CELT,
|
2011-04-16 15:45:30 +00:00
|
|
|
.priv_data_size = sizeof(struct libcelt_context),
|
|
|
|
.init = libcelt_dec_init,
|
|
|
|
.close = libcelt_dec_close,
|
|
|
|
.decode = libcelt_dec_decode,
|
2015-07-27 20:21:19 +00:00
|
|
|
.capabilities = AV_CODEC_CAP_DR1,
|
2017-12-11 15:18:44 +00:00
|
|
|
.wrapper_name = "libcelt",
|
2011-04-16 15:45:30 +00:00
|
|
|
};
|