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;

    int flags = 0, flags_size;



    if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A ||

        enc->codec_id == AV_CODEC_ID_AAC)

        flags_size = 2;

    else if (enc->codec_id == AV_CODEC_ID_H264)

        flags_size = 5;

    else

        flags_size = 1;



    if (flv->delay == AV_NOPTS_VALUE)

        flv->delay = -pkt->dts;



    if (pkt->dts < -flv->delay) {

        av_log(s, AV_LOG_WARNING,

               "Packets are not in the proper order with respect to DTS\n");

        return AVERROR(EINVAL);

    }



    ts = pkt->dts + flv->delay; // add delay to force positive dts



    if (s->event_flags & AVSTREAM_EVENT_FLAG_METADATA_UPDATED) {

        write_metadata(s, ts);

        s->event_flags &= ~AVSTREAM_EVENT_FLAG_METADATA_UPDATED;

    }



    switch (enc->codec_type) {

    case AVMEDIA_TYPE_VIDEO:

        avio_w8(pb, FLV_TAG_TYPE_VIDEO);



        flags = enc->codec_tag;

        if (flags == 0) {

            av_log(s, AV_LOG_ERROR,

                   "video codec %X not compatible with flv\n",

                   enc->codec_id);

            return -1;

        }



        flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;

        break;

    case AVMEDIA_TYPE_AUDIO:

        flags = get_audio_flags(s, enc);



        assert(size);



        avio_w8(pb, FLV_TAG_TYPE_AUDIO);

        break;

    case AVMEDIA_TYPE_DATA:

        avio_w8(pb, FLV_TAG_TYPE_META);

        break;

    default:

        return AVERROR(EINVAL);

    }



    if (enc->codec_id == AV_CODEC_ID_H264)

        /* check if extradata looks like MP4 */

        if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)

            if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)

                return -1;



    /* check Speex packet duration */

    if (enc->codec_id == AV_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 32 bits _signed_

    avio_wb24(pb, flv->reserved);



    if (enc->codec_type == AVMEDIA_TYPE_DATA) {

        int data_size;

        int64_t metadata_size_pos = avio_tell(pb);

        avio_w8(pb, AMF_DATA_TYPE_STRING);

        put_amf_string(pb, "onTextData");

        avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);

        avio_wb32(pb, 2);

        put_amf_string(pb, "type");

        avio_w8(pb, AMF_DATA_TYPE_STRING);

        put_amf_string(pb, "Text");

        put_amf_string(pb, "text");

        avio_w8(pb, AMF_DATA_TYPE_STRING);

        put_amf_string(pb, pkt->data);

        put_amf_string(pb, "");

        avio_w8(pb, AMF_END_OF_OBJECT);

        /* write total size of tag */

        data_size = avio_tell(pb) - metadata_size_pos;

        avio_seek(pb, metadata_size_pos - 10, SEEK_SET);

        avio_wb24(pb, data_size);

        avio_seek(pb, data_size + 10 - 3, SEEK_CUR);

        avio_wb32(pb, data_size + 11);

    } else {

        avio_w8(pb,flags);

        if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A) {

            if (enc->extradata_size)

                avio_w8(pb, enc->extradata[0]);

            else

                avio_w8(pb, ((FFALIGN(enc->width,  16) - enc->width) << 4) |

                             (FFALIGN(enc->height, 16) - enc->height));

        } else if (enc->codec_id == AV_CODEC_ID_AAC)

            avio_w8(pb, 1); // AAC raw

        else if (enc->codec_id == AV_CODEC_ID_H264) {

            avio_w8(pb, 1); // AVC NALU

            avio_wb24(pb, pkt->pts - pkt->dts);

        }



        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);

    }



    av_free(data);



    return pb->error;

}

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;

    int flags = 0, flags_size;



    if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A ||

        enc->codec_id == AV_CODEC_ID_AAC)

        flags_size = 2;

    else if (enc->codec_id == AV_CODEC_ID_H264)

        flags_size = 5;

    else

        flags_size = 1;



    if (flv->delay == AV_NOPTS_VALUE)

        flv->delay = -pkt->dts;



    if (pkt->dts < -flv->delay) {

        av_log(s, AV_LOG_WARNING,

               "Packets are not in the proper order with respect to DTS\n");

        return AVERROR(EINVAL);

    }



    ts = pkt->dts + flv->delay; 



    if (s->event_flags & AVSTREAM_EVENT_FLAG_METADATA_UPDATED) {

        write_metadata(s, ts);

        s->event_flags &= ~AVSTREAM_EVENT_FLAG_METADATA_UPDATED;

    }



    switch (enc->codec_type) {

    case AVMEDIA_TYPE_VIDEO:

        avio_w8(pb, FLV_TAG_TYPE_VIDEO);



        flags = enc->codec_tag;

        if (flags == 0) {

            av_log(s, AV_LOG_ERROR,

                   "video codec %X not compatible with flv\n",

                   enc->codec_id);

            return -1;

        }



        flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;

        break;

    case AVMEDIA_TYPE_AUDIO:

        flags = get_audio_flags(s, enc);



        assert(size);



        avio_w8(pb, FLV_TAG_TYPE_AUDIO);

        break;

    case AVMEDIA_TYPE_DATA:

        avio_w8(pb, FLV_TAG_TYPE_META);

        break;

    default:

        return AVERROR(EINVAL);

    }



    if (enc->codec_id == AV_CODEC_ID_H264)

        

        if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)

            if (ff_avc_parse_nal_units_buf(pkt->data, &data, &size) < 0)

                return -1;



    

    if (enc->codec_id == AV_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); 

    avio_wb24(pb, flv->reserved);



    if (enc->codec_type == AVMEDIA_TYPE_DATA) {

        int data_size;

        int64_t metadata_size_pos = avio_tell(pb);

        avio_w8(pb, AMF_DATA_TYPE_STRING);

        put_amf_string(pb, "onTextData");

        avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);

        avio_wb32(pb, 2);

        put_amf_string(pb, "type");

        avio_w8(pb, AMF_DATA_TYPE_STRING);

        put_amf_string(pb, "Text");

        put_amf_string(pb, "text");

        avio_w8(pb, AMF_DATA_TYPE_STRING);

        put_amf_string(pb, pkt->data);

        put_amf_string(pb, "");

        avio_w8(pb, AMF_END_OF_OBJECT);

        

        data_size = avio_tell(pb) - metadata_size_pos;

        avio_seek(pb, metadata_size_pos - 10, SEEK_SET);

        avio_wb24(pb, data_size);

        avio_seek(pb, data_size + 10 - 3, SEEK_CUR);

        avio_wb32(pb, data_size + 11);

    } else {

        avio_w8(pb,flags);

        if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A) {

            if (enc->extradata_size)

                avio_w8(pb, enc->extradata[0]);

            else

                avio_w8(pb, ((FFALIGN(enc->width,  16) - enc->width) << 4) |

                             (FFALIGN(enc->height, 16) - enc->height));

        } else if (enc->codec_id == AV_CODEC_ID_AAC)

            avio_w8(pb, 1); 

        else if (enc->codec_id == AV_CODEC_ID_H264) {

            avio_w8(pb, 1); 

            avio_wb24(pb, pkt->pts - pkt->dts);

        }



        avio_write(pb, data ? data : pkt->data, size);



        avio_wb32(pb, size + flags_size + 11); 

        flv->duration = FFMAX(flv->duration,

                              pkt->pts + flv->delay + pkt->duration);

    }



    av_free(data);



    return pb->error;

}

                return -1;         flags = enc->codec_tag;         if (flags == 0) {             av_log(s, AV_LOG_ERROR,                    "video codec %X not compatible with flv\n",                    enc->codec_id);             return -1;
static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)

