Statistics
| Branch: | Revision:

ffmpeg / libavformat / flvenc.c @ 6cc7f139

History | View | Annotate | Download (14.8 KB)

1 d4f5d74a Garrick Meeker
/*
2 7fbde343 Aurelien Jacobs
 * FLV muxer
3 2912e87a Mans Rullgard
 * Copyright (c) 2003 The Libav Project
4 d4f5d74a Garrick Meeker
 *
5 2912e87a Mans Rullgard
 * This file is part of Libav.
6 b78e7197 Diego Biurrun
 *
7 2912e87a Mans Rullgard
 * Libav is free software; you can redistribute it and/or
8 d4f5d74a Garrick Meeker
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 d4f5d74a Garrick Meeker
 *
12 2912e87a Mans Rullgard
 * Libav is distributed in the hope that it will be useful,
13 d4f5d74a Garrick Meeker
 * 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 2912e87a Mans Rullgard
 * License along with Libav; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 d4f5d74a Garrick Meeker
 */
21
#include "avformat.h"
22 6cac3a3b Allan Hsu
#include "flv.h"
23 80b39e1c Francesco Lavra
#include "internal.h"
24 f23496b5 Baptiste Coudurier
#include "avc.h"
25 16f82508 Tomás Touceda
#include "metadata.h"
26 d4f5d74a Garrick Meeker
27 068f2a22 Michael Niedermayer
#undef NDEBUG
28
#include <assert.h>
29
30 7caf0cc6 Michael Niedermayer
static const AVCodecTag flv_video_codec_ids[] = {
31 148c9bdb Allan Hsu
    {CODEC_ID_FLV1,    FLV_CODECID_H263  },
32
    {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
33 1b88277b Daniel Verkamp
    {CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2},
34 148c9bdb Allan Hsu
    {CODEC_ID_VP6F,    FLV_CODECID_VP6   },
35 09d8c0ae Benjamin Larsson
    {CODEC_ID_VP6,     FLV_CODECID_VP6   },
36 f23496b5 Baptiste Coudurier
    {CODEC_ID_H264,    FLV_CODECID_H264  },
37 148c9bdb Allan Hsu
    {CODEC_ID_NONE,    0}
38
};
39
40 7caf0cc6 Michael Niedermayer
static const AVCodecTag flv_audio_codec_ids[] = {
41 148c9bdb Allan Hsu
    {CODEC_ID_MP3,       FLV_CODECID_MP3    >> FLV_AUDIO_CODECID_OFFSET},
42 8e9efe43 Daniel Verkamp
    {CODEC_ID_PCM_U8,    FLV_CODECID_PCM    >> FLV_AUDIO_CODECID_OFFSET},
43 44de39f9 Michael Niedermayer
    {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM    >> FLV_AUDIO_CODECID_OFFSET},
44 148c9bdb Allan Hsu
    {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
45
    {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM  >> FLV_AUDIO_CODECID_OFFSET},
46 f23496b5 Baptiste Coudurier
    {CODEC_ID_AAC,       FLV_CODECID_AAC    >> FLV_AUDIO_CODECID_OFFSET},
47 b7d1cd02 Bartlomiej Wolowiec
    {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
48 046c4001 Justin Ruggles
    {CODEC_ID_SPEEX,     FLV_CODECID_SPEEX  >> FLV_AUDIO_CODECID_OFFSET},
49 148c9bdb Allan Hsu
    {CODEC_ID_NONE,      0}
50
};
51
52 d4f5d74a Garrick Meeker
typedef struct FLVContext {
53 11a8e425 Michael Niedermayer
    int reserved;
54 bc5c918e Diego Biurrun
    int64_t duration_offset;
55
    int64_t filesize_offset;
56 634b8cfa Baptiste Coudurier
    int64_t duration;
57 f23496b5 Baptiste Coudurier
    int delay; ///< first dts delay for AVC
58 df4f1d51 Thierry Foucu
    int64_t last_video_ts;
59 d4f5d74a Garrick Meeker
} FLVContext;
60
61 37cdf93d Michael Niedermayer
static int get_audio_flags(AVCodecContext *enc){
62 dd1c8f3e Luca Abeni
    int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
63 37cdf93d Michael Niedermayer
64 f23496b5 Baptiste Coudurier
    if (enc->codec_id == CODEC_ID_AAC) // specs force these parameters
65
        return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
66 046c4001 Justin Ruggles
    else if (enc->codec_id == CODEC_ID_SPEEX) {
67
        if (enc->sample_rate != 16000) {
68
            av_log(enc, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
69
            return -1;
70
        }
71
        if (enc->channels != 1) {
72
            av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
73
            return -1;
74
        }
75
        if (enc->frame_size / 320 > 8) {
76 68677dd8 Justin Ruggles
            av_log(enc, AV_LOG_WARNING, "Warning: Speex stream has more than "
77
                                        "8 frames per packet. Adobe Flash "
78
                                        "Player cannot handle this!\n");
79 046c4001 Justin Ruggles
        }
80
        return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
81
    } else {
82 37cdf93d Michael Niedermayer
    switch (enc->sample_rate) {
83
        case    44100:
84 6cac3a3b Allan Hsu
            flags |= FLV_SAMPLERATE_44100HZ;
85 37cdf93d Michael Niedermayer
            break;
86
        case    22050:
87 6cac3a3b Allan Hsu
            flags |= FLV_SAMPLERATE_22050HZ;
88 37cdf93d Michael Niedermayer
            break;
89
        case    11025:
90 6cac3a3b Allan Hsu
            flags |= FLV_SAMPLERATE_11025HZ;
91 37cdf93d Michael Niedermayer
            break;
92
        case     8000: //nellymoser only
93
        case     5512: //not mp3
94 4838727e Michael Niedermayer
            if(enc->codec_id != CODEC_ID_MP3){
95 ec627278 Michael Niedermayer
                flags |= FLV_SAMPLERATE_SPECIAL;
96
                break;
97 4838727e Michael Niedermayer
            }
98 37cdf93d Michael Niedermayer
        default:
99 755bfeab Diego Biurrun
            av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
100 37cdf93d Michael Niedermayer
            return -1;
101
    }
102 f23496b5 Baptiste Coudurier
    }
103 37cdf93d Michael Niedermayer
104
    if (enc->channels > 1) {
105 6cac3a3b Allan Hsu
        flags |= FLV_STEREO;
106 37cdf93d Michael Niedermayer
    }
107 115329f1 Diego Biurrun
108 37cdf93d Michael Niedermayer
    switch(enc->codec_id){
109
    case CODEC_ID_MP3:
110 6cac3a3b Allan Hsu
        flags |= FLV_CODECID_MP3    | FLV_SAMPLESSIZE_16BIT;
111 37cdf93d Michael Niedermayer
        break;
112 8e9efe43 Daniel Verkamp
    case CODEC_ID_PCM_U8:
113 44de39f9 Michael Niedermayer
        flags |= FLV_CODECID_PCM    | FLV_SAMPLESSIZE_8BIT;
114 bb270c08 Diego Biurrun
        break;
115 923bd441 Alex Beregszaszi
    case CODEC_ID_PCM_S16BE:
116 44de39f9 Michael Niedermayer
        flags |= FLV_CODECID_PCM    | FLV_SAMPLESSIZE_16BIT;
117 bb270c08 Diego Biurrun
        break;
118 923bd441 Alex Beregszaszi
    case CODEC_ID_PCM_S16LE:
119 6cac3a3b Allan Hsu
        flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
120 bb270c08 Diego Biurrun
        break;
121 2fde8aae Alex Beregszaszi
    case CODEC_ID_ADPCM_SWF:
122 e9509536 Benjamin Larsson
        flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
123 bb270c08 Diego Biurrun
        break;
124 b7d1cd02 Bartlomiej Wolowiec
    case CODEC_ID_NELLYMOSER:
125 8ddd280d Alexander Wichers
        if (enc->sample_rate == 8000) {
126
            flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
127
        } else {
128
            flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
129
        }
130 b7d1cd02 Bartlomiej Wolowiec
        break;
131 37cdf93d Michael Niedermayer
    case 0:
132
        flags |= enc->codec_tag<<4;
133
        break;
134
    default:
135 a254c574 Michael Niedermayer
        av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
136 37cdf93d Michael Niedermayer
        return -1;
137
    }
138 115329f1 Diego Biurrun
139 37cdf93d Michael Niedermayer
    return flags;
140
}
141
142 ae628ec1 Anton Khirnov
static void put_amf_string(AVIOContext *pb, const char *str)
143 fd0fb306 Michael Niedermayer
{
144
    size_t len = strlen(str);
145 77eb5504 Anton Khirnov
    avio_wb16(pb, len);
146
    avio_write(pb, str, len);
147 fd0fb306 Michael Niedermayer
}
148
149 ae628ec1 Anton Khirnov
static void put_avc_eos_tag(AVIOContext *pb, unsigned ts) {
150 77eb5504 Anton Khirnov
    avio_w8(pb, FLV_TAG_TYPE_VIDEO);
151
    avio_wb24(pb, 5);  /* Tag Data Size */
152
    avio_wb24(pb, ts);  /* lower 24 bits of timestamp in ms*/
153
    avio_w8(pb, (ts >> 24) & 0x7F);  /* MSB of ts in ms*/
154
    avio_wb24(pb, 0);  /* StreamId = 0 */
155
    avio_w8(pb, 23);  /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
156
    avio_w8(pb, 2);  /* AVC end of sequence */
157
    avio_wb24(pb, 0);  /* Always 0 for AVC EOS. */
158
    avio_wb32(pb, 16);  /* Size of FLV tag */
159 df4f1d51 Thierry Foucu
}
160
161 ae628ec1 Anton Khirnov
static void put_amf_double(AVIOContext *pb, double d)
162 fd0fb306 Michael Niedermayer
{
163 77eb5504 Anton Khirnov
    avio_w8(pb, AMF_DATA_TYPE_NUMBER);
164
    avio_wb64(pb, av_dbl2int(d));
165 fd0fb306 Michael Niedermayer
}
166
167 ae628ec1 Anton Khirnov
static void put_amf_bool(AVIOContext *pb, int b) {
168 77eb5504 Anton Khirnov
    avio_w8(pb, AMF_DATA_TYPE_BOOL);
169
    avio_w8(pb, !!b);
170 148c9bdb Allan Hsu
}
171
172 d4f5d74a Garrick Meeker
static int flv_write_header(AVFormatContext *s)
173
{
174 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
175 d4f5d74a Garrick Meeker
    FLVContext *flv = s->priv_data;
176 5366f15d Baptiste Coudurier
    AVCodecContext *audio_enc = NULL, *video_enc = NULL;
177
    int i;
178 fd0fb306 Michael Niedermayer
    double framerate = 0.0;
179
    int metadata_size_pos, data_size;
180 16f82508 Tomás Touceda
    AVMetadataTag *tag = NULL;
181 d4f5d74a Garrick Meeker
182 11a8e425 Michael Niedermayer
    for(i=0; i<s->nb_streams; i++){
183 01f4895c Michael Niedermayer
        AVCodecContext *enc = s->streams[i]->codec;
184 72415b2a Stefano Sabatini
        if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
185 fd0fb306 Michael Niedermayer
            if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
186
                framerate = av_q2d(s->streams[i]->r_frame_rate);
187
            } else {
188
                framerate = 1/av_q2d(s->streams[i]->codec->time_base);
189
            }
190 5366f15d Baptiste Coudurier
            video_enc = enc;
191
            if(enc->codec_tag == 0) {
192 148c9bdb Allan Hsu
                av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
193
                return -1;
194
            }
195 fd0fb306 Michael Niedermayer
        } else {
196 5366f15d Baptiste Coudurier
            audio_enc = enc;
197 c45388b1 Michael Niedermayer
            if(get_audio_flags(enc)<0)
198
                return -1;
199 fd0fb306 Michael Niedermayer
        }
200 254629b1 Baptiste Coudurier
        av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
201 c45388b1 Michael Niedermayer
    }
202 bbc413f9 Anton Khirnov
    avio_write(pb, "FLV", 3);
203 77eb5504 Anton Khirnov
    avio_w8(pb,1);
204
    avio_w8(pb,   FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
205 5366f15d Baptiste Coudurier
                 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
206 77eb5504 Anton Khirnov
    avio_wb32(pb,9);
207
    avio_wb32(pb,0);
208 c45388b1 Michael Niedermayer
209
    for(i=0; i<s->nb_streams; i++){
210
        if(s->streams[i]->codec->codec_tag == 5){
211 77eb5504 Anton Khirnov
            avio_w8(pb,8); // message type
212
            avio_wb24(pb,0); // include flags
213
            avio_wb24(pb,0); // time stamp
214
            avio_wb32(pb,0); // reserved
215
            avio_wb32(pb,11); // size
216 11a8e425 Michael Niedermayer
            flv->reserved=5;
217
        }
218
    }
219 d4f5d74a Garrick Meeker
220 df4f1d51 Thierry Foucu
    flv->last_video_ts = -1;
221
222 fd0fb306 Michael Niedermayer
    /* write meta_tag */
223 77eb5504 Anton Khirnov
    avio_w8(pb, 18);         // tag type META
224 a2704c97 Anton Khirnov
    metadata_size_pos= avio_tell(pb);
225 77eb5504 Anton Khirnov
    avio_wb24(pb, 0);          // size of data part (sum of all parts below)
226
    avio_wb24(pb, 0);          // time stamp
227
    avio_wb32(pb, 0);          // reserved
228 fd0fb306 Michael Niedermayer
229
    /* now data of data_size size */
230
231
    /* first event name as a string */
232 77eb5504 Anton Khirnov
    avio_w8(pb, AMF_DATA_TYPE_STRING);
233 fd0fb306 Michael Niedermayer
    put_amf_string(pb, "onMetaData"); // 12 bytes
234
235
    /* mixed array (hash) with size and string/type/data tuples */
236 77eb5504 Anton Khirnov
    avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
237
    avio_wb32(pb, 5*!!video_enc + 5*!!audio_enc + 2); // +2 for duration and file size
238 fd0fb306 Michael Niedermayer
239 634b8cfa Baptiste Coudurier
    put_amf_string(pb, "duration");
240 a2704c97 Anton Khirnov
    flv->duration_offset= avio_tell(pb);
241 c5e1e982 Justin Johnson
    put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect
242 fd0fb306 Michael Niedermayer
243 5366f15d Baptiste Coudurier
    if(video_enc){
244 fd0fb306 Michael Niedermayer
        put_amf_string(pb, "width");
245 5366f15d Baptiste Coudurier
        put_amf_double(pb, video_enc->width);
246 fd0fb306 Michael Niedermayer
247
        put_amf_string(pb, "height");
248 5366f15d Baptiste Coudurier
        put_amf_double(pb, video_enc->height);
249 fd0fb306 Michael Niedermayer
250
        put_amf_string(pb, "videodatarate");
251 426a6f34 Stefan de Konink
        put_amf_double(pb, video_enc->bit_rate / 1024.0);
252 fd0fb306 Michael Niedermayer
253
        put_amf_string(pb, "framerate");
254
        put_amf_double(pb, framerate);
255 148c9bdb Allan Hsu
256
        put_amf_string(pb, "videocodecid");
257 5366f15d Baptiste Coudurier
        put_amf_double(pb, video_enc->codec_tag);
258 fd0fb306 Michael Niedermayer
    }
259
260 5366f15d Baptiste Coudurier
    if(audio_enc){
261 426a6f34 Stefan de Konink
        put_amf_string(pb, "audiodatarate");
262
        put_amf_double(pb, audio_enc->bit_rate / 1024.0);
263
264 fd0fb306 Michael Niedermayer
        put_amf_string(pb, "audiosamplerate");
265 5366f15d Baptiste Coudurier
        put_amf_double(pb, audio_enc->sample_rate);
266 148c9bdb Allan Hsu
267
        put_amf_string(pb, "audiosamplesize");
268 8e9efe43 Daniel Verkamp
        put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
269 148c9bdb Allan Hsu
270
        put_amf_string(pb, "stereo");
271 5366f15d Baptiste Coudurier
        put_amf_bool(pb, audio_enc->channels == 2);
272 148c9bdb Allan Hsu
273
        put_amf_string(pb, "audiocodecid");
274 5366f15d Baptiste Coudurier
        put_amf_double(pb, audio_enc->codec_tag);
275 fd0fb306 Michael Niedermayer
    }
276
277 16f82508 Tomás Touceda
    while ((tag = av_metadata_get(s->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX))) {
278
        put_amf_string(pb, tag->key);
279 77eb5504 Anton Khirnov
        avio_w8(pb, AMF_DATA_TYPE_STRING);
280 16f82508 Tomás Touceda
        put_amf_string(pb, tag->value);
281
    }
282
283 634b8cfa Baptiste Coudurier
    put_amf_string(pb, "filesize");
284 a2704c97 Anton Khirnov
    flv->filesize_offset= avio_tell(pb);
285 634b8cfa Baptiste Coudurier
    put_amf_double(pb, 0); // delayed write
286 fd0fb306 Michael Niedermayer
287
    put_amf_string(pb, "");
288 77eb5504 Anton Khirnov
    avio_w8(pb, AMF_END_OF_OBJECT);
289 fd0fb306 Michael Niedermayer
290
    /* write total size of tag */
291 a2704c97 Anton Khirnov
    data_size= avio_tell(pb) - metadata_size_pos - 10;
292 6b4aa5da Anton Khirnov
    avio_seek(pb, metadata_size_pos, SEEK_SET);
293 77eb5504 Anton Khirnov
    avio_wb24(pb, data_size);
294 45a8a02a Anton Khirnov
    avio_skip(pb, data_size + 10 - 3);
295 77eb5504 Anton Khirnov
    avio_wb32(pb, data_size + 11);
296 fd0fb306 Michael Niedermayer
297 f23496b5 Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++) {
298
        AVCodecContext *enc = s->streams[i]->codec;
299
        if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
300 bc5c918e Diego Biurrun
            int64_t pos;
301 77eb5504 Anton Khirnov
            avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
302 f23496b5 Baptiste Coudurier
                     FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
303 77eb5504 Anton Khirnov
            avio_wb24(pb, 0); // size patched later
304
            avio_wb24(pb, 0); // ts
305
            avio_w8(pb, 0); // ts ext
306
            avio_wb24(pb, 0); // streamid
307 a2704c97 Anton Khirnov
            pos = avio_tell(pb);
308 f23496b5 Baptiste Coudurier
            if (enc->codec_id == CODEC_ID_AAC) {
309 77eb5504 Anton Khirnov
                avio_w8(pb, get_audio_flags(enc));
310
                avio_w8(pb, 0); // AAC sequence header
311
                avio_write(pb, enc->extradata, enc->extradata_size);
312 f23496b5 Baptiste Coudurier
            } else {
313 77eb5504 Anton Khirnov
                avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY); // flags
314
                avio_w8(pb, 0); // AVC sequence header
315
                avio_wb24(pb, 0); // composition time
316 f23496b5 Baptiste Coudurier
                ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
317
            }
318 a2704c97 Anton Khirnov
            data_size = avio_tell(pb) - pos;
319 6b4aa5da Anton Khirnov
            avio_seek(pb, -data_size - 10, SEEK_CUR);
320 77eb5504 Anton Khirnov
            avio_wb24(pb, data_size);
321 45a8a02a Anton Khirnov
            avio_skip(pb, data_size + 10 - 3);
322 77eb5504 Anton Khirnov
            avio_wb32(pb, data_size + 11); // previous tag size
323 f23496b5 Baptiste Coudurier
        }
324
    }
325
326 d4f5d74a Garrick Meeker
    return 0;
327
}
328
329
static int flv_write_trailer(AVFormatContext *s)
330
{
331 14b32253 Michael Niedermayer
    int64_t file_size;
332
333 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
334 d4f5d74a Garrick Meeker
    FLVContext *flv = s->priv_data;
335 df4f1d51 Thierry Foucu
    int i;
336
337
    /* Add EOS tag */
338
    for (i = 0; i < s->nb_streams; i++) {
339
        AVCodecContext *enc = s->streams[i]->codec;
340 b9f9e59a Stefano Sabatini
        if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
341 df4f1d51 Thierry Foucu
                enc->codec_id == CODEC_ID_H264) {
342
            put_avc_eos_tag(pb, flv->last_video_ts);
343
        }
344
    }
