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