i420_yuy2.c 31.3 KB
Newer Older
1
2
3
/*****************************************************************************
 * i420_yuy2.c : YUV to YUV conversion module for vlc
 *****************************************************************************
4
 * Copyright (C) 2000, 2001 the VideoLAN team
5
 * $Id$
6
7
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
8
 *          Damien Fouilleul <damien@videolan.org>
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
24
25
26
27
28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

29
30
31
32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
34
#include <vlc_vout.h>
35

36
#if defined (MODULE_NAME_IS_i420_yuy2_altivec) && defined(HAVE_ALTIVEC_H)
Sam Hocevar's avatar
Sam Hocevar committed
37
38
39
#   include <altivec.h>
#endif

40
41
42
43
44
45
#include "i420_yuy2.h"

#define SRC_FOURCC  "I420,IYUV,YV12"

#if defined (MODULE_NAME_IS_i420_yuy2)
#    define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv,Y211"
46
#elif defined (MODULE_NAME_IS_i420_yuy2_mmx)
47
#    define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv"
48
49
#elif defined (MODULE_NAME_IS_i420_yuy2_sse2)
#    define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,IUYV,cyuv"
50
#elif defined (MODULE_NAME_IS_i420_yuy2_altivec)
51
#    define DEST_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422"
52
53
54
55
56
57
58
59
60
61
#endif

/*****************************************************************************
 * Local and extern prototypes.
 *****************************************************************************/
static int  Activate ( vlc_object_t * );

static void I420_YUY2           ( vout_thread_t *, picture_t *, picture_t * );
static void I420_YVYU           ( vout_thread_t *, picture_t *, picture_t * );
static void I420_UYVY           ( vout_thread_t *, picture_t *, picture_t * );
62
#if !defined (MODULE_NAME_IS_i420_yuy2_altivec)
63
64
static void I420_IUYV           ( vout_thread_t *, picture_t *, picture_t * );
static void I420_cyuv           ( vout_thread_t *, picture_t *, picture_t * );
65
#endif
66
67
68
69
70
#if defined (MODULE_NAME_IS_i420_yuy2)
static void I420_Y211           ( vout_thread_t *, picture_t *, picture_t * );
#endif

#ifdef MODULE_NAME_IS_i420_yuy2_mmx
71
72
73
/* Initialize MMX-specific constants */
static const uint64_t i_00ffw = 0x00ff00ff00ff00ffULL;
static const uint64_t i_80w   = 0x0000000080808080ULL;
74
75
76
77
78
79
80
#endif

/*****************************************************************************
 * Module descriptor.
 *****************************************************************************/
vlc_module_begin();
#if defined (MODULE_NAME_IS_i420_yuy2)
81
    set_description( _("Conversions from " SRC_FOURCC " to " DEST_FOURCC) );
82
83
84
85
86
    set_capability( "chroma", 80 );
#elif defined (MODULE_NAME_IS_i420_yuy2_mmx)
    set_description( _("MMX conversions from " SRC_FOURCC " to " DEST_FOURCC) );
    set_capability( "chroma", 100 );
    add_requirement( MMX );
87
88
89
90
#elif defined (MODULE_NAME_IS_i420_yuy2_sse2)
    set_description( _("SSE2 conversions from " SRC_FOURCC " to " DEST_FOURCC) );
    set_capability( "chroma", 120 );
    add_requirement( SSE2 );
91
92
#elif defined (MODULE_NAME_IS_i420_yuy2_altivec)
    set_description(
93
            _("AltiVec conversions from " SRC_FOURCC " to " DEST_FOURCC) );
94
95
    set_capability( "chroma", 100 );
    add_requirement( ALTIVEC );
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
#endif
    set_callbacks( Activate, NULL );
vlc_module_end();

/*****************************************************************************
 * Activate: allocate a chroma function
 *****************************************************************************
 * This function allocates and initializes a chroma function
 *****************************************************************************/
