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

MemSubPic.cpp « SubPic « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1505f4cb0631df77308748a96b0e9ee007c10d0b (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
/*
 * (C) 2003-2006 Gabest
 * (C) 2006-2014 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC 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 3 of the License, or
 * (at your option) any later version.
 *
 * MPC-HC 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "stdafx.h"
#include "MemSubPic.h"

// For CPUID usage
#include "../DSUtil/vd.h"
#include <emmintrin.h>

// color conv

static unsigned char clipBase[256 * 3];
static unsigned char* clip = clipBase + 256;

static const int c2y_cyb = std::lround(0.114 * 219 / 255 * 65536);
static const int c2y_cyg = std::lround(0.587 * 219 / 255 * 65536);
static const int c2y_cyr = std::lround(0.299 * 219 / 255 * 65536);
static const int c2y_cu = std::lround(1.0 / 2.018 * 1024);
static const int c2y_cv = std::lround(1.0 / 1.596 * 1024);

int c2y_yb[256];
int c2y_yg[256];
int c2y_yr[256];

static const int y2c_cbu = std::lround(2.018 * 65536);
static const int y2c_cgu = std::lround(0.391 * 65536);
static const int y2c_cgv = std::lround(0.813 * 65536);
static const int y2c_crv = std::lround(1.596 * 65536);
static int y2c_bu[256];
static int y2c_gu[256];
static int y2c_gv[256];
static int y2c_rv[256];

static const int cy_cy = std::lround(255.0 / 219.0 * 65536);
static const int cy_cy2 = std::lround(255.0 / 219.0 * 32768);

void ColorConvInit()
{
    static bool bColorConvInitOK = false;
    if (bColorConvInitOK) {
        return;
    }

    for (int i = 0; i < 256; i++) {
        clipBase[i] = 0;
        clipBase[i + 256] = i;
        clipBase[i + 512] = 255;
    }

    for (int i = 0; i < 256; i++) {
        c2y_yb[i] = c2y_cyb * i;
        c2y_yg[i] = c2y_cyg * i;
        c2y_yr[i] = c2y_cyr * i;

        y2c_bu[i] = y2c_cbu * (i - 128);
        y2c_gu[i] = y2c_cgu * (i - 128);
        y2c_gv[i] = y2c_cgv * (i - 128);
        y2c_rv[i] = y2c_crv * (i - 128);
    }

    bColorConvInitOK = true;
}

//
// CMemSubPic
//

CMemSubPic::CMemSubPic(const SubPicDesc& spd)
    : m_spd(spd)
{
    m_maxsize.SetSize(spd.w, spd.h);
    m_rcDirty.SetRect(0, 0, spd.w, spd.h);
}

CMemSubPic::~CMemSubPic()
{
    delete [] m_spd.bits;
}

// ISubPic

STDMETHODIMP_(void*) CMemSubPic::GetObject()
{
    return (void*)&m_spd;
}

STDMETHODIMP CMemSubPic::GetDesc(SubPicDesc& spd)
{
    spd.type = m_spd.type;
    spd.w = m_size.cx;
    spd.h = m_size.cy;
    spd.bpp = m_spd.bpp;
    spd.pitch = m_spd.pitch;
    spd.bits = m_spd.bits;
    spd.bitsU = m_spd.bitsU;
    spd.bitsV = m_spd.bitsV;
    spd.vidrect = m_vidrect;

    return S_OK;
}

STDMETHODIMP CMemSubPic::CopyTo(ISubPic* pSubPic)
{
    HRESULT hr;
    if (FAILED(hr = __super::CopyTo(pSubPic))) {
        return hr;
    }

    SubPicDesc src, dst;
    if (FAILED(GetDesc(src)) || FAILED(pSubPic->GetDesc(dst))) {
        return E_FAIL;
    }

    int w = m_rcDirty.Width(), h = m_rcDirty.Height();
    BYTE* s = src.bits + src.pitch * m_rcDirty.top + m_rcDirty.left * 4;
    BYTE* d = dst.bits + dst.pitch * m_rcDirty.top + m_rcDirty.left * 4;

    for (ptrdiff_t j = 0; j < h; j++, s += src.pitch, d += dst.pitch) {
        memcpy(d, s, w * 4);
    }

    return S_OK;
}

STDMETHODIMP CMemSubPic::ClearDirtyRect(DWORD color)
{
    if (m_rcDirty.IsRectEmpty()) {
        return S_FALSE;
    }

    BYTE* p = m_spd.bits + m_spd.pitch * m_rcDirty.top + m_rcDirty.left * (m_spd.bpp >> 3);
    for (ptrdiff_t j = 0, h = m_rcDirty.Height(); j < h; j++, p += m_spd.pitch) {
        int w = m_rcDirty.Width();
#ifdef _WIN64
        memsetd(p, color, w * 4); // nya
#else
        __asm {
            mov eax, color
            mov ecx, w
            mov edi, p
            cld
            rep stosd
        }
#endif
    }

    m_rcDirty.SetRectEmpty();

    return S_OK;
}

STDMETHODIMP CMemSubPic::Lock(SubPicDesc& spd)
{
    return GetDesc(spd);
}

STDMETHODIMP CMemSubPic::Unlock(RECT* pDirtyRect)
{
    m_rcDirty = pDirtyRect ? *pDirtyRect : CRect(0, 0, m_spd.w, m_spd.h);

    if (m_rcDirty.IsRectEmpty()) {
        return S_OK;
    }

    if (m_spd.type == MSP_YUY2 || m_spd.type == MSP_YV12 || m_spd.type == MSP_IYUV || m_spd.type == MSP_AYUV) {
        ColorConvInit();

        if (m_spd.type == MSP_YUY2 || m_spd.type == MSP_YV12 || m_spd.type == MSP_IYUV) {
            m_rcDirty.left &= ~1;
            m_rcDirty.right = (m_rcDirty.right + 1) & ~1;

            if (m_spd.type == MSP_YV12 || m_spd.type == MSP_IYUV) {
                m_rcDirty.top &= ~1;
                m_rcDirty.bottom = (m_rcDirty.bottom + 1) & ~1;
            }
        }
    }

    int w = m_rcDirty.Width(), h = m_rcDirty.Height();
    BYTE* top = m_spd.bits + m_spd.pitch * m_rcDirty.top + m_rcDirty.left * 4;
    BYTE* bottom = top + m_spd.pitch * h;

    if (m_spd.type == MSP_RGB16) {
        for (; top < bottom ; top += m_spd.pitch) {
            DWORD* s = (DWORD*)top;
            DWORD* e = s + w;
            for (; s < e; s++) {
                *s = ((*s >> 3) & 0x1f000000) | ((*s >> 8) & 0xf800) | ((*s >> 5) & 0x07e0) | ((*s >> 3) & 0x001f);
                //*s = (*s&0xff000000)|((*s>>8)&0xf800)|((*s>>5)&0x07e0)|((*s>>3)&0x001f);
            }
        }
    } else if (m_spd.type == MSP_RGB15) {
        for (; top < bottom; top += m_spd.pitch) {
            DWORD* s = (DWORD*)top;
            DWORD* e = s + w;
            for (; s < e; s++) {
                *s = ((*s >> 3) & 0x1f000000) | ((*s >> 9) & 0x7c00) | ((*s >> 6) & 0x03e0) | ((*s >> 3) & 0x001f);
                //*s = (*s&0xff000000)|((*s>>9)&0x7c00)|((*s>>6)&0x03e0)|((*s>>3)&0x001f);
            }
        }
    } else if (m_spd.type == MSP_YUY2 || m_spd.type == MSP_YV12 || m_spd.type == MSP_IYUV) {
        for (; top < bottom ; top += m_spd.pitch) {
            BYTE* s = top;
            BYTE* e = s + w * 4;
            for (; s < e; s += 8) { // ARGB ARGB -> AxYU AxYV
                if ((s[3] + s[7]) < 0x1fe) {
                    s[1] = (c2y_yb[s[0]] + c2y_yg[s[1]] + c2y_yr[s[2]] + 0x108000) >> 16;
                    s[5] = (c2y_yb[s[4]] + c2y_yg[s[5]] + c2y_yr[s[6]] + 0x108000) >> 16;

                    int scaled_y = (s[1] + s[5] - 32) * cy_cy2;

                    s[0] = clip[(((((s[0] + s[4]) << 15) - scaled_y) >> 10) * c2y_cu + 0x800000 + 0x8000) >> 16];
                    s[4] = clip[(((((s[2] + s[6]) << 15) - scaled_y) >> 10) * c2y_cv + 0x800000 + 0x8000) >> 16];
                } else {
                    s[1] = s[5] = 0x10;
                    s[0] = s[4] = 0x80;
                }
            }
        }
    } else if (m_spd.type == MSP_AYUV) {
        for (; top < bottom ; top += m_spd.pitch) {
            BYTE* s = top;
            BYTE* e = s + w * 4;

            for (; s < e; s += 4) { // ARGB -> AYUV
                if (s[3] < 0xff) {
                    int y = (c2y_yb[s[0]] + c2y_yg[s[1]] + c2y_yr[s[2]] + 0x108000) >> 16;
                    int scaled_y = (y - 32) * cy_cy;
                    s[1] = clip[((((s[0] << 16) - scaled_y) >> 10) * c2y_cu + 0x800000 + 0x8000) >> 16];
                    s[0] = clip[((((s[2] << 16) - scaled_y) >> 10) * c2y_cv + 0x800000 + 0x8000) >> 16];
                    s[2] = y;
                } else {
                    s[0] = s[1] = 0x80;
                    s[2] = 0x10;
                }
            }
        }
    }

    return S_OK;
}

#ifdef _WIN64
void AlphaBlt_YUY2_SSE2(int w, int h, BYTE* d, int dstpitch, BYTE* s, int srcpitch)
{
    unsigned int ia;
    static const __int64 _8181 = 0x0080001000800010i64;

    for (ptrdiff_t j = 0; j < h; j++, s += srcpitch, d += dstpitch) {
        DWORD* d2 = (DWORD*)d;
        BYTE* s2 = s;
        BYTE* s2end = s2 + w * 4;

        for (; s2 < s2end; s2 += 8, d2++) {
            ia = (s2[3] + s2[7]) >> 1;
            if (ia < 0xff) {
                unsigned int c = (s2[4] << 24) | (s2[5] << 16) | (s2[0] << 8) | s2[1]; // (v<<24)|(y2<<16)|(u<<8)|y1;

                ia = (ia << 24) | (s2[7] << 16) | (ia << 8) | s2[3];
                // SSE2
                __m128i mm_zero = _mm_setzero_si128();
                __m128i mm_8181 = _mm_move_epi64(_mm_cvtsi64_si128(_8181));
                __m128i mm_c = _mm_cvtsi32_si128(c);
                mm_c = _mm_unpacklo_epi8(mm_c, mm_zero);
                __m128i mm_d = _mm_cvtsi32_si128(*d2);
                mm_d = _mm_unpacklo_epi8(mm_d, mm_zero);
                __m128i mm_a = _mm_cvtsi32_si128(ia);
                mm_a = _mm_unpacklo_epi8(mm_a, mm_zero);
                mm_a = _mm_srli_epi16(mm_a, 1);
                mm_d = _mm_sub_epi16(mm_d, mm_8181);
                mm_d = _mm_mullo_epi16(mm_d, mm_a);
                mm_d = _mm_srai_epi16(mm_d, 7);
                mm_d = _mm_adds_epi16(mm_d, mm_c);
                mm_d = _mm_packus_epi16(mm_d, mm_d);
                *d2 = (DWORD)_mm_cvtsi128_si32(mm_d);
            }
        }
    }
}

#else

void AlphaBlt_YUY2_MMX(int w, int h, BYTE* d, int dstpitch, BYTE* s, int srcpitch)
{
    unsigned int ia;
    static const __int64 _8181 = 0x0080001000800010i64;

    for (ptrdiff_t j = 0; j < h; j++, s += srcpitch, d += dstpitch) {
        DWORD* d2 = (DWORD*)d;
        BYTE* s2 = s;
        BYTE* s2end = s2 + w * 4;

        for (; s2 < s2end; s2 += 8, d2++) {
            ia = (s2[3] + s2[7]) >> 1;
            if (ia < 0xff) {
                unsigned int c = (s2[4] << 24) | (s2[5] << 16) | (s2[0] << 8) | s2[1]; // (v<<24)|(y2<<16)|(u<<8)|y1;
                ia = (ia << 24) | (s2[7] << 16) | (ia << 8) | s2[3];
                __asm {
                    mov         esi, s2
                    mov         edi, d2
                    pxor        mm0, mm0
                    movq        mm1, _8181
                    movd        mm2, c
                    punpcklbw   mm2, mm0
                    movd        mm3, [edi]
                    punpcklbw   mm3, mm0
                    movd        mm4, ia
                    punpcklbw   mm4, mm0
                    psrlw       mm4, 1
                    psubsw      mm3, mm1
                    pmullw      mm3, mm4
                    psraw       mm3, 7
                    paddsw      mm3, mm2
                    packuswb    mm3, mm3
                    movd        [edi], mm3
                };
            }
        }
    }
    _mm_empty();
}
#endif

void AlphaBlt_YUY2_C(int w, int h, BYTE* d, int dstpitch, BYTE* s, int srcpitch)
{
    unsigned int ia;

    for (ptrdiff_t j = 0; j < h; j++, s += srcpitch, d += dstpitch) {
        DWORD* d2 = (DWORD*)d;
        BYTE* s2 = s;
        BYTE* s2end = s2 + w * 4;

        for (; s2 < s2end; s2 += 8, d2++) {
            ia = (s2[3] + s2[7]) >> 1;
            if (ia < 0xff) {
                //unsigned int c = (s2[4]<<24)|(s2[5]<<16)|(s2[0]<<8)|s2[1]; // (v<<24)|(y2<<16)|(u<<8)|y1;

                // YUY2 colorspace fix. rewritten from sse2 asm
                DWORD y1 = (DWORD)(((((*d2 & 0xff) - 0x10) * (s2[3] >> 1)) >> 7) + s2[1]) & 0xff; // y1
                DWORD uu = (DWORD)((((((*d2 >> 8) & 0xff) - 0x80) * (ia >> 1)) >> 7) + s2[0]) & 0xff; // u
                DWORD y2 = (DWORD)((((((*d2 >> 16) & 0xff) - 0x10) * (s2[7] >> 1)) >> 7) + s2[5]) & 0xff; // y2
                DWORD vv = (DWORD)((((((*d2 >> 24) & 0xff) - 0x80) * (ia >> 1)) >> 7) + s2[4]) & 0xff; // v
                *d2 = (y1) | (uu << 8) | (y2 << 16) | (vv << 24);
            }
        }
    }
}

STDMETHODIMP CMemSubPic::AlphaBlt(RECT* pSrc, RECT* pDst, SubPicDesc* pTarget)
{
    ASSERT(pTarget);

    if (!pSrc || !pDst || !pTarget) {
        return E_POINTER;
    }

    const SubPicDesc& src = m_spd;
    SubPicDesc dst = *pTarget; // copy, because we might modify it

    if (src.type != dst.type) {
        return E_INVALIDARG;
    }

    CRect rs(*pSrc), rd(*pDst);

    if (dst.h < 0) {
        dst.h = -dst.h;
        rd.bottom = dst.h - rd.bottom;
        rd.top = dst.h - rd.top;
    }

    if (rs.Width() != rd.Width() || rs.Height() != abs(rd.Height())) {
        return E_INVALIDARG;
    }

    int w = rs.Width(), h = rs.Height();
    BYTE* s = src.bits + src.pitch * rs.top + rs.left * 4;
    BYTE* d = dst.bits + dst.pitch * rd.top + ((rd.left * dst.bpp) >> 3);

    if (rd.top > rd.bottom) {
        if (dst.type == MSP_RGB32 || dst.type == MSP_RGB24
                || dst.type == MSP_RGB16 || dst.type == MSP_RGB15
                || dst.type == MSP_YUY2 || dst.type == MSP_AYUV) {
            d = dst.bits + dst.pitch * (rd.top - 1) + (rd.left * dst.bpp >> 3);
        } else if (dst.type == MSP_YV12 || dst.type == MSP_IYUV) {
            d = dst.bits + dst.pitch * (rd.top - 1) + (rd.left * 8 >> 3);
        } else {
            return E_NOTIMPL;
        }

        dst.pitch = -dst.pitch;
    }

    // TODO: m_bInvAlpha support
    switch (dst.type) {
        case MSP_RGBA:
            for (ptrdiff_t j = 0; j < h; j++, s += src.pitch, d += dst.pitch) {
                BYTE* s2 = s;
                BYTE* s2end = s2 + w * 4;
                DWORD* d2 = (DWORD*)d;
                for (; s2 < s2end; s2 += 4, d2++) {
                    if (s2[3] < 0xff) {
                        DWORD bd = 0x00000100 - ((DWORD) s2[3]);
                        DWORD B = ((*((DWORD*)s2) & 0x000000ff) << 8) / bd;
                        DWORD V = ((*((DWORD*)s2) & 0x0000ff00) / bd) << 8;
                        DWORD R = (((*((DWORD*)s2) & 0x00ff0000) >> 8) / bd) << 16;
                        *d2 = B | V | R
                              | (0xff000000 - (*((DWORD*)s2) & 0xff000000)) & 0xff000000;
                    }
                }
            }
            break;
        case MSP_RGB32:
        case MSP_AYUV:
            for (ptrdiff_t j = 0; j < h; j++, s += src.pitch, d += dst.pitch) {
                BYTE* s2 = s;
                BYTE* s2end = s2 + w * 4;
                DWORD* d2 = (DWORD*)d;
                for (; s2 < s2end; s2 += 4, d2++) {
#ifdef _WIN64
                    DWORD ia = 256 - s2[3];
                    if (s2[3] < 0xff) {
                        *d2 = ((((*d2 & 0x00ff00ff) * s2[3]) >> 8) + (((*((DWORD*)s2) & 0x00ff00ff) * ia) >> 8) & 0x00ff00ff)
                              | ((((*d2 & 0x0000ff00) * s2[3]) >> 8) + (((*((DWORD*)s2) & 0x0000ff00) * ia) >> 8) & 0x0000ff00);
                    }
#else
                    if (s2[3] < 0xff) {
                        *d2 = ((((*d2 & 0x00ff00ff) * s2[3]) >> 8) + (*((DWORD*)s2) & 0x00ff00ff) & 0x00ff00ff)
                              | ((((*d2 & 0x0000ff00) * s2[3]) >> 8) + (*((DWORD*)s2) & 0x0000ff00) & 0x0000ff00);
                    }
#endif
                }
            }
            break;
        case MSP_RGB24:
            for (ptrdiff_t j = 0; j < h; j++, s += src.pitch, d += dst.pitch) {
                BYTE* s2 = s;
                BYTE* s2end = s2 + w * 4;
                BYTE* d2 = d;
                for (; s2 < s2end; s2 += 4, d2 += 3) {
                    if (s2[3] < 0xff) {
                        d2[0] = ((d2[0] * s2[3]) >> 8) + s2[0];
                        d2[1] = ((d2[1] * s2[3]) >> 8) + s2[1];
                        d2[2] = ((d2[2] * s2[3]) >> 8) + s2[2];
                    }
                }
            }
            break;
        case MSP_RGB16:
            for (ptrdiff_t j = 0; j < h; j++, s += src.pitch, d += dst.pitch) {
                BYTE* s2 = s;
                BYTE* s2end = s2 + w * 4;
                WORD* d2 = (WORD*)d;
                for (; s2 < s2end; s2 += 4, d2++) {
                    if (s2[3] < 0x1f) {
                        *d2 = (WORD)((((((*d2 & 0xf81f) * s2[3]) >> 5) + (*(DWORD*)s2 & 0xf81f)) & 0xf81f)
                                     | (((((*d2 & 0x07e0) * s2[3]) >> 5) + (*(DWORD*)s2 & 0x07e0)) & 0x07e0));
                    }
                }
            }
            break;
        case MSP_RGB15:
            for (ptrdiff_t j = 0; j < h; j++, s += src.pitch, d += dst.pitch) {
                BYTE* s2 = s;
                BYTE* s2end = s2 + w * 4;
                WORD* d2 = (WORD*)d;
                for (; s2 < s2end; s2 += 4, d2++) {
                    if (s2[3] < 0x1f) {
                        *d2 = (WORD)((((((*d2 & 0x7c1f) * s2[3]) >> 5) + (*(DWORD*)s2 & 0x7c1f)) & 0x7c1f)
                                     | (((((*d2 & 0x03e0) * s2[3]) >> 5) + (*(DWORD*)s2 & 0x03e0)) & 0x03e0));
                    }
                }
            }
            break;
        case MSP_YUY2: {
#ifdef _WIN64
            auto alphablt_func = AlphaBlt_YUY2_SSE2;
#else
            auto alphablt_func = AlphaBlt_YUY2_MMX;
#endif
            //alphablt_func = AlphaBlt_YUY2_C;

            alphablt_func(w, h, d, dst.pitch, s, src.pitch);
        }
        break;
        case MSP_YV12:
        case MSP_IYUV:
            for (ptrdiff_t j = 0; j < h; j++, s += src.pitch, d += dst.pitch) {
                BYTE* s2 = s;
                BYTE* s2end = s2 + w * 4;
                BYTE* d2 = d;
                for (; s2 < s2end; s2 += 4, d2++) {
                    if (s2[3] < 0xff) {
                        d2[0] = (((d2[0] - 0x10) * s2[3]) >> 8) + s2[1];
                    }
                }
            }
            break;
        default:
            return E_NOTIMPL;
    }

    dst.pitch = abs(dst.pitch);

    if (dst.type == MSP_YV12 || dst.type == MSP_IYUV) {
        int h2 = h / 2;

        if (!dst.pitchUV) {
            dst.pitchUV = dst.pitch / 2;
        }

        BYTE* ss[2];
        ss[0] = src.bits + src.pitch * rs.top + rs.left * 4;
        ss[1] = ss[0] + 4;

        if (!dst.bitsU || !dst.bitsV) {
            dst.bitsU = dst.bits + dst.pitch * dst.h;
            dst.bitsV = dst.bitsU + dst.pitchUV * dst.h / 2;

            if (dst.type == MSP_YV12) {
                BYTE* p = dst.bitsU;
                dst.bitsU = dst.bitsV;
                dst.bitsV = p;
            }
        }

        BYTE* dd[2];
        dd[0] = dst.bitsU + dst.pitchUV * rd.top / 2 + rd.left / 2;
        dd[1] = dst.bitsV + dst.pitchUV * rd.top / 2 + rd.left / 2;

        if (rd.top > rd.bottom) {
            dd[0] = dst.bitsU + dst.pitchUV * (rd.top / 2 - 1) + rd.left / 2;
            dd[1] = dst.bitsV + dst.pitchUV * (rd.top / 2 - 1) + rd.left / 2;
            dst.pitchUV = -dst.pitchUV;
        }

        for (ptrdiff_t i = 0; i < 2; i++) {
            s = ss[i];
            d = dd[i];
            BYTE* is = ss[1 - i];
            for (ptrdiff_t j = 0; j < h2; j++, s += src.pitch * 2, d += dst.pitchUV, is += src.pitch * 2) {
                BYTE* s2 = s;
                BYTE* s2end = s2 + w * 4;
                BYTE* d2 = d;
                BYTE* is2 = is;
                for (; s2 < s2end; s2 += 8, d2++, is2 += 8) {
                    unsigned int ia = (s2[3] + s2[3 + src.pitch] + is2[3] + is2[3 + src.pitch]) >> 2;
                    if (ia < 0xff) {
                        *d2 = (((*d2 - 0x80) * ia) >> 8) + ((s2[0] + s2[src.pitch]) >> 1);
                    }
                }
            }
        }
    }

    return S_OK;
}

//
// CMemSubPicAllocator
//

CMemSubPicAllocator::CMemSubPicAllocator(int type, SIZE maxsize)
    : CSubPicAllocatorImpl(maxsize, false)
    , m_type(type)
    , m_maxsize(maxsize)
{
}

// ISubPicAllocatorImpl

bool CMemSubPicAllocator::Alloc(bool fStatic, ISubPic** ppSubPic)
{
    if (!ppSubPic) {
        return false;
    }

    SubPicDesc spd;
    spd.w = m_maxsize.cx;
    spd.h = m_maxsize.cy;
    spd.bpp = 32;
    spd.pitch = (spd.w * spd.bpp) >> 3;
    spd.type = m_type;
    try {
        spd.bits = DEBUG_NEW BYTE[spd.pitch * spd.h];
    } catch (CMemoryException* e) {
        e->Delete();
        return false;
    }

    try {
        *ppSubPic = DEBUG_NEW CMemSubPic(spd);
    } catch (CMemoryException* e) {
        e->Delete();
        delete [] spd.bits;
        return false;
    }

    (*ppSubPic)->AddRef();

    return true;
}