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
24
25 #if CONFIG_ZLIB
26 #include <zlib.h>
27 #endif
28
36
42 // { AV_CODEC_ID_NELLYMOSER, 0x06 },
44 };
45
47 {
49
52
54 len = tag & 0x3f;
55 tag = tag >> 6;
56 if (len == 0x3f) {
58 }
61 }
62
63
65 {
67 int len, xmin, xmax, ymin, ymax;
68
70 return 0;
71
72 /* check file header */
75 return 0;
76
80
82 return 0;
83
86 if (!len)
87 return 0;
92 if (xmin || ymin || !xmax || !ymax)
93 return 0;
94
95 if (p->
buf[3] >= 20 || xmax < 16 || ymax < 16)
97
99 }
100
101 #if CONFIG_ZLIB
102 static int zlib_refill(
void *opaque,
uint8_t *
buf,
int buf_size)
103 {
106 z_stream *z = &swf->zstream;
107 int ret;
108
109 retry:
110 if (!z->avail_in) {
112 if (n < 0)
114 z->next_in = swf->zbuf_in;
116 }
117
119 z->avail_out = buf_size;
120
122 if (ret == Z_STREAM_END)
124 if (ret != Z_OK)
126
127 if (buf_size - z->avail_out == 0)
128 goto retry;
129
130 return buf_size - z->avail_out;
131 }
132 #endif
133
135 {
139
142
143 if (tag ==
MKBETAG(
'C',
'W',
'S', 0)) {
145 #if CONFIG_ZLIB
150 if (!swf->zbuf_in || !swf->zbuf_out || !swf->zpb)
152 swf->zpb->seekable = 0;
153 if (inflateInit(&swf->zstream) != Z_OK) {
156 }
157 pb = swf->zpb;
158 #else
161 #endif
162 }
else if (tag !=
MKBETAG(
'F',
'W',
'S', 0))
164 /* skip rectangle size */
166 len = (4 * nbits - 3 + 7) / 8;
170
173 return 0;
174 }
175
177 {
178 int sample_rate_code, sample_size_code;
180 if (!ast)
183 if (info & 1) {
186 } else {
189 }
193 sample_rate_code = info>>2 & 3;
194 sample_size_code = info>>1 & 1;
199 return ast;
200 }
201
203 {
208
209 #if CONFIG_ZLIB
210 if (swf->zpb)
211 pb = swf->zpb;
212 #endif
213
214 for(;;) {
217 if (tag < 0)
219 if (len < 0) {
222 }
225 len -= 2;
226
230 goto skip;
231 }
232
237 /* Check for FLV1 */
239 if (!vst)
245 len -= 8;
247 /* streaming found */
248
252 goto skip;
253 }
254
259 if (!ast)
261 len -= 4;
263 /* audio stream */
265
269 goto skip;
270 }
271
272 // FIXME: The entire audio stream is stored in a single chunk/tag. Normally,
273 // these are smaller audio streams in DEFINESOUND tags, but it's technically
274 // possible they could be huge. Break it up into multiple packets if it's big.
277 if (!ast)
279 ast->duration =
avio_rl32(pb);
// number of samples
280 if (((v>>4) & 15) == 2) { // MP3 sound data record
282 len -= 2;
283 }
284 len -= 7;
286 return res;
292 len -= 2;
297 len -= 2;
298 if (len <= 0)
299 goto skip;
301 return res;
306 }
307 }
309 #if CONFIG_ZLIB
310 long out_len;
314 const int colormapbpp = 3 + alpha_bmp;
315 int linesize, colormapsize = 0;
316
318 const int bmp_fmt =
avio_r8(pb);
322
323 len -= 2+1+2+2;
324
325 switch (bmp_fmt) {
326 case 3: // PAL-8
328 colormapsize =
avio_r8(pb) + 1;
329 len--;
330 break;
331 case 4: // RGB15
332 linesize = width * 2;
333 break;
334 case 5: // RGB24 (0RGB)
335 linesize = width * 4;
336 break;
337 default:
339 goto bitmap_end_skip;
340 }
341
342 linesize =
FFALIGN(linesize, 4);
343
345 linesize >= INT_MAX / height ||
346 linesize * height >= INT_MAX - colormapsize * colormapbpp) {
348 goto bitmap_end_skip;
349 }
350
351 out_len = colormapsize * colormapbpp + linesize *
height;
352
353 ff_dlog(s,
"bitmap: ch=%d fmt=%d %dx%d (linesize=%d) len=%d->%ld pal=%d\n",
354 ch_id, bmp_fmt, width, height, linesize, len, out_len, colormapsize);
355
358 if (!zbuf || !buf) {
360 goto bitmap_end;
361 }
362
364 if (len < 0 || (res = uncompress(buf, &out_len, zbuf, len)) != Z_OK) {
366 goto bitmap_end_skip;
367 }
368
372 break;
373 }
376 if (!vst) {
378 goto bitmap_end;
379 }
380 vst->
id = -3;
/* -3 to avoid clash with video stream and audio stream */
384 st = vst;
385 }
386
387 if ((res =
av_new_packet(pkt, out_len - colormapsize * colormapbpp)) < 0)
388 goto bitmap_end;
389 if (!st->codecpar->width && !st->codecpar->height) {
391 st->codecpar->height =
height;
392 } else {
394 }
397
398 if (linesize * height > pkt->
size) {
401 goto bitmap_end;
402 }
403
404 switch (bmp_fmt) {
405 case 3:
407 for (i = 0; i < colormapsize; i++)
408 if (alpha_bmp) colormap[i] = buf[3]<<24 |
AV_RB24(buf + 4*i);
409 else colormap[i] = 0xff
U <<24 |
AV_RB24(buf + 3*i);
411 if (!pal) {
413 goto bitmap_end;
414 }
416 break;
417 case 4:
419 break;
420 case 5:
422 break;
423 default:
425 }
426 if (st->codecpar->format !=
AV_PIX_FMT_NONE && st->codecpar->format != pix_fmt) {
428 } else
429 st->codecpar->format =
pix_fmt;
430
431 memcpy(pkt->
data, buf + colormapsize*colormapbpp, linesize * height);
432
434
435 bitmap_end:
438 return res;
439 bitmap_end_skip:
442 #else
444 #endif
451 len -= 4;
452 if (len <= 0)
453 goto skip;
455 return res;
456 } else { // ADPCM, PCM
457 if (len <= 0)
458 goto skip;
460 return res;
461 }
465 }
466 }
471 break;
472 }
475 if (!vst)
477 vst->
id = -2;
/* -2 to avoid clash with video stream and audio stream */
481 st = vst;
482 }
484 len -= 2;
485 if (len < 4)
486 goto skip;
488 return res;
492 }
495 /* old SWF files containing SOI/EOI as data start */
496 /* files created by swink have reversed tag */
500 } else {
502 if (res >= 0)
503 res += 4;
504 }
505 if (res != pkt->
size) {
506 if (res < 0) {
508 return res;
509 }
511 }
512
516 } else {
518 }
519 skip:
520 if(len<0)
524 }
525 }
526
527 #if CONFIG_ZLIB
529 {
531 inflateEnd(&s->zstream);
535 return 0;
536 }
537 #endif
538
546 #if CONFIG_ZLIB
548 #endif
549 };
const AVCodecTag ff_swf_codec_tags[]
static int swf_probe(AVProbeData *p)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static enum AVPixelFormat pix_fmt
static int get_swf_tag(AVIOContext *pb, int *len_ptr)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t pos
byte position in stream, -1 if unknown
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
#define AV_CH_LAYOUT_STEREO
int ctx_flags
Flags signalling stream properties.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
8 bits with AV_PIX_FMT_RGB32 palette
unsigned int avio_rb32(AVIOContext *s)
enum AVStreamParseType need_parsing
int id
Format-specific stream ID.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
AVStream ** streams
A list of all streams in the file.
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord)
#define AVERROR_EOF
End of file.
bitstream reader API header.
static av_cold int read_close(AVFormatContext *ctx)
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
uint64_t channel_layout
Audio only.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
AVInputFormat ff_swf_demuxer
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette...
unsigned int avio_rl32(AVIOContext *s)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
preferred ID for decoding MPEG audio layer 1, 2 or 3
enum AVMediaType codec_type
General type of the encoded data.
simple assert() macros that are a bit more flexible than ISO C assert().
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
int avio_r8(AVIOContext *s)
int buf_size
Size of buf except extra allocated bytes.
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
audio channel layout utility functions
static const AVCodecTag swf_audio_codec_tags[]
static AVStream * create_new_audio_stream(AVFormatContext *s, int id, int info)
static int swf_read_header(AVFormatContext *s)
static int read_header(FFV1Context *f)
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define AV_LOG_INFO
Standard information.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
AVIOContext * pb
I/O context.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
static void skip_bits(GetBitContext *s, int n)
This structure contains the data a format has to probe a file.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
int sample_rate
Audio only.
static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
unsigned int avio_rl16(AVIOContext *s)
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
#define AV_PIX_FMT_RGB555
#define MKBETAG(a, b, c, d)
void * priv_data
Format private data.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
#define AV_CH_LAYOUT_MONO
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...