Statistics
| Branch: | Revision:

ffmpeg / libavformat / flvenc.c @ 72415b2a

History | View | Annotate | Download (13.6 KB)

1
/*
2
 * FLV muxer
3
 * Copyright (c) 2003 The FFmpeg Project
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * 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
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22
#include "flv.h"
23
#include "riff.h"
24
#include "avc.h"
25

    
26
#undef NDEBUG
27
#include <assert.h>
28

    
29
static const AVCodecTag flv_video_codec_ids[] = {
30
    {CODEC_ID_FLV1,    FLV_CODECID_H263  },
31
    {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
32
    {CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2},
33
    {CODEC_ID_VP6F,    FLV_CODECID_VP6   },
34
    {CODEC_ID_VP6,     FLV_CODECID_VP6   },
35
    {CODEC_ID_H264,    FLV_CODECID_H264  },
36
    {CODEC_ID_NONE,    0}
37
};
38

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

    
51
typedef struct FLVContext {
52
    int reserved;
53
    int64_t duration_offset;
54
    int64_t filesize_offset;
55
    int64_t duration;
56
    int delay; ///< first dts delay for AVC
57
} FLVContext;
58

    
59
static int get_audio_flags(AVCodecContext *enc){
60
    int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
61

    
62
    if (enc->codec_id == CODEC_ID_AAC) // specs force these parameters
63
        return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
64
    else if (enc->codec_id == CODEC_ID_SPEEX) {
65
        if (enc->sample_rate != 16000) {
66
            av_log(enc, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
67
            return -1;
68
        }
69
        if (enc->channels != 1) {
70
            av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
71
            return -1;
72
        }
73
        if (enc->frame_size / 320 > 8) {
74
            av_log(enc, AV_LOG_WARNING, "Warning: Speex stream has more than "
75
                                        "8 frames per packet. Adobe Flash "
76
                                        "Player cannot handle this!\n");
77
        }
78
        return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
79
    } else {
80
    switch (enc->sample_rate) {
81
        case    44100:
82
            flags |= FLV_SAMPLERATE_44100HZ;
83
            break;
84
        case    22050:
85
            flags |= FLV_SAMPLERATE_22050HZ;
86
            break;
87
        case    11025:
88
            flags |= FLV_SAMPLERATE_11025HZ;
89
            break;
90
        case     8000: //nellymoser only
91
        case     5512: //not mp3
92
            if(enc->codec_id != CODEC_ID_MP3){
93
                flags |= FLV_SAMPLERATE_SPECIAL;
94
                break;
95
            }
96
        default:
97
            av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
98
            return -1;
99
    }
100
    }
101

    
102
    if (enc->channels > 1) {
103
        flags |= FLV_STEREO;
104
    }
105

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

    
137
    return flags;
138
}
139

    
140
static void put_amf_string(ByteIOContext *pb, const char *str)
141
{
142
    size_t len = strlen(str);
143
    put_be16(pb, len);
144
    put_buffer(pb, str, len);
145
}
146

    
147
static void put_amf_double(ByteIOContext *pb, double d)
148
{
149
    put_byte(pb, AMF_DATA_TYPE_NUMBER);
150
    put_be64(pb, av_dbl2int(d));
151
}
152

    
153
static void put_amf_bool(ByteIOContext *pb, int b) {
154
    put_byte(pb, AMF_DATA_TYPE_BOOL);
155
    put_byte(pb, !!b);
156
}
157

    
158
static int flv_write_header(AVFormatContext *s)
159
{
160
    ByteIOContext *pb = s->pb;
161
    FLVContext *flv = s->priv_data;
162
    AVCodecContext *audio_enc = NULL, *video_enc = NULL;
163
    int i;
164
    double framerate = 0.0;
165
    int metadata_size_pos, data_size;
166

    
167
    for(i=0; i<s->nb_streams; i++){
168
        AVCodecContext *enc = s->streams[i]->codec;
169
        if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
170
            if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
171
                framerate = av_q2d(s->streams[i]->r_frame_rate);
172
            } else {
173
                framerate = 1/av_q2d(s->streams[i]->codec->time_base);
174
            }
175
            video_enc = enc;
176
            if(enc->codec_tag == 0) {
177
                av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
178
                return -1;
179
            }
180
        } else {
181
            audio_enc = enc;
182
            if(get_audio_flags(enc)<0)
183
                return -1;
184
        }
185
        av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
186
    }
187
    put_tag(pb,"FLV");
188
    put_byte(pb,1);
189
    put_byte(pb,   FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
190
                 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
191
    put_be32(pb,9);
192
    put_be32(pb,0);
193

    
194
    for(i=0; i<s->nb_streams; i++){
195
        if(s->streams[i]->codec->codec_tag == 5){
196
            put_byte(pb,8); // message type
197
            put_be24(pb,0); // include flags
198
            put_be24(pb,0); // time stamp
199
            put_be32(pb,0); // reserved
200
            put_be32(pb,11); // size
201
            flv->reserved=5;
202
        }
203
    }
204

    
205
    /* write meta_tag */