{

    AVIOContext *pb      = VAR_0->pb;

    AVCodecContext *enc  = VAR_0->streams[VAR_1->stream_index]->codec;

    FLVContext *flv      = VAR_0->priv_data;

    FLVStreamContext *sc = VAR_0->streams[VAR_1->stream_index]->priv_data;

    unsigned VAR_2;

    int VAR_3 = VAR_1->VAR_3;

    uint8_t *data = NULL;

    int VAR_4 = 0, VAR_5;



    if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A ||

        enc->codec_id == AV_CODEC_ID_AAC)

        VAR_5 = 2;

    else if (enc->codec_id == AV_CODEC_ID_H264)

        VAR_5 = 5;

    else

        VAR_5 = 1;



    if (flv->delay == AV_NOPTS_VALUE)

        flv->delay = -VAR_1->dts;



    if (VAR_1->dts < -flv->delay) {

        av_log(VAR_0, AV_LOG_WARNING,

               "Packets are not in the proper order with respect to DTS\n");

        return AVERROR(EINVAL);

    }



    VAR_2 = VAR_1->dts + flv->delay; 



    if (VAR_0->event_flags & AVSTREAM_EVENT_FLAG_METADATA_UPDATED) {

        write_metadata(VAR_0, VAR_2);

        VAR_0->event_flags &= ~AVSTREAM_EVENT_FLAG_METADATA_UPDATED;

    }



    switch (enc->codec_type) {

    case AVMEDIA_TYPE_VIDEO:

        avio_w8(pb, FLV_TAG_TYPE_VIDEO);



        VAR_4 = enc->codec_tag;

        if (VAR_4 == 0) {

            av_log(VAR_0, AV_LOG_ERROR,

                   "video codec %X not compatible with flv\n",

                   enc->codec_id);

            return -1;

        }



        VAR_4 |= VAR_1->VAR_4 & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;

        break;

    case AVMEDIA_TYPE_AUDIO:

        VAR_4 = get_audio_flags(VAR_0, enc);



        assert(VAR_3);



        avio_w8(pb, FLV_TAG_TYPE_AUDIO);

        break;

    case AVMEDIA_TYPE_DATA:

        avio_w8(pb, FLV_TAG_TYPE_META);

        break;

    default:

        return AVERROR(EINVAL);

    }



    if (enc->codec_id == AV_CODEC_ID_H264)

        

        if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)

            if (ff_avc_parse_nal_units_buf(VAR_1->data, &data, &VAR_3) < 0)

                return -1;



    

    if (enc->codec_id == AV_CODEC_ID_SPEEX && VAR_2 - sc->last_ts > 160)

        av_log(VAR_0, AV_LOG_WARNING, "Warning: Speex stream has more than "

                                  "8 frames per packet. Adobe Flash "

                                  "Player cannot handle this!\n");



    if (sc->last_ts < VAR_2)

        sc->last_ts = VAR_2;



    avio_wb24(pb, VAR_3 + VAR_5);

    avio_wb24(pb, VAR_2);

    avio_w8(pb, (VAR_2 >> 24) & 0x7F); 

    avio_wb24(pb, flv->reserved);



    if (enc->codec_type == AVMEDIA_TYPE_DATA) {

        int VAR_6;

        int64_t metadata_size_pos = avio_tell(pb);

        avio_w8(pb, AMF_DATA_TYPE_STRING);

        put_amf_string(pb, "onTextData");

        avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY);

        avio_wb32(pb, 2);

        put_amf_string(pb, "type");

        avio_w8(pb, AMF_DATA_TYPE_STRING);

        put_amf_string(pb, "Text");

        put_amf_string(pb, "text");

        avio_w8(pb, AMF_DATA_TYPE_STRING);

        put_amf_string(pb, VAR_1->data);

        put_amf_string(pb, "");

        avio_w8(pb, AMF_END_OF_OBJECT);

        

        VAR_6 = avio_tell(pb) - metadata_size_pos;

        avio_seek(pb, metadata_size_pos - 10, SEEK_SET);

        avio_wb24(pb, VAR_6);

        avio_seek(pb, VAR_6 + 10 - 3, SEEK_CUR);

        avio_wb32(pb, VAR_6 + 11);

    } else {

        avio_w8(pb,VAR_4);

        if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A) {

            if (enc->extradata_size)

                avio_w8(pb, enc->extradata[0]);

            else

                avio_w8(pb, ((FFALIGN(enc->width,  16) - enc->width) << 4) |

                             (FFALIGN(enc->height, 16) - enc->height));

        } else if (enc->codec_id == AV_CODEC_ID_AAC)

            avio_w8(pb, 1); 

        else if (enc->codec_id == AV_CODEC_ID_H264) {

            avio_w8(pb, 1); 

            avio_wb24(pb, VAR_1->pts - VAR_1->dts);

        }



        avio_write(pb, data ? data : VAR_1->data, VAR_3);



        avio_wb32(pb, VAR_3 + VAR_5 + 11); 

        flv->duration = FFMAX(flv->duration,

                              VAR_1->pts + flv->delay + VAR_1->duration);

    }



    av_free(data);



    return pb->error;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)
{ AVIOContext *pb      = VAR_0->pb; AVCodecContext *enc  = VAR_0->streams[VAR_1->stream_index]->codec; FLVContext *flv      = VAR_0->priv_data; FLVStreamContext *sc = VAR_0->streams[VAR_1->stream_index]->priv_data; unsigned VAR_2; int VAR_3 = VAR_1->VAR_3; uint8_t *data = NULL; int VAR_4 = 0, VAR_5; if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A ||
enc->codec_id == AV_CODEC_ID_AAC)
VAR_5 = 2; else if (enc->codec_id == AV_CODEC_ID_H264)
VAR_5 = 5; else
VAR_5 = 1; if (flv->delay == AV_NOPTS_VALUE)
flv->delay = -VAR_1->dts; if (VAR_1->dts < -flv->delay) { av_log(VAR_0, AV_LOG_WARNING,
"Packets are not in the proper order with respect to DTS\n"); return AVERROR(EINVAL); } VAR_2 = VAR_1->dts + flv->delay; if (VAR_0->event_flags & AVSTREAM_EVENT_FLAG_METADATA_UPDATED) { write_metadata(VAR_0, VAR_2); VAR_0->event_flags &= ~AVSTREAM_EVENT_FLAG_METADATA_UPDATED; } switch (enc->codec_type) { case AVMEDIA_TYPE_VIDEO:
avio_w8(pb, FLV_TAG_TYPE_VIDEO); VAR_4 = enc->codec_tag; if (VAR_4 == 0) { av_log(VAR_0, AV_LOG_ERROR,
"video codec %X not compatible with flv\n",
enc->codec_id); return -1; } VAR_4 |= VAR_1->VAR_4 & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER; break; case AVMEDIA_TYPE_AUDIO:
VAR_4 = get_audio_flags(VAR_0, enc); assert(VAR_3); avio_w8(pb, FLV_TAG_TYPE_AUDIO); break; case AVMEDIA_TYPE_DATA:
avio_w8(pb, FLV_TAG_TYPE_META); break; default:
return AVERROR(EINVAL); } if (enc->codec_id == AV_CODEC_ID_H264)
if (enc->extradata_size > 0 && *(uint8_t*)enc->extradata != 1)
if (ff_avc_parse_nal_units_buf(VAR_1->data, &data, &VAR_3) < 0)
return -1; if (enc->codec_id == AV_CODEC_ID_SPEEX && VAR_2 - sc->last_ts > 160)
av_log(VAR_0, AV_LOG_WARNING, "Warning: Speex stream has more than "
"8 frames per packet. Adobe Flash "
"Player cannot handle this!\n"); if (sc->last_ts < VAR_2)
sc->last_ts = VAR_2; avio_wb24(pb, VAR_3 + VAR_5); avio_wb24(pb, VAR_2); avio_w8(pb, (VAR_2 >> 24) & 0x7F); avio_wb24(pb, flv->reserved); if (enc->codec_type == AVMEDIA_TYPE_DATA) { int VAR_6; int64_t metadata_size_pos = avio_tell(pb); avio_w8(pb, AMF_DATA_TYPE_STRING); put_amf_string(pb, "onTextData"); avio_w8(pb, AMF_DATA_TYPE_MIXEDARRAY); avio_wb32(pb, 2); put_amf_string(pb, "type"); avio_w8(pb, AMF_DATA_TYPE_STRING); put_amf_string(pb, "Text"); put_amf_string(pb, "text"); avio_w8(pb, AMF_DATA_TYPE_STRING); put_amf_string(pb, VAR_1->data); put_amf_string(pb, ""); avio_w8(pb, AMF_END_OF_OBJECT); VAR_6 = avio_tell(pb) - metadata_size_pos; avio_seek(pb, metadata_size_pos - 10, SEEK_SET); avio_wb24(pb, VAR_6); avio_seek(pb, VAR_6 + 10 - 3, SEEK_CUR); avio_wb32(pb, VAR_6 + 11); } else { avio_w8(pb,VAR_4); if (enc->codec_id == AV_CODEC_ID_VP6F || enc->codec_id == AV_CODEC_ID_VP6A) { if (enc->extradata_size)
avio_w8(pb, enc->extradata[0]); else
avio_w8(pb, ((FFALIGN(enc->width,  16) - enc->width) << 4) |
(FFALIGN(enc->height, 16) - enc->height)); } else if (enc->codec_id == AV_CODEC_ID_AAC) avio_w8(pb, 1); else if (enc->codec_id == AV_CODEC_ID_H264) { avio_w8(pb, 1); avio_wb24(pb, VAR_1->pts - VAR_1->dts); } avio_write(pb, data ? data : VAR_1->data, VAR_3); avio_wb32(pb, VAR_3 + VAR_5 + 11); flv->duration = FFMAX(flv->duration,
VAR_1->pts + flv->delay + VAR_1->duration); } av_free(data); return pb->error; }