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