greedyh.c 27.1 KB
Newer Older
1
2
3
4
/*
 *
 * GStreamer
 * Copyright (C) 2004 Billy Biggs <vektor@dumbterm.net>
5
 * Copyright (C) 2008,2010 Sebastian Dröge <slomo@collabora.co.uk>
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
 *
 * 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.
 */

/*
 * Relicensed for GStreamer from GPL to LGPL with permit from Billy Biggs.
 * See: http://bugzilla.gnome.org/show_bug.cgi?id=163578
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "greedyhmacros.h"

#include <stdlib.h>
#include <string.h>

37
#include <gst/gst.h>
38
#include "plugins.h"
39
#include "gstdeinterlacemethod.h"
40
41
42
#ifdef HAVE_ORC
#include <orc/orc.h>
#endif
43

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#define GST_TYPE_DEINTERLACE_METHOD_GREEDY_H	(gst_deinterlace_method_greedy_h_get_type ())
#define GST_IS_DEINTERLACE_METHOD_GREEDY_H(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_DEINTERLACE_METHOD_GREEDY_H))
#define GST_IS_DEINTERLACE_METHOD_GREEDY_H_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_DEINTERLACE_METHOD_GREEDY_H))
#define GST_DEINTERLACE_METHOD_GREEDY_H_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_DEINTERLACE_METHOD_GREEDY_H, GstDeinterlaceMethodGreedyHClass))
#define GST_DEINTERLACE_METHOD_GREEDY_H(obj)		(G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_DEINTERLACE_METHOD_GREEDY_H, GstDeinterlaceMethodGreedyH))
#define GST_DEINTERLACE_METHOD_GREEDY_H_CLASS(klass)	(G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_DEINTERLACE_METHOD_GREEDY_H, GstDeinterlaceMethodGreedyHClass))
#define GST_DEINTERLACE_METHOD_GREEDY_H_CAST(obj)	((GstDeinterlaceMethodGreedyH*)(obj))

typedef struct
{
  GstDeinterlaceMethod parent;

  guint max_comb, motion_threshold, motion_sense;
} GstDeinterlaceMethodGreedyH;

59
60
61
62
typedef void (*ScanlineFunction) (GstDeinterlaceMethodGreedyH * self,
    const guint8 * L2, const guint8 * L1, const guint8 * L3, const guint8 * L2P,
    guint8 * Dest, gint width);

63
64
65
typedef struct
{
  GstDeinterlaceMethodClass parent_class;
66
  ScanlineFunction scanline_yuy2;       /* This is for YVYU too */
67
  ScanlineFunction scanline_uyvy;
68
  ScanlineFunction scanline_ayuv;
69
70
  ScanlineFunction scanline_planar_y;
  ScanlineFunction scanline_planar_uv;
71
} GstDeinterlaceMethodGreedyHClass;
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
static void
greedyh_scanline_C_ayuv (GstDeinterlaceMethodGreedyH * self, const guint8 * L1,
    const guint8 * L2, const guint8 * L3, const guint8 * L2P, guint8 * Dest,
    gint width)
{
  gint Pos, Comp;
  guint8 l1, l1_1, l3, l3_1;
  guint8 avg, avg_1;
  guint8 avg__1[4] = { 0, };
  guint8 avg_s;
  guint8 avg_sc;
  guint8 best;
  guint16 mov;
  guint8 out;
  guint8 l2, lp2;
  guint8 l2_diff, lp2_diff;
  guint8 min, max;
  guint max_comb = self->max_comb;
  guint motion_sense = self->motion_sense;
  guint motion_threshold = self->motion_threshold;

  width /= 4;
  for (Pos = 0; Pos < width; Pos++) {
    for (Comp = 0; Comp < 4; Comp++) {
      l1 = L1[0];
      l3 = L3[0];

      if (Pos == width - 1) {
        l1_1 = l1;
        l3_1 = l3;
      } else {
        l1_1 = L1[4];
        l3_1 = L3[4];
      }

      /* Average of L1 and L3 */
      avg = (l1 + l3) / 2;

      if (Pos == 0) {
        avg__1[Comp] = avg;
      }

      /* Average of next L1 and next L3 */
      avg_1 = (l1_1 + l3_1) / 2;

      /* Calculate average of one pixel forward and previous */
      avg_s = (avg__1[Comp] + avg_1) / 2;

      /* Calculate average of center and surrounding pixels */
      avg_sc = (avg + avg_s) / 2;

      /* move forward */
      avg__1[Comp] = avg;

      /* Get best L2/L2P, i.e. least diff from above average */
      l2 = L2[0];
      lp2 = L2P[0];

      l2_diff = ABS (l2 - avg_sc);

      lp2_diff = ABS (lp2 - avg_sc);

      if (l2_diff > lp2_diff)
        best = lp2;
      else
        best = l2;

      /* Clip this best L2/L2P by L1/L3 and allow to differ by GreedyMaxComb */
      max = MAX (l1, l3);
      min = MIN (l1, l3);

      if (max < 256 - max_comb)
        max += max_comb;
      else
        max = 255;

      if (min > max_comb)
        min -= max_comb;
      else
        min = 0;

      out = CLAMP (best, min, max);

      if (Comp < 2) {
        /* Do motion compensation for luma, i.e. how much
         * the weave pixel differs */
        mov = ABS (l2 - lp2);
        if (mov > motion_threshold)
          mov -= motion_threshold;
        else
          mov = 0;

        mov = mov * motion_sense;
        if (mov > 256)
          mov = 256;

        /* Weighted sum on clipped weave pixel and average */
        out = (out * (256 - mov) + avg_sc * mov) / 256;
      }

      Dest[0] = out;

      Dest += 1;
      L1 += 1;
      L2 += 1;
      L3 += 1;
      L2P += 1;
    }
  }
}