static int Activate( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;

    if( p_vout->render.i_width & 1 || p_vout->render.i_height & 1 )
    {
        return -1;
    }

    switch( p_vout->render.i_chroma )
    {
        case VLC_FOURCC('Y','V','1','2'):
        case VLC_FOURCC('I','4','2','0'):
        case VLC_FOURCC('I','Y','U','V'):
            switch( p_vout->output.i_chroma )
            {
                case VLC_FOURCC('Y','U','Y','2'):
                case VLC_FOURCC('Y','U','N','V'):
                    p_vout->chroma.pf_convert = I420_YUY2;
                    break;

                case VLC_FOURCC('Y','V','Y','U'):
                    p_vout->chroma.pf_convert = I420_YVYU;
                    break;

                case VLC_FOURCC('U','Y','V','Y'):
                case VLC_FOURCC('U','Y','N','V'):
                case VLC_FOURCC('Y','4','2','2'):
                    p_vout->chroma.pf_convert = I420_UYVY;
                    break;
135
#if !defined (MODULE_NAME_IS_i420_yuy2_altivec)
136
137
138
139
140
141
142
                case VLC_FOURCC('I','U','Y','V'):
                    p_vout->chroma.pf_convert = I420_IUYV;
                    break;

                case VLC_FOURCC('c','y','u','v'):
                    p_vout->chroma.pf_convert = I420_cyuv;
                    break;
143
#endif
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

#if defined (MODULE_NAME_IS_i420_yuy2)
                case VLC_FOURCC('Y','2','1','1'):
                    p_vout->chroma.pf_convert = I420_Y211;
                    break;
#endif

                default:
                    return -1;
            }
            break;

        default:
            return -1;
    }

    return 0;
}

163
164
165
166
167
168
169
170
171
#if 0
static inline unsigned long long read_cycles(void)
{
    unsigned long long v;
    __asm__ __volatile__("rdtsc" : "=A" (v): );

    return v;
}
#endif
172

173
/* Following functions are local */
174
175
176
177
178
179
/*****************************************************************************
 * I420_YUY2: planar YUV 4:2:0 to packed YUYV 4:2:2
 *****************************************************************************/
