avformat: Remove non-compiling and/or silly commented-out printf/av_log statements
[libav.git] / libavformat / swfdec.c
CommitLineData
3b35f4ab 1/*
376aefdd 2 * Flash Compatible Streaming Format demuxer
406792e7
DB
3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2003 Tinic Uro
3b35f4ab 5 *
2912e87a 6 * This file is part of Libav.
3b35f4ab 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
3b35f4ab
BC
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 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
3b35f4ab
BC
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
2912e87a 19 * License along with Libav; if not, write to the Free Software
3b35f4ab
BC
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
6a5d31ac 23#include "libavutil/intreadwrite.h"
3b35f4ab
BC
24#include "swf.h"
25
2804ba6a 26static const AVCodecTag swf_audio_codec_tags[] = {
15c71dfd
AK
27 { AV_CODEC_ID_PCM_S16LE, 0x00 },
28 { AV_CODEC_ID_ADPCM_SWF, 0x01 },
29 { AV_CODEC_ID_MP3, 0x02 },
30 { AV_CODEC_ID_PCM_S16LE, 0x03 },
31// { AV_CODEC_ID_NELLYMOSER, 0x06 },
32 { AV_CODEC_ID_NONE, 0 },
2804ba6a
DB
33};
34
ae628ec1 35static int get_swf_tag(AVIOContext *pb, int *len_ptr)
3b35f4ab
BC
36{
37 int tag, len;
38
66e5b1df 39 if (pb->eof_reached)
3b35f4ab
BC
40 return -1;
41
b7effd4e 42 tag = avio_rl16(pb);
3b35f4ab
BC
43 len = tag & 0x3f;
44 tag = tag >> 6;
45 if (len == 0x3f) {
b7effd4e 46 len = avio_rl32(pb);
3b35f4ab 47 }
3b35f4ab
BC
48 *len_ptr = len;
49 return tag;
50}
51
52
53static int swf_probe(AVProbeData *p)
54{
55 /* check file header */
56 if ((p->buf[0] == 'F' || p->buf[0] == 'C') && p->buf[1] == 'W' &&
57 p->buf[2] == 'S')
58 return AVPROBE_SCORE_MAX;
59 else
60 return 0;
61}
62
6e9651d1 63static int swf_read_header(AVFormatContext *s)
3b35f4ab
BC
64{
65 SWFContext *swf = s->priv_data;
ae628ec1 66 AVIOContext *pb = s->pb;
3b35f4ab
BC
67 int nbits, len, tag;
68
b7effd4e 69 tag = avio_rb32(pb) & 0xffffff00;
3b35f4ab
BC
70
71 if (tag == MKBETAG('C', 'W', 'S', 0)) {
72 av_log(s, AV_LOG_ERROR, "Compressed SWF format not supported\n");
73 return AVERROR(EIO);
74 }
75 if (tag != MKBETAG('F', 'W', 'S', 0))
76 return AVERROR(EIO);
b7effd4e 77 avio_rl32(pb);
3b35f4ab 78 /* skip rectangle size */
b7effd4e 79 nbits = avio_r8(pb) >> 3;
3b35f4ab 80 len = (4 * nbits - 3 + 7) / 8;
45a8a02a 81 avio_skip(pb, len);
b7effd4e
AK
82 swf->frame_rate = avio_rl16(pb); /* 8.8 fixed */
83 avio_rl16(pb); /* frame count */
3b35f4ab
BC
84
85 swf->samples_per_frame = 0;
86 s->ctx_flags |= AVFMTCTX_NOHEADER;
87 return 0;
88}
89
90static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
91{
92 SWFContext *swf = s->priv_data;
ae628ec1 93 AVIOContext *pb = s->pb;
3b35f4ab 94 AVStream *vst = NULL, *ast = NULL, *st = 0;
31632e73 95 int tag, len, i, frame, v, res;
3b35f4ab
BC
96
97 for(;;) {
a2704c97 98 uint64_t pos = avio_tell(pb);
3b35f4ab
BC
99 tag = get_swf_tag(pb, &len);
100 if (tag < 0)
101 return AVERROR(EIO);
4e35ffa9 102 if (tag == TAG_VIDEOSTREAM) {
b7effd4e 103 int ch_id = avio_rl16(pb);
4e35ffa9
BC
104 len -= 2;
105
106 for (i=0; i<s->nb_streams; i++) {
107 st = s->streams[i];
72415b2a 108 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id)
4e35ffa9
BC
109 goto skip;
110 }
111
b7effd4e
AK
112 avio_rl16(pb);
113 avio_rl16(pb);
114 avio_rl16(pb);
115 avio_r8(pb);
3b35f4ab 116 /* Check for FLV1 */
84ad31ff 117 vst = avformat_new_stream(s, NULL);
3b35f4ab
BC
118 if (!vst)
119 return -1;
84ad31ff 120 vst->id = ch_id;
72415b2a 121 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
90f7e617 122 vst->codec->codec_id = ff_codec_get_id(ff_swf_codec_tags, avio_r8(pb));
c3f9ebf7 123 avpriv_set_pts_info(vst, 16, 256, swf->frame_rate);
4e35ffa9
BC
124 len -= 8;
125 } else if (tag == TAG_STREAMHEAD || tag == TAG_STREAMHEAD2) {
3b35f4ab
BC
126 /* streaming found */
127 int sample_rate_code;
4e35ffa9
BC
128
129 for (i=0; i<s->nb_streams; i++) {
130 st = s->streams[i];
72415b2a 131 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1)
4e35ffa9
BC
132 goto skip;
133 }
134
b7effd4e
AK
135 avio_r8(pb);
136 v = avio_r8(pb);
137 swf->samples_per_frame = avio_rl16(pb);
84ad31ff 138 ast = avformat_new_stream(s, NULL);
3b35f4ab
BC
139 if (!ast)
140 return -1;
84ad31ff 141 ast->id = -1; /* -1 to avoid clash with video stream ch_id */
3b35f4ab 142 ast->codec->channels = 1 + (v&1);
72415b2a 143 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1a40491e 144 ast->codec->codec_id = ff_codec_get_id(swf_audio_codec_tags, (v>>4) & 15);
3b35f4ab
BC
145 ast->need_parsing = AVSTREAM_PARSE_FULL;
146 sample_rate_code= (v>>2) & 3;
c0bc7bd1 147 ast->codec->sample_rate = 44100 >> (3 - sample_rate_code);
c3f9ebf7 148 avpriv_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
3b35f4ab
BC
149 len -= 4;
150 } else if (tag == TAG_VIDEOFRAME) {
b7effd4e 151 int ch_id = avio_rl16(pb);
3b35f4ab
BC
152 len -= 2;
153 for(i=0; i<s->nb_streams; i++) {
154 st = s->streams[i];
72415b2a 155 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
b7effd4e 156 frame = avio_rl16(pb);
31632e73
RB
157 if ((res = av_get_packet(pb, pkt, len-2)) < 0)
158 return res;
7e04cfba 159 pkt->pos = pos;
3b35f4ab
BC
160 pkt->pts = frame;
161 pkt->stream_index = st->index;
162 return pkt->size;
163 }
164 }
165 } else if (tag == TAG_STREAMBLOCK) {
4e35ffa9
BC
166 for (i = 0; i < s->nb_streams; i++) {
167 st = s->streams[i];
72415b2a 168 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
36ef5369 169 if (st->codec->codec_id == AV_CODEC_ID_MP3) {
45a8a02a 170 avio_skip(pb, 4);
31632e73
RB
171 if ((res = av_get_packet(pb, pkt, len-4)) < 0)
172 return res;
3b35f4ab 173 } else { // ADPCM, PCM
31632e73
RB
174 if ((res = av_get_packet(pb, pkt, len)) < 0)
175 return res;
3b35f4ab 176 }
7e04cfba 177 pkt->pos = pos;
3b35f4ab
BC
178 pkt->stream_index = st->index;
179 return pkt->size;
4e35ffa9
BC
180 }
181 }
3b35f4ab
BC
182 } else if (tag == TAG_JPEG2) {
183 for (i=0; i<s->nb_streams; i++) {
184 st = s->streams[i];
36ef5369 185 if (st->codec->codec_id == AV_CODEC_ID_MJPEG && st->id == -2)
3b35f4ab
BC
186 break;
187 }
188 if (i == s->nb_streams) {
84ad31ff 189 vst = avformat_new_stream(s, NULL);
3b35f4ab
BC
190 if (!vst)
191 return -1;
84ad31ff 192 vst->id = -2; /* -2 to avoid clash with video stream and audio stream */
72415b2a 193 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
36ef5369 194 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
c3f9ebf7 195 avpriv_set_pts_info(vst, 64, 256, swf->frame_rate);
3b35f4ab
BC
196 st = vst;
197 }
b7effd4e 198 avio_rl16(pb); /* BITMAP_ID */
31632e73
RB
199 if ((res = av_new_packet(pkt, len-2)) < 0)
200 return res;
b7effd4e 201 avio_read(pb, pkt->data, 4);
3b35f4ab
BC
202 if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
203 AV_RB32(pkt->data) == 0xffd9ffd8) {
204 /* old SWF files containing SOI/EOI as data start */
205 /* files created by swink have reversed tag */
206 pkt->size -= 4;
b7effd4e 207 avio_read(pb, pkt->data, pkt->size);
3b35f4ab 208 } else {
b7effd4e 209 avio_read(pb, pkt->data + 4, pkt->size - 4);
3b35f4ab 210 }
7e04cfba 211 pkt->pos = pos;
3b35f4ab
BC
212 pkt->stream_index = st->index;
213 return pkt->size;
214 }
4e35ffa9 215 skip:
45a8a02a 216 avio_skip(pb, len);
3b35f4ab 217 }
3b35f4ab
BC
218}
219
c6610a21 220AVInputFormat ff_swf_demuxer = {
dfc2c4d9 221 .name = "swf",
0177b7d2 222 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
dfc2c4d9
AK
223 .priv_data_size = sizeof(SWFContext),
224 .read_probe = swf_probe,
225 .read_header = swf_read_header,
226 .read_packet = swf_read_packet,
3b35f4ab 227};