184
static void
185
greedyh_scanline_C_yuy2 (GstDeinterlaceMethodGreedyH * self, const guint8 * L1,
186
187
    const guint8 * L2, const guint8 * L3, const guint8 * L2P, guint8 * Dest,
    gint width)
188
{
189
190
191
192
193
194
195
196
197
198
199
200
201
  gint Pos;
  guint8 l1_l, l1_1_l, l3_l, l3_1_l;
  guint8 l1_c, l1_1_c, l3_c, l3_1_c;
  guint8 avg_l, avg_c, avg_l_1, avg_c_1;
  guint8 avg_l__1 = 0, avg_c__1 = 0;
  guint8 avg_s_l, avg_s_c;
  guint8 avg_sc_l, avg_sc_c;
  guint8 best_l, best_c;
  guint16 mov_l;
  guint8 out_l, out_c;
  guint8 l2_l, l2_c, lp2_l, lp2_c;
  guint8 l2_l_diff, l2_c_diff, lp2_l_diff, lp2_c_diff;
  guint8 min_l, min_c, max_l, max_c;
202
203
204
  guint max_comb = self->max_comb;
  guint motion_sense = self->motion_sense;
  guint motion_threshold = self->motion_threshold;
205

206
  width /= 2;
207
  for (Pos = 0; Pos < width; Pos++) {
208
209
210
211
212
    l1_l = L1[0];
    l1_c = L1[1];
    l3_l = L3[0];
    l3_c = L3[1];

213
    if (Pos == width - 1) {
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
      l1_1_l = l1_l;
      l1_1_c = l1_c;
      l3_1_l = l3_l;
      l3_1_c = l3_c;
    } else {
      l1_1_l = L1[2];
      l1_1_c = L1[3];
      l3_1_l = L3[2];
      l3_1_c = L3[3];
    }

    /* Average of L1 and L3 */
    avg_l = (l1_l + l3_l) / 2;
    avg_c = (l1_c + l3_c) / 2;

229
230
231
232
233
    if (Pos == 0) {
      avg_l__1 = avg_l;
      avg_c__1 = avg_c;
    }

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
    /* Average of next L1 and next L3 */
    avg_l_1 = (l1_1_l + l3_1_l) / 2;
    avg_c_1 = (l1_1_c + l3_1_c) / 2;

    /* Calculate average of one pixel forward and previous */
    avg_s_l = (avg_l__1 + avg_l_1) / 2;
    avg_s_c = (avg_c__1 + avg_c_1) / 2;

    /* Calculate average of center and surrounding pixels */
    avg_sc_l = (avg_l + avg_s_l) / 2;
    avg_sc_c = (avg_c + avg_s_c) / 2;

    /* move forward */
    avg_l__1 = avg_l;
    avg_c__1 = avg_c;

    /* Get best L2/L2P, i.e. least diff from above average */
    l2_l = L2[0];
    l2_c = L2[1];
    lp2_l = L2P[0];
    lp2_c = L2P[1];

    l2_l_diff = ABS (l2_l - avg_sc_l);
    l2_c_diff = ABS (l2_c - avg_sc_c);

    lp2_l_diff = ABS (lp2_l - avg_sc_l);
    lp2_c_diff = ABS (lp2_c - avg_sc_c);

    if (l2_l_diff > lp2_l_diff)
      best_l = lp2_l;
    else
      best_l = l2_l;

    if (l2_c_diff > lp2_c_diff)
      best_c = lp2_c;
    else
      best_c = l2_c;

    /* Clip this best L2/L2P by L1/L3 and allow to differ by GreedyMaxComb */
    max_l = MAX (l1_l, l3_l);
    min_l = MIN (l1_l, l3_l);
275

276
277
    if (max_l < 256 - max_comb)
      max_l += max_comb;
278
279
    else
      max_l = 255;
280

281
282
    if (min_l > max_comb)
      min_l -= max_comb;
283
284
    else
      min_l = 0;
285

286
287
    max_c = MAX (l1_c, l3_c);
    min_c = MIN (l1_c, l3_c);
288

289
290
    if (max_c < 256 - max_comb)
      max_c += max_comb;
291
292
    else
      max_c = 255;
293

294
295
    if (min_c > max_comb)
      min_c -= max_comb;
296
297
298
299
300
301
302
303
304
    else
      min_c = 0;

    out_l = CLAMP (best_l, min_l, max_l);
    out_c = CLAMP (best_c, min_c, max_c);

    /* Do motion compensation for luma, i.e. how much
     * the weave pixel differs */
    mov_l = ABS (l2_l - lp2_l);
305
306
    if (mov_l > motion_threshold)
      mov_l -= motion_threshold;
307
308
309
    else
      mov_l = 0;

310
    mov_l = mov_l * motion_sense;
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
    if (mov_l > 256)
      mov_l = 256;

    /* Weighted sum on clipped weave pixel and average */
    out_l = (out_l * (256 - mov_l) + avg_sc_l * mov_l) / 256;

    Dest[0] = out_l;
    Dest[1] = out_c;

    Dest += 2;
    L1 += 2;
    L2 += 2;
    L3 += 2;
    L2P += 2;
  }
}

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
static void
greedyh_scanline_C_uyvy (GstDeinterlaceMethodGreedyH * self, const guint8 * L1,
    const guint8 * L2, const guint8 * L3, const guint8 * L2P, guint8 * Dest,
    gint width)
{
  gint Pos;
  guint8 l1_l, l1_1_l, l3_l, l3_1_l;
  guint8 l1_c, l1_1_c, l3_c, l3_1_c;
  guint8 avg_l, avg_c, avg_l_1, avg_c_1;
  guint8 avg_l__1 = 0, avg_c__1 = 0;
  guint8 avg_s_l, avg_s_c;
  guint8 avg_sc_l, avg_sc_c;
  guint8 best_l, best_c;
  guint16 mov_l;
  guint8 out_l, out_c;
  guint8 l2_l, l2_c, lp2_l, lp2_c;
  guint8 l2_l_diff, l2_c_diff, lp2_l_diff, lp2_c_diff;
  guint8 min_l, min_c, max_l, max_c;
  guint max_comb = self->max_comb;
  guint motion_sense = self->motion_sense;
  guint motion_threshold = self->motion_threshold;

  width /= 2;
  for (Pos = 0; Pos < width; Pos++) {
    l1_l = L1[1];
    l1_c = L1[0];
    l3_l = L3[1];
    l3_c = L3[0];

    if (Pos == width - 1) {
      l1_1_l = l1_l;
      l1_1_c = l1_c;
      l3_1_l = l3_l;
      l3_1_c = l3_c;
    } else {
      l1_1_l = L1[3];
      l1_1_c = L1[2];
      l3_1_l = L3[3];
      l3_1_c = L3[2];
    }

    /* Average of L1 and L3 */
    avg_l = (l1_l + l3_l) / 2;
    avg_c = (l1_c + l3_c) / 2;

    if (Pos == 0) {
      avg_l__1 = avg_l;
      avg_c__1 = avg_c;
    }

    /* Average of next L1 and next L3 */
    avg_l_1 = (l1_1_l + l3_1_l) / 2;
    avg_c_1 = (l1_1_c + l3_1_c) / 2;

    /* Calculate average of one pixel forward and previous */
    avg_s_l = (avg_l__1 + avg_l_1) / 2;
    avg_s_c = (avg_c__1 + avg_c_1) / 2;

    /* Calculate average of center and surrounding pixels */
    avg_sc_l = (avg_l + avg_s_l) / 2;
    avg_sc_c = (avg_c + avg_s_c) / 2;

    /* move forward */
    avg_l__1 = avg_l;
    avg_c__1 = avg_c;

    /* Get best L2/L2P, i.e. least diff from above average */
    l2_l = L2[1];
    l2_c = L2[0];
    lp2_l = L2P[1];
    lp2_c = L2P[0];

    l2_l_diff = ABS (l2_l - avg_sc_l);
    l2_c_diff = ABS (l2_c - avg_sc_c);

    lp2_l_diff = ABS (lp2_l - avg_sc_l);
    lp2_c_diff = ABS (lp2_c - avg_sc_c);

    if (l2_l_diff > lp2_l_diff)
      best_l = lp2_l;
    else
      best_l = l2_l;

    if (l2_c_diff > lp2_c_diff)
      best_c = lp2_c;
    else
      best_c = l2_c;

    /* Clip this best L2/L2P by L1/L3 and allow to differ by GreedyMaxComb */
    max_l = MAX (l1_l, l3_l);
    min_l = MIN (l1_l, l3_l);

    if (max_l < 256 - max_comb)
      max_l += max_comb;
    else
      max_l = 255;

    if (min_l > max_comb)
      min_l -= max_comb;
    else
      min_l = 0;

    max_c = MAX (l1_c, l3_c);
    min_c = MIN (l1_c, l3_c);

    if (max_c < 256 - max_comb)
      max_c += max_comb;
    else
      max_c = 255;

    if (min_c > max_comb)
      min_c -= max_comb;
    else
      min_c = 0;

    out_l = CLAMP (best_l, min_l, max_l);
    out_c = CLAMP (best_c, min_c, max_c);

    /* Do motion compensation for luma, i.e. how much
     * the weave pixel differs */
    mov_l = ABS (l2_l - lp2_l);
    if (mov_l > motion_threshold)
      mov_l -= motion_threshold;
    else
      mov_l = 0;

    mov_l = mov_l * motion_sense;
    if (mov_l > 256)
      mov_l = 256;

    /* Weighted sum on clipped weave pixel and average */
    out_l = (out_l * (256 - mov_l) + avg_sc_l * mov_l) / 256;

    Dest[1] = out_l;
    Dest[0] = out_c;

    Dest += 2;
    L1 += 2;
    L2 += 2;
    L3 += 2;
    L2P += 2;
  }
}

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
static void
greedyh_scanline_C_planar_y (GstDeinterlaceMethodGreedyH * self,
    const guint8 * L1, const guint8 * L2, const guint8 * L3, const guint8 * L2P,
    guint8 * Dest, gint width)
{
  gint Pos;
  guint8 l1, l1_1, l3, l3_1;
  guint8 avg, avg_1;
  guint8 avg__1 = 0;
  guint8 avg_s;
  guint8 avg_sc;
  guint8 best;
  guint16 mov;
  guint8 out;
  guint8 l2, lp2;
  guint8 l2_diff, lp2_diff;
  guint8 min, max;
  guint max_comb = self->max_comb;
  guint motion_sense = self->motion_sense;
  guint motion_threshold = self->motion_threshold;

  for (Pos = 0; Pos < width; Pos++) {
    l1 = L1[0];
    l3 = L3[0];

    if (Pos == width - 1) {
      l1_1 = l1;
      l3_1 = l3;
    } else {
      l1_1 = L1[1];
      l3_1 = L3[1];
    }

    /* Average of L1 and L3 */
    avg = (l1 + l3) / 2;

    if (Pos == 0) {
      avg__1 = avg;
    }

    /* Average of next L1 and next L3 */
    avg_1 = (l1_1 + l3_1) / 2;

    /* Calculate average of one pixel forward and previous */
    avg_s = (avg__1 + avg_1) / 2;

    /* Calculate average of center and surrounding pixels */
    avg_sc = (avg + avg_s) / 2;

    /* move forward */
    avg__1 = avg;

    /* Get best L2/L2P, i.e. least diff from above average */
    l2 = L2[0];
    lp2 = L2P[0];

    l2_diff = ABS (l2 - avg_sc);

    lp2_diff = ABS (lp2 - avg_sc);

    if (l2_diff > lp2_diff)
      best = lp2;
    else
      best = l2;

    /* Clip this best L2/L2P by L1/L3 and allow to differ by GreedyMaxComb */
    max = MAX (l1, l3);
    min = MIN (l1, l3);

    if (max < 256 - max_comb)
      max += max_comb;
    else
      max = 255;

    if (min > max_comb)
      min -= max_comb;
    else
      min = 0;

    out = CLAMP (best, min, max);

    /* Do motion compensation for luma, i.e. how much
     * the weave pixel differs */
    mov = ABS (l2 - lp2);
    if (mov > motion_threshold)
      mov -= motion_threshold;
    else
      mov = 0;

    mov = mov * motion_sense;
    if (mov > 256)
      mov = 256;

    /* Weighted sum on clipped weave pixel and average */
    out = (out * (256 - mov) + avg_sc * mov) / 256;

    Dest[0] = out;

    Dest += 1;
    L1 += 1;
    L2 += 1;
    L3 += 1;
    L2P += 1;
  }
}

