static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)

{

    LavfiContext *lavfi = avctx->priv_data;

    double min_pts = DBL_MAX;

    int stream_idx, min_pts_sink_idx = 0;

    AVFrame *frame = lavfi->decoded_frame;

    AVPicture pict;

    AVDictionary *frame_metadata;

    int ret, i;

    int size = 0;



    if (lavfi->subcc_packet.size) {

        *pkt = lavfi->subcc_packet;

        av_init_packet(&lavfi->subcc_packet);

        lavfi->subcc_packet.size = 0;

        lavfi->subcc_packet.data = NULL;

        return pkt->size;

    }



    /* iterate through all the graph sinks. Select the sink with the

     * minimum PTS */

    for (i = 0; i < lavfi->nb_sinks; i++) {

        AVRational tb = lavfi->sinks[i]->inputs[0]->time_base;

        double d;

        int ret;



        if (lavfi->sink_eof[i])

            continue;



        ret = av_buffersink_get_frame_flags(lavfi->sinks[i], frame,

                                            AV_BUFFERSINK_FLAG_PEEK);

        if (ret == AVERROR_EOF) {

            av_dlog(avctx, "EOF sink_idx:%d\n", i);

            lavfi->sink_eof[i] = 1;

            continue;

        } else if (ret < 0)

            return ret;

        d = av_rescale_q_rnd(frame->pts, tb, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);

        av_dlog(avctx, "sink_idx:%d time:%f\n", i, d);

        av_frame_unref(frame);



        if (d < min_pts) {

            min_pts = d;

            min_pts_sink_idx = i;

        }

    }

    if (min_pts == DBL_MAX)

        return AVERROR_EOF;



    av_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);



    av_buffersink_get_frame_flags(lavfi->sinks[min_pts_sink_idx], frame, 0);

    stream_idx = lavfi->sink_stream_map[min_pts_sink_idx];



    if (frame->width /* FIXME best way of testing a video */) {

        size = avpicture_get_size(frame->format, frame->width, frame->height);

        if ((ret = av_new_packet(pkt, size)) < 0)

            return ret;



        memcpy(pict.data,     frame->data,     4*sizeof(frame->data[0]));

        memcpy(pict.linesize, frame->linesize, 4*sizeof(frame->linesize[0]));



        avpicture_layout(&pict, frame->format, frame->width, frame->height,

                         pkt->data, size);

    } else if (av_frame_get_channels(frame) /* FIXME test audio */) {

        size = frame->nb_samples * av_get_bytes_per_sample(frame->format) *

                                   av_frame_get_channels(frame);

        if ((ret = av_new_packet(pkt, size)) < 0)

            return ret;

        memcpy(pkt->data, frame->data[0], size);

    }



    frame_metadata = av_frame_get_metadata(frame);

    if (frame_metadata) {

        uint8_t *metadata;

        AVDictionaryEntry *e = NULL;

        AVBPrint meta_buf;



        av_bprint_init(&meta_buf, 0, AV_BPRINT_SIZE_UNLIMITED);

        while ((e = av_dict_get(frame_metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {

            av_bprintf(&meta_buf, "%s", e->key);

            av_bprint_chars(&meta_buf, '\0', 1);

            av_bprintf(&meta_buf, "%s", e->value);

            av_bprint_chars(&meta_buf, '\0', 1);

        }

        if (!av_bprint_is_complete(&meta_buf) ||

            !(metadata = av_packet_new_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA,

                                                 meta_buf.len))) {

            av_bprint_finalize(&meta_buf, NULL);

            return AVERROR(ENOMEM);

        }

        memcpy(metadata, meta_buf.str, meta_buf.len);

        av_bprint_finalize(&meta_buf, NULL);

    }



    if ((ret = create_subcc_packet(avctx, frame, min_pts_sink_idx)) < 0) {

        av_frame_unref(frame);

        av_packet_unref(pkt);

        return ret;

    }



    pkt->stream_index = stream_idx;

    pkt->pts = frame->pts;

    pkt->pos = av_frame_get_pkt_pos(frame);

    pkt->size = size;

    av_frame_unref(frame);

    return size;

}

static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)

