$OpenBSD: patch-ffmpeg_tools_c,v 1.1 2009/08/17 00:24:38 jolan Exp $
--- ffmpeg_tools.c.orig	Sat Oct 11 10:41:00 2008
+++ ffmpeg_tools.c	Sun Aug 16 18:42:18 2009
@@ -40,53 +40,100 @@
 #include <php.h>
 
 #include "ffmpeg_tools.h"
-
-#ifdef HAVE_SWSCALER
+#include <avcodec.h>
 #include <swscale.h>
-#endif
 
 /* {{{ ffmpeg_img_convert() 
  * wrapper around ffmpeg image conversion routines
  */
-int ffmpeg_img_convert(AVPicture *dst, int dst_pix_fmt,
-        AVPicture *src, int src_pix_fmt,
-        int src_width, int src_height)
+int phpimg_convert(AVPicture *dst, int dst_pix_fmt,
+        AVPicture *src, int src_pix_fmt, int src_width, int src_height)
 {
-#ifndef HAVE_SWSCALER // No SWSCALER so just use img_convert
-    return img_convert(dst, dst_pix_fmt, 
-            src, src_pix_fmt, src_width, src_height);
-#else // Do swscale convert
-    int result = 0;
     struct SwsContext *sws_ctx = NULL;
 
-    if (src_pix_fmt == dst_pix_fmt) {
-        return 0;
-    }
-
     // TODO: Try to get cached sws_context first
-    sws_ctx = sws_getContext(
-            src_width, src_height, src_pix_fmt, 
+    sws_ctx = sws_getContext(src_width, src_height, 0, 
             src_width, src_height, dst_pix_fmt, 
             SWS_BICUBIC, NULL, NULL, NULL);
 
     if (sws_ctx == NULL){
-        return 1;
+        return 2;
     }
 
-    result = sws_scale(sws_ctx, 
-            src->data, src->linesize,
-            0, src_height,
-            dst->data, dst->linesize);
-
+    sws_scale(sws_ctx, src->data, src->linesize, 0, src_height, dst->data, dst->linesize);
     sws_freeContext(sws_ctx);
 
-    if (result == 0){
-        return 2;
-    }
-#endif // NOT HAVE_SWSCALER
     return 0;
 }
 /* }}} */
+
+
+
+void phpimg_resample(phpImgReSampleContext * context, AVPicture * pxOut, const AVPicture * pxIn)
+{
+    if (context != NULL && context->context != NULL) {
+        AVPicture shiftedInput; // = {0};
+        shiftedInput.data[0] = pxIn->data[0] + pxIn->linesize[0] * 
+            context->bandTop + context->bandLeft;
+        shiftedInput.data[1] = pxIn->data[1] + (pxIn->linesize[1] * 
+                (context->bandTop / 2)) + (context->bandLeft+1) / 2;
+        shiftedInput.data[2] = pxIn->data[2] + (pxIn->linesize[2] * 
+                (context->bandTop / 2)) + (context->bandLeft+1) / 2;
+        shiftedInput.linesize[0] = pxIn->linesize[0];
+        shiftedInput.linesize[1] = pxIn->linesize[1];
+        shiftedInput.linesize[2] = pxIn->linesize[2];
+        sws_scale(context->context, (uint8_t**)shiftedInput.data, 
+                (int*)shiftedInput.linesize, 0, context->height - context->bandBottom - 
+                context->bandTop, pxOut->data, pxOut->linesize);
+    }
+}
+
+phpImgReSampleContext * phpimg_resample_full_init (int owidth, int oheight, int iwidth, int iheight, int topBand, int bottomBand, int leftBand, int rightBand, int padtop, int padbottom, int padleft, int padright)
+{
+    phpImgReSampleContext * s = (phpImgReSampleContext *)av_malloc(sizeof(phpImgReSampleContext));
+    if (s == NULL) {
+        return NULL;
+    }
+    int srcSurface = (iwidth - rightBand - leftBand)* (iheight - topBand - bottomBand);
+    // We use bilinear when the source surface is big, and bicubic when the number of pixels to handle is less than 1 MPixels
+    s->context = sws_getContext(iwidth - rightBand - leftBand, 
+            iheight - topBand - bottomBand, PIX_FMT_YUV420P, owidth, oheight, 
+            PIX_FMT_YUV420P, srcSurface > 1024000 ? SWS_FAST_BILINEAR : SWS_BICUBIC, 
+            NULL, NULL, NULL);
+    if (s->context == NULL) {
+        av_free(s);
+        return NULL; }
+        s->bandLeft = leftBand;
+        s->bandRight = rightBand;
+        s->bandTop = topBand;
+        s->bandBottom = bottomBand;
+
+        s->padLeft = padleft;
+        s->padRight = padright;
+        s->padTop = padtop;
+        s->padBottom = padbottom;
+
+        s->width = iwidth;
+        s->height = iheight;
+
+        s->outWidth = owidth;
+        s->outHeight = oheight;
+
+        return s;
+}
+
+phpImgReSampleContext * phpimg_resample_init (int owidth, int oheight, int iwidth, int iheight)
+{
+    return phpimg_resample_full_init(owidth, oheight, iwidth, iheight, 0, 0, 0, 0, 0, 0, 0, 0);
+}
+
+void phpimg_resample_close(phpImgReSampleContext * s)
+{
+    if (s == NULL) return;
+    sws_freeContext(s->context);
+
+    av_free(s);   
+}
 
 /*
  * Local variables:
