Index: /pjproject/trunk/aconfigure
===================================================================
--- /pjproject/trunk/aconfigure	(revision 4068)
+++ /pjproject/trunk/aconfigure	(revision 4069)
@@ -606,4 +606,6 @@
 ac_v4l2_ldflags
 ac_v4l2_cflags
+PKG_CONFIG
+SAVED_PKG_CONFIG_PATH
 ac_ffmpeg_ldflags
 ac_ffmpeg_cflags
@@ -6043,152 +6045,108 @@
 
 
-				{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for deflate in -lz" >&5
-$as_echo_n "checking for deflate in -lz... " >&6; }
-if test "${ac_cv_lib_z_deflate+set}" = set; then :
-  $as_echo_n "(cached) " >&6
-else
-  ac_check_lib_save_LIBS=$LIBS
-LIBS="-lz  $LIBS"
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h.  */
-
-/* Override any GCC internal prototype to avoid an error.
-   Use char because int might match the return type of a GCC
-   builtin and then its argument prototype would still apply.  */
-#ifdef __cplusplus
-extern "C"
-#endif
-char deflate ();
-int
-main ()
-{
-return deflate ();
-  ;
-  return 0;
-}
-_ACEOF
-if ac_fn_c_try_link "$LINENO"; then :
-  ac_cv_lib_z_deflate=yes
-else
-  ac_cv_lib_z_deflate=no
-fi
-rm -f core conftest.err conftest.$ac_objext \
-    conftest$ac_exeext conftest.$ac_ext
-LIBS=$ac_check_lib_save_LIBS
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_z_deflate" >&5
-$as_echo "$ac_cv_lib_z_deflate" >&6; }
-if test "x$ac_cv_lib_z_deflate" = x""yes; then :
-  cat >>confdefs.h <<_ACEOF
-#define HAVE_LIBZ 1
-_ACEOF
-
-  LIBS="-lz $LIBS"
-
-fi
-
-		{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for x264_free in -lx264" >&5
-$as_echo_n "checking for x264_free in -lx264... " >&6; }
-if test "${ac_cv_lib_x264_x264_free+set}" = set; then :
-  $as_echo_n "(cached) " >&6
-else
-  ac_check_lib_save_LIBS=$LIBS
-LIBS="-lx264  $LIBS"
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h.  */
-
-/* Override any GCC internal prototype to avoid an error.
-   Use char because int might match the return type of a GCC
-   builtin and then its argument prototype would still apply.  */
-#ifdef __cplusplus
-extern "C"
-#endif
-char x264_free ();
-int
-main ()
-{
-return x264_free ();
-  ;
-  return 0;
-}
-_ACEOF
-if ac_fn_c_try_link "$LINENO"; then :
-  ac_cv_lib_x264_x264_free=yes
-else
-  ac_cv_lib_x264_x264_free=no
-fi
-rm -f core conftest.err conftest.$ac_objext \
-    conftest$ac_exeext conftest.$ac_ext
-LIBS=$ac_check_lib_save_LIBS
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_x264_x264_free" >&5
-$as_echo "$ac_cv_lib_x264_x264_free" >&6; }
-if test "x$ac_cv_lib_x264_x264_free" = x""yes; then :
-  cat >>confdefs.h <<_ACEOF
-#define HAVE_LIBX264 1
-_ACEOF
-
-  LIBS="-lx264 $LIBS"
-
-fi
-
-
-				{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for BZ2_bzDecompressInit in -lbz2" >&5
-$as_echo_n "checking for BZ2_bzDecompressInit in -lbz2... " >&6; }
-if test "${ac_cv_lib_bz2_BZ2_bzDecompressInit+set}" = set; then :
-  $as_echo_n "(cached) " >&6
-else
-  ac_check_lib_save_LIBS=$LIBS
-LIBS="-lbz2  $LIBS"
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h.  */
-
-/* Override any GCC internal prototype to avoid an error.
-   Use char because int might match the return type of a GCC
-   builtin and then its argument prototype would still apply.  */
-#ifdef __cplusplus
-extern "C"
-#endif
-char BZ2_bzDecompressInit ();
-int
-main ()
-{
-return BZ2_bzDecompressInit ();
-  ;
-  return 0;
-}
-_ACEOF
-if ac_fn_c_try_link "$LINENO"; then :
-  ac_cv_lib_bz2_BZ2_bzDecompressInit=yes
-else
-  ac_cv_lib_bz2_BZ2_bzDecompressInit=no
-fi
-rm -f core conftest.err conftest.$ac_objext \
-    conftest$ac_exeext conftest.$ac_ext
-LIBS=$ac_check_lib_save_LIBS
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_bz2_BZ2_bzDecompressInit" >&5
-$as_echo "$ac_cv_lib_bz2_BZ2_bzDecompressInit" >&6; }
-if test "x$ac_cv_lib_bz2_BZ2_bzDecompressInit" = x""yes; then :
-  cat >>confdefs.h <<_ACEOF
-#define HAVE_LIBBZ2 1
-_ACEOF
-
-  LIBS="-lbz2 $LIBS"
-
-fi
-
-
 		FFMPEG_PREFIX=""
