summaryrefslogtreecommitdiff
path: root/libavformat
diff options
context:
space:
mode:
authorJan Ekström <jan.ekstrom@24i.com>2021-07-15 09:57:48 +0300
committerJan Ekström <jeebjp@gmail.com>2021-10-04 17:55:27 +0300
commit7a446b1179301b6b9d05a7d39574e75e8fa5a862 (patch)
tree543651142a0d40eb59d970c2c450a612a51f690b /libavformat
parent847fd8de7c13abe41ca59464014f17c56555ef7b (diff)
avformat/{isom,mov,movenc}: add support for CMAF DASH roles
This information is coded in a standard MP4 KindBox and utilizes the scheme and values as per the DASH role scheme defined in MPEG-DASH. Other schemes are technically allowed, but where multiple schemes define the same concepts, the DASH scheme should be utilized. Such flagging is additionally utilized by the DASH-IF CMAF ingest specification, enabling an encoder to inform the following component of the roles of the incoming media streams. A test is added for this functionality in a similar manner to the matroska test. Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
Diffstat (limited to 'libavformat')
-rw-r--r--libavformat/isom.c19
-rw-r--r--libavformat/isom.h12
-rw-r--r--libavformat/mov.c91
-rw-r--r--libavformat/movenc.c51
-rw-r--r--libavformat/version.h2
5 files changed, 174 insertions, 1 deletions
diff --git a/libavformat/isom.c b/libavformat/isom.c
index 4df5440023..300ba927c2 100644
--- a/libavformat/isom.c
+++ b/libavformat/isom.c
@@ -430,3 +430,22 @@ void ff_mov_write_chan(AVIOContext *pb, int64_t channel_layout)
}
avio_wb32(pb, 0); // mNumberChannelDescriptions
}
+
+static const struct MP4TrackKindValueMapping dash_role_map[] = {
+ { AV_DISPOSITION_HEARING_IMPAIRED|AV_DISPOSITION_CAPTIONS,
+ "caption" },
+ { AV_DISPOSITION_COMMENT,
+ "commentary" },
+ { AV_DISPOSITION_VISUAL_IMPAIRED|AV_DISPOSITION_DESCRIPTIONS,
+ "description" },
+ { AV_DISPOSITION_DUB,
+ "dub" },
+ { AV_DISPOSITION_FORCED,
+ "forced-subtitle" },
+ { 0, NULL }
+};
+
+const struct MP4TrackKindMapping ff_mov_track_kind_table[] = {
+ { "urn:mpeg:dash:role:2011", dash_role_map },
+ { 0, NULL }
+};
diff --git a/libavformat/isom.h b/libavformat/isom.h
index 34a58c79b7..c62fcf2bfe 100644
--- a/libavformat/isom.h
+++ b/libavformat/isom.h
@@ -390,4 +390,16 @@ static inline enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
#define MOV_ISMV_TTML_TAG MKTAG('d', 'f', 'x', 'p')
#define MOV_MP4_TTML_TAG MKTAG('s', 't', 'p', 'p')
+struct MP4TrackKindValueMapping {
+ int disposition;
+ const char *value;
+};
+
+struct MP4TrackKindMapping {
+ const char *scheme_uri;
+ const struct MP4TrackKindValueMapping *value_maps;
+};
+
+extern const struct MP4TrackKindMapping ff_mov_track_kind_table[];
+
#endif /* AVFORMAT_ISOM_H */
diff --git a/libavformat/mov.c b/libavformat/mov.c
index d0b8b2595b..a811bc7677 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -28,6 +28,7 @@
#include <stdint.h>
#include "libavutil/attributes.h"
+#include "libavutil/bprint.h"
#include "libavutil/channel_layout.h"
#include "libavutil/internal.h"
#include "libavutil/intreadwrite.h"
@@ -6853,6 +6854,95 @@ static int mov_read_dvcc_dvvc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
return 0;
}
+static int mov_read_kind(MOVContext *c, AVIOContext *pb, MOVAtom atom)
+{
+ AVFormatContext *ctx = c->fc;
+ AVStream *st = NULL;
+ AVBPrint scheme_buf, value_buf;
+ int64_t scheme_str_len = 0, value_str_len = 0;
+ int version, flags, ret = AVERROR_BUG;
+ int64_t size = atom.size;
+
+ if (atom.size < 6)
+ // 4 bytes for version + flags, 2x 1 byte for null
+ return AVERROR_INVALIDDATA;
+
+ if (c->fc->nb_streams < 1)
+ return 0;
+ st = c->fc->streams[c->fc->nb_streams-1];
+
+ version = avio_r8(pb);
+ flags = avio_rb24(pb);
+ size -= 4;
+
+ if (version != 0 || flags != 0) {
+ av_log(ctx, AV_LOG_ERROR,
+ "Unsupported 'kind' box with version %d, flags: %x",
+ version, flags);
+ return AVERROR_INVALIDDATA;
+ }
+
+ av_bprint_init(&scheme_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+ av_bprint_init(&value_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+ if ((scheme_str_len = ff_read_string_to_bprint_overwrite(pb, &scheme_buf,
+ size)) < 0) {
+ ret = scheme_str_len;
+ goto cleanup;
+ }
+
+ if (scheme_str_len + 1 >= size) {
+ // we need to have another string, even if nullptr.
+ // we check with + 1 since we expect that if size was not hit,
+ // an additional null was read.
+ ret = AVERROR_INVALIDDATA;
+ goto cleanup;
+ }
+
+ size -= scheme_str_len + 1;
+
+ if ((value_str_len = ff_read_string_to_bprint_overwrite(pb, &value_buf,
+ size)) < 0) {
+ ret = value_str_len;
+ goto cleanup;
+ }
+
+ if (value_str_len == size) {
+ // in case of no trailing null, box is not valid.
+ ret = AVERROR_INVALIDDATA;
+ goto cleanup;
+ }
+
+ av_log(ctx, AV_LOG_TRACE,
+ "%s stream %d KindBox(scheme: %s, value: %s)\n",
+ av_get_media_type_string(st->codecpar->codec_type),
+ st->index,
+ scheme_buf.str, value_buf.str);
+
+ for (int i = 0; ff_mov_track_kind_table[i].scheme_uri; i++) {
+ const struct MP4TrackKindMapping map = ff_mov_track_kind_table[i];
+ if (!av_strstart(scheme_buf.str, map.scheme_uri, NULL))
+ continue;
+
+ for (int j = 0; map.value_maps[j].disposition; j++) {
+ const struct MP4TrackKindValueMapping value_map = map.value_maps[j];
+ if (!av_strstart(value_buf.str, value_map.value, NULL))
+ continue;
+
+ st->disposition |= value_map.disposition;
+ }
+ }
+
+ ret = 0;
+
+cleanup:
+
+ av_bprint_finalize(&scheme_buf, NULL);
+ av_bprint_finalize(&value_buf, NULL);
+
+ return ret;
+}
+
static const MOVParseTableEntry mov_default_parse_table[] = {
{ MKTAG('A','C','L','R'), mov_read_aclr },
{ MKTAG('A','P','R','G'), mov_read_avid },
@@ -6950,6 +7040,7 @@ static const MOVParseTableEntry mov_default_parse_table[] = {
{ MKTAG('c','l','l','i'), mov_read_clli },
{ MKTAG('d','v','c','C'), mov_read_dvcc_dvvc },
{ MKTAG('d','v','v','C'), mov_read_dvcc_dvvc },
+{ MKTAG('k','i','n','d'), mov_read_kind },
{ 0, NULL }
};
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 1a2f24c410..d43a086f4b 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -3322,6 +3322,52 @@ static int mov_write_track_metadata(AVIOContext *pb, AVStream *st,
return update_size(pb, pos);
}
+static int mov_write_track_kind(AVIOContext *pb, const char *scheme_uri,
+ const char *value)
+{
+ int64_t pos = avio_tell(pb);
+
+ /* Box|FullBox basics */
+ avio_wb32(pb, 0); /* size placeholder */
+ ffio_wfourcc(pb, (const unsigned char *)"kind");
+ avio_w8(pb, 0); /* version = 0 */
+ avio_wb24(pb, 0); /* flags = 0 */
+
+ /* Required null-terminated scheme URI */
+ avio_write(pb, (const unsigned char *)scheme_uri,
+ strlen(scheme_uri));
+ avio_w8(pb, 0);
+
+ /* Optional value string */
+ if (value && value[0])
+ avio_write(pb, (const unsigned char *)value,
+ strlen(value));
+
+ avio_w8(pb, 0);
+
+ return update_size(pb, pos);
+}
+
+static int mov_write_track_kinds(AVIOContext *pb, AVStream *st)
+{
+ int ret = AVERROR_BUG;
+
+ for (int i = 0; ff_mov_track_kind_table[i].scheme_uri; i++) {
+ const struct MP4TrackKindMapping map = ff_mov_track_kind_table[i];
+
+ for (int j = 0; map.value_maps[j].disposition; j++) {
+ const struct MP4TrackKindValueMapping value_map = map.value_maps[j];
+ if (!(st->disposition & value_map.disposition))
+ continue;
+
+ if ((ret = mov_write_track_kind(pb, map.scheme_uri, value_map.value)) < 0)
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
static int mov_write_track_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
AVStream *st)
{
@@ -3339,6 +3385,11 @@ static int mov_write_track_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
if (mov->mode & (MODE_MP4|MODE_MOV))
mov_write_track_metadata(pb_buf, st, "name", "title");
+ if (mov->mode & MODE_MP4) {
+ if ((ret = mov_write_track_kinds(pb_buf, st)) < 0)
+ return ret;
+ }
+
if ((size = avio_get_dyn_buf(pb_buf, &buf)) > 0) {
avio_wb32(pb, size + 8);
ffio_wfourcc(pb, "udta");
diff --git a/libavformat/version.h b/libavformat/version.h
index 13df244d97..85090c8111 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -33,7 +33,7 @@
// Also please add any ticket numbers that you believe might be affected here
#define LIBAVFORMAT_VERSION_MAJOR 59
#define LIBAVFORMAT_VERSION_MINOR 5
-#define LIBAVFORMAT_VERSION_MICRO 100
+#define LIBAVFORMAT_VERSION_MICRO 101
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
LIBAVFORMAT_VERSION_MINOR, \