static void
greedyh_scanline_C_planar_uv (GstDeinterlaceMethodGreedyH * self,
    const guint8 * L1, const guint8 * L2, const guint8 * L3, const guint8 * L2P,
    guint8 * Dest, gint width)
{
  gint Pos;
  guint8 l1, l1_1, l3, l3_1;
  guint8 avg, avg_1;
  guint8 avg__1 = 0;
  guint8 avg_s;
  guint8 avg_sc;
  guint8 best;
  guint8 out;
  guint8 l2, lp2;
  guint8 l2_diff, lp2_diff;
  guint8 min, max;
  guint max_comb = self->max_comb;

  for (Pos = 0; Pos < width; Pos++) {
    l1 = L1[0];
    l3 = L3[0];

    if (Pos == width - 1) {
      l1_1 = l1;
      l3_1 = l3;
    } else {
      l1_1 = L1[1];
      l3_1 = L3[1];
    }

    /* Average of L1 and L3 */
    avg = (l1 + l3) / 2;

    if (Pos == 0) {
      avg__1 = avg;
    }

    /* Average of next L1 and next L3 */
    avg_1 = (l1_1 + l3_1) / 2;

    /* Calculate average of one pixel forward and previous */
    avg_s = (avg__1 + avg_1) / 2;

    /* Calculate average of center and surrounding pixels */
    avg_sc = (avg + avg_s) / 2;

    /* move forward */
    avg__1 = avg;

    /* Get best L2/L2P, i.e. least diff from above average */
    l2 = L2[0];
    lp2 = L2P[0];

    l2_diff = ABS (l2 - avg_sc);

    lp2_diff = ABS (lp2 - avg_sc);

    if (l2_diff > lp2_diff)
      best = lp2;
    else
      best = l2;

    /* Clip this best L2/L2P by L1/L3 and allow to differ by GreedyMaxComb */
    max = MAX (l1, l3);
    min = MIN (l1, l3);

    if (max < 256 - max_comb)
      max += max_comb;
    else
      max = 255;

    if (min > max_comb)
      min -= max_comb;
    else
      min = 0;

    out = CLAMP (best, min, max);

    Dest[0] = out;

    Dest += 1;
    L1 += 1;
    L2 += 1;
    L3 += 1;
    L2P += 1;
  }
}

