]> git.sesse.net Git - ffmpeg/blob - libavformat/wvenc.c
oggdec: simplify start time calculation code.
[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 "avformat.h"
23 #include "internal.h"
24 #include "avio_internal.h"
25 #include "apetag.h"
26
27 typedef struct{
28     uint32_t duration;
29     int off;
30 } WVMuxContext;
31
32 static int write_header(AVFormatContext *s)
33 {
34     WVMuxContext *wc = s->priv_data;
35     AVCodecContext *codec = s->streams[0]->codec;
36
37     if (s->nb_streams > 1) {
38         av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
39         return AVERROR(EINVAL);
40     }
41     if (codec->codec_id != CODEC_ID_WAVPACK) {
42         av_log(s, AV_LOG_ERROR, "unsupported codec\n");
43         return AVERROR(EINVAL);
44     }
45     if (codec->extradata_size > 0) {
46         av_log_missing_feature(s, "remuxing from matroska container", 0);
47         return AVERROR_PATCHWELCOME;
48     }
49     wc->off = codec->channels > 2 ? 4 : 0;
50     avpriv_set_pts_info(s->streams[0], 64, 1, codec->sample_rate);
51
52     return 0;
53 }
54
55 static int write_packet(AVFormatContext *s, AVPacket *pkt)
56 {
57     WVMuxContext *wc = s->priv_data;
58     AVIOContext *pb = s->pb;
59
60     wc->duration += pkt->duration;
61     ffio_wfourcc(pb, "wvpk");
62     avio_wl32(pb, pkt->size + 12 + wc->off);
63     avio_wl16(pb, 0x410);
64     avio_w8(pb, 0);
65     avio_w8(pb, 0);
66     avio_wl32(pb, -1);
67     avio_wl32(pb, pkt->pts);
68     avio_write(s->pb, pkt->data, pkt->size);
69     avio_flush(s->pb);
70
71     return 0;
72 }
73
74 static int write_trailer(AVFormatContext *s)
75 {
76     WVMuxContext *wc = s->priv_data;
77     AVIOContext *pb = s->pb;
78
79     ff_ape_write(s);
80
81     if (pb->seekable) {
82         avio_seek(pb, 12, SEEK_SET);
83         avio_wl32(pb, wc->duration);
84         avio_flush(pb);
85     }
86
87     return 0;
88 }
89
90 AVOutputFormat ff_wv_muxer = {
91     .name              = "wv",
92     .long_name         = NULL_IF_CONFIG_SMALL("WavPack"),
93     .priv_data_size    = sizeof(WVMuxContext),
94     .extensions        = "wv",
95     .audio_codec       = CODEC_ID_WAVPACK,
96     .video_codec       = CODEC_ID_NONE,
97     .write_header      = write_header,
98     .write_packet      = write_packet,
99     .write_trailer     = write_trailer,
100 };