Statistics
| Branch: | Revision:

ffmpeg / libavcodec / audioconvert.c @ 5d6e4c16

History | View | Annotate | Download (8.32 KB)

1
/*
2
 * audio conversion
3
 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file
24
 * audio conversion
25
 * @author Michael Niedermayer <michaelni@gmx.at>
26
 */
27

    
28
#include "libavutil/avstring.h"
29
#include "libavutil/libm.h"
30
#include "libavcore/samplefmt.h"
31
#include "avcodec.h"
32
#include "audioconvert.h"
33

    
34
#if FF_API_OLD_SAMPLE_FMT
35
const char *avcodec_get_sample_fmt_name(int sample_fmt)
36
{
37
    return av_get_sample_fmt_name(sample_fmt);
38
}
39

    
40
enum AVSampleFormat avcodec_get_sample_fmt(const char* name)
41
{
42
    return av_get_sample_fmt(name);
43
}
44

    
45
void avcodec_sample_fmt_string (char *buf, int buf_size, int sample_fmt)
46
{
47
    av_get_sample_fmt_string(buf, buf_size, sample_fmt);
48
}
49
#endif
50

    
51
static const char* const channel_names[]={
52
    "FL", "FR", "FC", "LFE", "BL",  "BR",  "FLC", "FRC",
53
    "BC", "SL", "SR", "TC",  "TFL", "TFC", "TFR", "TBL",
54
    "TBC", "TBR",
55
    [29] = "DL",
56
    [30] = "DR",
57
};
58

    
59
static const char *get_channel_name(int channel_id)
60
{
61
    if (channel_id<0 || channel_id>=FF_ARRAY_ELEMS(channel_names))
62
        return NULL;
63
    return channel_names[channel_id];
64
}
65

    
66
int64_t avcodec_guess_channel_layout(int nb_channels, enum CodecID codec_id, const char *fmt_name)
67
{
68
    switch(nb_channels) {
69
    case 1: return CH_LAYOUT_MONO;
70
    case 2: return CH_LAYOUT_STEREO;
71
    case 3: return CH_LAYOUT_SURROUND;
72
    case 4: return CH_LAYOUT_QUAD;
73
    case 5: return CH_LAYOUT_5POINT0;
74
    case 6: return CH_LAYOUT_5POINT1;
75
    case 8: return CH_LAYOUT_7POINT1;
76
    default: return 0;
77
    }
78
}
79

    
80
static const struct {
81
    const char *name;
82
    int         nb_channels;
83
    int64_t     layout;
84
} channel_layout_map[] = {
85
    { "mono",        1,  CH_LAYOUT_MONO },
86
    { "stereo",      2,  CH_LAYOUT_STEREO },
87
    { "4.0",         4,  CH_LAYOUT_4POINT0 },
88
    { "quad",        4,  CH_LAYOUT_QUAD },
89
    { "5.0",         5,  CH_LAYOUT_5POINT0 },
90
    { "5.0",         5,  CH_LAYOUT_5POINT0_BACK },
91
    { "5.1",         6,  CH_LAYOUT_5POINT1 },
92
    { "5.1",         6,  CH_LAYOUT_5POINT1_BACK },
93
    { "5.1+downmix", 8,  CH_LAYOUT_5POINT1|CH_LAYOUT_STEREO_DOWNMIX, },
94
    { "7.1",         8,  CH_LAYOUT_7POINT1 },
95
    { "7.1(wide)",   8,  CH_LAYOUT_7POINT1_WIDE },
96
    { "7.1+downmix", 10, CH_LAYOUT_7POINT1|CH_LAYOUT_STEREO_DOWNMIX, },
97
    { 0 }
98
};
99

    
100
int64_t avcodec_get_channel_layout(const char *name)
101
{
102
    int i = 0;
103
    do {
104
        if (!strcmp(channel_layout_map[i].name, name))
105
            return channel_layout_map[i].layout;
106
        i++;
107
    } while (channel_layout_map[i].name);
108

    
109
    return 0;
110
}
111

    
112
void avcodec_get_channel_layout_string(char *buf, int buf_size, int nb_channels, int64_t channel_layout)
113
{
114
    int i;
115

    
116
    for (i=0; channel_layout_map[i].name; i++)
117
        if (nb_channels    == channel_layout_map[i].nb_channels &&
118
            channel_layout == channel_layout_map[i].layout) {
119
            av_strlcpy(buf, channel_layout_map[i].name, buf_size);
120
            return;
121
        }
122

    
123
    snprintf(buf, buf_size, "%d channels", nb_channels);
124
    if (channel_layout) {
125
        int i,ch;
126
        av_strlcat(buf, " (", buf_size);
127
        for(i=0,ch=0; i<64; i++) {
128
            if ((channel_layout & (1L<<i))) {
129
                const char *name = get_channel_name(i);
130
                if (name) {
131
                    if (ch>0) av_strlcat(buf, "|", buf_size);
132
                    av_strlcat(buf, name, buf_size);
133
                }
134
                ch++;
135
            }
136
        }
137
        av_strlcat(buf, ")", buf_size);
138
    }
139
}
140

    
141
int avcodec_channel_layout_num_channels(int64_t channel_layout)
142
{
143
    int count;
144
    uint64_t x = channel_layout;
145
    for (count = 0; x; count++)
146
        x &= x-1; // unset lowest set bit
147
    return count;
148
}
149

    
150
struct AVAudioConvert {
151
    int in_channels, out_channels;
152
    int fmt_pair;
153
};
154

    
155
AVAudioConvert *av_audio_convert_alloc(enum AVSampleFormat out_fmt, int out_channels,
156
                                       enum AVSampleFormat in_fmt, int in_channels,
157
                                       const float *matrix, int flags)
