# HG changeset patch
# User PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
# Date 1446189135 -19800
#      Fri Oct 30 12:42:15 2015 +0530
# Node ID 2d0e7456ddb13d6e1233ad7a0d91b03d0d4ccb4f
# Parent  31b9a4c9ad97cd6716f235aac7fb3c241a88640f
Update to compile with ffmpeg > 1.0.0

* configure.ac: check for av_set_parameters, av_codec_is_encoder functions, use pkg-config if available

* src/config.h.in: add HAVE_AV_SET_PARAMETERS, HAVE_AV_CODEC_IS_ENCODER

* src/AVHandler.cc: Use av_set_paramters if available, set URL_WRONLY to AVIO_FLAG_WRITE if not available

* src/Makefile.in: use CPPFLAGS from configure to build, use MKOCTFILE variable

diff -r 31b9a4c9ad97 -r 2d0e7456ddb1 src/AVHandler.cc
--- a/src/AVHandler.cc	Tue Sep 03 07:50:42 2013 -0400
+++ b/src/AVHandler.cc	Fri Oct 30 12:42:15 2015 +0530
@@ -120,18 +120,22 @@
         if (add_video_stream() != 0) return -1;
     }
     
+#ifdef HAVE_AV_SET_PARAMETERS
     /* av_set_parameters is mandatory */
-    // FIXME: deprecated, but there's no replacement yet
     if (av_set_parameters(av_output, NULL) < 0) {
         (*out) << "AVHandler: Error setting output format parameters" << std::endl;
         return -1;
     }
+#endif
 
     snprintf(av_output->filename, sizeof(av_output->filename), "%s", filename.c_str());
 // FIXME:    snprintf(av_output->title, sizeof(av_output->title), "%s", title.c_str());
 // FIXME:    snprintf(av_output->author, sizeof(av_output->author), "%s", author.c_str());
 // FIXME:    snprintf(av_output->comment, sizeof(av_output->comment), "%s", comment.c_str());
     
+#ifndef URL_WRONLY
+#define URL_WRONLY AVIO_FLAG_WRITE
+#endif
     if (avio_open(&av_output->pb, filename.c_str(), URL_WRONLY) < 0) {
         (*out) << "AVHandler: Could not open \"" << filename << "\" for output" << std::endl;
         return -1;
@@ -383,7 +387,12 @@
     AVCodec *codec;
     for (codec = av_codec_next(0); codec != NULL; codec = av_codec_next(codec)) {
         if ((codec->type == AVMEDIA_TYPE_VIDEO) &&
-            (codec->encode)) {
+#ifdef HAVE_AV_CODEC_IS_ENCODER
+            (av_codec_is_encoder(codec)))
+#else
+            (codec->encode)) 
+#endif
+        {
             (*out) << codec->name << " ";
         }
     }
@@ -461,7 +470,7 @@
                                   vstream->codec->width,
                                   vstream->codec->height);
 
