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 } SampleFmtInfo;
00029
00031 static const SampleFmtInfo sample_fmt_info[AV_SAMPLE_FMT_NB] = {
00032 [AV_SAMPLE_FMT_U8] = { .name = "u8", .bits = 8 },
00033 [AV_SAMPLE_FMT_S16] = { .name = "s16", .bits = 16 },
00034 [AV_SAMPLE_FMT_S32] = { .name = "s32", .bits = 32 },
00035 [AV_SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32 },
00036 [AV_SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64 },
00037 };
00038
00039 const char *av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
00040 {
00041 if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
00042 return NULL;
00043 return sample_fmt_info[sample_fmt].name;
00044 }
00045
00046 enum AVSampleFormat av_get_sample_fmt(const char *name)
00047 {
00048 int i;
00049
00050 for (i = 0; i < AV_SAMPLE_FMT_NB; i++)
00051 if (!strcmp(sample_fmt_info[i].name, name))
00052 return i;
00053 return AV_SAMPLE_FMT_NONE;
00054 }
00055
00056 char *av_get_sample_fmt_string (char *buf, int buf_size, enum AVSampleFormat sample_fmt)
00057 {
00058
00059 if (sample_fmt < 0)
00060 snprintf(buf, buf_size, "name " " depth");
00061 else if (sample_fmt < AV_SAMPLE_FMT_NB) {
00062 SampleFmtInfo info = sample_fmt_info[sample_fmt];
00063 snprintf (buf, buf_size, "%-6s" " %2d ", info.name, info.bits);
00064 }
00065
00066 return buf;
00067 }
00068
00069 int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
00070 {
00071 return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
00072 0 : sample_fmt_info[sample_fmt].bits >> 3;
00073 }
00074
00075 #if FF_API_GET_BITS_PER_SAMPLE_FMT
00076 int av_get_bits_per_sample_fmt(enum AVSampleFormat sample_fmt)
00077 {
00078 return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
00079 0 : sample_fmt_info[sample_fmt].bits;
00080 }
00081 #endif
00082
00083 int av_samples_fill_arrays(uint8_t *pointers[8], int linesizes[8],
00084 uint8_t *buf, int nb_channels, int nb_samples,
00085 enum AVSampleFormat sample_fmt, int planar, int align)
00086 {
00087 int i, linesize;
00088 int sample_size = av_get_bits_per_sample_fmt(sample_fmt) >> 3;
00089
00090 if (nb_channels * (uint64_t)nb_samples * sample_size >= INT_MAX - align*(uint64_t)nb_channels)
00091 return AVERROR(EINVAL);
00092 linesize = planar ? FFALIGN(nb_samples*sample_size, align) :
00093 FFALIGN(nb_samples*sample_size*nb_channels, align);
00094
00095 if (pointers) {
00096 pointers[0] = buf;
00097 for (i = 1; planar && i < nb_channels; i++) {
00098 pointers[i] = pointers[i-1] + linesize;
00099 }
00100 memset(&pointers[i], 0, (8-i) * sizeof(pointers[0]));
00101 }
00102
00103 if (linesizes) {
00104 linesizes[0] = linesize;
00105 for (i = 1; planar && i < nb_channels; i++)
00106 linesizes[i] = linesizes[0];
00107 memset(&linesizes[i], 0, (8-i) * sizeof(linesizes[0]));
00108 }
00109
00110 return planar ? linesize * nb_channels : linesize;
00111 }
00112
00113 int av_samples_alloc(uint8_t *pointers[8], int linesizes[8],
00114 int nb_channels, int nb_samples,
00115 enum AVSampleFormat sample_fmt, int planar,
00116 int align)
00117 {
00118 uint8_t *buf;
00119 int size = av_samples_fill_arrays(NULL, NULL,
00120 NULL, nb_channels, nb_samples,
00121 sample_fmt, planar, align);
00122
00123 buf = av_mallocz(size);
00124 if (!buf)
00125 return AVERROR(ENOMEM);
00126
00127 return av_samples_fill_arrays(pointers, linesizes,
00128 buf, nb_channels, nb_samples,
00129 sample_fmt, planar, align);
00130 }