summaryrefslogtreecommitdiff
blob: f8c3612e5b4a6c063a046db6940a6a397ce59042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
From c854bfe87a39d640dfd3381bcba83281d9316b50 Mon Sep 17 00:00:00 2001
From: Rob Clark <rob@ti.com>
Date: Fri, 27 Nov 2009 15:05:56 -0600
Subject: [PATCH 10/24] refactor stridetransform to make it easier to add new transforms (stride and/or colorspace)

---
 gst/stride/Makefile.am          |    1 +
 gst/stride/convert.c            |  267 +++++++++++++++++++++++++++++++++++
 gst/stride/gststridetransform.c |  295 ++++++++-------------------------------
 gst/stride/gststridetransform.h |   18 ++-
 4 files changed, 340 insertions(+), 241 deletions(-)
 create mode 100644 gst/stride/convert.c

diff --git a/gst/stride/Makefile.am b/gst/stride/Makefile.am
index 1adc197..0b61d55 100644
--- a/gst/stride/Makefile.am
+++ b/gst/stride/Makefile.am
@@ -2,6 +2,7 @@ plugin_LTLIBRARIES = libgststridetransform.la
 
 libgststridetransform_la_SOURCES = \
 	gststridetransform.c \
+	convert.c \
 	plugin.c
 
 libgststridetransform_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_BASE_CFLAGS) $(GST_CFLAGS)
