From de0d9f41d89b32cdb442c1747bd74371d3cc37f0 Mon Sep 17 00:00:00 2001
From: Martin Vignali <martin.vignali@gmail.com>
Date: Wed, 24 Oct 2018 22:00:46 +0200
Subject: [PATCH 1/2] avcodec : add prores_metadata bsf for set the color 
 property of each prores file

---
 doc/bitstream_filters.texi       |  16 +++++
 libavcodec/Makefile              |   1 +
 libavcodec/bitstream_filters.c   |   1 +
 libavcodec/prores_metadata_bsf.c | 122 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 140 insertions(+)
 create mode 100644 libavcodec/prores_metadata_bsf.c

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 53470c01ec..9798e3218c 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -530,6 +530,22 @@ ffmpeg -i INPUT -c copy -bsf noise[=1] output.mkv
 @section null
 This bitstream filter passes the packets through unchanged.
 
+@section prores_metadata
+
+Modify color property metadata embedded in prores stream.
+
+@table @option
+@item color_primaries
+@item transfer_characteristics
+@item matrix_coefficients
+Set the color description fields in the stream.
+@end table
+
+Set Rec709 colorspace for each frame of the file
+@example
+ffmpeg -i INPUT -c copy -bsf:v prores_metadata=colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1 output.mov
+@end example
+
 @section remove_extra
 
 Remove extradata from packets.
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index a97055ef3f..9d7de13673 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1078,6 +1078,7 @@ OBJS-$(CONFIG_MP3_HEADER_DECOMPRESS_BSF)  += mp3_header_decompress_bsf.o \
 OBJS-$(CONFIG_MPEG2_METADATA_BSF)         += mpeg2_metadata_bsf.o
 OBJS-$(CONFIG_NOISE_BSF)                  += noise_bsf.o
 OBJS-$(CONFIG_NULL_BSF)                   += null_bsf.o
+OBJS-$(CONFIG_PRORES_METADATA_BSF)        += prores_metadata_bsf.o
 OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF)       += remove_extradata_bsf.o
 OBJS-$(CONFIG_TEXT2MOVSUB_BSF)            += movsub_bsf.o
 OBJS-$(CONFIG_TRACE_HEADERS_BSF)          += trace_headers_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 96b1746a75..2a8598bac2 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -47,6 +47,7 @@ extern const AVBitStreamFilter ff_mpeg4_unpack_bframes_bsf;
 extern const AVBitStreamFilter ff_mov2textsub_bsf;
 extern const AVBitStreamFilter ff_noise_bsf;
 extern const AVBitStreamFilter ff_null_bsf;
+extern const AVBitStreamFilter ff_prores_metadata_bsf;
 extern const AVBitStreamFilter ff_remove_extradata_bsf;
 extern const AVBitStreamFilter ff_text2movsub_bsf;
 extern const AVBitStreamFilter ff_trace_headers_bsf;
diff --git a/libavcodec/prores_metadata_bsf.c b/libavcodec/prores_metadata_bsf.c
new file mode 100644
index 0000000000..7831149471
--- /dev/null
+++ b/libavcodec/prores_metadata_bsf.c
@@ -0,0 +1,122 @@
+/*
+ * Prores Metadata bitstream filter
+ * Copyright (c) 2018 Jokyo Images
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Prores Metadata bitstream filter
+ * set frame colorspace property
+ */
+
+#include "libavutil/common.h"
+#include "libavutil/opt.h"
+#include "bsf.h"
+#include "bytestream.h"
+
+typedef struct ProresMetadataContext {
+    const AVClass *class;
+
+    int colour_primaries;
+    int transfer_characteristics;
+    int matrix_coefficients;
+} ProresMetadataContext;
+
+static int prores_metadata(AVBSFContext *bsf, AVPacket *pkt)
+{
+    ProresMetadataContext *ctx = bsf->priv_data;
+    int ret = 0;
+    int buf_size;
+    uint8_t *buf;
+
+    ret = ff_bsf_get_packet_ref(bsf, pkt);
+    if (ret < 0)
+        return ret;
+
+    buf = pkt->data;
+    buf_size = pkt->size;
+
+    /* check start of the prores frame */
+    if (buf_size < 28) {
+        av_log(bsf, AV_LOG_ERROR, "not enough data in prores frame\n");
+        ret = AVERROR_INVALIDDATA;
+        goto fail;
+    }
+
+    if (AV_RL32(buf + 4) != AV_RL32("icpf")) {
+        av_log(bsf, AV_LOG_ERROR, "invalid frame header\n");
+        ret = AVERROR_INVALIDDATA;
+        goto fail;
+    }
+    buf += 8;
+
+    if (AV_RB16(buf) < 28) {
+        av_log(bsf, AV_LOG_ERROR, "invalid frame header size\n");
+        ret = AVERROR_INVALIDDATA;
+        goto fail;
+    }
+
+    ret = av_packet_make_writable(pkt);
+    if (ret < 0)
+        goto fail;
+
+    /* set the new values */
+    buf[14] = ctx->colour_primaries;
+    buf[15] = ctx->transfer_characteristics;
+    buf[16] = ctx->matrix_coefficients;
+
+fail:
+    if (ret < 0)
+        av_packet_unref(pkt);
+    return ret;
+}
+
+static const enum AVCodecID codec_ids[] = {
+    AV_CODEC_ID_PRORES, AV_CODEC_ID_NONE,
+};
+
+#define OFFSET(x) offsetof(ProresMetadataContext, x)
+#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_BSF_PARAM)
+static const AVOption options[] = {
+    { "colour_primaries", "Set colour primaries",
+        OFFSET(colour_primaries), AV_OPT_TYPE_INT,
+        { .i64 = -1 }, -1, 255, FLAGS },
+    { "transfer_characteristics", "Set transfer characteristics",
+        OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
+        { .i64 = -1 }, -1, 255, FLAGS },
+    { "matrix_coefficients", "Set matrix coefficients",
+        OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
+        { .i64 = -1 }, -1, 255, FLAGS },
+    { NULL },
+};
+
+static const AVClass prores_metadata_class = {
+    .class_name = "prores_metadata_bsf",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+const AVBitStreamFilter ff_prores_metadata_bsf = {
+    .name       = "prores_metadata",
+    .filter     = prores_metadata,
+    .priv_data_size = sizeof(ProresMetadataContext),
+    .priv_class = &prores_metadata_class,
+    .codec_ids  = codec_ids,
+};
-- 
2.14.3 (Apple Git-98)

