$OpenBSD: patch-channels_drdynvc_tsmf_ffmpeg_tsmf_ffmpeg_c,v 1.1 2014/04/23 22:10:50 brad Exp $

Deal with newer FFmpeg API.

--- channels/drdynvc/tsmf/ffmpeg/tsmf_ffmpeg.c.orig	Tue Apr 22 21:21:53 2014
+++ channels/drdynvc/tsmf/ffmpeg/tsmf_ffmpeg.c	Tue Apr 22 21:47:15 2014
@@ -34,12 +34,22 @@
 #define AVMEDIA_TYPE_AUDIO 1
 #endif
 
+#if LIBAVCODEC_VERSION_MAJOR < 54
+#define MAX_AUDIO_FRAME_SIZE AVCODEC_MAX_AUDIO_FRAME_SIZE
+#else
+#define MAX_AUDIO_FRAME_SIZE 192000
+#endif
+
 typedef struct _TSMFFFmpegDecoder
 {
 	ITSMFDecoder iface;
 
 	int media_type;
+#if LIBAVCODEC_VERSION_MAJOR < 55
 	enum CodecID codec_id;
+#else
+	enum AVCodecID codec_id;
+#endif
 	AVCodecContext* codec_context;
 	AVCodec* codec;
 	AVFrame* frame;
@@ -54,7 +64,7 @@ static boolean tsmf_ffmpeg_init_context(ITSMFDecoder* 
 {
 	TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
 
-	mdecoder->codec_context = avcodec_alloc_context();
+	mdecoder->codec_context = avcodec_alloc_context3(NULL);
 	if (!mdecoder->codec_context)
 	{
 		DEBUG_WARN("avcodec_alloc_context failed.");
@@ -88,6 +98,7 @@ static boolean tsmf_ffmpeg_init_audio_stream(ITSMFDeco
 	mdecoder->codec_context->channels = media_type->Channels;
 	mdecoder->codec_context->block_align = media_type->BlockAlign;
 
+#if LIBAVCODEC_VERSION_MAJOR < 55
 #ifdef AV_CPU_FLAG_SSE2
 	mdecoder->codec_context->dsp_mask = AV_CPU_FLAG_SSE2 | AV_CPU_FLAG_MMX2;
 #else
@@ -97,6 +108,13 @@ static boolean tsmf_ffmpeg_init_audio_stream(ITSMFDeco
 	mdecoder->codec_context->dsp_mask = FF_MM_SSE2 | FF_MM_MMX2;
 #endif
 #endif
+#else /* LIBAVCODEC_VERSION_MAJOR < 55 */
+#ifdef AV_CPU_FLAG_SSE2
+	av_set_cpu_flags_mask(AV_CPU_FLAG_SSE2 | AV_CPU_FLAG_MMX2);
+#else
+	av_set_cpu_flags_mask(FF_MM_SSE2 | FF_MM_MMX2);
+#endif
+#endif /* LIBAVCODEC_VERSION_MAJOR < 55 */
 
 	return true;
 }
@@ -174,9 +192,9 @@ static boolean tsmf_ffmpeg_prepare(ITSMFDecoder* decod
 {
 	TSMFFFmpegDecoder* mdecoder = (TSMFFFmpegDecoder*) decoder;
 
-	if (avcodec_open(mdecoder->codec_context, mdecoder->codec) < 0)
+	if (avcodec_open2(mdecoder->codec_context, mdecoder->codec, NULL) < 0)
 	{
-		DEBUG_WARN("avcodec_open failed.");
+		DEBUG_WARN("avcodec_open2 failed.");
 		return false;
 	}
 
@@ -337,7 +355,7 @@ static boolean tsmf_ffmpeg_decode_audio(ITSMFDecoder* 
 #endif
 
 	if (mdecoder->decoded_size_max == 0)
-		mdecoder->decoded_size_max = AVCODEC_MAX_AUDIO_FRAME_SIZE + 16;
+		mdecoder->decoded_size_max = MAX_AUDIO_FRAME_SIZE + 16;
 	mdecoder->decoded_data = xzalloc(mdecoder->decoded_size_max);
 	/* align the memory for SSE2 needs */
 	dst = (uint8*) (((uintptr_t)mdecoder->decoded_data + 15) & ~ 0x0F);
@@ -348,7 +366,7 @@ static boolean tsmf_ffmpeg_decode_audio(ITSMFDecoder* 
 	while (src_size > 0)
 	{
 		/* Ensure enough space for decoding */
-		if (mdecoder->decoded_size_max - mdecoder->decoded_size < AVCODEC_MAX_AUDIO_FRAME_SIZE)
+		if (mdecoder->decoded_size_max - mdecoder->decoded_size < MAX_AUDIO_FRAME_SIZE)
 		{
 			mdecoder->decoded_size_max = mdecoder->decoded_size_max * 2 + 16;
 			mdecoder->decoded_data = xrealloc(mdecoder->decoded_data, mdecoder->decoded_size_max);
@@ -368,12 +386,22 @@ static boolean tsmf_ffmpeg_decode_audio(ITSMFDecoder* 
 			src, src_size);
 #else
 		{
+			AVFrame* decoded_frame = avcodec_alloc_frame();
+			int got_frame = 0;
 			AVPacket pkt;
 			av_init_packet(&pkt);
 			pkt.data = (uint8*) src;
 			pkt.size = src_size;
-			len = avcodec_decode_audio3(mdecoder->codec_context,
-				(int16_t*) dst, &frame_size, &pkt);
+			len = avcodec_decode_audio4(mdecoder->codec_context, decoded_frame, &got_frame, &pkt);
+
+			if (len >= 0 && got_frame)
+			{
+				frame_size = av_samples_get_buffer_size(NULL, mdecoder->codec_context->channels,
+					decoded_frame->nb_samples, mdecoder->codec_context->sample_fmt, 1);
+				memcpy(dst, decoded_frame->data[0], frame_size);
+			}
+
+			av_free(decoded_frame);
 		}
 #endif
 		if (len <= 0 || frame_size <= 0)
@@ -499,7 +527,6 @@ TSMFDecoderEntry(void)
 
 	if (!initialized)
 	{
-		avcodec_init();
 		avcodec_register_all();
 		initialized = true;
 	}