diff --git a/gst/stride/convert.c b/gst/stride/convert.c
new file mode 100644
index 0000000..860f16c
--- /dev/null
+++ b/gst/stride/convert.c
@@ -0,0 +1,267 @@
+/* GStreamer
+ *
+ * Copyright (C) 2009 Texas Instruments, Inc - http://www.ti.com/
+ *
+ * Description: stride transform conversion utilities
+ *  Created on: Nov 27, 2009
+ *      Author: Rob Clark <rob@ti.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <string.h>
+#include <gst/video/video.h>
+
+#include "gststridetransform.h"
+
+
+GST_DEBUG_CATEGORY_EXTERN (stridetransform_debug);
+#define GST_CAT_DEFAULT stridetransform_debug
+
+
+/*
+ * Conversion utilities:
+ */
+
+static void
+memmove_demux (guchar *new_buf, guchar *orig_buf, gint sz, gint pxstride)
+{
+  if (new_buf > orig_buf) {
+    /* copy backwards */
+    new_buf += ((sz - 1) * pxstride);
+    orig_buf += sz - 1;
+    while(sz--) {
+      *new_buf = *orig_buf;
+      new_buf -= pxstride;
+      orig_buf--;
+    }
+  } else {
+    while(sz--) {
+      *new_buf = *orig_buf;
+      new_buf += pxstride;
+      orig_buf++;
+    }
+  }
+}
+
+static void
+stridemove_demux (guchar *new_buf, guchar *orig_buf, gint new_width, gint orig_width, gint height, gint pxstride)
+{
+  int row;
+
+  GST_DEBUG ("new_buf=%p, orig_buf=%p, new_width=%d, orig_width=%d, height=%d",
+      new_buf, orig_buf, new_width, orig_width, height);
+
+  /* if increasing the stride, work from bottom-up to avoid overwriting data
+   * that has not been moved yet.. otherwise, work in the opposite order,
+   * for the same reason.
+   */
+  if (new_width > orig_width) {
+    for (row=height-1; row>=0; row--) {
+      memmove_demux (new_buf+(new_width*row), orig_buf+(orig_width*row), orig_width, pxstride);
+    }
+  } else {
+    for (row=0; row<height; row++) {
+      memmove_demux (new_buf+(new_width*row), orig_buf+(orig_width*row), new_width, pxstride);
+    }
+  }
+}
+
+/**
+ * Convert from one stride to another... like memmove, but can convert stride in
+ * the process.  This function is not aware of pixels, only of bytes.  So widths
+ * are given in bytes, not pixels.  The new_buf and orig_buf can point to the
+ * same buffers to do an in-place conversion, but the buffer should be large
+ * enough.
+ */
+static void
+stridemove (guchar *new_buf, guchar *orig_buf, gint new_width, gint orig_width, gint height)
+{
+  int row;
+
+  GST_DEBUG ("new_buf=%p, orig_buf=%p, new_width=%d, orig_width=%d, height=%d",
+      new_buf, orig_buf, new_width, orig_width, height);
+
+  /* if increasing the stride, work from bottom-up to avoid overwriting data
+   * that has not been moved yet.. otherwise, work in the opposite order,
+   * for the same reason.
+   */
+  if (new_width > orig_width) {
+    for (row=height-1; row>=0; row--) {
+      memmove (new_buf+(new_width*row), orig_buf+(orig_width*row), orig_width);
+    }
+  } else {
+    for (row=0; row<height; row++) {
+      memmove (new_buf+(new_width*row), orig_buf+(orig_width*row), new_width);
+    }
+  }
+}
+
+/*
+ * Conversion Functions:
+ */
+
+/** convert 4:2:0 semiplanar to same 4:2:0 semiplanar */
+static GstFlowReturn
+unstridify_420sp_420sp (GstStrideTransform *self, guchar *unstrided, guchar *strided)
+{
+  gint width  = self->width;
+  gint height = self->height;
+  gint stride = self->in_rowstride;
+
+  g_return_val_if_fail (stride >= width, GST_FLOW_ERROR);
+
+  stridemove (unstrided, strided, width, stride,
+      (GST_ROUND_UP_2 (height) * 3) / 2);
+
+  return GST_FLOW_OK;
+}
+static GstFlowReturn
+stridify_420sp_420sp (GstStrideTransform *self, guchar *strided, guchar *unstrided)
+{
+  gint width  = self->width;
+  gint height = self->height;
+  gint stride = self->out_rowstride;
+
+  g_return_val_if_fail (stride >= width, GST_FLOW_ERROR);
+
+  g_return_val_if_fail (stride >= width, GST_FLOW_ERROR);
+  stridemove (strided, unstrided, stride, width,
+      (GST_ROUND_UP_2 (height) * 3) / 2);
+
+  return GST_FLOW_OK;
+}
+
+/** convert 4:2:0 planar to same 4:2:0 planar */
+static GstFlowReturn
+unstridify_420p_420p (GstStrideTransform *self, guchar *unstrided, guchar *strided)
+{
+  gint width  = self->width;
+  gint height = self->height;
+  gint stride = self->in_rowstride;
+
+  g_return_val_if_fail (stride >= width, GST_FLOW_ERROR);
+
+  stridemove (unstrided, strided, width, stride, height);   /* move Y */
+  stridemove (
+      unstrided + (height*width),
+      strided + (height*stride),
+      width/2, stride, height);                             /* move V/U */
+  /* XXX odd widths/heights/strides: */
+  stridemove (
+      unstrided + (int)(height*width*1.5),
+      strided + (int)(height*stride*1.5),
+      width/2, stride, height);                             /* move U/V */
+
+  return GST_FLOW_OK;
+}
+static GstFlowReturn
+stridify_420p_420p (GstStrideTransform *self, guchar *strided, guchar *unstrided)
+{
+  gint width  = self->width;
+  gint height = self->height;
+  gint stride = self->out_rowstride;
+
+  g_return_val_if_fail (stride >= width, GST_FLOW_ERROR);
+
+  /* XXX odd widths/heights/strides: */
+  stridemove (
+      strided + (int)(height*stride*1.5),
+      unstrided + (int)(height*width*1.5),
+      stride, width/2, height);                             /* move U/V */
+  stridemove (
+      strided + (height*stride),
+      unstrided + (height*width),
+      stride, width/2, height);                             /* move V/U */
+  stridemove (strided, unstrided, stride, width, height);   /* move Y */
+
+  return GST_FLOW_OK;
+}
+
+/** convert 4:2:2 packed to same 4:2:2 packed */
+static GstFlowReturn
+unstridify_422i_422i (GstStrideTransform *self, guchar *unstrided, guchar *strided)
+{
+  gint width  = self->width;
+  gint height = self->height;
+  gint stride = self->in_rowstride;
+
+  g_return_val_if_fail (stride >= (width*2), GST_FLOW_ERROR);
+
+  stridemove (unstrided, strided, width*2, stride, height);
+
+  return GST_FLOW_OK;
+}
+static GstFlowReturn
+stridify_422i_422i (GstStrideTransform *self, guchar *strided, guchar *unstrided)
+{
+  gint width  = self->width;
+  gint height = self->height;
+  gint stride = self->out_rowstride;
+
+  g_return_val_if_fail (stride >= (width*2), GST_FLOW_ERROR);
+
+  stridemove (strided, unstrided, stride, width*2, height);
+
+  return GST_FLOW_OK;
+}
+
+/** convert I420 unstrided to NV12 strided */
+static GstFlowReturn
+stridify_i420_nv12 (GstStrideTransform *self, guchar *strided, guchar *unstrided)
+{
+  gint width  = self->width;
+  gint height = self->height;
+  gint stride = self->out_rowstride;
+
+  g_return_val_if_fail (stride >= width, GST_FLOW_ERROR);
+
+  /* note: if not an in-place conversion, then doing the U&V in one pass
+   * would be more efficient... but if it is an in-place conversion, I'd
+   * need to think about whether it is potential for the new UV plane to
+   * corrupt the V plane before it is done copying..
+   */
+  stridemove_demux (
+      strided + (height*stride) + 1,
+      unstrided + (int)(height*width*1.25),
+      stride, width/2, height/2, 2);                        /* move V */
+  stridemove_demux (
+      strided + (height*stride),
+      unstrided + (height*width),
+      stride, width/2, height/2, 2);                        /* move U */
+  stridemove (strided, unstrided, stride, width, height);   /* move Y */
+
+  return GST_FLOW_OK;
+}
+
+/* last entry has GST_VIDEO_FORMAT_UNKNOWN for in/out formats */
+Conversion stride_conversions[] = {
+  { { GST_VIDEO_FORMAT_NV12, GST_VIDEO_FORMAT_NV12 }, stridify_420sp_420sp, unstridify_420sp_420sp },
+  { { GST_VIDEO_FORMAT_I420, GST_VIDEO_FORMAT_I420 }, stridify_420p_420p,   unstridify_420p_420p },
+  { { GST_VIDEO_FORMAT_YV12, GST_VIDEO_FORMAT_YV12 }, stridify_420p_420p,   unstridify_420p_420p },
+  { { GST_VIDEO_FORMAT_YUY2, GST_VIDEO_FORMAT_YUY2 }, stridify_422i_422i,   unstridify_422i_422i },
+  { { GST_VIDEO_FORMAT_UYVY, GST_VIDEO_FORMAT_UYVY }, stridify_422i_422i,   unstridify_422i_422i },
+  { { GST_VIDEO_FORMAT_I420, GST_VIDEO_FORMAT_NV12 }, stridify_i420_nv12,   NULL },
+  /* add new entries before here */
+  { { GST_VIDEO_FORMAT_UNKNOWN } }
+};
+
+
diff --git a/gst/stride/gststridetransform.c b/gst/stride/gststridetransform.c
index 143a9f7..6ab0479 100644
--- a/gst/stride/gststridetransform.c
+++ b/gst/stride/gststridetransform.c
@@ -47,12 +47,17 @@
 #endif
 
 #include <string.h>
