]> git.sesse.net Git - ffmpeg/blob - libavformat/wvenc.c
7fed4a687d2e4ef3b3a0bad6bc1349f1ac89e29e
[ffmpeg] / libavformat / wvenc.c
1 /*
2  * WavPack muxer
3  * Copyright (c) 2013 Kostya Shishkov <kostya.shishkov@gmail.com>
4  * Copyright (c) 2012 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23
24 #include "libavutil/attributes.h"
25
26 #include "apetag.h"
27 #include "avformat.h"
28 #include "wv.h"
29
30 typedef struct WvMuxContext {
31     int64_t samples;
32 } WvMuxContext;
33
34 static av_cold int wv_write_header(AVFormatContext *ctx)
35 {
36     if (ctx->nb_streams > 1 ||
37         ctx->streams[0]->codec->codec_id != AV_CODEC_ID_WAVPACK) {
38         av_log(ctx, AV_LOG_ERROR, "This muxer only supports a single WavPack stream.\n");
39         return AVERROR(EINVAL);
40     }
41
42     return 0;
43 }
44
45 static int wv_write_packet(AVFormatContext *ctx, AVPacket *pkt)
46 {
47     WvMuxContext *s = ctx->priv_data;
48     WvHeader header;
49     int ret;
50
51     if (pkt->size < WV_HEADER_SIZE ||
52         (ret = ff_wv_parse_header(&header, pkt->data)) < 0) {
53         av_log(ctx, AV_LOG_ERROR, "Invalid WavPack packet.\n");
54         return AVERROR(EINVAL);
55     }
56     s->samples += header.samples;
57
58     avio_write(ctx->pb, pkt->data, pkt->size);
59     avio_flush(ctx->pb);
60
61     return 0;
62 }
63
64 static av_cold int wv_write_trailer(AVFormatContext *ctx)
65 {
66     WvMuxContext *s = ctx->priv_data;
67
68     /* update total number of samples in the first block */
69     if (ctx->pb->seekable && s->samples &&
70         s->samples < UINT32_MAX) {
71         int64_t pos = avio_tell(ctx->pb);
72         avio_seek(ctx->pb, 12, SEEK_SET);
73         avio_wl32(ctx->pb, s->samples);
74         avio_seek(ctx->pb, pos, SEEK_SET);
75     }
76
77     ff_ape_write_tag(ctx);
78     return 0;
79 }
80
81 AVOutputFormat ff_wv_muxer = {
82     .name              = "wv",
83     .long_name         = NULL_IF_CONFIG_SMALL("raw WavPack"),
84     .mime_type         = "audio/x-wavpack",
85     .extensions        = "wv",
86     .priv_data_size    = sizeof(WvMuxContext),
87     .audio_codec       = AV_CODEC_ID_WAVPACK,
88     .video_codec       = AV_CODEC_ID_NONE,
89     .write_header      = wv_write_header,
90     .write_packet      = wv_write_packet,
91     .write_trailer     = wv_write_trailer,
92     .flags             = AVFMT_NOTIMESTAMPS,
93 };