X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fflvenc.c;h=0d1fa7312ad0e3fc1c099cb09e1d4edc22dbbf10;hb=41ac093f7e315e3af17612f580c387b3688f4f43;hp=4d49a966e9216cb54bf32a788fdeca756953c7f1;hpb=bbc413f943b53d96aa85fe272ec413c85794019f;p=ffmpeg diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c index 4d49a966e92..0d1fa7312ad 100644 --- a/libavformat/flvenc.c +++ b/libavformat/flvenc.c @@ -1,28 +1,31 @@ /* * FLV muxer - * Copyright (c) 2003 The FFmpeg Project + * Copyright (c) 2003 The Libav Project * - * This file is part of FFmpeg. + * This file is part of Libav. * - * FFmpeg is free software; you can redistribute it and/or + * Libav is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * FFmpeg is distributed in the hope that it will be useful, + * Libav is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software + * License along with Libav; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + +#include "libavutil/intfloat_readwrite.h" #include "avformat.h" #include "flv.h" #include "internal.h" #include "avc.h" #include "metadata.h" +#include "libavutil/dict.h" #undef NDEBUG #include @@ -54,10 +57,13 @@ typedef struct FLVContext { int64_t duration_offset; int64_t filesize_offset; int64_t duration; - int delay; ///< first dts delay for AVC - int64_t last_video_ts; } FLVContext; +typedef struct FLVStreamContext { + int delay; ///< first dts delay for each stream (needed for AVC & Speex) + int64_t last_ts; ///< last timestamp for each stream +} FLVStreamContext; + static int get_audio_flags(AVCodecContext *enc){ int flags = (enc->bits_per_coded_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT; @@ -72,11 +78,6 @@ static int get_audio_flags(AVCodecContext *enc){ av_log(enc, AV_LOG_ERROR, "flv only supports mono Speex audio\n"); return -1; } - if (enc->frame_size / 320 > 8) { - av_log(enc, AV_LOG_WARNING, "Warning: Speex stream has more than " - "8 frames per packet. Adobe Flash " - "Player cannot handle this!\n"); - } return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | FLV_SAMPLESSIZE_16BIT; } else { switch (enc->sample_rate) { @@ -174,13 +175,14 @@ static int flv_write_header(AVFormatContext *s) AVIOContext *pb = s->pb; FLVContext *flv = s->priv_data; AVCodecContext *audio_enc = NULL, *video_enc = NULL; - int i; + int i, metadata_count = 0; double framerate = 0.0; - int metadata_size_pos, data_size; - AVMetadataTag *tag = NULL; + int64_t metadata_size_pos, data_size, metadata_count_pos; + AVDictionaryEntry *tag = NULL; for(i=0; inb_streams; i++){ AVCodecContext *enc = s->streams[i]->codec; + FLVStreamContext *sc; if (enc->codec_type == AVMEDIA_TYPE_VIDEO) { if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) { framerate = av_q2d(s->streams[i]->r_frame_rate); @@ -198,6 +200,12 @@ static int flv_write_header(AVFormatContext *s) return -1; } av_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */ + + sc = av_mallocz(sizeof(FLVStreamContext)); + if (!sc) + return AVERROR(ENOMEM); + s->streams[i]->priv_data = sc; + sc->last_ts = -1; } avio_write(pb, "FLV", 3); avio_w8(pb,1); @@ -217,11 +225,9 @@ static int flv_write_header(AVFormatContext *s) } } - flv->last_video_ts = -1; - /* write meta_tag */ avio_w8(pb, 18); // tag type META - metadata_size_pos= url_ftell(pb); + metadata_size_pos= avio_tell(pb); avio_wb24(pb, 0); // size of data part (sum of all parts below) avio_wb24(pb, 0); // time stamp avio_wb32(pb, 0); // reserved @@ -234,10 +240,12 @@ static int flv_write_header(AVFormatContext *s) /* mixed array (hash) with size and string/type/data tuples */ avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY); - avio_wb32(pb, 5*!!video_enc + 5*!!audio_enc + 2); // +2 for duration and file size + metadata_count_pos = avio_tell(pb); + metadata_count = 5*!!video_enc + 5*!!audio_enc + 2; // +2 for duration and file size + avio_wb32(pb, metadata_count); put_amf_string(pb, "duration"); - flv->duration_offset= url_ftell(pb); + flv->duration_offset= avio_tell(pb); put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed duration, it'll be corrected later if incorrect if(video_enc){ @@ -274,24 +282,29 @@ static int flv_write_header(AVFormatContext *s) put_amf_double(pb, audio_enc->codec_tag); } - while ((tag = av_metadata_get(s->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX))) { + while ((tag = av_dict_get(s->metadata, "", tag, AV_DICT_IGNORE_SUFFIX))) { put_amf_string(pb, tag->key); avio_w8(pb, AMF_DATA_TYPE_STRING); put_amf_string(pb, tag->value); + metadata_count++; } put_amf_string(pb, "filesize"); - flv->filesize_offset= url_ftell(pb); + flv->filesize_offset= avio_tell(pb); put_amf_double(pb, 0); // delayed write put_amf_string(pb, ""); avio_w8(pb, AMF_END_OF_OBJECT); /* write total size of tag */ - data_size= url_ftell(pb) - metadata_size_pos - 10; - url_fseek(pb, metadata_size_pos, SEEK_SET); + data_size= avio_tell(pb) - metadata_size_pos - 10; + + avio_seek(pb, metadata_count_pos, SEEK_SET); + avio_wb32(pb, metadata_count); + + avio_seek(pb, metadata_size_pos, SEEK_SET); avio_wb24(pb, data_size); - url_fseek(pb, data_size + 10 - 3, SEEK_CUR); + avio_skip(pb, data_size + 10 - 3); avio_wb32(pb, data_size + 11); for (i = 0; i < s->nb_streams; i++) { @@ -304,7 +317,7 @@ static int flv_write_header(AVFormatContext *s) avio_wb24(pb, 0); // ts avio_w8(pb, 0); // ts ext avio_wb24(pb, 0); // streamid - pos = url_ftell(pb); + pos = avio_tell(pb); if (enc->codec_id == CODEC_ID_AAC) { avio_w8(pb, get_audio_flags(enc)); avio_w8(pb, 0); // AAC sequence header @@ -315,10 +328,10 @@ static int flv_write_header(AVFormatContext *s) avio_wb24(pb, 0); // composition time ff_isom_write_avcc(pb, enc->extradata, enc->extradata_size); } - data_size = url_ftell(pb) - pos; - url_fseek(pb, -data_size - 10, SEEK_CUR); + data_size = avio_tell(pb) - pos; + avio_seek(pb, -data_size - 10, SEEK_CUR); avio_wb24(pb, data_size); - url_fseek(pb, data_size + 10 - 3, SEEK_CUR); + avio_skip(pb, data_size + 10 - 3); avio_wb32(pb, data_size + 11); // previous tag size } } @@ -337,21 +350,22 @@ static int flv_write_trailer(AVFormatContext *s) /* Add EOS tag */ for (i = 0; i < s->nb_streams; i++) { AVCodecContext *enc = s->streams[i]->codec; + FLVStreamContext *sc = s->streams[i]->priv_data; if (enc->codec_type == AVMEDIA_TYPE_VIDEO && enc->codec_id == CODEC_ID_H264) { - put_avc_eos_tag(pb, flv->last_video_ts); + put_avc_eos_tag(pb, sc->last_ts); } } - file_size = url_ftell(pb); + file_size = avio_tell(pb); /* update informations */ - url_fseek(pb, flv->duration_offset, SEEK_SET); + avio_seek(pb, flv->duration_offset, SEEK_SET); put_amf_double(pb, flv->duration / (double)1000); - url_fseek(pb, flv->filesize_offset, SEEK_SET); + avio_seek(pb, flv->filesize_offset, SEEK_SET); put_amf_double(pb, file_size); - url_fseek(pb, file_size, SEEK_SET); + avio_seek(pb, file_size, SEEK_SET); return 0; } @@ -360,6 +374,7 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) AVIOContext *pb = s->pb; AVCodecContext *enc = s->streams[pkt->stream_index]->codec; FLVContext *flv = s->priv_data; + FLVStreamContext *sc = s->streams[pkt->stream_index]->priv_data; unsigned ts; int size= pkt->size; uint8_t *data= NULL; @@ -400,15 +415,22 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0) return -1; } - if (!flv->delay && pkt->dts < 0) - flv->delay = -pkt->dts; } + if (!sc->delay && pkt->dts < 0) + sc->delay = -pkt->dts; - ts = pkt->dts + flv->delay; // add delay to force positive dts - if (enc->codec_type == AVMEDIA_TYPE_VIDEO) { - if (flv->last_video_ts < ts) - flv->last_video_ts = ts; + ts = pkt->dts + sc->delay; // add delay to force positive dts + + /* check Speex packet duration */ + if (enc->codec_id == CODEC_ID_SPEEX && ts - sc->last_ts > 160) { + av_log(s, AV_LOG_WARNING, "Warning: Speex stream has more than " + "8 frames per packet. Adobe Flash " + "Player cannot handle this!\n"); } + + if (sc->last_ts < ts) + sc->last_ts = ts; + avio_wb24(pb,size + flags_size); avio_wb24(pb,ts); avio_w8(pb,(ts >> 24) & 0x7F); // timestamps are 32bits _signed_ @@ -428,30 +450,30 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) avio_write(pb, data ? data : pkt->data, size); avio_wb32(pb,size+flags_size+11); // previous tag size - flv->duration = FFMAX(flv->duration, pkt->pts + flv->delay + pkt->duration); + flv->duration = FFMAX(flv->duration, pkt->pts + sc->delay + pkt->duration); - put_flush_packet(pb); + avio_flush(pb); av_free(data); - return 0; + return pb->error; } AVOutputFormat ff_flv_muxer = { - "flv", - NULL_IF_CONFIG_SMALL("FLV format"), - "video/x-flv", - "flv", - sizeof(FLVContext), + .name = "flv", + .long_name = NULL_IF_CONFIG_SMALL("FLV format"), + .mime_type = "video/x-flv", + .extensions = "flv", + .priv_data_size = sizeof(FLVContext), #if CONFIG_LIBMP3LAME - CODEC_ID_MP3, + .audio_codec = CODEC_ID_MP3, #else // CONFIG_LIBMP3LAME - CODEC_ID_ADPCM_SWF, + .audio_codec = CODEC_ID_ADPCM_SWF, #endif // CONFIG_LIBMP3LAME - CODEC_ID_FLV1, - flv_write_header, - flv_write_packet, - flv_write_trailer, + .video_codec = CODEC_ID_FLV1, + .write_header = flv_write_header, + .write_packet = flv_write_packet, + .write_trailer = flv_write_trailer, .codec_tag= (const AVCodecTag* const []){flv_video_codec_ids, flv_audio_codec_ids, 0}, .flags= AVFMT_GLOBALHEADER | AVFMT_VARIABLE_FPS, };