666
#ifdef BUILD_X86_ASM
667

668
669
#define IS_MMXEXT
#define SIMD_TYPE MMXEXT
670
#define C_FUNCT_YUY2 greedyh_scanline_C_yuy2
671
#define C_FUNCT_UYVY greedyh_scanline_C_uyvy
672
673
674
#define C_FUNCT_PLANAR_Y greedyh_scanline_C_planar_y
#define C_FUNCT_PLANAR_UV greedyh_scanline_C_planar_uv
#define FUNCT_NAME_YUY2 greedyh_scanline_MMXEXT_yuy2
675
#define FUNCT_NAME_UYVY greedyh_scanline_MMXEXT_uyvy
676
677
#define FUNCT_NAME_PLANAR_Y greedyh_scanline_MMXEXT_planar_y
#define FUNCT_NAME_PLANAR_UV greedyh_scanline_MMXEXT_planar_uv
678
#include "greedyh.asm"
679
680
#undef SIMD_TYPE
#undef IS_MMXEXT
681
#undef FUNCT_NAME_YUY2
682
#undef FUNCT_NAME_UYVY
683
684
#undef FUNCT_NAME_PLANAR_Y
#undef FUNCT_NAME_PLANAR_UV
685

686
687
#define IS_3DNOW
#define SIMD_TYPE 3DNOW
688
#define FUNCT_NAME_YUY2 greedyh_scanline_3DNOW_yuy2
689
#define FUNCT_NAME_UYVY greedyh_scanline_3DNOW_uyvy
690
691
#define FUNCT_NAME_PLANAR_Y greedyh_scanline_3DNOW_planar_y
#define FUNCT_NAME_PLANAR_UV greedyh_scanline_3DNOW_planar_uv
692
#include "greedyh.asm"
693
#undef SIMD_TYPE
694
#undef IS_3DNOW
695
#undef FUNCT_NAME_YUY2
696
#undef FUNCT_NAME_UYVY
697
698
#undef FUNCT_NAME_PLANAR_Y
#undef FUNCT_NAME_PLANAR_UV
699
700

