| 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 | 7934 | static int genh_probe(const AVProbeData *p) | |
| 35 | { | ||
| 36 |
1/2✓ Branch 0 taken 7934 times.
✗ Branch 1 not taken.
|
7934 | if (AV_RL32(p->buf) != MKTAG('G','E','N','H')) |
| 37 | 7934 | 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 (avio_feof(s->pb)) | |
| 181 | ✗ | return AVERROR_EOF; | |
| 182 | ✗ | ret = av_new_packet(pkt, 8 * par->ch_layout.nb_channels); | |
| 183 | ✗ | if (ret < 0) | |
| 184 | ✗ | return ret; | |
| 185 | ✗ | for (i = 0; i < 8 / c->interleave_size; i++) { | |
| 186 | ✗ | for (ch = 0; ch < par->ch_layout.nb_channels; ch++) { | |
| 187 | ✗ | pkt->data[ch * 8 + i*c->interleave_size+0] = avio_r8(s->pb); | |
| 188 | ✗ | pkt->data[ch * 8 + i*c->interleave_size+1] = avio_r8(s->pb); | |
| 189 | } | ||
| 190 | } | ||
| 191 | ✗ | ret = 0; | |
| 192 | ✗ | } else if (par->codec_id == AV_CODEC_ID_SDX2_DPCM) { | |
| 193 | ✗ | ret = av_get_packet(s->pb, pkt, par->block_align * 1024); | |
| 194 | |||
| 195 | } else { | ||
| 196 | ✗ | ret = av_get_packet(s->pb, pkt, par->block_align ? par->block_align : | |
| 197 | ✗ | 1024 * par->ch_layout.nb_channels); | |
| 198 | } | ||
| 199 | |||
| 200 | ✗ | pkt->stream_index = 0; | |
| 201 | ✗ | return ret; | |
| 202 | } | ||
| 203 | |||
| 204 | const FFInputFormat ff_genh_demuxer = { | ||
| 205 | .p.name = "genh", | ||
| 206 | .p.long_name = NULL_IF_CONFIG_SMALL("GENeric Header"), | ||
| 207 | .p.extensions = "genh", | ||
| 208 | .priv_data_size = sizeof(GENHDemuxContext), | ||
| 209 | .read_probe = genh_probe, | ||
| 210 | .read_header = genh_read_header, | ||
| 211 | .read_packet = genh_read_packet, | ||
| 212 | }; | ||
| 213 |