• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/adx.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011  Justin Ruggles
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include "libavutil/intreadwrite.h"
00022 #include "libavutil/mathematics.h"
00023 #include "adx.h"
00024 
00025 void ff_adx_calculate_coeffs(int cutoff, int sample_rate, int bits, int *coeff)
00026 {
00027     double a, b, c;
00028 
00029     a = M_SQRT2 - cos(2.0 * M_PI * cutoff / sample_rate);
00030     b = M_SQRT2 - 1.0;
00031     c = (a - sqrt((a + b) * (a - b))) / b;
00032 
00033     coeff[0] = lrintf(c * 2.0  * (1 << bits));
00034     coeff[1] = lrintf(-(c * c) * (1 << bits));
00035 }
00036 
00037 int avpriv_adx_decode_header(AVCodecContext *avctx, const uint8_t *buf,
00038                              int bufsize, int *header_size, int *coeff)
00039 {
00040     int offset, cutoff;
00041 
00042     if (bufsize < 24)
00043         return AVERROR_INVALIDDATA;
00044 
00045     if (AV_RB16(buf) != 0x8000)
00046         return AVERROR_INVALIDDATA;
00047     offset = AV_RB16(buf + 2) + 4;
00048 
00049     /* if copyright string is within the provided data, validate it */
00050     if (bufsize >= offset && memcmp(buf + offset - 6, "(c)CRI", 6))
00051         return AVERROR_INVALIDDATA;
00052 
00053     /* check for encoding=3 block_size=18, sample_size=4 */
00054     if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) {
00055         av_log_ask_for_sample(avctx, "unsupported ADX format\n");
00056         return AVERROR_PATCHWELCOME;
00057     }
00058 
00059     /* channels */
00060     avctx->channels = buf[7];
00061     if (avctx->channels <= 0 || avctx->channels > 2)
00062         return AVERROR_INVALIDDATA;
00063 
00064     /* sample rate */
00065     avctx->sample_rate = AV_RB32(buf + 8);
00066     if (avctx->sample_rate < 1 ||
00067         avctx->sample_rate > INT_MAX / (avctx->channels * BLOCK_SIZE * 8))
00068         return AVERROR_INVALIDDATA;
00069 
00070     /* bit rate */
00071     avctx->bit_rate = avctx->sample_rate * avctx->channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES;
00072 
00073     /* LPC coefficients */
00074     if (coeff) {
00075         cutoff = AV_RB16(buf + 16);
00076         ff_adx_calculate_coeffs(cutoff, avctx->sample_rate, COEFF_BITS, coeff);
00077     }
00078 
00079     *header_size = offset;
00080     return 0;
00081 }
Generated on Sun Apr 22 2012 21:53:59 for Libav by doxygen 1.7.1