summaryrefslogtreecommitdiff
path: root/libavformat/rka.c
blob: 39e5b3bce127d4b53391ef853c30a793b6ba4850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * RKA demuxer
 * Copyright (c) 2023 Paul B Mahol
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libavutil/dict.h"
#include "libavutil/intreadwrite.h"

#include "apetag.h"
#include "avformat.h"
#include "avio_internal.h"
#include "demux.h"
#include "internal.h"

typedef struct RKAContext {
    int total_frames, currentframe;
    int frame_size;
    int last_frame_size;
} RKAContext;

static int rka_probe(const AVProbeData *p)
{
    if (AV_RL32(&p->buf[0]) == MKTAG('R', 'K', 'A', '7') &&
        AV_RL32(&p->buf[4]) > 0 &&
        AV_RL32(&p->buf[8]) > 0 &&
        p->buf[12] > 0 &&
        p->buf[12] <= 2 &&
        (p->buf[13] == 8 || p->buf[13] == 16) &&
        (p->buf[15] & 2) != 0)
        return AVPROBE_SCORE_EXTENSION + 30;
    return 0;
}

static int rka_read_header(AVFormatContext *s)
{
    int64_t nb_samples, size_offset;
    RKAContext *c = s->priv_data;
    int channels, bps, samplerate;
    AVCodecParameters *par;
    int64_t framepos;
    AVStream *st;
    int ret;

    st = avformat_new_stream(s, NULL);
    if (!st)
        return AVERROR(ENOMEM);

    par = st->codecpar;
    ret = ff_get_extradata(s, par, s->pb, 16);
    if (ret < 0)
        return ret;

    nb_samples = AV_RL32(par->extradata + 4);
    samplerate = AV_RL32(par->extradata + 8);
    channels = par->extradata[12];
    if (channels == 0)
        return AVERROR_INVALIDDATA;
    bps = par->extradata[13];
    if (bps == 0)
        return AVERROR_INVALIDDATA;
    size_offset = avio_rl32(s->pb);
    framepos = avio_tell(s->pb);
    c->frame_size = 131072;

    avpriv_set_pts_info(st, 64, 1, samplerate);
    st->start_time = 0;

    avio_seek(s->pb, size_offset, SEEK_SET);
    c->total_frames = (nb_samples + c->frame_size - 1) / c->frame_size;
    c->last_frame_size = nb_samples % c->frame_size;

    for (int i = 0; i < c->total_frames; i++) {
        int r, end = 0;
        int64_t size;

        if (avio_feof(s->pb))
            break;

        size = avio_rl24(s->pb);
        if (size == 0) {
            end = 1;
            size = size_offset - framepos;
            if (size <= 0)
                break;
        }

        if ((r = av_add_index_entry(st, framepos, (i * 131072LL) / (channels * (bps >> 3)),
                                    size, 0, AVINDEX_KEYFRAME)) < 0)
            return r;
        framepos += size;

        if (end)
            break;
    }

    par->codec_type = AVMEDIA_TYPE_AUDIO;
    par->codec_id = AV_CODEC_ID_RKA;
    par->ch_layout.nb_channels = channels;
    par->sample_rate = samplerate;
    par->bits_per_raw_sample = bps;
    st->duration = 8LL*nb_samples / (channels * bps);

    if (s->pb->seekable & AVIO_SEEKABLE_NORMAL)
        ff_ape_parse_tag(s);

    avio_seek(s->pb, 20, SEEK_SET);

    return 0;
}

static int rka_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    RKAContext *c = s->priv_data;
    AVStream *st = s->streams[0];
    FFStream *const sti = ffstream(st);
    int size, ret;

    if (avio_feof(s->pb))
        return AVERROR_EOF;

    if (c->currentframe >= sti->nb_index_entries)
        return AVERROR_EOF;

    size = sti->index_entries[c->currentframe].size;

    ret = av_get_packet(s->pb, pkt, size);
    pkt->dts = sti->index_entries[c->currentframe++].timestamp;
    pkt->duration = c->currentframe == c->total_frames ? c->last_frame_size :
                                                         131072;
    return ret;
}

static int rka_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
{
    RKAContext *c = s->priv_data;
    AVStream *st = s->streams[stream_index];
    int index = av_index_search_timestamp(st, timestamp, flags);
    if (index < 0)
        return -1;
    if (avio_seek(s->pb, ffstream(st)->index_entries[index].pos, SEEK_SET) < 0)
        return -1;

    c->currentframe = index;

    return 0;
}

const AVInputFormat ff_rka_demuxer = {
    .name           = "rka",
    .long_name      = NULL_IF_CONFIG_SMALL("RKA (RK Audio)"),
    .priv_data_size = sizeof(RKAContext),
    .read_probe     = rka_probe,
    .read_header    = rka_read_header,
    .read_packet    = rka_read_packet,
    .read_seek      = rka_read_seek,
    .extensions     = "rka",
};