static void I420_YUY2( vout_thread_t *p_vout, picture_t *p_source,
                                              picture_t *p_dest )
{
180
181
182
183
    uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
    uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
    uint8_t *p_u = p_source->U_PIXELS;
    uint8_t *p_v = p_source->V_PIXELS;
184
185
186

    int i_x, i_y;

187
#if defined (MODULE_NAME_IS_i420_yuy2_altivec)
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#define VEC_NEXT_LINES( ) \
    p_line1  = p_line2; \
    p_line2 += p_dest->p->i_pitch; \
    p_y1     = p_y2; \
    p_y2    += p_source->p[Y_PLANE].i_pitch;

#define VEC_LOAD_UV( ) \
    u_vec = vec_ld( 0, p_u ); p_u += 16; \
    v_vec = vec_ld( 0, p_v ); p_v += 16;

#define VEC_MERGE( a ) \
    uv_vec = a( u_vec, v_vec ); \
    y_vec = vec_ld( 0, p_y1 ); p_y1 += 16; \
    vec_st( vec_mergeh( y_vec, uv_vec ), 0, p_line1 ); p_line1 += 16; \
    vec_st( vec_mergel( y_vec, uv_vec ), 0, p_line1 ); p_line1 += 16; \
    y_vec = vec_ld( 0, p_y2 ); p_y2 += 16; \
    vec_st( vec_mergeh( y_vec, uv_vec ), 0, p_line2 ); p_line2 += 16; \
    vec_st( vec_mergel( y_vec, uv_vec ), 0, p_line2 ); p_line2 += 16;

    vector unsigned char u_vec;
    vector unsigned char v_vec;
    vector unsigned char uv_vec;
    vector unsigned char y_vec;

212
213
    if( !( ( p_vout->render.i_width % 32 ) |
           ( p_vout->render.i_height % 2 ) ) )
214
215
216
217
218
219
220
221
222
223
224
225
226
    {
        /* Width is a multiple of 32, we take 2 lines at a time */
        for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
        {
            VEC_NEXT_LINES( );
            for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
            {
                VEC_LOAD_UV( );
                VEC_MERGE( vec_mergeh );
                VEC_MERGE( vec_mergel );
            }
        }
    }
227
228
    else if( !( ( p_vout->render.i_width % 16 ) |
                ( p_vout->render.i_height % 4 ) ) )
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
    {
        /* Width is only a multiple of 16, we take 4 lines at a time */
        for( i_y = p_vout->render.i_height / 4 ; i_y-- ; )
        {
            /* Line 1 and 2, pixels 0 to ( width - 16 ) */
            VEC_NEXT_LINES( );
            for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
            {
                VEC_LOAD_UV( );
                VEC_MERGE( vec_mergeh );
                VEC_MERGE( vec_mergel );
            }

            /* Line 1 and 2, pixels ( width - 16 ) to ( width ) */
            VEC_LOAD_UV( );
            VEC_MERGE( vec_mergeh );

            /* Line 3 and 4, pixels 0 to 16 */
            VEC_NEXT_LINES( );
            VEC_MERGE( vec_mergel );

            /* Line 3 and 4, pixels 16 to ( width ) */
            for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
            {
                VEC_LOAD_UV( );
                VEC_MERGE( vec_mergeh );
                VEC_MERGE( vec_mergel );
            }
        }
    }
259
260
261
    else
    {
        /* Crap, use the C version */
262
263
264
#undef VEC_NEXT_LINES
#undef VEC_LOAD_UV
#undef VEC_MERGE
265
266
#endif

267
268
269
270
    const int i_source_margin = p_source->p[0].i_pitch
                                 - p_source->p[0].i_visible_pitch;
    const int i_source_margin_c = p_source->p[1].i_pitch
                                 - p_source->p[1].i_visible_pitch;
271
272
273
    const int i_dest_margin = p_dest->p->i_pitch
                               - p_dest->p->i_visible_pitch;

274
#if !defined(MODULE_NAME_IS_i420_yuy2_sse2)
275
276
277
278
279
280
281
282
283
    for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
    {
        p_line1 = p_line2;
        p_line2 += p_dest->p->i_pitch;

        p_y1 = p_y2;
        p_y2 += p_source->p[Y_PLANE].i_pitch;

#if !defined (MODULE_NAME_IS_i420_yuy2_mmx)
284
        for( i_x = p_vout->render.i_width / 8; i_x-- ; )
285
        {
286
            C_YUV420_YUYV( );
287
288
289
            C_YUV420_YUYV( );
            C_YUV420_YUYV( );
            C_YUV420_YUYV( );
290
        }
291
#else
292
293
        for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
        {
294
295
            MMX_CALL( MMX_YUV420_YUYV );
        }
296
#endif
297
298
299
300
        for( i_x = ( p_vout->render.i_width % 8 ) / 2; i_x-- ; )
        {
            C_YUV420_YUYV( );
        }
301
302
303

        p_y1 += i_source_margin;
        p_y2 += i_source_margin;
304
305
        p_u += i_source_margin_c;
        p_v += i_source_margin_c;
306
307
308
309
        p_line1 += i_dest_margin;
        p_line2 += i_dest_margin;
    }

310
#if defined (MODULE_NAME_IS_i420_yuy2_mmx)
311
    /* re-enable FPU registers */
312
    MMX_END;
313
314
#endif

315
316
#if defined (MODULE_NAME_IS_i420_yuy2_altivec)
    }
317
#endif
318
319
320

#else // defined(MODULE_NAME_IS_i420_yuy2_sse2)
    /*
321
    ** SSE2 128 bits fetch/store instructions are faster
322
323
    ** if memory access is 16 bytes aligned
    */
324

325
    if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
326
        ((intptr_t)p_line2|(intptr_t)p_y2))) )
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
    {
        /* use faster SSE2 aligned fetch and store */
        for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
        {
            p_line1 = p_line2;
            p_line2 += p_dest->p->i_pitch;

            p_y1 = p_y2;
            p_y2 += p_source->p[Y_PLANE].i_pitch;

            for( i_x = p_vout->render.i_width / 16 ; i_x-- ; )
            {
                SSE2_CALL( SSE2_YUV420_YUYV_ALIGNED );
            }
            for( i_x = ( p_vout->render.i_width % 16 ) / 2; i_x-- ; )
            {
                C_YUV420_YUYV( );
            }

            p_y1 += i_source_margin;
            p_y2 += i_source_margin;
            p_u += i_source_margin_c;
            p_v += i_source_margin_c;
            p_line1 += i_dest_margin;
            p_line2 += i_dest_margin;
        }
    }
    else
    {
        /* use slower SSE2 unaligned fetch and store */
        for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
        {
            p_line1 = p_line2;
            p_line2 += p_dest->p->i_pitch;

            p_y1 = p_y2;
            p_y2 += p_source->p[Y_PLANE].i_pitch;

            for( i_x = p_vout->render.i_width / 16 ; i_x-- ; )
            {
                SSE2_CALL( SSE2_YUV420_YUYV_UNALIGNED );
            }
            for( i_x = ( p_vout->render.i_width % 16 ) / 2; i_x-- ; )
            {
                C_YUV420_YUYV( );
            }

            p_y1 += i_source_margin;
            p_y2 += i_source_margin;
            p_u += i_source_margin_c;
            p_v += i_source_margin_c;
            p_line1 += i_dest_margin;
            p_line2 += i_dest_margin;
        }
    }
382
383
    /* make sure all SSE2 stores are visible thereafter */
    SSE2_END;
384

385
#endif // defined(MODULE_NAME_IS_i420_yuy2_sse2)
386
387
388
389
390
391
392
393
}

