From 9c6538d4c2d045cc55674adde7a758fd24e7b79e Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Tue, 19 Feb 2019 20:30:55 +0100 Subject: [PATCH] avformat: add raw AC-4 demuxer --- libavformat/Makefile | 1 + libavformat/ac4dec.c | 104 +++++++++++++++++++++++++++++++++++++++ libavformat/allformats.c | 1 + libavformat/version.h | 2 +- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 libavformat/ac4dec.c diff --git a/libavformat/Makefile b/libavformat/Makefile index d3503196e3..2c037c0b24 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -77,6 +77,7 @@ OBJS-$(CONFIG_AAC_DEMUXER) += aacdec.o apetag.o img2.o rawdec.o OBJS-$(CONFIG_AAX_DEMUXER) += aaxdec.o OBJS-$(CONFIG_AC3_DEMUXER) += ac3dec.o rawdec.o OBJS-$(CONFIG_AC3_MUXER) += rawenc.o +OBJS-$(CONFIG_AC4_DEMUXER) += ac4dec.o OBJS-$(CONFIG_ACE_DEMUXER) += acedec.o OBJS-$(CONFIG_ACM_DEMUXER) += acm.o rawdec.o OBJS-$(CONFIG_ACT_DEMUXER) += act.o diff --git a/libavformat/ac4dec.c b/libavformat/ac4dec.c new file mode 100644 index 0000000000..eb7bbaa655 --- /dev/null +++ b/libavformat/ac4dec.c @@ -0,0 +1,104 @@ +/* + * RAW AC-4 demuxer + * Copyright (c) 2019 Paul B Mahol + * + * 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 "libavutil/avassert.h" +#include "libavutil/crc.h" +#include "avformat.h" +#include "rawdec.h" + +static int ac4_probe(const AVProbeData *p) +{ + const uint8_t *buf = p->buf; + int left = p->buf_size; + int max_frames = 0; + + while (left > 7) { + int size; + + if (buf[0] == 0xAC && + (buf[1] == 0x40 || + buf[1] == 0x41)) { + size = (buf[2] << 8) | buf[3]; + if (size == 0xFFFF) + size = 3 + ((buf[4] << 16) | (buf[5] << 8) | buf[6]); + size += 4; + if (buf[1] == 0x41) + size += 2; + max_frames++; + left -= size; + buf += size; + } else { + break; + } + } + + return FFMIN(AVPROBE_SCORE_MAX, max_frames * 7); +} + +static int ac4_read_header(AVFormatContext *s) +{ + AVStream *st; + + st = avformat_new_stream(s, NULL); + if (!st) + return AVERROR(ENOMEM); + + st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; + st->codecpar->codec_id = AV_CODEC_ID_AC4; + + return 0; +} + +static int ac4_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + AVIOContext *pb = s->pb; + int64_t pos; + uint16_t sync; + int ret, size; + + if (avio_feof(s->pb)) + return AVERROR_EOF; + + pos = avio_tell(s->pb); + sync = avio_rb16(pb); + size = avio_rb16(pb); + if (size == 0xffff) + size = avio_rb24(pb); + + ret = av_get_packet(pb, pkt, size); + pkt->pos = pos; + pkt->stream_index = 0; + + if (sync == 0xAC41) + avio_skip(pb, 2); + + return ret; +} + +AVInputFormat ff_ac4_demuxer = { + .name = "ac4", + .long_name = NULL_IF_CONFIG_SMALL("raw AC-4"), + .read_probe = ac4_probe, + .read_header = ac4_read_header, + .read_packet = ac4_read_packet, + .flags = AVFMT_GENERIC_INDEX, + .extensions = "ac4", +}; diff --git a/libavformat/allformats.c b/libavformat/allformats.c index ae604236ae..70fa7f1460 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -34,6 +34,7 @@ extern const AVInputFormat ff_aac_demuxer; extern const AVInputFormat ff_aax_demuxer; extern const AVInputFormat ff_ac3_demuxer; extern const FFOutputFormat ff_ac3_muxer; +extern const AVInputFormat ff_ac4_demuxer; extern const AVInputFormat ff_ace_demuxer; extern const AVInputFormat ff_acm_demuxer; extern const AVInputFormat ff_act_demuxer; diff --git a/libavformat/version.h b/libavformat/version.h index 70c554c19c..35d796d318 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -31,7 +31,7 @@ #include "version_major.h" -#define LIBAVFORMAT_VERSION_MINOR 7 +#define LIBAVFORMAT_VERSION_MINOR 8 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \