From 230cceb84e1b6af14373f6276accd70096b3b8c6 Mon Sep 17 00:00:00 2001
From: Boris Grozev <boris@jitsi.org>
Date: Sat, 26 Jan 2013 19:43:59 +0000
Subject: [PATCH] Sets error resilient flags on the VP8 encoder (and fixes
 typos).

---
 src/native/vpx/README                         | 17 +++++++
 .../org_jitsi_impl_neomedia_codec_video_VPX.c | 15 ++++++
 .../org_jitsi_impl_neomedia_codec_video_VPX.h | 10 ++++
 .../jitsi/impl/neomedia/codec/video/VPX.java  | 51 +++++++++++++++++--
 .../neomedia/codec/video/vp8/VPXDecoder.java  |  8 +--
 .../neomedia/codec/video/vp8/VPXEncoder.java  |  4 +-
 6 files changed, 96 insertions(+), 9 deletions(-)
 create mode 100644 src/native/vpx/README

diff --git a/src/native/vpx/README b/src/native/vpx/README
new file mode 100644
index 00000000..a631e78a
--- /dev/null
+++ b/src/native/vpx/README
@@ -0,0 +1,17 @@
+To build the libjnvpx library:
+
+Configure the sources with
+./configure --disable-examples --disable-debug --disable-md5 --disable-shared \
+	--enable-vp8 --enable-realtime-only --enable-error-concealment 
+	
+For linux and MAC OS X add --enable-pic
+and a suitable target: 
+    --target=universal-darwin9-gcc
+    --target=x86-linux-gcc
+    --target=x86_64-linux-gcc
+
+If there are errors while linking on linux x86_64 try to add --extra-cflags='-fvisibility=protected'
+
+
+Run the 'libvpx' ant target from the libjitsi/ directory, setting the 'libvpx'
+property. 
diff --git a/src/native/vpx/org_jitsi_impl_neomedia_codec_video_VPX.c b/src/native/vpx/org_jitsi_impl_neomedia_codec_video_VPX.c
index d8e7a8cf..3540672a 100644
--- a/src/native/vpx/org_jitsi_impl_neomedia_codec_video_VPX.c
+++ b/src/native/vpx/org_jitsi_impl_neomedia_codec_video_VPX.c
@@ -547,6 +547,21 @@ DEFINE_ENC_CFG_INT_PROPERTY_SETTER(profile, g_profile)
 DEFINE_ENC_CFG_INT_PROPERTY_SETTER(threads, g_threads)
 DEFINE_ENC_CFG_INT_PROPERTY_SETTER(w, g_w)
 DEFINE_ENC_CFG_INT_PROPERTY_SETTER(h, g_h)
+
+/*
+ * Method:    codec_enc_cfg_set_error_resilient
+ */
+JNIEXPORT void JNICALL
+Java_org_jitsi_impl_neomedia_codec_video_VPX_codec_1enc_1cfg_1set_1error_1resilient
+    (JNIEnv *env,
+     jclass clazz,
+     jlong cfg,
+     jint flags)
+{
+    ((vpx_codec_enc_cfg_t *) (intptr_t) cfg)->g_error_resilient
+        = (vpx_codec_er_flags_t) flags;
+}
+
 DEFINE_ENC_CFG_INT_PROPERTY_SETTER(rc_1target_1bitrate, rc_target_bitrate)
 DEFINE_ENC_CFG_INT_PROPERTY_SETTER(rc_1dropframe_1thresh, rc_dropframe_thresh)
 DEFINE_ENC_CFG_INT_PROPERTY_SETTER(rc_1resize_1allowed, rc_resize_allowed)
