static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)

{

    MXFContext *mxf = s->priv_data;

    KLVPacket klv;

    int64_t essence_offset = 0;



    mxf->last_forward_tell = INT64_MAX;



    if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) {

        av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");

        return AVERROR_INVALIDDATA;

    }

    avio_seek(s->pb, -14, SEEK_CUR);

    mxf->fc = s;

    mxf->run_in = avio_tell(s->pb);



    while (!s->pb->eof_reached) {

        const MXFMetadataReadTableEntry *metadata;



        if (klv_read_packet(&klv, s->pb) < 0) {

            /* EOF - seek to previous partition or stop */

            if(mxf_parse_handle_partition_or_eof(mxf) <= 0)

                break;

            else

                continue;

        }



        PRINT_KEY(s, "read header", klv.key);

        av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);

        if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||

            IS_KLV_KEY(klv.key, mxf_essence_element_key) ||

            IS_KLV_KEY(klv.key, mxf_avid_essence_element_key) ||

            IS_KLV_KEY(klv.key, mxf_system_item_key)) {

            if (!mxf->current_partition->essence_offset) {

                compute_partition_essence_offset(s, mxf, &klv);

            }



            if (!essence_offset)

                essence_offset = klv.offset;



            /* seek to footer, previous partition or stop */

            if (mxf_parse_handle_essence(mxf) <= 0)

                break;

            continue;

        } else if (!memcmp(klv.key, mxf_header_partition_pack_key, 13) &&

                   klv.key[13] >= 2 && klv.key[13] <= 4 && mxf->current_partition) {

            /* next partition pack - keep going, seek to previous partition or stop */

            if(mxf_parse_handle_partition_or_eof(mxf) <= 0)

                break;

        }



        for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {

            if (IS_KLV_KEY(klv.key, metadata->key)) {

                int res;

                if (klv.key[5] == 0x53) {

                    res = mxf_read_local_tags(mxf, &klv, metadata->read, metadata->ctx_size, metadata->type);

                } else {

                    uint64_t next = avio_tell(s->pb) + klv.length;

                    res = metadata->read(mxf, s->pb, 0, klv.length, klv.key, klv.offset);

                    avio_seek(s->pb, next, SEEK_SET);

                }

                if (res < 0) {

                    av_log(s, AV_LOG_ERROR, "error reading header metadata\n");

                    return res;

                }

                break;

            }

        }

        if (!metadata->read)

            avio_skip(s->pb, klv.length);

    }

    /* FIXME avoid seek */

    if (!essence_offset)  {

        av_log(s, AV_LOG_ERROR, "no essence\n");

        return AVERROR_INVALIDDATA;

    }

    avio_seek(s->pb, essence_offset, SEEK_SET);



    mxf_compute_essence_containers(mxf);



    return mxf_parse_structural_metadata(mxf);

}

static int mxf_read_header(AVFormatContext *s, AVFormatParameters *ap)