+
+		SAVED_PKG_CONFIG_PATH=$PKG_CONFIG_PATH
     		if test "x$with_ffmpeg" != "xno" -a "x$with_ffmpeg" != "x"; then
 			FFMPEG_PREFIX=$with_ffmpeg
 			{ $as_echo "$as_me:${as_lineno-$LINENO}: result: Using ffmpeg prefix... $FFMPEG_PREFIX" >&5
 $as_echo "Using ffmpeg prefix... $FFMPEG_PREFIX" >&6; }
+			PKG_CONFIG_PATH=$FFMPEG_PREFIX/lib/pkgconfig
+		fi
+
+		for ac_prog in pkg-config "python pkgconfig.py"
+do
+  # Extract the first word of "$ac_prog", so it can be a program name with args.
+set dummy $ac_prog; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if test "${ac_cv_prog_PKG_CONFIG+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+  if test -n "$PKG_CONFIG"; then
+  ac_cv_prog_PKG_CONFIG="$PKG_CONFIG" # Let the user override the test.
+else
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if { test -f "$as_dir/$ac_word$ac_exec_ext" && $as_test_x "$as_dir/$ac_word$ac_exec_ext"; }; then
+    ac_cv_prog_PKG_CONFIG="$ac_prog"
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" >&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+fi
+fi
+PKG_CONFIG=$ac_cv_prog_PKG_CONFIG
+if test -n "$PKG_CONFIG"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $PKG_CONFIG" >&5
+$as_echo "$PKG_CONFIG" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+
+  test -n "$PKG_CONFIG" && break
+done
+test -n "$PKG_CONFIG" || PKG_CONFIG="none"
+
+
+		if test "$PKG_CONFIG" != "none"; then
+			{ $as_echo "$as_me:${as_lineno-$LINENO}: checking ffmpeg packages" >&5
+$as_echo_n "checking ffmpeg packages... " >&6; }
+			av_pkg=""
+			if $PKG_CONFIG --exists libdevice; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVDEVICE=1"
+				av_pkg="$av_pkg libdevice"
+			fi
+			if $PKG_CONFIG --exists libavformat; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVFORMAT=1"
+				av_pkg="$av_pkg libavformat"
+			fi
+			if $PKG_CONFIG --exists libavcodec; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVCODEC=1"
+				av_pkg="$av_pkg libavcodec"
+			fi
+			if $PKG_CONFIG --exists libswscale; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBSWSCALE=1"
+				av_pkg="$av_pkg libswscale"
+			fi
+			if $PKG_CONFIG --exists libavutil; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVUTIL=1"
+				av_pkg="$av_pkg libavutil"
+			fi
+			if $PKG_CONFIG --exists libavcore; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVCORE=1"
+				av_pkg="$av_pkg libavcore"
+			fi
+
+			if test "x$av_pkg" == "x"; then
+			   { $as_echo "$as_me:${as_lineno-$LINENO}: result: none detected (check the prefix)! **" >&5
+$as_echo "none detected (check the prefix)! **" >&6; }
+			else
+			   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $av_pkg" >&5
+$as_echo "$av_pkg" >&6; }
+			fi
+
+			ac_ffmpeg_cflags="$ac_ffmpeg_cflags `$PKG_CONFIG --cflags $av_pkg`"
+			ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags `$PKG_CONFIG --libs $av_pkg`"
+
+		else
+
+			{ $as_echo "$as_me:${as_lineno-$LINENO}: result: *** Warning: neither pkg-config nor python is available, ffmpeg dependency cannot be calculated. If ffmpeg libraries are not detected, you need to specify the correct CFLAGS and LDFLAGS settings for ffmpeg prior to invoking configure ***" >&5
+$as_echo "*** Warning: neither pkg-config nor python is available, ffmpeg dependency cannot be calculated. If ffmpeg libraries are not detected, you need to specify the correct CFLAGS and LDFLAGS settings for ffmpeg prior to invoking configure ***" >&6; }
+
 			LIBS="-L$FFMPEG_PREFIX/lib $LIBS"
 			LDFLAGS="-L$FFMPEG_PREFIX/lib $LDFLAGS"
 			CFLAGS="-I$FFMPEG_PREFIX/include $CFLAGS"
