FFmpeg
lafdec.c
Go to the documentation of this file.
1 /*
2  * Limitless Audio Format demuxer
3  * Copyright (c) 2022 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 "libavutil/mem.h"
24 #include "avformat.h"
25 #include "avio_internal.h"
26 #include "demux.h"
27 #include "internal.h"
28 
29 #define MAX_STREAMS 4096
30 
31 typedef struct StreamParams {
33  float horizontal;
34  float vertical;
35  int lfe;
36  int stored;
37 } StreamParams;
38 
39 typedef struct LAFContext {
40  uint8_t *data;
41  unsigned nb_stored;
42  unsigned stored_index;
43  unsigned index;
44  unsigned bpp;
45 
47 
49  uint8_t header[(MAX_STREAMS + 7) / 8];
50 } LAFContext;
51 
52 static int laf_probe(const AVProbeData *p)
53 {
54  if (memcmp(p->buf, "LIMITLESS", 9))
55  return 0;
56  if (memcmp(p->buf + 9, "HEAD", 4))
57  return 0;
58  return AVPROBE_SCORE_MAX;
59 }
60 
62 {
64  AVIOContext *pb = ctx->pb;
65  unsigned st_count, mode;
66  unsigned sample_rate;
67  int64_t duration;
68  int codec_id;
69  int quality;
70  int bpp;
71 
72  avio_skip(pb, 9);
73  if (avio_rb32(pb) != MKBETAG('H','E','A','D'))
74  return AVERROR_INVALIDDATA;
75 
76  quality = avio_r8(pb);
77  if (quality > 3)
78  return AVERROR_INVALIDDATA;
79  mode = avio_r8(pb);
80  if (mode > 1)
81  return AVERROR_INVALIDDATA;
82  st_count = avio_rl32(pb);
83  if (st_count == 0 || st_count > MAX_STREAMS)
84  return AVERROR_INVALIDDATA;
85 
86  for (int i = 0; i < st_count; i++) {
87  StreamParams *stp = &s->p[i];
88 
89  stp->vertical = av_int2float(avio_rl32(pb));
90  stp->horizontal = av_int2float(avio_rl32(pb));
91  stp->lfe = avio_r8(pb);
92  if (stp->lfe) {
94  } else if (stp->vertical == 0.f &&
95  stp->horizontal == 0.f) {
97  } else if (stp->vertical == 0.f &&
98  stp->horizontal == -30.f) {
100  } else if (stp->vertical == 0.f &&
101  stp->horizontal == 30.f) {
103  } else if (stp->vertical == 0.f &&
104  stp->horizontal == -110.f) {
106  } else if (stp->vertical == 0.f &&
107  stp->horizontal == 110.f) {
109  } else {
111  }
112  }
113 
114  sample_rate = avio_rl32(pb);
115  duration = avio_rl64(pb) / st_count;
116 
117  if (avio_feof(pb))
118  return AVERROR_INVALIDDATA;
119 
120  switch (quality) {
121  case 0:
123  bpp = 1;
124  break;
125  case 1:
127  bpp = 2;
128  break;
129  case 2:
131  bpp = 4;
132  break;
133  case 3:
135  bpp = 3;
136  break;
137  default:
138  return AVERROR_INVALIDDATA;
139  }
140 
141  s->index = 0;
142  s->stored_index = 0;
143  s->bpp = bpp;
144  if ((int64_t)bpp * st_count * (int64_t)sample_rate >= INT32_MAX ||
145  (int64_t)bpp * st_count * (int64_t)sample_rate == 0
146  )
147  return AVERROR_INVALIDDATA;
148  s->data = av_calloc(st_count * sample_rate, bpp);
149  if (!s->data)
150  return AVERROR(ENOMEM);
151 
152  for (unsigned i = 0; i < st_count; i++) {
153  StreamParams *stp = &s->p[i];
154  AVCodecParameters *par;
156  if (!st)
157  return AVERROR(ENOMEM);
158 
159  par = st->codecpar;
160  par->codec_id = codec_id;
162  par->ch_layout.nb_channels = 1;
163  par->ch_layout = stp->layout;
164  par->sample_rate = sample_rate;
165  st->duration = duration;
166 
167  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
168  }
169 
170  s->header_len = (ctx->nb_streams + 7) / 8;
171 
172  return 0;
173 }
174 
176 {
177  AVIOContext *pb = ctx->pb;
179  AVStream *st = ctx->streams[0];
180  const int bpp = s->bpp;
181  StreamParams *stp;
182  int64_t pos;
183  int ret;
184 
185  pos = avio_tell(pb);
186 
187 again:
188  if (avio_feof(pb))
189  return AVERROR_EOF;
190 
191  if (s->index >= ctx->nb_streams) {
192  int cur_st = 0, st_count = 0, st_index = 0;
193 
194  ret = ffio_read_size(pb, s->header, s->header_len);
195  if (ret < 0)
196  return ret;
197  for (int i = 0; i < s->header_len; i++) {
198  uint8_t val = s->header[i];
199 
200  for (int j = 0; j < 8 && cur_st < ctx->nb_streams; j++, cur_st++) {
201  StreamParams *stp = &s->p[st_index];
202 
203  stp->stored = 0;
204  if (val & 1) {
205  stp->stored = 1;
206  st_count++;
207  }
208  val >>= 1;
209  st_index++;
210  }
211  }
212 
213  s->index = s->stored_index = 0;
214  s->nb_stored = st_count;
215  if (!st_count)
216  return AVERROR_INVALIDDATA;
217  ret = ffio_read_size(pb, s->data, st_count * st->codecpar->sample_rate * bpp);
218  if (ret < 0)
219  return ret;
220  }
221 
222  st = ctx->streams[s->index];
223  stp = &s->p[s->index];
224  while (!stp->stored) {
225  s->index++;
226  if (s->index >= ctx->nb_streams)
227  goto again;
228  stp = &s->p[s->index];
229  }
230  st = ctx->streams[s->index];
231 
232  ret = av_new_packet(pkt, st->codecpar->sample_rate * bpp);
233  if (ret < 0)
234  return ret;
235 
236  switch (bpp) {
237  case 1:
238  for (int n = 0; n < st->codecpar->sample_rate; n++)
239  pkt->data[n] = s->data[n * s->nb_stored + s->stored_index];
240  break;
241  case 2:
242  for (int n = 0; n < st->codecpar->sample_rate; n++)
243  AV_WN16(pkt->data + n * 2, AV_RN16(s->data + n * s->nb_stored * 2 + s->stored_index * 2));
244  break;
245  case 3:
246  for (int n = 0; n < st->codecpar->sample_rate; n++)
247  AV_WL24(pkt->data + n * 3, AV_RL24(s->data + n * s->nb_stored * 3 + s->stored_index * 3));
248  break;
249  case 4:
250  for (int n = 0; n < st->codecpar->sample_rate; n++)
251  AV_WN32(pkt->data + n * 4, AV_RN32(s->data + n * s->nb_stored * 4 + s->stored_index * 4));
252  break;
253  }
254 
255  pkt->stream_index = s->index;
256  pkt->pos = pos;
257  s->index++;
258  s->stored_index++;
259 
260  return 0;
261 }
262 
264 {
266 
267  av_freep(&s->data);
268 
269  return 0;
270 }
271 
272 static int laf_read_seek(AVFormatContext *ctx, int stream_index,
273  int64_t timestamp, int flags)
274 {
276 
277  s->stored_index = s->index = s->nb_stored = 0;
278 
279  return -1;
280 }
281 
283  .p.name = "laf",
284  .p.long_name = NULL_IF_CONFIG_SMALL("LAF (Limitless Audio Format)"),
285  .p.extensions = "laf",
286  .p.flags = AVFMT_GENERIC_INDEX,
287  .priv_data_size = sizeof(LAFContext),
293  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
294 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:328
StreamParams::stored
int stored
Definition: lafdec.c:36
StreamParams::horizontal
float horizontal
Definition: lafdec.c:33
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AV_RN16
#define AV_RN16(p)
Definition: intreadwrite.h:358
laf_read_header
static int laf_read_header(AVFormatContext *ctx)
Definition: lafdec.c:61
laf_read_seek
static int laf_read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: lafdec.c:272
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1323
AVPacket::data
uint8_t * data
Definition: packet.h:524
StreamParams::lfe
int lfe
Definition: lafdec.c:35
LAFContext::header
uint8_t header[(MAX_STREAMS+7)/8]
Definition: lafdec.c:49
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
sample_rate
sample_rate
Definition: ffmpeg_filter.c:424
quality
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
Definition: rate_distortion.txt:12
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
LAFContext
Definition: lafdec.c:39
avpriv_set_pts_info
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:853
av_int2float
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
val
static double val(void *priv, double ch)
Definition: aeval.c:78
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:802
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:761
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_CH_LOW_FREQUENCY
#define AV_CH_LOW_FREQUENCY
Definition: channel_layout.h:171
duration
int64_t duration
Definition: movenc.c:65
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
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
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:49
LAFContext::index
unsigned index
Definition: lafdec.c:43
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:387
LAFContext::data
uint8_t * data
Definition: lafdec.c:40
FF_INFMT_FLAG_INIT_CLEANUP
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: demux.h:35
StreamParams
Definition: lafdec.c:31
LAFContext::p
StreamParams p[MAX_STREAMS]
Definition: lafdec.c:46
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
NULL
#define NULL
Definition: coverity.c:32
MAX_STREAMS
#define MAX_STREAMS
Definition: lafdec.c:29
ff_laf_demuxer
const FFInputFormat ff_laf_demuxer
Definition: lafdec.c:282
AV_WL24
#define AV_WL24(p, d)
Definition: intreadwrite.h:462
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1297
AV_RN32
#define AV_RN32(p)
Definition: intreadwrite.h:362
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
LAFContext::bpp
unsigned bpp
Definition: lafdec.c:44
AV_CH_FRONT_CENTER
#define AV_CH_FRONT_CENTER
Definition: channel_layout.h:170
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1311
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AV_CODEC_ID_PCM_S24LE
@ AV_CODEC_ID_PCM_S24LE
Definition: codec_id.h:340
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
AV_WN32
#define AV_WN32(p, v)
Definition: intreadwrite.h:374
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
AV_RL24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_RL24
Definition: bytestream.h:93
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
avio_internal.h
StreamParams::vertical
float vertical
Definition: lafdec.c:34
AV_CH_FRONT_LEFT
#define AV_CH_FRONT_LEFT
Definition: channel_layout.h:168
demux.h
AV_CH_SIDE_RIGHT
#define AV_CH_SIDE_RIGHT
Definition: channel_layout.h:178
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
LAFContext::stored_index
unsigned stored_index
Definition: lafdec.c:42
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
StreamParams::layout
AVChannelLayout layout
Definition: lafdec.c:32
laf_read_packet
static int laf_read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: lafdec.c:175
again
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining again
Definition: filter_design.txt:25
mode
mode
Definition: ebur128.h:83
LAFContext::header_len
int header_len
Definition: lafdec.c:48
laf_probe
static int laf_probe(const AVProbeData *p)
Definition: lafdec.c:52
laf_read_close
static int laf_read_close(AVFormatContext *ctx)
Definition: lafdec.c:263
AVPacket::stream_index
int stream_index
Definition: packet.h:526
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
AV_CH_FRONT_RIGHT
#define AV_CH_FRONT_RIGHT
Definition: channel_layout.h:169
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
mem.h
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:333
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:378
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:501
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:544
FFInputFormat
Definition: demux.h:37
avio_rl64
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:738
AV_CHANNEL_LAYOUT_MASK
#define AV_CHANNEL_LAYOUT_MASK(nb, m)
Macro to define native channel layouts.
Definition: channel_layout.h:368
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AV_CODEC_ID_PCM_F32LE
@ AV_CODEC_ID_PCM_F32LE
Definition: codec_id.h:349
LAFContext::nb_stored
unsigned nb_stored
Definition: lafdec.c:41
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:662
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1283
AV_CH_SIDE_LEFT
#define AV_CH_SIDE_LEFT
Definition: channel_layout.h:177
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346
AV_WN16
#define AV_WN16(p, v)
Definition: intreadwrite.h:370