+
 #include <gst/video/video.h>
 
 #include "gst/gst-i18n-plugin.h"
 #include "gststridetransform.h"
 
 
+/* last entry has GST_VIDEO_FORMAT_UNKNOWN for in/out formats */
+extern const Conversion stride_conversions[];
+
+
 static const GstElementDetails stridetransform_details =
 GST_ELEMENT_DETAILS ("Stride transform",
     "Filter/Converter/Video",
@@ -70,14 +75,14 @@ GST_STATIC_PAD_TEMPLATE ("src",
     GST_PAD_SRC,
     GST_PAD_ALWAYS,
     GST_STATIC_CAPS (SUPPORTED_CAPS)
-    );
+);
 
 static GstStaticPadTemplate sink_template =
 GST_STATIC_PAD_TEMPLATE ("sink",
     GST_PAD_SINK,
     GST_PAD_ALWAYS,
     GST_STATIC_CAPS (SUPPORTED_CAPS)
-    );
+);
 
 
 GST_DEBUG_CATEGORY (stridetransform_debug);
@@ -99,8 +104,6 @@ static gboolean gst_stride_transform_set_caps (GstBaseTransform *base,
     GstCaps *incaps, GstCaps *outcaps);
 static GstFlowReturn gst_stride_transform_transform (GstBaseTransform *base,
     GstBuffer *inbuf, GstBuffer *outbuf);
