# -------------------------------------------------------------------------
# A series of patches taken from ffmpeg's git,
# to fix ffmpeg compilation with x264 builds >= 153
# It takes more to actually use this new simultaneous 8- and 10-bitdepth
# in libx264, but we need to wait for a newer ffmpeg.
# -------------------------------------------------------------------------
From 89f704cabab446afc8ba6ecea76714a51b1df32b Mon Sep 17 00:00:00 2001
From: James Almer <jamrial@gmail.com>
Date: Mon, 25 Dec 2017 19:40:42 -0300
Subject: [PATCH] avcodec/libx264: use the pixfmt descriptor to check for high
 bit depths

The x264_bit_depth constant has been removed in newer x264 builds.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/libx264.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index e2455e18dec..2d36c5e5666 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -272,6 +272,7 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
                       int *got_packet)
 {
     X264Context *x4 = ctx->priv_data;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(ctx->pix_fmt);
     x264_nal_t *nal;
     int nnal, i, ret;
     x264_picture_t pic_out = {0};
@@ -279,7 +280,7 @@ static int X264_frame(AVCodecContext *ctx, AVPacket *pkt, const AVFrame *frame,
 
     x264_picture_init( &x4->pic );
     x4->pic.img.i_csp   = x4->params.i_csp;
-    if (x264_bit_depth > 8)
+    if (desc->comp[0].depth > 8)
         x4->pic.img.i_csp |= X264_CSP_HIGH_DEPTH;
     x4->pic.img.i_plane = avfmt2_num_planes(ctx->pix_fmt);
 
# -------------------------------------------------------------------------
From 2a111c99a60fdf4fe5eea2b073901630190c6c93 Mon Sep 17 00:00:00 2001
From: James Almer <jamrial@gmail.com>
Date: Mon, 25 Dec 2017 19:41:09 -0300
Subject: [PATCH] avcodec/libx264: fix compilation with x264 builds >= 153

x264 now supports multibitdepth builds, with a slightly changed API to
request bitdepth during initialization.

Reviewed-by: Ricardo Constantino <wiiaboo@gmail.com>
Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/libx264.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index 2d36c5e5666..754383e080d 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -724,6 +724,9 @@ FF_ENABLE_DEPRECATION_WARNINGS
 
     x4->params.i_width          = avctx->width;
     x4->params.i_height         = avctx->height;
+#if X264_BUILD >= 153
+    x4->params.i_bitdepth       = av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth;
+#endif
     av_reduce(&sw, &sh, avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 4096);
     x4->params.vui.i_sar_width  = sw;
     x4->params.vui.i_sar_height = sh;
@@ -837,6 +840,24 @@ FF_ENABLE_DEPRECATION_WARNINGS
     return 0;
 }
 
+static const enum AVPixelFormat pix_fmts[] = {
+    AV_PIX_FMT_YUV420P,
+    AV_PIX_FMT_YUVJ420P,
+    AV_PIX_FMT_YUV422P,
+    AV_PIX_FMT_YUVJ422P,
+    AV_PIX_FMT_YUV444P,
+    AV_PIX_FMT_YUVJ444P,
+    AV_PIX_FMT_YUV420P10,
+    AV_PIX_FMT_YUV422P10,
+    AV_PIX_FMT_YUV444P10,
+    AV_PIX_FMT_NV12,
+    AV_PIX_FMT_NV16,
+    AV_PIX_FMT_NV20,
+#ifdef X264_CSP_NV21
+    AV_PIX_FMT_NV21,
+#endif
+    AV_PIX_FMT_NONE
+};
 static const enum AVPixelFormat pix_fmts_8bit[] = {
     AV_PIX_FMT_YUV420P,
     AV_PIX_FMT_YUVJ420P,
@@ -874,12 +895,14 @@ static const enum AVPixelFormat pix_fmts_8bit_rgb[] = {
 
 static av_cold void X264_init_static(AVCodec *codec)
 {
-    if (x264_bit_depth == 8)
+    if (X264_BIT_DEPTH == 8)
         codec->pix_fmts = pix_fmts_8bit;
-    else if (x264_bit_depth == 9)
+    else if (X264_BIT_DEPTH == 9)
         codec->pix_fmts = pix_fmts_9bit;
-    else if (x264_bit_depth == 10)
+    else if (X264_BIT_DEPTH == 10)
         codec->pix_fmts = pix_fmts_10bit;
+    else /* X264_BIT_DEPTH == 0 */
+        codec->pix_fmts = pix_fmts;
 }
 
 #define OFFSET(x) offsetof(X264Context, x)

# -------------------------------------------------------------------------
From 7e60c74329353db28db00552028bc88cd2a52346 Mon Sep 17 00:00:00 2001
From: James Almer <jamrial@gmail.com>
Date: Tue, 26 Dec 2017 19:40:27 -0300
Subject: [PATCH] avcodec/libx264: set supported pix_fmts at runtime rather
 than build time

This partially reverts a change in behavior introduced in 2a111c99a60fdf4fe5eea2b073901630190c6c93.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavcodec/libx264.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
index 754383e080d..0e714f16152 100644
--- a/libavcodec/libx264.c
+++ b/libavcodec/libx264.c
@@ -895,14 +895,16 @@ static const enum AVPixelFormat pix_fmts_8bit_rgb[] = {
 
 static av_cold void X264_init_static(AVCodec *codec)
 {
-    if (X264_BIT_DEPTH == 8)
+#if X264_BUILD < 153
+    if (x264_bit_depth == 8)
         codec->pix_fmts = pix_fmts_8bit;
-    else if (X264_BIT_DEPTH == 9)
+    else if (x264_bit_depth == 9)
         codec->pix_fmts = pix_fmts_9bit;
-    else if (X264_BIT_DEPTH == 10)
+    else if (x264_bit_depth == 10)
         codec->pix_fmts = pix_fmts_10bit;
-    else /* X264_BIT_DEPTH == 0 */
-        codec->pix_fmts = pix_fmts;
+#else
+    codec->pix_fmts = pix_fmts;
+#endif
 }
 
 #define OFFSET(x) offsetof(X264Context, x)

# -------------------------------------------------------------------------
