mirror of https://github.com/FFmpeg/FFmpeg.git
* qatar/master: (42 commits) swscale: fix signed overflow in yuv2mono_X_c_template snow: fix integer overflows svq1enc: remove stale altivec-related hack snow: fix signed overflow in byte to 32-bit replication adx: rename ff_adx_decode_header() to avpriv_adx_decode_header() avformat: add CRI ADX format demuxer adx: add an ADX parser. adx: move header decoding to ADX common code adx: calculate the number of blocks in a packet adx: define and use 2 new macro constants BLOCK_SIZE and BLOCK_SAMPLES adx: check for unsupported ADX formats adx: simplify encoding by using put_sbits() adx: calculate correct LPC coeffs adx: use 12-bit coefficients instead of 14-bit to avoid integer overflow adx: simplify adx_decode() by using get_sbits() to read residual samples adx: fix the data offset parsing in adx_decode_header() adx: remove unneeded post-decode channel interleaving adx: validate header values adx: cosmetics: general pretty-printing and comment clean-up adx: remove useless comments ... Conflicts: Changelog libavcodec/cook.c libavcodec/fraps.c libavcodec/nuv.c libavcodec/pthread.c libavcodec/version.h libavformat/Makefile libavformat/version.h Merged-by: Michael Niedermayer <michaelni@gmx.at>pull/2/head
commit
5c15b78e4a
34 changed files with 494 additions and 182 deletions
@ -0,0 +1,81 @@ |
||||
/*
|
||||
* Copyright (c) 2011 Justin Ruggles |
||||
* |
||||
* This file is part of Libav. |
||||
* |
||||
* Libav is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU Lesser General Public |
||||
* License as published by the Free Software Foundation; either |
||||
* version 2.1 of the License, or (at your option) any later version. |
||||
* |
||||
* Libav is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||
* Lesser General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public |
||||
* License along with Libav; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||
*/ |
||||
|
||||
#include "libavutil/intreadwrite.h" |
||||
#include "libavutil/mathematics.h" |
||||
#include "adx.h" |
||||
|
||||
void ff_adx_calculate_coeffs(int cutoff, int sample_rate, int bits, int *coeff) |
||||
{ |
||||
double a, b, c; |
||||
|
||||
a = M_SQRT2 - cos(2.0 * M_PI * cutoff / sample_rate); |
||||
b = M_SQRT2 - 1.0; |
||||
c = (a - sqrt((a + b) * (a - b))) / b; |
||||
|
||||
coeff[0] = lrintf(c * 2.0 * (1 << bits)); |
||||
coeff[1] = lrintf(-(c * c) * (1 << bits)); |
||||
} |
||||
|
||||
int avpriv_adx_decode_header(AVCodecContext *avctx, const uint8_t *buf, |
||||
int bufsize, int *header_size, int *coeff) |
||||
{ |
||||
int offset, cutoff; |
||||
|
||||
if (bufsize < 24) |
||||
return AVERROR_INVALIDDATA; |
||||
|
||||
if (AV_RB16(buf) != 0x8000) |
||||
return AVERROR_INVALIDDATA; |
||||
offset = AV_RB16(buf + 2) + 4; |
||||
|
||||
/* if copyright string is within the provided data, validate it */ |
||||
if (bufsize >= offset && memcmp(buf + offset - 6, "(c)CRI", 6)) |
||||
return AVERROR_INVALIDDATA; |
||||
|
||||
/* check for encoding=3 block_size=18, sample_size=4 */ |
||||
if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) { |
||||
av_log_ask_for_sample(avctx, "unsupported ADX format\n"); |
||||
return AVERROR_PATCHWELCOME; |
||||
} |
||||
|
||||
/* channels */ |
||||
avctx->channels = buf[7]; |
||||
if (avctx->channels > 2) |
||||
return AVERROR_INVALIDDATA; |
||||
|
||||
/* sample rate */ |
||||
avctx->sample_rate = AV_RB32(buf + 8); |
||||
if (avctx->sample_rate < 1 || |
||||
avctx->sample_rate > INT_MAX / (avctx->channels * BLOCK_SIZE * 8)) |
||||
return AVERROR_INVALIDDATA; |
||||
|
||||
/* bit rate */ |
||||
avctx->bit_rate = avctx->sample_rate * avctx->channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES; |
||||
|
||||
/* LPC coefficients */ |
||||
if (coeff) { |
||||
cutoff = AV_RB16(buf + 16); |
||||
ff_adx_calculate_coeffs(cutoff, avctx->sample_rate, COEFF_BITS, coeff); |
||||
} |
||||
|
||||
*header_size = offset; |
||||
return 0; |
||||
} |
@ -0,0 +1,104 @@ |
||||
/*
|
||||
* Copyright (c) 2011 Justin Ruggles |
||||
* |
||||
* This file is part of Libav. |
||||
* |
||||
* Libav is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU Lesser General Public |
||||
* License as published by the Free Software Foundation; either |
||||
* version 2.1 of the License, or (at your option) any later version. |
||||
* |
||||
* Libav is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||
* Lesser General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public |
||||
* License along with Libav; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||
*/ |
||||
|
||||
/**
|
||||
* @file |
||||
* ADX audio parser |
||||
* |
||||
* Reads header to extradata and splits packets into individual blocks. |
||||
*/ |
||||
|
||||
#include "libavutil/intreadwrite.h" |
||||
#include "parser.h" |
||||
#include "adx.h" |
||||
|
||||
typedef struct ADXParseContext { |
||||
ParseContext pc; |
||||
int header_size; |
||||
int block_size; |
||||
int buf_pos; |
||||
} ADXParseContext; |
||||
|
||||
#define MIN_HEADER_SIZE 24 |
||||
|
||||
static int adx_parse(AVCodecParserContext *s1, |
||||
AVCodecContext *avctx, |
||||
const uint8_t **poutbuf, int *poutbuf_size, |
||||
const uint8_t *buf, int buf_size) |
||||
{ |
||||
ADXParseContext *s = s1->priv_data; |
||||
ParseContext *pc = &s->pc; |
||||
int next = END_NOT_FOUND; |
||||
|
||||
if (!avctx->extradata_size) { |
||||
int ret; |
||||
|
||||
ff_combine_frame(pc, END_NOT_FOUND, &buf, &buf_size); |
||||
|
||||
if (!s->header_size && pc->index >= MIN_HEADER_SIZE) { |
||||
if (ret = avpriv_adx_decode_header(avctx, pc->buffer, pc->index, |
||||
&s->header_size, NULL)) |
||||
return AVERROR_INVALIDDATA; |
||||
s->block_size = BLOCK_SIZE * avctx->channels; |
||||
} |
||||
if (s->header_size && s->header_size <= pc->index) { |
||||
avctx->extradata = av_mallocz(s->header_size + FF_INPUT_BUFFER_PADDING_SIZE); |
||||
if (!avctx->extradata) |
||||
return AVERROR(ENOMEM); |
||||
avctx->extradata_size = s->header_size; |
||||
memcpy(avctx->extradata, pc->buffer, s->header_size); |
||||
memmove(pc->buffer, pc->buffer + s->header_size, s->header_size); |
||||
pc->index -= s->header_size; |
||||
} |
||||
*poutbuf = NULL; |
||||
*poutbuf_size = 0; |
||||
return buf_size; |
||||
} |
||||
|
||||
if (pc->index - s->buf_pos >= s->block_size) { |
||||
*poutbuf = &pc->buffer[s->buf_pos]; |
||||
*poutbuf_size = s->block_size; |
||||
s->buf_pos += s->block_size; |
||||
return 0; |
||||
} |
||||
if (pc->index && s->buf_pos) { |
||||
memmove(pc->buffer, &pc->buffer[s->buf_pos], pc->index - s->buf_pos); |
||||
pc->index -= s->buf_pos; |
||||
s->buf_pos = 0; |
||||
} |
||||
if (buf_size + pc->index >= s->block_size) |
||||
next = s->block_size - pc->index; |
||||
|
||||
if (ff_combine_frame(pc, next, &buf, &buf_size) < 0 || !buf_size) { |
||||
*poutbuf = NULL; |
||||
*poutbuf_size = 0; |
||||
return buf_size; |
||||
} |
||||
*poutbuf = buf; |
||||
*poutbuf_size = buf_size; |
||||
return next; |
||||
} |
||||
|
||||
AVCodecParser ff_adx_parser = { |
||||
.codec_ids = { CODEC_ID_ADPCM_ADX }, |
||||
.priv_data_size = sizeof(ADXParseContext), |
||||
.parser_parse = adx_parse, |
||||
.parser_close = ff_parse_close, |
||||
}; |
@ -0,0 +1,111 @@ |
||||
/*
|
||||
* Copyright (c) 2011 Justin Ruggles |
||||
* |
||||
* This file is part of Libav. |
||||
* |
||||
* Libav is free software; you can redistribute it and/or |
||||
* modify it under the terms of the GNU Lesser General Public |
||||
* License as published by the Free Software Foundation; either |
||||
* version 2.1 of the License, or (at your option) any later version. |
||||
* |
||||
* Libav is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||
* Lesser General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU Lesser General Public |
||||
* License along with Libav; if not, write to the Free Software |
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||
*/ |
||||
|
||||
/**
|
||||
* @file |
||||
* CRI ADX demuxer |
||||
*/ |
||||
|
||||
#include "libavutil/intreadwrite.h" |
||||
#include "libavcodec/adx.h" |
||||
#include "avformat.h" |
||||
|
||||
#define BLOCK_SIZE 18 |
||||
#define BLOCK_SAMPLES 32 |
||||
|
||||
typedef struct ADXDemuxerContext { |
||||
int header_size; |
||||
} ADXDemuxerContext; |
||||
|
||||
static int adx_read_packet(AVFormatContext *s, AVPacket *pkt) |
||||
{ |
||||
ADXDemuxerContext *c = s->priv_data; |
||||
AVCodecContext *avctx = s->streams[0]->codec; |
||||
int ret, size; |
||||
|
||||
size = BLOCK_SIZE * avctx->channels; |
||||
|
||||
pkt->pos = avio_tell(s->pb); |
||||
pkt->stream_index = 0; |
||||
|
||||
ret = av_get_packet(s->pb, pkt, size); |
||||
if (ret != size) { |
||||
av_free_packet(pkt); |
||||
return ret < 0 ? ret : AVERROR(EIO); |
||||
} |
||||
if (AV_RB16(pkt->data) & 0x8000) { |
||||
av_free_packet(pkt); |
||||
return AVERROR_EOF; |
||||
} |
||||
pkt->size = size; |
||||
pkt->duration = 1; |
||||
pkt->pts = (pkt->pos - c->header_size) / size; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int adx_read_header(AVFormatContext *s, AVFormatParameters *ap) |
||||
{ |
||||
ADXDemuxerContext *c = s->priv_data; |
||||
AVCodecContext *avctx; |
||||
int ret; |
||||
|
||||
AVStream *st = avformat_new_stream(s, NULL); |
||||
if (!st) |
||||
return AVERROR(ENOMEM); |
||||
avctx = s->streams[0]->codec; |
||||
|
||||
if (avio_rb16(s->pb) != 0x8000) |
||||
return AVERROR_INVALIDDATA; |
||||
c->header_size = avio_rb16(s->pb) + 4; |
||||
avio_seek(s->pb, -4, SEEK_CUR); |
||||
|
||||
avctx->extradata = av_mallocz(c->header_size + FF_INPUT_BUFFER_PADDING_SIZE); |
||||
if (!avctx->extradata) |
||||
return AVERROR(ENOMEM); |
||||
if (avio_read(s->pb, avctx->extradata, c->header_size) < c->header_size) { |
||||
av_freep(&avctx->extradata); |
||||
return AVERROR(EIO); |
||||
} |
||||
avctx->extradata_size = c->header_size; |
||||
|
||||
ret = avpriv_adx_decode_header(avctx, avctx->extradata, |
||||
avctx->extradata_size, &c->header_size, |
||||
NULL); |
||||
if (ret) |
||||
return ret; |
||||
|
||||
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
||||
st->codec->codec_id = s->iformat->value; |
||||
|
||||
av_set_pts_info(st, 64, BLOCK_SAMPLES, avctx->sample_rate); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
AVInputFormat ff_adx_demuxer = { |
||||
.name = "adx", |
||||
.long_name = NULL_IF_CONFIG_SMALL("CRI ADX"), |
||||
.priv_data_size = sizeof(ADXDemuxerContext), |
||||
.read_header = adx_read_header, |
||||
.read_packet = adx_read_packet, |
||||
.extensions = "adx", |
||||
.value = CODEC_ID_ADPCM_ADX, |
||||
}; |
Loading…
Reference in new issue