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 "libavutil/intreadwrite.h"
24 #include "swf.h"
25 
26 static int get_swf_tag(AVIOContext *pb, int *len_ptr)
27 {
28  int tag, len;
29 
30  if (pb->eof_reached)
31  return -1;
32 
33  tag = avio_rl16(pb);
34  len = tag & 0x3f;
35  tag = tag >> 6;
36  if (len == 0x3f) {
37  len = avio_rl32(pb);
38  }
39 // av_log(NULL, AV_LOG_DEBUG, "Tag: %d - Len: %d\n", tag, len);
40  *len_ptr = len;
41  return tag;
42 }
43 
44 
45 static int swf_probe(AVProbeData *p)
46 {
47  /* check file header */
48  if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
49  p->buf[2] == 'S')
50  return AVPROBE_SCORE_MAX;
51  else
52  return 0;
53 }
54 
56 {
57  SWFContext *swf = s->priv_data;
58  AVIOContext *pb = s->pb;
59  int nbits, len, tag;
60 
61  tag = avio_rb32(pb) & 0xffffff00;
62 
63  if (tag == MKBETAG('C', 'W', 'S', 0)) {
64  av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n");
65  return AVERROR(EIO);
66  }
67  if (tag != MKBETAG('F', 'W', 'S', 0))
68  return AVERROR(EIO);
69  avio_rl32(pb);
70  /* skip rectangle size */
71  nbits = avio_r8(pb) >> 3;
72  len = (4 * nbits - 3 + 7) / 8;
73  avio_skip(pb, len);
74  swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
75  avio_rl16(pb); /* frame count */
76 
77  swf->samples_per_frame = 0;
79  return 0;
80 }
81 
83 {
84  SWFContext *swf = s->priv_data;
85  AVIOContext *pb = s->pb;
86  AVStream *vst = NULL, *ast = NULL, *st = 0;
87  int tag, len, i, frame, v, res;
88 
89  for(;;) {
90  uint64_t pos = avio_tell(pb);
91  tag = get_swf_tag(pb, &len);
92  if (tag < 0)
93  return AVERROR(EIO);
94  if (len < 0) {
95  av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
96  return AVERROR_INVALIDDATA;
97  }
98  if (tag == TAG_VIDEOSTREAM) {
99  int ch_id = avio_rl16(pb);
100  len -= 2;
101 
102  for (i=0; i<s->nb_streams; i++) {
103  st = s->streams[i];
104  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
105  goto skip;
106  }
107 
108  avio_rl16(pb);
109  avio_rl16(pb);
110  avio_rl16(pb);
111  avio_r8(pb);
112  /* Check for FLV1 */
113  vst = avformat_new_stream(s, NULL);
114  if (!vst)
115  return -1;
116  vst->id = ch_id;
119  avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
120  vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
121  len -= 8;
122  } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
123  /* streaming found */
124  int sample_rate_code;
125 
126  for (i=0; i<s->nb_streams; i++) {
127  st = s->streams[i];
128  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
129  goto skip;
130  }
131 
132  avio_r8(pb);
133  v = avio_r8(pb);
134  swf->samples_per_frame = avio_rl16(pb);
135  ast = avformat_new_stream(s, NULL);
136  if (!ast)
137  return -1;
138  ast->id = -1; /* -1 to avoid clash with video stream ch_id */
139  ast->codec->channels = 1 + (v&1);
140  ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
141  ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
142  ast->need_parsing = AVSTREAM_PARSE_FULL;
143  sample_rate_code= (v>>2) & 3;
144  if (!sample_rate_code)
145  ast->codec->sample_rate = 5512;
146  else
147  ast->codec->sample_rate = 11025 << (sample_rate_code-1);
148  avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
149  len -= 4;
150  } else if (tag == TAG_VIDEOFRAME) {
151  int ch_id = avio_rl16(pb);
152  len -= 2;
153  for(i=0; i<s->nb_streams; i++) {
154  st = s->streams[i];
155  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
156  frame = avio_rl16(pb);
157  len -= 2;
158  if (len <= 0)
159  goto skip;
160  if ((res = av_get_packet(pb, pkt, len)) < 0)
161  return res;
162  pkt->pos = pos;
163  pkt->pts = frame;
164  pkt->stream_index = st->index;
165  return pkt->size;
166  }
167  }
168  } else if (tag == TAG_STREAMBLOCK) {
169  for (i = 0; i < s->nb_streams; i++) {
170  st = s->streams[i];
171  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
172  if (st->codec->codec_id == CODEC_ID_MP3) {
173  avio_skip(pb, 4);
174  len -= 4;
175  if (len <= 0)
176  goto skip;
177  if ((res = av_get_packet(pb, pkt, len)) < 0)
178  return res;
179  } else { // ADPCM, PCM
180  if (len <= 0)
181  goto skip;
182  if ((res = av_get_packet(pb, pkt, len)) < 0)
183  return res;
184  }
185  pkt->pos = pos;
186  pkt->stream_index = st->index;
187  return pkt->size;
188  }
189  }
190  } else if (tag == TAG_JPEG2) {
191  for (i=0; i<s->nb_streams; i++) {
192  st = s->streams[i];
193  if (st->codec->codec_id == CODEC_ID_MJPEG && st->id == -2)
194  break;
195  }
196  if (i == s->nb_streams) {
197  vst = avformat_new_stream(s, NULL);
198  if (!vst)
199  return -1;
200  vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
202  vst->codec->codec_id = CODEC_ID_MJPEG;
203  avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
204  vst->codec->time_base = (AVRational){ 256, swf->frame_rate };
205  st = vst;
206  }
207  avio_rl16(pb); /* BITMAP_ID */
208  len -= 2;
209  if (len < 4)
210  goto skip;
211  if ((res = av_new_packet(pkt, len)) < 0)
212  return res;
213  avio_read(pb, pkt->data, 4);
214  if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
215  AV_RB32(pkt->data) == 0xffd9ffd8) {
216  /* old SWF files containing SOI/EOI as data start */
217  /* files created by swink have reversed tag */
218  pkt->size -= 4;
219  avio_read(pb, pkt->data, pkt->size);
220  } else {
221  avio_read(pb, pkt->data + 4, pkt->size - 4);
222  }
223  pkt->pos = pos;
224  pkt->stream_index = st->index;
225  return pkt->size;
226  }
227  skip:
228  len = FFMAX(0, len);
229  avio_skip(pb, len);
230  }
231 }
232 
234  .name = "swf",
235  .long_name = NULL_IF_CONFIG_SMALL("Flash format"),
236  .priv_data_size = sizeof(SWFContext),
240 };
static int swf_probe(AVProbeData *p)
Definition: swfdec.c:45
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:26
AV_WL32 AV_WL24 AV_WL16 AV_RB32
Definition: bytestream.h:89
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:933
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
#define TAG_STREAMHEAD2
Definition: swf.h:45
int size
Definition: avcodec.h:909
static int swf_read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: swfdec.c:55
int ctx_flags
Format-specific flags, see AVFMTCTX_xx.
Definition: avformat.h:919
#define v(n)
Definition: regs.h:34
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1398
Format I/O context.
Definition: avformat.h:863
#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:844
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:769
int id
format-specific stream ID
Definition: avformat.h:622
AVStream ** streams
Definition: avformat.h:908
static int read_header(FFV1Context *f)
Definition: ffv1.c:1513
uint8_t * data
Definition: avcodec.h:908
#define TAG_STREAMHEAD
Definition: swf.h:41
uint32_t tag
Definition: movenc.c:670
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:269
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:492
enum CodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:2206
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
AVInputFormat ff_swf_demuxer
Definition: swfdec.c:233
#define TAG_JPEG2
Definition: swf.h:43
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:738
#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:191
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
#define FFMAX(a, b)
Definition: common.h:53
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
AVCodecContext * codec
codec context
Definition: avformat.h:623
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:907
struct AVRational AVRational
rational number numerator/denominator
int samples_per_frame
Definition: swf.h:71
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:483
static int read_probe(AVProbeData *p)
Definition: img2.c:185
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1574
#define TAG_STREAMBLOCK
Definition: swf.h:42
AVIOContext * pb
Definition: avformat.h:896
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:109
static const AVCodecTag swf_codec_tags[]
Definition: swf.h:81
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
#define TAG_VIDEOSTREAM
Definition: swf.h:46
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:345
static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: swfdec.c:82
full parsing and repack
Definition: avformat.h:581
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:722
#define MKBETAG(a, b, c, d)
Definition: common.h:232
int eof_reached
true if eof reached
Definition: avio.h:98
int len
void * priv_data
Format private data.
Definition: avformat.h:883
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
static const AVCodecTag swf_audio_codec_tags[]
Definition: swf.h:87
int stream_index
Definition: avcodec.h:910
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:336
enum CodecID codec_id
Definition: avcodec.h:1575