345 d4f5d74a Garrick Meeker
346 a2704c97 Anton Khirnov
    file_size = avio_tell(pb);
347 634b8cfa Baptiste Coudurier
348
    /* update informations */
349 6b4aa5da Anton Khirnov
    avio_seek(pb, flv->duration_offset, SEEK_SET);
350 634b8cfa Baptiste Coudurier
    put_amf_double(pb, flv->duration / (double)1000);
351 6b4aa5da Anton Khirnov
    avio_seek(pb, flv->filesize_offset, SEEK_SET);
352 634b8cfa Baptiste Coudurier
    put_amf_double(pb, file_size);
353
354 6b4aa5da Anton Khirnov
    avio_seek(pb, file_size, SEEK_SET);
355 d4f5d74a Garrick Meeker
    return 0;
356
}
357
358 e928649b Michael Niedermayer
static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
359 d4f5d74a Garrick Meeker
{
360 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
361 01f4895c Michael Niedermayer
    AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
362 d4f5d74a Garrick Meeker
    FLVContext *flv = s->priv_data;
363 f23496b5 Baptiste Coudurier
    unsigned ts;
364 e928649b Michael Niedermayer
    int size= pkt->size;
365 71e685b0 Baptiste Coudurier
    uint8_t *data= NULL;
366 f683dbdc Michael Niedermayer
    int flags, flags_size;
367 d4f5d74a Garrick Meeker
368 949b1a13 Steve L'Homme
//    av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
369 115329f1 Diego Biurrun
370 f23496b5 Baptiste Coudurier
    if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
371
       enc->codec_id == CODEC_ID_AAC)
