#include "libavutil/intreadwrite.h"
#include "libavutil/parseutils.h"
#include "libavutil/timecode.h"
+#include "libavutil/opt.h"
#include "avformat.h"
#include "internal.h"
#include "mxf.h"
} MXFIndexTable;
typedef struct MXFContext {
+ const AVClass *class; /**< Class for private options. */
MXFPartition *partitions;
unsigned partitions_count;
MXFOP op;
int last_forward_partition;
int nb_index_tables;
MXFIndexTable *index_tables;
+ int eia608_extract;
} MXFContext;
/* NOTE: klv_offset is not set (-1) for local keys */
return mxf->partitions[a].body_sid;
}
+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;
+}
+
/* XXX: use AVBitStreamFilter */
static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt, int64_t length)
{
continue;
}
- if (physical_track->edit_rate.num <= 0 ||
- physical_track->edit_rate.den <= 0) {
- av_log(mxf->fc, AV_LOG_WARNING,
- "Invalid edit rate (%d/%d) found on structural"
- " component #%d, defaulting to 25/1\n",
- physical_track->edit_rate.num,
- physical_track->edit_rate.den, i);
- physical_track->edit_rate = (AVRational){25, 1};
- }
+ if (physical_track->edit_rate.num <= 0 ||
+ physical_track->edit_rate.den <= 0) {
+ av_log(mxf->fc, AV_LOG_WARNING,
+ "Invalid edit rate (%d/%d) found on structural"
+ " component #%d, defaulting to 25/1\n",
+ physical_track->edit_rate.num,
+ physical_track->edit_rate.den, i);
+ physical_track->edit_rate = (AVRational){25, 1};
+ }
for (k = 0; k < physical_track->sequence->structural_components_count; k++) {
if (!(mxf_tc = mxf_resolve_timecode_component(mxf, &physical_track->sequence->structural_components_refs[k])))
default:
av_log(mxf->fc, AV_LOG_INFO, "Unknown frame layout type: %d\n", descriptor->frame_layout);
}
+
+ if (st->codecpar->codec_id == AV_CODEC_ID_PRORES) {
+ switch (descriptor->essence_codec_ul[14]) {
+ case 1: st->codecpar->codec_tag = MKTAG('a','p','c','o'); break;
+ case 2: st->codecpar->codec_tag = MKTAG('a','p','c','s'); break;
+ case 3: st->codecpar->codec_tag = MKTAG('a','p','c','n'); break;
+ case 4: st->codecpar->codec_tag = MKTAG('a','p','c','h'); break;
+ case 5: st->codecpar->codec_tag = MKTAG('a','p','4','h'); break;
+ case 6: st->codecpar->codec_tag = MKTAG('a','p','4','x'); break;
+ }
+ }
+
if (st->codecpar->codec_id == AV_CODEC_ID_RAWVIDEO) {
st->codecpar->format = descriptor->pix_fmt;
if (st->codecpar->format == AV_PIX_FMT_NONE) {
if (st->codecpar->codec_id == AV_CODEC_ID_NONE || (st->codecpar->codec_id == AV_CODEC_ID_PCM_ALAW && (enum AVCodecID)container_ul->id != AV_CODEC_ID_NONE))
st->codecpar->codec_id = (enum AVCodecID)container_ul->id;
st->codecpar->channels = descriptor->channels;
- st->codecpar->bits_per_coded_sample = descriptor->bits_per_sample;
if (descriptor->sample_rate.den > 0) {
st->codecpar->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
} else if (st->codecpar->codec_id == AV_CODEC_ID_MP2) {
st->need_parsing = AVSTREAM_PARSE_FULL;
}
+ st->codecpar->bits_per_coded_sample = av_get_bits_per_sample(st->codecpar->codec_id);
} else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
enum AVMediaType type;
container_ul = mxf_get_codec_ul(mxf_data_essence_container_uls, essence_container_ul);
st->codecpar->codec_type = type;
if (container_ul->desc)
av_dict_set(&st->metadata, "data_type", container_ul->desc, 0);
+ if (mxf->eia608_extract &&
+ !strcmp(container_ul->desc, "vbi_vanc_smpte_436M")) {
+ st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
+ st->codecpar->codec_id = AV_CODEC_ID_EIA_608;
+ }
}
if (descriptor->extradata) {
if (!ff_alloc_extradata(st->codecpar, descriptor->extradata_size)) {
#define SET_TS_METADATA(pb, name, var, str) do { \
var = avio_rb64(pb); \
- if ((ret = avpriv_dict_set_timestamp(&s->metadata, name, mxf_timestamp_to_int64(var)) < 0)) \
+ if ((ret = avpriv_dict_set_timestamp(&s->metadata, name, mxf_timestamp_to_int64(var))) < 0) \
return ret; \
} while (0)
static int mxf_set_audio_pts(MXFContext *mxf, AVCodecParameters *par,
AVPacket *pkt)
{
- MXFTrack *track = mxf->fc->streams[pkt->stream_index]->priv_data;
+ AVStream *st = mxf->fc->streams[pkt->stream_index];
+ MXFTrack *track = st->priv_data;
int64_t bits_per_sample = par->bits_per_coded_sample;
if (!bits_per_sample)
if ( par->channels <= 0
|| bits_per_sample <= 0
|| par->channels * (int64_t)bits_per_sample < 8)
- return AVERROR(EINVAL);
- track->sample_count += pkt->size / (par->channels * (int64_t)bits_per_sample / 8);
+ track->sample_count = mxf_compute_sample_count(mxf, st, av_rescale_q(track->sample_count, st->time_base, av_inv_q(track->edit_rate)) + 1);
+ else
+ track->sample_count += pkt->size / (par->channels * (int64_t)bits_per_sample / 8);
+
return 0;
}
if (ret < 0)
return ret;
} else if (track) {
+ pkt->dts = pkt->pts = track->sample_count;
+ pkt->duration = 1;
track->sample_count++;
}
return 0;
mxf->current_klv_data = (KLVPacket){{0}};
return ret;
}
+ } else if (mxf->eia608_extract &&
+ s->streams[index]->codecpar->codec_id == AV_CODEC_ID_EIA_608) {
+ ret = mxf_get_eia608_packet(s, s->streams[index], pkt, klv.length);
+ if (ret < 0) {
+ mxf->current_klv_data = (KLVPacket){{0}};
+ return ret;
+ }
} else {
ret = av_get_packet(s->pb, pkt, klv.length);
if (ret < 0) {
return 0;
}
+static const AVOption options[] = {
+ { "eia608_extract", "extract eia 608 captions from s436m track",
+ offsetof(MXFContext, eia608_extract), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
+ AV_OPT_FLAG_DECODING_PARAM },
+ { NULL },
+};
+
+static const AVClass demuxer_class = {
+ .class_name = "mxf",
+ .item_name = av_default_item_name,
+ .option = options,
+ .version = LIBAVUTIL_VERSION_INT,
+ .category = AV_CLASS_CATEGORY_DEMUXER,
+};
+
AVInputFormat ff_mxf_demuxer = {
.name = "mxf",
.long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
.read_packet = mxf_read_packet,
.read_close = mxf_read_close,
.read_seek = mxf_read_seek,
+ .priv_class = &demuxer_class,
};