swfenc.c 16.6 KB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
1
/*
2
 * Flash Compatible Streaming Format muxer
3 4
 * Copyright (c) 2000 Fabrice Bellard
 * Copyright (c) 2003 Tinic Uro
Fabrice Bellard's avatar
Fabrice Bellard committed
5
 *
6 7 8
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
9 10
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
Fabrice Bellard's avatar
Fabrice Bellard committed
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
Fabrice Bellard's avatar
Fabrice Bellard committed
21
 */
22

23
#include "libavcodec/put_bits.h"
24
#include "libavutil/avassert.h"
Fabrice Bellard's avatar
Fabrice Bellard committed
25
#include "avformat.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
26
#include "swf.h"
27

Fabrice Bellard's avatar
Fabrice Bellard committed
28 29 30
static void put_swf_tag(AVFormatContext *s, int tag)
{
    SWFContext *swf = s->priv_data;
31
    AVIOContext *pb = s->pb;
Fabrice Bellard's avatar
Fabrice Bellard committed
32

33
    swf->tag_pos = avio_tell(pb);
Fabrice Bellard's avatar
Fabrice Bellard committed
34 35 36
    swf->tag = tag;
    /* reserve some room for the tag */
    if (tag & TAG_LONG) {
37 38
        avio_wl16(pb, 0);
        avio_wl32(pb, 0);
Fabrice Bellard's avatar
Fabrice Bellard committed
39
    } else {
40
        avio_wl16(pb, 0);
Fabrice Bellard's avatar
Fabrice Bellard committed
41 42 43 44 45 46
    }
}

static void put_swf_end_tag(AVFormatContext *s)
{
    SWFContext *swf = s->priv_data;
47
    AVIOContext *pb = s->pb;
48
    int64_t pos;
Fabrice Bellard's avatar
Fabrice Bellard committed
49 50
    int tag_len, tag;

51
    pos = avio_tell(pb);
Fabrice Bellard's avatar
Fabrice Bellard committed
52 53
    tag_len = pos - swf->tag_pos - 2;
    tag = swf->tag;
54
    avio_seek(pb, swf->tag_pos, SEEK_SET);
Fabrice Bellard's avatar
Fabrice Bellard committed
55 56
    if (tag & TAG_LONG) {
        tag &= ~TAG_LONG;
57 58
        avio_wl16(pb, (tag << 6) | 0x3f);
        avio_wl32(pb, tag_len - 4);
Fabrice Bellard's avatar
Fabrice Bellard committed
59
    } else {
60
        av_assert0(tag_len < 0x3f);
61
        avio_wl16(pb, (tag << 6) | tag_len);
Fabrice Bellard's avatar
Fabrice Bellard committed
62
    }
63
    avio_seek(pb, pos, SEEK_SET);
Fabrice Bellard's avatar
Fabrice Bellard committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
}

static inline void max_nbits(int *nbits_ptr, int val)
{
    int n;

    if (val == 0)
        return;
    val = abs(val);
    n = 1;
    while (val != 0) {
        n++;
        val >>= 1;
    }
    if (n > *nbits_ptr)
        *nbits_ptr = n;
}

