# HG changeset patch
# User Randell Jesup <rjesup@jesup.org>
# Date 1468252813 -32400
#      Tue Jul 12 01:00:13 2016 +0900
# Branch THUNDERBIRD3880_2016050308_RELBRANCH
# Node ID 2107bb1de4c417a1c773aa9e07503d67081f4187
# Parent  d5539dd692dfa795cbfb6b9a25e0d09a772b4914
Bug 1263384: validate input frames against configured resolution in vp8 r=rillian, a=ritu,lizzard

diff --git a/media/libvpx/input_frame_validation.patch b/media/libvpx/input_frame_validation.patch
new file mode 100644
--- /dev/null
+++ b/media/libvpx/input_frame_validation.patch
@@ -0,0 +1,46 @@
+# HG changeset patch
+# User Randell Jesup <rjesup@jesup.org>
+# Parent  1b77af186da211485fa9c5573d843d96c708a829
+Bug 1263384: validate input frames against configured resolution in vp8 r=rillian
+
+MozReview-Commit-ID: BxDCnJe0mzs
+
+diff --git a/media/libvpx/vp8/vp8_cx_iface.c b/media/libvpx/vp8/vp8_cx_iface.c
+--- a/media/libvpx/vp8/vp8_cx_iface.c
++++ b/media/libvpx/vp8/vp8_cx_iface.c
+@@ -916,21 +916,30 @@ static vpx_codec_err_t vp8e_encode(vpx_c
+         /* vp8 use 10,000,000 ticks/second as time stamp */
+         dst_time_stamp    = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
+         dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
+ 
+         if (img != NULL)
+         {
+             res = image2yuvconfig(img, &sd);
+ 
+-            if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
+-                                      &sd, dst_time_stamp, dst_end_time_stamp))
+-            {
+-                VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
+-                res = update_error_state(ctx, &cpi->common.error);
++            if (sd.y_width != ctx->cfg.g_w || sd.y_height != ctx->cfg.g_h) {
++                /* from vp8_encoder.h for g_w/g_h:
++                   "Note that the frames passed as input to the encoder must have this resolution"
++                */
++                ctx->base.err_detail = "Invalid input frame resolution";
++                res = VPX_CODEC_INVALID_PARAM;
++            } else {
++
++                if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
++                                          &sd, dst_time_stamp, dst_end_time_stamp))
++                {
++                    VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
++                    res = update_error_state(ctx, &cpi->common.error);
++                }
+             }
+ 
+             /* reset for next frame */
+             ctx->next_frame_flag = 0;
+         }
+ 
+         cx_data = ctx->cx_data;
+         cx_data_sz = ctx->cx_data_sz;
diff --git a/media/libvpx/update.py b/media/libvpx/update.py
--- a/media/libvpx/update.py
+++ b/media/libvpx/update.py
@@ -532,16 +532,18 @@
     # Patch to permit vpx users to specify their own <stdint.h> types.
     os.system("patch -p0 < stdint.patch")
     # Patch to allow older versions of Apple's clang to build libvpx.
     os.system("patch -p3 < apple-clang.patch")
     # Patch to allow MSVC 2015 to compile libvpx
     os.system("patch -p3 < msvc2015.patch")
     # Patch to fix a crash caused by MSVC 2013
     os.system("patch -p3 < bug1137614.patch")
+    # Bug 1263384 - Check input frame resolution
+    os.system("patch -p3 < input_frame_validation.patch")
 
 def update_readme(commit):
     with open('README_MOZILLA') as f:
         readme = f.read()
 
     if 'The git commit ID used was' in readme:
         new_readme = re.sub('The git commit ID used was [a-f0-9]+',
             'The git commit ID used was %s' % commit, readme)
diff --git a/media/libvpx/vp8/vp8_cx_iface.c b/media/libvpx/vp8/vp8_cx_iface.c
--- a/media/libvpx/vp8/vp8_cx_iface.c
+++ b/media/libvpx/vp8/vp8_cx_iface.c
@@ -873,21 +873,29 @@
         /* vp8 use 10,000,000 ticks/second as time stamp */
         dst_time_stamp    = pts * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
         dst_end_time_stamp = (pts + duration) * 10000000 * ctx->cfg.g_timebase.num / ctx->cfg.g_timebase.den;
 
         if (img != NULL)
         {
             res = image2yuvconfig(img, &sd);
 
-            if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
-                                      &sd, dst_time_stamp, dst_end_time_stamp))
-            {
-                VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
-                res = update_error_state(ctx, &cpi->common.error);
+            if (sd.y_width != ctx->cfg.g_w || sd.y_height != ctx->cfg.g_h) {
+                /* from vp8_encoder.h for g_w/g_h:
+                   "Note that the frames passed as input to the encoder must have this resolution"
+                */
+                ctx->base.err_detail = "Invalid input frame resolution";
+                res = VPX_CODEC_INVALID_PARAM;
+            } else {
+                if (vp8_receive_raw_frame(ctx->cpi, ctx->next_frame_flag | lib_flags,
+                                          &sd, dst_time_stamp, dst_end_time_stamp))
+                {
+                    VP8_COMP *cpi = (VP8_COMP *)ctx->cpi;
+                    res = update_error_state(ctx, &cpi->common.error);
+                }
             }
 
             /* reset for next frame */
             ctx->next_frame_flag = 0;
         }
 
         cx_data = ctx->cx_data;
         cx_data_sz = ctx->cx_data_sz;
