Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/rka.c
Line
Count
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
static int rka_probe(const AVProbeData *p)
38
967k
{
39
967k
    if (AV_RL32(&p->buf[0]) == MKTAG('R', 'K', 'A', '7') &&
40
1.68k
        AV_RL32(&p->buf[4]) > 0 &&
41
1.50k
        AV_RL32(&p->buf[8]) > 0 &&
42
1.32k
        p->buf[12] > 0 &&
43
847
        p->buf[12] <= 2 &&
44
622
        (p->buf[13] == 8 || p->buf[13] == 16) &&
45
443
        (p->buf[15] & 2) != 0)
46
205
        return AVPROBE_SCORE_EXTENSION + 30;
47
967k
    return 0;
48
967k
}
49
50
static int rka_read_header(AVFormatContext *s)
51
2.72k
{
52
2.72k
    int64_t nb_samples, size_offset;
53
2.72k
    RKAContext *c = s->priv_data;
54
2.72k
    int channels, bps, samplerate;
55
2.72k
    AVCodecParameters *par;
56
2.72k
    int64_t framepos;
57
2.72k
    AVStream *st;
58
2.72k
    int ret;
59
60
2.72k
    st = avformat_new_stream(s, NULL);
61
2.72k
    if (!st)
62
0
        return AVERROR(ENOMEM);
63
64
2.72k
    par = st->codecpar;
65
2.72k
    ret = ff_get_extradata(s, par, s->pb, 16);
66
2.72k
    if (ret < 0)
67
114
        return ret;
68
69
2.61k
    nb_samples = AV_RL32(par->extradata + 4);
70
2.61k
    samplerate = AV_RL32(par->extradata + 8);
71
2.61k
    channels = par->extradata[12];
72
2.61k
    if (channels == 0)
73
3
        return AVERROR_INVALIDDATA;
74
2.60k
    bps = par->extradata[13];
75
2.60k
    if (bps < 8)
76
8
        return AVERROR_INVALIDDATA;
77
2.60k
    size_offset = avio_rl32(s->pb);
78
2.60k
    framepos = avio_tell(s->pb);
79
2.60k
    c->frame_size = 131072;
80
81
2.60k
    avpriv_set_pts_info(st, 64, 1, samplerate);
82
2.60k
    st->start_time = 0;
83
84
2.60k
    avio_seek(s->pb, size_offset, SEEK_SET);
85
2.60k
    c->total_frames = (nb_samples + c->frame_size - 1) / c->frame_size;
86
2.60k
    c->last_frame_size = nb_samples % c->frame_size;
87
88
582k
    for (int i = 0; i < c->total_frames; i++) {
89
580k
        int r, end = 0;
90
580k
        int64_t size;
91
92
580k
        if (avio_feof(s->pb))
93
513
            break;
94
95
580k
        size = avio_rl24(s->pb);
96
580k
        if (size == 0) {
97
884
            end = 1;
98
884
            size = size_offset - framepos;
99
884
            if (size <= 0)
100
643
                break;
101
884
        }
102
103
579k
        if ((r = av_add_index_entry(st, framepos, (i * 131072LL) / (channels * (bps >> 3)),
104
579k
                                    size, 0, AVINDEX_KEYFRAME)) < 0)
105
37
            return r;
106
579k
        framepos += size;
107
108
579k
        if (end)
109
204
            break;
110
579k
    }
111
112
2.56k
    par->codec_type = AVMEDIA_TYPE_AUDIO;
113
2.56k
    par->codec_id = AV_CODEC_ID_RKA;
114
2.56k
    par->ch_layout.nb_channels = channels;
115
2.56k
    par->sample_rate = samplerate;
116
2.56k
    par->bits_per_raw_sample = bps;
117
2.56k
    st->duration = 8LL*nb_samples / (channels * bps);
118
119
2.56k
    if (s->pb->seekable & AVIO_SEEKABLE_NORMAL)
120
1.71k
        ff_ape_parse_tag(s);
121
122
2.56k
    avio_seek(s->pb, 20, SEEK_SET);
123
124
2.56k
    return 0;
125
2.60k
}
126
127
static int rka_read_packet(AVFormatContext *s, AVPacket *pkt)
128
227k
{
129
227k
    RKAContext *c = s->priv_data;
130
227k
    AVStream *st = s->streams[0];
131
227k
    FFStream *const sti = ffstream(st);
132
227k
    int size, ret;
133
134
227k
    if (avio_feof(s->pb))
135
2.80k
        return AVERROR_EOF;
136
137
224k
    if (c->currentframe >= sti->nb_index_entries)
138
432
        return AVERROR_EOF;
139
140
224k
    size = sti->index_entries[c->currentframe].size;
141
142
224k
    ret = av_get_packet(s->pb, pkt, size);
143
224k
    pkt->dts = sti->index_entries[c->currentframe++].timestamp;
144
224k
    pkt->duration = c->currentframe == c->total_frames ? c->last_frame_size :
145
224k
                                                         131072;
146
224k
    return ret;
147
224k
}
148
149
static int rka_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
150
0
{
151
0
    RKAContext *c = s->priv_data;
152
0
    AVStream *st = s->streams[stream_index];
153
0
    int index = av_index_search_timestamp(st, timestamp, flags);
154
0
    if (index < 0)
155
0
        return -1;
156
0
    if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
157
0
        return -1;
158
159
0
    c->currentframe = index;
160
161
0
    return 0;
162
0
}
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
};