00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "libavutil/common.h"
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/mathematics.h"
00024 #include "adx.h"
00025
00026 void ff_adx_calculate_coeffs(int cutoff, int sample_rate, int bits, int *coeff)
00027 {
00028 double a, b, c;
00029
00030 a = M_SQRT2 - cos(2.0 * M_PI * cutoff / sample_rate);
00031 b = M_SQRT2 - 1.0;
00032 c = (a - sqrt((a + b) * (a - b))) / b;
00033
00034 coeff[0] = lrintf(c * 2.0 * (1 << bits));
00035 coeff[1] = lrintf(-(c * c) * (1 << bits));
00036 }
00037
00038 int avpriv_adx_decode_header(AVCodecContext *avctx, const uint8_t *buf,
00039 int bufsize, int *header_size, int *coeff)
00040 {
00041 int offset, cutoff;
00042
00043 if (bufsize < 24)
00044 return AVERROR_INVALIDDATA;
00045
00046 if (AV_RB16(buf) != 0x8000)
00047 return AVERROR_INVALIDDATA;
00048 offset = AV_RB16(buf + 2) + 4;
00049
00050
00051 if (bufsize >= offset && memcmp(buf + offset - 6, "(c)CRI", 6))
00052 return AVERROR_INVALIDDATA;
00053
00054
00055 if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) {
00056 av_log_ask_for_sample(avctx, "unsupported ADX format\n");
00057 return AVERROR_PATCHWELCOME;
00058 }
00059
00060
00061 avctx->channels = buf[7];
00062 if (avctx->channels <= 0 || avctx->channels > 2)
00063 return AVERROR_INVALIDDATA;
00064
00065
00066 avctx->sample_rate = AV_RB32(buf + 8);
00067 if (avctx->sample_rate < 1 ||
00068 avctx->sample_rate > INT_MAX / (avctx->channels * BLOCK_SIZE * 8))
00069 return AVERROR_INVALIDDATA;
00070
00071
00072 avctx->bit_rate = avctx->sample_rate * avctx->channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES;
00073
00074
00075 if (coeff) {
00076 cutoff = AV_RB16(buf + 16);
00077 ff_adx_calculate_coeffs(cutoff, avctx->sample_rate, COEFF_BITS, coeff);
00078 }
00079
00080 *header_size = offset;
00081 return 0;
00082 }