]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/swfenc.c
simplify and use version instead
[frescor/ffmpeg.git] / libavformat / swfenc.c
1 /*
2  * Flash Compatible Streaming Format muxer
3  * Copyright (c) 2000 Fabrice Bellard.
4  * Copyright (c) 2003 Tinic Uro.
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavcodec/bitstream.h"
24 #include "avformat.h"
25 #include "swf.h"
26
27 static void put_swf_tag(AVFormatContext *s, int tag)
28 {
29     SWFContext *swf = s->priv_data;
30     ByteIOContext *pb = s->pb;
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
43 static void put_swf_end_tag(AVFormatContext *s)
44 {
45     SWFContext *swf = s->priv_data;
46     ByteIOContext *pb = s->pb;
47     offset_t pos;
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
65 static 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
81 static void put_swf_rect(ByteIOContext *pb,
82                          int xmin, int xmax, int ymin, int ymax)
83 {
84     PutBitContext p;
85     uint8_t buf[256];
86     int nbits, mask;
87
88     init_put_bits(&p, buf, sizeof(buf));
89
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);
103
104     flush_put_bits(&p);
105     put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
106 }
107
108 static 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) {
121         put_bits(pb, 1, 0);
122         put_bits(pb, 1, 1);
123         put_bits(pb, nbits, dy & mask);
124     } else if (dy == 0) {
125         put_bits(pb, 1, 0);
126         put_bits(pb, 1, 0);
127         put_bits(pb, nbits, dx & mask);
128     } else {
129         put_bits(pb, 1, 1);
130         put_bits(pb, nbits, dx & mask);
131         put_bits(pb, nbits, dy & mask);
132     }
133 }
134
135 #define FRAC_BITS 16
136
137 static void put_swf_matrix(ByteIOContext *pb,
138                            int a, int b, int c, int d, int tx, int ty)
139 {
140     PutBitContext p;
141     uint8_t buf[256];
142     int nbits;
143
144     init_put_bits(&p, buf, sizeof(buf));
145
146     put_bits(&p, 1, 1); /* a, d present */
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);
153
154     put_bits(&p, 1, 1); /* b, c present */
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);
168
169     flush_put_bits(&p);
170     put_buffer(pb, buf, pbBufPtr(&p) - p.buf);
171 }
172
173 static int swf_write_header(AVFormatContext *s)
174 {
175     SWFContext *swf = s->priv_data;
176     ByteIOContext *pb = s->pb;
177     AVCodecContext *enc, *audio_enc, *video_enc;
178     PutBitContext p;
179     uint8_t buf1[256];
180     int i, width, height, rate, rate_base;
181     int version;
182
183     swf->audio_in_pos = 0;
184     swf->sound_samples = 0;
185     swf->swf_frame_number = 0;
186     swf->video_frame_number = 0;
187
188     video_enc = NULL;
189     audio_enc = NULL;
190     for(i=0;i<s->nb_streams;i++) {
191         enc = s->streams[i]->codec;
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 {
200                 av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n");
201                 return -1;
202             }
203         } else {
204             if (enc->codec_id == CODEC_ID_VP6F ||
205                 enc->codec_id == CODEC_ID_FLV1 ||
206                 enc->codec_id == CODEC_ID_MJPEG) {
207                 video_enc = enc;
208             } else {
209                 av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n");
210                 return -1;
211             }
212         }
213     }
214
215     if (!video_enc) {
216         /* currently, cannot work correctly if audio only */
217         swf->video_type = 0;
218         width = 320;
219         height = 200;
220         rate = 10;
221         rate_base= 1;
222     } else {
223         swf->video_type = video_enc->codec_id;
224         width = video_enc->width;
225         height = video_enc->height;
226         rate = video_enc->time_base.den;
227         rate_base = video_enc->time_base.num;
228     }
229
230     if (!audio_enc) {
231         swf->audio_type = 0;
232         swf->samples_per_frame = (44100. * rate_base) / rate;
233     } else {
234         swf->audio_type = audio_enc->codec_id;
235         swf->samples_per_frame = (audio_enc->sample_rate * rate_base) / rate;
236     }
237
238     put_tag(pb, "FWS");
239
240     if (!strcmp("avm2", s->oformat->name))
241         version = 9;
242     else if (video_enc && video_enc->codec_id == CODEC_ID_VP6F)
243         version = 8; /* version 8 and above support VP6 codec */
244     else if (video_enc && video_enc->codec_id == CODEC_ID_FLV1)
245         version = 6; /* version 6 and above support FLV1 codec */
246     else
247         version = 4; /* version 4 for mpeg audio support */
248     put_byte(pb, version);
249
250     put_le32(pb, DUMMY_FILE_SIZE); /* dummy size
251                                       (will be patched if not streamed) */
252
253     put_swf_rect(pb, 0, width * 20, 0, height * 20);
254     put_le16(pb, (rate * 256) / rate_base); /* frame rate */
255     swf->duration_pos = url_ftell(pb);
256     put_le16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
257
258     /* avm2/swf v9 (also v8?) files require a file attribute tag */
259     if (version == 9) {
260         put_swf_tag(s, TAG_FILEATTRIBUTES);
261         put_le32(pb, 1<<3); /* set ActionScript v3/AVM2 flag */
262         put_swf_end_tag(s);
263     }
264
265     /* define a shape with the jpeg inside */
266     if (video_enc && video_enc->codec_id == CODEC_ID_MJPEG) {
267         put_swf_tag(s, TAG_DEFINESHAPE);
268
269         put_le16(pb, SHAPE_ID); /* ID of shape */
270         /* bounding rectangle */
271         put_swf_rect(pb, 0, width, 0, height);
272         /* style info */
273         put_byte(pb, 1); /* one fill style */
274         put_byte(pb, 0x41); /* clipped bitmap fill */
275         put_le16(pb, BITMAP_ID); /* bitmap ID */
276         /* position of the bitmap */
277         put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
278                        0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
279         put_byte(pb, 0); /* no line style */
280
281         /* shape drawing */
282         init_put_bits(&p, buf1, sizeof(buf1));
283         put_bits(&p, 4, 1); /* one fill bit */
284         put_bits(&p, 4, 0); /* zero line bit */
285
286         put_bits(&p, 1, 0); /* not an edge */
287         put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
288         put_bits(&p, 5, 1); /* nbits */
289         put_bits(&p, 1, 0); /* X */
290         put_bits(&p, 1, 0); /* Y */
291         put_bits(&p, 1, 1); /* set fill style 1 */
292
293         /* draw the rectangle ! */
294         put_swf_line_edge(&p, width, 0);
295         put_swf_line_edge(&p, 0, height);
296         put_swf_line_edge(&p, -width, 0);
297         put_swf_line_edge(&p, 0, -height);
298
299         /* end of shape */
300         put_bits(&p, 1, 0); /* not an edge */
301         put_bits(&p, 5, 0);
302
303         flush_put_bits(&p);
304         put_buffer(pb, buf1, pbBufPtr(&p) - p.buf);
305
306         put_swf_end_tag(s);
307     }
308
309     if (audio_enc && audio_enc->codec_id == CODEC_ID_MP3) {
310         int v;
311
312         /* start sound */
313         put_swf_tag(s, TAG_STREAMHEAD2);
314
315         v = 0;
316         switch(audio_enc->sample_rate) {
317         case 11025:
318             v |= 1 << 2;
319             break;
320         case 22050:
321             v |= 2 << 2;
322             break;
323         case 44100:
324             v |= 3 << 2;
325             break;
326         default:
327             /* not supported */
328             av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n");
329             return -1;
330         }
331         v |= 0x02; /* 16 bit playback */
332         if (audio_enc->channels == 2)
333             v |= 0x01; /* stereo playback */
334         put_byte(s->pb, v);
335         v |= 0x20; /* mp3 compressed */
336         put_byte(s->pb, v);
337         put_le16(s->pb, swf->samples_per_frame);  /* avg samples per frame */
338         put_le16(s->pb, 0);
339
340         put_swf_end_tag(s);
341     }
342
343     put_flush_packet(s->pb);
344     return 0;
345 }
346
347 static int swf_write_video(AVFormatContext *s,
348                            AVCodecContext *enc, const uint8_t *buf, int size)
349 {
350     SWFContext *swf = s->priv_data;
351     ByteIOContext *pb = s->pb;
352
353     /* Flash Player limit */
354     if (swf->swf_frame_number == 16000)
355         av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
356
357     if (swf->video_type == CODEC_ID_VP6F ||
358         swf->video_type == CODEC_ID_FLV1) {
359         if (swf->video_frame_number == 0) {
360             /* create a new video object */
361             put_swf_tag(s, TAG_VIDEOSTREAM);
362             put_le16(pb, VIDEO_ID);
363             put_le16(pb, 15000); /* hard flash player limit */
364             put_le16(pb, enc->width);
365             put_le16(pb, enc->height);
366             put_byte(pb, 0);
367             put_byte(pb,codec_get_tag(swf_codec_tags,swf->video_type));
368             put_swf_end_tag(s);
369
370             /* place the video object for the first time */
371             put_swf_tag(s, TAG_PLACEOBJECT2);
372             put_byte(pb, 0x36);
373             put_le16(pb, 1);
374             put_le16(pb, VIDEO_ID);
375             put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
376             put_le16(pb, swf->video_frame_number);
377             put_tag(pb, "video");
378             put_byte(pb, 0x00);
379             put_swf_end_tag(s);
380         } else {
381             /* mark the character for update */
382             put_swf_tag(s, TAG_PLACEOBJECT2);
383             put_byte(pb, 0x11);
384             put_le16(pb, 1);
385             put_le16(pb, swf->video_frame_number);
386             put_swf_end_tag(s);
387         }
388
389         /* set video frame data */
390         put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG);
391         put_le16(pb, VIDEO_ID);
392         put_le16(pb, swf->video_frame_number++);
393         put_buffer(pb, buf, size);
394         put_swf_end_tag(s);
395     } else if (swf->video_type == CODEC_ID_MJPEG) {
396         if (swf->swf_frame_number > 0) {
397             /* remove the shape */
398             put_swf_tag(s, TAG_REMOVEOBJECT);
399             put_le16(pb, SHAPE_ID); /* shape ID */
400             put_le16(pb, 1); /* depth */
401             put_swf_end_tag(s);
402
403             /* free the bitmap */
404             put_swf_tag(s, TAG_FREECHARACTER);
405             put_le16(pb, BITMAP_ID);
406             put_swf_end_tag(s);
407         }
408
409         put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
410
411         put_le16(pb, BITMAP_ID); /* ID of the image */
412
413         /* a dummy jpeg header seems to be required */
414         put_be32(pb, 0xffd8ffd9);
415         /* write the jpeg image */
416         put_buffer(pb, buf, size);
417
418         put_swf_end_tag(s);
419
420         /* draw the shape */
421
422         put_swf_tag(s, TAG_PLACEOBJECT);
423         put_le16(pb, SHAPE_ID); /* shape ID */
424         put_le16(pb, 1); /* depth */
425         put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0);
426         put_swf_end_tag(s);
427     }
428
429     swf->swf_frame_number ++;
430
431     /* streaming sound always should be placed just before showframe tags */
432     if (swf->audio_type && swf->audio_in_pos) {
433         put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
434         put_le16(pb, swf->sound_samples);
435         put_le16(pb, 0); // seek samples
436         put_buffer(pb, swf->audio_fifo, swf->audio_in_pos);
437         put_swf_end_tag(s);
438
439         /* update FIFO */
440         swf->sound_samples = 0;
441         swf->audio_in_pos = 0;
442     }
443
444     /* output the frame */
445     put_swf_tag(s, TAG_SHOWFRAME);
446     put_swf_end_tag(s);
447
448     put_flush_packet(s->pb);
449
450     return 0;
451 }
452
453 static int swf_write_audio(AVFormatContext *s,
454                            AVCodecContext *enc, const uint8_t *buf, int size)
455 {
456     SWFContext *swf = s->priv_data;
457
458     /* Flash Player limit */
459     if (swf->swf_frame_number == 16000)
460         av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
461
462     if (swf->audio_in_pos + size >= AUDIO_FIFO_SIZE) {
463         av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n");
464         return -1;
465     }
466
467     memcpy(swf->audio_fifo +  swf->audio_in_pos, buf, size);
468     swf->audio_in_pos += size;
469     swf->sound_samples += enc->frame_size;
470
471     /* if audio only stream make sure we add swf frames */
472     if (swf->video_type == 0)
473         swf_write_video(s, enc, 0, 0);
474
475     return 0;
476 }
477
478 static int swf_write_packet(AVFormatContext *s, AVPacket *pkt)
479 {
480     AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
481     if (codec->codec_type == CODEC_TYPE_AUDIO)
482         return swf_write_audio(s, codec, pkt->data, pkt->size);
483     else
484         return swf_write_video(s, codec, pkt->data, pkt->size);
485 }
486
487 static int swf_write_trailer(AVFormatContext *s)
488 {
489     SWFContext *swf = s->priv_data;
490     ByteIOContext *pb = s->pb;
491     AVCodecContext *enc, *video_enc;
492     int file_size, i;
493
494     video_enc = NULL;
495     for(i=0;i<s->nb_streams;i++) {
496         enc = s->streams[i]->codec;
497         if (enc->codec_type == CODEC_TYPE_VIDEO)
498             video_enc = enc;
499     }
500
501     put_swf_tag(s, TAG_END);
502     put_swf_end_tag(s);
503
504     put_flush_packet(s->pb);
505
506     /* patch file size and number of frames if not streamed */
507     if (!url_is_streamed(s->pb) && video_enc) {
508         file_size = url_ftell(pb);
509         url_fseek(pb, 4, SEEK_SET);
510         put_le32(pb, file_size);
511         url_fseek(pb, swf->duration_pos, SEEK_SET);
512         put_le16(pb, video_enc->frame_number);
513         url_fseek(pb, file_size, SEEK_SET);
514     }
515     return 0;
516 }
517
518 #ifdef CONFIG_SWF_MUXER
519 AVOutputFormat swf_muxer = {
520     "swf",
521     "Flash format",
522     "application/x-shockwave-flash",
523     "swf",
524     sizeof(SWFContext),
525     CODEC_ID_MP3,
526     CODEC_ID_FLV1,
527     swf_write_header,
528     swf_write_packet,
529     swf_write_trailer,
530 };
531 #endif
532 #ifdef CONFIG_AVM2_MUXER
533 AVOutputFormat avm2_muxer = {
534     "avm2",
535     "Flash 9 (AVM2) format",
536     "application/x-shockwave-flash",
537     NULL,
538     sizeof(SWFContext),
539     CODEC_ID_MP3,
540     CODEC_ID_FLV1,
541     swf_write_header,
542     swf_write_packet,
543     swf_write_trailer,
544 };
545 #endif