3 * Copyright (c) 2015 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
26 typedef struct GENHDemuxContext {
27 unsigned dsp_int_type;
28 unsigned interleave_size;
31 static int genh_probe(AVProbeData *p)
33 if (AV_RL32(p->buf) != MKTAG('G','E','N','H'))
36 return AVPROBE_SCORE_MAX / 3 * 2;
39 static int genh_read_header(AVFormatContext *s)
41 unsigned start_offset, header_size, codec, coef_type, coef[2];
42 GENHDemuxContext *c = s->priv_data;
43 unsigned coef_splitted[2];
49 st = avformat_new_stream(s, NULL);
51 return AVERROR(ENOMEM);
53 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
54 st->codec->channels = avio_rl32(s->pb);
55 if (st->codec->channels <= 0)
56 return AVERROR_INVALIDDATA;
57 if (st->codec->channels == 1)
58 st->codec->channel_layout = AV_CH_LAYOUT_MONO;
59 else if (st->codec->channels == 2)
60 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
62 c->interleave_size = avio_rl32(s->pb);
63 if (align < 0 || align > INT_MAX / st->codec->channels)
64 return AVERROR_INVALIDDATA;
65 st->codec->block_align = align * st->codec->channels;
66 st->codec->sample_rate = avio_rl32(s->pb);
68 st->duration = avio_rl32(s->pb);
70 codec = avio_rl32(s->pb);
72 case 0: st->codec->codec_id = AV_CODEC_ID_ADPCM_PSX; break;
74 case 11: st->codec->bits_per_coded_sample = 4;
75 st->codec->block_align = 36 * st->codec->channels;
76 st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_WAV; break;
77 case 2: st->codec->codec_id = AV_CODEC_ID_ADPCM_DTK; break;
78 case 3: st->codec->codec_id = st->codec->block_align > 0 ?
79 AV_CODEC_ID_PCM_S16BE_PLANAR :
80 AV_CODEC_ID_PCM_S16BE; break;
81 case 4: st->codec->codec_id = st->codec->block_align > 0 ?
82 AV_CODEC_ID_PCM_S16LE_PLANAR :
83 AV_CODEC_ID_PCM_S16LE; break;
84 case 5: st->codec->codec_id = st->codec->block_align > 0 ?
85 AV_CODEC_ID_PCM_S8_PLANAR :
86 AV_CODEC_ID_PCM_S8; break;
87 case 12: st->codec->codec_id = AV_CODEC_ID_ADPCM_THP; break;
88 case 13: st->codec->codec_id = AV_CODEC_ID_PCM_U8; break;
89 case 17: st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_QT; break;
91 avpriv_request_sample(s, "codec %d", codec);
92 return AVERROR_PATCHWELCOME;
95 start_offset = avio_rl32(s->pb);
96 header_size = avio_rl32(s->pb);
98 if (header_size > start_offset)
99 return AVERROR_INVALIDDATA;
101 if (header_size == 0)
102 start_offset = 0x800;
104 coef[0] = avio_rl32(s->pb);
105 coef[1] = avio_rl32(s->pb);
106 c->dsp_int_type = avio_rl32(s->pb);
107 coef_type = avio_rl32(s->pb);
108 coef_splitted[0] = avio_rl32(s->pb);
109 coef_splitted[1] = avio_rl32(s->pb);
111 if (st->codec->codec_id == AV_CODEC_ID_ADPCM_THP) {
112 if (st->codec->channels > 2) {
113 avpriv_request_sample(s, "channels %d>2", st->codec->channels);
114 return AVERROR_PATCHWELCOME;
117 ff_alloc_extradata(st->codec, 32 * st->codec->channels);
118 for (ch = 0; ch < st->codec->channels; ch++) {
120 avpriv_request_sample(s, "coef_type & 1");
121 return AVERROR_PATCHWELCOME;
123 avio_seek(s->pb, coef[ch], SEEK_SET);
124 avio_read(s->pb, st->codec->extradata + 32 * ch, 32);
128 if (c->dsp_int_type == 1) {
129 st->codec->block_align = 8 * st->codec->channels;
130 if (c->interleave_size != 1 &&
131 c->interleave_size != 2 &&
132 c->interleave_size != 4)
133 return AVERROR_INVALIDDATA;
137 avio_skip(s->pb, start_offset - avio_tell(s->pb));
139 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
144 static int genh_read_packet(AVFormatContext *s, AVPacket *pkt)
146 AVCodecContext *codec = s->streams[0]->codec;
147 GENHDemuxContext *c = s->priv_data;
150 if (c->dsp_int_type == 1 && codec->codec_id == AV_CODEC_ID_ADPCM_THP &&
151 codec->channels > 1) {
154 if (avio_feof(s->pb))
156 ret = av_new_packet(pkt, 8 * codec->channels);
159 for (i = 0; i < 8 / c->interleave_size; i++) {
160 for (ch = 0; ch < codec->channels; ch++) {
161 pkt->data[ch * 8 + i*c->interleave_size+0] = avio_r8(s->pb);
162 pkt->data[ch * 8 + i*c->interleave_size+1] = avio_r8(s->pb);
167 ret = av_get_packet(s->pb, pkt, codec->block_align ? codec->block_align : 1024 * codec->channels);
170 pkt->stream_index = 0;
174 AVInputFormat ff_genh_demuxer = {
176 .long_name = NULL_IF_CONFIG_SMALL("GENeric Header"),
177 .priv_data_size = sizeof(GENHDemuxContext),
178 .read_probe = genh_probe,
179 .read_header = genh_read_header,
180 .read_packet = genh_read_packet,
181 .extensions = "genh",