FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/genh.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 3 119 2.5%
Functions: 1 3 33.3%
Branches: 1 85 1.2%

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