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