Initialize Tizen 2.3
[framework/multimedia/gst-plugins-base0.10.git] / wearable / gst-libs / gst / video / video-blend.c
1 /* Gstreamer video blending utility functions
2  *
3  * Copied/pasted from gst/videoconvert/videoconvert.c
4  *    Copyright (C) 2010 David Schleef <ds@schleef.org>
5  *    Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
6  *
7  * Copyright (C) <2011> Intel Corporation
8  * Copyright (C) <2011> Collabora Ltd.
9  * Copyright (C) <2011> Thibault Saunier <thibault.saunier@collabora.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "video-blend.h"
31 #include "videoblendorc.h"
32
33 #include <string.h>
34
35 #ifndef GST_DISABLE_GST_DEBUG
36
37 #define GST_CAT_DEFAULT ensure_debug_category()
38
39 static GstDebugCategory *
40 ensure_debug_category (void)
41 {
42   static gsize cat_gonce = 0;
43
44   if (g_once_init_enter (&cat_gonce)) {
45     gsize cat_done;
46
47     cat_done = (gsize) _gst_debug_category_new ("video-blending", 0,
48         "video blending");
49
50     g_once_init_leave (&cat_gonce, cat_done);
51   }
52
53   return (GstDebugCategory *) cat_gonce;
54 }
55
56 #else
57
58 #define ensure_debug_category() /* NOOP */
59
60 #endif /* GST_DISABLE_GST_DEBUG */
61
62 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
63 # define ARGB_A 3
64 # define ARGB_R 2
65 # define ARGB_G 1
66 # define ARGB_B 0
67 #else
68 # define ARGB_A 0
69 # define ARGB_R 1
70 # define ARGB_G 2
71 # define ARGB_B 3
72 #endif
73
74 /* Copy/pasted from 0.11 video.c */
75 static int
76 fill_planes (GstBlendVideoFormatInfo * info)
77 {
78   gint width, height;
79
80   width = info->width;
81   height = info->height;
82
83   switch (info->fmt) {
84     case GST_VIDEO_FORMAT_YUY2:
85     case GST_VIDEO_FORMAT_YVYU:
86     case GST_VIDEO_FORMAT_UYVY:
87       info->stride[0] = GST_ROUND_UP_4 (width * 2);
88       info->offset[0] = 0;
89       break;
90     case GST_VIDEO_FORMAT_AYUV:
91     case GST_VIDEO_FORMAT_RGBx:
92     case GST_VIDEO_FORMAT_RGBA:
93     case GST_VIDEO_FORMAT_BGRx:
94     case GST_VIDEO_FORMAT_BGRA:
95     case GST_VIDEO_FORMAT_xRGB:
96     case GST_VIDEO_FORMAT_ARGB:
97     case GST_VIDEO_FORMAT_xBGR:
98     case GST_VIDEO_FORMAT_ABGR:
99     case GST_VIDEO_FORMAT_r210:
100       info->stride[0] = width * 4;
101       info->offset[0] = 0;
102       break;
103     case GST_VIDEO_FORMAT_RGB16:
104     case GST_VIDEO_FORMAT_BGR16:
105     case GST_VIDEO_FORMAT_RGB15:
106     case GST_VIDEO_FORMAT_BGR15:
107       info->stride[0] = GST_ROUND_UP_4 (width * 2);
108       info->offset[0] = 0;
109       break;
110     case GST_VIDEO_FORMAT_RGB:
111     case GST_VIDEO_FORMAT_BGR:
112     case GST_VIDEO_FORMAT_v308:
113       info->stride[0] = GST_ROUND_UP_4 (width * 3);
114       info->offset[0] = 0;
115       break;
116     case GST_VIDEO_FORMAT_v210:
117       info->stride[0] = ((width + 47) / 48) * 128;
118       info->offset[0] = 0;
119       break;
120     case GST_VIDEO_FORMAT_v216:
121       info->stride[0] = GST_ROUND_UP_8 (width * 4);
122       info->offset[0] = 0;
123       break;
124     case GST_VIDEO_FORMAT_GRAY8:
125     case GST_VIDEO_FORMAT_Y800:
126       info->stride[0] = GST_ROUND_UP_4 (width);
127       info->offset[0] = 0;
128       break;
129     case GST_VIDEO_FORMAT_GRAY16_BE:
130     case GST_VIDEO_FORMAT_GRAY16_LE:
131     case GST_VIDEO_FORMAT_Y16:
132       info->stride[0] = GST_ROUND_UP_4 (width * 2);
133       info->offset[0] = 0;
134       break;
135     case GST_VIDEO_FORMAT_UYVP:
136       info->stride[0] = GST_ROUND_UP_4 ((width * 2 * 5 + 3) / 4);
137       info->offset[0] = 0;
138       break;
139     case GST_VIDEO_FORMAT_RGB8_PALETTED:
140       info->stride[0] = GST_ROUND_UP_4 (width);
141       info->offset[0] = 0;
142       break;
143     case GST_VIDEO_FORMAT_IYU1:
144       info->stride[0] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) +
145           GST_ROUND_UP_4 (width) / 2);
146       info->offset[0] = 0;
147       break;
148     case GST_VIDEO_FORMAT_ARGB64:
149     case GST_VIDEO_FORMAT_AYUV64:
150       info->stride[0] = width * 8;
151       info->offset[0] = 0;
152       break;
153     case GST_VIDEO_FORMAT_I420:
154       info->stride[0] = GST_ROUND_UP_4 (width);
155       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
156       info->stride[2] = info->stride[1];
157       info->offset[0] = 0;
158       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
159       info->offset[2] = info->offset[1] +
160           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
161       break;
162     case GST_VIDEO_FORMAT_YV12:        /* same as I420, but plane 1+2 swapped */
163       info->stride[0] = GST_ROUND_UP_4 (width);
164       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
165       info->stride[2] = info->stride[1];
166       info->offset[0] = 0;
167       info->offset[2] = info->stride[0] * GST_ROUND_UP_2 (height);
168       info->offset[1] = info->offset[2] +
169           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
170       break;
171     case GST_VIDEO_FORMAT_Y41B:
172       info->stride[0] = GST_ROUND_UP_4 (width);
173       info->stride[1] = GST_ROUND_UP_16 (width) / 4;
174       info->stride[2] = info->stride[1];
175       info->offset[0] = 0;
176       info->offset[1] = info->stride[0] * height;
177       info->offset[2] = info->offset[1] + info->stride[1] * height;
178       /* simplification of ROUNDUP4(w)*h + 2*((ROUNDUP16(w)/4)*h */
179       break;
180     case GST_VIDEO_FORMAT_Y42B:
181       info->stride[0] = GST_ROUND_UP_4 (width);
182       info->stride[1] = GST_ROUND_UP_8 (width) / 2;
183       info->stride[2] = info->stride[1];
184       info->offset[0] = 0;
185       info->offset[1] = info->stride[0] * height;
186       info->offset[2] = info->offset[1] + info->stride[1] * height;
187       /* simplification of ROUNDUP4(w)*h + 2*(ROUNDUP8(w)/2)*h */
188       break;
189     case GST_VIDEO_FORMAT_Y444:
190       info->stride[0] = GST_ROUND_UP_4 (width);
191       info->stride[1] = info->stride[0];
192       info->stride[2] = info->stride[0];
193       info->offset[0] = 0;
194       info->offset[1] = info->stride[0] * height;
195       info->offset[2] = info->offset[1] * 2;
196       break;
197     case GST_VIDEO_FORMAT_NV12:
198     case GST_VIDEO_FORMAT_NV21:
199       info->stride[0] = GST_ROUND_UP_4 (width);
200       info->stride[1] = info->stride[0];
201       info->offset[0] = 0;
202       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
203       break;
204     case GST_VIDEO_FORMAT_A420:
205       info->stride[0] = GST_ROUND_UP_4 (width);
206       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_2 (width) / 2);
207       info->stride[2] = info->stride[1];
208       info->stride[3] = info->stride[0];
209       info->offset[0] = 0;
210       info->offset[1] = info->stride[0] * GST_ROUND_UP_2 (height);
211       info->offset[2] = info->offset[1] +
212           info->stride[1] * (GST_ROUND_UP_2 (height) / 2);
213       info->offset[3] = info->offset[2] +
214           info->stride[2] * (GST_ROUND_UP_2 (height) / 2);
215       break;
216     case GST_VIDEO_FORMAT_YUV9:
217       info->stride[0] = GST_ROUND_UP_4 (width);
218       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
219       info->stride[2] = info->stride[1];
220       info->offset[0] = 0;
221       info->offset[1] = info->stride[0] * height;
222       info->offset[2] = info->offset[1] +
223           info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
224       break;
225     case GST_VIDEO_FORMAT_YVU9:
226       info->stride[0] = GST_ROUND_UP_4 (width);
227       info->stride[1] = GST_ROUND_UP_4 (GST_ROUND_UP_4 (width) / 4);
228       info->stride[2] = info->stride[1];
229       info->offset[0] = 0;
230       info->offset[2] = info->stride[0] * height;
231       info->offset[1] = info->offset[2] +
232           info->stride[1] * (GST_ROUND_UP_4 (height) / 4);
233       break;
234     case GST_VIDEO_FORMAT_UNKNOWN:
235       GST_ERROR ("invalid format");
236       g_warning ("invalid format");
237       break;
238   }
239   return 0;
240 }
241
242 typedef struct
243 {
244   GstVideoFormat format;
245   void (*getline) (guint8 * dest, const GstBlendVideoFormatInfo * src,
246       guint xoff, int j);
247   void (*putline) (GstBlendVideoFormatInfo * dest,
248       GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff,
249       int j);
250   void (*matrix) (guint8 * tmpline, guint width);
251 } GetPutLine;
252
253
254 #define GET_LINE(info, comp, line) \
255   (info)->pixels + info->offset[(comp)] + ((info)->stride[(comp)] * (line))
256
257 /* Line conversion to AYUV */
258
259 /* Supports YV12 as well */
260 static void
261 getline_I420 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
262     int j)
263 {
264   cogorc_getline_I420 (dest,
265       GET_LINE (src, 0, j) + xoff,
266       GET_LINE (src, 1, j >> 1) + GST_ROUND_UP_2 (xoff / 2),
267       GET_LINE (src, 2, j >> 1) + GST_ROUND_UP_2 (xoff / 2), src->width);
268 }
269
270 /* Supports YV12 as well */
271 static void
272 putline_I420 (GstBlendVideoFormatInfo * dest,
273     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
274 {
275   cogorc_putline_I420 (GET_LINE (dest, 0, j) + xoff,
276       GET_LINE (dest, 1, j >> 1) + GST_ROUND_UP_2 (xoff / 2),
277       GET_LINE (dest, 2, j >> 1) + GST_ROUND_UP_2 (xoff / 2),
278       line, srcinfo->width / 2);
279 }
280
281 static void
282 getline_YUY2 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
283     int j)
284 {
285   cogorc_getline_YUY2 (dest, GET_LINE (src, 0, j) +
286       (GST_ROUND_UP_2 (xoff * 4) / 2), src->width / 2);
287 }
288
289 static void
290 putline_YUY2 (GstBlendVideoFormatInfo * dest, GstBlendVideoFormatInfo * srcinfo,
291     const guint8 * line, guint xoff, int j)
292 {
293   cogorc_putline_YUY2 (GET_LINE (dest, 0,
294           j) + (GST_ROUND_UP_2 (xoff * 4) / 2), line, srcinfo->width / 2);
295 }
296
297
298 static void
299 getline_AYUV (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
300     int j)
301 {
302   memcpy (dest, GET_LINE (src, 0, j) + (xoff * 4), (src->width - xoff) * 4);
303 }
304
305 static void
306 putline_AYUV (GstBlendVideoFormatInfo * dest, GstBlendVideoFormatInfo * srcinfo,
307     const guint8 * line, guint xoff, int j)
308 {
309   memcpy (GET_LINE (dest, 0, j) + (xoff * 4), line, srcinfo->width * 4);
310 }
311
312 static void
313 getline_UYVY (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
314     int j)
315 {
316   cogorc_getline_UYVY (dest, GET_LINE (src, 0, j) + xoff * 2, src->width / 2);
317 }
318
319 static void
320 putline_UYVY (GstBlendVideoFormatInfo * dest, GstBlendVideoFormatInfo * srcinfo,
321     const guint8 * line, guint xoff, int j)
322 {
323   cogorc_putline_UYVY (GET_LINE (dest, 0, j) +
324       (GST_ROUND_UP_2 (xoff * 4) / 2), line, srcinfo->width / 2);
325 }
326
327 static void
328 getline_v308 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
329     int j)
330 {
331   int i;
332   const guint8 *srcline = GET_LINE (src, 0, j) + GST_ROUND_UP_2 (xoff * 3);
333
334   for (i = 0; i < src->width; i++) {
335     dest[i * 4 + 0] = 0xff;
336     dest[i * 4 + 1] = srcline[i * 3 + 0];
337     dest[i * 4 + 2] = srcline[i * 3 + 1];
338     dest[i * 4 + 3] = srcline[i * 3 + 2];
339   }
340 }
341
342 static void
343 putline_v308 (GstBlendVideoFormatInfo * dest, GstBlendVideoFormatInfo * srcinfo,
344     const guint8 * line, guint xoff, int j)
345 {
346   int i;
347   guint8 *destline = GET_LINE (dest, 0, j) + GST_ROUND_UP_2 (xoff * 3);
348
349   for (i = 0; i < srcinfo->width; i++) {
350     destline[i * 3 + 0] = line[i * 4 + 1];
351     destline[i * 3 + 1] = line[i * 4 + 2];
352     destline[i * 3 + 2] = line[i * 4 + 3];
353   }
354 }
355
356 static void
357 getline_v210 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
358     int j)
359 {
360   int i;
361   const guint8 *srcline = GET_LINE (src, 0, j) + GST_ROUND_UP_2 (xoff * 4) / 5;
362
363   for (i = 0; i < src->width; i += 6) {
364     guint32 a0, a1, a2, a3;
365     guint16 y0, y1, y2, y3, y4, y5;
366     guint16 u0, u2, u4;
367     guint16 v0, v2, v4;
368
369     a0 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 0);
370     a1 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 4);
371     a2 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 8);
372     a3 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 12);
373
374     u0 = ((a0 >> 0) & 0x3ff) >> 2;
375     y0 = ((a0 >> 10) & 0x3ff) >> 2;
376     v0 = ((a0 >> 20) & 0x3ff) >> 2;
377     y1 = ((a1 >> 0) & 0x3ff) >> 2;
378
379     u2 = ((a1 >> 10) & 0x3ff) >> 2;
380     y2 = ((a1 >> 20) & 0x3ff) >> 2;
381     v2 = ((a2 >> 0) & 0x3ff) >> 2;
382     y3 = ((a2 >> 10) & 0x3ff) >> 2;
383
384     u4 = ((a2 >> 20) & 0x3ff) >> 2;
385     y4 = ((a3 >> 0) & 0x3ff) >> 2;
386     v4 = ((a3 >> 10) & 0x3ff) >> 2;
387     y5 = ((a3 >> 20) & 0x3ff) >> 2;
388
389     dest[4 * (i + 0) + 0] = 0xff;
390     dest[4 * (i + 0) + 1] = y0;
391     dest[4 * (i + 0) + 2] = u0;
392     dest[4 * (i + 0) + 3] = v0;
393
394     dest[4 * (i + 1) + 0] = 0xff;
395     dest[4 * (i + 1) + 1] = y1;
396     dest[4 * (i + 1) + 2] = u0;
397     dest[4 * (i + 1) + 3] = v0;
398
399     dest[4 * (i + 2) + 0] = 0xff;
400     dest[4 * (i + 2) + 1] = y2;
401     dest[4 * (i + 2) + 2] = u2;
402     dest[4 * (i + 2) + 3] = v2;
403
404     dest[4 * (i + 3) + 0] = 0xff;
405     dest[4 * (i + 3) + 1] = y3;
406     dest[4 * (i + 3) + 2] = u2;
407     dest[4 * (i + 3) + 3] = v2;
408
409     dest[4 * (i + 4) + 0] = 0xff;
410     dest[4 * (i + 4) + 1] = y4;
411     dest[4 * (i + 4) + 2] = u4;
412     dest[4 * (i + 4) + 3] = v4;
413
414     dest[4 * (i + 5) + 0] = 0xff;
415     dest[4 * (i + 5) + 1] = y5;
416     dest[4 * (i + 5) + 2] = u4;
417     dest[4 * (i + 5) + 3] = v4;
418
419   }
420
421 }
422
423 static void
424 putline_v210 (GstBlendVideoFormatInfo * dest, GstBlendVideoFormatInfo * srcinfo,
425     const guint8 * line, guint xoff, int j)
426 {
427   int i;
428   guint8 *destline = GET_LINE (dest, 0, j) + GST_ROUND_UP_2 (xoff * 4) / 5;
429
430
431   for (i = 0; i < srcinfo->width + 5; i += 6) {
432     guint32 a0, a1, a2, a3;
433     guint16 y0, y1, y2, y3, y4, y5;
434     guint16 u0, u1, u2;
435     guint16 v0, v1, v2;
436
437     y0 = line[4 * (i + 0) + 1] << 2;
438     y1 = line[4 * (i + 1) + 1] << 2;
439     y2 = line[4 * (i + 2) + 1] << 2;
440     y3 = line[4 * (i + 3) + 1] << 2;
441     y4 = line[4 * (i + 4) + 1] << 2;
442     y5 = line[4 * (i + 5) + 1] << 2;
443
444     u0 = (line[4 * (i + 0) + 2] + line[4 * (i + 1) + 2]) << 1;
445     u1 = (line[4 * (i + 2) + 2] + line[4 * (i + 3) + 2]) << 1;
446     u2 = (line[4 * (i + 4) + 2] + line[4 * (i + 5) + 2]) << 1;
447
448     v0 = (line[4 * (i + 0) + 3] + line[4 * (i + 1) + 3]) << 1;
449     v1 = (line[4 * (i + 2) + 3] + line[4 * (i + 3) + 3]) << 1;
450     v2 = (line[4 * (i + 4) + 3] + line[4 * (i + 5) + 3]) << 1;
451
452     a0 = u0 | (y0 << 10) | (v0 << 20);
453     a1 = y1 | (u1 << 10) | (y2 << 20);
454     a2 = v1 | (y3 << 10) | (u2 << 20);
455     a3 = y4 | (v2 << 10) | (y5 << 20);
456
457     GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 0, a0);
458     GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 4, a1);
459     GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 8, a2);
460     GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 12, a3);
461   }
462 }
463
464 static void
465 getline_v216 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
466     int j)
467 {
468   int i;
469   const guint8 *srcline = GET_LINE (src, 0, j) + GST_ROUND_UP_2 (xoff + 3);
470
471   for (i = 0; i < src->width; i++) {
472     dest[i * 4 + 0] = 0xff;
473     dest[i * 4 + 1] = GST_READ_UINT16_LE (srcline + i * 4 + 2) >> 8;
474     dest[i * 4 + 2] = GST_READ_UINT16_LE (srcline + (i >> 1) * 8 + 0) >> 8;
475     dest[i * 4 + 3] = GST_READ_UINT16_LE (srcline + (i >> 1) * 8 + 4) >> 8;
476   }
477 }
478
479 static void
480 putline_v216 (GstBlendVideoFormatInfo * dest, GstBlendVideoFormatInfo * srcinfo,
481     const guint8 * line, guint xoff, int j)
482 {
483   int i;
484   guint8 *destline = GET_LINE (dest, 0, j) + GST_ROUND_UP_2 (xoff + 3);
485
486   for (i = 0; i < srcinfo->width / 2; i++) {
487     GST_WRITE_UINT16_LE (destline + i * 8 + 0, line[(i * 2 + 0) * 4 + 2] << 8);
488     GST_WRITE_UINT16_LE (destline + i * 8 + 2, line[(i * 2 + 0) * 4 + 1] << 8);
489     GST_WRITE_UINT16_LE (destline + i * 8 + 4, line[(i * 2 + 1) * 4 + 3] << 8);
490     GST_WRITE_UINT16_LE (destline + i * 8 + 8, line[(i * 2 + 0) * 4 + 1] << 8);
491   }
492 }
493
494 static void
495 getline_Y41B (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
496     int j)
497 {
498   cogorc_getline_YUV9 (dest,
499       GET_LINE (src, 0, j) + xoff,
500       GET_LINE (src, 1, j) + (xoff / 4), GET_LINE (src, 2, j) + (xoff / 4),
501       src->width / 2);
502 }
503
504 static void
505 putline_Y41B (GstBlendVideoFormatInfo * dest,
506     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
507 {
508   int i;
509   guint8 *destlineY = GET_LINE (dest, 0, j) + xoff;
510   guint8 *destlineU = GET_LINE (dest, 1, j) + (xoff / 4);
511   guint8 *destlineV = GET_LINE (dest, 2, j) + (xoff / 4);
512
513   for (i = 0; i < srcinfo->width - 3; i += 4) {
514     destlineY[i] = line[i * 4 + 1];
515     destlineY[i + 1] = line[i * 4 + 5];
516     destlineY[i + 2] = line[i * 4 + 9];
517     destlineY[i + 3] = line[i * 4 + 13];
518
519     destlineU[i >> 2] =
520         (line[i * 4 + 2] + line[i * 4 + 6] + line[i * 4 + 10] + line[i * 4 +
521             14] + 2) >> 2;
522     destlineV[i >> 2] =
523         (line[i * 4 + 3] + line[i * 4 + 7] + line[i * 4 + 11] + line[i * 4 +
524             15] + 2) >> 2;
525   }
526
527   if (i == srcinfo->width - 3) {
528     destlineY[i] = line[i * 4 + 1];
529     destlineY[i + 1] = line[i * 4 + 5];
530     destlineY[i + 2] = line[i * 4 + 9];
531
532     destlineU[i >> 2] =
533         (line[i * 4 + 2] + line[i * 4 + 6] + line[i * 4 + 10] + 1) / 3;
534     destlineV[i >> 2] =
535         (line[i * 4 + 3] + line[i * 4 + 7] + line[i * 4 + 11] + 1) / 3;
536   } else if (i == srcinfo->width - 2) {
537     destlineY[i] = line[i * 4 + 1];
538     destlineY[i + 1] = line[i * 4 + 5];
539
540     destlineU[i >> 2] = (line[i * 4 + 2] + line[i * 4 + 6] + 1) >> 1;
541     destlineV[i >> 2] = (line[i * 4 + 3] + line[i * 4 + 7] + 1) >> 1;
542   } else if (i == srcinfo->width - 1) {
543     destlineY[i + 1] = line[i * 4 + 5];
544
545     destlineU[i >> 2] = line[i * 4 + 2];
546     destlineV[i >> 2] = line[i * 4 + 3];
547   }
548 }
549
550 static void
551 getline_Y42B (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
552     int j)
553 {
554   cogorc_getline_Y42B (dest,
555       GET_LINE (src, 0, j) + xoff,
556       GET_LINE (src, 1, j) + GST_ROUND_UP_2 (xoff / 2),
557       GET_LINE (src, 2, j) + GST_ROUND_UP_2 (xoff / 2), src->width / 2);
558 }
559
560 static void
561 putline_Y42B (GstBlendVideoFormatInfo * dest,
562     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
563 {
564   cogorc_putline_Y42B (GET_LINE (dest, 0, j) + xoff,
565       GET_LINE (dest, 1, j) + GST_ROUND_UP_2 (xoff / 2),
566       GET_LINE (dest, 2, j) + GST_ROUND_UP_2 (xoff / 2), line,
567       srcinfo->width / 2);
568 }
569
570 static void
571 getline_Y444 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
572     int j)
573 {
574   cogorc_getline_Y444 (dest,
575       GET_LINE (src, 0, j) + xoff,
576       GET_LINE (src, 1, j) + xoff, GET_LINE (src, 2, j) + xoff, src->width);
577 }
578
579 static void
580 putline_Y444 (GstBlendVideoFormatInfo * dest,
581     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
582 {
583   cogorc_putline_Y444 (GET_LINE (dest, 0, j) + xoff,
584       GET_LINE (dest, 1, j) + xoff,
585       GET_LINE (dest, 2, j) + xoff, line, srcinfo->width);
586 }
587
588 static void
589 getline_Y800 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
590     int j)
591 {
592   cogorc_getline_Y800 (dest, GET_LINE (src, 0, j) + xoff, src->width);
593 }
594
595 static void
596 putline_Y800 (GstBlendVideoFormatInfo * dest,
597     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
598 {
599   cogorc_putline_Y800 (GET_LINE (dest, 0, j) + xoff, line, srcinfo->width);
600 }
601
602 static void
603 getline_Y16 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
604     int j)
605 {
606   cogorc_getline_Y16 (dest, GET_LINE (src, 0, j) + xoff * 2, src->width);
607 }
608
609 static void
610 putline_Y16 (GstBlendVideoFormatInfo * dest,
611     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
612 {
613   cogorc_putline_Y16 (GET_LINE (dest, 0, j) + xoff * 2, line, srcinfo->width);
614 }
615
616 static void
617 getline_NV12 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
618     int j)
619 {
620   cogorc_getline_NV12 (dest,
621       GET_LINE (src, 0, j) + xoff,
622       GET_LINE (src, 1, j >> 1) + xoff, src->width / 2);
623 }
624
625 static void
626 putline_NV12 (GstBlendVideoFormatInfo * dest,
627     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
628 {
629   cogorc_putline_NV12 (GET_LINE (dest, 0, j) + xoff,
630       GET_LINE (dest, 1, j >> 1) + xoff, line, srcinfo->width / 2);
631 }
632
633 static void
634 getline_NV21 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
635     int j)
636 {
637   cogorc_getline_NV21 (dest,
638       GET_LINE (src, 0, j) + xoff,
639       GET_LINE (src, 1, j >> 1) + xoff, src->width / 2);
640 }
641
642 static void
643 putline_NV21 (GstBlendVideoFormatInfo * dest,
644     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
645 {
646   cogorc_putline_NV21 (GET_LINE (dest, 0, j) + xoff,
647       GET_LINE (dest, 1, j >> 1) + xoff, line, srcinfo->width / 2);
648 }
649
650 static void
651 getline_UYVP (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
652     int j)
653 {
654   int i;
655   const guint8 *srcline = GET_LINE (src, 0, j)
656       + xoff * 3;
657
658   for (i = 0; i < src->width; i += 2) {
659     guint16 y0, y1;
660     guint16 u0;
661     guint16 v0;
662
663     u0 = (srcline[(i / 2) * 5 + 0] << 2) | (srcline[(i / 2) * 5 + 1] >> 6);
664
665     y0 = ((srcline[(i / 2) * 5 + 1] & 0x3f) << 4) |
666         (srcline[(i / 2) * 5 + 2] >> 4);
667
668     v0 = ((srcline[(i / 2) * 5 + 2] & 0x0f) << 6) |
669         (srcline[(i / 2) * 5 + 3] >> 2);
670
671     y1 = ((srcline[(i / 2) * 5 + 3] & 0x03) << 8) | srcline[(i / 2) * 5 + 4];
672
673     dest[i * 4 + 0] = 0xff;
674     dest[i * 4 + 1] = y0 >> 2;
675     dest[i * 4 + 2] = u0 >> 2;
676     dest[i * 4 + 3] = v0 >> 2;
677     dest[i * 4 + 4] = 0xff;
678     dest[i * 4 + 5] = y1 >> 2;
679     dest[i * 4 + 6] = u0 >> 2;
680     dest[i * 4 + 7] = v0 >> 2;
681   }
682 }
683
684 static void
685 putline_UYVP (GstBlendVideoFormatInfo * dest,
686     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
687 {
688   int i;
689   guint8 *destline = GET_LINE (dest, 0, j) + xoff * 3;
690
691   for (i = 0; i < srcinfo->width; i += 2) {
692     guint16 y0, y1;
693     guint16 u0;
694     guint16 v0;
695
696     y0 = line[4 * (i + 0) + 1];
697     y1 = line[4 * (i + 1) + 1];
698     u0 = (line[4 * (i + 0) + 2] + line[4 * (i + 1) + 2] + 1) >> 1;
699     v0 = (line[4 * (i + 0) + 3] + line[4 * (i + 1) + 3] + 1) >> 1;
700
701     destline[(i / 2) * 5 + 0] = u0;
702     destline[(i / 2) * 5 + 1] = y0 >> 2;
703     destline[(i / 2) * 5 + 2] = (y0 << 6) | (v0 >> 4);
704     destline[(i / 2) * 5 + 3] = (v0 << 4) | (y1 >> 2);
705     destline[(i / 2) * 5 + 4] = (y1 << 2);
706   }
707 }
708
709 static void
710 getline_A420 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
711     int j)
712 {
713   cogorc_getline_A420 (dest,
714       GET_LINE (src, 0, j) + xoff,
715       GET_LINE (src, 1, j >> 1) + GST_ROUND_UP_2 (xoff / 2),
716       GET_LINE (src, 2, j >> 1) + GST_ROUND_UP_2 (xoff / 2),
717       GET_LINE (src, 3, j) + GST_ROUND_UP_2 (xoff / 2), src->width);
718 }
719
720 static void
721 putline_A420 (GstBlendVideoFormatInfo * dest,
722     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
723 {
724   cogorc_putline_A420 (GET_LINE (dest, 0, j) + xoff,
725       GET_LINE (dest, 1, j >> 1) + GST_ROUND_UP_2 (xoff / 2),
726       GET_LINE (dest, 2, j >> 1) + GST_ROUND_UP_2 (xoff / 2),
727       GET_LINE (dest, 3, j) + GST_ROUND_UP_2 (xoff / 2), line,
728       srcinfo->width / 2);
729 }
730
731 static void
732 getline_YUV9 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
733     int j)
734 {
735   cogorc_getline_YUV9 (dest,
736       GET_LINE (src, 0, j) + xoff,
737       GET_LINE (src, 1, j >> 2) + GST_ROUND_UP_4 (xoff / 4),
738       GET_LINE (src, 2, j >> 2) + GST_ROUND_UP_4 (xoff / 4), src->width / 2);
739 }
740
741 static void
742 putline_YUV9 (GstBlendVideoFormatInfo * dest,
743     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
744 {
745   int i;
746   guint8 *destY = GET_LINE (dest, 0, j) + xoff;
747   guint8 *destU = GET_LINE (dest, 1, j >> 2) + GST_ROUND_UP_4 (xoff / 4);
748   guint8 *destV = GET_LINE (dest, 2, j >> 2) + GST_ROUND_UP_4 (xoff / 4);
749   guint width = srcinfo->width;
750
751   for (i = 0; i < width - 3; i += 4) {
752     destY[i] = line[i * 4 + 1];
753     destY[i + 1] = line[i * 4 + 5];
754     destY[i + 2] = line[i * 4 + 9];
755     destY[i + 3] = line[i * 4 + 13];
756     if (j % 4 == 0) {
757       destU[i >> 2] =
758           (line[i * 4 + 2] + line[i * 4 + 6] + line[i * 4 + 10] + line[i * 4 +
759               14]) >> 2;
760       destV[i >> 2] =
761           (line[i * 4 + 3] + line[i * 4 + 7] + line[i * 4 + 11] + line[i * 4 +
762               15]) >> 2;
763     }
764   }
765
766   if (i == width - 3) {
767     destY[i] = line[i * 4 + 1];
768     destY[i + 1] = line[i * 4 + 5];
769     destY[i + 2] = line[i * 4 + 9];
770     if (j % 4 == 0) {
771       destU[i >> 2] =
772           (line[i * 4 + 2] + line[i * 4 + 6] + line[i * 4 + 10]) / 3;
773       destV[i >> 2] =
774           (line[i * 4 + 3] + line[i * 4 + 7] + line[i * 4 + 11]) / 3;
775     }
776   } else if (i == width - 2) {
777     destY[i] = line[i * 4 + 1];
778     destY[i + 1] = line[i * 4 + 5];
779     if (j % 4 == 0) {
780       destU[i >> 2] = (line[i * 4 + 2] + line[i * 4 + 6]) >> 1;
781       destV[i >> 2] = (line[i * 4 + 3] + line[i * 4 + 7]) >> 1;
782     }
783   } else if (i == width - 1) {
784     destY[i] = line[i * 4 + 1];
785     destU[i >> 2] = line[i * 4 + 2];
786     destV[i >> 2] = line[i * 4 + 3];
787   }
788 }
789
790 static void
791 getline_IYU1 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
792     int j)
793 {
794   int i;
795   const guint8 *srcline =
796       GET_LINE (src, 0, j) + GST_ROUND_UP_2 ((xoff * 3) / 2);
797   guint width = src->width;
798
799   for (i = 0; i < width - 3; i += 4) {
800     dest[i * 4 + 0] = 0xff;
801     dest[i * 4 + 4] = 0xff;
802     dest[i * 4 + 8] = 0xff;
803     dest[i * 4 + 12] = 0xff;
804     dest[i * 4 + 1] = srcline[(i >> 2) * 6 + 1];
805     dest[i * 4 + 5] = srcline[(i >> 2) * 6 + 2];
806     dest[i * 4 + 9] = srcline[(i >> 2) * 6 + 4];
807     dest[i * 4 + 13] = srcline[(i >> 2) * 6 + 5];
808     dest[i * 4 + 2] = dest[i * 4 + 6] = dest[i * 4 + 10] = dest[i * 4 + 14] =
809         srcline[(i >> 2) * 6 + 0];
810     dest[i * 4 + 3] = dest[i * 4 + 7] = dest[i * 4 + 11] = dest[i * 4 + 15] =
811         srcline[(i >> 2) * 6 + 3];
812   }
813
814   if (i == width - 3) {
815     dest[i * 4 + 0] = 0xff;
816     dest[i * 4 + 4] = 0xff;
817     dest[i * 4 + 8] = 0xff;
818     dest[i * 4 + 1] = srcline[(i >> 2) * 6 + 1];
819     dest[i * 4 + 5] = srcline[(i >> 2) * 6 + 2];
820     dest[i * 4 + 9] = srcline[(i >> 2) * 6 + 4];
821     dest[i * 4 + 2] = dest[i * 4 + 6] = dest[i * 4 + 10] =
822         srcline[(i >> 2) * 6 + 0];
823     dest[i * 4 + 3] = dest[i * 4 + 7] = dest[i * 4 + 11] =
824         srcline[(i >> 2) * 6 + 3];
825   } else if (i == width - 2) {
826     dest[i * 4 + 0] = 0xff;
827     dest[i * 4 + 4] = 0xff;
828     dest[i * 4 + 1] = srcline[(i >> 2) * 6 + 1];
829     dest[i * 4 + 5] = srcline[(i >> 2) * 6 + 2];
830     dest[i * 4 + 2] = dest[i * 4 + 6] = srcline[(i >> 2) * 6 + 0];
831     dest[i * 4 + 3] = dest[i * 4 + 7] = srcline[(i >> 2) * 6 + 3];
832   } else if (i == width - 1) {
833     dest[i * 4 + 0] = 0xff;
834     dest[i * 4 + 1] = srcline[(i >> 2) * 6 + 1];
835     dest[i * 4 + 2] = srcline[(i >> 2) * 6 + 0];
836     dest[i * 4 + 3] = srcline[(i >> 2) * 6 + 3];
837   }
838 }
839
840 static void
841 putline_IYU1 (GstBlendVideoFormatInfo * dest,
842     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
843 {
844   int i;
845   guint8 *destline = GET_LINE (dest, 0, j) + GST_ROUND_UP_2 ((xoff * 3) / 2);
846   guint width = srcinfo->width;
847
848   for (i = 0; i < width - 3; i += 4) {
849     destline[(i >> 2) * 6 + 1] = line[i * 4 + 1];
850     destline[(i >> 2) * 6 + 2] = line[i * 4 + 5];
851     destline[(i >> 2) * 6 + 4] = line[i * 4 + 9];
852     destline[(i >> 2) * 6 + 5] = line[i * 4 + 13];
853     destline[(i >> 2) * 6 + 0] =
854         (line[i * 4 + 2] + line[i * 4 + 6] + line[i * 4 + 10] + line[i * 4 +
855             14]) >> 2;
856     destline[(i >> 2) * 6 + 3] =
857         (line[i * 4 + 3] + line[i * 4 + 7] + line[i * 4 + 11] + line[i * 4 +
858             15]) >> 2;
859   }
860
861   if (i == width - 3) {
862     destline[(i >> 2) * 6 + 1] = line[i * 4 + 1];
863     destline[(i >> 2) * 6 + 2] = line[i * 4 + 5];
864     destline[(i >> 2) * 6 + 4] = line[i * 4 + 9];
865     destline[(i >> 2) * 6 + 0] =
866         (line[i * 4 + 2] + line[i * 4 + 6] + line[i * 4 + 10]) / 3;
867     destline[(i >> 2) * 6 + 3] =
868         (line[i * 4 + 3] + line[i * 4 + 7] + line[i * 4 + 11]) / 3;
869   } else if (i == width - 2) {
870     destline[(i >> 2) * 6 + 1] = line[i * 4 + 1];
871     destline[(i >> 2) * 6 + 2] = line[i * 4 + 5];
872     destline[(i >> 2) * 6 + 0] = (line[i * 4 + 2] + line[i * 4 + 6]) >> 1;
873     destline[(i >> 2) * 6 + 3] = (line[i * 4 + 3] + line[i * 4 + 7]) >> 1;
874   } else if (i == width - 1) {
875     destline[(i >> 2) * 6 + 1] = line[i * 4 + 1];
876     destline[(i >> 2) * 6 + 0] = line[i * 4 + 2];
877     destline[(i >> 2) * 6 + 3] = line[i * 4 + 3];
878   }
879 }
880
881
882 /* Line conversion to ARGB */
883 static void
884 getline_RGB (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
885     int j)
886 {
887   int i;
888   const guint8 *srcline = GET_LINE (src, 0, j) + xoff * 3;
889
890   for (i = 0; i < src->width; i++) {
891     dest[i * 4 + 0] = 0xff;
892     dest[i * 4 + 1] = srcline[i * 3 + 0];
893     dest[i * 4 + 2] = srcline[i * 3 + 1];
894     dest[i * 4 + 3] = srcline[i * 3 + 2];
895   }
896 }
897
898 static void
899 putline_RGB (GstBlendVideoFormatInfo * dest, GstBlendVideoFormatInfo * srcinfo,
900     const guint8 * line, guint xoff, int j)
901 {
902   int i;
903   guint8 *destline = GET_LINE (dest, 0, j) + xoff * 3;
904
905   for (i = 0; i < srcinfo->width; i++) {
906     destline[i * 3 + 0] = line[i * 4 + 1];
907     destline[i * 3 + 1] = line[i * 4 + 2];
908     destline[i * 3 + 2] = line[i * 4 + 3];
909   }
910 }
911
912 static void
913 getline_BGR (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
914     int j)
915 {
916   int i;
917   const guint8 *srcline = GET_LINE (src, 0, j) + xoff * 3;
918
919   for (i = 0; i < src->width; i++) {
920     dest[i * 4 + 0] = 0xff;
921     dest[i * 4 + 1] = srcline[i * 3 + 2];
922     dest[i * 4 + 2] = srcline[i * 3 + 1];
923     dest[i * 4 + 3] = srcline[i * 3 + 0];
924   }
925 }
926
927 static void
928 putline_BGR (GstBlendVideoFormatInfo * dest, GstBlendVideoFormatInfo * srcinfo,
929     const guint8 * line, guint xoff, int j)
930 {
931   int i;
932   guint8 *destline = GET_LINE (dest, 0, j) + xoff * 3;
933
934   for (i = 0; i < srcinfo->width; i++) {
935     destline[i * 3 + 0] = line[i * 4 + 3];
936     destline[i * 3 + 1] = line[i * 4 + 2];
937     destline[i * 3 + 2] = line[i * 4 + 1];
938   }
939 }
940
941 static void
942 getline_RGBA (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
943     int j)
944 {
945   cogorc_getline_RGBA (dest, GET_LINE (src, 0, j) + (4 * xoff), src->width);
946 }
947
948 static void
949 putline_RGBA (GstBlendVideoFormatInfo * dest, GstBlendVideoFormatInfo * srcinfo,
950     const guint8 * line, guint xoff, int j)
951 {
952   cogorc_putline_RGBA (GET_LINE (dest, 0, j) + (4 * xoff),
953       line, srcinfo->width);
954 }
955
956 static void
957 getline_ARGB (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
958     int j)
959 {
960
961   memcpy (dest, GET_LINE (src, 0, j), (src->width - xoff) * 4);
962 }
963
964 static void
965 putline_ARGB (GstBlendVideoFormatInfo * dest, GstBlendVideoFormatInfo * srcinfo,
966     const guint8 * line, guint xoff, int j)
967 {
968   memcpy (GET_LINE (dest, 0, j) + (xoff * 4), line, srcinfo->width * 4);
969 }
970
971 static void
972 getline_RGB16 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
973     int j)
974 {
975   int i;
976   const guint16 *srcline = (const guint16 *) GET_LINE (src, 0, j)
977       + (xoff * 3);
978
979   for (i = 0; i < src->width; i++) {
980     dest[i * 4 + 0] = 0xff;
981     dest[i * 4 + 1] = ((srcline[i] >> 11) & 0x1f) << 3;
982     dest[i * 4 + 2] = ((srcline[i] >> 5) & 0x3f) << 2;
983     dest[i * 4 + 3] = ((srcline[i]) & 0x1f) << 3;
984   }
985 }
986
987 static void
988 putline_RGB16 (GstBlendVideoFormatInfo * dest,
989     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
990 {
991   int i;
992   guint16 *destline = (guint16 *) GET_LINE (dest, 0, j) + (xoff * 3);
993
994   for (i = 0; i < srcinfo->width; i++) {
995     destline[i] = ((line[i * 4 + 1] >> 3) << 11) | ((line[i * 4 +
996                 2] >> 2) << 5) | (line[i * 4 + 3] >> 3);
997   }
998 }
999
1000 static void
1001 getline_RGB15 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
1002     int j)
1003 {
1004   int i;
1005   const guint16 *srcline = (const guint16 *) GET_LINE (src, 0, j)
1006       + (xoff * 3);
1007
1008   for (i = 0; i < src->width; i++) {
1009     dest[i * 4 + 0] = 0xff;
1010     dest[i * 4 + 1] = ((srcline[i] >> 10) & 0x1f) << 3;
1011     dest[i * 4 + 2] = ((srcline[i] >> 5) & 0x1f) << 3;
1012     dest[i * 4 + 3] = ((srcline[i]) & 0x1f) << 3;
1013   }
1014 }
1015
1016 static void
1017 putline_RGB15 (GstBlendVideoFormatInfo * dest,
1018     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
1019 {
1020   int i;
1021   guint16 *destline = (guint16 *) GET_LINE (dest, 0, j) + (xoff * 3);
1022
1023   for (i = 0; i < srcinfo->width; i++) {
1024     destline[i] = ((line[i * 4 + 1] >> 3) << 10) | ((line[i * 4 +
1025                 2] >> 3) << 5) | (line[i * 4 + 3] >> 3);
1026   }
1027 }
1028
1029 static void
1030 getline_BGR15 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
1031     int j)
1032 {
1033   int i;
1034   const guint16 *srcline = (const guint16 *) GET_LINE (src, 0, j)
1035       + (xoff * 3);
1036
1037   for (i = 0; i < src->width; i++) {
1038     dest[i * 4 + 0] = 0xff;
1039     dest[i * 4 + 3] = ((srcline[i] >> 10) & 0x1f) << 3;
1040     dest[i * 4 + 2] = ((srcline[i] >> 5) & 0x1f) << 3;
1041     dest[i * 4 + 1] = ((srcline[i]) & 0x1f) << 3;
1042   }
1043 }
1044
1045 static void
1046 putline_BGR15 (GstBlendVideoFormatInfo * dest,
1047     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
1048 {
1049   int i;
1050   guint16 *destline = (guint16 *) GET_LINE (dest, 0, j) + (xoff * 3);
1051
1052   for (i = 0; i < srcinfo->width; i++) {
1053     destline[i] = ((line[i * 4 + 3] >> 3) << 10) | ((line[i * 4 +
1054                 2] >> 3) << 5) | (line[i * 4 + 1] >> 3);
1055   }
1056 }
1057
1058 static void
1059 getline_BGR16 (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
1060     int j)
1061 {
1062   int i;
1063   const guint16 *srcline = (const guint16 *) GET_LINE (src, 0, j)
1064       + (xoff * 3);
1065
1066   for (i = 0; i < src->width; i++) {
1067     dest[i * 4 + 0] = 0xff;
1068     dest[i * 4 + 3] = ((srcline[i] >> 11) & 0x1f) << 3;
1069     dest[i * 4 + 2] = ((srcline[i] >> 5) & 0x3f) << 2;
1070     dest[i * 4 + 1] = ((srcline[i]) & 0x1f) << 3;
1071   }
1072 }
1073
1074 static void
1075 putline_BGR16 (GstBlendVideoFormatInfo * dest,
1076     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
1077 {
1078   int i;
1079   guint16 *destline = (guint16 *) GET_LINE (dest, 0, j) + (xoff * 3);
1080
1081   for (i = 0; i < srcinfo->width; i++) {
1082     destline[i] = ((line[i * 4 + 3] >> 3) << 11) | ((line[i * 4 +
1083                 2] >> 2) << 5) | (line[i * 4 + 1] >> 3);
1084   }
1085 }
1086
1087 static void
1088 getline_BGRA (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
1089     int j)
1090 {
1091   cogorc_getline_BGRA (dest, GET_LINE (src, 0, j) + xoff * 4, src->width);
1092 }
1093
1094 static void
1095 putline_BGRA (GstBlendVideoFormatInfo * dest,
1096     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
1097 {
1098   cogorc_putline_BGRA (GET_LINE (dest, 0, j) + xoff * 4, line, srcinfo->width);
1099 }
1100
1101 static void
1102 getline_ABGR (guint8 * dest, const GstBlendVideoFormatInfo * src, guint xoff,
1103     int j)
1104 {
1105   cogorc_getline_ABGR (dest, GET_LINE (src, 0, j) + (xoff * 4), src->width);
1106 }
1107
1108 static void
1109 putline_ABGR (GstBlendVideoFormatInfo * dest,
1110     GstBlendVideoFormatInfo * srcinfo, const guint8 * line, guint xoff, int j)
1111 {
1112   cogorc_putline_ABGR (GET_LINE (dest, 0, j) + (xoff * 4),
1113       line, srcinfo->width);
1114 }
1115
1116 static const GetPutLine lines[] = {
1117   /* YUV lines conversion */
1118   {GST_VIDEO_FORMAT_I420, getline_I420, putline_I420},
1119   {GST_VIDEO_FORMAT_YV12, getline_I420, putline_I420},
1120   {GST_VIDEO_FORMAT_AYUV, getline_AYUV, putline_AYUV},
1121   {GST_VIDEO_FORMAT_YUY2, getline_YUY2, putline_YUY2},
1122   {GST_VIDEO_FORMAT_UYVY, getline_UYVY, putline_UYVY},
1123   {GST_VIDEO_FORMAT_v308, getline_v308, putline_v308},
1124   {GST_VIDEO_FORMAT_v210, getline_v210, putline_v210},
1125   {GST_VIDEO_FORMAT_v216, getline_v216, putline_v216},
1126   {GST_VIDEO_FORMAT_Y41B, getline_Y41B, putline_Y41B},
1127   {GST_VIDEO_FORMAT_Y42B, getline_Y42B, putline_Y42B},
1128   {GST_VIDEO_FORMAT_Y444, getline_Y444, putline_Y444},
1129   {GST_VIDEO_FORMAT_Y800, getline_Y800, putline_Y800},
1130   {GST_VIDEO_FORMAT_Y16, getline_Y16, putline_Y16},
1131   {GST_VIDEO_FORMAT_NV12, getline_NV12, putline_NV12},
1132   {GST_VIDEO_FORMAT_NV21, getline_NV21, putline_NV21},
1133   {GST_VIDEO_FORMAT_UYVP, getline_UYVP, putline_UYVP},
1134   {GST_VIDEO_FORMAT_A420, getline_A420, putline_A420},
1135   {GST_VIDEO_FORMAT_YUV9, getline_YUV9, putline_YUV9},
1136   {GST_VIDEO_FORMAT_IYU1, getline_IYU1, putline_IYU1},
1137
1138   /* ARGB lines conversion */
1139   {GST_VIDEO_FORMAT_RGB, getline_RGB, putline_RGB},
1140   {GST_VIDEO_FORMAT_BGR, getline_BGR, putline_BGR},
1141   {GST_VIDEO_FORMAT_RGBx, getline_RGBA, putline_RGBA},
1142   {GST_VIDEO_FORMAT_RGBA, getline_RGBA, putline_RGBA},
1143   {GST_VIDEO_FORMAT_ARGB, getline_ARGB, putline_ARGB},
1144   {GST_VIDEO_FORMAT_RGB16, getline_RGB16, putline_RGB16},
1145   {GST_VIDEO_FORMAT_BGR16, getline_BGR16, putline_BGR16},
1146   {GST_VIDEO_FORMAT_BGR15, getline_BGR15, putline_BGR15},
1147   {GST_VIDEO_FORMAT_RGB15, getline_RGB15, putline_RGB15},
1148   {GST_VIDEO_FORMAT_BGRA, getline_BGRA, putline_BGRA},
1149   {GST_VIDEO_FORMAT_ABGR, getline_ABGR, putline_ABGR},
1150   {GST_VIDEO_FORMAT_BGRx, getline_BGRA, putline_BGRA}
1151 };
1152
1153 static void
1154 matrix_identity (guint8 * tmpline, guint width)
1155 {
1156 }
1157
1158 static void
1159 matrix_rgb_to_yuv (guint8 * tmpline, guint width)
1160 {
1161   int i;
1162   int r, g, b;
1163   int y, u, v;
1164
1165   for (i = 0; i < width; i++) {
1166     r = tmpline[i * 4 + 1];
1167     g = tmpline[i * 4 + 2];
1168     b = tmpline[i * 4 + 3];
1169
1170     y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
1171     u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
1172     v = (112 * r - 102 * g - 10 * b + 32768) >> 8;
1173
1174     tmpline[i * 4 + 1] = CLAMP (y, 0, 255);
1175     tmpline[i * 4 + 2] = CLAMP (u, 0, 255);
1176     tmpline[i * 4 + 3] = CLAMP (v, 0, 255);
1177   }
1178 }
1179
1180 static void
1181 matrix_yuv_to_rgb (guint8 * tmpline, guint width)
1182 {
1183   int i;
1184   int r, g, b;
1185   int y, u, v;
1186
1187   for (i = 0; i < width; i++) {
1188     y = tmpline[i * 4 + 1];
1189     u = tmpline[i * 4 + 2];
1190     v = tmpline[i * 4 + 3];
1191
1192     r = (298 * y + 459 * v - 63514) >> 8;
1193     g = (298 * y - 55 * u - 136 * v + 19681) >> 8;
1194     b = (298 * y + 541 * u - 73988) >> 8;
1195
1196     tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
1197     tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
1198     tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
1199   }
1200 }
1201
1202 static gboolean
1203 lookup_getput (GetPutLine * getput, GstVideoFormat fmt)
1204 {
1205   int i;
1206
1207   getput->getline = NULL;
1208   getput->putline = NULL;
1209   getput->matrix = matrix_identity;
1210
1211   for (i = 0; i < sizeof (lines) / sizeof (lines[0]); i++) {
1212     if (lines[i].format == fmt) {
1213       getput->getline = lines[i].getline;
1214       getput->putline = lines[i].putline;
1215
1216       return TRUE;
1217     }
1218   }
1219   GST_WARNING ("Conversion from %i not supported", fmt);
1220
1221   return FALSE;
1222 }
1223
1224 #define BLEND(ret, alpha, v0, v1) \
1225 { \
1226   ret = (v0 * alpha + v1 * (255 - alpha)) / 255; \
1227 }
1228
1229 void
1230 video_blend_scale_linear_RGBA (GstBlendVideoFormatInfo * src,
1231     gint dest_height, gint dest_width)
1232 {
1233   int acc;
1234   int y_increment;
1235   int x_increment;
1236   int y1;
1237   int i;
1238   int j;
1239   int x;
1240   int dest_size;
1241   guint dest_stride = dest_width * 4;
1242   guint src_stride = src->width * 4;
1243
1244   guint8 *tmpbuf = g_malloc (dest_width * 8 * 4);
1245   guint8 *dest_pixels =
1246       g_malloc (gst_video_format_get_size (src->fmt, dest_height,
1247           dest_width));
1248
1249   if (dest_height == 1)
1250     y_increment = 0;
1251   else
1252     y_increment = ((src->height - 1) << 16) / (dest_height - 1) - 1;
1253
1254   if (dest_width == 1)
1255     x_increment = 0;
1256   else
1257     x_increment = ((src->width - 1) << 16) / (dest_width - 1) - 1;
1258
1259   dest_size = dest_width * 4;
1260
1261 #define LINE(x) ((tmpbuf) + (dest_size)*((x)&1))
1262
1263   acc = 0;
1264   orc_resample_bilinear_u32 (LINE (0), src->pixels, 0, x_increment, dest_width);
1265   y1 = 0;
1266   for (i = 0; i < dest_height; i++) {
1267     j = acc >> 16;
1268     x = acc & 0xffff;
1269
1270     if (x == 0) {
1271       memcpy (dest_pixels + i * dest_stride, LINE (j), dest_size);
1272     } else {
1273       if (j > y1) {
1274         orc_resample_bilinear_u32 (LINE (j),
1275             src->pixels + j * src_stride, 0, x_increment, dest_width);
1276         y1++;
1277       }
1278       if (j >= y1) {
1279         orc_resample_bilinear_u32 (LINE (j + 1),
1280             src->pixels + (j + 1) * src_stride, 0, x_increment, dest_width);
1281         y1++;
1282       }
1283       orc_merge_linear_u8 (dest_pixels + i * dest_stride,
1284           LINE (j), LINE (j + 1), (x >> 8), dest_width * 4);
1285     }
1286
1287     acc += y_increment;
1288   }
1289
1290   /* Update src, our reference to the old src->pixels is lost */
1291   video_blend_format_info_init (src, dest_pixels, dest_height, dest_width,
1292       src->fmt);
1293
1294   g_free (tmpbuf);
1295 }
1296
1297 /* video_blend:
1298  * @dest: The #GstBlendVideoFormatInfo where to blend @src in
1299  * @src: the #GstBlendVideoFormatInfo that we want to blend into
1300  * @dest
1301  * @x: The x offset in pixel where the @src image should be blended
1302  * @y: the y offset in pixel where the @src image should be blended
1303  *
1304  * Lets you blend the @src image into the @dest image
1305  */
1306 gboolean
1307 video_blend (GstBlendVideoFormatInfo * dest,
1308     GstBlendVideoFormatInfo * src, guint x, guint y)
1309 {
1310   guint i, j;
1311   guint8 alpha;
1312   GetPutLine getputdest, getputsrc;
1313
1314   gint src_stride = src->width * 4;
1315   guint8 *tmpdestline = g_malloc (sizeof (guint8) * (dest->width + 8) * 4);
1316   guint8 *tmpsrcline = g_malloc (sizeof (guint8) * (dest->width + 8) * 4);
1317
1318   ensure_debug_category ();
1319
1320
1321   if (!lookup_getput (&getputdest, dest->fmt))
1322     goto failed;
1323
1324   if (!lookup_getput (&getputsrc, src->fmt))
1325     goto failed;
1326
1327   if (gst_video_format_is_rgb (src->fmt) != gst_video_format_is_rgb (dest->fmt))
1328     getputsrc.matrix = gst_video_format_is_rgb (src->fmt) ?
1329         matrix_rgb_to_yuv : matrix_yuv_to_rgb;
1330
1331   /* adjust src pointers for negative sizes */
1332   if (x < 0) {
1333     src += -x * 4;
1334     src->width -= -x;
1335     x = 0;
1336   }
1337
1338   if (y < 0) {
1339     src += -y * src_stride;
1340     src->height -= -y;
1341     y = 0;
1342   }
1343
1344   /* adjust width/height if the src is bigger than dest */
1345   if (x + src->width > dest->width)
1346     src->width = dest->width - x;
1347
1348   if (y + src->height > dest->height)
1349     src->height = dest->height - y;
1350
1351   /* Mainloop doing the needed conversions, and blending */
1352   for (i = y; i < y + src->height; i++) {
1353
1354     getputdest.getline (tmpdestline, dest, x, i);
1355     getputsrc.getline (tmpsrcline, src, 0, (i - y));
1356
1357     getputsrc.matrix (tmpsrcline, src->width);
1358
1359     /* Here dest and src are both either in AYUV or ARGB
1360      * TODO: Make the orc version working properly*/
1361     for (j = 0; j < src->width * 4; j += 4) {
1362       alpha = tmpsrcline[j];
1363
1364       BLEND (tmpdestline[j + 1], alpha, tmpsrcline[j + 1], tmpdestline[j + 1]);
1365       BLEND (tmpdestline[j + 2], alpha, tmpsrcline[j + 2], tmpdestline[j + 2]);
1366       BLEND (tmpdestline[j + 3], alpha, tmpsrcline[j + 3], tmpdestline[j + 3]);
1367     }
1368
1369     /* FIXME
1370      * #if G_BYTE_ORDER == LITTLE_ENDIAN
1371      * orc_blend_little (tmpdestline, tmpsrcline, dest->width);
1372      * #else
1373      * orc_blend_big (tmpdestline, tmpsrcline, src->width);
1374      * #endif
1375      */
1376
1377     getputdest.putline (dest, src, tmpdestline, x, i);
1378
1379   }
1380
1381   g_free (tmpdestline);
1382   g_free (tmpsrcline);
1383
1384   return TRUE;
1385
1386 failed:
1387   GST_WARNING ("Could not do the blending");
1388   g_free (tmpdestline);
1389   g_free (tmpsrcline);
1390
1391   return FALSE;
1392 }
1393
1394 /* video_blend_format_info_init:
1395  * @info: The #GstBlendVideoFormatInfo to initialize
1396  * @pixels: The pixels data in @fmt format
1397  * @height: The height of the image
1398  * @width: the width of the image
1399  * @fmt: The #GstVideoFormat of the image
1400  *
1401  * Initializes a GstBlendVideoFormatInfo.
1402  * This function can be called on already initialized instances.
1403  */
1404 void
1405 video_blend_format_info_init (GstBlendVideoFormatInfo * info,
1406     guint8 * pixels, guint height, guint width, GstVideoFormat fmt)
1407 {
1408   guint nb_component = gst_video_format_has_alpha (fmt) ? 4 : 3;
1409
1410   ensure_debug_category ();
1411
1412   GST_DEBUG
1413       ("Initializing video bleding info, height %i, width %i, fmt %i nb_component %i",
1414       height, width, fmt, nb_component);
1415
1416   info->width = width;
1417   info->height = height;
1418   info->pixels = pixels;
1419   info->fmt = fmt;
1420   info->size = gst_video_format_get_size (fmt, height, width);
1421
1422   fill_planes (info);
1423 }