206
    put_byte(pb, 18);         // tag type META
207
    metadata_size_pos= url_ftell(pb);
208
    put_be24(pb, 0);          // size of data part (sum of all parts below)
209
    put_be24(pb, 0);          // time stamp
210
    put_be32(pb, 0);          // reserved
211

    
212
    /* now data of data_size size */
213

    
214
    /* first event name as a string */
215
    put_byte(pb, AMF_DATA_TYPE_STRING);
216
    put_amf_string(pb, "onMetaData"); // 12 bytes
217

    
218
    /* mixed array (hash) with size and string/type/data tuples */
219
    put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
220
    put_be32(pb, 5*!!video_enc + 5*!!audio_enc + 2); // +2 for duration and file size
221

    
222
    put_amf_string(pb, "duration");
223
    flv->duration_offset= url_ftell(pb);
224
    put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect
225

    
226
    if(video_enc){
227
        put_amf_string(pb, "width");
228
        put_amf_double(pb, video_enc->width);
229

    
230
        put_amf_string(pb, "height");
231
        put_amf_double(pb, video_enc->height);
232

    
233
        put_amf_string(pb, "videodatarate");
234
        put_amf_double(pb, video_enc->bit_rate / 1024.0);
235

    
236
        put_amf_string(pb, "framerate");
237
        put_amf_double(pb, framerate);
238

    
239
        put_amf_string(pb, "videocodecid");
240
        put_amf_double(pb, video_enc->codec_tag);
241
    }
242

    
243
    if(audio_enc){
244
        put_amf_string(pb, "audiodatarate");
245
        put_amf_double(pb, audio_enc->bit_rate / 1024.0);
246

    
247
        put_amf_string(pb, "audiosamplerate");
248
        put_amf_double(pb, audio_enc->sample_rate);
249

    
250
        put_amf_string(pb, "audiosamplesize");
251
        put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
252

    
253
        put_amf_string(pb, "stereo");
254
        put_amf_bool(pb, audio_enc->channels == 2);
255

    
256
        put_amf_string(pb, "audiocodecid");
257
        put_amf_double(pb, audio_enc->codec_tag);
258
    }
259

    
260
    put_amf_string(pb, "filesize");
261
    flv->filesize_offset= url_ftell(pb);
262
    put_amf_double(pb, 0); // delayed write
263

    
264
    put_amf_string(pb, "");
265
    put_byte(pb, AMF_END_OF_OBJECT);
266

    
267
    /* write total size of tag */
268
    data_size= url_ftell(pb) - metadata_size_pos - 10;
269
    url_fseek(pb, metadata_size_pos, SEEK_SET);
270
    put_be24(pb, data_size);
271
    url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
272
    put_be32(pb, data_size + 11);
