annotate ffmpeg/libavcodec/libcelt_dec.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Xiph CELT decoder using libcelt
yading@10 3 * Copyright (c) 2011 Nicolas George
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #include <celt/celt.h>
yading@10 23 #include <celt/celt_header.h>
yading@10 24 #include "avcodec.h"
yading@10 25 #include "internal.h"
yading@10 26 #include "libavutil/intreadwrite.h"
yading@10 27
yading@10 28 struct libcelt_context {
yading@10 29 CELTMode *mode;
yading@10 30 CELTDecoder *dec;
yading@10 31 int discard;
yading@10 32 };
yading@10 33
yading@10 34 static int ff_celt_error_to_averror(int err)
yading@10 35 {
yading@10 36 switch (err) {
yading@10 37 case CELT_BAD_ARG: return AVERROR(EINVAL);
yading@10 38 #ifdef CELT_BUFFER_TOO_SMALL
yading@10 39 case CELT_BUFFER_TOO_SMALL: return AVERROR(ENOBUFS);
yading@10 40 #endif
yading@10 41 case CELT_INTERNAL_ERROR: return AVERROR(EFAULT);
yading@10 42 case CELT_CORRUPTED_DATA: return AVERROR_INVALIDDATA;
yading@10 43 case CELT_UNIMPLEMENTED: return AVERROR(ENOSYS);
yading@10 44 #ifdef ENOTRECOVERABLE
yading@10 45 case CELT_INVALID_STATE: return AVERROR(ENOTRECOVERABLE);
yading@10 46 #endif
yading@10 47 case CELT_ALLOC_FAIL: return AVERROR(ENOMEM);
yading@10 48 default: return AVERROR(EINVAL);
yading@10 49 }
yading@10 50 }
yading@10 51
yading@10 52 static int ff_celt_bitstream_version_hack(CELTMode *mode)
yading@10 53 {
yading@10 54 CELTHeader header = { .version_id = 0 };
yading@10 55 celt_header_init(&header, mode, 960, 2);
yading@10 56 return header.version_id;
yading@10 57 }
yading@10 58
yading@10 59 static av_cold int libcelt_dec_init(AVCodecContext *c)
yading@10 60 {
yading@10 61 struct libcelt_context *celt = c->priv_data;
yading@10 62 int err;
yading@10 63
yading@10 64 if (!c->channels || !c->frame_size ||
yading@10 65 c->frame_size > INT_MAX / sizeof(int16_t) / c->channels)
yading@10 66 return AVERROR(EINVAL);
yading@10 67 celt->mode = celt_mode_create(c->sample_rate, c->frame_size, &err);
yading@10 68 if (!celt->mode)
yading@10 69 return ff_celt_error_to_averror(err);
yading@10 70 celt->dec = celt_decoder_create_custom(celt->mode, c->channels, &err);
yading@10 71 if (!celt->dec) {
yading@10 72 celt_mode_destroy(celt->mode);
yading@10 73 return ff_celt_error_to_averror(err);
yading@10 74 }
yading@10 75 if (c->extradata_size >= 4) {
yading@10 76 celt->discard = AV_RL32(c->extradata);
yading@10 77 if (celt->discard < 0 || celt->discard >= c->frame_size) {
yading@10 78 av_log(c, AV_LOG_WARNING,
yading@10 79 "Invalid overlap (%d), ignored.\n", celt->discard);
yading@10 80 celt->discard = 0;
yading@10 81 }
yading@10 82 }
yading@10 83 if (c->extradata_size >= 8) {
yading@10 84 unsigned version = AV_RL32(c->extradata + 4);
yading@10 85 unsigned lib_version = ff_celt_bitstream_version_hack(celt->mode);
yading@10 86 if (version != lib_version)
yading@10 87 av_log(c, AV_LOG_WARNING,
yading@10 88 "CELT bitstream version 0x%x may be "
yading@10 89 "improperly decoded by libcelt for version 0x%x.\n",
yading@10 90 version, lib_version);
yading@10 91 }
yading@10 92 c->sample_fmt = AV_SAMPLE_FMT_S16;
yading@10 93 return 0;
yading@10 94 }
yading@10 95
yading@10 96 static av_cold int libcelt_dec_close(AVCodecContext *c)
yading@10 97 {
yading@10 98 struct libcelt_context *celt = c->priv_data;
yading@10 99
yading@10 100 celt_decoder_destroy(celt->dec);
yading@10 101 celt_mode_destroy(celt->mode);
yading@10 102 return 0;
yading@10 103 }
yading@10 104
yading@10 105 static int libcelt_dec_decode(AVCodecContext *c, void *data,
yading@10 106 int *got_frame_ptr, AVPacket *pkt)
yading@10 107 {
yading@10 108 struct libcelt_context *celt = c->priv_data;
yading@10 109 AVFrame *frame = data;
yading@10 110 int err;
yading@10 111 int16_t *pcm;
yading@10 112
yading@10 113 frame->nb_samples = c->frame_size;
yading@10 114 if ((err = ff_get_buffer(c, frame, 0)) < 0)
yading@10 115 return err;
yading@10 116 pcm = (int16_t *)frame->data[0];
yading@10 117 err = celt_decode(celt->dec, pkt->data, pkt->size, pcm, c->frame_size);
yading@10 118 if (err < 0)
yading@10 119 return ff_celt_error_to_averror(err);
yading@10 120 if (celt->discard) {
yading@10 121 frame->nb_samples -= celt->discard;
yading@10 122 memmove(pcm, pcm + celt->discard * c->channels,
yading@10 123 frame->nb_samples * c->channels * sizeof(int16_t));
yading@10 124 celt->discard = 0;
yading@10 125 }
yading@10 126 *got_frame_ptr = 1;
yading@10 127 return pkt->size;
yading@10 128 }
yading@10 129
yading@10 130 AVCodec ff_libcelt_decoder = {
yading@10 131 .name = "libcelt",
yading@10 132 .type = AVMEDIA_TYPE_AUDIO,
yading@10 133 .id = AV_CODEC_ID_CELT,
yading@10 134 .priv_data_size = sizeof(struct libcelt_context),
yading@10 135 .init = libcelt_dec_init,
yading@10 136 .close = libcelt_dec_close,
yading@10 137 .decode = libcelt_dec_decode,
yading@10 138 .capabilities = CODEC_CAP_DR1,
yading@10 139 .long_name = NULL_IF_CONFIG_SMALL("Xiph CELT decoder using libcelt"),
yading@10 140 };