{

    LavfiContext *lavfi = avctx->priv_data;

    double min_pts = DBL_MAX;

    int stream_idx, min_pts_sink_idx = 0;

    AVFrame *frame = lavfi->decoded_frame;

    AVPicture pict;

    AVDictionary *frame_metadata;

    int ret, i;

    int size = 0;



    if (lavfi->subcc_packet.size) {

        *pkt = lavfi->subcc_packet;

        av_init_packet(&lavfi->subcc_packet);

        lavfi->subcc_packet.size = 0;

        lavfi->subcc_packet.data = NULL;

        return pkt->size;

    }



    



    for (i = 0; i < lavfi->nb_sinks; i++) {

        AVRational tb = lavfi->sinks[i]->inputs[0]->time_base;

        double d;

        int ret;



        if (lavfi->sink_eof[i])

            continue;



        ret = av_buffersink_get_frame_flags(lavfi->sinks[i], frame,

                                            AV_BUFFERSINK_FLAG_PEEK);

        if (ret == AVERROR_EOF) {

            av_dlog(avctx, "EOF sink_idx:%d\n", i);

            lavfi->sink_eof[i] = 1;

            continue;

        } else if (ret < 0)

            return ret;

        d = av_rescale_q_rnd(frame->pts, tb, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);

        av_dlog(avctx, "sink_idx:%d time:%f\n", i, d);

        av_frame_unref(frame);



        if (d < min_pts) {

            min_pts = d;

            min_pts_sink_idx = i;

        }

    }

    if (min_pts == DBL_MAX)

        return AVERROR_EOF;



    av_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);



    av_buffersink_get_frame_flags(lavfi->sinks[min_pts_sink_idx], frame, 0);

    stream_idx = lavfi->sink_stream_map[min_pts_sink_idx];



    if (frame->width ) {

        size = avpicture_get_size(frame->format, frame->width, frame->height);

        if ((ret = av_new_packet(pkt, size)) < 0)

            return ret;



        memcpy(pict.data,     frame->data,     4*sizeof(frame->data[0]));

        memcpy(pict.linesize, frame->linesize, 4*sizeof(frame->linesize[0]));



        avpicture_layout(&pict, frame->format, frame->width, frame->height,

                         pkt->data, size);

    } else if (av_frame_get_channels(frame) ) {

        size = frame->nb_samples * av_get_bytes_per_sample(frame->format) *

                                   av_frame_get_channels(frame);

        if ((ret = av_new_packet(pkt, size)) < 0)

            return ret;

        memcpy(pkt->data, frame->data[0], size);

    }



    frame_metadata = av_frame_get_metadata(frame);

    if (frame_metadata) {

        uint8_t *metadata;

        AVDictionaryEntry *e = NULL;

        AVBPrint meta_buf;



        av_bprint_init(&meta_buf, 0, AV_BPRINT_SIZE_UNLIMITED);

        while ((e = av_dict_get(frame_metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {

            av_bprintf(&meta_buf, "%s", e->key);

            av_bprint_chars(&meta_buf, '\0', 1);

            av_bprintf(&meta_buf, "%s", e->value);

            av_bprint_chars(&meta_buf, '\0', 1);

        }

        if (!av_bprint_is_complete(&meta_buf) ||

            !(metadata = av_packet_new_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA,

                                                 meta_buf.len))) {

            av_bprint_finalize(&meta_buf, NULL);

            return AVERROR(ENOMEM);

        }

        memcpy(metadata, meta_buf.str, meta_buf.len);

        av_bprint_finalize(&meta_buf, NULL);

    }



    if ((ret = create_subcc_packet(avctx, frame, min_pts_sink_idx)) < 0) {

        av_frame_unref(frame);

        av_packet_unref(pkt);

        return ret;

    }



    pkt->stream_index = stream_idx;

    pkt->pts = frame->pts;

    pkt->pos = av_frame_get_pkt_pos(frame);

    pkt->size = size;

    av_frame_unref(frame);

    return size;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)

{

    LavfiContext *lavfi = VAR_0->priv_data;

    double VAR_2 = DBL_MAX;

    int VAR_3, VAR_4 = 0;

    AVFrame *frame = lavfi->decoded_frame;

    AVPicture pict;

    AVDictionary *frame_metadata;

    int VAR_5, VAR_6;

    int VAR_7 = 0;



    if (lavfi->subcc_packet.VAR_7) {

        *VAR_1 = lavfi->subcc_packet;

        av_init_packet(&lavfi->subcc_packet);

        lavfi->subcc_packet.VAR_7 = 0;

        lavfi->subcc_packet.data = NULL;

        return VAR_1->VAR_7;

    }



    



    for (VAR_6 = 0; VAR_6 < lavfi->nb_sinks; VAR_6++) {

        AVRational tb = lavfi->sinks[VAR_6]->inputs[0]->time_base;

        double d;

        int VAR_5;



        if (lavfi->sink_eof[VAR_6])

            continue;



        VAR_5 = av_buffersink_get_frame_flags(lavfi->sinks[VAR_6], frame,

                                            AV_BUFFERSINK_FLAG_PEEK);

        if (VAR_5 == AVERROR_EOF) {

            av_dlog(VAR_0, "EOF sink_idx:%d\n", VAR_6);

            lavfi->sink_eof[VAR_6] = 1;

            continue;

        } else if (VAR_5 < 0)

            return VAR_5;

        d = av_rescale_q_rnd(frame->pts, tb, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);

        av_dlog(VAR_0, "sink_idx:%d time:%f\n", VAR_6, d);

        av_frame_unref(frame);



        if (d < VAR_2) {

            VAR_2 = d;

            VAR_4 = VAR_6;

        }

    }

    if (VAR_2 == DBL_MAX)

        return AVERROR_EOF;



    av_dlog(VAR_0, "VAR_4:%VAR_6\n", VAR_4);



    av_buffersink_get_frame_flags(lavfi->sinks[VAR_4], frame, 0);

    VAR_3 = lavfi->sink_stream_map[VAR_4];



    if (frame->width ) {

        VAR_7 = avpicture_get_size(frame->format, frame->width, frame->height);

        if ((VAR_5 = av_new_packet(VAR_1, VAR_7)) < 0)

            return VAR_5;



        memcpy(pict.data,     frame->data,     4*sizeof(frame->data[0]));

        memcpy(pict.linesize, frame->linesize, 4*sizeof(frame->linesize[0]));



        avpicture_layout(&pict, frame->format, frame->width, frame->height,

                         VAR_1->data, VAR_7);

    } else if (av_frame_get_channels(frame) ) {

        VAR_7 = frame->nb_samples * av_get_bytes_per_sample(frame->format) *

                                   av_frame_get_channels(frame);

        if ((VAR_5 = av_new_packet(VAR_1, VAR_7)) < 0)

            return VAR_5;

        memcpy(VAR_1->data, frame->data[0], VAR_7);

    }



    frame_metadata = av_frame_get_metadata(frame);

    if (frame_metadata) {

        uint8_t *metadata;

        AVDictionaryEntry *e = NULL;

        AVBPrint meta_buf;



        av_bprint_init(&meta_buf, 0, AV_BPRINT_SIZE_UNLIMITED);

        while ((e = av_dict_get(frame_metadata, "", e, AV_DICT_IGNORE_SUFFIX))) {

            av_bprintf(&meta_buf, "%s", e->key);

            av_bprint_chars(&meta_buf, '\0', 1);

            av_bprintf(&meta_buf, "%s", e->value);

            av_bprint_chars(&meta_buf, '\0', 1);

        }

        if (!av_bprint_is_complete(&meta_buf) ||

            !(metadata = av_packet_new_side_data(VAR_1, AV_PKT_DATA_STRINGS_METADATA,

                                                 meta_buf.len))) {

            av_bprint_finalize(&meta_buf, NULL);

            return AVERROR(ENOMEM);

        }

        memcpy(metadata, meta_buf.str, meta_buf.len);

        av_bprint_finalize(&meta_buf, NULL);

    }



    if ((VAR_5 = create_subcc_packet(VAR_0, frame, VAR_4)) < 0) {

        av_frame_unref(frame);

        av_packet_unref(VAR_1);

        return VAR_5;

    }



    VAR_1->stream_index = VAR_3;

    VAR_1->pts = frame->pts;

    VAR_1->pos = av_frame_get_pkt_pos(frame);

    VAR_1->VAR_7 = VAR_7;

    av_frame_unref(frame);

    return VAR_7;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)
{ LavfiContext *lavfi = VAR_0->priv_data; double VAR_2 = DBL_MAX; int VAR_3, VAR_4 = 0; AVFrame *frame = lavfi->decoded_frame; AVPicture pict; AVDictionary *frame_metadata; int VAR_5, VAR_6; int VAR_7 = 0; if (lavfi->subcc_packet.VAR_7) { *VAR_1 = lavfi->subcc_packet; av_init_packet(&lavfi->subcc_packet); lavfi->subcc_packet.VAR_7 = 0; lavfi->subcc_packet.data = NULL; return VAR_1->VAR_7; } for (VAR_6 = 0; VAR_6 < lavfi->nb_sinks; VAR_6++) { AVRational tb = lavfi->sinks[VAR_6]->inputs[0]->time_base; double d; int VAR_5; if (lavfi->sink_eof[VAR_6])
continue; VAR_5 = av_buffersink_get_frame_flags(lavfi->sinks[VAR_6], frame,
AV_BUFFERSINK_FLAG_PEEK); if (VAR_5 == AVERROR_EOF) { av_dlog(VAR_0, "EOF sink_idx:%d\n", VAR_6); lavfi->sink_eof[VAR_6] = 1; continue; } else if (VAR_5 < 0) return VAR_5; d = av_rescale_q_rnd(frame->pts, tb, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX); av_dlog(VAR_0, "sink_idx:%d time:%f\n", VAR_6, d); av_frame_unref(frame); if (d < VAR_2) { VAR_2 = d; VAR_4 = VAR_6; } } if (VAR_2 == DBL_MAX)
return AVERROR_EOF; av_dlog(VAR_0, "VAR_4:%VAR_6\n", VAR_4); av_buffersink_get_frame_flags(lavfi->sinks[VAR_4], frame, 0); VAR_3 = lavfi->sink_stream_map[VAR_4]; if (frame->width ) { VAR_7 = avpicture_get_size(frame->format, frame->width, frame->height); if ((VAR_5 = av_new_packet(VAR_1, VAR_7)) < 0)
return VAR_5; memcpy(pict.data,     frame->data,     4*sizeof(frame->data[0])); memcpy(pict.linesize, frame->linesize, 4*sizeof(frame->linesize[0])); avpicture_layout(&pict, frame->format, frame->width, frame->height,
VAR_1->data, VAR_7); } else if (av_frame_get_channels(frame) ) { VAR_7 = frame->nb_samples * av_get_bytes_per_sample(frame->format) *
av_frame_get_channels(frame); if ((VAR_5 = av_new_packet(VAR_1, VAR_7)) < 0)
return VAR_5; memcpy(VAR_1->data, frame->data[0], VAR_7); } frame_metadata = av_frame_get_metadata(frame); if (frame_metadata) { uint8_t *metadata; AVDictionaryEntry *e = NULL; AVBPrint meta_buf; av_bprint_init(&meta_buf, 0, AV_BPRINT_SIZE_UNLIMITED); while ((e = av_dict_get(frame_metadata, "", e, AV_DICT_IGNORE_SUFFIX))) { av_bprintf(&meta_buf, "%s", e->key); av_bprint_chars(&meta_buf, '\0', 1); av_bprintf(&meta_buf, "%s", e->value); av_bprint_chars(&meta_buf, '\0', 1); } if (!av_bprint_is_complete(&meta_buf) ||
!(metadata = av_packet_new_side_data(VAR_1, AV_PKT_DATA_STRINGS_METADATA,
meta_buf.len))) { av_bprint_finalize(&meta_buf, NULL); return AVERROR(ENOMEM); } memcpy(metadata, meta_buf.str, meta_buf.len); av_bprint_finalize(&meta_buf, NULL); } if ((VAR_5 = create_subcc_packet(VAR_0, frame, VAR_4)) < 0) { av_frame_unref(frame); av_packet_unref(VAR_1); return VAR_5; } VAR_1->stream_index = VAR_3; VAR_1->pts = frame->pts; VAR_1->pos = av_frame_get_pkt_pos(frame); VAR_1->VAR_7 = VAR_7; av_frame_unref(frame); return VAR_7; }