--- C:/svn/digital_hub_services/trunk/Transcode/Ffmpeg/build/watermark-old.c	Thu May 29 14:40:43 2008
+++ C:/svn/digital_hub_services/trunk/Transcode/Ffmpeg/build/watermark.c	Fri Sep 04 18:14:28 2009
@@ -309,6 +309,140 @@
     av_free(buf);
 }
 
+/****************************************************************************
+ * For mode 2 - alpha blending
+ ****************************************************************************/
+static void AlphaBlendingProcess(void *ctx,
+              AVPicture *picture,
+              enum PixelFormat pix_fmt,
+              int src_width,
+              int src_height,
+              int64_t pts)
+{
+    ContextInfo *ci = (ContextInfo *) ctx;
+    char *buf = 0;
+    AVPicture picture1;
+    AVPicture *pict = picture;
+
+    AVFrame *pFrameRGB;
+    int xm_size;
+    int ym_size;
+
+    int x;
+    int y;
+    int offs, offsm;
+    int mpoffs;
+    uint32_t *p_pixel = 0;
+    uint32_t pixel_meck;
+    uint32_t pixel;
+    uint32_t pixelm;
+    uint32_t tmp;
+    uint32_t alpha, src_colour, dest_colour, inverse_alpha;
+
+    if (pix_fmt != PIX_FMT_RGB32) {
+        int size;
+
+        size = avpicture_get_size(PIX_FMT_RGB32, src_width, src_height);
+        buf = av_malloc(size);
+
+        avpicture_fill(&picture1, buf, PIX_FMT_RGB32, src_width, src_height);
+
+        // if we already got a SWS context, let's realloc if is not re-useable
+        ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
+                                    src_width, src_height, pix_fmt,
+                                    src_width, src_height, PIX_FMT_RGB32,
+                                    sws_flags, NULL, NULL, NULL);
+        if (ci->toRGB_convert_ctx == NULL) {
+            av_log(NULL, AV_LOG_ERROR,
+                   "Cannot initialize the toRGB conversion context\n");
+            return;
+        }
+
+// img_convert parameters are          2 first destination, then 4 source
+// sws_scale   parameters are context, 4 first source,      then 2 destination
+        sws_scale(ci->toRGB_convert_ctx,
+                 picture->data, picture->linesize, 0, src_height,
+                 picture1.data, picture1.linesize);
+
+        pict = &picture1;
+    }
+
+    /* Insert filter code here */ /* ok */
+
+    // Get me next frame
+    if (0 > get_watermark_picture(ci, 0)) {
+        return;
+    }
+    // These are the three original static variables in the ffmpeg hack.
+    pFrameRGB = ci->pFrameRGB;
+    xm_size = ci->x_size;
+    ym_size = ci->y_size;
+
+    // I'll do the *4 => <<2 crap later. Most compilers understand that anyway.
+    // According to avcodec.h PIX_FMT_RGB32 is handled in endian specific manner.
+    for (y=0; y<src_height; y++) {
+        offs = y * (src_width * 4);
+        offsm = (((y * ym_size) / src_height) * 4) * xm_size; // offsm first in maskline. byteoffs!
+        for (x=0; x<src_width; x++) {
+            mpoffs = offsm + (((x * xm_size) / src_width) * 4);
+            p_pixel = (uint32_t *)&((pFrameRGB->data[0])[mpoffs]);
+            pixelm = *p_pixel;
+            p_pixel = (uint32_t *)&((pict->data[0])[offs]);
+            pixel = *p_pixel;
+            pixel_meck = pixel & 0xff000000;
+
+			alpha = (uint32_t)((pixelm >> 24) & 0xff);
+
+			if (alpha) {
+				alpha++;
+				inverse_alpha = 0x100 - alpha;
+
+				// R
+				dest_colour = (int)((pixel >> 16) & 0xff);
+				src_colour = (int)((pixelm >> 16) & 0xff);
+				tmp = ((dest_colour * inverse_alpha) >> 8) + ((src_colour * alpha) >> 8);
+				pixel_meck |= (tmp << 16) & 0xff0000;
+				// G
+				dest_colour = (int)((pixel >> 8) & 0xff);
+				src_colour = (int)((pixelm >> 8) & 0xff);
+				tmp = ((dest_colour * inverse_alpha) >> 8) + ((src_colour * alpha) >> 8);
+				pixel_meck |= (tmp << 8) & 0xff00;
+				// B
+				dest_colour = (int)(pixel & 0xff);
+				src_colour = (int)(pixelm & 0xff);
+				tmp = ((dest_colour * inverse_alpha) >> 8) + ((src_colour * alpha) >> 8);
+				pixel_meck |= (tmp << 0) & 0xff;
+
+				*p_pixel = pixel_meck;
+			}
+
+            offs += 4;
+        } // foreach X
+    } // foreach Y
+
+
+
+
+    if (pix_fmt != PIX_FMT_RGB32) {
+        ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,
+                                      src_width, src_height, PIX_FMT_RGB32,
+                                      src_width, src_height, pix_fmt,
+                                      sws_flags, NULL, NULL, NULL);
+        if (ci->fromRGB_convert_ctx == NULL) {
+            av_log(NULL, AV_LOG_ERROR,
+                   "Cannot initialize the fromRGB conversion context\n");
+            return;
+        }
+// img_convert parameters are          2 first destination, then 4 source
+// sws_scale   parameters are context, 4 first source,      then 2 destination
+        sws_scale(ci->fromRGB_convert_ctx,
+                 picture1.data, picture1.linesize, 0, src_height,
+                 picture->data, picture->linesize);
+    }
+
+    av_free(buf);
+}
+
 
 /****************************************************************************
  * For mode 1 (the original one)
@@ -432,7 +566,9 @@
              int64_t pts)
 {
     ContextInfo *ci = (ContextInfo *) ctx;
-    if (1 == ci->mode) {
+    if (2 == ci->mode) {
+        AlphaBlendingProcess(ctx, picture, pix_fmt, src_width, src_height, pts);
+    } else if (1 == ci->mode) {
         Process1(ctx, picture, pix_fmt, src_width, src_height, pts);
     } else {
         Process0(ctx, picture, pix_fmt, src_width, src_height, pts);