diff --git a/src/native/vpx/org_jitsi_impl_neomedia_codec_video_VPX.h b/src/native/vpx/org_jitsi_impl_neomedia_codec_video_VPX.h
index 16c62bbe..03042669 100644
--- a/src/native/vpx/org_jitsi_impl_neomedia_codec_video_VPX.h
+++ b/src/native/vpx/org_jitsi_impl_neomedia_codec_video_VPX.h
@@ -13,6 +13,8 @@ extern "C" {
 #define org_jitsi_impl_neomedia_codec_video_VPX_CODEC_LIST_END 9L
 #undef org_jitsi_impl_neomedia_codec_video_VPX_CODEC_USE_XMA
 #define org_jitsi_impl_neomedia_codec_video_VPX_CODEC_USE_XMA 1L
+#undef org_jitsi_impl_neomedia_codec_video_VPX_CODEC_USE_OUTPUT_PARTITION
+#define org_jitsi_impl_neomedia_codec_video_VPX_CODEC_USE_OUTPUT_PARTITION 131072L
 #undef org_jitsi_impl_neomedia_codec_video_VPX_IMG_FMT_I420
 #define org_jitsi_impl_neomedia_codec_video_VPX_IMG_FMT_I420 258L
 #undef org_jitsi_impl_neomedia_codec_video_VPX_RC_MODE_VBR
@@ -401,6 +403,14 @@ JNIEXPORT void JNICALL Java_org_jitsi_impl_neomedia_codec_video_VPX_codec_1enc_1
 JNIEXPORT void JNICALL Java_org_jitsi_impl_neomedia_codec_video_VPX_codec_1enc_1cfg_1set_1h
   (JNIEnv *, jclass, jlong, jint);
 
+/*
+ * Class:     org_jitsi_impl_neomedia_codec_video_VPX
+ * Method:    codec_enc_cfg_set_error_resilient
+ * Signature: (JI)V
+ */
+JNIEXPORT void JNICALL Java_org_jitsi_impl_neomedia_codec_video_VPX_codec_1enc_1cfg_1set_1error_1resilient
+  (JNIEnv *, jclass, jlong, jint);
+
 /*
  * Class:     org_jitsi_impl_neomedia_codec_video_VPX
  * Method:    codec_enc_cfg_set_rc_target_bitrate
diff --git a/src/org/jitsi/impl/neomedia/codec/video/VPX.java b/src/org/jitsi/impl/neomedia/codec/video/VPX.java
index d44b7928..439b8db8 100644
--- a/src/org/jitsi/impl/neomedia/codec/video/VPX.java
+++ b/src/org/jitsi/impl/neomedia/codec/video/VPX.java
@@ -31,6 +31,39 @@ public class VPX {
      */
     public static final int CODEC_USE_XMA = 0x00000001;
 
+    /**
+     * Output one partition at a time. Each partition is returned in its own
+     * <tt>VPX_CODEC_CX_FRAME_PKT</tt>.
+     */
+    public static final int CODEC_USE_OUTPUT_PARTITION = 0x20000;
+
+    /**
+     * Improve resiliency against losses of whole frames.
+     *
+     * To set this option for an encoder, enable this bit in the value passed
+     * to <tt>vpx_enc_cft_set_error_resilient</tt> for the encoder's
+     * configuration.
+     *
+     * Corresponds to <tt>VPX_ERROR_RESILIENT_DEFAULT</tt> from
+     * <tt>vpx/vpx_encoder.h</tt>
+     */
+    public static final int ERROR_RESILIENT_DEFAULT = 0x1;
+
+    /**
+     * The frame partitions are independently decodable by the bool decoder,
+     * meaning that partitions can be decoded even though earlier partitions
+     * have been lost. Note that intra predicition is still done over the
+     * partition boundary.
+     *
+     * To set this option for Coan encoder, enable this bit in the value passed
+     * to <tt>vpx_enc_cft_set_error_resilient</tt> for the encoder's
+     * configuration.
+     *
+     * Corresponds to <tt>VPX_ERROR_RESILIENT_PARTITIONS</tt> from
+     * <tt>vpx/vpx_encoder.h</tt>
+     */
+    public static final int ERROR_RESILIENT_PARTITIONS = 0x2;
+
     /**
      * I420 format constant
      * Corresponds to <tt>VPX_IMG_FMT_I420</tt> from <tt>vpx/vpx_image.h</tt>
@@ -147,7 +180,7 @@ public static native int codec_decode(long context,
      * <tt>codec_decode</tt>.
      *
      * @param context The decoder context to use.
-     * @param iter Iterator storage, initialized by setting it's first element
+     * @param iter Iterator storage, initialized by setting its first element
      * to 0.
      *
      * @return Pointer to a <tt>vpx_image_t</tt> describing the decoded frame,
@@ -183,7 +216,7 @@ public static native long codec_get_frame(long context,
      *
      * @param context The codec context to use.
      * @param mmap Pointer to a <tt>vpx_codec_mmap_t</tt> to populate
-     * @param iter Iterator storage, initialized by setting it's first element
+     * @param iter Iterator storage, initialized by setting its first element
      * to 0.
      *
      * @return <tt>CODEC_OK</tt> on success, or an error code otherwise. The
@@ -249,7 +282,7 @@ public static native int codec_enc_config_set(long context,
      * <tt>offset0</tt>, <tt>offset1</tt> and <tt>offset2</tt>.
      *
      * Note that <tt>buf</tt> and the offsets describe where the frames is
-     * stored, but <tt>img</tt> has to have all of it's other parameters (format,
+     * stored, but <tt>img</tt> has to have all of its other parameters (format,
      * dimensions, strides) already set.
      *
      * The reason <tt>buf</tt> and the offsets are treated differently is to
@@ -291,7 +324,7 @@ public static native int codec_encode(long context,
      * application's muxer.
      *
      * @param context The codec context to use.
-     * @param iter Iterator storage, initialized by setting it's first element
+     * @param iter Iterator storage, initialized by setting its first element
      * to 0.
      *
      * @return Pointer to a vpx_codec_cx_pkt_t containing the output data
@@ -622,6 +655,16 @@ public static native void codec_enc_cfg_set_w(long cfg,
     public static native void codec_enc_cfg_set_h(long cfg,
                                                   int value);
 
+    /**
+     * Sets the <tt>g_error_resilient</tt> field of a
+     * <tt>vpx_codec_enc_cfg_t</tt>.
+     *
+     * @param cfg Pointer to a <tt>vpx_codec_enc_cfg_t</tt>.
+     * @param value The value to set.
+     */
+    public static native void codec_enc_cfg_set_error_resilient(long cfg,
+                                                                int value);
+
     /**
      * Sets the <tt>rc_target_bitrate</tt> field of a
      * <tt>vpx_codec_enc_cfg_t</tt>.
diff --git a/src/org/jitsi/impl/neomedia/codec/video/vp8/VPXDecoder.java b/src/org/jitsi/impl/neomedia/codec/video/vp8/VPXDecoder.java
index 405cb609..4bae7dc8 100644
--- a/src/org/jitsi/impl/neomedia/codec/video/vp8/VPXDecoder.java
+++ b/src/org/jitsi/impl/neomedia/codec/video/vp8/VPXDecoder.java
@@ -48,7 +48,7 @@ public class VPXDecoder
 
     /**
      * Iterator for the frames in the decoder context. Can be re-initialized by
-     * setting it's only element to 0.
+     * setting its only element to 0.
      */
     private long[] iter = new long[1];
 
@@ -265,13 +265,13 @@ private void updateOutputFormat(int width, int height, float frameRate)
     }
 
     /**
-     * Allocates a new AVFrame and set it's data fields to the data fields
-     * from the <tt>vpx_image_t</tt> pointed to by <tt>img</tt>. Also set it's
+     * Allocates a new AVFrame and set its data fields to the data fields
+     * from the <tt>vpx_image_t</tt> pointed to by <tt>img</tt>. Also set its
      * 'linesize' according to <tt>img</tt>.
      *
      * @param img pointer to a <tt>vpx_image_t</tt> whose data will be used
      *
-     * @return an AVFrame instance with it's data fields set to the fields from
+     * @return an AVFrame instance with its data fields set to the fields from
      * <tt>img</tt>
      */
     private AVFrame makeAVFrame(long img)
