2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libavutil/attributes.h"
25 typedef struct WvMuxContext {
29 static av_cold int wv_write_header(AVFormatContext *ctx)
31 if (ctx->nb_streams > 1 ||
32 ctx->streams[0]->codec->codec_id != AV_CODEC_ID_WAVPACK) {
33 av_log(ctx, AV_LOG_ERROR, "This muxer only supports a single WavPack stream.\n");
34 return AVERROR(EINVAL);
40 static int wv_write_packet(AVFormatContext *ctx, AVPacket *pkt)
42 WvMuxContext *s = ctx->priv_data;
46 if (pkt->size < WV_HEADER_SIZE ||
47 (ret = ff_wv_parse_header(&header, pkt->data)) < 0) {
48 av_log(ctx, AV_LOG_ERROR, "Invalid WavPack packet.\n");
49 return AVERROR(EINVAL);
51 s->samples += header.samples;
53 avio_write(ctx->pb, pkt->data, pkt->size);
58 static av_cold int wv_write_trailer(AVFormatContext *ctx)
60 WvMuxContext *s = ctx->priv_data;
62 /* update total number of samples in the first block */
63 if (ctx->pb->seekable && s->samples &&
64 s->samples < UINT32_MAX) {
65 int64_t pos = avio_tell(ctx->pb);
66 avio_seek(ctx->pb, 12, SEEK_SET);
67 avio_wl32(ctx->pb, s->samples);
68 avio_seek(ctx->pb, pos, SEEK_SET);
71 ff_ape_write_tag(ctx);
75 AVOutputFormat ff_wv_muxer = {
77 .long_name = NULL_IF_CONFIG_SMALL("raw WavPack"),
78 .mime_type = "audio/x-wavpack",
80 .priv_data_size = sizeof(WvMuxContext),
81 .audio_codec = AV_CODEC_ID_WAVPACK,
82 .video_codec = AV_CODEC_ID_NONE,
83 .write_header = wv_write_header,
84 .write_packet = wv_write_packet,
85 .write_trailer = wv_write_trailer,
86 .flags = AVFMT_NOTIMESTAMPS,