/*****************************************************************************
 * I420_YVYU: planar YUV 4:2:0 to packed YVYU 4:2:2
 *****************************************************************************/
static void I420_YVYU( vout_thread_t *p_vout, picture_t *p_source,
                                              picture_t *p_dest )
{
394
395
396
397
    uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
    uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
    uint8_t *p_u = p_source->U_PIXELS;
    uint8_t *p_v = p_source->V_PIXELS;
398
399
400

    int i_x, i_y;

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
#if defined (MODULE_NAME_IS_i420_yuy2_altivec)
#define VEC_NEXT_LINES( ) \
    p_line1  = p_line2; \
    p_line2 += p_dest->p->i_pitch; \
    p_y1     = p_y2; \
    p_y2    += p_source->p[Y_PLANE].i_pitch;

#define VEC_LOAD_UV( ) \
    u_vec = vec_ld( 0, p_u ); p_u += 16; \
    v_vec = vec_ld( 0, p_v ); p_v += 16;

#define VEC_MERGE( a ) \
    vu_vec = a( v_vec, u_vec ); \
    y_vec = vec_ld( 0, p_y1 ); p_y1 += 16; \
    vec_st( vec_mergeh( y_vec, vu_vec ), 0, p_line1 ); p_line1 += 16; \
    vec_st( vec_mergel( y_vec, vu_vec ), 0, p_line1 ); p_line1 += 16; \
    y_vec = vec_ld( 0, p_y2 ); p_y2 += 16; \
    vec_st( vec_mergeh( y_vec, vu_vec ), 0, p_line2 ); p_line2 += 16; \
    vec_st( vec_mergel( y_vec, vu_vec ), 0, p_line2 ); p_line2 += 16;

    vector unsigned char u_vec;
    vector unsigned char v_vec;
    vector unsigned char vu_vec;
    vector unsigned char y_vec;

    if( !( ( p_vout->render.i_width % 32 ) |
           ( p_vout->render.i_height % 2 ) ) )
    {
        /* Width is a multiple of 32, we take 2 lines at a time */
        for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
        {
            VEC_NEXT_LINES( );
            for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
            {
                VEC_LOAD_UV( );
                VEC_MERGE( vec_mergeh );
                VEC_MERGE( vec_mergel );
            }
        }
    }
    else if( !( ( p_vout->render.i_width % 16 ) |
                ( p_vout->render.i_height % 4 ) ) )
    {
        /* Width is only a multiple of 16, we take 4 lines at a time */
        for( i_y = p_vout->render.i_height / 4 ; i_y-- ; )
        {
            /* Line 1 and 2, pixels 0 to ( width - 16 ) */
            VEC_NEXT_LINES( );
            for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
            {
                VEC_LOAD_UV( );
                VEC_MERGE( vec_mergeh );
                VEC_MERGE( vec_mergel );
            }

            /* Line 1 and 2, pixels ( width - 16 ) to ( width ) */
            VEC_LOAD_UV( );
            VEC_MERGE( vec_mergeh );

            /* Line 3 and 4, pixels 0 to 16 */
            VEC_NEXT_LINES( );
            VEC_MERGE( vec_mergel );

            /* Line 3 and 4, pixels 16 to ( width ) */
            for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
            {
                VEC_LOAD_UV( );
                VEC_MERGE( vec_mergeh );
                VEC_MERGE( vec_mergel );
            }
        }
    }
    else
    {
        /* Crap, use the C version */
#undef VEC_NEXT_LINES
#undef VEC_LOAD_UV
#undef VEC_MERGE
#endif

481
482
483
484
    const int i_source_margin = p_source->p[0].i_pitch
                                 - p_source->p[0].i_visible_pitch;
    const int i_source_margin_c = p_source->p[1].i_pitch
                                 - p_source->p[1].i_visible_pitch;
485
486
487
    const int i_dest_margin = p_dest->p->i_pitch
                               - p_dest->p->i_visible_pitch;

488
#if !defined(MODULE_NAME_IS_i420_yuy2_sse2)
489
490
491
492
493
494
495
496
497
498
    for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
    {
        p_line1 = p_line2;
        p_line2 += p_dest->p->i_pitch;

        p_y1 = p_y2;
        p_y2 += p_source->p[Y_PLANE].i_pitch;

        for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
        {
499
#if !defined (MODULE_NAME_IS_i420_yuy2_mmx)
500
501
502
503
504
505
506
507
            C_YUV420_YVYU( );
            C_YUV420_YVYU( );
            C_YUV420_YVYU( );
            C_YUV420_YVYU( );
#else
            MMX_CALL( MMX_YUV420_YVYU );
#endif
        }
508
509
510
511
        for( i_x = ( p_vout->render.i_width % 8 ) / 2; i_x-- ; )
        {
            C_YUV420_YVYU( );
        }
512
513
514

        p_y1 += i_source_margin;
        p_y2 += i_source_margin;
515
516
        p_u += i_source_margin_c;
        p_v += i_source_margin_c;
517
518
519
        p_line1 += i_dest_margin;
        p_line2 += i_dest_margin;
    }
520
521

#if defined (MODULE_NAME_IS_i420_yuy2_mmx)
522
    /* re-enable FPU registers */
523
    MMX_END;
524
525
#endif

526
527
528
#if defined (MODULE_NAME_IS_i420_yuy2_altivec)
    }
#endif
529
530
531

#else // defined(MODULE_NAME_IS_i420_yuy2_sse2)
    /*
532
    ** SSE2 128 bits fetch/store instructions are faster
533
534
535
    ** if memory access is 16 bytes aligned
    */
    if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
536
        ((intptr_t)p_line2|(intptr_t)p_y2))) )
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
    {
        /* use faster SSE2 aligned fetch and store */
        for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
        {
            p_line1 = p_line2;
            p_line2 += p_dest->p->i_pitch;

            p_y1 = p_y2;
            p_y2 += p_source->p[Y_PLANE].i_pitch;

            for( i_x = p_vout->render.i_width / 16 ; i_x-- ; )
            {
                SSE2_CALL( SSE2_YUV420_YVYU_ALIGNED );
            }
            for( i_x = ( p_vout->render.i_width % 16 ) / 2; i_x-- ; )
            {
                C_YUV420_YVYU( );
            }

            p_y1 += i_source_margin;
            p_y2 += i_source_margin;
            p_u += i_source_margin_c;
            p_v += i_source_margin_c;
            p_line1 += i_dest_margin;
            p_line2 += i_dest_margin;
        }
    }
    else
    {
        /* use slower SSE2 unaligned fetch and store */
        for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
        {
            p_line1 = p_line2;
            p_line2 += p_dest->p->i_pitch;

            p_y1 = p_y2;
            p_y2 += p_source->p[Y_PLANE].i_pitch;

            for( i_x = p_vout->render.i_width / 16 ; i_x-- ; )
            {
                SSE2_CALL( SSE2_YUV420_YVYU_UNALIGNED );
            }
            for( i_x = ( p_vout->render.i_width % 16 ) / 2; i_x-- ; )
            {
                C_YUV420_YVYU( );
            }

            p_y1 += i_source_margin;
            p_y2 += i_source_margin;
            p_u += i_source_margin_c;
            p_v += i_source_margin_c;
            p_line1 += i_dest_margin;
            p_line2 += i_dest_margin;
        }
    }
