static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {

    unsigned int arraylen = 0, timeslen = 0, fileposlen = 0, i;

    double num_val;

    char str_val[256];

    int64_t *times = NULL;

    int64_t *filepositions = NULL;

    int ret = AVERROR(ENOSYS);

    int64_t initial_pos = avio_tell(ioc);

    AVDictionaryEntry *creator = av_dict_get(s->metadata, "metadatacreator",

                                             NULL, 0);



    if (creator && !strcmp(creator->value, "MEGA")) {

        /* Files with this metadatacreator tag seem to have filepositions

         * pointing at the 4 trailer bytes of the previous packet,

         * which isn't the norm (nor what we expect here, nor what

         * jwplayer + lighttpd expect, nor what flvtool2 produces).

         * Just ignore the index in this case, instead of risking trying

         * to adjust it to something that might or might not work. */

        return 0;

    }



    while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {

        int64_t* current_array;



        // Expect array object in context

        if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)

            break;



        arraylen = avio_rb32(ioc);

        /*

         * Expect only 'times' or 'filepositions' sub-arrays in other case refuse to use such metadata

         * for indexing

         */

        if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) {

            if (!(times = av_mallocz(sizeof(*times) * arraylen))) {

                ret = AVERROR(ENOMEM);

                goto finish;

            }

            timeslen = arraylen;

            current_array = times;

        } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions) {

            if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) {

                ret = AVERROR(ENOMEM);

                goto finish;

            }

            fileposlen = arraylen;

            current_array = filepositions;

        } else // unexpected metatag inside keyframes, will not use such metadata for indexing

            break;



        for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {

            if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)

                goto finish;

            num_val = av_int2dbl(avio_rb64(ioc));

            current_array[i] = num_val;

        }

        if (times && filepositions) {

            // All done, exiting at a position allowing amf_parse_object

            // to finish parsing the object

            ret = 0;

            break;

        }

    }



    if (timeslen == fileposlen)

         for(i = 0; i < arraylen; i++)

             av_add_index_entry(vstream, filepositions[i], times[i]*1000, 0, 0, AVINDEX_KEYFRAME);

    else

        av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");



finish:

    av_freep(&times);

    av_freep(&filepositions);

    // If we got unexpected data, but successfully reset back to

    // the start pos, the caller can continue parsing

    if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0)

        return 0;

    return ret;

}

static int parse_keyframes_index(AVFormatContext *s, AVIOContext *ioc, AVStream *vstream, int64_t max_pos) {

    unsigned int arraylen = 0, timeslen = 0, fileposlen = 0, i;

    double num_val;

    char str_val[256];

    int64_t *times = NULL;

    int64_t *filepositions = NULL;

    int ret = AVERROR(ENOSYS);

    int64_t initial_pos = avio_tell(ioc);

    AVDictionaryEntry *creator = av_dict_get(s->metadata, "metadatacreator",

                                             NULL, 0);



    if (creator && !strcmp(creator->value, "MEGA")) {

        











        return 0;

    }



    while (avio_tell(ioc) < max_pos - 2 && amf_get_string(ioc, str_val, sizeof(str_val)) > 0) {

        int64_t* current_array;



        

        if (avio_r8(ioc) != AMF_DATA_TYPE_ARRAY)

            break;



        arraylen = avio_rb32(ioc);

        







        if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, str_val) && !times) {

            if (!(times = av_mallocz(sizeof(*times) * arraylen))) {

                ret = AVERROR(ENOMEM);

                goto finish;

            }

            timeslen = arraylen;

            current_array = times;

        } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, str_val) && !filepositions) {

            if (!(filepositions = av_mallocz(sizeof(*filepositions) * arraylen))) {

                ret = AVERROR(ENOMEM);

                goto finish;

            }

            fileposlen = arraylen;

            current_array = filepositions;

        } else 

            break;



        for (i = 0; i < arraylen && avio_tell(ioc) < max_pos - 1; i++) {

            if (avio_r8(ioc) != AMF_DATA_TYPE_NUMBER)

                goto finish;

            num_val = av_int2dbl(avio_rb64(ioc));

            current_array[i] = num_val;

        }

        if (times && filepositions) {

            

            

            ret = 0;

            break;

        }

    }



    if (timeslen == fileposlen)

         for(i = 0; i < arraylen; i++)

             av_add_index_entry(vstream, filepositions[i], times[i]*1000, 0, 0, AVINDEX_KEYFRAME);

    else

        av_log(s, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");



finish:

    av_freep(&times);

    av_freep(&filepositions);

    

    

    if (ret < 0 && avio_seek(ioc, initial_pos, SEEK_SET) > 0)

        return 0;

    return ret;

}

    if (timeslen == fileposlen)          for(i = 0; i < arraylen; i++)
