FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/rka.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 65 83 78.3%
Functions: 3 4 75.0%
Branches: 27 48 56.2%

Line Branch Exec Source
1 /*
2 * RKA demuxer
3 * Copyright (c) 2023 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/dict.h"
23 #include "libavutil/intreadwrite.h"
24
25 #include "apetag.h"
26 #include "avformat.h"
27 #include "avio_internal.h"
28 #include "demux.h"
29 #include "internal.h"
30
31 typedef struct RKAContext {
32 int total_frames, currentframe;
33 int frame_size;
34 int last_frame_size;
35 } RKAContext;
36
37 7186 static int rka_probe(const AVProbeData *p)
38 {
39
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7185 times.
7186 if (AV_RL32(&p->buf[0]) == MKTAG('R', 'K', 'A', '7') &&
40
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL32(&p->buf[4]) > 0 &&
41
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 AV_RL32(&p->buf[8]) > 0 &&
42
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 p->buf[12] > 0 &&
43
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 p->buf[12] <= 2 &&
44
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 (p->buf[13] == 8 || p->buf[13] == 16) &&
45
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (p->buf[15] & 2) != 0)
46 1 return AVPROBE_SCORE_EXTENSION + 30;
47 7185 return 0;
48 }
49
50 1 static int rka_read_header(AVFormatContext *s)
51 {
52 int64_t nb_samples, size_offset;
53 1 RKAContext *c = s->priv_data;
54 int channels, bps, samplerate;
55 AVCodecParameters *par;
56 int64_t framepos;
57 AVStream *st;
58 int ret;
59
60 1 st = avformat_new_stream(s, NULL);
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
62 return AVERROR(ENOMEM);
63
64 1 par = st->codecpar;
65 1 ret = ff_get_extradata(s, par, s->pb, 16);
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
67 return ret;
68
69 1 nb_samples = AV_RL32(par->extradata + 4);
70 1 samplerate = AV_RL32(par->extradata + 8);
71 1 channels = par->extradata[12];
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (channels == 0)
73 return AVERROR_INVALIDDATA;
74 1 bps = par->extradata[13];
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (bps < 8)
76 return AVERROR_INVALIDDATA;
77 1 size_offset = avio_rl32(s->pb);
78 1 framepos = avio_tell(s->pb);
79 1 c->frame_size = 131072;
80
81 1 avpriv_set_pts_info(st, 64, 1, samplerate);
82 1 st->start_time = 0;
83
84 1 avio_seek(s->pb, size_offset, SEEK_SET);
85 1 c->total_frames = (nb_samples + c->frame_size - 1) / c->frame_size;
86 1 c->last_frame_size = nb_samples % c->frame_size;
87
88
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 for (int i = 0; i < c->total_frames; i++) {
89 13 int r, end = 0;
90 int64_t size;
91
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if (avio_feof(s->pb))
93 break;
94
95 13 size = avio_rl24(s->pb);
96
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (size == 0) {
97 1 end = 1;
98 1 size = size_offset - framepos;
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (size <= 0)
100 break;
101 }
102
103
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if ((r = av_add_index_entry(st, framepos, (i * 131072LL) / (channels * (bps >> 3)),
104 size, 0, AVINDEX_KEYFRAME)) < 0)
105 return r;
106 13 framepos += size;
107
108
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (end)
109 1 break;
110 }
111
112 1 par->codec_type = AVMEDIA_TYPE_AUDIO;
113 1 par->codec_id = AV_CODEC_ID_RKA;
114 1 par->ch_layout.nb_channels = channels;
115 1 par->sample_rate = samplerate;
116 1 par->bits_per_raw_sample = bps;
117 1 st->duration = 8LL*nb_samples / (channels * bps);
118
119
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL)
120 1 ff_ape_parse_tag(s);
121
122 1 avio_seek(s->pb, 20, SEEK_SET);
123
124 1 return 0;
125 }
126
127 14 static int rka_read_packet(AVFormatContext *s, AVPacket *pkt)
128 {
129 14 RKAContext *c = s->priv_data;
130 14 AVStream *st = s->streams[0];
131 14 FFStream *const sti = ffstream(st);
132 int size, ret;
133
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if (avio_feof(s->pb))
135 return AVERROR_EOF;
136
137
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 if (c->currentframe >= sti->nb_index_entries)
138 1 return AVERROR_EOF;
139
140 13 size = sti->index_entries[c->currentframe].size;
141
142 13 ret = av_get_packet(s->pb, pkt, size);
143 13 pkt->dts = sti->index_entries[c->currentframe++].timestamp;
144
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 pkt->duration = c->currentframe == c->total_frames ? c->last_frame_size :
145 131072;
146 13 return ret;
147 }
148
149 static int rka_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
150 {
151 RKAContext *c = s->priv_data;
152 AVStream *st = s->streams[stream_index];
153 int index = av_index_search_timestamp(st, timestamp, flags);
154 if (index < 0)
155 return -1;
156 if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
157 return -1;
158
159 c->currentframe = index;
160
161 return 0;
162 }
163
164 const FFInputFormat ff_rka_demuxer = {
165 .p.name = "rka",
166 .p.long_name = NULL_IF_CONFIG_SMALL("RKA (RK Audio)"),
167 .p.extensions = "rka",
168 .priv_data_size = sizeof(RKAContext),
169 .read_probe = rka_probe,
170 .read_header = rka_read_header,
171 .read_packet = rka_read_packet,
172 .read_seek = rka_read_seek,
173 };
174