Coverage Report

Created: 2025-08-28 07:12

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