simplify
[libav.git] / libavformat / swfenc.c
CommitLineData
de6d9b64 1/*
58c37c30 2 * Flash Compatible Streaming Format muxer
17269bdf 3 * Copyright (c) 2000 Fabrice Bellard.
747a0554 4 * Copyright (c) 2003 Tinic Uro.
de6d9b64 5 *
b78e7197
DB
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
17269bdf
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 12 *
b78e7197 13 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17269bdf
FB
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
de6d9b64 17 *
17269bdf 18 * You should have received a copy of the GNU Lesser General Public
b78e7197 19 * License along with FFmpeg; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 21 */
245976da
DB
22
23#include "libavcodec/bitstream.h"
de6d9b64 24#include "avformat.h"
3b35f4ab 25#include "swf.h"
dd8a46d9 26
de6d9b64
FB
27static void put_swf_tag(AVFormatContext *s, int tag)
28{
29 SWFContext *swf = s->priv_data;
899681cd 30 ByteIOContext *pb = s->pb;
de6d9b64
FB
31
32 swf->tag_pos = url_ftell(pb);
33 swf->tag = tag;
34 /* reserve some room for the tag */
35 if (tag & TAG_LONG) {
36 put_le16(pb, 0);
37 put_le32(pb, 0);
38 } else {
39 put_le16(pb, 0);
40 }
41}
42
43static void put_swf_end_tag(AVFormatContext *s)
44{
45 SWFContext *swf = s->priv_data;
899681cd 46 ByteIOContext *pb = s->pb;
8be1c656 47 offset_t pos;
de6d9b64
FB
48 int tag_len, tag;
49
50 pos = url_ftell(pb);
51 tag_len = pos - swf->tag_pos - 2;
52 tag = swf->tag;
53 url_fseek(pb, swf->tag_pos, SEEK_SET);
54 if (tag & TAG_LONG) {
55 tag &= ~TAG_LONG;
56 put_le16(pb, (tag << 6) | 0x3f);
57 put_le32(pb, tag_len - 4);
58 } else {
59 assert(tag_len < 0x3f);
60 put_le16(pb, (tag << 6) | tag_len);
61 }
62 url_fseek(pb, pos, SEEK_SET);
63}
64
65static inline void max_nbits(int *nbits_ptr, int val)
66{
67 int n;
68
69 if (val == 0)
70 return;
71 val = abs(val);
72 n = 1;
73 while (val != 0) {
74 n++;
75 val >>= 1;
76 }
77 if (n > *nbits_ptr)
78 *nbits_ptr = n;
79}
80
115329f1 81static void put_swf_rect(ByteIOContext *pb,
de6d9b64
FB
82 int xmin, int xmax, int ymin, int ymax)
83{
84 PutBitContext p;
0c1a9eda 85 uint8_t buf[256];
de6d9b64
FB
86 int nbits, mask;
87
117a5490 88 init_put_bits(&p, buf, sizeof(buf));
115329f1 89
de6d9b64
FB
90 nbits = 0;
91 max_nbits(&nbits, xmin);
92 max_nbits(&nbits, xmax);
93 max_nbits(&nbits, ymin);
94 max_nbits(&nbits, ymax);
95 mask = (1 << nbits) - 1;
96
97 /* rectangle info */
98 put_bits(&p, 5, nbits);
99 put_bits(&p, nbits, xmin & mask);
100 put_bits(&p, nbits, xmax & mask);
101 put_bits(&p, nbits, ymin & mask);
102 put_bits(&p, nbits, ymax & mask);
115329f1 103
de6d9b64 104 flush_put_bits(&p);
17592475 105 put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
de6d9b64
FB
106}
107
108static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
109{
110 int nbits, mask;
111
112 put_bits(pb, 1, 1); /* edge */
113 put_bits(pb, 1, 1); /* line select */
114 nbits = 2;
115 max_nbits(&nbits, dx);
116 max_nbits(&nbits, dy);
117
118 mask = (1 << nbits) - 1;
119 put_bits(pb, 4, nbits - 2); /* 16 bits precision */
120 if (dx == 0) {
c78ed542
BC
121 put_bits(pb, 1, 0);
122 put_bits(pb, 1, 1);
123 put_bits(pb, nbits, dy & mask);
de6d9b64 124 } else if (dy == 0) {
c78ed542
BC
125 put_bits(pb, 1, 0);
126 put_bits(pb, 1, 0);
127 put_bits(pb, nbits, dx & mask);
de6d9b64 128 } else {
c78ed542
BC
129 put_bits(pb, 1, 1);
130 put_bits(pb, nbits, dx & mask);
131 put_bits(pb, nbits, dy & mask);
de6d9b64
FB
132 }
133}
134
135#define FRAC_BITS 16
136
de6d9b64
FB
137static void put_swf_matrix(ByteIOContext *pb,
138 int a, int b, int c, int d, int tx, int ty)
139{
140 PutBitContext p;
0c1a9eda 141 uint8_t buf[256];
747a0554 142 int nbits;
de6d9b64 143
117a5490 144 init_put_bits(&p, buf, sizeof(buf));
115329f1 145
de6d9b64 146 put_bits(&p, 1, 1); /* a, d present */
747a0554
TU
147 nbits = 1;
148 max_nbits(&nbits, a);
149 max_nbits(&nbits, d);
150 put_bits(&p, 5, nbits); /* nb bits */
151 put_bits(&p, nbits, a);
152 put_bits(&p, nbits, d);
115329f1 153
de6d9b64 154 put_bits(&p, 1, 1); /* b, c present */
747a0554
TU
155 nbits = 1;
156 max_nbits(&nbits, c);
157 max_nbits(&nbits, b);
158 put_bits(&p, 5, nbits); /* nb bits */
159 put_bits(&p, nbits, c);
160 put_bits(&p, nbits, b);
161
162 nbits = 1;
163 max_nbits(&nbits, tx);
164 max_nbits(&nbits, ty);
165 put_bits(&p, 5, nbits); /* nb bits */
166 put_bits(&p, nbits, tx);
167 put_bits(&p, nbits, ty);
de6d9b64
FB
168
169 flush_put_bits(&p);
17592475 170 put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
de6d9b64
FB
171}
172
de6d9b64
FB
173static int swf_write_header(AVFormatContext *s)
174{
fed7d067 175 SWFContext *swf = s->priv_data;
899681cd 176 ByteIOContext *pb = s->pb;
de6d9b64
FB
177 AVCodecContext *enc, *audio_enc, *video_enc;
178 PutBitContext p;
0c1a9eda 179 uint8_t buf1[256];
14bea432 180 int i, width, height, rate, rate_base;
dfb400a8 181 int is_avm2;
de6d9b64 182
747a0554 183 swf->audio_in_pos = 0;
747a0554 184 swf->sound_samples = 0;
747a0554
TU
185 swf->swf_frame_number = 0;
186 swf->video_frame_number = 0;
747a0554 187
de6d9b64
FB
188 video_enc = NULL;
189 audio_enc = NULL;
190 for(i=0;i<s->nb_streams;i++) {
01f4895c 191 enc = s->streams[i]->codec;
013e0a8f
BC
192 if (enc->codec_type == CODEC_TYPE_AUDIO) {
193 if (enc->codec_id == CODEC_ID_MP3) {
194 if (!enc->frame_size) {
195 av_log(s, AV_LOG_ERROR, "audio frame size not set\n");
196 return -1;
197 }
198 audio_enc = enc;
199 } else {
5095aaa9 200 av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n");
013e0a8f
BC
201 return -1;
202 }
203 } else {
5fcf2df3
BC
204 if (enc->codec_id == CODEC_ID_VP6F ||
205 enc->codec_id == CODEC_ID_FLV1 ||
206 enc->codec_id == CODEC_ID_MJPEG) {
747a0554
TU
207 video_enc = enc;
208 } else {
5095aaa9 209 av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n");
747a0554
TU
210 return -1;
211 }
212 }
de6d9b64
FB
213 }
214
215 if (!video_enc) {
df3a80b5 216 /* currently, cannot work correctly if audio only */
747a0554 217 swf->video_type = 0;
de6d9b64
FB
218 width = 320;
219 height = 200;
14bea432
MN
220 rate = 10;
221 rate_base= 1;
de6d9b64 222 } else {
747a0554 223 swf->video_type = video_enc->codec_id;
de6d9b64
FB
224 width = video_enc->width;
225 height = video_enc->height;
c0df9d75
MN
226 rate = video_enc->time_base.den;
227 rate_base = video_enc->time_base.num;
de6d9b64
FB
228 }
229
5fcf2df3 230 if (!audio_enc) {
747a0554 231 swf->audio_type = 0;
5fcf2df3 232 swf->samples_per_frame = (44100. * rate_base) / rate;
747a0554
TU
233 } else {
234 swf->audio_type = audio_enc->codec_id;
5fcf2df3 235 swf->samples_per_frame = (audio_enc->sample_rate * rate_base) / rate;
747a0554
TU
236 }
237
dfb400a8
PE
238 is_avm2 = !strcmp("avm2", s->oformat->name);
239
de6d9b64 240 put_tag(pb, "FWS");
9caf6781 241 if (is_avm2)
dfb400a8 242 put_byte(pb, 9);
9caf6781 243 else if (video_enc && video_enc->codec_id == CODEC_ID_VP6F)
5ce117c3 244 put_byte(pb, 8); /* version (version 8 and above support VP6 codec) */
9caf6781 245 else if (video_enc && video_enc->codec_id == CODEC_ID_FLV1)
747a0554 246 put_byte(pb, 6); /* version (version 6 and above support FLV1 codec) */
9caf6781 247 else
747a0554 248 put_byte(pb, 4); /* version (should use 4 for mpeg audio support) */
115329f1
DB
249 put_le32(pb, DUMMY_FILE_SIZE); /* dummy size
250 (will be patched if not streamed) */
de6d9b64 251
747a0554 252 put_swf_rect(pb, 0, width * 20, 0, height * 20);
14bea432 253 put_le16(pb, (rate * 256) / rate_base); /* frame rate */
de6d9b64 254 swf->duration_pos = url_ftell(pb);
14bea432 255 put_le16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
115329f1 256
dfb400a8
PE
257 /* avm2/swf v9 (also v8?) files require a file attribute tag */
258 if (is_avm2) {
259 put_swf_tag(s, TAG_FILEATTRIBUTES);
260 put_le32(pb, 1<<3); /* set ActionScript v3/AVM2 flag */
261 put_swf_end_tag(s);
262 }
263
de6d9b64 264 /* define a shape with the jpeg inside */
9caf6781 265 if (video_enc && video_enc->codec_id == CODEC_ID_MJPEG) {
747a0554
TU
266 put_swf_tag(s, TAG_DEFINESHAPE);
267
268 put_le16(pb, SHAPE_ID); /* ID of shape */
269 /* bounding rectangle */
270 put_swf_rect(pb, 0, width, 0, height);
271 /* style info */
272 put_byte(pb, 1); /* one fill style */
273 put_byte(pb, 0x41); /* clipped bitmap fill */
274 put_le16(pb, BITMAP_ID); /* bitmap ID */
275 /* position of the bitmap */
115329f1 276 put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
c78ed542 277 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
747a0554 278 put_byte(pb, 0); /* no line style */
115329f1 279
747a0554
TU
280 /* shape drawing */
281 init_put_bits(&p, buf1, sizeof(buf1));
282 put_bits(&p, 4, 1); /* one fill bit */
283 put_bits(&p, 4, 0); /* zero line bit */
115329f1 284
747a0554
TU
285 put_bits(&p, 1, 0); /* not an edge */
286 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
287 put_bits(&p, 5, 1); /* nbits */
288 put_bits(&p, 1, 0); /* X */
289 put_bits(&p, 1, 0); /* Y */
290 put_bits(&p, 1, 1); /* set fill style 1 */
115329f1 291
747a0554
TU
292 /* draw the rectangle ! */
293 put_swf_line_edge(&p, width, 0);
294 put_swf_line_edge(&p, 0, height);
295 put_swf_line_edge(&p, -width, 0);
296 put_swf_line_edge(&p, 0, -height);
115329f1 297
747a0554
TU
298 /* end of shape */
299 put_bits(&p, 1, 0); /* not an edge */
300 put_bits(&p, 5, 0);
de6d9b64 301
747a0554
TU
302 flush_put_bits(&p);
303 put_buffer(pb, buf1, pbBufPtr(&p) - p.buf);
de6d9b64 304
747a0554
TU
305 put_swf_end_tag(s);
306 }
115329f1 307
5fcf2df3 308 if (audio_enc && audio_enc->codec_id == CODEC_ID_MP3) {
de6d9b64
FB
309 int v;
310
311 /* start sound */
747a0554 312 put_swf_tag(s, TAG_STREAMHEAD2);
de6d9b64
FB
313
314 v = 0;
315 switch(audio_enc->sample_rate) {
316 case 11025:
317 v |= 1 << 2;
318 break;
319 case 22050:
320 v |= 2 << 2;
321 break;
322 case 44100:
323 v |= 3 << 2;
324 break;
325 default:
326 /* not supported */
755bfeab 327 av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n");
de6d9b64
FB
328 return -1;
329 }
747a0554 330 v |= 0x02; /* 16 bit playback */
de6d9b64 331 if (audio_enc->channels == 2)
747a0554 332 v |= 0x01; /* stereo playback */
899681cd 333 put_byte(s->pb, v);
de6d9b64 334 v |= 0x20; /* mp3 compressed */
899681cd
BA
335 put_byte(s->pb, v);
336 put_le16(s->pb, swf->samples_per_frame); /* avg samples per frame */
337 put_le16(s->pb, 0);
115329f1 338
de6d9b64
FB
339 put_swf_end_tag(s);
340 }
341
899681cd 342 put_flush_packet(s->pb);
de6d9b64
FB
343 return 0;
344}
345
115329f1 346static int swf_write_video(AVFormatContext *s,
49057904 347 AVCodecContext *enc, const uint8_t *buf, int size)
de6d9b64 348{
747a0554 349 SWFContext *swf = s->priv_data;
899681cd 350 ByteIOContext *pb = s->pb;
115329f1 351
747a0554 352 /* Flash Player limit */
9caf6781 353 if (swf->swf_frame_number == 16000)
bc874dae 354 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
de6d9b64 355
5fcf2df3
BC
356 if (swf->video_type == CODEC_ID_VP6F ||
357 swf->video_type == CODEC_ID_FLV1) {
358 if (swf->video_frame_number == 0) {
c78ed542
BC
359 /* create a new video object */
360 put_swf_tag(s, TAG_VIDEOSTREAM);
361 put_le16(pb, VIDEO_ID);
5fcf2df3 362 put_le16(pb, 15000); /* hard flash player limit */
c78ed542
BC
363 put_le16(pb, enc->width);
364 put_le16(pb, enc->height);
365 put_byte(pb, 0);
366 put_byte(pb,codec_get_tag(swf_codec_tags,swf->video_type));
367 put_swf_end_tag(s);
368
369 /* place the video object for the first time */
370 put_swf_tag(s, TAG_PLACEOBJECT2);
371 put_byte(pb, 0x36);
372 put_le16(pb, 1);
373 put_le16(pb, VIDEO_ID);
374 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
5fcf2df3 375 put_le16(pb, swf->video_frame_number);
8d4f0e67 376 put_tag(pb, "video");
c78ed542
BC
377 put_byte(pb, 0x00);
378 put_swf_end_tag(s);
379 } else {
380 /* mark the character for update */
381 put_swf_tag(s, TAG_PLACEOBJECT2);
382 put_byte(pb, 0x11);
383 put_le16(pb, 1);
5fcf2df3 384 put_le16(pb, swf->video_frame_number);
c78ed542
BC
385 put_swf_end_tag(s);
386 }
115329f1 387
c78ed542
BC
388 /* set video frame data */
389 put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG);
390 put_le16(pb, VIDEO_ID);
5fcf2df3 391 put_le16(pb, swf->video_frame_number++);
c78ed542
BC
392 put_buffer(pb, buf, size);
393 put_swf_end_tag(s);
5fcf2df3 394 } else if (swf->video_type == CODEC_ID_MJPEG) {
c78ed542
BC
395 if (swf->swf_frame_number > 0) {
396 /* remove the shape */
397 put_swf_tag(s, TAG_REMOVEOBJECT);
398 put_le16(pb, SHAPE_ID); /* shape ID */
399 put_le16(pb, 1); /* depth */
400 put_swf_end_tag(s);
401
402 /* free the bitmap */
403 put_swf_tag(s, TAG_FREECHARACTER);
404 put_le16(pb, BITMAP_ID);
405 put_swf_end_tag(s);
406 }
115329f1 407
c78ed542 408 put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
115329f1 409
c78ed542 410 put_le16(pb, BITMAP_ID); /* ID of the image */
115329f1 411
c78ed542 412 /* a dummy jpeg header seems to be required */
6ef445fe 413 put_be32(pb, 0xffd8ffd9);
c78ed542
BC
414 /* write the jpeg image */
415 put_buffer(pb, buf, size);
115329f1 416
c78ed542 417 put_swf_end_tag(s);
115329f1 418
c78ed542 419 /* draw the shape */
115329f1 420
c78ed542
BC
421 put_swf_tag(s, TAG_PLACEOBJECT);
422 put_le16(pb, SHAPE_ID); /* shape ID */
423 put_le16(pb, 1); /* depth */
424 put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0);
425 put_swf_end_tag(s);
c78ed542 426 }
115329f1 427
c78ed542 428 swf->swf_frame_number ++;
de6d9b64 429
747a0554 430 /* streaming sound always should be placed just before showframe tags */
013e0a8f 431 if (swf->audio_type && swf->audio_in_pos) {
747a0554 432 put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
013e0a8f
BC
433 put_le16(pb, swf->sound_samples);
434 put_le16(pb, 0); // seek samples
435 put_buffer(pb, swf->audio_fifo, swf->audio_in_pos);
747a0554 436 put_swf_end_tag(s);
115329f1 437
747a0554 438 /* update FIFO */
013e0a8f
BC
439 swf->sound_samples = 0;
440 swf->audio_in_pos = 0;
747a0554
TU
441 }
442
de6d9b64
FB
443 /* output the frame */
444 put_swf_tag(s, TAG_SHOWFRAME);
445 put_swf_end_tag(s);
115329f1 446
899681cd 447 put_flush_packet(s->pb);
115329f1 448
de6d9b64
FB
449 return 0;
450}
451
115329f1 452static int swf_write_audio(AVFormatContext *s,
747a0554 453 AVCodecContext *enc, const uint8_t *buf, int size)
de6d9b64 454{
747a0554 455 SWFContext *swf = s->priv_data;
de6d9b64 456
747a0554 457 /* Flash Player limit */
9caf6781 458 if (swf->swf_frame_number == 16000)
bc874dae 459 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
747a0554 460
013e0a8f
BC
461 if (swf->audio_in_pos + size >= AUDIO_FIFO_SIZE) {
462 av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n");
463 return -1;
747a0554
TU
464 }
465
013e0a8f
BC
466 memcpy(swf->audio_fifo + swf->audio_in_pos, buf, size);
467 swf->audio_in_pos += size;
468 swf->sound_samples += enc->frame_size;
469
747a0554 470 /* if audio only stream make sure we add swf frames */
9caf6781 471 if (swf->video_type == 0)
747a0554 472 swf_write_video(s, enc, 0, 0);
de6d9b64 473
de6d9b64
FB
474 return 0;
475}
476
e928649b 477static int swf_write_packet(AVFormatContext *s, AVPacket *pkt)
de6d9b64 478{
01f4895c 479 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
de6d9b64 480 if (codec->codec_type == CODEC_TYPE_AUDIO)
e928649b 481 return swf_write_audio(s, codec, pkt->data, pkt->size);
de6d9b64 482 else
e928649b 483 return swf_write_video(s, codec, pkt->data, pkt->size);
de6d9b64
FB
484}
485
486static int swf_write_trailer(AVFormatContext *s)
487{
488 SWFContext *swf = s->priv_data;
899681cd 489 ByteIOContext *pb = s->pb;
de6d9b64
FB
490 AVCodecContext *enc, *video_enc;
491 int file_size, i;
492
493 video_enc = NULL;
494 for(i=0;i<s->nb_streams;i++) {
01f4895c 495 enc = s->streams[i]->codec;
de6d9b64
FB
496 if (enc->codec_type == CODEC_TYPE_VIDEO)
497 video_enc = enc;
498 }
499
500 put_swf_tag(s, TAG_END);
501 put_swf_end_tag(s);
115329f1 502
899681cd 503 put_flush_packet(s->pb);
de6d9b64
FB
504
505 /* patch file size and number of frames if not streamed */
899681cd 506 if (!url_is_streamed(s->pb) && video_enc) {
de6d9b64
FB
507 file_size = url_ftell(pb);
508 url_fseek(pb, 4, SEEK_SET);
509 put_le32(pb, file_size);
510 url_fseek(pb, swf->duration_pos, SEEK_SET);
511 put_le16(pb, video_enc->frame_number);
3439dc95 512 url_fseek(pb, file_size, SEEK_SET);
de6d9b64 513 }
de6d9b64
FB
514 return 0;
515}
de6d9b64 516
ff70e601
MR
517#ifdef CONFIG_SWF_MUXER
518AVOutputFormat swf_muxer = {
de6d9b64
FB
519 "swf",
520 "Flash format",
521 "application/x-shockwave-flash",
522 "swf",
c9a65ca8 523 sizeof(SWFContext),
747a0554
TU
524 CODEC_ID_MP3,
525 CODEC_ID_FLV1,
de6d9b64
FB
526 swf_write_header,
527 swf_write_packet,
528 swf_write_trailer,
de6d9b64 529};
ff70e601 530#endif
dfb400a8
PE
531#ifdef CONFIG_AVM2_MUXER
532AVOutputFormat avm2_muxer = {
533 "avm2",
534 "Flash 9 (AVM2) format",
535 "application/x-shockwave-flash",
afecbec2 536 NULL,
dfb400a8
PE
537 sizeof(SWFContext),
538 CODEC_ID_MP3,
539 CODEC_ID_FLV1,
540 swf_write_header,
541 swf_write_packet,
542 swf_write_trailer,
543};
544#endif