test
[libav.git] / swfenc.c
CommitLineData
9aeeeb63
FB
1/*
2 * Flash Compatible Streaming Format
3 * Copyright (c) 2000 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19#include <stdlib.h>
20#include <stdio.h>
21#include <netinet/in.h>
22#include <string.h>
23#include "mpegenc.h"
24
25#include <assert.h>
26
27/* should have a generic way to indicate probable size */
28#define DUMMY_FILE_SIZE (100 * 1024 * 1024)
29#define DUMMY_DURATION 600 /* in seconds */
30
31#define TAG_END 0
32#define TAG_SHOWFRAME 1
33#define TAG_DEFINESHAPE 2
34#define TAG_FREECHARACTER 3
35#define TAG_PLACEOBJECT 4
36#define TAG_REMOVEOBJECT 5
37#define TAG_JPEG2 21
38
39#define TAG_LONG 0x100
40
41/* flags for shape definition */
42#define FLAG_MOVETO 0x01
43#define FLAG_SETFILL0 0x02
44#define FLAG_SETFILL1 0x04
45
46/* character id used */
47#define BITMAP_ID 0
48#define SHAPE_ID 1
49
50typedef struct {
51 long long duration_pos;
52 long long tag_pos;
53 int tag;
54} SWFContext;
55
56static void put_swf_tag(AVFormatContext *s, int tag)
57{
58 SWFContext *swf = s->priv_data;
59 PutByteContext *pb = &s->pb;
60
61 swf->tag_pos = put_pos(pb);
62 swf->tag = tag;
63 /* reserve some room for the tag */
64 if (tag & TAG_LONG) {
65 put_le16(pb, 0);
66 put_le32(pb, 0);
67 } else {
68 put_le16(pb, 0);
69 }
70}
71
72static void put_swf_end_tag(AVFormatContext *s)
73{
74 SWFContext *swf = s->priv_data;
75 PutByteContext *pb = &s->pb;
76 long long pos;
77 int tag_len, tag;
78
79 pos = put_pos(pb);
80 tag_len = pos - swf->tag_pos - 2;
81 tag = swf->tag;
82 put_seek(pb, swf->tag_pos, SEEK_SET);
83 if (tag & TAG_LONG) {
84 tag &= ~TAG_LONG;
85 put_le16(pb, (tag << 6) | 0x3f);
86 put_le32(pb, tag_len - 4);
87 } else {
88 assert(tag_len < 0x3f);
89 put_le16(pb, (tag << 6) | tag_len);
90 }
91 put_seek(pb, pos, SEEK_SET);
92}
93
94static inline void max_nbits(int *nbits_ptr, int val)
95{
96 int n;
97
98 if (val == 0)
99 return;
100 val = abs(val);
101 n = 1;
102 while (val != 0) {
103 n++;
104 val >>= 1;
105 }
106 if (n > *nbits_ptr)
107 *nbits_ptr = n;
108}
109
110static void put_swf_rect(PutByteContext *pb,
111 int xmin, int xmax, int ymin, int ymax)
112{
113 PutBitContext p;
114 UINT8 buf[256];
115 int nbits, mask;
116
117 init_put_bits(&p, buf, sizeof(buf), NULL, NULL);
118
119 nbits = 0;
120 max_nbits(&nbits, xmin);
121 max_nbits(&nbits, xmax);
122 max_nbits(&nbits, ymin);
123 max_nbits(&nbits, ymax);
124 mask = (1 << nbits) - 1;
125
126 /* rectangle info */
127 put_bits(&p, 5, nbits);
128 put_bits(&p, nbits, xmin & mask);
129 put_bits(&p, nbits, xmax & mask);
130 put_bits(&p, nbits, ymin & mask);
131 put_bits(&p, nbits, ymax & mask);
132
133 flush_put_bits(&p);
134 put_buffer(pb, buf, p.buf_ptr - p.buf);
135}
136
137static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
138{
139 int nbits, mask;
140
141 put_bits(pb, 1, 1); /* edge */
142 put_bits(pb, 1, 1); /* line select */
143 nbits = 2;
144 max_nbits(&nbits, dx);
145 max_nbits(&nbits, dy);
146
147 mask = (1 << nbits) - 1;
148 put_bits(pb, 4, nbits - 2); /* 16 bits precision */
149 if (dx == 0) {
150 put_bits(pb, 1, 0);
151 put_bits(pb, 1, 1);
152 put_bits(pb, nbits, dy & mask);
153 } else if (dy == 0) {
154 put_bits(pb, 1, 0);
155 put_bits(pb, 1, 0);
156 put_bits(pb, nbits, dx & mask);
157 } else {
158 put_bits(pb, 1, 1);
159 put_bits(pb, nbits, dx & mask);
160 put_bits(pb, nbits, dy & mask);
161 }
162}
163
164#define FRAC_BITS 16
165
166/* put matrix (not size optimized */
167static void put_swf_matrix(PutByteContext *pb,
168 int a, int b, int c, int d, int tx, int ty)
169{
170 PutBitContext p;
171 UINT8 buf[256];
172
173 init_put_bits(&p, buf, sizeof(buf), NULL, NULL);
174
175 put_bits(&p, 1, 1); /* a, d present */
176 put_bits(&p, 5, 20); /* nb bits */
177 put_bits(&p, 20, a);
178 put_bits(&p, 20, d);
179
180 put_bits(&p, 1, 1); /* b, c present */
181 put_bits(&p, 5, 20); /* nb bits */
182 put_bits(&p, 20, c);
183 put_bits(&p, 20, b);
184
185 put_bits(&p, 5, 20); /* nb bits */
186 put_bits(&p, 20, tx);
187 put_bits(&p, 20, ty);
188
189 flush_put_bits(&p);
190 put_buffer(pb, buf, p.buf_ptr - p.buf);
191}
192
193static int swf_write_header(AVFormatContext *s)
194{
195 SWFContext *swf;
196 PutByteContext *pb = &s->pb;
197 AVEncodeContext *enc = s->video_enc;
198 PutBitContext p;
199 UINT8 buf1[256];
200
201 swf = malloc(sizeof(SWFContext));
202 if (!swf)
203 return -1;
204 s->priv_data = swf;
205
206 put_tag(pb, "FWS");
207 put_byte(pb, 3); /* version (should use 4 for mpeg audio support) */
208 put_le32(pb, DUMMY_FILE_SIZE); /* dummy size
209 (will be patched if not streamed) */
210
211 put_swf_rect(pb, 0, enc->width, 0, enc->height);
212 put_le16(pb, enc->rate << 8); /* frame rate */
213 swf->duration_pos = put_pos(pb);
214 put_le16(pb, DUMMY_DURATION * enc->rate); /* frame count */
215
216 /* define a shape with the jpeg inside */
217
218 put_swf_tag(s, TAG_DEFINESHAPE);
219
220 put_le16(pb, SHAPE_ID); /* ID of shape */
221 /* bounding rectangle */
222 put_swf_rect(pb, 0, enc->width, 0, enc->height);
223 /* style info */
224 put_byte(pb, 1); /* one fill style */
225 put_byte(pb, 0x41); /* clipped bitmap fill */
226 put_le16(pb, BITMAP_ID); /* bitmap ID */
227 /* position of the bitmap */
228 put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
229 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
230 put_byte(pb, 0); /* no line style */
231
232 /* shape drawing */
233 init_put_bits(&p, buf1, sizeof(buf1), NULL, NULL);
234 put_bits(&p, 4, 1); /* one fill bit */
235 put_bits(&p, 4, 0); /* zero line bit */
236
237 put_bits(&p, 1, 0); /* not an edge */
238 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
239 put_bits(&p, 5, 1); /* nbits */
240 put_bits(&p, 1, 0); /* X */
241 put_bits(&p, 1, 0); /* Y */
242 put_bits(&p, 1, 1); /* set fill style 1 */
243
244 /* draw the rectangle ! */
245 put_swf_line_edge(&p, enc->width, 0);
246 put_swf_line_edge(&p, 0, enc->height);
247 put_swf_line_edge(&p, -enc->width, 0);
248 put_swf_line_edge(&p, 0, -enc->height);
249
250 /* end of shape */
251 put_bits(&p, 1, 0); /* not an edge */
252 put_bits(&p, 5, 0);
253
254 flush_put_bits(&p);
255 put_buffer(pb, buf1, p.buf_ptr - p.buf);
256
257 put_swf_end_tag(s);
258
259 put_flush_packet(&s->pb);
260 return 0;
261}
262
263static int swf_write_video(AVFormatContext *s, UINT8 *buf, int size)
264{
265 PutByteContext *pb = &s->pb;
266 AVEncodeContext *enc = s->video_enc;
267 static int tag_id = 0;
268
269 if (enc->frame_number > 1) {
270 /* remove the shape */
271 put_swf_tag(s, TAG_REMOVEOBJECT);
272 put_le16(pb, SHAPE_ID); /* shape ID */
273 put_le16(pb, 1); /* depth */
274 put_swf_end_tag(s);
275
276 /* free the bitmap */
277 put_swf_tag(s, TAG_FREECHARACTER);
278 put_le16(pb, BITMAP_ID);
279 put_swf_end_tag(s);
280 }
281
282 put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
283
284 put_le16(pb, tag_id); /* ID of the image */
285
286 /* a dummy jpeg header seems to be required */
287 put_byte(pb, 0xff);
288 put_byte(pb, 0xd8);
289 put_byte(pb, 0xff);
290 put_byte(pb, 0xd9);
291 /* write the jpeg image */
292 put_buffer(pb, buf, size);
293
294 put_swf_end_tag(s);
295
296 /* draw the shape */
297
298 put_swf_tag(s, TAG_PLACEOBJECT);
299 put_le16(pb, SHAPE_ID); /* shape ID */
300 put_le16(pb, 1); /* depth */
301 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
302 put_swf_end_tag(s);
303
304 /* output the frame */
305 put_swf_tag(s, TAG_SHOWFRAME);
306 put_swf_end_tag(s);
307
308 put_flush_packet(&s->pb);
309 return 0;
310}
311
312static int swf_write_trailer(AVFormatContext *s)
313{
314 SWFContext *swf = s->priv_data;
315 PutByteContext *pb = &s->pb;
316 int file_size;
317 AVEncodeContext *enc = s->video_enc;
318
319 put_swf_tag(s, TAG_END);
320 put_swf_end_tag(s);
321
322 put_flush_packet(&s->pb);
323
324 /* patch file size and number of frames if not streamed */
325 if (!s->is_streamed) {
326 file_size = put_pos(pb);
327 put_seek(pb, 4, SEEK_SET);
328 put_le32(pb, file_size);
329 put_seek(pb, swf->duration_pos, SEEK_SET);
330 put_le16(pb, enc->frame_number);
331 }
332 free(swf);
333 return 0;
334}
335
336AVFormat swf_format = {
337 "swf",
338 "Flash format",
339 "application/x-shockwave-flash",
340 "swf",
341 CODEC_ID_NONE,
342 CODEC_ID_MJPEG,
343 swf_write_header,
344 NULL,
345 swf_write_video,
346 swf_write_trailer,
347};