+static int mxf_get_eia608_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt, int64_t length)
+{
+ int count = avio_rb16(s->pb);
+ int cdp_identifier, cdp_length, cdp_footer_id, ccdata_id, cc_count;
+ int line_num, sample_coding, sample_count;
+ int did, sdid, data_length;
+ int i, ret;
+
+ if (count != 1)
+ av_log(s, AV_LOG_WARNING, "unsupported multiple ANC packets (%d) per KLV packet\n", count);
+
+ for (i = 0; i < count; i++) {
+ if (length < 6) {
+ av_log(s, AV_LOG_ERROR, "error reading s436m packet %"PRId64"\n", length);
+ return AVERROR_INVALIDDATA;
+ }
+ line_num = avio_rb16(s->pb);
+ avio_r8(s->pb); // wrapping type
+ sample_coding = avio_r8(s->pb);
+ sample_count = avio_rb16(s->pb);
+ length -= 6 + 8 + sample_count;
+ if (line_num != 9 && line_num != 11)
+ continue;
+ if (sample_coding == 7 || sample_coding == 8 || sample_coding == 9) {
+ av_log(s, AV_LOG_WARNING, "unsupported s436m 10 bit sample coding\n");
+ continue;
+ }
+ if (length < 0)
+ return AVERROR_INVALIDDATA;
+
+ avio_rb32(s->pb); // array count
+ avio_rb32(s->pb); // array elem size
+ did = avio_r8(s->pb);
+ sdid = avio_r8(s->pb);
+ data_length = avio_r8(s->pb);
+ if (did != 0x61 || sdid != 1) {
+ av_log(s, AV_LOG_WARNING, "unsupported did or sdid: %x %x\n", did, sdid);
+ continue;
+ }
+ cdp_identifier = avio_rb16(s->pb); // cdp id
+ if (cdp_identifier != 0x9669) {
+ av_log(s, AV_LOG_ERROR, "wrong cdp identifier %x\n", cdp_identifier);
+ return AVERROR_INVALIDDATA;
+ }
+ cdp_length = avio_r8(s->pb);
+ avio_r8(s->pb); // cdp_frame_rate
+ avio_r8(s->pb); // cdp_flags
+ avio_rb16(s->pb); // cdp_hdr_sequence_cntr
+ ccdata_id = avio_r8(s->pb); // ccdata_id
+ if (ccdata_id != 0x72) {
+ av_log(s, AV_LOG_ERROR, "wrong cdp data section %x\n", ccdata_id);
+ return AVERROR_INVALIDDATA;
+ }
+ cc_count = avio_r8(s->pb) & 0x1f;
+ ret = av_get_packet(s->pb, pkt, cc_count * 3);
+ if (ret < 0)
+ return ret;
+ if (cdp_length - 9 - 4 < cc_count * 3) {
+ av_log(s, AV_LOG_ERROR, "wrong cdp size %d cc count %d\n", cdp_length, cc_count);
+ return AVERROR_INVALIDDATA;
+ }
+ avio_skip(s->pb, data_length - 9 - 4 - cc_count * 3);
+ cdp_footer_id = avio_r8(s->pb);
+ if (cdp_footer_id != 0x74) {
+ av_log(s, AV_LOG_ERROR, "wrong cdp footer section %x\n", cdp_footer_id);
+ return AVERROR_INVALIDDATA;
+ }
+ avio_rb16(s->pb); // cdp_ftr_sequence_cntr
+ avio_r8(s->pb); // packet_checksum
+ break;
+ }
+
+ return 0;
+}
+