00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "libavcodec/put_bits.h"
00024 #include "libavutil/avassert.h"
00025 #include "avformat.h"
00026 #include "swf.h"
00027
00028 static void put_swf_tag(AVFormatContext *s, int tag)
00029 {
00030 SWFContext *swf = s->priv_data;
00031 AVIOContext *pb = s->pb;
00032
00033 swf->tag_pos = avio_tell(pb);
00034 swf->tag = tag;
00035
00036 if (tag & TAG_LONG) {
00037 avio_wl16(pb, 0);
00038 avio_wl32(pb, 0);
00039 } else {
00040 avio_wl16(pb, 0);
00041 }
00042 }
00043
00044 static void put_swf_end_tag(AVFormatContext *s)
00045 {
00046 SWFContext *swf = s->priv_data;
00047 AVIOContext *pb = s->pb;
00048 int64_t pos;
00049 int tag_len, tag;
00050
00051 pos = avio_tell(pb);
00052 tag_len = pos - swf->tag_pos - 2;
00053 tag = swf->tag;
00054 avio_seek(pb, swf->tag_pos, SEEK_SET);
00055 if (tag & TAG_LONG) {
00056 tag &= ~TAG_LONG;
00057 avio_wl16(pb, (tag << 6) | 0x3f);
00058 avio_wl32(pb, tag_len - 4);
00059 } else {
00060 av_assert0(tag_len < 0x3f);
00061 avio_wl16(pb, (tag << 6) | tag_len);
00062 }
00063 avio_seek(pb, pos, SEEK_SET);
00064 }
00065
00066 static inline void max_nbits(int *nbits_ptr, int val)
00067 {
00068 int n;
00069
00070 if (val == 0)
00071 return;
00072 val = abs(val);
00073 n = 1;
00074 while (val != 0) {
00075 n++;
00076 val >>= 1;
00077 }
00078 if (n > *nbits_ptr)
00079 *nbits_ptr = n;
00080 }
00081
00082 static void put_swf_rect(AVIOContext *pb,
00083 int xmin, int xmax, int ymin, int ymax)
00084 {
00085 PutBitContext p;
00086 uint8_t buf[256];
00087 int nbits, mask;
00088
00089 init_put_bits(&p, buf, sizeof(buf));
00090
00091 nbits = 0;
00092 max_nbits(&nbits, xmin);
00093 max_nbits(&nbits, xmax);
00094 max_nbits(&nbits, ymin);
00095 max_nbits(&nbits, ymax);
00096 mask = (1 << nbits) - 1;
00097
00098
00099 put_bits(&p, 5, nbits);
00100 put_bits(&p, nbits, xmin & mask);
00101 put_bits(&p, nbits, xmax & mask);
00102 put_bits(&p, nbits, ymin & mask);
00103 put_bits(&p, nbits, ymax & mask);
00104
00105 flush_put_bits(&p);
00106 avio_write(pb, buf, put_bits_ptr(&p) - p.buf);
00107 }
00108
00109 static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
00110 {
00111 int nbits, mask;
00112
00113 put_bits(pb, 1, 1);
00114 put_bits(pb, 1, 1);
00115 nbits = 2;
00116 max_nbits(&nbits, dx);
00117 max_nbits(&nbits, dy);
00118
00119 mask = (1 << nbits) - 1;
00120 put_bits(pb, 4, nbits - 2);
00121 if (dx == 0) {
00122 put_bits(pb, 1, 0);
00123 put_bits(pb, 1, 1);
00124 put_bits(pb, nbits, dy & mask);
00125 } else if (dy == 0) {
00126 put_bits(pb, 1, 0);
00127 put_bits(pb, 1, 0);
00128 put_bits(pb, nbits, dx & mask);
00129 } else {
00130 put_bits(pb, 1, 1);
00131 put_bits(pb, nbits, dx & mask);
00132 put_bits(pb, nbits, dy & mask);
00133 }
00134 }
00135
00136 #define FRAC_BITS 16
00137
00138 static void put_swf_matrix(AVIOContext *pb,
00139 int a, int b, int c, int d, int tx, int ty)
00140 {
00141 PutBitContext p;
00142 uint8_t buf[256];
00143 int nbits;
00144
00145 init_put_bits(&p, buf, sizeof(buf));
00146
00147 put_bits(&p, 1, 1);
00148 nbits = 1;
00149 max_nbits(&nbits, a);
00150 max_nbits(&nbits, d);
00151 put_bits(&p, 5, nbits);
00152 put_bits(&p, nbits, a);
00153 put_bits(&p, nbits, d);
00154
00155 put_bits(&p, 1, 1);
00156 nbits = 1;
00157 max_nbits(&nbits, c);
00158 max_nbits(&nbits, b);
00159 put_bits(&p, 5, nbits);
00160 put_bits(&p, nbits, c);
00161 put_bits(&p, nbits, b);
00162
00163 nbits = 1;
00164 max_nbits(&nbits, tx);
00165 max_nbits(&nbits, ty);
00166 put_bits(&p, 5, nbits);
00167 put_bits(&p, nbits, tx);
00168 put_bits(&p, nbits, ty);
00169
00170 flush_put_bits(&p);
00171 avio_write(pb, buf, put_bits_ptr(&p) - p.buf);
00172 }
00173
00174 static int swf_write_header(AVFormatContext *s)
00175 {
00176 SWFContext *swf = s->priv_data;
00177 AVIOContext *pb = s->pb;
00178 PutBitContext p;
00179 uint8_t buf1[256];
00180 int i, width, height, rate, rate_base;
00181 int version;
00182
00183 swf->sound_samples = 0;
00184 swf->swf_frame_number = 0;
00185 swf->video_frame_number = 0;
00186
00187 for(i=0;i<s->nb_streams;i++) {
00188 AVCodecContext *enc = s->streams[i]->codec;
00189 if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00190 if (enc->codec_id == AV_CODEC_ID_MP3) {
00191 swf->audio_enc = enc;
00192 swf->audio_fifo= av_fifo_alloc(AUDIO_FIFO_SIZE);
00193 if (!swf->audio_fifo)
00194 return AVERROR(ENOMEM);
00195 } else {
00196 av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n");
00197 return -1;
00198 }
00199 } else {
00200 if (enc->codec_id == AV_CODEC_ID_VP6F ||
00201 enc->codec_id == AV_CODEC_ID_FLV1 ||
00202 enc->codec_id == AV_CODEC_ID_MJPEG) {
00203 swf->video_enc = enc;
00204 } else {
00205 av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n");
00206 return -1;
00207 }
00208 }
00209 }
00210
00211 if (!swf->video_enc) {
00212
00213 width = 320;
00214 height = 200;
00215 rate = 10;
00216 rate_base= 1;
00217 } else {
00218 width = swf->video_enc->width;
00219 height = swf->video_enc->height;
00220 rate = swf->video_enc->time_base.den;
00221 rate_base = swf->video_enc->time_base.num;
00222 }
00223
00224 if (!swf->audio_enc)
00225 swf->samples_per_frame = (44100. * rate_base) / rate;
00226 else
00227 swf->samples_per_frame = (swf->audio_enc->sample_rate * rate_base) / rate;
00228
00229 avio_write(pb, "FWS", 3);
00230
00231 if (!strcmp("avm2", s->oformat->name))
00232 version = 9;
00233 else if (swf->video_enc && swf->video_enc->codec_id == AV_CODEC_ID_VP6F)
00234 version = 8;
00235 else if (swf->video_enc && swf->video_enc->codec_id == AV_CODEC_ID_FLV1)
00236 version = 6;
00237 else
00238 version = 4;
00239 avio_w8(pb, version);
00240
00241 avio_wl32(pb, DUMMY_FILE_SIZE);
00242
00243
00244 put_swf_rect(pb, 0, width * 20, 0, height * 20);
00245 avio_wl16(pb, (rate * 256) / rate_base);
00246 swf->duration_pos = avio_tell(pb);
00247 avio_wl16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base));
00248
00249
00250 if (version == 9) {
00251 put_swf_tag(s, TAG_FILEATTRIBUTES);
00252 avio_wl32(pb, 1<<3);
00253 put_swf_end_tag(s);
00254 }
00255
00256
00257 if (swf->video_enc && swf->video_enc->codec_id == AV_CODEC_ID_MJPEG) {
00258 put_swf_tag(s, TAG_DEFINESHAPE);
00259
00260 avio_wl16(pb, SHAPE_ID);
00261
00262 put_swf_rect(pb, 0, width, 0, height);
00263
00264 avio_w8(pb, 1);
00265 avio_w8(pb, 0x41);
00266 avio_wl16(pb, BITMAP_ID);
00267
00268 put_swf_matrix(pb, (int)(1.0 * (1 << FRAC_BITS)), 0,
00269 0, (int)(1.0 * (1 << FRAC_BITS)), 0, 0);
00270 avio_w8(pb, 0);
00271
00272
00273 init_put_bits(&p, buf1, sizeof(buf1));
00274 put_bits(&p, 4, 1);
00275 put_bits(&p, 4, 0);
00276
00277 put_bits(&p, 1, 0);
00278 put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
00279 put_bits(&p, 5, 1);
00280 put_bits(&p, 1, 0);
00281 put_bits(&p, 1, 0);
00282 put_bits(&p, 1, 1);
00283
00284
00285 put_swf_line_edge(&p, width, 0);
00286 put_swf_line_edge(&p, 0, height);
00287 put_swf_line_edge(&p, -width, 0);
00288 put_swf_line_edge(&p, 0, -height);
00289
00290
00291 put_bits(&p, 1, 0);
00292 put_bits(&p, 5, 0);
00293
00294 flush_put_bits(&p);
00295 avio_write(pb, buf1, put_bits_ptr(&p) - p.buf);
00296
00297 put_swf_end_tag(s);
00298 }
00299
00300 if (swf->audio_enc && swf->audio_enc->codec_id == AV_CODEC_ID_MP3) {
00301 int v = 0;
00302
00303
00304 put_swf_tag(s, TAG_STREAMHEAD2);
00305 switch(swf->audio_enc->sample_rate) {
00306 case 11025: v |= 1 << 2; break;
00307 case 22050: v |= 2 << 2; break;
00308 case 44100: v |= 3 << 2; break;
00309 default:
00310
00311 av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n");
00312 return -1;
00313 }
00314 v |= 0x02;
00315 if (swf->audio_enc->channels == 2)
00316 v |= 0x01;
00317 avio_w8(s->pb, v);
00318 v |= 0x20;
00319 avio_w8(s->pb, v);
00320 avio_wl16(s->pb, swf->samples_per_frame);
00321 avio_wl16(s->pb, 0);
00322
00323 put_swf_end_tag(s);
00324 }
00325
00326 avio_flush(s->pb);
00327 return 0;
00328 }
00329
00330 static int swf_write_video(AVFormatContext *s,
00331 AVCodecContext *enc, const uint8_t *buf, int size)
00332 {
00333 SWFContext *swf = s->priv_data;
00334 AVIOContext *pb = s->pb;
00335
00336
00337 if (swf->swf_frame_number == 16000)
00338 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
00339
00340 if (enc->codec_id == AV_CODEC_ID_VP6F ||
00341 enc->codec_id == AV_CODEC_ID_FLV1) {
00342 if (swf->video_frame_number == 0) {
00343
00344 put_swf_tag(s, TAG_VIDEOSTREAM);
00345 avio_wl16(pb, VIDEO_ID);
00346 swf->vframes_pos = avio_tell(pb);
00347 avio_wl16(pb, 15000);
00348 avio_wl16(pb, enc->width);
00349 avio_wl16(pb, enc->height);
00350 avio_w8(pb, 0);
00351 avio_w8(pb,ff_codec_get_tag(ff_swf_codec_tags, enc->codec_id));
00352 put_swf_end_tag(s);
00353
00354
00355 put_swf_tag(s, TAG_PLACEOBJECT2);
00356 avio_w8(pb, 0x36);
00357 avio_wl16(pb, 1);
00358 avio_wl16(pb, VIDEO_ID);
00359 put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
00360 avio_wl16(pb, swf->video_frame_number);
00361 avio_write(pb, "video", 5);
00362 avio_w8(pb, 0x00);
00363 put_swf_end_tag(s);
00364 } else {
00365
00366 put_swf_tag(s, TAG_PLACEOBJECT2);
00367 avio_w8(pb, 0x11);
00368 avio_wl16(pb, 1);
00369 avio_wl16(pb, swf->video_frame_number);
00370 put_swf_end_tag(s);
00371 }
00372
00373
00374 put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG);
00375 avio_wl16(pb, VIDEO_ID);
00376 avio_wl16(pb, swf->video_frame_number++);
00377 avio_write(pb, buf, size);
00378 put_swf_end_tag(s);
00379 } else if (enc->codec_id == AV_CODEC_ID_MJPEG) {
00380 if (swf->swf_frame_number > 0) {
00381
00382 put_swf_tag(s, TAG_REMOVEOBJECT);
00383 avio_wl16(pb, SHAPE_ID);
00384 avio_wl16(pb, 1);
00385 put_swf_end_tag(s);
00386
00387
00388 put_swf_tag(s, TAG_FREECHARACTER);
00389 avio_wl16(pb, BITMAP_ID);
00390 put_swf_end_tag(s);
00391 }
00392
00393 put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
00394
00395 avio_wl16(pb, BITMAP_ID);
00396
00397
00398 avio_wb32(pb, 0xffd8ffd9);
00399
00400 avio_write(pb, buf, size);
00401
00402 put_swf_end_tag(s);
00403
00404
00405
00406 put_swf_tag(s, TAG_PLACEOBJECT);
00407 avio_wl16(pb, SHAPE_ID);
00408 avio_wl16(pb, 1);
00409 put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0);
00410 put_swf_end_tag(s);
00411 }
00412
00413 swf->swf_frame_number++;
00414
00415
00416 if (swf->audio_enc && av_fifo_size(swf->audio_fifo)) {
00417 int frame_size = av_fifo_size(swf->audio_fifo);
00418 put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
00419 avio_wl16(pb, swf->sound_samples);
00420 avio_wl16(pb, 0);
00421 av_fifo_generic_read(swf->audio_fifo, pb, frame_size, (void*)avio_write);
00422 put_swf_end_tag(s);
00423
00424
00425 swf->sound_samples = 0;
00426 }
00427
00428
00429 put_swf_tag(s, TAG_SHOWFRAME);
00430 put_swf_end_tag(s);
00431
00432 avio_flush(s->pb);
00433
00434 return 0;
00435 }
00436
00437 static int swf_write_audio(AVFormatContext *s,
00438 AVCodecContext *enc, uint8_t *buf, int size)
00439 {
00440 SWFContext *swf = s->priv_data;
00441
00442
00443 if (swf->swf_frame_number == 16000)
00444 av_log(enc, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
00445
00446 if (av_fifo_size(swf->audio_fifo) + size > AUDIO_FIFO_SIZE) {
00447 av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n");
00448 return -1;
00449 }
00450
00451 av_fifo_generic_write(swf->audio_fifo, buf, size, NULL);
00452 swf->sound_samples += av_get_audio_frame_duration(enc, size);
00453
00454
00455 if (!swf->video_enc)
00456 swf_write_video(s, enc, 0, 0);
00457
00458 return 0;
00459 }
00460
00461 static int swf_write_packet(AVFormatContext *s, AVPacket *pkt)
00462 {
00463 AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
00464 if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
00465 return swf_write_audio(s, codec, pkt->data, pkt->size);
00466 else
00467 return swf_write_video(s, codec, pkt->data, pkt->size);
00468 }
00469
00470 static int swf_write_trailer(AVFormatContext *s)
00471 {
00472 SWFContext *swf = s->priv_data;
00473 AVIOContext *pb = s->pb;
00474 AVCodecContext *enc, *video_enc;
00475 int file_size, i;
00476
00477 video_enc = NULL;
00478 for(i=0;i<s->nb_streams;i++) {
00479 enc = s->streams[i]->codec;
00480 if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
00481 video_enc = enc;
00482 else
00483 av_fifo_free(swf->audio_fifo);
00484 }
00485
00486 put_swf_tag(s, TAG_END);
00487 put_swf_end_tag(s);
00488
00489
00490 if (s->pb->seekable && video_enc) {
00491 file_size = avio_tell(pb);
00492 avio_seek(pb, 4, SEEK_SET);
00493 avio_wl32(pb, file_size);
00494 avio_seek(pb, swf->duration_pos, SEEK_SET);
00495 avio_wl16(pb, swf->video_frame_number);
00496 if (swf->vframes_pos) {
00497 avio_seek(pb, swf->vframes_pos, SEEK_SET);
00498 avio_wl16(pb, swf->video_frame_number);
00499 }
00500 avio_seek(pb, file_size, SEEK_SET);
00501 }
00502 return 0;
00503 }
00504
00505 #if CONFIG_SWF_MUXER
00506 AVOutputFormat ff_swf_muxer = {
00507 .name = "swf",
00508 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
00509 .mime_type = "application/x-shockwave-flash",
00510 .extensions = "swf",
00511 .priv_data_size = sizeof(SWFContext),
00512 .audio_codec = AV_CODEC_ID_MP3,
00513 .video_codec = AV_CODEC_ID_FLV1,
00514 .write_header = swf_write_header,
00515 .write_packet = swf_write_packet,
00516 .write_trailer = swf_write_trailer,
00517 .flags = AVFMT_TS_NONSTRICT,
00518 };
00519 #endif
00520 #if CONFIG_AVM2_MUXER
00521 AVOutputFormat ff_avm2_muxer = {
00522 .name = "avm2",
00523 .long_name = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash) (AVM2)"),
00524 .mime_type = "application/x-shockwave-flash",
00525 .priv_data_size = sizeof(SWFContext),
00526 .audio_codec = AV_CODEC_ID_MP3,
00527 .video_codec = AV_CODEC_ID_FLV1,
00528 .write_header = swf_write_header,
00529 .write_packet = swf_write_packet,
00530 .write_trailer = swf_write_trailer,
00531 .flags = AVFMT_TS_NONSTRICT,
00532 };
00533 #endif