82
static void put_swf_rect(AVIOContext *pb,
Fabrice Bellard's avatar
Fabrice Bellard committed
83 84 85
                         int xmin, int xmax, int ymin, int ymax)
{
    PutBitContext p;
86
    uint8_t buf[256];
Fabrice Bellard's avatar
Fabrice Bellard committed
87 88
    int nbits, mask;

Alex Beregszaszi's avatar
Alex Beregszaszi committed
89
    init_put_bits(&p, buf, sizeof(buf));
90

Fabrice Bellard's avatar
Fabrice Bellard committed
91 92 93 94 95 96 97 98 99 100 101 102 103
    nbits = 0;
    max_nbits(&nbits, xmin);
    max_nbits(&nbits, xmax);
    max_nbits(&nbits, ymin);
    max_nbits(&nbits, ymax);
    mask = (1 << nbits) - 1;

    /* rectangle info */
    put_bits(&p, 5, nbits);
    put_bits(&p, nbits, xmin & mask);
    put_bits(&p, nbits, xmax & mask);
    put_bits(&p, nbits, ymin & mask);
    put_bits(&p, nbits, ymax & mask);
104

Fabrice Bellard's avatar
Fabrice Bellard committed
105
    flush_put_bits(&p);
106
    avio_write(pb, buf, put_bits_ptr(&p) - p.buf);
Fabrice Bellard's avatar
Fabrice Bellard committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

static void put_swf_line_edge(PutBitContext *pb, int dx, int dy)
{
    int nbits, mask;

    put_bits(pb, 1, 1); /* edge */
    put_bits(pb, 1, 1); /* line select */
    nbits = 2;
    max_nbits(&nbits, dx);
    max_nbits(&nbits, dy);

    mask = (1 << nbits) - 1;
    put_bits(pb, 4, nbits - 2); /* 16 bits precision */
    if (dx == 0) {
122 123 124
        put_bits(pb, 1, 0);
        put_bits(pb, 1, 1);
        put_bits(pb, nbits, dy & mask);
Fabrice Bellard's avatar
Fabrice Bellard committed
125
    } else if (dy == 0) {
126 127 128
        put_bits(pb, 1, 0);
        put_bits(pb, 1, 0);
        put_bits(pb, nbits, dx & mask);
Fabrice Bellard's avatar
Fabrice Bellard committed
129
    } else {
130 131 132
        put_bits(pb, 1, 1);
        put_bits(pb, nbits, dx & mask);
        put_bits(pb, nbits, dy & mask);
Fabrice Bellard's avatar
Fabrice Bellard committed
133 134 135 136 137
    }
}

#define FRAC_BITS 16

138
static void put_swf_matrix(AVIOContext *pb,
Fabrice Bellard's avatar
Fabrice Bellard committed
139 140 141
                           int a, int b, int c, int d, int tx, int ty)
{
    PutBitContext p;
142
    uint8_t buf[256];
143
    int nbits;
Fabrice Bellard's avatar
Fabrice Bellard committed
144

Alex Beregszaszi's avatar
Alex Beregszaszi committed
145
    init_put_bits(&p, buf, sizeof(buf));
146

Fabrice Bellard's avatar
Fabrice Bellard committed
147
    put_bits(&p, 1, 1); /* a, d present */
148 149 150 151 152 153
    nbits = 1;
    max_nbits(&nbits, a);
    max_nbits(&nbits, d);
    put_bits(&p, 5, nbits); /* nb bits */
    put_bits(&p, nbits, a);
    put_bits(&p, nbits, d);
154

Fabrice Bellard's avatar
Fabrice Bellard committed
155
    put_bits(&p, 1, 1); /* b, c present */
156 157 158 159 160 161 162 163 164 165 166 167 168
    nbits = 1;
    max_nbits(&nbits, c);
    max_nbits(&nbits, b);
    put_bits(&p, 5, nbits); /* nb bits */
    put_bits(&p, nbits, c);
    put_bits(&p, nbits, b);

    nbits = 1;
    max_nbits(&nbits, tx);
    max_nbits(&nbits, ty);
    put_bits(&p, 5, nbits); /* nb bits */
    put_bits(&p, nbits, tx);
    put_bits(&p, nbits, ty);
Fabrice Bellard's avatar
Fabrice Bellard committed
169 170

    flush_put_bits(&p);
171
    avio_write(pb, buf, put_bits_ptr(&p) - p.buf);
Fabrice Bellard's avatar
Fabrice Bellard committed
172 173 174 175
}

static int swf_write_header(AVFormatContext *s)
{
176
    SWFContext *swf = s->priv_data;
177
    AVIOContext *pb = s->pb;
Fabrice Bellard's avatar
Fabrice Bellard committed
178
    PutBitContext p;
179
    uint8_t buf1[256];
180
    int i, width, height, rate, rate_base;
181
    int version;
Fabrice Bellard's avatar
Fabrice Bellard committed
182

183 184 185 186
    swf->sound_samples = 0;
    swf->swf_frame_number = 0;
    swf->video_frame_number = 0;

Fabrice Bellard's avatar
Fabrice Bellard committed
187
    for(i=0;i<s->nb_streams;i++) {
188 189 190
        AVCodecParameters *par = s->streams[i]->codecpar;
        if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
            if (swf->audio_par) {
191 192 193
                av_log(s, AV_LOG_ERROR, "SWF muxer only supports 1 audio stream\n");
                return AVERROR_INVALIDDATA;
            }
194 195
            if (par->codec_id == AV_CODEC_ID_MP3) {
                swf->audio_par = par;
196
                swf->audio_fifo= av_fifo_alloc(AUDIO_FIFO_SIZE);
197 198
                if (!swf->audio_fifo)
                    return AVERROR(ENOMEM);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
199
            } else {
200
                av_log(s, AV_LOG_ERROR, "SWF muxer only supports MP3\n");
Baptiste Coudurier's avatar
Baptiste Coudurier committed
201 202 203
                return -1;
            }
        } else {
204
            if (swf->video_par) {
205 206 207
                av_log(s, AV_LOG_ERROR, "SWF muxer only supports 1 video stream\n");
                return AVERROR_INVALIDDATA;
            }
208 209 210
            if (par->codec_id == AV_CODEC_ID_VP6F ||
                par->codec_id == AV_CODEC_ID_FLV1 ||
                par->codec_id == AV_CODEC_ID_MJPEG) {
211
                swf->video_st  = s->streams[i];
212
                swf->video_par = par;
213
            } else {
214
                av_log(s, AV_LOG_ERROR, "SWF muxer only supports VP6, FLV1 and MJPEG\n");
215 216 217
                return -1;
            }
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
218 219
    }

220
    if (!swf->video_par) {
221
        /* currently, cannot work correctly if audio only */
Fabrice Bellard's avatar
Fabrice Bellard committed
222 223
        width = 320;
        height = 200;
224 225
        rate = 10;
        rate_base= 1;
Fabrice Bellard's avatar
Fabrice Bellard committed
226
    } else {
227 228
        width = swf->video_par->width;
        height = swf->video_par->height;
229 230 231
        // TODO: should be avg_frame_rate
        rate = swf->video_st->time_base.den;
        rate_base = swf->video_st->time_base.num;
Fabrice Bellard's avatar
Fabrice Bellard committed
232 233
    }

234
    if (!swf->audio_par)
235
        swf->samples_per_frame = (44100LL * rate_base) / rate;
236
    else
237
        swf->samples_per_frame = (swf->audio_par->sample_rate * rate_base) / rate;
238

239
    avio_write(pb, "FWS", 3);
240 241 242

    if (!strcmp("avm2", s->oformat->name))
        version = 9;
243
    else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_VP6F)
244
        version = 8; /* version 8 and above support VP6 codec */
245
    else if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_FLV1)
