OCaml 4.14.0 rebuild
[arch-packages.git] / gst-libav / trunk / 0001-gst-libav-fix-build-with-ffmpeg-5.0.0.patch
blob46bb56e68d6d0a527f05d1965083af959d8d334c
1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Xi Ruoyao <xry111@mengyan1223.wang>
3 Date: Mon, 17 Jan 2022 01:33:47 +0800
4 Subject: [PATCH] gst-libav: fix build with ffmpeg-5.0.0
6 Latest ffmpeg has removed avcodec_get_context_defaults(), and its
7 documentation says a new AVCodecContext should be allocated for this
8 purpose. The pointer returned by avcodec_find_decoder() is now
9 const-qualified so we also need to adjust for it. And, AVCOL_RANGE_MPEG
10 is now rejected with strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL.
12 Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1531>
13 ---
14 ext/libav/gstavauddec.c | 22 ++++++++-------------
15 ext/libav/gstavaudenc.c | 40 +++++++++++++++++++--------------------
16 ext/libav/gstavcodecmap.c | 7 ++++---
17 ext/libav/gstavutils.c | 2 +-
18 ext/libav/gstavviddec.c | 28 +++++++++++----------------
19 ext/libav/gstavvidenc.c | 21 ++++++++++----------
20 6 files changed, 54 insertions(+), 66 deletions(-)
22 diff --git a/ext/libav/gstavauddec.c b/ext/libav/gstavauddec.c
23 index baf7aa55a9d9..b03a724001fa 100644
24 --- a/ext/libav/gstavauddec.c
25 +++ b/ext/libav/gstavauddec.c
26 @@ -168,59 +168,53 @@ gst_ffmpegauddec_finalize (GObject * object)
27 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) object;
29 av_frame_free (&ffmpegdec->frame);
31 - if (ffmpegdec->context != NULL) {
32 - gst_ffmpeg_avcodec_close (ffmpegdec->context);
33 - av_free (ffmpegdec->context);
34 - ffmpegdec->context = NULL;
35 - }
36 + avcodec_free_context (&ffmpegdec->context);
38 G_OBJECT_CLASS (parent_class)->finalize (object);
41 /* With LOCK */
42 static gboolean
43 gst_ffmpegauddec_close (GstFFMpegAudDec * ffmpegdec, gboolean reset)
45 GstFFMpegAudDecClass *oclass;
47 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
49 GST_LOG_OBJECT (ffmpegdec, "closing libav codec");
51 gst_caps_replace (&ffmpegdec->last_caps, NULL);
53 gst_ffmpeg_avcodec_close (ffmpegdec->context);
54 ffmpegdec->opened = FALSE;
56 - if (ffmpegdec->context->extradata) {
57 - av_free (ffmpegdec->context->extradata);
58 - ffmpegdec->context->extradata = NULL;
59 - }
60 + av_freep (&ffmpegdec->context->extradata);
62 if (reset) {
63 - if (avcodec_get_context_defaults3 (ffmpegdec->context,
64 - oclass->in_plugin) < 0) {
65 + avcodec_free_context (&ffmpegdec->context);
66 + ffmpegdec->context = avcodec_alloc_context3 (oclass->in_plugin);
67 + if (ffmpegdec->context == NULL) {
68 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
69 return FALSE;
71 ffmpegdec->context->opaque = ffmpegdec;
74 return TRUE;
77 static gboolean
78 gst_ffmpegauddec_start (GstAudioDecoder * decoder)
80 GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) decoder;
81 GstFFMpegAudDecClass *oclass;
83 oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
85 GST_OBJECT_LOCK (ffmpegdec);
86 - gst_ffmpeg_avcodec_close (ffmpegdec->context);
87 - if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
88 + avcodec_free_context (&ffmpegdec->context);
89 + ffmpegdec->context = avcodec_alloc_context3 (oclass->in_plugin);
90 + if (ffmpegdec->context == NULL) {
91 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
92 GST_OBJECT_UNLOCK (ffmpegdec);
93 return FALSE;
94 diff --git a/ext/libav/gstavaudenc.c b/ext/libav/gstavaudenc.c
95 index 3ff64325954a..689982f3e8bb 100644
96 --- a/ext/libav/gstavaudenc.c
97 +++ b/ext/libav/gstavaudenc.c
98 @@ -175,27 +175,25 @@ gst_ffmpegaudenc_finalize (GObject * object)
100 /* clean up remaining allocated data */
101 av_frame_free (&ffmpegaudenc->frame);
102 - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
103 - gst_ffmpeg_avcodec_close (ffmpegaudenc->refcontext);
104 - av_free (ffmpegaudenc->context);
105 - av_free (ffmpegaudenc->refcontext);
106 + avcodec_free_context (&ffmpegaudenc->context);
107 + avcodec_free_context (&ffmpegaudenc->refcontext);
109 G_OBJECT_CLASS (parent_class)->finalize (object);
112 static gboolean
113 gst_ffmpegaudenc_start (GstAudioEncoder * encoder)
115 GstFFMpegAudEnc *ffmpegaudenc = (GstFFMpegAudEnc *) encoder;
116 GstFFMpegAudEncClass *oclass =
117 (GstFFMpegAudEncClass *) G_OBJECT_GET_CLASS (ffmpegaudenc);
119 ffmpegaudenc->opened = FALSE;
120 ffmpegaudenc->need_reopen = FALSE;
122 - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
123 - if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
124 - oclass->in_plugin) < 0) {
125 + avcodec_free_context (&ffmpegaudenc->context);
126 + ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
127 + if (ffmpegaudenc->context == NULL) {
128 GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
129 return FALSE;
131 @@ -241,10 +239,10 @@ gst_ffmpegaudenc_set_format (GstAudioEncoder * encoder, GstAudioInfo * info)
133 /* close old session */
134 if (ffmpegaudenc->opened) {
135 - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
136 + avcodec_free_context (&ffmpegaudenc->context);
137 ffmpegaudenc->opened = FALSE;
138 - if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
139 - oclass->in_plugin) < 0) {
140 + ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
141 + if (ffmpegaudenc->context == NULL) {
142 GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
143 return FALSE;
145 @@ -286,55 +284,55 @@ gst_ffmpegaudenc_set_format (GstAudioEncoder * encoder, GstAudioInfo * info)
146 /* open codec */
147 if (gst_ffmpeg_avcodec_open (ffmpegaudenc->context, oclass->in_plugin) < 0) {
148 gst_caps_unref (allowed_caps);
149 - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
150 + avcodec_free_context (&ffmpegaudenc->context);
151 GST_DEBUG_OBJECT (ffmpegaudenc, "avenc_%s: Failed to open FFMPEG codec",
152 oclass->in_plugin->name);
153 - if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
154 - oclass->in_plugin) < 0)
155 + ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
156 + if (ffmpegaudenc->context == NULL)
157 GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
159 if ((oclass->in_plugin->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
160 ffmpegaudenc->context->strict_std_compliance !=
161 FF_COMPLIANCE_EXPERIMENTAL) {
162 GST_ELEMENT_ERROR (ffmpegaudenc, LIBRARY, SETTINGS,
163 ("Codec is experimental, but settings don't allow encoders to "
164 "produce output of experimental quality"),
165 ("This codec may not create output that is conformant to the specs "
166 "or of good quality. If you must use it anyway, set the "
167 "compliance property to experimental"));
169 return FALSE;
172 /* try to set this caps on the other side */
173 other_caps = gst_ffmpeg_codecid_to_caps (oclass->in_plugin->id,
174 ffmpegaudenc->context, TRUE);
176 if (!other_caps) {
177 gst_caps_unref (allowed_caps);
178 - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
179 + avcodec_free_context (&ffmpegaudenc->context);
180 GST_DEBUG ("Unsupported codec - no caps found");
181 - if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
182 - oclass->in_plugin) < 0)
183 + ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
184 + if (ffmpegaudenc->context == NULL)
185 GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
186 return FALSE;
189 icaps = gst_caps_intersect (allowed_caps, other_caps);
190 gst_caps_unref (allowed_caps);
191 gst_caps_unref (other_caps);
192 if (gst_caps_is_empty (icaps)) {
193 gst_caps_unref (icaps);
194 return FALSE;
196 icaps = gst_caps_fixate (icaps);
198 if (!gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (ffmpegaudenc),
199 icaps)) {
200 - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
201 + avcodec_free_context (&ffmpegaudenc->context);
202 gst_caps_unref (icaps);
203 - if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
204 - oclass->in_plugin) < 0)
205 + ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
206 + if (ffmpegaudenc->context == NULL)
207 GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
208 return FALSE;
210 @@ -403,8 +401,8 @@ buffer_info_free (void *opaque, guint8 * data)
211 gst_buffer_unmap (info->buffer, &info->map);
212 gst_buffer_unref (info->buffer);
213 } else {
214 - av_free (info->ext_data);
215 - av_free (info->ext_data_array);
216 + av_freep (&info->ext_data);
217 + av_freep (&info->ext_data_array);
219 g_slice_free (BufferInfo, info);
221 diff --git a/ext/libav/gstavcodecmap.c b/ext/libav/gstavcodecmap.c
222 index ebd4e0490cab..df3aa6bc181f 100644
223 --- a/ext/libav/gstavcodecmap.c
224 +++ b/ext/libav/gstavcodecmap.c
225 @@ -2355,7 +2355,7 @@ gst_ffmpeg_codecid_to_caps (enum AVCodecID codec_id,
228 if (buildcaps) {
229 - AVCodec *codec;
230 + const AVCodec *codec;
232 if ((codec = avcodec_find_decoder (codec_id)) ||
233 (codec = avcodec_find_encoder (codec_id))) {
234 @@ -3001,6 +3001,7 @@ gst_ffmpeg_videoinfo_to_context (GstVideoInfo * info, AVCodecContext * context)
235 context->color_range = AVCOL_RANGE_JPEG;
236 } else {
237 context->color_range = AVCOL_RANGE_MPEG;
238 + context->strict_std_compliance = FF_COMPLIANCE_UNOFFICIAL;
242 @@ -4373,19 +4374,19 @@ gst_ffmpeg_caps_to_codecid (const GstCaps * caps, AVCodecContext * context)
243 audio = TRUE;
244 } else if (!strncmp (mimetype, "audio/x-gst-av-", 15)) {
245 gchar ext[16];
246 - AVCodec *codec;
247 + const AVCodec *codec;
249 if (strlen (mimetype) <= 30 &&
250 sscanf (mimetype, "audio/x-gst-av-%s", ext) == 1) {
251 if ((codec = avcodec_find_decoder_by_name (ext)) ||
252 (codec = avcodec_find_encoder_by_name (ext))) {
253 id = codec->id;
254 audio = TRUE;
257 } else if (!strncmp (mimetype, "video/x-gst-av-", 15)) {
258 gchar ext[16];
259 - AVCodec *codec;
260 + const AVCodec *codec;
262 if (strlen (mimetype) <= 30 &&
263 sscanf (mimetype, "video/x-gst-av-%s", ext) == 1) {
264 diff --git a/ext/libav/gstavutils.c b/ext/libav/gstavutils.c
265 index 3780cff4f82c..f3878c37e273 100644
266 --- a/ext/libav/gstavutils.c
267 +++ b/ext/libav/gstavutils.c
268 @@ -36,7 +36,7 @@
269 const gchar *
270 gst_ffmpeg_get_codecid_longname (enum AVCodecID codec_id)
272 - AVCodec *codec;
273 + const AVCodec *codec;
274 /* Let's use what ffmpeg can provide us */
276 if ((codec = avcodec_find_decoder (codec_id)) ||
277 diff --git a/ext/libav/gstavviddec.c b/ext/libav/gstavviddec.c
278 index a14b6df3064a..f5197fbe1439 100644
279 --- a/ext/libav/gstavviddec.c
280 +++ b/ext/libav/gstavviddec.c
281 @@ -340,12 +340,7 @@ gst_ffmpegviddec_finalize (GObject * object)
282 GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
284 av_frame_free (&ffmpegdec->picture);
286 - if (ffmpegdec->context != NULL) {
287 - gst_ffmpeg_avcodec_close (ffmpegdec->context);
288 - av_free (ffmpegdec->context);
289 - ffmpegdec->context = NULL;
291 + avcodec_free_context (&ffmpegdec->context);
293 G_OBJECT_CLASS (parent_class)->finalize (object);
295 @@ -395,13 +390,11 @@ gst_ffmpegviddec_close (GstFFMpegVidDec * ffmpegdec, gboolean reset)
297 gst_buffer_replace (&ffmpegdec->palette, NULL);
299 - if (ffmpegdec->context->extradata) {
300 - av_free (ffmpegdec->context->extradata);
301 - ffmpegdec->context->extradata = NULL;
303 + av_freep (&ffmpegdec->context->extradata);
304 if (reset) {
305 - if (avcodec_get_context_defaults3 (ffmpegdec->context,
306 - oclass->in_plugin) < 0) {
307 + avcodec_free_context (&ffmpegdec->context);
308 + ffmpegdec->context = avcodec_alloc_context3 (oclass->in_plugin);
309 + if (ffmpegdec->context == NULL) {
310 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
311 return FALSE;
313 @@ -1801,7 +1794,7 @@ gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
314 if (side_data) {
315 GST_LOG_OBJECT (ffmpegdec,
316 "Found CC side data of type AV_FRAME_DATA_A53_CC, size %d",
317 - side_data->size);
318 + (int) side_data->size);
319 GST_MEMDUMP ("A53 CC", side_data->data, side_data->size);
321 /* do not add closed caption meta if it already exists */
322 @@ -2086,8 +2079,9 @@ gst_ffmpegviddec_start (GstVideoDecoder * decoder)
323 oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
325 GST_OBJECT_LOCK (ffmpegdec);
326 - gst_ffmpeg_avcodec_close (ffmpegdec->context);
327 - if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
328 + avcodec_free_context (&ffmpegdec->context);
329 + ffmpegdec->context = avcodec_alloc_context3 (oclass->in_plugin);
330 + if (ffmpegdec->context == NULL) {
331 GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
332 GST_OBJECT_UNLOCK (ffmpegdec);
333 return FALSE;
334 @@ -2382,10 +2376,10 @@ gst_ffmpegviddec_get_property (GObject * object,
336 switch (prop_id) {
337 case PROP_LOWRES:
338 - g_value_set_enum (value, ffmpegdec->context->lowres);
339 + g_value_set_enum (value, ffmpegdec->lowres);
340 break;
341 case PROP_SKIPFRAME:
342 - g_value_set_enum (value, ffmpegdec->context->skip_frame);
343 + g_value_set_enum (value, ffmpegdec->skip_frame);
344 break;
345 case PROP_DIRECT_RENDERING:
346 g_value_set_boolean (value, ffmpegdec->direct_rendering);
347 diff --git a/ext/libav/gstavvidenc.c b/ext/libav/gstavvidenc.c
348 index 0468d88804ce..2ed9e5ae64a1 100644
349 --- a/ext/libav/gstavvidenc.c
350 +++ b/ext/libav/gstavvidenc.c
351 @@ -224,33 +224,33 @@ gst_ffmpegvidenc_finalize (GObject * object)
352 av_frame_free (&ffmpegenc->picture);
353 gst_ffmpeg_avcodec_close (ffmpegenc->context);
354 gst_ffmpeg_avcodec_close (ffmpegenc->refcontext);
355 - av_free (ffmpegenc->context);
356 - av_free (ffmpegenc->refcontext);
357 + av_freep (&ffmpegenc->context);
358 + av_freep (&ffmpegenc->refcontext);
360 G_OBJECT_CLASS (parent_class)->finalize (object);
363 static gboolean
364 gst_ffmpegvidenc_set_format (GstVideoEncoder * encoder,
365 GstVideoCodecState * state)
367 GstCaps *other_caps;
368 GstCaps *allowed_caps;
369 GstCaps *icaps;
370 GstVideoCodecState *output_format;
371 enum AVPixelFormat pix_fmt;
372 GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
373 GstFFMpegVidEncClass *oclass =
374 (GstFFMpegVidEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
376 ffmpegenc->need_reopen = FALSE;
378 /* close old session */
379 if (ffmpegenc->opened) {
380 - gst_ffmpeg_avcodec_close (ffmpegenc->context);
381 + avcodec_free_context (&ffmpegenc->context);
382 ffmpegenc->opened = FALSE;
383 - if (avcodec_get_context_defaults3 (ffmpegenc->context,
384 - oclass->in_plugin) < 0) {
385 + ffmpegenc->context = avcodec_alloc_context3 (oclass->in_plugin);
386 + if (ffmpegenc->context == NULL) {
387 GST_DEBUG_OBJECT (ffmpegenc, "Failed to set context defaults");
388 return FALSE;
390 @@ -454,9 +454,9 @@ bad_input_fmt:
392 close_codec:
394 - gst_ffmpeg_avcodec_close (ffmpegenc->context);
395 - if (avcodec_get_context_defaults3 (ffmpegenc->context,
396 - oclass->in_plugin) < 0)
397 + avcodec_free_context (&ffmpegenc->context);
398 + ffmpegenc->context = avcodec_alloc_context3 (oclass->in_plugin);
399 + if (ffmpegenc->context == NULL)
400 GST_DEBUG_OBJECT (ffmpegenc, "Failed to set context defaults");
401 goto cleanup_stats_in;
403 @@ -896,8 +896,9 @@ gst_ffmpegvidenc_start (GstVideoEncoder * encoder)
404 ffmpegenc->need_reopen = FALSE;
406 /* close old session */
407 - gst_ffmpeg_avcodec_close (ffmpegenc->context);
408 - if (avcodec_get_context_defaults3 (ffmpegenc->context, oclass->in_plugin) < 0) {
409 + avcodec_free_context (&ffmpegenc->context);
410 + ffmpegenc->context = avcodec_alloc_context3 (oclass->in_plugin);
411 + if (ffmpegenc->context == NULL) {
412 GST_DEBUG_OBJECT (ffmpegenc, "Failed to set context defaults");
413 return FALSE;