diff --git a/src/org/jitsi/impl/neomedia/codec/video/vp8/VPXEncoder.java b/src/org/jitsi/impl/neomedia/codec/video/vp8/VPXEncoder.java
index 06001b52..fb496370 100644
--- a/src/org/jitsi/impl/neomedia/codec/video/vp8/VPXEncoder.java
+++ b/src/org/jitsi/impl/neomedia/codec/video/vp8/VPXEncoder.java
@@ -57,7 +57,7 @@ public class VPXEncoder
 
     /**
      * Iterator for the compressed frames in the encoder context. Can be
-     * re-initialized by setting it's only element to 0.
+     * re-initialized by setting its only element to 0.
      */
     private long[] iter = new long[1];
 
@@ -179,6 +179,8 @@ protected void doOpen() throws ResourceUnavailableException
         VPX.codec_enc_cfg_set_kf_mode(cfg, VPX.KF_MODE_AUTO);
         VPX.codec_enc_cfg_set_w(cfg, width);
         VPX.codec_enc_cfg_set_h(cfg, height);
+        VPX.codec_enc_cfg_set_error_resilient(cfg,
+            VPX.ERROR_RESILIENT_DEFAULT | VPX.ERROR_RESILIENT_PARTITIONS);
 
         context = VPX.codec_ctx_malloc();
         int ret = VPX.codec_enc_init(context, INTERFACE, cfg, flags);
-- 
GitLab