FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libcelt_dec.c
Go to the documentation of this file.
1 /*
2  * Xiph CELT decoder using libcelt
3  * Copyright (c) 2011 Nicolas George
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <celt/celt.h>
23 #include <celt/celt_header.h>
24 #include "avcodec.h"
25 #include "internal.h"
26 #include "libavutil/intreadwrite.h"
27 
29  CELTMode *mode;
30  CELTDecoder *dec;
31  int discard;
32 };
33 
34 static int ff_celt_error_to_averror(int err)
35 {
36  switch (err) {
37  case CELT_BAD_ARG: return AVERROR(EINVAL);
38 #ifdef CELT_BUFFER_TOO_SMALL
39  case CELT_BUFFER_TOO_SMALL: return AVERROR(ENOBUFS);
40 #endif
41  case CELT_INTERNAL_ERROR: return AVERROR(EFAULT);
42  case CELT_CORRUPTED_DATA: return AVERROR_INVALIDDATA;
43  case CELT_UNIMPLEMENTED: return AVERROR(ENOSYS);
44 #ifdef ENOTRECOVERABLE
45  case CELT_INVALID_STATE: return AVERROR(ENOTRECOVERABLE);
46 #endif
47  case CELT_ALLOC_FAIL: return AVERROR(ENOMEM);
48  default: return AVERROR(EINVAL);
49  }
50 }
51 
52 static int ff_celt_bitstream_version_hack(CELTMode *mode)
53 {
54  CELTHeader header = { .version_id = 0 };
55  celt_header_init(&header, mode, 960, 2);
56  return header.version_id;
57 }
58 
60 {
61  struct libcelt_context *celt = c->priv_data;
62  int err;
63 
64  if (!c->channels || !c->frame_size ||
65  c->frame_size > INT_MAX / sizeof(int16_t) / c->channels)
66  return AVERROR(EINVAL);
67  celt->mode = celt_mode_create(c->sample_rate, c->frame_size, &err);
68  if (!celt->mode)
69  return ff_celt_error_to_averror(err);
70  celt->dec = celt_decoder_create_custom(celt->mode, c->channels, &err);
71  if (!celt->dec) {
72  celt_mode_destroy(celt->mode);
73  return ff_celt_error_to_averror(err);
74  }
75  if (c->extradata_size >= 4) {
76  celt->discard = AV_RL32(c->extradata);
77  if (celt->discard < 0 || celt->discard >= c->frame_size) {
79  "Invalid overlap (%d), ignored.\n", celt->discard);
80  celt->discard = 0;
81  }
82  }
83  if (c->extradata_size >= 8) {
84  unsigned version = AV_RL32(c->extradata + 4);
85  unsigned lib_version = ff_celt_bitstream_version_hack(celt->mode);
86  if (version != lib_version)
88  "CELT bitstream version 0x%x may be "
89  "improperly decoded by libcelt for version 0x%x.\n",
90  version, lib_version);
91  }
93  return 0;
94 }
95 
97 {
98  struct libcelt_context *celt = c->priv_data;
99 
100  celt_decoder_destroy(celt->dec);
101  celt_mode_destroy(celt->mode);
102  return 0;
103 }
104 
106  int *got_frame_ptr, AVPacket *pkt)
107 {
108  struct libcelt_context *celt = c->priv_data;
109  AVFrame *frame = data;
110  int err;
111  int16_t *pcm;
112 
113  frame->nb_samples = c->frame_size;
114  if ((err = ff_get_buffer(c, frame, 0)) < 0)
115  return err;
116  pcm = (int16_t *)frame->data[0];
117  err = celt_decode(celt->dec, pkt->data, pkt->size, pcm, c->frame_size);
118  if (err < 0)
119  return ff_celt_error_to_averror(err);
120  if (celt->discard) {
121  frame->nb_samples -= celt->discard;
122  memmove(pcm, pcm + celt->discard * c->channels,
123  frame->nb_samples * c->channels * sizeof(int16_t));
124  celt->discard = 0;
125  }
126  *got_frame_ptr = 1;
127  return pkt->size;
128 }
129 
131  .name = "libcelt",
132  .long_name = NULL_IF_CONFIG_SMALL("Xiph CELT decoder using libcelt"),
133  .type = AVMEDIA_TYPE_AUDIO,
134  .id = AV_CODEC_ID_CELT,
135  .priv_data_size = sizeof(struct libcelt_context),
136  .init = libcelt_dec_init,
137  .close = libcelt_dec_close,
138  .decode = libcelt_dec_decode,
139  .capabilities = AV_CODEC_CAP_DR1,
140 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int size
Definition: avcodec.h:1602
int version
Definition: avisynth_c.h:766
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3600
static int libcelt_dec_decode(AVCodecContext *c, void *data, int *got_frame_ptr, AVPacket *pkt)
Definition: libcelt_dec.c:105
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2446
#define av_cold
Definition: attributes.h:82
mode
Definition: f_perms.c:27
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1791
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1601
static const uint8_t header[24]
Definition: sdr2.c:67
#define av_log(a,...)
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
const char * name
Name of the codec implementation.
Definition: avcodec.h:3607
AVCodec ff_libcelt_decoder
Definition: libcelt_dec.c:130
CELTDecoder * dec
Definition: libcelt_dec.c:30
static int ff_celt_bitstream_version_hack(CELTMode *mode)
Definition: libcelt_dec.c:52
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2458
Libavcodec external API header.
int sample_rate
samples per second
Definition: avcodec.h:2438
main external API structure.
Definition: avcodec.h:1676
static av_cold int libcelt_dec_close(AVCodecContext *c)
Definition: libcelt_dec.c:96
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:947
CELTMode * mode
Definition: libcelt_dec.c:29
int extradata_size
Definition: avcodec.h:1792
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
common internal api header.
signed 16 bits
Definition: samplefmt.h:61
static double c[64]
void * priv_data
Definition: avcodec.h:1718
int channels
number of audio channels
Definition: avcodec.h:2439
static av_cold int libcelt_dec_init(AVCodecContext *c)
Definition: libcelt_dec.c:59
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
static int ff_celt_error_to_averror(int err)
Definition: libcelt_dec.c:34
This structure stores compressed data.
Definition: avcodec.h:1578
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959