/src/ffmpeg/libavformat/swfdec.c
Line | Count | Source |
1 | | /* |
2 | | * Flash Compatible Streaming Format demuxer |
3 | | * Copyright (c) 2000 Fabrice Bellard |
4 | | * Copyright (c) 2003 Tinic Uro |
5 | | * |
6 | | * This file is part of FFmpeg. |
7 | | * |
8 | | * FFmpeg is free software; you can redistribute it and/or |
9 | | * modify it under the terms of the GNU Lesser General Public |
10 | | * License as published by the Free Software Foundation; either |
11 | | * version 2.1 of the License, or (at your option) any later version. |
12 | | * |
13 | | * FFmpeg is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | | * Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with FFmpeg; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | */ |
22 | | |
23 | | #include "config.h" |
24 | | |
25 | | #if CONFIG_ZLIB |
26 | | #include <zlib.h> |
27 | | #endif |
28 | | |
29 | | #include "libavutil/avassert.h" |
30 | | #include "libavutil/channel_layout.h" |
31 | | #include "libavutil/imgutils.h" |
32 | | #include "libavutil/internal.h" |
33 | | #include "libavutil/intreadwrite.h" |
34 | | #include "libavutil/mem.h" |
35 | | #include "libavcodec/get_bits.h" |
36 | | #include "demux.h" |
37 | | #include "swf.h" |
38 | | #include "flv.h" |
39 | | |
40 | | typedef struct SWFDecContext { |
41 | | int samples_per_frame; |
42 | | int frame_rate; |
43 | | #if CONFIG_ZLIB |
44 | 37.5k | #define ZBUF_SIZE 4096 |
45 | | AVIOContext *zpb; |
46 | | uint8_t *zbuf_in; |
47 | | uint8_t *zbuf_out; |
48 | | z_stream zstream; |
49 | | #endif |
50 | | } SWFDecContext; |
51 | | |
52 | | static const AVCodecTag swf_audio_codec_tags[] = { |
53 | | { AV_CODEC_ID_PCM_S16LE, 0x00 }, |
54 | | { AV_CODEC_ID_ADPCM_SWF, 0x01 }, |
55 | | { AV_CODEC_ID_MP3, 0x02 }, |
56 | | { AV_CODEC_ID_PCM_S16LE, 0x03 }, |
57 | | { AV_CODEC_ID_NELLYMOSER, 0x06 }, |
58 | | { AV_CODEC_ID_NONE, 0 }, |
59 | | }; |
60 | | |
61 | | static int get_swf_tag(AVIOContext *pb, int *len_ptr) |
62 | 33.3M | { |
63 | 33.3M | int tag, len; |
64 | | |
65 | 33.3M | if (avio_feof(pb)) |
66 | 10.6k | return AVERROR_EOF; |
67 | | |
68 | 33.3M | tag = avio_rl16(pb); |
69 | 33.3M | len = tag & 0x3f; |
70 | 33.3M | tag = tag >> 6; |
71 | 33.3M | if (len == 0x3f) { |
72 | 158k | len = avio_rl32(pb); |
73 | 158k | } |
74 | 33.3M | *len_ptr = len; |
75 | 33.3M | return tag; |
76 | 33.3M | } |
77 | | |
78 | | |
79 | | static int swf_probe(const AVProbeData *p) |
80 | 958k | { |
81 | 958k | GetBitContext gb; |
82 | 958k | int len, xmin, xmax, ymin, ymax; |
83 | | |
84 | 958k | if(p->buf_size < 15) |
85 | 144k | return 0; |
86 | | |
87 | | /* check file header */ |
88 | 813k | if ( AV_RB24(p->buf) != AV_RB24("CWS") |
89 | 813k | && AV_RB24(p->buf) != AV_RB24("FWS")) |
90 | 810k | return 0; |
91 | | |
92 | 3.70k | if ( AV_RB24(p->buf) == AV_RB24("CWS") |
93 | 858 | && p->buf[3] <= 20) |
94 | 571 | return AVPROBE_SCORE_MAX / 4 + 1; |
95 | | |
96 | 3.13k | if (init_get_bits8(&gb, p->buf + 8, p->buf_size - 8) < 0) |
97 | 0 | return 0; |
98 | | |
99 | 3.13k | len = get_bits(&gb, 5); |
100 | 3.13k | if (!len) |
101 | 447 | return 0; |
102 | 2.68k | xmin = get_bits_long(&gb, len); |
103 | 2.68k | xmax = get_bits_long(&gb, len); |
104 | 2.68k | ymin = get_bits_long(&gb, len); |
105 | 2.68k | ymax = get_bits_long(&gb, len); |
106 | 2.68k | if (xmin || ymin || !xmax || !ymax) |
107 | 1.64k | return 0; |
108 | | |
109 | 1.04k | if (p->buf[3] >= 20 || xmax < 16 || ymax < 16) |
110 | 913 | return AVPROBE_SCORE_MAX / 4; |
111 | | |
112 | 128 | return AVPROBE_SCORE_EXTENSION + 1; |
113 | 1.04k | } |
114 | | |
115 | | #if CONFIG_ZLIB |
116 | | static int zlib_refill(void *opaque, uint8_t *buf, int buf_size) |
117 | 166k | { |
118 | 166k | AVFormatContext *s = opaque; |
119 | 166k | SWFDecContext *swf = s->priv_data; |
120 | 166k | z_stream *z = &swf->zstream; |
121 | 166k | int ret; |
122 | | |
123 | 166k | retry: |
124 | 166k | if (!z->avail_in) { |
125 | 24.9k | int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE); |
126 | 24.9k | if (n < 0) |
127 | 11.2k | return n; |
128 | 13.7k | z->next_in = swf->zbuf_in; |
129 | 13.7k | z->avail_in = n; |
130 | 13.7k | } |
131 | | |
132 | 155k | z->next_out = buf; |
133 | 155k | z->avail_out = buf_size; |
134 | | |
135 | 155k | ret = inflate(z, Z_NO_FLUSH); |
136 | 155k | if (ret == Z_STREAM_END) |
137 | 6 | return AVERROR_EOF; |
138 | 155k | if (ret != Z_OK) |
139 | 1.05k | return AVERROR(EINVAL); |
140 | | |
141 | 154k | if (buf_size - z->avail_out == 0) |
142 | 106 | goto retry; |
143 | | |
144 | 154k | return buf_size - z->avail_out; |
145 | 154k | } |
146 | | |
147 | | static av_cold int swf_read_close(AVFormatContext *avctx); |
148 | | #endif |
149 | | |
150 | | static int swf_read_header(AVFormatContext *s) |
151 | 6.23k | { |
152 | 6.23k | SWFDecContext *swf = s->priv_data; |
153 | 6.23k | AVIOContext *pb = s->pb; |
154 | 6.23k | int nbits, len, tag; |
155 | | |
156 | 6.23k | tag = avio_rb32(pb) & 0xffffff00; |
157 | 6.23k | avio_rl32(pb); |
158 | | |
159 | 6.23k | if (tag == MKBETAG('C', 'W', 'S', 0)) { |
160 | 4.21k | av_log(s, AV_LOG_INFO, "SWF compressed file detected\n"); |
161 | 4.21k | #if CONFIG_ZLIB |
162 | 4.21k | if (inflateInit(&swf->zstream) != Z_OK) { |
163 | 0 | av_log(s, AV_LOG_ERROR, "Unable to init zlib context\n"); |
164 | 0 | return AVERROR(EINVAL); |
165 | 0 | } |
166 | 4.21k | if (!(swf->zbuf_in = av_malloc(ZBUF_SIZE)) || |
167 | 4.21k | !(swf->zbuf_out = av_malloc(ZBUF_SIZE)) || |
168 | 4.21k | !(swf->zpb = avio_alloc_context(swf->zbuf_out, ZBUF_SIZE, 0, |
169 | 4.21k | s, zlib_refill, NULL, NULL))) { |
170 | 0 | swf_read_close(s); |
171 | 0 | return AVERROR(ENOMEM); |
172 | 0 | } |
173 | 4.21k | swf->zpb->seekable = 0; |
174 | 4.21k | pb = swf->zpb; |
175 | | #else |
176 | | av_log(s, AV_LOG_ERROR, "zlib support is required to read SWF compressed files\n"); |
177 | | return AVERROR(ENOSYS); |
178 | | #endif |
179 | 4.21k | } else if (tag != MKBETAG('F', 'W', 'S', 0)) |
180 | 160 | return AVERROR_INVALIDDATA; |
181 | | /* skip rectangle size */ |
182 | 6.07k | nbits = avio_r8(pb) >> 3; |
183 | 6.07k | len = (4 * nbits - 3 + 7) / 8; |
184 | 6.07k | avio_skip(pb, len); |
185 | 6.07k | swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */ |
186 | 6.07k | avio_rl16(pb); /* frame count */ |
187 | | |
188 | 6.07k | swf->samples_per_frame = 0; |
189 | 6.07k | s->ctx_flags |= AVFMTCTX_NOHEADER; |
190 | 6.07k | return 0; |
191 | 6.23k | } |
192 | | |
193 | | static AVStream *create_new_audio_stream(AVFormatContext *s, int id, int info) |
194 | 10.5k | { |
195 | 10.5k | int sample_rate_code, sample_size_code; |
196 | 10.5k | AVStream *ast = avformat_new_stream(s, NULL); |
197 | 10.5k | if (!ast) |
198 | 0 | return NULL; |
199 | 10.5k | ast->id = id; |
200 | 10.5k | av_channel_layout_default(&ast->codecpar->ch_layout, 1 + (info & 1)); |
201 | 10.5k | ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
202 | 10.5k | ast->codecpar->codec_id = ff_codec_get_id(swf_audio_codec_tags, info>>4 & 15); |
203 | 10.5k | ffstream(ast)->need_parsing = AVSTREAM_PARSE_FULL; |
204 | 10.5k | sample_rate_code = info>>2 & 3; |
205 | 10.5k | sample_size_code = info>>1 & 1; |
206 | 10.5k | if (!sample_size_code && ast->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE) |
207 | 1.15k | ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8; |
208 | 10.5k | ast->codecpar->sample_rate = 44100 >> (3 - sample_rate_code); |
209 | 10.5k | avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate); |
210 | 10.5k | return ast; |
211 | 10.5k | } |
212 | | |
213 | | static int swf_read_packet(AVFormatContext *s, AVPacket *pkt) |
214 | 10.1M | { |
215 | 10.1M | SWFDecContext *swf = s->priv_data; |
216 | 10.1M | AVIOContext *pb = s->pb; |
217 | 10.1M | AVStream *vst = NULL, *ast = NULL, *st = 0; |
218 | 10.1M | int tag, len, i, frame, v, res; |
219 | | |
220 | 10.1M | #if CONFIG_ZLIB |
221 | 10.1M | if (swf->zpb) |
222 | 10.1M | pb = swf->zpb; |
223 | 10.1M | #endif |
224 | | |
225 | 33.3M | for(;;) { |
226 | 33.3M | uint64_t pos = avio_tell(pb); |
227 | 33.3M | tag = get_swf_tag(pb, &len); |
228 | 33.3M | if (tag < 0) |
229 | 10.6k | return tag; |
230 | 33.3M | if (len < 0) { |
231 | 9.30k | av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len); |
232 | 9.30k | return AVERROR_INVALIDDATA; |
233 | 9.30k | } |
234 | 33.2M | if (tag == TAG_VIDEOSTREAM) { |
235 | 1.22M | int ch_id = avio_rl16(pb); |
236 | 1.22M | len -= 2; |
237 | | |
238 | 127M | for (i=0; i<s->nb_streams; i++) { |
239 | 127M | st = s->streams[i]; |
240 | 127M | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) |
241 | 1.18M | goto skip; |
242 | 127M | } |
243 | | |
244 | 42.5k | avio_rl16(pb); |
245 | 42.5k | avio_rl16(pb); |
246 | 42.5k | avio_rl16(pb); |
247 | 42.5k | avio_r8(pb); |
248 | | /* Check for FLV1 */ |
249 | 42.5k | vst = avformat_new_stream(s, NULL); |
250 | 42.5k | if (!vst) |
251 | 0 | return AVERROR(ENOMEM); |
252 | 42.5k | vst->id = ch_id; |
253 | 42.5k | vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
254 | 42.5k | vst->codecpar->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb)); |
255 | 42.5k | avpriv_set_pts_info(vst, 16, 256, swf->frame_rate); |
256 | 42.5k | len -= 8; |
257 | 32.0M | } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) { |
258 | | /* streaming found */ |
259 | | |
260 | 184k | for (i=0; i<s->nb_streams; i++) { |
261 | 182k | st = s->streams[i]; |
262 | 182k | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) |
263 | 7.16k | goto skip; |
264 | 182k | } |
265 | | |
266 | 2.04k | avio_r8(pb); |
267 | 2.04k | v = avio_r8(pb); |
268 | 2.04k | swf->samples_per_frame = avio_rl16(pb); |
269 | 2.04k | ast = create_new_audio_stream(s, -1, v); /* -1 to avoid clash with video stream ch_id */ |
270 | 2.04k | if (!ast) |
271 | 0 | return AVERROR(ENOMEM); |
272 | 2.04k | len -= 4; |
273 | 32.0M | } else if (tag == TAG_DEFINESOUND) { |
274 | | /* audio stream */ |
275 | 76.9k | int ch_id = avio_rl16(pb); |
276 | | |
277 | 3.08M | for (i=0; i<s->nb_streams; i++) { |
278 | 3.08M | st = s->streams[i]; |
279 | 3.08M | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->id == ch_id) |
280 | 68.4k | goto skip; |
281 | 3.08M | } |
282 | | |
283 | | // FIXME: The entire audio stream is stored in a single chunk/tag. Normally, |
284 | | // these are smaller audio streams in DEFINESOUND tags, but it's technically |
285 | | // possible they could be huge. Break it up into multiple packets if it's big. |
286 | 8.48k | v = avio_r8(pb); |
287 | 8.48k | ast = create_new_audio_stream(s, ch_id, v); |
288 | 8.48k | if (!ast) |
289 | 0 | return AVERROR(ENOMEM); |
290 | 8.48k | ast->duration = avio_rl32(pb); // number of samples |
291 | 8.48k | if (((v>>4) & 15) == 2) { // MP3 sound data record |
292 | 493 | ffstream(ast)->skip_samples = avio_rl16(pb); |
293 | 493 | len -= 2; |
294 | 493 | } |
295 | 8.48k | len -= 7; |
296 | 8.48k | if ((res = av_get_packet(pb, pkt, len)) < 0) |
297 | 525 | return res; |
298 | 7.96k | pkt->pos = pos; |
299 | 7.96k | pkt->stream_index = ast->index; |
300 | 7.96k | return pkt->size; |
301 | 31.9M | } else if (tag == TAG_VIDEOFRAME) { |
302 | 5.68M | int ch_id = avio_rl16(pb); |
303 | 5.68M | len -= 2; |
304 | 72.3M | for(i=0; i<s->nb_streams; i++) { |
305 | 71.2M | st = s->streams[i]; |
306 | 71.2M | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) { |
307 | 4.62M | int pkt_flags = 0; |
308 | 4.62M | frame = avio_rl16(pb); |
309 | 4.62M | len -= 2; |
310 | 4.62M | if (len <= 0) |
311 | 3.42k | goto skip; |
312 | 4.61M | if (st->codecpar->codec_id == AV_CODEC_ID_FLASHSV) { |
313 | 1.72k | unsigned flags = avio_r8(pb); |
314 | 1.72k | len--; |
315 | 1.72k | if (len <= 0) |
316 | 201 | goto skip; |
317 | 1.52k | pkt_flags |= (flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ? AV_PKT_FLAG_KEY : 0; |
318 | 1.52k | } |
319 | 4.61M | if ((res = av_get_packet(pb, pkt, len)) < 0) |
320 | 64 | return res; |
321 | 4.61M | pkt->pos = pos; |
322 | 4.61M | pkt->pts = frame; |
323 | 4.61M | pkt->stream_index = st->index; |
324 | 4.61M | pkt->flags |= pkt_flags; |
325 | 4.61M | return pkt->size; |
326 | 4.61M | } |
327 | 71.2M | } |
328 | 26.2M | } else if (tag == TAG_DEFINEBITSLOSSLESS || tag == TAG_DEFINEBITSLOSSLESS2) { |
329 | 111k | #if CONFIG_ZLIB |
330 | 111k | long out_len; |
331 | 111k | uint8_t *buf = NULL, *zbuf = NULL, *pal; |
332 | 111k | uint32_t colormap[AVPALETTE_COUNT] = {0}; |
333 | 111k | const int alpha_bmp = tag == TAG_DEFINEBITSLOSSLESS2; |
334 | 111k | const int colormapbpp = 3 + alpha_bmp; |
335 | 111k | int linesize, colormapsize = 0; |
336 | | |
337 | 111k | const int ch_id = avio_rl16(pb); |
338 | 111k | const int bmp_fmt = avio_r8(pb); |
339 | 111k | const int width = avio_rl16(pb); |
340 | 111k | const int height = avio_rl16(pb); |
341 | 111k | int pix_fmt; |
342 | | |
343 | 111k | len -= 2+1+2+2; |
344 | | |
345 | 111k | switch (bmp_fmt) { |
346 | 1.84k | case 3: // PAL-8 |
347 | 1.84k | linesize = width; |
348 | 1.84k | colormapsize = avio_r8(pb) + 1; |
349 | 1.84k | len--; |
350 | 1.84k | break; |
351 | 2.25k | case 4: // RGB15 |
352 | 2.25k | linesize = width * 2; |
353 | 2.25k | break; |
354 | 51.6k | case 5: // RGB24 (0RGB) |
355 | 51.6k | linesize = width * 4; |
356 | 51.6k | break; |
357 | 55.6k | default: |
358 | 55.6k | av_log(s, AV_LOG_ERROR, "invalid bitmap format %d, skipped\n", bmp_fmt); |
359 | 55.6k | goto bitmap_end_skip; |
360 | 111k | } |
361 | | |
362 | 55.7k | linesize = FFALIGN(linesize, 4); |
363 | | |
364 | 55.7k | if (av_image_check_size(width, height, 0, s) < 0 || |
365 | 52.6k | linesize >= INT_MAX / height || |
366 | 52.6k | linesize * height >= INT_MAX - colormapsize * colormapbpp) { |
367 | 3.10k | av_log(s, AV_LOG_ERROR, "invalid frame size %dx%d\n", width, height); |
368 | 3.10k | goto bitmap_end_skip; |
369 | 3.10k | } |
370 | | |
371 | 52.6k | out_len = colormapsize * colormapbpp + linesize * height; |
372 | | |
373 | 52.6k | ff_dlog(s, "bitmap: ch=%d fmt=%d %dx%d (linesize=%d) len=%d->%ld pal=%d\n", |
374 | 52.6k | ch_id, bmp_fmt, width, height, linesize, len, out_len, colormapsize); |
375 | | |
376 | 52.6k | if (len * 17373LL < out_len) |
377 | 31.3k | goto bitmap_end_skip; |
378 | | |
379 | 21.3k | zbuf = av_malloc(len); |
380 | 21.3k | if (!zbuf) { |
381 | 0 | res = AVERROR(ENOMEM); |
382 | 0 | goto bitmap_end; |
383 | 0 | } |
384 | | |
385 | 21.3k | len = avio_read(pb, zbuf, len); |
386 | 21.3k | if (len < 0) |
387 | 34 | goto bitmap_end_skip; |
388 | | |
389 | 21.3k | buf = av_malloc(out_len); |
390 | 21.3k | if (!buf) { |
391 | 0 | res = AVERROR(ENOMEM); |
392 | 0 | goto bitmap_end; |
393 | 0 | } |
394 | 21.3k | if ((res = uncompress(buf, &out_len, zbuf, len)) != Z_OK) { |
395 | 20.4k | av_log(s, AV_LOG_WARNING, "Failed to uncompress one bitmap\n"); |
396 | 20.4k | goto bitmap_end_skip; |
397 | 20.4k | } |
398 | | |
399 | 2.05k | for (i = 0; i < s->nb_streams; i++) { |
400 | 1.97k | st = s->streams[i]; |
401 | 1.97k | if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO && st->id == -3) |
402 | 791 | break; |
403 | 1.97k | } |
404 | 871 | if (i == s->nb_streams) { |
405 | 80 | vst = avformat_new_stream(s, NULL); |
406 | 80 | if (!vst) { |
407 | 0 | res = AVERROR(ENOMEM); |
408 | 0 | goto bitmap_end; |
409 | 0 | } |
410 | 80 | vst->id = -3; /* -3 to avoid clash with video stream and audio stream */ |
411 | 80 | vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
412 | 80 | vst->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO; |
413 | 80 | avpriv_set_pts_info(vst, 64, 256, swf->frame_rate); |
414 | 80 | st = vst; |
415 | 80 | } |
416 | | |
417 | 871 | if ((res = av_new_packet(pkt, out_len - colormapsize * colormapbpp)) < 0) |
418 | 204 | goto bitmap_end; |
419 | 667 | if (!st->codecpar->width && !st->codecpar->height) { |
420 | 71 | st->codecpar->width = width; |
421 | 71 | st->codecpar->height = height; |
422 | 596 | } else { |
423 | 596 | ff_add_param_change(pkt, 0, 0, 0, width, height); |
424 | 596 | } |
425 | 667 | pkt->pos = pos; |
426 | 667 | pkt->stream_index = st->index; |
427 | | |
428 | 667 | if (linesize * height > pkt->size) { |
429 | 667 | res = AVERROR_INVALIDDATA; |
430 | 667 | goto bitmap_end; |
431 | 667 | } |
432 | | |
433 | 0 | switch (bmp_fmt) { |
434 | 0 | case 3: |
435 | 0 | pix_fmt = AV_PIX_FMT_PAL8; |
436 | 0 | for (i = 0; i < colormapsize; i++) |
437 | 0 | if (alpha_bmp) colormap[i] = buf[3]<<24 | AV_RB24(buf + 4*i); |
438 | 0 | else colormap[i] = 0xffU <<24 | AV_RB24(buf + 3*i); |
439 | 0 | pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE); |
440 | 0 | if (!pal) { |
441 | 0 | res = AVERROR(ENOMEM); |
442 | 0 | goto bitmap_end; |
443 | 0 | } |
444 | 0 | memcpy(pal, colormap, AVPALETTE_SIZE); |
445 | 0 | break; |
446 | 0 | case 4: |
447 | 0 | pix_fmt = AV_PIX_FMT_RGB555; |
448 | 0 | break; |
449 | 0 | case 5: |
450 | 0 | pix_fmt = alpha_bmp ? AV_PIX_FMT_ARGB : AV_PIX_FMT_0RGB; |
451 | 0 | break; |
452 | 0 | default: |
453 | 0 | av_assert0(0); |
454 | 0 | } |
455 | 0 | if (st->codecpar->format != AV_PIX_FMT_NONE && st->codecpar->format != pix_fmt) { |
456 | 0 | av_log(s, AV_LOG_ERROR, "pixel format change unsupported\n"); |
457 | 0 | } else |
458 | 0 | st->codecpar->format = pix_fmt; |
459 | |
|
460 | 0 | memcpy(pkt->data, buf + colormapsize*colormapbpp, linesize * height); |
461 | |
|
462 | 0 | res = pkt->size; |
463 | |
|
464 | 871 | bitmap_end: |
465 | 871 | av_freep(&zbuf); |
466 | 871 | av_freep(&buf); |
467 | 871 | return res; |
468 | 110k | bitmap_end_skip: |
469 | 110k | av_freep(&zbuf); |
470 | 110k | av_freep(&buf); |
471 | | #else |
472 | | av_log(s, AV_LOG_ERROR, "this file requires zlib support compiled in\n"); |
473 | | #endif |
474 | 26.1M | } else if (tag == TAG_STREAMBLOCK) { |
475 | 5.59M | for (i = 0; i < s->nb_streams; i++) { |
476 | 5.59M | st = s->streams[i]; |
477 | 5.59M | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) { |
478 | 5.43M | if (st->codecpar->codec_id == AV_CODEC_ID_MP3) { |
479 | 23.7k | avio_skip(pb, 4); |
480 | 23.7k | len -= 4; |
481 | 23.7k | if (len <= 0) |
482 | 354 | goto skip; |
483 | 23.4k | if ((res = av_get_packet(pb, pkt, len)) < 0) |
484 | 318 | return res; |
485 | 5.41M | } else { // ADPCM, PCM |
486 | 5.41M | if (len <= 0) |
487 | 394 | goto skip; |
488 | 5.41M | if ((res = av_get_packet(pb, pkt, len)) < 0) |
489 | 452 | return res; |
490 | 5.41M | } |
491 | 5.43M | pkt->pos = pos; |
492 | 5.43M | pkt->stream_index = st->index; |
493 | 5.43M | return pkt->size; |
494 | 5.43M | } |
495 | 5.59M | } |
496 | 20.7M | } else if (tag == TAG_JPEG2) { |
497 | 813k | for (i=0; i<s->nb_streams; i++) { |
498 | 812k | st = s->streams[i]; |
499 | 812k | if (st->codecpar->codec_id == AV_CODEC_ID_MJPEG && st->id == -2) |
500 | 575k | break; |
501 | 812k | } |
502 | 576k | if (i == s->nb_streams) { |
503 | 1.28k | vst = avformat_new_stream(s, NULL); |
504 | 1.28k | if (!vst) |
505 | 0 | return AVERROR(ENOMEM); |
506 | 1.28k | vst->id = -2; /* -2 to avoid clash with video stream and audio stream */ |
507 | 1.28k | vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
508 | 1.28k | vst->codecpar->codec_id = AV_CODEC_ID_MJPEG; |
509 | 1.28k | avpriv_set_pts_info(vst, 64, 256, swf->frame_rate); |
510 | 1.28k | st = vst; |
511 | 1.28k | } |
512 | 576k | avio_rl16(pb); /* BITMAP_ID */ |
513 | 576k | len -= 2; |
514 | 576k | if (len < 4) |
515 | 488k | goto skip; |
516 | 88.7k | if ((res = av_new_packet(pkt, len)) < 0) |
517 | 195 | return res; |
518 | 88.5k | if (avio_read(pb, pkt->data, 4) != 4) { |
519 | 194 | return AVERROR_INVALIDDATA; |
520 | 194 | } |
521 | 88.3k | if (AV_RB32(pkt->data) == 0xffd8ffd9 || |
522 | 88.1k | AV_RB32(pkt->data) == 0xffd9ffd8) { |
523 | | /* old SWF files containing SOI/EOI as data start */ |
524 | | /* files created by swink have reversed tag */ |
525 | 436 | pkt->size -= 4; |
526 | 436 | memset(pkt->data+pkt->size, 0, 4); |
527 | 436 | res = avio_read(pb, pkt->data, pkt->size); |
528 | 87.9k | } else { |
529 | 87.9k | res = avio_read(pb, pkt->data + 4, pkt->size - 4); |
530 | 87.9k | if (res >= 0) |
531 | 87.9k | res += 4; |
532 | 87.9k | } |
533 | 88.3k | if (res != pkt->size) { |
534 | 178 | if (res < 0) { |
535 | 24 | return res; |
536 | 24 | } |
537 | 154 | av_shrink_packet(pkt, res); |
538 | 154 | } |
539 | | |
540 | 88.3k | pkt->pos = pos; |
541 | 88.3k | pkt->stream_index = st->index; |
542 | 88.3k | return pkt->size; |
543 | 20.1M | } else { |
544 | 20.1M | av_log(s, AV_LOG_DEBUG, "Unknown tag: %d\n", tag); |
545 | 20.1M | } |
546 | 23.1M | skip: |
547 | 23.1M | if(len<0) |
548 | 575k | av_log(s, AV_LOG_WARNING, "Clipping len %d\n", len); |
549 | 23.1M | len = FFMAX(0, len); |
550 | 23.1M | avio_skip(pb, len); |
551 | 23.1M | } |
552 | 10.1M | } |
553 | | |
554 | | #if CONFIG_ZLIB |
555 | | static av_cold int swf_read_close(AVFormatContext *avctx) |
556 | 6.07k | { |
557 | 6.07k | SWFDecContext *s = avctx->priv_data; |
558 | 6.07k | inflateEnd(&s->zstream); |
559 | 6.07k | av_freep(&s->zbuf_in); |
560 | 6.07k | av_freep(&s->zbuf_out); |
561 | 6.07k | avio_context_free(&s->zpb); |
562 | 6.07k | return 0; |
563 | 6.07k | } |
564 | | #endif |
565 | | |
566 | | const FFInputFormat ff_swf_demuxer = { |
567 | | .p.name = "swf", |
568 | | .p.long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"), |
569 | | .priv_data_size = sizeof(SWFDecContext), |
570 | | .read_probe = swf_probe, |
571 | | .read_header = swf_read_header, |
572 | | .read_packet = swf_read_packet, |
573 | | #if CONFIG_ZLIB |
574 | | .read_close = swf_read_close, |
575 | | #endif |
576 | | }; |