Merge "Add VPXD_SET_DECRYPTOR support to the VP9 decoder."
diff --git a/build/make/configure.sh b/build/make/configure.sh
index 514c442..a65d395 100755
--- a/build/make/configure.sh
+++ b/build/make/configure.sh
@@ -864,7 +864,7 @@
             CC=armcc
             AR=armar
             AS=armasm
-            LD=${source_path}/build/make/armlink_adapter.sh
+            LD="${source_path}/build/make/armlink_adapter.sh"
             STRIP=arm-none-linux-gnueabi-strip
             NM=arm-none-linux-gnueabi-nm
             tune_cflags="--cpu="
@@ -1339,10 +1339,10 @@
     if enabled source_path_used; then
     # Prepare the PWD for building.
     for f in ${OOT_INSTALLS}; do
-            install -D ${source_path}/$f $f
+            install -D "${source_path}/$f" "$f"
     done
     fi
-    cp ${source_path}/build/make/Makefile .
+    cp "${source_path}/build/make/Makefile" .
 
     clean_temp_files
     true
diff --git a/build/make/version.sh b/build/make/version.sh
index e31e568..b340142 100755
--- a/build/make/version.sh
+++ b/build/make/version.sh
@@ -24,9 +24,9 @@
 id=${3:-VERSION_STRING}
 
 git_version_id=""
-if [ -d ${source_path}/.git ]; then
+if [ -d "${source_path}/.git" ]; then
     # Source Path is a git working copy. Check for local modifications.
-    export GIT_DIR=${source_path}/.git
+    export GIT_DIR="${source_path}/.git"
     git_version_id=`git describe --match=v[0-9]* 2>/dev/null`
 fi
 
diff --git a/configure b/configure
index 937e0b5..29683cd 100755
--- a/configure
+++ b/configure
@@ -158,7 +158,7 @@
 
 # all targets available are enabled, by default.
 for t in ${all_targets}; do
-    [ -f ${source_path}/${t}.mk ] && enable_feature ${t}
+    [ -f "${source_path}/${t}.mk" ] && enable_feature ${t}
 done
 
 if ! perl --version >/dev/null; then
@@ -166,9 +166,9 @@
 fi
 
 
-if [ "`cd ${source_path} && pwd`" != "`pwd`" ]; then
+if [ "`cd \"${source_path}\" && pwd`" != "`pwd`" ]; then
   # test to see if source_path already configured
-  if [ -f ${source_path}/vpx_config.h ]; then
+  if [ -f "${source_path}/vpx_config.h" ]; then
     die "source directory already configured; run 'make distclean' there first"
   fi
 fi
@@ -201,27 +201,27 @@
 enable_feature os_support
 enable_feature temporal_denoising
 
-[ -d ${source_path}/../include ] && enable_feature alt_tree_layout
+[ -d "${source_path}/../include" ] && enable_feature alt_tree_layout
 for d in vp8 vp9; do
-    [ -d ${source_path}/${d} ] && disable_feature alt_tree_layout;
+    [ -d "${source_path}/${d}" ] && disable_feature alt_tree_layout;
 done
 
 if ! enabled alt_tree_layout; then
 # development environment
-[ -d ${source_path}/vp8 ] && CODECS="${CODECS} vp8_encoder vp8_decoder"
-[ -d ${source_path}/vp9 ] && CODECS="${CODECS} vp9_encoder vp9_decoder"
+[ -d "${source_path}/vp8" ] && CODECS="${CODECS} vp8_encoder vp8_decoder"
+[ -d "${source_path}/vp9" ] && CODECS="${CODECS} vp9_encoder vp9_decoder"
 else
 # customer environment
-[ -f ${source_path}/../include/vpx/vp8cx.h ] && CODECS="${CODECS} vp8_encoder"
-[ -f ${source_path}/../include/vpx/vp8dx.h ] && CODECS="${CODECS} vp8_decoder"
-[ -f ${source_path}/../include/vpx/vp9cx.h ] && CODECS="${CODECS} vp9_encoder"
-[ -f ${source_path}/../include/vpx/vp9dx.h ] && CODECS="${CODECS} vp9_decoder"
-[ -f ${source_path}/../include/vpx/vp8cx.h ] || disable_feature vp8_encoder
-[ -f ${source_path}/../include/vpx/vp8dx.h ] || disable_feature vp8_decoder
-[ -f ${source_path}/../include/vpx/vp9cx.h ] || disable_feature vp9_encoder
-[ -f ${source_path}/../include/vpx/vp9dx.h ] || disable_feature vp9_decoder
+[ -f "${source_path}/../include/vpx/vp8cx.h" ] && CODECS="${CODECS} vp8_encoder"
+[ -f "${source_path}/../include/vpx/vp8dx.h" ] && CODECS="${CODECS} vp8_decoder"
+[ -f "${source_path}/../include/vpx/vp9cx.h" ] && CODECS="${CODECS} vp9_encoder"
+[ -f "${source_path}/../include/vpx/vp9dx.h" ] && CODECS="${CODECS} vp9_decoder"
+[ -f "${source_path}/../include/vpx/vp8cx.h" ] || disable_feature vp8_encoder
+[ -f "${source_path}/../include/vpx/vp8dx.h" ] || disable_feature vp8_decoder
+[ -f "${source_path}/../include/vpx/vp9cx.h" ] || disable_feature vp9_encoder
+[ -f "${source_path}/../include/vpx/vp9dx.h" ] || disable_feature vp9_decoder
 
