3 * Copyright (c) 2023 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
25 #include "avio_internal.h"
29 typedef struct WavArcContext
{
33 static int wavarc_probe(const AVProbeData
*p
)
38 if (len
== 0 || len
+ 6 >= p
->buf_size
)
41 if (p
->buf
[len
+ 1] != 0)
44 id
= AV_RL32(p
->buf
+ len
+ 2);
45 if (id
!= MKTAG('0','C','P','Y') &&
46 id
!= MKTAG('1','D','I','F') &&
47 id
!= MKTAG('2','S','L','P') &&
48 id
!= MKTAG('3','N','L','P') &&
49 id
!= MKTAG('4','A','L','P') &&
50 id
!= MKTAG('5','E','L','P'))
53 return AVPROBE_SCORE_MAX
/ 3 * 2;
56 static int wavarc_read_header(AVFormatContext
*s
)
58 WavArcContext
*w
= s
->priv_data
;
59 AVIOContext
*pb
= s
->pb
;
60 AVCodecParameters
*par
;
61 int filename_len
, fmt_len
, ret
;
66 filename_len
= avio_r8(pb
);
67 if (filename_len
== 0)
68 return AVERROR_INVALIDDATA
;
69 avio_skip(pb
, filename_len
);
71 return AVERROR_INVALIDDATA
;
73 w
->data_end
= avio_tell(pb
);
74 if ((ret
= ffio_read_size(pb
, data
, sizeof(data
))) < 0)
76 w
->data_end
+= 16LL + AV_RL32(data
+ 4);
77 fmt_len
= AV_RL32(data
+ 32);
79 return AVERROR_INVALIDDATA
;
81 st
= avformat_new_stream(s
, NULL
);
83 return AVERROR(ENOMEM
);
86 ret
= ff_alloc_extradata(par
, fmt_len
+ sizeof(data
));
89 memcpy(par
->extradata
, data
, sizeof(data
));
90 ret
= ffio_read_size(pb
, par
->extradata
+ sizeof(data
), fmt_len
);
94 par
->codec_type
= AVMEDIA_TYPE_AUDIO
;
95 par
->codec_id
= AV_CODEC_ID_WAVARC
;
100 if (id
!= MKTAG('d','a','t','a'))
101 avio_skip(pb
, avio_rl32(pb
));
102 } while (id
!= MKTAG('d','a','t','a') && !avio_feof(pb
));
105 if (AV_RL32(par
->extradata
+ 16) != MKTAG('R','I','F','F'))
106 return AVERROR_INVALIDDATA
;
107 if (AV_RL32(par
->extradata
+ 24) != MKTAG('W','A','V','E'))
108 return AVERROR_INVALIDDATA
;
109 if (AV_RL32(par
->extradata
+ 28) != MKTAG('f','m','t',' '))
110 return AVERROR_INVALIDDATA
;
112 av_channel_layout_default(&par
->ch_layout
, AV_RL16(par
->extradata
+ 38));
113 par
->sample_rate
= AV_RL32(par
->extradata
+ 40);
114 avpriv_set_pts_info(st
, 64, 1, par
->sample_rate
);
120 static int wavarc_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
122 WavArcContext
*w
= s
->priv_data
;
123 AVIOContext
*pb
= s
->pb
;
124 int64_t size
, left
= w
->data_end
- avio_tell(pb
);
127 size
= FFMIN(left
, 1024);
131 ret
= av_get_packet(pb
, pkt
, size
);
132 pkt
->stream_index
= 0;
136 const FFInputFormat ff_wavarc_demuxer
= {
138 .p
.long_name
= NULL_IF_CONFIG_SMALL("Waveform Archiver"),
139 .p
.flags
= AVFMT_NOBINSEARCH
| AVFMT_NOGENSEARCH
| AVFMT_NO_BYTE_SEEK
| AVFMT_NOTIMESTAMPS
,
140 .p
.extensions
= "wa",
141 .priv_data_size
= sizeof(WavArcContext
),
142 .read_probe
= wavarc_probe
,
143 .read_packet
= wavarc_read_packet
,
144 .read_header
= wavarc_read_header
,
145 .raw_codec_id
= AV_CODEC_ID_WAVARC
,