]> git.sesse.net Git - ffmpeg/blob - libavformat/wvenc.c
mov: Wrap stsc index and count compare in a separate function
[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]->codecpar->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
55     return 0;
56 }
57
58 static av_cold int wv_write_trailer(AVFormatContext *ctx)
59 {
60     WvMuxContext *s = ctx->priv_data;
61
62     /* update total number of samples in the first block */
63     if (ctx->pb->seekable && s->samples &&
64         s->samples < UINT32_MAX) {
65         int64_t pos = avio_tell(ctx->pb);
66         avio_seek(ctx->pb, 12, SEEK_SET);
67         avio_wl32(ctx->pb, s->samples);
68         avio_seek(ctx->pb, pos, SEEK_SET);
69     }
70
71     ff_ape_write_tag(ctx);
72     return 0;
73 }
74
75 AVOutputFormat ff_wv_muxer = {
76     .name              = "wv",
77     .long_name         = NULL_IF_CONFIG_SMALL("raw WavPack"),
78     .mime_type         = "audio/x-wavpack",
79     .extensions        = "wv",
80     .priv_data_size    = sizeof(WvMuxContext),
81     .audio_codec       = AV_CODEC_ID_WAVPACK,
82     .video_codec       = AV_CODEC_ID_NONE,
83     .write_header      = wv_write_header,
84     .write_packet      = wv_write_packet,
85     .write_trailer     = wv_write_trailer,
86     .flags             = AVFMT_NOTIMESTAMPS,
87 };