]> git.sesse.net Git - ffmpeg/blob - libavformat/genh.c
Merge commit '8161220eee152dad8b2ea9e2755c78c8e127f747'
[ffmpeg] / libavformat / genh.c
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/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25
26 typedef struct GENHDemuxContext {
27     unsigned dsp_int_type;
28     unsigned interleave_size;
29 } GENHDemuxContext;
30
31 static int genh_probe(AVProbeData *p)
32 {
33     if (AV_RL32(p->buf) != MKTAG('G','E','N','H'))
34         return 0;
35
36     return AVPROBE_SCORE_MAX / 3 * 2;
37 }
38
39 static int genh_read_header(AVFormatContext *s)
40 {
41     unsigned start_offset, header_size, codec, coef_type, coef[2];
42     GENHDemuxContext *c = s->priv_data;
43     unsigned coef_splitted[2];
44     int align, ch, ret;
45     AVStream *st;
46
47     avio_skip(s->pb, 4);
48
49     st = avformat_new_stream(s, NULL);
50     if (!st)
51         return AVERROR(ENOMEM);
52
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;
61     align                  =
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);
67     avio_skip(s->pb, 4);
68     st->duration = avio_rl32(s->pb);
69
70     codec = avio_rl32(s->pb);
71     switch (codec) {
72     case  0: st->codec->codec_id = AV_CODEC_ID_ADPCM_PSX;        break;
73     case  1:
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  6: st->codec->codec_id = AV_CODEC_ID_SDX2_DPCM;        break;
88     case  7: ret = ff_alloc_extradata(st->codec, 2);
89              if (ret < 0)
90                  return ret;
91              AV_WL16(st->codec->extradata, 3);
92              st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_WS;     break;
93     case 10: st->codec->codec_id = AV_CODEC_ID_ADPCM_AICA;       break;
94     case 12: st->codec->codec_id = AV_CODEC_ID_ADPCM_THP;        break;
95     case 13: st->codec->codec_id = AV_CODEC_ID_PCM_U8;           break;
96     case 17: st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_QT;     break;
97     default:
98              avpriv_request_sample(s, "codec %d", codec);
99              return AVERROR_PATCHWELCOME;
100     }
101
102     start_offset = avio_rl32(s->pb);
103     header_size  = avio_rl32(s->pb);
104
105     if (header_size > start_offset)
106         return AVERROR_INVALIDDATA;
107
108     if (header_size == 0)
109         start_offset = 0x800;
110
111     coef[0]          = avio_rl32(s->pb);
112     coef[1]          = avio_rl32(s->pb);
113     c->dsp_int_type  = avio_rl32(s->pb);
114     coef_type        = avio_rl32(s->pb);
115     coef_splitted[0] = avio_rl32(s->pb);
116     coef_splitted[1] = avio_rl32(s->pb);
117
118     if (st->codec->codec_id == AV_CODEC_ID_ADPCM_THP) {
119         if (st->codec->channels > 2) {
120             avpriv_request_sample(s, "channels %d>2", st->codec->channels);
121             return AVERROR_PATCHWELCOME;
122         }
123
124         ff_alloc_extradata(st->codec, 32 * st->codec->channels);
125         for (ch = 0; ch < st->codec->channels; ch++) {
126             if (coef_type & 1) {
127                 avpriv_request_sample(s, "coef_type & 1");
128                 return AVERROR_PATCHWELCOME;
129             } else {
130                 avio_seek(s->pb, coef[ch], SEEK_SET);
131                 avio_read(s->pb, st->codec->extradata + 32 * ch, 32);
132             }
133         }
134
135         if (c->dsp_int_type == 1) {
136             st->codec->block_align = 8 * st->codec->channels;
137             if (c->interleave_size != 1 &&
138                 c->interleave_size != 2 &&
139                 c->interleave_size != 4)
140                 return AVERROR_INVALIDDATA;
141         }
142     }
143
144     avio_skip(s->pb, start_offset - avio_tell(s->pb));
145
146     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
147
148     return 0;
149 }
150
151 static int genh_read_packet(AVFormatContext *s, AVPacket *pkt)
152 {
153     AVCodecContext *codec = s->streams[0]->codec;
154     GENHDemuxContext *c = s->priv_data;
155     int ret;
156
157     if (c->dsp_int_type == 1 && codec->codec_id == AV_CODEC_ID_ADPCM_THP &&
158         codec->channels > 1) {
159         int i, ch;
160
161         if (avio_feof(s->pb))
162             return AVERROR_EOF;
163         ret = av_new_packet(pkt, 8 * codec->channels);
164         if (ret < 0)
165             return ret;
166         for (i = 0; i < 8 / c->interleave_size; i++) {
167             for (ch = 0; ch < codec->channels; ch++) {
168                 pkt->data[ch * 8 + i*c->interleave_size+0] = avio_r8(s->pb);
169                 pkt->data[ch * 8 + i*c->interleave_size+1] = avio_r8(s->pb);
170             }
171         }
172         ret = 0;
173     } else if (codec->codec_id == AV_CODEC_ID_SDX2_DPCM) {
174         ret = av_get_packet(s->pb, pkt, codec->block_align * 1024);
175
176     } else {
177         ret = av_get_packet(s->pb, pkt, codec->block_align ? codec->block_align : 1024 * codec->channels);
178     }
179
180     pkt->stream_index = 0;
181     return ret;
182 }
183
184 AVInputFormat ff_genh_demuxer = {
185     .name           = "genh",
186     .long_name      = NULL_IF_CONFIG_SMALL("GENeric Header"),
187     .priv_data_size = sizeof(GENHDemuxContext),
188     .read_probe     = genh_probe,
189     .read_header    = genh_read_header,
190     .read_packet    = genh_read_packet,
191     .extensions     = "genh",
192 };