handle malloc failures in ff_get_wav_header
[libav.git] / libavformat / dxa.c
1 /*
2 * DXA demuxer
3 * Copyright (c) 2007 Konstantin Shishkov
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "riff.h"
25
26 #define DXA_EXTRA_SIZE 9
27
28 typedef struct{
29 int frames;
30 int has_sound;
31 int bpc;
32 uint32_t bytes_left;
33 int64_t wavpos, vidpos;
34 int readvid;
35 }DXAContext;
36
37 static int dxa_probe(AVProbeData *p)
38 {
39 int w, h;
40 if (p->buf_size < 15)
41 return 0;
42 w = AV_RB16(p->buf + 11);
43 h = AV_RB16(p->buf + 13);
44 /* check file header */
45 if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
46 p->buf[2] == 'X' && p->buf[3] == 'A' &&
47 w && w <= 2048 && h && h <= 2048)
48 return AVPROBE_SCORE_MAX;
49 else
50 return 0;
51 }
52
53 static int dxa_read_header(AVFormatContext *s, AVFormatParameters *ap)
54 {
55 AVIOContext *pb = s->pb;
56 DXAContext *c = s->priv_data;
57 AVStream *st, *ast;
58 uint32_t tag;
59 int32_t fps;
60 int w, h;
61 int num, den;
62 int flags;
63 int ret;
64
65 tag = avio_rl32(pb);
66 if (tag != MKTAG('D', 'E', 'X', 'A'))
67 return -1;
68 flags = avio_r8(pb);
69 c->frames = avio_rb16(pb);
70 if(!c->frames){
71 av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
72 return -1;
73 }
74
75 fps = avio_rb32(pb);
76 if(fps > 0){
77 den = 1000;
78 num = fps;
79 }else if (fps < 0){
80 den = 100000;
81 num = -fps;
82 }else{
83 den = 10;
84 num = 1;
85 }
86 w = avio_rb16(pb);
87 h = avio_rb16(pb);
88 c->has_sound = 0;
89
90 st = av_new_stream(s, 0);
91 if (!st)
92 return -1;
93
94 // Parse WAV data header
95 if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){
96 uint32_t size, fsize;
97 c->has_sound = 1;
98 size = avio_rb32(pb);
99 c->vidpos = avio_tell(pb) + size;
100 avio_skip(pb, 16);
101 fsize = avio_rl32(pb);
102
103 ast = av_new_stream(s, 0);
104 if (!ast)
105 return -1;
106 ret = ff_get_wav_header(pb, ast->codec, fsize);
107 if (ret < 0)
108 return ret;
109 // find 'data' chunk
110 while(avio_tell(pb) < c->vidpos && !pb->eof_reached){
111 tag = avio_rl32(pb);
112 fsize = avio_rl32(pb);
113 if(tag == MKTAG('d', 'a', 't', 'a')) break;
114 avio_skip(pb, fsize);
115 }
116 c->bpc = (fsize + c->frames - 1) / c->frames;
117 if(ast->codec->block_align)
118 c->bpc = ((c->bpc + ast->codec->block_align - 1) / ast->codec->block_align) * ast->codec->block_align;
119 c->bytes_left = fsize;
120 c->wavpos = avio_tell(pb);
121 avio_seek(pb, c->vidpos, SEEK_SET);
122 }
123
124 /* now we are ready: build format streams */
125 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
126 st->codec->codec_id = CODEC_ID_DXA;
127 st->codec->width = w;
128 st->codec->height = h;
129 av_reduce(&den, &num, den, num, (1UL<<31)-1);
130 av_set_pts_info(st, 33, num, den);
131 /* flags & 0x80 means that image is interlaced,
132 * flags & 0x40 means that image has double height
133 * either way set true height
134 */
135 if(flags & 0xC0){
136 st->codec->height >>= 1;
137 }
138 c->readvid = !c->has_sound;
139 c->vidpos = avio_tell(pb);
140 s->start_time = 0;
141 s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
142 av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
143
144 return 0;
145 }
146
147 static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
148 {
149 DXAContext *c = s->priv_data;
150 int ret;
151 uint32_t size;
152 uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
153 int pal_size = 0;
154
155 if(!c->readvid && c->has_sound && c->bytes_left){
156 c->readvid = 1;
157 avio_seek(s->pb, c->wavpos, SEEK_SET);
158 size = FFMIN(c->bytes_left, c->bpc);
159 ret = av_get_packet(s->pb, pkt, size);
160 pkt->stream_index = 1;
161 if(ret != size)
162 return AVERROR(EIO);
163 c->bytes_left -= size;
164 c->wavpos = avio_tell(s->pb);
165 return 0;
166 }
167 avio_seek(s->pb, c->vidpos, SEEK_SET);
168 while(!s->pb->eof_reached && c->frames){
169 avio_read(s->pb, buf, 4);
170 switch(AV_RL32(buf)){
171 case MKTAG('N', 'U', 'L', 'L'):
172 if(av_new_packet(pkt, 4 + pal_size) < 0)
173 return AVERROR(ENOMEM);
174 pkt->stream_index = 0;
175 if(pal_size) memcpy(pkt->data, pal, pal_size);
176 memcpy(pkt->data + pal_size, buf, 4);
177 c->frames--;
178 c->vidpos = avio_tell(s->pb);
179 c->readvid = 0;
180 return 0;
181 case MKTAG('C', 'M', 'A', 'P'):
182 pal_size = 768+4;
183 memcpy(pal, buf, 4);
184 avio_read(s->pb, pal + 4, 768);
185 break;
186 case MKTAG('F', 'R', 'A', 'M'):
187 avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4);
188 size = AV_RB32(buf + 5);
189 if(size > 0xFFFFFF){
190 av_log(s, AV_LOG_ERROR, "Frame size is too big: %d\n", size);
191 return -1;
192 }
193 if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
194 return AVERROR(ENOMEM);
195 memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
196 ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
197 if(ret != size){
198 av_free_packet(pkt);
199 return AVERROR(EIO);
200 }
201 if(pal_size) memcpy(pkt->data, pal, pal_size);
202 pkt->stream_index = 0;
203 c->frames--;
204 c->vidpos = avio_tell(s->pb);
205 c->readvid = 0;
206 return 0;
207 default:
208 av_log(s, AV_LOG_ERROR, "Unknown tag %c%c%c%c\n", buf[0], buf[1], buf[2], buf[3]);
209 return -1;
210 }
211 }
212 return AVERROR(EIO);
213 }
214
215 AVInputFormat ff_dxa_demuxer = {
216 "dxa",
217 NULL_IF_CONFIG_SMALL("DXA"),
218 sizeof(DXAContext),
219 dxa_probe,
220 dxa_read_header,
221 dxa_read_packet,
222 };