3 * Copyright (c) 2012 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/intreadwrite.h"
25 #include "avio_internal.h"
28 #define WV_EXTRA_SIZE 12
29 #define WV_END_BLOCK 0x1000
35 static int write_header(AVFormatContext *s)
37 AVCodecContext *codec = s->streams[0]->codec;
39 if (s->nb_streams > 1) {
40 av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
41 return AVERROR(EINVAL);
43 if (codec->codec_id != AV_CODEC_ID_WAVPACK) {
44 av_log(s, AV_LOG_ERROR, "unsupported codec\n");
45 return AVERROR(EINVAL);
47 if (codec->extradata_size > 0) {
48 av_log_missing_feature(s, "remuxing from matroska container", 0);
49 return AVERROR_PATCHWELCOME;
51 avpriv_set_pts_info(s->streams[0], 64, 1, codec->sample_rate);
56 static int write_packet(AVFormatContext *s, AVPacket *pkt)
58 WVMuxContext *wc = s->priv_data;
59 AVCodecContext *codec = s->streams[0]->codec;
60 AVIOContext *pb = s->pb;
63 uint32_t left = pkt->size;
64 uint8_t *ptr = pkt->data;
65 int off = codec->channels > 2 ? 4 : 0;
67 /* FIXME: Simplify decoder/demuxer so bellow code can support midstream
68 * change of stream parameters */
69 wc->duration += pkt->duration;
70 ffio_wfourcc(pb, "wvpk");
72 size = AV_RL32(pkt->data);
74 return AVERROR_INVALIDDATA;
80 if (size + off > left)
81 return AVERROR_INVALIDDATA;
83 avio_wl32(pb, size + 12);
88 avio_wl32(pb, pkt->pts);
89 ptr += off; left -= off;
90 flags = AV_RL32(ptr + 4);
91 avio_write(pb, ptr, size);
92 ptr += size; left -= size;
94 while (!(flags & WV_END_BLOCK) &&
95 (left >= 4 + WV_EXTRA_SIZE)) {
96 ffio_wfourcc(pb, "wvpk");
99 if (size < 24 || size - 24 > left)
100 return AVERROR_INVALIDDATA;
102 avio_wl16(pb, 0x410);
106 avio_wl32(pb, pkt->pts);
107 flags = AV_RL32(ptr + 4);
108 avio_write(pb, ptr, WV_EXTRA_SIZE);
109 ptr += WV_EXTRA_SIZE; left -= WV_EXTRA_SIZE;
110 avio_write(pb, ptr, size - 24);
111 ptr += size - 24; left -= size - 24;
118 static int write_trailer(AVFormatContext *s)
120 WVMuxContext *wc = s->priv_data;
121 AVIOContext *pb = s->pb;
126 avio_seek(pb, 12, SEEK_SET);
127 avio_wl32(pb, wc->duration);
134 AVOutputFormat ff_wv_muxer = {
136 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
137 .priv_data_size = sizeof(WVMuxContext),
139 .audio_codec = AV_CODEC_ID_WAVPACK,
140 .video_codec = AV_CODEC_ID_NONE,
141 .write_header = write_header,
142 .write_packet = write_packet,
143 .write_trailer = write_trailer,