]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/audioconvert.c
Removed static omkization
[frescor/ffmpeg.git] / libavcodec / audioconvert.c
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 libavcodec/audioconvert.c
24  * audio conversion
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27
28 #include "libavutil/avstring.h"
29 #include "avcodec.h"
30 #include "audioconvert.h"
31
32 typedef struct SampleFmtInfo {
33     const char *name;
34     int bits;
35 } SampleFmtInfo;
36
37 /** this table gives more information about formats */
38 static const SampleFmtInfo sample_fmt_info[SAMPLE_FMT_NB] = {
39     [SAMPLE_FMT_U8]  = { .name = "u8",  .bits = 8 },
40     [SAMPLE_FMT_S16] = { .name = "s16", .bits = 16 },
41     [SAMPLE_FMT_S32] = { .name = "s32", .bits = 32 },
42     [SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32 },
43     [SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64 },
44 };
45
46 const char *avcodec_get_sample_fmt_name(int sample_fmt)
47 {
48     if (sample_fmt < 0 || sample_fmt >= SAMPLE_FMT_NB)
49         return NULL;
50     return sample_fmt_info[sample_fmt].name;
51 }
52
53 enum SampleFormat avcodec_get_sample_fmt(const char* name)
54 {
55     int i;
56
57     for (i=0; i < SAMPLE_FMT_NB; i++)
58         if (!strcmp(sample_fmt_info[i].name, name))
59             return i;
60     return SAMPLE_FMT_NONE;
61 }
62
63 void avcodec_sample_fmt_string (char *buf, int buf_size, int sample_fmt)
64 {
65     /* print header */
66     if (sample_fmt < 0)
67         snprintf (buf, buf_size, "name  " " depth");
68     else if (sample_fmt < SAMPLE_FMT_NB) {
69         SampleFmtInfo info= sample_fmt_info[sample_fmt];
70         snprintf (buf, buf_size, "%-6s" "   %2d ", info.name, info.bits);
71     }
72 }
73
74 static const char* const channel_names[]={
75     "FL", "FR", "FC", "LFE", "BL",  "BR",  "FLC", "FRC",
76     "BC", "SL", "SR", "TC",  "TFL", "TFC", "TFR", "TBL",
77     "TBC", "TBR",
78     [29] = "DL",
79     [30] = "DR",
80 };
81
82 const char *get_channel_name(int channel_id)
83 {
84     if (channel_id<0 || channel_id>=FF_ARRAY_ELEMS(channel_names))
85         return NULL;
86     return channel_names[channel_id];
87 }
88
89 int64_t avcodec_guess_channel_layout(int nb_channels, enum CodecID codec_id, const char *fmt_name)
90 {
91     switch(nb_channels) {
92     case 1: return CH_LAYOUT_MONO;
93     case 2: return CH_LAYOUT_STEREO;
94     case 3: return CH_LAYOUT_SURROUND;
95     case 4: return CH_LAYOUT_QUAD;
96     case 5: return CH_LAYOUT_5POINT0;
97     case 6: return CH_LAYOUT_5POINT1;
98     case 8: return CH_LAYOUT_7POINT1;
99     default: return 0;
100     }
101 }
102
103 static const struct {
104     const char *name;
105     int         nb_channels;
106     int64_t     layout;
107 } channel_layout_map[] = {
108     { "mono",        1,  CH_LAYOUT_MONO },
109     { "stereo",      2,  CH_LAYOUT_STEREO },
110     { "surround",    3,  CH_LAYOUT_SURROUND },
111     { "4.0",         4,  CH_LAYOUT_4POINT0 },
112     { "quad",        4,  CH_LAYOUT_QUAD },
113     { "5.0",         5,  CH_LAYOUT_5POINT0 },
114     { "5.0",         5,  CH_LAYOUT_5POINT0_BACK },
115     { "5.1",         6,  CH_LAYOUT_5POINT1 },
116     { "5.1",         6,  CH_LAYOUT_5POINT1_BACK },
117     { "5.1+downmix", 8,  CH_LAYOUT_5POINT1|CH_LAYOUT_STEREO_DOWNMIX, },
118     { "7.1",         8,  CH_LAYOUT_7POINT1 },
119     { "7.1(wide)",   8,  CH_LAYOUT_7POINT1_WIDE },
120     { "7.1+downmix", 10, CH_LAYOUT_7POINT1|CH_LAYOUT_STEREO_DOWNMIX, },
121     { 0 }
122 };
123
124 void avcodec_get_channel_layout_string(char *buf, int buf_size, int nb_channels, int64_t channel_layout)
125 {
126     int i;
127
128     if (channel_layout==0)
129         channel_layout = avcodec_guess_channel_layout(nb_channels, CODEC_ID_NONE, NULL);
130
131     for (i=0; channel_layout_map[i].name; i++)
132         if (nb_channels    == channel_layout_map[i].nb_channels &&
133             channel_layout == channel_layout_map[i].layout) {
134             av_strlcpy(buf, channel_layout_map[i].name, buf_size);
135             return;
136         }
137
138     snprintf(buf, buf_size, "%d channels", nb_channels);
139     if (channel_layout) {
140         int i,ch;
141         av_strlcat(buf, " (", buf_size);
142         for(i=0,ch=0; i<64; i++) {
143             if ((channel_layout & (1L<<i))) {
144                 const char *name = get_channel_name(i);
145                 if (name) {
146                     if (ch>0) av_strlcat(buf, "|", buf_size);
147                     av_strlcat(buf, name, buf_size);
148                 }
149                 ch++;
150             }
151         }
152         av_strlcat(buf, ")", buf_size);
153     }
154 }
155
156 int avcodec_channel_layout_num_channels(int64_t channel_layout)
157 {
158     int count;
159     uint64_t x = channel_layout;
160     for (count = 0; x; count++)
161         x &= x-1; // unset lowest set bit
162     return count;
163 }
164
165 struct AVAudioConvert {
166     int in_channels, out_channels;
167     int fmt_pair;
168 };
169
170 AVAudioConvert *av_audio_convert_alloc(enum SampleFormat out_fmt, int out_channels,
171                                        enum SampleFormat in_fmt, int in_channels,
172                                        const float *matrix, int flags)
173 {
174     AVAudioConvert *ctx;
175     if (in_channels!=out_channels)
176         return NULL;  /* FIXME: not supported */
177     ctx = av_malloc(sizeof(AVAudioConvert));
178     if (!ctx)
179         return NULL;
180     ctx->in_channels = in_channels;
181     ctx->out_channels = out_channels;
182     ctx->fmt_pair = out_fmt + SAMPLE_FMT_NB*in_fmt;
183     return ctx;
184 }
185
186 void av_audio_convert_free(AVAudioConvert *ctx)
187 {
188     av_free(ctx);
189 }
190
191 int av_audio_convert(AVAudioConvert *ctx,
192                            void * const out[6], const int out_stride[6],
193                      const void * const  in[6], const int  in_stride[6], int len)
194 {
195     int ch;
196
197     //FIXME optimize common cases
198
199     for(ch=0; ch<ctx->out_channels; ch++){
200         const int is=  in_stride[ch];
201         const int os= out_stride[ch];
202         const uint8_t *pi=  in[ch];
203         uint8_t *po= out[ch];
204         uint8_t *end= po + os*len;
205         if(!out[ch])
206             continue;
207
208 #define CONV(ofmt, otype, ifmt, expr)\
209 if(ctx->fmt_pair == ofmt + SAMPLE_FMT_NB*ifmt){\
210     do{\
211         *(otype*)po = expr; pi += is; po += os;\
212     }while(po < end);\
213 }
214
215 //FIXME put things below under ifdefs so we do not waste space for cases no codec will need
216 //FIXME rounding and clipping ?
217
218              CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_U8 ,  *(const uint8_t*)pi)
219         else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<8)
220         else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<24)
221         else CONV(SAMPLE_FMT_FLT, float  , SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
222         else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
223         else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_S16, (*(const int16_t*)pi>>8) + 0x80)
224         else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_S16,  *(const int16_t*)pi)
225         else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_S16,  *(const int16_t*)pi<<16)
226         else CONV(SAMPLE_FMT_FLT, float  , SAMPLE_FMT_S16,  *(const int16_t*)pi*(1.0 / (1<<15)))
227         else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_S16,  *(const int16_t*)pi*(1.0 / (1<<15)))
228         else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_S32, (*(const int32_t*)pi>>24) + 0x80)
229         else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_S32,  *(const int32_t*)pi>>16)
230         else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_S32,  *(const int32_t*)pi)
231         else CONV(SAMPLE_FMT_FLT, float  , SAMPLE_FMT_S32,  *(const int32_t*)pi*(1.0 / (1<<31)))
232         else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_S32,  *(const int32_t*)pi*(1.0 / (1<<31)))
233         else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_FLT, lrintf(*(const float*)pi * (1<<7)) + 0x80)
234         else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_FLT, lrintf(*(const float*)pi * (1<<15)))
235         else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_FLT, lrintf(*(const float*)pi * (1<<31)))
236         else CONV(SAMPLE_FMT_FLT, float  , SAMPLE_FMT_FLT, *(const float*)pi)
237         else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_FLT, *(const float*)pi)
238         else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_DBL, lrint(*(const double*)pi * (1<<7)) + 0x80)
239         else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_DBL, lrint(*(const double*)pi * (1<<15)))
240         else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_DBL, lrint(*(const double*)pi * (1<<31)))
241         else CONV(SAMPLE_FMT_FLT, float  , SAMPLE_FMT_DBL, *(const double*)pi)
242         else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_DBL, *(const double*)pi)
243         else return -1;
244     }
245     return 0;
246 }