{

    MXFContext *mxf = s->priv_data;

    KLVPacket klv;

    int64_t essence_offset = 0;



    mxf->last_forward_tell = INT64_MAX;



    if (!mxf_read_sync(s->pb, mxf_header_partition_pack_key, 14)) {

        av_log(s, AV_LOG_ERROR, "could not find header partition pack key\n");

        return AVERROR_INVALIDDATA;

    }

    avio_seek(s->pb, -14, SEEK_CUR);

    mxf->fc = s;

    mxf->run_in = avio_tell(s->pb);



    while (!s->pb->eof_reached) {

        const MXFMetadataReadTableEntry *metadata;



        if (klv_read_packet(&klv, s->pb) < 0) {

            

            if(mxf_parse_handle_partition_or_eof(mxf) <= 0)

                break;

            else

                continue;

        }



        PRINT_KEY(s, "read header", klv.key);

        av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);

        if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||

            IS_KLV_KEY(klv.key, mxf_essence_element_key) ||

            IS_KLV_KEY(klv.key, mxf_avid_essence_element_key) ||

            IS_KLV_KEY(klv.key, mxf_system_item_key)) {

            if (!mxf->current_partition->essence_offset) {

                compute_partition_essence_offset(s, mxf, &klv);

            }



            if (!essence_offset)

                essence_offset = klv.offset;



            

            if (mxf_parse_handle_essence(mxf) <= 0)

                break;

            continue;

        } else if (!memcmp(klv.key, mxf_header_partition_pack_key, 13) &&

                   klv.key[13] >= 2 && klv.key[13] <= 4 && mxf->current_partition) {

            

            if(mxf_parse_handle_partition_or_eof(mxf) <= 0)

                break;

        }



        for (metadata = mxf_metadata_read_table; metadata->read; metadata++) {

            if (IS_KLV_KEY(klv.key, metadata->key)) {

                int res;

                if (klv.key[5] == 0x53) {

                    res = mxf_read_local_tags(mxf, &klv, metadata->read, metadata->ctx_size, metadata->type);

                } else {

                    uint64_t next = avio_tell(s->pb) + klv.length;

                    res = metadata->read(mxf, s->pb, 0, klv.length, klv.key, klv.offset);

                    avio_seek(s->pb, next, SEEK_SET);

                }

                if (res < 0) {

                    av_log(s, AV_LOG_ERROR, "error reading header metadata\n");

                    return res;

                }

                break;

            }

        }

        if (!metadata->read)

            avio_skip(s->pb, klv.length);

    }

    

    if (!essence_offset)  {

        av_log(s, AV_LOG_ERROR, "no essence\n");

        return AVERROR_INVALIDDATA;

    }

    avio_seek(s->pb, essence_offset, SEEK_SET);



    mxf_compute_essence_containers(mxf);



    return mxf_parse_structural_metadata(mxf);

}

    KLVPacket klv;     while (!s->pb->eof_reached) {         av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);             avio_skip(s->pb, klv.length);         return AVERROR_INVALIDDATA;             continue;                 break;     return mxf_parse_structural_metadata(mxf);
static int FUNC_0(AVFormatContext *VAR_0, AVFormatParameters *VAR_1)

{

    MXFContext *mxf = VAR_0->priv_data;

    KLVPacket klv;

    int64_t essence_offset = 0;



    mxf->last_forward_tell = INT64_MAX;



    if (!mxf_read_sync(VAR_0->pb, mxf_header_partition_pack_key, 14)) {

        av_log(VAR_0, AV_LOG_ERROR, "could not find header partition pack key\n");

        return AVERROR_INVALIDDATA;

    }

    avio_seek(VAR_0->pb, -14, SEEK_CUR);

    mxf->fc = VAR_0;

    mxf->run_in = avio_tell(VAR_0->pb);



    while (!VAR_0->pb->eof_reached) {

        const MXFMetadataReadTableEntry *VAR_2;



        if (klv_read_packet(&klv, VAR_0->pb) < 0) {

            

            if(mxf_parse_handle_partition_or_eof(mxf) <= 0)

                break;

            else

                continue;

        }



        PRINT_KEY(VAR_0, "read header", klv.key);

        av_dlog(VAR_0, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);

        if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||

            IS_KLV_KEY(klv.key, mxf_essence_element_key) ||

            IS_KLV_KEY(klv.key, mxf_avid_essence_element_key) ||

            IS_KLV_KEY(klv.key, mxf_system_item_key)) {

            if (!mxf->current_partition->essence_offset) {

                compute_partition_essence_offset(VAR_0, mxf, &klv);

            }



            if (!essence_offset)

                essence_offset = klv.offset;



            

            if (mxf_parse_handle_essence(mxf) <= 0)

                break;

            continue;

        } else if (!memcmp(klv.key, mxf_header_partition_pack_key, 13) &&

                   klv.key[13] >= 2 && klv.key[13] <= 4 && mxf->current_partition) {

            

            if(mxf_parse_handle_partition_or_eof(mxf) <= 0)

                break;

        }



        for (VAR_2 = mxf_metadata_read_table; VAR_2->read; VAR_2++) {

            if (IS_KLV_KEY(klv.key, VAR_2->key)) {

                int res;

                if (klv.key[5] == 0x53) {

                    res = mxf_read_local_tags(mxf, &klv, VAR_2->read, VAR_2->ctx_size, VAR_2->type);

                } else {

                    uint64_t next = avio_tell(VAR_0->pb) + klv.length;

                    res = VAR_2->read(mxf, VAR_0->pb, 0, klv.length, klv.key, klv.offset);

                    avio_seek(VAR_0->pb, next, SEEK_SET);

                }

                if (res < 0) {

                    av_log(VAR_0, AV_LOG_ERROR, "error reading header VAR_2\n");

                    return res;

                }

                break;

            }

        }

        if (!VAR_2->read)

            avio_skip(VAR_0->pb, klv.length);

    }

    

    if (!essence_offset)  {

        av_log(VAR_0, AV_LOG_ERROR, "no essence\n");

        return AVERROR_INVALIDDATA;

    }

    avio_seek(VAR_0->pb, essence_offset, SEEK_SET);



    mxf_compute_essence_containers(mxf);



    return mxf_parse_structural_metadata(mxf);

}