592
593
    /* make sure all SSE2 stores are visible thereafter */
    SSE2_END;
594
#endif // defined(MODULE_NAME_IS_i420_yuy2_sse2)
595
596
597
598
599
600
601
602
}

/*****************************************************************************
 * I420_UYVY: planar YUV 4:2:0 to packed UYVY 4:2:2
 *****************************************************************************/
static void I420_UYVY( vout_thread_t *p_vout, picture_t *p_source,
                                              picture_t *p_dest )
{
603
604
605
606
    uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
    uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
    uint8_t *p_u = p_source->U_PIXELS;
    uint8_t *p_v = p_source->V_PIXELS;
607
608
609

    int i_x, i_y;

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
#if defined (MODULE_NAME_IS_i420_yuy2_altivec)
#define VEC_NEXT_LINES( ) \
    p_line1  = p_line2; \
    p_line2 += p_dest->p->i_pitch; \
    p_y1     = p_y2; \
    p_y2    += p_source->p[Y_PLANE].i_pitch;

#define VEC_LOAD_UV( ) \
    u_vec = vec_ld( 0, p_u ); p_u += 16; \
    v_vec = vec_ld( 0, p_v ); p_v += 16;

#define VEC_MERGE( a ) \
    uv_vec = a( u_vec, v_vec ); \
    y_vec = vec_ld( 0, p_y1 ); p_y1 += 16; \
    vec_st( vec_mergeh( uv_vec, y_vec ), 0, p_line1 ); p_line1 += 16; \
    vec_st( vec_mergel( uv_vec, y_vec ), 0, p_line1 ); p_line1 += 16; \
    y_vec = vec_ld( 0, p_y2 ); p_y2 += 16; \
    vec_st( vec_mergeh( uv_vec, y_vec ), 0, p_line2 ); p_line2 += 16; \
    vec_st( vec_mergel( uv_vec, y_vec ), 0, p_line2 ); p_line2 += 16;

    vector unsigned char u_vec;
    vector unsigned char v_vec;
    vector unsigned char uv_vec;
    vector unsigned char y_vec;

    if( !( ( p_vout->render.i_width % 32 ) |
           ( p_vout->render.i_height % 2 ) ) )
    {
        /* Width is a multiple of 32, we take 2 lines at a time */
        for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
        {
            VEC_NEXT_LINES( );
            for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
            {
                VEC_LOAD_UV( );
                VEC_MERGE( vec_mergeh );
                VEC_MERGE( vec_mergel );
            }
        }
    }
    else if( !( ( p_vout->render.i_width % 16 ) |
                ( p_vout->render.i_height % 4 ) ) )
    {
        /* Width is only a multiple of 16, we take 4 lines at a time */
        for( i_y = p_vout->render.i_height / 4 ; i_y-- ; )
        {
            /* Line 1 and 2, pixels 0 to ( width - 16 ) */
            VEC_NEXT_LINES( );
            for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
            {
                VEC_LOAD_UV( );
                VEC_MERGE( vec_mergeh );
                VEC_MERGE( vec_mergel );
            }

            /* Line 1 and 2, pixels ( width - 16 ) to ( width ) */
            VEC_LOAD_UV( );
            VEC_MERGE( vec_mergeh );

            /* Line 3 and 4, pixels 0 to 16 */
            VEC_NEXT_LINES( );
            VEC_MERGE( vec_mergel );

            /* Line 3 and 4, pixels 16 to ( width ) */
            for( i_x = p_vout->render.i_width / 32 ; i_x-- ; )
            {
                VEC_LOAD_UV( );
                VEC_MERGE( vec_mergeh );
                VEC_MERGE( vec_mergel );
            }
        }
    }
    else
    {
        /* Crap, use the C version */
#undef VEC_NEXT_LINES
#undef VEC_LOAD_UV
#undef VEC_MERGE
#endif

690
691
692
693
    const int i_source_margin = p_source->p[0].i_pitch
                                 - p_source->p[0].i_visible_pitch;
    const int i_source_margin_c = p_source->p[1].i_pitch
                                 - p_source->p[1].i_visible_pitch;
694
695
696
    const int i_dest_margin = p_dest->p->i_pitch
                               - p_dest->p->i_visible_pitch;

697
#if !defined(MODULE_NAME_IS_i420_yuy2_sse2)
698
699
700
701
702
703
704
705
706
707
    for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
    {
        p_line1 = p_line2;
        p_line2 += p_dest->p->i_pitch;

        p_y1 = p_y2;
        p_y2 += p_source->p[Y_PLANE].i_pitch;

        for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
        {
708
#if !defined (MODULE_NAME_IS_i420_yuy2_mmx)
709
710
711
712
713
714
715
716
            C_YUV420_UYVY( );
            C_YUV420_UYVY( );
            C_YUV420_UYVY( );
            C_YUV420_UYVY( );
#else
            MMX_CALL( MMX_YUV420_UYVY );
#endif
        }
717
718
719
720
        for( i_x = ( p_vout->render.i_width % 8 ) / 2; i_x--; )
        {
            C_YUV420_UYVY( );
        }
721
722
723

        p_y1 += i_source_margin;
        p_y2 += i_source_margin;
724
725
        p_u += i_source_margin_c;
        p_v += i_source_margin_c;
726
727
728
        p_line1 += i_dest_margin;
        p_line2 += i_dest_margin;
    }
729
730

#if defined (MODULE_NAME_IS_i420_yuy2_mmx)
731
    /* re-enable FPU registers */
732
    MMX_END;
733
#endif
734
735
736
737

#if defined (MODULE_NAME_IS_i420_yuy2_altivec)
    }
#endif
738
739
740

#else // defined(MODULE_NAME_IS_i420_yuy2_sse2)
    /*
741
    ** SSE2 128 bits fetch/store instructions are faster
742
743
744
    ** if memory access is 16 bytes aligned
    */
    if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
745
        ((intptr_t)p_line2|(intptr_t)p_y2))) )
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
    {
        /* use faster SSE2 aligned fetch and store */
        for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
        {
            p_line1 = p_line2;
            p_line2 += p_dest->p->i_pitch;

            p_y1 = p_y2;
            p_y2 += p_source->p[Y_PLANE].i_pitch;

            for( i_x = p_vout->render.i_width / 16 ; i_x-- ; )
            {
                SSE2_CALL( SSE2_YUV420_UYVY_ALIGNED );
            }
            for( i_x = ( p_vout->render.i_width % 16 ) / 2; i_x-- ; )
            {
                C_YUV420_UYVY( );
            }

            p_y1 += i_source_margin;
            p_y2 += i_source_margin;
            p_u += i_source_margin_c;
            p_v += i_source_margin_c;
            p_line1 += i_dest_margin;
            p_line2 += i_dest_margin;
        }
    }
    else
    {
        /* use slower SSE2 unaligned fetch and store */
        for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
        {
            p_line1 = p_line2;
            p_line2 += p_dest->p->i_pitch;

            p_y1 = p_y2;
            p_y2 += p_source->p[Y_PLANE].i_pitch;

            for( i_x = p_vout->render.i_width / 16 ; i_x-- ; )
            {
                SSE2_CALL( SSE2_YUV420_UYVY_UNALIGNED );
            }
            for( i_x = ( p_vout->render.i_width % 16 ) / 2; i_x-- ; )
            {
                C_YUV420_UYVY( );
            }

            p_y1 += i_source_margin;
            p_y2 += i_source_margin;
            p_u += i_source_margin_c;
            p_v += i_source_margin_c;
            p_line1 += i_dest_margin;
            p_line2 += i_dest_margin;
        }
    }
