Revision 3e00abab

View differences:

libavcodec/resample.c
29 29
#include "libavutil/opt.h"
30 30
#include "libavutil/samplefmt.h"
31 31

  
32
#define MAX_CHANNELS 8
33

  
32 34
struct AVResampleContext;
33 35

  
34 36
static const char *context_to_name(void *ptr)
......
41 43

  
42 44
struct ReSampleContext {
43 45
    struct AVResampleContext *resample_context;
44
    short *temp[2];
46
    short *temp[MAX_CHANNELS];
45 47
    int temp_len;
46 48
    float ratio;
47 49
    /* channel convert */
......
104 106
    }
105 107
}
106 108

  
107
/* XXX: should use more abstract 'N' channels system */
108
static void stereo_split(short *output1, short *output2, short *input, int n)
109
static void deinterleave(short **output, short *input, int channels, int samples)
109 110
{
110
    int i;
111
    int i, j;
111 112

  
112
    for(i=0;i<n;i++) {
113
        *output1++ = *input++;
114
        *output2++ = *input++;
113
    for (i = 0; i < samples; i++) {
114
        for (j = 0; j < channels; j++) {
115
            *output[j]++ = *input++;
116
        }
115 117
    }
116 118
}
117 119

  
118
static void stereo_mux(short *output, short *input1, short *input2, int n)
120
static void interleave(short *output, short **input, int channels, int samples)
119 121
{
120
    int i;
122
    int i, j;
121 123

  
122
    for(i=0;i<n;i++) {
123
        *output++ = *input1++;
124
        *output++ = *input2++;
124
    for (i = 0; i < samples; i++) {
125
        for (j = 0; j < channels; j++) {
126
            *output++ = *input[j]++;
127
        }
125 128
    }
126 129
}
127 130

  
......
151 154
{
152 155
    ReSampleContext *s;
153 156

  
154
    if ( input_channels > 2)
157
    if (input_channels > MAX_CHANNELS)
155 158
      {
156
        av_log(NULL, AV_LOG_ERROR, "Resampling with input channels greater than 2 unsupported.\n");
159
        av_log(NULL, AV_LOG_ERROR,
160
               "Resampling with input channels greater than %d is unsupported.\n",
161
               MAX_CHANNELS);
157 162
        return NULL;
158 163
      }
159
    if (output_channels > 2 && !(output_channels == 6 && input_channels == 2)) {
164
    if (  output_channels > 2 &&
165
        !(output_channels == 6 && input_channels == 2) &&
166
          output_channels != input_channels) {
160 167
        av_log(NULL, AV_LOG_ERROR,
161
               "Resampling output channel count must be 1 or 2 for mono input and 1, 2 or 6 for stereo input.\n");
168
               "Resampling output channel count must be 1 or 2 for mono input; 1, 2 or 6 for stereo input; or N for N channel input.\n");
162 169
        return NULL;
163 170
    }
164 171

  
......
206 213
        }
207 214
    }
208 215

  
209
/*
210
 * AC-3 output is the only case where filter_channels could be greater than 2.
211
 * input channels can't be greater than 2, so resample the 2 channels and then
212
 * expand to 6 channels after the resampling.
213
 */
214
    if(s->filter_channels>2)
215
      s->filter_channels = 2;
216

  
217 216
#define TAPS 16
218 217
    s->resample_context= av_resample_init(output_rate, input_rate,
219 218
                         filter_length, log2_phase_count, linear, cutoff);
......
228 227
int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
229 228
{
230 229
    int i, nb_samples1;
231
    short *bufin[2];
232
    short *bufout[2];
233
    short *buftmp2[2], *buftmp3[2];
230
    short *bufin[MAX_CHANNELS];
231
    short *bufout[MAX_CHANNELS];
232
    short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
234 233
    short *output_bak = NULL;
235 234
    int lenout;
236 235

  
......
291 290
        bufin[i]= av_malloc( (nb_samples + s->temp_len) * sizeof(short) );
292 291
        memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
293 292
        buftmp2[i] = bufin[i] + s->temp_len;
293
        bufout[i] = av_malloc(lenout * sizeof(short));
294 294
    }
295 295

  
296
    /* make some zoom to avoid round pb */
297
    bufout[0]= av_malloc( lenout * sizeof(short) );
298
    bufout[1]= av_malloc( lenout * sizeof(short) );
299

  
300 296
    if (s->input_channels == 2 &&
301 297
        s->output_channels == 1) {
302 298
        buftmp3[0] = output;
......
304 300
    } else if (s->output_channels >= 2 && s->input_channels == 1) {
305 301
        buftmp3[0] = bufout[0];
306 302
        memcpy(buftmp2[0], input, nb_samples*sizeof(short));
307
    } else if (s->output_channels >= 2) {
308
        buftmp3[0] = bufout[0];
309
        buftmp3[1] = bufout[1];
310
        stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
303
    } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
304
        for (i = 0; i < s->input_channels; i++) {
305
            buftmp3[i] = bufout[i];
306
        }
307
        deinterleave(buftmp2, input, s->input_channels, nb_samples);
311 308
    } else {
312 309
        buftmp3[0] = output;
313 310
        memcpy(buftmp2[0], input, nb_samples*sizeof(short));
......
329 326

  
330 327
    if (s->output_channels == 2 && s->input_channels == 1) {
331 328
        mono_to_stereo(output, buftmp3[0], nb_samples1);
332
    } else if (s->output_channels == 2) {
333
        stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
334
    } else if (s->output_channels == 6) {
329
    } else if (s->output_channels == 6 && s->input_channels == 2) {
335 330
        ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
331
    } else if (s->output_channels == s->input_channels && s->input_channels >= 2) {
332
        interleave(output, buftmp3, s->output_channels, nb_samples1);
336 333
    }
337 334

  
338 335
    if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
......
348 345
        }
349 346
    }
350 347

  
351
    for(i=0; i<s->filter_channels; i++)
348
    for (i = 0; i < s->filter_channels; i++) {
352 349
        av_free(bufin[i]);
350
        av_free(bufout[i]);
351
    }
353 352

  
354
    av_free(bufout[0]);
355
    av_free(bufout[1]);
356 353
    return nb_samples1;
357 354
}
358 355

  
359 356
void audio_resample_close(ReSampleContext *s)
360 357
{
358
    int i;
361 359
    av_resample_close(s->resample_context);
362
    av_freep(&s->temp[0]);
363
    av_freep(&s->temp[1]);
360
    for (i = 0; i < s->filter_channels; i++)
361
        av_freep(&s->temp[i]);
364 362
    av_freep(&s->buffer[0]);
365 363
    av_freep(&s->buffer[1]);
366 364
    av_audio_convert_free(s->convert_ctx[0]);

Also available in: Unified diff