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