]> git.sesse.net Git - ffmpeg/blob - libavformat/wvenc.c
rtpproto: Add an option for writing return packets to the address of the last receive...
[ffmpeg] / libavformat / wvenc.c
1 /*
2  * This file is part of Libav.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "libavutil/attributes.h"
20
21 #include "apetag.h"
22 #include "avformat.h"
23 #include "wv.h"
24
25 typedef struct WvMuxContext {
26     int64_t samples;
27 } WvMuxContext;
28
29 static av_cold int wv_write_header(AVFormatContext *ctx)
30 {
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);
35     }
36
37     return 0;
38 }
39
40 static int wv_write_packet(AVFormatContext *ctx, AVPacket *pkt)
41 {
42     WvMuxContext *s = ctx->priv_data;
43     WvHeader header;
44     int ret;
45
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);
50     }
51     s->samples += header.samples;
52
53     avio_write(ctx->pb, pkt->data, pkt->size);
54     avio_flush(ctx->pb);
55
56     return 0;
57 }
58
59 static av_cold int wv_write_trailer(AVFormatContext *ctx)
60 {
61     WvMuxContext *s = ctx->priv_data;
62
63     /* update total number of samples in the first block */
64     if (ctx->pb->seekable && s->samples &&
65         s->samples < UINT32_MAX) {
66         int64_t pos = avio_tell(ctx->pb);
67         avio_seek(ctx->pb, 12, SEEK_SET);
68         avio_wl32(ctx->pb, s->samples);
69         avio_seek(ctx->pb, pos, SEEK_SET);
70     }
71
72     ff_ape_write_tag(ctx);
73     return 0;
74 }
75
76 AVOutputFormat ff_wv_muxer = {
77     .name              = "wv",
78     .long_name         = NULL_IF_CONFIG_SMALL("raw WavPack"),
79     .mime_type         = "audio/x-wavpack",
80     .extensions        = "wv",
81     .priv_data_size    = sizeof(WvMuxContext),
82     .audio_codec       = AV_CODEC_ID_WAVPACK,
83     .video_codec       = AV_CODEC_ID_NONE,
84     .write_header      = wv_write_header,
85     .write_packet      = wv_write_packet,
86     .write_trailer     = wv_write_trailer,
87     .flags             = AVFMT_NOTIMESTAMPS,
88 };