FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/genh.c
Date: 2026-09-24 20:08:24
Exec Total Coverage
Lines: 3 126 2.4%
Functions: 1 3 33.3%
Branches: 1 91 1.1%

Line Branch Exec Source
1 /*
2 * GENH demuxer
3 * Copyright (c) 2015 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/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "avio_internal.h"
26 #include "demux.h"
27 #include "internal.h"
28
29 typedef struct GENHDemuxContext {
30 unsigned dsp_int_type;
31 unsigned interleave_size;
32 } GENHDemuxContext;
33
34 8064 static int genh_probe(const AVProbeData *p)
35 {
36
1/2
✓ Branch 0 taken 8064 times.
✗ Branch 1 not taken.
8064 if (AV_RL32(p->buf) != MKTAG('G','E','N','H'))
37 8064 return 0;
38 ✗ if (AV_RL32(p->buf+4) <= 0 || AV_RL32(p->buf+4) > 0xFFFF) // channels
39 ✗ return 0;
40
41 ✗ return AVPROBE_SCORE_MAX / 3 * 2;
42 }
43
44 ✗ static int genh_read_header(AVFormatContext *s)
45 {
46 unsigned start_offset, header_size, codec, coef_type, coef[2];
47 ✗ GENHDemuxContext *c = s->priv_data;
48 av_unused unsigned coef_splitted[2];
49 int align, ch, ret;
50 AVStream *st;
51
52 ✗ avio_skip(s->pb, 4);
53
54 ✗ st = avformat_new_stream(s, NULL);
55 ✗ if (!st)
56 ✗ return AVERROR(ENOMEM);
57
58 ✗ st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
59 ✗ st->codecpar->ch_layout.nb_channels = avio_rl32(s->pb);
60 ✗ if (st->codecpar->ch_layout.nb_channels <= 0)
61 ✗ return AVERROR_INVALIDDATA;
62 ✗ if (st->codecpar->ch_layout.nb_channels == 1)
63 ✗ st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
64 ✗ else if (st->codecpar->ch_layout.nb_channels == 2)
65 ✗ st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
66 ✗ align =
67 ✗ c->interleave_size = avio_rl32(s->pb);
68 ✗ if (align < 0 || align > INT_MAX / st->codecpar->ch_layout.nb_channels)
69 ✗ return AVERROR_INVALIDDATA;
70 ✗ st->codecpar->block_align = align * st->codecpar->ch_layout.nb_channels;
71 ✗ st->codecpar->sample_rate = avio_rl32(s->pb);
72 ✗ if (st->codecpar->sample_rate < 0)
73 ✗ return AVERROR_INVALIDDATA;
74
75 ✗ avio_skip(s->pb, 4);
76 ✗ st->duration = avio_rl32(s->pb);
77
78 ✗ codec = avio_rl32(s->pb);
79 ✗ switch (codec) {
80 ✗ case 0: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_PSX; break;
81 ✗ case 1:
82 ✗ case 11: st->codecpar->bits_per_coded_sample = 4;
83 ✗ if (st->codecpar->ch_layout.nb_channels > INT_MAX / 36)
84 ✗ return AVERROR_INVALIDDATA;
85 ✗ st->codecpar->block_align = 36 * st->codecpar->ch_layout.nb_channels;
86 ✗ st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_WAV; break;
87 ✗ case 2: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_DTK; break;
88 ✗ case 3: st->codecpar->codec_id = st->codecpar->block_align > 0 ?
89 ✗ AV_CODEC_ID_PCM_S16BE_PLANAR :
90 ✗ AV_CODEC_ID_PCM_S16BE; break;
91 ✗ case 4: st->codecpar->codec_id = st->codecpar->block_align > 0 ?
92 ✗ AV_CODEC_ID_PCM_S16LE_PLANAR :
93 ✗ AV_CODEC_ID_PCM_S16LE; break;
94 ✗ case 5: st->codecpar->codec_id = st->codecpar->block_align > 0 ?
95 ✗ AV_CODEC_ID_PCM_S8_PLANAR :
96 ✗ AV_CODEC_ID_PCM_S8; break;
97 ✗ case 6: if (st->codecpar->block_align > INT_MAX/1024)
98 ✗ return AVERROR_INVALIDDATA;
99 ✗ st->codecpar->codec_id = AV_CODEC_ID_SDX2_DPCM; break;
100 ✗ case 7: ret = ff_alloc_extradata(st->codecpar, 2);
101 ✗ if (ret < 0)
102 ✗ return ret;
103 ✗ AV_WL16(st->codecpar->extradata, 3);
104 ✗ st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_WS; break;
105 ✗ case 10: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_AICA; break;
106 ✗ case 12: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_THP; break;
107 ✗ case 13: st->codecpar->codec_id = AV_CODEC_ID_PCM_U8; break;
108 ✗ case 17: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_QT; break;
109 ✗ default:
110 ✗ avpriv_request_sample(s, "codec %d", codec);
111 ✗ return AVERROR_PATCHWELCOME;
112 }
113
114 ✗ start_offset = avio_rl32(s->pb);
115 ✗ header_size = avio_rl32(s->pb);
116
117 ✗ if (header_size > start_offset)
118 ✗ return AVERROR_INVALIDDATA;
119
120 ✗ if (header_size == 0)
121 ✗ start_offset = 0x800;
122
123 ✗ coef[0] = avio_rl32(s->pb);
124 ✗ coef[1] = avio_rl32(s->pb);
125 ✗ c->dsp_int_type = avio_rl32(s->pb);
126 ✗ coef_type = avio_rl32(s->pb);
127 ✗ coef_splitted[0] = avio_rl32(s->pb);
128 ✗ coef_splitted[1] = avio_rl32(s->pb);
129
130 ✗ if (st->codecpar->codec_id == AV_CODEC_ID_ADPCM_THP) {
131 ✗ if (st->codecpar->ch_layout.nb_channels > 2) {
132 ✗ avpriv_request_sample(s, "channels %d>2", st->codecpar->ch_layout.nb_channels);
133 ✗ return AVERROR_PATCHWELCOME;
134 }
135
136 ✗ ret = ff_alloc_extradata(st->codecpar, 32 * st->codecpar->ch_layout.nb_channels);
137 ✗ if (ret < 0)
138 ✗ return ret;
139 ✗ for (ch = 0; ch < st->codecpar->ch_layout.nb_channels; ch++) {
140 ✗ if (coef_type & 1) {
141 ✗ avpriv_request_sample(s, "coef_type & 1");
142 ✗ return AVERROR_PATCHWELCOME;
143 } else {
144 ✗ avio_seek(s->pb, coef[ch], SEEK_SET);
145 ✗ ret = ffio_read_size(s->pb, st->codecpar->extradata + 32 * ch, 32);
146 ✗ if (ret < 0)
147 ✗ return ret;
148 }
149 }
150
151 ✗ if (c->dsp_int_type == 1) {
152 ✗ st->codecpar->block_align = 8 * st->codecpar->ch_layout.nb_channels;
153 ✗ if (c->interleave_size != 1 &&
154 ✗ c->interleave_size != 2 &&
155 ✗ c->interleave_size != 4)
156 ✗ return AVERROR_INVALIDDATA;
157 }
158 }
159
160 ✗ if (st->codecpar->block_align <= 0)
161 ✗ return AVERROR_INVALIDDATA;
162
163 ✗ avio_skip(s->pb, start_offset - avio_tell(s->pb));
164
165 ✗ avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
166
167 ✗ return 0;
168 }
169
170 ✗ static int genh_read_packet(AVFormatContext *s, AVPacket *pkt)
171 {
172 ✗ AVCodecParameters *par = s->streams[0]->codecpar;
173 ✗ GENHDemuxContext *c = s->priv_data;
174 int ret;
175
176 ✗ if (c->dsp_int_type == 1 && par->codec_id == AV_CODEC_ID_ADPCM_THP &&
177 ✗ par->ch_layout.nb_channels > 1) {
178 int i, ch;
179
180 ✗ if (c->interleave_size != 2) {
181 ✗ avpriv_request_sample(s, "type1 THP interleave size %d", c->interleave_size);
182 ✗ return AVERROR_PATCHWELCOME;
183 }
184
185 ✗ if (avio_feof(s->pb))
186 ✗ return AVERROR_EOF;
187 ✗ ret = av_new_packet(pkt, 8 * par->ch_layout.nb_channels);
188 ✗ if (ret < 0)
189 ✗ return ret;
190 ✗ for (i = 0; i < 8 / c->interleave_size; i++) {
191 ✗ for (ch = 0; ch < par->ch_layout.nb_channels; ch++) {
192 ✗ pkt->data[ch * 8 + i*c->interleave_size+0] = avio_r8(s->pb);
193 ✗ pkt->data[ch * 8 + i*c->interleave_size+1] = avio_r8(s->pb);
194 }
195 }
196 ✗ ret = 0;
197 ✗ } else if (par->codec_id == AV_CODEC_ID_SDX2_DPCM) {
198 ✗ ret = av_get_packet(s->pb, pkt, par->block_align * 1024);
199
200 } else {
201 ✗ ret = av_get_packet(s->pb, pkt, par->block_align ? par->block_align :
202 ✗ 1024 * par->ch_layout.nb_channels);
203 }
204
205 ✗ pkt->stream_index = 0;
206 ✗ return ret;
207 }
208
209 const FFInputFormat ff_genh_demuxer = {
210 .p.name = "genh",
211 .p.long_name = NULL_IF_CONFIG_SMALL("GENeric Header"),
212 .p.extensions = "genh",
213 .priv_data_size = sizeof(GENHDemuxContext),
214 .read_probe = genh_probe,
215 .read_header = genh_read_header,
216 .read_packet = genh_read_packet,
217 };
218