FFmpeg
Loading...
Searching...
No Matches
genh.c
Go to the documentation of this file.
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
24#include "avformat.h"
25#include "avio_internal.h"
26#include "demux.h"
27#include "internal.h"
28
29typedef struct GENHDemuxContext {
30 unsigned dsp_int_type;
33
34static int genh_probe(const AVProbeData *p)
35{
36 if (AV_RL32(p->buf) != MKTAG('G','E','N','H'))
37 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
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
55 if (!st)
56 return AVERROR(ENOMEM);
57
60 if (st->codecpar->ch_layout.nb_channels <= 0)
62 if (st->codecpar->ch_layout.nb_channels == 1)
64 else if (st->codecpar->ch_layout.nb_channels == 2)
66 align =
67 c->interleave_size = avio_rl32(s->pb);
71 st->codecpar->sample_rate = avio_rl32(s->pb);
72 if (st->codecpar->sample_rate < 0)
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)
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 ?
91 case 4: st->codecpar->codec_id = st->codecpar->block_align > 0 ?
94 case 5: st->codecpar->codec_id = st->codecpar->block_align > 0 ?
96 AV_CODEC_ID_PCM_S8; break;
97 case 6: if (st->codecpar->block_align > INT_MAX/1024)
100 case 7: ret = ff_alloc_extradata(st->codecpar, 2);
101 if (ret < 0)
102 return ret;
103 AV_WL16(st->codecpar->extradata, 3);
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);
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
131 if (st->codecpar->ch_layout.nb_channels > 2) {
132 avpriv_request_sample(s, "channels %d>2", st->codecpar->ch_layout.nb_channels);
134 }
135
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");
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) {
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
166
167 return 0;
168}
169
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
205 .p.name = "genh",
206 .p.long_name = NULL_IF_CONFIG_SMALL("GENeric Header"),
207 .p.extensions = "genh",
208 .priv_data_size = sizeof(GENHDemuxContext),
212};
const FFInputFormat ff_genh_demuxer
Definition genh.c:204
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static const uint8_t *BS_FUNC align(BSCTX *bc)
Skip bits to a byte boundary.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
static int genh_probe(const AVProbeData *p)
Definition genh.c:34
static int genh_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition genh.c:170
static int genh_read_header(AVFormatContext *s)
Definition genh.c:44
@ AV_CODEC_ID_ADPCM_IMA_WS
Definition codec_id.h:374
@ AV_CODEC_ID_PCM_S16BE_PLANAR
Definition codec_id.h:360
@ AV_CODEC_ID_PCM_U8
Definition codec_id.h:335
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_CODEC_ID_ADPCM_DTK
Definition codec_id.h:403
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:331
@ AV_CODEC_ID_ADPCM_PSX
Definition codec_id.h:407
@ AV_CODEC_ID_SDX2_DPCM
Definition codec_id.h:446
@ AV_CODEC_ID_PCM_S8
Definition codec_id.h:334
@ AV_CODEC_ID_ADPCM_IMA_QT
Definition codec_id.h:370
@ AV_CODEC_ID_ADPCM_AICA
Definition codec_id.h:408
@ AV_CODEC_ID_ADPCM_THP
Definition codec_id.h:388
@ AV_CODEC_ID_ADPCM_IMA_WAV
Definition codec_id.h:371
@ AV_CODEC_ID_PCM_S16LE_PLANAR
Definition codec_id.h:348
@ AV_CODEC_ID_PCM_S8_PLANAR
Definition codec_id.h:357
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
#define AV_RL32(p)
#define AV_WL16(p, v)
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define av_unused
Definition attributes.h:164
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define MKTAG(a, b, c, d)
Definition macros.h:55
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
unsigned dsp_int_type
Definition genh.c:30
unsigned interleave_size
Definition genh.c:31
#define avpriv_request_sample(...)
static double c[64]