|
|
|
@ -57,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_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; |
|
|
|
|
|
|
|
|
@ -179,6 +182,7 @@ static int flv_write_header(AVFormatContext *s) |
|
|
|
|
|
|
|
|
|
for(i=0; i<s->nb_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); |
|
|
|
@ -196,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); |
|
|
|
@ -215,8 +225,6 @@ static int flv_write_header(AVFormatContext *s) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
flv->last_ts = -1; |
|
|
|
|
|
|
|
|
|
/* write meta_tag */ |
|
|
|
|
avio_w8(pb, 18); // tag type META
|
|
|
|
|
metadata_size_pos= avio_tell(pb); |
|
|
|
@ -342,9 +350,10 @@ 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_ts); |
|
|
|
|
put_avc_eos_tag(pb, sc->last_ts); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -365,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; |
|
|
|
@ -406,20 +416,20 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) |
|
|
|
|
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
|
|
|
|
|
ts = pkt->dts + sc->delay; // add delay to force positive dts
|
|
|
|
|
|
|
|
|
|
/* check Speex packet duration */ |
|
|
|
|
if (enc->codec_id == CODEC_ID_SPEEX && ts - flv->last_ts > 160) { |
|
|
|
|
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 (flv->last_ts < ts) |
|
|
|
|
flv->last_ts = ts; |
|
|
|
|
if (sc->last_ts < ts) |
|
|
|
|
sc->last_ts = ts; |
|
|
|
|
|
|
|
|
|
avio_wb24(pb,size + flags_size); |
|
|
|
|
avio_wb24(pb,ts); |
|
|
|
@ -440,7 +450,7 @@ 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); |
|
|
|
|
|
|
|
|
|
avio_flush(pb); |
|
|
|
|
|
|
|
|
|