FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/westwood_audenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 38 41 92.7%
Functions: 4 4 100.0%
Branches: 4 8 50.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2021 Aidan Richmond
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Westwood Studios AUD file muxer
24 * by Aidan Richmond (aidan.is@hotmail.co.uk)
25 *
26 * This muxer supports IMA ADPCM packed in westwoods format.
27 *
28 * @see http://xhp.xwis.net/documents/aud3.txt
29 */
30
31 #include "avformat.h"
32 #include "avio_internal.h"
33 #include "internal.h"
34 #include "mux.h"
35 #include <stdint.h>
36
37 #define AUD_CHUNK_SIGNATURE 0x0000DEAF
38
39 typedef struct AUDMuxContext {
40 int uncomp_size;
41 int size;
42 } AUDMuxContext;
43
44 1 static int wsaud_write_init(AVFormatContext *ctx)
45 {
46 1 AVIOContext *pb = ctx->pb;
47
48 /* Stream must be seekable to correctly write the file. */
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
50 av_log(ctx, AV_LOG_ERROR, "Cannot write Westwood AUD to"
51 " non-seekable stream.\n");
52 return AVERROR(EINVAL);
53 }
54
55 1 return 0;
56 }
57
58 1 static int wsaud_write_header(AVFormatContext *ctx)
59 {
60 1 AVStream *st = ctx->streams[0];
61 1 AVIOContext *pb = ctx->pb;
62 1 AUDMuxContext *a = ctx->priv_data;
63 1 unsigned char flags = 0;
64
65 1 a->uncomp_size = 0;
66 1 a->size = 0;
67
68 /* Flag if we have stereo data. */
69
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (st->codecpar->ch_layout.nb_channels == 2)
70 1 flags |= 1;
71
72 /* This flags that the file contains 16 bit samples rather than 8 bit
73 since the encoder only encodes 16 bit samples this should be set. */
74
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (av_get_bits_per_sample(st->codecpar->codec_id) == 4)
75 1 flags |= 2;
76
77 1 avio_wl16(pb, st->codecpar->sample_rate);
78 /* We don't know the file size yet, so just zero 8 bytes */
79 1 ffio_fill(pb, 0, 8);
80 1 avio_w8(pb, flags);
81 /* 99 indicates the ADPCM format. Other formats not supported. */
82 1 avio_w8(pb, 99);
83
84 1 return 0;
85 }
86
87 259 static int wsaud_write_packet(AVFormatContext *ctx, AVPacket *pkt)
88 {
89 259 AVIOContext *pb = ctx->pb;
90 259 AUDMuxContext *a = ctx->priv_data;
91
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259 times.
259 if (pkt->size > UINT16_MAX / 4)
93 return AVERROR_INVALIDDATA;
94 /* Assumes ADPCM since this muxer doesn't support SND1 or PCM format. */
95 259 avio_wl16(pb, pkt->size);
96 259 avio_wl16(pb, pkt->size * 4);
97 259 avio_wl32(pb, AUD_CHUNK_SIGNATURE);
98 259 avio_write(pb, pkt->data, pkt->size);
99 259 a->size += pkt->size + 8;
100 259 a->uncomp_size += pkt->size * 4;
101
102 259 return 0;
103 }
104
105 1 static int wsaud_write_trailer(AVFormatContext *ctx)
106 {
107 1 AVIOContext *pb = ctx->pb;
108 1 AUDMuxContext *a = ctx->priv_data;
109
110 1 avio_seek(pb, 2, SEEK_SET);
111 1 avio_wl32(pb, a->size);
112 1 avio_wl32(pb, a->uncomp_size);
113
114 1 return 0;
115 }
116
117 const FFOutputFormat ff_wsaud_muxer = {
118 .p.name = "wsaud",
119 .p.long_name = NULL_IF_CONFIG_SMALL("Westwood Studios audio"),
120 .p.extensions = "aud",
121 .priv_data_size = sizeof(AUDMuxContext),
122 .p.audio_codec = AV_CODEC_ID_ADPCM_IMA_WS,
123 .p.video_codec = AV_CODEC_ID_NONE,
124 .p.subtitle_codec = AV_CODEC_ID_NONE,
125 .init = wsaud_write_init,
126 .write_header = wsaud_write_header,
127 .write_packet = wsaud_write_packet,
128 .write_trailer = wsaud_write_trailer,
129 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
130 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
131 };
132