FFmpeg
 All Data Structures 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;
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->channels || !c->frame_size ||
66  c->frame_size > INT_MAX / sizeof(int16_t) / c->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->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  }
95  c->coded_frame = &celt->frame;
96  return 0;
97 }
98 
100 {
101  struct libcelt_context *celt = c->priv_data;
102 
103  celt_decoder_destroy(celt->dec);
104  celt_mode_destroy(celt->mode);
105  return 0;
106 }
107 
109  int *got_frame_ptr, AVPacket *pkt)
110 {
111  struct libcelt_context *celt = c->priv_data;
112  int err;
113  int16_t *pcm;
114 
115  celt->frame.nb_samples = c->frame_size;
116  err = ff_get_buffer(c, &celt->frame);
117  if (err < 0) {
118  av_log(c, AV_LOG_ERROR, "get_buffer() failed\n");
119  return err;
120  }
121  pcm = (int16_t *)celt->frame.data[0];
122  err = celt_decode(celt->dec, pkt->data, pkt->size, pcm, c->frame_size);
123  if (err < 0)
124  return ff_celt_error_to_averror(err);
125  if (celt->discard) {
126  celt->frame.nb_samples -= celt->discard;
127  memmove(pcm, pcm + celt->discard * c->channels,
128  celt->frame.nb_samples * c->channels * sizeof(int16_t));
129  celt->discard = 0;
130  }
131  *got_frame_ptr = 1;
132  *(AVFrame *)frame = celt->frame;
133  return pkt->size;
134 }
135 
137  .name = "libcelt",
138  .type = AVMEDIA_TYPE_AUDIO,
139  .id = AV_CODEC_ID_CELT,
140  .priv_data_size = sizeof(struct libcelt_context),
141  .init = libcelt_dec_init,
142  .close = libcelt_dec_close,
143  .decode = libcelt_dec_decode,
144  .capabilities = CODEC_CAP_DR1,
145  .long_name = NULL_IF_CONFIG_SMALL("Xiph CELT decoder using libcelt"),
146 };