Libav
swfdec.c
Go to the documentation of this file.
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 
30 #include "libavutil/intreadwrite.h"
31 #include "swf.h"
32 
33 static const AVCodecTag swf_audio_codec_tags[] = {
34  { AV_CODEC_ID_PCM_S16LE, 0x00 },
35  { AV_CODEC_ID_ADPCM_SWF, 0x01 },
36  { AV_CODEC_ID_MP3, 0x02 },
37  { AV_CODEC_ID_PCM_S16LE, 0x03 },
38 // { AV_CODEC_ID_NELLYMOSER, 0x06 },
39  { AV_CODEC_ID_NONE, 0 },
40 };
41 
42 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
43 {
44  int tag, len;
45 
46  if (pb->eof_reached)
47  return -1;
48 
49  tag = avio_rl16(pb);
50  len = tag & 0x3f;
51  tag = tag >> 6;
52  if (len == 0x3f) {
53  len = avio_rl32(pb);
54  }
55  *len_ptr = len;
56  return tag;
57 }
58 
59 
60 static int swf_probe(AVProbeData *p)
61 {
62  /* check file header */
63  if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
64  p->buf[2] == 'S')
65  return AVPROBE_SCORE_MAX;
66  else
67  return 0;
68 }
69 
70 #if CONFIG_ZLIB
71 static int zlib_refill(void *opaque, uint8_t *buf, int buf_size)
72 {
73  AVFormatContext *s = opaque;
74  SWFContext *swf = s->priv_data;
75  z_stream *z = &swf->zstream;
76  int ret;
77 
78 retry:
79  if (!z->avail_in) {
80  int n = avio_read(s->pb, swf->zbuf_in, ZBUF_SIZE);
81  if (n < 0)
82  return n;
83  z->next_in = swf->zbuf_in;
84  z->avail_in = n;
85  }
86 
87  z->next_out = buf;
88  z->avail_out = buf_size;
89 
90  ret = inflate(z, Z_NO_FLUSH);
91  if (ret != Z_OK && ret != Z_STREAM_END) {
92  av_log(s, AV_LOG_ERROR, "Inflate error: %d\n", ret);
93  return AVERROR_UNKNOWN;
94  }
95 
96  if (buf_size - z->avail_out == 0)
97  goto retry;
98 
99  return buf_size - z->avail_out;
100 }
101 #endif
102 
104 {
105  SWFContext *swf = s->priv_data;
106  AVIOContext *pb = s->pb;
107  int nbits, len, tag;
108 
109  tag = avio_rb32(pb) & 0xffffff00;
110  avio_rl32(pb);
111 
112  if (tag == MKBETAG('C', 'W', 'S', 0)) {
113  av_log(s, AV_LOG_INFO, "Compressed SWF file detected\n");
114 #if CONFIG_ZLIB
115  if (inflateInit(&swf->zstream) != Z_OK) {
116  av_log(s, AV_LOG_ERROR, "Unable to init zlib context\n");
117  return AVERROR(EINVAL);
118  }
119  swf->zbuf_in = av_malloc(ZBUF_SIZE);
120  swf->zbuf_out = av_malloc(ZBUF_SIZE);
121  swf->zpb = avio_alloc_context(swf->zbuf_out, ZBUF_SIZE, 0, s,
122  zlib_refill, NULL, NULL);
123  if (!swf->zbuf_in || !swf->zbuf_out || !swf->zpb) {
124  av_freep(&swf->zbuf_in);
125  av_freep(&swf->zbuf_out);
126  av_freep(&swf->zpb);
127  return AVERROR(ENOMEM);
128  }
129  swf->zpb->seekable = 0;
130  pb = swf->zpb;
131 #else
132  av_log(s, AV_LOG_ERROR, "missing zlib support, unable to open\n");
133  return AVERROR(EIO);
134 #endif
135  } else if (tag != MKBETAG('F', 'W', 'S', 0))
136  return AVERROR(EIO);
137  /* skip rectangle size */
138  nbits = avio_r8(pb) >> 3;
139  len = (4 * nbits - 3 + 7) / 8;
140  avio_skip(pb, len);
141  swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
142  avio_rl16(pb); /* frame count */
143 
144  swf->samples_per_frame = 0;
146  return 0;
147 }
148 
150 {
151  SWFContext *swf = s->priv_data;
152  AVIOContext *pb = s->pb;
153  AVStream *vst = NULL, *ast = NULL, *st = 0;
154  int tag, len, i, frame, v, res;
155 
156 #if CONFIG_ZLIB
157  if (swf->zpb)
158  pb = swf->zpb;
159 #endif
160 
161  for(;;) {
162  uint64_t pos = avio_tell(pb);
163  tag = get_swf_tag(pb, &len);
164  if (tag < 0)
165  return AVERROR(EIO);
166  if (len < 0) {
167  av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
168  return AVERROR_INVALIDDATA;
169  }
170  if (tag == TAG_VIDEOSTREAM) {
171  int ch_id = avio_rl16(pb);
172  len -= 2;
173 
174  for (i=0; i<s->nb_streams; i++) {
175  st = s->streams[i];
176  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
177  goto skip;
178  }
179 
180  avio_rl16(pb);
181  avio_rl16(pb);
182  avio_rl16(pb);
183  avio_r8(pb);
184  /* Check for FLV1 */
185  vst = avformat_new_stream(s, NULL);
186  if (!vst)
187  return -1;
188  vst->id = ch_id;
191  avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
192  len -= 8;
193  } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
194  /* streaming found */
195  int sample_rate_code;
196 
197  for (i=0; i<s->nb_streams; i++) {
198  st = s->streams[i];
199  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
200  goto skip;
201  }
202 
203  avio_r8(pb);
204  v = avio_r8(pb);
205  swf->samples_per_frame = avio_rl16(pb);
206  ast = avformat_new_stream(s, NULL);
207  if (!ast)
208  return -1;
209  ast->id = -1; /* -1 to avoid clash with video stream ch_id */
210  if (v & 1) {
211  ast->codecpar->channels = 2;
212  ast->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
213  } else {
214  ast->codecpar->channels = 1;
215  ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
216  }
217  ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
218  ast->codecpar->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
219  ast->need_parsing = AVSTREAM_PARSE_FULL;
220  sample_rate_code= (v>>2) & 3;
221  ast->codecpar->sample_rate = 44100 >> (3 - sample_rate_code);
222  avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
223  len -= 4;
224  } else if (tag == TAG_VIDEOFRAME) {
225  int ch_id = avio_rl16(pb);
226  len -= 2;
227  for(i=0; i<s->nb_streams; i++) {
228  st = s->streams[i];
229  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
230  frame = avio_rl16(pb);
231  len -= 2;
232  if (len <= 0)
233  goto skip;
234  if ((res = av_get_packet(pb, pkt, len)) < 0)
235  return res;
236  pkt->pos = pos;
237  pkt->pts = frame;
238  pkt->stream_index = st->index;
239  return pkt->size;
240  }
241  }
242  } else if (tag == TAG_STREAMBLOCK) {
243  for (i = 0; i < s->nb_streams; i++) {
244  st = s->streams[i];
245  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
246  if (st->codecpar->codec_id == AV_CODEC_ID_MP3) {
247  avio_skip(pb, 4);
248  len -= 4;
249  if (len <= 0)
250  goto skip;
251  if ((res = av_get_packet(pb, pkt, len)) < 0)
252  return res;
253  } else { // ADPCM, PCM
254  if (len <= 0)
255  goto skip;
256  if ((res = av_get_packet(pb, pkt, len)) < 0)
257  return res;
258  }
259  pkt->pos = pos;
260  pkt->stream_index = st->index;
261  return pkt->size;
262  }
263  }
264  } else if (tag == TAG_JPEG2) {
265  for (i=0; i<s->nb_streams; i++) {
266  st = s->streams[i];
267  if (st->codecpar->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
268  break;
269  }
270  if (i == s->nb_streams) {
271  vst = avformat_new_stream(s, NULL);
272  if (!vst)
273  return -1;
274  vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
277  avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
278  st = vst;
279  }
280  avio_rl16(pb); /* BITMAP_ID */
281  len -= 2;
282  if (len < 4)
283  goto skip;
284  if ((res = av_new_packet(pkt, len)) < 0)
285  return res;
286  avio_read(pb, pkt->data, 4);
287  if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
288  AV_RB32(pkt->data) == 0xffd9ffd8) {
289  /* old SWF files containing SOI/EOI as data start */
290  /* files created by swink have reversed tag */
291  pkt->size -= 4;
292  avio_read(pb, pkt->data, pkt->size);
293  } else {
294  avio_read(pb, pkt->data + 4, pkt->size - 4);
295  }
296  pkt->pos = pos;
297  pkt->stream_index = st->index;
298  return pkt->size;
299  }
300  skip:
301  len = FFMAX(0, len);
302  avio_skip(pb, len);
303  }
304 }
305 
306 #if CONFIG_ZLIB
307 static av_cold int swf_read_close(AVFormatContext *avctx)
308 {
309  SWFContext *s = avctx->priv_data;
310  inflateEnd(&s->zstream);
311  av_freep(&s->zbuf_in);
312  av_freep(&s->zbuf_out);
313  av_freep(&s->zpb);
314  return 0;
315 }
316 #endif
317 
319  .name = "swf",
320  .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
321  .priv_data_size = sizeof(SWFContext),
325 #if CONFIG_ZLIB
326  .read_close = swf_read_close,
327 #endif
328 };
const AVCodecTag ff_swf_codec_tags[]
Definition: swf.c:25
static int swf_probe(AVProbeData *p)
Definition: swfdec.c:60
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:104
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
Definition: swf.h:70
static int get_swf_tag(AVIOContext *pb, int *len_ptr)
Definition: swfdec.c:42
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:1983
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1366
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2986
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
#define TAG_STREAMHEAD2
Definition: swf.h:51
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
#define AV_CH_LAYOUT_STEREO
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:989
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Format I/O context.
Definition: avformat.h:940
#define TAG_VIDEOFRAME
Definition: swf.h:53
int frame_rate
Definition: swf.h:78
uint8_t
#define av_cold
Definition: attributes.h:66
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:919
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:696
#define AV_RB32
Definition: intreadwrite.h:130
int id
Format-specific stream ID.
Definition: avformat.h:712
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
uint8_t * data
Definition: avcodec.h:1346
#define TAG_STREAMHEAD
Definition: swf.h:47
uint32_t tag
Definition: movenc.c:854
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:117
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
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.
Definition: aviobuf.c:151
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVInputFormat ff_swf_demuxer
Definition: swfdec.c:318
#define TAG_JPEG2
Definition: swf.h:49
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:665
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:479
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
#define CONFIG_ZLIB
Definition: config.h:387
#define FFMAX(a, b)
Definition: common.h:64
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:536
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:400
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
int samples_per_frame
Definition: swf.h:74
audio channel layout utility functions
static const AVCodecTag swf_audio_codec_tags[]
Definition: swfdec.c:33
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:286
static int swf_read_header(AVFormatContext *s)
Definition: swfdec.c:103
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:546
Stream structure.
Definition: avformat.h:705
NULL
Definition: eval.c:55
#define AV_LOG_INFO
Standard information.
Definition: log.h:135
#define TAG_STREAMBLOCK
Definition: swf.h:48
AVIOContext * pb
I/O context.
Definition: avformat.h:982
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
#define TAG_VIDEOSTREAM
Definition: swf.h:52
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:407
static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: swfdec.c:149
full parsing and repack
Definition: avformat.h:657
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:649
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:61
#define MKBETAG(a, b, c, d)
Definition: common.h:257
int eof_reached
true if eof reached
Definition: avio.h:132
int len
void * priv_data
Format private data.
Definition: avformat.h:968
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
AVCodecParameters * codecpar
Definition: avformat.h:831
int stream_index
Definition: avcodec.h:1348
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1323
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339