-		fi
-
-		{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for avdevice_version in -lavdevice" >&5
+
+			{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for avdevice_version in -lavdevice" >&5
 $as_echo_n "checking for avdevice_version in -lavdevice... " >&6; }
 if test "${ac_cv_lib_avdevice_avdevice_version+set}" = set; then :
@@ -6228,10 +6186,96 @@
 if test "x$ac_cv_lib_avdevice_avdevice_version" = x""yes; then :
   ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVDEVICE=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavdevice"
-
-
-fi
-
-		{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for av_register_all in -lavformat" >&5
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavdevice"
+
+
+fi
+
+			{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for av_malloc in -lavutil" >&5
+$as_echo_n "checking for av_malloc in -lavutil... " >&6; }
+if test "${ac_cv_lib_avutil_av_malloc+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-lavutil  $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char av_malloc ();
+int
+main ()
+{
+return av_malloc ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  ac_cv_lib_avutil_av_malloc=yes
+else
+  ac_cv_lib_avutil_av_malloc=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_avutil_av_malloc" >&5
+$as_echo "$ac_cv_lib_avutil_av_malloc" >&6; }
+if test "x$ac_cv_lib_avutil_av_malloc" = x""yes; then :
+  ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVUTIL=1"
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavutil"
+
+
+fi
+
+			{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for avcodec_init in -lavcodec" >&5
+$as_echo_n "checking for avcodec_init in -lavcodec... " >&6; }
+if test "${ac_cv_lib_avcodec_avcodec_init+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-lavcodec -lavutil
+				      $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char avcodec_init ();
+int
+main ()
+{
+return avcodec_init ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  ac_cv_lib_avcodec_avcodec_init=yes
+else
+  ac_cv_lib_avcodec_avcodec_init=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_avcodec_avcodec_init" >&5
+$as_echo "$ac_cv_lib_avcodec_avcodec_init" >&6; }
+if test "x$ac_cv_lib_avcodec_avcodec_init" = x""yes; then :
+  ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVCODEC=1"
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavcodec"
+
+fi
+
+			{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for av_register_all in -lavformat" >&5
 $as_echo_n "checking for av_register_all in -lavformat... " >&6; }
 if test "${ac_cv_lib_avformat_av_register_all+set}" = set; then :
@@ -6240,5 +6284,5 @@
   ac_check_lib_save_LIBS=$LIBS
 LIBS="-lavformat -lavcodec -lavutil
-			      $LIBS"
+				      $LIBS"
 cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
@@ -6272,52 +6316,9 @@
 if test "x$ac_cv_lib_avformat_av_register_all" = x""yes; then :
   ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVFORMAT=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavformat"
-
-fi
-
-		{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for avcodec_init in -lavcodec" >&5
-$as_echo_n "checking for avcodec_init in -lavcodec... " >&6; }
-if test "${ac_cv_lib_avcodec_avcodec_init+set}" = set; then :
-  $as_echo_n "(cached) " >&6
-else
-  ac_check_lib_save_LIBS=$LIBS
-LIBS="-lavcodec -lavutil
-			      $LIBS"
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h.  */
-
-/* Override any GCC internal prototype to avoid an error.
-   Use char because int might match the return type of a GCC
-   builtin and then its argument prototype would still apply.  */
-#ifdef __cplusplus
-extern "C"
-#endif
-char avcodec_init ();
-int
-main ()
-{
-return avcodec_init ();
-  ;
-  return 0;
-}
-_ACEOF
-if ac_fn_c_try_link "$LINENO"; then :
-  ac_cv_lib_avcodec_avcodec_init=yes
-else
-  ac_cv_lib_avcodec_avcodec_init=no
-fi
-rm -f core conftest.err conftest.$ac_objext \
-    conftest$ac_exeext conftest.$ac_ext
-LIBS=$ac_check_lib_save_LIBS
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_avcodec_avcodec_init" >&5
-$as_echo "$ac_cv_lib_avcodec_avcodec_init" >&6; }
-if test "x$ac_cv_lib_avcodec_avcodec_init" = x""yes; then :
-  ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVCODEC=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavcodec"
-
-fi
-
-		{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for sws_scale in -lswscale" >&5
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavformat"
+
+fi
+
+			{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for sws_scale in -lswscale" >&5
 $as_echo_n "checking for sws_scale in -lswscale... " >&6; }
 if test "${ac_cv_lib_swscale_sws_scale+set}" = set; then :
@@ -6326,5 +6327,5 @@
   ac_check_lib_save_LIBS=$LIBS
 LIBS="-lswscale -lavutil
-			      $LIBS"
+				      $LIBS"
 cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
@@ -6358,52 +6359,9 @@
 if test "x$ac_cv_lib_swscale_sws_scale" = x""yes; then :
   ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBSWSCALE=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lswscale"
-
-fi
-
-		{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for av_malloc in -lavutil" >&5
-$as_echo_n "checking for av_malloc in -lavutil... " >&6; }
-if test "${ac_cv_lib_avutil_av_malloc+set}" = set; then :
-  $as_echo_n "(cached) " >&6
-else
-  ac_check_lib_save_LIBS=$LIBS
-LIBS="-lavutil  $LIBS"
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
-/* end confdefs.h.  */
-
-/* Override any GCC internal prototype to avoid an error.
-   Use char because int might match the return type of a GCC
-   builtin and then its argument prototype would still apply.  */
-#ifdef __cplusplus
-extern "C"
-#endif
-char av_malloc ();
-int
-main ()
-{
-return av_malloc ();
-  ;
-  return 0;
-}
-_ACEOF
-if ac_fn_c_try_link "$LINENO"; then :
-  ac_cv_lib_avutil_av_malloc=yes
-else
-  ac_cv_lib_avutil_av_malloc=no
-fi
-rm -f core conftest.err conftest.$ac_objext \
-    conftest$ac_exeext conftest.$ac_ext
-LIBS=$ac_check_lib_save_LIBS
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_avutil_av_malloc" >&5
-$as_echo "$ac_cv_lib_avutil_av_malloc" >&6; }
-if test "x$ac_cv_lib_avutil_av_malloc" = x""yes; then :
-  ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVUTIL=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavutil"
-
-
-fi
-
-		{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for avcore_version in -lavcore" >&5
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lswscale"
+
+fi
+
+			{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for avcore_version in -lavcore" >&5
 $as_echo_n "checking for avcore_version in -lavcore... " >&6; }
 if test "${ac_cv_lib_avcore_avcore_version+set}" = set; then :
@@ -6443,10 +6401,14 @@
 if test "x$ac_cv_lib_avcore_avcore_version" = x""yes; then :
   ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVCORE=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavcore"
-
-
-fi
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavcore"
+
+
+fi
+
+
+		fi
 
 		LIBS="$LIBS $ac_ffmpeg_ldflags"
+		PKG_CONFIG_PATH=$SAVED_PKG_CONFIG_PATH
 
 
Index: /pjproject/trunk/aconfigure.ac
===================================================================
--- /pjproject/trunk/aconfigure.ac	(revision 4068)
+++ /pjproject/trunk/aconfigure.ac	(revision 4069)
@@ -852,63 +852,110 @@
 		AC_SUBST(ac_ffmpeg_ldflags)
 
-		dnl # libz, needed by newer ffmpeg
-		AC_CHECK_LIB(z, deflate)
-		AC_CHECK_LIB(x264, x264_free)
-
-		dnl # libbz2, needed by matroskadec.o in ffmpeg 0.9 in mac os
-		AC_CHECK_LIB(bz2, BZ2_bzDecompressInit)
-
 		FFMPEG_PREFIX=""
+		AC_SUBST(SAVED_PKG_CONFIG_PATH)
+		SAVED_PKG_CONFIG_PATH=$PKG_CONFIG_PATH
     		if test "x$with_ffmpeg" != "xno" -a "x$with_ffmpeg" != "x"; then
 			FFMPEG_PREFIX=$with_ffmpeg
 			AC_MSG_RESULT([Using ffmpeg prefix... $FFMPEG_PREFIX])
+			PKG_CONFIG_PATH=$FFMPEG_PREFIX/lib/pkgconfig
+		fi
+
+		AC_CHECK_PROGS(PKG_CONFIG,pkg-config "python pkgconfig.py",none)
+
+		if test "$PKG_CONFIG" != "none"; then
+			AC_MSG_CHECKING([ffmpeg packages])
+			av_pkg=""
+			if $PKG_CONFIG --exists libdevice; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVDEVICE=1"
+				av_pkg="$av_pkg libdevice"
+			fi
+			if $PKG_CONFIG --exists libavformat; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVFORMAT=1"
+				av_pkg="$av_pkg libavformat"
+			fi
+			if $PKG_CONFIG --exists libavcodec; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVCODEC=1"
+				av_pkg="$av_pkg libavcodec"
+			fi
+			if $PKG_CONFIG --exists libswscale; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBSWSCALE=1"
+				av_pkg="$av_pkg libswscale"
+			fi
+			if $PKG_CONFIG --exists libavutil; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVUTIL=1"
+				av_pkg="$av_pkg libavutil"
+			fi
+			if $PKG_CONFIG --exists libavcore; then
+				ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVCORE=1"
+				av_pkg="$av_pkg libavcore"
+			fi
+			
+			if test "x$av_pkg" == "x"; then
+			   AC_MSG_RESULT([none detected (check the prefix)! **])
+			else
+			   AC_MSG_RESULT([$av_pkg])
+			fi
+			
+			ac_ffmpeg_cflags="$ac_ffmpeg_cflags `$PKG_CONFIG --cflags $av_pkg`"
+			ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags `$PKG_CONFIG --libs $av_pkg`"
+
+		else
+			dnl #
+			dnl # Use hardcoded values to configure ffmpeg
+			dnl #
+			
+			AC_MSG_RESULT([*** Warning: neither pkg-config nor python is available, ffmpeg dependency cannot be calculated. If ffmpeg libraries are not detected, you need to specify the correct CFLAGS and LDFLAGS settings for ffmpeg prior to invoking configure ***])
+			
 			LIBS="-L$FFMPEG_PREFIX/lib $LIBS"
 			LDFLAGS="-L$FFMPEG_PREFIX/lib $LDFLAGS"
 			CFLAGS="-I$FFMPEG_PREFIX/include $CFLAGS"
+			
+			AC_CHECK_LIB(avdevice,
+				     avdevice_version,
+				     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVDEVICE=1"
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavdevice"
+				     ]
+				     )
+			AC_CHECK_LIB(avutil,
+				     av_malloc,
+				     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVUTIL=1"
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavutil"
+				     ]
+				     )
+			AC_CHECK_LIB(avcodec,
+				     avcodec_init,
+				     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVCODEC=1"
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavcodec"
+				     ],
+				     [],
+				     [-lavutil]
+				     )
+			AC_CHECK_LIB(avformat,
+				     av_register_all,
+				     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVFORMAT=1"
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavformat"
+				     ],
+				     [],
+				     [-lavcodec -lavutil]
+				     )
+			AC_CHECK_LIB(swscale,
+				     sws_scale,
+				     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBSWSCALE=1"
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lswscale"
+				     ],
+				     [],
+				     [-lavutil]
+				     )
+			AC_CHECK_LIB(avcore,
+				     avcore_version,
+				     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVCORE=1"
+				      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavcore"
+				     ]
+				     )
+			
 		fi