246
        version = 6; /* version 6 and above support FLV1 codec */
247
    else
248
        version = 4; /* version 4 for mpeg audio support */
249
    avio_w8(pb, version);
250

251
    avio_wl32(pb, DUMMY_FILE_SIZE); /* dummy size
252
                                      (will be patched if not streamed) */
Fabrice Bellard's avatar
Fabrice Bellard committed
253

254
    put_swf_rect(pb, 0, width * 20, 0, height * 20);
255 256 257 258
    if ((rate * 256LL) / rate_base >= (1<<16)) {
        av_log(s, AV_LOG_ERROR, "Invalid (too large) frame rate %d/%d\n", rate, rate_base);
        return AVERROR(EINVAL);
    }
259
    avio_wl16(pb, (rate * 256) / rate_base); /* frame rate */
260
    swf->duration_pos = avio_tell(pb);
261
    avio_wl16(pb, (uint16_t)(DUMMY_DURATION * (int64_t)rate / rate_base)); /* frame count */
262

263
    /* avm2/swf v9 (also v8?) files require a file attribute tag */
264
    if (version == 9) {
265
        put_swf_tag(s, TAG_FILEATTRIBUTES);
266
        avio_wl32(pb, 1<<3); /* set ActionScript v3/AVM2 flag */
267 268 269
        put_swf_end_tag(s);
    }

Fabrice Bellard's avatar
Fabrice Bellard committed
270
    /* define a shape with the jpeg inside */
