3 * Copyright (c) 2015-2016 Florian Nouwt
4 * Copyright (c) 2017 Adib Surani
5 * Copyright (c) 2020 Paul B Mahol
7 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavcodec/bytestream.h"
29 typedef struct BitReader {
34 typedef struct MOFLEXDemuxContext {
44 static int pop(BitReader *br, AVIOContext *pb)
49 if ((br->pos & 7) == 0)
50 br->last = (unsigned)avio_r8(pb) << 24U;
55 return !!(br->last & 0x80000000);
58 static int pop_int(BitReader *br, AVIOContext *pb, int n)
62 for (int i = 0; i < n; i++) {
63 int ret = pop(br, pb);
67 if (ret > INT_MAX - value - value)
68 return AVERROR_INVALIDDATA;
69 value = 2 * value + ret;
75 static int pop_length(BitReader *br, AVIOContext *pb)
79 while ((ret = pop(br, pb)) == 0)
87 static int read_var_byte(AVFormatContext *s, unsigned *out)
89 AVIOContext *pb = s->pb;
90 unsigned value = 0, data;
98 value = (data & 0x7F) << 7;
100 if (!(data & 0x80)) {
106 value = ((data & 0x7F) | value) << 7;
108 if (!(data & 0x80)) {
114 value = (((data & 0x7F) | value) << 7) | avio_r8(pb);
120 static int moflex_probe(const AVProbeData *p)
125 bytestream2_init(&gb, p->buf, p->buf_size);
127 if (bytestream2_get_be16(&gb) != 0x4C32)
131 bytestream2_skip(&gb, 10);
132 if (bytestream2_get_be16(&gb) == 0)
136 while (bytestream2_get_bytes_left(&gb) > 0) {
137 int type = bytestream2_get_byte(&gb);
138 int size = bytestream2_get_byte(&gb);
141 score += 5 * (size == 0);
144 if ((type == 1 && size == 12) ||
145 (type == 2 && size == 6) ||
146 (type == 3 && size == 13) ||
147 (type == 4 && size == 2))
149 bytestream2_skip(&gb, size);
152 return FFMIN(AVPROBE_SCORE_MAX, score);
155 static int moflex_read_sync(AVFormatContext *s)
157 MOFLEXDemuxContext *m = s->priv_data;
158 AVIOContext *pb = s->pb;
160 if (avio_rb16(pb) != 0x4C32) {
163 avio_seek(pb, -2, SEEK_CUR);
168 m->ts = avio_rb64(pb);
169 m->size = avio_rb16(pb) + 1;
171 while (!avio_feof(pb)) {
172 unsigned type, ssize, codec_id = 0;
173 unsigned codec_type, width = 0, height = 0, sample_rate = 0, channels = 0;
174 int stream_index = -1;
178 read_var_byte(s, &type);
179 read_var_byte(s, &ssize);
184 avio_skip(pb, ssize);
187 codec_type = AVMEDIA_TYPE_AUDIO;
188 stream_index = avio_r8(pb);
189 codec_id = avio_r8(pb);
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;
195 av_log(s, AV_LOG_ERROR, "Unsupported audio codec: %d\n", codec_id);
196 return AVERROR_PATCHWELCOME;
198 sample_rate = avio_rb24(pb) + 1;
199 channels = avio_r8(pb) + 1;
203 codec_type = AVMEDIA_TYPE_VIDEO;
204 stream_index = avio_r8(pb);
205 codec_id = avio_r8(pb);
207 case 0: codec_id = AV_CODEC_ID_MOBICLIP; break;
209 av_log(s, AV_LOG_ERROR, "Unsupported video codec: %d\n", codec_id);
210 return AVERROR_PATCHWELCOME;
212 fps.num = avio_rb16(pb);
213 fps.den = avio_rb16(pb);
214 width = avio_rb16(pb);
215 height = avio_rb16(pb);
216 format = AV_PIX_FMT_YUV420P;
217 avio_skip(pb, type == 3 ? 3 : 2);
220 codec_type = AVMEDIA_TYPE_DATA;
221 stream_index = avio_r8(pb);
226 if (stream_index == s->nb_streams) {
227 AVStream *st = avformat_new_stream(s, NULL);
230 return AVERROR(ENOMEM);
232 st->codecpar->codec_type = codec_type;
233 st->codecpar->codec_id = codec_id;
234 st->codecpar->width = width;
235 st->codecpar->height = height;
236 st->codecpar->sample_rate= sample_rate;
237 st->codecpar->channels = channels;
238 st->codecpar->format = format;
239 st->priv_data = av_packet_alloc();
241 return AVERROR(ENOMEM);
244 avpriv_set_pts_info(st, 63, 1, sample_rate);
246 avpriv_set_pts_info(st, 63, fps.den, fps.num);
253 static int moflex_read_header(AVFormatContext *s)
257 ret = moflex_read_sync(s);
261 s->ctx_flags |= AVFMTCTX_NOHEADER;
262 avio_seek(s->pb, 0, SEEK_SET);
267 static int moflex_read_packet(AVFormatContext *s, AVPacket *pkt)
269 MOFLEXDemuxContext *m = s->priv_data;
270 AVIOContext *pb = s->pb;
271 BitReader *br = &m->br;
274 while (!avio_feof(pb)) {
276 m->pos = avio_tell(pb);
278 ret = moflex_read_sync(s);
282 m->flags = avio_r8(pb);
287 while ((avio_tell(pb) < m->pos + m->size) && !avio_feof(pb) && avio_r8(pb)) {
288 int stream_index, bits, pkt_size, endframe;
293 avio_seek(pb, -1, SEEK_CUR);
294 br->pos = br->last = 0;
296 bits = pop_length(br, pb);
299 stream_index = pop_int(br, pb, bits);
300 if (stream_index < 0)
302 if (stream_index >= s->nb_streams)
303 return AVERROR_INVALIDDATA;
305 endframe = pop(br, pb);
309 bits = pop_length(br, pb);
312 pop_int(br, pb, bits);
314 bits = pop_length(br, pb);
317 pop_int(br, pb, bits * 2 + 26);
320 pkt_size = pop_int(br, pb, 13) + 1;
321 if (pkt_size > m->size)
322 return AVERROR_INVALIDDATA;
323 packet = s->streams[stream_index]->priv_data;
325 avio_skip(pb, pkt_size);
329 ret = av_append_packet(pb, packet, pkt_size);
332 if (endframe && packet->size > 0) {
333 av_packet_move_ref(pkt, packet);
335 pkt->stream_index = stream_index;
336 if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
338 if (pkt->data[0] & 0x80)
339 pkt->flags |= AV_PKT_FLAG_KEY;
341 pkt->flags |= AV_PKT_FLAG_KEY;
349 if (m->flags % 2 == 0) {
351 return AVERROR_INVALIDDATA;
352 avio_seek(pb, m->pos + m->size, SEEK_SET);
359 static int moflex_read_seek(AVFormatContext *s, int stream_index,
360 int64_t pts, int flags)
362 MOFLEXDemuxContext *m = s->priv_data;
369 static int moflex_read_close(AVFormatContext *s)
371 for (int i = 0; i < s->nb_streams; i++) {
372 av_packet_free((AVPacket **)&s->streams[i]->priv_data);
378 const AVInputFormat ff_moflex_demuxer = {
380 .long_name = NULL_IF_CONFIG_SMALL("MobiClip MOFLEX"),
381 .priv_data_size = sizeof(MOFLEXDemuxContext),
382 .read_probe = moflex_probe,
383 .read_header = moflex_read_header,
384 .read_packet = moflex_read_packet,
385 .read_seek = moflex_read_seek,
386 .read_close = moflex_read_close,
387 .extensions = "moflex",
388 .flags = AVFMT_GENERIC_INDEX,