3 * Copyright (c) 2020 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavcodec/bytestream.h"
27 typedef struct BitReader {
32 typedef struct MOFLEXDemuxContext {
42 static int pop(BitReader *br, AVIOContext *pb)
47 if ((br->pos & 7) == 0)
48 br->last = (unsigned)avio_r8(pb) << 24U;
53 return !!(br->last & 0x80000000);
56 static int pop_int(BitReader *br, AVIOContext *pb, int n)
60 for (int i = 0; i < n; i++) {
61 int ret = pop(br, pb);
65 if (ret > INT_MAX - value - value)
66 return AVERROR_INVALIDDATA;
67 value = 2 * value + ret;
73 static int pop_length(BitReader *br, AVIOContext *pb)
77 while ((ret = pop(br, pb)) == 0)
85 static int read_var_byte(AVFormatContext *s, unsigned *out)
87 AVIOContext *pb = s->pb;
88 unsigned value = 0, data;
96 value = (data & 0x7F) << 7;
104 value = ((data & 0x7F) | value) << 7;
106 if (!(data & 0x80)) {
112 value = (((data & 0x7F) | value) << 7) | avio_r8(pb);
118 static int moflex_probe(const AVProbeData *p)
123 bytestream2_init(&gb, p->buf, p->buf_size);
125 if (bytestream2_get_be16(&gb) != 0x4C32)
129 bytestream2_skip(&gb, 10);
130 if (bytestream2_get_be16(&gb) == 0)
134 while (bytestream2_get_bytes_left(&gb) > 0) {
135 int type = bytestream2_get_byte(&gb);
136 int size = bytestream2_get_byte(&gb);
139 score += 5 * (size == 0);
142 if ((type == 1 && size == 12) ||
143 (type == 2 && size == 6) ||
144 (type == 3 && size == 13) ||
145 (type == 4 && size == 2))
147 bytestream2_skip(&gb, size);
150 return FFMIN(AVPROBE_SCORE_MAX, score);
153 static int moflex_read_sync(AVFormatContext *s)
155 MOFLEXDemuxContext *m = s->priv_data;
156 AVIOContext *pb = s->pb;
158 if (avio_rb16(pb) != 0x4C32) {
161 avio_seek(pb, -2, SEEK_CUR);
166 m->ts = avio_rb64(pb);
167 m->size = avio_rb16(pb) + 1;
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;
176 read_var_byte(s, &type);
177 read_var_byte(s, &ssize);
182 avio_skip(pb, ssize);
185 codec_type = AVMEDIA_TYPE_AUDIO;
186 stream_index = avio_r8(pb);
187 codec_id = avio_r8(pb);
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;
193 av_log(s, AV_LOG_ERROR, "Unsupported audio codec: %d\n", codec_id);
194 return AVERROR_PATCHWELCOME;
196 sample_rate = avio_rb24(pb) + 1;
197 channels = avio_r8(pb) + 1;
201 codec_type = AVMEDIA_TYPE_VIDEO;
202 stream_index = avio_r8(pb);
203 codec_id = avio_r8(pb);
205 case 0: codec_id = AV_CODEC_ID_MOBICLIP; break;
207 av_log(s, AV_LOG_ERROR, "Unsupported video codec: %d\n", codec_id);
208 return AVERROR_PATCHWELCOME;
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);
218 codec_type = AVMEDIA_TYPE_DATA;
219 stream_index = avio_r8(pb);
224 if (stream_index == s->nb_streams) {
225 AVStream *st = avformat_new_stream(s, NULL);
228 return AVERROR(ENOMEM);
230 st->codecpar->codec_type = codec_type;
231 st->codecpar->codec_id = codec_id;
232 st->codecpar->width = width;
233 st->codecpar->height = height;
234 st->codecpar->sample_rate= sample_rate;
235 st->codecpar->channels = channels;
236 st->codecpar->format = format;
237 st->priv_data = av_packet_alloc();
239 return AVERROR(ENOMEM);
242 avpriv_set_pts_info(st, 63, 1, sample_rate);
244 avpriv_set_pts_info(st, 63, fps.den, fps.num);
251 static int moflex_read_header(AVFormatContext *s)
255 ret = moflex_read_sync(s);
259 s->ctx_flags |= AVFMTCTX_NOHEADER;
260 avio_seek(s->pb, 0, SEEK_SET);
265 static int moflex_read_packet(AVFormatContext *s, AVPacket *pkt)
267 MOFLEXDemuxContext *m = s->priv_data;
268 AVIOContext *pb = s->pb;
269 BitReader *br = &m->br;
272 while (!avio_feof(pb)) {
274 m->pos = avio_tell(pb);
276 ret = moflex_read_sync(s);
280 m->flags = avio_r8(pb);
285 while ((avio_tell(pb) < m->pos + m->size) && !avio_feof(pb) && avio_r8(pb)) {
286 int stream_index, bits, pkt_size, endframe;
291 avio_seek(pb, -1, SEEK_CUR);
292 br->pos = br->last = 0;
294 bits = pop_length(br, pb);
297 stream_index = pop_int(br, pb, bits);
298 if (stream_index < 0)
300 if (stream_index >= s->nb_streams)
301 return AVERROR_INVALIDDATA;
303 endframe = pop(br, pb);
307 bits = pop_length(br, pb);
310 pop_int(br, pb, bits);
312 bits = pop_length(br, pb);
315 pop_int(br, pb, bits * 2 + 26);
318 pkt_size = pop_int(br, pb, 13) + 1;
319 if (pkt_size > m->size)
320 return AVERROR_INVALIDDATA;
321 packet = s->streams[stream_index]->priv_data;
323 avio_skip(pb, pkt_size);
327 ret = av_append_packet(pb, packet, pkt_size);
330 if (endframe && packet->size > 0) {
331 av_packet_move_ref(pkt, packet);
333 pkt->stream_index = stream_index;
334 if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
336 if (pkt->data[0] & 0x80)
337 pkt->flags |= AV_PKT_FLAG_KEY;
339 pkt->flags |= AV_PKT_FLAG_KEY;
347 if (m->flags % 2 == 0) {
349 return AVERROR_INVALIDDATA;
350 avio_seek(pb, m->pos + m->size, SEEK_SET);
357 static int moflex_read_seek(AVFormatContext *s, int stream_index,
358 int64_t pts, int flags)
360 MOFLEXDemuxContext *m = s->priv_data;
367 static int moflex_read_close(AVFormatContext *s)
369 for (int i = 0; i < s->nb_streams; i++) {
370 AVPacket *packet = s->streams[i]->priv_data;
372 av_packet_free(&packet);
373 s->streams[i]->priv_data = 0;
379 AVInputFormat ff_moflex_demuxer = {
381 .long_name = NULL_IF_CONFIG_SMALL("MobiClip MOFLEX"),
382 .priv_data_size = sizeof(MOFLEXDemuxContext),
383 .read_probe = moflex_probe,
384 .read_header = moflex_read_header,
385 .read_packet = moflex_read_packet,
386 .read_seek = moflex_read_seek,
387 .read_close = moflex_read_close,
388 .extensions = "moflex",
389 .flags = AVFMT_GENERIC_INDEX,