271
    if (swf->video_par && swf->video_par->codec_id == AV_CODEC_ID_MJPEG) {
272 273
        put_swf_tag(s, TAG_DEFINESHAPE);

274
        avio_wl16(pb, SHAPE_ID); /* ID of shape */
275 276 277
        /* bounding rectangle */
        put_swf_rect(pb, 0, width, 0, height);
        /* style info */
278 279 280
        avio_w8(pb, 1); /* one fill style */
        avio_w8(pb, 0x41); /* clipped bitmap fill */
        avio_wl16(pb, BITMAP_ID); /* bitmap ID */
281
        /* position of the bitmap */
282 283
        put_swf_matrix(pb, 1 << FRAC_BITS, 0,
                       0,  1 << FRAC_BITS, 0, 0);
284
        avio_w8(pb, 0); /* no line style */
285

286 287 288 289
        /* shape drawing */
        init_put_bits(&p, buf1, sizeof(buf1));
        put_bits(&p, 4, 1); /* one fill bit */
        put_bits(&p, 4, 0); /* zero line bit */
290

291 292 293 294 295 296
        put_bits(&p, 1, 0); /* not an edge */
        put_bits(&p, 5, FLAG_MOVETO | FLAG_SETFILL0);
        put_bits(&p, 5, 1); /* nbits */
        put_bits(&p, 1, 0); /* X */
        put_bits(&p, 1, 0); /* Y */
        put_bits(&p, 1, 1); /* set fill style 1 */
297

298 299 300 301 302
        /* draw the rectangle ! */
        put_swf_line_edge(&p, width, 0);
        put_swf_line_edge(&p, 0, height);
        put_swf_line_edge(&p, -width, 0);
        put_swf_line_edge(&p, 0, -height);
303

304 305 306
        /* end of shape */
        put_bits(&p, 1, 0); /* not an edge */
        put_bits(&p, 5, 0);
Fabrice Bellard's avatar
Fabrice Bellard committed
307

308
        flush_put_bits(&p);
309
        avio_write(pb, buf1, put_bits_ptr(&p) - p.buf);
Fabrice Bellard's avatar
Fabrice Bellard committed
310

311 312
        put_swf_end_tag(s);
    }
313

314
    if (swf->audio_par && swf->audio_par->codec_id == AV_CODEC_ID_MP3) {
315
        int v = 0;
Fabrice Bellard's avatar
Fabrice Bellard committed
316 317

        /* start sound */
318
        put_swf_tag(s, TAG_STREAMHEAD2);
319
        switch(swf->audio_par->sample_rate) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
320 321 322
        case 11025: v |= 1 << 2; break;
        case 22050: v |= 2 << 2; break;
        case 44100: v |= 3 << 2; break;
Fabrice Bellard's avatar
Fabrice Bellard committed
323 324
        default:
            /* not supported */
Diego Biurrun's avatar
Diego Biurrun committed
325
            av_log(s, AV_LOG_ERROR, "swf does not support that sample rate, choose from (44100, 22050, 11025).\n");
Fabrice Bellard's avatar
Fabrice Bellard committed
326 327
            return -1;
        }
328
        v |= 0x02; /* 16 bit playback */
329
        if (swf->audio_par->channels == 2)
330
            v |= 0x01; /* stereo playback */
331
        avio_w8(s->pb, v);
Fabrice Bellard's avatar
Fabrice Bellard committed
332
        v |= 0x20; /* mp3 compressed */
333 334 335
        avio_w8(s->pb, v);
        avio_wl16(s->pb, swf->samples_per_frame);  /* avg samples per frame */
        avio_wl16(s->pb, 0);
336

Fabrice Bellard's avatar
Fabrice Bellard committed
337 338 339
        put_swf_end_tag(s);
    }

340
    avio_flush(s->pb);
Fabrice Bellard's avatar
Fabrice Bellard committed
341 342 343
    return 0;
}

