annotate ffmpeg/libavcodec/adx.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 * Copyright (c) 2011 Justin Ruggles
yading@10 3 *
yading@10 4 * This file is part of Libav.
yading@10 5 *
yading@10 6 * Libav is free software; you can redistribute it and/or
yading@10 7 * modify it under the terms of the GNU Lesser General Public
yading@10 8 * License as published by the Free Software Foundation; either
yading@10 9 * version 2.1 of the License, or (at your option) any later version.
yading@10 10 *
yading@10 11 * Libav is distributed in the hope that it will be useful,
yading@10 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 14 * Lesser General Public License for more details.
yading@10 15 *
yading@10 16 * You should have received a copy of the GNU Lesser General Public
yading@10 17 * License along with Libav; if not, write to the Free Software
yading@10 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 19 */
yading@10 20
yading@10 21 #include "libavutil/common.h"
yading@10 22 #include "libavutil/intreadwrite.h"
yading@10 23 #include "libavutil/mathematics.h"
yading@10 24 #include "adx.h"
yading@10 25
yading@10 26 void ff_adx_calculate_coeffs(int cutoff, int sample_rate, int bits, int *coeff)
yading@10 27 {
yading@10 28 double a, b, c;
yading@10 29
yading@10 30 a = M_SQRT2 - cos(2.0 * M_PI * cutoff / sample_rate);
yading@10 31 b = M_SQRT2 - 1.0;
yading@10 32 c = (a - sqrt((a + b) * (a - b))) / b;
yading@10 33
yading@10 34 coeff[0] = lrintf(c * 2.0 * (1 << bits));
yading@10 35 coeff[1] = lrintf(-(c * c) * (1 << bits));
yading@10 36 }
yading@10 37
yading@10 38 int avpriv_adx_decode_header(AVCodecContext *avctx, const uint8_t *buf,
yading@10 39 int bufsize, int *header_size, int *coeff)
yading@10 40 {
yading@10 41 int offset, cutoff;
yading@10 42
yading@10 43 if (bufsize < 24)
yading@10 44 return AVERROR_INVALIDDATA;
yading@10 45
yading@10 46 if (AV_RB16(buf) != 0x8000)
yading@10 47 return AVERROR_INVALIDDATA;
yading@10 48 offset = AV_RB16(buf + 2) + 4;
yading@10 49
yading@10 50 if (offset < 6) {
yading@10 51 av_log(avctx, AV_LOG_ERROR, "offset is prior data\n");
yading@10 52 return AVERROR_INVALIDDATA;
yading@10 53 }
yading@10 54
yading@10 55 /* if copyright string is within the provided data, validate it */
yading@10 56 if (bufsize >= offset && memcmp(buf + offset - 6, "(c)CRI", 6))
yading@10 57 return AVERROR_INVALIDDATA;
yading@10 58
yading@10 59 /* check for encoding=3 block_size=18, sample_size=4 */
yading@10 60 if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) {
yading@10 61 avpriv_request_sample(avctx, "Support for this ADX format");
yading@10 62 return AVERROR_PATCHWELCOME;
yading@10 63 }
yading@10 64
yading@10 65 /* channels */
yading@10 66 avctx->channels = buf[7];
yading@10 67 if (avctx->channels <= 0 || avctx->channels > 2)
yading@10 68 return AVERROR_INVALIDDATA;
yading@10 69
yading@10 70 /* sample rate */
yading@10 71 avctx->sample_rate = AV_RB32(buf + 8);
yading@10 72 if (avctx->sample_rate < 1 ||
yading@10 73 avctx->sample_rate > INT_MAX / (avctx->channels * BLOCK_SIZE * 8))
yading@10 74 return AVERROR_INVALIDDATA;
yading@10 75
yading@10 76 /* bit rate */
yading@10 77 avctx->bit_rate = avctx->sample_rate * avctx->channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES;
yading@10 78
yading@10 79 /* LPC coefficients */
yading@10 80 if (coeff) {
yading@10 81 cutoff = AV_RB16(buf + 16);
yading@10 82 ff_adx_calculate_coeffs(cutoff, avctx->sample_rate, COEFF_BITS, coeff);
yading@10 83 }
yading@10 84
yading@10 85 *header_size = offset;
yading@10 86 return 0;
yading@10 87 }