You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
12 KiB

  1. From 6ea56e7fd39c48e718a10b6e69ec214532789a59 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
  5. Latest ffmpeg has removed avcodec_get_context_defaults(), and its
  6. documentation says a new AVCodecContext should be allocated for this
  7. purpose. The pointer returned by avcodec_find_decoder() is now
  8. const-qualified so we also need to adjust for it. And, AVCOL_RANGE_MPEG
  9. is now rejected with strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL.
  10. Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1531>
  11. ---
  12. ext/libav/gstavauddec.c | 22 ++++------
  13. ext/libav/gstavaudenc.c | 40 +++++++++----------
  14. .../gst-libav/ext/libav/gstavcodecmap.c | 7 ++--
  15. ext/libav/gstavutils.c | 2 +-
  16. ext/libav/gstavviddec.c | 28 +++++--------
  17. ext/libav/gstavvidenc.c | 21 +++++-----
  18. 6 files changed, 54 insertions(+), 66 deletions(-)
  19. --- a/ext/libav/gstavauddec.c
  20. +++ b/ext/libav/gstavauddec.c
  21. @@ -168,12 +168,7 @@ gst_ffmpegauddec_finalize (GObject * obj
  22. GstFFMpegAudDec *ffmpegdec = (GstFFMpegAudDec *) object;
  23. av_frame_free (&ffmpegdec->frame);
  24. -
  25. - if (ffmpegdec->context != NULL) {
  26. - gst_ffmpeg_avcodec_close (ffmpegdec->context);
  27. - av_free (ffmpegdec->context);
  28. - ffmpegdec->context = NULL;
  29. - }
  30. + avcodec_free_context (&ffmpegdec->context);
  31. G_OBJECT_CLASS (parent_class)->finalize (object);
  32. }
  33. @@ -193,14 +188,12 @@ gst_ffmpegauddec_close (GstFFMpegAudDec
  34. gst_ffmpeg_avcodec_close (ffmpegdec->context);
  35. ffmpegdec->opened = FALSE;
  36. - if (ffmpegdec->context->extradata) {
  37. - av_free (ffmpegdec->context->extradata);
  38. - ffmpegdec->context->extradata = NULL;
  39. - }
  40. + av_freep (&ffmpegdec->context->extradata);
  41. if (reset) {
  42. - if (avcodec_get_context_defaults3 (ffmpegdec->context,
  43. - oclass->in_plugin) < 0) {
  44. + avcodec_free_context (&ffmpegdec->context);
  45. + ffmpegdec->context = avcodec_alloc_context3 (oclass->in_plugin);
  46. + if (ffmpegdec->context == NULL) {
  47. GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
  48. return FALSE;
  49. }
  50. @@ -219,8 +212,9 @@ gst_ffmpegauddec_start (GstAudioDecoder
  51. oclass = (GstFFMpegAudDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
  52. GST_OBJECT_LOCK (ffmpegdec);
  53. - gst_ffmpeg_avcodec_close (ffmpegdec->context);
  54. - if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
  55. + avcodec_free_context (&ffmpegdec->context);
  56. + ffmpegdec->context = avcodec_alloc_context3 (oclass->in_plugin);
  57. + if (ffmpegdec->context == NULL) {
  58. GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
  59. GST_OBJECT_UNLOCK (ffmpegdec);
  60. return FALSE;
  61. --- a/ext/libav/gstavaudenc.c
  62. +++ b/ext/libav/gstavaudenc.c
  63. @@ -175,10 +175,8 @@ gst_ffmpegaudenc_finalize (GObject * obj
  64. /* clean up remaining allocated data */
  65. av_frame_free (&ffmpegaudenc->frame);
  66. - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
  67. - gst_ffmpeg_avcodec_close (ffmpegaudenc->refcontext);
  68. - av_free (ffmpegaudenc->context);
  69. - av_free (ffmpegaudenc->refcontext);
  70. + avcodec_free_context (&ffmpegaudenc->context);
  71. + avcodec_free_context (&ffmpegaudenc->refcontext);
  72. G_OBJECT_CLASS (parent_class)->finalize (object);
  73. }
  74. @@ -193,9 +191,9 @@ gst_ffmpegaudenc_start (GstAudioEncoder
  75. ffmpegaudenc->opened = FALSE;
  76. ffmpegaudenc->need_reopen = FALSE;
  77. - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
  78. - if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
  79. - oclass->in_plugin) < 0) {
  80. + avcodec_free_context (&ffmpegaudenc->context);
  81. + ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
  82. + if (ffmpegaudenc->context == NULL) {
  83. GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
  84. return FALSE;
  85. }
  86. @@ -241,10 +239,10 @@ gst_ffmpegaudenc_set_format (GstAudioEnc
  87. /* close old session */
  88. if (ffmpegaudenc->opened) {
  89. - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
  90. + avcodec_free_context (&ffmpegaudenc->context);
  91. ffmpegaudenc->opened = FALSE;
  92. - if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
  93. - oclass->in_plugin) < 0) {
  94. + ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
  95. + if (ffmpegaudenc->context == NULL) {
  96. GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
  97. return FALSE;
  98. }
  99. @@ -286,11 +284,11 @@ gst_ffmpegaudenc_set_format (GstAudioEnc
  100. /* open codec */
  101. if (gst_ffmpeg_avcodec_open (ffmpegaudenc->context, oclass->in_plugin) < 0) {
  102. gst_caps_unref (allowed_caps);
  103. - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
  104. + avcodec_free_context (&ffmpegaudenc->context);
  105. GST_DEBUG_OBJECT (ffmpegaudenc, "avenc_%s: Failed to open FFMPEG codec",
  106. oclass->in_plugin->name);
  107. - if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
  108. - oclass->in_plugin) < 0)
  109. + ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
  110. + if (ffmpegaudenc->context == NULL)
  111. GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
  112. if ((oclass->in_plugin->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
  113. @@ -312,10 +310,10 @@ gst_ffmpegaudenc_set_format (GstAudioEnc
  114. if (!other_caps) {
  115. gst_caps_unref (allowed_caps);
  116. - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
  117. + avcodec_free_context (&ffmpegaudenc->context);
  118. GST_DEBUG ("Unsupported codec - no caps found");
  119. - if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
  120. - oclass->in_plugin) < 0)
  121. + ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
  122. + if (ffmpegaudenc->context == NULL)
  123. GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
  124. return FALSE;
  125. }
  126. @@ -331,10 +329,10 @@ gst_ffmpegaudenc_set_format (GstAudioEnc
  127. if (!gst_audio_encoder_set_output_format (GST_AUDIO_ENCODER (ffmpegaudenc),
  128. icaps)) {
  129. - gst_ffmpeg_avcodec_close (ffmpegaudenc->context);
  130. + avcodec_free_context (&ffmpegaudenc->context);
  131. gst_caps_unref (icaps);
  132. - if (avcodec_get_context_defaults3 (ffmpegaudenc->context,
  133. - oclass->in_plugin) < 0)
  134. + ffmpegaudenc->context = avcodec_alloc_context3 (oclass->in_plugin);
  135. + if (ffmpegaudenc->context == NULL)
  136. GST_DEBUG_OBJECT (ffmpegaudenc, "Failed to set context defaults");
  137. return FALSE;
  138. }
  139. @@ -403,8 +401,8 @@ buffer_info_free (void *opaque, guint8 *
  140. gst_buffer_unmap (info->buffer, &info->map);
  141. gst_buffer_unref (info->buffer);
  142. } else {
  143. - av_free (info->ext_data);
  144. - av_free (info->ext_data_array);
  145. + av_freep (&info->ext_data);
  146. + av_freep (&info->ext_data_array);
  147. }
  148. g_slice_free (BufferInfo, info);
  149. }
  150. --- a/ext/libav/gstavcodecmap.c
  151. +++ b/ext/libav/gstavcodecmap.c
  152. @@ -2331,7 +2331,7 @@ gst_ffmpeg_codecid_to_caps (enum AVCodec
  153. }
  154. if (buildcaps) {
  155. - AVCodec *codec;
  156. + const AVCodec *codec;
  157. if ((codec = avcodec_find_decoder (codec_id)) ||
  158. (codec = avcodec_find_encoder (codec_id))) {
  159. @@ -2975,6 +2975,7 @@ gst_ffmpeg_videoinfo_to_context (GstVide
  160. context->color_range = AVCOL_RANGE_JPEG;
  161. } else {
  162. context->color_range = AVCOL_RANGE_MPEG;
  163. + context->strict_std_compliance = FF_COMPLIANCE_UNOFFICIAL;
  164. }
  165. }
  166. @@ -4330,7 +4331,7 @@ gst_ffmpeg_caps_to_codecid (const GstCap
  167. audio = TRUE;
  168. } else if (!strncmp (mimetype, "audio/x-gst-av-", 15)) {
  169. gchar ext[16];
  170. - AVCodec *codec;
  171. + const AVCodec *codec;
  172. if (strlen (mimetype) <= 30 &&
  173. sscanf (mimetype, "audio/x-gst-av-%s", ext) == 1) {
  174. @@ -4342,7 +4343,7 @@ gst_ffmpeg_caps_to_codecid (const GstCap
  175. }
  176. } else if (!strncmp (mimetype, "video/x-gst-av-", 15)) {
  177. gchar ext[16];
  178. - AVCodec *codec;
  179. + const AVCodec *codec;
  180. if (strlen (mimetype) <= 30 &&
  181. sscanf (mimetype, "video/x-gst-av-%s", ext) == 1) {
  182. --- a/ext/libav/gstavutils.c
  183. +++ b/ext/libav/gstavutils.c
  184. @@ -36,7 +36,7 @@
  185. const gchar *
  186. gst_ffmpeg_get_codecid_longname (enum AVCodecID codec_id)
  187. {
  188. - AVCodec *codec;
  189. + const AVCodec *codec;
  190. /* Let's use what ffmpeg can provide us */
  191. if ((codec = avcodec_find_decoder (codec_id)) ||
  192. --- a/ext/libav/gstavviddec.c
  193. +++ b/ext/libav/gstavviddec.c
  194. @@ -320,12 +320,7 @@ gst_ffmpegviddec_finalize (GObject * obj
  195. GstFFMpegVidDec *ffmpegdec = (GstFFMpegVidDec *) object;
  196. av_frame_free (&ffmpegdec->picture);
  197. -
  198. - if (ffmpegdec->context != NULL) {
  199. - gst_ffmpeg_avcodec_close (ffmpegdec->context);
  200. - av_free (ffmpegdec->context);
  201. - ffmpegdec->context = NULL;
  202. - }
  203. + avcodec_free_context (&ffmpegdec->context);
  204. G_OBJECT_CLASS (parent_class)->finalize (object);
  205. }
  206. @@ -363,13 +358,11 @@ gst_ffmpegviddec_close (GstFFMpegVidDec
  207. gst_buffer_replace (&ffmpegdec->palette, NULL);
  208. - if (ffmpegdec->context->extradata) {
  209. - av_free (ffmpegdec->context->extradata);
  210. - ffmpegdec->context->extradata = NULL;
  211. - }
  212. + av_freep (&ffmpegdec->context->extradata);
  213. if (reset) {
  214. - if (avcodec_get_context_defaults3 (ffmpegdec->context,
  215. - oclass->in_plugin) < 0) {
  216. + avcodec_free_context (&ffmpegdec->context);
  217. + ffmpegdec->context = avcodec_alloc_context3 (oclass->in_plugin);
  218. + if (ffmpegdec->context == NULL) {
  219. GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
  220. return FALSE;
  221. }
  222. @@ -1704,7 +1697,7 @@ gst_ffmpegviddec_video_frame (GstFFMpegV
  223. if (side_data) {
  224. GST_LOG_OBJECT (ffmpegdec,
  225. "Found CC side data of type AV_FRAME_DATA_A53_CC, size %d",
  226. - side_data->size);
  227. + (int) side_data->size);
  228. GST_MEMDUMP ("A53 CC", side_data->data, side_data->size);
  229. /* do not add closed caption meta if it already exists */
  230. @@ -1966,8 +1959,9 @@ gst_ffmpegviddec_start (GstVideoDecoder
  231. oclass = (GstFFMpegVidDecClass *) (G_OBJECT_GET_CLASS (ffmpegdec));
  232. GST_OBJECT_LOCK (ffmpegdec);
  233. - gst_ffmpeg_avcodec_close (ffmpegdec->context);
  234. - if (avcodec_get_context_defaults3 (ffmpegdec->context, oclass->in_plugin) < 0) {
  235. + avcodec_free_context (&ffmpegdec->context);
  236. + ffmpegdec->context = avcodec_alloc_context3 (oclass->in_plugin);
  237. + if (ffmpegdec->context == NULL) {
  238. GST_DEBUG_OBJECT (ffmpegdec, "Failed to set context defaults");
  239. GST_OBJECT_UNLOCK (ffmpegdec);
  240. return FALSE;
  241. @@ -2261,10 +2255,10 @@ gst_ffmpegviddec_get_property (GObject *
  242. switch (prop_id) {
  243. case PROP_LOWRES:
  244. - g_value_set_enum (value, ffmpegdec->context->lowres);
  245. + g_value_set_enum (value, ffmpegdec->lowres);
  246. break;
  247. case PROP_SKIPFRAME:
  248. - g_value_set_enum (value, ffmpegdec->context->skip_frame);
  249. + g_value_set_enum (value, ffmpegdec->skip_frame);
  250. break;
  251. case PROP_DIRECT_RENDERING:
  252. g_value_set_boolean (value, ffmpegdec->direct_rendering);
  253. --- a/ext/libav/gstavvidenc.c
  254. +++ b/ext/libav/gstavvidenc.c
  255. @@ -224,8 +224,8 @@ gst_ffmpegvidenc_finalize (GObject * obj
  256. av_frame_free (&ffmpegenc->picture);
  257. gst_ffmpeg_avcodec_close (ffmpegenc->context);
  258. gst_ffmpeg_avcodec_close (ffmpegenc->refcontext);
  259. - av_free (ffmpegenc->context);
  260. - av_free (ffmpegenc->refcontext);
  261. + av_freep (&ffmpegenc->context);
  262. + av_freep (&ffmpegenc->refcontext);
  263. G_OBJECT_CLASS (parent_class)->finalize (object);
  264. }
  265. @@ -247,10 +247,10 @@ gst_ffmpegvidenc_set_format (GstVideoEnc
  266. /* close old session */
  267. if (ffmpegenc->opened) {
  268. - gst_ffmpeg_avcodec_close (ffmpegenc->context);
  269. + avcodec_free_context (&ffmpegenc->context);
  270. ffmpegenc->opened = FALSE;
  271. - if (avcodec_get_context_defaults3 (ffmpegenc->context,
  272. - oclass->in_plugin) < 0) {
  273. + ffmpegenc->context = avcodec_alloc_context3 (oclass->in_plugin);
  274. + if (ffmpegenc->context == NULL) {
  275. GST_DEBUG_OBJECT (ffmpegenc, "Failed to set context defaults");
  276. return FALSE;
  277. }
  278. @@ -454,9 +454,9 @@ bad_input_fmt:
  279. }
  280. close_codec:
  281. {
  282. - gst_ffmpeg_avcodec_close (ffmpegenc->context);
  283. - if (avcodec_get_context_defaults3 (ffmpegenc->context,
  284. - oclass->in_plugin) < 0)
  285. + avcodec_free_context (&ffmpegenc->context);
  286. + ffmpegenc->context = avcodec_alloc_context3 (oclass->in_plugin);
  287. + if (ffmpegenc->context == NULL)
  288. GST_DEBUG_OBJECT (ffmpegenc, "Failed to set context defaults");
  289. goto cleanup_stats_in;
  290. }
  291. @@ -896,8 +896,9 @@ gst_ffmpegvidenc_start (GstVideoEncoder
  292. ffmpegenc->need_reopen = FALSE;
  293. /* close old session */
  294. - gst_ffmpeg_avcodec_close (ffmpegenc->context);
  295. - if (avcodec_get_context_defaults3 (ffmpegenc->context, oclass->in_plugin) < 0) {
  296. + avcodec_free_context (&ffmpegenc->context);
  297. + ffmpegenc->context = avcodec_alloc_context3 (oclass->in_plugin);
  298. + if (ffmpegenc->context == NULL) {
  299. GST_DEBUG_OBJECT (ffmpegenc, "Failed to set context defaults");
  300. return FALSE;
  301. }