2010-11-03 00:08:02 +02:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "common.h"
|
2010-11-03 00:08:02 +02:00
|
|
|
#include "samplefmt.h"
|
|
|
|
|
2011-02-07 15:37:08 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2010-11-03 00:08:02 +02:00
|
|
|
typedef struct SampleFmtInfo {
|
2011-12-03 02:10:56 +03:00
|
|
|
char name[8];
|
2010-11-03 00:08:02 +02:00
|
|
|
int bits;
|
2011-11-12 23:58:40 +03:00
|
|
|
int planar;
|
2011-11-24 06:30:09 +03:00
|
|
|
enum AVSampleFormat altform; ///< planar<->packed alternative form
|
2010-11-03 00:08:02 +02:00
|
|
|
} SampleFmtInfo;
|
|
|
|
|
|
|
|
/** this table gives more information about formats */
|
|
|
|
static const SampleFmtInfo sample_fmt_info[AV_SAMPLE_FMT_NB] = {
|
2011-11-24 06:30:09 +03:00
|
|
|
[AV_SAMPLE_FMT_U8] = { .name = "u8", .bits = 8, .planar = 0, .altform = AV_SAMPLE_FMT_U8P },
|
|
|
|
[AV_SAMPLE_FMT_S16] = { .name = "s16", .bits = 16, .planar = 0, .altform = AV_SAMPLE_FMT_S16P },
|
|
|
|
[AV_SAMPLE_FMT_S32] = { .name = "s32", .bits = 32, .planar = 0, .altform = AV_SAMPLE_FMT_S32P },
|
|
|
|
[AV_SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32, .planar = 0, .altform = AV_SAMPLE_FMT_FLTP },
|
|
|
|
[AV_SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64, .planar = 0, .altform = AV_SAMPLE_FMT_DBLP },
|
|
|
|
[AV_SAMPLE_FMT_U8P] = { .name = "u8p", .bits = 8, .planar = 1, .altform = AV_SAMPLE_FMT_U8 },
|
|
|
|
[AV_SAMPLE_FMT_S16P] = { .name = "s16p", .bits = 16, .planar = 1, .altform = AV_SAMPLE_FMT_S16 },
|
|
|
|
[AV_SAMPLE_FMT_S32P] = { .name = "s32p", .bits = 32, .planar = 1, .altform = AV_SAMPLE_FMT_S32 },
|
|
|
|
[AV_SAMPLE_FMT_FLTP] = { .name = "fltp", .bits = 32, .planar = 1, .altform = AV_SAMPLE_FMT_FLT },
|
|
|
|
[AV_SAMPLE_FMT_DBLP] = { .name = "dblp", .bits = 64, .planar = 1, .altform = AV_SAMPLE_FMT_DBL },
|
2010-11-03 00:08:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const char *av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
|
|
|
|
{
|
|
|
|
if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
|
|
|
|
return NULL;
|
|
|
|
return sample_fmt_info[sample_fmt].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum AVSampleFormat av_get_sample_fmt(const char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < AV_SAMPLE_FMT_NB; i++)
|
|
|
|
if (!strcmp(sample_fmt_info[i].name, name))
|
|
|
|
return i;
|
|
|
|
return AV_SAMPLE_FMT_NONE;
|
|
|
|
}
|
|
|
|
|
2011-11-24 06:30:09 +03:00
|
|
|
enum AVSampleFormat av_get_alt_sample_fmt(enum AVSampleFormat sample_fmt, int planar)
|
|
|
|
{
|
|
|
|
if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
|
|
|
|
return AV_SAMPLE_FMT_NONE;
|
|
|
|
if (sample_fmt_info[sample_fmt].planar == planar)
|
|
|
|
return sample_fmt;
|
|
|
|
return sample_fmt_info[sample_fmt].altform;
|
|
|
|
}
|
|
|
|
|
2012-04-06 00:00:53 +03:00
|
|
|
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
|
|
|
|
{
|
|
|
|
if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
|
|
|
|
return AV_SAMPLE_FMT_NONE;
|
|
|
|
if (sample_fmt_info[sample_fmt].planar)
|
|
|
|
return sample_fmt_info[sample_fmt].altform;
|
|
|
|
return sample_fmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
|
|
|
|
{
|
|
|
|
if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
|
|
|
|
return AV_SAMPLE_FMT_NONE;
|
|
|
|
if (sample_fmt_info[sample_fmt].planar)
|
|
|
|
return sample_fmt;
|
|
|
|
return sample_fmt_info[sample_fmt].altform;
|
|
|
|
}
|
|
|
|
|
2010-11-03 00:08:02 +02:00
|
|
|
char *av_get_sample_fmt_string (char *buf, int buf_size, enum AVSampleFormat sample_fmt)
|
|
|
|
{
|
|
|
|
/* print header */
|
|
|
|
if (sample_fmt < 0)
|
|
|
|
snprintf(buf, buf_size, "name " " depth");
|
|
|
|
else if (sample_fmt < AV_SAMPLE_FMT_NB) {
|
|
|
|
SampleFmtInfo info = sample_fmt_info[sample_fmt];
|
|
|
|
snprintf (buf, buf_size, "%-6s" " %2d ", info.name, info.bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2010-11-03 00:20:49 +02:00
|
|
|
|
2011-06-06 02:14:50 +03:00
|
|
|
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
|
|
|
|
{
|
|
|
|
return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
|
|
|
|
0 : sample_fmt_info[sample_fmt].bits >> 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if FF_API_GET_BITS_PER_SAMPLE_FMT
|
2010-11-03 00:20:49 +02:00
|
|
|
int av_get_bits_per_sample_fmt(enum AVSampleFormat sample_fmt)
|
|
|
|
{
|
|
|
|
return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
|
|
|
|
0 : sample_fmt_info[sample_fmt].bits;
|
|
|
|
}
|
2011-06-06 02:14:50 +03:00
|
|
|
#endif
|
2011-01-15 01:00:00 +02:00
|
|
|
|
2011-11-12 23:58:40 +03:00
|
|
|
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
|
|
|
|
{
|
|
|
|
if (sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB)
|
|
|
|
return 0;
|
|
|
|
return sample_fmt_info[sample_fmt].planar;
|
|
|
|
}
|
2011-11-12 23:43:43 +03:00
|
|
|
|
|
|
|
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
|
|
|
|
enum AVSampleFormat sample_fmt, int align)
|
2011-01-15 01:00:00 +02:00
|
|
|
{
|
2011-11-12 23:43:43 +03:00
|
|
|
int line_size;
|
2011-06-25 22:54:58 +03:00
|
|
|
int sample_size = av_get_bytes_per_sample(sample_fmt);
|
2011-11-12 23:43:43 +03:00
|
|
|
int planar = av_sample_fmt_is_planar(sample_fmt);
|
2011-01-15 01:00:00 +02:00
|
|
|
|
2011-11-12 23:43:43 +03:00
|
|
|
/* validate parameter ranges */
|
|
|
|
if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
|
2011-01-15 01:00:00 +02:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2012-03-28 04:31:14 +03:00
|
|
|
/* auto-select alignment if not specified */
|
2012-04-10 17:53:55 +03:00
|
|
|
if (!align) {
|
2014-01-30 22:08:38 +03:00
|
|
|
if (nb_samples > INT_MAX - 31)
|
|
|
|
return AVERROR(EINVAL);
|
2012-04-10 17:53:55 +03:00
|
|
|
align = 1;
|
|
|
|
nb_samples = FFALIGN(nb_samples, 32);
|
|
|
|
}
|
2012-03-28 04:31:14 +03:00
|
|
|
|
2011-11-12 23:43:43 +03:00
|
|
|
/* check for integer overflow */
|
|
|
|
if (nb_channels > INT_MAX / align ||
|
|
|
|
(int64_t)nb_channels * nb_samples > (INT_MAX - (align * nb_channels)) / sample_size)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
line_size = planar ? FFALIGN(nb_samples * sample_size, align) :
|
|
|
|
FFALIGN(nb_samples * sample_size * nb_channels, align);
|
|
|
|
if (linesize)
|
|
|
|
*linesize = line_size;
|
2011-01-15 01:00:00 +02:00
|
|
|
|
2011-11-12 23:43:43 +03:00
|
|
|
return planar ? line_size * nb_channels : line_size;
|
2011-01-15 01:00:00 +02:00
|
|
|
}
|
|
|
|
|
2011-11-12 23:43:43 +03:00
|
|
|
int av_samples_fill_arrays(uint8_t **audio_data, int *linesize,
|
2012-04-11 15:51:57 +03:00
|
|
|
const uint8_t *buf, int nb_channels, int nb_samples,
|
2011-11-12 23:43:43 +03:00
|
|
|
enum AVSampleFormat sample_fmt, int align)
|
|
|
|
{
|
2012-03-28 04:34:47 +03:00
|
|
|
int ch, planar, buf_size, line_size;
|
2011-11-12 23:43:43 +03:00
|
|
|
|
|
|
|
planar = av_sample_fmt_is_planar(sample_fmt);
|
2012-03-28 04:34:47 +03:00
|
|
|
buf_size = av_samples_get_buffer_size(&line_size, nb_channels, nb_samples,
|
2011-11-12 23:43:43 +03:00
|
|
|
sample_fmt, align);
|
|
|
|
if (buf_size < 0)
|
|
|
|
return buf_size;
|
|
|
|
|
2012-07-27 19:11:02 +03:00
|
|
|
audio_data[0] = (uint8_t *)buf;
|
2011-11-12 23:43:43 +03:00
|
|
|
for (ch = 1; planar && ch < nb_channels; ch++)
|
2012-03-28 04:34:47 +03:00
|
|
|
audio_data[ch] = audio_data[ch-1] + line_size;
|
|
|
|
|
|
|
|
if (linesize)
|
|
|
|
*linesize = line_size;
|
2011-11-12 23:43:43 +03:00
|
|
|
|
2012-11-25 22:23:32 +03:00
|
|
|
#if FF_API_SAMPLES_UTILS_RETURN_ZERO
|
2011-11-12 23:43:43 +03:00
|
|
|
return 0;
|
2012-11-25 22:23:32 +03:00
|
|
|
#else
|
|
|
|
return buf_size;
|
|
|
|
#endif
|
2011-11-12 23:43:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,
|
|
|
|
int nb_samples, enum AVSampleFormat sample_fmt, int align)
|
2011-01-15 01:00:00 +02:00
|
|
|
{
|
|
|
|
uint8_t *buf;
|
2011-11-12 23:43:43 +03:00
|
|
|
int size = av_samples_get_buffer_size(NULL, nb_channels, nb_samples,
|
|
|
|
sample_fmt, align);
|
|
|
|
if (size < 0)
|
|
|
|
return size;
|
2011-01-15 01:00:00 +02:00
|
|
|
|
2012-10-03 08:54:45 +03:00
|
|
|
buf = av_malloc(size);
|
2011-01-15 01:00:00 +02:00
|
|
|
if (!buf)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2011-11-12 23:43:43 +03:00
|
|
|
size = av_samples_fill_arrays(audio_data, linesize, buf, nb_channels,
|
|
|
|
nb_samples, sample_fmt, align);
|
|
|
|
if (size < 0) {
|
|
|
|
av_free(buf);
|
|
|
|
return size;
|
|
|
|
}
|
2012-10-03 08:54:45 +03:00
|
|
|
|
|
|
|
av_samples_set_silence(audio_data, 0, nb_samples, nb_channels, sample_fmt);
|
|
|
|
|
2012-11-25 22:23:32 +03:00
|
|
|
#if FF_API_SAMPLES_UTILS_RETURN_ZERO
|
2011-11-12 23:43:43 +03:00
|
|
|
return 0;
|
2012-11-25 22:23:32 +03:00
|
|
|
#else
|
|
|
|
return size;
|
|
|
|
#endif
|
2011-01-15 01:00:00 +02:00
|
|
|
}
|
2012-05-06 15:10:38 +03:00
|
|
|
|
2013-03-21 13:48:28 +03:00
|
|
|
int av_samples_alloc_array_and_samples(uint8_t ***audio_data, int *linesize, int nb_channels,
|
|
|
|
int nb_samples, enum AVSampleFormat sample_fmt, int align)
|
|
|
|
{
|
|
|
|
int ret, nb_planes = av_sample_fmt_is_planar(sample_fmt) ? nb_channels : 1;
|
|
|
|
|
2013-04-20 23:55:47 +03:00
|
|
|
*audio_data = av_calloc(nb_planes, sizeof(**audio_data));
|
2013-03-21 13:48:28 +03:00
|
|
|
if (!*audio_data)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = av_samples_alloc(*audio_data, linesize, nb_channels,
|
|
|
|
nb_samples, sample_fmt, align);
|
|
|
|
if (ret < 0)
|
|
|
|
av_freep(audio_data);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-06 15:10:38 +03:00
|
|
|
int av_samples_copy(uint8_t **dst, uint8_t * const *src, int dst_offset,
|
|
|
|
int src_offset, int nb_samples, int nb_channels,
|
|
|
|
enum AVSampleFormat sample_fmt)
|
|
|
|
{
|
|
|
|
int planar = av_sample_fmt_is_planar(sample_fmt);
|
|
|
|
int planes = planar ? nb_channels : 1;
|
|
|
|
int block_align = av_get_bytes_per_sample(sample_fmt) * (planar ? 1 : nb_channels);
|
|
|
|
int data_size = nb_samples * block_align;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dst_offset *= block_align;
|
|
|
|
src_offset *= block_align;
|
|
|
|
|
2012-07-12 22:36:22 +03:00
|
|
|
if((dst[0] < src[0] ? src[0] - dst[0] : dst[0] - src[0]) >= data_size) {
|
|
|
|
for (i = 0; i < planes; i++)
|
|
|
|
memcpy(dst[i] + dst_offset, src[i] + src_offset, data_size);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < planes; i++)
|
|
|
|
memmove(dst[i] + dst_offset, src[i] + src_offset, data_size);
|
|
|
|
}
|
2012-05-06 15:10:38 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-05-08 14:56:37 +03:00
|
|
|
|
|
|
|
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples,
|
|
|
|
int nb_channels, enum AVSampleFormat sample_fmt)
|
|
|
|
{
|
|
|
|
int planar = av_sample_fmt_is_planar(sample_fmt);
|
|
|
|
int planes = planar ? nb_channels : 1;
|
|
|
|
int block_align = av_get_bytes_per_sample(sample_fmt) * (planar ? 1 : nb_channels);
|
|
|
|
int data_size = nb_samples * block_align;
|
|
|
|
int fill_char = (sample_fmt == AV_SAMPLE_FMT_U8 ||
|
|
|
|
sample_fmt == AV_SAMPLE_FMT_U8P) ? 0x80 : 0x00;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
offset *= block_align;
|
|
|
|
|
|
|
|
for (i = 0; i < planes; i++)
|
|
|
|
memset(audio_data[i] + offset, fill_char, data_size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|