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 (nb_channels > INT_MAX / align ||
00109 (int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)
00110 return AVERROR(EINVAL);
00111
00112 line_size = planar ? FFALIGN(nb_samples * sample_size, align) :
00113 FFALIGN(nb_samples * sample_size * nb_channels, align);
00114 if (linesize)
00115 *linesize = line_size;
00116
00117 return planar ? line_size * nb_channels : line_size;
00118 }
00119
00120 int av_samples_fill_arrays(uint8_t **audio_data, int *linesize,
00121 uint8_t *buf, int nb_channels, int nb_samples,
00122 enum AVSampleFormat sample_fmt, int align)
00123 {
00124 int ch, planar, buf_size;
00125
00126 planar = av_sample_fmt_is_planar(sample_fmt);
00127 buf_size = av_samples_get_buffer_size(linesize, nb_channels, nb_samples,
00128 sample_fmt, align);
00129 if (buf_size < 0)
00130 return buf_size;
00131
00132 audio_data[0] = buf;
00133 for (ch = 1; planar && ch < nb_channels; ch++)
00134 audio_data[ch] = audio_data[ch-1] + *linesize;
00135
00136 return 0;
00137 }
00138
00139 int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,
00140 int nb_samples, enum AVSampleFormat sample_fmt, int align)
00141 {
00142 uint8_t *buf;
00143 int size = av_samples_get_buffer_size(NULL, nb_channels, nb_samples,
00144 sample_fmt, align);
00145 if (size < 0)
00146 return size;
00147
00148 buf = av_mallocz(size);
00149 if (!buf)
00150 return AVERROR(ENOMEM);
00151
00152 size = av_samples_fill_arrays(audio_data, linesize, buf, nb_channels,
00153 nb_samples, sample_fmt, align);
00154 if (size < 0) {
00155 av_free(buf);
00156 return size;
00157 }
00158 return 0;
00159 }