00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "samplefmt.h"
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024
00025 typedef struct SampleFmtInfo {
00026 char name[8];
00027 int bits;
00028 int planar;
00029 enum AVSampleFormat altform;
00030 } SampleFmtInfo;
00031
00033 static const SampleFmtInfo sample_fmt_info[AV_SAMPLE_FMT_NB] = {
00034 [AV_SAMPLE_FMT_U8] = { .name = "u8", .bits = 8, .planar = 0, .altform = AV_SAMPLE_FMT_U8P },
00035 [AV_SAMPLE_FMT_S16] = { .name = "s16", .bits = 16, .planar = 0, .altform = AV_SAMPLE_FMT_S16P },
00036 [AV_SAMPLE_FMT_S32] = { .name = "s32", .bits = 32, .planar = 0, .altform = AV_SAMPLE_FMT_S32P },
00037 [AV_SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32, .planar = 0, .altform = AV_SAMPLE_FMT_FLTP },
00038 [AV_SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64, .planar = 0, .altform = AV_SAMPLE_FMT_DBLP },
00039 [AV_SAMPLE_FMT_U8P] = { .name = "u8p", .bits = 8, .planar = 1, .altform = AV_SAMPLE_FMT_U8 },
00040 [AV_SAMPLE_FMT_S16P] = { .name = "s16p", .bits = 16, .planar = 1, .altform = AV_SAMPLE_FMT_S16 },
00041 [AV_SAMPLE_FMT_S32P] = { .name = "s32p", .bits = 32, .planar = 1, .altform = AV_SAMPLE_FMT_S32 },
00042 [AV_SAMPLE_FMT_FLTP] = { .name = "fltp", .bits = 32, .planar = 1, .altform = AV_SAMPLE_FMT_FLT },
00043 [AV_SAMPLE_FMT_DBLP] = { .name = "dblp", .bits = 64, .planar = 1, .altform = AV_SAMPLE_FMT_DBL },
00044 };
00045
00046 const char *av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
00047 {
00048 if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
00049 return NULL;
00050 return sample_fmt_info[sample_fmt].name;
00051 }
00052
00053 enum AVSampleFormat av_get_sample_fmt(const char *name)
00054 {
00055 int i;
00056
00057 for (i = 0; i < AV_SAMPLE_FMT_NB; i++)
00058 if (!strcmp(sample_fmt_info[i].name, name))
00059 return i;
00060 return AV_SAMPLE_FMT_NONE;
00061 }
00062
00063 enum AVSampleFormat av_get_alt_sample_fmt(enum AVSampleFormat sample_fmt, int planar)
00064 {
00065 if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
00066 return AV_SAMPLE_FMT_NONE;
00067 if (sample_fmt_info[sample_fmt].planar == planar)
00068 return sample_fmt;
00069 return sample_fmt_info[sample_fmt].altform;
00070 }
00071
00072 enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
00073 {
00074 if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
00075 return AV_SAMPLE_FMT_NONE;
00076 if (sample_fmt_info[sample_fmt].planar)
00077 return sample_fmt_info[sample_fmt].altform;
00078 return sample_fmt;
00079 }
00080
00081 enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
00082 {
00083 if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
00084 return AV_SAMPLE_FMT_NONE;
00085 if (sample_fmt_info[sample_fmt].planar)
00086 return sample_fmt;
00087 return sample_fmt_info[sample_fmt].altform;
00088 }
00089
00090 char *av_get_sample_fmt_string (char *buf, int buf_size, enum AVSampleFormat sample_fmt)
00091 {
00092
00093 if (sample_fmt < 0)
00094 snprintf(buf, buf_size, "name " " depth");
00095 else if (sample_fmt < AV_SAMPLE_FMT_NB) {
00096 SampleFmtInfo info = sample_fmt_info[sample_fmt];
00097 snprintf (buf, buf_size, "%-6s" " %2d ", info.name, info.bits);
00098 }
00099
00100 return buf;
00101 }
00102
00103 int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
00104 {
00105 return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
00106 0 : sample_fmt_info[sample_fmt].bits >> 3;
00107 }
00108
00109 #if FF_API_GET_BITS_PER_SAMPLE_FMT
00110 int av_get_bits_per_sample_fmt(enum AVSampleFormat sample_fmt)
00111 {
00112 return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
00113 0 : sample_fmt_info[sample_fmt].bits;
00114 }
00115 #endif
00116
00117 int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
00118 {
00119 if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
00120 return 0;
00121 return sample_fmt_info[sample_fmt].planar;
00122 }
00123
00124 int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
00125 enum AVSampleFormat sample_fmt, int align)
00126 {
00127 int line_size;
00128 int sample_size = av_get_bytes_per_sample(sample_fmt);
00129 int planar = av_sample_fmt_is_planar(sample_fmt);
00130
00131
00132 if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
00133 return AVERROR(EINVAL);
00134
00135
00136 if (!align) {
00137 align = 1;
00138 nb_samples = FFALIGN(nb_samples, 32);
00139 }
00140
00141
00142 if (nb_channels > INT_MAX / align ||
00143 (int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)
00144 return AVERROR(EINVAL);
00145
00146 line_size = planar ? FFALIGN(nb_samples * sample_size, align) :
00147 FFALIGN(nb_samples * sample_size * nb_channels, align);
00148 if (linesize)
00149 *linesize = line_size;
00150
00151 return planar ? line_size * nb_channels : line_size;
00152 }
00153
00154 int av_samples_fill_arrays(uint8_t **audio_data, int *linesize,
00155 const uint8_t *buf, int nb_channels, int nb_samples,
00156 enum AVSampleFormat sample_fmt, int align)
00157 {
00158 int ch, planar, buf_size, line_size;
00159
00160 planar = av_sample_fmt_is_planar(sample_fmt);
00161 buf_size = av_samples_get_buffer_size(&line_size, nb_channels, nb_samples,
00162 sample_fmt, align);
00163 if (buf_size < 0)
00164 return buf_size;
00165
00166 audio_data[0] = buf;
00167 for (ch = 1; planar && ch < nb_channels; ch++)
00168 audio_data[ch] = audio_data[ch-1] + line_size;
00169
00170 if (linesize)
00171 *linesize = line_size;
00172
00173 return 0;
00174 }
00175
00176 int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,
00177 int nb_samples, enum AVSampleFormat sample_fmt, int align)
00178 {
00179 uint8_t *buf;
00180 int size = av_samples_get_buffer_size(NULL, nb_channels, nb_samples,
00181 sample_fmt, align);
00182 if (size < 0)
00183 return size;
00184
00185 buf = av_mallocz(size);
00186 if (!buf)
00187 return AVERROR(ENOMEM);
00188
00189 size = av_samples_fill_arrays(audio_data, linesize, buf, nb_channels,
00190 nb_samples, sample_fmt, align);
00191 if (size < 0) {
00192 av_free(buf);
00193 return size;
00194 }
00195 return 0;
00196 }
00197
00198 int av_samples_copy(uint8_t **dst, uint8_t * const *src, int dst_offset,
00199 int src_offset, int nb_samples, int nb_channels,
00200 enum AVSampleFormat sample_fmt)
00201 {
00202 int planar = av_sample_fmt_is_planar(sample_fmt);
00203 int planes = planar ? nb_channels : 1;
00204 int block_align = av_get_bytes_per_sample(sample_fmt) * (planar ? 1 : nb_channels);
00205 int data_size = nb_samples * block_align;
00206 int i;
00207
00208 dst_offset *= block_align;
00209 src_offset *= block_align;
00210
00211 for (i = 0; i < planes; i++)
00212 memcpy(dst[i] + dst_offset, src[i] + src_offset, data_size);
00213
00214 return 0;
00215 }
00216
00217 int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples,
00218 int nb_channels, enum AVSampleFormat sample_fmt)
00219 {
00220 int planar = av_sample_fmt_is_planar(sample_fmt);
00221 int planes = planar ? nb_channels : 1;
00222 int block_align = av_get_bytes_per_sample(sample_fmt) * (planar ? 1 : nb_channels);
00223 int data_size = nb_samples * block_align;
00224 int fill_char = (sample_fmt == AV_SAMPLE_FMT_U8 ||
00225 sample_fmt == AV_SAMPLE_FMT_U8P) ? 0x80 : 0x00;
00226 int i;
00227
00228 offset *= block_align;
00229
00230 for (i = 0; i < planes; i++)
00231 memset(audio_data[i] + offset, fill_char, data_size);
00232
00233 return 0;
00234 }