#define IS_MMX
701
#define SIMD_TYPE MMX
702
#define FUNCT_NAME_YUY2 greedyh_scanline_MMX_yuy2
703
#define FUNCT_NAME_UYVY greedyh_scanline_MMX_uyvy
704
705
#define FUNCT_NAME_PLANAR_Y greedyh_scanline_MMX_planar_y
#define FUNCT_NAME_PLANAR_UV greedyh_scanline_MMX_planar_uv
706
#include "greedyh.asm"
707
#undef SIMD_TYPE
708
#undef IS_MMX
709
#undef FUNCT_NAME_YUY2
710
#undef FUNCT_NAME_UYVY
711
712
713
714
715
#undef FUNCT_NAME_PLANAR_Y
#undef FUNCT_NAME_PLANAR_UV
#undef C_FUNCT_YUY2
#undef C_FUNCT_PLANAR_Y
#undef C_FUNCT_PLANAR_UV
716

717
718
#endif

719
static void
720
721
722
deinterlace_frame_di_greedyh_packed (GstDeinterlaceMethod * method,
    const GstDeinterlaceField * history, guint history_count,
    GstBuffer * outbuf)
723
{
724
  GstDeinterlaceMethodGreedyH *self = GST_DEINTERLACE_METHOD_GREEDY_H (method);
725
726
  GstDeinterlaceMethodGreedyHClass *klass =
      GST_DEINTERLACE_METHOD_GREEDY_H_GET_CLASS (self);
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
  gint InfoIsOdd = 0;
  gint Line;
  gint RowStride = method->row_stride[0];
  gint FieldHeight = method->frame_height / 2;
  gint Pitch = method->row_stride[0] * 2;
  const guint8 *L1;             // ptr to Line1, of 3
  const guint8 *L2;             // ptr to Line2, the weave line
  const guint8 *L3;             // ptr to Line3
  const guint8 *L2P;            // ptr to prev Line2
  guint8 *Dest = GST_BUFFER_DATA (outbuf);
  ScanlineFunction scanline;

  switch (method->format) {
    case GST_VIDEO_FORMAT_YUY2:
    case GST_VIDEO_FORMAT_YVYU:
      scanline = klass->scanline_yuy2;
      break;
744
745
746
    case GST_VIDEO_FORMAT_UYVY:
      scanline = klass->scanline_uyvy;
      break;
747
748
749
    case GST_VIDEO_FORMAT_AYUV:
      scanline = klass->scanline_ayuv;
      break;
750
751
    default:
      g_assert_not_reached ();
752
      return;
753
  }
754
755
756
757

  // copy first even line no matter what, and the first odd line if we're
  // processing an EVEN field. (note diff from other deint rtns.)

758
  if (history[history_count - 1].flags == PICTURE_INTERLACED_BOTTOM) {
759
760
    InfoIsOdd = 1;

761
762
763
764
765
766
767
768
    L1 = GST_BUFFER_DATA (history[history_count - 2].buf);
    if (history[history_count - 2].flags & PICTURE_INTERLACED_BOTTOM)
      L1 += RowStride;

    L2 = GST_BUFFER_DATA (history[history_count - 1].buf);
    if (history[history_count - 1].flags & PICTURE_INTERLACED_BOTTOM)
      L2 += RowStride;

769
    L3 = L1 + Pitch;
770
771
772
    L2P = GST_BUFFER_DATA (history[history_count - 3].buf);
    if (history[history_count - 3].flags & PICTURE_INTERLACED_BOTTOM)
      L2P += RowStride;
773
774

    // copy first even line
775
    memcpy (Dest, L1, RowStride);
776
    Dest += RowStride;
777
778
  } else {
    InfoIsOdd = 0;
779
780
781
782
783
784
785
786
    L1 = GST_BUFFER_DATA (history[history_count - 2].buf);
    if (history[history_count - 2].flags & PICTURE_INTERLACED_BOTTOM)
      L1 += RowStride;

    L2 = GST_BUFFER_DATA (history[history_count - 1].buf) + Pitch;
    if (history[history_count - 1].flags & PICTURE_INTERLACED_BOTTOM)
      L2 += RowStride;

787
    L3 = L1 + Pitch;
788
789
790
    L2P = GST_BUFFER_DATA (history[history_count - 3].buf) + Pitch;
    if (history[history_count - 3].flags & PICTURE_INTERLACED_BOTTOM)
      L2P += RowStride;
791
792

    // copy first even line
793
    memcpy (Dest, L1, RowStride);
794
    Dest += RowStride;
795
    // then first odd line
796
    memcpy (Dest, L1, RowStride);
797
    Dest += RowStride;
798
799
  }

800
801
802
  for (Line = 0; Line < (FieldHeight - 1); ++Line) {
    scanline (self, L1, L2, L3, L2P, Dest, RowStride);
    Dest += RowStride;
803
    memcpy (Dest, L3, RowStride);
804
    Dest += RowStride;
805
806
807
808
809
810
811
812

    L1 += Pitch;
    L2 += Pitch;
    L3 += Pitch;
    L2P += Pitch;
  }

  if (InfoIsOdd) {
813
    memcpy (Dest, L2, RowStride);
814
815
816
  }
}

