FFmpeg
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 "codec_internal.h"
26 #include "decode.h"
27 #include "libavutil/intreadwrite.h"
28 
30  CELTMode *mode;
31  CELTDecoder *dec;
32  int discard;
33 };
34 
35 static int ff_celt_error_to_averror(int err)
36 {
37  switch (err) {
38  case CELT_BAD_ARG: return AVERROR(EINVAL);
39 #ifdef CELT_BUFFER_TOO_SMALL
40  case CELT_BUFFER_TOO_SMALL: return AVERROR(ENOBUFS);
41 #endif
42  case CELT_INTERNAL_ERROR: return AVERROR(EFAULT);
43  case CELT_CORRUPTED_DATA: return AVERROR_INVALIDDATA;
44  case CELT_UNIMPLEMENTED: return AVERROR(ENOSYS);
45 #ifdef ENOTRECOVERABLE
46  case CELT_INVALID_STATE: return AVERROR(ENOTRECOVERABLE);
47 #endif
48  case CELT_ALLOC_FAIL: return AVERROR(ENOMEM);
49  default: return AVERROR(EINVAL);
50  }
51 }
52 
53 static int ff_celt_bitstream_version_hack(CELTMode *mode)
54 {
55  CELTHeader header = { .version_id = 0 };
56  celt_header_init(&header, mode, 960, 2);
57  return header.version_id;
58 }
59 
61 {
62  struct libcelt_context *celt = c->priv_data;
63  int err;
64 
65  if (!c->ch_layout.nb_channels || !c->frame_size ||
66  c->frame_size > INT_MAX / sizeof(int16_t) / c->ch_layout.nb_channels)
67  return AVERROR(EINVAL);
68  celt->mode = celt_mode_create(c->sample_rate, c->frame_size, &err);
69  if (!celt->mode)
70  return ff_celt_error_to_averror(err);
71  celt->dec = celt_decoder_create_custom(celt->mode, c->ch_layout.nb_channels, &err);
72  if (!celt->dec) {
73  celt_mode_destroy(celt->mode);
74  return ff_celt_error_to_averror(err);
75  }
76  if (c->extradata_size >= 4) {
77  celt->discard = AV_RL32(c->extradata);
78  if (celt->discard < 0 || celt->discard >= c->frame_size) {
80  "Invalid overlap (%d), ignored.\n", celt->discard);
81  celt->discard = 0;
82  }
83  }
84  if (c->extradata_size >= 8) {
85  unsigned version = AV_RL32(c->extradata + 4);
86  unsigned lib_version = ff_celt_bitstream_version_hack(celt->mode);
87  if (version != lib_version)
89  "CELT bitstream version 0x%x may be "
90  "improperly decoded by libcelt for version 0x%x.\n",
91  version, lib_version);
92  }
93  c->sample_fmt = AV_SAMPLE_FMT_S16;
94  return 0;
95 }
96 
98 {
99  struct libcelt_context *celt = c->priv_data;
100 
101  celt_decoder_destroy(celt->dec);
102  celt_mode_destroy(celt->mode);
103  return 0;
104 }
105 
107  int *got_frame_ptr, AVPacket *pkt)
108 {
109  struct libcelt_context *celt = c->priv_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->ch_layout.nb_channels,
123  frame->nb_samples * c->ch_layout.nb_channels * sizeof(int16_t));
124  celt->discard = 0;
125  }
126  *got_frame_ptr = 1;
127  return pkt->size;
128 }
129 
131  .p.name = "libcelt",
132  CODEC_LONG_NAME("Xiph CELT decoder using libcelt"),
133  .p.type = AVMEDIA_TYPE_AUDIO,
134  .p.id = AV_CODEC_ID_CELT,
135  .p.capabilities = AV_CODEC_CAP_DR1,
136  .p.wrapper_name = "libcelt",
137  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
138  .priv_data_size = sizeof(struct libcelt_context),
140  .close = libcelt_dec_close,
142 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVPacket::data
uint8_t * data
Definition: packet.h:524
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:34
FFCodec
Definition: codec_internal.h:126
ff_celt_bitstream_version_hack
static int ff_celt_bitstream_version_hack(CELTMode *mode)
Definition: libcelt_dec.c:53
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
ff_libcelt_decoder
const FFCodec ff_libcelt_decoder
Definition: libcelt_dec.c:130
pkt
AVPacket * pkt
Definition: movenc.c:60
av_cold
#define av_cold
Definition: attributes.h:90
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:286
intreadwrite.h
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
libcelt_dec_decode
static int libcelt_dec_decode(AVCodecContext *c, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition: libcelt_dec.c:106
decode.h
libcelt_context::dec
CELTDecoder * dec
Definition: libcelt_dec.c:31
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:271
ff_celt_error_to_averror
static int ff_celt_error_to_averror(int err)
Definition: libcelt_dec.c:35
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1556
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:525
codec_internal.h
header
static const uint8_t header[24]
Definition: sdr2.c:68
version
version
Definition: libkvazaar.c:321
libcelt_dec_init
static av_cold int libcelt_dec_init(AVCodecContext *c)
Definition: libcelt_dec.c:60
libcelt_context::discard
int discard
Definition: libcelt_dec.c:32
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
avcodec.h
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVCodecContext
main external API structure.
Definition: avcodec.h:445
mode
mode
Definition: ebur128.h:83
libcelt_context::mode
CELTMode * mode
Definition: libcelt_dec.c:30
libcelt_dec_close
static av_cold int libcelt_dec_close(AVCodecContext *c)
Definition: libcelt_dec.c:97
AVPacket
This structure stores compressed data.
Definition: packet.h:501
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_CELT
@ AV_CODEC_ID_CELT
Definition: codec_id.h:491
libcelt_context
Definition: libcelt_dec.c:29