-
-		AC_CHECK_LIB(avdevice,
-			     avdevice_version,
-			     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVDEVICE=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavdevice"
-			     ]
-			     )
-		AC_CHECK_LIB(avformat,
-			     av_register_all,
-			     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVFORMAT=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavformat"
-			     ],
-			     [],
-			     [-lavcodec -lavutil]
-			     )
-		AC_CHECK_LIB(avcodec,
-			     avcodec_init,
-			     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVCODEC=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavcodec"
-			     ],
-			     [],
-			     [-lavutil]
-			     )
-		AC_CHECK_LIB(swscale,
-			     sws_scale,
-			     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBSWSCALE=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lswscale"
-			     ],
-			     [],
-			     [-lavutil]
-			     )
-		AC_CHECK_LIB(avutil,
-			     av_malloc,
-			     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVUTIL=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavutil"
-			     ]
-			     )
-		AC_CHECK_LIB(avcore,
-			     avcore_version,
-			     [ac_ffmpeg_cflags="$ac_ffmpeg_cflags -DPJMEDIA_HAS_LIBAVCORE=1"
-			      ac_ffmpeg_ldflags="$ac_ffmpeg_ldflags -lavcore"
-			     ]
-			     )
+		
 		LIBS="$LIBS $ac_ffmpeg_ldflags"
