if (av_write_frame(ctx, pkt.stream_index, pkt.data, pkt.size)) {
if (av_write_frame(ctx, &pkt)) {
static int http_prepare_data(HTTPContext *c){    int i, len, ret;    AVFormatContext *ctx;    av_freep(&c->pb_buffer);    switch(c->state) {    case HTTPSTATE_SEND_DATA_HEADER:        memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));        pstrcpy(c->fmt_ctx.author, sizeof(c->fmt_ctx.author),                 c->stream->author);        pstrcpy(c->fmt_ctx.comment, sizeof(c->fmt_ctx.comment),                 c->stream->comment);        pstrcpy(c->fmt_ctx.copyright, sizeof(c->fmt_ctx.copyright),                 c->stream->copyright);        pstrcpy(c->fmt_ctx.title, sizeof(c->fmt_ctx.title),                 c->stream->title);        /* open output stream by using specified codecs */        c->fmt_ctx.oformat = c->stream->fmt;        c->fmt_ctx.nb_streams = c->stream->nb_streams;        for(i=0;i<c->fmt_ctx.nb_streams;i++) {            AVStream *st;            st = av_mallocz(sizeof(AVStream));            c->fmt_ctx.streams[i] = st;            /* if file or feed, then just take streams from FFStream struct */            if (!c->stream->feed ||                 c->stream->feed == c->stream)                memcpy(st, c->stream->streams[i], sizeof(AVStream));            else                memcpy(st, c->stream->feed->streams[c->stream->feed_streams[i]],                           sizeof(AVStream));            st->codec.frame_number = 0; /* XXX: should be done in                                           AVStream, not in codec */            /* I'm pretty sure that this is not correct...             * However, without it, we crash             */            st->codec.coded_frame = &dummy_frame;        }        c->got_key_frame = 0;        /* prepare header and save header data in a stream */        if (url_open_dyn_buf(&c->fmt_ctx.pb) < 0) {            /* XXX: potential leak */            return -1;        }        c->fmt_ctx.pb.is_streamed = 1;        av_set_parameters(&c->fmt_ctx, NULL);        av_write_header(&c->fmt_ctx);        len = url_close_dyn_buf(&c->fmt_ctx.pb, &c->pb_buffer);        c->buffer_ptr = c->pb_buffer;        c->buffer_end = c->pb_buffer + len;        c->state = HTTPSTATE_SEND_DATA;        c->last_packet_sent = 0;        break;    case HTTPSTATE_SEND_DATA:        /* find a new packet */        {            AVPacket pkt;                        /* read a packet from the input stream */            if (c->stream->feed) {                ffm_set_write_index(c->fmt_in,                                     c->stream->feed->feed_write_index,                                    c->stream->feed->feed_size);            }            if (c->stream->max_time &&                 c->stream->max_time + c->start_time - cur_time < 0) {                /* We have timed out */                c->state = HTTPSTATE_SEND_DATA_TRAILER;            } else {            redo:                if (av_read_frame(c->fmt_in, &pkt) < 0) {                    if (c->stream->feed && c->stream->feed->feed_opened) {                        /* if coming from feed, it means we reached the end of the                           ffm file, so must wait for more data */                        c->state = HTTPSTATE_WAIT_FEED;                        return 1; /* state changed */                    } else {                        if (c->stream->loop) {                            av_close_input_file(c->fmt_in);                            c->fmt_in = NULL;                            if (open_input_stream(c, "") < 0)                                goto no_loop;                            goto redo;                        } else {                        no_loop:                            /* must send trailer now because eof or error */                            c->state = HTTPSTATE_SEND_DATA_TRAILER;                        }                    }                } else {                    /* update first pts if needed */                    if (c->first_pts == AV_NOPTS_VALUE) {                        c->first_pts = pkt.dts;                        c->start_time = cur_time;                    }                    /* send it to the appropriate stream */                    if (c->stream->feed) {                        /* if coming from a feed, select the right stream */                        if (c->switch_pending) {                            c->switch_pending = 0;                            for(i=0;i<c->stream->nb_streams;i++) {                                if (c->switch_feed_streams[i] == pkt.stream_index) {                                    if (pkt.flags & PKT_FLAG_KEY) {                                        do_switch_stream(c, i);                                    }                                }                                if (c->switch_feed_streams[i] >= 0) {                                    c->switch_pending = 1;                                }                            }                        }                        for(i=0;i<c->stream->nb_streams;i++) {                            if (c->feed_streams[i] == pkt.stream_index) {                                pkt.stream_index = i;                                if (pkt.flags & PKT_FLAG_KEY) {                                    c->got_key_frame |= 1 << i;                                }                                /* See if we have all the key frames, then                                  * we start to send. This logic is not quite                                 * right, but it works for the case of a                                  * single video stream with one or more                                 * audio streams (for which every frame is                                  * typically a key frame).                                  */                                if (!c->stream->send_on_key ||                                     ((c->got_key_frame + 1) >> c->stream->nb_streams)) {                                    goto send_it;                                }                            }                        }                    } else {                        AVCodecContext *codec;                                            send_it:                        /* specific handling for RTP: we use several                           output stream (one for each RTP                           connection). XXX: need more abstract handling */                        if (c->is_packetized) {                            AVStream *st;                            /* compute send time and duration */                            st = c->fmt_in->streams[pkt.stream_index];                            c->cur_pts = pkt.dts;                            if (st->start_time != AV_NOPTS_VALUE)                                c->cur_pts -= st->start_time;                            c->cur_frame_duration = pkt.duration;#if 0                            printf("index=%d pts=%0.3f duration=%0.6f\n",                                   pkt.stream_index,                                   (double)c->cur_pts /                                    AV_TIME_BASE,                                   (double)c->cur_frame_duration /                                    AV_TIME_BASE);#endif                            /* find RTP context */                            c->packet_stream_index = pkt.stream_index;                            ctx = c->rtp_ctx[c->packet_stream_index];                            if(!ctx) {                              av_free_packet(&pkt);                              break;                            }                            codec = &ctx->streams[0]->codec;                            /* only one stream per RTP connection */                            pkt.stream_index = 0;                        } else {                            ctx = &c->fmt_ctx;                            /* Fudge here */                            codec = &ctx->streams[pkt.stream_index]->codec;                        }                                                codec->coded_frame->key_frame = ((pkt.flags & PKT_FLAG_KEY) != 0);                        if (c->is_packetized) {                            int max_packet_size;                            if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP)                                max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;                            else                                max_packet_size = url_get_max_packet_size(c->rtp_handles[c->packet_stream_index]);                            ret = url_open_dyn_packet_buf(&ctx->pb, max_packet_size);                        } else {                            ret = url_open_dyn_buf(&ctx->pb);                        }                        if (ret < 0) {                            /* XXX: potential leak */                            return -1;                        }                        if (av_write_frame(ctx, pkt.stream_index, pkt.data, pkt.size)) {                            c->state = HTTPSTATE_SEND_DATA_TRAILER;                        }                                                len = url_close_dyn_buf(&ctx->pb, &c->pb_buffer);                        c->cur_frame_bytes = len;                        c->buffer_ptr = c->pb_buffer;                        c->buffer_end = c->pb_buffer + len;                                                codec->frame_number++;                        if (len == 0)                            goto redo;                    }                    av_free_packet(&pkt);                }            }        }        break;    default:    case HTTPSTATE_SEND_DATA_TRAILER:        /* last packet test ? */        if (c->last_packet_sent || c->is_packetized)            return -1;        ctx = &c->fmt_ctx;        /* prepare header */        if (url_open_dyn_buf(&ctx->pb) < 0) {            /* XXX: potential leak */            return -1;        }        av_write_trailer(ctx);        len = url_close_dyn_buf(&ctx->pb, &c->pb_buffer);        c->buffer_ptr = c->pb_buffer;        c->buffer_end = c->pb_buffer + len;        c->last_packet_sent = 1;        break;    }    return 0;}