801
802
    /* make sure all SSE2 stores are visible thereafter */
    SSE2_END;
803
#endif // defined(MODULE_NAME_IS_i420_yuy2_sse2)
804
805
}

806
#if !defined (MODULE_NAME_IS_i420_yuy2_altivec)
807
808
809
810
811
812
/*****************************************************************************
 * I420_IUYV: planar YUV 4:2:0 to interleaved packed UYVY 4:2:2
 *****************************************************************************/
static void I420_IUYV( vout_thread_t *p_vout, picture_t *p_source,
                                              picture_t *p_dest )
{
Rafaël Carré's avatar
Rafaël Carré committed
813
    VLC_UNUSED(p_source); VLC_UNUSED(p_dest);
814
815
816
817
818
819
820
821
822
823
    /* FIXME: TODO ! */
    msg_Err( p_vout, "I420_IUYV unimplemented, please harass <sam@zoy.org>" );
}

/*****************************************************************************
 * I420_cyuv: planar YUV 4:2:0 to upside-down packed UYVY 4:2:2
 *****************************************************************************/
static void I420_cyuv( vout_thread_t *p_vout, picture_t *p_source,
                                              picture_t *p_dest )
{
824
    uint8_t *p_line1 = p_dest->p->p_pixels +
825
                       p_dest->p->i_visible_lines * p_dest->p->i_pitch
826
827
                       + p_dest->p->i_pitch;
    uint8_t *p_line2 = p_dest->p->p_pixels +
828
                       p_dest->p->i_visible_lines * p_dest->p->i_pitch;
829
830
831
    uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
    uint8_t *p_u = p_source->U_PIXELS;
    uint8_t *p_v = p_source->V_PIXELS;
832
833
834

    int i_x, i_y;

835
836
837
838
    const int i_source_margin = p_source->p[0].i_pitch
                                 - p_source->p[0].i_visible_pitch;
    const int i_source_margin_c = p_source->p[1].i_pitch
                                 - p_source->p[1].i_visible_pitch;
839
840
841
    const int i_dest_margin = p_dest->p->i_pitch
                               - p_dest->p->i_visible_pitch;

842
#if !defined(MODULE_NAME_IS_i420_yuy2_sse2)
843
844
845
846
847
848
849
850
851
852
    for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
    {
        p_line1 -= 3 * p_dest->p->i_pitch;
        p_line2 -= 3 * p_dest->p->i_pitch;

        p_y1 = p_y2;
        p_y2 += p_source->p[Y_PLANE].i_pitch;

        for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
        {
853
#if !defined (MODULE_NAME_IS_i420_yuy2_mmx)
854
855
856
857
858
859
860
861
            C_YUV420_UYVY( );
            C_YUV420_UYVY( );
            C_YUV420_UYVY( );
            C_YUV420_UYVY( );
#else
            MMX_CALL( MMX_YUV420_UYVY );
#endif
        }
862
863
864
865
        for( i_x = ( p_vout->render.i_width % 8 ) / 2; i_x-- ; )
        {
            C_YUV420_UYVY( );
        }
866
867
868

        p_y1 += i_source_margin;
        p_y2 += i_source_margin;
869
870
        p_u += i_source_margin_c;
        p_v += i_source_margin_c;
871
872
873
        p_line1 += i_dest_margin;
        p_line2 += i_dest_margin;
    }
874
875

#if defined (MODULE_NAME_IS_i420_yuy2_mmx)
876
    /* re-enable FPU registers */
877
    MMX_END;
878
879
880
881
#endif

#else // defined(MODULE_NAME_IS_i420_yuy2_sse2)
    /*
882
    ** SSE2 128 bits fetch/store instructions are faster
883
884
885
    ** if memory access is 16 bytes aligned
    */
    if( 0 == (15 & (p_source->p[Y_PLANE].i_pitch|p_dest->p->i_pitch|
886
        ((intptr_t)p_line2|(intptr_t)p_y2))) )
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
    {
        /* use faster SSE2 aligned fetch and store */
        for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
        {
            p_line1 = p_line2;
            p_line2 += p_dest->p->i_pitch;

            p_y1 = p_y2;
            p_y2 += p_source->p[Y_PLANE].i_pitch;

            for( i_x = p_vout->render.i_width / 16 ; i_x-- ; )
            {
                SSE2_CALL( SSE2_YUV420_UYVY_ALIGNED );
            }
            for( i_x = ( p_vout->render.i_width % 16 ) / 2; i_x-- ; )
            {
                C_YUV420_UYVY( );
            }

            p_y1 += i_source_margin;
            p_y2 += i_source_margin;
            p_u += i_source_margin_c;
            p_v += i_source_margin_c;
            p_line1 += i_dest_margin;
            p_line2 += i_dest_margin;
        }
    }
    else
    {
        /* use slower SSE2 unaligned fetch and store */
        for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
        {
            p_line1 = p_line2;
            p_line2 += p_dest->p->i_pitch;

            p_y1 = p_y2;
            p_y2 += p_source->p[Y_PLANE].i_pitch;

            for( i_x = p_vout->render.i_width / 16 ; i_x-- ; )
            {
                SSE2_CALL( SSE2_YUV420_UYVY_UNALIGNED );
            }
            for( i_x = ( p_vout->render.i_width % 16 ) / 2; i_x-- ; )
            {
                C_YUV420_UYVY( );
            }

            p_y1 += i_source_margin;
            p_y2 += i_source_margin;
            p_u += i_source_margin_c;
            p_v += i_source_margin_c;
            p_line1 += i_dest_margin;
            p_line2 += i_dest_margin;
        }
    }
942
943
    /* make sure all SSE2 stores are visible thereafter */
    SSE2_END;
944
#endif // defined(MODULE_NAME_IS_i420_yuy2_sse2)
945
}
946
#endif // !defined (MODULE_NAME_IS_i420_yuy2_altivec)
947
948
949
950
951
952
953
954

