00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 #include "flv.h"
00025 #include "internal.h"
00026 #include "avc.h"
00027 #include "metadata.h"
00028 #include "libavutil/dict.h"
00029
00030 #undef NDEBUG
00031 #include <assert.h>
00032
00033 static const AVCodecTag flv_video_codec_ids[] = {
00034 {CODEC_ID_FLV1, FLV_CODECID_H263 },
00035 {CODEC_ID_FLASHSV, FLV_CODECID_SCREEN},
00036 {CODEC_ID_FLASHSV2, FLV_CODECID_SCREEN2},
00037 {CODEC_ID_VP6F, FLV_CODECID_VP6 },
00038 {CODEC_ID_VP6, FLV_CODECID_VP6 },
00039 {CODEC_ID_H264, FLV_CODECID_H264 },
00040 {CODEC_ID_NONE, 0}
00041 };
00042
00043 static const AVCodecTag flv_audio_codec_ids[] = {
00044 {CODEC_ID_MP3, FLV_CODECID_MP3 >> FLV_AUDIO_CODECID_OFFSET},
00045 {CODEC_ID_PCM_U8, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
00046 {CODEC_ID_PCM_S16BE, FLV_CODECID_PCM >> FLV_AUDIO_CODECID_OFFSET},
00047 {CODEC_ID_PCM_S16LE, FLV_CODECID_PCM_LE >> FLV_AUDIO_CODECID_OFFSET},
00048 {CODEC_ID_ADPCM_SWF, FLV_CODECID_ADPCM >> FLV_AUDIO_CODECID_OFFSET},
00049 {CODEC_ID_AAC, FLV_CODECID_AAC >> FLV_AUDIO_CODECID_OFFSET},
00050 {CODEC_ID_NELLYMOSER, FLV_CODECID_NELLYMOSER >> FLV_AUDIO_CODECID_OFFSET},
00051 {CODEC_ID_SPEEX, FLV_CODECID_SPEEX >> FLV_AUDIO_CODECID_OFFSET},
00052 {CODEC_ID_NONE, 0}
00053 };
00054
00055 typedef struct FLVContext {
00056 int reserved;
00057 int64_t duration_offset;
00058 int64_t filesize_offset;
00059 int64_t duration;
00060 int delay;
00061 int64_t last_video_ts;
00062 } FLVContext;
00063
00064 static int get_audio_flags(AVCodecContext *enc){
00065 int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
00066
00067 if (enc->codec_id == CODEC_ID_AAC)
00068 return FLV_CODECID_AAC | FLV_SAMPLERATE_44100HZ | FLV_SAMPLESSIZE_16BIT | FLV_STEREO;
00069 else if (enc->codec_id == CODEC_ID_SPEEX) {
00070 if (enc->sample_rate != 16000) {
00071 av_log(enc, AV_LOG_ERROR, "flv only supports wideband (16kHz) Speex audio\n");
00072 return -1;
00073 }
00074 if (enc->channels != 1) {
00075 av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n");
00076 return -1;
00077 }
00078 if (enc->frame_size / 320 > 8) {
00079 av_log(enc, AV_LOG_WARNING, "Warning: Speex stream has more than "
00080 "8 frames per packet. Adobe Flash "
00081 "Player cannot handle this!\n");
00082 }
00083 return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT;
00084 } else {
00085 switch (enc->sample_rate) {
00086 case 44100:
00087 flags |= FLV_SAMPLERATE_44100HZ;
00088 break;
00089 case 22050:
00090 flags |= FLV_SAMPLERATE_22050HZ;
00091 break;
00092 case 11025:
00093 flags |= FLV_SAMPLERATE_11025HZ;
00094 break;
00095 case 8000:
00096 case 5512:
00097 if(enc->codec_id != CODEC_ID_MP3){
00098 flags |= FLV_SAMPLERATE_SPECIAL;
00099 break;
00100 }
00101 default:
00102 av_log(enc, AV_LOG_ERROR, "flv does not support that sample rate, choose from (44100, 22050, 11025).\n");
00103 return -1;
00104 }
00105 }
00106
00107 if (enc->channels > 1) {
00108 flags |= FLV_STEREO;
00109 }
00110
00111 switch(enc->codec_id){
00112 case CODEC_ID_MP3:
00113 flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
00114 break;
00115 case CODEC_ID_PCM_U8:
00116 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_8BIT;
00117 break;
00118 case CODEC_ID_PCM_S16BE:
00119 flags |= FLV_CODECID_PCM | FLV_SAMPLESSIZE_16BIT;
00120 break;
00121 case CODEC_ID_PCM_S16LE:
00122 flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
00123 break;
00124 case CODEC_ID_ADPCM_SWF:
00125 flags |= FLV_CODECID_ADPCM | FLV_SAMPLESSIZE_16BIT;
00126 break;
00127 case CODEC_ID_NELLYMOSER:
00128 if (enc->sample_rate == 8000) {
00129 flags |= FLV_CODECID_NELLYMOSER_8KHZ_MONO | FLV_SAMPLESSIZE_16BIT;
00130 } else {
00131 flags |= FLV_CODECID_NELLYMOSER | FLV_SAMPLESSIZE_16BIT;
00132 }
00133 break;
00134 case 0:
00135 flags |= enc->codec_tag<<4;
00136 break;
00137 default:
00138 av_log(enc, AV_LOG_ERROR, "codec not compatible with flv\n");
00139 return -1;
00140 }
00141
00142 return flags;
00143 }
00144
00145 static void put_amf_string(AVIOContext *pb, const char *str)
00146 {
00147 size_t len = strlen(str);
00148 avio_wb16(pb, len);
00149 avio_write(pb, str, len);
00150 }
00151
00152 static void put_avc_eos_tag(AVIOContext *pb, unsigned ts) {
00153 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
00154 avio_wb24(pb, 5);
00155 avio_wb24(pb, ts);
00156 avio_w8(pb, (ts >> 24) & 0x7F);
00157 avio_wb24(pb, 0);
00158 avio_w8(pb, 23);
00159 avio_w8(pb, 2);
00160 avio_wb24(pb, 0);
00161 avio_wb32(pb, 16);
00162 }
00163
00164 static void put_amf_double(AVIOContext *pb, double d)
00165 {
00166 avio_w8(pb, AMF_DATA_TYPE_NUMBER);
00167 avio_wb64(pb, av_dbl2int(d));
00168 }
00169
00170 static void put_amf_bool(AVIOContext *pb, int b) {
00171 avio_w8(pb, AMF_DATA_TYPE_BOOL);
00172 avio_w8(pb, !!b);
00173 }
00174
00175 static int flv_write_header(AVFormatContext *s)
00176 {
00177 AVIOContext *pb = s->pb;
00178 FLVContext *flv = s->priv_data;
00179 AVCodecContext *audio_enc = NULL, *video_enc = NULL;
00180 int i;
00181 double framerate = 0.0;
00182 int64_t metadata_size_pos, data_size;
00183 AVDictionaryEntry *tag = NULL;
00184
00185 for(i=0; i<s->nb_streams; i++){
00186 AVCodecContext *enc = s->streams[i]->codec;
00187 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
00188 if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
00189 framerate = av_q2d(s->streams[i]->r_frame_rate);
00190 } else {
00191 framerate = 1/av_q2d(s->streams[i]->codec->time_base);
00192 }
00193 video_enc = enc;
00194 if(enc->codec_tag == 0) {
00195 av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
00196 return -1;
00197 }
00198 } else {
00199 audio_enc = enc;
00200 if(get_audio_flags(enc)<0)
00201 return -1;
00202 }
00203 av_set_pts_info(s->streams[i], 32, 1, 1000);
00204 }
00205 avio_write(pb, "FLV", 3);
00206 avio_w8(pb,1);
00207 avio_w8(pb, FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
00208 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
00209 avio_wb32(pb,9);
00210 avio_wb32(pb,0);
00211
00212 for(i=0; i<s->nb_streams; i++){
00213 if(s->streams[i]->codec->codec_tag == 5){
00214 avio_w8(pb,8);
00215 avio_wb24(pb,0);
00216 avio_wb24(pb,0);
00217 avio_wb32(pb,0);
00218 avio_wb32(pb,11);
00219 flv->reserved=5;
00220 }
00221 }
00222
00223 flv->last_video_ts = -1;
00224
00225
00226 avio_w8(pb, 18);
00227 metadata_size_pos= avio_tell(pb);
00228 avio_wb24(pb, 0);
00229 avio_wb24(pb, 0);
00230 avio_wb32(pb, 0);
00231
00232
00233
00234
00235 avio_w8(pb, AMF_DATA_TYPE_STRING);
00236 put_amf_string(pb, "onMetaData");
00237
00238
00239 avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);
00240 avio_wb32(pb, 5*!!video_enc + 5*!!audio_enc + 2);
00241
00242 put_amf_string(pb, "duration");
00243 flv->duration_offset= avio_tell(pb);
00244 put_amf_double(pb, s->duration / AV_TIME_BASE);
00245
00246 if(video_enc){
00247 put_amf_string(pb, "width");
00248 put_amf_double(pb, video_enc->width);
00249
00250 put_amf_string(pb, "height");
00251 put_amf_double(pb, video_enc->height);
00252
00253 put_amf_string(pb, "videodatarate");
00254 put_amf_double(pb, video_enc->bit_rate / 1024.0);
00255
00256 put_amf_string(pb, "framerate");
00257 put_amf_double(pb, framerate);
00258
00259 put_amf_string(pb, "videocodecid");
00260 put_amf_double(pb, video_enc->codec_tag);
00261 }
00262
00263 if(audio_enc){
00264 put_amf_string(pb, "audiodatarate");
00265 put_amf_double(pb, audio_enc->bit_rate / 1024.0);
00266
00267 put_amf_string(pb, "audiosamplerate");
00268 put_amf_double(pb, audio_enc->sample_rate);
00269
00270 put_amf_string(pb, "audiosamplesize");
00271 put_amf_double(pb, audio_enc->codec_id == CODEC_ID_PCM_U8 ? 8 : 16);
00272
00273 put_amf_string(pb, "stereo");
00274 put_amf_bool(pb, audio_enc->channels == 2);
00275
00276 put_amf_string(pb, "audiocodecid");
00277 put_amf_double(pb, audio_enc->codec_tag);
00278 }
00279
00280 while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) {
00281 put_amf_string(pb, tag->key);
00282 avio_w8(pb, AMF_DATA_TYPE_STRING);
00283 put_amf_string(pb, tag->value);
00284 }
00285
00286 put_amf_string(pb, "filesize");
00287 flv->filesize_offset= avio_tell(pb);
00288 put_amf_double(pb, 0);
00289
00290 put_amf_string(pb, "");
00291 avio_w8(pb, AMF_END_OF_OBJECT);
00292
00293
00294 data_size= avio_tell(pb) - metadata_size_pos - 10;
00295 avio_seek(pb, metadata_size_pos, SEEK_SET);
00296 avio_wb24(pb, data_size);
00297 avio_skip(pb, data_size + 10 - 3);
00298 avio_wb32(pb, data_size + 11);
00299
00300 for (i = 0; i < s->nb_streams; i++) {
00301 AVCodecContext *enc = s->streams[i]->codec;
00302 if (enc->codec_id == CODEC_ID_AAC || enc->codec_id == CODEC_ID_H264) {
00303 int64_t pos;
00304 avio_w8(pb, enc->codec_type == AVMEDIA_TYPE_VIDEO ?
00305 FLV_TAG_TYPE_VIDEO : FLV_TAG_TYPE_AUDIO);
00306 avio_wb24(pb, 0);
00307 avio_wb24(pb, 0);
00308 avio_w8(pb, 0);
00309 avio_wb24(pb, 0);
00310 pos = avio_tell(pb);
00311 if (enc->codec_id == CODEC_ID_AAC) {
00312 avio_w8(pb, get_audio_flags(enc));
00313 avio_w8(pb, 0);
00314 avio_write(pb, enc->extradata, enc->extradata_size);
00315 } else {
00316 avio_w8(pb, enc->codec_tag | FLV_FRAME_KEY);
00317 avio_w8(pb, 0);
00318 avio_wb24(pb, 0);
00319 ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size);
00320 }
00321 data_size = avio_tell(pb) - pos;
00322 avio_seek(pb, -data_size - 10, SEEK_CUR);
00323 avio_wb24(pb, data_size);
00324 avio_skip(pb, data_size + 10 - 3);
00325 avio_wb32(pb, data_size + 11);
00326 }
00327 }
00328
00329 return 0;
00330 }
00331
00332 static int flv_write_trailer(AVFormatContext *s)
00333 {
00334 int64_t file_size;
00335
00336 AVIOContext *pb = s->pb;
00337 FLVContext *flv = s->priv_data;
00338 int i;
00339
00340
00341 for (i = 0; i < s->nb_streams; i++) {
00342 AVCodecContext *enc = s->streams[i]->codec;
00343 if (enc->codec_type == AVMEDIA_TYPE_VIDEO &&
00344 enc->codec_id == CODEC_ID_H264) {
00345 put_avc_eos_tag(pb, flv->last_video_ts);
00346 }
00347 }
00348
00349 file_size = avio_tell(pb);
00350
00351
00352 avio_seek(pb, flv->duration_offset, SEEK_SET);
00353 put_amf_double(pb, flv->duration / (double)1000);
00354 avio_seek(pb, flv->filesize_offset, SEEK_SET);
00355 put_amf_double(pb, file_size);
00356
00357 avio_seek(pb, file_size, SEEK_SET);
00358 return 0;
00359 }
00360
00361 static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
00362 {
00363 AVIOContext *pb = s->pb;
00364 AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
00365 FLVContext *flv = s->priv_data;
00366 unsigned ts;
00367 int size= pkt->size;
00368 uint8_t *data= NULL;
00369 int flags, flags_size;
00370
00371
00372
00373 if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
00374 enc->codec_id == CODEC_ID_AAC)
00375 flags_size= 2;
00376 else if(enc->codec_id == CODEC_ID_H264)
00377 flags_size= 5;
00378 else
00379 flags_size= 1;
00380
00381 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
00382 avio_w8(pb, FLV_TAG_TYPE_VIDEO);
00383
00384 flags = enc->codec_tag;
00385 if(flags == 0) {
00386 av_log(enc, AV_LOG_ERROR, "video codec %X not compatible with flv\n",enc->codec_id);
00387 return -1;
00388 }
00389
00390 flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
00391 } else {
00392 assert(enc->codec_type == AVMEDIA_TYPE_AUDIO);
00393 flags = get_audio_flags(enc);
00394
00395 assert(size);
00396
00397 avio_w8(pb, FLV_TAG_TYPE_AUDIO);
00398 }
00399
00400 if (enc->codec_id == CODEC_ID_H264) {
00401
00402 if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1) {
00403 if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)
00404 return -1;
00405 }
00406 if (!flv->delay && pkt->dts < 0)
00407 flv->delay = -pkt->dts;
00408 } else if (enc->codec_id == CODEC_ID_AAC && pkt->size > 2 &&
00409 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
00410 av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
00411 return -1;
00412 }
00413
00414 ts = pkt->dts + flv->delay;
00415 if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
00416 if (flv->last_video_ts < ts)
00417 flv->last_video_ts = ts;
00418 }
00419 avio_wb24(pb,size + flags_size);
00420 avio_wb24(pb,ts);
00421 avio_w8(pb,(ts >> 24) & 0x7F);
00422 avio_wb24(pb,flv->reserved);
00423 avio_w8(pb,flags);
00424 if (enc->codec_id == CODEC_ID_VP6)
00425 avio_w8(pb,0);
00426 if (enc->codec_id == CODEC_ID_VP6F)
00427 avio_w8(pb, enc->extradata_size ? enc->extradata[0] : 0);
00428 else if (enc->codec_id == CODEC_ID_AAC)
00429 avio_w8(pb,1);
00430 else if (enc->codec_id == CODEC_ID_H264) {
00431 avio_w8(pb,1);
00432 avio_wb24(pb,pkt->pts - pkt->dts);
00433 }
00434
00435 avio_write(pb, data ? data : pkt->data, size);
00436
00437 avio_wb32(pb,size+flags_size+11);
00438 flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration);
00439
00440 avio_flush(pb);
00441
00442 av_free(data);
00443
00444 return pb->error;
00445 }
00446
00447 AVOutputFormat ff_flv_muxer = {
00448 "flv",
00449 NULL_IF_CONFIG_SMALL("FLV format"),
00450 "video/x-flv",
00451 "flv",
00452 sizeof(FLVContext),
00453 #if CONFIG_LIBMP3LAME
00454 CODEC_ID_MP3,
00455 #else
00456 CODEC_ID_ADPCM_SWF,
00457 #endif
00458 CODEC_ID_FLV1,
00459 flv_write_header,
00460 flv_write_packet,
00461 flv_write_trailer,
00462 .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0},
00463 .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS,
00464 };