372 f683dbdc Michael Niedermayer
        flags_size= 2;
373 f23496b5 Baptiste Coudurier
    else if(enc->codec_id == CODEC_ID_H264)
374
        flags_size= 5;
375 f683dbdc Michael Niedermayer
    else
376
        flags_size= 1;
377
378 72415b2a Stefano Sabatini
    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
379 77eb5504 Anton Khirnov
        avio_w8(pb, FLV_TAG_TYPE_VIDEO);
380 09d8c0ae Benjamin Larsson
381 bb85077f Michael Niedermayer
        flags = enc->codec_tag;
382 09d8c0ae Benjamin Larsson
        if(flags == 0) {
383
            av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
384
            return -1;
385
        }
386
387 cc947f04 Jean-Daniel Dupas
        flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
388 75293f05 Michael Niedermayer
    } else {
389 72415b2a Stefano Sabatini
        assert(enc->codec_type == AVMEDIA_TYPE_AUDIO);
390 37cdf93d Michael Niedermayer
        flags = get_audio_flags(enc);
391 115329f1 Diego Biurrun
392 068f2a22 Michael Niedermayer
        assert(size);
393
394 77eb5504 Anton Khirnov
        avio_w8(pb, FLV_TAG_TYPE_AUDIO);
395 75293f05 Michael Niedermayer
    }
