From 10d48c63b2a7e2f929eb9c56e3012dd2d7334925 Mon Sep 17 00:00:00 2001 From: Peter Ross Date: Fri, 29 Apr 2016 18:48:45 +0200 Subject: [PATCH] avformat: add Wideband Single-bit Data (WSD) demuxer Signed-off-by: Paul B Mahol --- Changelog | 1 + libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/version.h | 4 +- libavformat/wsddec.c | 172 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 libavformat/wsddec.c diff --git a/Changelog b/Changelog index 328ba46ac01..86b72852a71 100644 --- a/Changelog +++ b/Changelog @@ -29,6 +29,7 @@ version : - VAAPI-accelerated format conversion and scaling - libnpp/CUDA-accelerated format conversion and scaling - Duck TrueMotion 2.0 Real Time decoder +- Wideband Single-bit Data (WSD) demuxer version 3.0: - Common Encryption (CENC) MP4 encoding and decoding support diff --git a/libavformat/Makefile b/libavformat/Makefile index 51260f4a712..0f401766bd7 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -493,6 +493,7 @@ OBJS-$(CONFIG_WEBP_MUXER) += webpenc.o OBJS-$(CONFIG_WEBVTT_DEMUXER) += webvttdec.o subtitles.o OBJS-$(CONFIG_WEBVTT_MUXER) += webvttenc.o OBJS-$(CONFIG_WSAUD_DEMUXER) += westwood_aud.o +OBJS-$(CONFIG_WSD_DEMUXER) += wsddec.o rawdec.o OBJS-$(CONFIG_WSVQA_DEMUXER) += westwood_vqa.o OBJS-$(CONFIG_WTV_DEMUXER) += wtvdec.o wtv_common.o asf.o \ avlanguage.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index dbf2737b48b..e6ee8d67078 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -335,6 +335,7 @@ void av_register_all(void) REGISTER_MUXER (WEBP, webp); REGISTER_MUXDEMUX(WEBVTT, webvtt); REGISTER_DEMUXER (WSAUD, wsaud); + REGISTER_DEMUXER (WSD, wsd); REGISTER_DEMUXER (WSVQA, wsvqa); REGISTER_MUXDEMUX(WTV, wtv); REGISTER_DEMUXER (WVE, wve); diff --git a/libavformat/version.h b/libavformat/version.h index 5141b5b16f6..6a3a17fd21e 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -30,8 +30,8 @@ #include "libavutil/version.h" #define LIBAVFORMAT_VERSION_MAJOR 57 -#define LIBAVFORMAT_VERSION_MINOR 34 -#define LIBAVFORMAT_VERSION_MICRO 103 +#define LIBAVFORMAT_VERSION_MINOR 35 +#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \ diff --git a/libavformat/wsddec.c b/libavformat/wsddec.c new file mode 100644 index 00000000000..e84d093c656 --- /dev/null +++ b/libavformat/wsddec.c @@ -0,0 +1,172 @@ +/* + * Wideband Single-bit Data (WSD) demuxer + * Copyright (c) 2014 Peter Ross + * + * 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/intreadwrite.h" +#include "libavutil/timecode.h" +#include "avformat.h" +#include "internal.h" +#include "rawdec.h" + +static int wsd_probe(AVProbeData *p) +{ + if (p->buf_size < 45 || memcmp(p->buf, "1bit", 4) || + !AV_RB32(p->buf + 36) || !p->buf[44] || + (p->buf[0] >= 0x10 && (AV_RB32(p->buf + 20) < 0x80 || AV_RB32(p->buf + 24) < 0x80))) + return 0; + return AVPROBE_SCORE_MAX; +} + +static int empty_string(const char *buf, unsigned size) +{ + while (size--) { + if (*buf++ != ' ') + return 0; + } + return 1; +} + +static int wsd_to_av_channel_layoyt(AVFormatContext *s, int bit) +{ + switch (bit) { + case 2: return AV_CH_BACK_RIGHT; + case 3: + avpriv_request_sample(s, "Rr-middle"); + break; + case 4: return AV_CH_BACK_CENTER; + case 5: + avpriv_request_sample(s, "Lr-middle"); + break; + case 6: return AV_CH_BACK_LEFT; + case 24: return AV_CH_LOW_FREQUENCY; + case 26: return AV_CH_FRONT_RIGHT; + case 27: return AV_CH_FRONT_RIGHT_OF_CENTER; + case 28: return AV_CH_FRONT_CENTER; + case 29: return AV_CH_FRONT_LEFT_OF_CENTER; + case 30: return AV_CH_FRONT_LEFT; + default: + av_log(s, AV_LOG_WARNING, "reserved channel assignment\n"); + break; + } + return 0; +} + +static int get_metadata(AVFormatContext *s, const char *const tag, const unsigned size) +{ + uint8_t *buf; + if (!(size + 1)) + return AVERROR(ENOMEM); + + buf = av_malloc(size + 1); + if (!buf) + return AVERROR(ENOMEM); + + if (avio_read(s->pb, buf, size) != size) { + av_free(buf); + return AVERROR(EIO); + } + + if (empty_string(buf, size)) { + av_free(buf); + return 0; + } + + buf[size] = 0; + av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL); + return 0; +} + +static int wsd_read_header(AVFormatContext *s) +{ + AVIOContext *pb = s->pb; + AVStream *st; + int version; + uint32_t text_offset, data_offset, channel_assign; + char playback_time[AV_TIMECODE_STR_SIZE]; + + st = avformat_new_stream(s, NULL); + if (!st) + return AVERROR(ENOMEM); + + avio_skip(pb, 8); + version = avio_r8(pb); + av_log(s, AV_LOG_DEBUG, "version: %i.%i\n", version >> 4, version & 0xF); + avio_skip(pb, 11); + + if (version < 0x10) { + text_offset = 0x80; + data_offset = 0x800; + avio_skip(pb, 8); + } else { + text_offset = avio_rb32(pb); + data_offset = avio_rb32(pb); + } + + avio_skip(pb, 4); + av_timecode_make_smpte_tc_string(playback_time, avio_rb32(pb), 0); + av_dict_set(&s->metadata, "playback_time", playback_time, 0); + + st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; + st->codecpar->codec_id = s->iformat->raw_codec_id; + st->codecpar->sample_rate = avio_rb32(pb) / 8; + avio_skip(pb, 4); + st->codecpar->channels = avio_r8(pb) & 0xF; + if (!st->codecpar->channels) + return AVERROR_INVALIDDATA; + + avio_skip(pb, 3); + channel_assign = avio_rb32(pb); + if (!(channel_assign & 1)) { + int i; + for (i = 1; i < 32; i++) + if (channel_assign & (1 << i)) + st->codecpar->channel_layout |= wsd_to_av_channel_layoyt(s, i); + } + + avio_skip(pb, 16); + if (avio_rb32(pb)) + avpriv_request_sample(s, "emphasis"); + + if (avio_seek(pb, text_offset, SEEK_SET) >= 0) { + get_metadata(s, "title", 128); + get_metadata(s, "composer", 128); + get_metadata(s, "song_writer", 128); + get_metadata(s, "artist", 128); + get_metadata(s, "album", 128); + get_metadata(s, "genre", 32); + get_metadata(s, "date", 32); + get_metadata(s, "location", 32); + get_metadata(s, "comment", 512); + get_metadata(s, "user", 512); + } + + return avio_seek(pb, data_offset, SEEK_SET); +} + +AVInputFormat ff_wsd_demuxer = { + .name = "wsd", + .long_name = NULL_IF_CONFIG_SMALL("Wideband Single-bit Data (WSD)"), + .read_probe = wsd_probe, + .read_header = wsd_read_header, + .read_packet = ff_raw_read_partial_packet, + .extensions = "wsd", + .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK, + .raw_codec_id = AV_CODEC_ID_DSD_MSBF, +}; -- 2.39.2