diff --git a/.drone.yml b/.drone.yml
index 5331fea2ad0..6633204592b 100644
--- a/.drone.yml
+++ b/.drone.yml
@@ -2,20 +2,30 @@
kind: pipeline
name: unit-tests
+node:
+ freeswitch: public
+
steps:
- name: run-tests
image: signalwire/freeswitch-public-base
pull: true
commands:
+ - cat /proc/sys/kernel/core_pattern
- ./bootstrap.sh -j
- echo "applications/mod_test" >> modules.conf
- - ./configure
+ - sed -i '/applications\/mod_http_cache/s/^#//g' modules.conf
+ - sed -i '/event_handlers\/mod_rayo/s/^#//g' modules.conf
+ - sed -i '/formats\/mod_opusfile/s/^#//g' modules.conf
+ - sed -i '/languages\/mod_lua/s/^#//g' modules.conf
+ - export ASAN_OPTIONS=log_path=stdout:disable_coredump=0:unmap_shadow_on_exit=1;
+ - ./configure --enable-address-sanitizer
- echo "#!/bin/bash\nmake -j`nproc --all` |& tee ./unit-tests-build-result.txt\nexitstatus=\${PIPESTATUS[0]}\necho \$exitstatus > ./build-status.txt\nmake install\n" > build.sh
- chmod +x build.sh
- ./build.sh
- cd tests/unit
- ./run-tests.sh
- - mkdir logs && (mv log_run-tests_*.html logs || true)
+ - ls -la /cores
+ - mkdir logs && (mv log_run-tests_*.html logs || true) && (mv backtrace_*.txt logs || true)
- echo 0 > run-tests-status.txt
- ./collect-test-logs.sh && exit 0 || echo 'Some tests failed'
- echo 1 > run-tests-status.txt
@@ -40,8 +50,73 @@ trigger:
event:
- pull_request
- push
+
+---
+kind: pipeline
+name: scan-build
+
+node:
+ freeswitch: public
+
+steps:
+ - name: scan-build
+ image: signalwire/freeswitch-public-base:stretch
+ pull: true
+ commands:
+ - ./bootstrap.sh -j
+ - cp build/modules.conf.most modules.conf
+ #Enable/Uncomment mods
+ - sed -i "/mod_mariadb/s/^#//g" modules.conf
+ - sed -i "/mod_v8/s/^#//g" modules.conf
+ #Disable/Comment out mods
+ - sed -i '/mod_ilbc/s/^/#/g' modules.conf
+ - sed -i '/mod_isac/s/^/#/g' modules.conf
+ - sed -i '/mod_mp4/s/^/#/g' modules.conf
+ - sed -i '/mod_mongo/s/^/#/g' modules.conf
+ - sed -i '/mod_pocketsphinx/s/^/#/g' modules.conf
+ - sed -i '/mod_sangoma_codec/s/^/#/g' modules.conf
+ - sed -i '/mod_siren/s/^/#/g' modules.conf
+ #Comment out mods for a while
+ - sed -i '/mod_avmd/s/^/#/g' modules.conf
+ - sed -i '/mod_basic/s/^/#/g' modules.conf
+ - sed -i '/mod_cdr_mongodb/s/^/#/g' modules.conf
+ - sed -i '/mod_cv/s/^/#/g' modules.conf
+ - sed -i '/mod_erlang_event/s/^/#/g' modules.conf
+ - sed -i '/mod_perl/s/^/#/g' modules.conf
+ - sed -i '/mod_rtmp/s/^/#/g' modules.conf
+ - sed -i '/mod_unimrcp/s/^/#/g' modules.conf
+ - sed -i '/mod_xml_rpc/s/^/#/g' modules.conf
+ - ./configure
+ - mkdir -p scan-build
+ - echo "#!/bin/bash\nscan-build-4.0 -o ./scan-build/ make -j`nproc --all` |& tee ./scan-build-result.txt\nexitstatus=\${PIPESTATUS[0]}\necho \$exitstatus > ./scan-build-status.txt\n" > scan.sh
+ - chmod +x scan.sh
+ - ./scan.sh
+ - exitstatus=`cat ./scan-build-status.txt`
+ - echo "*** Exit status is $exitstatus"
+
+ - name: notify
+ image: signalwire/scan-build-notify
+ pull: true
+ environment:
+ GITHUB_CI_APP_PEM:
+ from_secret: github_ci_app_pem
+ SSH_KEY:
+ from_secret: ssh_key
+ SLACK_WEBHOOK_URL:
+ from_secret: slack_webhook_url
+ commands:
+ - /root/notify.sh
+
+
+trigger:
+ branch:
+ - master
+ event:
+ - pull_request
+ - push
+
---
kind: signature
-hmac: a34718dd1e2b9468a845962219ff05cac0c922ddf90d885af557a937a9e412e0
+hmac: d354f6d232ae6417b539fb9b40fc15765c3247ab58c87a5135a0ac6c448e1cd0
...
diff --git a/.gitconfig b/.gitconfig
new file mode 100644
index 00000000000..c1edb813655
--- /dev/null
+++ b/.gitconfig
@@ -0,0 +1,9 @@
+# This repository contains two commits with multiple authors.
+
+# By default, fsck will show errors for those commits.
+# Run the following command to ignore them:
+
+# git config --local include.path ../.gitconfig
+
+[fsck]
+ multipleAuthors=ignore
diff --git a/.gitignore b/.gitignore
index fb53ac2cf06..053abd7afb6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -271,4 +271,11 @@ src/mod/applications/mod_http_cache/test/test-suite.log
src/mod/applications/mod_http_cache/test/test_aws
src/mod/applications/mod_http_cache/test/test_aws.log
src/mod/applications/mod_http_cache/test/test_aws.trs
-
+src/mod/formats/mod_sndfile/test/test_sndfile
+src/mod/formats/mod_sndfile/test/test_sndfile_conf
+src/mod/*/*/test/*.log
+src/mod/*/*/test/*.trs
+src/mod/*/*/test/[0-9]*/*
+test-suite.log
+src/mod/applications/mod_av/test/test_BT7.mp4
+src/mod/applications/mod_av/test/test_RGB.mp4
diff --git a/Makefile.am b/Makefile.am
index 61b2c609dac..8e3a2e852e7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -547,6 +547,8 @@ $(switch_builddir)/modules.conf:
src/mod/modules.inc: $(switch_builddir)/modules.conf
@echo "OUR_MODULES=$(OUR_MODS)" > $(switch_builddir)/src/mod/modules.inc
@echo "OUR_CLEAN_MODULES=$(OUR_CLEAN_MODS)" >> $(switch_builddir)/src/mod/modules.inc
+ @echo "OUR_TEST_MODULES=$(OUR_TEST_MODS)" >> $(switch_builddir)/src/mod/modules.inc
+ @echo "OUR_CHECK_MODULES=$(OUR_CHECK_MODS)" >> $(switch_builddir)/src/mod/modules.inc
@echo "OUR_INSTALL_MODULES=$(OUR_INSTALL_MODS)" >> $(switch_builddir)/src/mod/modules.inc
@echo "OUR_UNINSTALL_MODULES=$(OUR_UNINSTALL_MODS)" >> $(switch_builddir)/src/mod/modules.inc
@echo "OUR_DISABLED_MODULES=$(OUR_DISABLED_MODS)" >> $(switch_builddir)/src/mod/modules.inc
@@ -848,6 +850,10 @@ modclean: $(switch_builddir)/modules.conf src/mod/modules.inc
modwipe:
rm -f $(modulesdir)/*.so $(modulesdir)/*.la $(modulesdir)/*.dll $(modulesdir)/*.dylib
+print_tests: libfreeswitch.la $(switch_builddir)/modules.conf src/mod/modules.inc
+ @cd tests/unit && $(MAKE) $(AM_MAKEFLAGS) print_tests
+ @cd src/mod && $(MAKE) $(AM_MAKEFLAGS) print_tests
+
dox:
cd docs && doxygen $(PWD)/docs/Doxygen.conf
diff --git a/build/modmake.rules.in b/build/modmake.rules.in
index 6ebcf7a6b31..dbce9bd48ec 100644
--- a/build/modmake.rules.in
+++ b/build/modmake.rules.in
@@ -107,6 +107,7 @@ install-modules: all-modules depend_install local_install mod_install
uninstall-modules: local_uninstall mod_uninstall
distclean-modules: clean-modules local_distclean
extraclean-modules: distclean local_extraclean
+print_tests:
Makefile:
@if test ! -f $@; then \
@@ -270,4 +271,4 @@ $(switch_srcdir)/src/include/switch_xml.h:
.PHONY: all clean depend install distclean extraclean \
all-modules clean-modules depend-modules install-modules distclean-modules extraclean-modules \
local_all local_clean local_depend local_install local_distclean local_extraclean \
- mod_clean mod_install mod_uninstall depend_install
+ mod_clean mod_install mod_uninstall depend_install print_tests
diff --git a/build/modmake.rulesam b/build/modmake.rulesam
index bffa0db4ac0..5ccdf8b5c20 100644
--- a/build/modmake.rulesam
+++ b/build/modmake.rulesam
@@ -14,3 +14,8 @@ install-modules: install
uninstall-modules: uninstall
distclean-modules: distclean
extraclean-modules: extraclean
+
+print_tests:
+ @set +e; \
+ test -z "$(TESTS)" || for i in $(TESTS); do echo $(subdir)/$$i; done;
+
\ No newline at end of file
diff --git a/build/next-release.txt b/build/next-release.txt
index 7c64495cce4..145fe2dd008 100644
--- a/build/next-release.txt
+++ b/build/next-release.txt
@@ -1 +1 @@
-1.10.2-release
+1.10.3-release
diff --git a/conf/testing/autoload_configs/sndfile.conf.xml b/conf/testing/autoload_configs/sndfile.conf.xml
new file mode 100644
index 00000000000..d9e0cff75d8
--- /dev/null
+++ b/conf/testing/autoload_configs/sndfile.conf.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
diff --git a/conf/testing/autoload_configs/switch.conf.xml b/conf/testing/autoload_configs/switch.conf.xml
index 69783e62c58..d07c1b2d6e2 100644
--- a/conf/testing/autoload_configs/switch.conf.xml
+++ b/conf/testing/autoload_configs/switch.conf.xml
@@ -190,7 +190,9 @@
-
+
+
+
diff --git a/conf/vanilla/autoload_configs/httapi.conf.xml b/conf/vanilla/autoload_configs/httapi.conf.xml
index e19210b4da5..c3566d148a1 100644
--- a/conf/vanilla/autoload_configs/httapi.conf.xml
+++ b/conf/vanilla/autoload_configs/httapi.conf.xml
@@ -105,6 +105,8 @@
+
+
+
+
+
+
diff --git a/conf/vanilla/autoload_configs/sofia.conf.xml b/conf/vanilla/autoload_configs/sofia.conf.xml
index d764a59e51c..629a0019fa2 100644
--- a/conf/vanilla/autoload_configs/sofia.conf.xml
+++ b/conf/vanilla/autoload_configs/sofia.conf.xml
@@ -2,6 +2,7 @@
+
diff --git a/conf/vanilla/autoload_configs/switch.conf.xml b/conf/vanilla/autoload_configs/switch.conf.xml
index 5b3930e7965..714b62c54cc 100644
--- a/conf/vanilla/autoload_configs/switch.conf.xml
+++ b/conf/vanilla/autoload_configs/switch.conf.xml
@@ -200,6 +200,8 @@
+
+
diff --git a/conf/vanilla/sip_profiles/external.xml b/conf/vanilla/sip_profiles/external.xml
index 1bf1b04595c..e2d161d5983 100644
--- a/conf/vanilla/sip_profiles/external.xml
+++ b/conf/vanilla/sip_profiles/external.xml
@@ -64,7 +64,7 @@
-
+
diff --git a/conf/vanilla/sip_profiles/internal.xml b/conf/vanilla/sip_profiles/internal.xml
index 3205e179b97..da7abc07a62 100644
--- a/conf/vanilla/sip_profiles/internal.xml
+++ b/conf/vanilla/sip_profiles/internal.xml
@@ -282,7 +282,7 @@
auto-nat - Use ip learned from NAT-PMP or UPNP
-->
-
+
diff --git a/configure.ac b/configure.ac
index 57373a6b1e4..c5f10ef2adf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3,10 +3,10 @@
# Must change all of the below together
# For a release, set revision for that tagged release as well and uncomment
-AC_INIT([freeswitch], [1.10.2-release], bugs@freeswitch.org)
+AC_INIT([freeswitch], [1.10.3-release], bugs@freeswitch.org)
AC_SUBST(SWITCH_VERSION_MAJOR, [1])
AC_SUBST(SWITCH_VERSION_MINOR, [10])
-AC_SUBST(SWITCH_VERSION_MICRO, [2-release])
+AC_SUBST(SWITCH_VERSION_MICRO, [3-release])
AC_SUBST(SWITCH_VERSION_REVISION, [])
AC_SUBST(SWITCH_VERSION_REVISION_HUMAN, [])
@@ -708,7 +708,11 @@ AC_SUBST([POSTGRESQL_LIBDIR], [$POSTGRESQL_LIBDIR])
PKG_CHECK_MODULES([MARIADB], [libmariadb >= 3.0.9],[
AM_CONDITIONAL([HAVE_MARIADB],[true])],[
- AC_MSG_RESULT([no]); AM_CONDITIONAL([HAVE_MARIADB],[false])])
+ PKG_CHECK_MODULES([MARIADB], [mariadb >= 3.0.9],[
+ AM_CONDITIONAL([HAVE_MARIADB],[true])],[
+ AC_MSG_RESULT([no]); AM_CONDITIONAL([HAVE_MARIADB],[false])
+ ])
+])
AC_ARG_ENABLE(deprecated-core-db-events,
[AS_HELP_STRING([--enable-deprecated-core-db-events], [Keep deprecated core db events])],,[enable_deprecated_core_db_events="no"])
@@ -1873,7 +1877,6 @@ AC_CONFIG_FILES([Makefile
src/mod/applications/mod_hiredis/Makefile
src/mod/applications/mod_httapi/Makefile
src/mod/applications/mod_http_cache/Makefile
- src/mod/applications/mod_http_cache/test/Makefile
src/mod/applications/mod_ladspa/Makefile
src/mod/applications/mod_lcr/Makefile
src/mod/applications/mod_limit/Makefile
@@ -1901,7 +1904,6 @@ AC_CONFIG_FILES([Makefile
src/mod/applications/mod_spy/Makefile
src/mod/applications/mod_stress/Makefile
src/mod/applications/mod_test/Makefile
- src/mod/applications/mod_test/test/Makefile
src/mod/applications/mod_translate/Makefile
src/mod/applications/mod_valet_parking/Makefile
src/mod/applications/mod_vmd/Makefile
@@ -1970,7 +1972,6 @@ AC_CONFIG_FILES([Makefile
src/mod/event_handlers/mod_radius_cdr/Makefile
src/mod/event_handlers/mod_odbc_cdr/Makefile
src/mod/event_handlers/mod_rayo/Makefile
- src/mod/event_handlers/mod_rayo/test/Makefile
src/mod/event_handlers/mod_smpp/Makefile
src/mod/event_handlers/mod_snmp/Makefile
src/mod/event_handlers/mod_event_zmq/Makefile
@@ -1978,6 +1979,7 @@ AC_CONFIG_FILES([Makefile
src/mod/formats/mod_local_stream/Makefile
src/mod/formats/mod_native_file/Makefile
src/mod/formats/mod_opusfile/Makefile
+ src/mod/formats/mod_png/Makefile
src/mod/formats/mod_shell_stream/Makefile
src/mod/formats/mod_shout/Makefile
src/mod/formats/mod_sndfile/Makefile
@@ -1987,7 +1989,6 @@ AC_CONFIG_FILES([Makefile
src/mod/formats/mod_portaudio_stream/Makefile
src/mod/languages/mod_java/Makefile
src/mod/languages/mod_lua/Makefile
- src/mod/languages/mod_lua/test/Makefile
src/mod/languages/mod_managed/Makefile
src/mod/languages/mod_perl/Makefile
src/mod/languages/mod_python/Makefile
@@ -2061,6 +2062,8 @@ OUR_MODS='$$(if test -z "$(MODULES)" ; then tmp_mods="$(CONF_MODULES)"; else tmp
OUR_CLEAN_MODS='$$(if test -z "$(MODULES)" ; then tmp_mods="$(CONF_MODULES)"; else tmp_mods="$(MODULES)" ; fi ; mods="$$(for i in $$tmp_mods ; do echo $$i-clean ; done )"; echo $$mods )'
OUR_INSTALL_MODS='$$(if test -z "$(MODULES)" ; then tmp_mods="$(CONF_MODULES)"; else tmp_mods="$(MODULES)" ; fi ; mods="$$(for i in $$tmp_mods ; do echo $$i-install ; done)"; echo $$mods )'
OUR_UNINSTALL_MODS='$$(if test -z "$(MODULES)" ; then tmp_mods="$(CONF_MODULES)"; else tmp_mods="$(MODULES)" ; fi ; mods="$$(for i in $$tmp_mods ; do echo $$i-uninstall ; done)"; echo $$mods )'
+OUR_TEST_MODS='$$(if test -z "$(MODULES)" ; then tmp_mods="$(CONF_MODULES)"; else tmp_mods="$(MODULES)" ; fi ; mods="$$(for i in $$tmp_mods ; do echo $$i-print_tests ; done )"; echo $$mods )'
+OUR_CHECK_MODS='$$(if test -z "$(MODULES)" ; then tmp_mods="$(CONF_MODULES)"; else tmp_mods="$(MODULES)" ; fi ; mods="$$(for i in $$tmp_mods ; do echo $$i-check ; done )"; echo $$mods )'
OUR_DISABLED_MODS='$$(tmp_mods="$(CONF_DISABLED_MODULES)"; mods="$$(for i in $$tmp_mods ; do echo $$i-all ; done )"; echo $$mods )'
OUR_DISABLED_CLEAN_MODS='$$(tmp_mods="$(CONF_DISABLED_MODULES)"; mods="$$(for i in $$tmp_mods ; do echo $$i-clean ; done )"; echo $$mods )'
OUR_DISABLED_INSTALL_MODS='$$(tmp_mods="$(CONF_DISABLED_MODULES)"; mods="$$(for i in $$tmp_mods ; do echo $$i-install ; done)"; echo $$mods )'
@@ -2075,6 +2078,8 @@ AC_SUBST(CONF_MODULES)
AC_SUBST(OUR_MODS)
AC_SUBST(OUR_CLEAN_MODS)
+AC_SUBST(OUR_TEST_MODS)
+AC_SUBST(OUR_CHECK_MODS)
AC_SUBST(OUR_INSTALL_MODS)
AC_SUBST(OUR_UNINSTALL_MODS)
AC_SUBST(OUR_DISABLED_MODS)
diff --git a/debian/bootstrap.sh b/debian/bootstrap.sh
index 9ca918bc036..a19eb272813 100755
--- a/debian/bootstrap.sh
+++ b/debian/bootstrap.sh
@@ -44,7 +44,6 @@ avoid_mods=(
applications/mod_cluechoo
asr_tts/mod_cepstral
codecs/mod_com_g729
- codecs/mod_ilbc
codecs/mod_openh264
codecs/mod_siren
codecs/mod_sangoma_codec
@@ -943,7 +942,7 @@ Description: Cross-Platform Scalable Multi-Protocol Soft Switch
Package: freeswitch-all-dbg
Section: debug
-Priority: extra
+Priority: optional
Architecture: amd64 armhf
Depends: \${misc:Depends}, freeswitch-meta-all (= \${binary:Version}), freeswitch-meta-all-dbg (= \${binary:Version})
Description: debugging symbols for FreeSWITCH
@@ -953,7 +952,7 @@ Description: debugging symbols for FreeSWITCH
Package: freeswitch-dbg
Section: debug
-Priority: extra
+Priority: optional
Architecture: amd64 armhf
Depends: \${misc:Depends}, freeswitch (= \${binary:Version})
Description: debugging symbols for FreeSWITCH
@@ -963,7 +962,7 @@ Description: debugging symbols for FreeSWITCH
Package: libfreeswitch1-dbg
Section: debug
-Priority: extra
+Priority: optional
Architecture: amd64 armhf
Depends: \${misc:Depends}, libfreeswitch1 (= \${binary:Version})
Description: debugging symbols for FreeSWITCH
@@ -1066,7 +1065,7 @@ Description: ${description} for FreeSWITCH
Package: freeswitch-${module_name//_/-}-dbg
Section: debug
-Priority: extra
+Priority: optional
Architecture: amd64 armhf
Depends: \${misc:Depends},
freeswitch-${module_name//_/-} (= \${binary:Version})
diff --git a/debian/control-modules b/debian/control-modules
index dcf8f29a219..56772fa780b 100644
--- a/debian/control-modules
+++ b/debian/control-modules
@@ -117,7 +117,7 @@ Module: applications/mod_hiredis
Description: Redis client support
This module provides a mechanism to use Redis as a datastore.
Build-Depends: libhiredis-dev
-Depends: libhiredis0.10 | libhiredis0.13
+Depends: libhiredis0.10 | libhiredis0.13 | libhiredis0.14
Module: applications/mod_httapi
Description: HT-TAPI Hypertext Telephony API
diff --git a/docs/phrase/phrase_de.xml b/docs/phrase/phrase_de.xml
index afe7dda430f..52d453ea011 100644
--- a/docs/phrase/phrase_de.xml
+++ b/docs/phrase/phrase_de.xml
@@ -5,6 +5,34 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -67,6 +95,8 @@
+
+
@@ -98,49 +128,125 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
-
+
+
+
+
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
+
+
-
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/docs/phrase/phrase_en.xml b/docs/phrase/phrase_en.xml
index 9a71b0616b5..e47ca4f9870 100644
--- a/docs/phrase/phrase_en.xml
+++ b/docs/phrase/phrase_en.xml
@@ -248,7 +248,6 @@
-
@@ -393,10 +392,10 @@
-
-
-
-
+
+
+
+
@@ -463,7 +462,6 @@
-
@@ -680,7 +678,6 @@
-
@@ -690,9 +687,6 @@
-
-
-
diff --git a/freeswitch.spec b/freeswitch.spec
index bc94233a242..4fa22123058 100644
--- a/freeswitch.spec
+++ b/freeswitch.spec
@@ -2028,6 +2028,7 @@ fi
%config(noreplace) %attr(0640, freeswitch, daemon) %{sysconfdir}/autoload_configs/skinny.conf.xml
%config(noreplace) %attr(0640, freeswitch, daemon) %{sysconfdir}/autoload_configs/smpp.conf.xml
%config(noreplace) %attr(0640, freeswitch, daemon) %{sysconfdir}/autoload_configs/sms_flowroute.conf.xml
+%config(noreplace) %attr(0640, freeswitch, daemon) %{sysconfdir}/autoload_configs/sndfile.conf.xml
%config(noreplace) %attr(0640, freeswitch, daemon) %{sysconfdir}/autoload_configs/sofia.conf.xml
%config(noreplace) %attr(0640, freeswitch, daemon) %{sysconfdir}/autoload_configs/spandsp.conf.xml
%config(noreplace) %attr(0640, freeswitch, daemon) %{sysconfdir}/autoload_configs/switch.conf.xml
@@ -2629,6 +2630,8 @@ fi
#
######################################################################################################################
%changelog
+* Fri Jan 31 2020 - Andrey Volk
+- Add sndfile.conf.xml
* Tue Apr 23 2019 - Andrey Volk
- Fix build for Stack 20.x
* Tue Dec 11 2018 - Andrey Volk
diff --git a/libs/apr-util/hooks/apr_hooks.c b/libs/apr-util/hooks/apr_hooks.c
index da0ae970581..776bc884e74 100644
--- a/libs/apr-util/hooks/apr_hooks.c
+++ b/libs/apr-util/hooks/apr_hooks.c
@@ -180,7 +180,9 @@ static TSort *tsort(TSort *pData,int nItems)
break;
}
}
- pTail->pNext=NULL; /* unfudge the tail */
+ if (pTail) {
+ pTail->pNext = NULL; /* unfudge the tail */
+ }
return pHead;
}
diff --git a/libs/apr-util/misc/apr_queue.c b/libs/apr-util/misc/apr_queue.c
index e905a53ebba..a947df8a089 100644
--- a/libs/apr-util/misc/apr_queue.c
+++ b/libs/apr-util/misc/apr_queue.c
@@ -222,7 +222,7 @@ APU_DECLARE(apr_status_t) apr_queue_trypush(apr_queue_t *queue, void *data)
}
if (apr_queue_full(queue)) {
- rv = apr_thread_mutex_unlock(queue->one_big_mutex);
+ apr_thread_mutex_unlock(queue->one_big_mutex);
return APR_EAGAIN;
}
@@ -397,7 +397,7 @@ APU_DECLARE(apr_status_t) apr_queue_trypop(apr_queue_t *queue, void **data)
}
if (apr_queue_empty(queue)) {
- rv = apr_thread_mutex_unlock(queue->one_big_mutex);
+ apr_thread_mutex_unlock(queue->one_big_mutex);
return APR_EAGAIN;
}
diff --git a/libs/apr-util/xml/apr_xml.c b/libs/apr-util/xml/apr_xml.c
index 73543555ff8..a05348d60f5 100644
--- a/libs/apr-util/xml/apr_xml.c
+++ b/libs/apr-util/xml/apr_xml.c
@@ -82,7 +82,7 @@ static int find_prefix(apr_xml_parser *parser, const char *prefix)
** prefix.
*/
for (; elem; elem = elem->parent) {
- apr_xml_ns_scope *ns_scope = elem->ns_scope;
+ apr_xml_ns_scope *ns_scope;
for (ns_scope = elem->ns_scope; ns_scope; ns_scope = ns_scope->next) {
if (strcmp(prefix, ns_scope->prefix) == 0) {
diff --git a/libs/apr-util/xml/expat/lib/xmltok.c b/libs/apr-util/xml/expat/lib/xmltok.c
index e3fe3f788f9..74c681f1e6b 100644
--- a/libs/apr-util/xml/expat/lib/xmltok.c
+++ b/libs/apr-util/xml/expat/lib/xmltok.c
@@ -1225,7 +1225,7 @@ void unknown_toUtf8(const ENCODING *enc,
const char **fromP, const char *fromLim,
char **toP, const char *toLim)
{
- char buf[XML_UTF8_ENCODE_MAX];
+ char buf[XML_UTF8_ENCODE_MAX] = {0};
for (;;) {
const char *utf8;
int n;
diff --git a/libs/apr/file_io/unix/filepath.c b/libs/apr/file_io/unix/filepath.c
index 78797bd990c..64f1b2fbd40 100644
--- a/libs/apr/file_io/unix/filepath.c
+++ b/libs/apr/file_io/unix/filepath.c
@@ -133,12 +133,15 @@ APR_DECLARE(apr_status_t) apr_filepath_merge(char **newpath,
* but required since the compiler (at least vc) doesn't like
* passing the address of a char const* for a char** arg.
*/
- char *getpath;
+ char *getpath = NULL;
rv = apr_filepath_get(&getpath, flags, p);
rootpath = getpath;
if (rv != APR_SUCCESS)
return errno;
+ if (!getpath)
+ return APR_ENOMEM;
+
/* XXX: Any kernel subject to goofy, uncanonical results
* must run the rootpath against the user's given flags.
* Simplest would be a recursive call to apr_filepath_merge
diff --git a/libs/apr/file_io/unix/filestat.c b/libs/apr/file_io/unix/filestat.c
index d27eafdcdf2..a34b22c3700 100644
--- a/libs/apr/file_io/unix/filestat.c
+++ b/libs/apr/file_io/unix/filestat.c
@@ -130,7 +130,7 @@ APR_DECLARE(apr_status_t) apr_file_attrs_set(const char *fname,
apr_pool_t *pool)
{
apr_status_t status;
- apr_finfo_t finfo;
+ apr_finfo_t finfo = {0};
/* Don't do anything if we can't handle the requested attributes */
if (!(attr_mask & (APR_FILE_ATTR_READONLY
@@ -185,7 +185,7 @@ APR_DECLARE(apr_status_t) apr_file_mtime_set(const char *fname,
apr_pool_t *pool)
{
apr_status_t status;
- apr_finfo_t finfo;
+ apr_finfo_t finfo = {0};
status = apr_stat(&finfo, fname, APR_FINFO_ATIME, pool);
if (status) {
diff --git a/libs/apr/memory/unix/apr_pools.c b/libs/apr/memory/unix/apr_pools.c
index c412a4aef05..7e37a5d0a07 100644
--- a/libs/apr/memory/unix/apr_pools.c
+++ b/libs/apr/memory/unix/apr_pools.c
@@ -821,8 +821,17 @@ APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
if (!abort_fn && parent)
abort_fn = parent->abort_fn;
- if (allocator == NULL)
+ if (allocator == NULL) {
+ if (!parent) {
+ /* There is no way to continue without an allocator when no parent */
+ if (abort_fn)
+ abort_fn(APR_EINVAL);
+
+ return APR_EINVAL;
+ }
+
allocator = parent->allocator;
+ }
if ((node = allocator_alloc(allocator,
MIN_ALLOC - APR_MEMNODE_T_SIZE)) == NULL) {
diff --git a/libs/apr/misc/unix/otherchild.c b/libs/apr/misc/unix/otherchild.c
index 427a57e7e47..c97cfdd6446 100644
--- a/libs/apr/misc/unix/otherchild.c
+++ b/libs/apr/misc/unix/otherchild.c
@@ -96,7 +96,7 @@ APR_DECLARE(void) apr_proc_other_child_unregister(void *data)
}
/* segfault if this function called with invalid parm */
- apr_pool_cleanup_kill(cur->p, cur->data, other_child_cleanup);
+ if (cur) apr_pool_cleanup_kill(cur->p, cur->data, other_child_cleanup);
other_child_cleanup(data);
}
diff --git a/libs/apr/network_io/unix/sendrecv.c b/libs/apr/network_io/unix/sendrecv.c
index 9ebc3cf3b4b..ca7230accca 100644
--- a/libs/apr/network_io/unix/sendrecv.c
+++ b/libs/apr/network_io/unix/sendrecv.c
@@ -27,6 +27,8 @@
#include
#endif
+#include /* assert() */
+
apr_status_t apr_socket_send(apr_socket_t *sock, const char *buf,
apr_size_t *len)
{
@@ -287,6 +289,7 @@ apr_status_t apr_socket_sendfile(apr_socket_t *sock, apr_file_t *file,
/* Ignore flags for now. */
flags = 0;
+ assert(flags==0);
if (hdtr->numheaders > 0) {
apr_size_t hdrbytes;
diff --git a/libs/apr/network_io/unix/sockets.c b/libs/apr/network_io/unix/sockets.c
index 4cff3bbca19..72dab7b89cf 100644
--- a/libs/apr/network_io/unix/sockets.c
+++ b/libs/apr/network_io/unix/sockets.c
@@ -32,7 +32,11 @@ static apr_status_t socket_cleanup(void *sock)
{
apr_socket_t *thesocket = sock;
- if (thesocket && thesocket->socketdes == -1) {
+ if (!thesocket) {
+ return APR_ENOTSOCK;
+ }
+
+ if (thesocket->socketdes == -1) {
return APR_SUCCESS;
}
diff --git a/libs/apr/random/unix/sha2.c b/libs/apr/random/unix/sha2.c
index 169afd5a78f..b8bd241d85f 100644
--- a/libs/apr/random/unix/sha2.c
+++ b/libs/apr/random/unix/sha2.c
@@ -444,6 +444,16 @@ void apr__SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
/* Clean up */
a = b = c = d = e = f = g = h = T1 = T2 = 0;
+ assert(a==0);
+ assert(b==0);
+ assert(c==0);
+ assert(d==0);
+ assert(e==0);
+ assert(f==0);
+ assert(g==0);
+ assert(h==0);
+ assert(T1==0);
+ assert(T2==0);
}
#endif /* SHA2_UNROLL_TRANSFORM */
@@ -478,6 +488,8 @@ void apr__SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len)
context->bitcount += len << 3;
/* Clean up: */
usedspace = freespace = 0;
+ assert(usedspace==0);
+ assert(freespace==0);
return;
}
}
@@ -495,6 +507,8 @@ void apr__SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len)
}
/* Clean up: */
usedspace = freespace = 0;
+ assert(usedspace==0);
+ assert(freespace==0);
}
void apr__SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
@@ -559,6 +573,7 @@ void apr__SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
/* Clean up state data: */
MEMSET_BZERO(context, sizeof(*context));
usedspace = 0;
+ assert(usedspace==0);
}
char *apr__SHA256_End(SHA256_CTX* context, char buffer[]) {
@@ -768,6 +783,16 @@ void apr__SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
/* Clean up */
a = b = c = d = e = f = g = h = T1 = T2 = 0;
+ assert(a==0);
+ assert(b==0);
+ assert(c==0);
+ assert(d==0);
+ assert(e==0);
+ assert(f==0);
+ assert(g==0);
+ assert(h==0);
+ assert(T1==0);
+ assert(T2==0);
}
#endif /* SHA2_UNROLL_TRANSFORM */
@@ -802,6 +827,8 @@ void apr__SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len)
ADDINC128(context->bitcount, len << 3);
/* Clean up: */
usedspace = freespace = 0;
+ assert(usedspace==0);
+ assert(freespace==0);
return;
}
}
@@ -819,6 +846,8 @@ void apr__SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len)
}
/* Clean up: */
usedspace = freespace = 0;
+ assert(usedspace==0);
+ assert(freespace==0);
}
void apr__SHA512_Last(SHA512_CTX* context) {
diff --git a/libs/apr/strings/apr_snprintf.c b/libs/apr/strings/apr_snprintf.c
index 4f59f92c764..611f2d7972b 100644
--- a/libs/apr/strings/apr_snprintf.c
+++ b/libs/apr/strings/apr_snprintf.c
@@ -110,7 +110,6 @@ static char *apr_cvt(double arg, int ndigits, int *decpt, int *sign,
arg = -arg;
}
arg = modf(arg, &fi);
- p1 = &buf[NDIG];
/*
* Do integer part
*/
diff --git a/libs/apr/strings/apr_strings.c b/libs/apr/strings/apr_strings.c
index ec687913c53..771d351f19d 100644
--- a/libs/apr/strings/apr_strings.c
+++ b/libs/apr/strings/apr_strings.c
@@ -124,7 +124,7 @@ APR_DECLARE(void *) apr_pmemdup(apr_pool_t *a, const void *m, apr_size_t n)
APR_DECLARE_NONSTD(char *) apr_pstrcat(apr_pool_t *a, ...)
{
char *cp, *argp, *res;
- apr_size_t saved_lengths[MAX_SAVED_LENGTHS];
+ apr_size_t saved_lengths[MAX_SAVED_LENGTHS] = { 0 };
int nargs = 0;
/* Pass one --- find length of required string */
diff --git a/libs/apr/tables/apr_hash.c b/libs/apr/tables/apr_hash.c
index 4e3723e19f1..5cac297af96 100644
--- a/libs/apr/tables/apr_hash.c
+++ b/libs/apr/tables/apr_hash.c
@@ -459,7 +459,7 @@ APR_DECLARE(apr_hash_t *) apr_hash_merge(apr_pool_t *p,
break;
}
}
- if (!ent) {
+ if (new_vals && !ent) {
new_vals[j].klen = iter->klen;
new_vals[j].key = iter->key;
new_vals[j].val = iter->val;
diff --git a/libs/esl/src/cJSON.c b/libs/esl/src/cJSON.c
index 3206091a5b1..1f092ea8f94 100644
--- a/libs/esl/src/cJSON.c
+++ b/libs/esl/src/cJSON.c
@@ -1,16 +1,13 @@
/*
- Copyright (c) 2009 Dave Gamble
-
+ Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
-
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -23,29 +20,72 @@
/* cJSON */
/* JSON parser in C. */
+/* disable warnings about old C89 functions in MSVC */
+#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
+#define _CRT_SECURE_NO_DEPRECATE
+#endif
+
+#ifdef __GNUC__
+#pragma GCC visibility push(default)
+#endif
+#if defined(_MSC_VER)
+#pragma warning (push)
+/* disable warning about single line comments in system headers */
+#pragma warning (disable : 4001)
+#endif
+
#include
#include
#include
#include
-#include
#include
#include
+
+#ifdef ENABLE_LOCALES
+#include
+#endif
+
+#if defined(_MSC_VER)
+#pragma warning (pop)
+#endif
+#ifdef __GNUC__
+#pragma GCC visibility pop
+#endif
+
#include "esl_cJSON.h"
/* define our own boolean type */
-typedef int cjbool;
-#define true ((cjbool)1)
-#define false ((cjbool)0)
+#ifdef true
+#undef true
+#endif
+#define true ((cJSON_bool)1)
+
+#ifdef false
+#undef false
+#endif
+#define false ((cJSON_bool)0)
-static const unsigned char *global_ep = NULL;
+typedef struct {
+ const unsigned char *json;
+ size_t position;
+} error;
+static error global_error = { NULL, 0 };
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
{
- return (const char*) global_ep;
+ return (const char*) (global_error.json + global_error.position);
+}
+
+CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item) {
+ if (!cJSON_IsString(item)) {
+ return NULL;
+ }
+
+ return item->valuestring;
}
/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
-#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 3) || (CJSON_VERSION_PATCH != 0)
+#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 12)
#error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
#endif
@@ -57,49 +97,79 @@ CJSON_PUBLIC(const char*) cJSON_Version(void)
return version;
}
-/* case insensitive strcmp */
-static int cJSON_strcasecmp(const unsigned char *s1, const unsigned char *s2)
+/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
+static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2)
{
- if (!s1)
+ if ((string1 == NULL) || (string2 == NULL))
{
- return (s1 == s2) ? 0 : 1; /* both NULL? */
+ return 1;
}
- if (!s2)
+
+ if (string1 == string2)
{
- return 1;
+ return 0;
}
- for(; tolower(*s1) == tolower(*s2); ++s1, ++s2)
+
+ for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
{
- if (*s1 == '\0')
+ if (*string1 == '\0')
{
return 0;
}
}
- return tolower(*s1) - tolower(*s2);
+ return tolower(*string1) - tolower(*string2);
}
typedef struct internal_hooks
{
- void *(*allocate)(size_t size);
- void (*deallocate)(void *pointer);
- void *(*reallocate)(void *pointer, size_t size);
+ void *(CJSON_CDECL *allocate)(size_t size);
+ void (CJSON_CDECL *deallocate)(void *pointer);
+ void *(CJSON_CDECL *reallocate)(void *pointer, size_t size);
} internal_hooks;
-static internal_hooks global_hooks = { malloc, free, realloc };
+#if defined(_MSC_VER)
+/* work around MSVC error C2322: '...' address of dillimport '...' is not static */
+static void * CJSON_CDECL internal_malloc(size_t size)
+{
+ return malloc(size);
+}
+static void CJSON_CDECL internal_free(void *pointer)
+{
+ free(pointer);
+}
+static void * CJSON_CDECL internal_realloc(void *pointer, size_t size)
+{
+ return realloc(pointer, size);
+}
+#else
+#define internal_malloc malloc
+#define internal_free free
+#define internal_realloc realloc
+#endif
+
+/* strlen of character literals resolved at compile time */
+#define static_strlen(string_literal) (sizeof(string_literal) - sizeof(""))
-static unsigned char* cJSON_strdup(const unsigned char* str, const internal_hooks * const hooks)
+static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc };
+
+static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks)
{
- size_t len = 0;
+ size_t length = 0;
unsigned char *copy = NULL;
- const unsigned char *s = str ? str : (unsigned char *)"";
- len = strlen((const char*)s) + 1;
- if (!(copy = (unsigned char*)hooks->allocate(len)))
+ if (string == NULL)
+ {
+ return NULL;
+ }
+
+ length = strlen((const char*)string) + sizeof("");
+ copy = (unsigned char*)hooks->allocate(length);
+ if (copy == NULL)
{
return NULL;
}
- memcpy(copy, s, len);
+ memcpy(copy, string, length);
return copy;
}
@@ -148,44 +218,110 @@ static cJSON *cJSON_New_Item(const internal_hooks * const hooks)
}
/* Delete a cJSON structure. */
-CJSON_PUBLIC(void) cJSON_Delete(cJSON *c)
+CJSON_PUBLIC(void) cJSON_Delete(cJSON *item)
{
cJSON *next = NULL;
- while (c)
+ while (item != NULL)
{
- next = c->next;
- if (!(c->type & cJSON_IsReference) && c->child)
+ next = item->next;
+ if (!(item->type & cJSON_IsReference) && (item->child != NULL))
{
- cJSON_Delete(c->child);
+ cJSON_Delete(item->child);
}
- if (!(c->type & cJSON_IsReference) && c->valuestring)
+ if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL))
{
- global_hooks.deallocate(c->valuestring);
+ global_hooks.deallocate(item->valuestring);
}
- if (!(c->type & cJSON_StringIsConst) && c->string)
+ if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
{
- global_hooks.deallocate(c->string);
+ global_hooks.deallocate(item->string);
}
- global_hooks.deallocate(c);
- c = next;
+ global_hooks.deallocate(item);
+ item = next;
}
}
+/* get the decimal point character of the current locale */
+static unsigned char get_decimal_point(void)
+{
+#ifdef ENABLE_LOCALES
+ struct lconv *lconv = localeconv();
+ return (unsigned char) lconv->decimal_point[0];
+#else
+ return '.';
+#endif
+}
+
+typedef struct
+{
+ const unsigned char *content;
+ size_t length;
+ size_t offset;
+ size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */
+ internal_hooks hooks;
+} parse_buffer;
+
+/* check if the given size is left to read in a given parse buffer (starting with 1) */
+#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length))
+/* check if the buffer can be accessed at the given index (starting with 0) */
+#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length))
+#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index))
+/* get a pointer to the buffer at the position */
+#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset)
+
/* Parse the input text to generate a number, and populate the result into item. */
-static const unsigned char *parse_number(cJSON * const item, const unsigned char * const input)
+static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer)
{
double number = 0;
unsigned char *after_end = NULL;
+ unsigned char number_c_string[64];
+ unsigned char decimal_point = get_decimal_point();
+ size_t i = 0;
- if (input == NULL)
+ if ((input_buffer == NULL) || (input_buffer->content == NULL))
{
- return NULL;
+ return false;
+ }
+
+ /* copy the number into a temporary buffer and replace '.' with the decimal point
+ * of the current locale (for strtod)
+ * This also takes care of '\0' not necessarily being available for marking the end of the input */
+ for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++)
+ {
+ switch (buffer_at_offset(input_buffer)[i])
+ {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case '+':
+ case '-':
+ case 'e':
+ case 'E':
+ number_c_string[i] = buffer_at_offset(input_buffer)[i];
+ break;
+
+ case '.':
+ number_c_string[i] = decimal_point;
+ break;
+
+ default:
+ goto loop_end;
+ }
}
+loop_end:
+ number_c_string[i] = '\0';
- number = strtod((const char*)input, (char**)&after_end);
- if (input == after_end)
+ number = strtod((const char*)number_c_string, (char**)&after_end);
+ if (number_c_string == after_end)
{
- return NULL; /* parse_error */
+ return false; /* parse_error */
}
item->valuedouble = number;
@@ -195,7 +331,7 @@ static const unsigned char *parse_number(cJSON * const item, const unsigned char
{
item->valueint = INT_MAX;
}
- else if (number <= INT_MIN)
+ else if (number <= (double)INT_MIN)
{
item->valueint = INT_MIN;
}
@@ -206,7 +342,8 @@ static const unsigned char *parse_number(cJSON * const item, const unsigned char
item->type = cJSON_Number;
- return after_end;
+ input_buffer->offset += (size_t)(after_end - number_c_string);
+ return true;
}
/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */
@@ -216,13 +353,13 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
{
object->valueint = INT_MAX;
}
- else if (number <= INT_MIN)
+ else if (number <= (double)INT_MIN)
{
object->valueint = INT_MIN;
}
else
{
- object->valueint = cJSON_Number;
+ object->valueint = (int)number;
}
return object->valuedouble = number;
@@ -233,11 +370,14 @@ typedef struct
unsigned char *buffer;
size_t length;
size_t offset;
- cjbool noalloc;
+ size_t depth; /* current nesting depth (for formatted printing) */
+ cJSON_bool noalloc;
+ cJSON_bool format; /* is this print a formatted print */
+ internal_hooks hooks;
} printbuffer;
/* realloc printbuffer if necessary to have at least "needed" bytes more */
-static unsigned char* ensure(printbuffer * const p, size_t needed, const internal_hooks * const hooks)
+static unsigned char* ensure(printbuffer * const p, size_t needed)
{
unsigned char *newbuffer = NULL;
size_t newsize = 0;
@@ -247,13 +387,19 @@ static unsigned char* ensure(printbuffer * const p, size_t needed, const interna
return NULL;
}
+ if ((p->length > 0) && (p->offset >= p->length))
+ {
+ /* make sure that offset is valid */
+ return NULL;
+ }
+
if (needed > INT_MAX)
{
/* sizes bigger than INT_MAX are currently not supported */
return NULL;
}
- needed += p->offset;
+ needed += p->offset + 1;
if (needed <= p->length)
{
return p->buffer + p->offset;
@@ -264,8 +410,7 @@ static unsigned char* ensure(printbuffer * const p, size_t needed, const interna
}
/* calculate new buffer size */
- newsize = needed * 2;
- if (newsize > INT_MAX)
+ if (needed > (INT_MAX / 2))
{
/* overflow of int, use INT_MAX if possible */
if (needed <= INT_MAX)
@@ -277,26 +422,41 @@ static unsigned char* ensure(printbuffer * const p, size_t needed, const interna
return NULL;
}
}
+ else
+ {
+ newsize = needed * 2;
+ }
- if (hooks->reallocate != NULL)
+ if (p->hooks.reallocate != NULL)
{
/* reallocate with realloc if available */
- newbuffer = (unsigned char*)hooks->reallocate(p->buffer, newsize);
+ newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
+ if (newbuffer == NULL)
+ {
+ p->hooks.deallocate(p->buffer);
+ p->length = 0;
+ p->buffer = NULL;
+
+ return NULL;
+ }
}
else
{
/* otherwise reallocate manually */
- newbuffer = (unsigned char*)hooks->allocate(newsize);
+ newbuffer = (unsigned char*)p->hooks.allocate(newsize);
if (!newbuffer)
{
- hooks->deallocate(p->buffer);
+ p->hooks.deallocate(p->buffer);
p->length = 0;
p->buffer = NULL;
return NULL;
}
- memcpy(newbuffer, p->buffer, p->offset + 1);
- hooks->deallocate(p->buffer);
+ if (newbuffer)
+ {
+ memcpy(newbuffer, p->buffer, p->offset + 1);
+ }
+ p->hooks.deallocate(p->buffer);
}
p->length = newsize;
p->buffer = newbuffer;
@@ -318,54 +478,69 @@ static void update_offset(printbuffer * const buffer)
}
/* Render the number nicely from the given item into a string. */
-static unsigned char *print_number(const cJSON * const item, printbuffer * const output_buffer, const internal_hooks * const hooks)
+static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer)
{
unsigned char *output_pointer = NULL;
double d = item->valuedouble;
+ int length = 0;
+ size_t i = 0;
+ unsigned char number_buffer[26]; /* temporary buffer to print the number into */
+ unsigned char decimal_point = get_decimal_point();
+ double test;
if (output_buffer == NULL)
{
- return NULL;
+ return false;
}
- /* value is an int */
- if ((fabs(((double)item->valueint) - d) <= DBL_EPSILON) && (d <= INT_MAX) && (d >= INT_MIN))
+ /* This checks for NaN and Infinity */
+ if ((d * 0) != 0)
+ {
+ length = sprintf((char*)number_buffer, "null");
+ }
+ else
{
- /* 2^64+1 can be represented in 21 chars. */
- output_pointer = ensure(output_buffer, 21, hooks);
- if (output_pointer != NULL)
+ /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
+ length = sprintf((char*)number_buffer, "%1.15g", d);
+
+ /* Check whether the original double can be recovered */
+ if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || ((double)test != d))
{
- sprintf((char*)output_pointer, "%d", item->valueint);
+ /* If not, print with 17 decimal places of precision */
+ length = sprintf((char*)number_buffer, "%1.17g", d);
}
}
- /* value is a floating point number */
- else
+
+ /* sprintf failed or buffer overrun occurred */
+ if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1)))
+ {
+ return false;
+ }
+
+ /* reserve appropriate space in the output */
+ output_pointer = ensure(output_buffer, (size_t)length + sizeof(""));
+ if (output_pointer == NULL)
+ {
+ return false;
+ }
+
+ /* copy the printed number to the output and replace locale
+ * dependent decimal point with '.' */
+ for (i = 0; i < ((size_t)length); i++)
{
- /* This is a nice tradeoff. */
- output_pointer = ensure(output_buffer, 64, hooks);
- if (output_pointer != NULL)
+ if (number_buffer[i] == decimal_point)
{
- /* This checks for NaN and Infinity */
- if ((d * 0) != 0)
- {
- sprintf((char*)output_pointer, "null");
- }
- else if ((fabs(floor(d) - d) <= DBL_EPSILON) && (fabs(d) < 1.0e60))
- {
- sprintf((char*)output_pointer, "%.0f", d);
- }
- else if ((fabs(d) < 1.0e-6) || (fabs(d) > 1.0e9))
- {
- sprintf((char*)output_pointer, "%e", d);
- }
- else
- {
- sprintf((char*)output_pointer, "%f", d);
- }
+ output_pointer[i] = '.';
+ continue;
}
+
+ output_pointer[i] = number_buffer[i];
}
+ output_pointer[i] = '\0';
+
+ output_buffer->offset += (size_t)length;
- return output_pointer;
+ return true;
}
/* parse 4 digit hexadecimal number */
@@ -406,28 +581,19 @@ static unsigned parse_hex4(const unsigned char * const input)
/* converts a UTF-16 literal to UTF-8
* A literal can be one or two sequences of the form \uXXXX */
-static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer, const unsigned char **error_pointer)
+static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer)
{
- /* first bytes of UTF8 encoding for a given length in bytes */
- static const unsigned char firstByteMark[5] =
- {
- 0x00, /* should never happen */
- 0x00, /* 0xxxxxxx */
- 0xC0, /* 110xxxxx */
- 0xE0, /* 1110xxxx */
- 0xF0 /* 11110xxx */
- };
-
long unsigned int codepoint = 0;
unsigned int first_code = 0;
const unsigned char *first_sequence = input_pointer;
unsigned char utf8_length = 0;
+ unsigned char utf8_position = 0;
unsigned char sequence_length = 0;
+ unsigned char first_byte_mark = 0;
if ((input_end - first_sequence) < 6)
{
/* input ends unexpectedly */
- *error_pointer = first_sequence;
goto fail;
}
@@ -435,9 +601,8 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi
first_code = parse_hex4(first_sequence + 2);
/* check that the code is valid */
- if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)) || (first_code == 0))
+ if (((first_code >= 0xDC00) && (first_code <= 0xDFFF)))
{
- *error_pointer = first_sequence;
goto fail;
}
@@ -451,14 +616,12 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi
if ((input_end - second_sequence) < 6)
{
/* input ends unexpectedly */
- *error_pointer = first_sequence;
goto fail;
}
if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u'))
{
/* missing second half of the surrogate pair */
- *error_pointer = first_sequence;
goto fail;
}
@@ -468,7 +631,6 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi
if ((second_code < 0xDC00) || (second_code > 0xDFFF))
{
/* invalid second half of the surrogate pair */
- *error_pointer = first_sequence;
goto fail;
}
@@ -494,47 +656,43 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi
{
/* two bytes, encoding 110xxxxx 10xxxxxx */
utf8_length = 2;
+ first_byte_mark = 0xC0; /* 11000000 */
}
else if (codepoint < 0x10000)
{
/* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */
utf8_length = 3;
+ first_byte_mark = 0xE0; /* 11100000 */
}
else if (codepoint <= 0x10FFFF)
{
/* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */
utf8_length = 4;
+ first_byte_mark = 0xF0; /* 11110000 */
}
else
{
/* invalid unicode codepoint */
- *error_pointer = first_sequence;
goto fail;
}
/* encode as utf8 */
- switch (utf8_length)
- {
- case 4:
- /* 10xxxxxx */
- (*output_pointer)[3] = (unsigned char)((codepoint | 0x80) & 0xBF);
- codepoint >>= 6;
- case 3:
- /* 10xxxxxx */
- (*output_pointer)[2] = (unsigned char)((codepoint | 0x80) & 0xBF);
- codepoint >>= 6;
- case 2:
- (*output_pointer)[1] = (unsigned char)((codepoint | 0x80) & 0xBF);
- codepoint >>= 6;
- case 1:
- /* depending on the length in bytes this determines the
- encoding of the first UTF8 byte */
- (*output_pointer)[0] = (unsigned char)((codepoint | firstByteMark[utf8_length]) & 0xFF);
- break;
- default:
- *error_pointer = first_sequence;
- goto fail;
+ for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--)
+ {
+ /* 10xxxxxx */
+ (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF);
+ codepoint >>= 6;
+ }
+ /* encode first byte */
+ if (utf8_length > 1)
+ {
+ (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF);
}
+ else
+ {
+ (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F);
+ }
+
*output_pointer += utf8_length;
return sequence_length;
@@ -544,17 +702,16 @@ static unsigned char utf16_literal_to_utf8(const unsigned char * const input_poi
}
/* Parse the input text into an unescaped cinput, and populate item. */
-static const unsigned char *parse_string(cJSON * const item, const unsigned char * const input, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
+static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer)
{
- const unsigned char *input_pointer = input + 1;
- const unsigned char *input_end = input + 1;
+ const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1;
+ const unsigned char *input_end = buffer_at_offset(input_buffer) + 1;
unsigned char *output_pointer = NULL;
unsigned char *output = NULL;
/* not a string */
- if (*input != '\"')
+ if (buffer_at_offset(input_buffer)[0] != '\"')
{
- *error_pointer = input;
goto fail;
}
@@ -562,12 +719,12 @@ static const unsigned char *parse_string(cJSON * const item, const unsigned char
/* calculate approximate size of the output (overestimate) */
size_t allocation_length = 0;
size_t skipped_bytes = 0;
- while ((*input_end != '\"') && (*input_end != '\0'))
+ while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"'))
{
/* is escape sequence */
if (input_end[0] == '\\')
{
- if (input_end[1] == '\0')
+ if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length)
{
/* prevent buffer overflow when last input character is a backslash */
goto fail;
@@ -577,14 +734,14 @@ static const unsigned char *parse_string(cJSON * const item, const unsigned char
}
input_end++;
}
- if (*input_end == '\0')
+ if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"'))
{
goto fail; /* string ended unexpectedly */
}
/* This is at most how much we need for the output */
- allocation_length = (size_t) (input_end - input) - skipped_bytes;
- output = (unsigned char*)hooks->allocate(allocation_length + sizeof('\0'));
+ allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes;
+ output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof(""));
if (output == NULL)
{
goto fail; /* allocation failure */
@@ -603,6 +760,11 @@ static const unsigned char *parse_string(cJSON * const item, const unsigned char
else
{
unsigned char sequence_length = 2;
+ if ((input_end - input_pointer) < 1)
+ {
+ goto fail;
+ }
+
switch (input_pointer[1])
{
case 'b':
@@ -628,7 +790,7 @@ static const unsigned char *parse_string(cJSON * const item, const unsigned char
/* UTF-16 literal */
case 'u':
- sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer, error_pointer);
+ sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer);
if (sequence_length == 0)
{
/* failed to convert UTF16-literal to UTF-8 */
@@ -637,7 +799,6 @@ static const unsigned char *parse_string(cJSON * const item, const unsigned char
break;
default:
- *error_pointer = input_pointer;
goto fail;
}
input_pointer += sequence_length;
@@ -650,19 +811,27 @@ static const unsigned char *parse_string(cJSON * const item, const unsigned char
item->type = cJSON_String;
item->valuestring = (char*)output;
- return input_end + 1;
+ input_buffer->offset = (size_t) (input_end - input_buffer->content);
+ input_buffer->offset++;
+
+ return true;
fail:
if (output != NULL)
{
- hooks->deallocate(output);
+ input_buffer->hooks.deallocate(output);
}
- return NULL;
+ if (input_pointer != NULL)
+ {
+ input_buffer->offset = (size_t)(input_pointer - input_buffer->content);
+ }
+
+ return false;
}
/* Render the cstring provided to an escaped version that can be printed. */
-static unsigned char *print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer, const internal_hooks * const hooks)
+static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer)
{
const unsigned char *input_pointer = NULL;
unsigned char *output = NULL;
@@ -673,42 +842,52 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb
if (output_buffer == NULL)
{
- return NULL;
+ return false;
}
/* empty string */
if (input == NULL)
{
- output = ensure(output_buffer, sizeof("\"\""), hooks);
+ output = ensure(output_buffer, sizeof("\"\""));
if (output == NULL)
{
- return NULL;
+ return false;
}
strcpy((char*)output, "\"\"");
- return output;
+ return true;
}
/* set "flag" to 1 if something needs to be escaped */
for (input_pointer = input; *input_pointer; input_pointer++)
{
- if (strchr("\"\\\b\f\n\r\t", *input_pointer))
- {
- /* one character escape sequence */
- escape_characters++;
- }
- else if (*input_pointer < 32)
- {
- /* UTF-16 escape sequence uXXXX */
- escape_characters += 5;
+ switch (*input_pointer)
+ {
+ case '\"':
+ case '\\':
+ case '\b':
+ case '\f':
+ case '\n':
+ case '\r':
+ case '\t':
+ /* one character escape sequence */
+ escape_characters++;
+ break;
+ default:
+ if (*input_pointer < 32)
+ {
+ /* UTF-16 escape sequence uXXXX */
+ escape_characters += 5;
+ }
+ break;
}
}
output_length = (size_t)(input_pointer - input) + escape_characters;
- output = ensure(output_buffer, output_length + sizeof("\"\""), hooks);
+ output = ensure(output_buffer, output_length + sizeof("\"\""));
if (output == NULL)
{
- return NULL;
+ return false;
}
/* no characters have to be escaped */
@@ -719,13 +898,13 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb
output[output_length + 1] = '\"';
output[output_length + 2] = '\0';
- return output;
+ return true;
}
output[0] = '\"';
output_pointer = output + 1;
/* copy the string */
- for (input_pointer = input; *input_pointer != '\0'; input_pointer++, output_pointer++)
+ for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++)
{
if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\'))
{
@@ -770,72 +949,138 @@ static unsigned char *print_string_ptr(const unsigned char * const input, printb
output[output_length + 1] = '\"';
output[output_length + 2] = '\0';
- return output;
+ return true;
}
/* Invoke print_string_ptr (which is useful) on an item. */
-static unsigned char *print_string(const cJSON * const item, printbuffer * const p, const internal_hooks * const hooks)
+static cJSON_bool print_string(const cJSON * const item, printbuffer * const p)
{
- return print_string_ptr((unsigned char*)item->valuestring, p, hooks);
+ return print_string_ptr((unsigned char*)item->valuestring, p);
}
/* Predeclare these prototypes. */
-static const unsigned char *parse_value(cJSON * const item, const unsigned char * const input, const unsigned char ** const ep, const internal_hooks * const hooks);
-static unsigned char *print_value(const cJSON * const item, const size_t depth, const cjbool format, printbuffer * const output_buffer, const internal_hooks * const hooks);
-static const unsigned char *parse_array(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks);
-static unsigned char *print_array(const cJSON * const item, const size_t depth, const cjbool format, printbuffer * const output_buffer, const internal_hooks * const hooks);
-static const unsigned char *parse_object(cJSON * const item, const unsigned char *input, const unsigned char ** const ep, const internal_hooks * const hooks);
-static unsigned char *print_object(const cJSON * const item, const size_t depth, const cjbool format, printbuffer * const output_buffer, const internal_hooks * const hooks);
+static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer);
+static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer);
+static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer);
+static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer);
+static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer);
+static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer);
/* Utility to jump whitespace and cr/lf */
-static const unsigned char *skip_whitespace(const unsigned char *in)
+static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer)
{
- while (in && *in && (*in <= 32))
+ if ((buffer == NULL) || (buffer->content == NULL))
{
- in++;
+ return NULL;
+ }
+
+ while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32))
+ {
+ buffer->offset++;
}
- return in;
+ if (buffer->offset == buffer->length)
+ {
+ buffer->offset--;
+ }
+
+ return buffer;
}
-/* Parse an object - create a new root, and populate. */
-CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cjbool require_null_terminated)
+/* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */
+static parse_buffer *skip_utf8_bom(parse_buffer * const buffer)
{
- const unsigned char *end = NULL;
- /* use global error pointer if no specific one was given */
- const unsigned char **ep = return_parse_end ? (const unsigned char**)return_parse_end : &global_ep;
- cJSON *c = cJSON_New_Item(&global_hooks);
- *ep = NULL;
- if (!c) /* memory fail */
+ if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0))
{
return NULL;
}
- end = parse_value(c, skip_whitespace((const unsigned char*)value), ep, &global_hooks);
- if (!end)
+ if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0))
+ {
+ buffer->offset += 3;
+ }
+
+ return buffer;
+}
+
+/* Parse an object - create a new root, and populate. */
+CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated)
+{
+ parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } };
+ cJSON *item = NULL;
+
+ /* reset error position */
+ global_error.json = NULL;
+ global_error.position = 0;
+
+ if (value == NULL)
+ {
+ goto fail;
+ }
+
+ buffer.content = (const unsigned char*)value;
+ buffer.length = strlen((const char*)value) + sizeof("");
+ buffer.offset = 0;
+ buffer.hooks = global_hooks;
+
+ item = cJSON_New_Item(&global_hooks);
+ if (item == NULL) /* memory fail */
+ {
+ goto fail;
+ }
+
+ if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer))))
{
/* parse failure. ep is set. */
- cJSON_Delete(c);
- return NULL;
+ goto fail;
}
/* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */
if (require_null_terminated)
{
- end = skip_whitespace(end);
- if (*end)
+ buffer_skip_whitespace(&buffer);
+ if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0')
{
- cJSON_Delete(c);
- *ep = end;
- return NULL;
+ goto fail;
}
}
if (return_parse_end)
{
- *return_parse_end = (const char*)end;
+ *return_parse_end = (const char*)buffer_at_offset(&buffer);
+ }
+
+ return item;
+
+fail:
+ if (item != NULL)
+ {
+ cJSON_Delete(item);
+ }
+
+ if (value != NULL)
+ {
+ error local_error;
+ local_error.json = (const unsigned char*)value;
+ local_error.position = 0;
+
+ if (buffer.offset < buffer.length)
+ {
+ local_error.position = buffer.offset;
+ }
+ else if (buffer.length > 0)
+ {
+ local_error.position = buffer.length - 1;
+ }
+
+ if (return_parse_end != NULL)
+ {
+ *return_parse_end = (const char*)local_error.json + local_error.position;
+ }
+
+ global_error = local_error;
}
- return c;
+ return NULL;
}
/* Default options for cJSON_Parse */
@@ -844,40 +1089,55 @@ CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value)
return cJSON_ParseWithOpts(value, 0, 0);
}
-#define min(a, b) ((a < b) ? a : b)
+#define cjson_min(a, b) ((a < b) ? a : b)
-static unsigned char *print(const cJSON * const item, cjbool format, const internal_hooks * const hooks)
+static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
{
+ static const size_t default_buffer_size = 256;
printbuffer buffer[1];
unsigned char *printed = NULL;
memset(buffer, 0, sizeof(buffer));
/* create buffer */
- buffer->buffer = (unsigned char*) hooks->allocate(256);
+ buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size);
+ buffer->length = default_buffer_size;
+ buffer->format = format;
+ buffer->hooks = *hooks;
if (buffer->buffer == NULL)
{
goto fail;
}
/* print the value */
- if (print_value(item, 0, format, buffer, hooks) == NULL)
+ if (!print_value(item, buffer))
{
goto fail;
}
update_offset(buffer);
- /* copy the buffer over to a new one */
- printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
- if (printed == NULL)
+ /* check if reallocate is available */
+ if (hooks->reallocate != NULL)
{
- goto fail;
+ printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
+ if (printed == NULL) {
+ goto fail;
+ }
+ buffer->buffer = NULL;
}
- strncpy((char*)printed, (char*)buffer->buffer, min(buffer->length, buffer->offset + 1));
- printed[buffer->offset] = '\0'; /* just to be sure */
+ else /* otherwise copy the JSON over to a new buffer */
+ {
+ printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
+ if (printed == NULL)
+ {
+ goto fail;
+ }
+ memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
+ printed[buffer->offset] = '\0'; /* just to be sure */
- /* free the buffer */
- hooks->deallocate(buffer->buffer);
+ /* free the buffer */
+ hooks->deallocate(buffer->buffer);
+ }
return printed;
@@ -887,6 +1147,11 @@ static unsigned char *print(const cJSON * const item, cjbool format, const inter
hooks->deallocate(buffer->buffer);
}
+ if (printed != NULL)
+ {
+ hooks->deallocate(printed);
+ }
+
return NULL;
}
@@ -901,9 +1166,9 @@ CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item)
return (char*)print(item, false, &global_hooks);
}
-CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cjbool fmt)
+CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt)
{
- printbuffer p;
+ printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
if (prebuffer < 0)
{
@@ -919,15 +1184,23 @@ CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cjboo
p.length = (size_t)prebuffer;
p.offset = 0;
p.noalloc = false;
+ p.format = fmt;
+ p.hooks = global_hooks;
+
+ if (!print_value(item, &p))
+ {
+ global_hooks.deallocate(p.buffer);
+ return NULL;
+ }
- return (char*)print_value(item, 0, fmt, &p, &global_hooks);
+ return (char*)p.buffer;
}
-CJSON_PUBLIC(int) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cjbool fmt)
+CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const cJSON_bool fmt)
{
- printbuffer p;
+ printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } };
- if (len < 0)
+ if ((len < 0) || (buf == NULL))
{
return false;
}
@@ -936,164 +1209,181 @@ CJSON_PUBLIC(int) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len,
p.length = (size_t)len;
p.offset = 0;
p.noalloc = true;
- return print_value(item, 0, fmt, &p, &global_hooks) != NULL;
+ p.format = fmt;
+ p.hooks = global_hooks;
+
+ return print_value(item, &p);
}
/* Parser core - when encountering text, process appropriately. */
-static const unsigned char *parse_value(cJSON * const item, const unsigned char * const input, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
+static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer)
{
- if (input == NULL)
+ if ((input_buffer == NULL) || (input_buffer->content == NULL))
{
- return NULL; /* no input */
+ return false; /* no input */
}
/* parse the different types of values */
/* null */
- if (!strncmp((const char*)input, "null", 4))
+ if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0))
{
item->type = cJSON_NULL;
- return input + 4;
+ input_buffer->offset += 4;
+ return true;
}
/* false */
- if (!strncmp((const char*)input, "false", 5))
+ if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0))
{
item->type = cJSON_False;
- return input + 5;
+ input_buffer->offset += 5;
+ return true;
}
/* true */
- if (!strncmp((const char*)input, "true", 4))
+ if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0))
{
item->type = cJSON_True;
item->valueint = 1;
- return input + 4;
+ input_buffer->offset += 4;
+ return true;
}
/* string */
- if (*input == '\"')
+ if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"'))
{
- return parse_string(item, input, error_pointer, hooks);
+ return parse_string(item, input_buffer);
}
/* number */
- if ((*input == '-') || ((*input >= '0') && (*input <= '9')))
+ if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9'))))
{
- return parse_number(item, input);
+ return parse_number(item, input_buffer);
}
/* array */
- if (*input == '[')
+ if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '['))
{
- return parse_array(item, input, error_pointer, hooks);
+ return parse_array(item, input_buffer);
}
/* object */
- if (*input == '{')
+ if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{'))
{
- return parse_object(item, input, error_pointer, hooks);
+ return parse_object(item, input_buffer);
}
- /* failure. */
- *error_pointer = input;
- return NULL;
+ return false;
}
/* Render a value to text. */
-static unsigned char *print_value(const cJSON * const item, const size_t depth, const cjbool format, printbuffer * const output_buffer, const internal_hooks * const hooks)
+static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer)
{
unsigned char *output = NULL;
if ((item == NULL) || (output_buffer == NULL))
{
- return NULL;
+ return false;
}
switch ((item->type) & 0xFF)
{
case cJSON_NULL:
- output = ensure(output_buffer, 5, hooks);
- if (output != NULL)
+ output = ensure(output_buffer, 5);
+ if (output == NULL)
{
- strcpy((char*)output, "null");
+ return false;
}
- break;
+ strcpy((char*)output, "null");
+ return true;
+
case cJSON_False:
- output = ensure(output_buffer, 6, hooks);
- if (output != NULL)
+ output = ensure(output_buffer, 6);
+ if (output == NULL)
{
- strcpy((char*)output, "false");
+ return false;
}
- break;
+ strcpy((char*)output, "false");
+ return true;
+
case cJSON_True:
- output = ensure(output_buffer, 5, hooks);
- if (output != NULL)
+ output = ensure(output_buffer, 5);
+ if (output == NULL)
{
- strcpy((char*)output, "true");
+ return false;
}
- break;
+ strcpy((char*)output, "true");
+ return true;
+
case cJSON_Number:
- output = print_number(item, output_buffer, hooks);
- break;
+ return print_number(item, output_buffer);
+
case cJSON_Raw:
{
size_t raw_length = 0;
if (item->valuestring == NULL)
{
- if (!output_buffer->noalloc)
- {
- hooks->deallocate(output_buffer->buffer);
- }
- output = NULL;
- break;
+ return false;
}
- raw_length = strlen(item->valuestring) + sizeof('\0');
- output = ensure(output_buffer, raw_length, hooks);
- if (output != NULL)
+ raw_length = strlen(item->valuestring) + sizeof("");
+ output = ensure(output_buffer, raw_length);
+ if (output == NULL)
{
- memcpy(output, item->valuestring, raw_length);
+ return false;
}
- break;
+ memcpy(output, item->valuestring, raw_length);
+ return true;
}
+
case cJSON_String:
- output = print_string(item, output_buffer, hooks);
- break;
+ return print_string(item, output_buffer);
+
case cJSON_Array:
- output = print_array(item, depth, format, output_buffer, hooks);
- break;
+ return print_array(item, output_buffer);
+
case cJSON_Object:
- output = print_object(item, depth, format, output_buffer, hooks);
- break;
+ return print_object(item, output_buffer);
+
default:
- output = NULL;
- break;
+ return false;
}
-
- return output;
}
/* Build an array from input text. */
-static const unsigned char *parse_array(cJSON * const item, const unsigned char *input, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
+static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer)
{
cJSON *head = NULL; /* head of the linked list */
cJSON *current_item = NULL;
- if (*input != '[')
+ if (input_buffer->depth >= CJSON_NESTING_LIMIT)
+ {
+ return false; /* to deeply nested */
+ }
+ input_buffer->depth++;
+
+ if (buffer_at_offset(input_buffer)[0] != '[')
{
/* not an array */
- *error_pointer = input;
goto fail;
}
- input = skip_whitespace(input + 1);
- if (*input == ']')
+ input_buffer->offset++;
+ buffer_skip_whitespace(input_buffer);
+ if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']'))
{
/* empty array */
goto success;
}
+ /* check if we skipped to the end of the buffer */
+ if (cannot_access_at_index(input_buffer, 0))
+ {
+ input_buffer->offset--;
+ goto fail;
+ }
+
/* step back to character in front of the first element */
- input--;
+ input_buffer->offset--;
/* loop through the comma separated array elements */
do
{
/* allocate next item */
- cJSON *new_item = cJSON_New_Item(hooks);
+ cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
if (new_item == NULL)
{
goto fail; /* allocation failure */
@@ -1114,27 +1404,30 @@ static const unsigned char *parse_array(cJSON * const item, const unsigned char
}
/* parse next value */
- input = skip_whitespace(input + 1);
- input = parse_value(current_item, input, error_pointer, hooks);
- input = skip_whitespace(input);
- if (input == NULL)
+ input_buffer->offset++;
+ buffer_skip_whitespace(input_buffer);
+ if (!parse_value(current_item, input_buffer))
{
goto fail; /* failed to parse value */
}
+ buffer_skip_whitespace(input_buffer);
}
- while (*input == ',');
+ while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
- if (*input != ']')
+ if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']')
{
- *error_pointer = input;
goto fail; /* expected end of array */
}
success:
+ input_buffer->depth--;
+
item->type = cJSON_Array;
item->child = head;
- return input + 1;
+ input_buffer->offset++;
+
+ return true;
fail:
if (head != NULL)
@@ -1142,52 +1435,50 @@ static const unsigned char *parse_array(cJSON * const item, const unsigned char
cJSON_Delete(head);
}
- return NULL;
+ return false;
}
/* Render an array to text */
-static unsigned char *print_array(const cJSON * const item, const size_t depth, const cjbool format, printbuffer * const output_buffer, const internal_hooks * const hooks)
+static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer)
{
- unsigned char *output = NULL;
unsigned char *output_pointer = NULL;
size_t length = 0;
cJSON *current_element = item->child;
- size_t output_offset = 0;
if (output_buffer == NULL)
{
- return NULL;
+ return false;
}
/* Compose the output array. */
/* opening square bracket */
- output_offset = output_buffer->offset;
- output_pointer = ensure(output_buffer, 1, hooks);
+ output_pointer = ensure(output_buffer, 1);
if (output_pointer == NULL)
{
- return NULL;
+ return false;
}
*output_pointer = '[';
output_buffer->offset++;
+ output_buffer->depth++;
while (current_element != NULL)
{
- if (print_value(current_element, depth + 1, format, output_buffer, hooks) == NULL)
+ if (!print_value(current_element, output_buffer))
{
- return NULL;
+ return false;
}
update_offset(output_buffer);
if (current_element->next)
{
- length = format ? 2 : 1;
- output_pointer = ensure(output_buffer, length + 1, hooks);
+ length = (size_t) (output_buffer->format ? 2 : 1);
+ output_pointer = ensure(output_buffer, length + 1);
if (output_pointer == NULL)
{
- return NULL;
+ return false;
}
*output_pointer++ = ',';
- if(format)
+ if(output_buffer->format)
{
*output_pointer++ = ' ';
}
@@ -1197,43 +1488,56 @@ static unsigned char *print_array(const cJSON * const item, const size_t depth,
current_element = current_element->next;
}
- output_pointer = ensure(output_buffer, 2, hooks);
+ output_pointer = ensure(output_buffer, 2);
if (output_pointer == NULL)
{
- return NULL;
+ return false;
}
*output_pointer++ = ']';
*output_pointer = '\0';
- output = output_buffer->buffer + output_offset;
+ output_buffer->depth--;
- return output;
+ return true;
}
/* Build an object from the text. */
-static const unsigned char *parse_object(cJSON * const item, const unsigned char *input, const unsigned char ** const error_pointer, const internal_hooks * const hooks)
+static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer)
{
cJSON *head = NULL; /* linked list head */
cJSON *current_item = NULL;
- if (*input != '{')
+ if (input_buffer->depth >= CJSON_NESTING_LIMIT)
+ {
+ return false; /* to deeply nested */
+ }
+ input_buffer->depth++;
+
+ if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{'))
{
- *error_pointer = input;
goto fail; /* not an object */
}
- input = skip_whitespace(input + 1);
- if (*input == '}')
+ input_buffer->offset++;
+ buffer_skip_whitespace(input_buffer);
+ if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}'))
{
goto success; /* empty object */
}
+ /* check if we skipped to the end of the buffer */
+ if (cannot_access_at_index(input_buffer, 0))
+ {
+ input_buffer->offset--;
+ goto fail;
+ }
+
/* step back to character in front of the first element */
- input--;
+ input_buffer->offset--;
/* loop through the comma separated array elements */
do
{
/* allocate next item */
- cJSON *new_item = cJSON_New_Item(hooks);
+ cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks));
if (new_item == NULL)
{
goto fail; /* allocation failure */
@@ -1254,46 +1558,47 @@ static const unsigned char *parse_object(cJSON * const item, const unsigned char
}
/* parse the name of the child */
- input = skip_whitespace(input + 1);
- input = parse_string(current_item, input, error_pointer, hooks);
- input = skip_whitespace(input);
- if (input == NULL)
+ input_buffer->offset++;
+ buffer_skip_whitespace(input_buffer);
+ if (!parse_string(current_item, input_buffer))
{
- goto fail; /* faile to parse name */
+ goto fail; /* failed to parse name */
}
+ buffer_skip_whitespace(input_buffer);
/* swap valuestring and string, because we parsed the name */
current_item->string = current_item->valuestring;
current_item->valuestring = NULL;
- if (*input != ':')
+ if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':'))
{
- *error_pointer = input;
goto fail; /* invalid object */
}
/* parse the value */
- input = skip_whitespace(input + 1);
- input = parse_value(current_item, input, error_pointer, hooks);
- input = skip_whitespace(input);
- if (input == NULL)
+ input_buffer->offset++;
+ buffer_skip_whitespace(input_buffer);
+ if (!parse_value(current_item, input_buffer))
{
goto fail; /* failed to parse value */
}
+ buffer_skip_whitespace(input_buffer);
}
- while (*input == ',');
+ while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
- if (*input != '}')
+ if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}'))
{
- *error_pointer = input;
goto fail; /* expected end of object */
}
success:
+ input_buffer->depth--;
+
item->type = cJSON_Object;
item->child = head;
- return input + 1;
+ input_buffer->offset++;
+ return true;
fail:
if (head != NULL)
@@ -1301,34 +1606,32 @@ static const unsigned char *parse_object(cJSON * const item, const unsigned char
cJSON_Delete(head);
}
- return NULL;
+ return false;
}
/* Render an object to text. */
-static unsigned char *print_object(const cJSON * const item, const size_t depth, const cjbool format, printbuffer * const output_buffer, const internal_hooks * const hooks)
+static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer)
{
- unsigned char *output = NULL;
unsigned char *output_pointer = NULL;
size_t length = 0;
- size_t output_offset = 0;
cJSON *current_item = item->child;
if (output_buffer == NULL)
{
- return NULL;
+ return false;
}
/* Compose the output: */
- output_offset = output_buffer->offset;
- length = format ? 2 : 1; /* fmt: {\n */
- output_pointer = ensure(output_buffer, length + 1, hooks);
+ length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */
+ output_pointer = ensure(output_buffer, length + 1);
if (output_pointer == NULL)
{
- return NULL;
+ return false;
}
*output_pointer++ = '{';
- if (format)
+ output_buffer->depth++;
+ if (output_buffer->format)
{
*output_pointer++ = '\n';
}
@@ -1336,61 +1639,61 @@ static unsigned char *print_object(const cJSON * const item, const size_t depth,
while (current_item)
{
- if (format)
+ if (output_buffer->format)
{
size_t i;
- output_pointer = ensure(output_buffer, depth + 1, hooks);
+ output_pointer = ensure(output_buffer, output_buffer->depth);
if (output_pointer == NULL)
{
- return NULL;
+ return false;
}
- for (i = 0; i < depth + 1; i++)
+ for (i = 0; i < output_buffer->depth; i++)
{
*output_pointer++ = '\t';
}
- output_buffer->offset += depth + 1;
+ output_buffer->offset += output_buffer->depth;
}
/* print key */
- if (print_string_ptr((unsigned char*)current_item->string, output_buffer, hooks) == NULL)
+ if (!print_string_ptr((unsigned char*)current_item->string, output_buffer))
{
- return NULL;
+ return false;
}
update_offset(output_buffer);
- length = format ? 2 : 1;
- output_pointer = ensure(output_buffer, length, hooks);
+ length = (size_t) (output_buffer->format ? 2 : 1);
+ output_pointer = ensure(output_buffer, length);
if (output_pointer == NULL)
{
- return NULL;
+ return false;
}
*output_pointer++ = ':';
- if (format)
+ if (output_buffer->format)
{
*output_pointer++ = '\t';
}
output_buffer->offset += length;
/* print value */
- if (!print_value(current_item, depth + 1, format, output_buffer, hooks))
+ if (!print_value(current_item, output_buffer))
{
- return NULL;
+ return false;
}
update_offset(output_buffer);
/* print comma if not last */
- length = (size_t) (format ? 1 : 0) + (current_item->next ? 1 : 0);
- output_pointer = ensure(output_buffer, length + 1, hooks);
+ length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0));
+ output_pointer = ensure(output_buffer, length + 1);
if (output_pointer == NULL)
{
- return NULL;
+ return false;
}
if (current_item->next)
{
*output_pointer++ = ',';
}
- if (format)
+ if (output_buffer->format)
{
*output_pointer++ = '\n';
}
@@ -1400,83 +1703,122 @@ static unsigned char *print_object(const cJSON * const item, const size_t depth,
current_item = current_item->next;
}
- output_pointer = ensure(output_buffer, format ? (depth + 2) : 2, hooks);
+ output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2);
if (output_pointer == NULL)
{
- return NULL;
+ return false;
}
- if (format)
+ if (output_buffer->format)
{
size_t i;
- for (i = 0; i < (depth); i++)
+ for (i = 0; i < (output_buffer->depth - 1); i++)
{
*output_pointer++ = '\t';
}
}
*output_pointer++ = '}';
*output_pointer = '\0';
- output = (output_buffer->buffer) + output_offset;
+ output_buffer->depth--;
- return output;
+ return true;
}
/* Get Array size/item / object item. */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
{
- cJSON *c = array->child;
- size_t i = 0;
- while(c)
+ cJSON *child = NULL;
+ size_t size = 0;
+
+ if (array == NULL)
{
- i++;
- c = c->next;
+ return 0;
+ }
+
+ child = array->child;
+
+ while(child != NULL)
+ {
+ size++;
+ child = child->next;
}
/* FIXME: Can overflow here. Cannot be fixed without breaking the API */
- return (int)i;
+ return (int)size;
}
-CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item)
+static cJSON* get_array_item(const cJSON *array, size_t index)
{
- cJSON *c = array ? array->child : NULL;
- while (c && item > 0)
+ cJSON *current_child = NULL;
+
+ if (array == NULL)
{
- item--;
- c = c->next;
+ return NULL;
}
- return c;
+ current_child = array->child;
+ while ((current_child != NULL) && (index > 0))
+ {
+ index--;
+ current_child = current_child->next;
+ }
+
+ return current_child;
}
-CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *object, const char *string)
+CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index)
{
- cJSON *c = object ? object->child : NULL;
- while (c && cJSON_strcasecmp((unsigned char*)c->string, (const unsigned char*)string))
+ if (index < 0)
{
- c = c->next;
+ return NULL;
}
- return c;
+
+ return get_array_item(array, (size_t)index);
}
-CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string)
+static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive)
{
cJSON *current_element = NULL;
- if ((object == NULL) || (string == NULL))
+ if ((object == NULL) || (name == NULL))
{
return NULL;
}
current_element = object->child;
- while ((current_element != NULL) && (strcmp(string, current_element->string) != 0))
+ if (case_sensitive)
{
- current_element = current_element->next;
+ while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0))
+ {
+ current_element = current_element->next;
+ }
+ }
+ else
+ {
+ while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0))
+ {
+ current_element = current_element->next;
+ }
+ }
+
+ if ((current_element == NULL) || (current_element->string == NULL)) {
+ return NULL;
}
return current_element;
}
-CJSON_PUBLIC(cjbool) cJSON_HasObjectItem(const cJSON *object, const char *string)
+CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)
+{
+ return get_object_item(object, string, false);
+}
+
+CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string)
+{
+ return get_object_item(object, string, true);
+}
+
+CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string)
{
return cJSON_GetObjectItem(object, string) ? 1 : 0;
}
@@ -1491,26 +1833,32 @@ static void suffix_object(cJSON *prev, cJSON *item)
/* Utility for handling references. */
static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks)
{
- cJSON *ref = cJSON_New_Item(hooks);
- if (!ref)
+ cJSON *reference = NULL;
+ if (item == NULL)
{
return NULL;
}
- memcpy(ref, item, sizeof(cJSON));
- ref->string = NULL;
- ref->type |= cJSON_IsReference;
- ref->next = ref->prev = NULL;
- return ref;
+
+ reference = cJSON_New_Item(hooks);
+ if (reference == NULL)
+ {
+ return NULL;
+ }
+
+ memcpy(reference, item, sizeof(cJSON));
+ reference->string = NULL;
+ reference->type |= cJSON_IsReference;
+ reference->next = reference->prev = NULL;
+ return reference;
}
-/* Add item to array/object. */
-CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item)
+static cJSON_bool add_item_to_array(cJSON *array, cJSON *item)
{
cJSON *child = NULL;
if ((item == NULL) || (array == NULL))
{
- return;
+ return false;
}
child = array->child;
@@ -1529,76 +1877,238 @@ CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item)
}
suffix_object(child, item);
}
+
+ return true;
+}
+
+/* Add item to array/object. */
+CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item)
+{
+ add_item_to_array(array, item);
+}
+
+#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
+ #pragma GCC diagnostic push
+#endif
+#ifdef __GNUC__
+#pragma GCC diagnostic ignored "-Wcast-qual"
+#endif
+/* helper function to cast away const */
+static void* cast_away_const(const void* string)
+{
+ return (void*)string;
+}
+#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5))))
+ #pragma GCC diagnostic pop
+#endif
+
+
+static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key)
+{
+ char *new_key = NULL;
+ int new_type = cJSON_Invalid;
+
+ if ((object == NULL) || (string == NULL) || (item == NULL))
+ {
+ return false;
+ }
+
+ if (constant_key)
+ {
+ new_key = (char*)cast_away_const(string);
+ new_type = item->type | cJSON_StringIsConst;
+ }
+ else
+ {
+ new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks);
+ if (new_key == NULL)
+ {
+ return false;
+ }
+
+ new_type = item->type & ~cJSON_StringIsConst;
+ }
+
+ if (!(item->type & cJSON_StringIsConst) && (item->string != NULL))
+ {
+ hooks->deallocate(item->string);
+ }
+
+ item->string = new_key;
+ item->type = new_type;
+
+ return add_item_to_array(object, item);
}
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
{
- /* call cJSON_AddItemToObjectCS for code reuse */
- cJSON_AddItemToObjectCS(object, (char*)cJSON_strdup((const unsigned char*)string, &global_hooks), item);
- /* remove cJSON_StringIsConst flag */
- item->type &= ~cJSON_StringIsConst;
+ add_item_to_object(object, string, item, &global_hooks, false);
}
/* Add an item to an object with constant string as key */
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item)
{
- if (!item)
+ add_item_to_object(object, string, item, &global_hooks, true);
+}
+
+CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
+{
+ if (array == NULL)
{
return;
}
- if (!(item->type & cJSON_StringIsConst) && item->string)
+
+ add_item_to_array(array, create_reference(item, &global_hooks));
+}
+
+CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
+{
+ if ((object == NULL) || (string == NULL))
{
- global_hooks.deallocate(item->string);
+ return;
}
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wcast-qual"
- item->string = (char*)string;
-#pragma GCC diagnostic pop
- item->type |= cJSON_StringIsConst;
- cJSON_AddItemToArray(object, item);
+
+ add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false);
}
-CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)
+CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name)
{
- cJSON_AddItemToArray(array, create_reference(item, &global_hooks));
+ cJSON *null = cJSON_CreateNull();
+ if (add_item_to_object(object, name, null, &global_hooks, false))
+ {
+ return null;
+ }
+
+ cJSON_Delete(null);
+ return NULL;
}
-CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item)
+CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name)
{
- cJSON_AddItemToObject(object, string, create_reference(item, &global_hooks));
+ cJSON *true_item = cJSON_CreateTrue();
+ if (add_item_to_object(object, name, true_item, &global_hooks, false))
+ {
+ return true_item;
+ }
+
+ cJSON_Delete(true_item);
+ return NULL;
}
-static cJSON *DetachItemFromArray(cJSON *array, size_t which)
+CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name)
{
- cJSON *c = array->child;
- while (c && (which > 0))
+ cJSON *false_item = cJSON_CreateFalse();
+ if (add_item_to_object(object, name, false_item, &global_hooks, false))
{
- c = c->next;
- which--;
+ return false_item;
}
- if (!c)
+
+ cJSON_Delete(false_item);
+ return NULL;
+}
+
+CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean)
+{
+ cJSON *bool_item = cJSON_CreateBool(boolean);
+ if (add_item_to_object(object, name, bool_item, &global_hooks, false))
+ {
+ return bool_item;
+ }
+
+ cJSON_Delete(bool_item);
+ return NULL;
+}
+
+CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number)
+{
+ cJSON *number_item = cJSON_CreateNumber(number);
+ if (add_item_to_object(object, name, number_item, &global_hooks, false))
+ {
+ return number_item;
+ }
+
+ cJSON_Delete(number_item);
+ return NULL;
+}
+
+CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string)
+{
+ cJSON *string_item = cJSON_CreateString(string);
+ if (add_item_to_object(object, name, string_item, &global_hooks, false))
+ {
+ return string_item;
+ }
+
+ cJSON_Delete(string_item);
+ return NULL;
+}
+
+CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw)
+{
+ cJSON *raw_item = cJSON_CreateRaw(raw);
+ if (add_item_to_object(object, name, raw_item, &global_hooks, false))
+ {
+ return raw_item;
+ }
+
+ cJSON_Delete(raw_item);
+ return NULL;
+}
+
+CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name)
+{
+ cJSON *object_item = cJSON_CreateObject();
+ if (add_item_to_object(object, name, object_item, &global_hooks, false))
+ {
+ return object_item;
+ }
+
+ cJSON_Delete(object_item);
+ return NULL;
+}
+
+CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name)
+{
+ cJSON *array = cJSON_CreateArray();
+ if (add_item_to_object(object, name, array, &global_hooks, false))
+ {
+ return array;
+ }
+
+ cJSON_Delete(array);
+ return NULL;
+}
+
+CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item)
+{
+ if ((parent == NULL) || (item == NULL))
{
- /* item doesn't exist */
return NULL;
}
- if (c->prev)
+
+ if (item->prev != NULL)
{
/* not the first element */
- c->prev->next = c->next;
+ item->prev->next = item->next;
}
- if (c->next)
+ if (item->next != NULL)
{
- c->next->prev = c->prev;
+ /* not the last element */
+ item->next->prev = item->prev;
}
- if (c==array->child)
+
+ if (item == parent->child)
{
- array->child = c->next;
+ /* first element */
+ parent->child = item->next;
}
/* make sure the detached item doesn't point anywhere anymore */
- c->prev = c->next = NULL;
+ item->prev = NULL;
+ item->next = NULL;
- return c;
+ return item;
}
+
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
{
if (which < 0)
@@ -1606,7 +2116,7 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which)
return NULL;
}
- return DetachItemFromArray(array, (size_t)which);
+ return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which));
}
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
@@ -1616,19 +2126,16 @@ CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which)
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string)
{
- size_t i = 0;
- cJSON *c = object->child;
- while (c && cJSON_strcasecmp((unsigned char*)c->string, (const unsigned char*)string))
- {
- i++;
- c = c->next;
- }
- if (c)
- {
- return DetachItemFromArray(object, i);
- }
+ cJSON *to_detach = cJSON_GetObjectItem(object, string);
- return NULL;
+ return cJSON_DetachItemViaPointer(object, to_detach);
+}
+
+CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string)
+{
+ cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string);
+
+ return cJSON_DetachItemViaPointer(object, to_detach);
}
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
@@ -1636,24 +2143,32 @@ CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
cJSON_Delete(cJSON_DetachItemFromObject(object, string));
}
+CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string)
+{
+ cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string));
+}
+
/* Replace array/object items with new ones. */
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem)
{
- cJSON *c = array->child;
- while (c && (which > 0))
+ cJSON *after_inserted = NULL;
+
+ if (which < 0)
{
- c = c->next;
- which--;
+ return;
}
- if (!c)
+
+ after_inserted = get_array_item(array, (size_t)which);
+ if (after_inserted == NULL)
{
- cJSON_AddItemToArray(array, newitem);
+ add_item_to_array(array, newitem);
return;
}
- newitem->next = c;
- newitem->prev = c->prev;
- c->prev = newitem;
- if (c == array->child)
+
+ newitem->next = after_inserted;
+ newitem->prev = after_inserted->prev;
+ after_inserted->prev = newitem;
+ if (after_inserted == array->child)
{
array->child = newitem;
}
@@ -1663,35 +2178,41 @@ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newit
}
}
-static void ReplaceItemInArray(cJSON *array, size_t which, cJSON *newitem)
+CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement)
{
- cJSON *c = array->child;
- while (c && (which > 0))
+ if ((parent == NULL) || (replacement == NULL) || (item == NULL))
{
- c = c->next;
- which--;
+ return false;
}
- if (!c)
+
+ if (replacement == item)
{
- return;
+ return true;
}
- newitem->next = c->next;
- newitem->prev = c->prev;
- if (newitem->next)
+
+ replacement->next = item->next;
+ replacement->prev = item->prev;
+
+ if (replacement->next != NULL)
{
- newitem->next->prev = newitem;
+ replacement->next->prev = replacement;
}
- if (c == array->child)
+ if (replacement->prev != NULL)
{
- array->child = newitem;
+ replacement->prev->next = replacement;
}
- else
+ if (parent->child == item)
{
- newitem->prev->next = newitem;
+ parent->child = replacement;
}
- c->next = c->prev = NULL;
- cJSON_Delete(c);
+
+ item->next = NULL;
+ item->prev = NULL;
+ cJSON_Delete(item);
+
+ return true;
}
+
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem)
{
if (which < 0)
@@ -1699,29 +2220,37 @@ CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newi
return;
}
- ReplaceItemInArray(array, (size_t)which, newitem);
+ cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem);
}
-CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
+static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive)
{
- size_t i = 0;
- cJSON *c = object->child;
- while(c && cJSON_strcasecmp((unsigned char*)c->string, (const unsigned char*)string))
+ if ((replacement == NULL) || (string == NULL))
{
- i++;
- c = c->next;
+ return false;
}
- if(c)
- {
- /* free the old string if not const */
- if (!(newitem->type & cJSON_StringIsConst) && newitem->string)
- {
- global_hooks.deallocate(newitem->string);
- }
- newitem->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
- ReplaceItemInArray(object, i, newitem);
+ /* replace the name in the replacement */
+ if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL))
+ {
+ cJSON_free(replacement->string);
}
+ replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks);
+ replacement->type &= ~cJSON_StringIsConst;
+
+ cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement);
+
+ return true;
+}
+
+CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem)
+{
+ replace_item_in_object(object, string, newitem, false);
+}
+
+CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem)
+{
+ replace_item_in_object(object, string, newitem, true);
}
/* Create basic types: */
@@ -1758,7 +2287,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void)
return item;
}
-CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cjbool b)
+CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool b)
{
cJSON *item = cJSON_New_Item(&global_hooks);
if(item)
@@ -1782,7 +2311,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num)
{
item->valueint = INT_MAX;
}
- else if (num <= INT_MIN)
+ else if (num <= (double)INT_MIN)
{
item->valueint = INT_MIN;
}
@@ -1812,6 +2341,39 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
return item;
}
+CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string)
+{
+ cJSON *item = cJSON_New_Item(&global_hooks);
+ if (item != NULL)
+ {
+ item->type = cJSON_String | cJSON_IsReference;
+ item->valuestring = (char*)cast_away_const(string);
+ }
+
+ return item;
+}
+
+CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child)
+{
+ cJSON *item = cJSON_New_Item(&global_hooks);
+ if (item != NULL) {
+ item->type = cJSON_Object | cJSON_IsReference;
+ item->child = (cJSON*)cast_away_const(child);
+ }
+
+ return item;
+}
+
+CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) {
+ cJSON *item = cJSON_New_Item(&global_hooks);
+ if (item != NULL) {
+ item->type = cJSON_Array | cJSON_IsReference;
+ item->child = (cJSON*)cast_away_const(child);
+ }
+
+ return item;
+}
+
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw)
{
cJSON *item = cJSON_New_Item(&global_hooks);
@@ -1859,7 +2421,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
cJSON *p = NULL;
cJSON *a = NULL;
- if (count < 0)
+ if ((count < 0) || (numbers == NULL))
{
return NULL;
}
@@ -1894,7 +2456,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
cJSON *p = NULL;
cJSON *a = NULL;
- if (count < 0)
+ if ((count < 0) || (numbers == NULL))
{
return NULL;
}
@@ -1903,7 +2465,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
for(i = 0; a && (i < (size_t)count); i++)
{
- n = cJSON_CreateNumber(numbers[i]);
+ n = cJSON_CreateNumber((double)numbers[i]);
if(!n)
{
cJSON_Delete(a);
@@ -1930,7 +2492,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
cJSON *p = NULL;
cJSON *a = NULL;
- if (count < 0)
+ if ((count < 0) || (numbers == NULL))
{
return NULL;
}
@@ -1966,7 +2528,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count)
cJSON *p = NULL;
cJSON *a = NULL;
- if (count < 0)
+ if ((count < 0) || (strings == NULL))
{
return NULL;
}
@@ -1996,7 +2558,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count)
}
/* Duplication */
-CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cjbool recurse)
+CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
{
cJSON *newitem = NULL;
cJSON *child = NULL;
@@ -2075,66 +2637,334 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cjbool recurse)
return NULL;
}
-CJSON_PUBLIC(void) cJSON_Minify(char *json)
+static void skip_oneline_comment(char **input)
{
- unsigned char *into = (unsigned char*)json;
- while (*json)
+ *input += static_strlen("//");
+
+ for (; (*input)[0] != '\0'; ++(*input))
{
- if (*json == ' ')
- {
- json++;
+ if ((*input)[0] == '\n') {
+ *input += static_strlen("\n");
+ return;
}
- else if (*json == '\t')
+ }
+}
+
+static void skip_multiline_comment(char **input)
+{
+ *input += static_strlen("/*");
+
+ for (; (*input)[0] != '\0'; ++(*input))
+ {
+ if (((*input)[0] == '*') && ((*input)[1] == '/'))
{
- /* Whitespace characters. */
- json++;
+ *input += static_strlen("*/");
+ return;
}
- else if (*json == '\r')
- {
- json++;
+ }
+}
+
+static void minify_string(char **input, char **output) {
+ (*output)[0] = (*input)[0];
+ *input += static_strlen("\"");
+ *output += static_strlen("\"");
+
+
+ for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) {
+ (*output)[0] = (*input)[0];
+
+ if ((*input)[0] == '\"') {
+ (*output)[0] = '\"';
+ *input += static_strlen("\"");
+ *output += static_strlen("\"");
+ return;
+ } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) {
+ (*output)[1] = (*input)[1];
+ *input += static_strlen("\"");
+ *output += static_strlen("\"");
}
- else if (*json=='\n')
+ }
+}
+
+CJSON_PUBLIC(void) cJSON_Minify(char *json)
+{
+ char *into = json;
+
+ if (json == NULL)
+ {
+ return;
+ }
+
+ while (json[0] != '\0')
+ {
+ switch (json[0])
{
- json++;
+ case ' ':
+ case '\t':
+ case '\r':
+ case '\n':
+ json++;
+ break;
+
+ case '/':
+ if (json[1] == '/')
+ {
+ skip_oneline_comment(&json);
+ }
+ else if (json[1] == '*')
+ {
+ skip_multiline_comment(&json);
+ } else {
+ json++;
+ }
+ break;
+
+ case '\"':
+ minify_string(&json, (char**)&into);
+ break;
+
+ default:
+ into[0] = json[0];
+ json++;
+ into++;
}
- else if ((*json == '/') && (json[1] == '/'))
- {
- /* double-slash comments, to end of line. */
- while (*json && (*json != '\n'))
+ }
+
+ /* and null-terminate. */
+ *into = '\0';
+}
+
+CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item)
+{
+ if (item == NULL)
+ {
+ return false;
+ }
+
+ return (item->type & 0xFF) == cJSON_Invalid;
+}
+
+CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item)
+{
+ if (item == NULL)
+ {
+ return false;
+ }
+
+ return (item->type & 0xFF) == cJSON_False;
+}
+
+CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item)
+{
+ if (item == NULL)
+ {
+ return false;
+ }
+
+ return (item->type & 0xff) == cJSON_True;
+}
+
+
+CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item)
+{
+ if (item == NULL)
+ {
+ return false;
+ }
+
+ return (item->type & (cJSON_True | cJSON_False)) != 0;
+}
+CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item)
+{
+ if (item == NULL)
+ {
+ return false;
+ }
+
+ return (item->type & 0xFF) == cJSON_NULL;
+}
+
+CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item)
+{
+ if (item == NULL)
+ {
+ return false;
+ }
+
+ return (item->type & 0xFF) == cJSON_Number;
+}
+
+CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item)
+{
+ if (item == NULL)
+ {
+ return false;
+ }
+
+ return (item->type & 0xFF) == cJSON_String;
+}
+
+CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item)
+{
+ if (item == NULL)
+ {
+ return false;
+ }
+
+ return (item->type & 0xFF) == cJSON_Array;
+}
+
+CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item)
+{
+ if (item == NULL)
+ {
+ return false;
+ }
+
+ return (item->type & 0xFF) == cJSON_Object;
+}
+
+CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item)
+{
+ if (item == NULL)
+ {
+ return false;
+ }
+
+ return (item->type & 0xFF) == cJSON_Raw;
+}
+
+CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive)
+{
+ if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)) || cJSON_IsInvalid(a))
+ {
+ return false;
+ }
+
+ /* check if type is valid */
+ switch (a->type & 0xFF)
+ {
+ case cJSON_False:
+ case cJSON_True:
+ case cJSON_NULL:
+ case cJSON_Number:
+ case cJSON_String:
+ case cJSON_Raw:
+ case cJSON_Array:
+ case cJSON_Object:
+ break;
+
+ default:
+ return false;
+ }
+
+ /* identical objects are equal */
+ if (a == b)
+ {
+ return true;
+ }
+
+ switch (a->type & 0xFF)
+ {
+ /* in these cases and equal type is enough */
+ case cJSON_False:
+ case cJSON_True:
+ case cJSON_NULL:
+ return true;
+
+ case cJSON_Number:
+ if (a->valuedouble == b->valuedouble)
{
- json++;
+ return true;
}
- }
- else if ((*json == '/') && (json[1] == '*'))
- {
- /* multiline comments. */
- while (*json && !((*json == '*') && (json[1] == '/')))
+ return false;
+
+ case cJSON_String:
+ case cJSON_Raw:
+ if ((a->valuestring == NULL) || (b->valuestring == NULL))
{
- json++;
+ return false;
}
- json += 2;
- }
- else if (*json == '\"')
+ if (strcmp(a->valuestring, b->valuestring) == 0)
+ {
+ return true;
+ }
+
+ return false;
+
+ case cJSON_Array:
{
- /* string literals, which are \" sensitive. */
- *into++ = (unsigned char)*json++;
- while (*json && (*json != '\"'))
+ cJSON *a_element = a->child;
+ cJSON *b_element = b->child;
+
+ for (; (a_element != NULL) && (b_element != NULL);)
{
- if (*json == '\\')
+ if (!cJSON_Compare(a_element, b_element, case_sensitive))
{
- *into++ = (unsigned char)*json++;
+ return false;
}
- *into++ = (unsigned char)*json++;
+
+ a_element = a_element->next;
+ b_element = b_element->next;
+ }
+
+ /* one of the arrays is longer than the other */
+ if (a_element != b_element) {
+ return false;
}
- *into++ = (unsigned char)*json++;
+
+ return true;
}
- else
+
+ case cJSON_Object:
{
- /* All other characters. */
- *into++ = (unsigned char)*json++;
+ cJSON *a_element = NULL;
+ cJSON *b_element = NULL;
+ cJSON_ArrayForEach(a_element, a)
+ {
+ /* TODO This has O(n^2) runtime, which is horrible! */
+ b_element = get_object_item(b, a_element->string, case_sensitive);
+ if (b_element == NULL)
+ {
+ return false;
+ }
+
+ if (!cJSON_Compare(a_element, b_element, case_sensitive))
+ {
+ return false;
+ }
+ }
+
+ /* doing this twice, once on a and b to prevent true comparison if a subset of b
+ * TODO: Do this the proper way, this is just a fix for now */
+ cJSON_ArrayForEach(b_element, b)
+ {
+ a_element = get_object_item(a, b_element->string, case_sensitive);
+ if (a_element == NULL)
+ {
+ return false;
+ }
+
+ if (!cJSON_Compare(b_element, a_element, case_sensitive))
+ {
+ return false;
+ }
+ }
+
+ return true;
}
+
+ default:
+ return false;
}
+}
- /* and null-terminate. */
- *into = '\0';
+CJSON_PUBLIC(void *) cJSON_malloc(size_t size)
+{
+ return global_hooks.allocate(size);
+}
+
+CJSON_PUBLIC(void) cJSON_free(void *object)
+{
+ global_hooks.deallocate(object);
}
diff --git a/libs/esl/src/cJSON_Utils.c b/libs/esl/src/cJSON_Utils.c
index 395aa736923..2bacf642ac8 100644
--- a/libs/esl/src/cJSON_Utils.c
+++ b/libs/esl/src/cJSON_Utils.c
@@ -1,126 +1,194 @@
+/*
+ Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+*/
+
+/* disable warnings about old C89 functions in MSVC */
+#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
+#define _CRT_SECURE_NO_DEPRECATE
+#endif
+
+#ifdef __GNUCC__
+#pragma GCC visibility push(default)
+#endif
+#if defined(_MSC_VER)
+#pragma warning (push)
+/* disable warning about single line comments in system headers */
+#pragma warning (disable : 4001)
+#endif
+
#include
#include
#include
#include
#include
-#include
+
+#if defined(_MSC_VER)
+#pragma warning (pop)
+#endif
+#ifdef __GNUCC__
+#pragma GCC visibility pop
+#endif
#include "esl_cJSON_Utils.h"
-static unsigned char* cJSONUtils_strdup(const unsigned char* str)
+/* define our own boolean type */
+#ifdef true
+#undef true
+#endif
+#define true ((cJSON_bool)1)
+
+#ifdef false
+#undef false
+#endif
+#define false ((cJSON_bool)0)
+
+static unsigned char* cJSONUtils_strdup(const unsigned char* const string)
{
- size_t len = 0;
+ size_t length = 0;
unsigned char *copy = NULL;
- len = strlen((const char*)str) + 1;
- if (!(copy = (unsigned char*)malloc(len)))
+ length = strlen((const char*)string) + sizeof("");
+ copy = (unsigned char*) cJSON_malloc(length);
+ if (copy == NULL)
{
return NULL;
}
- memcpy(copy, str, len);
+ memcpy(copy, string, length);
return copy;
}
-static int cJSONUtils_strcasecmp(const unsigned char *s1, const unsigned char *s2)
+/* string comparison which doesn't consider NULL pointers equal */
+static int compare_strings(const unsigned char *string1, const unsigned char *string2, const cJSON_bool case_sensitive)
{
- if (!s1)
+ if ((string1 == NULL) || (string2 == NULL))
{
- return (s1 == s2) ? 0 : 1; /* both NULL? */
+ return 1;
}
- if (!s2)
+
+ if (string1 == string2)
{
- return 1;
+ return 0;
}
- for(; tolower(*s1) == tolower(*s2); ++s1, ++s2)
+
+ if (case_sensitive)
{
- if(*s1 == 0)
+ return strcmp((const char*)string1, (const char*)string2);
+ }
+
+ for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
+ {
+ if (*string1 == '\0')
{
return 0;
}
}
- return tolower(*s1) - tolower(*s2);
+ return tolower(*string1) - tolower(*string2);
}
-/* JSON Pointer implementation: */
-static int cJSONUtils_Pstrcasecmp(const unsigned char *a, const unsigned char *e)
+/* Compare the next path element of two JSON pointers, two NULL pointers are considered unequal: */
+static cJSON_bool compare_pointers(const unsigned char *name, const unsigned char *pointer, const cJSON_bool case_sensitive)
{
- if (!a || !e)
+ if ((name == NULL) || (pointer == NULL))
{
- return (a == e) ? 0 : 1; /* both NULL? */
+ return false;
}
- for (; *a && *e && (*e != '/'); a++, e++) /* compare until next '/' */
+
+ for (; (*name != '\0') && (*pointer != '\0') && (*pointer != '/'); (void)name++, pointer++) /* compare until next '/' */
{
- if (*e == '~')
+ if (*pointer == '~')
{
/* check for escaped '~' (~0) and '/' (~1) */
- if (!((e[1] == '0') && (*a == '~')) && !((e[1] == '1') && (*a == '/')))
+ if (((pointer[1] != '0') || (*name != '~')) && ((pointer[1] != '1') || (*name != '/')))
{
- /* invalid escape sequence or wrong character in *a */
- return 1;
+ /* invalid escape sequence or wrong character in *name */
+ return false;
}
else
{
- e++;
+ pointer++;
}
}
- else if (tolower(*a) != tolower(*e))
+ else if ((!case_sensitive && (tolower(*name) != tolower(*pointer))) || (case_sensitive && (*name != *pointer)))
{
- return 1;
+ return false;
}
}
- if (((*e != 0) && (*e != '/')) != (*a != 0))
+ if (((*pointer != 0) && (*pointer != '/')) != (*name != 0))
{
/* one string has ended, the other not */
- return 1;
+ return false;;
}
- return 0;
+ return true;
}
-static size_t cJSONUtils_PointerEncodedstrlen(const unsigned char *s)
+/* calculate the length of a string if encoded as JSON pointer with ~0 and ~1 escape sequences */
+static size_t pointer_encoded_length(const unsigned char *string)
{
- size_t l = 0;
- for (; *s; s++, l++)
+ size_t length;
+ for (length = 0; *string != '\0'; (void)string++, length++)
{
- if ((*s == '~') || (*s == '/'))
+ /* character needs to be escaped? */
+ if ((*string == '~') || (*string == '/'))
{
- l++;
+ length++;
}
}
- return l;
+ return length;
}
-static void cJSONUtils_PointerEncodedstrcpy(unsigned char *d, const unsigned char *s)
+/* copy a string while escaping '~' and '/' with ~0 and ~1 JSON pointer escape codes */
+static void encode_string_as_pointer(unsigned char *destination, const unsigned char *source)
{
- for (; *s; s++)
+ for (; source[0] != '\0'; (void)source++, destination++)
{
- if (*s == '/')
+ if (source[0] == '/')
{
- *d++ = '~';
- *d++ = '1';
+ destination[1] = '1';
+ destination++;
}
- else if (*s == '~')
+ else if (source[0] == '~')
{
- *d++ = '~';
- *d++ = '0';
+ destination[0] = '~';
+ destination[1] = '1';
+ destination++;
}
else
{
- *d++ = *s;
+ destination[0] = source[0];
}
}
- *d = '\0';
+ destination[0] = '\0';
}
-CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target)
+CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const object, const cJSON * const target)
{
- int type = object->type;
- size_t c = 0;
- cJSON *obj = 0;
+ size_t child_index = 0;
+ cJSON *current_child = 0;
+
+ if ((object == NULL) || (target == NULL))
+ {
+ return NULL;
+ }
if (object == target)
{
@@ -128,44 +196,44 @@ CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *ta
return (char*)cJSONUtils_strdup((const unsigned char*)"");
}
- /* recursively search all children of the object */
- for (obj = object->child; obj; obj = obj->next, c++)
+ /* recursively search all children of the object or array */
+ for (current_child = object->child; current_child != NULL; (void)(current_child = current_child->next), child_index++)
{
- unsigned char *found = (unsigned char*)cJSONUtils_FindPointerFromObjectTo(obj, target);
- if (found)
+ unsigned char *target_pointer = (unsigned char*)cJSONUtils_FindPointerFromObjectTo(current_child, target);
+ /* found the target? */
+ if (target_pointer != NULL)
{
- if ((type & 0xFF) == cJSON_Array)
+ if (cJSON_IsArray(object))
{
/* reserve enough memory for a 64 bit integer + '/' and '\0' */
- unsigned char *ret = (unsigned char*)malloc(strlen((char*)found) + 23);
- assert(ret);
+ unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + 20 + sizeof("/"));
/* check if conversion to unsigned long is valid
* This should be eliminated at compile time by dead code elimination
* if size_t is an alias of unsigned long, or if it is bigger */
- if (c > ULONG_MAX)
+ if (child_index > ULONG_MAX)
{
- free(found);
+ cJSON_free(target_pointer);
return NULL;
}
- sprintf((char*)ret, "/%lu%s", (unsigned long)c, found); /* / */
- free(found);
+ sprintf((char*)full_pointer, "/%lu%s", (unsigned long)child_index, target_pointer); /* / */
+ cJSON_free(target_pointer);
- return (char*)ret;
+ return (char*)full_pointer;
}
- else if ((type & 0xFF) == cJSON_Object)
+
+ if (cJSON_IsObject(object))
{
- unsigned char *ret = (unsigned char*)malloc(strlen((char*)found) + cJSONUtils_PointerEncodedstrlen((unsigned char*)obj->string) + 2);
- assert(ret);
- *ret = '/';
- cJSONUtils_PointerEncodedstrcpy(ret + 1, (unsigned char*)obj->string);
- strcat((char*)ret, (char*)found);
- free(found);
+ unsigned char *full_pointer = (unsigned char*)cJSON_malloc(strlen((char*)target_pointer) + pointer_encoded_length((unsigned char*)current_child->string) + 2);
+ full_pointer[0] = '/';
+ encode_string_as_pointer(full_pointer + 1, (unsigned char*)current_child->string);
+ strcat((char*)full_pointer, (char*)target_pointer);
+ cJSON_free(target_pointer);
- return (char*)ret;
+ return (char*)full_pointer;
}
/* reached leaf of the tree, found nothing */
- free(found);
+ cJSON_free(target_pointer);
return NULL;
}
}
@@ -174,392 +242,881 @@ CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *ta
return NULL;
}
-CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON *object, const char *pointer)
+/* non broken version of cJSON_GetArrayItem */
+static cJSON *get_array_item(const cJSON *array, size_t item)
+{
+ cJSON *child = array ? array->child : NULL;
+ while ((child != NULL) && (item > 0))
+ {
+ item--;
+ child = child->next;
+ }
+
+ return child;
+}
+
+static cJSON_bool decode_array_index_from_pointer(const unsigned char * const pointer, size_t * const index)
{
+ size_t parsed_index = 0;
+ size_t position = 0;
+
+ if ((pointer[0] == '0') && ((pointer[1] != '\0') && (pointer[1] != '/')))
+ {
+ /* leading zeroes are not permitted */
+ return 0;
+ }
+
+ for (position = 0; (pointer[position] >= '0') && (pointer[0] <= '9'); position++)
+ {
+ parsed_index = (10 * parsed_index) + (size_t)(pointer[position] - '0');
+
+ }
+
+ if ((pointer[position] != '\0') && (pointer[position] != '/'))
+ {
+ return 0;
+ }
+
+ *index = parsed_index;
+
+ return 1;
+}
+
+static cJSON *get_item_from_pointer(cJSON * const object, const char * pointer, const cJSON_bool case_sensitive)
+{
+ cJSON *current_element = object;
+
+ if (pointer == NULL)
+ {
+ return NULL;
+ }
+
/* follow path of the pointer */
- while ((*pointer++ == '/') && object)
+ while ((pointer[0] == '/') && (current_element != NULL))
{
- if ((object->type & 0xFF) == cJSON_Array)
+ pointer++;
+ if (cJSON_IsArray(current_element))
{
- size_t which = 0;
- /* parse array index */
- while ((*pointer >= '0') && (*pointer <= '9'))
- {
- which = (10 * which) + (size_t)(*pointer++ - '0');
- }
- if (*pointer && (*pointer != '/'))
- {
- /* not end of string or new path token */
- return NULL;
- }
- if (which > INT_MAX)
+ size_t index = 0;
+ if (!decode_array_index_from_pointer((const unsigned char*)pointer, &index))
{
return NULL;
}
- object = cJSON_GetArrayItem(object, (int)which);
+
+ current_element = get_array_item(current_element, index);
}
- else if ((object->type & 0xFF) == cJSON_Object)
+ else if (cJSON_IsObject(current_element))
{
- object = object->child;
+ current_element = current_element->child;
/* GetObjectItem. */
- while (object && cJSONUtils_Pstrcasecmp((unsigned char*)object->string, (const unsigned char*)pointer))
+ while ((current_element != NULL) && !compare_pointers((unsigned char*)current_element->string, (const unsigned char*)pointer, case_sensitive))
{
- object = object->next;
- }
- /* skip to the next path token or end of string */
- while (*pointer && (*pointer != '/'))
- {
- pointer++;
+ current_element = current_element->next;
}
}
else
{
return NULL;
}
+
+ /* skip to the next path token or end of string */
+ while ((pointer[0] != '\0') && (pointer[0] != '/'))
+ {
+ pointer++;
+ }
}
- return object;
+ return current_element;
+}
+
+CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer)
+{
+ return get_item_from_pointer(object, pointer, false);
+}
+
+CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointerCaseSensitive(cJSON * const object, const char *pointer)
+{
+ return get_item_from_pointer(object, pointer, true);
}
/* JSON Patch implementation. */
-static void cJSONUtils_InplaceDecodePointerString(unsigned char *string)
+static void decode_pointer_inplace(unsigned char *string)
{
- unsigned char *s2 = string;
+ unsigned char *decoded_string = string;
if (string == NULL) {
return;
}
- for (; *string; s2++, string++)
+ for (; *string; (void)decoded_string++, string++)
{
- *s2 = (*string != '~')
- ? (*string)
- : ((*(++string) == '0')
- ? '~'
- : '/');
+ if (string[0] == '~')
+ {
+ if (string[1] == '0')
+ {
+ decoded_string[0] = '~';
+ }
+ else if (string[1] == '1')
+ {
+ decoded_string[1] = '/';
+ }
+ else
+ {
+ /* invalid escape sequence */
+ return;
+ }
+
+ string++;
+ }
}
- *s2 = '\0';
+ decoded_string[0] = '\0';
}
-static cJSON *cJSONUtils_PatchDetach(cJSON *object, const unsigned char *path)
+/* non-broken cJSON_DetachItemFromArray */
+static cJSON *detach_item_from_array(cJSON *array, size_t which)
{
- unsigned char *parentptr = NULL;
- unsigned char *childptr = NULL;
+ cJSON *c = array->child;
+ while (c && (which > 0))
+ {
+ c = c->next;
+ which--;
+ }
+ if (!c)
+ {
+ /* item doesn't exist */
+ return NULL;
+ }
+ if (c->prev)
+ {
+ /* not the first element */
+ c->prev->next = c->next;
+ }
+ if (c->next)
+ {
+ c->next->prev = c->prev;
+ }
+ if (c==array->child)
+ {
+ array->child = c->next;
+ }
+ /* make sure the detached item doesn't point anywhere anymore */
+ c->prev = c->next = NULL;
+
+ return c;
+}
+
+/* detach an item at the given path */
+static cJSON *detach_path(cJSON *object, const unsigned char *path, const cJSON_bool case_sensitive)
+{
+ unsigned char *parent_pointer = NULL;
+ unsigned char *child_pointer = NULL;
cJSON *parent = NULL;
- cJSON *ret = NULL;
+ cJSON *detached_item = NULL;
/* copy path and split it in parent and child */
- parentptr = cJSONUtils_strdup(path);
- if (parentptr == NULL) {
- return NULL;
+ parent_pointer = cJSONUtils_strdup(path);
+ if (parent_pointer == NULL) {
+ goto cleanup;
}
- childptr = (unsigned char*)strrchr((char*)parentptr, '/'); /* last '/' */
- if (childptr == NULL)
+ child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/'); /* last '/' */
+ if (child_pointer == NULL)
{
- free(parentptr);
- return NULL;
+ goto cleanup;
}
/* split strings */
- *childptr++ = '\0';
+ child_pointer[0] = '\0';
+ child_pointer++;
- parent = cJSONUtils_GetPointer(object, (char*)parentptr);
- cJSONUtils_InplaceDecodePointerString(childptr);
+ parent = get_item_from_pointer(object, (char*)parent_pointer, case_sensitive);
+ decode_pointer_inplace(child_pointer);
- if (!parent)
+ if (cJSON_IsArray(parent))
+ {
+ size_t index = 0;
+ if (!decode_array_index_from_pointer(child_pointer, &index))
+ {
+ goto cleanup;
+ }
+ detached_item = detach_item_from_array(parent, index);
+ }
+ else if (cJSON_IsObject(parent))
+ {
+ detached_item = cJSON_DetachItemFromObject(parent, (char*)child_pointer);
+ }
+ else
{
/* Couldn't find object to remove child from. */
- ret = NULL;
+ goto cleanup;
+ }
+
+cleanup:
+ if (parent_pointer != NULL)
+ {
+ cJSON_free(parent_pointer);
+ }
+
+ return detached_item;
+}
+
+/* sort lists using mergesort */
+static cJSON *sort_list(cJSON *list, const cJSON_bool case_sensitive)
+{
+ cJSON *first = list;
+ cJSON *second = list;
+ cJSON *current_item = list;
+ cJSON *result = list;
+ cJSON *result_tail = NULL;
+
+ if ((list == NULL) || (list->next == NULL))
+ {
+ /* One entry is sorted already. */
+ return result;
+ }
+
+ while ((current_item != NULL) && (current_item->next != NULL) && (compare_strings((unsigned char*)current_item->string, (unsigned char*)current_item->next->string, case_sensitive) < 0))
+ {
+ /* Test for list sorted. */
+ current_item = current_item->next;
}
- else if ((parent->type & 0xFF) == cJSON_Array)
+ if ((current_item == NULL) || (current_item->next == NULL))
{
- ret = cJSON_DetachItemFromArray(parent, atoi((char*)childptr));
+ /* Leave sorted lists unmodified. */
+ return result;
}
- else if ((parent->type & 0xFF) == cJSON_Object)
+
+ /* reset pointer to the beginning */
+ current_item = list;
+ while (current_item != NULL)
{
- ret = cJSON_DetachItemFromObject(parent, (char*)childptr);
+ /* Walk two pointers to find the middle. */
+ second = second->next;
+ current_item = current_item->next;
+ /* advances current_item two steps at a time */
+ if (current_item != NULL)
+ {
+ current_item = current_item->next;
+ }
+ }
+ if ((second != NULL) && (second->prev != NULL))
+ {
+ /* Split the lists */
+ second->prev->next = NULL;
+ second->prev = NULL;
}
- free(parentptr);
- /* return the detachted item */
- return ret;
+ /* Recursively sort the sub-lists. */
+ first = sort_list(first, case_sensitive);
+ second = sort_list(second, case_sensitive);
+ result = NULL;
+
+ /* Merge the sub-lists */
+ while ((first != NULL) && (second != NULL))
+ {
+ cJSON *smaller = NULL;
+ if (compare_strings((unsigned char*)first->string, (unsigned char*)second->string, case_sensitive) < 0)
+ {
+ smaller = first;
+ }
+ else
+ {
+ smaller = second;
+ }
+
+ if (result == NULL)
+ {
+ /* start merged list with the smaller element */
+ result_tail = smaller;
+ result = smaller;
+ }
+ else
+ {
+ /* add smaller element to the list */
+ result_tail->next = smaller;
+ smaller->prev = result_tail;
+ result_tail = smaller;
+ }
+
+ if (first == smaller)
+ {
+ first = first->next;
+ }
+ else
+ {
+ second = second->next;
+ }
+ }
+
+ if (first != NULL)
+ {
+ /* Append rest of first list. */
+ if (result == NULL)
+ {
+ return first;
+ }
+ result_tail->next = first;
+ first->prev = result_tail;
+ }
+ if (second != NULL)
+ {
+ /* Append rest of second list */
+ if (result == NULL)
+ {
+ return second;
+ }
+ result_tail->next = second;
+ second->prev = result_tail;
+ }
+
+ return result;
+}
+
+static void sort_object(cJSON * const object, const cJSON_bool case_sensitive)
+{
+ if (object == NULL)
+ {
+ return;
+ }
+ object->child = sort_list(object->child, case_sensitive);
}
-static int cJSONUtils_Compare(cJSON *a, cJSON *b)
+static cJSON_bool compare_json(cJSON *a, cJSON *b, const cJSON_bool case_sensitive)
{
if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF)))
{
/* mismatched type. */
- return -1;
+ return false;
}
switch (a->type & 0xFF)
{
case cJSON_Number:
/* numeric mismatch. */
- return ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble)) ? -2 : 0;
+ if ((a->valueint != b->valueint) || (a->valuedouble != b->valuedouble))
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+
case cJSON_String:
/* string mismatch. */
- return (strcmp(a->valuestring, b->valuestring) != 0) ? -3 : 0;
+ if (strcmp(a->valuestring, b->valuestring) != 0)
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+
case cJSON_Array:
- for (a = a->child, b = b->child; a && b; a = a->next, b = b->next)
+ for ((void)(a = a->child), b = b->child; (a != NULL) && (b != NULL); (void)(a = a->next), b = b->next)
{
- int err = cJSONUtils_Compare(a, b);
- if (err)
+ cJSON_bool identical = compare_json(a, b, case_sensitive);
+ if (!identical)
{
- return err;
+ return false;
}
}
+
/* array size mismatch? (one of both children is not NULL) */
- return (a || b) ? -4 : 0;
+ if ((a != NULL) || (b != NULL))
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
+
case cJSON_Object:
- cJSONUtils_SortObject(a);
- cJSONUtils_SortObject(b);
- a = a->child;
- b = b->child;
- while (a && b)
+ sort_object(a, case_sensitive);
+ sort_object(b, case_sensitive);
+ for ((void)(a = a->child), b = b->child; (a != NULL) && (b != NULL); (void)(a = a->next), b = b->next)
{
- int err = 0;
+ cJSON_bool identical = false;
/* compare object keys */
- if (cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string))
+ if (compare_strings((unsigned char*)a->string, (unsigned char*)b->string, case_sensitive))
{
/* missing member */
- return -6;
+ return false;
}
- err = cJSONUtils_Compare(a, b);
- if (err)
+ identical = compare_json(a, b, case_sensitive);
+ if (!identical)
{
- return err;
+ return false;
}
- a = a->next;
- b = b->next;
}
+
/* object length mismatch (one of both children is not null) */
- return (a || b) ? -5 : 0;
+ if ((a != NULL) || (b != NULL))
+ {
+ return false;
+ }
+ else
+ {
+ return true;
+ }
default:
break;
}
+
/* null, true or false */
- return 0;
+ return true;
}
-static int cJSONUtils_ApplyPatch(cJSON *object, cJSON *patch)
+/* non broken version of cJSON_InsertItemInArray */
+static cJSON_bool insert_item_in_array(cJSON *array, size_t which, cJSON *newitem)
{
- cJSON *op = NULL;
- cJSON *path = NULL;
- cJSON *value = NULL;
- cJSON *parent = NULL;
- int opcode = 0;
- unsigned char *parentptr = NULL;
- unsigned char *childptr = NULL;
+ cJSON *child = array->child;
+ while (child && (which > 0))
+ {
+ child = child->next;
+ which--;
+ }
+ if (which > 0)
+ {
+ /* item is after the end of the array */
+ return 0;
+ }
+ if (child == NULL)
+ {
+ cJSON_AddItemToArray(array, newitem);
+ return 1;
+ }
+
+ /* insert into the linked list */
+ newitem->next = child;
+ newitem->prev = child->prev;
+ child->prev = newitem;
- op = cJSON_GetObjectItem(patch, "op");
- path = cJSON_GetObjectItem(patch, "path");
- if (!op || !path)
+ /* was it at the beginning */
+ if (child == array->child)
{
- /* malformed patch. */
- return 2;
+ array->child = newitem;
+ }
+ else
+ {
+ newitem->prev->next = newitem;
+ }
+
+ return 1;
+}
+
+static cJSON *get_object_item(const cJSON * const object, const char* name, const cJSON_bool case_sensitive)
+{
+ if (case_sensitive)
+ {
+ return cJSON_GetObjectItemCaseSensitive(object, name);
}
- /* decode operation */
- if (!strcmp(op->valuestring, "add"))
+ return cJSON_GetObjectItem(object, name);
+}
+
+enum patch_operation { INVALID, ADD, REMOVE, REPLACE, MOVE, COPY, TEST };
+
+static enum patch_operation decode_patch_operation(const cJSON * const patch, const cJSON_bool case_sensitive)
+{
+ cJSON *operation = get_object_item(patch, "op", case_sensitive);
+ if (!cJSON_IsString(operation))
{
- opcode = 0;
+ return INVALID;
}
- else if (!strcmp(op->valuestring, "remove"))
+
+ if (strcmp(operation->valuestring, "add") == 0)
{
- opcode = 1;
+ return ADD;
}
- else if (!strcmp(op->valuestring, "replace"))
+
+ if (strcmp(operation->valuestring, "remove") == 0)
{
- opcode = 2;
+ return REMOVE;
}
- else if (!strcmp(op->valuestring, "move"))
+
+ if (strcmp(operation->valuestring, "replace") == 0)
{
- opcode = 3;
+ return REPLACE;
}
- else if (!strcmp(op->valuestring, "copy"))
+
+ if (strcmp(operation->valuestring, "move") == 0)
{
- opcode = 4;
+ return MOVE;
}
- else if (!strcmp(op->valuestring, "test"))
+
+ if (strcmp(operation->valuestring, "copy") == 0)
+ {
+ return COPY;
+ }
+
+ if (strcmp(operation->valuestring, "test") == 0)
+ {
+ return TEST;
+ }
+
+ return INVALID;
+}
+
+/* overwrite and existing item with another one and free resources on the way */
+static void overwrite_item(cJSON * const root, const cJSON replacement)
+{
+ if (root == NULL)
+ {
+ return;
+ }
+
+ if (root->string != NULL)
+ {
+ cJSON_free(root->string);
+ }
+ if (root->valuestring != NULL)
+ {
+ cJSON_free(root->valuestring);
+ }
+ if (root->child != NULL)
+ {
+ cJSON_Delete(root->child);
+ }
+
+ memcpy(root, &replacement, sizeof(cJSON));
+}
+
+static int apply_patch(cJSON *object, const cJSON *patch, const cJSON_bool case_sensitive)
+{
+ cJSON *path = NULL;
+ cJSON *value = NULL;
+ cJSON *parent = NULL;
+ enum patch_operation opcode = INVALID;
+ unsigned char *parent_pointer = NULL;
+ unsigned char *child_pointer = NULL;
+ int status = 0;
+
+ path = get_object_item(patch, "path", case_sensitive);
+ if (!cJSON_IsString(path))
+ {
+ /* malformed patch. */
+ status = 2;
+ goto cleanup;
+ }
+
+ opcode = decode_patch_operation(patch, case_sensitive);
+ if (opcode == INVALID)
+ {
+ status = 3;
+ goto cleanup;
+ }
+ else if (opcode == TEST)
{
/* compare value: {...} with the given path */
- return cJSONUtils_Compare(cJSONUtils_GetPointer(object, path->valuestring), cJSON_GetObjectItem(patch, "value"));
+ status = !compare_json(get_item_from_pointer(object, path->valuestring, case_sensitive), get_object_item(patch, "value", case_sensitive), case_sensitive);
+ goto cleanup;
}
- else
+
+ /* special case for replacing the root */
+ if (path->valuestring[0] == '\0')
{
- /* unknown opcode. */
- return 3;
+ if (opcode == REMOVE)
+ {
+ static const cJSON invalid = { NULL, NULL, NULL, cJSON_Invalid, NULL, 0, 0, NULL};
+
+ overwrite_item(object, invalid);
+
+ status = 0;
+ goto cleanup;
+ }
+
+ if ((opcode == REPLACE) || (opcode == ADD))
+ {
+ value = get_object_item(patch, "value", case_sensitive);
+ if (value == NULL)
+ {
+ /* missing "value" for add/replace. */
+ status = 7;
+ goto cleanup;
+ }
+
+ value = cJSON_Duplicate(value, 1);
+ if (value == NULL)
+ {
+ /* out of memory for add/replace. */
+ status = 8;
+ goto cleanup;
+ }
+
+ overwrite_item(object, *value);
+
+ /* delete the duplicated value */
+ cJSON_free(value);
+ value = NULL;
+
+ /* the string "value" isn't needed */
+ if (object->string != NULL)
+ {
+ cJSON_free(object->string);
+ object->string = NULL;
+ }
+
+ status = 0;
+ goto cleanup;
+ }
}
- /* Remove/Replace */
- if ((opcode == 1) || (opcode == 2))
+ if ((opcode == REMOVE) || (opcode == REPLACE))
{
/* Get rid of old. */
- cJSON_Delete(cJSONUtils_PatchDetach(object, (unsigned char*)path->valuestring));
- if (opcode == 1)
+ cJSON *old_item = detach_path(object, (unsigned char*)path->valuestring, case_sensitive);
+ if (old_item == NULL)
{
- /* For Remove, this is job done. */
- return 0;
+ status = 13;
+ goto cleanup;
+ }
+ cJSON_Delete(old_item);
+ if (opcode == REMOVE)
+ {
+ /* For Remove, this job is done. */
+ status = 0;
+ goto cleanup;
}
}
/* Copy/Move uses "from". */
- if ((opcode == 3) || (opcode == 4))
+ if ((opcode == MOVE) || (opcode == COPY))
{
- cJSON *from = cJSON_GetObjectItem(patch, "from");
- if (!from)
+ cJSON *from = get_object_item(patch, "from", case_sensitive);
+ if (from == NULL)
{
/* missing "from" for copy/move. */
- return 4;
+ status = 4;
+ goto cleanup;
}
- if (opcode == 3)
+ if (opcode == MOVE)
{
- /* move */
- value = cJSONUtils_PatchDetach(object, (unsigned char*)from->valuestring);
+ value = detach_path(object, (unsigned char*)from->valuestring, case_sensitive);
}
- if (opcode == 4)
+ if (opcode == COPY)
{
- /* copy */
- value = cJSONUtils_GetPointer(object, from->valuestring);
+ value = get_item_from_pointer(object, from->valuestring, case_sensitive);
}
- if (!value)
+ if (value == NULL)
{
/* missing "from" for copy/move. */
- return 5;
+ status = 5;
+ goto cleanup;
}
- if (opcode == 4)
+ if (opcode == COPY)
{
value = cJSON_Duplicate(value, 1);
}
- if (!value)
+ if (value == NULL)
{
/* out of memory for copy/move. */
- return 6;
+ status = 6;
+ goto cleanup;
}
}
else /* Add/Replace uses "value". */
{
- value = cJSON_GetObjectItem(patch, "value");
- if (!value)
+ value = get_object_item(patch, "value", case_sensitive);
+ if (value == NULL)
{
/* missing "value" for add/replace. */
- return 7;
+ status = 7;
+ goto cleanup;
}
value = cJSON_Duplicate(value, 1);
- if (!value)
+ if (value == NULL)
{
/* out of memory for add/replace. */
- return 8;
+ status = 8;
+ goto cleanup;
}
}
/* Now, just add "value" to "path". */
/* split pointer in parent and child */
- parentptr = cJSONUtils_strdup((unsigned char*)path->valuestring);
- childptr = (unsigned char*)strrchr((char*)parentptr, '/');
- if (childptr)
+ parent_pointer = cJSONUtils_strdup((unsigned char*)path->valuestring);
+ child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/');
+ if (child_pointer != NULL)
{
- *childptr++ = '\0';
+ child_pointer[0] = '\0';
+ child_pointer++;
}
- parent = cJSONUtils_GetPointer(object, (char*)parentptr);
- cJSONUtils_InplaceDecodePointerString(childptr);
+ parent = get_item_from_pointer(object, (char*)parent_pointer, case_sensitive);
+ decode_pointer_inplace(child_pointer);
/* add, remove, replace, move, copy, test. */
- if (!parent)
+ if ((parent == NULL) || (child_pointer == NULL))
{
/* Couldn't find object to add to. */
- free(parentptr);
- cJSON_Delete(value);
- return 9;
+ status = 9;
+ goto cleanup;
}
- else if ((parent->type & 0xFF) == cJSON_Array)
+ else if (cJSON_IsArray(parent))
{
- if (!strcmp((char*)childptr, "-"))
+ if (strcmp((char*)child_pointer, "-") == 0)
{
cJSON_AddItemToArray(parent, value);
+ value = NULL;
+ }
+ else
+ {
+ size_t index = 0;
+ if (!decode_array_index_from_pointer(child_pointer, &index))
+ {
+ status = 11;
+ goto cleanup;
+ }
+
+ if (!insert_item_in_array(parent, index, value))
+ {
+ status = 10;
+ goto cleanup;
+ }
+ value = NULL;
+ }
+ }
+ else if (cJSON_IsObject(parent))
+ {
+ if (case_sensitive)
+ {
+ cJSON_DeleteItemFromObjectCaseSensitive(parent, (char*)child_pointer);
}
else
{
- cJSON_InsertItemInArray(parent, atoi((char*)childptr), value);
+ cJSON_DeleteItemFromObject(parent, (char*)child_pointer);
}
+ cJSON_AddItemToObject(parent, (char*)child_pointer, value);
+ value = NULL;
}
- else if ((parent->type & 0xFF) == cJSON_Object)
+ else /* parent is not an object */
{
- cJSON_DeleteItemFromObject(parent, (char*)childptr);
- cJSON_AddItemToObject(parent, (char*)childptr, value);
+ /* Couldn't find object to add to. */
+ status = 9;
+ goto cleanup;
}
- else
+
+cleanup:
+ if (value != NULL)
{
cJSON_Delete(value);
}
- free(parentptr);
+ if (parent_pointer != NULL)
+ {
+ cJSON_free(parent_pointer);
+ }
- return 0;
+ return status;
}
-CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON *object, cJSON *patches)
+CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * const patches)
{
- int err = 0;
+ const cJSON *current_patch = NULL;
+ int status = 0;
- if (patches == NULL)
+ if (!cJSON_IsArray(patches))
{
+ /* malformed patches. */
return 1;
}
- if ((patches->type & 0xFF) != cJSON_Array)
+ if (patches != NULL)
+ {
+ current_patch = patches->child;
+ }
+
+ while (current_patch != NULL)
+ {
+ status = apply_patch(object, current_patch, false);
+ if (status != 0)
+ {
+ return status;
+ }
+ current_patch = current_patch->next;
+ }
+
+ return 0;
+}
+
+CJSON_PUBLIC(int) cJSONUtils_ApplyPatchesCaseSensitive(cJSON * const object, const cJSON * const patches)
+{
+ const cJSON *current_patch = NULL;
+ int status = 0;
+
+ if (!cJSON_IsArray(patches))
{
/* malformed patches. */
return 1;
}
- patches = patches->child;
+ if (patches != NULL)
+ {
+ current_patch = patches->child;
+ }
- while (patches)
+ while (current_patch != NULL)
{
- if ((err = cJSONUtils_ApplyPatch(object, patches)))
+ status = apply_patch(object, current_patch, true);
+ if (status != 0)
{
- return err;
+ return status;
}
- patches = patches->next;
+ current_patch = current_patch->next;
}
return 0;
}
-static void cJSONUtils_GeneratePatch(cJSON *patches, const unsigned char *op, const unsigned char *path, const unsigned char *suffix, cJSON *val)
+static void compose_patch(cJSON * const patches, const unsigned char * const operation, const unsigned char * const path, const unsigned char *suffix, const cJSON * const value)
{
- cJSON *patch = cJSON_CreateObject();
- cJSON_AddItemToObject(patch, "op", cJSON_CreateString((const char*)op));
- if (suffix)
+ cJSON *patch = NULL;
+
+ if ((patches == NULL) || (operation == NULL) || (path == NULL))
+ {
+ return;
+ }
+
+ patch = cJSON_CreateObject();
+ if (patch == NULL)
{
- unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen(suffix) + 2);
- assert(newpath);
- cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", (const char*)path), suffix);
- cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)newpath));
- free(newpath);
+ return;
}
- else
+ cJSON_AddItemToObject(patch, "op", cJSON_CreateString((const char*)operation));
+
+ if (suffix == NULL)
{
cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)path));
}
- if (val)
+ else
{
- cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(val, 1));
+ size_t suffix_length = pointer_encoded_length(suffix);
+ size_t path_length = strlen((const char*)path);
+ unsigned char *full_path = (unsigned char*)cJSON_malloc(path_length + suffix_length + sizeof("/"));
+
+ sprintf((char*)full_path, "%s/", (const char*)path);
+ encode_string_as_pointer(full_path + path_length + 1, suffix);
+
+ cJSON_AddItemToObject(patch, "path", cJSON_CreateString((const char*)full_path));
+ cJSON_free(full_path);
+ }
+
+ if (value != NULL)
+ {
+ cJSON_AddItemToObject(patch, "value", cJSON_Duplicate(value, 1));
}
cJSON_AddItemToArray(patches, patch);
}
-CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON *array, const char *op, const char *path, cJSON *val)
+CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON * const array, const char * const operation, const char * const path, const cJSON * const value)
{
- cJSONUtils_GeneratePatch(array, (const unsigned char*)op, (const unsigned char*)path, 0, val);
+ compose_patch(array, (const unsigned char*)operation, (const unsigned char*)path, NULL, value);
}
-static void cJSONUtils_CompareToPatch(cJSON *patches, const unsigned char *path, cJSON *from, cJSON *to)
+static void create_patches(cJSON * const patches, const unsigned char * const path, cJSON * const from, cJSON * const to, const cJSON_bool case_sensitive)
{
if ((from == NULL) || (to == NULL))
{
@@ -568,104 +1125,127 @@ static void cJSONUtils_CompareToPatch(cJSON *patches, const unsigned char *path,
if ((from->type & 0xFF) != (to->type & 0xFF))
{
- cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
+ compose_patch(patches, (const unsigned char*)"replace", path, 0, to);
return;
}
- switch ((from->type & 0xFF))
+ switch (from->type & 0xFF)
{
case cJSON_Number:
if ((from->valueint != to->valueint) || (from->valuedouble != to->valuedouble))
{
- cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
+ compose_patch(patches, (const unsigned char*)"replace", path, NULL, to);
}
return;
case cJSON_String:
if (strcmp(from->valuestring, to->valuestring) != 0)
{
- cJSONUtils_GeneratePatch(patches, (const unsigned char*)"replace", path, 0, to);
+ compose_patch(patches, (const unsigned char*)"replace", path, NULL, to);
}
return;
case cJSON_Array:
{
- size_t c = 0;
- unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + 23); /* Allow space for 64bit int. */
- assert(newpath);
- /* generate patches for all array elements that exist in "from" and "to" */
- for (c = 0, from = from->child, to = to->child; from && to; from = from->next, to = to->next, c++)
+ size_t index = 0;
+ cJSON *from_child = from->child;
+ cJSON *to_child = to->child;
+ unsigned char *new_path = (unsigned char*)cJSON_malloc(strlen((const char*)path) + 20 + sizeof("/")); /* Allow space for 64bit int. log10(2^64) = 20 */
+
+ /* generate patches for all array elements that exist in both "from" and "to" */
+ for (index = 0; (from_child != NULL) && (to_child != NULL); (void)(from_child = from_child->next), (void)(to_child = to_child->next), index++)
{
/* check if conversion to unsigned long is valid
* This should be eliminated at compile time by dead code elimination
* if size_t is an alias of unsigned long, or if it is bigger */
- if (c > ULONG_MAX)
+ if (index > ULONG_MAX)
{
- free(newpath);
+ cJSON_free(new_path);
return;
}
- sprintf((char*)newpath, "%s/%lu", path, (unsigned long)c); /* path of the current array element */
- cJSONUtils_CompareToPatch(patches, newpath, from, to);
+ sprintf((char*)new_path, "%s/%lu", path, (unsigned long)index); /* path of the current array element */
+ create_patches(patches, new_path, from_child, to_child, case_sensitive);
}
+
/* remove leftover elements from 'from' that are not in 'to' */
- for (; from; from = from->next, c++)
+ for (; (from_child != NULL); (void)(from_child = from_child->next))
{
/* check if conversion to unsigned long is valid
* This should be eliminated at compile time by dead code elimination
* if size_t is an alias of unsigned long, or if it is bigger */
- if (c > ULONG_MAX)
+ if (index > ULONG_MAX)
{
- free(newpath);
+ cJSON_free(new_path);
return;
}
- sprintf((char*)newpath, "%lu", (unsigned long)c);
- cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, newpath, 0);
+ sprintf((char*)new_path, "%lu", (unsigned long)index);
+ compose_patch(patches, (const unsigned char*)"remove", path, new_path, NULL);
}
/* add new elements in 'to' that were not in 'from' */
- for (; to; to = to->next, c++)
+ for (; (to_child != NULL); (void)(to_child = to_child->next), index++)
{
- cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to);
+ compose_patch(patches, (const unsigned char*)"add", path, (const unsigned char*)"-", to_child);
}
- free(newpath);
+ cJSON_free(new_path);
return;
}
case cJSON_Object:
{
- cJSON *a = NULL;
- cJSON *b = NULL;
- cJSONUtils_SortObject(from);
- cJSONUtils_SortObject(to);
+ cJSON *from_child = NULL;
+ cJSON *to_child = NULL;
+ sort_object(from, case_sensitive);
+ sort_object(to, case_sensitive);
- a = from->child;
- b = to->child;
+ from_child = from->child;
+ to_child = to->child;
/* for all object values in the object with more of them */
- while (a || b)
+ while ((from_child != NULL) || (to_child != NULL))
{
- int diff = (!a) ? 1 : ((!b) ? -1 : cJSONUtils_strcasecmp((unsigned char*)a->string, (unsigned char*)b->string));
- if (!diff)
+ int diff;
+ if (from_child == NULL)
+ {
+ diff = 1;
+ }
+ else if (to_child == NULL)
+ {
+ diff = -1;
+ }
+ else
+ {
+ diff = compare_strings((unsigned char*)from_child->string, (unsigned char*)to_child->string, case_sensitive);
+ }
+
+ if (diff == 0)
{
/* both object keys are the same */
- unsigned char *newpath = (unsigned char*)malloc(strlen((const char*)path) + cJSONUtils_PointerEncodedstrlen((unsigned char*)a->string) + 2);
- assert(newpath);
- cJSONUtils_PointerEncodedstrcpy(newpath + sprintf((char*)newpath, "%s/", path), (unsigned char*)a->string);
+ size_t path_length = strlen((const char*)path);
+ size_t from_child_name_length = pointer_encoded_length((unsigned char*)from_child->string);
+ unsigned char *new_path = (unsigned char*)cJSON_malloc(path_length + from_child_name_length + sizeof("/"));
+
+ sprintf((char*)new_path, "%s/", path);
+ encode_string_as_pointer(new_path + path_length + 1, (unsigned char*)from_child->string);
+
/* create a patch for the element */
- cJSONUtils_CompareToPatch(patches, newpath, a, b);
- free(newpath);
- a = a->next;
- b = b->next;
+ create_patches(patches, new_path, from_child, to_child, case_sensitive);
+ cJSON_free(new_path);
+
+ from_child = from_child->next;
+ to_child = to_child->next;
}
else if (diff < 0)
{
/* object element doesn't exist in 'to' --> remove it */
- cJSONUtils_GeneratePatch(patches, (const unsigned char*)"remove", path, (unsigned char*)a->string, 0);
- a = a->next;
+ compose_patch(patches, (const unsigned char*)"remove", path, (unsigned char*)from_child->string, NULL);
+
+ from_child = from_child->next;
}
else
{
/* object element doesn't exist in 'from' --> add it */
- cJSONUtils_GeneratePatch(patches, (const unsigned char*)"add", path, (unsigned char*)b->string, b);
- b = b->next;
+ compose_patch(patches, (const unsigned char*)"add", path, (unsigned char*)to_child->string, to_child);
+
+ to_child = to_child->next;
}
}
return;
@@ -676,211 +1256,199 @@ static void cJSONUtils_CompareToPatch(cJSON *patches, const unsigned char *path,
}
}
-CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON *from, cJSON *to)
+CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON * const from, cJSON * const to)
{
- cJSON *patches = cJSON_CreateArray();
- cJSONUtils_CompareToPatch(patches, (const unsigned char*)"", from, to);
+ cJSON *patches = NULL;
+
+ if ((from == NULL) || (to == NULL))
+ {
+ return NULL;
+ }
+
+ patches = cJSON_CreateArray();
+ create_patches(patches, (const unsigned char*)"", from, to, false);
return patches;
}
-/* sort lists using mergesort */
-static cJSON *cJSONUtils_SortList(cJSON *list)
+CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatchesCaseSensitive(cJSON * const from, cJSON * const to)
{
- cJSON *first = list;
- cJSON *second = list;
- cJSON *ptr = list;
+ cJSON *patches = NULL;
- if (!list || !list->next)
+ if ((from == NULL) || (to == NULL))
{
- /* One entry is sorted already. */
- return list;
+ return NULL;
}
- while (ptr && ptr->next && (cJSONUtils_strcasecmp((unsigned char*)ptr->string, (unsigned char*)ptr->next->string) < 0))
- {
- /* Test for list sorted. */
- ptr = ptr->next;
- }
- if (!ptr || !ptr->next)
- {
- /* Leave sorted lists unmodified. */
- return list;
- }
+ patches = cJSON_CreateArray();
+ create_patches(patches, (const unsigned char*)"", from, to, true);
- /* reset ptr to the beginning */
- ptr = list;
- while (ptr)
+ return patches;
+}
+
+CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON * const object)
+{
+ sort_object(object, false);
+}
+
+CJSON_PUBLIC(void) cJSONUtils_SortObjectCaseSensitive(cJSON * const object)
+{
+ sort_object(object, true);
+}
+
+static cJSON *merge_patch(cJSON *target, const cJSON * const patch, const cJSON_bool case_sensitive)
+{
+ cJSON *patch_child = NULL;
+
+ if (!cJSON_IsObject(patch))
{
- /* Walk two pointers to find the middle. */
- second = second->next;
- ptr = ptr->next;
- /* advances ptr two steps at a time */
- if (ptr)
- {
- ptr = ptr->next;
- }
+ /* scalar value, array or NULL, just duplicate */
+ cJSON_Delete(target);
+ return cJSON_Duplicate(patch, 1);
}
- if (second && second->prev)
+
+ if (!cJSON_IsObject(target))
{
- /* Split the lists */
- second->prev->next = NULL;
+ cJSON_Delete(target);
+ target = cJSON_CreateObject();
}
- /* Recursively sort the sub-lists. */
- first = cJSONUtils_SortList(first);
- second = cJSONUtils_SortList(second);
- list = ptr = NULL;
-
- while (first && second) /* Merge the sub-lists */
+ patch_child = patch->child;
+ while (patch_child != NULL)
{
- if (cJSONUtils_strcasecmp((unsigned char*)first->string, (unsigned char*)second->string) < 0)
+ if (cJSON_IsNull(patch_child))
{
- if (!list)
+ /* NULL is the indicator to remove a value, see RFC7396 */
+ if (case_sensitive)
{
- /* start merged list with the first element of the first list */
- list = ptr = first;
+ cJSON_DeleteItemFromObjectCaseSensitive(target, patch_child->string);
}
else
{
- /* add first element of first list to merged list */
- ptr->next = first;
- first->prev = ptr;
- ptr = first;
+ cJSON_DeleteItemFromObject(target, patch_child->string);
}
- first = first->next;
}
else
{
- if (!list)
+ cJSON *replace_me = NULL;
+ cJSON *replacement = NULL;
+
+ if (case_sensitive)
{
- /* start merged list with the first element of the second list */
- list = ptr = second;
+ replace_me = cJSON_DetachItemFromObjectCaseSensitive(target, patch_child->string);
}
else
{
- /* add first element of second list to merged list */
- ptr->next = second;
- second->prev = ptr;
- ptr = second;
+ replace_me = cJSON_DetachItemFromObject(target, patch_child->string);
}
- second = second->next;
- }
- }
- if (first)
- {
- /* Append rest of first list. */
- if (!list)
- {
- return first;
- }
- ptr->next = first;
- first->prev = ptr;
- }
- if (second)
- {
- /* Append rest of second list */
- if (!list)
- {
- return second;
+
+ replacement = merge_patch(replace_me, patch_child, case_sensitive);
+ if (replacement == NULL)
+ {
+ return NULL;
+ }
+
+ cJSON_AddItemToObject(target, patch_child->string, replacement);
}
- ptr->next = second;
- second->prev = ptr;
+ patch_child = patch_child->next;
}
-
- return list;
+ return target;
}
-CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON *object)
+CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch)
{
- object->child = cJSONUtils_SortList(object->child);
+ return merge_patch(target, patch, false);
}
-CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
+CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatchCaseSensitive(cJSON *target, const cJSON * const patch)
{
- if (!patch || ((patch->type & 0xFF) != cJSON_Object))
- {
- /* scalar value, array or NULL, just duplicate */
- cJSON_Delete(target);
- return cJSON_Duplicate(patch, 1);
- }
-
- if (!target || ((target->type & 0xFF) != cJSON_Object))
- {
- cJSON_Delete(target);
- target = cJSON_CreateObject();
- }
-
- patch = patch->child;
- while (patch)
- {
- if ((patch->type & 0xFF) == cJSON_NULL)
- {
- /* NULL is the indicator to remove a value, see RFC7396 */
- cJSON_DeleteItemFromObject(target, patch->string);
- }
- else
- {
- cJSON *replaceme = cJSON_DetachItemFromObject(target, patch->string);
- cJSON_AddItemToObject(target, patch->string, cJSONUtils_MergePatch(replaceme, patch));
- }
- patch = patch->next;
- }
- return target;
+ return merge_patch(target, patch, true);
}
-CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON *from, cJSON *to)
+static cJSON *generate_merge_patch(cJSON * const from, cJSON * const to, const cJSON_bool case_sensitive)
{
+ cJSON *from_child = NULL;
+ cJSON *to_child = NULL;
cJSON *patch = NULL;
- if (!to)
+ if (to == NULL)
{
/* patch to delete everything */
return cJSON_CreateNull();
}
- if (((to->type & 0xFF) != cJSON_Object) || !from || ((from->type & 0xFF) != cJSON_Object))
+ if (!cJSON_IsObject(to) || !cJSON_IsObject(from))
{
return cJSON_Duplicate(to, 1);
}
- cJSONUtils_SortObject(from);
- cJSONUtils_SortObject(to);
+ sort_object(from, case_sensitive);
+ sort_object(to, case_sensitive);
- from = from->child;
- to = to->child;
+ from_child = from->child;
+ to_child = to->child;
patch = cJSON_CreateObject();
- while (from || to)
+ while (from_child || to_child)
{
- int compare = from ? (to ? strcmp(from->string, to->string) : -1) : 1;
- if (compare < 0)
+ int diff;
+ if (from_child != NULL)
+ {
+ if (to_child != NULL)
+ {
+ diff = strcmp(from_child->string, to_child->string);
+ }
+ else
+ {
+ diff = -1;
+ }
+ }
+ else
+ {
+ diff = 1;
+ }
+
+ if (diff < 0)
{
/* from has a value that to doesn't have -> remove */
- cJSON_AddItemToObject(patch, from->string, cJSON_CreateNull());
- from = from->next;
+ cJSON_AddItemToObject(patch, from_child->string, cJSON_CreateNull());
+
+ from_child = from_child->next;
}
- else if (compare > 0)
+ else if (diff > 0)
{
/* to has a value that from doesn't have -> add to patch */
- cJSON_AddItemToObject(patch, to->string, cJSON_Duplicate(to, 1));
- to = to->next;
+ cJSON_AddItemToObject(patch, to_child->string, cJSON_Duplicate(to_child, 1));
+
+ to_child = to_child->next;
}
else
{
/* object key exists in both objects */
- if (cJSONUtils_Compare(from, to))
+ if (!compare_json(from_child, to_child, case_sensitive))
{
/* not identical --> generate a patch */
- cJSON_AddItemToObject(patch, to->string, cJSONUtils_GenerateMergePatch(from, to));
+ cJSON_AddItemToObject(patch, to_child->string, cJSONUtils_GenerateMergePatch(from_child, to_child));
}
+
/* next key in the object */
- from = from->next;
- to = to->next;
+ from_child = from_child->next;
+ to_child = to_child->next;
}
}
- if (!patch->child)
+ if (patch->child == NULL)
{
+ /* no patch generated */
cJSON_Delete(patch);
return NULL;
}
return patch;
}
+
+CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON * const from, cJSON * const to)
+{
+ return generate_merge_patch(from, to, false);
+}
+
+CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatchCaseSensitive(cJSON * const from, cJSON * const to)
+{
+ return generate_merge_patch(from, to, true);
+}
diff --git a/libs/esl/src/include/esl_cJSON.h b/libs/esl/src/include/esl_cJSON.h
index 79dbb98157a..514251c9b24 100644
--- a/libs/esl/src/include/esl_cJSON.h
+++ b/libs/esl/src/include/esl_cJSON.h
@@ -1,16 +1,13 @@
/*
- Copyright (c) 2009 Dave Gamble
-
+ Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
-
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -28,10 +25,55 @@ extern "C"
{
#endif
+#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
+#define __WINDOWS__
+#endif
+
+#ifdef __WINDOWS__
+
+/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options:
+CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
+CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
+CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
+For *nix builds that support visibility attribute, you can define similar behavior by
+setting default visibility to hidden by adding
+-fvisibility=hidden (for gcc)
+or
+-xldscope=hidden (for sun cc)
+to CFLAGS
+then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
+*/
+
+#define CJSON_CDECL __cdecl
+#define CJSON_STDCALL __stdcall
+
+/* export symbols by default, this is necessary for copy pasting the C and header file */
+#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
+#define CJSON_EXPORT_SYMBOLS
+#endif
+
+#if defined(CJSON_HIDE_SYMBOLS)
+#define CJSON_PUBLIC(type) type CJSON_STDCALL
+#elif defined(CJSON_EXPORT_SYMBOLS)
+#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL
+#elif defined(CJSON_IMPORT_SYMBOLS)
+#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL
+#endif
+#else /* !__WINDOWS__ */
+#define CJSON_CDECL
+#define CJSON_STDCALL
+
+#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
+#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
+#else
+#define CJSON_PUBLIC(type) type
+#endif
+#endif
+
/* project version */
#define CJSON_VERSION_MAJOR 1
-#define CJSON_VERSION_MINOR 3
-#define CJSON_VERSION_PATCH 0
+#define CJSON_VERSION_MINOR 7
+#define CJSON_VERSION_PATCH 12
#include
@@ -63,7 +105,7 @@ typedef struct cJSON
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
char *valuestring;
- /* The item's number, if type==cJSON_Number */
+ /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
int valueint;
/* The item's number, if type==cJSON_Number */
double valuedouble;
@@ -74,45 +116,17 @@ typedef struct cJSON
typedef struct cJSON_Hooks
{
- void *(*malloc_fn)(size_t sz);
- void (*free_fn)(void *ptr);
+ /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */
+ void *(CJSON_CDECL *malloc_fn)(size_t sz);
+ void (CJSON_CDECL *free_fn)(void *ptr);
} cJSON_Hooks;
-#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
-#define __WINDOWS__
-#endif
-#ifdef __WINDOWS__
-
-/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 2 define options:
-
-CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
-CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols
-
-For *nix builds that support visibility attribute, you can define similar behavior by
-
-setting default visibility to hidden by adding
--fvisibility=hidden (for gcc)
-or
--xldscope=hidden (for sun cc)
-to CFLAGS
-
-then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
-
-*/
+typedef int cJSON_bool;
-#if defined(CJSON_HIDE_SYMBOLS)
-#define CJSON_PUBLIC(type) type __stdcall
-#elif defined(CJSON_EXPORT_SYMBOLS)
-#define CJSON_PUBLIC(type) __declspec(dllexport) type __stdcall
-#else
-#define CJSON_PUBLIC(type) __declspec(dllimport) type __stdcall
-#endif
-#else /* !WIN32 */
-#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
-#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
-#else
-#define CJSON_PUBLIC(type) type
-#endif
+/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
+ * This is to prevent stack overflows. */
+#ifndef CJSON_NESTING_LIMIT
+#define CJSON_NESTING_LIMIT 1000
#endif
/* returns the version of cJSON as a string */
@@ -121,36 +135,56 @@ CJSON_PUBLIC(const char*) cJSON_Version(void);
/* Supply malloc, realloc and free functions to cJSON */
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
-
-/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
+/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
+/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
-/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
+/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
+/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
+CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
+
+/* Render a cJSON entity to text for transfer/storage. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
-/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
+/* Render a cJSON entity to text for transfer/storage without any formatting. */
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
-CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, int fmt);
-/* Render a cJSON entity to text using a buffer already allocated in memory with length buf_len. Returns 1 on success and 0 on failure. */
-CJSON_PUBLIC(int) cJSON_PrintPreallocated(cJSON *item, char *buf, const int len, const int fmt);
+CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
+/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
+/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
+CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
/* Delete a cJSON entity and all subentities. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
/* Returns the number of items in an array (or object). */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
-/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
-CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item);
+/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
+CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
/* Get item "string" from object. Case insensitive. */
-CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *object, const char *string);
-CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON *object, const char *string);
-CJSON_PUBLIC(int) cJSON_HasObjectItem(const cJSON *object, const char *string);
+CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
+CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
+CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
+/* Check if the item is a string and return its valuestring */
+CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);
+
+/* These functions check the type of an item */
+CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
+CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
+CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
+CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
+CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
+CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
+CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
+CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
+CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
+CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
+
/* These calls create a cJSON item of the appropriate type. */
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
-CJSON_PUBLIC(cJSON *) cJSON_CreateBool(int b);
+CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
/* raw json */
@@ -158,6 +192,14 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
+/* Create a string where valuestring references a string so
+ * it will not be freed by cJSON_Delete */
+CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
+/* Create an object/arrray that only references it's elements so
+ * they will not be freed by cJSON_Delete */
+CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
+CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
+
/* These utilities create an Array of count items. */
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
@@ -176,36 +218,44 @@ CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
/* Remove/Detatch items from Arrays/Objects. */
+CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
+CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
+CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
/* Update array items. */
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
+CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
+CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
/* Duplicate a cJSON item */
-CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, int recurse);
+CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
need to be released. With recurse!=0, it will duplicate any children connected to the item.
The item->next and ->prev pointers are always zero on return from Duplicate. */
+/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
+ * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
+CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
-/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
-/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error. If not, then cJSON_GetErrorPtr() does the job. */
-CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, int require_null_terminated);
CJSON_PUBLIC(void) cJSON_Minify(char *json);
-/* Macros for creating things quickly. */
-#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
-#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
-#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
-#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
-#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
-#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
-#define cJSON_AddRawToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateRaw(s))
+/* Helper functions for creating and adding items to an object at the same time.
+ * They return the added item or NULL on failure. */
+CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
+CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
+CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
+CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
+CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
+CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
+CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
+CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
+CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
@@ -213,9 +263,13 @@ CJSON_PUBLIC(void) cJSON_Minify(char *json);
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
-/* Macro for iterating over an array */
+/* Macro for iterating over an array or object */
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
+/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
+CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
+CJSON_PUBLIC(void) cJSON_free(void *object);
+
#ifdef __cplusplus
}
#endif
diff --git a/libs/esl/src/include/esl_cJSON_Utils.h b/libs/esl/src/include/esl_cJSON_Utils.h
index 4418c8c5552..95c5a37edd4 100644
--- a/libs/esl/src/include/esl_cJSON_Utils.h
+++ b/libs/esl/src/include/esl_cJSON_Utils.h
@@ -1,14 +1,45 @@
+/*
+ Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+*/
+
+#ifndef cJSON_Utils__h
+#define cJSON_Utils__h
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
#include "esl_cJSON.h"
/* Implement RFC6901 (https://tools.ietf.org/html/rfc6901) JSON Pointer spec. */
-CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON *object, const char *pointer);
+CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointer(cJSON * const object, const char *pointer);
+CJSON_PUBLIC(cJSON *) cJSONUtils_GetPointerCaseSensitive(cJSON * const object, const char *pointer);
/* Implement RFC6902 (https://tools.ietf.org/html/rfc6902) JSON Patch spec. */
-CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON *from, cJSON *to);
+/* NOTE: This modifies objects in 'from' and 'to' by sorting the elements by their key */
+CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatches(cJSON * const from, cJSON * const to);
+CJSON_PUBLIC(cJSON *) cJSONUtils_GeneratePatchesCaseSensitive(cJSON * const from, cJSON * const to);
/* Utility for generating patch array entries. */
-CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON *array, const char *op, const char *path, cJSON *val);
+CJSON_PUBLIC(void) cJSONUtils_AddPatchToArray(cJSON * const array, const char * const operation, const char * const path, const cJSON * const value);
/* Returns 0 for success. */
-CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON *object, cJSON *patches);
+CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON * const object, const cJSON * const patches);
+CJSON_PUBLIC(int) cJSONUtils_ApplyPatchesCaseSensitive(cJSON * const object, const cJSON * const patches);
/*
// Note that ApplyPatches is NOT atomic on failure. To implement an atomic ApplyPatches, use:
@@ -33,12 +64,22 @@ CJSON_PUBLIC(int) cJSONUtils_ApplyPatches(cJSON *object, cJSON *patches);
/* Implement RFC7386 (https://tools.ietf.org/html/rfc7396) JSON Merge Patch spec. */
/* target will be modified by patch. return value is new ptr for target. */
-CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, cJSON *patch);
+CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch);
+CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatchCaseSensitive(cJSON *target, const cJSON * const patch);
/* generates a patch to move from -> to */
-CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON *from, cJSON *to);
+/* NOTE: This modifies objects in 'from' and 'to' by sorting the elements by their key */
+CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON * const from, cJSON * const to);
+CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatchCaseSensitive(cJSON * const from, cJSON * const to);
/* Given a root object and a target object, construct a pointer from one to the other. */
-CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(cJSON *object, cJSON *target);
+CJSON_PUBLIC(char *) cJSONUtils_FindPointerFromObjectTo(const cJSON * const object, const cJSON * const target);
/* Sorts the members of the object into alphabetical order. */
-CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON *object);
+CJSON_PUBLIC(void) cJSONUtils_SortObject(cJSON * const object);
+CJSON_PUBLIC(void) cJSONUtils_SortObjectCaseSensitive(cJSON * const object);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/libs/iksemel/src/ikstack.c b/libs/iksemel/src/ikstack.c
index 9771a41e8b0..b5fd75ef682 100644
--- a/libs/iksemel/src/ikstack.c
+++ b/libs/iksemel/src/ikstack.c
@@ -32,7 +32,7 @@ static ikschunk *
find_space (ikstack *s, ikschunk *c, size_t size)
{
/* FIXME: dont use *2 after over allocated chunks */
- while (1) {
+ while (c) {
if (c->size - c->used >= size) return c;
if (!c->next) {
if ((c->size * 2) > size) size = c->size * 2;
diff --git a/libs/iksemel/src/sax.c b/libs/iksemel/src/sax.c
index 338a5cf7f94..beb3621588d 100644
--- a/libs/iksemel/src/sax.c
+++ b/libs/iksemel/src/sax.c
@@ -213,22 +213,16 @@ sax_core (iksparser *prs, char *buf, int len)
goto cont;
} else {
if (c & 0x80) {
- unsigned char mask;
if ((c & 0x60) == 0x40) {
prs->uni_max = 2;
- mask = 0x1F;
} else if ((c & 0x70) == 0x60) {
prs->uni_max = 3;
- mask = 0x0F;
} else if ((c & 0x78) == 0x70) {
prs->uni_max = 4;
- mask = 0x07;
} else if ((c & 0x7C) == 0x78) {
prs->uni_max = 5;
- mask = 0x03;
} else if ((c & 0x7E) == 0x7C) {
prs->uni_max = 6;
- mask = 0x01;
} else {
return IKS_BADXML;
}
diff --git a/libs/iksemel/test/tst-ikstack.c b/libs/iksemel/test/tst-ikstack.c
index 1a761ad708b..d123e200472 100644
--- a/libs/iksemel/test/tst-ikstack.c
+++ b/libs/iksemel/test/tst-ikstack.c
@@ -36,7 +36,7 @@ test_stack (int cs)
memset (mem, 'x', i);
old = iks_stack_strcat (s, old, 0, buf + i, 1);
}
- if (strcmp (old, buf) != 0) {
+ if (old && strcmp (old, buf) != 0) {
printf ("ikstack strcat bug:\nExpected: %s\n Result: %s\n", buf, old);
exit (1);
}
diff --git a/libs/iksemel/test/tst-sax.c b/libs/iksemel/test/tst-sax.c
index 77a5c13e95d..c0a041aee1c 100644
--- a/libs/iksemel/test/tst-sax.c
+++ b/libs/iksemel/test/tst-sax.c
@@ -160,7 +160,7 @@ tagHook (void *udata, char *name, char **atts, int type)
nr = tester.cur->nr_atts;
while (nr) {
flag = 0;
- for (i = 0;atts[i]; i+= 2) {
+ for (i = 0;atts&&atts[i]; i+= 2) {
if (iks_strcmp (atts[i], tester.cur->atts[nr-1]) == 0 && iks_strcmp (atts[i+1], tester.cur->vals[nr-1]) == 0) {
flag = 1;
break;
diff --git a/libs/iksemel/tools/hash.c b/libs/iksemel/tools/hash.c
index 8ba83a0eeac..c4b1179a873 100644
--- a/libs/iksemel/tools/hash.c
+++ b/libs/iksemel/tools/hash.c
@@ -117,7 +117,7 @@ hash_print (hash *h, char *title_fmt, char *line_fmt)
struct item **tags, *t;
unsigned int i = 0, pos = 0;
- tags = calloc (sizeof (struct tag *), h->count);
+ tags = calloc (sizeof (struct item *), h->count);
for (; i < h->size; i ++) {
for (t = h->table[i]; t; t = t->next) {
diff --git a/libs/iksemel/tools/iksperf.c b/libs/iksemel/tools/iksperf.c
index 81e98ad1c4f..4bcd4b3e57a 100644
--- a/libs/iksemel/tools/iksperf.c
+++ b/libs/iksemel/tools/iksperf.c
@@ -207,7 +207,6 @@ serialize_test (char *buf, int len)
unsigned long time;
iks *x;
iksparser *prs;
- char *xml;
int err;
prs = iks_dom_new (&x);
@@ -228,7 +227,7 @@ serialize_test (char *buf, int len)
t_reset ();
- xml = iks_string (iks_stack (x), x);
+ iks_string (iks_stack (x), x);
time = t_elapsed ();
diff --git a/libs/libdingaling/src/libdingaling.c b/libs/libdingaling/src/libdingaling.c
index 8efe5c4af53..6fbedfda6ed 100644
--- a/libs/libdingaling/src/libdingaling.c
+++ b/libs/libdingaling/src/libdingaling.c
@@ -499,7 +499,10 @@ static ldl_status parse_session_code(ldl_handle_t *handle, char *id, char *from,
unsigned int *candidate_len = NULL;
ldl_candidate_t (*candidates)[LDL_MAX_CANDIDATES] = NULL;
- if ((key = iks_find_attrib(tag, "preference"))) {
+ if (!(key = iks_find_attrib(tag, "preference"))) {
+ globals.logger(DL_LOG_WARNING, "Field preference was not set\n");
+ continue;
+ } else {
unsigned int x;
pref = strtod(key, NULL);
@@ -927,7 +930,7 @@ static int on_disco_default(void *user_data, ikspak *pak)
char *node = NULL;
char *ns = NULL;
ldl_handle_t *handle = user_data;
- iks *iq, *query, *tag;
+ iks *iq = NULL, *query, *tag;
uint8_t send = 0;
int x;
@@ -936,10 +939,10 @@ static int on_disco_default(void *user_data, ikspak *pak)
node = iks_find_attrib(pak->query, "node");
}
- if (pak->subtype == IKS_TYPE_RESULT) {
+ if (pak && pak->subtype == IKS_TYPE_RESULT) {
globals.logger(DL_LOG_CRIT, "FixME!!! node=[%s]\n", node?node:"");
- } else if (pak->subtype == IKS_TYPE_GET) {
- if ((iq = iks_new("iq"))) {
+ } else if (pak && pak->subtype == IKS_TYPE_GET) {
+ if (ns && (iq = iks_new("iq"))) {
int all = 0;
iks_insert_attrib(iq, "from", handle->login);
@@ -1081,7 +1084,6 @@ static int on_presence(void *user_data, ikspak *pak)
if (ext && strstr(ext, "voice-v1") && (buffer = apr_hash_get(handle->probe_hash, id, APR_HASH_KEY_STRING))) {
apr_cpystrn(buffer->buf, from, buffer->len);
buffer->hit = 1;
- done = 1;
}
}
}
@@ -1149,7 +1151,6 @@ static ldl_avatar_t *ldl_get_avatar(ldl_handle_t *handle, char *path, char *from
bytes = read(fd, image, sizeof(image));
close(fd);
- fd = -1;
ap = malloc(sizeof(*ap));
assert(ap != NULL);
@@ -2565,7 +2566,6 @@ unsigned int ldl_session_candidates(ldl_session_t *session,
iq = NULL;
sess = NULL;
tag = NULL;
- x = 0;
id = 0;
}
diff --git a/libs/libnatpmp/natpmp.c b/libs/libnatpmp/natpmp.c
index e3ab64af7dd..0e2c4ea0b49 100644
--- a/libs/libnatpmp/natpmp.c
+++ b/libs/libnatpmp/natpmp.c
@@ -195,7 +195,7 @@ int sendnewportmappingrequest(natpmp_t * p, int protocol,
int readnatpmpresponse(natpmp_t * p, natpmpresp_t * response)
{
unsigned char buf[16];
- struct sockaddr_in addr;
+ struct sockaddr_in addr = {0};
socklen_t addrlen = sizeof(addr);
int n;
if(!p)
diff --git a/libs/libvpx/AUTHORS b/libs/libvpx/AUTHORS
index 04c28724329..2f1f8a6946a 100644
--- a/libs/libvpx/AUTHORS
+++ b/libs/libvpx/AUTHORS
@@ -4,12 +4,13 @@
Aaron Watry
Abo Talib Mahfoodh
Adrian Grange
-Aℓex Converse
Ahmad Sharif
+Aidan Welch
Aleksey Vasenev
Alexander Potapenko
Alexander Voronov
Alexandra Hájková
+Aℓex Converse
Alexis Ballier
Alok Ahuja
Alpha Lam
@@ -26,11 +27,13 @@ Brion Vibber
changjun.yang
Charles 'Buck' Krasic
Cheng Chen
+Chi Yo Tsai
chm
Chris Cunningham
Christian Duvivier
Daniele Castagna
Daniel Kang
+Dan Zhu
Deb Mukherjee
Deepa K G
Dim Temp
@@ -38,11 +41,13 @@ Dmitry Kovalev
Dragan Mrdjan
Ed Baker
Ehsan Akhgari
+Elliott Karpilovsky
Erik Niemeyer
Fabio Pedretti
Frank Galligan
Fredrik Söderquist
Fritz Koenig
+Fyodor Kyslov
Gabriel Marin
Gaute Strokkenes
Geza Lore
@@ -55,7 +60,9 @@ Guillermo Ballester Valor
Hangyu Kuang
Hanno Böck
Han Shen
+Harish Mahendrakar
Henrik Lundin
+Hien Ho
Hui Su
Ivan Krasin
Ivan Maltz
@@ -81,6 +88,7 @@ Johann Koenig
John Koleszar
Johnny Klonaris
John Stark
+Jon Kunkee
Joshua Bleecher Snyder
Joshua Litt
Julia Robson
@@ -91,15 +99,19 @@ KO Myung-Hun
Kyle Siefring
Lawrence Velázquez
Linfeng Zhang
+Liu Peng
Lou Quillio
Luca Barbato
+Luc Trudeau
Makoto Kato
Mans Rullgard
Marco Paniconi
Mark Mentovai
Martin Ettl
-Martin Storsjo
+Martin Storsjö
Matthew Heaney
+Matthias Räncker
+Michael Horowitz
Michael Kohler
Mike Frysinger
Mike Hommey
@@ -107,10 +119,12 @@ Mikhal Shemer
Min Chen
Minghai Shang
Min Ye
+Mirko Bonadei
Moriyoshi Koizumi
Morton Jonuschat
Nathan E. Egge
Nico Weber
+Niveditha Rau
Parag Salasakar
Pascal Massimino
Patrik Westin
@@ -129,9 +143,13 @@ Rafael de Lucena Valle
Rahul Chaudhry
Ralph Giles
Ranjit Kumar Tulabandu
+Raphael Kubo da Costa
+Ravi Chaudhary
+Ritu Baldwa
Rob Bradford
Ronald S. Bultje
Rui Ueyama
+Sai Deng
Sami Pietilä
Sarah Parker
Sasi Inguva
@@ -139,12 +157,15 @@ Scott Graham
Scott LaVarnway
Sean McGovern
Sergey Kolomenkin
+Sergey Silkin
Sergey Ulanov
Shimon Doodkin
Shiyou Yin
+Shubham Tandle
Shunyao Li
Stefan Holmer
Suman Sunkara
+Supradeep T R
Sylvestre Ledru
Taekhyun Kim
Takanori MATSUURA
@@ -157,11 +178,15 @@ Timothy B. Terriberry
Tom Finegan
Tristan Matthews
Urvang Joshi
+Venkatarama NG. Avadhani
Vignesh Venkatasubramanian
Vlad Tsyrklevich
+Wan-Teh Chang
+xiwei gu
Yaowu Xu
Yi Luo
Yongzhe Wang
+Yue Chen
Yunqing Wang
Yury Gitman
Zoe Liu
diff --git a/libs/libvpx/CHANGELOG b/libs/libvpx/CHANGELOG
index 2281394c8ed..a7d8311c5f9 100644
--- a/libs/libvpx/CHANGELOG
+++ b/libs/libvpx/CHANGELOG
@@ -1,4 +1,63 @@
-2017-01-04 v1.7.0 "Mandarin Duck"
+2019-07-15 v1.8.1 "Orpington Duck"
+ This release collects incremental improvements to many aspects of the library.
+
+ - Upgrading:
+ VP8E_SET_CPUUSED now accepts values up to 9 for vp9.
+ VPX_CTRL_VP9E_SET_MAX_INTER_BITRATE_PCT had a spelling fix (was VP8E).
+ The --sdk-path option has been removed. If you were using it to build for
+ Android please read build/make/Android.mk for alternatives.
+ All PPC optimizations have been disabled:
+ https://bugs.chromium.org/p/webm/issues/detail?id=1522.
+
+ - Enhancements:
+ Various changes to improve encoder rate control, quality and speed
+ for practically every use case.
+
+ - Bug fixes:
+ vp9-rtc: Fix color artifacts for speed >= 8.
+
+2019-01-31 v1.8.0 "Northern Shoveler Duck"
+ This release focused on encoding performance for realtime and VOD use cases.
+
+ - Upgrading:
+ This adds and improves several vp9 controls. Most are related to SVC:
+ VP9E_SET_SVC_FRAME_DROP_LAYER:
+ - Frame dropping in SVC.
+ VP9E_SET_SVC_INTER_LAYER_PRED:
+ - Inter-layer prediction in SVC.
+ VP9E_SET_SVC_GF_TEMPORAL_REF:
+ - Enable long term temporal reference in SVC.
+ VP9E_SET_SVC_REF_FRAME_CONFIG/VP9E_GET_SVC_REF_FRAME_CONFIG:
+ - Extend and improve this control for better flexibility in setting SVC
+ pattern dynamically.
+ VP9E_SET_POSTENCODE_DROP:
+ - Allow for post-encode frame dropping (applies to non-SVC too).
+ VP9E_SET_SVC_SPATIAL_LAYER_SYNC:
+ - Enable spatial layer sync frames.
+ VP9E_SET_SVC_LAYER_ID:
+ - Extend api to specify temporal id for each spatial layers.
+ VP9E_SET_ROI_MAP:
+ - Extend Region of Interest functionality to VP9.
+
+ - Enhancements:
+ 2 pass vp9 encoding has improved substantially. When using --auto-alt-ref=6,
+ we see approximately 8% for VBR and 10% for CQ. When using --auto-alt-ref=1,
+ the gains are approximately 4% for VBR and 5% for CQ.
+
+ For real-time encoding, speed 7 has improved by ~5-10%. Encodes targeted at
+ screen sharing have improved when the content changes significantly (slide
+ sharing) or scrolls. There is a new speed 9 setting for mobile devices which
+ is about 10-20% faster than speed 8.
+
+ - Bug fixes:
+ VP9 denoiser issue.
+ VP9 partition issue for 1080p.
+ VP9 rate control improvments.
+ Postprocessing Multi Frame Quality Enhancement (MFQE) issue.
+ VP8 multithread decoder issues.
+ A variety of fuzzing issues.
+
+2018-01-04 v1.7.0 "Mandarin Duck"
This release focused on high bit depth performance (10/12 bit) and vp9
encoding improvements.
diff --git a/libs/libvpx/README b/libs/libvpx/README
index 73304dd62f4..a1000e08509 100644
--- a/libs/libvpx/README
+++ b/libs/libvpx/README
@@ -1,4 +1,4 @@
-README - 24 January 2018
+README - 15 July 2019
Welcome to the WebM VP8/VP9 Codec SDK!
@@ -9,22 +9,26 @@ COMPILING THE APPLICATIONS/LIBRARIES:
1. Prerequisites
- * All x86 targets require the Yasm[1] assembler be installed.
- * All Windows builds require that Cygwin[2] be installed.
- * Building the documentation requires Doxygen[3]. If you do not
+ * All x86 targets require the Yasm[1] assembler be installed[2].
+ * All Windows builds require that Cygwin[3] be installed.
+ * Building the documentation requires Doxygen[4]. If you do not
have this package, the install-docs option will be disabled.
- * Downloading the data for the unit tests requires curl[4] and sha1sum.
+ * Downloading the data for the unit tests requires curl[5] and sha1sum.
sha1sum is provided via the GNU coreutils, installed by default on
many *nix platforms, as well as MinGW and Cygwin. If coreutils is not
available, a compatible version of sha1sum can be built from
- source[5]. These requirements are optional if not running the unit
+ source[6]. These requirements are optional if not running the unit
tests.
[1]: http://www.tortall.net/projects/yasm
- [2]: http://www.cygwin.com
- [3]: http://www.doxygen.org
- [4]: http://curl.haxx.se
- [5]: http://www.microbrew.org/tools/md5sha1sum/
+ [2]: For Visual Studio the base yasm binary (not vsyasm) should be in the
+ PATH for Visual Studio. For VS2017 it is sufficient to rename
+ yasm--.exe to yasm.exe and place it in:
+ Program Files (x86)/Microsoft Visual Studio/2017//Common7/Tools/
+ [3]: http://www.cygwin.com
+ [4]: http://www.doxygen.org
+ [5]: http://curl.haxx.se
+ [6]: http://www.microbrew.org/tools/md5sha1sum/
2. Out-of-tree builds
Out of tree builds are a supported method of building the application. For
@@ -41,7 +45,16 @@ COMPILING THE APPLICATIONS/LIBRARIES:
used to get a list of supported options:
$ ../libvpx/configure --help
- 4. Cross development
+ 4. Compiler analyzers
+ Compilers have added sanitizers which instrument binaries with information
+ about address calculation, memory usage, threading, undefined behavior, and
+ other common errors. To simplify building libvpx with some of these features
+ use tools/set_analyzer_env.sh before running configure. It will set the
+ compiler and necessary flags for building as well as environment variables
+ read by the analyzer when testing the binaries.
+ $ source ../libvpx/tools/set_analyzer_env.sh address
+
+ 5. Cross development
For cross development, the most notable option is the --target option. The
most up-to-date list of supported targets can be found at the bottom of the
--help output of the configure script. As of this writing, the list of
@@ -50,20 +63,20 @@ COMPILING THE APPLICATIONS/LIBRARIES:
arm64-android-gcc
arm64-darwin-gcc
arm64-linux-gcc
+ arm64-win64-gcc
+ arm64-win64-vs15
armv7-android-gcc
armv7-darwin-gcc
armv7-linux-rvct
armv7-linux-gcc
armv7-none-rvct
- armv7-win32-vs11
- armv7-win32-vs12
+ armv7-win32-gcc
armv7-win32-vs14
armv7-win32-vs15
armv7s-darwin-gcc
armv8-linux-gcc
mips32-linux-gcc
mips64-linux-gcc
- ppc64-linux-gcc
ppc64le-linux-gcc
sparc-solaris-gcc
x86-android-gcc
@@ -78,15 +91,13 @@ COMPILING THE APPLICATIONS/LIBRARIES:
x86-darwin14-gcc
x86-darwin15-gcc
x86-darwin16-gcc
+ x86-darwin17-gcc
x86-iphonesimulator-gcc
x86-linux-gcc
x86-linux-icc
x86-os2-gcc
x86-solaris-gcc
x86-win32-gcc
- x86-win32-vs10
- x86-win32-vs11
- x86-win32-vs12
x86-win32-vs14
x86-win32-vs15
x86_64-android-gcc
@@ -98,14 +109,12 @@ COMPILING THE APPLICATIONS/LIBRARIES:
x86_64-darwin14-gcc
x86_64-darwin15-gcc
x86_64-darwin16-gcc
+ x86_64-darwin17-gcc
x86_64-iphonesimulator-gcc
x86_64-linux-gcc
x86_64-linux-icc
x86_64-solaris-gcc
x86_64-win64-gcc
- x86_64-win64-vs10
- x86_64-win64-vs11
- x86_64-win64-vs12
x86_64-win64-vs14
x86_64-win64-vs15
generic-gnu
@@ -123,7 +132,7 @@ COMPILING THE APPLICATIONS/LIBRARIES:
environment variables: CC, AR, LD, AS, STRIP, NM. Additional flags can be
passed to these executables with CFLAGS, LDFLAGS, and ASFLAGS.
- 5. Configuration errors
+ 6. Configuration errors
If the configuration step fails, the first step is to look in the error log.
This defaults to config.log. This should give a good indication of what went
wrong. If not, contact us for support.
diff --git a/libs/libvpx/args.h b/libs/libvpx/args.h
index 54abe04607d..aae8ec06a53 100644
--- a/libs/libvpx/args.h
+++ b/libs/libvpx/args.h
@@ -8,8 +8,8 @@
* be found in the AUTHORS file in the root of the source tree.
*/
-#ifndef ARGS_H_
-#define ARGS_H_
+#ifndef VPX_ARGS_H_
+#define VPX_ARGS_H_
#include
#ifdef __cplusplus
@@ -60,4 +60,4 @@ int arg_parse_enum_or_int(const struct arg *arg);
} // extern "C"
#endif
-#endif // ARGS_H_
+#endif // VPX_ARGS_H_
diff --git a/libs/libvpx/build/.gitattributes b/libs/libvpx/build/.gitattributes
deleted file mode 100644
index 03db79bc088..00000000000
--- a/libs/libvpx/build/.gitattributes
+++ /dev/null
@@ -1,2 +0,0 @@
-*-vs8/*.rules -crlf
-*-msvs/*.rules -crlf
diff --git a/libs/libvpx/build/.gitignore b/libs/libvpx/build/.gitignore
deleted file mode 100644
index 1350fcb5eba..00000000000
--- a/libs/libvpx/build/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-x86*-win32-vs*
diff --git a/libs/libvpx/build/make/Android.mk b/libs/libvpx/build/make/Android.mk
index a88f90056e4..6cb3af027b4 100644
--- a/libs/libvpx/build/make/Android.mk
+++ b/libs/libvpx/build/make/Android.mk
@@ -14,7 +14,7 @@
# Run the configure script from the jni directory. Base libvpx
# encoder/decoder configuration will look similar to:
# ./libvpx/configure --target=armv7-android-gcc --disable-examples \
-# --sdk-path=/opt/android-ndk-r6b/
+# --enable-external-build
#
# When targeting Android, realtime-only is enabled by default. This can
# be overridden by adding the command line flag:
@@ -29,37 +29,20 @@
# include $(CLEAR_VARS)
# include jni/libvpx/build/make/Android.mk
#
-# By default libvpx will detect at runtime the existance of NEON extension.
-# For this we import the 'cpufeatures' module from the NDK sources.
-# libvpx can also be configured without this runtime detection method.
-# Configuring with --disable-runtime-cpu-detect will assume presence of NEON.
-# Configuring with --disable-runtime-cpu-detect --disable-neon \
-# --disable-neon-asm
-# will remove any NEON dependency.
+# By default libvpx will use the 'cpufeatures' module from the NDK. This allows
+# the library to be built with all available optimizations (SSE2->AVX512 for
+# x86, NEON for arm, DSPr2 for mips). This can be disabled with
+# --disable-runtime-cpu-detect
+# but the resulting library *must* be run on devices supporting all of the
+# enabled extensions. They can be disabled individually with
+# --disable-{sse2, sse3, ssse3, sse4_1, avx, avx2, avx512}
+# --disable-neon[-asm]
+# --disable-{dspr2, msa}
#
# Running ndk-build will build libvpx and include it in your project.
#
-# Alternatively, building the examples and unit tests can be accomplished in the
-# following way:
-#
-# Create a standalone toolchain from the NDK:
-# https://developer.android.com/ndk/guides/standalone_toolchain.html
-#
-# For example - to test on arm64 devices with clang:
-# $NDK/build/tools/make_standalone_toolchain.py \
-# --arch arm64 --install-dir=/tmp/my-android-toolchain
-# export PATH=/tmp/my-android-toolchain/bin:$PATH
-# CROSS=aarch64-linux-android- CC=clang CXX=clang++ /path/to/libvpx/configure \
-# --target=arm64-android-gcc
-#
-# Push the resulting binaries to a device and run them:
-# adb push test_libvpx /data/tmp/test_libvpx
-# adb shell /data/tmp/test_libvpx --gtest_filter=\*Sixtap\*
-#
-# Make sure to push the test data as well and set LIBVPX_TEST_DATA
-
CONFIG_DIR := $(LOCAL_PATH)/
LIBVPX_PATH := $(LOCAL_PATH)/libvpx
ASM_CNV_PATH_LOCAL := $(TARGET_ARCH_ABI)/ads2gas
diff --git a/libs/libvpx/build/make/Makefile b/libs/libvpx/build/make/Makefile
index f6b3f0630f1..c070cd0e0c6 100644
--- a/libs/libvpx/build/make/Makefile
+++ b/libs/libvpx/build/make/Makefile
@@ -99,6 +99,7 @@ distclean: clean
rm -f Makefile; \
rm -f config.log config.mk; \
rm -f vpx_config.[hc] vpx_config.asm; \
+ rm -f arm_neon.h; \
else \
rm -f $(target)-$(TOOLCHAIN).mk; \
fi
diff --git a/libs/libvpx/build/make/ads2gas.pl b/libs/libvpx/build/make/ads2gas.pl
index 029cc4a56f2..b6a8f53eae2 100755
--- a/libs/libvpx/build/make/ads2gas.pl
+++ b/libs/libvpx/build/make/ads2gas.pl
@@ -23,16 +23,17 @@
use thumb;
my $thumb = 0;
+my $elf = 1;
foreach my $arg (@ARGV) {
$thumb = 1 if ($arg eq "-thumb");
+ $elf = 0 if ($arg eq "-noelf");
}
print "@ This file was created from a .asm file\n";
print "@ using the ads2gas.pl script.\n";
-print "\t.equ DO1STROUNDING, 0\n";
+print "\t.syntax unified\n";
if ($thumb) {
- print "\t.syntax unified\n";
print "\t.thumb\n";
}
@@ -140,7 +141,11 @@
# Make function visible to linker, and make additional symbol with
# prepended underscore
- s/EXPORT\s+\|([\$\w]*)\|/.global $1 \n\t.type $1, function/;
+ if ($elf) {
+ s/EXPORT\s+\|([\$\w]*)\|/.global $1 \n\t.type $1, function/;
+ } else {
+ s/EXPORT\s+\|([\$\w]*)\|/.global $1/;
+ }
s/IMPORT\s+\|([\$\w]*)\|/.global $1/;
s/EXPORT\s+([\$\w]*)/.global $1/;
@@ -181,11 +186,16 @@
# eabi_attributes numerical equivalents can be found in the
# "ARM IHI 0045C" document.
- # REQUIRE8 Stack is required to be 8-byte aligned
- s/\sREQUIRE8/.eabi_attribute 24, 1 \@Tag_ABI_align_needed/g;
+ if ($elf) {
+ # REQUIRE8 Stack is required to be 8-byte aligned
+ s/\sREQUIRE8/.eabi_attribute 24, 1 \@Tag_ABI_align_needed/g;
- # PRESERVE8 Stack 8-byte align is preserved
- s/\sPRESERVE8/.eabi_attribute 25, 1 \@Tag_ABI_align_preserved/g;
+ # PRESERVE8 Stack 8-byte align is preserved
+ s/\sPRESERVE8/.eabi_attribute 25, 1 \@Tag_ABI_align_preserved/g;
+ } else {
+ s/\sREQUIRE8//;
+ s/\sPRESERVE8//;
+ }
# Use PROC and ENDP to give the symbols a .size directive.
# This makes them show up properly in debugging tools like gdb and valgrind.
@@ -202,7 +212,7 @@
my $proc;
s/\bENDP\b/@ $&/;
$proc = pop(@proc_stack);
- $_ = "\t.size $proc, .-$proc".$_ if ($proc);
+ $_ = "\t.size $proc, .-$proc".$_ if ($proc and $elf);
}
# EQU directive
@@ -225,4 +235,4 @@
}
# Mark that this object doesn't need an executable stack.
-printf ("\t.section\t.note.GNU-stack,\"\",\%\%progbits\n");
+printf ("\t.section\t.note.GNU-stack,\"\",\%\%progbits\n") if $elf;
diff --git a/libs/libvpx/build/make/ads2gas_apple.pl b/libs/libvpx/build/make/ads2gas_apple.pl
index e1ae7b4f871..848872fa7d1 100755
--- a/libs/libvpx/build/make/ads2gas_apple.pl
+++ b/libs/libvpx/build/make/ads2gas_apple.pl
@@ -20,9 +20,7 @@
print "@ This file was created from a .asm file\n";
print "@ using the ads2gas_apple.pl script.\n\n";
-print "\t.set WIDE_REFERENCE, 0\n";
-print "\t.set ARCHITECTURE, 5\n";
-print "\t.set DO1STROUNDING, 0\n";
+print "\t.syntax unified\n";
my %register_aliases;
my %macro_aliases;
diff --git a/libs/libvpx/build/make/configure.sh b/libs/libvpx/build/make/configure.sh
index 4bf61eb5eb8..45f97adacdb 100644
--- a/libs/libvpx/build/make/configure.sh
+++ b/libs/libvpx/build/make/configure.sh
@@ -319,6 +319,12 @@ check_ld() {
&& check_cmd ${LD} ${LDFLAGS} "$@" -o ${TMP_X} ${TMP_O} ${extralibs}
}
+check_lib() {
+ log check_lib "$@"
+ check_cc $@ \
+ && check_cmd ${LD} ${LDFLAGS} -o ${TMP_X} ${TMP_O} "$@" ${extralibs}
+}
+
check_header(){
log check_header "$@"
header=$1
@@ -420,6 +426,26 @@ check_gcc_machine_options() {
fi
}
+check_gcc_avx512_compiles() {
+ if disabled gcc; then
+ return
+ fi
+
+ check_cc -mavx512f <
+void f(void) {
+ __m512i x = _mm512_set1_epi16(0);
+ (void)x;
+}
+EOF
+ compile_result=$?
+ if [ ${compile_result} -ne 0 ]; then
+ log_echo " disabling avx512: not supported by compiler"
+ disable_feature avx512
+ RTCD_OPTIONS="${RTCD_OPTIONS}--disable-avx512 "
+ fi
+}
+
write_common_config_banner() {
print_webm_license config.mk "##" ""
echo '# This file automatically generated by configure. Do not edit!' >> config.mk
@@ -481,6 +507,7 @@ AS_SFX = ${AS_SFX:-.asm}
EXE_SFX = ${EXE_SFX}
VCPROJ_SFX = ${VCPROJ_SFX}
RTCD_OPTIONS = ${RTCD_OPTIONS}
+LIBYUV_CXXFLAGS = ${LIBYUV_CXXFLAGS}
EOF
if enabled rvct; then cat >> $1 << EOF
@@ -520,6 +547,24 @@ EOF
cmp "$1" ${TMP_H} >/dev/null 2>&1 || mv ${TMP_H} "$1"
}
+write_win_arm64_neon_h_workaround() {
+ print_webm_license ${TMP_H} "/*" " */"
+ cat >> ${TMP_H} << EOF
+/* This file automatically generated by configure. Do not edit! */
+#ifndef VPX_WIN_ARM_NEON_H_WORKAROUND
+#define VPX_WIN_ARM_NEON_H_WORKAROUND
+/* The Windows SDK has arm_neon.h, but unlike on other platforms it is
+ * ARM32-only. ARM64 NEON support is provided by arm64_neon.h, a proper
+ * superset of arm_neon.h. Work around this by providing a more local
+ * arm_neon.h that simply #includes arm64_neon.h.
+ */
+#include
+#endif /* VPX_WIN_ARM_NEON_H_WORKAROUND */
+EOF
+ mkdir -p `dirname "$1"`
+ cmp "$1" ${TMP_H} >/dev/null 2>&1 || mv ${TMP_H} "$1"
+}
+
process_common_cmdline() {
for opt in "$@"; do
optval="${opt#*=}"
@@ -602,11 +647,7 @@ process_common_cmdline() {
--libdir=*)
libdir="${optval}"
;;
- --sdk-path=*)
- [ -d "${optval}" ] || die "Not a directory: ${optval}"
- sdk_path="${optval}"
- ;;
- --libc|--as|--prefix|--libdir|--sdk-path)
+ --libc|--as|--prefix|--libdir)
die "Option ${opt} requires argument"
;;
--help|-h)
@@ -713,11 +754,8 @@ process_common_toolchain() {
*sparc*)
tgt_isa=sparc
;;
- power*64*-*)
- tgt_isa=ppc64
- ;;
- power*)
- tgt_isa=ppc
+ power*64le*-*)
+ tgt_isa=ppc64le
;;
*mips64el*)
tgt_isa=mips64
@@ -837,7 +875,7 @@ process_common_toolchain() {
IOS_VERSION_MIN="8.0"
else
IOS_VERSION_OPTIONS=""
- IOS_VERSION_MIN="6.0"
+ IOS_VERSION_MIN="7.0"
fi
# Handle darwin variants. Newer SDKs allow targeting older
@@ -957,7 +995,6 @@ process_common_toolchain() {
setup_gnu_toolchain
arch_int=${tgt_isa##armv}
arch_int=${arch_int%%te}
- check_add_asflags --defsym ARCHITECTURE=${arch_int}
tune_cflags="-mtune="
if [ ${tgt_isa} = "armv7" ] || [ ${tgt_isa} = "armv7s" ]; then
if [ -z "${float_abi}" ]; then
@@ -984,6 +1021,16 @@ EOF
enabled debug && add_asflags -g
asm_conversion_cmd="${source_path}/build/make/ads2gas.pl"
+
+ case ${tgt_os} in
+ win*)
+ asm_conversion_cmd="$asm_conversion_cmd -noelf"
+ AS="$CC -c"
+ EXE_SFX=.exe
+ enable_feature thumb
+ ;;
+ esac
+
if enabled thumb; then
asm_conversion_cmd="$asm_conversion_cmd -thumb"
check_add_cflags -mthumb
@@ -991,18 +1038,41 @@ EOF
fi
;;
vs*)
- asm_conversion_cmd="${source_path}/build/make/ads2armasm_ms.pl"
- AS_SFX=.S
- msvs_arch_dir=arm-msvs
- disable_feature multithread
- disable_feature unit_tests
- vs_version=${tgt_cc##vs}
- if [ $vs_version -ge 12 ]; then
- # MSVC 2013 doesn't allow doing plain .exe projects for ARM,
- # only "AppContainerApplication" which requires an AppxManifest.
- # Therefore disable the examples, just build the library.
- disable_feature examples
- disable_feature tools
+ # A number of ARM-based Windows platforms are constrained by their
+ # respective SDKs' limitations. Fortunately, these are all 32-bit ABIs
+ # and so can be selected as 'win32'.
+ if [ ${tgt_os} = "win32" ]; then
+ asm_conversion_cmd="${source_path}/build/make/ads2armasm_ms.pl"
+ AS_SFX=.S
+ msvs_arch_dir=arm-msvs
+ disable_feature multithread
+ disable_feature unit_tests
+ if [ ${tgt_cc##vs} -ge 12 ]; then
+ # MSVC 2013 doesn't allow doing plain .exe projects for ARM32,
+ # only "AppContainerApplication" which requires an AppxManifest.
+ # Therefore disable the examples, just build the library.
+ disable_feature examples
+ disable_feature tools
+ fi
+ else
+ # Windows 10 on ARM, on the other hand, has full Windows SDK support
+ # for building Win32 ARM64 applications in addition to ARM64
+ # Windows Store apps. It is the only 64-bit ARM ABI that
+ # Windows supports, so it is the default definition of 'win64'.
+ # ARM64 build support officially shipped in Visual Studio 15.9.0.
+
+ # Because the ARM64 Windows SDK's arm_neon.h is ARM32-specific
+ # while LLVM's is not, probe its validity.
+ if enabled neon; then
+ if [ -n "${CC}" ]; then
+ check_header arm_neon.h || check_header arm64_neon.h && \
+ enable_feature win_arm64_neon_h_workaround
+ else
+ # If a probe is not possible, assume this is the pure Windows
+ # SDK and so the workaround is necessary.
+ enable_feature win_arm64_neon_h_workaround
+ fi
+ fi
fi
;;
rvct)
@@ -1030,7 +1100,6 @@ EOF
fi
arch_int=${tgt_isa##armv}
arch_int=${arch_int%%te}
- check_add_asflags --pd "\"ARCHITECTURE SETA ${arch_int}\""
enabled debug && add_asflags -g
add_cflags --gnu
add_cflags --enum_is_int
@@ -1045,51 +1114,10 @@ EOF
;;
android*)
- if [ -n "${sdk_path}" ]; then
- SDK_PATH=${sdk_path}
- COMPILER_LOCATION=`find "${SDK_PATH}" \
- -name "arm-linux-androideabi-gcc*" -print -quit`
- TOOLCHAIN_PATH=${COMPILER_LOCATION%/*}/arm-linux-androideabi-
- CC=${TOOLCHAIN_PATH}gcc
- CXX=${TOOLCHAIN_PATH}g++
- AR=${TOOLCHAIN_PATH}ar
- LD=${TOOLCHAIN_PATH}gcc
- AS=${TOOLCHAIN_PATH}as
- STRIP=${TOOLCHAIN_PATH}strip
- NM=${TOOLCHAIN_PATH}nm
-
- if [ -z "${alt_libc}" ]; then
- alt_libc=`find "${SDK_PATH}" -name arch-arm -print | \
- awk '{n = split($0,a,"/"); \
- split(a[n-1],b,"-"); \
- print $0 " " b[2]}' | \
- sort -g -k 2 | \
- awk '{ print $1 }' | tail -1`
- fi
-
- if [ -d "${alt_libc}" ]; then
- add_cflags "--sysroot=${alt_libc}"
- add_ldflags "--sysroot=${alt_libc}"
- fi
-
- # linker flag that routes around a CPU bug in some
- # Cortex-A8 implementations (NDK Dev Guide)
- add_ldflags "-Wl,--fix-cortex-a8"
-
- enable_feature pic
- soft_enable realtime_only
- if [ ${tgt_isa} = "armv7" ]; then
- soft_enable runtime_cpu_detect
- fi
- if enabled runtime_cpu_detect; then
- add_cflags "-I${SDK_PATH}/sources/android/cpufeatures"
- fi
- else
- echo "Assuming standalone build with NDK toolchain."
- echo "See build/make/Android.mk for details."
- check_add_ldflags -static
- soft_enable unit_tests
- fi
+ echo "Assuming standalone build with NDK toolchain."
+ echo "See build/make/Android.mk for details."
+ check_add_ldflags -static
+ soft_enable unit_tests
;;
darwin*)
@@ -1204,6 +1232,11 @@ EOF
esac
if enabled msa; then
+ # TODO(libyuv:793)
+ # The new mips functions in libyuv do not build
+ # with the toolchains we currently use for testing.
+ soft_disable libyuv
+
add_cflags -mmsa
add_asflags -mmsa
add_ldflags -mmsa
@@ -1219,13 +1252,25 @@ EOF
check_add_asflags -march=${tgt_isa}
check_add_asflags -KPIC
;;
- ppc*)
+ ppc64le*)
link_with_cc=gcc
setup_gnu_toolchain
- check_gcc_machine_option "vsx"
+ # Do not enable vsx by default.
+ # https://bugs.chromium.org/p/webm/issues/detail?id=1522
+ enabled vsx || RTCD_OPTIONS="${RTCD_OPTIONS}--disable-vsx "
+ if [ -n "${tune_cpu}" ]; then
+ case ${tune_cpu} in
+ power?)
+ tune_cflags="-mcpu="
+ ;;
+ esac
+ fi
;;
x86*)
case ${tgt_os} in
+ android)
+ soft_enable realtime_only
+ ;;
win*)
enabled gcc && add_cflags -fno-common
;;
@@ -1277,28 +1322,13 @@ EOF
# Skip the check by setting AS arbitrarily
AS=msvs
msvs_arch_dir=x86-msvs
- vc_version=${tgt_cc##vs}
- case $vc_version in
- 7|8|9|10|11|12|13|14)
+ case ${tgt_cc##vs} in
+ 14)
echo "${tgt_cc} does not support avx512, disabling....."
RTCD_OPTIONS="${RTCD_OPTIONS}--disable-avx512 "
soft_disable avx512
;;
esac
- case $vc_version in
- 7|8|9|10)
- echo "${tgt_cc} does not support avx/avx2, disabling....."
- RTCD_OPTIONS="${RTCD_OPTIONS}--disable-avx --disable-avx2 "
- soft_disable avx
- soft_disable avx2
- ;;
- esac
- case $vc_version in
- 7|8|9)
- echo "${tgt_cc} omits stdint.h, disabling webm-io..."
- soft_disable webm_io
- ;;
- esac
;;
esac
@@ -1331,16 +1361,12 @@ EOF
else
if [ "$ext" = "avx512" ]; then
check_gcc_machine_options $ext avx512f avx512cd avx512bw avx512dq avx512vl
+ check_gcc_avx512_compiles
else
# use the shortened version for the flag: sse4_1 -> sse4
check_gcc_machine_option ${ext%_*} $ext
fi
fi
-
- # https://bugs.chromium.org/p/webm/issues/detail?id=1464
- # The assembly optimizations for vpx_sub_pixel_variance do not link with
- # gcc 6.
- enabled sse2 && soft_enable pic
done
if enabled external_build; then
@@ -1400,7 +1426,8 @@ EOF
add_cflags ${sim_arch}
add_ldflags ${sim_arch}
- if [ "$(show_darwin_sdk_major_version iphonesimulator)" -gt 8 ]; then
+ if [ "$(disabled external_build)" ] &&
+ [ "$(show_darwin_sdk_major_version iphonesimulator)" -gt 8 ]; then
# yasm v1.3.0 doesn't know what -fembed-bitcode means, so turning it
# on is pointless (unless building a C-only lib). Warn the user, but
# do nothing here.
@@ -1490,7 +1517,13 @@ EOF
# bionic includes basic pthread functionality, obviating -lpthread.
;;
*)
- check_header pthread.h && add_extralibs -lpthread
+ check_header pthread.h && check_lib -lpthread <
+#include
+void *thread_check (void *);
+void *thread_check (void *obj){ return obj;};
+int main(void) { pthread_t thread_id; return pthread_create(&thread_id, NULL, thread_check, NULL); }
+EOF
;;
esac
fi
diff --git a/libs/libvpx/build/make/gen_msvs_vcxproj.sh b/libs/libvpx/build/make/gen_msvs_vcxproj.sh
index 171d0b99b6e..84515ecff4e 100755
--- a/libs/libvpx/build/make/gen_msvs_vcxproj.sh
+++ b/libs/libvpx/build/make/gen_msvs_vcxproj.sh
@@ -261,6 +261,11 @@ case "$target" in
asm_Debug_cmdline="yasm -Xvc -g cv8 -f win32 ${yasmincs} "%(FullPath)""
asm_Release_cmdline="yasm -Xvc -f win32 ${yasmincs} "%(FullPath)""
;;
+ arm64*)
+ platforms[0]="ARM64"
+ asm_Debug_cmdline="armasm64 -nologo -oldit "%(FullPath)""
+ asm_Release_cmdline="armasm64 -nologo -oldit "%(FullPath)""
+ ;;
arm*)
platforms[0]="ARM"
asm_Debug_cmdline="armasm -nologo -oldit "%(FullPath)""
@@ -307,6 +312,16 @@ generate_vcxproj() {
tag_content ApplicationType "Windows Store"
tag_content ApplicationTypeRevision 8.1
fi
+ if [ "${platforms[0]}" = "ARM64" ]; then
+ # Require the first Visual Studio version to have ARM64 support.
+ tag_content MinimumVisualStudioVersion 15.9
+ fi
+ if [ $vs_ver -eq 15 ] && [ "${platforms[0]}" = "ARM64" ]; then
+ # Since VS 15 does not have a 'use latest SDK version' facility,
+ # specifically require the contemporaneous SDK with official ARM64
+ # support.
+ tag_content WindowsTargetPlatformVersion 10.0.17763.0
+ fi
close_tag PropertyGroup
tag Import \
diff --git a/libs/libvpx/build/make/iosbuild.sh b/libs/libvpx/build/make/iosbuild.sh
index 3211d4f5efe..e1633a89a82 100755
--- a/libs/libvpx/build/make/iosbuild.sh
+++ b/libs/libvpx/build/make/iosbuild.sh
@@ -132,7 +132,8 @@ create_vpx_framework_config_shim() {
done
# Consume the last line of output from the loop: We don't want it.
- sed -i '' -e '$d' "${config_file}"
+ sed -i.bak -e '$d' "${config_file}"
+ rm "${config_file}.bak"
printf "#endif\n\n" >> "${config_file}"
printf "#endif // ${include_guard}" >> "${config_file}"
@@ -244,7 +245,7 @@ build_framework() {
# Trap function. Cleans up the subtree used to build all targets contained in
# $TARGETS.
cleanup() {
- local readonly res=$?
+ local res=$?
cd "${ORIG_PWD}"
if [ $res -ne 0 ]; then
@@ -350,7 +351,7 @@ if [ "$ENABLE_SHARED" = "yes" ]; then
IOS_VERSION_MIN="8.0"
else
IOS_VERSION_OPTIONS=""
- IOS_VERSION_MIN="6.0"
+ IOS_VERSION_MIN="7.0"
fi
if [ "${VERBOSE}" = "yes" ]; then
diff --git a/libs/libvpx/build/make/msvs_common.sh b/libs/libvpx/build/make/msvs_common.sh
index 88f1cf9b570..27ddf7fd91d 100644
--- a/libs/libvpx/build/make/msvs_common.sh
+++ b/libs/libvpx/build/make/msvs_common.sh
@@ -41,6 +41,15 @@ fix_path() {
# Corrects the paths in file_list in one pass for efficiency.
# $1 is the name of the array to be modified.
fix_file_list() {
+ if [ "${FIXPATH}" = "echo_path" ] ; then
+ # When used with echo_path, fix_file_list is a no-op. Avoid warning about
+ # unsupported 'declare -n' when it is not important.
+ return 0
+ elif [ "${BASH_VERSINFO}" -lt 4 ] ; then
+ echo "Cygwin path conversion has failed. Please use a version of bash"
+ echo "which supports nameref (-n), introduced in bash 4.3"
+ return 1
+ fi
declare -n array_ref=$1
files=$(fix_path "${array_ref[@]}")
local IFS=$'\n'
diff --git a/libs/libvpx/build/make/rtcd.pl b/libs/libvpx/build/make/rtcd.pl
index 68e92b52cc5..7483200411a 100755
--- a/libs/libvpx/build/make/rtcd.pl
+++ b/libs/libvpx/build/make/rtcd.pl
@@ -400,12 +400,13 @@ ()
#
&require("c");
+&require(keys %required);
if ($opts{arch} eq 'x86') {
@ALL_ARCHS = filter(qw/mmx sse sse2 sse3 ssse3 sse4_1 avx avx2 avx512/);
x86;
} elsif ($opts{arch} eq 'x86_64') {
@ALL_ARCHS = filter(qw/mmx sse sse2 sse3 ssse3 sse4_1 avx avx2 avx512/);
- @REQUIRES = filter(keys %required ? keys %required : qw/mmx sse sse2/);
+ @REQUIRES = filter(qw/mmx sse sse2/);
&require(@REQUIRES);
x86;
} elsif ($opts{arch} eq 'mips32' || $opts{arch} eq 'mips64') {
@@ -433,6 +434,7 @@ ()
arm;
} elsif ($opts{arch} eq 'armv8' || $opts{arch} eq 'arm64' ) {
@ALL_ARCHS = filter(qw/neon/);
+ &require("neon");
arm;
} elsif ($opts{arch} =~ /^ppc/ ) {
@ALL_ARCHS = filter(qw/vsx/);
diff --git a/libs/libvpx/build/make/thumb.pm b/libs/libvpx/build/make/thumb.pm
index 483c2539c68..9c49e2d8b72 100644
--- a/libs/libvpx/build/make/thumb.pm
+++ b/libs/libvpx/build/make/thumb.pm
@@ -54,13 +54,6 @@ sub FixThumbInstructions($$)
# "addne r0, r0, r2".
s/^(\s*)((ldr|str)(ne)?[bhd]?)(\s+)(\w+),(\s*\w+,)?\s*\[(\w+)\],\s*(\w+)/$1$2$5$6,$7 [$8]\n$1add$4$5$8, $8, $9/g;
- # Convert a conditional addition to the pc register into a series of
- # instructions. This converts "addlt pc, pc, r3, lsl #2" into
- # "itttt lt", "movlt.n r12, pc", "addlt.w r12, #12",
- # "addlt.w r12, r12, r3, lsl #2", "movlt.n pc, r12".
- # This assumes that r12 is free at this point.
- s/^(\s*)addlt(\s+)pc,\s*pc,\s*(\w+),\s*lsl\s*#(\d+)/$1itttt$2lt\n$1movlt.n$2r12, pc\n$1addlt.w$2r12, #12\n$1addlt.w$2r12, r12, $3, lsl #($4-$branch_shift_offset)\n$1movlt.n$2pc, r12/g;
-
# Convert "mov pc, lr" into "bx lr", since the former only works
# for switching from arm to thumb (and only in armv7), but not
# from thumb to arm.
diff --git a/libs/libvpx/codereview.settings b/libs/libvpx/codereview.settings
index 34c6f1d9dea..ccba2eeed24 100644
--- a/libs/libvpx/codereview.settings
+++ b/libs/libvpx/codereview.settings
@@ -1,5 +1,4 @@
-# This file is used by gcl to get repository specific information.
-GERRIT_HOST: chromium-review.googlesource.com
-GERRIT_PORT: 29418
+# This file is used by git cl to get repository specific information.
+GERRIT_HOST: True
CODE_REVIEW_SERVER: chromium-review.googlesource.com
GERRIT_SQUASH_UPLOADS: False
diff --git a/libs/libvpx/configure b/libs/libvpx/configure
index c84c891c0b6..fb4d7b4ba8c 100755
--- a/libs/libvpx/configure
+++ b/libs/libvpx/configure
@@ -31,7 +31,6 @@ Advanced options:
--libc=PATH path to alternate libc
--size-limit=WxH max size to allow in the decoder
--as={yasm|nasm|auto} use specified assembler [auto, yasm preferred]
- --sdk-path=PATH path to root of sdk (android builds only)
${toggle_codec_srcs} in/exclude codec library source code
${toggle_debug_libs} in/exclude debug version of libraries
${toggle_static_msvcrt} use static MSVCRT (VS builds only)
@@ -101,20 +100,20 @@ EOF
all_platforms="${all_platforms} arm64-android-gcc"
all_platforms="${all_platforms} arm64-darwin-gcc"
all_platforms="${all_platforms} arm64-linux-gcc"
+all_platforms="${all_platforms} arm64-win64-gcc"
+all_platforms="${all_platforms} arm64-win64-vs15"
all_platforms="${all_platforms} armv7-android-gcc" #neon Cortex-A8
all_platforms="${all_platforms} armv7-darwin-gcc" #neon Cortex-A8
all_platforms="${all_platforms} armv7-linux-rvct" #neon Cortex-A8
all_platforms="${all_platforms} armv7-linux-gcc" #neon Cortex-A8
all_platforms="${all_platforms} armv7-none-rvct" #neon Cortex-A8
-all_platforms="${all_platforms} armv7-win32-vs11"
-all_platforms="${all_platforms} armv7-win32-vs12"
+all_platforms="${all_platforms} armv7-win32-gcc"
all_platforms="${all_platforms} armv7-win32-vs14"
all_platforms="${all_platforms} armv7-win32-vs15"
all_platforms="${all_platforms} armv7s-darwin-gcc"
all_platforms="${all_platforms} armv8-linux-gcc"
all_platforms="${all_platforms} mips32-linux-gcc"
all_platforms="${all_platforms} mips64-linux-gcc"
-all_platforms="${all_platforms} ppc64-linux-gcc"
all_platforms="${all_platforms} ppc64le-linux-gcc"
all_platforms="${all_platforms} sparc-solaris-gcc"
all_platforms="${all_platforms} x86-android-gcc"
@@ -137,9 +136,6 @@ all_platforms="${all_platforms} x86-linux-icc"
all_platforms="${all_platforms} x86-os2-gcc"
all_platforms="${all_platforms} x86-solaris-gcc"
all_platforms="${all_platforms} x86-win32-gcc"
-all_platforms="${all_platforms} x86-win32-vs10"
-all_platforms="${all_platforms} x86-win32-vs11"
-all_platforms="${all_platforms} x86-win32-vs12"
all_platforms="${all_platforms} x86-win32-vs14"
all_platforms="${all_platforms} x86-win32-vs15"
all_platforms="${all_platforms} x86_64-android-gcc"
@@ -159,9 +155,6 @@ all_platforms="${all_platforms} x86_64-linux-gcc"
all_platforms="${all_platforms} x86_64-linux-icc"
all_platforms="${all_platforms} x86_64-solaris-gcc"
all_platforms="${all_platforms} x86_64-win64-gcc"
-all_platforms="${all_platforms} x86_64-win64-vs10"
-all_platforms="${all_platforms} x86_64-win64-vs11"
-all_platforms="${all_platforms} x86_64-win64-vs12"
all_platforms="${all_platforms} x86_64-win64-vs14"
all_platforms="${all_platforms} x86_64-win64-vs15"
all_platforms="${all_platforms} generic-gnu"
@@ -278,9 +271,9 @@ HAVE_LIST="
unistd_h
"
EXPERIMENT_LIST="
- spatial_svc
fp_mb_stats
emulate_hardware
+ non_greedy_mv
"
CONFIG_LIST="
dependency_tracking
@@ -330,12 +323,15 @@ CONFIG_LIST="
multi_res_encoding
temporal_denoising
vp9_temporal_denoising
+ consistent_recode
coefficient_range_checking
vp9_highbitdepth
better_hw_compatibility
experimental
size_limit
always_adjust_bpm
+ bitstream_debug
+ mismatch_debug
${EXPERIMENT_LIST}
"
CMDLINE_SELECT="
@@ -391,11 +387,14 @@ CMDLINE_SELECT="
multi_res_encoding
temporal_denoising
vp9_temporal_denoising
+ consistent_recode
coefficient_range_checking
better_hw_compatibility
vp9_highbitdepth
experimental
always_adjust_bpm
+ bitstream_debug
+ mismatch_debug
"
process_cmdline() {
@@ -426,6 +425,12 @@ process_cmdline() {
}
post_process_cmdline() {
+ if enabled coefficient_range_checking; then
+ echo "coefficient-range-checking is for decoders only, disabling encoders:"
+ soft_disable vp8_encoder
+ soft_disable vp9_encoder
+ fi
+
c=""
# Enable all detected codecs, if they haven't been disabled
@@ -447,6 +452,7 @@ process_targets() {
enabled child || write_common_config_banner
write_common_target_config_h ${BUILD_PFX}vpx_config.h
write_common_config_targets
+ enabled win_arm64_neon_h_workaround && write_win_arm64_neon_h_workaround ${BUILD_PFX}arm_neon.h
# Calculate the default distribution name, based on the enabled features
cf=""
@@ -523,7 +529,7 @@ process_detect() {
# here rather than at option parse time because the target auto-detect
# magic happens after the command line has been parsed.
case "${tgt_os}" in
- linux|os2|darwin*|iphonesimulator*)
+ linux|os2|solaris|darwin*|iphonesimulator*)
# Supported platforms
;;
*)
@@ -575,16 +581,32 @@ process_detect() {
check_ld() {
true
}
+ check_lib() {
+ true
+ }
fi
check_header stdio.h || die "Unable to invoke compiler: ${CC} ${CFLAGS}"
check_ld <
+#include
+void *thread_check (void *);
+void *thread_check (void *obj){ return obj;};
+int main(void) { pthread_t thread_id; return pthread_create(&thread_id, NULL, thread_check, NULL); }
+EOF
check_header unistd.h # for sysconf(3) and friends.
check_header vpx/vpx_integer.h -I${source_path} && enable_feature vpx_ports
+
+ if enabled neon && ! enabled external_build; then
+ check_header arm_neon.h || die "Unable to find arm_neon.h"
+ fi
}
process_toolchain() {
@@ -603,22 +625,39 @@ process_toolchain() {
check_add_cflags -Wcast-qual
check_add_cflags -Wvla
check_add_cflags -Wimplicit-function-declaration
+ check_add_cflags -Wmissing-declarations
+ check_add_cflags -Wmissing-prototypes
check_add_cflags -Wuninitialized
check_add_cflags -Wunused
- # -Wextra has some tricky cases. Rather than fix them all now, get the
- # flag for as many files as possible and fix the remaining issues
- # piecemeal.
- # https://bugs.chromium.org/p/webm/issues/detail?id=1069
check_add_cflags -Wextra
# check_add_cflags also adds to cxxflags. gtest does not do well with
- # -Wundef so add it explicitly to CFLAGS only.
+ # these flags so add them explicitly to CFLAGS only.
check_cflags -Wundef && add_cflags_only -Wundef
+ check_cflags -Wframe-larger-than=52000 && \
+ add_cflags_only -Wframe-larger-than=52000
if enabled mips || [ -z "${INLINE}" ]; then
enabled extra_warnings || check_add_cflags -Wno-unused-function
fi
+ # Enforce c89 for c files. Don't be too strict about it though. Allow
+ # gnu extensions like "//" for comments.
+ check_cflags -std=gnu89 && add_cflags_only -std=gnu89
# Avoid this warning for third_party C++ sources. Some reorganization
# would be needed to apply this only to test/*.cc.
check_cflags -Wshorten-64-to-32 && add_cflags_only -Wshorten-64-to-32
+
+ # Quiet gcc 6 vs 7 abi warnings:
+ # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77728
+ if enabled arm; then
+ check_add_cxxflags -Wno-psabi
+ fi
+
+ # disable some warnings specific to libyuv.
+ check_cxxflags -Wno-missing-declarations \
+ && LIBYUV_CXXFLAGS="${LIBYUV_CXXFLAGS} -Wno-missing-declarations"
+ check_cxxflags -Wno-missing-prototypes \
+ && LIBYUV_CXXFLAGS="${LIBYUV_CXXFLAGS} -Wno-missing-prototypes"
+ check_cxxflags -Wno-unused-parameter \
+ && LIBYUV_CXXFLAGS="${LIBYUV_CXXFLAGS} -Wno-unused-parameter"
fi
if enabled icc; then
@@ -689,7 +728,7 @@ process_toolchain() {
soft_enable libyuv
;;
*-android-*)
- soft_enable webm_io
+ check_add_cxxflags -std=c++11 && soft_enable webm_io
soft_enable libyuv
# GTestLog must be modified to use Android logging utilities.
;;
@@ -698,30 +737,23 @@ process_toolchain() {
# x86 targets.
;;
*-iphonesimulator-*)
- soft_enable webm_io
+ check_add_cxxflags -std=c++11 && soft_enable webm_io
soft_enable libyuv
;;
*-win*)
# Some mingw toolchains don't have pthread available by default.
# Treat these more like visual studio where threading in gtest
# would be disabled for the same reason.
- check_cxx "$@" < $@
+ @echo "ENABLED_SECTIONS += samples" >> $@
diff --git a/libs/libvpx/vpx/svc_context.h b/libs/libvpx/examples/svc_context.h
similarity index 83%
rename from libs/libvpx/vpx/svc_context.h
rename to libs/libvpx/examples/svc_context.h
index 462785075cb..c5779ce8a9f 100644
--- a/libs/libvpx/vpx/svc_context.h
+++ b/libs/libvpx/examples/svc_context.h
@@ -13,11 +13,11 @@
* spatial SVC frame
*/
-#ifndef VPX_SVC_CONTEXT_H_
-#define VPX_SVC_CONTEXT_H_
+#ifndef VPX_EXAMPLES_SVC_CONTEXT_H_
+#define VPX_EXAMPLES_SVC_CONTEXT_H_
-#include "./vp8cx.h"
-#include "./vpx_encoder.h"
+#include "vpx/vp8cx.h"
+#include "vpx/vpx_encoder.h"
#ifdef __cplusplus
extern "C" {
@@ -35,10 +35,8 @@ typedef struct {
int temporal_layers; // number of temporal layers
int temporal_layering_mode;
SVC_LOG_LEVEL log_level; // amount of information to display
- int log_print; // when set, printf log messages instead of returning the
- // message with svc_get_message
- int output_rc_stat; // for outputting rc stats
- int speed; // speed setting for codec
+ int output_rc_stat; // for outputting rc stats
+ int speed; // speed setting for codec
int threads;
int aqmode; // turns on aq-mode=3 (cyclic_refresh): 0=off, 1=on.
// private storage for vpx_svc_encode
@@ -71,7 +69,6 @@ typedef struct SvcInternal {
int layer;
int use_multiple_frame_contexts;
- char message_buffer[2048];
vpx_codec_ctx_t *codec_ctx;
} SvcInternal_t;
@@ -106,15 +103,10 @@ void vpx_svc_release(SvcContext *svc_ctx);
/**
* dump accumulated statistics and reset accumulated values
*/
-const char *vpx_svc_dump_statistics(SvcContext *svc_ctx);
-
-/**
- * get status message from previous encode
- */
-const char *vpx_svc_get_message(const SvcContext *svc_ctx);
+void vpx_svc_dump_statistics(SvcContext *svc_ctx);
#ifdef __cplusplus
} // extern "C"
#endif
-#endif // VPX_SVC_CONTEXT_H_
+#endif // VPX_EXAMPLES_SVC_CONTEXT_H_
diff --git a/libs/libvpx/vpx/src/svc_encodeframe.c b/libs/libvpx/examples/svc_encodeframe.c
similarity index 85%
rename from libs/libvpx/vpx/src/svc_encodeframe.c
rename to libs/libvpx/examples/svc_encodeframe.c
index f633600c799..a73ee8ed660 100644
--- a/libs/libvpx/vpx/src/svc_encodeframe.c
+++ b/libs/libvpx/examples/svc_encodeframe.c
@@ -22,7 +22,7 @@
#include
#define VPX_DISABLE_CTRL_TYPECHECKS 1
#include "./vpx_config.h"
-#include "vpx/svc_context.h"
+#include "./svc_context.h"
#include "vpx/vp8cx.h"
#include "vpx/vpx_encoder.h"
#include "vpx_mem/vpx_mem.h"
@@ -95,17 +95,11 @@ static const SvcInternal_t *get_const_svc_internal(const SvcContext *svc_ctx) {
return (const SvcInternal_t *)svc_ctx->internal;
}
-static void svc_log_reset(SvcContext *svc_ctx) {
- SvcInternal_t *const si = (SvcInternal_t *)svc_ctx->internal;
- si->message_buffer[0] = '\0';
-}
-
static int svc_log(SvcContext *svc_ctx, SVC_LOG_LEVEL level, const char *fmt,
...) {
char buf[512];
int retval = 0;
va_list ap;
- SvcInternal_t *const si = get_svc_internal(svc_ctx);
if (level > svc_ctx->log_level) {
return retval;
@@ -115,16 +109,8 @@ static int svc_log(SvcContext *svc_ctx, SVC_LOG_LEVEL level, const char *fmt,
retval = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
- if (svc_ctx->log_print) {
- printf("%s", buf);
- } else {
- strncat(si->message_buffer, buf,
- sizeof(si->message_buffer) - strlen(si->message_buffer) - 1);
- }
+ printf("%s", buf);
- if (level == SVC_LOG_ERROR) {
- si->codec_ctx->err_detail = si->message_buffer;
- }
return retval;
}
@@ -169,6 +155,7 @@ static vpx_codec_err_t parse_layer_options_from_string(SvcContext *svc_ctx,
return VPX_CODEC_INVALID_PARAM;
input_string = strdup(input);
+ if (input_string == NULL) return VPX_CODEC_MEM_ERROR;
token = strtok_r(input_string, delim, &save_ptr);
for (i = 0; i < num_layers; ++i) {
if (token != NULL) {
@@ -208,6 +195,7 @@ static vpx_codec_err_t parse_options(SvcContext *svc_ctx, const char *options) {
if (options == NULL) return VPX_CODEC_OK;
input_string = strdup(options);
+ if (input_string == NULL) return VPX_CODEC_MEM_ERROR;
// parse option name
option_name = strtok_r(input_string, "=", &input_ptr);
@@ -294,8 +282,8 @@ vpx_codec_err_t vpx_svc_set_options(SvcContext *svc_ctx, const char *options) {
return VPX_CODEC_OK;
}
-vpx_codec_err_t assign_layer_bitrates(const SvcContext *svc_ctx,
- vpx_codec_enc_cfg_t *const enc_cfg) {
+static vpx_codec_err_t assign_layer_bitrates(
+ const SvcContext *svc_ctx, vpx_codec_enc_cfg_t *const enc_cfg) {
int i;
const SvcInternal_t *const si = get_const_svc_internal(svc_ctx);
int sl, tl, spatial_layer_target;
@@ -471,8 +459,7 @@ vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
svc_log(svc_ctx, SVC_LOG_ERROR,
"spatial layers * temporal layers exceeds the maximum number of "
"allowed layers of %d\n",
- svc_ctx->spatial_layers * svc_ctx->temporal_layers,
- (int)VPX_MAX_LAYERS);
+ svc_ctx->spatial_layers * svc_ctx->temporal_layers, VPX_MAX_LAYERS);
return VPX_CODEC_INVALID_PARAM;
}
res = assign_layer_bitrates(svc_ctx, enc_cfg);
@@ -485,11 +472,6 @@ vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
return VPX_CODEC_INVALID_PARAM;
}
-#if CONFIG_SPATIAL_SVC
- for (i = 0; i < svc_ctx->spatial_layers; ++i)
- enc_cfg->ss_enable_auto_alt_ref[i] = si->enable_auto_alt_ref[i];
-#endif
-
if (svc_ctx->temporal_layers > 1) {
int i;
for (i = 0; i < svc_ctx->temporal_layers; ++i) {
@@ -514,7 +496,17 @@ vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
enc_cfg->rc_buf_initial_sz = 500;
enc_cfg->rc_buf_optimal_sz = 600;
enc_cfg->rc_buf_sz = 1000;
- enc_cfg->rc_dropframe_thresh = 0;
+ }
+
+ for (tl = 0; tl < svc_ctx->temporal_layers; ++tl) {
+ for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
+ i = sl * svc_ctx->temporal_layers + tl;
+ if (enc_cfg->rc_end_usage == VPX_CBR &&
+ enc_cfg->g_pass == VPX_RC_ONE_PASS) {
+ si->svc_params.max_quantizers[i] = enc_cfg->rc_max_quantizer;
+ si->svc_params.min_quantizers[i] = enc_cfg->rc_min_quantizer;
+ }
+ }
}
if (enc_cfg->g_error_resilient == 0 && si->use_multiple_frame_contexts == 0)
@@ -548,8 +540,6 @@ vpx_codec_err_t vpx_svc_encode(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
return VPX_CODEC_INVALID_PARAM;
}
- svc_log_reset(svc_ctx);
-
res =
vpx_codec_encode(codec_ctx, rawimg, pts, (uint32_t)duration, 0, deadline);
if (res != VPX_CODEC_OK) {
@@ -559,56 +549,7 @@ vpx_codec_err_t vpx_svc_encode(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
iter = NULL;
while ((cx_pkt = vpx_codec_get_cx_data(codec_ctx, &iter))) {
switch (cx_pkt->kind) {
-#if CONFIG_SPATIAL_SVC && defined(VPX_TEST_SPATIAL_SVC)
- case VPX_CODEC_SPATIAL_SVC_LAYER_PSNR: {
- int i;
- for (i = 0; i < svc_ctx->spatial_layers; ++i) {
- int j;
- svc_log(svc_ctx, SVC_LOG_DEBUG,
- "SVC frame: %d, layer: %d, PSNR(Total/Y/U/V): "
- "%2.3f %2.3f %2.3f %2.3f \n",
- si->psnr_pkt_received, i, cx_pkt->data.layer_psnr[i].psnr[0],
- cx_pkt->data.layer_psnr[i].psnr[1],
- cx_pkt->data.layer_psnr[i].psnr[2],
- cx_pkt->data.layer_psnr[i].psnr[3]);
- svc_log(svc_ctx, SVC_LOG_DEBUG,
- "SVC frame: %d, layer: %d, SSE(Total/Y/U/V): "
- "%2.3f %2.3f %2.3f %2.3f \n",
- si->psnr_pkt_received, i, cx_pkt->data.layer_psnr[i].sse[0],
- cx_pkt->data.layer_psnr[i].sse[1],
- cx_pkt->data.layer_psnr[i].sse[2],
- cx_pkt->data.layer_psnr[i].sse[3]);
-
- for (j = 0; j < COMPONENTS; ++j) {
- si->psnr_sum[i][j] += cx_pkt->data.layer_psnr[i].psnr[j];
- si->sse_sum[i][j] += cx_pkt->data.layer_psnr[i].sse[j];
- }
- }
- ++si->psnr_pkt_received;
- break;
- }
- case VPX_CODEC_SPATIAL_SVC_LAYER_SIZES: {
- int i;
- for (i = 0; i < svc_ctx->spatial_layers; ++i)
- si->bytes_sum[i] += cx_pkt->data.layer_sizes[i];
- break;
- }
-#endif
case VPX_CODEC_PSNR_PKT: {
-#if CONFIG_SPATIAL_SVC && defined(VPX_TEST_SPATIAL_SVC)
- int j;
- svc_log(svc_ctx, SVC_LOG_DEBUG,
- "frame: %d, layer: %d, PSNR(Total/Y/U/V): "
- "%2.3f %2.3f %2.3f %2.3f \n",
- si->psnr_pkt_received, 0, cx_pkt->data.layer_psnr[0].psnr[0],
- cx_pkt->data.layer_psnr[0].psnr[1],
- cx_pkt->data.layer_psnr[0].psnr[2],
- cx_pkt->data.layer_psnr[0].psnr[3]);
- for (j = 0; j < COMPONENTS; ++j) {
- si->psnr_sum[0][j] += cx_pkt->data.layer_psnr[0].psnr[j];
- si->sse_sum[0][j] += cx_pkt->data.layer_psnr[0].sse[j];
- }
-#endif
}
++si->psnr_pkt_received;
break;
@@ -619,19 +560,13 @@ vpx_codec_err_t vpx_svc_encode(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
return VPX_CODEC_OK;
}
-const char *vpx_svc_get_message(const SvcContext *svc_ctx) {
- const SvcInternal_t *const si = get_const_svc_internal(svc_ctx);
- if (svc_ctx == NULL || si == NULL) return NULL;
- return si->message_buffer;
-}
-
static double calc_psnr(double d) {
if (d == 0) return 100;
return -10.0 * log(d) / log(10.0);
}
// dump accumulated statistics and reset accumulated values
-const char *vpx_svc_dump_statistics(SvcContext *svc_ctx) {
+void vpx_svc_dump_statistics(SvcContext *svc_ctx) {
int number_of_frames;
int i, j;
uint32_t bytes_total = 0;
@@ -641,21 +576,19 @@ const char *vpx_svc_dump_statistics(SvcContext *svc_ctx) {
double y_scale;
SvcInternal_t *const si = get_svc_internal(svc_ctx);
- if (svc_ctx == NULL || si == NULL) return NULL;
-
- svc_log_reset(svc_ctx);
+ if (svc_ctx == NULL || si == NULL) return;
number_of_frames = si->psnr_pkt_received;
- if (number_of_frames <= 0) return vpx_svc_get_message(svc_ctx);
+ if (number_of_frames <= 0) return;
svc_log(svc_ctx, SVC_LOG_INFO, "\n");
for (i = 0; i < svc_ctx->spatial_layers; ++i) {
svc_log(svc_ctx, SVC_LOG_INFO,
"Layer %d Average PSNR=[%2.3f, %2.3f, %2.3f, %2.3f], Bytes=[%u]\n",
- i, (double)si->psnr_sum[i][0] / number_of_frames,
- (double)si->psnr_sum[i][1] / number_of_frames,
- (double)si->psnr_sum[i][2] / number_of_frames,
- (double)si->psnr_sum[i][3] / number_of_frames, si->bytes_sum[i]);
+ i, si->psnr_sum[i][0] / number_of_frames,
+ si->psnr_sum[i][1] / number_of_frames,
+ si->psnr_sum[i][2] / number_of_frames,
+ si->psnr_sum[i][3] / number_of_frames, si->bytes_sum[i]);
// the following psnr calculation is deduced from ffmpeg.c#print_report
y_scale = si->width * si->height * 255.0 * 255.0 * number_of_frames;
scale[1] = y_scale;
@@ -686,7 +619,6 @@ const char *vpx_svc_dump_statistics(SvcContext *svc_ctx) {
si->psnr_pkt_received = 0;
svc_log(svc_ctx, SVC_LOG_INFO, "Total Bytes=[%u]\n", bytes_total);
- return vpx_svc_get_message(svc_ctx);
}
void vpx_svc_release(SvcContext *svc_ctx) {
diff --git a/libs/libvpx/examples/vp8_multi_resolution_encoder.c b/libs/libvpx/examples/vp8_multi_resolution_encoder.c
index b14b1ff3972..e72f8a01970 100644
--- a/libs/libvpx/examples/vp8_multi_resolution_encoder.c
+++ b/libs/libvpx/examples/vp8_multi_resolution_encoder.c
@@ -61,7 +61,7 @@ void usage_exit(void) { exit(EXIT_FAILURE); }
int (*read_frame_p)(FILE *f, vpx_image_t *img);
-static int read_frame(FILE *f, vpx_image_t *img) {
+static int mulres_read_frame(FILE *f, vpx_image_t *img) {
size_t nbytes, to_read;
int res = 1;
@@ -75,7 +75,7 @@ static int read_frame(FILE *f, vpx_image_t *img) {
return res;
}
-static int read_frame_by_row(FILE *f, vpx_image_t *img) {
+static int mulres_read_frame_by_row(FILE *f, vpx_image_t *img) {
size_t nbytes, to_read;
int res = 1;
int plane;
@@ -471,9 +471,9 @@ int main(int argc, char **argv) {
die("Failed to allocate image", cfg[i].g_w, cfg[i].g_h);
if (raw[0].stride[VPX_PLANE_Y] == (int)raw[0].d_w)
- read_frame_p = read_frame;
+ read_frame_p = mulres_read_frame;
else
- read_frame_p = read_frame_by_row;
+ read_frame_p = mulres_read_frame_by_row;
for (i = 0; i < NUM_ENCODERS; i++)
if (outfile[i]) write_ivf_file_header(outfile[i], &cfg[i], 0);
diff --git a/libs/libvpx/examples/vp9_spatial_svc_encoder.c b/libs/libvpx/examples/vp9_spatial_svc_encoder.c
index 0987cbfb858..b987989a86c 100644
--- a/libs/libvpx/examples/vp9_spatial_svc_encoder.c
+++ b/libs/libvpx/examples/vp9_spatial_svc_encoder.c
@@ -25,13 +25,19 @@
#include "../video_writer.h"
#include "../vpx_ports/vpx_timer.h"
-#include "vpx/svc_context.h"
+#include "./svc_context.h"
#include "vpx/vp8cx.h"
#include "vpx/vpx_encoder.h"
#include "../vpxstats.h"
#include "vp9/encoder/vp9_encoder.h"
+#include "./y4minput.h"
+
#define OUTPUT_RC_STATS 1
+#define SIMULCAST_MODE 0
+
+static const arg_def_t outputfile =
+ ARG_DEF("o", "output", 1, "Output filename");
static const arg_def_t skip_frames_arg =
ARG_DEF("s", "skip-frames", 1, "input frames to skip");
static const arg_def_t frames_arg =
@@ -86,6 +92,19 @@ static const arg_def_t aqmode_arg =
ARG_DEF("aq", "aqmode", 1, "aq-mode off/on");
static const arg_def_t bitrates_arg =
ARG_DEF("bl", "bitrates", 1, "bitrates[sl * num_tl + tl]");
+static const arg_def_t dropframe_thresh_arg =
+ ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
+static const struct arg_enum_list tune_content_enum[] = {
+ { "default", VP9E_CONTENT_DEFAULT },
+ { "screen", VP9E_CONTENT_SCREEN },
+ { "film", VP9E_CONTENT_FILM },
+ { NULL, 0 }
+};
+
+static const arg_def_t tune_content_arg = ARG_DEF_ENUM(
+ NULL, "tune-content", 1, "Tune content type", tune_content_enum);
+static const arg_def_t inter_layer_pred_arg = ARG_DEF(
+ NULL, "inter-layer-pred", 1, "0 - 3: On, Off, Key-frames, Constrained");
#if CONFIG_VP9_HIGHBITDEPTH
static const struct arg_enum_list bitdepth_enum[] = {
@@ -97,6 +116,7 @@ static const arg_def_t bitdepth_arg = ARG_DEF_ENUM(
#endif // CONFIG_VP9_HIGHBITDEPTH
static const arg_def_t *svc_args[] = { &frames_arg,
+ &outputfile,
&width_arg,
&height_arg,
&timebase_arg,
@@ -127,6 +147,9 @@ static const arg_def_t *svc_args[] = { &frames_arg,
&speed_arg,
&rc_end_usage_arg,
&bitrates_arg,
+ &dropframe_thresh_arg,
+ &tune_content_arg,
+ &inter_layer_pred_arg,
NULL };
static const uint32_t default_frames_to_skip = 0;
@@ -145,7 +168,6 @@ static const int32_t default_speed = -1; // -1 means use library default.
static const uint32_t default_threads = 0; // zero means use library default.
typedef struct {
- const char *input_filename;
const char *output_filename;
uint32_t frames_to_code;
uint32_t frames_to_skip;
@@ -153,12 +175,14 @@ typedef struct {
stats_io_t rc_stats;
int passes;
int pass;
+ int tune_content;
+ int inter_layer_pred;
} AppInput;
static const char *exec_name;
void usage_exit(void) {
- fprintf(stderr, "Usage: %s input_filename output_filename\n",
+ fprintf(stderr, "Usage: %s input_filename -o output_filename\n",
exec_name);
fprintf(stderr, "Options:\n");
arg_show_usage(stderr, svc_args);
@@ -217,6 +241,8 @@ static void parse_command_line(int argc, const char **argv_,
if (arg_match(&arg, &frames_arg, argi)) {
app_input->frames_to_code = arg_parse_uint(&arg);
+ } else if (arg_match(&arg, &outputfile, argi)) {
+ app_input->output_filename = arg.val;
} else if (arg_match(&arg, &width_arg, argi)) {
enc_cfg->g_w = arg_parse_uint(&arg);
} else if (arg_match(&arg, &height_arg, argi)) {
@@ -237,6 +263,9 @@ static void parse_command_line(int argc, const char **argv_,
#endif
} else if (arg_match(&arg, &speed_arg, argi)) {
svc_ctx->speed = arg_parse_uint(&arg);
+ if (svc_ctx->speed > 9) {
+ warn("Mapping speed %d to speed 9.\n", svc_ctx->speed);
+ }
} else if (arg_match(&arg, &aqmode_arg, argi)) {
svc_ctx->aqmode = arg_parse_uint(&arg);
} else if (arg_match(&arg, &threads_arg, argi)) {
@@ -251,11 +280,15 @@ static void parse_command_line(int argc, const char **argv_,
enc_cfg->kf_min_dist = arg_parse_uint(&arg);
enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
} else if (arg_match(&arg, &scale_factors_arg, argi)) {
- snprintf(string_options, sizeof(string_options), "%s scale-factors=%s",
- string_options, arg.val);
+ strncat(string_options, " scale-factors=",
+ sizeof(string_options) - strlen(string_options) - 1);
+ strncat(string_options, arg.val,
+ sizeof(string_options) - strlen(string_options) - 1);
} else if (arg_match(&arg, &bitrates_arg, argi)) {
- snprintf(string_options, sizeof(string_options), "%s bitrates=%s",
- string_options, arg.val);
+ strncat(string_options, " bitrates=",
+ sizeof(string_options) - strlen(string_options) - 1);
+ strncat(string_options, arg.val,
+ sizeof(string_options) - strlen(string_options) - 1);
} else if (arg_match(&arg, &passes_arg, argi)) {
passes = arg_parse_uint(&arg);
if (passes < 1 || passes > 2) {
@@ -269,11 +302,15 @@ static void parse_command_line(int argc, const char **argv_,
} else if (arg_match(&arg, &fpf_name_arg, argi)) {
fpf_file_name = arg.val;
} else if (arg_match(&arg, &min_q_arg, argi)) {
- snprintf(string_options, sizeof(string_options), "%s min-quantizers=%s",
- string_options, arg.val);
+ strncat(string_options, " min-quantizers=",
+ sizeof(string_options) - strlen(string_options) - 1);
+ strncat(string_options, arg.val,
+ sizeof(string_options) - strlen(string_options) - 1);
} else if (arg_match(&arg, &max_q_arg, argi)) {
- snprintf(string_options, sizeof(string_options), "%s max-quantizers=%s",
- string_options, arg.val);
+ strncat(string_options, " max-quantizers=",
+ sizeof(string_options) - strlen(string_options) - 1);
+ strncat(string_options, arg.val,
+ sizeof(string_options) - strlen(string_options) - 1);
} else if (arg_match(&arg, &min_bitrate_arg, argi)) {
min_bitrate = arg_parse_uint(&arg);
} else if (arg_match(&arg, &max_bitrate_arg, argi)) {
@@ -303,6 +340,12 @@ static void parse_command_line(int argc, const char **argv_,
break;
}
#endif // CONFIG_VP9_HIGHBITDEPTH
+ } else if (arg_match(&arg, &dropframe_thresh_arg, argi)) {
+ enc_cfg->rc_dropframe_thresh = arg_parse_uint(&arg);
+ } else if (arg_match(&arg, &tune_content_arg, argi)) {
+ app_input->tune_content = arg_parse_uint(&arg);
+ } else if (arg_match(&arg, &inter_layer_pred_arg, argi)) {
+ app_input->inter_layer_pred = arg_parse_uint(&arg);
} else {
++argj;
}
@@ -358,13 +401,18 @@ static void parse_command_line(int argc, const char **argv_,
if (argi[0][0] == '-' && strlen(argi[0]) > 1)
die("Error: Unrecognized option %s\n", *argi);
- if (argv[0] == NULL || argv[1] == 0) {
+ if (argv[0] == NULL) {
usage_exit();
}
- app_input->input_filename = argv[0];
- app_input->output_filename = argv[1];
+ app_input->input_ctx.filename = argv[0];
free(argv);
+ open_input_file(&app_input->input_ctx);
+ if (app_input->input_ctx.file_type == FILE_TYPE_Y4M) {
+ enc_cfg->g_w = app_input->input_ctx.width;
+ enc_cfg->g_h = app_input->input_ctx.height;
+ }
+
if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
enc_cfg->g_h % 2)
die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
@@ -429,8 +477,9 @@ static void set_rate_control_stats(struct RateControlStats *rc,
rc->layer_framerate[layer] = framerate / cfg->ts_rate_decimator[tl];
if (tl > 0) {
rc->layer_pfb[layer] =
- 1000.0 * (cfg->layer_target_bitrate[layer] -
- cfg->layer_target_bitrate[layer - 1]) /
+ 1000.0 *
+ (cfg->layer_target_bitrate[layer] -
+ cfg->layer_target_bitrate[layer - 1]) /
(rc->layer_framerate[layer] - rc->layer_framerate[layer - 1]);
} else {
rc->layer_pfb[layer] = 1000.0 * cfg->layer_target_bitrate[layer] /
@@ -502,14 +551,13 @@ static void printout_rate_control_summary(struct RateControlStats *rc,
printf("Average, rms-variance, and percent-fluct: %f %f %f \n",
rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
perc_fluctuation);
- if (frame_cnt != tot_num_frames)
- die("Error: Number of input frames not equal to output encoded frames != "
- "%d tot_num_frames = %d\n",
- frame_cnt, tot_num_frames);
+ printf("Num of input, num of encoded (super) frames: %d %d \n", frame_cnt,
+ tot_num_frames);
}
-vpx_codec_err_t parse_superframe_index(const uint8_t *data, size_t data_sz,
- uint64_t sizes[8], int *count) {
+static vpx_codec_err_t parse_superframe_index(const uint8_t *data,
+ size_t data_sz, uint64_t sizes[8],
+ int *count) {
// A chunk ending with a byte matching 0xc0 is an invalid chunk unless
// it is a super frame index. If the last byte of real video compression
// data is 0xc0 the encoder must add a 0 byte. If we have the marker but
@@ -561,106 +609,386 @@ vpx_codec_err_t parse_superframe_index(const uint8_t *data, size_t data_sz,
// bypass/flexible mode. The pattern corresponds to the pattern
// VP9E_TEMPORAL_LAYERING_MODE_0101 (temporal_layering_mode == 2) used in
// non-flexible mode.
-void set_frame_flags_bypass_mode(int sl, int tl, int num_spatial_layers,
- int is_key_frame,
- vpx_svc_ref_frame_config_t *ref_frame_config) {
+static void set_frame_flags_bypass_mode_ex0(
+ int tl, int num_spatial_layers, int is_key_frame,
+ vpx_svc_ref_frame_config_t *ref_frame_config) {
+ int sl;
+ for (sl = 0; sl < num_spatial_layers; ++sl)
+ ref_frame_config->update_buffer_slot[sl] = 0;
+
for (sl = 0; sl < num_spatial_layers; ++sl) {
+ // Set the buffer idx.
+ if (tl == 0) {
+ ref_frame_config->lst_fb_idx[sl] = sl;
+ if (sl) {
+ if (is_key_frame) {
+ ref_frame_config->lst_fb_idx[sl] = sl - 1;
+ ref_frame_config->gld_fb_idx[sl] = sl;
+ } else {
+ ref_frame_config->gld_fb_idx[sl] = sl - 1;
+ }
+ } else {
+ ref_frame_config->gld_fb_idx[sl] = 0;
+ }
+ ref_frame_config->alt_fb_idx[sl] = 0;
+ } else if (tl == 1) {
+ ref_frame_config->lst_fb_idx[sl] = sl;
+ ref_frame_config->gld_fb_idx[sl] = num_spatial_layers + sl - 1;
+ ref_frame_config->alt_fb_idx[sl] = num_spatial_layers + sl;
+ }
+ // Set the reference and update flags.
if (!tl) {
if (!sl) {
- ref_frame_config->frame_flags[sl] =
- VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF |
- VP8_EFLAG_NO_UPD_ARF;
+ // Base spatial and base temporal (sl = 0, tl = 0)
+ ref_frame_config->reference_last[sl] = 1;
+ ref_frame_config->reference_golden[sl] = 0;
+ ref_frame_config->reference_alt_ref[sl] = 0;
+ ref_frame_config->update_buffer_slot[sl] |=
+ 1 << ref_frame_config->lst_fb_idx[sl];
} else {
if (is_key_frame) {
- ref_frame_config->frame_flags[sl] =
- VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_ARF |
- VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
+ ref_frame_config->reference_last[sl] = 1;
+ ref_frame_config->reference_golden[sl] = 0;
+ ref_frame_config->reference_alt_ref[sl] = 0;
+ ref_frame_config->update_buffer_slot[sl] |=
+ 1 << ref_frame_config->gld_fb_idx[sl];
} else {
- ref_frame_config->frame_flags[sl] =
- VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF;
+ // Non-zero spatiall layer.
+ ref_frame_config->reference_last[sl] = 1;
+ ref_frame_config->reference_golden[sl] = 1;
+ ref_frame_config->reference_alt_ref[sl] = 1;
+ ref_frame_config->update_buffer_slot[sl] |=
+ 1 << ref_frame_config->lst_fb_idx[sl];
}
}
} else if (tl == 1) {
if (!sl) {
- ref_frame_config->frame_flags[sl] =
- VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST |
- VP8_EFLAG_NO_UPD_GF;
+ // Base spatial and top temporal (tl = 1)
+ ref_frame_config->reference_last[sl] = 1;
+ ref_frame_config->reference_golden[sl] = 0;
+ ref_frame_config->reference_alt_ref[sl] = 0;
+ ref_frame_config->update_buffer_slot[sl] |=
+ 1 << ref_frame_config->alt_fb_idx[sl];
} else {
- ref_frame_config->frame_flags[sl] =
- VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF;
+ // Non-zero spatial.
+ if (sl < num_spatial_layers - 1) {
+ ref_frame_config->reference_last[sl] = 1;
+ ref_frame_config->reference_golden[sl] = 1;
+ ref_frame_config->reference_alt_ref[sl] = 0;
+ ref_frame_config->update_buffer_slot[sl] |=
+ 1 << ref_frame_config->alt_fb_idx[sl];
+ } else if (sl == num_spatial_layers - 1) {
+ // Top spatial and top temporal (non-reference -- doesn't update any
+ // reference buffers)
+ ref_frame_config->reference_last[sl] = 1;
+ ref_frame_config->reference_golden[sl] = 1;
+ ref_frame_config->reference_alt_ref[sl] = 0;
+ }
}
}
- if (tl == 0) {
- ref_frame_config->lst_fb_idx[sl] = sl;
- if (sl)
- ref_frame_config->gld_fb_idx[sl] = sl - 1;
- else
- ref_frame_config->gld_fb_idx[sl] = 0;
- ref_frame_config->alt_fb_idx[sl] = 0;
- } else if (tl == 1) {
- ref_frame_config->lst_fb_idx[sl] = sl;
- ref_frame_config->gld_fb_idx[sl] = num_spatial_layers + sl - 1;
- ref_frame_config->alt_fb_idx[sl] = num_spatial_layers + sl;
+ }
+}
+
+// Example pattern for 2 spatial layers and 2 temporal layers used in the
+// bypass/flexible mode, except only 1 spatial layer when temporal_layer_id = 1.
+static void set_frame_flags_bypass_mode_ex1(
+ int tl, int num_spatial_layers, int is_key_frame,
+ vpx_svc_ref_frame_config_t *ref_frame_config) {
+ int sl;
+ for (sl = 0; sl < num_spatial_layers; ++sl)
+ ref_frame_config->update_buffer_slot[sl] = 0;
+
+ if (tl == 0) {
+ if (is_key_frame) {
+ ref_frame_config->lst_fb_idx[1] = 0;
+ ref_frame_config->gld_fb_idx[1] = 1;
+ } else {
+ ref_frame_config->lst_fb_idx[1] = 1;
+ ref_frame_config->gld_fb_idx[1] = 0;
+ }
+ ref_frame_config->alt_fb_idx[1] = 0;
+
+ ref_frame_config->lst_fb_idx[0] = 0;
+ ref_frame_config->gld_fb_idx[0] = 0;
+ ref_frame_config->alt_fb_idx[0] = 0;
+ }
+ if (tl == 1) {
+ ref_frame_config->lst_fb_idx[0] = 0;
+ ref_frame_config->gld_fb_idx[0] = 1;
+ ref_frame_config->alt_fb_idx[0] = 2;
+
+ ref_frame_config->lst_fb_idx[1] = 1;
+ ref_frame_config->gld_fb_idx[1] = 2;
+ ref_frame_config->alt_fb_idx[1] = 3;
+ }
+ // Set the reference and update flags.
+ if (tl == 0) {
+ // Base spatial and base temporal (sl = 0, tl = 0)
+ ref_frame_config->reference_last[0] = 1;
+ ref_frame_config->reference_golden[0] = 0;
+ ref_frame_config->reference_alt_ref[0] = 0;
+ ref_frame_config->update_buffer_slot[0] |=
+ 1 << ref_frame_config->lst_fb_idx[0];
+
+ if (is_key_frame) {
+ ref_frame_config->reference_last[1] = 1;
+ ref_frame_config->reference_golden[1] = 0;
+ ref_frame_config->reference_alt_ref[1] = 0;
+ ref_frame_config->update_buffer_slot[1] |=
+ 1 << ref_frame_config->gld_fb_idx[1];
+ } else {
+ // Non-zero spatiall layer.
+ ref_frame_config->reference_last[1] = 1;
+ ref_frame_config->reference_golden[1] = 1;
+ ref_frame_config->reference_alt_ref[1] = 1;
+ ref_frame_config->update_buffer_slot[1] |=
+ 1 << ref_frame_config->lst_fb_idx[1];
+ }
+ }
+ if (tl == 1) {
+ // Top spatial and top temporal (non-reference -- doesn't update any
+ // reference buffers)
+ ref_frame_config->reference_last[1] = 1;
+ ref_frame_config->reference_golden[1] = 0;
+ ref_frame_config->reference_alt_ref[1] = 0;
+ }
+}
+
+#if CONFIG_VP9_DECODER && !SIMULCAST_MODE
+static void test_decode(vpx_codec_ctx_t *encoder, vpx_codec_ctx_t *decoder,
+ const int frames_out, int *mismatch_seen) {
+ vpx_image_t enc_img, dec_img;
+ struct vp9_ref_frame ref_enc, ref_dec;
+ if (*mismatch_seen) return;
+ /* Get the internal reference frame */
+ ref_enc.idx = 0;
+ ref_dec.idx = 0;
+ vpx_codec_control(encoder, VP9_GET_REFERENCE, &ref_enc);
+ enc_img = ref_enc.img;
+ vpx_codec_control(decoder, VP9_GET_REFERENCE, &ref_dec);
+ dec_img = ref_dec.img;
+#if CONFIG_VP9_HIGHBITDEPTH
+ if ((enc_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) !=
+ (dec_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH)) {
+ if (enc_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
+ vpx_img_alloc(&enc_img, enc_img.fmt - VPX_IMG_FMT_HIGHBITDEPTH,
+ enc_img.d_w, enc_img.d_h, 16);
+ vpx_img_truncate_16_to_8(&enc_img, &ref_enc.img);
+ }
+ if (dec_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
+ vpx_img_alloc(&dec_img, dec_img.fmt - VPX_IMG_FMT_HIGHBITDEPTH,
+ dec_img.d_w, dec_img.d_h, 16);
+ vpx_img_truncate_16_to_8(&dec_img, &ref_dec.img);
+ }
+ }
+#endif
+
+ if (!compare_img(&enc_img, &dec_img)) {
+ int y[4], u[4], v[4];
+#if CONFIG_VP9_HIGHBITDEPTH
+ if (enc_img.fmt & VPX_IMG_FMT_HIGHBITDEPTH) {
+ find_mismatch_high(&enc_img, &dec_img, y, u, v);
+ } else {
+ find_mismatch(&enc_img, &dec_img, y, u, v);
+ }
+#else
+ find_mismatch(&enc_img, &dec_img, y, u, v);
+#endif
+ decoder->err = 1;
+ printf(
+ "Encode/decode mismatch on frame %d at"
+ " Y[%d, %d] {%d/%d},"
+ " U[%d, %d] {%d/%d},"
+ " V[%d, %d] {%d/%d}\n",
+ frames_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
+ v[2], v[3]);
+ *mismatch_seen = frames_out;
+ }
+
+ vpx_img_free(&enc_img);
+ vpx_img_free(&dec_img);
+}
+#endif
+
+#if OUTPUT_RC_STATS
+static void svc_output_rc_stats(
+ vpx_codec_ctx_t *codec, vpx_codec_enc_cfg_t *enc_cfg,
+ vpx_svc_layer_id_t *layer_id, const vpx_codec_cx_pkt_t *cx_pkt,
+ struct RateControlStats *rc, VpxVideoWriter **outfile,
+ const uint32_t frame_cnt, const double framerate) {
+ int num_layers_encoded = 0;
+ unsigned int sl, tl;
+ uint64_t sizes[8];
+ uint64_t sizes_parsed[8];
+ int count = 0;
+ double sum_bitrate = 0.0;
+ double sum_bitrate2 = 0.0;
+ vp9_zero(sizes);
+ vp9_zero(sizes_parsed);
+ vpx_codec_control(codec, VP9E_GET_SVC_LAYER_ID, layer_id);
+ parse_superframe_index(cx_pkt->data.frame.buf, cx_pkt->data.frame.sz,
+ sizes_parsed, &count);
+ if (enc_cfg->ss_number_layers == 1) sizes[0] = cx_pkt->data.frame.sz;
+ for (sl = 0; sl < enc_cfg->ss_number_layers; ++sl) {
+ sizes[sl] = 0;
+ if (cx_pkt->data.frame.spatial_layer_encoded[sl]) {
+ sizes[sl] = sizes_parsed[num_layers_encoded];
+ num_layers_encoded++;
+ }
+ }
+ for (sl = 0; sl < enc_cfg->ss_number_layers; ++sl) {
+ unsigned int sl2;
+ uint64_t tot_size = 0;
+#if SIMULCAST_MODE
+ for (sl2 = 0; sl2 < sl; ++sl2) {
+ if (cx_pkt->data.frame.spatial_layer_encoded[sl2]) tot_size += sizes[sl2];
+ }
+ vpx_video_writer_write_frame(outfile[sl],
+ (uint8_t *)(cx_pkt->data.frame.buf) + tot_size,
+ (size_t)(sizes[sl]), cx_pkt->data.frame.pts);
+#else
+ for (sl2 = 0; sl2 <= sl; ++sl2) {
+ if (cx_pkt->data.frame.spatial_layer_encoded[sl2]) tot_size += sizes[sl2];
+ }
+ if (tot_size > 0)
+ vpx_video_writer_write_frame(outfile[sl], cx_pkt->data.frame.buf,
+ (size_t)(tot_size), cx_pkt->data.frame.pts);
+#endif // SIMULCAST_MODE
+ }
+ for (sl = 0; sl < enc_cfg->ss_number_layers; ++sl) {
+ if (cx_pkt->data.frame.spatial_layer_encoded[sl]) {
+ for (tl = layer_id->temporal_layer_id; tl < enc_cfg->ts_number_layers;
+ ++tl) {
+ const int layer = sl * enc_cfg->ts_number_layers + tl;
+ ++rc->layer_tot_enc_frames[layer];
+ rc->layer_encoding_bitrate[layer] += 8.0 * sizes[sl];
+ // Keep count of rate control stats per layer, for non-key
+ // frames.
+ if (tl == (unsigned int)layer_id->temporal_layer_id &&
+ !(cx_pkt->data.frame.flags & VPX_FRAME_IS_KEY)) {
+ rc->layer_avg_frame_size[layer] += 8.0 * sizes[sl];
+ rc->layer_avg_rate_mismatch[layer] +=
+ fabs(8.0 * sizes[sl] - rc->layer_pfb[layer]) /
+ rc->layer_pfb[layer];
+ ++rc->layer_enc_frames[layer];
+ }
+ }
+ }
+ }
+
+ // Update for short-time encoding bitrate states, for moving
+ // window of size rc->window, shifted by rc->window / 2.
+ // Ignore first window segment, due to key frame.
+ if (frame_cnt > (unsigned int)rc->window_size) {
+ for (sl = 0; sl < enc_cfg->ss_number_layers; ++sl) {
+ if (cx_pkt->data.frame.spatial_layer_encoded[sl])
+ sum_bitrate += 0.001 * 8.0 * sizes[sl] * framerate;
+ }
+ if (frame_cnt % rc->window_size == 0) {
+ rc->window_count += 1;
+ rc->avg_st_encoding_bitrate += sum_bitrate / rc->window_size;
+ rc->variance_st_encoding_bitrate +=
+ (sum_bitrate / rc->window_size) * (sum_bitrate / rc->window_size);
+ }
+ }
+
+ // Second shifted window.
+ if (frame_cnt > (unsigned int)(rc->window_size + rc->window_size / 2)) {
+ for (sl = 0; sl < enc_cfg->ss_number_layers; ++sl) {
+ sum_bitrate2 += 0.001 * 8.0 * sizes[sl] * framerate;
+ }
+
+ if (frame_cnt > (unsigned int)(2 * rc->window_size) &&
+ frame_cnt % rc->window_size == 0) {
+ rc->window_count += 1;
+ rc->avg_st_encoding_bitrate += sum_bitrate2 / rc->window_size;
+ rc->variance_st_encoding_bitrate +=
+ (sum_bitrate2 / rc->window_size) * (sum_bitrate2 / rc->window_size);
}
}
}
+#endif
int main(int argc, const char **argv) {
AppInput app_input;
VpxVideoWriter *writer = NULL;
VpxVideoInfo info;
- vpx_codec_ctx_t codec;
+ vpx_codec_ctx_t encoder;
vpx_codec_enc_cfg_t enc_cfg;
SvcContext svc_ctx;
+ vpx_svc_frame_drop_t svc_drop_frame;
uint32_t i;
uint32_t frame_cnt = 0;
vpx_image_t raw;
vpx_codec_err_t res;
int pts = 0; /* PTS starts at 0 */
int frame_duration = 1; /* 1 timebase tick per frame */
- FILE *infile = NULL;
int end_of_stream = 0;
int frames_received = 0;
#if OUTPUT_RC_STATS
- VpxVideoWriter *outfile[VPX_TS_MAX_LAYERS] = { NULL };
+ VpxVideoWriter *outfile[VPX_SS_MAX_LAYERS] = { NULL };
struct RateControlStats rc;
vpx_svc_layer_id_t layer_id;
vpx_svc_ref_frame_config_t ref_frame_config;
- unsigned int sl, tl;
- double sum_bitrate = 0.0;
- double sum_bitrate2 = 0.0;
+ unsigned int sl;
double framerate = 30.0;
#endif
struct vpx_usec_timer timer;
int64_t cx_time = 0;
+#if CONFIG_INTERNAL_STATS
+ FILE *f = fopen("opsnr.stt", "a");
+#endif
+#if CONFIG_VP9_DECODER && !SIMULCAST_MODE
+ int mismatch_seen = 0;
+ vpx_codec_ctx_t decoder;
+#endif
memset(&svc_ctx, 0, sizeof(svc_ctx));
- svc_ctx.log_print = 1;
+ memset(&app_input, 0, sizeof(AppInput));
+ memset(&info, 0, sizeof(VpxVideoInfo));
+ memset(&layer_id, 0, sizeof(vpx_svc_layer_id_t));
+ memset(&rc, 0, sizeof(struct RateControlStats));
exec_name = argv[0];
+
+ /* Setup default input stream settings */
+ app_input.input_ctx.framerate.numerator = 30;
+ app_input.input_ctx.framerate.denominator = 1;
+ app_input.input_ctx.only_i420 = 1;
+ app_input.input_ctx.bit_depth = 0;
+
parse_command_line(argc, argv, &app_input, &svc_ctx, &enc_cfg);
+ // Y4M reader handles its own allocation.
+ if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
// Allocate image buffer
#if CONFIG_VP9_HIGHBITDEPTH
- if (!vpx_img_alloc(&raw,
- enc_cfg.g_input_bit_depth == 8 ? VPX_IMG_FMT_I420
- : VPX_IMG_FMT_I42016,
- enc_cfg.g_w, enc_cfg.g_h, 32)) {
- die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
- }
+ if (!vpx_img_alloc(&raw,
+ enc_cfg.g_input_bit_depth == 8 ? VPX_IMG_FMT_I420
+ : VPX_IMG_FMT_I42016,
+ enc_cfg.g_w, enc_cfg.g_h, 32)) {
+ die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
+ }
#else
- if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32)) {
- die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
- }
+ if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, enc_cfg.g_w, enc_cfg.g_h, 32)) {
+ die("Failed to allocate image %dx%d\n", enc_cfg.g_w, enc_cfg.g_h);
+ }
#endif // CONFIG_VP9_HIGHBITDEPTH
-
- if (!(infile = fopen(app_input.input_filename, "rb")))
- die("Failed to open %s for reading\n", app_input.input_filename);
+ }
// Initialize codec
- if (vpx_svc_init(&svc_ctx, &codec, vpx_codec_vp9_cx(), &enc_cfg) !=
+ if (vpx_svc_init(&svc_ctx, &encoder, vpx_codec_vp9_cx(), &enc_cfg) !=
VPX_CODEC_OK)
die("Failed to initialize encoder\n");
+#if CONFIG_VP9_DECODER && !SIMULCAST_MODE
+ if (vpx_codec_dec_init(
+ &decoder, get_vpx_decoder_by_name("vp9")->codec_interface(), NULL, 0))
+ die("Failed to initialize decoder\n");
+#endif
#if OUTPUT_RC_STATS
+ rc.window_count = 1;
+ rc.window_size = 15; // Silence a static analysis warning.
+ rc.avg_st_encoding_bitrate = 0.0;
+ rc.variance_st_encoding_bitrate = 0.0;
if (svc_ctx.output_rc_stat) {
set_rate_control_stats(&rc, &enc_cfg);
framerate = enc_cfg.g_timebase.den / enc_cfg.g_timebase.num;
@@ -668,6 +996,8 @@ int main(int argc, const char **argv) {
#endif
info.codec_fourcc = VP9_FOURCC;
+ info.frame_width = enc_cfg.g_w;
+ info.frame_height = enc_cfg.g_h;
info.time_base.numerator = enc_cfg.g_timebase.num;
info.time_base.denominator = enc_cfg.g_timebase.den;
@@ -679,43 +1009,65 @@ int main(int argc, const char **argv) {
die("Failed to open %s for writing\n", app_input.output_filename);
}
#if OUTPUT_RC_STATS
- // For now, just write temporal layer streams.
- // TODO(marpan): do spatial by re-writing superframe.
+ // Write out spatial layer stream.
+ // TODO(marpan/jianj): allow for writing each spatial and temporal stream.
if (svc_ctx.output_rc_stat) {
- for (tl = 0; tl < enc_cfg.ts_number_layers; ++tl) {
+ for (sl = 0; sl < enc_cfg.ss_number_layers; ++sl) {
char file_name[PATH_MAX];
- snprintf(file_name, sizeof(file_name), "%s_t%d.ivf",
- app_input.output_filename, tl);
- outfile[tl] = vpx_video_writer_open(file_name, kContainerIVF, &info);
- if (!outfile[tl]) die("Failed to open %s for writing", file_name);
+ snprintf(file_name, sizeof(file_name), "%s_s%d.ivf",
+ app_input.output_filename, sl);
+ outfile[sl] = vpx_video_writer_open(file_name, kContainerIVF, &info);
+ if (!outfile[sl]) die("Failed to open %s for writing", file_name);
}
}
#endif
// skip initial frames
- for (i = 0; i < app_input.frames_to_skip; ++i) vpx_img_read(&raw, infile);
+ for (i = 0; i < app_input.frames_to_skip; ++i)
+ read_frame(&app_input.input_ctx, &raw);
if (svc_ctx.speed != -1)
- vpx_codec_control(&codec, VP8E_SET_CPUUSED, svc_ctx.speed);
+ vpx_codec_control(&encoder, VP8E_SET_CPUUSED, svc_ctx.speed);
if (svc_ctx.threads) {
- vpx_codec_control(&codec, VP9E_SET_TILE_COLUMNS, (svc_ctx.threads >> 1));
+ vpx_codec_control(&encoder, VP9E_SET_TILE_COLUMNS,
+ get_msb(svc_ctx.threads));
if (svc_ctx.threads > 1)
- vpx_codec_control(&codec, VP9E_SET_ROW_MT, 1);
+ vpx_codec_control(&encoder, VP9E_SET_ROW_MT, 1);
else
- vpx_codec_control(&codec, VP9E_SET_ROW_MT, 0);
+ vpx_codec_control(&encoder, VP9E_SET_ROW_MT, 0);
}
if (svc_ctx.speed >= 5 && svc_ctx.aqmode == 1)
- vpx_codec_control(&codec, VP9E_SET_AQ_MODE, 3);
+ vpx_codec_control(&encoder, VP9E_SET_AQ_MODE, 3);
if (svc_ctx.speed >= 5)
- vpx_codec_control(&codec, VP8E_SET_STATIC_THRESHOLD, 1);
- vpx_codec_control(&codec, VP8E_SET_MAX_INTRA_BITRATE_PCT, 900);
+ vpx_codec_control(&encoder, VP8E_SET_STATIC_THRESHOLD, 1);
+ vpx_codec_control(&encoder, VP8E_SET_MAX_INTRA_BITRATE_PCT, 900);
+
+ vpx_codec_control(&encoder, VP9E_SET_SVC_INTER_LAYER_PRED,
+ app_input.inter_layer_pred);
+
+ vpx_codec_control(&encoder, VP9E_SET_NOISE_SENSITIVITY, 0);
+
+ vpx_codec_control(&encoder, VP9E_SET_TUNE_CONTENT, app_input.tune_content);
+
+ svc_drop_frame.framedrop_mode = FULL_SUPERFRAME_DROP;
+ for (sl = 0; sl < (unsigned int)svc_ctx.spatial_layers; ++sl)
+ svc_drop_frame.framedrop_thresh[sl] = enc_cfg.rc_dropframe_thresh;
+ svc_drop_frame.max_consec_drop = INT_MAX;
+ vpx_codec_control(&encoder, VP9E_SET_SVC_FRAME_DROP_LAYER, &svc_drop_frame);
// Encode frames
while (!end_of_stream) {
vpx_codec_iter_t iter = NULL;
const vpx_codec_cx_pkt_t *cx_pkt;
- if (frame_cnt >= app_input.frames_to_code || !vpx_img_read(&raw, infile)) {
+ // Example patterns for bypass/flexible mode:
+ // example_pattern = 0: 2 temporal layers, and spatial_layers = 1,2,3. Exact
+ // to fixed SVC patterns. example_pattern = 1: 2 spatial and 2 temporal
+ // layers, with SL0 only has TL0, and SL1 has both TL0 and TL1. This example
+ // uses the extended API.
+ int example_pattern = 0;
+ if (frame_cnt >= app_input.frames_to_code ||
+ !read_frame(&app_input.input_ctx, &raw)) {
// We need one extra vpx_svc_encode call at end of stream to flush
// encoder and get remaining data
end_of_stream = 1;
@@ -723,140 +1075,97 @@ int main(int argc, const char **argv) {
// For BYPASS/FLEXIBLE mode, set the frame flags (reference and updates)
// and the buffer indices for each spatial layer of the current
- // (super)frame to be encoded. The temporal layer_id for the current frame
- // also needs to be set.
+ // (super)frame to be encoded. The spatial and temporal layer_id for the
+ // current frame also needs to be set.
// TODO(marpan): Should rename the "VP9E_TEMPORAL_LAYERING_MODE_BYPASS"
// mode to "VP9E_LAYERING_MODE_BYPASS".
if (svc_ctx.temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
layer_id.spatial_layer_id = 0;
// Example for 2 temporal layers.
- if (frame_cnt % 2 == 0)
+ if (frame_cnt % 2 == 0) {
layer_id.temporal_layer_id = 0;
- else
+ for (i = 0; i < VPX_SS_MAX_LAYERS; i++)
+ layer_id.temporal_layer_id_per_spatial[i] = 0;
+ } else {
layer_id.temporal_layer_id = 1;
- // Note that we only set the temporal layer_id, since we are calling
- // the encode for the whole superframe. The encoder will internally loop
- // over all the spatial layers for the current superframe.
- vpx_codec_control(&codec, VP9E_SET_SVC_LAYER_ID, &layer_id);
- set_frame_flags_bypass_mode(sl, layer_id.temporal_layer_id,
- svc_ctx.spatial_layers, frame_cnt == 0,
- &ref_frame_config);
- vpx_codec_control(&codec, VP9E_SET_SVC_REF_FRAME_CONFIG,
+ for (i = 0; i < VPX_SS_MAX_LAYERS; i++)
+ layer_id.temporal_layer_id_per_spatial[i] = 1;
+ }
+ if (example_pattern == 1) {
+ // example_pattern 1 is hard-coded for 2 spatial and 2 temporal layers.
+ assert(svc_ctx.spatial_layers == 2);
+ assert(svc_ctx.temporal_layers == 2);
+ if (frame_cnt % 2 == 0) {
+ // Spatial layer 0 and 1 are encoded.
+ layer_id.temporal_layer_id_per_spatial[0] = 0;
+ layer_id.temporal_layer_id_per_spatial[1] = 0;
+ layer_id.spatial_layer_id = 0;
+ } else {
+ // Only spatial layer 1 is encoded here.
+ layer_id.temporal_layer_id_per_spatial[1] = 1;
+ layer_id.spatial_layer_id = 1;
+ }
+ }
+ vpx_codec_control(&encoder, VP9E_SET_SVC_LAYER_ID, &layer_id);
+ // TODO(jianj): Fix the parameter passing for "is_key_frame" in
+ // set_frame_flags_bypass_model() for case of periodic key frames.
+ if (example_pattern == 0) {
+ set_frame_flags_bypass_mode_ex0(layer_id.temporal_layer_id,
+ svc_ctx.spatial_layers, frame_cnt == 0,
+ &ref_frame_config);
+ } else if (example_pattern == 1) {
+ set_frame_flags_bypass_mode_ex1(layer_id.temporal_layer_id,
+ svc_ctx.spatial_layers, frame_cnt == 0,
+ &ref_frame_config);
+ }
+ ref_frame_config.duration[0] = frame_duration * 1;
+ ref_frame_config.duration[1] = frame_duration * 1;
+
+ vpx_codec_control(&encoder, VP9E_SET_SVC_REF_FRAME_CONFIG,
&ref_frame_config);
// Keep track of input frames, to account for frame drops in rate control
// stats/metrics.
- for (sl = 0; sl < (unsigned int)enc_cfg.ss_number_layers; ++sl) {
+ for (sl = 0; sl < enc_cfg.ss_number_layers; ++sl) {
++rc.layer_input_frames[sl * enc_cfg.ts_number_layers +
layer_id.temporal_layer_id];
}
+ } else {
+ // For the fixed pattern SVC, temporal layer is given by superframe count.
+ unsigned int tl = 0;
+ if (enc_cfg.ts_number_layers == 2)
+ tl = (frame_cnt % 2 != 0);
+ else if (enc_cfg.ts_number_layers == 3) {
+ if (frame_cnt % 2 != 0) tl = 2;
+ if ((frame_cnt > 1) && ((frame_cnt - 2) % 4 == 0)) tl = 1;
+ }
+ for (sl = 0; sl < enc_cfg.ss_number_layers; ++sl)
+ ++rc.layer_input_frames[sl * enc_cfg.ts_number_layers + tl];
}
vpx_usec_timer_start(&timer);
res = vpx_svc_encode(
- &svc_ctx, &codec, (end_of_stream ? NULL : &raw), pts, frame_duration,
+ &svc_ctx, &encoder, (end_of_stream ? NULL : &raw), pts, frame_duration,
svc_ctx.speed >= 5 ? VPX_DL_REALTIME : VPX_DL_GOOD_QUALITY);
vpx_usec_timer_mark(&timer);
cx_time += vpx_usec_timer_elapsed(&timer);
- printf("%s", vpx_svc_get_message(&svc_ctx));
fflush(stdout);
if (res != VPX_CODEC_OK) {
- die_codec(&codec, "Failed to encode frame");
+ die_codec(&encoder, "Failed to encode frame");
}
- while ((cx_pkt = vpx_codec_get_cx_data(&codec, &iter)) != NULL) {
+ while ((cx_pkt = vpx_codec_get_cx_data(&encoder, &iter)) != NULL) {
switch (cx_pkt->kind) {
case VPX_CODEC_CX_FRAME_PKT: {
SvcInternal_t *const si = (SvcInternal_t *)svc_ctx.internal;
if (cx_pkt->data.frame.sz > 0) {
-#if OUTPUT_RC_STATS
- uint64_t sizes[8];
- int count = 0;
-#endif
vpx_video_writer_write_frame(writer, cx_pkt->data.frame.buf,
cx_pkt->data.frame.sz,
cx_pkt->data.frame.pts);
#if OUTPUT_RC_STATS
- // TODO(marpan): Put this (to line728) in separate function.
if (svc_ctx.output_rc_stat) {
- vpx_codec_control(&codec, VP9E_GET_SVC_LAYER_ID, &layer_id);
- parse_superframe_index(cx_pkt->data.frame.buf,
- cx_pkt->data.frame.sz, sizes, &count);
- if (enc_cfg.ss_number_layers == 1)
- sizes[0] = cx_pkt->data.frame.sz;
- // Note computing input_layer_frames here won't account for frame
- // drops in rate control stats.
- // TODO(marpan): Fix this for non-bypass mode so we can get stats
- // for dropped frames.
- if (svc_ctx.temporal_layering_mode !=
- VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
- for (sl = 0; sl < enc_cfg.ss_number_layers; ++sl) {
- ++rc.layer_input_frames[sl * enc_cfg.ts_number_layers +
- layer_id.temporal_layer_id];
- }
- }
- for (tl = layer_id.temporal_layer_id;
- tl < enc_cfg.ts_number_layers; ++tl) {
- vpx_video_writer_write_frame(
- outfile[tl], cx_pkt->data.frame.buf, cx_pkt->data.frame.sz,
- cx_pkt->data.frame.pts);
- }
-
- for (sl = 0; sl < enc_cfg.ss_number_layers; ++sl) {
- for (tl = layer_id.temporal_layer_id;
- tl < enc_cfg.ts_number_layers; ++tl) {
- const int layer = sl * enc_cfg.ts_number_layers + tl;
- ++rc.layer_tot_enc_frames[layer];
- rc.layer_encoding_bitrate[layer] += 8.0 * sizes[sl];
- // Keep count of rate control stats per layer, for non-key
- // frames.
- if (tl == (unsigned int)layer_id.temporal_layer_id &&
- !(cx_pkt->data.frame.flags & VPX_FRAME_IS_KEY)) {
- rc.layer_avg_frame_size[layer] += 8.0 * sizes[sl];
- rc.layer_avg_rate_mismatch[layer] +=
- fabs(8.0 * sizes[sl] - rc.layer_pfb[layer]) /
- rc.layer_pfb[layer];
- ++rc.layer_enc_frames[layer];
- }
- }
- }
-
- // Update for short-time encoding bitrate states, for moving
- // window of size rc->window, shifted by rc->window / 2.
- // Ignore first window segment, due to key frame.
- if (frame_cnt > (unsigned int)rc.window_size) {
- tl = layer_id.temporal_layer_id;
- for (sl = 0; sl < enc_cfg.ss_number_layers; ++sl) {
- sum_bitrate += 0.001 * 8.0 * sizes[sl] * framerate;
- }
- if (frame_cnt % rc.window_size == 0) {
- rc.window_count += 1;
- rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
- rc.variance_st_encoding_bitrate +=
- (sum_bitrate / rc.window_size) *
- (sum_bitrate / rc.window_size);
- sum_bitrate = 0.0;
- }
- }
-
- // Second shifted window.
- if (frame_cnt >
- (unsigned int)(rc.window_size + rc.window_size / 2)) {
- tl = layer_id.temporal_layer_id;
- for (sl = 0; sl < enc_cfg.ss_number_layers; ++sl) {
- sum_bitrate2 += 0.001 * 8.0 * sizes[sl] * framerate;
- }
-
- if (frame_cnt > (unsigned int)(2 * rc.window_size) &&
- frame_cnt % rc.window_size == 0) {
- rc.window_count += 1;
- rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
- rc.variance_st_encoding_bitrate +=
- (sum_bitrate2 / rc.window_size) *
- (sum_bitrate2 / rc.window_size);
- sum_bitrate2 = 0.0;
- }
- }
+ svc_output_rc_stats(&encoder, &enc_cfg, &layer_id, cx_pkt, &rc,
+ outfile, frame_cnt, framerate);
}
#endif
}
@@ -868,6 +1177,11 @@ int main(int argc, const char **argv) {
if (enc_cfg.ss_number_layers == 1 && enc_cfg.ts_number_layers == 1)
si->bytes_sum[0] += (int)cx_pkt->data.frame.sz;
++frames_received;
+#if CONFIG_VP9_DECODER && !SIMULCAST_MODE
+ if (vpx_codec_decode(&decoder, cx_pkt->data.frame.buf,
+ (unsigned int)cx_pkt->data.frame.sz, NULL, 0))
+ die_codec(&decoder, "Failed to decode frame.");
+#endif
break;
}
case VPX_CODEC_STATS_PKT: {
@@ -877,6 +1191,19 @@ int main(int argc, const char **argv) {
}
default: { break; }
}
+
+#if CONFIG_VP9_DECODER && !SIMULCAST_MODE
+ vpx_codec_control(&encoder, VP9E_GET_SVC_LAYER_ID, &layer_id);
+ // Don't look for mismatch on top spatial and top temporal layers as they
+ // are non reference frames.
+ if ((enc_cfg.ss_number_layers > 1 || enc_cfg.ts_number_layers > 1) &&
+ !(layer_id.temporal_layer_id > 0 &&
+ layer_id.temporal_layer_id == (int)enc_cfg.ts_number_layers - 1 &&
+ cx_pkt->data.frame
+ .spatial_layer_encoded[enc_cfg.ss_number_layers - 1])) {
+ test_decode(&encoder, &decoder, frame_cnt, &mismatch_seen);
+ }
+#endif
}
if (!end_of_stream) {
@@ -885,41 +1212,45 @@ int main(int argc, const char **argv) {
}
}
- // Compensate for the extra frame count for the bypass mode.
- if (svc_ctx.temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS) {
- for (sl = 0; sl < enc_cfg.ss_number_layers; ++sl) {
- const int layer =
- sl * enc_cfg.ts_number_layers + layer_id.temporal_layer_id;
- --rc.layer_input_frames[layer];
- }
- }
-
printf("Processed %d frames\n", frame_cnt);
- fclose(infile);
+
+ close_input_file(&app_input.input_ctx);
+
#if OUTPUT_RC_STATS
if (svc_ctx.output_rc_stat) {
printout_rate_control_summary(&rc, &enc_cfg, frame_cnt);
printf("\n");
}
#endif
- if (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
+ if (vpx_codec_destroy(&encoder))
+ die_codec(&encoder, "Failed to destroy codec");
if (app_input.passes == 2) stats_close(&app_input.rc_stats, 1);
if (writer) {
vpx_video_writer_close(writer);
}
#if OUTPUT_RC_STATS
if (svc_ctx.output_rc_stat) {
- for (tl = 0; tl < enc_cfg.ts_number_layers; ++tl) {
- vpx_video_writer_close(outfile[tl]);
+ for (sl = 0; sl < enc_cfg.ss_number_layers; ++sl) {
+ vpx_video_writer_close(outfile[sl]);
}
}
+#endif
+#if CONFIG_INTERNAL_STATS
+ if (mismatch_seen) {
+ fprintf(f, "First mismatch occurred in frame %d\n", mismatch_seen);
+ } else {
+ fprintf(f, "No mismatch detected in recon buffers\n");
+ }
+ fclose(f);
#endif
printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
1000000 * (double)frame_cnt / (double)cx_time);
- vpx_img_free(&raw);
+ if (app_input.input_ctx.file_type != FILE_TYPE_Y4M) {
+ vpx_img_free(&raw);
+ }
// display average size, psnr
- printf("%s", vpx_svc_dump_statistics(&svc_ctx));
+ vpx_svc_dump_statistics(&svc_ctx);
vpx_svc_release(&svc_ctx);
return EXIT_SUCCESS;
}
diff --git a/libs/libvpx/examples/vp9cx_set_ref.c b/libs/libvpx/examples/vp9cx_set_ref.c
index 3472689db2f..911ad38630c 100644
--- a/libs/libvpx/examples/vp9cx_set_ref.c
+++ b/libs/libvpx/examples/vp9cx_set_ref.c
@@ -68,128 +68,6 @@ void usage_exit() {
exit(EXIT_FAILURE);
}
-static int compare_img(const vpx_image_t *const img1,
- const vpx_image_t *const img2) {
- uint32_t l_w = img1->d_w;
- uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
- const uint32_t c_h =
- (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
- uint32_t i;
- int match = 1;
-
- match &= (img1->fmt == img2->fmt);
- match &= (img1->d_w == img2->d_w);
- match &= (img1->d_h == img2->d_h);
-
- for (i = 0; i < img1->d_h; ++i)
- match &= (memcmp(img1->planes[VPX_PLANE_Y] + i * img1->stride[VPX_PLANE_Y],
- img2->planes[VPX_PLANE_Y] + i * img2->stride[VPX_PLANE_Y],
- l_w) == 0);
-
- for (i = 0; i < c_h; ++i)
- match &= (memcmp(img1->planes[VPX_PLANE_U] + i * img1->stride[VPX_PLANE_U],
- img2->planes[VPX_PLANE_U] + i * img2->stride[VPX_PLANE_U],
- c_w) == 0);
-
- for (i = 0; i < c_h; ++i)
- match &= (memcmp(img1->planes[VPX_PLANE_V] + i * img1->stride[VPX_PLANE_V],
- img2->planes[VPX_PLANE_V] + i * img2->stride[VPX_PLANE_V],
- c_w) == 0);
-
- return match;
-}
-
-#define mmin(a, b) ((a) < (b) ? (a) : (b))
-static void find_mismatch(const vpx_image_t *const img1,
- const vpx_image_t *const img2, int yloc[4],
- int uloc[4], int vloc[4]) {
- const uint32_t bsize = 64;
- const uint32_t bsizey = bsize >> img1->y_chroma_shift;
- const uint32_t bsizex = bsize >> img1->x_chroma_shift;
- const uint32_t c_w =
- (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
- const uint32_t c_h =
- (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
- int match = 1;
- uint32_t i, j;
- yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
- for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
- for (j = 0; match && j < img1->d_w; j += bsize) {
- int k, l;
- const int si = mmin(i + bsize, img1->d_h) - i;
- const int sj = mmin(j + bsize, img1->d_w) - j;
- for (k = 0; match && k < si; ++k) {
- for (l = 0; match && l < sj; ++l) {
- if (*(img1->planes[VPX_PLANE_Y] +
- (i + k) * img1->stride[VPX_PLANE_Y] + j + l) !=
- *(img2->planes[VPX_PLANE_Y] +
- (i + k) * img2->stride[VPX_PLANE_Y] + j + l)) {
- yloc[0] = i + k;
- yloc[1] = j + l;
- yloc[2] = *(img1->planes[VPX_PLANE_Y] +
- (i + k) * img1->stride[VPX_PLANE_Y] + j + l);
- yloc[3] = *(img2->planes[VPX_PLANE_Y] +
- (i + k) * img2->stride[VPX_PLANE_Y] + j + l);
- match = 0;
- break;
- }
- }
- }
- }
- }
-
- uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
- for (i = 0, match = 1; match && i < c_h; i += bsizey) {
- for (j = 0; match && j < c_w; j += bsizex) {
- int k, l;
- const int si = mmin(i + bsizey, c_h - i);
- const int sj = mmin(j + bsizex, c_w - j);
- for (k = 0; match && k < si; ++k) {
- for (l = 0; match && l < sj; ++l) {
- if (*(img1->planes[VPX_PLANE_U] +
- (i + k) * img1->stride[VPX_PLANE_U] + j + l) !=
- *(img2->planes[VPX_PLANE_U] +
- (i + k) * img2->stride[VPX_PLANE_U] + j + l)) {
- uloc[0] = i + k;
- uloc[1] = j + l;
- uloc[2] = *(img1->planes[VPX_PLANE_U] +
- (i + k) * img1->stride[VPX_PLANE_U] + j + l);
- uloc[3] = *(img2->planes[VPX_PLANE_U] +
- (i + k) * img2->stride[VPX_PLANE_U] + j + l);
- match = 0;
- break;
- }
- }
- }
- }
- }
- vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
- for (i = 0, match = 1; match && i < c_h; i += bsizey) {
- for (j = 0; match && j < c_w; j += bsizex) {
- int k, l;
- const int si = mmin(i + bsizey, c_h - i);
- const int sj = mmin(j + bsizex, c_w - j);
- for (k = 0; match && k < si; ++k) {
- for (l = 0; match && l < sj; ++l) {
- if (*(img1->planes[VPX_PLANE_V] +
- (i + k) * img1->stride[VPX_PLANE_V] + j + l) !=
- *(img2->planes[VPX_PLANE_V] +
- (i + k) * img2->stride[VPX_PLANE_V] + j + l)) {
- vloc[0] = i + k;
- vloc[1] = j + l;
- vloc[2] = *(img1->planes[VPX_PLANE_V] +
- (i + k) * img1->stride[VPX_PLANE_V] + j + l);
- vloc[3] = *(img2->planes[VPX_PLANE_V] +
- (i + k) * img2->stride[VPX_PLANE_V] + j + l);
- match = 0;
- break;
- }
- }
- }
- }
- }
-}
-
static void testing_decode(vpx_codec_ctx_t *encoder, vpx_codec_ctx_t *decoder,
unsigned int frame_out, int *mismatch_seen) {
vpx_image_t enc_img, dec_img;
diff --git a/libs/libvpx/examples/vpx_dec_fuzzer.cc b/libs/libvpx/examples/vpx_dec_fuzzer.cc
new file mode 100644
index 00000000000..d55fe1571be
--- /dev/null
+++ b/libs/libvpx/examples/vpx_dec_fuzzer.cc
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2018 The WebM project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+/*
+ * Fuzzer for libvpx decoders
+ * ==========================
+ * Requirements
+ * --------------
+ * Requires Clang 6.0 or above as -fsanitize=fuzzer is used as a linker
+ * option.
+
+ * Steps to build
+ * --------------
+ * Clone libvpx repository
+ $git clone https://chromium.googlesource.com/webm/libvpx
+
+ * Create a directory in parallel to libvpx and change directory
+ $mkdir vpx_dec_fuzzer
+ $cd vpx_dec_fuzzer/
+
+ * Enable sanitizers (Supported: address integer memory thread undefined)
+ $source ../libvpx/tools/set_analyzer_env.sh address
+
+ * Configure libvpx.
+ * Note --size-limit and VPX_MAX_ALLOCABLE_MEMORY are defined to avoid
+ * Out of memory errors when running generated fuzzer binary
+ $../libvpx/configure --disable-unit-tests --size-limit=12288x12288 \
+ --extra-cflags="-fsanitize=fuzzer-no-link \
+ -DVPX_MAX_ALLOCABLE_MEMORY=1073741824" \
+ --disable-webm-io --enable-debug --disable-vp8-encoder \
+ --disable-vp9-encoder --disable-examples
+
+ * Build libvpx
+ $make -j32
+
+ * Build vp9 fuzzer
+ $ $CXX $CXXFLAGS -std=c++11 -DDECODER=vp9 \
+ -fsanitize=fuzzer -I../libvpx -I. -Wl,--start-group \
+ ../libvpx/examples/vpx_dec_fuzzer.cc -o ./vpx_dec_fuzzer_vp9 \
+ ./libvpx.a -Wl,--end-group
+
+ * DECODER should be defined as vp9 or vp8 to enable vp9/vp8
+ *
+ * create a corpus directory and copy some ivf files there.
+ * Based on which codec (vp8/vp9) is being tested, it is recommended to
+ * have corresponding ivf files in corpus directory
+ * Empty corpus directoy also is acceptable, though not recommended
+ $mkdir CORPUS && cp some-files CORPUS
+
+ * Run fuzzing:
+ $./vpx_dec_fuzzer_vp9 CORPUS
+
+ * References:
+ * http://llvm.org/docs/LibFuzzer.html
+ * https://github.com/google/oss-fuzz
+ */
+
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "vpx/vp8dx.h"
+#include "vpx/vpx_decoder.h"
+#include "vpx_ports/mem_ops.h"
+
+#define IVF_FRAME_HDR_SZ (4 + 8) /* 4 byte size + 8 byte timestamp */
+#define IVF_FILE_HDR_SZ 32
+
+#define VPXD_INTERFACE(name) VPXD_INTERFACE_(name)
+#define VPXD_INTERFACE_(name) vpx_codec_##name##_dx()
+
+extern "C" void usage_exit(void) { exit(EXIT_FAILURE); }
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
+ if (size <= IVF_FILE_HDR_SZ) {
+ return 0;
+ }
+
+ vpx_codec_ctx_t codec;
+ // Set thread count in the range [1, 64].
+ const unsigned int threads = (data[IVF_FILE_HDR_SZ] & 0x3f) + 1;
+ vpx_codec_dec_cfg_t cfg = { threads, 0, 0 };
+ if (vpx_codec_dec_init(&codec, VPXD_INTERFACE(DECODER), &cfg, 0)) {
+ return 0;
+ }
+
+ data += IVF_FILE_HDR_SZ;
+ size -= IVF_FILE_HDR_SZ;
+
+ while (size > IVF_FRAME_HDR_SZ) {
+ size_t frame_size = mem_get_le32(data);
+ size -= IVF_FRAME_HDR_SZ;
+ data += IVF_FRAME_HDR_SZ;
+ frame_size = std::min(size, frame_size);
+
+ const vpx_codec_err_t err =
+ vpx_codec_decode(&codec, data, frame_size, nullptr, 0);
+ static_cast(err);
+ vpx_codec_iter_t iter = nullptr;
+ vpx_image_t *img = nullptr;
+ while ((img = vpx_codec_get_frame(&codec, &iter)) != nullptr) {
+ }
+ data += frame_size;
+ size -= frame_size;
+ }
+ vpx_codec_destroy(&codec);
+ return 0;
+}
diff --git a/libs/libvpx/examples/vpx_temporal_svc_encoder.c b/libs/libvpx/examples/vpx_temporal_svc_encoder.c
index f5736ea45d2..6afbee83d22 100644
--- a/libs/libvpx/examples/vpx_temporal_svc_encoder.c
+++ b/libs/libvpx/examples/vpx_temporal_svc_encoder.c
@@ -19,14 +19,18 @@
#include
#include "./vpx_config.h"
+#include "./y4minput.h"
#include "../vpx_ports/vpx_timer.h"
#include "vpx/vp8cx.h"
#include "vpx/vpx_encoder.h"
+#include "vpx_ports/bitops.h"
#include "../tools_common.h"
#include "../video_writer.h"
-#define VP8_ROI_MAP 0
+#define ROI_MAP 0
+
+#define zero(Dest) memset(&(Dest), 0, sizeof(Dest));
static const char *exec_name;
@@ -89,19 +93,21 @@ struct RateControlMetrics {
// in the stream.
static void set_rate_control_metrics(struct RateControlMetrics *rc,
vpx_codec_enc_cfg_t *cfg) {
- unsigned int i = 0;
+ int i = 0;
// Set the layer (cumulative) framerate and the target layer (non-cumulative)
// per-frame-bandwidth, for the rate control encoding stats below.
const double framerate = cfg->g_timebase.den / cfg->g_timebase.num;
+ const int ts_number_layers = cfg->ts_number_layers;
rc->layer_framerate[0] = framerate / cfg->ts_rate_decimator[0];
rc->layer_pfb[0] =
1000.0 * rc->layer_target_bitrate[0] / rc->layer_framerate[0];
- for (i = 0; i < cfg->ts_number_layers; ++i) {
+ for (i = 0; i < ts_number_layers; ++i) {
if (i > 0) {
rc->layer_framerate[i] = framerate / cfg->ts_rate_decimator[i];
- rc->layer_pfb[i] = 1000.0 * (rc->layer_target_bitrate[i] -
- rc->layer_target_bitrate[i - 1]) /
- (rc->layer_framerate[i] - rc->layer_framerate[i - 1]);
+ rc->layer_pfb[i] =
+ 1000.0 *
+ (rc->layer_target_bitrate[i] - rc->layer_target_bitrate[i - 1]) /
+ (rc->layer_framerate[i] - rc->layer_framerate[i - 1]);
}
rc->layer_input_frames[i] = 0;
rc->layer_enc_frames[i] = 0;
@@ -114,6 +120,9 @@ static void set_rate_control_metrics(struct RateControlMetrics *rc,
rc->window_size = 15;
rc->avg_st_encoding_bitrate = 0.0;
rc->variance_st_encoding_bitrate = 0.0;
+ // Target bandwidth for the whole stream.
+ // Set to layer_target_bitrate for highest layer (total bitrate).
+ cfg->rc_target_bitrate = rc->layer_target_bitrate[ts_number_layers - 1];
}
static void printout_rate_control_summary(struct RateControlMetrics *rc,
@@ -164,38 +173,60 @@ static void printout_rate_control_summary(struct RateControlMetrics *rc,
die("Error: Number of input frames not equal to output! \n");
}
-#if VP8_ROI_MAP
-static void vp8_set_roi_map(vpx_codec_enc_cfg_t *cfg, vpx_roi_map_t *roi) {
+#if ROI_MAP
+static void set_roi_map(const char *enc_name, vpx_codec_enc_cfg_t *cfg,
+ vpx_roi_map_t *roi) {
unsigned int i, j;
- memset(roi, 0, sizeof(*roi));
+ int block_size = 0;
+ uint8_t is_vp8 = strncmp(enc_name, "vp8", 3) == 0 ? 1 : 0;
+ uint8_t is_vp9 = strncmp(enc_name, "vp9", 3) == 0 ? 1 : 0;
+ if (!is_vp8 && !is_vp9) {
+ die("unsupported codec.");
+ }
+ zero(*roi);
+
+ block_size = is_vp9 && !is_vp8 ? 8 : 16;
// ROI is based on the segments (4 for vp8, 8 for vp9), smallest unit for
// segment is 16x16 for vp8, 8x8 for vp9.
- roi->rows = (cfg->g_h + 15) / 16;
- roi->cols = (cfg->g_w + 15) / 16;
+ roi->rows = (cfg->g_h + block_size - 1) / block_size;
+ roi->cols = (cfg->g_w + block_size - 1) / block_size;
// Applies delta QP on the segment blocks, varies from -63 to 63.
// Setting to negative means lower QP (better quality).
// Below we set delta_q to the extreme (-63) to show strong effect.
- roi->delta_q[0] = 0;
+ // VP8 uses the first 4 segments. VP9 uses all 8 segments.
+ zero(roi->delta_q);
roi->delta_q[1] = -63;
- roi->delta_q[2] = 0;
- roi->delta_q[3] = 0;
// Applies delta loopfilter strength on the segment blocks, varies from -63 to
- // 63. Setting to positive means stronger loopfilter.
- roi->delta_lf[0] = 0;
- roi->delta_lf[1] = 0;
- roi->delta_lf[2] = 0;
- roi->delta_lf[3] = 0;
-
- // Applies skip encoding threshold on the segment blocks, varies from 0 to
- // UINT_MAX. Larger value means more skipping of encoding is possible.
- // This skip threshold only applies on delta frames.
- roi->static_threshold[0] = 0;
- roi->static_threshold[1] = 0;
- roi->static_threshold[2] = 0;
- roi->static_threshold[3] = 0;
+ // 63. Setting to positive means stronger loopfilter. VP8 uses the first 4
+ // segments. VP9 uses all 8 segments.
+ zero(roi->delta_lf);
+
+ if (is_vp8) {
+ // Applies skip encoding threshold on the segment blocks, varies from 0 to
+ // UINT_MAX. Larger value means more skipping of encoding is possible.
+ // This skip threshold only applies on delta frames.
+ zero(roi->static_threshold);
+ }
+
+ if (is_vp9) {
+ // Apply skip segment. Setting to 1 means this block will be copied from
+ // previous frame.
+ zero(roi->skip);
+ }
+
+ if (is_vp9) {
+ // Apply ref frame segment.
+ // -1 : Do not apply this segment.
+ // 0 : Froce using intra.
+ // 1 : Force using last.
+ // 2 : Force using golden.
+ // 3 : Force using alfref but not used in non-rd pickmode for 0 lag.
+ memset(roi->ref_frame, -1, sizeof(roi->ref_frame));
+ roi->ref_frame[1] = 1;
+ }
// Use 2 states: 1 is center square, 0 is the rest.
roi->roi_map =
@@ -563,12 +594,12 @@ int main(int argc, char **argv) {
int layering_mode = 0;
int layer_flags[VPX_TS_MAX_PERIODICITY] = { 0 };
int flag_periodicity = 1;
-#if VP8_ROI_MAP
+#if ROI_MAP
vpx_roi_map_t roi;
#endif
- vpx_svc_layer_id_t layer_id = { 0, 0 };
+ vpx_svc_layer_id_t layer_id;
const VpxInterface *encoder = NULL;
- FILE *infile = NULL;
+ struct VpxInputContext input_ctx;
struct RateControlMetrics rc;
int64_t cx_time = 0;
const int min_args_base = 13;
@@ -583,6 +614,15 @@ int main(int argc, char **argv) {
double sum_bitrate2 = 0.0;
double framerate = 30.0;
+ zero(rc.layer_target_bitrate);
+ memset(&layer_id, 0, sizeof(vpx_svc_layer_id_t));
+ memset(&input_ctx, 0, sizeof(input_ctx));
+ /* Setup default input stream settings */
+ input_ctx.framerate.numerator = 30;
+ input_ctx.framerate.denominator = 1;
+ input_ctx.only_i420 = 1;
+ input_ctx.bit_depth = 0;
+
exec_name = argv[0];
// Check usage and arguments.
if (argc < min_args) {
@@ -621,6 +661,9 @@ int main(int argc, char **argv) {
die("Invalid number of arguments");
}
+ input_ctx.filename = argv[1];
+ open_input_file(&input_ctx);
+
#if CONFIG_VP9_HIGHBITDEPTH
switch (strtol(argv[argc - 1], NULL, 0)) {
case 8:
@@ -637,14 +680,22 @@ int main(int argc, char **argv) {
break;
default: die("Invalid bit depth (8, 10, 12) %s", argv[argc - 1]);
}
- if (!vpx_img_alloc(
- &raw, bit_depth == VPX_BITS_8 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_I42016,
- width, height, 32)) {
- die("Failed to allocate image", width, height);
+
+ // Y4M reader has its own allocation.
+ if (input_ctx.file_type != FILE_TYPE_Y4M) {
+ if (!vpx_img_alloc(
+ &raw,
+ bit_depth == VPX_BITS_8 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_I42016,
+ width, height, 32)) {
+ die("Failed to allocate image", width, height);
+ }
}
#else
- if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 32)) {
- die("Failed to allocate image", width, height);
+ // Y4M reader has its own allocation.
+ if (input_ctx.file_type != FILE_TYPE_Y4M) {
+ if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 32)) {
+ die("Failed to allocate image", width, height);
+ }
}
#endif // CONFIG_VP9_HIGHBITDEPTH
@@ -675,6 +726,9 @@ int main(int argc, char **argv) {
if (speed < 0) {
die("Invalid speed setting: must be positive");
}
+ if (strncmp(encoder->name, "vp9", 3) == 0 && speed > 9) {
+ warn("Mapping speed %d to speed 9.\n", speed);
+ }
for (i = min_args_base;
(int)i < min_args_base + mode_to_num_layers[layering_mode]; ++i) {
@@ -722,13 +776,15 @@ int main(int argc, char **argv) {
set_rate_control_metrics(&rc, &cfg);
- // Target bandwidth for the whole stream.
- // Set to layer_target_bitrate for highest layer (total bitrate).
- cfg.rc_target_bitrate = rc.layer_target_bitrate[cfg.ts_number_layers - 1];
-
- // Open input file.
- if (!(infile = fopen(argv[1], "rb"))) {
- die("Failed to open %s for reading", argv[1]);
+ if (input_ctx.file_type == FILE_TYPE_Y4M) {
+ if (input_ctx.width != cfg.g_w || input_ctx.height != cfg.g_h) {
+ die("Incorrect width or height: %d x %d", cfg.g_w, cfg.g_h);
+ }
+ if (input_ctx.framerate.numerator != cfg.g_timebase.den ||
+ input_ctx.framerate.denominator != cfg.g_timebase.num) {
+ die("Incorrect framerate: numerator %d denominator %d",
+ cfg.g_timebase.num, cfg.g_timebase.den);
+ }
}
framerate = cfg.g_timebase.den / cfg.g_timebase.num;
@@ -766,8 +822,8 @@ int main(int argc, char **argv) {
vpx_codec_control(&codec, VP8E_SET_NOISE_SENSITIVITY, kVp8DenoiserOff);
vpx_codec_control(&codec, VP8E_SET_STATIC_THRESHOLD, 1);
vpx_codec_control(&codec, VP8E_SET_GF_CBR_BOOST_PCT, 0);
-#if VP8_ROI_MAP
- vp8_set_roi_map(&cfg, &roi);
+#if ROI_MAP
+ set_roi_map(encoder->name, &cfg, &roi);
if (vpx_codec_control(&codec, VP8E_SET_ROI_MAP, &roi))
die_codec(&codec, "Failed to set ROI map");
#endif
@@ -783,7 +839,13 @@ int main(int argc, char **argv) {
vpx_codec_control(&codec, VP9E_SET_NOISE_SENSITIVITY, kVp9DenoiserOff);
vpx_codec_control(&codec, VP8E_SET_STATIC_THRESHOLD, 1);
vpx_codec_control(&codec, VP9E_SET_TUNE_CONTENT, 0);
- vpx_codec_control(&codec, VP9E_SET_TILE_COLUMNS, (cfg.g_threads >> 1));
+ vpx_codec_control(&codec, VP9E_SET_TILE_COLUMNS, get_msb(cfg.g_threads));
+#if ROI_MAP
+ set_roi_map(encoder->name, &cfg, &roi);
+ if (vpx_codec_control(&codec, VP9E_SET_ROI_MAP, &roi))
+ die_codec(&codec, "Failed to set ROI map");
+ vpx_codec_control(&codec, VP9E_SET_AQ_MODE, 0);
+#endif
// TODO(marpan/jianj): There is an issue with row-mt for low resolutons at
// high speed settings, disable its use for those cases for now.
if (cfg.g_threads > 1 && ((cfg.g_w > 320 && cfg.g_h > 240) || speed < 7))
@@ -822,6 +884,7 @@ int main(int argc, char **argv) {
layer_id.spatial_layer_id = 0;
layer_id.temporal_layer_id =
cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
+ layer_id.temporal_layer_id_per_spatial[0] = layer_id.temporal_layer_id;
if (strncmp(encoder->name, "vp9", 3) == 0) {
vpx_codec_control(&codec, VP9E_SET_SVC_LAYER_ID, &layer_id);
} else if (strncmp(encoder->name, "vp8", 3) == 0) {
@@ -830,7 +893,7 @@ int main(int argc, char **argv) {
}
flags = layer_flags[frame_cnt % flag_periodicity];
if (layering_mode == 0) flags = 0;
- frame_avail = vpx_img_read(&raw, infile);
+ frame_avail = read_frame(&input_ctx, &raw);
if (frame_avail) ++rc.layer_input_frames[layer_id.temporal_layer_id];
vpx_usec_timer_start(&timer);
if (vpx_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags,
@@ -898,7 +961,7 @@ int main(int argc, char **argv) {
++frame_cnt;
pts += frame_duration;
}
- fclose(infile);
+ close_input_file(&input_ctx);
printout_rate_control_summary(&rc, &cfg, frame_cnt);
printf("\n");
printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
@@ -910,6 +973,12 @@ int main(int argc, char **argv) {
// Try to rewrite the output file headers with the actual frame count.
for (i = 0; i < cfg.ts_number_layers; ++i) vpx_video_writer_close(outfile[i]);
- vpx_img_free(&raw);
+ if (input_ctx.file_type != FILE_TYPE_Y4M) {
+ vpx_img_free(&raw);
+ }
+
+#if ROI_MAP
+ free(roi.roi_map);
+#endif
return EXIT_SUCCESS;
}
diff --git a/libs/libvpx/ivfdec.c b/libs/libvpx/ivfdec.c
index f64e594ab0e..3e179bc6ed2 100644
--- a/libs/libvpx/ivfdec.c
+++ b/libs/libvpx/ivfdec.c
@@ -76,12 +76,12 @@ int ivf_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
size_t frame_size = 0;
if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) {
- if (!feof(infile)) warn("Failed to read frame size\n");
+ if (!feof(infile)) warn("Failed to read frame size");
} else {
frame_size = mem_get_le32(raw_header);
if (frame_size > 256 * 1024 * 1024) {
- warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
+ warn("Read invalid frame size (%u)", (unsigned int)frame_size);
frame_size = 0;
}
@@ -92,7 +92,7 @@ int ivf_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
*buffer = new_buffer;
*buffer_size = 2 * frame_size;
} else {
- warn("Failed to allocate compressed data buffer\n");
+ warn("Failed to allocate compressed data buffer");
frame_size = 0;
}
}
@@ -100,7 +100,7 @@ int ivf_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
if (!feof(infile)) {
if (fread(*buffer, 1, frame_size, infile) != frame_size) {
- warn("Failed to read full frame\n");
+ warn("Failed to read full frame");
return 1;
}
diff --git a/libs/libvpx/ivfdec.h b/libs/libvpx/ivfdec.h
index af725572b48..847cd79f3fe 100644
--- a/libs/libvpx/ivfdec.h
+++ b/libs/libvpx/ivfdec.h
@@ -7,8 +7,8 @@
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
-#ifndef IVFDEC_H_
-#define IVFDEC_H_
+#ifndef VPX_IVFDEC_H_
+#define VPX_IVFDEC_H_
#include "./tools_common.h"
@@ -25,4 +25,4 @@ int ivf_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
} /* extern "C" */
#endif
-#endif // IVFDEC_H_
+#endif // VPX_IVFDEC_H_
diff --git a/libs/libvpx/ivfenc.h b/libs/libvpx/ivfenc.h
index ebdce47be8f..483f2d2c591 100644
--- a/libs/libvpx/ivfenc.h
+++ b/libs/libvpx/ivfenc.h
@@ -7,8 +7,8 @@
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
-#ifndef IVFENC_H_
-#define IVFENC_H_
+#ifndef VPX_IVFENC_H_
+#define VPX_IVFENC_H_
#include "./tools_common.h"
@@ -30,4 +30,4 @@ void ivf_write_frame_size(FILE *outfile, size_t frame_size);
} /* extern "C" */
#endif
-#endif // IVFENC_H_
+#endif // VPX_IVFENC_H_
diff --git a/libs/libvpx/libs.doxy_template b/libs/libvpx/libs.doxy_template
index 5a8f847280e..1eacc8fe2db 100644
--- a/libs/libvpx/libs.doxy_template
+++ b/libs/libvpx/libs.doxy_template
@@ -943,18 +943,6 @@ GENERATE_XML = NO
XML_OUTPUT = xml
-# The XML_SCHEMA tag can be used to specify an XML schema,
-# which can be used by a validating XML parser to check the
-# syntax of the XML files.
-
-XML_SCHEMA =
-
-# The XML_DTD tag can be used to specify an XML DTD,
-# which can be used by a validating XML parser to check the
-# syntax of the XML files.
-
-XML_DTD =
-
# If the XML_PROGRAMLISTING tag is set to YES Doxygen will
# dump the program listings (including syntax highlighting
# and cross-referencing information) to the XML output. Note that
diff --git a/libs/libvpx/libs.mk b/libs/libvpx/libs.mk
index a3e2f9d0ebd..67d7512abea 100644
--- a/libs/libvpx/libs.mk
+++ b/libs/libvpx/libs.mk
@@ -88,7 +88,6 @@ ifeq ($(CONFIG_VP9_ENCODER),yes)
CODEC_EXPORTS-yes += $(addprefix $(VP9_PREFIX),$(VP9_CX_EXPORTS))
CODEC_SRCS-yes += $(VP9_PREFIX)vp9cx.mk vpx/vp8.h vpx/vp8cx.h
INSTALL-LIBS-yes += include/vpx/vp8.h include/vpx/vp8cx.h
- INSTALL-LIBS-$(CONFIG_SPATIAL_SVC) += include/vpx/svc_context.h
INSTALL_MAPS += include/vpx/% $(SRC_PATH_BARE)/$(VP9_PREFIX)/%
CODEC_DOC_SRCS += vpx/vp8.h vpx/vp8cx.h
CODEC_DOC_SECTIONS += vp9 vp9_encoder
@@ -113,13 +112,6 @@ ifeq ($(CONFIG_DECODERS),yes)
CODEC_DOC_SECTIONS += decoder
endif
-# Suppress -Wextra warnings in third party code.
-$(BUILD_PFX)third_party/googletest/%.cc.o: CXXFLAGS += -Wno-missing-field-initializers
-# Suppress -Wextra warnings in first party code pending investigation.
-# https://bugs.chromium.org/p/webm/issues/detail?id=1069
-$(BUILD_PFX)vp8/encoder/onyx_if.c.o: CFLAGS += -Wno-unknown-warning-option -Wno-clobbered
-$(BUILD_PFX)vp8/decoder/onyxd_if.c.o: CFLAGS += -Wno-unknown-warning-option -Wno-clobbered
-
ifeq ($(CONFIG_MSVS),yes)
CODEC_LIB=$(if $(CONFIG_STATIC_MSVCRT),vpxmt,vpxmd)
GTEST_LIB=$(if $(CONFIG_STATIC_MSVCRT),gtestmt,gtestmd)
@@ -153,9 +145,6 @@ INSTALL-SRCS-$(CONFIG_CODEC_SRCS) += vpx_dsp/x86/bitdepth_conversion_sse2.asm
endif
CODEC_EXPORTS-yes += vpx/exports_com
CODEC_EXPORTS-$(CONFIG_ENCODERS) += vpx/exports_enc
-ifeq ($(CONFIG_SPATIAL_SVC),yes)
-CODEC_EXPORTS-$(CONFIG_ENCODERS) += vpx/exports_spatial_svc
-endif
CODEC_EXPORTS-$(CONFIG_DECODERS) += vpx/exports_dec
INSTALL-LIBS-yes += include/vpx/vpx_codec.h
@@ -206,6 +195,8 @@ vpx.def: $(call enabled,CODEC_EXPORTS)
--out=$@ $^
CLEAN-OBJS += vpx.def
+vpx.$(VCPROJ_SFX): VCPROJ_SRCS=$(filter-out $(addprefix %, $(ASM_INCLUDES)), $^)
+
vpx.$(VCPROJ_SFX): $(CODEC_SRCS) vpx.def
@echo " [CREATE] $@"
$(qexec)$(GEN_VCPROJ) \
@@ -218,7 +209,15 @@ vpx.$(VCPROJ_SFX): $(CODEC_SRCS) vpx.def
--ver=$(CONFIG_VS_VERSION) \
--src-path-bare="$(SRC_PATH_BARE)" \
--out=$@ $(CFLAGS) \
- $(filter-out $(addprefix %, $(ASM_INCLUDES)), $^) \
+ $(filter $(SRC_PATH_BARE)/vp8/%.c, $(VCPROJ_SRCS)) \
+ $(filter $(SRC_PATH_BARE)/vp8/%.h, $(VCPROJ_SRCS)) \
+ $(filter $(SRC_PATH_BARE)/vp9/%.c, $(VCPROJ_SRCS)) \
+ $(filter $(SRC_PATH_BARE)/vp9/%.h, $(VCPROJ_SRCS)) \
+ $(filter $(SRC_PATH_BARE)/vpx/%, $(VCPROJ_SRCS)) \
+ $(filter $(SRC_PATH_BARE)/vpx_dsp/%, $(VCPROJ_SRCS)) \
+ $(filter-out $(addprefix $(SRC_PATH_BARE)/, \
+ vp8/%.c vp8/%.h vp9/%.c vp9/%.h vpx/% vpx_dsp/%), \
+ $(VCPROJ_SRCS)) \
--src-path-bare="$(SRC_PATH_BARE)" \
PROJECTS-yes += vpx.$(VCPROJ_SFX)
@@ -233,8 +232,8 @@ OBJS-yes += $(LIBVPX_OBJS)
LIBS-$(if yes,$(CONFIG_STATIC)) += $(BUILD_PFX)libvpx.a $(BUILD_PFX)libvpx_g.a
$(BUILD_PFX)libvpx_g.a: $(LIBVPX_OBJS)
-SO_VERSION_MAJOR := 5
-SO_VERSION_MINOR := 0
+SO_VERSION_MAJOR := 6
+SO_VERSION_MINOR := 1
SO_VERSION_PATCH := 0
ifeq ($(filter darwin%,$(TGT_OS)),$(TGT_OS))
LIBVPX_SO := libvpx.$(SO_VERSION_MAJOR).dylib
@@ -274,18 +273,6 @@ $(BUILD_PFX)$(LIBVPX_SO): extralibs += -lm
$(BUILD_PFX)$(LIBVPX_SO): SONAME = libvpx.so.$(SO_VERSION_MAJOR)
$(BUILD_PFX)$(LIBVPX_SO): EXPORTS_FILE = $(EXPORT_FILE)
-libvpx.ver: $(call enabled,CODEC_EXPORTS)
- @echo " [CREATE] $@"
- $(qexec)echo "{ global:" > $@
- $(qexec)for f in $?; do awk '{print $$2";"}' < $$f >>$@; done
- $(qexec)echo "local: *; };" >> $@
-CLEAN-OBJS += libvpx.ver
-
-libvpx.syms: $(call enabled,CODEC_EXPORTS)
- @echo " [CREATE] $@"
- $(qexec)awk '{print "_"$$2}' $^ >$@
-CLEAN-OBJS += libvpx.syms
-
libvpx.def: $(call enabled,CODEC_EXPORTS)
@echo " [CREATE] $@"
$(qexec)echo LIBRARY $(LIBVPX_SO:.dll=) INITINSTANCE TERMINSTANCE > $@
@@ -345,6 +332,18 @@ INSTALL_MAPS += $(LIBSUBDIR)/pkgconfig/%.pc %.pc
CLEAN-OBJS += vpx.pc
endif
+libvpx.ver: $(call enabled,CODEC_EXPORTS)
+ @echo " [CREATE] $@"
+ $(qexec)echo "{ global:" > $@
+ $(qexec)for f in $?; do awk '{print $$2";"}' < $$f >>$@; done
+ $(qexec)echo "local: *; };" >> $@
+CLEAN-OBJS += libvpx.ver
+
+libvpx.syms: $(call enabled,CODEC_EXPORTS)
+ @echo " [CREATE] $@"
+ $(qexec)awk '{print "_"$$2}' $^ >$@
+CLEAN-OBJS += libvpx.syms
+
#
# Rule to make assembler configuration file from C configuration file
#
diff --git a/libs/libvpx/mainpage.dox b/libs/libvpx/mainpage.dox
index ec202fa4fb5..4b0dff08710 100644
--- a/libs/libvpx/mainpage.dox
+++ b/libs/libvpx/mainpage.dox
@@ -25,8 +25,10 @@
release.
- The \ref readme contains instructions on recompiling the sample applications.
- Read the \ref usage "usage" for a narrative on codec usage.
+ \if samples
- Read the \ref samples "sample code" for examples of how to interact with the
codec.
+ \endif
- \ref codec reference
\if encoder
- \ref encoder reference
diff --git a/libs/libvpx/md5_utils.c b/libs/libvpx/md5_utils.c
index 093798b8339..9ddb104c8a6 100644
--- a/libs/libvpx/md5_utils.c
+++ b/libs/libvpx/md5_utils.c
@@ -163,7 +163,7 @@ void MD5Final(md5byte digest[16], struct MD5Context *ctx) {
*/
VPX_NO_UNSIGNED_OVERFLOW_CHECK void MD5Transform(UWORD32 buf[4],
UWORD32 const in[16]) {
- register UWORD32 a, b, c, d;
+ UWORD32 a, b, c, d;
a = buf[0];
b = buf[1];
diff --git a/libs/libvpx/md5_utils.h b/libs/libvpx/md5_utils.h
index bd4991b3ad9..e0d5a2d1fbd 100644
--- a/libs/libvpx/md5_utils.h
+++ b/libs/libvpx/md5_utils.h
@@ -20,8 +20,8 @@
* Still in the public domain.
*/
-#ifndef MD5_UTILS_H_
-#define MD5_UTILS_H_
+#ifndef VPX_MD5_UTILS_H_
+#define VPX_MD5_UTILS_H_
#ifdef __cplusplus
extern "C" {
@@ -46,4 +46,4 @@ void MD5Transform(UWORD32 buf[4], UWORD32 const in[16]);
} // extern "C"
#endif
-#endif // MD5_UTILS_H_
+#endif // VPX_MD5_UTILS_H_
diff --git a/libs/libvpx/rate_hist.h b/libs/libvpx/rate_hist.h
index 00a1676a617..d6a4c685195 100644
--- a/libs/libvpx/rate_hist.h
+++ b/libs/libvpx/rate_hist.h
@@ -8,8 +8,8 @@
* be found in the AUTHORS file in the root of the source tree.
*/
-#ifndef RATE_HIST_H_
-#define RATE_HIST_H_
+#ifndef VPX_RATE_HIST_H_
+#define VPX_RATE_HIST_H_
#include "vpx/vpx_encoder.h"
@@ -37,4 +37,4 @@ void show_rate_histogram(struct rate_hist *hist, const vpx_codec_enc_cfg_t *cfg,
} // extern "C"
#endif
-#endif // RATE_HIST_H_
+#endif // VPX_RATE_HIST_H_
diff --git a/libs/libvpx/test/acm_random.h b/libs/libvpx/test/acm_random.h
index d915cf91336..ccfa20681aa 100644
--- a/libs/libvpx/test/acm_random.h
+++ b/libs/libvpx/test/acm_random.h
@@ -8,8 +8,8 @@
* be found in the AUTHORS file in the root of the source tree.
*/
-#ifndef TEST_ACM_RANDOM_H_
-#define TEST_ACM_RANDOM_H_
+#ifndef VPX_TEST_ACM_RANDOM_H_
+#define VPX_TEST_ACM_RANDOM_H_
#include
@@ -34,6 +34,24 @@ class ACMRandom {
return (value >> 15) & 0xffff;
}
+ int32_t Rand20Signed(void) {
+ // Use 20 bits: values between 524287 and -524288.
+ const uint32_t value = random_.Generate(1048576);
+ return static_cast(value) - 524288;
+ }
+
+ int16_t Rand16Signed(void) {
+ // Use 16 bits: values between 32767 and -32768.
+ const uint32_t value = random_.Generate(65536);
+ return static_cast(value) - 32768;
+ }
+
+ int16_t Rand13Signed(void) {
+ // Use 13 bits: values between 4095 and -4096.
+ const uint32_t value = random_.Generate(8192);
+ return static_cast(value) - 4096;
+ }
+
int16_t Rand9Signed(void) {
// Use 9 bits: values between 255 (0x0FF) and -256 (0x100).
const uint32_t value = random_.Generate(512);
@@ -73,4 +91,4 @@ class ACMRandom {
} // namespace libvpx_test
-#endif // TEST_ACM_RANDOM_H_
+#endif // VPX_TEST_ACM_RANDOM_H_
diff --git a/libs/libvpx/test/active_map_refresh_test.cc b/libs/libvpx/test/active_map_refresh_test.cc
index d893635505c..a985ed4f119 100644
--- a/libs/libvpx/test/active_map_refresh_test.cc
+++ b/libs/libvpx/test/active_map_refresh_test.cc
@@ -74,7 +74,7 @@ class ActiveMapRefreshTest
::libvpx_test::Encoder *encoder) {
::libvpx_test::Y4mVideoSource *y4m_video =
static_cast(video);
- if (video->frame() == 1) {
+ if (video->frame() == 0) {
encoder->Control(VP8E_SET_CPUUSED, cpu_used_);
encoder->Control(VP9E_SET_AQ_MODE, kAqModeCyclicRefresh);
} else if (video->frame() >= 2 && video->img()) {
diff --git a/libs/libvpx/test/active_map_test.cc b/libs/libvpx/test/active_map_test.cc
index 1d24f956f59..03536c81ef4 100644
--- a/libs/libvpx/test/active_map_test.cc
+++ b/libs/libvpx/test/active_map_test.cc
@@ -35,7 +35,7 @@ class ActiveMapTest
virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
::libvpx_test::Encoder *encoder) {
- if (video->frame() == 1) {
+ if (video->frame() == 0) {
encoder->Control(VP8E_SET_CPUUSED, cpu_used_);
} else if (video->frame() == 3) {
vpx_active_map_t map = vpx_active_map_t();
diff --git a/libs/libvpx/test/add_noise_test.cc b/libs/libvpx/test/add_noise_test.cc
index eae32c33bb8..0d1893c524a 100644
--- a/libs/libvpx/test/add_noise_test.cc
+++ b/libs/libvpx/test/add_noise_test.cc
@@ -8,8 +8,11 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include
+#include
+
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
+#include "test/util.h"
#include "third_party/googletest/src/include/gtest/gtest.h"
#include "./vpx_dsp_rtcd.h"
#include "vpx/vpx_integer.h"
@@ -25,7 +28,10 @@ typedef void (*AddNoiseFunc)(uint8_t *start, const int8_t *noise,
int blackclamp, int whiteclamp, int width,
int height, int pitch);
-class AddNoiseTest : public ::testing::TestWithParam {
+typedef std::tuple AddNoiseTestFPParam;
+
+class AddNoiseTest : public ::testing::Test,
+ public ::testing::WithParamInterface {
public:
virtual void TearDown() { libvpx_test::ClearSystemState(); }
virtual ~AddNoiseTest() {}
@@ -44,14 +50,14 @@ TEST_P(AddNoiseTest, CheckNoiseAdded) {
const int height = 64;
const int image_size = width * height;
int8_t noise[kNoiseSize];
- const int clamp = vpx_setup_noise(4.4, noise, kNoiseSize);
+ const int clamp = vpx_setup_noise(GET_PARAM(0), noise, kNoiseSize);
uint8_t *const s =
reinterpret_cast(vpx_calloc(image_size, sizeof(*s)));
ASSERT_TRUE(s != NULL);
memset(s, 99, image_size * sizeof(*s));
ASM_REGISTER_STATE_CHECK(
- GetParam()(s, noise, clamp, clamp, width, height, width));
+ GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
// Check to make sure we don't end up having either the same or no added
// noise either vertically or horizontally.
@@ -70,7 +76,7 @@ TEST_P(AddNoiseTest, CheckNoiseAdded) {
memset(s, 255, image_size);
ASM_REGISTER_STATE_CHECK(
- GetParam()(s, noise, clamp, clamp, width, height, width));
+ GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
// Check to make sure don't roll over.
for (int i = 0; i < image_size; ++i) {
@@ -81,7 +87,7 @@ TEST_P(AddNoiseTest, CheckNoiseAdded) {
memset(s, 0, image_size);
ASM_REGISTER_STATE_CHECK(
- GetParam()(s, noise, clamp, clamp, width, height, width));
+ GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
// Check to make sure don't roll under.
for (int i = 0; i < image_size; ++i) {
@@ -108,7 +114,7 @@ TEST_P(AddNoiseTest, CheckCvsAssembly) {
srand(0);
ASM_REGISTER_STATE_CHECK(
- GetParam()(s, noise, clamp, clamp, width, height, width));
+ GET_PARAM(1)(s, noise, clamp, clamp, width, height, width));
srand(0);
ASM_REGISTER_STATE_CHECK(
vpx_plane_add_noise_c(d, noise, clamp, clamp, width, height, width));
@@ -121,16 +127,24 @@ TEST_P(AddNoiseTest, CheckCvsAssembly) {
vpx_free(s);
}
-INSTANTIATE_TEST_CASE_P(C, AddNoiseTest,
- ::testing::Values(vpx_plane_add_noise_c));
+using std::make_tuple;
+
+INSTANTIATE_TEST_CASE_P(
+ C, AddNoiseTest,
+ ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_c),
+ make_tuple(4.4, vpx_plane_add_noise_c)));
#if HAVE_SSE2
-INSTANTIATE_TEST_CASE_P(SSE2, AddNoiseTest,
- ::testing::Values(vpx_plane_add_noise_sse2));
+INSTANTIATE_TEST_CASE_P(
+ SSE2, AddNoiseTest,
+ ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_sse2),
+ make_tuple(4.4, vpx_plane_add_noise_sse2)));
#endif
#if HAVE_MSA
-INSTANTIATE_TEST_CASE_P(MSA, AddNoiseTest,
- ::testing::Values(vpx_plane_add_noise_msa));
+INSTANTIATE_TEST_CASE_P(
+ MSA, AddNoiseTest,
+ ::testing::Values(make_tuple(3.25, vpx_plane_add_noise_msa),
+ make_tuple(4.4, vpx_plane_add_noise_msa)));
#endif
} // namespace
diff --git a/libs/libvpx/test/alt_ref_aq_segment_test.cc b/libs/libvpx/test/alt_ref_aq_segment_test.cc
index 64a3011eb99..6e03a478525 100644
--- a/libs/libvpx/test/alt_ref_aq_segment_test.cc
+++ b/libs/libvpx/test/alt_ref_aq_segment_test.cc
@@ -32,7 +32,7 @@ class AltRefAqSegmentTest
virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
::libvpx_test::Encoder *encoder) {
- if (video->frame() == 1) {
+ if (video->frame() == 0) {
encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
encoder->Control(VP9E_SET_ALT_REF_AQ, alt_ref_aq_mode_);
encoder->Control(VP9E_SET_AQ_MODE, aq_mode_);
diff --git a/libs/libvpx/test/altref_test.cc b/libs/libvpx/test/altref_test.cc
index f9308c2717a..0119be4da0a 100644
--- a/libs/libvpx/test/altref_test.cc
+++ b/libs/libvpx/test/altref_test.cc
@@ -35,7 +35,7 @@ class AltRefTest : public ::libvpx_test::EncoderTest,
virtual void PreEncodeFrameHook(libvpx_test::VideoSource *video,
libvpx_test::Encoder *encoder) {
- if (video->frame() == 1) {
+ if (video->frame() == 0) {
encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
encoder->Control(VP8E_SET_CPUUSED, 3);
}
diff --git a/libs/libvpx/test/android/README b/libs/libvpx/test/android/README
index 4a1adcf7f48..ee21f9b652c 100644
--- a/libs/libvpx/test/android/README
+++ b/libs/libvpx/test/android/README
@@ -3,12 +3,12 @@ Android.mk will build vpx unittests on android.
./libvpx/configure --target=armv7-android-gcc --enable-external-build \
--enable-postproc --disable-install-srcs --enable-multi-res-encoding \
--enable-temporal-denoising --disable-unit-tests --disable-install-docs \
- --disable-examples --disable-runtime-cpu-detect --sdk-path=$NDK
+ --disable-examples --disable-runtime-cpu-detect
2) From the parent directory, invoke ndk-build:
NDK_PROJECT_PATH=. ndk-build APP_BUILD_SCRIPT=./libvpx/test/android/Android.mk \
APP_ABI=armeabi-v7a APP_PLATFORM=android-18 APP_OPTIM=release \
- APP_STL=gnustl_static
+ APP_STL=c++_static
Note: Both adb and ndk-build are available prebuilt at:
https://chromium.googlesource.com/android_tools
diff --git a/libs/libvpx/test/aq_segment_test.cc b/libs/libvpx/test/aq_segment_test.cc
index 1c2147fbb2c..3c4053be7f3 100644
--- a/libs/libvpx/test/aq_segment_test.cc
+++ b/libs/libvpx/test/aq_segment_test.cc
@@ -31,7 +31,7 @@ class AqSegmentTest
virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
::libvpx_test::Encoder *encoder) {
- if (video->frame() == 1) {
+ if (video->frame() == 0) {
encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
encoder->Control(VP9E_SET_AQ_MODE, aq_mode_);
encoder->Control(VP8E_SET_MAX_INTRA_BITRATE_PCT, 100);
diff --git a/libs/libvpx/test/avg_test.cc b/libs/libvpx/test/avg_test.cc
index ad21198e4b4..3d24f1cdb6d 100644
--- a/libs/libvpx/test/avg_test.cc
+++ b/libs/libvpx/test/avg_test.cc
@@ -11,6 +11,7 @@
#include
#include
#include
+#include
#include "third_party/googletest/src/include/gtest/gtest.h"
@@ -22,40 +23,43 @@
#include "test/clear_system_state.h"
#include "test/register_state_check.h"
#include "test/util.h"
+#include "vpx/vpx_codec.h"
#include "vpx_mem/vpx_mem.h"
#include "vpx_ports/vpx_timer.h"
using libvpx_test::ACMRandom;
namespace {
+
+template
class AverageTestBase : public ::testing::Test {
public:
- AverageTestBase(int width, int height) : width_(width), height_(height) {}
-
- static void SetUpTestCase() {
- source_data_ = reinterpret_cast(
- vpx_memalign(kDataAlignment, kDataBlockSize));
- }
+ AverageTestBase(int width, int height)
+ : width_(width), height_(height), source_data_(NULL), source_stride_(0),
+ bit_depth_(8) {}
- static void TearDownTestCase() {
+ virtual void TearDown() {
vpx_free(source_data_);
source_data_ = NULL;
+ libvpx_test::ClearSystemState();
}
- virtual void TearDown() { libvpx_test::ClearSystemState(); }
-
protected:
// Handle blocks up to 4 blocks 64x64 with stride up to 128
static const int kDataAlignment = 16;
static const int kDataBlockSize = 64 * 128;
virtual void SetUp() {
+ source_data_ = reinterpret_cast(
+ vpx_memalign(kDataAlignment, kDataBlockSize * sizeof(source_data_[0])));
+ ASSERT_TRUE(source_data_ != NULL);
source_stride_ = (width_ + 31) & ~31;
+ bit_depth_ = 8;
rnd_.Reset(ACMRandom::DeterministicSeed());
}
// Sum Pixels
- static unsigned int ReferenceAverage8x8(const uint8_t *source, int pitch) {
+ static unsigned int ReferenceAverage8x8(const Pixel *source, int pitch) {
unsigned int average = 0;
for (int h = 0; h < 8; ++h) {
for (int w = 0; w < 8; ++w) average += source[h * pitch + w];
@@ -63,7 +67,7 @@ class AverageTestBase : public ::testing::Test {
return ((average + 32) >> 6);
}
- static unsigned int ReferenceAverage4x4(const uint8_t *source, int pitch) {
+ static unsigned int ReferenceAverage4x4(const Pixel *source, int pitch) {
unsigned int average = 0;
for (int h = 0; h < 4; ++h) {
for (int w = 0; w < 4; ++w) average += source[h * pitch + w];
@@ -71,7 +75,7 @@ class AverageTestBase : public ::testing::Test {
return ((average + 8) >> 4);
}
- void FillConstant(uint8_t fill_constant) {
+ void FillConstant(Pixel fill_constant) {
for (int i = 0; i < width_ * height_; ++i) {
source_data_[i] = fill_constant;
}
@@ -79,21 +83,22 @@ class AverageTestBase : public ::testing::Test {
void FillRandom() {
for (int i = 0; i < width_ * height_; ++i) {
- source_data_[i] = rnd_.Rand8();
+ source_data_[i] = rnd_.Rand16() & ((1 << bit_depth_) - 1);
}
}
int width_, height_;
- static uint8_t *source_data_;
+ Pixel *source_data_;
int source_stride_;
+ int bit_depth_;
ACMRandom rnd_;
};
typedef unsigned int (*AverageFunction)(const uint8_t *s, int pitch);
-typedef std::tr1::tuple AvgFunc;
+typedef std::tuple AvgFunc;
-class AverageTest : public AverageTestBase,
+class AverageTest : public AverageTestBase,
public ::testing::WithParamInterface {
public:
AverageTest() : AverageTestBase(GET_PARAM(0), GET_PARAM(1)) {}
@@ -119,12 +124,40 @@ class AverageTest : public AverageTestBase,
}
};
+#if CONFIG_VP9_HIGHBITDEPTH
+class AverageTestHBD : public AverageTestBase,
+ public ::testing::WithParamInterface {
+ public:
+ AverageTestHBD() : AverageTestBase(GET_PARAM(0), GET_PARAM(1)) {}
+
+ protected:
+ void CheckAverages() {
+ const int block_size = GET_PARAM(3);
+ unsigned int expected = 0;
+ if (block_size == 8) {
+ expected =
+ ReferenceAverage8x8(source_data_ + GET_PARAM(2), source_stride_);
+ } else if (block_size == 4) {
+ expected =
+ ReferenceAverage4x4(source_data_ + GET_PARAM(2), source_stride_);
+ }
+
+ ASM_REGISTER_STATE_CHECK(GET_PARAM(4)(
+ CONVERT_TO_BYTEPTR(source_data_ + GET_PARAM(2)), source_stride_));
+ unsigned int actual = GET_PARAM(4)(
+ CONVERT_TO_BYTEPTR(source_data_ + GET_PARAM(2)), source_stride_);
+
+ EXPECT_EQ(expected, actual);
+ }
+};
+#endif // CONFIG_VP9_HIGHBITDEPTH
+
typedef void (*IntProRowFunc)(int16_t hbuf[16], uint8_t const *ref,
const int ref_stride, const int height);
-typedef std::tr1::tuple IntProRowParam;
+typedef std::tuple IntProRowParam;
-class IntProRowTest : public AverageTestBase,
+class IntProRowTest : public AverageTestBase,
public ::testing::WithParamInterface {
public:
IntProRowTest()
@@ -135,6 +168,10 @@ class IntProRowTest : public AverageTestBase,
protected:
virtual void SetUp() {
+ source_data_ = reinterpret_cast(
+ vpx_memalign(kDataAlignment, kDataBlockSize * sizeof(source_data_[0])));
+ ASSERT_TRUE(source_data_ != NULL);
+
hbuf_asm_ = reinterpret_cast(
vpx_memalign(kDataAlignment, sizeof(*hbuf_asm_) * 16));
hbuf_c_ = reinterpret_cast(
@@ -142,6 +179,8 @@ class IntProRowTest : public AverageTestBase,
}
virtual void TearDown() {
+ vpx_free(source_data_);
+ source_data_ = NULL;
vpx_free(hbuf_c_);
hbuf_c_ = NULL;
vpx_free(hbuf_asm_);
@@ -164,9 +203,9 @@ class IntProRowTest : public AverageTestBase,
typedef int16_t (*IntProColFunc)(uint8_t const *ref, const int width);
-typedef std::tr1::tuple IntProColParam;
+typedef std::tuple IntProColParam;
-class IntProColTest : public AverageTestBase,
+class IntProColTest : public AverageTestBase,
public ::testing::WithParamInterface {
public:
IntProColTest() : AverageTestBase(GET_PARAM(0), 1), sum_asm_(0), sum_c_(0) {
@@ -189,7 +228,7 @@ class IntProColTest : public AverageTestBase,
};
typedef int (*SatdFunc)(const tran_low_t *coeffs, int length);
-typedef std::tr1::tuple SatdTestParam;
+typedef std::tuple SatdTestParam;
class SatdTest : public ::testing::Test,
public ::testing::WithParamInterface {
@@ -212,12 +251,7 @@ class SatdTest : public ::testing::Test,
for (int i = 0; i < satd_size_; ++i) src_[i] = val;
}
- void FillRandom() {
- for (int i = 0; i < satd_size_; ++i) {
- const int16_t tmp = rnd_.Rand16();
- src_[i] = (tran_low_t)tmp;
- }
- }
+ virtual void FillRandom() = 0;
void Check(const int expected) {
int total;
@@ -225,17 +259,29 @@ class SatdTest : public ::testing::Test,
EXPECT_EQ(expected, total);
}
+ tran_low_t *GetCoeff() const { return src_; }
+
int satd_size_;
+ ACMRandom rnd_;
+ tran_low_t *src_;
private:
- tran_low_t *src_;
SatdFunc satd_func_;
- ACMRandom rnd_;
+};
+
+class SatdLowbdTest : public SatdTest {
+ protected:
+ virtual void FillRandom() {
+ for (int i = 0; i < satd_size_; ++i) {
+ const int16_t tmp = rnd_.Rand16Signed();
+ src_[i] = (tran_low_t)tmp;
+ }
+ }
};
typedef int64_t (*BlockErrorFunc)(const tran_low_t *coeff,
const tran_low_t *dqcoeff, int block_size);
-typedef std::tr1::tuple BlockErrorTestFPParam;
+typedef std::tuple BlockErrorTestFPParam;
class BlockErrorTestFP
: public ::testing::Test,
@@ -279,6 +325,10 @@ class BlockErrorTestFP
EXPECT_EQ(expected, total);
}
+ tran_low_t *GetCoeff() const { return coeff_; }
+
+ tran_low_t *GetDQCoeff() const { return dqcoeff_; }
+
int txfm_size_;
private:
@@ -288,8 +338,6 @@ class BlockErrorTestFP
ACMRandom rnd_;
};
-uint8_t *AverageTestBase::source_data_ = NULL;
-
TEST_P(AverageTest, MinValue) {
FillConstant(0);
CheckAverages();
@@ -308,6 +356,27 @@ TEST_P(AverageTest, Random) {
CheckAverages();
}
}
+#if CONFIG_VP9_HIGHBITDEPTH
+TEST_P(AverageTestHBD, MinValue) {
+ FillConstant(0);
+ CheckAverages();
+}
+
+TEST_P(AverageTestHBD, MaxValue) {
+ FillConstant((1 << VPX_BITS_12) - 1);
+ CheckAverages();
+}
+
+TEST_P(AverageTestHBD, Random) {
+ bit_depth_ = VPX_BITS_12;
+ // The reference frame, but not the source frame, may be unaligned for
+ // certain types of searches.
+ for (int i = 0; i < 1000; i++) {
+ FillRandom();
+ CheckAverages();
+ }
+}
+#endif // CONFIG_VP9_HIGHBITDEPTH
TEST_P(IntProRowTest, MinValue) {
FillConstant(0);
@@ -339,27 +408,27 @@ TEST_P(IntProColTest, Random) {
RunComparison();
}
-TEST_P(SatdTest, MinValue) {
+TEST_P(SatdLowbdTest, MinValue) {
const int kMin = -32640;
const int expected = -kMin * satd_size_;
FillConstant(kMin);
Check(expected);
}
-TEST_P(SatdTest, MaxValue) {
+TEST_P(SatdLowbdTest, MaxValue) {
const int kMax = 32640;
const int expected = kMax * satd_size_;
FillConstant(kMax);
Check(expected);
}
-TEST_P(SatdTest, Random) {
+TEST_P(SatdLowbdTest, Random) {
int expected;
switch (satd_size_) {
- case 16: expected = 205298; break;
- case 64: expected = 1113950; break;
- case 256: expected = 4268415; break;
- case 1024: expected = 16954082; break;
+ case 16: expected = 263252; break;
+ case 64: expected = 1105420; break;
+ case 256: expected = 4252250; break;
+ case 1024: expected = 16876840; break;
default:
FAIL() << "Invalid satd size (" << satd_size_
<< ") valid: 16/64/256/1024";
@@ -368,11 +437,12 @@ TEST_P(SatdTest, Random) {
Check(expected);
}
-TEST_P(SatdTest, DISABLED_Speed) {
+TEST_P(SatdLowbdTest, DISABLED_Speed) {
const int kCountSpeedTestBlock = 20000;
vpx_usec_timer timer;
- DECLARE_ALIGNED(16, tran_low_t, coeff[1024]);
const int blocksize = GET_PARAM(0);
+ FillRandom();
+ tran_low_t *coeff = GetCoeff();
vpx_usec_timer_start(&timer);
for (int i = 0; i < kCountSpeedTestBlock; ++i) {
@@ -383,6 +453,62 @@ TEST_P(SatdTest, DISABLED_Speed) {
printf("blocksize: %4d time: %4d us\n", blocksize, elapsed_time);
}
+#if CONFIG_VP9_HIGHBITDEPTH
+class SatdHighbdTest : public SatdTest {
+ protected:
+ virtual void FillRandom() {
+ for (int i = 0; i < satd_size_; ++i) {
+ src_[i] = rnd_.Rand20Signed();
+ }
+ }
+};
+
+TEST_P(SatdHighbdTest, MinValue) {
+ const int kMin = -524280;
+ const int expected = -kMin * satd_size_;
+ FillConstant(kMin);
+ Check(expected);
+}
+
+TEST_P(SatdHighbdTest, MaxValue) {
+ const int kMax = 524280;
+ const int expected = kMax * satd_size_;
+ FillConstant(kMax);
+ Check(expected);
+}
+
+TEST_P(SatdHighbdTest, Random) {
+ int expected;
+ switch (satd_size_) {
+ case 16: expected = 5249712; break;
+ case 64: expected = 18362120; break;
+ case 256: expected = 66100520; break;
+ case 1024: expected = 266094734; break;
+ default:
+ FAIL() << "Invalid satd size (" << satd_size_
+ << ") valid: 16/64/256/1024";
+ }
+ FillRandom();
+ Check(expected);
+}
+
+TEST_P(SatdHighbdTest, DISABLED_Speed) {
+ const int kCountSpeedTestBlock = 20000;
+ vpx_usec_timer timer;
+ const int blocksize = GET_PARAM(0);
+ FillRandom();
+ tran_low_t *coeff = GetCoeff();
+
+ vpx_usec_timer_start(&timer);
+ for (int i = 0; i < kCountSpeedTestBlock; ++i) {
+ GET_PARAM(1)(coeff, blocksize);
+ }
+ vpx_usec_timer_mark(&timer);
+ const int elapsed_time = static_cast(vpx_usec_timer_elapsed(&timer));
+ printf("blocksize: %4d time: %4d us\n", blocksize, elapsed_time);
+}
+#endif // CONFIG_VP9_HIGHBITDEPTH
+
TEST_P(BlockErrorTestFP, MinValue) {
const int64_t kMin = -32640;
const int64_t expected = kMin * kMin * txfm_size_;
@@ -415,9 +541,10 @@ TEST_P(BlockErrorTestFP, Random) {
TEST_P(BlockErrorTestFP, DISABLED_Speed) {
const int kCountSpeedTestBlock = 20000;
vpx_usec_timer timer;
- DECLARE_ALIGNED(16, tran_low_t, coeff[1024]);
- DECLARE_ALIGNED(16, tran_low_t, dqcoeff[1024]);
const int blocksize = GET_PARAM(0);
+ FillRandom();
+ tran_low_t *coeff = GetCoeff();
+ tran_low_t *dqcoeff = GetDQCoeff();
vpx_usec_timer_start(&timer);
for (int i = 0; i < kCountSpeedTestBlock; ++i) {
@@ -428,14 +555,34 @@ TEST_P(BlockErrorTestFP, DISABLED_Speed) {
printf("blocksize: %4d time: %4d us\n", blocksize, elapsed_time);
}
-using std::tr1::make_tuple;
+using std::make_tuple;
INSTANTIATE_TEST_CASE_P(
C, AverageTest,
::testing::Values(make_tuple(16, 16, 1, 8, &vpx_avg_8x8_c),
make_tuple(16, 16, 1, 4, &vpx_avg_4x4_c)));
-INSTANTIATE_TEST_CASE_P(C, SatdTest,
+#if CONFIG_VP9_HIGHBITDEPTH
+INSTANTIATE_TEST_CASE_P(
+ C, AverageTestHBD,
+ ::testing::Values(make_tuple(16, 16, 1, 8, &vpx_highbd_avg_8x8_c),
+ make_tuple(16, 16, 1, 4, &vpx_highbd_avg_4x4_c)));
+
+#if HAVE_SSE2
+INSTANTIATE_TEST_CASE_P(
+ SSE2, AverageTestHBD,
+ ::testing::Values(make_tuple(16, 16, 1, 8, &vpx_highbd_avg_8x8_sse2),
+ make_tuple(16, 16, 1, 4, &vpx_highbd_avg_4x4_sse2)));
+#endif // HAVE_SSE2
+
+INSTANTIATE_TEST_CASE_P(C, SatdHighbdTest,
+ ::testing::Values(make_tuple(16, &vpx_satd_c),
+ make_tuple(64, &vpx_satd_c),
+ make_tuple(256, &vpx_satd_c),
+ make_tuple(1024, &vpx_satd_c)));
+#endif // CONFIG_VP9_HIGHBITDEPTH
+
+INSTANTIATE_TEST_CASE_P(C, SatdLowbdTest,
::testing::Values(make_tuple(16, &vpx_satd_c),
make_tuple(64, &vpx_satd_c),
make_tuple(256, &vpx_satd_c),
@@ -472,7 +619,7 @@ INSTANTIATE_TEST_CASE_P(
make_tuple(64, &vpx_int_pro_col_sse2,
&vpx_int_pro_col_c)));
-INSTANTIATE_TEST_CASE_P(SSE2, SatdTest,
+INSTANTIATE_TEST_CASE_P(SSE2, SatdLowbdTest,
::testing::Values(make_tuple(16, &vpx_satd_sse2),
make_tuple(64, &vpx_satd_sse2),
make_tuple(256, &vpx_satd_sse2),
@@ -487,12 +634,21 @@ INSTANTIATE_TEST_CASE_P(
#endif // HAVE_SSE2
#if HAVE_AVX2
-INSTANTIATE_TEST_CASE_P(AVX2, SatdTest,
+INSTANTIATE_TEST_CASE_P(AVX2, SatdLowbdTest,
::testing::Values(make_tuple(16, &vpx_satd_avx2),
make_tuple(64, &vpx_satd_avx2),
make_tuple(256, &vpx_satd_avx2),
make_tuple(1024, &vpx_satd_avx2)));
+#if CONFIG_VP9_HIGHBITDEPTH
+INSTANTIATE_TEST_CASE_P(
+ AVX2, SatdHighbdTest,
+ ::testing::Values(make_tuple(16, &vpx_highbd_satd_avx2),
+ make_tuple(64, &vpx_highbd_satd_avx2),
+ make_tuple(256, &vpx_highbd_satd_avx2),
+ make_tuple(1024, &vpx_highbd_satd_avx2)));
+#endif // CONFIG_VP9_HIGHBITDEPTH
+
INSTANTIATE_TEST_CASE_P(
AVX2, BlockErrorTestFP,
::testing::Values(make_tuple(16, &vp9_block_error_fp_avx2),
@@ -525,7 +681,7 @@ INSTANTIATE_TEST_CASE_P(
make_tuple(64, &vpx_int_pro_col_neon,
&vpx_int_pro_col_c)));
-INSTANTIATE_TEST_CASE_P(NEON, SatdTest,
+INSTANTIATE_TEST_CASE_P(NEON, SatdLowbdTest,
::testing::Values(make_tuple(16, &vpx_satd_neon),
make_tuple(64, &vpx_satd_neon),
make_tuple(256, &vpx_satd_neon),
@@ -570,7 +726,7 @@ INSTANTIATE_TEST_CASE_P(
// TODO(jingning): Remove the highbitdepth flag once the SIMD functions are
// in place.
#if !CONFIG_VP9_HIGHBITDEPTH
-INSTANTIATE_TEST_CASE_P(MSA, SatdTest,
+INSTANTIATE_TEST_CASE_P(MSA, SatdLowbdTest,
::testing::Values(make_tuple(16, &vpx_satd_msa),
make_tuple(64, &vpx_satd_msa),
make_tuple(256, &vpx_satd_msa),
diff --git a/libs/libvpx/test/bench.cc b/libs/libvpx/test/bench.cc
new file mode 100644
index 00000000000..4b883d8250e
--- /dev/null
+++ b/libs/libvpx/test/bench.cc
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018 The WebM project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include
+#include
+
+#include "test/bench.h"
+#include "vpx_ports/vpx_timer.h"
+
+void AbstractBench::RunNTimes(int n) {
+ for (int r = 0; r < VPX_BENCH_ROBUST_ITER; r++) {
+ vpx_usec_timer timer;
+ vpx_usec_timer_start(&timer);
+ for (int j = 0; j < n; ++j) {
+ Run();
+ }
+ vpx_usec_timer_mark(&timer);
+ times_[r] = static_cast(vpx_usec_timer_elapsed(&timer));
+ }
+}
+
+void AbstractBench::PrintMedian(const char *title) {
+ std::sort(times_, times_ + VPX_BENCH_ROBUST_ITER);
+ const int med = times_[VPX_BENCH_ROBUST_ITER >> 1];
+ int sad = 0;
+ for (int t = 0; t < VPX_BENCH_ROBUST_ITER; t++) {
+ sad += abs(times_[t] - med);
+ }
+ printf("[%10s] %s %.1f ms ( ±%.1f ms )\n", "BENCH ", title, med / 1000.0,
+ sad / (VPX_BENCH_ROBUST_ITER * 1000.0));
+}
diff --git a/libs/libvpx/test/bench.h b/libs/libvpx/test/bench.h
new file mode 100644
index 00000000000..57ca9118bac
--- /dev/null
+++ b/libs/libvpx/test/bench.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2018 The WebM project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VPX_TEST_BENCH_H_
+#define VPX_TEST_BENCH_H_
+
+// Number of iterations used to compute median run time.
+#define VPX_BENCH_ROBUST_ITER 15
+
+class AbstractBench {
+ public:
+ void RunNTimes(int n);
+ void PrintMedian(const char *title);
+
+ protected:
+ // Implement this method and put the code to benchmark in it.
+ virtual void Run() = 0;
+
+ private:
+ int times_[VPX_BENCH_ROBUST_ITER];
+};
+
+#endif // VPX_TEST_BENCH_H_
diff --git a/libs/libvpx/test/blockiness_test.cc b/libs/libvpx/test/blockiness_test.cc
index 2fa10192f15..ced6e66c620 100644
--- a/libs/libvpx/test/blockiness_test.cc
+++ b/libs/libvpx/test/blockiness_test.cc
@@ -11,6 +11,7 @@
#include
#include
#include
+#include
#include "third_party/googletest/src/include/gtest/gtest.h"
@@ -25,10 +26,7 @@
#include "test/util.h"
#include "vpx_mem/vpx_mem.h"
-
-extern "C" double vp9_get_blockiness(const unsigned char *img1, int img1_pitch,
- const unsigned char *img2, int img2_pitch,
- int width, int height);
+#include "vp9/encoder/vp9_blockiness.h"
using libvpx_test::ACMRandom;
@@ -141,7 +139,7 @@ class BlockinessTestBase : public ::testing::Test {
};
#if CONFIG_VP9_ENCODER
-typedef std::tr1::tuple BlockinessParam;
+typedef std::tuple BlockinessParam;
class BlockinessVP9Test
: public BlockinessTestBase,
public ::testing::WithParamInterface {
@@ -208,15 +206,15 @@ TEST_P(BlockinessVP9Test, WorstCaseBlockiness) {
}
#endif // CONFIG_VP9_ENCODER
-using std::tr1::make_tuple;
+using std::make_tuple;
//------------------------------------------------------------------------------
// C functions
#if CONFIG_VP9_ENCODER
-const BlockinessParam c_vp9_tests[] = {
- make_tuple(320, 240), make_tuple(318, 242), make_tuple(318, 238),
-};
+const BlockinessParam c_vp9_tests[] = { make_tuple(320, 240),
+ make_tuple(318, 242),
+ make_tuple(318, 238) };
INSTANTIATE_TEST_CASE_P(C, BlockinessVP9Test, ::testing::ValuesIn(c_vp9_tests));
#endif
diff --git a/libs/libvpx/test/borders_test.cc b/libs/libvpx/test/borders_test.cc
index e66ff02e25e..b91a15b8003 100644
--- a/libs/libvpx/test/borders_test.cc
+++ b/libs/libvpx/test/borders_test.cc
@@ -31,7 +31,7 @@ class BordersTest
virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
::libvpx_test::Encoder *encoder) {
- if (video->frame() == 1) {
+ if (video->frame() == 0) {
encoder->Control(VP8E_SET_CPUUSED, 1);
encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
diff --git a/libs/libvpx/test/buffer.h b/libs/libvpx/test/buffer.h
index 2175dad9d90..b003d2f0d02 100644
--- a/libs/libvpx/test/buffer.h
+++ b/libs/libvpx/test/buffer.h
@@ -8,8 +8,8 @@
* be found in the AUTHORS file in the root of the source tree.
*/
-#ifndef TEST_BUFFER_H_
-#define TEST_BUFFER_H_
+#ifndef VPX_TEST_BUFFER_H_
+#define VPX_TEST_BUFFER_H_
#include
@@ -379,4 +379,4 @@ bool Buffer::BufferSizesMatch(const Buffer &a) const {
return true;
}
} // namespace libvpx_test
-#endif // TEST_BUFFER_H_
+#endif // VPX_TEST_BUFFER_H_
diff --git a/libs/libvpx/test/byte_alignment_test.cc b/libs/libvpx/test/byte_alignment_test.cc
index 5a058b27561..0ef6c4c5192 100644
--- a/libs/libvpx/test/byte_alignment_test.cc
+++ b/libs/libvpx/test/byte_alignment_test.cc
@@ -171,8 +171,9 @@ TEST_F(ByteAlignmentTest, SwitchByteAlignment) {
TEST_P(ByteAlignmentTest, TestAlignment) {
const ByteAlignmentTestParam t = GetParam();
SetByteAlignment(t.byte_alignment, t.expected_value);
- if (t.decode_remaining)
+ if (t.decode_remaining) {
ASSERT_EQ(VPX_CODEC_OK, DecodeRemainingFrames(t.byte_alignment));
+ }
}
INSTANTIATE_TEST_CASE_P(Alignments, ByteAlignmentTest,
diff --git a/libs/libvpx/test/clear_system_state.h b/libs/libvpx/test/clear_system_state.h
index 044a5c75834..ba3c0b386a1 100644
--- a/libs/libvpx/test/clear_system_state.h
+++ b/libs/libvpx/test/clear_system_state.h
@@ -7,23 +7,17 @@
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
-#ifndef TEST_CLEAR_SYSTEM_STATE_H_
-#define TEST_CLEAR_SYSTEM_STATE_H_
+#ifndef VPX_TEST_CLEAR_SYSTEM_STATE_H_
+#define VPX_TEST_CLEAR_SYSTEM_STATE_H_
#include "./vpx_config.h"
-#if ARCH_X86 || ARCH_X86_64
-#include "vpx_ports/x86.h"
-#endif
+#include "vpx_ports/system_state.h"
namespace libvpx_test {
// Reset system to a known state. This function should be used for all non-API
// test cases.
-inline void ClearSystemState() {
-#if ARCH_X86 || ARCH_X86_64
- vpx_reset_mmx_state();
-#endif
-}
+inline void ClearSystemState() { vpx_clear_system_state(); }
} // namespace libvpx_test
-#endif // TEST_CLEAR_SYSTEM_STATE_H_
+#endif // VPX_TEST_CLEAR_SYSTEM_STATE_H_
diff --git a/libs/libvpx/test/codec_factory.h b/libs/libvpx/test/codec_factory.h
index d5882ed9c8b..17c9512ca8b 100644
--- a/libs/libvpx/test/codec_factory.h
+++ b/libs/libvpx/test/codec_factory.h
@@ -7,8 +7,10 @@
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
-#ifndef TEST_CODEC_FACTORY_H_
-#define TEST_CODEC_FACTORY_H_
+#ifndef VPX_TEST_CODEC_FACTORY_H_
+#define VPX_TEST_CODEC_FACTORY_H_
+
+#include
#include "./vpx_config.h"
#include "vpx/vpx_decoder.h"
@@ -53,23 +55,22 @@ class CodecFactory {
template
class CodecTestWithParam
: public ::testing::TestWithParam<
- std::tr1::tuple > {};
+ std::tuple > {};
template
class CodecTestWith2Params
: public ::testing::TestWithParam<
- std::tr1::tuple > {};
+ std::tuple > {};
template
class CodecTestWith3Params
: public ::testing::TestWithParam<
- std::tr1::tuple > {};
+ std::tuple > {};
template
class CodecTestWith4Params
: public ::testing::TestWithParam<
- std::tr1::tuple > {
-};
+ std::tuple > {};
/*
* VP8 Codec Definitions
@@ -264,4 +265,4 @@ const libvpx_test::VP9CodecFactory kVP9;
#endif // CONFIG_VP9
} // namespace libvpx_test
-#endif // TEST_CODEC_FACTORY_H_
+#endif // VPX_TEST_CODEC_FACTORY_H_
diff --git a/libs/libvpx/test/comp_avg_pred_test.cc b/libs/libvpx/test/comp_avg_pred_test.cc
index 110e0658365..56e701e09cb 100644
--- a/libs/libvpx/test/comp_avg_pred_test.cc
+++ b/libs/libvpx/test/comp_avg_pred_test.cc
@@ -29,6 +29,10 @@ uint8_t avg_with_rounding(uint8_t a, uint8_t b) { return (a + b + 1) >> 1; }
void reference_pred(const Buffer &pred, const Buffer &ref,
int width, int height, Buffer