[FFmpeg-devel] [RFC] libavfilter audio API and related issues
Michael Niedermayer
michaelni
Fri Jul 2 20:35:06 CEST 2010
On Thu, Jul 01, 2010 at 01:51:36AM -0700, S.N. Hemanth Meenakshisundaram wrote:
> On 07/01/2010 01:42 AM, S.N. Hemanth Meenakshisundaram wrote:
>> On 06/25/2010 05:10 PM, Stefano Sabatini wrote:
>>> On date Friday 2010-06-25 03:52:45 -0700, S.N. Hemanth Meenakshisundaram
>>> encoded:
>>>> [...]
>>>
>>
>> [...]
>
> libavutil changes - some common functions moved from lavc to avoid lavfi
> dependence on lavc.
>
> Also attaching the definitions removed from lavc.
> Makefile | 3 +
> audiodesc.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> audiodesc.h | 87 +++++++++++++++++++++++++++++++
> audiofmt.h | 94 +++++++++++++++++++++++++++++++++
> avutil.h | 1
> 5 files changed, 353 insertions(+)
> 760d28b98717e4a1a7715d899aff16bbae83b4f2 libavutil_audio.diff
> diff --git a/libavutil/Makefile b/libavutil/Makefile
> index f6961ac..5a770a3 100644
> --- a/libavutil/Makefile
> +++ b/libavutil/Makefile
> @@ -4,6 +4,8 @@ NAME = avutil
>
> HEADERS = adler32.h \
> attributes.h \
> + audiodesc.h \
> + audiofmt.h \
> avstring.h \
> avutil.h \
> base64.h \
> @@ -28,6 +30,7 @@ BUILT_HEADERS = avconfig.h
>
> OBJS = adler32.o \
> aes.o \
> + audiodesc.o \
> avstring.o \
> base64.o \
> crc.o \
> diff --git a/libavutil/audiodesc.c b/libavutil/audiodesc.c
> new file mode 100644
> index 0000000..fbc613b
> --- /dev/null
> +++ b/libavutil/audiodesc.c
> @@ -0,0 +1,168 @@
> +/*
> + * audio utilities
> + * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram <smeenaks at ucsd.edu>
> + * based on audioconvert.c in libavcodec by Michael Niedermayer
> + *
> + * 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
> + */
> +
> +/**
> + * @file
> + * audio sample format and channel layout utilities
> + * @author Michael Niedermayer <michaelni at gmx.at>
> + */
> +
> +#include "avstring.h"
> +#include "libm.h"
> +#include "avutil.h"
> +#include "audiodesc.h"
> +
> +typedef struct SampleFmtInfo {
> + const char *name;
> + int bits;
> +} SampleFmtInfo;
> +
> +/** this table gives more information about formats */
> +static const SampleFmtInfo sample_fmt_info[SAMPLE_FMT_NB] = {
> + [SAMPLE_FMT_U8] = { .name = "u8", .bits = 8 },
> + [SAMPLE_FMT_S16] = { .name = "s16", .bits = 16 },
> + [SAMPLE_FMT_S32] = { .name = "s32", .bits = 32 },
> + [SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32 },
> + [SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64 },
> +};
> +
> +const char *av_get_sample_fmt_name(int sample_fmt)
> +{
> + if (sample_fmt < 0 || sample_fmt >= SAMPLE_FMT_NB)
make sample_fmt unsigned and you need just one check
> + return NULL;
> + return sample_fmt_info[sample_fmt].name;
> +}
> +
> +enum SampleFormat av_get_sample_fmt(const char* name)
> +{
> + int i;
> +
> + for (i=0; i < SAMPLE_FMT_NB; i++)
> + if (!strcmp(sample_fmt_info[i].name, name))
> + return i;
> + return SAMPLE_FMT_NONE;
> +}
> +
> +void av_sample_fmt_string (char *buf, int buf_size, int sample_fmt)
> +{
> + /* print header */
> + if (sample_fmt < 0)
> + snprintf (buf, buf_size, "name " " depth");
> + else if (sample_fmt < SAMPLE_FMT_NB) {
> + SampleFmtInfo info= sample_fmt_info[sample_fmt];
> + snprintf (buf, buf_size, "%-6s" " %2d ", info.name, info.bits);
> + }
> +}
> +
> +int av_get_bits_per_sample_fmt(enum SampleFormat sample_fmt) {
> + if (sample_fmt < 0 || sample_fmt >= SAMPLE_FMT_NB)
> + return -1;
> + return sample_fmt_info[sample_fmt].bits;
> +}
is there a reason not to access the table directly?
this large number of accessor functions are a bit ugly in a lib that really
isnt specific to audio
> +
> +static const char* const channel_names[]={
> + "FL", "FR", "FC", "LFE", "BL", "BR", "FLC", "FRC",
> + "BC", "SL", "SR", "TC", "TFL", "TFC", "TFR", "TBL",
> + "TBC", "TBR",
> + [29] = "DL",
> + [30] = "DR",
> +};
> +
> +static const char *get_channel_name(int channel_id)
> +{
> + if (channel_id<0 || channel_id>=FF_ARRAY_ELEMS(channel_names))
> + return NULL;
> + return channel_names[channel_id];
> +}
just as an internal function this seems useless, direct table access
make more sense
> +
> +int64_t av_guess_channel_layout(int nb_channels, const char *fmt_name)
> +{
> + switch(nb_channels) {
> + case 1: return CH_LAYOUT_MONO;
> + case 2: return CH_LAYOUT_STEREO;
> + case 3: return CH_LAYOUT_SURROUND;
> + case 4: return CH_LAYOUT_QUAD;
> + case 5: return CH_LAYOUT_5POINT0;
> + case 6: return CH_LAYOUT_5POINT1;
> + case 8: return CH_LAYOUT_7POINT1;
> + default: return 0;
> + }
> +}
> +
> +static const struct {
> + const char *name;
> + int nb_channels;
> + int64_t layout;
> +} channel_layout_map[] = {
> + { "mono", 1, CH_LAYOUT_MONO },
> + { "stereo", 2, CH_LAYOUT_STEREO },
> + { "4.0", 4, CH_LAYOUT_4POINT0 },
> + { "quad", 4, CH_LAYOUT_QUAD },
> + { "5.0", 5, CH_LAYOUT_5POINT0 },
> + { "5.0", 5, CH_LAYOUT_5POINT0_BACK },
> + { "5.1", 6, CH_LAYOUT_5POINT1 },
> + { "5.1", 6, CH_LAYOUT_5POINT1_BACK },
> + { "5.1+downmix", 8, CH_LAYOUT_5POINT1|CH_LAYOUT_STEREO_DOWNMIX, },
> + { "7.1", 8, CH_LAYOUT_7POINT1 },
> + { "7.1(wide)", 8, CH_LAYOUT_7POINT1_WIDE },
> + { "7.1+downmix", 10, CH_LAYOUT_7POINT1|CH_LAYOUT_STEREO_DOWNMIX, },
> + { 0 }
> +};
av_guess_channel_layout() can be implemented by picking the first match
from channel_layout_map
> +
> +void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, int64_t channel_layout)
> +{
> + int i;
> +
> + for (i=0; channel_layout_map[i].name; i++)
> + if (nb_channels == channel_layout_map[i].nb_channels &&
> + channel_layout == channel_layout_map[i].layout) {
> + av_strlcpy(buf, channel_layout_map[i].name, buf_size);
> + return;
> + }
> +
> + snprintf(buf, buf_size, "%d channels", nb_channels);
> + if (channel_layout) {
> + int i,ch;
> + av_strlcat(buf, " (", buf_size);
> + for(i=0,ch=0; i<64; i++) {
> + if ((channel_layout & (1L<<i))) {
> + const char *name = get_channel_name(i);
> + if (name) {
> + if (ch>0) av_strlcat(buf, "|", buf_size);
> + av_strlcat(buf, name, buf_size);
> + }
> + ch++;
> + }
> + }
> + av_strlcat(buf, ")", buf_size);
> + }
> +}
> +
> +int av_channel_layout_num_channels(int64_t channel_layout)
> +{
> + int count;
> + uint64_t x = channel_layout;
> + for (count = 0; x; count++)
> + x &= x-1; // unset lowest set bit
> + return count;
> +}
thats a count of set bits aka population count aka hamming weight
if we dont have a function for this yet it makes sense to add one with
appropriate name. Theres no point though to have a function specific
to audio channels.
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Opposition brings concord. Out of discord comes the fairest harmony.
-- Heraclitus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20100702/75740357/attachment.pgp>
More information about the ffmpeg-devel
mailing list