$OpenBSD: patch-modules_highgui_src_cap_ffmpeg_cpp,v 1.6 2013/01/19 10:13:15 brad Exp $

Update for newer FFmpeg API.

--- modules/highgui/src/cap_ffmpeg.cpp.orig	Sat Dec  4 22:35:25 2010
+++ modules/highgui/src/cap_ffmpeg.cpp	Fri Jan 18 16:45:53 2013
@@ -53,7 +53,7 @@ extern "C" {
 #define UINT64_C
 #define __STDC_CONSTANT_MACROS
 // force re-inclusion of stdint.h to get INT64_C macro
-#undef _STDINT_H
+#undef _SYS_STDINT_H_
 #include <stdint.h>
 #endif
 #include <errno.h>
@@ -109,6 +109,8 @@ extern "C" {
 
 }
 
+#define CALC_FFMPEG_VERSION(a,b,c) ( a<<16 | b<<8 | c )
+
 #if defined _MSC_VER && _MSC_VER >= 1200
 #pragma warning( default: 4244 4510 4512 4610 )
 #endif
@@ -384,7 +386,11 @@ void CvCapture_FFMPEG::close()
 
     if( ic )
     {
+#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 24, 2)
+        avformat_close_input(&ic);
+#else
         av_close_input_file(ic);
+#endif
         ic = NULL;
     }
 
@@ -413,18 +419,35 @@ bool CvCapture_FFMPEG::reopen()
 #else
     avcodec_close( &video_st->codec );
 #endif
+#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 24, 2)
+    avformat_close_input(&ic);
+#else
     av_close_input_file(ic);
+#endif
 
     // reopen video
+#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
+    avformat_open_input(&ic, filename, NULL, NULL);
+#else
     av_open_input_file(&ic, filename, NULL, 0, NULL);
+#endif
+
+#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 3, 0)
+    avformat_find_stream_info(ic, NULL);
+#else
     av_find_stream_info(ic);
+#endif
 #if LIBAVFORMAT_BUILD > 4628
     AVCodecContext *enc = ic->streams[video_stream]->codec;
 #else
     AVCodecContext *enc = &ic->streams[video_stream]->codec;
 #endif
     AVCodec *codec = avcodec_find_decoder(enc->codec_id);
+#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
+    avcodec_open2(enc, codec, NULL);
+#else
     avcodec_open(enc, codec);
+#endif
     video_st = ic->streams[video_stream];
 
     // reset framenumber to zero
@@ -449,12 +472,22 @@ bool CvCapture_FFMPEG::open( const char* _filename )
     // av_log_level = AV_LOG_QUIET;
 #endif
 
+#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
+    int err = avformat_open_input(&ic, _filename, NULL, NULL);
+#else
     int err = av_open_input_file(&ic, _filename, NULL, 0, NULL);
+#endif
+
     if (err < 0) {
 	    CV_WARN("Error opening file");
 	    goto exit_func;
     }
-    err = av_find_stream_info(ic);
+    err =
+#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 3, 0)
+    avformat_find_stream_info(ic, NULL);
+#else
+    av_find_stream_info(ic);
+#endif
     if (err < 0) {
 	    CV_WARN("Could not find codec parameters");
 	    goto exit_func;
@@ -466,10 +499,15 @@ bool CvCapture_FFMPEG::open( const char* _filename )
         AVCodecContext *enc = &ic->streams[i]->codec;
 #endif
 
-        if( CODEC_TYPE_VIDEO == enc->codec_type && video_stream < 0) {
+        if( AVMEDIA_TYPE_VIDEO == enc->codec_type && video_stream < 0) {
             AVCodec *codec = avcodec_find_decoder(enc->codec_id);
             if (!codec ||
-            avcodec_open(enc, codec) < 0)
+#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
+            avcodec_open2(enc, codec, NULL)
+#else
+            avcodec_open(enc, codec)
+#endif
+            < 0)
             goto exit_func;
             video_stream = i;
             video_st = ic->streams[i];
@@ -550,7 +588,9 @@ bool CvCapture_FFMPEG::grabFrame()
 		        continue;
     		}
 
-#if LIBAVFORMAT_BUILD > 4628
+#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
+        avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);
+#elif LIBAVFORMAT_BUILD > 4628
         avcodec_decode_video(video_st->codec,
                              picture, &got_picture,
                              packet.data, packet.size);
@@ -806,15 +846,15 @@ class CvVideoWriter_FFMPEG : public CvVideoWriter (pro
 static const char * icvFFMPEGErrStr(int err)
 {
     switch(err) {
-    case AVERROR_NUMEXPECTED:
+    case AVERROR(EDOM):
 		return "Incorrect filename syntax";
-    case AVERROR_INVALIDDATA:
+    case AVERROR(EINVAL):
 		return "Invalid data in header";
-    case AVERROR_NOFMT:
+    case AVERROR(EILSEQ):
 		return "Unknown format";
-    case AVERROR_IO:
+    case AVERROR(EIO):
 		return "I/O error occurred";
-    case AVERROR_NOMEM:
+    case AVERROR(ENOMEM):
 		return "Memory allocation error";
     default:
 		break;
@@ -885,8 +925,12 @@ static AVStream *icv_add_video_stream_FFMPEG(AVFormatC
 	int frame_rate, frame_rate_base;
 	AVCodec *codec;
 
-
+#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 10, 0)
+	st = avformat_new_stream(oc, 0);
+#else
 	st = av_new_stream(oc, 0);
+#endif
+
 	if (!st) {
 		CV_WARN("Could not allocate stream");
 		return NULL;
@@ -899,7 +943,7 @@ static AVStream *icv_add_video_stream_FFMPEG(AVFormatC
 #endif
 
 #if LIBAVFORMAT_BUILD > 4621
-	c->codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_VIDEO);
+	c->codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
 #else
 	c->codec_id = oc->oformat->video_codec;
 #endif
@@ -911,7 +955,7 @@ static AVStream *icv_add_video_stream_FFMPEG(AVFormatC
     //if(codec_tag) c->codec_tag=codec_tag;
 	codec = avcodec_find_encoder(c->codec_id);
 
-	c->codec_type = CODEC_TYPE_VIDEO;
+	c->codec_type = AVMEDIA_TYPE_VIDEO;
 
 	/* put sample parameters */
 	c->bit_rate = bitrate;
@@ -998,7 +1042,7 @@ int icv_av_write_frame_FFMPEG( AVFormatContext * oc, A
         AVPacket pkt;
         av_init_packet(&pkt);
 
-        pkt.flags |= PKT_FLAG_KEY;
+        pkt.flags |= AV_PKT_FLAG_KEY;
         pkt.stream_index= video_st->index;
         pkt.data= (uint8_t *)picture;
         pkt.size= sizeof(AVPicture);
@@ -1018,7 +1062,7 @@ int icv_av_write_frame_FFMPEG( AVFormatContext * oc, A
 			pkt.pts = c->coded_frame->pts;
 #endif
             if(c->coded_frame->key_frame)
-                pkt.flags |= PKT_FLAG_KEY;
+                pkt.flags |= AV_PKT_FLAG_KEY;
             pkt.stream_index= video_st->index;
             pkt.data= outbuf;
             pkt.size= out_size;
@@ -1180,12 +1224,15 @@ void CvVideoWriter_FFMPEG::close()
 	if (!(fmt->flags & AVFMT_NOFILE)) {
 		/* close the output file */
 
-
+#if LIBAVCODEC_VERSION_INT < ((52<<16)+(123<<8)+0)
 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(49<<8)+0)
 		url_fclose(oc->pb);
 #else
 		url_fclose(&oc->pb);
 #endif
+#else
+		avio_close(oc->pb);
+#endif
 
 	}
 
@@ -1215,7 +1262,13 @@ bool CvVideoWriter_FFMPEG::open( const char * filename
 	av_register_all ();
 
 	/* auto detect the output format from the name and fourcc code. */
+
+#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
+	fmt = av_guess_format(NULL, filename, NULL);
+#else
 	fmt = guess_format(NULL, filename, NULL);
+#endif
+
 	if (!fmt)
         return false;
 
@@ -1238,7 +1291,7 @@ bool CvVideoWriter_FFMPEG::open( const char * filename
 #endif
 
     // alloc memory for context
-	oc = av_alloc_format_context();
+	oc = avformat_alloc_context();
 	assert (oc);
 
 	/* set file name */
@@ -1279,11 +1332,15 @@ bool CvVideoWriter_FFMPEG::open( const char * filename
 
 	/* set the output parameters (must be done even if no
        parameters). */
+#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
     if (av_set_parameters(oc, NULL) < 0) {
 		CV_Error(CV_StsBadArg, "Invalid output format parameters");
     }
+#endif
 
+#if 0
     dump_format(oc, 0, filename, 1);
+#endif
 
     /* now that all the parameters are set, we can open the audio and
        video codecs and allocate the necessary encode buffers */
@@ -1308,7 +1365,13 @@ bool CvVideoWriter_FFMPEG::open( const char * filename
     }
 
     /* open the codec */
-    if ( (err=avcodec_open(c, codec)) < 0) {
+    if ( (err=
+#if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0)
+          avcodec_open2(c, codec, NULL)
+#else
+          avcodec_open(c, codec)
+#endif
+          ) < 0) {
 		char errtext[256];
 		sprintf(errtext, "Could not open codec '%s': %s", codec->name, icvFFMPEGErrStr(err));
 		CV_Error(CV_StsBadArg, errtext);
@@ -1345,13 +1408,23 @@ bool CvVideoWriter_FFMPEG::open( const char * filename
 
 	/* open the output file, if needed */
     if (!(fmt->flags & AVFMT_NOFILE)) {
-        if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
+        if (
+#if LIBAVFORMAT_BUILD < CALC_FFMPEG_VERSION(53, 2, 0)
+        url_fopen(&oc->pb, filename, URL_WRONLY)
+#else
+        avio_open(&oc->pb, filename, AVIO_FLAG_WRITE)
+#endif
+        < 0) {
 			CV_Error(CV_StsBadArg, "Couldn't open output file for writing");
         }
     }
 
     /* write the stream header, if any */
+#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)
+    avformat_write_header(oc, NULL);
+#else
     av_write_header( oc );
+#endif
 
 	return true;
 }
