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 if (offset < 6) {
00051 av_log(avctx, AV_LOG_ERROR, "offset is prior data\n");
00052 return AVERROR_INVALIDDATA;
00053 }
00054
00055
00056 if (bufsize >= offset && memcmp(buf + offset - 6, "(c)CRI", 6))
00057 return AVERROR_INVALIDDATA;
00058
00059
00060 if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) {
00061 av_log_ask_for_sample(avctx, "unsupported ADX format\n");
00062 return AVERROR_PATCHWELCOME;
00063 }
00064
00065
00066 avctx->channels = buf[7];
00067 if (avctx->channels <= 0 || avctx->channels > 2)
00068 return AVERROR_INVALIDDATA;
00069
00070
00071 avctx->sample_rate = AV_RB32(buf + 8);
00072 if (avctx->sample_rate < 1 ||
00073 avctx->sample_rate > INT_MAX / (avctx->channels * BLOCK_SIZE * 8))
00074 return AVERROR_INVALIDDATA;
00075
00076
00077 avctx->bit_rate = avctx->sample_rate * avctx->channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES;
00078
00079
00080 if (coeff) {
00081 cutoff = AV_RB16(buf + 16);
00082 ff_adx_calculate_coeffs(cutoff, avctx->sample_rate, COEFF_BITS, coeff);
00083 }
00084
00085 *header_size = offset;
00086 return 0;
00087 }