/*****************************************************************************
 * I420_Y211: planar YUV 4:2:0 to packed YUYV 2:1:1
 *****************************************************************************/
#if defined (MODULE_NAME_IS_i420_yuy2)
static void I420_Y211( vout_thread_t *p_vout, picture_t *p_source,
                                              picture_t *p_dest )
{
955
956
957
958
    uint8_t *p_line1, *p_line2 = p_dest->p->p_pixels;
    uint8_t *p_y1, *p_y2 = p_source->Y_PIXELS;
    uint8_t *p_u = p_source->U_PIXELS;
    uint8_t *p_v = p_source->V_PIXELS;
959
960
961

    int i_x, i_y;

962
963
964
965
    const int i_source_margin = p_source->p[0].i_pitch
                                 - p_source->p[0].i_visible_pitch;
    const int i_source_margin_c = p_source->p[1].i_pitch
                                 - p_source->p[1].i_visible_pitch;
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
    const int i_dest_margin = p_dest->p->i_pitch
                               - p_dest->p->i_visible_pitch;

    for( i_y = p_vout->render.i_height / 2 ; i_y-- ; )
    {
        p_line1 = p_line2;
        p_line2 += p_dest->p->i_pitch;

        p_y1 = p_y2;
        p_y2 += p_source->p[Y_PLANE].i_pitch;

        for( i_x = p_vout->render.i_width / 8 ; i_x-- ; )
        {
            C_YUV420_Y211( );
            C_YUV420_Y211( );
        }

        p_y1 += i_source_margin;
        p_y2 += i_source_margin;
985
986
        p_u += i_source_margin_c;
        p_v += i_source_margin_c;
987
988
989
990
991
        p_line1 += i_dest_margin;
        p_line2 += i_dest_margin;
    }
}
#endif