avformat/mxfdec: Check edit unit for overflow in mxf_set_current_edit_unit()
[FFMpeg-mirror.git] / libavformat / frmdec.c
blob412430e4fcfcfedd66d72ec63a748483fb6cd244
1 /*
2 * Megalux Frame demuxer
3 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
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 /**
23 * @file
24 * Megalux Frame demuxer
27 #include "libavutil/imgutils.h"
28 #include "libavutil/intreadwrite.h"
29 #include "avformat.h"
30 #include "demux.h"
32 static const enum AVPixelFormat frm_pix_fmt_tags[] = {
33 AV_PIX_FMT_RGB555,
34 AV_PIX_FMT_RGB0,
35 AV_PIX_FMT_RGB24,
36 AV_PIX_FMT_BGR0,
37 AV_PIX_FMT_BGRA,
40 typedef struct {
41 int count;
42 } FrmContext;
44 static int frm_read_probe(const AVProbeData *p)
46 if (p->buf_size > 8 &&
47 p->buf[0] == 'F' && p->buf[1] == 'R' && p->buf[2] == 'M' &&
48 AV_RL16(&p->buf[4]) && AV_RL16(&p->buf[6]))
49 return AVPROBE_SCORE_MAX / 4;
50 return 0;
53 static int frm_read_header(AVFormatContext *avctx)
55 AVIOContext *pb = avctx->pb;
56 AVStream *st = avformat_new_stream(avctx, 0);
57 unsigned idx;
59 if (!st)
60 return AVERROR(ENOMEM);
62 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
63 st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
64 avio_skip(pb, 3);
66 idx = avio_r8(pb) - 1;
67 if (idx >= FF_ARRAY_ELEMS(frm_pix_fmt_tags))
68 return AVERROR_INVALIDDATA;
69 st->codecpar->format = frm_pix_fmt_tags[idx];
71 st->codecpar->codec_tag = 0;
72 st->codecpar->width = avio_rl16(pb);
73 st->codecpar->height = avio_rl16(pb);
74 return 0;
77 static int frm_read_packet(AVFormatContext *avctx, AVPacket *pkt)
79 FrmContext *s = avctx->priv_data;
80 AVCodecParameters *par = avctx->streams[0]->codecpar;
81 int packet_size, ret;
83 if (s->count)
84 return AVERROR_EOF;
86 packet_size = av_image_get_buffer_size(par->format, par->width, par->height, 1);
87 if (packet_size < 0)
88 return AVERROR_INVALIDDATA;
90 ret = av_get_packet(avctx->pb, pkt, packet_size);
91 if (ret < 0)
92 return ret;
94 if (par->format == AV_PIX_FMT_BGRA) {
95 int i;
96 for (i = 3; i + 1 <= pkt->size; i += 4)
97 pkt->data[i] = 0xFF - pkt->data[i];
100 pkt->stream_index = 0;
101 s->count++;
103 return 0;
106 const FFInputFormat ff_frm_demuxer = {
107 .p.name = "frm",
108 .p.long_name = NULL_IF_CONFIG_SMALL("Megalux Frame"),
109 .priv_data_size = sizeof(FrmContext),
110 .read_probe = frm_read_probe,
111 .read_header = frm_read_header,
112 .read_packet = frm_read_packet,