FFmpeg
Loading...
Searching...
No Matches
moflex.c
Go to the documentation of this file.
1/*
2 * MOFLEX demuxer
3 * Copyright (c) 2015-2016 Florian Nouwt
4 * Copyright (c) 2017 Adib Surani
5 * Copyright (c) 2020 Paul B Mahol
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
25
26#include "avformat.h"
27#include "demux.h"
28#include "internal.h"
29
30typedef struct BitReader {
31 unsigned last;
32 unsigned pos;
33} BitReader;
34
44
45static int pop(BitReader *br, AVIOContext *pb)
46{
47 if (avio_feof(pb))
48 return AVERROR_EOF;
49
50 if ((br->pos & 7) == 0)
51 br->last = (unsigned)avio_r8(pb) << 24U;
52 else
53 br->last <<= 1;
54
55 br->pos++;
56 return !!(br->last & 0x80000000);
57}
58
59static int pop_int(BitReader *br, AVIOContext *pb, int n)
60{
61 int value = 0;
62
63 for (int i = 0; i < n; i++) {
64 int ret = pop(br, pb);
65
66 if (ret < 0)
67 return ret;
68 if (ret > INT_MAX - value - value)
70 value = 2 * value + ret;
71 }
72
73 return value;
74}
75
76static int pop_length(BitReader *br, AVIOContext *pb)
77{
78 int ret, n = 1;
79
80 while ((ret = pop(br, pb)) == 0)
81 n++;
82
83 if (ret < 0)
84 return ret;
85 return n;
86}
87
88static int read_var_byte(AVFormatContext *s, unsigned *out)
89{
90 AVIOContext *pb = s->pb;
91 unsigned value = 0, data;
92
93 data = avio_r8(pb);
94 if (!(data & 0x80)) {
95 *out = data;
96 return 0;
97 }
98
99 value = (data & 0x7F) << 7;
100 data = avio_r8(pb);
101 if (!(data & 0x80)) {
102 value |= data;
103 *out = value;
104 return 0;
105 }
106
107 value = ((data & 0x7F) | value) << 7;
108 data = avio_r8(pb);
109 if (!(data & 0x80)) {
110 value |= data;
111 *out = value;
112 return 0;
113 }
114
115 value = (((data & 0x7F) | value) << 7) | avio_r8(pb);
116 *out = value;
117
118 return 0;
119}
120
121static int moflex_probe(const AVProbeData *p)
122{
124 int score = 0;
125
126 bytestream2_init(&gb, p->buf, p->buf_size);
127
128 if (bytestream2_get_be16(&gb) != 0x4C32)
129 return 0;
130 score += 10;
131
132 bytestream2_skip(&gb, 10);
133 if (bytestream2_get_be16(&gb) == 0)
134 return 0;
135 score += 5;
136
137 while (bytestream2_get_bytes_left(&gb) > 0) {
138 int type = bytestream2_get_byte(&gb);
139 int size = bytestream2_get_byte(&gb);
140
141 if (type == 0) {
142 score += 5 * (size == 0);
143 break;
144 }
145 if ((type == 1 && size == 12) ||
146 (type == 2 && size == 6) ||
147 (type == 3 && size == 13) ||
148 (type == 4 && size == 2))
149 score += 20;
151 }
152
153 return FFMIN(AVPROBE_SCORE_MAX, score);
154}
155
157{
158 MOFLEXDemuxContext *m = s->priv_data;
159 AVIOContext *pb = s->pb;
160
161 if (avio_rb16(pb) != 0x4C32) {
162 if (avio_feof(pb))
163 return AVERROR_EOF;
164 avio_seek(pb, -2, SEEK_CUR);
165 return 1;
166 }
167
168 avio_skip(pb, 2);
169 m->ts = avio_rb64(pb);
170 m->size = avio_rb16(pb) + 1;
171
172 while (!avio_feof(pb)) {
173 unsigned type, ssize, codec_id = 0;
174 unsigned codec_type, width = 0, height = 0, sample_rate = 0, channels = 0;
175 int stream_index = -1;
176 AVRational tb = av_make_q(0, 1);
177
179 read_var_byte(s, &ssize);
180
181 switch (type) {
182 case 0:
183 if (ssize > 0)
184 avio_skip(pb, ssize);
185 return 0;
186 case 2:
188 stream_index = avio_r8(pb);
189 codec_id = avio_r8(pb);
190 switch (codec_id) {
191 case 0: codec_id = AV_CODEC_ID_FASTAUDIO; break;
192 case 1: codec_id = AV_CODEC_ID_ADPCM_IMA_MOFLEX; break;
193 case 2: codec_id = AV_CODEC_ID_PCM_S16LE; break;
194 default:
195 av_log(s, AV_LOG_ERROR, "Unsupported audio codec: %d\n", codec_id);
197 }
198 sample_rate = avio_rb24(pb) + 1;
199 tb = av_make_q(1, sample_rate);
200 channels = avio_r8(pb) + 1;
201 break;
202 case 1:
203 case 3:
205 stream_index = avio_r8(pb);
206 codec_id = avio_r8(pb);
207 switch (codec_id) {
208 case 0: codec_id = AV_CODEC_ID_MOBICLIP; break;
209 default:
210 av_log(s, AV_LOG_ERROR, "Unsupported video codec: %d\n", codec_id);
212 }
213 tb.den = avio_rb16(pb);
214 tb.num = avio_rb16(pb);
215 width = avio_rb16(pb);
216 height = avio_rb16(pb);
217 avio_skip(pb, type == 3 ? 3 : 2);
218 break;
219 case 4:
221 stream_index = avio_r8(pb);
222 avio_skip(pb, 1);
223 break;
224 }
225
226 if (stream_index == s->nb_streams) {
228
229 if (!st)
230 return AVERROR(ENOMEM);
231
234 st->codecpar->width = width;
235 st->codecpar->height = height;
236 st->codecpar->sample_rate= sample_rate;
239 if (!st->priv_data)
240 return AVERROR(ENOMEM);
241
242 if (tb.num)
243 avpriv_set_pts_info(st, 63, tb.num, tb.den);
244 }
245 }
246
247 return 0;
248}
249
251{
252 int ret;
253
254 ret = moflex_read_sync(s);
255 if (ret < 0)
256 return ret;
257
258 s->ctx_flags |= AVFMTCTX_NOHEADER;
259 avio_seek(s->pb, 0, SEEK_SET);
260
261 return 0;
262}
263
265{
266 MOFLEXDemuxContext *m = s->priv_data;
267 AVIOContext *pb = s->pb;
268 BitReader *br = &m->br;
269 int ret;
270
271 while (!avio_feof(pb)) {
272 if (!m->in_block) {
273 m->pos = avio_tell(pb);
274
275 ret = moflex_read_sync(s);
276 if (ret < 0)
277 return ret;
278
279 m->flags = avio_r8(pb);
280 if (m->flags & 2)
281 avio_skip(pb, 2);
282 }
283
284 while ((avio_tell(pb) < m->pos + m->size) && !avio_feof(pb) && avio_r8(pb)) {
285 int stream_index, bits, pkt_size, endframe;
286 AVPacket *packet;
287
288 m->in_block = 1;
289
290 avio_seek(pb, -1, SEEK_CUR);
291 br->pos = br->last = 0;
292
293 bits = pop_length(br, pb);
294 if (bits < 0)
295 return bits;
296 stream_index = pop_int(br, pb, bits);
297 if (stream_index < 0)
298 return stream_index;
299 if (stream_index >= s->nb_streams)
300 return AVERROR_INVALIDDATA;
301
302 endframe = pop(br, pb);
303 if (endframe < 0)
304 return endframe;
305 if (endframe) {
306 bits = pop_length(br, pb);
307 if (bits < 0)
308 return bits;
309 pop_int(br, pb, bits);
310 pop(br, pb);
311 bits = pop_length(br, pb);
312 if (bits < 0)
313 return bits;
314 pop_int(br, pb, bits * 2 + 26);
315 }
316
317 pkt_size = pop_int(br, pb, 13) + 1;
318 if (pkt_size > m->size)
319 return AVERROR_INVALIDDATA;
320 packet = s->streams[stream_index]->priv_data;
321 if (!packet) {
322 avio_skip(pb, pkt_size);
323 continue;
324 }
325
326 ret = av_append_packet(pb, packet, pkt_size);
327 if (ret < 0)
328 return ret;
329 if (endframe && packet->size > 0) {
330 av_packet_move_ref(pkt, packet);
331 pkt->pos = m->pos;
332 pkt->stream_index = stream_index;
333 if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
334 pkt->duration = 1;
335 if (pkt->data[0] & 0x80)
336 pkt->flags |= AV_PKT_FLAG_KEY;
337 } else {
338 pkt->flags |= AV_PKT_FLAG_KEY;
339 }
340 return ret;
341 }
342 }
343
344 m->in_block = 0;
345
346 if (m->flags % 2 == 0) {
347 if (m->size <= 0)
348 return AVERROR_INVALIDDATA;
349 avio_seek(pb, m->pos + m->size, SEEK_SET);
350 }
351 }
352
353 return AVERROR_EOF;
354}
355
356static int moflex_read_seek(AVFormatContext *s, int stream_index,
357 int64_t pts, int flags)
358{
359 MOFLEXDemuxContext *m = s->priv_data;
360
361 m->in_block = 0;
362
363 return -1;
364}
365
367{
368 for (int i = 0; i < s->nb_streams; i++) {
369 av_packet_free((AVPacket **)&s->streams[i]->priv_data);
370 }
371
372 return 0;
373}
374
376 .p.name = "moflex",
377 .p.long_name = NULL_IF_CONFIG_SMALL("MobiClip MOFLEX"),
378 .p.extensions = "moflex",
379 .p.flags = AVFMT_GENERIC_INDEX,
380 .priv_data_size = sizeof(MOFLEXDemuxContext),
386 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
387};
const FFInputFormat ff_moflex_demuxer
Definition moflex.c:375
channels
Definition aptx.h:31
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.
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:114
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1284
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
uint64_t avio_rb64(AVIOContext *s)
Definition aviobuf.c:911
unsigned int avio_rb16(AVIOContext *s)
Definition aviobuf.c:749
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_rb24(AVIOContext *s)
Definition aviobuf.c:757
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
#define flags(name, subs,...)
Definition cbs_h264.c:74
#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
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#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
static AVPacket * pkt
double value
Definition eval.c:102
static const uint8_t bits[8]
Definition fastaudio.c:100
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_CODEC_ID_FASTAUDIO
Definition codec_id.h:547
@ AV_CODEC_ID_MOBICLIP
Definition codec_id.h:299
@ AV_CODEC_ID_ADPCM_IMA_MOFLEX
Definition codec_id.h:419
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:491
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#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
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
cl_device_type type
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
#define FFMIN(a, b)
Definition macros.h:49
static int moflex_read_sync(AVFormatContext *s)
Definition moflex.c:156
static int pop_int(BitReader *br, AVIOContext *pb, int n)
Definition moflex.c:59
static int pop(BitReader *br, AVIOContext *pb)
Definition moflex.c:45
static int read_var_byte(AVFormatContext *s, unsigned *out)
Definition moflex.c:88
static int moflex_probe(const AVProbeData *p)
Definition moflex.c:121
static int moflex_read_close(AVFormatContext *s)
Definition moflex.c:366
static int moflex_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition moflex.c:264
static int pop_length(BitReader *br, AVIOContext *pb)
Definition moflex.c:76
static int moflex_read_header(AVFormatContext *s)
Definition moflex.c:250
static int moflex_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
Definition moflex.c:356
const char data[16]
Definition mxf.c:149
enum AVMediaType codec_type
Definition rtp.c:37
int nb_channels
Number of channels in this layout.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
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
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
void * priv_data
Definition avformat.h:791
unsigned pos
Definition moflex.c:32
unsigned last
Definition moflex.c:31
BitReader br
Definition moflex.c:42
unsigned size
Definition moflex.c:36
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
static int64_t pts
int size
enum AVCodecID codec_id