817
818
819
820
821
822
823
824
825
826
827
828
829
static void
deinterlace_frame_di_greedyh_planar_plane (GstDeinterlaceMethodGreedyH * self,
    const guint8 * L1, const guint8 * L2, const guint8 * L3, const guint8 * L2P,
    guint8 * Dest, gint RowStride, gint FieldHeight, gint Pitch, gint InfoIsOdd,
    ScanlineFunction scanline)
{
  gint Line;

  // copy first even line no matter what, and the first odd line if we're
  // processing an EVEN field. (note diff from other deint rtns.)

  if (InfoIsOdd) {
    // copy first even line
830
    memcpy (Dest, L1, RowStride);
831
832
833
    Dest += RowStride;
  } else {
    // copy first even line
834
    memcpy (Dest, L1, RowStride);
835
836
    Dest += RowStride;
    // then first odd line
837
    memcpy (Dest, L1, RowStride);
838
839
840
841
842
843
    Dest += RowStride;
  }

  for (Line = 0; Line < (FieldHeight - 1); ++Line) {
    scanline (self, L1, L2, L3, L2P, Dest, RowStride);
    Dest += RowStride;
844
    memcpy (Dest, L3, RowStride);
845
846
847
848
849
850
851
852
853
    Dest += RowStride;

    L1 += Pitch;
    L2 += Pitch;
    L3 += Pitch;
    L2P += Pitch;
  }

  if (InfoIsOdd) {
854
    memcpy (Dest, L2, RowStride);
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
  }
}

