flvenc.c 16 KB
Newer Older
1
/*
2
 * FLV muxer
3
 * Copyright (c) 2003 The Libav Project
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

22
#include "libavutil/intfloat.h"
23
#include "avformat.h"
24
#include "flv.h"
25
#include "internal.h"
26
#include "avc.h"
Tomás Touceda's avatar
Tomás Touceda committed
27
#include "metadata.h"
28
#include "libavutil/dict.h"
29

Michael Niedermayer's avatar
Michael Niedermayer committed
30 31 32
#undef NDEBUG
#include <assert.h>

33
static const AVCodecTag flv_video_codec_ids[] = {
34 35
    {CODEC_ID_FLV1,    FLV_CODECID_H263  },
    {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
36
    {CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2},
37
    {CODEC_ID_VP6F,    FLV_CODECID_VP6   },
38
    {CODEC_ID_VP6,     FLV_CODECID_VP6   },
39
    {CODEC_ID_H264,    FLV_CODECID_H264  },
40 41 42
    {CODEC_ID_NONE,    0}
};

43
static const AVCodecTag flv_audio_codec_ids[] = {
44
    {CODEC_ID_MP3,       FLV_CODECID_MP3    >> FLV_AUDIO_CODECID_OFFSET},
45
    {CODEC_ID_PCM_U8,    FLV_CODECID_PCM    >> FLV_AUDIO_CODECID_OFFSET},
46
    {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM    >> FLV_AUDIO_CODECID_OFFSET},
47 48
    {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
    {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM  >> FLV_AUDIO_CODECID_OFFSET},
49
    {CODEC_ID_AAC,       FLV_CODECID_AAC    >> FLV_AUDIO_CODECID_OFFSET},
50
    {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
51
    {CODEC_ID_SPEEX,     FLV_CODECID_SPEEX  >> FLV_AUDIO_CODECID_OFFSET},
52 53 54
    {CODEC_ID_NONE,      0}
};

55
typedef struct FLVContext {
Michael Niedermayer's avatar
Michael Niedermayer committed
56
    int reserved;
57 58
    int64_t duration_offset;
    int64_t filesize_offset;
59
    int64_t duration;
60
    int64_t delay;      ///< first dts delay (needed for AVC & Speex)
61 62
} FLVContext;

63 64 65 66
typedef struct FLVStreamContext {
    int64_t last_ts;    ///< last timestamp for each stream
} FLVStreamContext;

67
static int get_audio_flags(AVCodecContext *enc){
68
    int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
69

70 71
    if (enc->codec_id == CODEC_ID_AAC) // specs force these parameters
        return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
72 73 74 75 76 77 78 79 80 81 82
    else if (enc->codec_id == CODEC_ID_SPEEX) {
        if (enc->sample_rate != 16000) {
            av_log(enc, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
            return -1;
        }
        if (enc->channels != 1) {
            av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
            return -1;
        }
        return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
    } else {
83 84
    switch (enc->sample_rate) {
        case    44100:
85
            flags |= FLV_SAMPLERATE_44100HZ;
86 87
            break;
        case    22050:
88
            flags |= FLV_SAMPLERATE_22050HZ;
89 90
            break;
        case    11025:
91
            flags |= FLV_SAMPLERATE_11025HZ;
92
            break;
93
        case    16000: //nellymoser only
94 95
        case     8000: //nellymoser only
        case     5512: //not mp3
96
            if(enc->codec_id != CODEC_ID_MP3){
Michael Niedermayer's avatar
Michael Niedermayer committed
97 98
                flags |= FLV_SAMPLERATE_SPECIAL;
                break;
99
            }
100
        default:
Diego Biurrun's avatar
Diego Biurrun committed
101
            av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
102 103
            return -1;
    }
104
    }
105 106

    if (enc->channels > 1) {
107
        flags |= FLV_STEREO;
108
    }
109

110 111
    switch(enc->codec_id){
    case CODEC_ID_MP3:
112
        flags |= FLV_CODECID_MP3    | FLV_SAMPLESSIZE_16BIT;
113
        break;
114
    case CODEC_ID_PCM_U8:
115
        flags |= FLV_CODECID_PCM    | FLV_SAMPLESSIZE_8BIT;
116
        break;
117
    case CODEC_ID_PCM_S16BE:
118
        flags |= FLV_CODECID_PCM    | FLV_SAMPLESSIZE_16BIT;
119
        break;
120
    case CODEC_ID_PCM_S16LE:
121
        flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
122
        break;
123
    case CODEC_ID_ADPCM_SWF:
124
        flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
125
        break;
126
    case CODEC_ID_NELLYMOSER:
127 128
        if (enc->sample_rate == 8000) {
            flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
129 130
        } else if (enc->sample_rate == 16000) {
            flags |= FLV_CODECID_NELLYMOSER_16KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
131 132 133
        } else {
            flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
        }
134
        break;
135 136 137 138
    case 0:
        flags |= enc->codec_tag<<4;
        break;
    default:
139
        av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
140 141
        return -1;
    }
142

143 144 145
    return flags;
}

146
static void put_amf_string(AVIOContext *pb, const char *str)
147 148
{
    size_t len = strlen(str);
149 150
    avio_wb16(pb, len);
    avio_write(pb, str, len);
151 152
}

153
static void put_avc_eos_tag(AVIOContext *pb, unsigned ts) {
154 155 156 157 158 159 160 161 162
    avio_w8(pb, FLV_TAG_TYPE_VIDEO);
    avio_wb24(pb, 5);  /* Tag Data Size */
    avio_wb24(pb, ts);  /* lower 24 bits of timestamp in ms*/
    avio_w8(pb, (ts >> 24) & 0x7F);  /* MSB of ts in ms*/
    avio_wb24(pb, 0);  /* StreamId = 0 */
    avio_w8(pb, 23);  /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
    avio_w8(pb, 2);  /* AVC end of sequence */
    avio_wb24(pb, 0);  /* Always 0 for AVC EOS. */
    avio_wb32(pb, 16);  /* Size of FLV tag */
