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 
24 #include "libavutil/intreadwrite.h"
25 #include "swf.h"
26 
27 static const AVCodecTag swf_audio_codec_tags[] = {
28  { AV_CODEC_ID_PCM_S16LE, 0x00 },
29  { AV_CODEC_ID_ADPCM_SWF, 0x01 },
30  { AV_CODEC_ID_MP3, 0x02 },
31  { AV_CODEC_ID_PCM_S16LE, 0x03 },
32 // { AV_CODEC_ID_NELLYMOSER, 0x06 },
33  { AV_CODEC_ID_NONE, 0 },
34 };
35 
36 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
37 {
38  int tag, len;
39 
40  if (pb->eof_reached)
41  return -1;
42 
43  tag = avio_rl16(pb);
44  len = tag & 0x3f;
45  tag = tag >> 6;
46  if (len == 0x3f) {
47  len = avio_rl32(pb);
48  }
49  *len_ptr = len;
50  return tag;
51 }
52 
53 
54 static int swf_probe(AVProbeData *p)
55 {
56  /* check file header */
57  if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
58  p->buf[2] == 'S')
59  return AVPROBE_SCORE_MAX;
60  else
61  return 0;
62 }
63 
65 {
66  SWFContext *swf = s->priv_data;
67  AVIOContext *pb = s->pb;
68  int nbits, len, tag;
69 
70  tag = avio_rb32(pb) & 0xffffff00;
71 
72  if (tag == MKBETAG('C', 'W', 'S', 0)) {
73  av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n");
74  return AVERROR(EIO);
75  }
76  if (tag != MKBETAG('F', 'W', 'S', 0))
77  return AVERROR(EIO);
78  avio_rl32(pb);
79  /* skip rectangle size */
80  nbits = avio_r8(pb) >> 3;
81  len = (4 * nbits - 3 + 7) / 8;
82  avio_skip(pb, len);
83  swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
84  avio_rl16(pb); /* frame count */
85 
86  swf->samples_per_frame = 0;
88  return 0;
89 }
90 
92 {
93  SWFContext *swf = s->priv_data;
94  AVIOContext *pb = s->pb;
95  AVStream *vst = NULL, *ast = NULL, *st = 0;
96  int tag, len, i, frame, v, res;
97 
98  for(;;) {
99  uint64_t pos = avio_tell(pb);
100  tag = get_swf_tag(pb, &len);
101  if (tag < 0)
102  return AVERROR(EIO);
103  if (len < 0) {
104  av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
105  return AVERROR_INVALIDDATA;
106  }
107  if (tag == TAG_VIDEOSTREAM) {
108  int ch_id = avio_rl16(pb);
109  len -= 2;
110 
111  for (i=0; i<s->nb_streams; i++) {
112  st = s->streams[i];
113  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
114  goto skip;
115  }
116 
117  avio_rl16(pb);
118  avio_rl16(pb);
119  avio_rl16(pb);
120  avio_r8(pb);
121  /* Check for FLV1 */
122  vst = avformat_new_stream(s, NULL);
123  if (!vst)
124  return -1;
125  vst->id = ch_id;
128  avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
129  len -= 8;
130  } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
131  /* streaming found */
132  int sample_rate_code;
133 
134  for (i=0; i<s->nb_streams; i++) {
135  st = s->streams[i];
136  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
137  goto skip;
138  }
139 
140  avio_r8(pb);
141  v = avio_r8(pb);
142  swf->samples_per_frame = avio_rl16(pb);
143  ast = avformat_new_stream(s, NULL);
144  if (!ast)
145  return -1;
146  ast->id = -1; /* -1 to avoid clash with video stream ch_id */
147  if (v & 1) {
148  ast->codec->channels = 2;
149  ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
150  } else {
151  ast->codec->channels = 1;
152  ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
153  }
154  ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
155  ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
156  ast->need_parsing = AVSTREAM_PARSE_FULL;
157  sample_rate_code= (v>>2) & 3;
158  ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
159  avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
160  len -= 4;
161  } else if (tag == TAG_VIDEOFRAME) {
162  int ch_id = avio_rl16(pb);
163  len -= 2;
164  for(i=0; i<s->nb_streams; i++) {
165  st = s->streams[i];
166  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
167  frame = avio_rl16(pb);
168  len -= 2;
169  if (len <= 0)
170  goto skip;
171  if ((res = av_get_packet(pb, pkt, len)) < 0)
172  return res;
173  pkt->pos = pos;
174  pkt->pts = frame;
175  pkt->stream_index = st->index;
176  return pkt->size;
177  }
178  }
179  } else if (tag == TAG_STREAMBLOCK) {
180  for (i = 0; i < s->nb_streams; i++) {
181  st = s->streams[i];
182  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
183  if (st->codec->codec_id == AV_CODEC_ID_MP3) {
184  avio_skip(pb, 4);
185  len -= 4;
186  if (len <= 0)
187  goto skip;
188  if ((res = av_get_packet(pb, pkt, len)) < 0)
189  return res;
190  } else { // ADPCM, PCM
191  if (len <= 0)
192  goto skip;
193  if ((res = av_get_packet(pb, pkt, len)) < 0)
194  return res;
195  }
196  pkt->pos = pos;
197  pkt->stream_index = st->index;
198  return pkt->size;
199  }
200  }
201  } else if (tag == TAG_JPEG2) {
202  for (i=0; i<s->nb_streams; i++) {
203  st = s->streams[i];
204  if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
205  break;
206  }
207  if (i == s->nb_streams) {
208  vst = avformat_new_stream(s, NULL);
209  if (!vst)
210  return -1;
211  vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
214  avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
215  st = vst;
216  }
217  avio_rl16(pb); /* BITMAP_ID */
218  len -= 2;
219  if (len < 4)
220  goto skip;
221  if ((res = av_new_packet(pkt, len)) < 0)
222  return res;
223  avio_read(pb, pkt->data, 4);
224  if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
225  AV_RB32(pkt->data) == 0xffd9ffd8) {
226  /* old SWF files containing SOI/EOI as data start */
227  /* files created by swink have reversed tag */
228  pkt->size -= 4;
229  avio_read(pb, pkt->data, pkt->size);
230  } else {
231  avio_read(pb, pkt->data + 4, pkt->size - 4);
232  }
233  pkt->pos = pos;
234  pkt->stream_index = st->index;
235  return pkt->size;
236  }
237  skip:
238  len = FFMAX(0, len);
239  avio_skip(pb, len);
240  }
241 }
242 
244  .name = "swf",
245  .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
246  .priv_data_size = sizeof(SWFContext),
250 };
const AVCodecTag ff_swf_codec_tags[]
Definition: swf.c:25
static int swf_probe(AVProbeData *p)
Definition: swfdec.c:54
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
Definition: swf.h:67
static int get_swf_tag(AVIOContext *pb, int *len_ptr)
Definition: swfdec.c:36
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:1943
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:998
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:2829
#define TAG_STREAMHEAD2
Definition: swf.h:45
int size
Definition: avcodec.h:974
#define AV_CH_LAYOUT_STEREO
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:971
Format I/O context.
Definition: avformat.h:922
#define TAG_VIDEOFRAME
Definition: swf.h:47
int frame_rate
Definition: swf.h:75
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:901
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:595
#define AV_RB32
Definition: intreadwrite.h:130
int id
Format-specific stream ID.
Definition: avformat.h:706
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
uint8_t * data
Definition: avcodec.h:973
#define TAG_STREAMHEAD
Definition: swf.h:41
uint32_t tag
Definition: movenc.c:844
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:219
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
AVInputFormat ff_swf_demuxer
Definition: swfdec.c:243
#define TAG_JPEG2
Definition: swf.h:43
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#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:150
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:379
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
#define FFMAX(a, b)
Definition: common.h:55
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
int samples_per_frame
Definition: swf.h:71
audio channel layout utility functions
static const AVCodecTag swf_audio_codec_tags[]
Definition: swfdec.c:27
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:210
static int swf_read_header(AVFormatContext *s)
Definition: swfdec.c:64
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
#define TAG_STREAMBLOCK
Definition: swf.h:42
enum AVCodecID codec_id
Definition: avcodec.h:1067
AVIOContext * pb
I/O context.
Definition: avformat.h:964
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:395
#define TAG_VIDEOSTREAM
Definition: swf.h:46
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: swfdec.c:91
full parsing and repack
Definition: avformat.h:653
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:548
#define MKBETAG(a, b, c, d)
Definition: common.h:239
int eof_reached
true if eof reached
Definition: avio.h:96
int len
void * priv_data
Format private data.
Definition: avformat.h:950
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
int stream_index
Definition: avcodec.h:975
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966