--- /dev/null
+From 055ae13e08ed1af220001de7b55d5c9defe64476 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= <sebastian@centricular.com>
+Date: Mon, 9 Mar 2015 22:01:43 +0100
+Subject: avviddec: Error out if we try to allocate a buffer without being
+ negotiated
+
+Otherwise we just run into assertions because we should've errored out
+already.
+
+diff --git a/ext/libav/gstavviddec.c b/ext/libav/gstavviddec.c
+index 5a1b7ed..23f9c74 100644
+--- a/ext/libav/gstavviddec.c
++++ b/ext/libav/gstavviddec.c
+@@ -1114,6 +1114,9 @@ get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
+
+ GST_LOG_OBJECT (ffmpegdec, "get output buffer");
+
++ if (!ffmpegdec->output_state)
++ goto not_negotiated;
++
+ ret =
+ gst_video_decoder_allocate_output_frame (GST_VIDEO_DECODER (ffmpegdec),
+ frame);
+@@ -1154,9 +1157,14 @@ get_output_buffer (GstFFMpegVidDec * ffmpegdec, GstVideoCodecFrame * frame)
+ /* special cases */
+ alloc_failed:
+ {
+- GST_DEBUG_OBJECT (ffmpegdec, "pad_alloc failed");
++ GST_DEBUG_OBJECT (ffmpegdec, "allocation failed");
+ return ret;
+ }
++not_negotiated:
++ {
++ GST_DEBUG_OBJECT (ffmpegdec, "not negotiated");
++ return GST_FLOW_NOT_NEGOTIATED;
++ }
+ }
+
+ static void
+--
+cgit v0.10.2
+
--- /dev/null
+From 98f0b80c8d0ff5da7b3dc579a0230e9efeae87fd Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= <sebastian@centricular.com>
+Date: Tue, 2 Jun 2015 09:09:09 +0200
+Subject: avauddev: Unref decoded AVFrame after we're done with it
+
+Otherwise we might leak some memory, like all compressed data when using
+avdec_ac3.
+
+diff --git a/ext/libav/gstavauddec.c b/ext/libav/gstavauddec.c
+index dd32466..bf16857 100644
+--- a/ext/libav/gstavauddec.c
++++ b/ext/libav/gstavauddec.c
+@@ -590,6 +590,7 @@ gst_ffmpegauddec_audio_frame (GstFFMpegAudDec * ffmpegdec,
+ }
+
+ beach:
++ av_frame_unref (&frame);
+ GST_DEBUG_OBJECT (ffmpegdec, "return flow %d, out %p, len %d",
+ *ret, *outbuf, len);
+ return len;
+--
+cgit v0.10.2
+
--- /dev/null
+From ac54ee0473f71e0c7fc195883b9ac6ae15f302af Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= <sebastian@centricular.com>
+Date: Fri, 5 Jun 2015 11:57:37 +0200
+Subject: avviddec: Release stream lock while calling avcodec_decode_video2()
+
+It might call back into us from another thread and try to take
+the stream lock again, e.g. to allocate a buffer. Fixes avdec_h265
+not outputting anything.
+
+https://bugzilla.gnome.org/show_bug.cgi?id=726020
+
+diff --git a/ext/libav/gstavviddec.c b/ext/libav/gstavviddec.c
+index e2d8245..b876b18 100644
+--- a/ext/libav/gstavviddec.c
++++ b/ext/libav/gstavviddec.c
+@@ -1246,8 +1246,15 @@ gst_ffmpegviddec_video_frame (GstFFMpegVidDec * ffmpegdec,
+ GST_DEBUG_OBJECT (ffmpegdec, "copy pal %p %p", &packet, pal);
+ }
+
++ /* This might call into get_buffer() from another thread,
++ * which would cause a deadlock. Release the lock here
++ * and taking it again later seems safe
++ * See https://bugzilla.gnome.org/show_bug.cgi?id=726020
++ */
++ GST_VIDEO_DECODER_STREAM_UNLOCK (ffmpegdec);
+ len = avcodec_decode_video2 (ffmpegdec->context,
+ ffmpegdec->picture, have_data, &packet);
++ GST_VIDEO_DECODER_STREAM_LOCK (ffmpegdec);
+
+ GST_DEBUG_OBJECT (ffmpegdec, "after decode: len %d, have_data %d",
+ len, *have_data);
+--
+cgit v0.10.2
+
--- /dev/null
+From 07d185161ea5d543f2ae1c6485da79dd7ee01173 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Olivier=20Cr=C3=AAte?= <olivier.crete@collabora.com>
+Date: Tue, 2 Jun 2015 20:48:33 -0400
+Subject: avviddec: Post error message before returning a flow error
+
+This is required.
+
+diff --git a/ext/libav/gstavviddec.c b/ext/libav/gstavviddec.c
+index 23f9c74..e2d8245 100644
+--- a/ext/libav/gstavviddec.c
++++ b/ext/libav/gstavviddec.c
+@@ -1479,7 +1479,8 @@ gst_ffmpegviddec_handle_frame (GstVideoDecoder * decoder,
+ GST_TIME_ARGS (frame->pts), GST_TIME_ARGS (frame->duration));
+
+ if (!gst_buffer_map (frame->input_buffer, &minfo, GST_MAP_READ)) {
+- GST_ERROR_OBJECT (ffmpegdec, "Failed to map buffer");
++ GST_ELEMENT_ERROR (ffmpegdec, STREAM, DECODE, ("Decoding problem"),
++ ("Failed to map buffer for reading"));
+ return GST_FLOW_ERROR;
+ }
+
+--
+cgit v0.10.2
+
--- /dev/null
+# Copyright 1999-2015 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id$
+
+EAPI="5"
+inherit eutils flag-o-matic multilib-minimal
+
+MY_PN="gst-libav"
+DESCRIPTION="FFmpeg based gstreamer plugin"
+HOMEPAGE="http://gstreamer.freedesktop.org/modules/gst-libav.html"
+SRC_URI="http://gstreamer.freedesktop.org/src/${MY_PN}/${MY_PN}-${PV}.tar.xz"
+
+LICENSE="GPL-2"
+SLOT="1.0"
+KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~mips ~ppc ~ppc64 ~sparc ~x86 ~amd64-fbsd ~x86-fbsd"
+IUSE="libav +orc"
+
+RDEPEND="
+ >=media-libs/gstreamer-1.4.0:1.0[${MULTILIB_USEDEP}]
+ >=media-libs/gst-plugins-base-1.4.0:1.0[${MULTILIB_USEDEP}]
+ !libav? ( >=media-video/ffmpeg-2.2:0=[${MULTILIB_USEDEP}] )
+ libav? ( >=media-video/libav-10:0=[${MULTILIB_USEDEP}] )
+ orc? ( >=dev-lang/orc-0.4.17[${MULTILIB_USEDEP}] )
+"
+DEPEND="${RDEPEND}
+ app-arch/xz-utils
+ >=dev-util/gtk-doc-am-1.12
+ >=virtual/pkgconfig-0-r1[${MULTILIB_USEDEP}]
+"
+
+S="${WORKDIR}/${MY_PN}-${PV}"
+
+src_prepare() {
+ # Upstream patches
+ # avviddec: Error out if we try to allocate a buffer without being negotiated
+ epatch "${FILESDIR}"/${P}-allocate-buffer.patch
+
+ # avauddev: Unref decoded AVFrame after we're done with it
+ epatch "${FILESDIR}"/${P}-fix-memleak.patch
+
+ # avviddec: Post error message before returning a flow error
+ epatch "${FILESDIR}"/${P}-post-error.patch
+
+ # avviddec: Release stream lock while calling avcodec_decode_video2()
+ epatch "${FILESDIR}"/${P}-h265-fixes.patch
+}
+
+multilib_src_configure() {
+ GST_PLUGINS_BUILD=""
+ # always use system ffmpeg/libav if possible
+ ECONF_SOURCE=${S} \
+ econf \
+ --disable-maintainer-mode \
+ --with-package-name="Gentoo GStreamer ebuild" \
+ --with-package-origin="https://www.gentoo.org" \
+ --disable-fatal-warnings \
+ --with-system-libav \
+ $(use_enable orc)
+}
+
+multilib_src_compile() {
+ # Don't build with -Werror
+ emake ERROR_CFLAGS=
+}
+
+multilib_src_install_all() {
+ einstalldocs
+ prune_libtool_files --modules
+}