static void
deinterlace_frame_di_greedyh_planar (GstDeinterlaceMethod * method,
    const GstDeinterlaceField * history, guint history_count,
    GstBuffer * outbuf)
{
  GstDeinterlaceMethodGreedyH *self = GST_DEINTERLACE_METHOD_GREEDY_H (method);
  GstDeinterlaceMethodGreedyHClass *klass =
      GST_DEINTERLACE_METHOD_GREEDY_H_GET_CLASS (self);
  gint InfoIsOdd;
  gint RowStride;
  gint FieldHeight;
  gint Pitch;
  const guint8 *L1;             // ptr to Line1, of 3
  const guint8 *L2;             // ptr to Line2, the weave line
  const guint8 *L3;             // ptr to Line3
  const guint8 *L2P;            // ptr to prev Line2
  guint8 *Dest;
  gint i;
  gint Offset;
  ScanlineFunction scanline;

  for (i = 0; i < 3; i++) {
    Offset = method->offset[i];

    InfoIsOdd = (history[history_count - 1].flags == PICTURE_INTERLACED_BOTTOM);
    RowStride = method->row_stride[i];
    FieldHeight = method->height[i] / 2;
    Pitch = method->row_stride[i] * 2;

    if (i == 0)
      scanline = klass->scanline_planar_y;
    else
      scanline = klass->scanline_planar_uv;

    Dest = GST_BUFFER_DATA (outbuf) + Offset;

    L1 = GST_BUFFER_DATA (history[history_count - 2].buf) + Offset;
    if (history[history_count - 2].flags & PICTURE_INTERLACED_BOTTOM)
      L1 += RowStride;

    L2 = GST_BUFFER_DATA (history[history_count - 1].buf) + Offset;
    if (history[history_count - 1].flags & PICTURE_INTERLACED_BOTTOM)
      L2 += RowStride;

    L3 = L1 + Pitch;
    L2P = GST_BUFFER_DATA (history[history_count - 3].buf) + Offset;
    if (history[history_count - 3].flags & PICTURE_INTERLACED_BOTTOM)
      L2P += RowStride;

    deinterlace_frame_di_greedyh_planar_plane (self, L1, L2, L3, L2P, Dest,
        RowStride, FieldHeight, Pitch, InfoIsOdd, scanline);
  }
}

912
913
914
915
916
G_DEFINE_TYPE (GstDeinterlaceMethodGreedyH, gst_deinterlace_method_greedy_h,
    GST_TYPE_DEINTERLACE_METHOD);

enum
{
917
918
919
920
  PROP_0,
  PROP_MAX_COMB,
  PROP_MOTION_THRESHOLD,
  PROP_MOTION_SENSE
921
922
};