163 164
}

165
static void put_amf_double(AVIOContext *pb, double d)
166
{
167
    avio_w8(pb, AMF_DATA_TYPE_NUMBER);
168
    avio_wb64(pb, av_double2int(d));
169 170
}

171
static void put_amf_bool(AVIOContext *pb, int b) {
172 173
    avio_w8(pb, AMF_DATA_TYPE_BOOL);
    avio_w8(pb, !!b);
174 175
}

176 177
static int flv_write_header(AVFormatContext *s)
{
178
    AVIOContext *pb = s->pb;
179
    FLVContext *flv = s->priv_data;
180
    AVCodecContext *audio_enc = NULL, *video_enc = NULL;
181
    int i, metadata_count = 0;
182
    double framerate = 0.0;
183
    int64_t metadata_size_pos, data_size, metadata_count_pos;
184
    AVDictionaryEntry *tag = NULL;
185

Michael Niedermayer's avatar
Michael Niedermayer committed
186
    for(i=0; i<s->nb_streams; i++){
187
        AVCodecContext *enc = s->streams[i]->codec;
188
        FLVStreamContext *sc;
189
        if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
190 191 192 193 194
            if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
                framerate = av_q2d(s->streams[i]->r_frame_rate);
            } else {
                framerate = 1/av_q2d(s->streams[i]->codec->time_base);
            }
195 196
            video_enc = enc;
            if(enc->codec_tag == 0) {
197 198 199
                av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
                return -1;
            }
200
        } else {
201
            audio_enc = enc;
202 203
            if(get_audio_flags(enc)<0)
                return -1;
204
        }
205
        avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
206 207 208 209 210 211

        sc = av_mallocz(sizeof(FLVStreamContext));
        if (!sc)
            return AVERROR(ENOMEM);
        s->streams[i]->priv_data = sc;
        sc->last_ts = -1;
212
    }
213 214
    flv->delay = AV_NOPTS_VALUE;

215
    avio_write(pb, "FLV", 3);
