Browse code

Merge commit '50a65e7a540ce6747f81d6dbf6a602ad35be77ff'

* commit '50a65e7a540ce6747f81d6dbf6a602ad35be77ff': (24 commits)
vmdaudio: set channel layout
twinvq: validate sample rate code
twinvq: set channel layout
twinvq: validate that channels is not <= 0
truespeech: set channel layout
sipr: set channel layout
shorten: validate that the channel count in the header is not <= 0
ra288dec: set channel layout
ra144dec: set channel layout
qdm2: remove unneeded checks for channel count
qdm2: make sure channels is not <= 0 and set channel layout
qcelpdec: set channel layout
nellymoserdec: set channels to 1
libopencore-amr: set channel layout for amr-nb or if not set by the user
libilbc: set channel layout
dpcm: use AVCodecContext.channels instead of keeping a private copy
imc: set channels to 1 instead of validating it
gsmdec: always set channel layout and sample rate at initialization
libgsmdec: always set channel layout and sample rate at initialization
g726dec: do not validate sample rate
...

Conflicts:
libavcodec/dpcm.c
libavcodec/qdm2.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2012/11/02 22:15:28
Showing 21 changed files
... ...
@@ -44,7 +44,6 @@
44 44
 
45 45
 typedef struct DPCMContext {
46 46
     AVFrame frame;
47
-    int channels;
48 47
     int16_t roq_square_array[256];
49 48
     int sample[2];                  ///< previous sample (for SOL_DPCM)
50 49
     const int8_t *sol_table;        ///< delta table for SOL_DPCM
... ...
@@ -123,7 +122,6 @@ static av_cold int dpcm_decode_init(AVCodecContext *avctx)
123 123
         return AVERROR(EINVAL);
124 124
     }
125 125
 
126
-    s->channels = avctx->channels;
127 126
     s->sample[0] = s->sample[1] = 0;
128 127
 
129 128
     switch(avctx->codec->id) {
... ...
@@ -179,7 +177,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
179 179
     int out = 0, ret;
180 180
     int predictor[2];
181 181
     int ch = 0;
182
-    int stereo = s->channels - 1;
182
+    int stereo = avctx->channels - 1;
183 183
     int16_t *output_samples, *samples_end;
184 184
     GetByteContext gb;
185 185
 
... ...
@@ -193,10 +191,10 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
193 193
         out = buf_size - 8;
194 194
         break;
195 195
     case AV_CODEC_ID_INTERPLAY_DPCM:
196
-        out = buf_size - 6 - s->channels;
196
+        out = buf_size - 6 - avctx->channels;
197 197
         break;
198 198
     case AV_CODEC_ID_XAN_DPCM:
199
-        out = buf_size - 2 * s->channels;
199
+        out = buf_size - 2 * avctx->channels;
200 200
         break;
201 201
     case AV_CODEC_ID_SOL_DPCM:
202 202
         if (avctx->codec_tag != 3)
... ...
@@ -209,12 +207,12 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
209 209
         av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
210 210
         return AVERROR(EINVAL);
211 211
     }
