]> git.sesse.net Git - ffmpeg/blob - libavformat/wvenc.c
wrap_timestamp: remove unneeded check
[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 "avio_internal.h"
26 #include "apetag.h"
27
28 #define WV_EXTRA_SIZE 12
29 #define WV_END_BLOCK  0x1000
30
31 typedef struct{
32     uint32_t duration;
33 } WVMuxContext;
34
35 static int write_header(AVFormatContext *s)
36 {
37     AVCodecContext *codec = s->streams[0]->codec;
38
39     if (s->nb_streams > 1) {
40         av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
41         return AVERROR(EINVAL);
42     }
43     if (codec->codec_id != AV_CODEC_ID_WAVPACK) {
44         av_log(s, AV_LOG_ERROR, "unsupported codec\n");
45         return AVERROR(EINVAL);
46     }
47     if (codec->extradata_size > 0) {
48         av_log_missing_feature(s, "remuxing from matroska container", 0);
49         return AVERROR_PATCHWELCOME;
50     }
51     avpriv_set_pts_info(s->streams[0], 64, 1, codec->sample_rate);
52
53     return 0;
54 }
55
56 static int write_packet(AVFormatContext *s, AVPacket *pkt)
57 {
58     WVMuxContext *wc = s->priv_data;
59     AVCodecContext *codec = s->streams[0]->codec;
60     AVIOContext *pb = s->pb;
61     uint64_t size;
62     uint32_t flags;
63     uint32_t left = pkt->size;
64     uint8_t *ptr = pkt->data;
65     int off = codec->channels > 2 ? 4 : 0;
66
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");
71     if (off) {
72         size = AV_RL32(pkt->data);
73         if (size <= 12)
74             return AVERROR_INVALIDDATA;
75         size -= 12;
76     } else {
77         size = pkt->size;
78     }
79
80     if (size + off > left)
81         return AVERROR_INVALIDDATA;
82
83     avio_wl32(pb, size + 12);
84     avio_wl16(pb, 0x410);
85     avio_w8(pb, 0);
86     avio_w8(pb, 0);
87     avio_wl32(pb, -1);
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;
93
94     while (!(flags & WV_END_BLOCK) &&
95             (left >= 4 + WV_EXTRA_SIZE)) {
96         ffio_wfourcc(pb, "wvpk");
97         size = AV_RL32(ptr);
98         ptr += 4; left -= 4;
99         if (size < 24 || size - 24 > left)
100             return AVERROR_INVALIDDATA;
101         avio_wl32(pb, size);
102         avio_wl16(pb, 0x410);
103         avio_w8(pb, 0);
104         avio_w8(pb, 0);
105         avio_wl32(pb, -1);
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;
112     }
113     avio_flush(pb);
114
115     return 0;
116 }
117
118 static int write_trailer(AVFormatContext *s)
119 {
120     WVMuxContext *wc = s->priv_data;
121     AVIOContext *pb = s->pb;
122
123     ff_ape_write(s);
124
125     if (pb->seekable) {
126         avio_seek(pb, 12, SEEK_SET);
127         avio_wl32(pb, wc->duration);
128         avio_flush(pb);
129     }
130
131     return 0;
132 }
133
134 AVOutputFormat ff_wv_muxer = {
135     .name              = "wv",
136     .long_name         = NULL_IF_CONFIG_SMALL("WavPack"),
137     .priv_data_size    = sizeof(WVMuxContext),
138     .extensions        = "wv",
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,
144 };