Commit db9f426c authored by Michael Niedermayer's avatar Michael Niedermayer

Merge commit '8ac0f676'

* commit '8ac0f676':
  dcadec: allow the decoder to change the channel layout mid-stream
  cook: use av_dlog() for debug logging instead of av_log() with AV_LOG_ERROR
  cook: move samples_per_frame from COOKSubpacket to where it is used
  cook: use av_get_channel_layout_nb_channels() instead of cook_count_channels()
  cook: reverse a condition so that the code makes more sense
  cook: remove unneeded COOKContext variable, sample_rate
  cook: remove unneeded COOKContext variable, bit_rate
  cook: use AVCodecContext.channels instead of keeping a private copy
  bmvaudio: set channel layout at init() rather than validating it
  atrac1: do not keep a copy of channel count in the private context
  dsicinaudio: set channels and channel layout
  g722dec: set channel layout at initialization instead of validating it
  amrwbdec: set channels, channel_layout, and sample_rate
  amrnbdec: set channels, channel_layout, and sample_rate
  dca_parser: allow the parser to change the sample rate
  lavc: check channel count after decoder init
  lavc: move SANE_NB_CHANNELS to internal.h and use it in the PCM decoders

Conflicts:
	libavcodec/dcadec.c
	libavcodec/pcm.c
