FFmpeg
moflex.c
Go to the documentation of this file.
1 /*
2  * MOFLEX demuxer
3  * Copyright (c) 2020 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 "libavcodec/bytestream.h"
23 
24 #include "avformat.h"
25 #include "internal.h"
26 
27 typedef struct BitReader {
28  unsigned last;
29  unsigned pos;
30 } BitReader;
31 
32 typedef struct MOFLEXDemuxContext {
33  unsigned size;
34  int64_t pos;
35  int64_t ts;
36  int flags;
37  int in_block;
38 
41 
42 static int pop(BitReader *br, AVIOContext *pb)
43 {
44  if (avio_feof(pb))
45  return AVERROR_EOF;
46 
47  if ((br->pos & 7) == 0)
48  br->last = (unsigned)avio_r8(pb) << 24U;
49  else
50  br->last <<= 1;
51 
52  br->pos++;
53  return !!(br->last & 0x80000000);
54 }
55 
56 static int pop_int(BitReader *br, AVIOContext *pb, int n)
57 {
58  int value = 0;
59 
60  for (int i = 0; i < n; i++) {
61  int ret = pop(br, pb);
62 
63  if (ret < 0)
64  return ret;
65  if (ret > INT_MAX - value - value)
66  return AVERROR_INVALIDDATA;
67  value = 2 * value + ret;
68  }
69 
70  return value;
71 }
72 
73 static int pop_length(BitReader *br, AVIOContext *pb)
74 {
75  int ret, n = 1;
76 
77  while ((ret = pop(br, pb)) == 0)
78  n++;
79 
80  if (ret < 0)
81  return ret;
82  return n;
83 }
84 
85 static int read_var_byte(AVFormatContext *s, unsigned *out)
86 {
87  AVIOContext *pb = s->pb;
88  unsigned value = 0, data;
89 
90  data = avio_r8(pb);
91  if (!(data & 0x80)) {
92  *out = data;
93  return 0;
94  }
95 
96  value = (data & 0x7F) << 7;
97  data = avio_r8(pb);
98  if (!(data & 0x80)) {
99  value |= data;
100  *out = value;
101  return 0;
102  }
103 
104  value = ((data & 0x7F) | value) << 7;
105  data = avio_r8(pb);
106  if (!(data & 0x80)) {
107  value |= data;
108  *out = value;
109  return 0;
110  }
111 
112  value = (((data & 0x7F) | value) << 7) | avio_r8(pb);
113  *out = value;
114 
115  return 0;
116 }
117 
118 static int moflex_probe(const AVProbeData *p)
119 {
120  GetByteContext gb;
121  int score = 0;
122 
123  bytestream2_init(&gb, p->buf, p->buf_size);
124 
125  if (bytestream2_get_be16(&gb) != 0x4C32)
126  return 0;
127  score += 10;
128 
129  bytestream2_skip(&gb, 10);
130  if (bytestream2_get_be16(&gb) == 0)
131  return 0;
132  score += 5;
133 
134  while (bytestream2_get_bytes_left(&gb) > 0) {
135  int type = bytestream2_get_byte(&gb);
136  int size = bytestream2_get_byte(&gb);
137 
138  if (type == 0) {
139  score += 5 * (size == 0);
140  break;
141  }
142  if ((type == 1 && size == 12) ||
143  (type == 2 && size == 6) ||
144  (type == 3 && size == 13) ||
145  (type == 4 && size == 2))
146  score += 20;
147  bytestream2_skip(&gb, size);
148  }
149 
150  return FFMIN(AVPROBE_SCORE_MAX, score);
151 }
152 
154 {
156  AVIOContext *pb = s->pb;
157 
158  if (avio_rb16(pb) != 0x4C32) {
159  if (avio_feof(pb))
160  return AVERROR_EOF;
161  avio_seek(pb, -2, SEEK_CUR);
162  return 1;
163  }
164 
165  avio_skip(pb, 2);
166  m->ts = avio_rb64(pb);
167  m->size = avio_rb16(pb) + 1;
168 
169  while (!avio_feof(pb)) {
170  unsigned type, ssize, codec_id = 0;
171  unsigned codec_type, width = 0, height = 0, sample_rate = 0, channels = 0;
172  int stream_index = -1;
173  int format;
174  AVRational fps;
175 
176  read_var_byte(s, &type);
177  read_var_byte(s, &ssize);
178 
179  switch (type) {
180  case 0:
181  if (ssize > 0)
182  avio_skip(pb, ssize);
183  return 0;
184  case 2:
185  codec_type = AVMEDIA_TYPE_AUDIO;
186  stream_index = avio_r8(pb);
187  codec_id = avio_r8(pb);
188  switch (codec_id) {
189  case 0: codec_id = AV_CODEC_ID_FASTAUDIO; break;
190  case 1: codec_id = AV_CODEC_ID_ADPCM_IMA_MOFLEX; break;
191  case 2: codec_id = AV_CODEC_ID_PCM_S16LE; break;
192  default:
193  av_log(s, AV_LOG_ERROR, "Unsupported audio codec: %d\n", codec_id);
194  return AVERROR_PATCHWELCOME;
195  }
196  sample_rate = avio_rb24(pb) + 1;
197  channels = avio_r8(pb) + 1;
198  break;
199  case 1:
200  case 3:
201  codec_type = AVMEDIA_TYPE_VIDEO;
202  stream_index = avio_r8(pb);
203  codec_id = avio_r8(pb);
204  switch (codec_id) {
205  case 0: codec_id = AV_CODEC_ID_MOBICLIP; break;
206  default:
207  av_log(s, AV_LOG_ERROR, "Unsupported video codec: %d\n", codec_id);
208  return AVERROR_PATCHWELCOME;
209  }
210  fps.num = avio_rb16(pb);
211  fps.den = avio_rb16(pb);
212  width = avio_rb16(pb);
213  height = avio_rb16(pb);
214  format = AV_PIX_FMT_YUV420P;
215  avio_skip(pb, type == 3 ? 3 : 2);
216  break;
217  case 4:
218  codec_type = AVMEDIA_TYPE_DATA;
219  stream_index = avio_r8(pb);
220  avio_skip(pb, 1);
221  break;
222  }
223 
224  if (stream_index == s->nb_streams) {
226 
227  if (!st)
228  return AVERROR(ENOMEM);
229 
231  st->codecpar->codec_id = codec_id;
232  st->codecpar->width = width;
233  st->codecpar->height = height;
235  st->codecpar->channels = channels;
236  st->codecpar->format = format;
237  st->priv_data = av_packet_alloc();
238  if (!st->priv_data)
239  return AVERROR(ENOMEM);
240 
241  if (sample_rate)
242  avpriv_set_pts_info(st, 63, 1, sample_rate);
243  else
244  avpriv_set_pts_info(st, 63, fps.den, fps.num);
245  }
246  }
247 
248  return 0;
249 }
250 
252 {
253  int ret;
254 
255  ret = moflex_read_sync(s);
256  if (ret < 0)
257  return ret;
258 
260  avio_seek(s->pb, 0, SEEK_SET);
261 
262  return 0;
263 }
264 
266 {
268  AVIOContext *pb = s->pb;
269  BitReader *br = &m->br;
270  int ret;
271 
272  while (!avio_feof(pb)) {
273  if (!m->in_block) {
274  m->pos = avio_tell(pb);
275 
276  ret = moflex_read_sync(s);
277  if (ret < 0)
278  return ret;
279 
280  m->flags = avio_r8(pb);
281  if (m->flags & 2)
282  avio_skip(pb, 2);
283  }
284 
285  while ((avio_tell(pb) < m->pos + m->size) && !avio_feof(pb) && avio_r8(pb)) {
286  int stream_index, bits, pkt_size, endframe;
287  AVPacket *packet;
288 
289  m->in_block = 1;
290 
291  avio_seek(pb, -1, SEEK_CUR);
292  br->pos = br->last = 0;
293 
294  bits = pop_length(br, pb);
295  if (bits < 0)
296  return bits;
297  stream_index = pop_int(br, pb, bits);
298  if (stream_index < 0)
299  return stream_index;
300  if (stream_index >= s->nb_streams)
301  return AVERROR_INVALIDDATA;
302 
303  endframe = pop(br, pb);
304  if (endframe < 0)
305  return endframe;
306  if (endframe) {
307  bits = pop_length(br, pb);
308  if (bits < 0)
309  return bits;
310  pop_int(br, pb, bits);
311  pop(br, pb);
312  bits = pop_length(br, pb);
313  if (bits < 0)
314  return bits;
315  pop_int(br, pb, bits * 2 + 26);
316  }
317 
318  pkt_size = pop_int(br, pb, 13) + 1;
319  packet = s->streams[stream_index]->priv_data;
320  if (!packet) {
321  avio_skip(pb, pkt_size);
322  continue;
323  }
324 
325  ret = av_append_packet(pb, packet, pkt_size);
326  if (ret < 0)
327  return ret;
328  if (endframe && packet->size > 0) {
329  av_packet_move_ref(pkt, packet);
330  pkt->pos = m->pos;
331  pkt->stream_index = stream_index;
332  if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
333  pkt->duration = 1;
334  if (pkt->data[0] & 0x80)
335  pkt->flags |= AV_PKT_FLAG_KEY;
336  } else {
337  pkt->flags |= AV_PKT_FLAG_KEY;
338  }
339  return ret;
340  }
341  }
342 
343  m->in_block = 0;
344 
345  if (m->flags % 2 == 0) {
346  if (m->size <= 0)
347  return AVERROR_INVALIDDATA;
348  avio_seek(pb, m->pos + m->size, SEEK_SET);
349  }
350  }
351 
352  return AVERROR_EOF;
353 }
354 
355 static int moflex_read_seek(AVFormatContext *s, int stream_index,
356  int64_t pts, int flags)
357 {
359 
360  m->in_block = 0;
361 
362  return -1;
363 }
364 
366 {
367  for (int i = 0; i < s->nb_streams; i++) {
368  AVPacket *packet = s->streams[i]->priv_data;
369 
370  av_packet_free(&packet);
371  s->streams[i]->priv_data = 0;
372  }
373 
374  return 0;
375 }
376 
378  .name = "moflex",
379  .long_name = NULL_IF_CONFIG_SMALL("MobiClip MOFLEX"),
380  .priv_data_size = sizeof(MOFLEXDemuxContext),
386  .extensions = "moflex",
388 };
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:383
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4871
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int num
Numerator.
Definition: rational.h:59
int size
Definition: packet.h:364
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
enum AVMediaType codec_type
Definition: rtp.c:37
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
GLint GLenum type
Definition: opengl_enc.c:104
void * priv_data
Definition: avformat.h:895
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:341
static AVPacket pkt
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:768
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1288
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 format(the sample packing is implied by the sample format) and sample rate.The lists are not just lists
Format I/O context.
Definition: avformat.h:1239
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:64
Opaque data information usually continuous.
Definition: avutil.h:203
int width
Video only.
Definition: codec_par.h:126
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1183
static int moflex_probe(const AVProbeData *p)
Definition: moflex.c:118
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:381
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4453
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1307
#define height
uint8_t * data
Definition: packet.h:363
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:664
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
ptrdiff_t size
Definition: opengl_enc.c:100
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:904
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
static int moflex_read_header(AVFormatContext *s)
Definition: moflex.c:251
channels
Definition: aptx.h:33
#define av_log(a,...)
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
Definition: utils.c:315
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:401
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
#define U(x)
Definition: vp56_arith.h:37
static int moflex_read_close(AVFormatContext *s)
Definition: moflex.c:365
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
static int pop(BitReader *br, AVIOContext *pb)
Definition: moflex.c:42
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
uint8_t bits
Definition: vp3data.h:202
static int moflex_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition: moflex.c:355
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:369
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:628
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1295
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:776
#define FFMIN(a, b)
Definition: common.h:96
#define width
AVInputFormat ff_moflex_demuxer
Definition: moflex.c:377
int64_t pos
Definition: moflex.c:34
#define s(width, name)
Definition: cbs_vp9.c:257
unsigned pos
Definition: moflex.c:29
enum AVCodecID codec_id
Definition: vaapi_decode.c:369
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
static int moflex_read_sync(AVFormatContext *s)
Definition: moflex.c:153
Stream structure.
Definition: avformat.h:880
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
unsigned size
Definition: moflex.c:33
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
sample_rate
AVIOContext * pb
I/O context.
Definition: avformat.h:1281
int64_t ts
Definition: moflex.c:35
static int moflex_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: moflex.c:265
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
Rational number (pair of numerator and denominator).
Definition: rational.h:58
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
static int pop_length(BitReader *br, AVIOContext *pb)
Definition: moflex.c:73
static int pop_int(BitReader *br, AVIOContext *pb, int n)
Definition: moflex.c:56
static int64_t pts
#define flags(name, subs,...)
Definition: cbs_av1.c:560
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
int sample_rate
Audio only.
Definition: codec_par.h:170
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
Main libavformat public API header.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
unsigned last
Definition: moflex.c:28
int den
Denominator.
Definition: rational.h:60
static int read_var_byte(AVFormatContext *s, unsigned *out)
Definition: moflex.c:85
void * priv_data
Format private data.
Definition: avformat.h:1267
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:53
int channels
Audio only.
Definition: codec_par.h:166
FILE * out
Definition: movenc.c:54
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:652
BitReader br
Definition: moflex.c:39
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1045
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:368
int stream_index
Definition: packet.h:365
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
This structure stores compressed data.
Definition: packet.h:340
int i
Definition: input.c:407