216 217
    avio_w8(pb,1);
    avio_w8(pb,   FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
218
                 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
219 220
    avio_wb32(pb,9);
    avio_wb32(pb,0);
221 222 223

    for(i=0; i<s->nb_streams; i++){
        if(s->streams[i]->codec->codec_tag == 5){
224 225 226 227 228
            avio_w8(pb,8); // message type
            avio_wb24(pb,0); // include flags
            avio_wb24(pb,0); // time stamp
            avio_wb32(pb,0); // reserved
            avio_wb32(pb,11); // size
Michael Niedermayer's avatar
Michael Niedermayer committed
229 230 231
            flv->reserved=5;
        }
    }
232

233
    /* write meta_tag */
234
    avio_w8(pb, 18);         // tag type META
235
    metadata_size_pos= avio_tell(pb);
236 237 238
    avio_wb24(pb, 0);          // size of data part (sum of all parts below)
    avio_wb24(pb, 0);          // time stamp
    avio_wb32(pb, 0);          // reserved
239 240 241 242

    /* now data of data_size size */

    /* first event name as a string */
243
    avio_w8(pb, AMF_DATA_TYPE_STRING);
244 245 246
    put_amf_string(pb, "onMetaData"); // 12 bytes

    /* mixed array (hash) with size and string/type/data tuples */
247
    avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
248 249 250
    metadata_count_pos = avio_tell(pb);
    metadata_count = 5*!!video_enc + 5*!!audio_enc + 2; // +2 for duration and file size
    avio_wb32(pb, metadata_count);
251

252
    put_amf_string(pb, "duration");
253
    flv->duration_offset= avio_tell(pb);
254
    put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect
255

256
    if(video_enc){
257
        put_amf_string(pb, "width");
258
        put_amf_double(pb, video_enc->width);
259 260

        put_amf_string(pb, "height");
261
        put_amf_double(pb, video_enc->height);
262 263

        put_amf_string(pb, "videodatarate");
264
        put_amf_double(pb, video_enc->bit_rate / 1024.0);
265 266 267

        put_amf_string(pb, "framerate");
        put_amf_double(pb, framerate);
268 269

        put_amf_string(pb, "videocodecid");
270
        put_amf_double(pb, video_enc->codec_tag);
271 272
    }

273
    if(audio_enc){
274 275 276
        put_amf_string(pb, "audiodatarate");
        put_amf_double(pb, audio_enc->bit_rate / 1024.0);

277
        put_amf_string(pb, "audiosamplerate");
278
        put_amf_double(pb, audio_enc->sample_rate);
279 280

        put_amf_string(pb, "audiosamplesize");
281
        put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
282 283

        put_amf_string(pb, "stereo");
284
        put_amf_bool(pb, audio_enc->channels == 2);
285 286

        put_amf_string(pb, "audiocodecid");
287
        put_amf_double(pb, audio_enc->codec_tag);
288 289
    }

290
    while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
Tomás Touceda's avatar
Tomás Touceda committed
291
        put_amf_string(pb, tag->key);
292
        avio_w8(pb, AMF_DATA_TYPE_STRING);
Tomás Touceda's avatar
Tomás Touceda committed
293
        put_amf_string(pb, tag->value);
294
        metadata_count++;
Tomás Touceda's avatar
Tomás Touceda committed
295 296
    }

297
    put_amf_string(pb, "filesize");
298
    flv->filesize_offset= avio_tell(pb);
299
    put_amf_double(pb, 0); // delayed write
300 301

    put_amf_string(pb, "");
302
    avio_w8(pb, AMF_END_OF_OBJECT);
303 304

    /* write total size of tag */
305
    data_size= avio_tell(pb) - metadata_size_pos - 10;
306 307 308 309

    avio_seek(pb, metadata_count_pos, SEEK_SET);
    avio_wb32(pb, metadata_count);

310
    avio_seek(pb, metadata_size_pos, SEEK_SET);
311
    avio_wb24(pb, data_size);
312
    avio_skip(pb, data_size + 10 - 3);
313
    avio_wb32(pb, data_size + 11);
314

315 316 317
    for (i = 0; i < s->nb_streams; i++) {
        AVCodecContext *enc = s->streams[i]->codec;
        if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
318
            int64_t pos;
319
            avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
320
                     FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
321 322 323 324
            avio_wb24(pb, 0); // size patched later
            avio_wb24(pb, 0); // ts
            avio_w8(pb, 0); // ts ext
            avio_wb24(pb, 0); // streamid
325
            pos = avio_tell(pb);
326
            if (enc->codec_id == CODEC_ID_AAC) {
327 328 329
                avio_w8(pb, get_audio_flags(enc));
                avio_w8(pb, 0); // AAC sequence header
                avio_write(pb, enc->extradata, enc->extradata_size);
330
            } else {
331 332 333
                avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
                avio_w8(pb, 0); // AVC sequence header
                avio_wb24(pb, 0); // composition time
334 335
                ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
            }
336
            data_size = avio_tell(pb) - pos;
337
            avio_seek(pb, -data_size - 10, SEEK_CUR);
338
            avio_wb24(pb, data_size);
339
            avio_skip(pb, data_size + 10 - 3);
340
            avio_wb32(pb, data_size + 11); // previous tag size
341 342 343
        }
    }

344 345 346 347 348
    return 0;
}

static int flv_write_trailer(AVFormatContext *s)
{
349 350
    int64_t file_size;

351
    AVIOContext *pb = s->pb;
352
    FLVContext *flv = s->priv_data;
353 354 355 356 357
    int i;

    /* Add EOS tag */
    for (i = 0; i < s->nb_streams; i++) {
        AVCodecContext *enc = s->streams[i]->codec;
358
        FLVStreamContext *sc = s->streams[i]->priv_data;
359
        if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
360
                enc->codec_id == CODEC_ID_H264) {
361
            put_avc_eos_tag(pb, sc->last_ts);
362 363
        }
    }