Merged-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parents a5f6720f 8ac0f676
......@@ -43,6 +43,7 @@
#include <string.h>
#include <math.h>
#include "libavutil/audioconvert.h"
#include "avcodec.h"
#include "dsputil.h"
#include "libavutil/common.h"
......@@ -161,7 +162,15 @@ static av_cold int amrnb_decode_init(AVCodecContext *avctx)
AMRContext *p = avctx->priv_data;
int i;
avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
if (avctx->channels > 1) {
av_log_missing_feature(avctx, "multi-channel AMR", 0);
return AVERROR_PATCHWELCOME;
}
avctx->channels = 1;
avctx->channel_layout = AV_CH_LAYOUT_MONO;
avctx->sample_rate = 8000;
avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
// p->excitation always points to the same position in p->excitation_buf
p->excitation = &p->excitation_buf[PITCH_DELAY_MAX + LP_FILTER_ORDER + 1];
......
......@@ -24,6 +24,7 @@
* AMR wideband decoder
*/
#include "libavutil/audioconvert.h"
#include "libavutil/common.h"
#include "libavutil/lfg.h"
......@@ -97,7 +98,15 @@ static av_cold int amrwb_decode_init(AVCodecContext *avctx)
AMRWBContext *ctx = avctx->priv_data;
int i;
avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
if (avctx->channels > 1) {
av_log_missing_feature(avctx, "multi-channel AMR", 0);
return AVERROR_PATCHWELCOME;
}
avctx->channels = 1;
avctx->channel_layout = AV_CH_LAYOUT_MONO;
avctx->sample_rate = 16000;
avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
av_lfg_init(&ctx->prng, 1);
......
......@@ -80,7 +80,6 @@ typedef struct {
DECLARE_ALIGNED(32, float, high)[512];
float* bands[3];
FFTContext mdct_ctx[3];
int channels;
DSPContext dsp;
} AT1Ctx;
......@@ -280,7 +279,7 @@ static int atrac1_decode_frame(AVCodecContext *avctx, void *data,
GetBitContext gb;
if (buf_size < 212 * q->channels) {
if (buf_size < 212 * avctx->channels) {
av_log(avctx, AV_LOG_ERROR, "Not enough data to decode!\n");
return AVERROR_INVALIDDATA;
}
......@@ -292,7 +291,7 @@ static int atrac1_decode_frame(AVCodecContext *avctx, void *data,
return ret;
}
for (ch = 0; ch < q->channels; ch++) {
for (ch = 0; ch < avctx->channels; ch++) {
AT1SUCtx* su = &q->SUs[ch];
init_get_bits(&gb, &buf[212 * ch], 212 * 8);
......@@ -343,7 +342,6 @@ static av_cold int atrac1_decode_init(AVCodecContext *avctx)
avctx->channels);
return AVERROR(EINVAL);
}
q->channels = avctx->channels;
/* Init the mdct transforms */
if ((ret = ff_mdct_init(&q->mdct_ctx[0], 6, 1, -1.0/ (1 << 15))) ||
......
......@@ -19,6 +19,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/audioconvert.h"
#include "avcodec.h"
#include "bytestream.h"
#include "libavutil/avassert.h"
......@@ -311,12 +312,9 @@ static av_cold int bmv_aud_decode_init(AVCodecContext *avctx)
{
BMVAudioDecContext *c = avctx->priv_data;
if (avctx->channels != 2) {
av_log(avctx, AV_LOG_INFO, "invalid number of channels\n");
return AVERROR(EINVAL);
}
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
avctx->channels = 2;
avctx->channel_layout = AV_CH_LAYOUT_STEREO;
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
avcodec_get_frame_defaults(&c->frame);
avctx->coded_frame = &c->frame;
......
......@@ -72,7 +72,6 @@ typedef struct {
int size;
int num_channels;
int cookversion;
int samples_per_frame;
int subbands;
int js_subband_start;
int js_vlc_bits;
......@@ -126,9 +125,6 @@ typedef struct cook {
AVFrame frame;
GetBitContext gb;
/* stream data */
int nb_channels;
int bit_rate;
int sample_rate;
int num_vectors;
int samples_per_channel;
/* states */
......@@ -1028,19 +1024,18 @@ static int cook_decode_frame(AVCodecContext *avctx, void *data,
static void dump_cook_context(COOKContext *q)
{
//int i=0;
#define PRINT(a, b) av_log(q->avctx, AV_LOG_ERROR, " %s = %d\n", a, b);
av_log(q->avctx, AV_LOG_ERROR, "COOKextradata\n");
av_log(q->avctx, AV_LOG_ERROR, "cookversion=%x\n", q->subpacket[0].cookversion);
#define PRINT(a, b) av_dlog(q->avctx, " %s = %d\n", a, b);
av_dlog(q->avctx, "COOKextradata\n");
av_dlog(q->avctx, "cookversion=%x\n", q->subpacket[0].cookversion);
if (q->subpacket[0].cookversion > STEREO) {
PRINT("js_subband_start", q->subpacket[0].js_subband_start);
PRINT("js_vlc_bits", q->subpacket[0].js_vlc_bits);
}
av_log(q->avctx, AV_LOG_ERROR, "COOKContext\n");
PRINT("nb_channels", q->nb_channels);
PRINT("bit_rate", q->bit_rate);
PRINT("sample_rate", q->sample_rate);
av_dlog(q->avctx, "COOKContext\n");
PRINT("nb_channels", q->avctx->channels);
PRINT("bit_rate", q->avctx->bit_rate);
PRINT("sample_rate", q->avctx->sample_rate);
PRINT("samples_per_channel", q->subpacket[0].samples_per_channel);
PRINT("samples_per_frame", q->subpacket[0].samples_per_frame);
PRINT("subbands", q->subpacket[0].subbands);
PRINT("js_subband_start", q->subpacket[0].js_subband_start);
PRINT("log2_numvector_size", q->subpacket[0].log2_numvector_size);
......@@ -1049,16 +1044,6 @@ static void dump_cook_context(COOKContext *q)
}
#endif
static av_cold int cook_count_channels(unsigned int mask)
{
int i;
int channels = 0;
for (i = 0; i < 32; i++)
if (mask & (1 << i))
++channels;
return channels;
}
/**
* Cook initialization
*
......@@ -1072,6 +1057,7 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
int extradata_size = avctx->extradata_size;
int s = 0;
unsigned int channel_mask = 0;
int samples_per_frame;
int ret;
q->avctx = avctx;
......@@ -1083,10 +1069,7 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
av_log(avctx, AV_LOG_DEBUG, "codecdata_length=%d\n", avctx->extradata_size);
/* Take data from the AVCodecContext (RM container). */
q->sample_rate = avctx->sample_rate;
q->nb_channels = avctx->channels;
q->bit_rate = avctx->bit_rate;
if (!q->nb_channels) {
if (!avctx->channels) {
av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
return AVERROR_INVALIDDATA;
}
......@@ -1101,7 +1084,7 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
Swap to right endianness so we don't need to care later on. */
if (extradata_size >= 8) {
q->subpacket[s].cookversion = bytestream_get_be32(&edata_ptr);
q->subpacket[s].samples_per_frame = bytestream_get_be16(&edata_ptr);
samples_per_frame = bytestream_get_be16(&edata_ptr);
q->subpacket[s].subbands = bytestream_get_be16(&edata_ptr);
extradata_size -= 8;
}
......@@ -1113,7 +1096,7 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
}
/* Initialize extradata related variables. */
q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame / q->nb_channels;
q->subpacket[s].samples_per_channel = samples_per_frame / avctx->channels;
q->subpacket[s].bits_per_subpacket = avctx->block_align * 8;
/* Initialize default data states. */
......@@ -1128,21 +1111,21 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
q->subpacket[s].joint_stereo = 0;
switch (q->subpacket[s].cookversion) {
case MONO:
if (q->nb_channels != 1) {
if (avctx->channels != 1) {
av_log_ask_for_sample(avctx, "Container channels != 1.\n");
return AVERROR_PATCHWELCOME;
}
av_log(avctx, AV_LOG_DEBUG, "MONO\n");
break;
case STEREO:
if (q->nb_channels != 1) {
if (avctx->channels != 1) {
q->subpacket[s].bits_per_subpdiv = 1;
q->subpacket[s].num_channels = 2;
}
av_log(avctx, AV_LOG_DEBUG, "STEREO\n");
break;
case JOINT_STEREO:
if (q->nb_channels != 2) {
if (avctx->channels != 2) {
av_log_ask_for_sample(avctx, "Container channels != 2.\n");
return AVERROR_PATCHWELCOME;
}
......@@ -1165,12 +1148,12 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
if (extradata_size >= 4)
channel_mask |= q->subpacket[s].channel_mask = bytestream_get_be32(&edata_ptr);
if (cook_count_channels(q->subpacket[s].channel_mask) > 1) {
if (av_get_channel_layout_nb_channels(q->subpacket[s].channel_mask) > 1) {
q->subpacket[s].total_subbands = q->subpacket[s].subbands +
q->subpacket[s].js_subband_start;
q->subpacket[s].joint_stereo = 1;
q->subpacket[s].num_channels = 2;
q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame >> 1;
q->subpacket[s].samples_per_channel = samples_per_frame >> 1;
if (q->subpacket[s].samples_per_channel > 256) {
q->subpacket[s].log2_numvector_size = 6;
......@@ -1179,7 +1162,7 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
q->subpacket[s].log2_numvector_size = 7;
}
} else
q->subpacket[s].samples_per_channel = q->subpacket[s].samples_per_frame;
q->subpacket[s].samples_per_channel = samples_per_frame;
break;
default:
......@@ -1219,8 +1202,8 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
q->subpacket[s].gains2.now = q->subpacket[s].gain_3;
q->subpacket[s].gains2.previous = q->subpacket[s].gain_4;
if (q->num_subpackets + q->subpacket[s].num_channels > q->nb_channels) {
av_log(avctx, AV_LOG_ERROR, "Too many subpackets %d for channels %d\n", q->num_subpackets, q->nb_channels);
if (q->num_subpackets + q->subpacket[s].num_channels > q->avctx->channels) {
av_log(avctx, AV_LOG_ERROR, "Too many subpackets %d for channels %d\n", q->num_subpackets, q->avctx->channels);
return AVERROR_INVALIDDATA;
}
......@@ -1267,9 +1250,8 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
}
/* Try to catch some obviously faulty streams, othervise it might be exploitable */
if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512)
|| (q->samples_per_channel == 1024)) {
} else {
if (q->samples_per_channel != 256 && q->samples_per_channel != 512 &&
q->samples_per_channel != 1024) {
av_log_ask_for_sample(avctx,
"unknown amount of samples_per_channel = %d\n",
q->samples_per_channel);
......
......@@ -190,8 +190,7 @@ static int dca_parse(AVCodecParserContext * s,
/* read the duration and sample rate from the frame header */
if (!dca_parse_params(buf, buf_size, &duration, &sample_rate)) {
s->duration = duration;
if (!avctx->sample_rate)
avctx->sample_rate = sample_rate;
avctx->sample_rate = sample_rate;
} else
s->duration = 0;
......
......@@ -24,6 +24,7 @@
* Delphine Software International CIN audio/video decoders
*/
#include "libavutil/audioconvert.h"
#include "avcodec.h"
#include "bytestream.h"
#include "mathops.h"
......@@ -341,14 +342,11 @@ static av_cold int cinaudio_decode_init(AVCodecContext *avctx)
{
CinAudioContext *cin = avctx->priv_data;
if (avctx->channels != 1) {
av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
return AVERROR_PATCHWELCOME;
}
cin->initial_decode_frame = 1;
cin->delta = 0;
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
cin->delta = 0;
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
avctx->channels = 1;
avctx->channel_layout = AV_CH_LAYOUT_MONO;
avcodec_get_frame_defaults(&cin->frame);
avctx->coded_frame = &cin->frame;
......
......@@ -34,6 +34,7 @@
* respectively of each byte are ignored.
*/
#include "libavutil/audioconvert.h"
#include "avcodec.h"
#include "get_bits.h"
#include "g722.h"
......@@ -57,11 +58,9 @@ static av_cold int g722_decode_init(AVCodecContext * avctx)
{
G722Context *c = avctx->priv_data;
if (avctx->channels != 1) {
av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
return AVERROR_INVALIDDATA;
}
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
avctx->channels = 1;
avctx->channel_layout = AV_CH_LAYOUT_MONO;
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
c->band[0].scale_factor = 8;
c->band[1].scale_factor = 2;
......
......@@ -30,6 +30,8 @@
#include "libavutil/pixfmt.h"
#include "avcodec.h"
#define FF_SANE_NB_CHANNELS 128U
typedef struct InternalBuffer {
uint8_t *base[AV_NUM_DATA_POINTERS];
uint8_t *data[AV_NUM_DATA_POINTERS];
......
......@@ -31,8 +31,6 @@
#include "mathops.h"
#include "pcm_tablegen.h"
#define MAX_CHANNELS 64
static av_cold int pcm_encode_init(AVCodecContext *avctx)
{
avctx->frame_size = 0;
......@@ -208,7 +206,7 @@ static av_cold int pcm_decode_init(AVCodecContext *avctx)
PCMDecode *s = avctx->priv_data;
int i;
if (avctx->channels <= 0 || avctx->channels > MAX_CHANNELS) {
if (avctx->channels <= 0) {
av_log(avctx, AV_LOG_ERROR, "PCM channels out of bounds\n");
return AVERROR(EINVAL);
}
......
......@@ -907,8 +907,7 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
if (av_codec_is_decoder(codec))
av_freep(&avctx->subtitle_header);
#define SANE_NB_CHANNELS 128U
if (avctx->channels > SANE_NB_CHANNELS) {
if (avctx->channels > FF_SANE_NB_CHANNELS) {
ret = AVERROR(EINVAL);
goto free_and_end;
}
......@@ -1091,6 +1090,11 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
avctx->channel_layout = 0;
}
}
if (avctx->channels && avctx->channels < 0 ||
avctx->channels > FF_SANE_NB_CHANNELS) {
ret = AVERROR(EINVAL);
goto free_and_end;
}
}
end:
entangled_thread_counter--;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment