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 const char *name;
00027 int bits;
00028 int planar;
00029 } SampleFmtInfo;
00030
00032 static const SampleFmtInfo sample_fmt_info[AV_SAMPLE_FMT_NB] = {
00033 [AV_SAMPLE_FMT_U8] = { .name = "u8", .bits = 8, .planar = 0 },
00034 [AV_SAMPLE_FMT_S16] = { .name = "s16", .bits = 16, .planar = 0 },
00035 [AV_SAMPLE_FMT_S32] = { .name = "s32", .bits = 32, .planar = 0 },
00036 [AV_SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32, .planar = 0 },
00037 [AV_SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64, .planar = 0 },
00038 [AV_SAMPLE_FMT_U8P] = { .name = "u8p", .bits = 8, .planar = 1 },
00039 [AV_SAMPLE_FMT_S16P] = { .name = "s16p", .bits = 16, .planar = 1 },
00040 [AV_SAMPLE_FMT_S32P] = { .name = "s32p", .bits = 32, .planar = 1 },
00041 [AV_SAMPLE_FMT_FLTP] = { .name = "fltp", .bits = 32, .planar = 1 },
00042 [AV_SAMPLE_FMT_DBLP] = { .name = "dblp", .bits = 64, .planar = 1 },
00043 };
00044
00045 const char *av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
00046 {
00047 if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
00048 return NULL;
00049 return sample_fmt_info[sample_fmt].name;
00050 }
00051
00052 enum AVSampleFormat av_get_sample_fmt(const char *name)
00053 {
00054 int i;
00055
00056 for (i = 0; i < AV_SAMPLE_FMT_NB; i++)
00057 if (!strcmp(sample_fmt_info[i].name, name))
00058 return i;
00059 return AV_SAMPLE_FMT_NONE;
00060 }
00061
00062 char *av_get_sample_fmt_string (char *buf, int buf_size, enum AVSampleFormat sample_fmt)
00063 {
00064
00065 if (sample_fmt < 0)
00066 snprintf(buf, buf_size, "name " " depth");
00067 else if (sample_fmt < AV_SAMPLE_FMT_NB) {
00068 SampleFmtInfo info = sample_fmt_info[sample_fmt];
00069 snprintf (buf, buf_size, "%-6s" " %2d ", info.name, info.bits);
00070 }
00071
00072 return buf;
00073 }
00074
00075 int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
00076 {
00077 return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
00078 0 : sample_fmt_info[sample_fmt].bits >> 3;
00079 }
00080
00081 #if FF_API_GET_BITS_PER_SAMPLE_FMT
00082 int av_get_bits_per_sample_fmt(enum AVSampleFormat sample_fmt)
00083 {
00084 return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
00085 0 : sample_fmt_info[sample_fmt].bits;
00086 }
00087 #endif
00088
00089 int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
00090 {
00091 if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
00092 return 0;
00093 return sample_fmt_info[sample_fmt].planar;
00094 }
00095
00096 int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
00097 enum AVSampleFormat sample_fmt, int align)
00098 {
00099 int line_size;
00100 int sample_size = av_get_bytes_per_sample(sample_fmt);
00101 int planar = av_sample_fmt_is_planar(sample_fmt);
00102
00103
00104 if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
00105 return AVERROR(EINVAL);
00106
00107
00108 if (!align) {
00109 if (nb_samples > INT_MAX - 31)
00110 return AVERROR(EINVAL);
00111 align = 32;
00112 }
00113
00114
00115 if (nb_channels > INT_MAX / align ||
00116 (int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)
00117 return AVERROR(EINVAL);
00118
00119 line_size = planar ? FFALIGN(nb_samples * sample_size, align) :
00120 FFALIGN(nb_samples * sample_size * nb_channels, align);
00121 if (linesize)
00122 *linesize = line_size;
00123
00124 return planar ? line_size * nb_channels : line_size;
00125 }
00126
00127 int av_samples_fill_arrays(uint8_t **audio_data, int *linesize,
00128 uint8_t *buf, int nb_channels, int nb_samples,
00129 enum AVSampleFormat sample_fmt, int align)
00130 {
00131 int ch, planar, buf_size;
00132
00133 planar = av_sample_fmt_is_planar(sample_fmt);
00134 buf_size = av_samples_get_buffer_size(linesize, nb_channels, nb_samples,
00135 sample_fmt, align);
00136 if (buf_size < 0)
00137 return buf_size;
00138
00139 audio_data[0] = buf;
00140 for (ch = 1; planar && ch < nb_channels; ch++)
00141 audio_data[ch] = audio_data[ch-1] + *linesize;
00142
00143 return 0;
00144 }
00145
00146 int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,
00147 int nb_samples, enum AVSampleFormat sample_fmt, int align)
00148 {
00149 uint8_t *buf;
00150 int size = av_samples_get_buffer_size(NULL, nb_channels, nb_samples,
00151 sample_fmt, align);
00152 if (size < 0)
00153 return size;
00154
00155 buf = av_mallocz(size);
00156 if (!buf)
00157 return AVERROR(ENOMEM);
00158
00159 size = av_samples_fill_arrays(audio_data, linesize, buf, nb_channels,
00160 nb_samples, sample_fmt, align);
00161 if (size < 0) {
00162 av_free(buf);
00163 return size;
00164 }
00165 return 0;
00166 }