FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/ttaenc.c
Date: 2024-09-07 18:49:03
Exec Total Coverage
Lines: 57 67 85.1%
Functions: 6 6 100.0%
Branches: 11 20 55.0%

Line Branch Exec Source
1 /*
2 * True Audio (TTA) muxer
3 * Copyright (c) 2016 James Almer
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/crc.h"
23 #include "libavutil/intreadwrite.h"
24
25 #include "libavcodec/packet_internal.h"
26 #include "apetag.h"
27 #include "avformat.h"
28 #include "avio_internal.h"
29 #include "internal.h"
30 #include "mux.h"
31
32 typedef struct TTAMuxContext {
33 AVIOContext *seek_table;
34 PacketList queue;
35 uint32_t nb_samples;
36 int frame_size;
37 int last_frame;
38 } TTAMuxContext;
39
40 2 static int tta_init(AVFormatContext *s)
41 {
42 2 TTAMuxContext *tta = s->priv_data;
43 2 AVCodecParameters *par = s->streams[0]->codecpar;
44
45
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (par->codec_id != AV_CODEC_ID_TTA) {
46 av_log(s, AV_LOG_ERROR, "Unsupported codec\n");
47 return AVERROR(EINVAL);
48 }
49
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (par->extradata && par->extradata_size < 22) {
50 av_log(s, AV_LOG_ERROR, "Invalid TTA extradata\n");
51 return AVERROR_INVALIDDATA;
52 }
53
54 /* Prevent overflow */
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (par->sample_rate > 0x7FFFFFu) {
56 av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
57 return AVERROR(EINVAL);
58 }
59 2 tta->frame_size = par->sample_rate * 256 / 245;
60 2 avpriv_set_pts_info(s->streams[0], 64, 1, par->sample_rate);
61
62 2 return 0;
63 }
64
65 2 static int tta_write_header(AVFormatContext *s)
66 {
67 2 TTAMuxContext *tta = s->priv_data;
68 2 AVCodecParameters *par = s->streams[0]->codecpar;
69 int ret;
70
71
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = avio_open_dyn_buf(&tta->seek_table)) < 0)
72 return ret;
73
74 /* Ignore most extradata information if present. It can be innacurate
75 if for example remuxing from Matroska */
76 2 ffio_init_checksum(s->pb, ff_crcEDB88320_update, UINT32_MAX);
77 2 ffio_init_checksum(tta->seek_table, ff_crcEDB88320_update, UINT32_MAX);
78 2 avio_write(s->pb, "TTA1", 4);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 avio_wl16(s->pb, par->extradata ? AV_RL16(par->extradata + 4) : 1);
80 2 avio_wl16(s->pb, par->ch_layout.nb_channels);
81 2 avio_wl16(s->pb, par->bits_per_raw_sample);
82 2 avio_wl32(s->pb, par->sample_rate);
83
84 2 return 0;
85 }
86
87 7 static int tta_write_packet(AVFormatContext *s, AVPacket *pkt)
88 {
89 7 TTAMuxContext *tta = s->priv_data;
90 int ret;
91
92 7 ret = avpriv_packet_list_put(&tta->queue, pkt, NULL, 0);
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0) {
94 return ret;
95 }
96 7 pkt = &tta->queue.tail->pkt;
97
98 7 avio_wl32(tta->seek_table, pkt->size);
99 7 tta->nb_samples += pkt->duration;
100
101
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (tta->frame_size != pkt->duration) {
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (tta->last_frame) {
103 /* Two frames with a different duration than the default frame
104 size means the TTA stream comes from a faulty container, and
105 there's no way the last frame duration will be correct. */
106 av_log(s, AV_LOG_ERROR, "Invalid frame durations\n");
107
108 return AVERROR_INVALIDDATA;
109 }
110 /* First frame with a different duration than the default frame size.
111 Assume it's the last frame in the stream and continue. */
112 2 tta->last_frame++;
113 }
114
115 7 return 0;
116 }
117
118 2 static void tta_queue_flush(AVFormatContext *s)
119 {
120 2 TTAMuxContext *tta = s->priv_data;
121 2 AVPacket *const pkt = ffformatcontext(s)->pkt;
122
123
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 while (tta->queue.head) {
124 7 avpriv_packet_list_get(&tta->queue, pkt);
125 7 avio_write(s->pb, pkt->data, pkt->size);
126 7 av_packet_unref(pkt);
127 }
128 2 }
129
130 2 static int tta_write_trailer(AVFormatContext *s)
131 {
132 2 TTAMuxContext *tta = s->priv_data;
133 uint8_t *ptr;
134 unsigned int crc;
135 int size;
136
137 2 avio_wl32(s->pb, tta->nb_samples);
138 2 crc = ffio_get_checksum(s->pb) ^ UINT32_MAX;
139 2 avio_wl32(s->pb, crc);
140
141 /* Write Seek table */
142 2 crc = ffio_get_checksum(tta->seek_table) ^ UINT32_MAX;
143 2 avio_wl32(tta->seek_table, crc);
144 2 size = avio_get_dyn_buf(tta->seek_table, &ptr);
145 2 avio_write(s->pb, ptr, size);
146
147 /* Write audio data */
148 2 tta_queue_flush(s);
149
150 2 ff_ape_write_tag(s);
151
152 2 return 0;
153 }
154
155 2 static void tta_deinit(AVFormatContext *s)
156 {
157 2 TTAMuxContext *tta = s->priv_data;
158
159 2 ffio_free_dyn_buf(&tta->seek_table);
160 2 avpriv_packet_list_free(&tta->queue);
161 2 }
162
163 const FFOutputFormat ff_tta_muxer = {
164 .p.name = "tta",
165 .p.long_name = NULL_IF_CONFIG_SMALL("TTA (True Audio)"),
166 .p.mime_type = "audio/x-tta",
167 .p.extensions = "tta",
168 .priv_data_size = sizeof(TTAMuxContext),
169 .p.audio_codec = AV_CODEC_ID_TTA,
170 .p.video_codec = AV_CODEC_ID_NONE,
171 .p.subtitle_codec = AV_CODEC_ID_NONE,
172 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
173 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
174 .init = tta_init,
175 .deinit = tta_deinit,
176 .write_header = tta_write_header,
177 .write_packet = tta_write_packet,
178 .write_trailer = tta_write_trailer,
179 };
180