static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, unsigned int max_pos, int depth) {
    AVCodecContext *acodec, *vcodec;
    ByteIOContext *ioc;
    AMFDataType amf_type;
    char str_val[256];
    double num_val;
    num_val = 0;
    ioc = s->pb;
    amf_type = get_byte(ioc);
    switch(amf_type) {
        case AMF_DATA_TYPE_NUMBER:
            num_val = av_int2dbl(get_be64(ioc)); break;
        case AMF_DATA_TYPE_BOOL:
            num_val = get_byte(ioc); break;
        case AMF_DATA_TYPE_STRING:
            if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)
                return -1;
            break;
        case AMF_DATA_TYPE_OBJECT: {
            unsigned int keylen;
            while(url_ftell(ioc) < max_pos - 2 && (keylen = get_be16(ioc))) {
                url_fskip(ioc, keylen);
                if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
                    return -1;
            }
            if(get_byte(ioc) != AMF_END_OF_OBJECT)
                return -1;
        }
            break;
        case AMF_DATA_TYPE_NULL:
        case AMF_DATA_TYPE_UNDEFINED:
        case AMF_DATA_TYPE_UNSUPPORTED:
            break;
        case AMF_DATA_TYPE_MIXEDARRAY:
            url_fskip(ioc, 4);
            while(url_ftell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {
                if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)
                    return -1;
            }
            if(get_byte(ioc) != AMF_END_OF_OBJECT)
                return -1;
            break;
        case AMF_DATA_TYPE_ARRAY: {
            unsigned int arraylen, i;
            arraylen = get_be32(ioc);
            for(i = 0; i < arraylen && url_ftell(ioc) < max_pos - 1; i++) {
                if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)
                    return -1;
            }
        }
            break;
        case AMF_DATA_TYPE_DATE:
            url_fskip(ioc, 8 + 2);
            break;
        default:
            return -1;
    }
    if(depth == 1 && key) {
        acodec = astream ? astream->codec : NULL;
        vcodec = vstream ? vstream->codec : NULL;
        if(amf_type == AMF_DATA_TYPE_BOOL) {
            if(!strcmp(key, "stereo") && acodec) acodec->channels = num_val > 0 ? 2 : 1;
        } else if(amf_type == AMF_DATA_TYPE_NUMBER) {
            if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE;
            else if(!strcmp(key, "audiocodecid") && acodec && 0 <= (int)num_val)
                flv_set_audio_codec(s, astream, (int)num_val << FLV_AUDIO_CODECID_OFFSET);
            else if(!strcmp(key, "videocodecid") && vcodec && 0 <= (int)num_val)
                flv_set_video_codec(s, vstream, (int)num_val);
            else if(!strcmp(key, "audiosamplesize") && acodec && 0 < (int)num_val) {
                acodec->bits_per_sample = num_val;
                if(num_val == 8 && (acodec->codec_id == CODEC_ID_PCM_S16BE || acodec->codec_id == CODEC_ID_PCM_S16LE))
                    acodec->codec_id = CODEC_ID_PCM_S8;
            }
            else if(!strcmp(key, "audiosamplerate") && acodec && num_val >= 0) {
                if (!acodec->sample_rate) {
                    switch((int)num_val) {
                        case 44000: acodec->sample_rate = 44100  ; break;
                        case 22000: acodec->sample_rate = 22050  ; break;
                        case 11000: acodec->sample_rate = 11025  ; break;
                        case 5000 : acodec->sample_rate = 5512   ; break;
                        default   : acodec->sample_rate = num_val;
                    }
                }
            }
        }
    }
    return 0;
}
['static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)\n{\n    int ret, i, type, size, pts, flags, is_audio, next, pos;\n    AVStream *st = NULL;\n for(;;){\n    pos = url_ftell(s->pb);\n    url_fskip(s->pb, 4);\n    type = get_byte(s->pb);\n    size = get_be24(s->pb);\n    pts = get_be24(s->pb);\n    pts |= get_byte(s->pb) << 24;\n    if (url_feof(s->pb))\n        return AVERROR(EIO);\n    url_fskip(s->pb, 3);\n    flags = 0;\n    if(size == 0)\n        continue;\n    next= size + url_ftell(s->pb);\n    if (type == FLV_TAG_TYPE_AUDIO) {\n        is_audio=1;\n        flags = get_byte(s->pb);\n    } else if (type == FLV_TAG_TYPE_VIDEO) {\n        is_audio=0;\n        flags = get_byte(s->pb);\n    } else {\n        if (type == FLV_TAG_TYPE_META && size > 13+1+4)\n            flv_read_metabody(s, next);\n        else\n            av_log(s, AV_LOG_ERROR, "skipping flv packet: type %d, size %d, flags %d\\n", type, size, flags);\n        url_fseek(s->pb, next, SEEK_SET);\n        continue;\n    }\n    for(i=0;i<s->nb_streams;i++) {\n        st = s->streams[i];\n        if (st->id == is_audio)\n            break;\n    }\n    if(i == s->nb_streams){\n        av_log(NULL, AV_LOG_ERROR, "invalid stream\\n");\n        st= create_stream(s, is_audio);\n    }\n    if(  (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||         is_audio))\n       ||(st->discard >= AVDISCARD_BIDIR  &&  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))\n       || st->discard >= AVDISCARD_ALL\n       ){\n        url_fseek(s->pb, next, SEEK_SET);\n        continue;\n    }\n    if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)\n        av_add_index_entry(st, pos, pts, size, 0, AVINDEX_KEYFRAME);\n    break;\n }\n    if(!url_is_streamed(s->pb) && s->duration==AV_NOPTS_VALUE){\n        int size;\n        const int pos= url_ftell(s->pb);\n        const int fsize= url_fsize(s->pb);\n        url_fseek(s->pb, fsize-4, SEEK_SET);\n        size= get_be32(s->pb);\n        url_fseek(s->pb, fsize-3-size, SEEK_SET);\n        if(size == get_be24(s->pb) + 11){\n            s->duration= get_be24(s->pb) * (int64_t)AV_TIME_BASE / 1000;\n        }\n        url_fseek(s->pb, pos, SEEK_SET);\n    }\n    if(is_audio){\n        if(!st->codec->sample_rate || !st->codec->bits_per_sample || (!st->codec->codec_id && !st->codec->codec_tag)) {\n            st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;\n            if((flags & FLV_AUDIO_CODECID_MASK) == FLV_CODECID_NELLYMOSER_8HZ_MONO)\n                st->codec->sample_rate= 8000;\n            else\n                st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);\n            st->codec->bits_per_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;\n            flv_set_audio_codec(s, st, flags & FLV_AUDIO_CODECID_MASK);\n        }\n    }else{\n        size -= flv_set_video_codec(s, st, flags & FLV_VIDEO_CODECID_MASK);\n    }\n    ret= av_get_packet(s->pb, pkt, size - 1);\n    if (ret <= 0) {\n        return AVERROR(EIO);\n    }\n    pkt->size = ret;\n    pkt->pts = pts;\n    pkt->stream_index = st->index;\n    if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))\n        pkt->flags |= PKT_FLAG_KEY;\n    return ret;\n}', 'unsigned int get_be24(ByteIOContext *s)\n{\n    unsigned int val;\n    val = get_be16(s) << 8;\n    val |= get_byte(s);\n    return val;\n}', 'unsigned int get_be16(ByteIOContext *s)\n{\n    unsigned int val;\n    val = get_byte(s) << 8;\n    val |= get_byte(s);\n    return val;\n}', 'int get_byte(ByteIOContext *s)\n{\n    if (s->buf_ptr < s->buf_end) {\n        return *s->buf_ptr++;\n    } else {\n        fill_buffer(s);\n        if (s->buf_ptr < s->buf_end)\n            return *s->buf_ptr++;\n        else\n            return 0;\n    }\n}', 'static int flv_read_metabody(AVFormatContext *s, unsigned int next_pos) {\n    AMFDataType type;\n    AVStream *stream, *astream, *vstream;\n    ByteIOContext *ioc;\n    int i, keylen;\n    char buffer[11];\n    astream = NULL;\n    vstream = NULL;\n    keylen = 0;\n    ioc = s->pb;\n    type = get_byte(ioc);\n    if(type != AMF_DATA_TYPE_STRING || amf_get_string(ioc, buffer, sizeof(buffer)) < 0 || strcmp(buffer, "onMetaData"))\n        return -1;\n    for(i = 0; i < s->nb_streams; i++) {\n        stream = s->streams[i];\n        if     (stream->codec->codec_type == CODEC_TYPE_AUDIO) astream = stream;\n        else if(stream->codec->codec_type == CODEC_TYPE_VIDEO) vstream = stream;\n    }\n    if(amf_parse_object(s, astream, vstream, buffer, next_pos, 0) < 0)\n        return -1;\n    return 0;\n}', 'static int amf_parse_object(AVFormatContext *s, AVStream *astream, AVStream *vstream, const char *key, unsigned int max_pos, int depth) {\n    AVCodecContext *acodec, *vcodec;\n    ByteIOContext *ioc;\n    AMFDataType amf_type;\n    char str_val[256];\n    double num_val;\n    num_val = 0;\n    ioc = s->pb;\n    amf_type = get_byte(ioc);\n    switch(amf_type) {\n        case AMF_DATA_TYPE_NUMBER:\n            num_val = av_int2dbl(get_be64(ioc)); break;\n        case AMF_DATA_TYPE_BOOL:\n            num_val = get_byte(ioc); break;\n        case AMF_DATA_TYPE_STRING:\n            if(amf_get_string(ioc, str_val, sizeof(str_val)) < 0)\n                return -1;\n            break;\n        case AMF_DATA_TYPE_OBJECT: {\n            unsigned int keylen;\n            while(url_ftell(ioc) < max_pos - 2 && (keylen = get_be16(ioc))) {\n                url_fskip(ioc, keylen);\n                if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)\n                    return -1;\n            }\n            if(get_byte(ioc) != AMF_END_OF_OBJECT)\n                return -1;\n        }\n            break;\n        case AMF_DATA_TYPE_NULL:\n        case AMF_DATA_TYPE_UNDEFINED:\n        case AMF_DATA_TYPE_UNSUPPORTED:\n            break;\n        case AMF_DATA_TYPE_MIXEDARRAY:\n            url_fskip(ioc, 4);\n            while(url_ftell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {\n                if(amf_parse_object(s, astream, vstream, str_val, max_pos, depth + 1) < 0)\n                    return -1;\n            }\n            if(get_byte(ioc) != AMF_END_OF_OBJECT)\n                return -1;\n            break;\n        case AMF_DATA_TYPE_ARRAY: {\n            unsigned int arraylen, i;\n            arraylen = get_be32(ioc);\n            for(i = 0; i < arraylen && url_ftell(ioc) < max_pos - 1; i++) {\n                if(amf_parse_object(s, NULL, NULL, NULL, max_pos, depth + 1) < 0)\n                    return -1;\n            }\n        }\n            break;\n        case AMF_DATA_TYPE_DATE:\n            url_fskip(ioc, 8 + 2);\n            break;\n        default:\n            return -1;\n    }\n    if(depth == 1 && key) {\n        acodec = astream ? astream->codec : NULL;\n        vcodec = vstream ? vstream->codec : NULL;\n        if(amf_type == AMF_DATA_TYPE_BOOL) {\n            if(!strcmp(key, "stereo") && acodec) acodec->channels = num_val > 0 ? 2 : 1;\n        } else if(amf_type == AMF_DATA_TYPE_NUMBER) {\n            if(!strcmp(key, "duration")) s->duration = num_val * AV_TIME_BASE;\n            else if(!strcmp(key, "audiocodecid") && acodec && 0 <= (int)num_val)\n                flv_set_audio_codec(s, astream, (int)num_val << FLV_AUDIO_CODECID_OFFSET);\n            else if(!strcmp(key, "videocodecid") && vcodec && 0 <= (int)num_val)\n                flv_set_video_codec(s, vstream, (int)num_val);\n            else if(!strcmp(key, "audiosamplesize") && acodec && 0 < (int)num_val) {\n                acodec->bits_per_sample = num_val;\n                if(num_val == 8 && (acodec->codec_id == CODEC_ID_PCM_S16BE || acodec->codec_id == CODEC_ID_PCM_S16LE))\n                    acodec->codec_id = CODEC_ID_PCM_S8;\n            }\n            else if(!strcmp(key, "audiosamplerate") && acodec && num_val >= 0) {\n                if (!acodec->sample_rate) {\n                    switch((int)num_val) {\n                        case 44000: acodec->sample_rate = 44100  ; break;\n                        case 22000: acodec->sample_rate = 22050  ; break;\n                        case 11000: acodec->sample_rate = 11025  ; break;\n                        case 5000 : acodec->sample_rate = 5512   ; break;\n                        default   : acodec->sample_rate = num_val;\n                    }\n                }\n            }\n        }\n    }\n    return 0;\n}']