344
static int swf_write_video(AVFormatContext *s,
345
                           AVCodecParameters *par, const uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
346
{
347
    SWFContext *swf = s->priv_data;
348
    AVIOContext *pb = s->pb;
349

350
    /* Flash Player limit */
351
    if (swf->swf_frame_number == 16000)
352
        av_log(s, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
Fabrice Bellard's avatar
Fabrice Bellard committed
353

354 355
    if (par->codec_id == AV_CODEC_ID_VP6F ||
        par->codec_id == AV_CODEC_ID_FLV1) {
356
        if (swf->video_frame_number == 0) {
357 358
            /* create a new video object */
            put_swf_tag(s, TAG_VIDEOSTREAM);
359
            avio_wl16(pb, VIDEO_ID);
360
            swf->vframes_pos = avio_tell(pb);
361
            avio_wl16(pb, 15000); /* hard flash player limit */
362 363
            avio_wl16(pb, par->width);
            avio_wl16(pb, par->height);
364
            avio_w8(pb, 0);
365
            avio_w8(pb,ff_codec_get_tag(ff_swf_codec_tags, par->codec_id));
366 367 368 369
            put_swf_end_tag(s);

            /* place the video object for the first time */
            put_swf_tag(s, TAG_PLACEOBJECT2);
370 371 372
            avio_w8(pb, 0x36);
            avio_wl16(pb, 1);
            avio_wl16(pb, VIDEO_ID);
373
            put_swf_matrix(pb, 1 << FRAC_BITS, 0, 0, 1 << FRAC_BITS, 0, 0);
374
            avio_wl16(pb, swf->video_frame_number);
375
            avio_write(pb, "video", 5);
376
            avio_w8(pb, 0x00);
377 378 379 380
            put_swf_end_tag(s);
        } else {
            /* mark the character for update */
            put_swf_tag(s, TAG_PLACEOBJECT2);
381 382 383
            avio_w8(pb, 0x11);
            avio_wl16(pb, 1);
            avio_wl16(pb, swf->video_frame_number);
384 385
            put_swf_end_tag(s);
        }
386

387 388
        /* set video frame data */
        put_swf_tag(s, TAG_VIDEOFRAME | TAG_LONG);
389 390 391
        avio_wl16(pb, VIDEO_ID);
        avio_wl16(pb, swf->video_frame_number++);
        avio_write(pb, buf, size);
392
        put_swf_end_tag(s);
393
    } else if (par->codec_id == AV_CODEC_ID_MJPEG) {
394 395 396
        if (swf->swf_frame_number > 0) {
            /* remove the shape */
            put_swf_tag(s, TAG_REMOVEOBJECT);
397 398
            avio_wl16(pb, SHAPE_ID); /* shape ID */
            avio_wl16(pb, 1); /* depth */
399 400 401 402
            put_swf_end_tag(s);

            /* free the bitmap */
            put_swf_tag(s, TAG_FREECHARACTER);
403
            avio_wl16(pb, BITMAP_ID);
404 405
            put_swf_end_tag(s);
        }
406

407
        put_swf_tag(s, TAG_JPEG2 | TAG_LONG);
408

409
        avio_wl16(pb, BITMAP_ID); /* ID of the image */
410

411
        /* a dummy jpeg header seems to be required */
412
        avio_wb32(pb, 0xffd8ffd9);
413
        /* write the jpeg image */
414
        avio_write(pb, buf, size);
415

416
        put_swf_end_tag(s);
417

418
        /* draw the shape */
419

420
        put_swf_tag(s, TAG_PLACEOBJECT);
421 422
        avio_wl16(pb, SHAPE_ID); /* shape ID */
        avio_wl16(pb, 1); /* depth */
423 424 425
        put_swf_matrix(pb, 20 << FRAC_BITS, 0, 0, 20 << FRAC_BITS, 0, 0);
        put_swf_end_tag(s);
    }
426

Baptiste Coudurier's avatar
Baptiste Coudurier committed
427
    swf->swf_frame_number++;
Fabrice Bellard's avatar
Fabrice Bellard committed
428

429
    /* streaming sound always should be placed just before showframe tags */
430
    if (swf->audio_par && av_fifo_size(swf->audio_fifo)) {
431
        int frame_size = av_fifo_size(swf->audio_fifo);
432
        put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
433 434
        avio_wl16(pb, swf->sound_samples);
        avio_wl16(pb, 0); // seek samples
435
        av_fifo_generic_read(swf->audio_fifo, pb, frame_size, (void*)avio_write);
436
        put_swf_end_tag(s);
437

438
        /* update FIFO */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
439
        swf->sound_samples = 0;
440 441
    }

Fabrice Bellard's avatar
Fabrice Bellard committed
442 443 444
    /* output the frame */
    put_swf_tag(s, TAG_SHOWFRAME);
    put_swf_end_tag(s);
445

Fabrice Bellard's avatar
Fabrice Bellard committed
446 447 448
    return 0;
}

449
static int swf_write_audio(AVFormatContext *s,
450
                           AVCodecParameters *par, uint8_t *buf, int size)
Fabrice Bellard's avatar
Fabrice Bellard committed
451
{
452
    SWFContext *swf = s->priv_data;
Fabrice Bellard's avatar
Fabrice Bellard committed
453

454
    /* Flash Player limit */
455
    if (swf->swf_frame_number == 16000)
456
        av_log(s, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
457

458
    if (av_fifo_size(swf->audio_fifo) + size > AUDIO_FIFO_SIZE) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
459 460
        av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n");
        return -1;
461 462
    }

463
    av_fifo_generic_write(swf->audio_fifo, buf, size, NULL);
464
    swf->sound_samples += av_get_audio_frame_duration2(par, size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
465

466
    /* if audio only stream make sure we add swf frames */
467 468
    if (!swf->video_par)
        swf_write_video(s, par, 0, 0);
Fabrice Bellard's avatar
Fabrice Bellard committed
469 470 471 472

    return 0;
}

473
static int swf_write_packet(AVFormatContext *s, AVPacket *pkt)
Fabrice Bellard's avatar
Fabrice Bellard committed
474
{
475 476 477
    AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar;
    if (par->codec_type == AVMEDIA_TYPE_AUDIO)
        return swf_write_audio(s, par, pkt->data, pkt->size);
Fabrice Bellard's avatar
Fabrice Bellard committed
478
    else
479
        return swf_write_video(s, par, pkt->data, pkt->size);
Fabrice Bellard's avatar
Fabrice Bellard committed
480 481 482 483 484
}

static int swf_write_trailer(AVFormatContext *s)
{
    SWFContext *swf = s->priv_data;
485
    AVIOContext *pb = s->pb;
486
    AVCodecParameters *par, *video_par;
Fabrice Bellard's avatar
Fabrice Bellard committed
487 488
    int file_size, i;

489
    video_par = NULL;
Fabrice Bellard's avatar
Fabrice Bellard committed
490
    for(i=0;i<s->nb_streams;i++) {
491 492 493
        par = s->streams[i]->codecpar;
        if (par->codec_type == AVMEDIA_TYPE_VIDEO)
            video_par = par;
494
        else {
Lukasz Marek's avatar
Lukasz Marek committed
495
            av_fifo_freep(&swf->audio_fifo);
496
        }
Fabrice Bellard's avatar
Fabrice Bellard committed
497 498 499 500
    }

    put_swf_tag(s, TAG_END);
    put_swf_end_tag(s);
501

Fabrice Bellard's avatar
Fabrice Bellard committed
502
    /* patch file size and number of frames if not streamed */
503
    if (s->pb->seekable && video_par) {
504
        file_size = avio_tell(pb);
505
        avio_seek(pb, 4, SEEK_SET);
506
        avio_wl32(pb, file_size);
507
        avio_seek(pb, swf->duration_pos, SEEK_SET);
508
        avio_wl16(pb, swf->video_frame_number);
509
        if (swf->vframes_pos) {
510
        avio_seek(pb, swf->vframes_pos, SEEK_SET);
511
        avio_wl16(pb, swf->video_frame_number);
512
        }
513
        avio_seek(pb, file_size, SEEK_SET);
Fabrice Bellard's avatar
Fabrice Bellard committed
514 515 516 517
    }
    return 0;
}

518
#if CONFIG_SWF_MUXER
519
AVOutputFormat ff_swf_muxer = {
520
    .name              = "swf",
521
    .long_name         = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash)"),
522 523 524
    .mime_type         = "application/x-shockwave-flash",
    .extensions        = "swf",
    .priv_data_size    = sizeof(SWFContext),
525 526
    .audio_codec       = AV_CODEC_ID_MP3,
    .video_codec       = AV_CODEC_ID_FLV1,
527 528 529
    .write_header      = swf_write_header,
    .write_packet      = swf_write_packet,
    .write_trailer     = swf_write_trailer,
530
    .flags             = AVFMT_TS_NONSTRICT,
Fabrice Bellard's avatar
Fabrice Bellard committed
531
};
532
#endif
533
#if CONFIG_AVM2_MUXER
534
AVOutputFormat ff_avm2_muxer = {
535
    .name              = "avm2",
536
    .long_name         = NULL_IF_CONFIG_SMALL("SWF (ShockWave Flash) (AVM2)"),
537 538
    .mime_type         = "application/x-shockwave-flash",
    .priv_data_size    = sizeof(SWFContext),
539 540
    .audio_codec       = AV_CODEC_ID_MP3,
    .video_codec       = AV_CODEC_ID_FLV1,
541 542 543
    .write_header      = swf_write_header,
    .write_packet      = swf_write_packet,
    .write_trailer     = swf_write_trailer,
544
    .flags             = AVFMT_TS_NONSTRICT,
545 546
};
#endif