Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/hca.c
Line
Count
Source
1
/*
2
 * HCA demuxer
3
 * Copyright (c) 2020 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/opt.h"
23
#include "libavutil/intreadwrite.h"
24
#include "libavcodec/bytestream.h"
25
26
#include "avformat.h"
27
#include "avio_internal.h"
28
#include "demux.h"
29
#include "internal.h"
30
31
938k
#define HCA_MASK 0x7f7f7f7f
32
33
typedef struct HCADemuxContext {
34
    AVClass *class;
35
    int64_t keyl;
36
    int64_t keyh;
37
    int subkey;
38
} HCADemuxContext;
39
40
static int hca_probe(const AVProbeData *p)
41
936k
{
42
936k
    if ((AV_RL32(p->buf) & HCA_MASK) != MKTAG('H', 'C', 'A', 0))
43
936k
        return 0;
44
45
159
    if ((AV_RL32(p->buf + 8) & HCA_MASK) != MKTAG('f', 'm', 't', 0))
46
153
        return 0;
47
48
6
    return AVPROBE_SCORE_MAX / 3;
49
159
}
50
51
static int hca_read_header(AVFormatContext *s)
52
1.18k
{
53
1.18k
    HCADemuxContext *hca = s->priv_data;
54
1.18k
    AVCodecParameters *par;
55
1.18k
    GetByteContext gb;
56
1.18k
    AVIOContext *pb = s->pb;
57
1.18k
    AVStream *st;
58
1.18k
    uint32_t chunk;
59
1.18k
    uint16_t version;
60
1.18k
    uint32_t block_count;
61
1.18k
    uint16_t block_size, data_offset;
62
1.18k
    int ret;
63
64
1.18k
    avio_skip(pb, 4);
65
1.18k
    version = avio_rb16(pb);
66
67
1.18k
    data_offset = avio_rb16(pb);
68
1.18k
    if (data_offset <= 8)
69
122
        return AVERROR_INVALIDDATA;
70
71
1.06k
    st = avformat_new_stream(s, NULL);
72
1.06k
    if (!st)
73
0
        return AVERROR(ENOMEM);
74
75
1.06k
    par = st->codecpar;
76
1.06k
    ret = ff_alloc_extradata(par, data_offset + 10);
77
1.06k
    if (ret < 0)
78
0
        return ret;
79
80
1.06k
    ret = ffio_read_size(pb, par->extradata + 8, par->extradata_size - 8 - 10);
81
1.06k
    if (ret < 0)
82
144
        return AVERROR_INVALIDDATA;
83
922
    AV_WL32(par->extradata, MKTAG('H', 'C', 'A', 0));
84
922
    AV_WB16(par->extradata + 4, version);
85
922
    AV_WB16(par->extradata + 6, data_offset);
86
922
    AV_WB32(par->extradata + par->extradata_size - 10, hca->keyh);
87
922
    AV_WB32(par->extradata + par->extradata_size -  6, hca->keyl);
88
922
    AV_WB16(par->extradata + par->extradata_size -  2, hca->subkey);
89
90
922
    bytestream2_init(&gb, par->extradata + 8, par->extradata_size - 8);
91
92
922
    if ((bytestream2_get_le32(&gb) & HCA_MASK) != MKTAG('f', 'm', 't', 0))
93
101
        return AVERROR_INVALIDDATA;
94
95
821
    par->codec_type  = AVMEDIA_TYPE_AUDIO;
96
821
    par->codec_id    = AV_CODEC_ID_HCA;
97
821
    par->codec_tag   = 0;
98
821
    st->codecpar->ch_layout.nb_channels = bytestream2_get_byte(&gb);
99
821
    par->sample_rate = bytestream2_get_be24(&gb);
100
821
    block_count      = bytestream2_get_be32(&gb);
101
821
    bytestream2_skip(&gb, 4);
102
821
    chunk = bytestream2_get_le32(&gb) & HCA_MASK;
103
821
    if (chunk == MKTAG('c', 'o', 'm', 'p')) {
104
25
        block_size = bytestream2_get_be16(&gb);
105
796
    } else if (chunk == MKTAG('d', 'e', 'c', 0)) {
106
702
        block_size = bytestream2_get_be16(&gb);
107
702
    } else {
108
94
        return AVERROR_INVALIDDATA;
109
94
    }
110
111
727
    if (block_size < 8)
112
6
        return AVERROR_INVALIDDATA;
113
721
    par->block_align = block_size;
114
721
    st->duration = 1024 * block_count;
115
116
721
    avio_seek(pb, data_offset, SEEK_SET);
117
721
    avpriv_set_pts_info(st, 64, 1, par->sample_rate);
118
119
721
    return 0;
120
727
}
121
122
static int hca_read_packet(AVFormatContext *s, AVPacket *pkt)
123
2.04M
{
124
2.04M
    AVCodecParameters *par = s->streams[0]->codecpar;
125
2.04M
    int ret;
126
127
2.04M
    ret = av_get_packet(s->pb, pkt, par->block_align);
128
2.04M
    pkt->duration = 1024;
129
2.04M
    return ret;
130
2.04M
}
131
132
#define OFFSET(x) offsetof(HCADemuxContext, x)
133
static const AVOption hca_options[] = {
134
    { "hca_lowkey",
135
        "Low key used for handling CRI HCA files", OFFSET(keyl),
136
        AV_OPT_TYPE_INT64, {.i64=0}, .min = 0, .max = UINT32_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM, },
137
    { "hca_highkey",
138
        "High key used for handling CRI HCA files", OFFSET(keyh),
139
        AV_OPT_TYPE_INT64, {.i64=0}, .min = 0, .max = UINT32_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM, },
140
    { "hca_subkey",
141
        "Subkey used for handling CRI HCA files", OFFSET(subkey),
142
        AV_OPT_TYPE_INT, {.i64=0}, .min = 0, .max = UINT16_MAX, .flags = AV_OPT_FLAG_DECODING_PARAM },
143
    { NULL },
144
};
145
146
static const AVClass hca_class = {
147
    .class_name = "hca",
148
    .item_name  = av_default_item_name,
149
    .option     = hca_options,
150
    .version    = LIBAVUTIL_VERSION_INT,
151
};
152
153
const FFInputFormat ff_hca_demuxer = {
154
    .p.name         = "hca",
155
    .p.long_name    = NULL_IF_CONFIG_SMALL("CRI HCA"),
156
    .p.priv_class   = &hca_class,
157
    .p.extensions   = "hca",
158
    .p.flags        = AVFMT_GENERIC_INDEX,
159
    .priv_data_size = sizeof(HCADemuxContext),
160
    .read_probe     = hca_probe,
161
    .read_header    = hca_read_header,
162
    .read_packet    = hca_read_packet,
163
};