00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00050 if (bufsize >= offset && memcmp(buf + offset - 6, "(c)CRI", 6))
00051 return AVERROR_INVALIDDATA;
00052
00053
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
00060 avctx->channels = buf[7];
00061 if (avctx->channels <= 0 || avctx->channels > 2)
00062 return AVERROR_INVALIDDATA;
00063
00064
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
00071 avctx->bit_rate = avctx->sample_rate * avctx->channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES;
00072
00073
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 }