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 value = 2 * value + ret;
71 static int pop_length(BitReader *br, AVIOContext *pb)
75 while ((ret = pop(br, pb)) == 0)
83 static int read_var_byte(AVFormatContext *s, unsigned *out)
85 AVIOContext *pb = s->pb;
86 unsigned value = 0, data;
94 value = (data & 0x7F) << 7;
102 value = ((data & 0x7F) | value) << 7;
104 if (!(data & 0x80)) {
110 value = (((data & 0x7F) | value) << 7) | avio_r8(pb);
116 static int moflex_probe(const AVProbeData *p)
121 bytestream2_init(&gb, p->buf, p->buf_size);
123 if (bytestream2_get_be16(&gb) != 0x4C32)
127 bytestream2_skip(&gb, 10);
128 if (bytestream2_get_be16(&gb) == 0)
132 while (bytestream2_get_bytes_left(&gb) > 0) {
133 int type = bytestream2_get_byte(&gb);
134 int size = bytestream2_get_byte(&gb);
137 score += 5 * (size == 0);
140 if ((type == 1 && size == 12) ||
141 (type == 2 && size == 6) ||
142 (type == 3 && size == 13) ||
143 (type == 4 && size == 2))
145 bytestream2_skip(&gb, size);
148 return FFMIN(AVPROBE_SCORE_MAX, score);
151 static int moflex_read_sync(AVFormatContext *s)
153 MOFLEXDemuxContext *m = s->priv_data;
154 AVIOContext *pb = s->pb;
156 if (avio_rb16(pb) != 0x4C32) {
159 avio_seek(pb, -2, SEEK_CUR);
164 m->ts = avio_rb64(pb);
165 m->size = avio_rb16(pb) + 1;
167 while (!avio_feof(pb)) {
168 unsigned type, ssize, codec_id = 0;
169 unsigned codec_type, width = 0, height = 0, sample_rate = 0, channels = 0;
170 int stream_index = -1;
174 read_var_byte(s, &type);
175 read_var_byte(s, &ssize);
180 avio_skip(pb, ssize);
183 codec_type = AVMEDIA_TYPE_AUDIO;
184 stream_index = avio_r8(pb);
185 codec_id = avio_r8(pb);
187 case 0: codec_id = AV_CODEC_ID_FASTAUDIO; break;
188 case 1: codec_id = AV_CODEC_ID_ADPCM_IMA_MOFLEX; break;
189 case 2: codec_id = AV_CODEC_ID_PCM_S16LE; break;
191 av_log(s, AV_LOG_ERROR, "Unsupported audio codec: %d\n", codec_id);
192 return AVERROR_PATCHWELCOME;
194 sample_rate = avio_rb24(pb) + 1;
195 channels = avio_r8(pb) + 1;
199 codec_type = AVMEDIA_TYPE_VIDEO;
200 stream_index = avio_r8(pb);
201 codec_id = avio_r8(pb);
203 case 0: codec_id = AV_CODEC_ID_MOBICLIP; break;
205 av_log(s, AV_LOG_ERROR, "Unsupported video codec: %d\n", codec_id);
206 return AVERROR_PATCHWELCOME;
208 fps.num = avio_rb16(pb);
209 fps.den = avio_rb16(pb);
210 width = avio_rb16(pb);
211 height = avio_rb16(pb);
212 format = AV_PIX_FMT_YUV420P;
213 avio_skip(pb, type == 3 ? 3 : 2);
216 codec_type = AVMEDIA_TYPE_DATA;
217 stream_index = avio_r8(pb);
222 if (stream_index == s->nb_streams) {
223 AVStream *st = avformat_new_stream(s, NULL);
226 return AVERROR(ENOMEM);
228 st->codecpar->codec_type = codec_type;
229 st->codecpar->codec_id = codec_id;
230 st->codecpar->width = width;
231 st->codecpar->height = height;
232 st->codecpar->sample_rate= sample_rate;
233 st->codecpar->channels = channels;
234 st->codecpar->format = format;
235 st->priv_data = av_packet_alloc();
237 return AVERROR(ENOMEM);
240 avpriv_set_pts_info(st, 63, 1, sample_rate);
242 avpriv_set_pts_info(st, 63, fps.den, fps.num);
249 static int moflex_read_header(AVFormatContext *s)
253 ret = moflex_read_sync(s);
257 s->ctx_flags |= AVFMTCTX_NOHEADER;
258 avio_seek(s->pb, 0, SEEK_SET);
263 static int moflex_read_packet(AVFormatContext *s, AVPacket *pkt)
265 MOFLEXDemuxContext *m = s->priv_data;
266 AVIOContext *pb = s->pb;
267 BitReader *br = &m->br;
270 while (!avio_feof(pb)) {
272 m->pos = avio_tell(pb);
274 ret = moflex_read_sync(s);
278 m->flags = avio_r8(pb);
283 while ((avio_tell(pb) < m->pos + m->size) && !avio_feof(pb) && avio_r8(pb)) {
284 int stream_index, bits, pkt_size, endframe;
289 avio_seek(pb, -1, SEEK_CUR);
290 br->pos = br->last = 0;
292 bits = pop_length(br, pb);
295 stream_index = pop_int(br, pb, bits);
296 if (stream_index < 0)
298 if (stream_index >= s->nb_streams)
299 return AVERROR_INVALIDDATA;
301 endframe = pop(br, pb);
305 bits = pop_length(br, pb);
308 pop_int(br, pb, bits);
310 bits = pop_length(br, pb);
313 pop_int(br, pb, bits * 2 + 26);
316 pkt_size = pop_int(br, pb, 13) + 1;
317 packet = s->streams[stream_index]->priv_data;
319 avio_skip(pb, pkt_size);
323 ret = av_append_packet(pb, packet, pkt_size);
326 if (endframe && packet->size > 0) {
327 av_packet_move_ref(pkt, packet);
329 pkt->stream_index = stream_index;
330 if (s->streams[stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
332 if (pkt->data[0] & 0x80)
333 pkt->flags |= AV_PKT_FLAG_KEY;
335 pkt->flags |= AV_PKT_FLAG_KEY;
343 if (m->flags % 2 == 0)
344 avio_seek(pb, m->pos + m->size, SEEK_SET);
350 static int moflex_read_seek(AVFormatContext *s, int stream_index,
351 int64_t pts, int flags)
353 MOFLEXDemuxContext *m = s->priv_data;
360 static int moflex_read_close(AVFormatContext *s)
362 for (int i = 0; i < s->nb_streams; i++) {
363 AVPacket *packet = s->streams[i]->priv_data;
365 av_packet_free(&packet);
366 s->streams[i]->priv_data = 0;
372 AVInputFormat ff_moflex_demuxer = {
374 .long_name = NULL_IF_CONFIG_SMALL("MobiClip MOFLEX"),
375 .priv_data_size = sizeof(MOFLEXDemuxContext),
376 .read_probe = moflex_probe,
377 .read_header = moflex_read_header,
378 .read_packet = moflex_read_packet,
379 .read_seek = moflex_read_seek,
380 .read_close = moflex_read_close,
381 .extensions = "moflex",
382 .flags = AVFMT_GENERIC_INDEX,