From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: "Jan Alexander Steffens (heftig)" <heftig@archlinux.org>
Date: Sat, 5 Oct 2024 04:21:10 +0200
Subject: [PATCH] x265enc: Unbreak build with x265 4.0

Following a [similar change][1] in FFmpeg. x265 also reverted the change
post-4.0 and FFmpeg [adapted again][2].

[1]: https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/1f801dfdb5066aadf0ade9cb5e94d620f33eacdc
[2]: https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/099f88b8641dfc299f3896d17d9addc5b9ae7799
---
 .../gst-plugins-bad/ext/x265/gstx265enc.c     | 29 ++++++++++++++-----
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/subprojects/gst-plugins-bad/ext/x265/gstx265enc.c b/subprojects/gst-plugins-bad/ext/x265/gstx265enc.c
index ee72389720ef..470d334b8ffb 100644
--- a/subprojects/gst-plugins-bad/ext/x265/gstx265enc.c
+++ b/subprojects/gst-plugins-bad/ext/x265/gstx265enc.c
@@ -1519,7 +1519,12 @@ gst_x265_enc_encode_frame (GstX265Enc * encoder, x265_picture * pic_in,
 {
   GstVideoCodecFrame *frame = NULL;
   GstBuffer *out_buf = NULL;
-  x265_picture pic_out;
+#if (X265_BUILD >= 210) && (X265_BUILD < 213)
+  x265_picture pics_out[MAX_SCALABLE_LAYERS], *pics_outp[MAX_SCALABLE_LAYERS];
+#else
+  x265_picture pics_out[1];
+#endif
+  x265_picture *pic_out = &pics_out[0];
   x265_nal *nal;
   int i_size, i, offset;
   int encoder_return;
@@ -1554,75 +1559,83 @@ gst_x265_enc_encode_frame (GstX265Enc * encoder, x265_picture * pic_in,
   if (G_UNLIKELY (update_latency))
     gst_x265_enc_set_latency (encoder);
 
+#if (X265_BUILD >= 210) && (X265_BUILD < 213)
+  for (i = 0; i < MAX_SCALABLE_LAYERS; i++)
+    pics_outp[i] = &pics_out[i];
+
   encoder_return = api->encoder_encode (encoder->x265enc,
-      &nal, i_nal, pic_in, &pic_out);
+      &nal, i_nal, pic_in, pics_outp);
+#else
+  encoder_return = api->encoder_encode (encoder->x265enc,
+      &nal, i_nal, pic_in, pic_out);
+#endif
 
   GST_DEBUG_OBJECT (encoder, "encoder result (%d) with %u nal units",
       encoder_return, *i_nal);
 
   if (encoder_return < 0) {
     GST_ELEMENT_ERROR (encoder, STREAM, ENCODE, ("Encode x265 frame failed."),
         ("x265_encoder_encode return code=%d", encoder_return));
     ret = GST_FLOW_ERROR;
     /* Make sure we finish this frame */
     frame = input_frame;
     goto out;
   }
 
   /* Input frame is now queued */
   if (input_frame)
     gst_video_codec_frame_unref (input_frame);
 
   if (!*i_nal) {
     ret = GST_FLOW_OK;
     GST_LOG_OBJECT (encoder, "no output yet");
     goto out;
   }
 
   frame = gst_video_encoder_get_frame (GST_VIDEO_ENCODER (encoder),
-      GPOINTER_TO_INT (pic_out.userData));
+      GPOINTER_TO_INT (pic_out->userData));
   g_assert (frame || !send);
 
   GST_DEBUG_OBJECT (encoder,
-      "output picture ready POC=%d system=%d frame found %d", pic_out.poc,
-      GPOINTER_TO_INT (pic_out.userData), frame != NULL);
+      "output picture ready POC=%d system=%d frame found %d", pic_out->poc,
+      GPOINTER_TO_INT (pic_out->userData), frame != NULL);
 
   if (!send || !frame) {
     GST_LOG_OBJECT (encoder, "not sending (%d) or frame not found (%d)", send,
         frame != NULL);
     ret = GST_FLOW_OK;
     goto out;
   }
 
   i_size = 0;
   offset = 0;
   for (i = 0; i < *i_nal; i++)
     i_size += nal[i].sizeBytes;
   out_buf = gst_buffer_new_allocate (NULL, i_size, NULL);
   for (i = 0; i < *i_nal; i++) {
     gst_buffer_fill (out_buf, offset, nal[i].payload, nal[i].sizeBytes);
     offset += nal[i].sizeBytes;
   }
 
-  if (pic_out.sliceType == X265_TYPE_IDR || pic_out.sliceType == X265_TYPE_I) {
+  if (pic_out->sliceType == X265_TYPE_IDR || pic_out->sliceType == X265_TYPE_I) {
     GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
   }
 
   frame->output_buffer = out_buf;
 
   if (encoder->push_header) {
     GstBuffer *header;
 
     header = gst_x265_enc_get_header_buffer (encoder);
     frame->output_buffer = gst_buffer_append (header, frame->output_buffer);
     encoder->push_header = FALSE;
   }
 
   GST_LOG_OBJECT (encoder,
       "output: dts %" G_GINT64_FORMAT " pts %" G_GINT64_FORMAT,
-      (gint64) pic_out.dts, (gint64) pic_out.pts);
+      (gint64) pic_out->dts, (gint64) pic_out->pts);
 
-  frame->dts = pic_out.dts + encoder->dts_offset;
+  frame->dts = pic_out->dts + encoder->dts_offset;
 
 out:
   if (frame) {
