[FFmpeg-devel] [PATCH] AST muxer
Paul B Mahol
onemda at gmail.com
Sat Nov 24 10:03:28 CET 2012
On 11/23/12, jamal <jamrial at gmail.com> wrote:
> Subject: [PATCH 2/2] AST Muxer
>
> ---
> Changelog | 2 +-
> doc/general.texi | 2 +-
> libavformat/Makefile | 3 +-
> libavformat/allformats.c | 2 +-
> libavformat/ast.c | 30 +++++++
> libavformat/ast.h | 30 +++++++
> libavformat/astdec.c | 15 ++--
> libavformat/astenc.c | 202 +++++++++++++++++++++++++++++++++++++++++++++++
> libavformat/version.h | 2 +-
> tests/fate/avformat.mak | 1 +
> tests/lavf-regression.sh | 4 +
> tests/ref/lavf/ast | 3 +
> 12 files changed, 281 insertions(+), 15 deletions(-)
> create mode 100644 libavformat/ast.c
> create mode 100644 libavformat/ast.h
> create mode 100644 libavformat/astenc.c
> create mode 100644 tests/ref/lavf/ast
>
> diff --git a/Changelog b/Changelog
> index bca5568..d2e7d5e 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -24,7 +24,7 @@ version <next>:
> - AVR demuxer
> - geq filter ported from libmpcodecs
> - remove ffserver daemon mode
> -- AST demuxer
> +- AST muxer/demuxer
> - new expansion syntax for drawtext
> - BRender PIX image decoder
> - ffprobe -show_entries option
> diff --git a/doc/general.texi b/doc/general.texi
> index 4d145a7..2c9aaf7 100644
> --- a/doc/general.texi
> +++ b/doc/general.texi
> @@ -148,7 +148,7 @@ library:
> @item Apple HTTP Live Streaming @tab @tab X
> @item Artworx Data Format @tab @tab X
> @item ASF @tab X @tab X
> - at item AST @tab @tab X
> + at item AST @tab X @tab X
> @tab Used on the Nintendo Wii.
> @item AVI @tab X @tab X
> @item AVISynth @tab @tab X
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index fc00811..2f83e25 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -50,7 +50,8 @@ OBJS-$(CONFIG_ASF_DEMUXER) += asfdec.o asf.o asfcrypt.o \
> OBJS-$(CONFIG_ASF_MUXER) += asfenc.o asf.o
> OBJS-$(CONFIG_ASS_DEMUXER) += assdec.o
> OBJS-$(CONFIG_ASS_MUXER) += assenc.o
> -OBJS-$(CONFIG_AST_DEMUXER) += astdec.o
> +OBJS-$(CONFIG_AST_DEMUXER) += ast.o astdec.o
> +OBJS-$(CONFIG_AST_MUXER) += ast.o astenc.o
> OBJS-$(CONFIG_AU_DEMUXER) += au.o pcm.o
> OBJS-$(CONFIG_AU_MUXER) += au.o
> OBJS-$(CONFIG_AVI_DEMUXER) += avidec.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index eaeb51a..b73eeae 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -64,7 +64,7 @@ void av_register_all(void)
> REGISTER_DEMUXER (APE, ape);
> REGISTER_MUXDEMUX (ASF, asf);
> REGISTER_MUXDEMUX (ASS, ass);
> - REGISTER_DEMUXER (AST, ast);
> + REGISTER_MUXDEMUX (AST, ast);
> REGISTER_MUXER (ASF_STREAM, asf_stream);
> REGISTER_MUXDEMUX (AU, au);
> REGISTER_MUXDEMUX (AVI, avi);
> diff --git a/libavformat/ast.c b/libavformat/ast.c
> new file mode 100644
> index 0000000..ddb6e2c
> --- /dev/null
> +++ b/libavformat/ast.c
> @@ -0,0 +1,30 @@
> +/*
> + * AST common code
> + * Copyright (c) 2012 James Almer
> + *
> + * 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
> + */
> +
> +#include "avformat.h"
> +#include "internal.h"
> +#include "ast.h"
> +
> +const AVCodecTag ff_codec_ast_tags[] = {
> + { AV_CODEC_ID_ADPCM_AFC, 0 },
> + { AV_CODEC_ID_PCM_S16BE_PLANAR, 1 },
> + { AV_CODEC_ID_NONE, 0 },
Better use some other value for NONE.
> +};
> diff --git a/libavformat/ast.h b/libavformat/ast.h
> new file mode 100644
> index 0000000..4a399ea
> --- /dev/null
> +++ b/libavformat/ast.h
> @@ -0,0 +1,30 @@
> +/*
> + * AST common code
> + * Copyright (c) 2012 James Almer
> + *
> + * 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
> + */
> +
> +#ifndef AVFORMAT_AST_H
> +#define AVFORMAT_AST_H
> +
> +#include "avformat.h"
> +#include "internal.h"
> +
> +extern const AVCodecTag ff_codec_ast_tags[];
> +
> +#endif /* AVFORMAT_AST_H */
> diff --git a/libavformat/astdec.c b/libavformat/astdec.c
> index 4f83540..4d74d4c 100644
> --- a/libavformat/astdec.c
> +++ b/libavformat/astdec.c
> @@ -23,6 +23,8 @@
> #include "libavutil/intreadwrite.h"
> #include "avformat.h"
> #include "internal.h"
> +#include "riff.h"
> +#include "ast.h"
>
> static int ast_probe(AVProbeData *p)
> {
> @@ -42,16 +44,6 @@ static int ast_read_header(AVFormatContext *s)
>
> avio_skip(s->pb, 8);
> codec = avio_rb16(s->pb);
> - switch (codec) {
> - case 0:
> - st->codec->codec_id = AV_CODEC_ID_ADPCM_AFC;
> - break;
> - case 1:
> - st->codec->codec_id = AV_CODEC_ID_PCM_S16BE_PLANAR;
> - break;
> - default:
> - av_log(s, AV_LOG_ERROR, "unsupported codec %d\n", codec);
> - }
>
> depth = avio_rb16(s->pb);
> if (depth != 16) {
> @@ -60,6 +52,8 @@ static int ast_read_header(AVFormatContext *s)
> }
>
> st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
> + st->codec->codec_id = ff_codec_get_id(ff_codec_ast_tags, codec);
> + st->codec->codec_tag = codec;
Do not set this.
> st->codec->channels = avio_rb16(s->pb);
> if (!st->codec->channels)
> return AVERROR_INVALIDDATA;
> @@ -121,4 +115,5 @@ AVInputFormat ff_ast_demuxer = {
> .read_packet = ast_read_packet,
> .extensions = "ast",
> .flags = AVFMT_GENERIC_INDEX,
> + .codec_tag = (const AVCodecTag* const []){ff_codec_ast_tags, 0},
> };
> diff --git a/libavformat/astenc.c b/libavformat/astenc.c
> new file mode 100644
> index 0000000..0fa45c2
> --- /dev/null
> +++ b/libavformat/astenc.c
> @@ -0,0 +1,202 @@
> +/*
> + * AST muxer
> + * Copyright (c) 2012 James Almer
> + *
> + * 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
> + */
> +
> +#include "avformat.h"
> +#include "avio_internal.h"
> +#include "internal.h"
> +#include "ast.h"
> +#include "libavutil/mathematics.h"
> +#include "libavutil/opt.h"
> +
> +typedef struct ASTMuxContext {
> + AVClass *class;
> + int64_t size;
> + int64_t samples;
> + int64_t loopstart;
> + int64_t loopend;
> + int fbs;
> +} ASTMuxContext;
> +
> +#define CHECK_LOOP(type) \
> + if(ast->loop ## type) { \
> + ast->loop ## type = av_rescale_q_rnd(ast->loop ## type, (AVRational){enc->sample_rate, 1}, (AVRational){1000, 1}, AV_ROUND_DOWN); \
> + if(ast->loop ## type < 0 || ast->loop ## type > UINT_MAX) { \
> + av_log(s, AV_LOG_ERROR, "Invalid loop" #type " value\n"); \
> + return AVERROR(EINVAL); \
> + } \
> + }
> +
> +static int ast_write_header(AVFormatContext *s)
> +{
> + ASTMuxContext *ast = s->priv_data;
> + AVIOContext *pb = s->pb;
> + AVCodecContext *enc = NULL;
> +
> + if(s->nb_streams = 1) {
> + enc = s->streams[0]->codec;
> + } else {
> + av_log(s, AV_LOG_ERROR, "Muxer supports only one stream\n");
> + return AVERROR_INVALIDDATA;
> + }
> +
> + if(!enc->codec_tag && enc->codec_id != AV_CODEC_ID_ADPCM_AFC) {
This can be simplified by returning 0xFFFF codec tag or similar.
> + av_log(s, AV_LOG_ERROR, "Unsupported codec\n");
> + return AVERROR_INVALIDDATA;
> + }
> +
> + if(ast->loopstart && ast->loopend && ast->loopstart >= ast->loopend) {
> + av_log(s, AV_LOG_ERROR, "Loopend can't be less or equal to loopstart\n");
> + return AVERROR(EINVAL);
> + }
> +
> + /* Convert milliseconds to samples */
> + CHECK_LOOP(start)
> + CHECK_LOOP(end)
> +
> + ffio_wfourcc(pb, "STRM");
> +
> + ast->size = avio_tell(pb);
> + avio_wb32(pb, 0); /* File size minus header */
> + avio_wb16(pb, enc->codec_tag);
> + avio_wb16(pb, 16); /* Bit depth */
> + avio_wb16(pb, enc->channels);
> + avio_wb16(pb, 0xFFFF);
> + avio_wb32(pb, enc->sample_rate);
> +
> + ast->samples = avio_tell(pb);
> + avio_wb32(pb, 0); /* Number of samples */
> + avio_wb32(pb, 0); /* Loopstart */
> + avio_wb32(pb, 0); /* Loopend */
> + avio_wb32(pb, 0); /* Size of first block */
> +
> + /* Unknown */
> + avio_wb32(pb, 0);
> + avio_wl32(pb, 0x7F);
> + avio_wb64(pb, 0);
> + avio_wb64(pb, 0);
> + avio_wb32(pb, 0);
> +
> + avio_flush(pb);
> +
> + return 0;
> +}
> +
> +static int ast_write_packet(AVFormatContext *s, AVPacket *pkt)
> +{
> + AVIOContext *pb = s->pb;
> + ASTMuxContext *ast = s->priv_data;
> + AVCodecContext *enc = s->streams[0]->codec;
> + int size = pkt->size / enc->channels;
> +
> + if(enc->frame_number == 1)
> + ast->fbs = size;
> +
> + ffio_wfourcc(pb, "BLCK");
> + avio_wb32(pb, size); /* Block size */
> +
> + /* padding */
> + avio_wb64(pb, 0);
> + avio_wb64(pb, 0);
> + avio_wb64(pb, 0);
> +
> + avio_write(pb, pkt->data, pkt->size);
> +
> + return 0;
> +}
> +
> +static int ast_write_trailer(AVFormatContext *s)
> +{
> + AVIOContext *pb = s->pb;
> + ASTMuxContext *ast = s->priv_data;
> + AVCodecContext *enc = s->streams[0]->codec;
> + int64_t samples, file_size = avio_tell(pb);
> +
> + if(enc->codec_tag)
> + samples = (file_size - 64 - (32 * enc->frame_number)) / enc->block_align;
> + else
> + samples = enc->frame_number * ((ast->fbs * 2) / (9 * enc->channels) * 16);
> +
> + av_log(s, AV_LOG_DEBUG, "total samples: %"PRId64"\n", samples);
> +
> + if (s->pb->seekable) {
> + /* File size minus header */
> + avio_seek(pb, ast->size, SEEK_SET);
> + avio_wb32(pb, file_size - 64);
> +
> + /* Number of samples */
> + avio_seek(pb, ast->samples, SEEK_SET);
> + avio_wb32(pb, samples);
> +
> + /* Loopstart if provided */
> + if(ast->loopstart && ast->loopstart >= samples) {
> + av_log(s, AV_LOG_WARNING, "Loopstart value is out of range and will be ignored\n");
> + ast->loopstart = 0;
> + }
> + avio_wb32(pb, ast->loopstart);
> +
> + /* Loopend if provided. Otherwise number of samples again */
> + if(ast->loopend) {
> + if(ast->loopend > samples) {
> + av_log(s, AV_LOG_WARNING, "Loopend value is out of range and will be ignored\n");
> + ast->loopend = samples;
> + }
> + avio_wb32(pb, ast->loopend);
> + } else {
> + avio_wb32(pb, samples);
> + }
> +
> + /* Size of first block */
> + avio_seek(pb, ast->samples + 12, SEEK_SET);
> + avio_wb32(pb, ast->fbs);
> +
> + avio_seek(pb, file_size, SEEK_END);
> + avio_flush(pb);
> + }
> + return 0;
> +}
> +
> +#define OFFSET(obj) offsetof(ASTMuxContext, obj)
> +static const AVOption options[] = {
> + { "loopstart", "Loopstart position in milliseconds.", OFFSET(loopstart), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
> + { "loopend", "Loopend position in milliseconds.", OFFSET(loopend), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
> + { NULL },
> +};
> +
> +static const AVClass ast_muxer_class = {
> + .class_name = "AST muxer",
> + .item_name = av_default_item_name,
> + .option = options,
> + .version = LIBAVUTIL_VERSION_INT,
> +};
> +
> +AVOutputFormat ff_ast_muxer = {
> + .name = "ast",
> + .long_name = NULL_IF_CONFIG_SMALL("AST (Audio Stream)"),
> + .extensions = "ast",
> + .priv_data_size = sizeof(ASTMuxContext),
> + .audio_codec = AV_CODEC_ID_PCM_S16BE_PLANAR,
> + .video_codec = AV_CODEC_ID_NONE,
> + .write_header = ast_write_header,
> + .write_packet = ast_write_packet,
> + .write_trailer = ast_write_trailer,
> + .priv_class = &ast_muxer_class,
> + .codec_tag = (const AVCodecTag* const []){ff_codec_ast_tags, 0},
> +};
> diff --git a/libavformat/version.h b/libavformat/version.h
> index 02ebb8c..10391b3 100644
> --- a/libavformat/version.h
> +++ b/libavformat/version.h
> @@ -30,7 +30,7 @@
> #include "libavutil/avutil.h"
>
> #define LIBAVFORMAT_VERSION_MAJOR 54
> -#define LIBAVFORMAT_VERSION_MINOR 37
> +#define LIBAVFORMAT_VERSION_MINOR 38
> #define LIBAVFORMAT_VERSION_MICRO 100
>
> #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
> diff --git a/tests/fate/avformat.mak b/tests/fate/avformat.mak
> index 77c6a2f..4c99707 100644
> --- a/tests/fate/avformat.mak
> +++ b/tests/fate/avformat.mak
> @@ -1,6 +1,7 @@
> FATE_LAVF-$(call ENCDEC, PCM_S16BE, AIFF) += aiff
> FATE_LAVF-$(call ENCDEC, PCM_ALAW, PCM_ALAW) += alaw
> FATE_LAVF-$(call ENCDEC2, MSMPEG4V3, MP2, ASF) += asf
> +FATE_LAVF-$(call ENCDEC, PCM_S16BE_PLANAR, AST) += ast
> FATE_LAVF-$(call ENCDEC, PCM_S16BE, AU) += au
> FATE_LAVF-$(call ENCDEC2, MPEG4, MP2, AVI) += avi
> FATE_LAVF-$(call ENCDEC, BMP, IMAGE2) += bmp
> diff --git a/tests/lavf-regression.sh b/tests/lavf-regression.sh
> index 64ebc0a..473c235 100755
> --- a/tests/lavf-regression.sh
> +++ b/tests/lavf-regression.sh
> @@ -326,6 +326,10 @@ if [ -n "$do_caf" ] ; then
> do_audio_only caf
> fi
>
> +if [ -n "$do_ast" ] ; then
> +do_audio_only ast "-ac 2" "-loopstart 1 -loopend 10"
> +fi
> +
> # pix_fmt conversions
>
> if [ -n "$do_pixfmt" ] ; then
> diff --git a/tests/ref/lavf/ast b/tests/ref/lavf/ast
> new file mode 100644
> index 0000000..72a9824
> --- /dev/null
> +++ b/tests/ref/lavf/ast
> @@ -0,0 +1,3 @@
> +7fa8cd2dd7453428e71930a7c65f7b62 *./tests/data/lavf/lavf.ast
> +181696 ./tests/data/lavf/lavf.ast
> +./tests/data/lavf/lavf.ast CRC=0x7bd585ff
> --
> 1.8.0.msysgit.0
>
More information about the ffmpeg-devel
mailing list