avformat/mxfdec: Check edit unit for overflow in mxf_set_current_edit_unit()
[FFMpeg-mirror.git] / libavformat / c93.c
blob933fe4a99e2b340af14b946fe87300cbd97b67f2
1 /*
2 * Interplay C93 demuxer
3 * Copyright (c) 2007 Anssi Hannula <anssi.hannula@gmail.com>
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 "avformat.h"
23 #include "demux.h"
24 #include "internal.h"
25 #include "voc.h"
26 #include "libavutil/intreadwrite.h"
28 typedef struct C93BlockRecord {
29 uint16_t index;
30 uint8_t length;
31 uint8_t frames;
32 } C93BlockRecord;
34 typedef struct C93DemuxContext {
35 VocDecContext voc;
37 C93BlockRecord block_records[512];
38 int current_block;
40 uint32_t frame_offsets[32];
41 int current_frame;
42 int next_pkt_is_audio;
44 AVStream *audio;
45 } C93DemuxContext;
47 static int probe(const AVProbeData *p)
49 int i;
50 int index = 1;
51 if (p->buf_size < 16)
52 return 0;
53 for (i = 0; i < 16; i += 4) {
54 if (AV_RL16(p->buf + i) != index || !p->buf[i + 2] || !p->buf[i + 3])
55 return 0;
56 index += p->buf[i + 2];
58 return AVPROBE_SCORE_MAX;
61 static int read_header(AVFormatContext *s)
63 AVStream *video;
64 AVIOContext *pb = s->pb;
65 C93DemuxContext *c93 = s->priv_data;
66 int i;
67 int framecount = 0;
69 for (i = 0; i < 512; i++) {
70 c93->block_records[i].index = avio_rl16(pb);
71 c93->block_records[i].length = avio_r8(pb);
72 c93->block_records[i].frames = avio_r8(pb);
73 if (c93->block_records[i].frames > 32) {
74 av_log(s, AV_LOG_ERROR, "too many frames in block\n");
75 return AVERROR_INVALIDDATA;
77 framecount += c93->block_records[i].frames;
80 /* Audio streams are added if audio packets are found */
81 s->ctx_flags |= AVFMTCTX_NOHEADER;
83 video = avformat_new_stream(s, NULL);
84 if (!video)
85 return AVERROR(ENOMEM);
87 video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
88 video->codecpar->codec_id = AV_CODEC_ID_C93;
89 video->codecpar->width = 320;
90 video->codecpar->height = 192;
91 /* 4:3 320x200 with 8 empty lines */
92 video->sample_aspect_ratio = (AVRational) { 5, 6 };
93 avpriv_set_pts_info(video, 64, 2, 25);
94 video->nb_frames = framecount;
95 video->duration = framecount;
96 video->start_time = 0;
98 c93->current_block = 0;
99 c93->current_frame = 0;
100 c93->next_pkt_is_audio = 0;
101 return 0;
104 #define C93_HAS_PALETTE 0x01
105 #define C93_FIRST_FRAME 0x02
107 static int read_packet(AVFormatContext *s, AVPacket *pkt)
109 AVIOContext *pb = s->pb;
110 C93DemuxContext *c93 = s->priv_data;
111 C93BlockRecord *br = &c93->block_records[c93->current_block];
112 int datasize;
113 int ret, i;
115 if (c93->next_pkt_is_audio) {
116 c93->current_frame++;
117 c93->next_pkt_is_audio = 0;
118 datasize = avio_rl16(pb);
119 if (datasize > 42) {
120 if (!c93->audio) {
121 c93->audio = avformat_new_stream(s, NULL);
122 if (!c93->audio)
123 return AVERROR(ENOMEM);
124 c93->audio->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
126 avio_skip(pb, 26); /* VOC header */
127 ret = ff_voc_get_packet(s, pkt, c93->audio, datasize - 26);
128 if (ret > 0) {
129 pkt->stream_index = 1;
130 pkt->flags |= AV_PKT_FLAG_KEY;
131 return ret;
135 if (c93->current_frame >= br->frames) {
136 if (c93->current_block >= 511 || !br[1].length)
137 return AVERROR_EOF;
138 br++;
139 c93->current_block++;
140 c93->current_frame = 0;
143 if (c93->current_frame == 0) {
144 avio_seek(pb, br->index * 2048, SEEK_SET);
145 for (i = 0; i < 32; i++) {
146 c93->frame_offsets[i] = avio_rl32(pb);
150 avio_seek(pb,br->index * 2048 +
151 c93->frame_offsets[c93->current_frame], SEEK_SET);
152 datasize = avio_rl16(pb); /* video frame size */
154 ret = av_new_packet(pkt, datasize + 768 + 1);
155 if (ret < 0)
156 return ret;
157 pkt->data[0] = 0;
158 pkt->size = datasize + 1;
160 ret = avio_read(pb, pkt->data + 1, datasize);
161 if (ret < datasize) {
162 return AVERROR(EIO);
165 datasize = avio_rl16(pb); /* palette size */
166 if (datasize) {
167 if (datasize != 768) {
168 av_log(s, AV_LOG_ERROR, "invalid palette size %u\n", datasize);
169 return AVERROR_INVALIDDATA;
171 pkt->data[0] |= C93_HAS_PALETTE;
172 ret = avio_read(pb, pkt->data + pkt->size, datasize);
173 if (ret < datasize) {
174 return AVERROR(EIO);
176 pkt->size += 768;
178 pkt->stream_index = 0;
179 c93->next_pkt_is_audio = 1;
181 /* only the first frame is guaranteed to not reference previous frames */
182 if (c93->current_block == 0 && c93->current_frame == 0) {
183 pkt->flags |= AV_PKT_FLAG_KEY;
184 pkt->data[0] |= C93_FIRST_FRAME;
186 return 0;
189 const FFInputFormat ff_c93_demuxer = {
190 .p.name = "c93",
191 .p.long_name = NULL_IF_CONFIG_SMALL("Interplay C93"),
192 .priv_data_size = sizeof(C93DemuxContext),
193 .read_probe = probe,
194 .read_header = read_header,
195 .read_packet = read_packet,