static int FUNC_0(AVFormatContext *VAR_0, AVFormatParameters *VAR_1)
{ MXFContext *mxf = VAR_0->priv_data; KLVPacket klv; int64_t essence_offset = 0; mxf->last_forward_tell = INT64_MAX; if (!mxf_read_sync(VAR_0->pb, mxf_header_partition_pack_key, 14)) { av_log(VAR_0, AV_LOG_ERROR, "could not find header partition pack key\n"); return AVERROR_INVALIDDATA; } avio_seek(VAR_0->pb, -14, SEEK_CUR); mxf->fc = VAR_0; mxf->run_in = avio_tell(VAR_0->pb); while (!VAR_0->pb->eof_reached) { const MXFMetadataReadTableEntry *VAR_2; if (klv_read_packet(&klv, VAR_0->pb) < 0) { if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
break; else
continue; } PRINT_KEY(VAR_0, "read header", klv.key); av_dlog(VAR_0, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset); if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key) ||
IS_KLV_KEY(klv.key, mxf_essence_element_key) ||
IS_KLV_KEY(klv.key, mxf_avid_essence_element_key) ||
IS_KLV_KEY(klv.key, mxf_system_item_key)) { if (!mxf->current_partition->essence_offset) { compute_partition_essence_offset(VAR_0, mxf, &klv); } if (!essence_offset)
essence_offset = klv.offset; if (mxf_parse_handle_essence(mxf) <= 0)
break; continue; } else if (!memcmp(klv.key, mxf_header_partition_pack_key, 13) && klv.key[13] >= 2 && klv.key[13] <= 4 && mxf->current_partition) { if(mxf_parse_handle_partition_or_eof(mxf) <= 0)
break; } for (VAR_2 = mxf_metadata_read_table; VAR_2->read; VAR_2++) { if (IS_KLV_KEY(klv.key, VAR_2->key)) { int res; if (klv.key[5] == 0x53) { res = mxf_read_local_tags(mxf, &klv, VAR_2->read, VAR_2->ctx_size, VAR_2->type); } else { uint64_t next = avio_tell(VAR_0->pb) + klv.length; res = VAR_2->read(mxf, VAR_0->pb, 0, klv.length, klv.key, klv.offset); avio_seek(VAR_0->pb, next, SEEK_SET); } if (res < 0) { av_log(VAR_0, AV_LOG_ERROR, "error reading header VAR_2\n"); return res; } break; } } if (!VAR_2->read)
avio_skip(VAR_0->pb, klv.length); } if (!essence_offset)  { av_log(VAR_0, AV_LOG_ERROR, "no essence\n"); return AVERROR_INVALIDDATA; } avio_seek(VAR_0->pb, essence_offset, SEEK_SET); mxf_compute_essence_containers(mxf); return mxf_parse_structural_metadata(mxf); }