923
924
925
926
927
928
929
static void
gst_deinterlace_method_greedy_h_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstDeinterlaceMethodGreedyH *self = GST_DEINTERLACE_METHOD_GREEDY_H (object);

  switch (prop_id) {
930
    case PROP_MAX_COMB:
931
932
      self->max_comb = g_value_get_uint (value);
      break;
933
    case PROP_MOTION_THRESHOLD:
934
935
      self->motion_threshold = g_value_get_uint (value);
      break;
936
    case PROP_MOTION_SENSE:
937
938
939
940
941
942
943
944
945
946
947
948
949
950
      self->motion_sense = g_value_get_uint (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
gst_deinterlace_method_greedy_h_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstDeinterlaceMethodGreedyH *self = GST_DEINTERLACE_METHOD_GREEDY_H (object);

  switch (prop_id) {
951
    case PROP_MAX_COMB:
952
953
      g_value_set_uint (value, self->max_comb);
      break;
954
    case PROP_MOTION_THRESHOLD:
955
956
      g_value_set_uint (value, self->motion_threshold);
      break;
957
    case PROP_MOTION_SENSE:
958
959
960
961
962
963
964
965
966
967
968
969
970
      g_value_set_uint (value, self->motion_sense);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  }
}

static void
gst_deinterlace_method_greedy_h_class_init (GstDeinterlaceMethodGreedyHClass *
    klass)
{
  GstDeinterlaceMethodClass *dim_class = (GstDeinterlaceMethodClass *) klass;
  GObjectClass *gobject_class = (GObjectClass *) klass;
971
#ifdef BUILD_X86_ASM
972
973
  guint cpu_flags =
      orc_target_get_default_flags (orc_target_get_by_name ("mmx"));
974
#endif
975
976
977
978

  gobject_class->set_property = gst_deinterlace_method_greedy_h_set_property;
  gobject_class->get_property = gst_deinterlace_method_greedy_h_get_property;

979
  g_object_class_install_property (gobject_class, PROP_MAX_COMB,
980
981
982
983
984
      g_param_spec_uint ("max-comb",
          "Max comb",
          "Max Comb", 0, 255, 5, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
      );

985
  g_object_class_install_property (gobject_class, PROP_MOTION_THRESHOLD,
986
987
988
989
990
991
      g_param_spec_uint ("motion-threshold",
          "Motion Threshold",
          "Motion Threshold",
          0, 255, 25, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
      );

992
  g_object_class_install_property (gobject_class, PROP_MOTION_SENSE,
993
994
995
996
997
998
999
1000
1001
1002
1003
      g_param_spec_uint ("motion-sense",
          "Motion Sense",
          "Motion Sense",
          0, 255, 30, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
      );

  dim_class->fields_required = 4;
  dim_class->name = "Motion Adaptive: Advanced Detection";
  dim_class->nick = "greedyh";
  dim_class->latency = 1;

1004
1005
  dim_class->deinterlace_frame_yuy2 = deinterlace_frame_di_greedyh_packed;
  dim_class->deinterlace_frame_yvyu = deinterlace_frame_di_greedyh_packed;
1006
  dim_class->deinterlace_frame_uyvy = deinterlace_frame_di_greedyh_packed;
1007
  dim_class->deinterlace_frame_ayuv = deinterlace_frame_di_greedyh_packed;
1008
1009
1010
1011
1012
  dim_class->deinterlace_frame_y444 = deinterlace_frame_di_greedyh_planar;
  dim_class->deinterlace_frame_i420 = deinterlace_frame_di_greedyh_planar;
  dim_class->deinterlace_frame_yv12 = deinterlace_frame_di_greedyh_planar;
  dim_class->deinterlace_frame_y42b = deinterlace_frame_di_greedyh_planar;
  dim_class->deinterlace_frame_y41b = deinterlace_frame_di_greedyh_planar;
1013

1014
#ifdef BUILD_X86_ASM
1015
  if (cpu_flags & ORC_TARGET_MMX_MMXEXT) {
1016
    klass->scanline_yuy2 = greedyh_scanline_MMXEXT_yuy2;
1017
    klass->scanline_uyvy = greedyh_scanline_MMXEXT_uyvy;
1018
  } else if (cpu_flags & ORC_TARGET_MMX_3DNOW) {
1019
    klass->scanline_yuy2 = greedyh_scanline_3DNOW_yuy2;
1020
    klass->scanline_uyvy = greedyh_scanline_3DNOW_uyvy;
1021
  } else if (cpu_flags & ORC_TARGET_MMX_MMX) {
1022
    klass->scanline_yuy2 = greedyh_scanline_MMX_yuy2;
1023
    klass->scanline_uyvy = greedyh_scanline_MMX_uyvy;
1024
  } else {
1025
    klass->scanline_yuy2 = greedyh_scanline_C_yuy2;
1026
    klass->scanline_uyvy = greedyh_scanline_C_uyvy;
1027
1028
  }
#else
1029
  klass->scanline_yuy2 = greedyh_scanline_C_yuy2;
1030
  klass->scanline_uyvy = greedyh_scanline_C_uyvy;
1031
#endif
1032
  /* TODO: MMX implementation of these two */
1033
  klass->scanline_ayuv = greedyh_scanline_C_ayuv;
1034
1035
  klass->scanline_planar_y = greedyh_scanline_C_planar_y;
  klass->scanline_planar_uv = greedyh_scanline_C_planar_uv;
1036
1037
1038
1039
}

static void
gst_deinterlace_method_greedy_h_init (GstDeinterlaceMethodGreedyH * self)
1040
{
1041
1042
1043
  self->max_comb = 5;
  self->motion_threshold = 25;
  self->motion_sense = 30;
1044
}