Coverage Report

Created: 2024-09-06 07:53

/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
358k
{
35
358k
    if (AV_RL32(p->buf) != MKTAG('G','E','N','H'))
36
358k
        return 0;
37
265
    if (AV_RL32(p->buf+4) <= 0 || AV_RL32(p->buf+4) > 0xFFFF) // channels
38
155
        return 0;
39
40
110
    return AVPROBE_SCORE_MAX / 3 * 2;
41
265
}
42
43
static int genh_read_header(AVFormatContext *s)
44
5.89k
{
45
5.89k
    unsigned start_offset, header_size, codec, coef_type, coef[2];
46
5.89k
    GENHDemuxContext *c = s->priv_data;
47
5.89k
    av_unused unsigned coef_splitted[2];
48
5.89k
    int align, ch, ret;
49
5.89k
    AVStream *st;
50
51
5.89k
    avio_skip(s->pb, 4);
52
53
5.89k
    st = avformat_new_stream(s, NULL);
54
5.89k
    if (!st)
55
0
        return AVERROR(ENOMEM);
56
57
5.89k
    st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
58
5.89k
    st->codecpar->ch_layout.nb_channels    = avio_rl32(s->pb);
59
5.89k
    if (st->codecpar->ch_layout.nb_channels <= 0)
60
3.99k
        return AVERROR_INVALIDDATA;
61
1.90k
    if (st->codecpar->ch_layout.nb_channels == 1)
62
688
        st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
63
1.21k
    else if (st->codecpar->ch_layout.nb_channels == 2)
64
172
        st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
65
1.90k
    align                  =
66
1.90k
    c->interleave_size     = avio_rl32(s->pb);
67
1.90k
    if (align < 0 || align > INT_MAX / st->codecpar->ch_layout.nb_channels)
68
177
        return AVERROR_INVALIDDATA;
69
1.72k
    st->codecpar->block_align = align * st->codecpar->ch_layout.nb_channels;
70
1.72k
    st->codecpar->sample_rate = avio_rl32(s->pb);
71
1.72k
    if (st->codecpar->sample_rate < 0)
72
31
        return AVERROR_INVALIDDATA;
73
74
1.69k
    avio_skip(s->pb, 4);
75
1.69k
    st->duration = avio_rl32(s->pb);
76
77
1.69k
    codec = avio_rl32(s->pb);
78
1.69k
    switch (codec) {
79
797
    case  0: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_PSX;        break;
80
73
    case  1:
81
86
    case 11: st->codecpar->bits_per_coded_sample = 4;
82
86
             if (st->codecpar->ch_layout.nb_channels > INT_MAX / 36)
83
12
                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
20
    case  2: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_DTK;        break;
87
59
    case  3: st->codecpar->codec_id = st->codecpar->block_align > 0 ?
88
58
                                   AV_CODEC_ID_PCM_S16BE_PLANAR :
89
59
                                   AV_CODEC_ID_PCM_S16BE;        break;
90
62
    case  4: st->codecpar->codec_id = st->codecpar->block_align > 0 ?
91
62
                                   AV_CODEC_ID_PCM_S16LE_PLANAR :
92
62
                                   AV_CODEC_ID_PCM_S16LE;        break;
93
74
    case  5: st->codecpar->codec_id = st->codecpar->block_align > 0 ?
94
74
                                   AV_CODEC_ID_PCM_S8_PLANAR :
95
74
                                   AV_CODEC_ID_PCM_S8;           break;
96
49
    case  6: if (st->codecpar->block_align > INT_MAX/1024)
97
14
                 return AVERROR_INVALIDDATA;
98
35
             st->codecpar->codec_id = AV_CODEC_ID_SDX2_DPCM;        break;
99
175
    case  7: ret = ff_alloc_extradata(st->codecpar, 2);
100
175
             if (ret < 0)
101
0
                 return ret;
102
175
             AV_WL16(st->codecpar->extradata, 3);
103
175
             st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_WS;     break;
104
32
    case 10: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_AICA;       break;
105
247
    case 12: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_THP;        break;
106
40
    case 13: st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;           break;
107
49
    case 17: st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_QT;     break;
108
4
    default:
109
4
             avpriv_request_sample(s, "codec %d", codec);
110
4
             return AVERROR_PATCHWELCOME;
111
1.69k
    }
112
113
1.66k
    start_offset = avio_rl32(s->pb);
114
1.66k
    header_size  = avio_rl32(s->pb);
115
116
1.66k
    if (header_size > start_offset)
117
6
        return AVERROR_INVALIDDATA;
118
119
1.65k
    if (header_size == 0)
120
815
        start_offset = 0x800;
121
122
1.65k
    coef[0]          = avio_rl32(s->pb);
123
1.65k
    coef[1]          = avio_rl32(s->pb);
124
1.65k
    c->dsp_int_type  = avio_rl32(s->pb);
125
1.65k
    coef_type        = avio_rl32(s->pb);
126
1.65k
    coef_splitted[0] = avio_rl32(s->pb);
127
1.65k
    coef_splitted[1] = avio_rl32(s->pb);
128
129
1.65k
    if (st->codecpar->codec_id == AV_CODEC_ID_ADPCM_THP) {
130
247
        if (st->codecpar->ch_layout.nb_channels > 2) {
131
32
            avpriv_request_sample(s, "channels %d>2", st->codecpar->ch_layout.nb_channels);
132
32
            return AVERROR_PATCHWELCOME;
133
32
        }
134
135
215
        ff_alloc_extradata(st->codecpar, 32 * st->codecpar->ch_layout.nb_channels);
136
523
        for (ch = 0; ch < st->codecpar->ch_layout.nb_channels; ch++) {
137
311
            if (coef_type & 1) {
138
3
                avpriv_request_sample(s, "coef_type & 1");
139
3
                return AVERROR_PATCHWELCOME;
140
308
            } else {
141
308
                avio_seek(s->pb, coef[ch], SEEK_SET);
142
308
                avio_read(s->pb, st->codecpar->extradata + 32 * ch, 32);
143
308
            }
144
311
        }
145
146
212
        if (c->dsp_int_type == 1) {
147
70
            st->codecpar->block_align = 8 * st->codecpar->ch_layout.nb_channels;
148
70
            if (c->interleave_size != 1 &&
149
70
                c->interleave_size != 2 &&
150
70
                c->interleave_size != 4)
151
29
                return AVERROR_INVALIDDATA;
152
70
        }
153
212
    }
154
155
1.59k
    if (st->codecpar->block_align <= 0)
156
212
        return AVERROR_INVALIDDATA;
157
158
1.38k
    avio_skip(s->pb, start_offset - avio_tell(s->pb));
159
160
1.38k
    avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
161
162
1.38k
    return 0;
163
1.59k
}
164
165
static int genh_read_packet(AVFormatContext *s, AVPacket *pkt)
166
1.57M
{
167
1.57M
    AVCodecParameters *par = s->streams[0]->codecpar;
168
1.57M
    GENHDemuxContext *c = s->priv_data;
169
1.57M
    int ret;
170
171
1.57M
    if (c->dsp_int_type == 1 && par->codec_id == AV_CODEC_ID_ADPCM_THP &&
172
1.57M
        par->ch_layout.nb_channels > 1) {
173
760
        int i, ch;
174
175
760
        if (avio_feof(s->pb))
176
53
            return AVERROR_EOF;
177
707
        ret = av_new_packet(pkt, 8 * par->ch_layout.nb_channels);
178
707
        if (ret < 0)
179
0
            return ret;
180
2.63k
        for (i = 0; i < 8 / c->interleave_size; i++) {
181
5.79k
            for (ch = 0; ch < par->ch_layout.nb_channels; ch++) {
182
3.86k
                pkt->data[ch * 8 + i*c->interleave_size+0] = avio_r8(s->pb);
183
3.86k
                pkt->data[ch * 8 + i*c->interleave_size+1] = avio_r8(s->pb);
184
3.86k
            }
185
1.93k
        }
186
707
        ret = 0;
187
1.57M
    } else if (par->codec_id == AV_CODEC_ID_SDX2_DPCM) {
188
513
        ret = av_get_packet(s->pb, pkt, par->block_align * 1024);
189
190
1.57M
    } else {
191
1.57M
        ret = av_get_packet(s->pb, pkt, par->block_align ? par->block_align :
192
1.57M
                                                           1024 * par->ch_layout.nb_channels);
193
1.57M
    }
194
195
1.57M
    pkt->stream_index = 0;
196
1.57M
    return ret;
197
1.57M
}
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
};