364

365
    file_size = avio_tell(pb);
366

367
    /* update information */
368
    avio_seek(pb, flv->duration_offset, SEEK_SET);
369
    put_amf_double(pb, flv->duration / (double)1000);
370
    avio_seek(pb, flv->filesize_offset, SEEK_SET);
371 372
    put_amf_double(pb, file_size);

373
    avio_seek(pb, file_size, SEEK_SET);
374 375 376
    return 0;
}

377
static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
378
{
379
    AVIOContext *pb = s->pb;
380
    AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
381
    FLVContext *flv = s->priv_data;
382
    FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data;
383
    unsigned ts;
384
    int size= pkt->size;
385
    uint8_t *data= NULL;
386
    int flags, flags_size;
387

388
//    av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
389

390 391
    if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
       enc->codec_id == CODEC_ID_AAC)
392
        flags_size= 2;
393 394
    else if(enc->codec_id == CODEC_ID_H264)
        flags_size= 5;
395 396 397
    else
        flags_size= 1;

398
    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
399
        avio_w8(pb, FLV_TAG_TYPE_VIDEO);
400

401
        flags = enc->codec_tag;
402 403 404 405 406
        if(flags == 0) {
            av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
            return -1;
        }

407
        flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
Michael Niedermayer's avatar
Michael Niedermayer committed
408
    } else {
409
        assert(enc->codec_type == AVMEDIA_TYPE_AUDIO);
410
        flags = get_audio_flags(enc);
411

Michael Niedermayer's avatar
Michael Niedermayer committed
412 413
        assert(size);

414
        avio_w8(pb, FLV_TAG_TYPE_AUDIO);
Michael Niedermayer's avatar
Michael Niedermayer committed
415 416
    }

417
    if (enc->codec_id == CODEC_ID_H264) {
418
        /* check if extradata looks like MP4 */
419 420 421 422
        if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
            if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
                return -1;
        }
423
    }
424 425 426 427 428 429 430
    if (flv->delay == AV_NOPTS_VALUE)
        flv->delay = -pkt->dts;
    if (pkt->dts < -flv->delay) {
        av_log(s, AV_LOG_WARNING, "Packets are not in the proper order with "
                                  "respect to DTS\n");
        return AVERROR(EINVAL);
    }
431

432
    ts = pkt->dts + flv->delay; // add delay to force positive dts
433 434

    /* check Speex packet duration */
435
    if (enc->codec_id == CODEC_ID_SPEEX && ts - sc->last_ts > 160) {
436 437 438
        av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than "
                                  "8 frames per packet. Adobe Flash "
                                  "Player cannot handle this!\n");
439
    }
440

441 442
    if (sc->last_ts < ts)
        sc->last_ts = ts;
443

444 445 446 447 448
    avio_wb24(pb,size + flags_size);
    avio_wb24(pb,ts);
    avio_w8(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
    avio_wb24(pb,flv->reserved);
    avio_w8(pb,flags);
449
    if (enc->codec_id == CODEC_ID_VP6)
450
        avio_w8(pb,0);
451
    if (enc->codec_id == CODEC_ID_VP6F)
452
        avio_w8(pb, enc->extradata_size ? enc->extradata[0] : 0);
453
    else if (enc->codec_id == CODEC_ID_AAC)
454
        avio_w8(pb,1); // AAC raw
455
    else if (enc->codec_id == CODEC_ID_H264) {
456 457
        avio_w8(pb,1); // AVC NALU
        avio_wb24(pb,pkt->pts - pkt->dts);
458
    }
459

460
    avio_write(pb, data ? data : pkt->data, size);
461

462
    avio_wb32(pb,size+flags_size+11); // previous tag size
463
    flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
464

465
    avio_flush(pb);
466 467 468

    av_free(data);

469
    return pb->error;
470 471
}

472
AVOutputFormat ff_flv_muxer = {
473 474 475 476 477
    .name           = "flv",
    .long_name      = NULL_IF_CONFIG_SMALL("FLV format"),
    .mime_type      = "video/x-flv",
    .extensions     = "flv",
    .priv_data_size = sizeof(FLVContext),
478
#if CONFIG_LIBMP3LAME
479
    .audio_codec    = CODEC_ID_MP3,
480
#else // CONFIG_LIBMP3LAME
481
    .audio_codec    = CODEC_ID_ADPCM_SWF,
482
#endif // CONFIG_LIBMP3LAME
483 484 485 486
    .video_codec    = CODEC_ID_FLV1,
    .write_header   = flv_write_header,
    .write_packet   = flv_write_packet,
    .write_trailer  = flv_write_trailer,
487
    .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
488
    .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
489
};