FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/hca.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 3 58 5.2%
Functions: 1 3 33.3%
Branches: 1 20 5.0%

Line Branch Exec 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 #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 7480 static int hca_probe(const AVProbeData *p)
41 {
42
1/2
✓ Branch 0 taken 7480 times.
✗ Branch 1 not taken.
7480 if ((AV_RL32(p->buf) & HCA_MASK) != MKTAG('H', 'C', 'A', 0))
43 7480 return 0;
44
45 if ((AV_RL32(p->buf + 8) & HCA_MASK) != MKTAG('f', 'm', 't', 0))
46 return 0;
47
48 return AVPROBE_SCORE_MAX / 3;
49 }
50
51 static int hca_read_header(AVFormatContext *s)
52 {
53 HCADemuxContext *hca = s->priv_data;
54 AVCodecParameters *par;
55 GetByteContext gb;
56 AVIOContext *pb = s->pb;
57 AVStream *st;
58 uint32_t chunk;
59 uint16_t version;
60 uint32_t block_count;
61 uint16_t block_size, data_offset;
62 int ret;
63
64 avio_skip(pb, 4);
65 version = avio_rb16(pb);
66
67 data_offset = avio_rb16(pb);
68 if (data_offset <= 8)
69 return AVERROR_INVALIDDATA;
70
71 st = avformat_new_stream(s, NULL);
72 if (!st)
73 return AVERROR(ENOMEM);
74
75 par = st->codecpar;
76 ret = ff_alloc_extradata(par, data_offset + 10);
77 if (ret < 0)
78 return ret;
79
80 ret = ffio_read_size(pb, par->extradata + 8, par->extradata_size - 8 - 10);
81 if (ret < 0)
82 return AVERROR_INVALIDDATA;
83 AV_WL32(par->extradata, MKTAG('H', 'C', 'A', 0));
84 AV_WB16(par->extradata + 4, version);
85 AV_WB16(par->extradata + 6, data_offset);
86 AV_WB32(par->extradata + par->extradata_size - 10, hca->keyh);
87 AV_WB32(par->extradata + par->extradata_size - 6, hca->keyl);
88 AV_WB16(par->extradata + par->extradata_size - 2, hca->subkey);
89
90 bytestream2_init(&gb, par->extradata + 8, par->extradata_size - 8);
91
92 if ((bytestream2_get_le32(&gb) & HCA_MASK) != MKTAG('f', 'm', 't', 0))
93 return AVERROR_INVALIDDATA;
94
95 par->codec_type = AVMEDIA_TYPE_AUDIO;
96 par->codec_id = AV_CODEC_ID_HCA;
97 par->codec_tag = 0;
98 st->codecpar->ch_layout.nb_channels = bytestream2_get_byte(&gb);
99 par->sample_rate = bytestream2_get_be24(&gb);
100 block_count = bytestream2_get_be32(&gb);
101 bytestream2_skip(&gb, 4);
102 chunk = bytestream2_get_le32(&gb) & HCA_MASK;
103 if (chunk == MKTAG('c', 'o', 'm', 'p')) {
104 block_size = bytestream2_get_be16(&gb);
105 } else if (chunk == MKTAG('d', 'e', 'c', 0)) {
106 block_size = bytestream2_get_be16(&gb);
107 } else {
108 return AVERROR_INVALIDDATA;
109 }
110
111 if (block_size < 8)
112 return AVERROR_INVALIDDATA;
113 par->block_align = block_size;
114 st->duration = 1024 * block_count;
115
116 avio_seek(pb, data_offset, SEEK_SET);
117 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
118
119 return 0;
120 }
121
122 static int hca_read_packet(AVFormatContext *s, AVPacket *pkt)
123 {
124 AVCodecParameters *par = s->streams[0]->codecpar;
125 int ret;
126
127 ret = av_get_packet(s->pb, pkt, par->block_align);
128 pkt->duration = 1024;
129 return ret;
130 }
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 };
164