libavutil/samplefmt.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of FFmpeg.
00003  *
00004  * FFmpeg is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * FFmpeg is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with FFmpeg; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 char *av_get_sample_fmt_string (char *buf, int buf_size, enum AVSampleFormat sample_fmt)
00073 {
00074     /* print header */
00075     if (sample_fmt < 0)
00076         snprintf(buf, buf_size, "name  " " depth");
00077     else if (sample_fmt < AV_SAMPLE_FMT_NB) {
00078         SampleFmtInfo info = sample_fmt_info[sample_fmt];
00079         snprintf (buf, buf_size, "%-6s" "   %2d ", info.name, info.bits);
00080     }
00081 
00082     return buf;
00083 }
00084 
00085 int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
00086 {
00087      return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
00088         0 : sample_fmt_info[sample_fmt].bits >> 3;
00089 }
00090 
00091 #if FF_API_GET_BITS_PER_SAMPLE_FMT
00092 int av_get_bits_per_sample_fmt(enum AVSampleFormat sample_fmt)
00093 {
00094     return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
00095         0 : sample_fmt_info[sample_fmt].bits;
00096 }
00097 #endif
00098 
00099 int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
00100 {
00101      if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
00102          return 0;
00103      return sample_fmt_info[sample_fmt].planar;
00104 }
00105 
00106 int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
00107                                enum AVSampleFormat sample_fmt, int align)
00108 {
00109     int line_size;
00110     int sample_size = av_get_bytes_per_sample(sample_fmt);
00111     int planar      = av_sample_fmt_is_planar(sample_fmt);
00112 
00113     /* validate parameter ranges */
00114     if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
00115         return AVERROR(EINVAL);
00116 
00117     /* check for integer overflow */
00118     if (nb_channels > INT_MAX / align ||
00119         (int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)
00120         return AVERROR(EINVAL);
00121 
00122     line_size = planar ? FFALIGN(nb_samples * sample_size,               align) :
00123                          FFALIGN(nb_samples * sample_size * nb_channels, align);
00124     if (linesize)
00125         *linesize = line_size;
00126 
00127     return planar ? line_size * nb_channels : line_size;
00128 }
00129 
00130 int av_samples_fill_arrays(uint8_t **audio_data, int *linesize,
00131                            uint8_t *buf, int nb_channels, int nb_samples,
00132                            enum AVSampleFormat sample_fmt, int align)
00133 {
00134     int ch, planar, buf_size;
00135 
00136     planar   = av_sample_fmt_is_planar(sample_fmt);
00137     buf_size = av_samples_get_buffer_size(linesize, nb_channels, nb_samples,
00138                                           sample_fmt, align);
00139     if (buf_size < 0)
00140         return buf_size;
00141 
00142     audio_data[0] = buf;
00143     for (ch = 1; planar && ch < nb_channels; ch++)
00144         audio_data[ch] = audio_data[ch-1] + *linesize;
00145 
00146     return 0;
00147 }
00148 
00149 int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,
00150                      int nb_samples, enum AVSampleFormat sample_fmt, int align)
00151 {
00152     uint8_t *buf;
00153     int size = av_samples_get_buffer_size(NULL, nb_channels, nb_samples,
00154                                           sample_fmt, align);
00155     if (size < 0)
00156         return size;
00157 
00158     buf = av_mallocz(size);
00159     if (!buf)
00160         return AVERROR(ENOMEM);
00161 
00162     size = av_samples_fill_arrays(audio_data, linesize, buf, nb_channels,
00163                                   nb_samples, sample_fmt, align);
00164     if (size < 0) {
00165         av_free(buf);
00166         return size;
00167     }
00168     return 0;
00169 }