]> git.sesse.net Git - ffmpeg/blob - libavformat/moflex.c
avformat/moflex: Check pop_int() for overflow
[ffmpeg] / libavformat / moflex.c
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
39     BitReader br;
40 } MOFLEXDemuxContext;
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
153 static int moflex_read_sync(AVFormatContext *s)
154 {
155     MOFLEXDemuxContext *m = s->priv_data;
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) {
225             AVStream *st = avformat_new_stream(s, NULL);
226
227             if (!st)
228                 return AVERROR(ENOMEM);
229
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();
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
251 static int moflex_read_header(AVFormatContext *s)
252 {
253     int ret;
254
255     ret = moflex_read_sync(s);
256     if (ret < 0)
257         return ret;
258
259     s->ctx_flags |= AVFMTCTX_NOHEADER;
260     avio_seek(s->pb, 0, SEEK_SET);
261
262     return 0;
263 }
264
265 static int moflex_read_packet(AVFormatContext *s, AVPacket *pkt)
266 {
267     MOFLEXDemuxContext *m = s->priv_data;
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             avio_seek(pb, m->pos + m->size, SEEK_SET);
347     }
348
349     return AVERROR_EOF;
350 }
351
352 static int moflex_read_seek(AVFormatContext *s, int stream_index,
353                             int64_t pts, int flags)
354 {
355     MOFLEXDemuxContext *m = s->priv_data;
356
357     m->in_block = 0;
358
359     return -1;
360 }
361
362 static int moflex_read_close(AVFormatContext *s)
363 {
364     for (int i = 0; i < s->nb_streams; i++) {
365         AVPacket *packet = s->streams[i]->priv_data;
366
367         av_packet_free(&packet);
368         s->streams[i]->priv_data = 0;
369     }
370
371     return 0;
372 }
373
374 AVInputFormat ff_moflex_demuxer = {
375     .name           = "moflex",
376     .long_name      = NULL_IF_CONFIG_SMALL("MobiClip MOFLEX"),
377     .priv_data_size = sizeof(MOFLEXDemuxContext),
378     .read_probe     = moflex_probe,
379     .read_header    = moflex_read_header,
380     .read_packet    = moflex_read_packet,
381     .read_seek      = moflex_read_seek,
382     .read_close     = moflex_read_close,
383     .extensions     = "moflex",
384     .flags          = AVFMT_GENERIC_INDEX,
385 };