]> git.sesse.net Git - ffmpeg/blob - libavformat/wvenc.c
Merge commit '28243b0d35b47bbf9abbd454fc444a6e0a9e7b71'
[ffmpeg] / libavformat / wvenc.c
1 /*
2  * WavPack muxer
3  * Copyright (c) 2012 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "apetag.h"
26
27 typedef struct WVMuxContext {
28     int64_t samples;
29 } WVMuxContext;
30
31 static int write_header(AVFormatContext *s)
32 {
33     AVCodecContext *codec = s->streams[0]->codec;
34
35     if (s->nb_streams > 1) {
36         av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
37         return AVERROR(EINVAL);
38     }
39     if (codec->codec_id != AV_CODEC_ID_WAVPACK) {
40         av_log(s, AV_LOG_ERROR, "unsupported codec\n");
41         return AVERROR(EINVAL);
42     }
43     return 0;
44 }
45
46 static int write_packet(AVFormatContext *ctx, AVPacket *pkt)
47 {
48     WVMuxContext *s = ctx->priv_data;
49
50     if (pkt->size >= 24)
51         s->samples += AV_RL32(pkt->data + 20);
52     avio_write(ctx->pb, pkt->data, pkt->size);
53
54     return 0;
55 }
56
57 static int write_trailer(AVFormatContext *ctx)
58 {
59     WVMuxContext *s = ctx->priv_data;
60
61     ff_ape_write(ctx);
62
63     if (ctx->pb->seekable && s->samples) {
64         avio_seek(ctx->pb, 12, SEEK_SET);
65         if (s->samples < 0xFFFFFFFFu)
66             avio_wl32(ctx->pb, s->samples);
67         else
68             avio_wl32(ctx->pb, 0xFFFFFFFFu);
69         avio_flush(ctx->pb);
70     }
71
72     return 0;
73 }
74
75 AVOutputFormat ff_wv_muxer = {
76     .name              = "wv",
77     .long_name         = NULL_IF_CONFIG_SMALL("WavPack"),
78     .priv_data_size    = sizeof(WVMuxContext),
79     .extensions        = "wv",
80     .audio_codec       = AV_CODEC_ID_WAVPACK,
81     .video_codec       = AV_CODEC_ID_NONE,
82     .write_header      = write_header,
83     .write_packet      = write_packet,
84     .write_trailer     = write_trailer,
85     .flags             = AVFMT_NOTIMESTAMPS,
86 };