Welcome to mirror list, hosted at ThFree Co, Russian Federation.

yuv2yuv_unscaled.cpp « pixconv « LAVVideo « decoder - github.com/mpc-hc/LAVFilters.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 97a344ec7891b767789b5b11d8b917714366a102 (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
/*
 *      Copyright (C) 2011 Hendrik Leppkes
 *      http://www.1f0.de
 *
 *  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, 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; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *  http://www.gnu.org/copyleft/gpl.html
 */

#include "stdafx.h"

#include <emmintrin.h>

#include "pixconv_internal.h"
#include "pixconv_sse2_templates.h"

template <int nv12>
DECLARE_CONV_FUNC_IMPL(convert_yuv420_yv12_nv12_dither_le)
{
  const uint16_t *y = (const uint16_t *)src[0];
  const uint16_t *u = (const uint16_t *)src[1];
  const uint16_t *v = (const uint16_t *)src[2];

  const int inYStride = srcStride[0] >> 1;
  const int inUVStride = srcStride[1] >> 1;
  const int outYStride = dstStride;
  const int outUVStride = dstStride >> 1;
  const int shift = bpp - 8;

  int line, i;

  __m128i xmm0,xmm1,xmm2,xmm3,xmm4;

  uint8_t *dstY = dst;
  uint8_t *dstV = dst + outYStride * height;
  uint8_t *dstU = dstV + outUVStride * (height >> 1);

  // Process Y
  for (line = 0; line < height; ++line) {
    // Load dithering coefficients for this line
    PIXCONV_LOAD_DITHER_COEFFS(xmm4,line,shift,dithers);

    __m128i *dst128Y = (__m128i *)(dst + line * outYStride);

    for (i = 0; i < width; i+=16) {
      // Load pixels into registers, and apply dithering
      PIXCONV_LOAD_PIXEL16_DITHER(xmm0, xmm4, (y+i), shift);   /* Y0Y0Y0Y0 */
      PIXCONV_LOAD_PIXEL16_DITHER(xmm1, xmm4, (y+i+8), shift); /* Y0Y0Y0Y0 */
      xmm0 = _mm_packus_epi16(xmm0, xmm1);                     /* YYYYYYYY */

      // Write data back
      _mm_stream_si128(dst128Y++, xmm0);
    }

    y += inYStride;
  }

  // Process U & V

  for (line = 0; line < (height >> 1); ++line) {
    // Load dithering coefficients for this line
    PIXCONV_LOAD_DITHER_COEFFS(xmm4,line,shift,dithers);

    __m128i *dst128UV = (__m128i *)(dstV + line * outYStride);
    __m128i *dst128U = (__m128i *)(dstU + line * outUVStride);
    __m128i *dst128V = (__m128i *)(dstV + line * outUVStride);

    for (i = 0; i < (width >> 1); i+=16) {
      PIXCONV_LOAD_PIXEL16_DITHER(xmm0, xmm4, (u+i), shift);    /* U0U0U0U0 */
      PIXCONV_LOAD_PIXEL16_DITHER(xmm1, xmm4, (u+i+8), shift);  /* U0U0U0U0 */
      PIXCONV_LOAD_PIXEL16_DITHER(xmm2, xmm4, (v+i), shift);    /* V0V0V0V0 */
      PIXCONV_LOAD_PIXEL16_DITHER(xmm3, xmm4, (v+i+8), shift);  /* V0V0V0V0 */

      if (nv12) {
        xmm2 = _mm_slli_epi16(xmm2, 8);                         /* 0V0V0V0V */
        xmm3 = _mm_slli_epi16(xmm3, 8);                         /* 0V0V0V0V */
        xmm0 = _mm_or_si128(xmm0, xmm2);                        /* UVUVUVUV */
        xmm1 = _mm_or_si128(xmm1, xmm3);                        /* UVUVUVUV */
        _mm_stream_si128(dst128UV++, xmm0);
        _mm_stream_si128(dst128UV++, xmm1);
      } else {
        xmm0 = _mm_packus_epi16(xmm0, xmm1);                    /* UUUUUUUU */
        _mm_stream_si128(dst128U++, xmm0);

        xmm2 = _mm_packus_epi16(xmm2, xmm3);                    /* VVVVVVVV */
        _mm_stream_si128(dst128V++, xmm2);
      }
    }

    u += inUVStride;
    v += inUVStride;
  }

  return S_OK;
}

// Force creation of these two variants
template HRESULT CLAVPixFmtConverter::convert_yuv420_yv12_nv12_dither_le<0>CONV_FUNC_PARAMS;
template HRESULT CLAVPixFmtConverter::convert_yuv420_yv12_nv12_dither_le<1>CONV_FUNC_PARAMS;

template <int shift>
DECLARE_CONV_FUNC_IMPL(convert_yuv420_px1x_le)
{
  const uint16_t *y = (const uint16_t *)src[0];
  const uint16_t *u = (const uint16_t *)src[1];
  const uint16_t *v = (const uint16_t *)src[2];

  const int inYStride = srcStride[0] >> 1;
  const int inUVStride = srcStride[1] >> 1;
  const int outStride = dstStride << 1;
  const int uvHeight = (outputFormat == LAVOutPixFmt_P010 || outputFormat == LAVOutPixFmt_P016) ? (height >> 1) : height;
  const int uvWidth = width >> 1;

  int line, i;
  __m128i xmm0,xmm1,xmm2;

  // Process Y
  for (line = 0; line < height; ++line) {
    __m128i *dst128Y = (__m128i *)(dst + line * outStride);

    for (i = 0; i < width; i+=16) {
      // Load 8 pixels into register
      PIXCONV_LOAD_PIXEL16(xmm0, (y+i), shift); /* YYYY */
      PIXCONV_LOAD_PIXEL16(xmm1, (y+i+8), shift); /* YYYY */
      // and write them out
      _mm_stream_si128(dst128Y++, xmm0);
      _mm_stream_si128(dst128Y++, xmm1);
    }

    y += inYStride;
  }

  BYTE *dstUV = dst + (height * outStride);

  // Process UV
  for (line = 0; line < uvHeight; ++line) {
    __m128i *dst128UV = (__m128i *)(dstUV + line * outStride);

    for (i = 0; i < uvWidth; i+=8) {
      // Load 8 pixels into register
      PIXCONV_LOAD_PIXEL16(xmm0, (v+i), shift); /* VVVV */
      PIXCONV_LOAD_PIXEL16(xmm1, (u+i), shift); /* UUUU */

      xmm2 = xmm0;
      xmm0 = _mm_unpacklo_epi16(xmm1, xmm0);    /* UVUV */
      xmm2 = _mm_unpackhi_epi16(xmm1, xmm2);    /* UVUV */

      _mm_stream_si128(dst128UV++, xmm0);
      _mm_stream_si128(dst128UV++, xmm2);
    }

    u += inUVStride;
    v += inUVStride;
  }

  return S_OK;
}

// Force creation of these two variants
template HRESULT CLAVPixFmtConverter::convert_yuv420_px1x_le<0>CONV_FUNC_PARAMS;
template HRESULT CLAVPixFmtConverter::convert_yuv420_px1x_le<6>CONV_FUNC_PARAMS;
template HRESULT CLAVPixFmtConverter::convert_yuv420_px1x_le<7>CONV_FUNC_PARAMS;

DECLARE_CONV_FUNC_IMPL(convert_yuv420_yv12)
{
  const uint8_t *y = src[0];
  const uint8_t *u = src[1];
  const uint8_t *v = src[2];

  const int inLumaStride    = srcStride[0];
  const int inChromaStride  = srcStride[1];
  const int outLumaStride   = dstStride;
  const int outChromaStride = dstStride >> 1;

  const int chromaWidth     = width >> 1;
  const int chromaHeight    = height >> 1;

  int line;

  // Copy planes

  // Y
  for(line = 0; line < height; ++line) {
    memcpy(dst, y, width);
    y += inLumaStride;
    dst += outLumaStride;
  }

  uint8_t *dstV = dst;
  uint8_t *dstU = dst + chromaHeight * outChromaStride;

  // U/V
  for(line = 0; line < chromaHeight; ++line) {
    memcpy(dstU, u, chromaWidth);
    memcpy(dstV, v, chromaWidth);
    u += inChromaStride;
    v += inChromaStride;
    dstU += outChromaStride;
    dstV += outChromaStride;
  }

  return S_OK;
}

DECLARE_CONV_FUNC_IMPL(convert_yuv420_nv12)
{
  const uint8_t *y = src[0];
  const uint8_t *u = src[1];
  const uint8_t *v = src[2];

  const int inLumaStride    = srcStride[0];
  const int inChromaStride  = srcStride[1];
  const int outStride       = dstStride;

  const int chromaWidth     = width >> 1;
  const int chromaHeight    = height >> 1;

  int line,i;
  __m128i xmm0,xmm1,xmm2,xmm3;

  // Y
  for(line = 0; line < height; ++line) {
    memcpy(dst, y, width);
    y += inLumaStride;
    dst += outStride;
  }

  // U/V
  for(line = 0; line < chromaHeight; ++line) {
    __m128i *dst128UV = (__m128i *)(dst + line * outStride);

    for (i = 0; i < chromaWidth; i+=16) {
      PIXCONV_LOAD_PIXEL8_ALIGNED(xmm0, (v+i));  /* VVVV */
      PIXCONV_LOAD_PIXEL8_ALIGNED(xmm1, (u+i));  /* UUUU */

      xmm2 = _mm_unpacklo_epi8(xmm1, xmm0);      /* UVUV */
      xmm3 = _mm_unpackhi_epi8(xmm1, xmm0);      /* UVUV */

      _mm_stream_si128(dst128UV++, xmm2);
      _mm_stream_si128(dst128UV++, xmm3);
    }

    u += inChromaStride;
    v += inChromaStride;
  }

  return S_OK;
}

template <int uyvy>
DECLARE_CONV_FUNC_IMPL(convert_yuv422_yuy2_uyvy)
{
  const uint8_t *y = src[0];
  const uint8_t *u = src[1];
  const uint8_t *v = src[2];

  const int inLumaStride    = srcStride[0];
  const int inChromaStride  = srcStride[1];
  const int outStride       = dstStride << 1;

  const int chromaWidth     = width >> 1;

  int line,i;
  __m128i xmm0,xmm1,xmm2,xmm3,xmm4,xmm5;

  for (line = 0;  line < height; ++line) {
    __m128i *dst128 = (__m128i *)(dst + line * outStride);

    for (i = 0; i < chromaWidth; i+=16) {
      // Load pixels
      PIXCONV_LOAD_PIXEL8_ALIGNED(xmm0, (y+(i*2)+0));  /* YYYY */
      PIXCONV_LOAD_PIXEL8_ALIGNED(xmm1, (y+(i*2)+16)); /* YYYY */
      PIXCONV_LOAD_PIXEL8_ALIGNED(xmm2, (u+i));        /* UUUU */
      PIXCONV_LOAD_PIXEL8_ALIGNED(xmm3, (v+i));        /* VVVV */

      // Interleave Us and Vs
      xmm4 = xmm2;
      xmm4 = _mm_unpacklo_epi8(xmm4,xmm3);
      xmm2 = _mm_unpackhi_epi8(xmm2,xmm3);

      // Interlave those with the Ys
      if (uyvy) {
        xmm3 = xmm4;
        xmm3 = _mm_unpacklo_epi8(xmm3, xmm0);
        xmm4 = _mm_unpackhi_epi8(xmm4, xmm0);
      } else {
        xmm3 = xmm0;
        xmm3 = _mm_unpacklo_epi8(xmm3, xmm4);
        xmm4 = _mm_unpackhi_epi8(xmm0, xmm4);
      }

      _mm_stream_si128(dst128++, xmm3);
      _mm_stream_si128(dst128++, xmm4);

      // Interlave those with the Ys
      if (uyvy) {
        xmm5 = xmm2;
        xmm5 = _mm_unpacklo_epi8(xmm5, xmm1);
        xmm2 = _mm_unpackhi_epi8(xmm2, xmm1);
      } else {
        xmm5 = xmm1;
        xmm5 = _mm_unpacklo_epi8(xmm5, xmm2);
        xmm2 = _mm_unpackhi_epi8(xmm1, xmm2);
      }

      _mm_stream_si128(dst128++, xmm5);
      _mm_stream_si128(dst128++, xmm2);
    }
    y += inLumaStride;
    u += inChromaStride;
    v += inChromaStride;
  }

  return S_OK;
}

// Force creation of these two variants
template HRESULT CLAVPixFmtConverter::convert_yuv422_yuy2_uyvy<0>CONV_FUNC_PARAMS;
template HRESULT CLAVPixFmtConverter::convert_yuv422_yuy2_uyvy<1>CONV_FUNC_PARAMS;