From 8ae63b8301e6822686a7885202938fd6e4cba6f2 Mon Sep 17 00:00:00 2001
From: Kieran O'Leary <kieran.o.leary@gmail.com>
Date: Wed, 1 Feb 2017 12:06:38 +0000
Subject: [PATCH] avcodec/dpxenc: support colour metadata in DPX encoder, fixes
 ticket #6023

---
 doc/encoders.texi   | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/dpxenc.c | 25 ++++++++++++++---
 2 files changed, 99 insertions(+), 3 deletions(-)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index 8137465..d3d8eb2 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -1269,6 +1269,83 @@ disabled
 A description of some of the currently available video encoders
 follows.
 
+@section dpx
+
+DPX image encoder.
+
+@subsection Options
+
+@table @option
+@item trc @var{integer}
+Set the transfer characteristics as listed in Table 5A in SMPTE 268M-2003:
+
+@table @samp
+@item 0
+User Defined
+@item 1
+Printing Density
+@item 2
+Linear
+@item 3
+Logarithmic [to be defined by SMPTE I23 Technology Committee, sub-group on “Transfer Characteristics”]
+@item 4
+Unspecified Video
+@item 5
+SMPTE 274M
+@item 6
+ITU-R 709-4
+@item 7
+ITU-R 601-5 system B or G (625)
+@item 8
+ITU-R 601-5 system M (525)
+@item 9
+Composite video (NTSC); see SMPTE 170M
+@item 10
+Composite video (PAL); see ITU-R 624-4
+@item 11
+Z (depth) – linear
+@item 12
+Z (depth) – homogeneous (distance to screen and angle of view must also be specified in user-defined section)
+@end table
+
+Default value is @var{2}.
+
+@item clr @var{integer}
+Set the Colorimetric Specification as listed in Table 5B in SMPTE 268M-2003:
+
+@table @samp
+@item 0
+User Defined
+@item 1
+Printing Density
+@item 2
+Not applicable
+@item 3
+Not Applicable
+@item 4
+Unspecified Video
+@item 5
+SMPTE 274M
+@item 6
+ITU-R 709-4
+@item 7
+ITU-R 601-5 system B or G (625)
+@item 8
+ITU-R 601-5 system M (525)
+@item 9
+Composite video (NTSC); see SMPTE 170M
+@item 10
+Composite video (PAL); see ITU-R 624-4
+@item 11
+Not applicable
+@item 12
+Not applicable
+@end table
+
+Default value is @var{2}.
+
+@end table
+
 @section Hap
 
 Vidvox Hap video encoder.
diff --git a/libavcodec/dpxenc.c b/libavcodec/dpxenc.c
index a596033..3b0e890 100644
--- a/libavcodec/dpxenc.c
+++ b/libavcodec/dpxenc.c
@@ -24,15 +24,20 @@
 #include "libavutil/imgutils.h"
 #include "avcodec.h"
 #include "internal.h"
+#include "libavutil/opt.h"
 
 typedef struct DPXContext {
+    AVClass *class;
     int big_endian;
     int bits_per_component;
     int num_components;
     int descriptor;
     int planar;
+    int transfer_characteristic;
+    int colorimetric_specification;
 } DPXContext;
 
+
 static av_cold int encode_init(AVCodecContext *avctx)
 {
     DPXContext *s = avctx->priv_data;
@@ -218,8 +223,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     write32(buf + 772, avctx->width);
     write32(buf + 776, avctx->height);
     buf[800] = s->descriptor;
-    buf[801] = 2; /* linear transfer */
-    buf[802] = 2; /* linear colorimetric */
+    buf[801] = s->transfer_characteristic;
+    buf[802] = s->colorimetric_specification;
     buf[803] = s->bits_per_component;
     write16(buf + 804, (s->bits_per_component == 10 || s->bits_per_component == 12) ?
                        1 : 0); /* packing method */
@@ -275,7 +280,20 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
 
     return 0;
 }
+#define OFFSET(x) offsetof(DPXContext, x)
+#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    {"trc", "Transfer Characteristics", OFFSET(transfer_characteristic), AV_OPT_TYPE_INT, {.i64 = 2}, 0, 12, VE },
+    {"clr", "Colorimetric Specification", OFFSET(colorimetric_specification), AV_OPT_TYPE_INT, {.i64 = 2}, 0, 12, VE },
+    { NULL},
+};
 
+static const AVClass dpxenc_class = {
+    .class_name = "DPX encoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
 AVCodec ff_dpx_encoder = {
     .name           = "dpx",
     .long_name      = NULL_IF_CONFIG_SMALL("DPX (Digital Picture Exchange) image"),
@@ -293,4 +311,5 @@ AVCodec ff_dpx_encoder = {
         AV_PIX_FMT_GBRP10LE, AV_PIX_FMT_GBRP10BE,
         AV_PIX_FMT_GBRP12LE, AV_PIX_FMT_GBRP12BE,
         AV_PIX_FMT_NONE},
-};
+    .priv_class     = &dpxenc_class,
+};
\ No newline at end of file
-- 
2.7.4 (Apple Git-66)