273

    
274
    for (i = 0; i < s->nb_streams; i++) {
275
        AVCodecContext *enc = s->streams[i]->codec;
276
        if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
277
            int64_t pos;
278
            put_byte(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
279
                     FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
280
            put_be24(pb, 0); // size patched later
281
            put_be24(pb, 0); // ts
282
            put_byte(pb, 0); // ts ext
283
            put_be24(pb, 0); // streamid
284
            pos = url_ftell(pb);
285
            if (enc->codec_id == CODEC_ID_AAC) {
286
                put_byte(pb, get_audio_flags(enc));
287
                put_byte(pb, 0); // AAC sequence header
288
                put_buffer(pb, enc->extradata, enc->extradata_size);
289
            } else {
290
                put_byte(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
291
                put_byte(pb, 0); // AVC sequence header
292
                put_be24(pb, 0); // composition time
293
                ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
294
            }
295
            data_size = url_ftell(pb) - pos;
296
            url_fseek(pb, -data_size - 10, SEEK_CUR);
297
            put_be24(pb, data_size);
298
            url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
299
            put_be32(pb, data_size + 11); // previous tag size
300
        }
301
    }
302

    
303
    return 0;
304
}
305

    
306
static int flv_write_trailer(AVFormatContext *s)
307
{
308
    int64_t file_size;
309

    
310
    ByteIOContext *pb = s->pb;
311
    FLVContext *flv = s->priv_data;
312

    
313
    file_size = url_ftell(pb);
314

    
315
    /* update informations */
316
    url_fseek(pb, flv->duration_offset, SEEK_SET);
317
    put_amf_double(pb, flv->duration / (double)1000);
318
    url_fseek(pb, flv->filesize_offset, SEEK_SET);
319
    put_amf_double(pb, file_size);
320

    
321
    url_fseek(pb, file_size, SEEK_SET);
322
    return 0;
323
}
324

    
325
static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
326
{
327
    ByteIOContext *pb = s->pb;
328
    AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
329
    FLVContext *flv = s->priv_data;
330
    unsigned ts;
331
    int size= pkt->size;
332
    uint8_t *data= NULL;
333
    int flags, flags_size;
334

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

    
337
    if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
338
       enc->codec_id == CODEC_ID_AAC)
339
        flags_size= 2;
340
    else if(enc->codec_id == CODEC_ID_H264)
341
        flags_size= 5;
342
    else
343
        flags_size= 1;
344

    
345
    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
346
        put_byte(pb, FLV_TAG_TYPE_VIDEO);
347

    
348
        flags = enc->codec_tag;
349
        if(flags == 0) {
350
            av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
351
            return -1;
352
        }
353

    
354
        flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
355
    } else {
356
        assert(enc->codec_type == AVMEDIA_TYPE_AUDIO);
357
        flags = get_audio_flags(enc);
358

    
359
        assert(size);
360

    
361
        put_byte(pb, FLV_TAG_TYPE_AUDIO);
362
    }
363

    
364
    if (enc->codec_id == CODEC_ID_H264) {
365
        /* check if extradata looks like mp4 formated */
366
        if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
367
            if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
368
                return -1;
369
        }
370
        if (!flv->delay && pkt->dts < 0)
371
            flv->delay = -pkt->dts;
372
    }
373

    
374
    ts = pkt->dts + flv->delay; // add delay to force positive dts
375
    put_be24(pb,size + flags_size);
376
    put_be24(pb,ts);
377
    put_byte(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
378
    put_be24(pb,flv->reserved);
379
    put_byte(pb,flags);
380
    if (enc->codec_id == CODEC_ID_VP6)
381
        put_byte(pb,0);
382
    if (enc->codec_id == CODEC_ID_VP6F)
383
        put_byte(pb, enc->extradata_size ? enc->extradata[0] : 0);
384
    else if (enc->codec_id == CODEC_ID_AAC)
385
        put_byte(pb,1); // AAC raw
386
    else if (enc->codec_id == CODEC_ID_H264) {
387
        put_byte(pb,1); // AVC NALU
388
        put_be24(pb,pkt->pts - pkt->dts);
389
    }
390

    
391
    put_buffer(pb, data ? data : pkt->data, size);
392

    
393
    put_be32(pb,size+flags_size+11); // previous tag size
394
    flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
395

    
396
    put_flush_packet(pb);
397

    
398
    av_free(data);
399

    
400
    return 0;
401
}
402

    
403
AVOutputFormat flv_muxer = {
404
    "flv",
405
    NULL_IF_CONFIG_SMALL("FLV format"),
406
    "video/x-flv",
407
    "flv",
408
    sizeof(FLVContext),
409
#if CONFIG_LIBMP3LAME
410
    CODEC_ID_MP3,
411
#else // CONFIG_LIBMP3LAME
412
    CODEC_ID_ADPCM_SWF,
413
#endif // CONFIG_LIBMP3LAME
414
    CODEC_ID_FLV1,
415
    flv_write_header,
416
    flv_write_packet,
417
    flv_write_trailer,
418
    .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
419
    .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
420
};