-    frame_buf = (uint8_t *)malloc(size);
+    frame_buf = (uint8_t *)av_malloc(size);
     if (!frame_buf) {
         av_free(frame);
         (*out) << "AVHandler: error initialising frame" << std::endl;
diff -r 31b9a4c9ad97 -r 2d0e7456ddb1 src/Makefile.in
--- a/src/Makefile.in	Tue Sep 03 07:50:42 2013 -0400
+++ b/src/Makefile.in	Fri Oct 30 12:42:15 2015 +0530
@@ -2,6 +2,8 @@
 AVOBJS = AVHandler.o oct-avifile.o
 FILES_WITH_TESTS = avifile.cc addframe.cc
 
+MKOCTFILE ?= mkoctfile
+
 .PHONY: clean
 .PHONY: realclean
 .PHONY: distclean
@@ -11,10 +13,10 @@
 oct-avifile.o: AVHandler.o
 
 ${AVOBJS}: %.o: %.cc
-	mkoctfile -Wall -v -c @DEFS@ $< -o $@
+	$(MKOCTFILE) -Wall -v -c @DEFS@ $< -o $@ @CPPFLAGS@
 
 ${OCTFILES}: %.oct: %.cc AVHandler.o oct-avifile.o
-	mkoctfile -Wall -v @DEFS@ $< $(filter %.o, $^) @LIBS@
+	$(MKOCTFILE) -Wall -v @DEFS@ $< $(filter %.o, $^) @LIBS@ @CPPFLAGS@
 
 clean:
 	rm -f *.o *~ *.orig octave-workspace
diff -r 31b9a4c9ad97 -r 2d0e7456ddb1 src/avifile.cc
--- a/src/avifile.cc	Tue Sep 03 07:50:42 2013 -0400
+++ b/src/avifile.cc	Fri Oct 30 12:42:15 2015 +0530
@@ -90,7 +90,7 @@
     std::string author = "";
     std::string comment = "Created using Octave-Avifile";
 
-    for (unsigned int i = 1; i < args.length(); i++) {
+    for (octave_idx_type i = 1; i < args.length(); i++) {
         std::string p = args(i).string_value();
         octave_value v = args(i+1);
         if (!error_state) {
diff -r 31b9a4c9ad97 -r 2d0e7456ddb1 src/bootstrap
--- a/src/bootstrap	Tue Sep 03 07:50:42 2013 -0400
+++ b/src/bootstrap	Fri Oct 30 12:42:15 2015 +0530
@@ -3,4 +3,5 @@
 ## Run this to generate the configure script
 
 set -e      # halt if unhandled error
+aclocal
 autoconf    # generate configure script
diff -r 31b9a4c9ad97 -r 2d0e7456ddb1 src/config.h.in
--- a/src/config.h.in	Tue Sep 03 07:50:42 2013 -0400
+++ b/src/config.h.in	Fri Oct 30 12:42:15 2015 +0530
@@ -1,4 +1,10 @@
-/* config.h.in.  Generated from configure.ac by autoheader and edited by hand */
+/* config.h.in.  Generated from configure.ac by autoheader.  */
+
+/* Define to 1 if you have the `av_codec_is_encoder' function. */
+#undef HAVE_AV_CODEC_IS_ENCODER
+
+/* Define to 1 if you have the `av_set_parameters' function. */
+#undef HAVE_AV_SET_PARAMETERS
 
 /* Define to 1 if you have the <ffmpeg/avcodec.h> header file. */
 #undef HAVE_FFMPEG_AVCODEC_H
@@ -21,6 +27,9 @@
 /* Define to 1 if you have the <libavformat/avformat.h> header file. */
 #undef HAVE_LIBAVFORMAT_AVFORMAT_H
 
+/* Define to 1 if you have the `avutil' library (-lavutil). */
+#undef HAVE_LIBAVUTIL
+
 /* Define to 1 if you have the `swscale' library (-lswscale). */
 #undef HAVE_LIBSWSCALE
 
@@ -71,7 +80,7 @@
    #define below would cause a syntax error. */
 #undef _UINT8_T
 
-/* workaraound for C++ programs to use C99 makros */
+/* workaround for C++ programs to use C99 macros */
 #undef __STDC_CONSTANT_MACROS
 
 /* Define to rpl_malloc if the replacement function should be used. */
diff -r 31b9a4c9ad97 -r 2d0e7456ddb1 src/configure.ac
--- a/src/configure.ac	Tue Sep 03 07:50:42 2013 -0400
+++ b/src/configure.ac	Fri Oct 30 12:42:15 2015 +0530
@@ -6,6 +6,7 @@
 AC_CONFIG_HEADERS([config.h])
 
 # Checks for programs.
+PKG_PROG_PKG_CONFIG
 AC_PROG_CXX
 AC_LANG(C++)
 
@@ -20,12 +21,36 @@
 
 # Checks for libraries.
 
+if test "x$PKG_CONFIG" = "x"; then
+AC_CHECK_LIB([avutil], [av_mallocz], [],[])
 AC_CHECK_LIB([avformat], [av_write_frame], [],
              [AC_MSG_ERROR([$PACKAGE_NAME requires libavformat from FFmpeg])])
-AC_CHECK_LIB([avcodec], [av_malloc], [],
+AC_CHECK_LIB([avcodec], [avcodec_alloc_frame], [],
              [AC_MSG_ERROR([$PACKAGE_NAME requires libavcodec from FFmpeg])])
 AC_CHECK_LIB([swscale], [sws_scale], [],
              [AC_MSG_ERROR([$PACKAGE_NAME requires libswscale from FFmpeg])])
+else
+PKG_CHECK_MODULES(avutil, [libavutil],
+ [
+   CPPFLAGS="$CPPFLAGS $avutil_CFLAGS"
+   LIBS="$LIBS $avutil_LIBS"
+ ],[])
+PKG_CHECK_MODULES(avformat, [libavformat],
+ [
+   CPPFLAGS="$CPPFLAGS $avformat_CFLAGS"
+   LIBS="$LIBS $avformat_LIBS"
+ ], [AC_MSG_ERROR([$PACKAGE_NAME requires libavformat from FFmpeg])])
+PKG_CHECK_MODULES(avcodec, [libavcodec],
+ [
+   CPPFLAGS="$CPPFLAGS $avcodec_CFLAGS"
+   LIBS="$LIBS $avcodec_LIBS"
+ ], [AC_MSG_ERROR([$PACKAGE_NAME requires libavcodec from FFmpeg])])
+PKG_CHECK_MODULES(swscale, [libswscale],
+ [
+   CPPFLAGS="$CPPFLAGS $swscale_CFLAGS"
+   LIBS="$LIBS $swscale_LIBS"
+ ], [AC_MSG_ERROR([$PACKAGE_NAME requires libswscale from FFmpeg])])
+fi
              
 # Checks for header files.
 
@@ -52,6 +77,10 @@
 AC_FUNC_ERROR_AT_LINE
 AC_FUNC_MALLOC
 
+AC_CHECK_FUNCS([av_set_parameters av_codec_is_encoder],[],[])
+
+AC_SUBST(CPPFLAGS)
+
 AC_CONFIG_FILES([Makefile])
 AC_OUTPUT
 