-static GstFlowReturn gst_stride_transform_transform_ip (GstBaseTransform *base,
-    GstBuffer *buf);
 
 GST_BOILERPLATE (GstStrideTransform, gst_stride_transform, GstVideoFilter, GST_TYPE_VIDEO_FILTER);
 
@@ -136,8 +139,6 @@ gst_stride_transform_class_init (GstStrideTransformClass *klass)
       GST_DEBUG_FUNCPTR (gst_stride_transform_transform_caps);
   basetransform_class->set_caps =
       GST_DEBUG_FUNCPTR (gst_stride_transform_set_caps);
-  basetransform_class->transform_ip =
-      GST_DEBUG_FUNCPTR (gst_stride_transform_transform_ip);
   basetransform_class->transform =
       GST_DEBUG_FUNCPTR (gst_stride_transform_transform);
 
@@ -219,7 +220,7 @@ gst_stride_transform_transform_size (GstBaseTransform *base,
  * helper to add all fields, other than rowstride to @caps, copied from @s.
  */
 static void
-add_all_fields (GstCaps *caps, const gchar *name, GstStructure *s, gboolean rowstride)
+add_all_fields (GstCaps *caps, const gchar *name, GstStructure *s, gboolean rowstride, GstPadDirection direction)
 {
   gint idx;
   GstStructure *new_s = gst_structure_new (name, NULL);
@@ -232,38 +233,39 @@ add_all_fields (GstCaps *caps, const gchar *name, GstStructure *s, gboolean rows
   while (idx >= 0) {
     const gchar *name = gst_structure_nth_field_name (s, idx);
     idx--;
+
+    /* for format field, check the stride_conversions table to see what
+     * we can support:
+     */
     if (!strcmp ("format", name)) {
-      // we can do simple color format translations, such as converting from one
-      // YUV420 format to another:
-      GValue formats = {0};
-      GValue fourccval = {0};
       guint fourcc;
-      if (gst_structure_get_fourcc (s, name, &fourcc)) {
-        switch (gst_video_format_from_fourcc (fourcc)) {
-          case GST_VIDEO_FORMAT_NV12:
-          case GST_VIDEO_FORMAT_I420:
-GST_DEBUG ("Hmm, let's say I can convert I420<-->NV12..");
-            g_value_init (&formats, GST_TYPE_LIST);
-            g_value_init (&fourccval, GST_TYPE_FOURCC);
-            gst_value_set_fourcc (&fourccval,
-                GST_MAKE_FOURCC ('I', '4', '2', '0'));
-            gst_value_list_append_value (&formats, &fourccval);
-            gst_value_set_fourcc (&fourccval,
-                GST_MAKE_FOURCC ('N', 'V', '1', '2'));
+
+      /* XXX double check this: */
+      gint to_format = (direction == GST_PAD_SINK) ? 1 : 0;
+      gint from_format = (direction == GST_PAD_SRC) ? 1 : 0;
+
+      if (gst_structure_get_fourcc (s, "format", &fourcc)) {
+        GValue formats = {0};
+        GValue fourccval = {0};
+        gint i;
+        GstVideoFormat format = gst_video_format_from_fourcc (fourcc);
+
+        g_value_init (&formats, GST_TYPE_LIST);
+        g_value_init (&fourccval, GST_TYPE_FOURCC);
+
+        for (i=0; stride_conversions[i].format[0]!=GST_VIDEO_FORMAT_UNKNOWN; i++) {
+          if (stride_conversions[i].format[from_format] == format) {
+            gst_value_set_fourcc (&fourccval, gst_video_format_to_fourcc
+                (stride_conversions[i].format[to_format]));
             gst_value_list_append_value (&formats, &fourccval);
-            gst_structure_set_value (new_s, "format", &formats);
-            continue;
-/* maybe handle other cases later..
-          case GST_VIDEO_FORMAT_YV12:
-          case GST_VIDEO_FORMAT_YUY2:
-          case GST_VIDEO_FORMAT_UYVY:
-*/
-          default:
-            break;
+          }
         }
+
+        continue;
       }
     }
 
+    /* copy over all other non-rowstride fields: */
     if (strcmp ("rowstride", name)) {
       const GValue *val = gst_structure_get_value (s, name);
       gst_structure_set_value (new_s, name, val);
@@ -297,14 +299,14 @@ gst_stride_transform_transform_caps (GstBaseTransform *base,
   if (gst_structure_has_name (s, "video/x-raw-yuv") ||
       gst_structure_has_name (s, "video/x-raw-yuv-strided")) {
 
-    add_all_fields (ret, "video/x-raw-yuv", s, FALSE);
-    add_all_fields (ret, "video/x-raw-yuv-strided", s, TRUE);
+    add_all_fields (ret, "video/x-raw-yuv", s, FALSE, direction);
+    add_all_fields (ret, "video/x-raw-yuv-strided", s, TRUE, direction);
 
   } else if (gst_structure_has_name (s, "video/x-raw-rgb") ||
       gst_structure_has_name (s, "video/x-raw-rgb-strided")) {
 
-    add_all_fields (ret, "video/x-raw-rgb", s, FALSE);
-    add_all_fields (ret, "video/x-raw-rgb-strided", s, TRUE);
+    add_all_fields (ret, "video/x-raw-rgb", s, FALSE, direction);
+    add_all_fields (ret, "video/x-raw-rgb-strided", s, TRUE, direction);
 
   }
 
@@ -324,211 +326,37 @@ gst_stride_transform_set_caps (GstBaseTransform *base,
 {
   GstStrideTransform *self = GST_STRIDE_TRANSFORM (base);
   gint width, height;
+  GstVideoFormat in_format, out_format;
+  gint i;
 
   LOG_CAPS (self, incaps);
   LOG_CAPS (self, outcaps);
 
   g_return_val_if_fail (gst_video_format_parse_caps_strided (incaps,
-      &self->in_format, &self->width, &self->height, &self->in_rowstride), FALSE);
+      &in_format, &self->width, &self->height, &self->in_rowstride), FALSE);
   g_return_val_if_fail (gst_video_format_parse_caps_strided (outcaps,
-      &self->out_format, &width, &height, &self->out_rowstride), FALSE);
-
-  g_return_val_if_fail (self->width  == width,  FALSE);
-  g_return_val_if_fail (self->height == height, FALSE);
-
-  return TRUE;
-}
-
-/* ************************************************************************* */
-
-static void
-memmove_demux (guchar *new_buf, guchar *orig_buf, gint sz, gint pxstride)
-{
-  if (new_buf > orig_buf) {
-    /* copy backwards */
-    new_buf += (sz * pxstride);
-    orig_buf += sz;
-    while(sz--) {
-      *new_buf = *orig_buf;
-      new_buf -= pxstride;
-      orig_buf--;
-    }
-  } else {
-    while(sz--) {
-      *new_buf = *orig_buf;
-      new_buf += pxstride;
-      orig_buf++;
-    }
-  }
-}
-
-static void
-stridemove_demux (guchar *new_buf, guchar *orig_buf, gint new_width, gint orig_width, gint height, gint pxstride)
-{
-  int row;
-
-  GST_DEBUG ("new_buf=%p, orig_buf=%p, new_width=%d, orig_width=%d, height=%d",
-      new_buf, orig_buf, new_width, orig_width, height);
-  /* if increasing the stride, work from bottom-up to avoid overwriting data
-   * that has not been moved yet.. otherwise, work in the opposite order,
-   * for the same reason.
-   */
-  if (new_width > orig_width) {
-    for (row=height-1; row>=0; row--) {
-      memmove_demux (new_buf+(new_width*row), orig_buf+(orig_width*row), orig_width, pxstride);
-    }
-  } else {
-    for (row=0; row<height; row++) {
-      memmove_demux (new_buf+(new_width*row), orig_buf+(orig_width*row), new_width, pxstride);
-    }
-  }
-}
+      &out_format, &width, &height, &self->out_rowstride), FALSE);
 
-/**
- * Convert from one stride to another... like memmove, but can convert stride in
- * the process.  This function is not aware of pixels, only of bytes.  So widths
- * are given in bytes, not pixels.  The new_buf and orig_buf can point to the
- * same buffers to do an in-place conversion, but the buffer should be large
- * enough.
- */
-static void
-stridemove (guchar *new_buf, guchar *orig_buf, gint new_width, gint orig_width, gint height)
-{
-  int row;
-
-  GST_DEBUG ("new_buf=%p, orig_buf=%p, new_width=%d, orig_width=%d, height=%d",
-      new_buf, orig_buf, new_width, orig_width, height);
-  /* if increasing the stride, work from bottom-up to avoid overwriting data
-   * that has not been moved yet.. otherwise, work in the opposite order,
-   * for the same reason.
-   */
-  if (new_width > orig_width) {
-    for (row=height-1; row>=0; row--) {
-      memmove (new_buf+(new_width*row), orig_buf+(orig_width*row), orig_width);
-    }
-  } else {
-    for (row=0; row<height; row++) {
-      memmove (new_buf+(new_width*row), orig_buf+(orig_width*row), new_width);
-    }
-  }
-}
+  self->conversion = NULL;
 
-
-/**
- * Convert from a non-strided buffer to strided.  The two buffer pointers could
- * be pointing to the same memory block for in-place transform.. assuming that
- * the buffer is large enough
- *
- * @strided:    the pointer to the resulting strided buffer
- * @unstrided:  the pointer to the initial unstrided buffer
- * @fourcc:     the color format
- * @stride:     the stride, in bytes
- * @width:      the width in pixels
- * @height:     the height in pixels
- */
-static GstFlowReturn
-stridify (GstStrideTransform *self, guchar *strided, guchar *unstrided)
-{
-  gint width  = self->width;
-  gint height = self->height;
-  gint stride = self->out_rowstride;
-
-  if (self->out_format != self->in_format) {
-
-    if ((self->in_format == GST_VIDEO_FORMAT_I420) &&
-        (self->out_format == GST_VIDEO_FORMAT_NV12)) {
-      /* note: if not an in-place conversion, then doing the U&V in one pass
-       * would be more efficient... but if it is an in-place conversion, I'd
-       * need to think about whether it is potential for the new UV plane to
-       * corrupt the V plane before it is done copying..
-       */
-      stridemove_demux (
-          strided + (height*stride) + 1,
-          unstrided + (int)(height*width*1.25),
-          stride, width/2, height/2, 2);                        /* move V */
-      stridemove_demux (
-          strided + (height*stride),
-          unstrided + (height*width),
-          stride, width/2, height/2, 2);                        /* move U */
-      stridemove (strided, unstrided, stride, width, height);   /* move Y */
-      return GST_FLOW_OK;
+  for (i=0; stride_conversions[i].format[0]!=GST_VIDEO_FORMAT_UNKNOWN; i++) {
+    if ((stride_conversions[i].format[0] == in_format) &&
+        (stride_conversions[i].format[1] == out_format)) {
+      GST_DEBUG_OBJECT (self, "found stride_conversion: %d", i);
+      self->conversion = &stride_conversions[i];
+      break;
     }
   }
 
-  switch (self->out_format) {
-    case GST_VIDEO_FORMAT_NV12:
-      g_return_val_if_fail (stride >= width, GST_FLOW_ERROR);
-      stridemove (strided, unstrided, stride, width, (GST_ROUND_UP_2 (height) * 3) / 2);
-      return GST_FLOW_OK;
-    case GST_VIDEO_FORMAT_I420:
-    case GST_VIDEO_FORMAT_YV12:
-      g_return_val_if_fail (stride >= width, GST_FLOW_ERROR);
-      stridemove (
-          strided + (int)(height*stride*1.5),
-          unstrided + (int)(height*width*1.5),
-          stride, width/2, height);                             /* move U/V */
-      stridemove (
-          strided + (height*stride),
-          unstrided + (height*width),
-          stride, width/2, height);                             /* move V/U */
-      stridemove (strided, unstrided, stride, width, height);   /* move Y */
-      return GST_FLOW_OK;
-    case GST_VIDEO_FORMAT_YUY2:
-    case GST_VIDEO_FORMAT_UYVY:
-      g_return_val_if_fail (stride >= (width*2), GST_FLOW_ERROR);
-      stridemove (strided, unstrided, stride, width*2, height);
-      return GST_FLOW_OK;
-    default:
-      GST_WARNING ("unknown color format!\n");
-      return GST_FLOW_ERROR;
-  }
-}
-
+  g_return_val_if_fail (self->conversion, FALSE);
+  g_return_val_if_fail (self->conversion->unstridify || !self->in_rowstride, FALSE);
+  g_return_val_if_fail (self->conversion->stridify || !self->out_rowstride, FALSE);
+  g_return_val_if_fail (self->width  == width,  FALSE);
+  g_return_val_if_fail (self->height == height, FALSE);
 
-/**
- * Convert from a strided buffer to non-strided.  The two buffer pointers could
- * be pointing to the same memory block for in-place transform..
- *
- * @unstrided:  the pointer to the resulting unstrided buffer
- * @strided:    the pointer to the initial strided buffer
- */
-static GstFlowReturn
-unstridify (GstStrideTransform *self, guchar *unstrided, guchar *strided)
-{
-  gint width  = self->width;
-  gint height = self->height;
-  gint stride = self->in_rowstride;
-
-  switch (self->out_format) {
-    case GST_VIDEO_FORMAT_NV12:
-      g_return_val_if_fail (stride >= width, GST_FLOW_ERROR);
-      stridemove (unstrided, strided, width, stride, (GST_ROUND_UP_2 (height) * 3) / 2);
-      return GST_FLOW_OK;
-    case GST_VIDEO_FORMAT_I420:
-    case GST_VIDEO_FORMAT_YV12:
-      g_return_val_if_fail (stride >= width, GST_FLOW_ERROR);
-      stridemove (unstrided, strided, width, stride, height);   /* move Y */
-      stridemove (
-          unstrided + (height*width),
-          strided + (height*stride),
-          width/2, stride, height);                             /* move V/U */
-      stridemove (
-          unstrided + (int)(height*width*1.5),
-          strided + (int)(height*stride*1.5),
-          width/2, stride, height);                             /* move U/V */
-      return GST_FLOW_OK;
-    case GST_VIDEO_FORMAT_YUY2:
-    case GST_VIDEO_FORMAT_UYVY:
-      g_return_val_if_fail (stride >= (width*2), GST_FLOW_ERROR);
-      stridemove (unstrided, strided, width*2, stride, height);
-      return GST_FLOW_OK;
-    default:
-      GST_WARNING ("unknown color format!\n");
-      return GST_FLOW_ERROR;
-  }
+  return TRUE;
 }
 
-
 static GstFlowReturn
 gst_stride_transform_transform (GstBaseTransform *base,
     GstBuffer *inbuf, GstBuffer *outbuf)
@@ -543,10 +371,10 @@ gst_stride_transform_transform (GstBaseTransform *base,
     GST_DEBUG_OBJECT (self, "not implemented");  // TODO
     return GST_FLOW_ERROR;
   } else if (self->in_rowstride) {
-    return unstridify (self,
+    return self->conversion->unstridify (self,
         GST_BUFFER_DATA (outbuf), GST_BUFFER_DATA (inbuf));
   } else if (self->out_rowstride) {
-    return stridify (self,
+    return self->conversion->stridify (self,
         GST_BUFFER_DATA (outbuf), GST_BUFFER_DATA (inbuf));
   }
 
@@ -555,12 +383,3 @@ gst_stride_transform_transform (GstBaseTransform *base,
 
   return GST_FLOW_ERROR;
 }
-
-static GstFlowReturn
-gst_stride_transform_transform_ip (GstBaseTransform *base,
-    GstBuffer *buf)
-{
-  /* transform function is safe to call with same buffer ptr:
-   */
-  return gst_stride_transform_transform (base, buf, buf);
-}
diff --git a/gst/stride/gststridetransform.h b/gst/stride/gststridetransform.h
index 0141571..bce2526 100644
--- a/gst/stride/gststridetransform.h
+++ b/gst/stride/gststridetransform.h
@@ -2,7 +2,7 @@
  *
  * Copyright (C) 2009 Texas Instruments, Inc - http://www.ti.com/
  *
- * Description: V4L2 sink element
+ * Description: stride transform element
  *  Created on: Jul 2, 2009
  *      Author: Rob Clark <rob@ti.com>
  *
@@ -29,7 +29,6 @@
 #include <gst/video/gstvideofilter.h>
 #include <gst/video/video.h>
 
-
 G_BEGIN_DECLS
 
 #define GST_TYPE_STRIDE_TRANSFORM \
@@ -47,6 +46,19 @@ typedef struct _GstStrideTransform GstStrideTransform;
 typedef struct _GstStrideTransformClass GstStrideTransformClass;
 
 /**
+ * stride/colorspace conversion table (used internally)
+ */
+typedef struct {
+
+  GstVideoFormat format[2];   /* in_format, out_format */
+
+  GstFlowReturn (*stridify) (GstStrideTransform *self, guchar *strided, guchar *unstrided);
+  GstFlowReturn (*unstridify) (GstStrideTransform *self, guchar *unstrided, guchar *strided);
+
+} Conversion;
+
+
+/**
  * GstStrideTransform:
  *
  * Opaque datastructure.
@@ -55,10 +67,10 @@ struct _GstStrideTransform {
   GstVideoFilter videofilter;
 
   /*< private >*/
-  GstVideoFormat in_format, out_format;
   gint width, height;
   gint in_rowstride;
   gint out_rowstride;
+  const Conversion *conversion;
 
   /* for caching the tranform_size() results.. */
   GstCaps *cached_caps[2];
-- 
1.7.1