396
397 7d637efa Baptiste Coudurier
    if (enc->codec_id == CODEC_ID_H264) {
398 71e685b0 Baptiste Coudurier
        /* check if extradata looks like mp4 formated */
399
        if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
400
            if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
401
                return -1;
402
        }
403 6f4eb12d Baptiste Coudurier
        if (!flv->delay && pkt->dts < 0)
404
            flv->delay = -pkt->dts;
405 f23496b5 Baptiste Coudurier
    }
406
407
    ts = pkt->dts + flv->delay; // add delay to force positive dts
408 b9f9e59a Stefano Sabatini
    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
409 df4f1d51 Thierry Foucu
        if (flv->last_video_ts < ts)
410
            flv->last_video_ts = ts;
411
    }
412 77eb5504 Anton Khirnov
    avio_wb24(pb,size + flags_size);
413
    avio_wb24(pb,ts);
414
    avio_w8(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_
415
    avio_wb24(pb,flv->reserved);
416
    avio_w8(pb,flags);
417 09d8c0ae Benjamin Larsson
    if (enc->codec_id == CODEC_ID_VP6)
418 77eb5504 Anton Khirnov
        avio_w8(pb,0);
419 09d8c0ae Benjamin Larsson
    if (enc->codec_id == CODEC_ID_VP6F)
420 77eb5504 Anton Khirnov
        avio_w8(pb, enc->extradata_size ? enc->extradata[0] : 0);
421 f23496b5 Baptiste Coudurier
    else if (enc->codec_id == CODEC_ID_AAC)
422 77eb5504 Anton Khirnov
        avio_w8(pb,1); // AAC raw
423 f23496b5 Baptiste Coudurier
    else if (enc->codec_id == CODEC_ID_H264) {
424 77eb5504 Anton Khirnov
        avio_w8(pb,1); // AVC NALU
425
        avio_wb24(pb,pkt->pts - pkt->dts);
426 f23496b5 Baptiste Coudurier
    }
427 71e685b0 Baptiste Coudurier
428 77eb5504 Anton Khirnov
    avio_write(pb, data ? data : pkt->data, size);
429 71e685b0 Baptiste Coudurier
430 77eb5504 Anton Khirnov
    avio_wb32(pb,size+flags_size+11); // previous tag size
431 f23496b5 Baptiste Coudurier
    flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
432 115329f1 Diego Biurrun
433 b7f2fdde Anton Khirnov
    avio_flush(pb);
434 71e685b0 Baptiste Coudurier
435
    av_free(data);
436
437 d4f5d74a Garrick Meeker
    return 0;
438
}
439
440 c6610a21 Diego Elio Pettenò
AVOutputFormat ff_flv_muxer = {
441 d4f5d74a Garrick Meeker
    "flv",
442 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("FLV format"),
443 e817a73d Michael Niedermayer
    "video/x-flv",
444 d4f5d74a Garrick Meeker
    "flv",
445
    sizeof(FLVContext),
446 b250f9c6 Aurelien Jacobs
#if CONFIG_LIBMP3LAME
447 80783dc2 Fabrice Bellard
    CODEC_ID_MP3,
448 6ebe07fb Diego Biurrun
#else // CONFIG_LIBMP3LAME
449 964ff354 Alex Beregszaszi
    CODEC_ID_ADPCM_SWF,
450 6ebe07fb Diego Biurrun
#endif // CONFIG_LIBMP3LAME
451 d4f5d74a Garrick Meeker
    CODEC_ID_FLV1,
452
    flv_write_header,
453
    flv_write_packet,
454
    flv_write_trailer,
455 c1854592 Reimar Döffinger
    .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
456 e458dd0b Michael Niedermayer
    .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
457 d4f5d74a Garrick Meeker
};