annotate swf.c @ 104:1f218c4b0e2e libavformat

Clear out an unused field in the created AVI file. This helps make the MD5 checksums *much* more predictable!!
author philipjsg
date Sat, 12 Apr 2003 03:04:08 +0000
parents 25062c9b1f86
children 2fa5e94ba716
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1 /*
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
2 * Flash Compatible Streaming Format
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
3 * Copyright (c) 2000 Fabrice Bellard.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
4 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
9 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
13 * Lesser General Public License for more details.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
14 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
18 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
19 #include "avformat.h"
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
20
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
21 /* should have a generic way to indicate probable size */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
22 #define DUMMY_FILE_SIZE (100 * 1024 * 1024)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
23 #define DUMMY_DURATION 600 /* in seconds */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
24
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
25 #define TAG_END 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
26 #define TAG_SHOWFRAME 1
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
27 #define TAG_DEFINESHAPE 2
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
28 #define TAG_FREECHARACTER 3
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
29 #define TAG_PLACEOBJECT 4
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
30 #define TAG_REMOVEOBJECT 5
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
31 #define TAG_STREAMHEAD 18
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
32 #define TAG_STREAMBLOCK 19
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
33 #define TAG_JPEG2 21
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
35 #define TAG_LONG 0x100
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
36
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
37 /* flags for shape definition */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38 #define FLAG_MOVETO 0x01
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
39 #define FLAG_SETFILL0 0x02
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
40 #define FLAG_SETFILL1 0x04
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
41
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
42 /* character id used */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
43 #define BITMAP_ID 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
44 #define SHAPE_ID 1
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
45
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
46 typedef struct {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
47 offset_t duration_pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
48 offset_t tag_pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
49 int tag;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
50 } SWFContext;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
51
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
52 static void put_swf_tag(AVFormatContext *s, int tag)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
53 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
54 SWFContext *swf = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
55 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
56
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
57 swf->tag_pos = url_ftell(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
58 swf->tag = tag;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
59 /* reserve some room for the tag */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
60 if (tag & TAG_LONG) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
61 put_le16(pb, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
62 put_le32(pb, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
63 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
64 put_le16(pb, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
65 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
66 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
67
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
68 static void put_swf_end_tag(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
69 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
70 SWFContext *swf = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
71 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
72 offset_t pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
73 int tag_len, tag;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
74
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
75 pos = url_ftell(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
76 tag_len = pos - swf->tag_pos - 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
77 tag = swf->tag;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
78 url_fseek(pb, swf->tag_pos, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
79 if (tag & TAG_LONG) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
80 tag &= ~TAG_LONG;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
81 put_le16(pb, (tag << 6) | 0x3f);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
82 put_le32(pb, tag_len - 4);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
83 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
84 assert(tag_len < 0x3f);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
85 put_le16(pb, (tag << 6) | tag_len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
86 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
87 url_fseek(pb, pos, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
88 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
89
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
90 static inline void max_nbits(int *nbits_ptr, int val)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
91 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
92 int n;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
93
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
94 if (val == 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
95 return;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
96 val = abs(val);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
97 n = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
98 while (val != 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 n++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
100 val >>= 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
102 if (n > *nbits_ptr)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
103 *nbits_ptr = n;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
104 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
105
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
106 static void put_swf_rect(ByteIOContext *pb,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
107 int xmin, int xmax, int ymin, int ymax)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109 PutBitContext p;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 5
diff changeset
110 uint8_t buf[256];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
111 int nbits, mask;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
112
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
113 init_put_bits(&p, buf, sizeof(buf), NULL, NULL);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
114
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
115 nbits = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
116 max_nbits(&nbits, xmin);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
117 max_nbits(&nbits, xmax);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
118 max_nbits(&nbits, ymin);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
119 max_nbits(&nbits, ymax);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
120 mask = (1 << nbits) - 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
121
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
122 /* rectangle info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
123 put_bits(&p, 5, nbits);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
124 put_bits(&p, nbits, xmin & mask);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
125 put_bits(&p, nbits, xmax & mask);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
126 put_bits(&p, nbits, ymin & mask);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
127 put_bits(&p, nbits, ymax & mask);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
128
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
129 flush_put_bits(&p);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
130 put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
131 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
132
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
133 static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
134 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
135 int nbits, mask;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
136
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
137 put_bits(pb, 1, 1); /* edge */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
138 put_bits(pb, 1, 1); /* line select */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
139 nbits = 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
140 max_nbits(&nbits, dx);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
141 max_nbits(&nbits, dy);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
142
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
143 mask = (1 << nbits) - 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
144 put_bits(pb, 4, nbits - 2); /* 16 bits precision */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
145 if (dx == 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
146 put_bits(pb, 1, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
147 put_bits(pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
148 put_bits(pb, nbits, dy & mask);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
149 } else if (dy == 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
150 put_bits(pb, 1, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
151 put_bits(pb, 1, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
152 put_bits(pb, nbits, dx & mask);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
153 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
154 put_bits(pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
155 put_bits(pb, nbits, dx & mask);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
156 put_bits(pb, nbits, dy & mask);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
157 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
158 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
159
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
160 #define FRAC_BITS 16
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
161
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
162 /* put matrix (not size optimized */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
163 static void put_swf_matrix(ByteIOContext *pb,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
164 int a, int b, int c, int d, int tx, int ty)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
165 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
166 PutBitContext p;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 5
diff changeset
167 uint8_t buf[256];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
168
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
169 init_put_bits(&p, buf, sizeof(buf), NULL, NULL);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
170
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
171 put_bits(&p, 1, 1); /* a, d present */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172 put_bits(&p, 5, 20); /* nb bits */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
173 put_bits(&p, 20, a);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
174 put_bits(&p, 20, d);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
175
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
176 put_bits(&p, 1, 1); /* b, c present */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
177 put_bits(&p, 5, 20); /* nb bits */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
178 put_bits(&p, 20, c);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
179 put_bits(&p, 20, b);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
180
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
181 put_bits(&p, 5, 20); /* nb bits */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
182 put_bits(&p, 20, tx);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
183 put_bits(&p, 20, ty);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
184
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
185 flush_put_bits(&p);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
186 put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189 /* XXX: handle audio only */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
190 static int swf_write_header(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
191 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192 SWFContext *swf;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
193 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
194 AVCodecContext *enc, *audio_enc, *video_enc;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
195 PutBitContext p;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 5
diff changeset
196 uint8_t buf1[256];
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
197 int i, width, height, rate, rate_base;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
199 swf = av_malloc(sizeof(SWFContext));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
200 if (!swf)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
201 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
202 s->priv_data = swf;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
203
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
204 video_enc = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205 audio_enc = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207 enc = &s->streams[i]->codec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
208 if (enc->codec_type == CODEC_TYPE_AUDIO)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
209 audio_enc = enc;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
210 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
211 video_enc = enc;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
212 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
214 if (!video_enc) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215 /* currenty, cannot work correctly if audio only */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216 width = 320;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
217 height = 200;
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
218 rate = 10;
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
219 rate_base= 1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
221 width = video_enc->width;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
222 height = video_enc->height;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
223 rate = video_enc->frame_rate;
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
224 rate_base = video_enc->frame_rate_base;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
225 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
226
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
227 put_tag(pb, "FWS");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
228 put_byte(pb, 4); /* version (should use 4 for mpeg audio support) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
229 put_le32(pb, DUMMY_FILE_SIZE); /* dummy size
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
230 (will be patched if not streamed) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
231
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
232 put_swf_rect(pb, 0, width, 0, height);
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
233 put_le16(pb, (rate * 256) / rate_base); /* frame rate */
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
234 swf->duration_pos = url_ftell(pb);
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
235 put_le16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
236
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
237 /* define a shape with the jpeg inside */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
238
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
239 put_swf_tag(s, TAG_DEFINESHAPE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
240
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 put_le16(pb, SHAPE_ID); /* ID of shape */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
242 /* bounding rectangle */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
243 put_swf_rect(pb, 0, width, 0, height);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
244 /* style info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
245 put_byte(pb, 1); /* one fill style */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246 put_byte(pb, 0x41); /* clipped bitmap fill */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
247 put_le16(pb, BITMAP_ID); /* bitmap ID */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
248 /* position of the bitmap */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
249 put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
250 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251 put_byte(pb, 0); /* no line style */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
252
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
253 /* shape drawing */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
254 init_put_bits(&p, buf1, sizeof(buf1), NULL, NULL);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
255 put_bits(&p, 4, 1); /* one fill bit */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
256 put_bits(&p, 4, 0); /* zero line bit */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
257
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
258 put_bits(&p, 1, 0); /* not an edge */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
259 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
260 put_bits(&p, 5, 1); /* nbits */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
261 put_bits(&p, 1, 0); /* X */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
262 put_bits(&p, 1, 0); /* Y */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
263 put_bits(&p, 1, 1); /* set fill style 1 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
264
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
265 /* draw the rectangle ! */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
266 put_swf_line_edge(&p, width, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
267 put_swf_line_edge(&p, 0, height);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
268 put_swf_line_edge(&p, -width, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269 put_swf_line_edge(&p, 0, -height);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
271 /* end of shape */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272 put_bits(&p, 1, 0); /* not an edge */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
273 put_bits(&p, 5, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275 flush_put_bits(&p);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
276 put_buffer(pb, buf1, pbBufPtr(&p) - p.buf);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278 put_swf_end_tag(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
279
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
280
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
281 if (audio_enc) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
282 int v;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
283
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
284 /* start sound */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
285
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
286 v = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
287 switch(audio_enc->sample_rate) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
288 case 11025:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
289 v |= 1 << 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
290 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
291 case 22050:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
292 v |= 2 << 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
293 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
294 case 44100:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
295 v |= 3 << 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
296 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
297 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
298 /* not supported */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
299 av_free(swf);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
300 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
301 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
302 if (audio_enc->channels == 2)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
303 v |= 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
304 v |= 0x20; /* mp3 compressed */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
305 v |= 0x02; /* 16 bits */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
306
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
307 put_swf_tag(s, TAG_STREAMHEAD);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
308 put_byte(&s->pb, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
309 put_byte(&s->pb, v);
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
310 put_le16(&s->pb, (audio_enc->sample_rate * rate_base) / rate); /* avg samples per frame */
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
311
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
312
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
313 put_swf_end_tag(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
314 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
315
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
316 put_flush_packet(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
317 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
318 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
319
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
320 static int swf_write_video(AVFormatContext *s,
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 5
diff changeset
321 AVCodecContext *enc, uint8_t *buf, int size)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
322 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
323 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
324 static int tag_id = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
325
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
326 if (enc->frame_number > 1) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
327 /* remove the shape */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
328 put_swf_tag(s, TAG_REMOVEOBJECT);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
329 put_le16(pb, SHAPE_ID); /* shape ID */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
330 put_le16(pb, 1); /* depth */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
331 put_swf_end_tag(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
332
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
333 /* free the bitmap */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
334 put_swf_tag(s, TAG_FREECHARACTER);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
335 put_le16(pb, BITMAP_ID);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
336 put_swf_end_tag(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
337 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
338
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
339 put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
340
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
341 put_le16(pb, tag_id); /* ID of the image */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
342
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
343 /* a dummy jpeg header seems to be required */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
344 put_byte(pb, 0xff);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
345 put_byte(pb, 0xd8);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
346 put_byte(pb, 0xff);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
347 put_byte(pb, 0xd9);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
348 /* write the jpeg image */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
349 put_buffer(pb, buf, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
350
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
351 put_swf_end_tag(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
352
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
353 /* draw the shape */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
354
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
355 put_swf_tag(s, TAG_PLACEOBJECT);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
356 put_le16(pb, SHAPE_ID); /* shape ID */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
357 put_le16(pb, 1); /* depth */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
358 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
359 put_swf_end_tag(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
360
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
361 /* output the frame */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
362 put_swf_tag(s, TAG_SHOWFRAME);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
363 put_swf_end_tag(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
364
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
365 put_flush_packet(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
366 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
367 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
368
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 5
diff changeset
369 static int swf_write_audio(AVFormatContext *s, uint8_t *buf, int size)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
370 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
371 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
372
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
373 put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
374
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
375 put_buffer(pb, buf, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
376
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
377 put_swf_end_tag(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
378 put_flush_packet(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
379 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
380 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
381
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
382 static int swf_write_packet(AVFormatContext *s, int stream_index,
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 5
diff changeset
383 uint8_t *buf, int size, int force_pts)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
384 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
385 AVCodecContext *codec = &s->streams[stream_index]->codec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
386 if (codec->codec_type == CODEC_TYPE_AUDIO)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
387 return swf_write_audio(s, buf, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
388 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
389 return swf_write_video(s, codec, buf, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
390 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
391
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
392 static int swf_write_trailer(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
393 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
394 SWFContext *swf = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
395 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
396 AVCodecContext *enc, *video_enc;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
397 int file_size, i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
398
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
399 video_enc = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
400 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
401 enc = &s->streams[i]->codec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
402 if (enc->codec_type == CODEC_TYPE_VIDEO)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
403 video_enc = enc;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
404 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
405
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
406 put_swf_tag(s, TAG_END);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
407 put_swf_end_tag(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
408
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
409 put_flush_packet(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
410
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
411 /* patch file size and number of frames if not streamed */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
412 if (!url_is_streamed(&s->pb) && video_enc) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
413 file_size = url_ftell(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
414 url_fseek(pb, 4, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
415 put_le32(pb, file_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
416 url_fseek(pb, swf->duration_pos, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
417 put_le16(pb, video_enc->frame_number);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
418 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
419 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
420 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
421
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
422 /***********************************/
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
423 /* just to extract MP3 from swf */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
424
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
425 static int get_swf_tag(ByteIOContext *pb, int *len_ptr)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
426 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
427 int tag, len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
428
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
429 if (url_feof(pb))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
430 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
431
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
432 tag = get_le16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
433 len = tag & 0x3f;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
434 tag = tag >> 6;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
435 if (len == 0x3f) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
436 len = get_le32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
437 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
438 *len_ptr = len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
439 return tag;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
440 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
441
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
442
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
443 static int swf_probe(AVProbeData *p)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
444 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
445 /* check file header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
446 if (p->buf_size <= 16)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
447 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
448 if (p->buf[0] == 'F' && p->buf[1] == 'W' &&
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
449 p->buf[2] == 'S')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
450 return AVPROBE_SCORE_MAX;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
451 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
452 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
453 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
454
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
455 static int swf_read_header(AVFormatContext *s, AVFormatParameters *ap)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
456 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
457 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
458 int nbits, len, frame_rate, tag, v;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
459 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
460
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
461 if ((get_be32(pb) & 0xffffff00) != MKBETAG('F', 'W', 'S', 0))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
462 return -EIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
463 get_le32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
464 /* skip rectangle size */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
465 nbits = get_byte(pb) >> 3;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
466 len = (4 * nbits - 3 + 7) / 8;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
467 url_fskip(pb, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
468 frame_rate = get_le16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
469 get_le16(pb); /* frame count */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
470
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
471 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
472 tag = get_swf_tag(pb, &len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
473 if (tag < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
474 fprintf(stderr, "No streaming found in SWF\n");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
475 return -EIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
476 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
477 if (tag == TAG_STREAMHEAD) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
478 /* streaming found */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
479 get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
480 v = get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
481 get_le16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
482 /* if mp3 streaming found, OK */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
483 if ((v & 0x20) != 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
484 st = av_mallocz(sizeof(AVStream));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
485 if (!st)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
486 return -ENOMEM;
5
39c4c4336486 cleanup
michaelni
parents: 0
diff changeset
487 avcodec_get_context_defaults(&st->codec);
39c4c4336486 cleanup
michaelni
parents: 0
diff changeset
488
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
489 if (v & 0x01)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
490 st->codec.channels = 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
491 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
492 st->codec.channels = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
493 s->nb_streams = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
494 s->streams[0] = st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
495
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
496 switch((v>> 2) & 0x03) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
497 case 1:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
498 st->codec.sample_rate = 11025;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
499 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
500 case 2:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
501 st->codec.sample_rate = 22050;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
502 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
503 case 3:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
504 st->codec.sample_rate = 44100;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
505 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
506 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
507 av_free(st);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
508 return -EIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
509 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
510 st->codec.codec_type = CODEC_TYPE_AUDIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
511 st->codec.codec_id = CODEC_ID_MP2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
512 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
513 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
514 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
515 url_fskip(pb, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
516 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
517 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
518
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
519 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
520 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
521
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
522 static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
523 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
524 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
525 int tag, len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
526
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
527 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
528 tag = get_swf_tag(pb, &len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
529 if (tag < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
530 return -EIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
531 if (tag == TAG_STREAMBLOCK) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
532 av_new_packet(pkt, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
533 get_buffer(pb, pkt->data, pkt->size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
534 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
535 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
536 url_fskip(pb, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
537 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
538 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
539 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
540 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
541
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
542 static int swf_read_close(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
543 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
544 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
545 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
546
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
547 static AVInputFormat swf_iformat = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
548 "swf",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
549 "Flash format",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
550 0,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
551 swf_probe,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
552 swf_read_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
553 swf_read_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
554 swf_read_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
555 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
556
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
557 static AVOutputFormat swf_oformat = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
558 "swf",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
559 "Flash format",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
560 "application/x-shockwave-flash",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
561 "swf",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
562 sizeof(SWFContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
563 CODEC_ID_MP2,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
564 CODEC_ID_MJPEG,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
565 swf_write_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
566 swf_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
567 swf_write_trailer,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
568 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
569
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
570 int swf_init(void)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
571 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
572 av_register_input_format(&swf_iformat);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
573 av_register_output_format(&swf_oformat);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
574 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
575 }