212
-    if (out % s->channels) {
212
+    if (out % avctx->channels) {
213 213
         av_log(avctx, AV_LOG_WARNING, "channels have differing number of samples\n");
214 214
     }
215 215
 
216 216
     /* get output buffer */
217
-    s->frame.nb_samples = (out + s->channels - 1) / s->channels;
217
+    s->frame.nb_samples = (out + avctx->channels - 1) / avctx->channels;
218 218
     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
219 219
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
220 220
         return ret;
... ...
@@ -248,7 +246,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
248 248
     case AV_CODEC_ID_INTERPLAY_DPCM:
249 249
         bytestream2_skipu(&gb, 6);  /* skip over the stream mask and stream length */
250 250
 
251
-        for (ch = 0; ch < s->channels; ch++) {
251
+        for (ch = 0; ch < avctx->channels; ch++) {
252 252
             predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);
253 253
             *output_samples++ = predictor[ch];
254 254
         }
... ...
@@ -268,7 +266,7 @@ static int dpcm_decode_frame(AVCodecContext *avctx, void *data,
268 268
     {
269 269
         int shift[2] = { 4, 4 };
270 270
 
271
-        for (ch = 0; ch < s->channels; ch++)
271
+        for (ch = 0; ch < avctx->channels; ch++)
272 272
             predictor[ch] = sign_extend(bytestream2_get_le16u(&gb), 16);
273 273
 
274 274
         ch = 0;
... ...
@@ -19,6 +19,7 @@
19 19
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
  */
21 21
 
22
+#include "libavutil/audioconvert.h"
22 23
 #include "libavutil/crc.h"
23 24
 #include "libavutil/log.h"
24 25
 #include "bytestream.h"
... ...
@@ -28,6 +29,15 @@
28 28
 
29 29
 static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
30 30
 
31
+static const int64_t flac_channel_layouts[6] = {
32
+    AV_CH_LAYOUT_MONO,
33
+    AV_CH_LAYOUT_STEREO,
34
+    AV_CH_LAYOUT_SURROUND,
35
+    AV_CH_LAYOUT_QUAD,
36
+    AV_CH_LAYOUT_5POINT0,
37
+    AV_CH_LAYOUT_5POINT1
38
+};
39
+
31 40
 static int64_t get_utf8(GetBitContext *gb)
32 41
 {
33 42
     int64_t val;
... ...
@@ -181,6 +191,14 @@ int avpriv_flac_is_extradata_valid(AVCodecContext *avctx,
181 181
     return 1;
182 182
 }
183 183
 
184
+void ff_flac_set_channel_layout(AVCodecContext *avctx)
185
+{
186
+    if (avctx->channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
187
+        avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
188
+    else
189
+        avctx->channel_layout = 0;
190
+}
191
+
184 192
 void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
185 193
                               const uint8_t *buffer)
186 194
 {
... ...
@@ -205,6 +223,7 @@ void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *
205 205
     avctx->channels = s->channels;
206 206
     avctx->sample_rate = s->samplerate;
207 207
     avctx->bits_per_raw_sample = s->bps;
208
+    ff_flac_set_channel_layout(avctx);
208 209
 
209 210
     s->samples = get_bits_longlong(&gb, 36);
210 211
 
... ...
@@ -137,4 +137,7 @@ int ff_flac_get_max_frame_size(int blocksize, int ch, int bps);
137 137
  */
138 138
 int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
139 139
                                 FLACFrameInfo *fi, int log_level_offset);
140
+
141
+void ff_flac_set_channel_layout(AVCodecContext *avctx);
142
+
140 143
 #endif /* AVCODEC_FLAC_H */
... ...
@@ -459,6 +459,7 @@ static int get_best_header(FLACParseContext* fpc, const uint8_t **poutbuf,
459 459
 
460 460
     fpc->avctx->sample_rate = header->fi.samplerate;
461 461
     fpc->avctx->channels    = header->fi.channels;
462
+    ff_flac_set_channel_layout(fpc->avctx);
462 463
     fpc->pc->duration       = header->fi.blocksize;
463 464
     *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size,
464 465
                                         &fpc->wrap_buf,
... ...
@@ -58,20 +58,13 @@ typedef struct FLACContext {
58 58
     int got_streaminfo;                     ///< indicates if the STREAMINFO has been read
59 59
 
60 60
     int32_t *decoded[FLAC_MAX_CHANNELS];    ///< decoded samples
61
+    uint8_t *decoded_buffer;
62
+    unsigned int decoded_buffer_size;
61 63
 
62 64
     FLACDSPContext dsp;
63 65
 } FLACContext;
64 66
 
65
-static const int64_t flac_channel_layouts[6] = {
66
-    AV_CH_LAYOUT_MONO,
67
-    AV_CH_LAYOUT_STEREO,
68
-    AV_CH_LAYOUT_SURROUND,
69
-    AV_CH_LAYOUT_QUAD,
70
-    AV_CH_LAYOUT_5POINT0,
71
-    AV_CH_LAYOUT_5POINT1
72
-};
73
-
74
-static void allocate_buffers(FLACContext *s);
67
+static int allocate_buffers(FLACContext *s);
75 68
 
76 69
 static void flac_set_bps(FLACContext *s)
77 70
 {
... ...
@@ -99,6 +92,7 @@ static av_cold int flac_decode_init(AVCodecContext *avctx)
99 99
 {
100 100
     enum FLACExtradataFormat format;
101 101
     uint8_t *streaminfo;
102
+    int ret;
102 103
     FLACContext *s = avctx->priv_data;
103 104
     s->avctx = avctx;
104 105
 
... ...
@@ -112,7 +106,9 @@ static av_cold int flac_decode_init(AVCodecContext *avctx)
112 112
 
113 113
     /* initialize based on the demuxer-supplied streamdata header */
114 114
     avpriv_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, streaminfo);
115
-    allocate_buffers(s);
115
+    ret = allocate_buffers(s);
116
+    if (ret < 0)
117
+        return ret;
116 118
     flac_set_bps(s);
117 119
     ff_flacdsp_init(&s->dsp, avctx->sample_fmt, s->bps);
118 120
     s->got_streaminfo = 1;
... ...
@@ -120,9 +116,6 @@ static av_cold int flac_decode_init(AVCodecContext *avctx)
120 120
     avcodec_get_frame_defaults(&s->frame);
121 121
     avctx->coded_frame = &s->frame;
122 122
 
123
-    if (avctx->channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
124
-        avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
125
-
126 123
     return 0;
127 124
 }
128 125
 
... ...
@@ -135,15 +128,24 @@ static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
135 135
     av_log(avctx, AV_LOG_DEBUG, "  Bits: %d\n", s->bps);
136 136
 }
137 137
 
138
-static void allocate_buffers(FLACContext *s)
138
+static int allocate_buffers(FLACContext *s)
139 139
 {
140
-    int i;
140
+    int buf_size;
141 141
 
142 142
     av_assert0(s->max_blocksize);
143 143
 
144
-    for (i = 0; i < s->channels; i++) {
145
-        s->decoded[i] = av_malloc(sizeof(int32_t)*s->max_blocksize);
146
-    }
144
+    buf_size = av_samples_get_buffer_size(NULL, s->channels, s->max_blocksize,
145
+                                          AV_SAMPLE_FMT_S32P, 0);
146
+    if (buf_size < 0)
147
+        return buf_size;
148
+
149
+    av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size);
150
+    if (!s->decoded_buffer)
151
+        return AVERROR(ENOMEM);
152
+
153
+    return av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
154
+                                  s->decoded_buffer, s->channels,
155
+                                  s->max_blocksize, AV_SAMPLE_FMT_S32P, 0);
147 156
 }
148 157
 
149 158
 /**
... ...
@@ -155,7 +157,7 @@ static void allocate_buffers(FLACContext *s)
155 155
  */
156 156
 static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
157 157
 {
158
-    int metadata_type, metadata_size;
158
+    int metadata_type, metadata_size, ret;
159 159
 
160 160
     if (buf_size < FLAC_STREAMINFO_SIZE+8) {
161 161
         /* need more data */
... ...
@@ -167,7 +169,9 @@ static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
167 167
         return AVERROR_INVALIDDATA;
168 168
     }
169 169
     avpriv_flac_parse_streaminfo(s->avctx, (FLACStreaminfo *)s, &buf[8]);
170
-    allocate_buffers(s);
170
+    ret = allocate_buffers(s);
171
+    if (ret < 0)
172
+        return ret;
171 173
     flac_set_bps(s);
172 174
     ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->bps);
173 175
     s->got_streaminfo = 1;
... ...
@@ -403,7 +407,7 @@ static inline int decode_subframe(FLACContext *s, int channel)
403 403
 
404 404
 static int decode_frame(FLACContext *s)
405 405
 {
406
-    int i;
406
+    int i, ret;
407 407
     GetBitContext *gb = &s->gb;
408 408
     FLACFrameInfo fi;
409 409
 
... ...
@@ -412,12 +416,15 @@ static int decode_frame(FLACContext *s)
412 412
         return -1;
413 413
     }
414 414
 
415
-    if (s->channels && fi.channels != s->channels) {
416
-        av_log(s->avctx, AV_LOG_ERROR, "switching channel layout mid-stream "
417
-                                       "is not supported\n");
418
-        return -1;
415
+    if (s->channels && fi.channels != s->channels && s->got_streaminfo) {
416
+        s->channels = s->avctx->channels = fi.channels;
417
+        ff_flac_set_channel_layout(s->avctx);
418
+        ret = allocate_buffers(s);
419
+        if (ret < 0)
420
+            return ret;
419 421
     }
420 422
     s->channels = s->avctx->channels = fi.channels;
423
+    ff_flac_set_channel_layout(s->avctx);
421 424
     s->ch_mode = fi.ch_mode;
422 425
 
423 426
     if (!s->bps && !fi.bps) {
... ...
@@ -451,16 +458,14 @@ static int decode_frame(FLACContext *s)
451 451
                                         " or frame header\n");
452 452
         return -1;
453 453
     }
454
-    if (fi.samplerate == 0) {
454
+    if (fi.samplerate == 0)
455 455
         fi.samplerate = s->samplerate;
456
-    } else if (s->samplerate && fi.samplerate != s->samplerate) {
457
-        av_log(s->avctx, AV_LOG_WARNING, "sample rate changed from %d to %d\n",
458
-               s->samplerate, fi.samplerate);
459
-    }
460 456
     s->samplerate = s->avctx->sample_rate = fi.samplerate;
461 457
 
462 458
     if (!s->got_streaminfo) {
463
-        allocate_buffers(s);
459
+        ret = allocate_buffers(s);
460
+        if (ret < 0)
461
+            return ret;
464 462
         ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->bps);
465 463
         s->got_streaminfo = 1;
466 464
         dump_headers(s->avctx, (FLACStreaminfo *)s);
... ...
@@ -550,11 +555,8 @@ static int flac_decode_frame(AVCodecContext *avctx, void *data,
550 550
 static av_cold int flac_decode_close(AVCodecContext *avctx)
551 551
 {
552 552
     FLACContext *s = avctx->priv_data;
553
-    int i;
554 553
 
555
-    for (i = 0; i < s->channels; i++) {
556
-        av_freep(&s->decoded[i]);
557
-    }
554
+    av_freep(&s->decoded_buffer);
558 555
 
559 556
     return 0;
560 557
 }
... ...
@@ -22,6 +22,8 @@
22 22
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 23
  */
24 24
 #include <limits.h>
25
+
26
+#include "libavutil/audioconvert.h"
25 27
 #include "libavutil/avassert.h"
26 28
 #include "libavutil/opt.h"
27 29
 #include "avcodec.h"
... ...
@@ -418,18 +420,8 @@ static av_cold int g726_decode_init(AVCodecContext *avctx)
418 418
 {
419 419
     G726Context* c = avctx->priv_data;
420 420
 
421
-    if (avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
422
-        avctx->sample_rate != 8000) {
423
-        av_log(avctx, AV_LOG_ERROR, "Only 8kHz sample rate is allowed when "
424
-               "the compliance level is strict. Reduce the compliance level "
425
-               "if you wish to decode the stream anyway.\n");
426
-        return AVERROR(EINVAL);
427
-    }
428
-
429
-    if(avctx->channels != 1){
430
-        av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
431
-        return AVERROR(EINVAL);
432
-    }
421
+    avctx->channels       = 1;
422
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
433 423
 
434 424
     c->code_size = avctx->bits_per_coded_sample;
435 425
     if (c->code_size < 2 || c->code_size > 5) {
... ...
@@ -24,6 +24,7 @@
24 24
  * GSM decoder
25 25
  */
26 26
 
27
+#include "libavutil/audioconvert.h"
27 28
 #include "avcodec.h"
28 29
 #include "get_bits.h"
29 30
 #include "msgsmdec.h"
... ...
@@ -34,10 +35,10 @@ static av_cold int gsm_init(AVCodecContext *avctx)
34 34
 {
35 35
     GSMContext *s = avctx->priv_data;
36 36
 
37
-    avctx->channels = 1;
38
-    if (!avctx->sample_rate)
39
-        avctx->sample_rate = 8000;
40
-    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
37
+    avctx->channels       = 1;
38
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
39
+    avctx->sample_rate    = 8000;
40
+    avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
41 41
 
42 42
     switch (avctx->codec_id) {
43 43
     case AV_CODEC_ID_GSM:
... ...
@@ -176,8 +176,10 @@ static av_cold int imc_decode_init(AVCodecContext *avctx)
176 176
     IMCContext *q = avctx->priv_data;
177 177
     double r1, r2;
178 178
 
179
-    if ((avctx->codec_id == AV_CODEC_ID_IMC && avctx->channels != 1)
180
-        || (avctx->codec_id == AV_CODEC_ID_IAC && avctx->channels > 2)) {
179
+    if (avctx->codec_id == AV_CODEC_ID_IMC)
180
+        avctx->channels = 1;
181
+
182
+    if (avctx->channels > 2) {
181 183
         av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
182 184
         return AVERROR_PATCHWELCOME;
183 185
     }
... ...
@@ -29,6 +29,7 @@
29 29
 
30 30
 #include <gsm/gsm.h>
31 31
 
32
+#include "libavutil/audioconvert.h"
32 33
 #include "avcodec.h"
33 34
 #include "internal.h"
34 35
 #include "gsm.h"
... ...
@@ -153,19 +154,10 @@ typedef struct LibGSMDecodeContext {
153 153
 static av_cold int libgsm_decode_init(AVCodecContext *avctx) {
154 154
     LibGSMDecodeContext *s = avctx->priv_data;
155 155
 
156
-    if (avctx->channels > 1) {
157
-        av_log(avctx, AV_LOG_ERROR, "Mono required for GSM, got %d channels\n",
158
-               avctx->channels);
159
-        return -1;
160
-    }
161
-
162
-    if (!avctx->channels)
163
-        avctx->channels = 1;
164
-
165
-    if (!avctx->sample_rate)
166
-        avctx->sample_rate = 8000;
167
-
168
-    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
156
+    avctx->channels       = 1;
157
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
158
+    avctx->sample_rate    = 8000;
159
+    avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
169 160
 
170 161
     s->state = gsm_create();
171 162
 
... ...
@@ -21,6 +21,7 @@
21 21
 
22 22
 #include <ilbc.h>
23 23
 
24
+#include "libavutil/audioconvert.h"
24 25
 #include "avcodec.h"
25 26
 #include "libavutil/common.h"
26 27
 #include "libavutil/opt.h"
... ...
@@ -71,9 +72,10 @@ static av_cold int ilbc_decode_init(AVCodecContext *avctx)
71 71
     avcodec_get_frame_defaults(&s->frame);
72 72
     avctx->coded_frame = &s->frame;
73 73
 
74
-    avctx->channels = 1;
75
-    avctx->sample_rate = 8000;
76
-    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
74
+    avctx->channels       = 1;
75
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
76
+    avctx->sample_rate    = 8000;
77
+    avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
77 78
 
78 79
     return 0;
79 80
 }
... ...
@@ -19,6 +19,7 @@
19 19
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
  */
21 21
 
22
+#include "libavutil/audioconvert.h"
22 23
 #include "avcodec.h"
23 24
 #include "libavutil/avstring.h"
24 25
 #include "libavutil/common.h"
... ...
@@ -30,13 +31,16 @@ static void amr_decode_fix_avctx(AVCodecContext *avctx)
30 30
 {
31 31
     const int is_amr_wb = 1 + (avctx->codec_id == AV_CODEC_ID_AMR_WB);
32 32
 
33
-    if (!avctx->sample_rate)
34
-        avctx->sample_rate = 8000 * is_amr_wb;
33
+    avctx->sample_rate = 8000 * is_amr_wb;
35 34
 
36
-    if (!avctx->channels)
37
-        avctx->channels = 1;
35
+    if (avctx->channels > 1) {
36
+        av_log_missing_feature(avctx, "multi-channel AMR", 0);
37
+        return AVERROR_PATCHWELCOME;
38
+    }
38 39
 
39
-    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
40
+    avctx->channels       = 1;
41
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
42
+    avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
40 43
 }
41 44
 
42 45
 #if CONFIG_LIBOPENCORE_AMRNB
... ...
@@ -129,6 +129,7 @@ static av_cold int decode_init(AVCodecContext * avctx) {
129 129
     if (!ff_sine_128[127])
130 130
         ff_init_ff_sine_windows(7);
131 131
 
132
+    avctx->channels       = 1;
132 133
     avctx->channel_layout = AV_CH_LAYOUT_MONO;
133 134
 
134 135
     avcodec_get_frame_defaults(&s->frame);
... ...
@@ -29,6 +29,7 @@
29 29
 
30 30
 #include <stddef.h>
31 31
 
32
+#include "libavutil/audioconvert.h"
32 33
 #include "avcodec.h"
33 34
 #include "internal.h"
34 35
 #include "get_bits.h"
... ...
@@ -89,7 +90,9 @@ static av_cold int qcelp_decode_init(AVCodecContext *avctx)
89 89
     QCELPContext *q = avctx->priv_data;
90 90
     int i;
91 91
 
92
-    avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
92
+    avctx->channels       = 1;
93
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
94
+    avctx->sample_fmt     = AV_SAMPLE_FMT_FLT;
93 95
 
94 96
     for (i = 0; i < 10; i++)
95 97
         q->prev_lspf[i] = (i + 1) / 11.;
... ...
@@ -36,6 +36,7 @@
36 36
 #include <stdio.h>
37 37
 
38 38
 #define BITSTREAM_READER_LE
39
+#include "libavutil/audioconvert.h"
39 40
 #include "avcodec.h"
40 41
 #include "get_bits.h"
41 42
 #include "dsputil.h"
... ...
@@ -550,10 +551,6 @@ static void fill_tone_level_array (QDM2Context *q, int flag)
550 550
     int i, sb, ch, sb_used;
551 551
     int tmp, tab;
552 552
 
553
-    // This should never happen
554
-    if (q->nb_channels <= 0)
555
-        return;
556
-
557 553
     for (ch = 0; ch < q->nb_channels; ch++)
558 554
         for (sb = 0; sb < 30; sb++)
559 555
             for (i = 0; i < 8; i++) {
... ...
@@ -649,10 +646,6 @@ static void fill_coding_method_array (sb_int8_array tone_level_idx, sb_int8_arra
649 649
     int add1, add2, add3, add4;
650 650
     int64_t multres;
651 651
 
652
-    // This should never happen
653
-    if (nb_channels <= 0)
654
-        return;
655
-
656 652
     if (!superblocktype_2_3) {
657 653
         /* This case is untested, no samples available */
658 654
         SAMPLES_NEEDED
... ...
@@ -1792,10 +1785,12 @@ static av_cold int qdm2_decode_init(AVCodecContext *avctx)
1792 1792
 
1793 1793
     avctx->channels = s->nb_channels = s->channels = AV_RB32(extradata);
1794 1794
     extradata += 4;
1795
-    if (s->channels > MPA_MAX_CHANNELS) {
1796
-        av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
1795
+    if (s->channels <= 0 || s->channels > MPA_MAX_CHANNELS) {
1796
+        av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
1797 1797
         return AVERROR_INVALIDDATA;
1798 1798
     }
1799
+    avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
1800
+                                                   AV_CH_LAYOUT_MONO;
1799 1801
 
1800 1802
     avctx->sample_rate = AV_RB32(extradata);
1801 1803
     extradata += 4;
... ...
@@ -22,6 +22,7 @@
22 22
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 23
  */
24 24
 
25
+#include "libavutil/audioconvert.h"
25 26
 #include "libavutil/intmath.h"
26 27
 #include "avcodec.h"
27 28
 #include "get_bits.h"
... ...
@@ -37,7 +38,9 @@ static av_cold int ra144_decode_init(AVCodecContext * avctx)
37 37
     ractx->lpc_coef[0] = ractx->lpc_tables[0];
38 38
     ractx->lpc_coef[1] = ractx->lpc_tables[1];
39 39
 
40
-    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
40
+    avctx->channels       = 1;
41
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
42
+    avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
41 43
 
42 44
     avcodec_get_frame_defaults(&ractx->frame);
43 45
     avctx->coded_frame = &ractx->frame;
... ...
@@ -19,6 +19,7 @@
19 19
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
  */
21 21
 
22
+#include "libavutil/audioconvert.h"
22 23
 #include "libavutil/float_dsp.h"
23 24
 #include "avcodec.h"
24 25
 #define BITSTREAM_READER_LE
... ...
@@ -61,7 +62,11 @@ typedef struct {
61 61
 static av_cold int ra288_decode_init(AVCodecContext *avctx)
62 62
 {
63 63
     RA288Context *ractx = avctx->priv_data;
64
-    avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
64
+
65
+    avctx->channels       = 1;
66
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
67
+    avctx->sample_fmt     = AV_SAMPLE_FMT_FLT;
68
+
65 69
     avpriv_float_dsp_init(&ractx->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
66 70
 
67 71
     avcodec_get_frame_defaults(&ractx->frame);
... ...
@@ -340,7 +340,7 @@ static int read_header(ShortenContext *s)
340 340
     s->internal_ftype = get_uint(s, TYPESIZE);
341 341
 
342 342
     s->channels = get_uint(s, CHANSIZE);
343
-    if (s->channels > MAX_CHANNELS) {
343
+    if (s->channels <= 0 || s->channels > MAX_CHANNELS) {
344 344
         av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
345 345
         return -1;
346 346
     }
... ...
@@ -25,6 +25,7 @@
25 25
 #include <stdint.h>
26 26
 #include <string.h>
27 27
 
28
+#include "libavutil/audioconvert.h"
28 29
 #include "libavutil/mathematics.h"
29 30
 #include "avcodec.h"
30 31
 #define BITSTREAM_READER_LE
... ...
@@ -509,7 +510,9 @@ static av_cold int sipr_decoder_init(AVCodecContext * avctx)
509 509
     for (i = 0; i < 4; i++)
510 510
         ctx->energy_history[i] = -14;
511 511
 
512
-    avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
512
+    avctx->channels       = 1;
513
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
514
+    avctx->sample_fmt     = AV_SAMPLE_FMT_FLT;
513 515
 
514 516
     avcodec_get_frame_defaults(&ctx->frame);
515 517
     avctx->coded_frame = &ctx->frame;
... ...
@@ -19,6 +19,7 @@
19 19
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
  */
21 21
 
22
+#include "libavutil/audioconvert.h"
22 23
 #include "libavutil/intreadwrite.h"
23 24
 #include "avcodec.h"
24 25
 #include "dsputil.h"
... ...
@@ -66,7 +67,8 @@ static av_cold int truespeech_decode_init(AVCodecContext * avctx)
66 66
         return AVERROR(EINVAL);
67 67
     }
68 68
 
69
-    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
69
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
70
+    avctx->sample_fmt     = AV_SAMPLE_FMT_S16;
70 71
 
71 72
     ff_dsputil_init(&c->dsp, avctx);
72 73
 
... ...
@@ -19,6 +19,7 @@
19 19
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
  */
21 21
 
22
+#include "libavutil/audioconvert.h"
22 23
 #include "libavutil/float_dsp.h"
23 24
 #include "avcodec.h"
24 25
 #include "get_bits.h"
... ...
@@ -1119,6 +1120,11 @@ static av_cold int twin_decode_init(AVCodecContext *avctx)
1119 1119
     avctx->channels = AV_RB32(avctx->extradata    ) + 1;
1120 1120
     avctx->bit_rate = AV_RB32(avctx->extradata + 4) * 1000;
1121 1121
     isampf          = AV_RB32(avctx->extradata + 8);
1122
+
1123
+    if (isampf < 8 || isampf > 44) {
1124
+        av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate\n");
1125
+        return AVERROR_INVALIDDATA;
1126
+    }
1122 1127
     switch (isampf) {
1123 1128
     case 44: avctx->sample_rate = 44100;         break;
1124 1129
     case 22: avctx->sample_rate = 22050;         break;
... ...
@@ -1126,11 +1132,14 @@ static av_cold int twin_decode_init(AVCodecContext *avctx)
1126 1126
     default: avctx->sample_rate = isampf * 1000; break;
1127 1127
     }
1128 1128
 
1129
-    if (avctx->channels > CHANNELS_MAX) {
1129
+    if (avctx->channels <= 0 || avctx->channels > CHANNELS_MAX) {
1130 1130
         av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %i\n",
1131 1131
                avctx->channels);
1132 1132
         return -1;
1133 1133
     }
1134
+    avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
1135
+                                                   AV_CH_LAYOUT_STEREO;
1136
+
1134 1137
     ibps = avctx->bit_rate / (1000 * avctx->channels);
1135 1138
 
1136 1139
     switch ((isampf << 8) +  ibps) {
... ...
@@ -43,6 +43,7 @@
43 43
 #include <stdlib.h>
44 44
 #include <string.h>
45 45
 
46
+#include "libavutil/audioconvert.h"
46 47
 #include "libavutil/common.h"
47 48
 #include "libavutil/intreadwrite.h"
48 49
 #include "avcodec.h"
... ...
@@ -501,6 +502,9 @@ static av_cold int vmdaudio_decode_init(AVCodecContext *avctx)
501 501
         return AVERROR(EINVAL);
502 502
     }
503 503
 
504
+    avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO :
505
+                                                   AV_CH_LAYOUT_STEREO;
506
+
504 507
     if (avctx->bits_per_coded_sample == 16)
505 508
         avctx->sample_fmt = AV_SAMPLE_FMT_S16;
506 509
     else