-[ -f ${source_path}/../lib/*/*mt.lib ] && soft_enable static_msvcrt
+[ -f "${source_path}/../lib/*/*mt.lib" ] && soft_enable static_msvcrt
 fi
 
 CODECS="$(echo ${CODECS} | tr ' ' '\n')"
@@ -466,7 +466,7 @@
           ;;
     esac
     if [ -f "${source_path}/build/make/version.sh" ]; then
-        local ver=`"$source_path/build/make/version.sh" --bare $source_path`
+        local ver=`"$source_path/build/make/version.sh" --bare "$source_path"`
         DIST_DIR="${DIST_DIR}-${ver}"
         VERSION_STRING=${ver}
         ver=${ver%%-*}
diff --git a/test/external_frame_buffer_test.cc b/test/external_frame_buffer_test.cc
index 54c79e9..fb0449d 100644
--- a/test/external_frame_buffer_test.cc
+++ b/test/external_frame_buffer_test.cc
@@ -10,13 +10,16 @@
 
 #include <string>
 
+#include "./vpx_config.h"
 #include "test/codec_factory.h"
 #include "test/decode_test_driver.h"
 #include "test/ivf_video_source.h"
 #include "test/md5_helper.h"
 #include "test/test_vectors.h"
 #include "test/util.h"
+#if CONFIG_WEBM_IO
 #include "test/webm_video_source.h"
+#endif
 
 namespace {
 
@@ -267,6 +270,7 @@
   ExternalFrameBufferList fb_list_;
 };
 
+#if CONFIG_WEBM_IO
 // Class for testing passing in external frame buffers to libvpx.
 class ExternalFrameBufferTest : public ::testing::Test {
  protected:
@@ -340,6 +344,7 @@
   int num_buffers_;
   ExternalFrameBufferList fb_list_;
 };
+#endif  // CONFIG_WEBM_IO
 
 // This test runs through the set of test vectors, and decodes them.
 // Libvpx will call into the application to allocate a frame buffer when
@@ -366,7 +371,13 @@
   if (filename.substr(filename.length() - 3, 3) == "ivf") {
     video = new libvpx_test::IVFVideoSource(filename);
   } else {
+#if CONFIG_WEBM_IO
     video = new libvpx_test::WebMVideoSource(filename);
+#else
+    fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n",
+            filename.c_str());
+    return;
+#endif
   }
   ASSERT_TRUE(video != NULL);
   video->Init();
@@ -380,6 +391,7 @@
   delete video;
 }
 
+#if CONFIG_WEBM_IO
 TEST_F(ExternalFrameBufferTest, MinFrameBuffers) {
   // Minimum number of external frame buffers for VP9 is
   // #VP9_MAXIMUM_REF_BUFFERS + #VPX_MAXIMUM_WORK_BUFFERS.
@@ -460,6 +472,7 @@
             SetFrameBufferFunctions(
                 num_buffers, get_vp9_frame_buffer, release_vp9_frame_buffer));
 }