158
{
159
    AVAudioConvert *ctx;
160
    if (in_channels!=out_channels)
161
        return NULL;  /* FIXME: not supported */
162
    ctx = av_malloc(sizeof(AVAudioConvert));
163
    if (!ctx)
164
        return NULL;
165
    ctx->in_channels = in_channels;
166
    ctx->out_channels = out_channels;
167
    ctx->fmt_pair = out_fmt + AV_SAMPLE_FMT_NB*in_fmt;
168
    return ctx;
169
}
170

    
171
void av_audio_convert_free(AVAudioConvert *ctx)
172
{
173
    av_free(ctx);
174
}
175

    
176
int av_audio_convert(AVAudioConvert *ctx,
177
                           void * const out[6], const int out_stride[6],
178
                     const void * const  in[6], const int  in_stride[6], int len)
179
{
180
    int ch;
181

    
182
    //FIXME optimize common cases
183

    
184
    for(ch=0; ch<ctx->out_channels; ch++){
185
        const int is=  in_stride[ch];
186
        const int os= out_stride[ch];
187
        const uint8_t *pi=  in[ch];
188
        uint8_t *po= out[ch];
189
        uint8_t *end= po + os*len;
190
        if(!out[ch])
191
            continue;
192

    
193
#define CONV(ofmt, otype, ifmt, expr)\
194
if(ctx->fmt_pair == ofmt + AV_SAMPLE_FMT_NB*ifmt){\
195
    do{\
196
        *(otype*)po = expr; pi += is; po += os;\
197
    }while(po < end);\
198
}
199

    
200
//FIXME put things below under ifdefs so we do not waste space for cases no codec will need
201
//FIXME rounding ?
202

    
203
             CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_U8 ,  *(const uint8_t*)pi)
204
        else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<8)
205
        else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<24)
206
        else CONV(AV_SAMPLE_FMT_FLT, float  , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
207
        else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
208
        else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S16, (*(const int16_t*)pi>>8) + 0x80)
209
        else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16,  *(const int16_t*)pi)
210
        else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16,  *(const int16_t*)pi<<16)
211
        else CONV(AV_SAMPLE_FMT_FLT, float  , AV_SAMPLE_FMT_S16,  *(const int16_t*)pi*(1.0 / (1<<15)))
212
        else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S16,  *(const int16_t*)pi*(1.0 / (1<<15)))
213
        else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S32, (*(const int32_t*)pi>>24) + 0x80)
214
        else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32,  *(const int32_t*)pi>>16)
215
        else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32,  *(const int32_t*)pi)
216
        else CONV(AV_SAMPLE_FMT_FLT, float  , AV_SAMPLE_FMT_S32,  *(const int32_t*)pi*(1.0 / (1<<31)))
217
        else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S32,  *(const int32_t*)pi*(1.0 / (1<<31)))
218
        else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(  lrintf(*(const float*)pi * (1<<7)) + 0x80))
219
        else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(  lrintf(*(const float*)pi * (1<<15))))
220
        else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float*)pi * (1U<<31))))
221
        else CONV(AV_SAMPLE_FMT_FLT, float  , AV_SAMPLE_FMT_FLT, *(const float*)pi)
222
        else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_FLT, *(const float*)pi)
223
        else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(  lrint(*(const double*)pi * (1<<7)) + 0x80))
224
        else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(  lrint(*(const double*)pi * (1<<15))))
225
        else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double*)pi * (1U<<31))))
226
        else CONV(AV_SAMPLE_FMT_FLT, float  , AV_SAMPLE_FMT_DBL, *(const double*)pi)
227
        else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_DBL, *(const double*)pi)
228
        else return -1;
229
    }
230
    return 0;
231
}