+		PKG_CONFIG_PATH=$SAVED_PKG_CONFIG_PATH
 	      ]
 	      )
Index: /pjproject/trunk/pkgconfig.py
===================================================================
--- /pjproject/trunk/pkgconfig.py	(revision 4069)
+++ /pjproject/trunk/pkgconfig.py	(revision 4069)
@@ -0,0 +1,171 @@
+import sys
+import os
+
+REMOVE_THESE = ["-I/usr/include", "-I/usr/include/", "-L/usr/lib", "-L/usr/lib/"]
+
+class Pkg:
+	def __init__(self, pkg_name):
+		self.name = pkg_name
+		self.priority = 0
+		self.vars = {}
+
+	def parse(self, pkg_config_path):
+		f = None
+		for pkg_path in pkg_config_path.split(':'):
+			if pkg_path[-1] != '/':
+				pkg_path += '/'
+			fname = pkg_path + self.name + '.pc'
+			try:
+				f = open(fname, "r")
+				break
+			except:
+				continue
+		if not f:
+		  	#sys.stderr.write("pkgconfig.py: unable to find %s.pc in %s\n" % (self.name, pkg_config_path))
+			return False
+
+		for line in f.readlines():
+			line = line.strip()
+			if not line:
+				continue
+			if line[0]=='#':
+				continue
+			pos1 = line.find('=')
+			pos2 = line.find(':')
+			if pos1 > 0 and (pos1 < pos2 or pos2 < 0):
+				pos = pos1
+			elif pos2 > 0 and (pos2 < pos1 or pos1 < 0):
+				pos = pos2
+			else:
+				continue
+			name = line[:pos].lower()
+			value = line[pos+1:]
+			self.vars[name] = value
+		f.close()
+
+		for name in self.vars.keys():
+			value = self.vars[name]
+			while True:
+				pos1 = value.find("${")
+				if pos1 < 0:
+					break
+				pos2 = value.find("}")
+				if pos2 < 0:
+					break
+				value = value.replace(value[pos1:pos2+1], self.vars[value[pos1+2:pos2]])
+			self.vars[name] = value
+		return True
+
+	def requires(self):
+		if not 'requires' in self.vars:
+			return []
+		deps = []
+		req_list = self.vars['requires']
+		for req_item in req_list.split(','):
+			req_item = req_item.strip()
+			for i in range(len(req_item)):
+				if "=<>".find(req_item[i]) >= 0:
+					deps.append(req_item[:i].strip())
+					break
+		return deps
+
+	def libs(self):
+		if not 'libs' in self.vars:
+			return []
+		return self.vars['libs'].split(' ')
+
+	def cflags(self):
+		if not 'cflags' in self.vars:
+			return []
+		return self.vars['cflags'].split(' ')
+
+
+def calculate_pkg_priority(pkg, pkg_dict, loop_cnt):
+	if loop_cnt > 10:
+		sys.stderr.write("Circular dependency with pkg %s\n" % (pkg))
+		return 0
+	reqs = pkg.requires()
+	prio = 1
+	for req in reqs:
+		if not req in pkg_dict:
+			continue
+		req_pkg = pkg_dict[req]
+		prio += calculate_pkg_priority(req_pkg, pkg_dict, loop_cnt+1)
+	return prio
+
+if __name__ == "__main__":
+	pkg_names = []
+	pkg_dict = {}
+	commands = []
+	exist_check = False
+
+	for i in range(1,len(sys.argv)):
+		if sys.argv[i][0] == '-':
+			cmd = sys.argv[i]
+			commands.append(cmd)
+			if cmd=='--exists':
+				exist_check = True
+			elif cmd=="--help":
+				print "This is not very helpful, is it"
+				sys.exit(0)
+			elif cmd=="--version":
+				print "0.1"
+				sys.exit(0)
+		else:
+			pkg_names.append(sys.argv[i])
+			
+	# Fix search path
+	PKG_CONFIG_PATH = os.getenv("PKG_CONFIG_PATH", "").strip()
+	if not PKG_CONFIG_PATH:
+		PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/usr/lib/pkgconfig"
+	PKG_CONFIG_PATH = PKG_CONFIG_PATH.replace(";", ":")
+
+	# Parse files
+	for pkg_name in pkg_names:
+		pkg = Pkg(pkg_name)
+		if not pkg.parse(PKG_CONFIG_PATH):
+			sys.exit(1)
+		pkg_dict[pkg_name] = pkg
+
+	if exist_check:
+		sys.exit(0)
+
+	# Calculate priority based on dependency
+	for pkg_name in pkg_dict.keys():
+		pkg = pkg_dict[pkg_name]
+		pkg.priority = calculate_pkg_priority(pkg, pkg_dict, 1)
+	
+	# Sort package based on dependency
+	pkg_names = sorted(pkg_names, key=lambda pkg_name: pkg_dict[pkg_name].priority, reverse=True)
+
+	# Get the options
+	opts = []
+	for cmd in commands:
+		if cmd=='--libs':
+			for pkg_name in pkg_names:
+				libs = pkg_dict[pkg_name].libs()
+				for lib in libs:
+					opts.append(lib)
+					if lib[:2]=="-l":
+						break
+			for pkg_name in pkg_names:
+				opts += pkg_dict[pkg_name].libs()
+		elif cmd=='--cflags':
+			for pkg_name in pkg_names:
+				opts += pkg_dict[pkg_name].cflags()
+		elif cmd[0]=='-':
+			sys.stderr.write("pkgconfig.py: I don't know how to handle " + sys.argv[i] + "\n")
+	
+	filtered_opts = []
+	for opt in opts:
+		opt = opt.strip()
+		if not opt:
+			continue
+		if REMOVE_THESE.count(opt) != 0:
+			continue
+		if filtered_opts.count(opt) != 0:
+			continue
+		filtered_opts.append(opt)
+
+	print ' '.join(filtered_opts)
+
