2010-08-31 00:17:34 +03:00
|
|
|
/*
|
|
|
|
* RAW PCM demuxers
|
|
|
|
* Copyright (c) 2002 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2022-02-23 14:56:49 +02:00
|
|
|
#include "config_components.h"
|
|
|
|
|
2021-02-24 03:45:58 +02:00
|
|
|
#include "libavutil/avstring.h"
|
2022-03-18 20:24:58 +02:00
|
|
|
#include "libavutil/channel_layout.h"
|
2010-08-31 00:17:34 +03:00
|
|
|
#include "avformat.h"
|
2012-10-24 20:15:50 +03:00
|
|
|
#include "internal.h"
|
2010-08-31 00:17:34 +03:00
|
|
|
#include "pcm.h"
|
2011-09-14 10:54:29 +03:00
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "libavutil/opt.h"
|
2012-06-15 00:52:23 +03:00
|
|
|
#include "libavutil/avassert.h"
|
2010-08-31 00:17:34 +03:00
|
|
|
|
2001-01-01 02:08:20 +02:00
|
|
|
typedef struct PCMAudioDemuxerContext {
|
2012-10-24 20:15:50 +03:00
|
|
|
AVClass *class;
|
|
|
|
int sample_rate;
|
2022-03-20 20:12:59 +02:00
|
|
|
#if FF_API_OLD_CHANNEL_LAYOUT
|
2012-10-24 20:15:50 +03:00
|
|
|
int channels;
|
2022-03-20 20:12:59 +02:00
|
|
|
#endif
|
2022-03-18 20:24:58 +02:00
|
|
|
AVChannelLayout ch_layout;
|
2001-01-01 02:08:20 +02:00
|
|
|
} PCMAudioDemuxerContext;
|
2012-10-24 20:15:50 +03:00
|
|
|
|
2001-01-01 02:08:20 +02:00
|
|
|
static int pcm_read_header(AVFormatContext *s)
|
2012-10-24 20:15:50 +03:00
|
|
|
{
|
2001-01-01 02:08:20 +02:00
|
|
|
PCMAudioDemuxerContext *s1 = s->priv_data;
|
2021-02-24 04:14:36 +02:00
|
|
|
AVCodecParameters *par;
|
2012-10-24 20:15:50 +03:00
|
|
|
AVStream *st;
|
2016-08-17 18:07:24 +02:00
|
|
|
uint8_t *mime_type = NULL;
|
2022-03-20 20:12:59 +02:00
|
|
|
int ret;
|
2012-10-24 20:15:50 +03:00
|
|
|
|
|
|
|
st = avformat_new_stream(s, NULL);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
2021-02-24 04:14:36 +02:00
|
|
|
par = st->codecpar;
|
2012-10-24 20:15:50 +03:00
|
|
|
|
2021-02-24 04:14:36 +02:00
|
|
|
par->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
par->codec_id = s->iformat->raw_codec_id;
|
|
|
|
par->sample_rate = s1->sample_rate;
|
2022-03-20 20:12:59 +02:00
|
|
|
#if FF_API_OLD_CHANNEL_LAYOUT
|
|
|
|
if (s1->ch_layout.nb_channels) {
|
|
|
|
#endif
|
|
|
|
ret = av_channel_layout_copy(&par->ch_layout, &s1->ch_layout);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
#if FF_API_OLD_CHANNEL_LAYOUT
|
|
|
|
} else
|
2022-03-18 20:24:58 +02:00
|
|
|
par->ch_layout.nb_channels = s1->channels;
|
2022-03-20 20:12:59 +02:00
|
|
|
#endif
|
2012-10-24 20:15:50 +03:00
|
|
|
|
2016-08-17 18:07:24 +02:00
|
|
|
av_opt_get(s->pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type);
|
|
|
|
if (mime_type && s->iformat->mime_type) {
|
2018-11-22 09:54:42 +02:00
|
|
|
int rate = 0, channels = 0, little_endian = 0;
|
2021-02-24 03:45:58 +02:00
|
|
|
const char *options;
|
|
|
|
if (av_stristart(mime_type, s->iformat->mime_type, &options)) { /* audio/L16 */
|
|
|
|
while (options = strchr(options, ';')) {
|
2021-02-24 03:08:32 +02:00
|
|
|
options++;
|
2016-08-17 18:07:24 +02:00
|
|
|
if (!rate)
|
|
|
|
sscanf(options, " rate=%d", &rate);
|
|
|
|
if (!channels)
|
|
|
|
sscanf(options, " channels=%d", &channels);
|
2018-11-22 09:54:42 +02:00
|
|
|
if (!little_endian) {
|
2021-02-24 04:14:36 +02:00
|
|
|
char val[sizeof("little-endian")];
|
2018-11-22 09:54:42 +02:00
|
|
|
if (sscanf(options, " endianness=%13s", val) == 1) {
|
|
|
|
little_endian = strcmp(val, "little-endian") == 0;
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 18:07:24 +02:00
|
|
|
}
|
|
|
|
if (rate <= 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Invalid sample_rate found in mime_type \"%s\"\n",
|
|
|
|
mime_type);
|
2017-06-11 15:19:17 +02:00
|
|
|
av_freep(&mime_type);
|
2016-08-17 18:07:24 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2021-02-24 04:14:36 +02:00
|
|
|
par->sample_rate = rate;
|
2022-03-18 20:24:58 +02:00
|
|
|
if (channels > 0) {
|
|
|
|
av_channel_layout_uninit(&par->ch_layout);
|
2017-03-31 18:06:05 +02:00
|
|
|
par->ch_layout.nb_channels = channels;
|
2022-03-18 20:24:58 +02:00
|
|
|
}
|
2018-11-22 09:54:42 +02:00
|
|
|
if (little_endian)
|
2021-02-24 04:14:36 +02:00
|
|
|
par->codec_id = AV_CODEC_ID_PCM_S16LE;
|
2016-08-17 18:07:24 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-11 15:19:17 +02:00
|
|
|
av_freep(&mime_type);
|
2016-08-17 18:07:24 +02:00
|
|
|
|
2021-02-24 04:14:36 +02:00
|
|
|
par->bits_per_coded_sample = av_get_bits_per_sample(par->codec_id);
|
2012-10-24 20:15:50 +03:00
|
|
|
|
2021-02-24 04:14:36 +02:00
|
|
|
av_assert0(par->bits_per_coded_sample > 0);
|
2012-10-24 20:15:50 +03:00
|
|
|
|
2017-03-31 18:06:05 +02:00
|
|
|
par->block_align = par->bits_per_coded_sample * par->ch_layout.nb_channels / 8;
|
2012-10-24 20:15:50 +03:00
|
|
|
|
2021-02-24 04:14:36 +02:00
|
|
|
avpriv_set_pts_info(st, 64, 1, par->sample_rate);
|
2012-10-24 20:15:50 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-14 10:54:29 +03:00
|
|
|
static const AVOption pcm_options[] = {
|
2012-10-26 15:55:15 +03:00
|
|
|
{ "sample_rate", "", offsetof(PCMAudioDemuxerContext, sample_rate), AV_OPT_TYPE_INT, {.i64 = 44100}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
|
2022-03-20 20:12:59 +02:00
|
|
|
#if FF_API_OLD_CHANNEL_LAYOUT
|
|
|
|
{ "channels", "", offsetof(PCMAudioDemuxerContext, channels), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_DEPRECATED },
|
|
|
|
{ "ch_layout", "", offsetof(PCMAudioDemuxerContext, ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
|
|
|
|
#else
|
2022-03-18 20:24:58 +02:00
|
|
|
{ "ch_layout", "", offsetof(PCMAudioDemuxerContext, ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = "mono"}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
|
2022-03-20 20:12:59 +02:00
|
|
|
#endif
|
2011-09-14 10:54:29 +03:00
|
|
|
{ NULL },
|
|
|
|
};
|
2021-06-07 16:13:33 +02:00
|
|
|
static const AVClass pcm_demuxer_class = {
|
|
|
|
.class_name = "pcm demuxer",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = pcm_options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
2011-09-14 10:54:29 +03:00
|
|
|
|
2021-02-24 02:02:30 +02:00
|
|
|
#define PCMDEF_0(name_, long_name_, ext, codec, ...)
|
|
|
|
#define PCMDEF_1(name_, long_name_, ext, codec, ...) \
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVInputFormat ff_pcm_ ## name_ ## _demuxer = { \
|
2011-09-23 21:50:11 +03:00
|
|
|
.name = #name_, \
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL(long_name_), \
|
2001-01-01 02:08:20 +02:00
|
|
|
.priv_data_size = sizeof(PCMAudioDemuxerContext), \
|
|
|
|
.read_header = pcm_read_header, \
|
2012-12-03 20:22:21 +03:00
|
|
|
.read_packet = ff_pcm_read_packet, \
|
2012-02-15 12:28:39 +03:00
|
|
|
.read_seek = ff_pcm_read_seek, \
|
2011-09-23 21:50:11 +03:00
|
|
|
.flags = AVFMT_GENERIC_INDEX, \
|
|
|
|
.extensions = ext, \
|
2012-01-31 09:50:31 +03:00
|
|
|
.raw_codec_id = codec, \
|
2021-06-07 16:13:33 +02:00
|
|
|
.priv_class = &pcm_demuxer_class, \
|
2016-08-18 09:45:25 +02:00
|
|
|
__VA_ARGS__ \
|
2010-08-31 00:17:34 +03:00
|
|
|
};
|
2021-02-24 02:02:30 +02:00
|
|
|
#define PCMDEF_2(name, long_name, ext, codec, enabled, ...) \
|
|
|
|
PCMDEF_ ## enabled(name, long_name, ext, codec, __VA_ARGS__)
|
|
|
|
#define PCMDEF_3(name, long_name, ext, codec, config, ...) \
|
|
|
|
PCMDEF_2(name, long_name, ext, codec, config, __VA_ARGS__)
|
|
|
|
#define PCMDEF_EXT(name, long_name, ext, uppercase, ...) \
|
|
|
|
PCMDEF_3(name, long_name, ext, AV_CODEC_ID_PCM_ ## uppercase, \
|
|
|
|
CONFIG_PCM_ ## uppercase ## _DEMUXER, __VA_ARGS__)
|
|
|
|
#define PCMDEF(name, long_name, ext, uppercase) \
|
|
|
|
PCMDEF_EXT(name, long_name, ext, uppercase, )
|
|
|
|
|
|
|
|
PCMDEF(f64be, "PCM 64-bit floating-point big-endian", NULL, F64BE)
|
|
|
|
PCMDEF(f64le, "PCM 64-bit floating-point little-endian", NULL, F64LE)
|
|
|
|
PCMDEF(f32be, "PCM 32-bit floating-point big-endian", NULL, F32BE)
|
|
|
|
PCMDEF(f32le, "PCM 32-bit floating-point little-endian", NULL, F32LE)
|
|
|
|
PCMDEF(s32be, "PCM signed 32-bit big-endian", NULL, S32BE)
|
|
|
|
PCMDEF(s32le, "PCM signed 32-bit little-endian", NULL, S32LE)
|
|
|
|
PCMDEF(s24be, "PCM signed 24-bit big-endian", NULL, S24BE)
|
|
|
|
PCMDEF(s24le, "PCM signed 24-bit little-endian", NULL, S24LE)
|
|
|
|
PCMDEF_EXT(s16be, "PCM signed 16-bit big-endian",
|
|
|
|
AV_NE("sw", NULL), S16BE, .mime_type = "audio/L16")
|
|
|
|
PCMDEF(s16le, "PCM signed 16-bit little-endian", AV_NE(NULL, "sw"), S16LE)
|
|
|
|
PCMDEF(s8, "PCM signed 8-bit", "sb", S8)
|
|
|
|
PCMDEF(u32be, "PCM unsigned 32-bit big-endian", NULL, U32BE)
|
|
|
|
PCMDEF(u32le, "PCM unsigned 32-bit little-endian", NULL, U32LE)
|
|
|
|
PCMDEF(u24be, "PCM unsigned 24-bit big-endian", NULL, U24BE)
|
|
|
|
PCMDEF(u24le, "PCM unsigned 24-bit little-endian", NULL, U24LE)
|
|
|
|
PCMDEF(u16be, "PCM unsigned 16-bit big-endian", AV_NE("uw", NULL), U16BE)
|
|
|
|
PCMDEF(u16le, "PCM unsigned 16-bit little-endian", AV_NE(NULL, "uw"), U16LE)
|
|
|
|
PCMDEF(u8, "PCM unsigned 8-bit", "ub", U8)
|
|
|
|
PCMDEF(alaw, "PCM A-law", "al", ALAW)
|
|
|
|
PCMDEF(mulaw, "PCM mu-law", "ul", MULAW)
|
|
|
|
PCMDEF(vidc, "PCM Archimedes VIDC", NULL, VIDC)
|
|
|
|
|
|
|
|
#if CONFIG_SLN_DEMUXER
|
2014-05-22 16:15:04 +03:00
|
|
|
static const AVOption sln_options[] = {
|
|
|
|
{ "sample_rate", "", offsetof(PCMAudioDemuxerContext, sample_rate), AV_OPT_TYPE_INT, {.i64 = 8000}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
|
2022-03-20 20:18:50 +02:00
|
|
|
#if FF_API_OLD_CHANNEL_LAYOUT
|
|
|
|
{ "channels", "", offsetof(PCMAudioDemuxerContext, channels), AV_OPT_TYPE_INT, {.i64 = 1}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_DEPRECATED },
|
|
|
|
{ "ch_layout", "", offsetof(PCMAudioDemuxerContext, ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = NULL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
|
|
|
|
#else
|
|
|
|
{ "ch_layout", "", offsetof(PCMAudioDemuxerContext, ch_layout), AV_OPT_TYPE_CHLAYOUT, {.str = "mono"}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
|
|
|
|
#endif
|
2014-05-22 16:15:04 +03:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass sln_demuxer_class = {
|
|
|
|
.class_name = "sln demuxer",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = sln_options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVInputFormat ff_sln_demuxer = {
|
2014-05-22 16:15:04 +03:00
|
|
|
.name = "sln",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Asterisk raw pcm"),
|
|
|
|
.priv_data_size = sizeof(PCMAudioDemuxerContext),
|
|
|
|
.read_header = pcm_read_header,
|
|
|
|
.read_packet = ff_pcm_read_packet,
|
|
|
|
.read_seek = ff_pcm_read_seek,
|
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
|
|
|
.extensions = "sln",
|
|
|
|
.raw_codec_id = AV_CODEC_ID_PCM_S16LE,
|
|
|
|
.priv_class = &sln_demuxer_class,
|
|
|
|
};
|
2021-02-24 02:02:30 +02:00
|
|
|
#endif
|