+#endif  // CONFIG_WEBM_IO
 
 VP9_INSTANTIATE_TEST_CASE(ExternalFrameBufferMD5Test,
                           ::testing::ValuesIn(libvpx_test::kVP9TestVectors,
diff --git a/test/test.mk b/test/test.mk
index bceedee..da56b00 100644
--- a/test/test.mk
+++ b/test/test.mk
@@ -42,6 +42,7 @@
 LIBVPX_TEST_SRCS-yes                   += encode_test_driver.h
 
 ## WebM Parsing
+ifeq ($(CONFIG_WEBM_IO), yes)
 NESTEGG_SRCS                           += ../third_party/nestegg/halloc/halloc.h
 NESTEGG_SRCS                           += ../third_party/nestegg/halloc/src/align.h
 NESTEGG_SRCS                           += ../third_party/nestegg/halloc/src/halloc.c
@@ -53,11 +54,14 @@
 LIBVPX_TEST_SRCS-$(CONFIG_DECODERS)    += ../webmdec.c
 LIBVPX_TEST_SRCS-$(CONFIG_DECODERS)    += ../webmdec.h
 LIBVPX_TEST_SRCS-$(CONFIG_DECODERS)    += webm_video_source.h
+endif
 
 LIBVPX_TEST_SRCS-$(CONFIG_DECODERS)    += test_vector_test.cc
 
-# Currently we only support decoder perf tests for vp9
-ifeq ($(CONFIG_DECODE_PERF_TESTS)$(CONFIG_VP9_DECODER), yesyes)
+# Currently we only support decoder perf tests for vp9. Also they read from WebM
+# files, so WebM IO is required.
+ifeq ($(CONFIG_DECODE_PERF_TESTS)$(CONFIG_VP9_DECODER)$(CONFIG_WEBM_IO), \
+      yesyesyes)
 LIBVPX_TEST_SRCS-yes                   += decode_perf_test.cc
 endif
 
diff --git a/test/test_vector_test.cc b/test/test_vector_test.cc
index 9ba18da..1f294f2 100644
--- a/test/test_vector_test.cc
+++ b/test/test_vector_test.cc
@@ -12,13 +12,16 @@
 #include <cstdlib>
 #include <string>
 #include "third_party/googletest/src/include/gtest/gtest.h"
+#include "./vpx_config.h"
 #include "test/codec_factory.h"
 #include "test/decode_test_driver.h"
 #include "test/ivf_video_source.h"
 #include "test/md5_helper.h"
 #include "test/test_vectors.h"
 #include "test/util.h"
+#if CONFIG_WEBM_IO
 #include "test/webm_video_source.h"
+#endif
 #include "vpx_mem/vpx_mem.h"
 
 namespace {
@@ -75,7 +78,13 @@
   if (filename.substr(filename.length() - 3, 3) == "ivf") {
     video = new libvpx_test::IVFVideoSource(filename);
   } else if (filename.substr(filename.length() - 4, 4) == "webm") {
+#if CONFIG_WEBM_IO
     video = new libvpx_test::WebMVideoSource(filename);
+#else
+    fprintf(stderr, "WebM IO is disabled, skipping test vector %s\n",
+            filename.c_str());
+    return;
+#endif
   }
   video->Init();
 
diff --git a/test/vp9_thread_test.cc b/test/vp9_thread_test.cc
index 5523f20..0c9f71b 100644
--- a/test/vp9_thread_test.cc
+++ b/test/vp9_thread_test.cc
@@ -11,10 +11,13 @@
 #include <string>
 
 #include "third_party/googletest/src/include/gtest/gtest.h"
+#include "./vpx_config.h"
 #include "test/codec_factory.h"
 #include "test/decode_test_driver.h"
 #include "test/md5_helper.h"
+#if CONFIG_WEBM_IO
 #include "test/webm_video_source.h"
+#endif
 #include "vp9/decoder/vp9_thread.h"
 
 namespace {
@@ -97,6 +100,7 @@
 // -----------------------------------------------------------------------------
 // Multi-threaded decode tests
 
+#if CONFIG_WEBM_IO
 // Decodes |filename| with |num_threads|. Returns the md5 of the decoded frames.
 string DecodeFile(const string& filename, int num_threads) {
   libvpx_test::WebMVideoSource video(filename);
@@ -212,6 +216,7 @@
     }
   }
 }
+#endif  // CONFIG_WEBM_IO
 
 INSTANTIATE_TEST_CASE_P(Synchronous, VP9WorkerThreadTest, ::testing::Bool());
 
diff --git a/vp9/encoder/vp9_firstpass.c b/vp9/encoder/vp9_firstpass.c
index 314f8ab..1879b15 100644
--- a/vp9/encoder/vp9_firstpass.c
+++ b/vp9/encoder/vp9_firstpass.c
@@ -1935,7 +1935,8 @@
 
   // Find the next keyframe.
   i = 0;
-  while (twopass->stats_in < twopass->stats_in_end) {
+  while (twopass->stats_in < twopass->stats_in_end &&
+         rc->frames_to_key < cpi->key_frame_frequency) {
     // Accumulate kf group error.
     kf_group_err += calculate_modified_err(cpi, this_frame);
 
@@ -2003,7 +2004,8 @@
       input_stats(twopass, &tmp_frame);
     }
     rc->next_key_frame_forced = 1;
-  } else if (twopass->stats_in == twopass->stats_in_end) {
+  } else if (twopass->stats_in == twopass->stats_in_end ||
+             rc->frames_to_key >= cpi->key_frame_frequency) {
     rc->next_key_frame_forced = 1;
   } else {
     rc->next_key_frame_forced = 0;
diff --git a/vp9/vp9_cx_iface.c b/vp9/vp9_cx_iface.c
index 1d82b19..1ca9fb9 100644
--- a/vp9/vp9_cx_iface.c
+++ b/vp9/vp9_cx_iface.c
@@ -185,7 +185,7 @@
         ERROR("ts_rate_decimator factors are not powers of 2");
   }
 
-  // VP8 does not support a lower bound on the keyframe interval in
+  // VP9 does not support a lower bound on the keyframe interval in
   // automatic keyframe placement mode.
   if (cfg->kf_mode != VPX_KF_DISABLED &&
       cfg->kf_min_dist != cfg->kf_max_dist &&