static int FUNC_0(AVFormatContext *VAR_0, AVIOContext *VAR_1, AVStream *VAR_2, int64_t VAR_3) {

    unsigned int VAR_4 = 0, VAR_5 = 0, VAR_6 = 0, VAR_7;

    double VAR_8;

    char VAR_9[256];

    int64_t *times = NULL;

    int64_t *filepositions = NULL;

    int VAR_10 = AVERROR(ENOSYS);

    int64_t initial_pos = avio_tell(VAR_1);

    AVDictionaryEntry *creator = av_dict_get(VAR_0->metadata, "metadatacreator",

                                             NULL, 0);



    if (creator && !strcmp(creator->value, "MEGA")) {

        











        return 0;

    }



    while (avio_tell(VAR_1) < VAR_3 - 2 && amf_get_string(VAR_1, VAR_9, sizeof(VAR_9)) > 0) {

        int64_t* current_array;



        

        if (avio_r8(VAR_1) != AMF_DATA_TYPE_ARRAY)

            break;



        VAR_4 = avio_rb32(VAR_1);

        







        if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, VAR_9) && !times) {

            if (!(times = av_mallocz(sizeof(*times) * VAR_4))) {

                VAR_10 = AVERROR(ENOMEM);

                goto finish;

            }

            VAR_5 = VAR_4;

            current_array = times;

        } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, VAR_9) && !filepositions) {

            if (!(filepositions = av_mallocz(sizeof(*filepositions) * VAR_4))) {

                VAR_10 = AVERROR(ENOMEM);

                goto finish;

            }

            VAR_6 = VAR_4;

            current_array = filepositions;

        } else 

            break;



        for (VAR_7 = 0; VAR_7 < VAR_4 && avio_tell(VAR_1) < VAR_3 - 1; VAR_7++) {

            if (avio_r8(VAR_1) != AMF_DATA_TYPE_NUMBER)

                goto finish;

            VAR_8 = av_int2dbl(avio_rb64(VAR_1));

            current_array[VAR_7] = VAR_8;

        }

        if (times && filepositions) {

            

            

            VAR_10 = 0;

            break;

        }

    }



    if (VAR_5 == VAR_6)

         for(VAR_7 = 0; VAR_7 < VAR_4; VAR_7++)

             av_add_index_entry(VAR_2, filepositions[VAR_7], times[VAR_7]*1000, 0, 0, AVINDEX_KEYFRAME);

    else

        av_log(VAR_0, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n");



finish:

    av_freep(&times);

    av_freep(&filepositions);

    

    

    if (VAR_10 < 0 && avio_seek(VAR_1, initial_pos, SEEK_SET) > 0)

        return 0;

    return VAR_10;

}

static int FUNC_0(AVFormatContext *VAR_0, AVIOContext *VAR_1, AVStream *VAR_2, int64_t VAR_3) { unsigned int VAR_4 = 0, VAR_5 = 0, VAR_6 = 0, VAR_7; double VAR_8; char VAR_9[256]; int64_t *times = NULL; int64_t *filepositions = NULL; int VAR_10 = AVERROR(ENOSYS); int64_t initial_pos = avio_tell(VAR_1); AVDictionaryEntry *creator = av_dict_get(VAR_0->metadata, "metadatacreator",
NULL, 0); if (creator && !strcmp(creator->value, "MEGA")) { return 0; } while (avio_tell(VAR_1) < VAR_3 - 2 && amf_get_string(VAR_1, VAR_9, sizeof(VAR_9)) > 0) { int64_t* current_array; if (avio_r8(VAR_1) != AMF_DATA_TYPE_ARRAY)
break; VAR_4 = avio_rb32(VAR_1); if (!strcmp(KEYFRAMES_TIMESTAMP_TAG, VAR_9) && !times) { if (!(times = av_mallocz(sizeof(*times) * VAR_4))) { VAR_10 = AVERROR(ENOMEM); goto finish; } VAR_5 = VAR_4; current_array = times; } else if (!strcmp(KEYFRAMES_BYTEOFFSET_TAG, VAR_9) && !filepositions) { if (!(filepositions = av_mallocz(sizeof(*filepositions) * VAR_4))) { VAR_10 = AVERROR(ENOMEM); goto finish; } VAR_6 = VAR_4; current_array = filepositions; } else break; for (VAR_7 = 0; VAR_7 < VAR_4 && avio_tell(VAR_1) < VAR_3 - 1; VAR_7++) { if (avio_r8(VAR_1) != AMF_DATA_TYPE_NUMBER)
goto finish; VAR_8 = av_int2dbl(avio_rb64(VAR_1)); current_array[VAR_7] = VAR_8; } if (times && filepositions) { VAR_10 = 0; break; } } if (VAR_5 == VAR_6)
for(VAR_7 = 0; VAR_7 < VAR_4; VAR_7++) av_add_index_entry(VAR_2, filepositions[VAR_7], times[VAR_7]*1000, 0, 0, AVINDEX_KEYFRAME); else
av_log(VAR_0, AV_LOG_WARNING, "Invalid keyframes object, skipping.\n"); finish:
av_freep(&times); av_freep(&filepositions); if (VAR_10 < 0 && avio_seek(VAR_1, initial_pos, SEEK_SET) > 0)
return 0; return VAR_10; }