+static int mxf_read_packet_old(AVFormatContext *s, AVPacket *pkt)
+{
+ KLVPacket klv;
+
+ while (!s->pb->eof_reached) {
+ if (klv_read_packet(&klv, s->pb) < 0)
+ return -1;
+ PRINT_KEY(s, "read packet", klv.key);
+ av_dlog(s, "size %"PRIu64" offset %#"PRIx64"\n", klv.length, klv.offset);
+ if (IS_KLV_KEY(klv.key, mxf_encrypted_triplet_key)) {
+ int res = mxf_decrypt_triplet(s, pkt, &klv);
+ if (res < 0) {
+ av_log(s, AV_LOG_ERROR, "invalid encoded triplet\n");
+ return -1;
+ }
+ return 0;
+ }
+ if (IS_KLV_KEY(klv.key, mxf_essence_element_key) ||
+ IS_KLV_KEY(klv.key, mxf_avid_essence_element_key)) {
+ int index = mxf_get_stream_index(s, &klv);
+ if (index < 0) {
+ av_log(s, AV_LOG_ERROR, "error getting stream index %d\n", AV_RB32(klv.key+12));
+ goto skip;
+ }
+ if (s->streams[index]->discard == AVDISCARD_ALL)
+ goto skip;
+ /* check for 8 channels AES3 element */
+ if (klv.key[12] == 0x06 && klv.key[13] == 0x01 && klv.key[14] == 0x10) {
+ if (mxf_get_d10_aes3_packet(s->pb, s->streams[index], pkt, klv.length) < 0) {
+ av_log(s, AV_LOG_ERROR, "error reading D-10 aes3 frame\n");
+ return -1;
+ }
+ } else {
+ int ret = av_get_packet(s->pb, pkt, klv.length);
+ if (ret < 0)
+ return ret;
+ }
+ pkt->stream_index = index;
+ pkt->pos = klv.offset;
+
+ if (s->streams[index]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
+ mxf_packet_timestamps(s->priv_data, pkt); /* offset -> EditUnit -> DTS/PTS */
+
+ return 0;
+ } else
+ skip:
+ avio_skip(s->pb, klv.length);
+ }
+ return AVERROR_EOF;
+}
+
+static int mxf_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+ MXFContext *mxf = s->priv_data;
+ int ret, size;
+ int64_t ret64, pos, next_pos;
+ AVStream *st;
+ MXFIndexTable *t;
+
+ if (mxf->op != OPAtom)
+ return mxf_read_packet_old(s, pkt);
+
+ /* OPAtom - clip wrapped demuxing */
+ st = s->streams[0];
+ t = &mxf->index_tables[0];
+
+ if (mxf->current_edit_unit >= st->duration)
+ return AVERROR_EOF;
+
+ if ((ret = mxf_edit_unit_absolute_offset(mxf, t, mxf->current_edit_unit, NULL, &pos, 1)) < 0)
+ return ret;
+
+ /* compute size by finding the next edit unit or the end of the essence container
+ * not pretty, but it works */
+ if ((ret = mxf_edit_unit_absolute_offset(mxf, t, mxf->current_edit_unit + 1, NULL, &next_pos, 0)) < 0 &&
+ (next_pos = mxf_essence_container_end(mxf, t->body_sid)) <= 0) {
+ av_log(s, AV_LOG_ERROR, "unable to compute the size of the last packet\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ if ((size = next_pos - pos) <= 0) {
+ av_log(s, AV_LOG_ERROR, "bad size: %i\n", size);
+ return AVERROR_INVALIDDATA;
+ }
+
+ if ((ret64 = avio_seek(s->pb, pos, SEEK_SET)) < 0)
+ return ret64;
+
+ if ((ret = av_get_packet(s->pb, pkt, size)) != size)
+ return ret < 0 ? ret : AVERROR_EOF;
+
+ if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && t->ptses &&
+ mxf->current_edit_unit >= 0 && mxf->current_edit_unit < t->nb_ptses) {
+ pkt->dts = mxf->current_edit_unit + t->first_dts;
+ pkt->pts = t->ptses[mxf->current_edit_unit];
+ }
+
+ pkt->stream_index = 0;
+ mxf->current_edit_unit++;
+
+ return 0;
+}
+
+