| @@ -0,0 +1,126 @@ |
| 1 | +/* |
| 2 | + * WavArc demuxer |
| 3 | + * Copyright (c) 2023 Paul B Mahol |
| 4 | + * |
| 5 | + * This file is part of FFmpeg. |
| 6 | + * |
| 7 | + * FFmpeg is free software; you can redistribute it and/or |
| 8 | + * modify it under the terms of the GNU Lesser General Public |
| 9 | + * License as published by the Free Software Foundation; either |
| 10 | + * version 2.1 of the License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * FFmpeg is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public |
| 18 | + * License along with FFmpeg; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + */ |
| 21 | + |
| 22 | +#include "libavutil/channel_layout.h" |
| 23 | +#include "libavutil/intreadwrite.h" |
| 24 | +#include "avformat.h" |
| 25 | +#include "demux.h" |
| 26 | +#include "internal.h" |
| 27 | +#include "rawdec.h" |
| 28 | + |
| 29 | +static int wavarc_probe(const AVProbeData *p) |
| 30 | +{ |
| 31 | + int len = p->buf[0]; |
| 32 | + uint32_t id; |
| 33 | + |
| 34 | + if (len == 0 || len + 6 >= p->buf_size) |
| 35 | + return 0; |
| 36 | + |
| 37 | + if (p->buf[len + 1] != 0) |
| 38 | + return 0; |
| 39 | + |
| 40 | + id = AV_RL32(p->buf + len + 2); |
| 41 | + if (id != MKTAG('1','D','I','F') && |
| 42 | + id != MKTAG('2','S','L','P') && |
| 43 | + id != MKTAG('3','N','L','P') && |
| 44 | + id != MKTAG('4','A','L','P') && |
| 45 | + id != MKTAG('5','E','L','P')) |
| 46 | + return 0; |
| 47 | + |
| 48 | + return AVPROBE_SCORE_MAX / 3 * 2; |
| 49 | +} |
| 50 | + |
| 51 | +static int wavarc_read_header(AVFormatContext *s) |
| 52 | +{ |
| 53 | + AVIOContext *pb = s->pb; |
| 54 | + AVCodecParameters *par; |
| 55 | + int filename_len, fmt_len, ret; |
| 56 | + uint8_t data[36]; |
| 57 | + AVStream *st; |
| 58 | + uint32_t id; |
| 59 | + |
| 60 | + filename_len = avio_r8(pb); |
| 61 | + if (filename_len == 0) |
| 62 | + return AVERROR_INVALIDDATA; |
| 63 | + avio_skip(pb, filename_len); |
| 64 | + if (avio_r8(pb)) |
| 65 | + return AVERROR_INVALIDDATA; |
| 66 | + id = avio_rl32(pb); |
| 67 | + if (avio_read(pb, data, sizeof(data)) != sizeof(data)) |
| 68 | + return AVERROR(EIO); |
| 69 | + fmt_len = AV_RL32(data + 32); |
| 70 | + if (fmt_len < 12) |
| 71 | + return AVERROR_INVALIDDATA; |
| 72 | + |
| 73 | + st = avformat_new_stream(s, NULL); |
| 74 | + if (!st) |
| 75 | + return AVERROR(ENOMEM); |
| 76 | + par = st->codecpar; |
| 77 | + |
| 78 | + ret = ff_alloc_extradata(par, fmt_len + sizeof(data)); |
| 79 | + if (ret < 0) |
| 80 | + return ret; |
| 81 | + memcpy(par->extradata, data, sizeof(data)); |
| 82 | + avio_read(pb, par->extradata + sizeof(data), fmt_len); |
| 83 | + |
| 84 | + par->codec_type = AVMEDIA_TYPE_AUDIO; |
| 85 | + par->codec_id = AV_CODEC_ID_WAVARC; |
| 86 | + par->codec_tag = id; |
| 87 | + |
| 88 | + do { |
| 89 | + id = avio_rl32(pb); |
| 90 | + if (id != MKTAG('d','a','t','a')) |
| 91 | + avio_skip(pb, avio_rl32(pb)); |
| 92 | + } while (id != MKTAG('d','a','t','a')); |
| 93 | + avio_skip(pb, 4); |
| 94 | + |
| 95 | + if (AV_RL32(par->extradata + 16) != MKTAG('R','I','F','F')) |
| 96 | + return AVERROR_INVALIDDATA; |
| 97 | + if (AV_RL32(par->extradata + 24) != MKTAG('W','A','V','E')) |
| 98 | + return AVERROR_INVALIDDATA; |
| 99 | + if (AV_RL32(par->extradata + 28) != MKTAG('f','m','t',' ')) |
| 100 | + return AVERROR_INVALIDDATA; |
| 101 | + |
| 102 | + av_channel_layout_default(&par->ch_layout, AV_RL16(par->extradata + 38)); |
| 103 | + par->sample_rate = AV_RL32(par->extradata + 40); |
| 104 | + avpriv_set_pts_info(st, 64, 1, par->sample_rate); |
| 105 | + st->start_time = 0; |
| 106 | + |
| 107 | + switch (par->extradata[36]) { |
| 108 | + case 0: par->format = AV_SAMPLE_FMT_U8P; break; |
| 109 | + case 1: par->format = AV_SAMPLE_FMT_S16P; break; |
| 110 | + } |
| 111 | + |
| 112 | + return 0; |
| 113 | +} |
| 114 | + |
| 115 | +const AVInputFormat ff_wavarc_demuxer = { |
| 116 | + .name = "wavarc", |
| 117 | + .long_name = NULL_IF_CONFIG_SMALL("Waveform Archiver"), |
| 118 | + .read_probe = wavarc_probe, |
| 119 | + .read_packet = ff_raw_read_partial_packet, |
| 120 | + .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK | AVFMT_NOTIMESTAMPS, |
| 121 | + .read_header = wavarc_read_header, |
| 122 | + .extensions = "wa", |
| 123 | + .raw_codec_id = AV_CODEC_ID_WAVARC, |
| 124 | + .priv_data_size = sizeof(FFRawDemuxerContext), |
| 125 | + .priv_class = &ff_raw_demuxer_class, |
| 126 | +}; |