+static int flv_queue_extradata(FLVContext *flv, AVIOContext *pb, int stream,
+ int size)
+{
+ av_free(flv->new_extradata[stream]);
+ flv->new_extradata[stream] = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
+ if (!flv->new_extradata[stream])
+ return AVERROR(ENOMEM);
+ flv->new_extradata_size[stream] = size;
+ avio_read(pb, flv->new_extradata[stream], size);
+ return 0;
+}
+
+static void clear_index_entries(AVFormatContext *s, int64_t pos)
+{
+ int i, j, out;
+ av_log(s, AV_LOG_WARNING, "Found invalid index entries, clearing the index.\n");
+ for (i = 0; i < s->nb_streams; i++) {
+ AVStream *st = s->streams[i];
+ /* Remove all index entries that point to >= pos */
+ out = 0;
+ for (j = 0; j < st->nb_index_entries; j++) {
+ if (st->index_entries[j].pos < pos)
+ st->index_entries[out++] = st->index_entries[j];
+ }
+ st->nb_index_entries = out;
+ }
+}
+
+
+static int flv_data_packet(AVFormatContext *s, AVPacket *pkt,
+ int64_t dts, int64_t next)
+{
+ int ret = AVERROR_INVALIDDATA, i;
+ AVIOContext *pb = s->pb;
+ AVStream *st = NULL;
+ AMFDataType type;
+ char buf[20];
+ int length;
+
+ type = avio_r8(pb);
+ if (type == AMF_DATA_TYPE_MIXEDARRAY)
+ avio_seek(pb, 4, SEEK_CUR);
+ else if (type != AMF_DATA_TYPE_OBJECT)
+ goto out;
+
+ amf_get_string(pb, buf, sizeof(buf));
+ if (strcmp(buf, "type") || avio_r8(pb) != AMF_DATA_TYPE_STRING)
+ goto out;
+
+ amf_get_string(pb, buf, sizeof(buf));
+ //FIXME parse it as codec_id
+ amf_get_string(pb, buf, sizeof(buf));
+ if (strcmp(buf, "text") || avio_r8(pb) != AMF_DATA_TYPE_STRING)
+ goto out;
+
+ length = avio_rb16(pb);
+ ret = av_get_packet(s->pb, pkt, length);
+ if (ret < 0) {
+ ret = AVERROR(EIO);
+ goto out;
+ }
+
+ for (i = 0; i < s->nb_streams; i++) {
+ st = s->streams[i];
+ if (st->codec->codec_type == AVMEDIA_TYPE_DATA)
+ break;
+ }
+
+ if (i == s->nb_streams) {
+ st = create_stream(s, AVMEDIA_TYPE_DATA);
+ if (!st)
+ goto out;
+ st->codec->codec_id = AV_CODEC_ID_TEXT;
+ }
+
+ pkt->dts = dts;
+ pkt->pts = dts;
+ pkt->size = ret;
+
+ pkt->stream_index = st->index;
+ pkt->flags |= AV_PKT_FLAG_KEY;
+
+ avio_seek(s->pb, next + 4, SEEK_SET);
+out:
+ return ret;
+}
+