Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * AltiVec acceleration for colorspace conversion
  3.  *
  4.  * copyright (C) 2004 Marc Hoffman <marc.hoffman@analog.com>
  5.  *
  6.  * This file is part of FFmpeg.
  7.  *
  8.  * FFmpeg is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Lesser General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  * FFmpeg is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Lesser General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Lesser General Public
  19.  * License along with FFmpeg; if not, write to the Free Software
  20.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21.  */
  22.  
  23. /*
  24.  * Convert I420 YV12 to RGB in various formats,
  25.  * it rejects images that are not in 420 formats,
  26.  * it rejects images that don't have widths of multiples of 16,
  27.  * it rejects images that don't have heights of multiples of 2.
  28.  * Reject defers to C simulation code.
  29.  *
  30.  * Lots of optimizations to be done here.
  31.  *
  32.  * 1. Need to fix saturation code. I just couldn't get it to fly with packs
  33.  * and adds, so we currently use max/min to clip.
  34.  *
  35.  * 2. The inefficient use of chroma loading needs a bit of brushing up.
  36.  *
  37.  * 3. Analysis of pipeline stalls needs to be done. Use shark to identify
  38.  * pipeline stalls.
  39.  *
  40.  *
  41.  * MODIFIED to calculate coeffs from currently selected color space.
  42.  * MODIFIED core to be a macro where you specify the output format.
  43.  * ADDED UYVY conversion which is never called due to some thing in swscale.
  44.  * CORRECTED algorithim selection to be strict on input formats.
  45.  * ADDED runtime detection of AltiVec.
  46.  *
  47.  * ADDED altivec_yuv2packedX vertical scl + RGB converter
  48.  *
  49.  * March 27,2004
  50.  * PERFORMANCE ANALYSIS
  51.  *
  52.  * The C version uses 25% of the processor or ~250Mips for D1 video rawvideo
  53.  * used as test.
  54.  * The AltiVec version uses 10% of the processor or ~100Mips for D1 video
  55.  * same sequence.
  56.  *
  57.  * 720 * 480 * 30  ~10MPS
  58.  *
  59.  * so we have roughly 10 clocks per pixel. This is too high, something has
  60.  * to be wrong.
  61.  *
  62.  * OPTIMIZED clip codes to utilize vec_max and vec_packs removing the
  63.  * need for vec_min.
  64.  *
  65.  * OPTIMIZED DST OUTPUT cache/DMA controls. We are pretty much guaranteed to
  66.  * have the input video frame, it was just decompressed so it probably resides
  67.  * in L1 caches. However, we are creating the output video stream. This needs
  68.  * to use the DSTST instruction to optimize for the cache. We couple this with
  69.  * the fact that we are not going to be visiting the input buffer again so we
  70.  * mark it Least Recently Used. This shaves 25% of the processor cycles off.
  71.  *
  72.  * Now memcpy is the largest mips consumer in the system, probably due
  73.  * to the inefficient X11 stuff.
  74.  *
  75.  * GL libraries seem to be very slow on this machine 1.33Ghz PB running
  76.  * Jaguar, this is not the case for my 1Ghz PB.  I thought it might be
  77.  * a versioning issue, however I have libGL.1.2.dylib for both
  78.  * machines. (We need to figure this out now.)
  79.  *
  80.  * GL2 libraries work now with patch for RGB32.
  81.  *
  82.  * NOTE: quartz vo driver ARGB32_to_RGB24 consumes 30% of the processor.
  83.  *
  84.  * Integrated luma prescaling adjustment for saturation/contrast/brightness
  85.  * adjustment.
  86.  */
  87.  
  88. #include <stdio.h>
  89. #include <stdlib.h>
  90. #include <string.h>
  91. #include <inttypes.h>
  92. #include <assert.h>
  93.  
  94. #include "config.h"
  95. #include "libswscale/rgb2rgb.h"
  96. #include "libswscale/swscale.h"
  97. #include "libswscale/swscale_internal.h"
  98. #include "libavutil/attributes.h"
  99. #include "libavutil/cpu.h"
  100. #include "libavutil/pixdesc.h"
  101. #include "yuv2rgb_altivec.h"
  102.  
  103. #if HAVE_ALTIVEC
  104.  
  105. #undef PROFILE_THE_BEAST
  106. #undef INC_SCALING
  107.  
  108. typedef unsigned char ubyte;
  109. typedef signed char   sbyte;
  110.  
  111. /* RGB interleaver, 16 planar pels 8-bit samples per channel in
  112.  * homogeneous vector registers x0,x1,x2 are interleaved with the
  113.  * following technique:
  114.  *
  115.  *    o0 = vec_mergeh(x0, x1);
  116.  *    o1 = vec_perm(o0, x2, perm_rgb_0);
  117.  *    o2 = vec_perm(o0, x2, perm_rgb_1);
  118.  *    o3 = vec_mergel(x0, x1);
  119.  *    o4 = vec_perm(o3, o2, perm_rgb_2);
  120.  *    o5 = vec_perm(o3, o2, perm_rgb_3);
  121.  *
  122.  * perm_rgb_0:   o0(RG).h v1(B) --> o1*
  123.  *            0   1  2   3   4
  124.  *           rgbr|gbrg|brgb|rgbr
  125.  *           0010 0100 1001 0010
  126.  *           0102 3145 2673 894A
  127.  *
  128.  * perm_rgb_1:   o0(RG).h v1(B) --> o2
  129.  *            0   1  2   3   4
  130.  *           gbrg|brgb|bbbb|bbbb
  131.  *           0100 1001 1111 1111
  132.  *           B5CD 6EF7 89AB CDEF
  133.  *
  134.  * perm_rgb_2:   o3(RG).l o2(rgbB.l) --> o4*
  135.  *            0   1  2   3   4
  136.  *           gbrg|brgb|rgbr|gbrg
  137.  *           1111 1111 0010 0100
  138.  *           89AB CDEF 0182 3945
  139.  *
  140.  * perm_rgb_2:   o3(RG).l o2(rgbB.l) ---> o5*
  141.  *            0   1  2   3   4
  142.  *           brgb|rgbr|gbrg|brgb
  143.  *           1001 0010 0100 1001
  144.  *           a67b 89cA BdCD eEFf
  145.  *
  146.  */
  147. static const vector unsigned char
  148.     perm_rgb_0 = { 0x00, 0x01, 0x10, 0x02, 0x03, 0x11, 0x04, 0x05,
  149.                    0x12, 0x06, 0x07, 0x13, 0x08, 0x09, 0x14, 0x0a },
  150.     perm_rgb_1 = { 0x0b, 0x15, 0x0c, 0x0d, 0x16, 0x0e, 0x0f, 0x17,
  151.                    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
  152.     perm_rgb_2 = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  153.                    0x00, 0x01, 0x18, 0x02, 0x03, 0x19, 0x04, 0x05 },
  154.     perm_rgb_3 = { 0x1a, 0x06, 0x07, 0x1b, 0x08, 0x09, 0x1c, 0x0a,
  155.                    0x0b, 0x1d, 0x0c, 0x0d, 0x1e, 0x0e, 0x0f, 0x1f };
  156.  
  157. #define vec_merge3(x2, x1, x0, y0, y1, y2)     \
  158.     do {                                       \
  159.         __typeof__(x0) o0, o2, o3;             \
  160.         o0 = vec_mergeh(x0, x1);               \
  161.         y0 = vec_perm(o0, x2, perm_rgb_0);     \
  162.         o2 = vec_perm(o0, x2, perm_rgb_1);     \
  163.         o3 = vec_mergel(x0, x1);               \
  164.         y1 = vec_perm(o3, o2, perm_rgb_2);     \
  165.         y2 = vec_perm(o3, o2, perm_rgb_3);     \
  166.     } while (0)
  167.  
  168. #define vec_mstbgr24(x0, x1, x2, ptr)          \
  169.     do {                                       \
  170.         __typeof__(x0) _0, _1, _2;             \
  171.         vec_merge3(x0, x1, x2, _0, _1, _2);    \
  172.         vec_st(_0, 0, ptr++);                  \
  173.         vec_st(_1, 0, ptr++);                  \
  174.         vec_st(_2, 0, ptr++);                  \
  175.     } while (0)
  176.  
  177. #define vec_mstrgb24(x0, x1, x2, ptr)          \
  178.     do {                                       \
  179.         __typeof__(x0) _0, _1, _2;             \
  180.         vec_merge3(x2, x1, x0, _0, _1, _2);    \
  181.         vec_st(_0, 0, ptr++);                  \
  182.         vec_st(_1, 0, ptr++);                  \
  183.         vec_st(_2, 0, ptr++);                  \
  184.     } while (0)
  185.  
  186. /* pack the pixels in rgb0 format
  187.  * msb R
  188.  * lsb 0
  189.  */
  190. #define vec_mstrgb32(T, x0, x1, x2, x3, ptr)                            \
  191.     do {                                                                \
  192.         T _0, _1, _2, _3;                                               \
  193.         _0 = vec_mergeh(x0, x1);                                        \
  194.         _1 = vec_mergeh(x2, x3);                                        \
  195.         _2 = (T) vec_mergeh((vector unsigned short) _0,                 \
  196.                             (vector unsigned short) _1);                \
  197.         _3 = (T) vec_mergel((vector unsigned short) _0,                 \
  198.                             (vector unsigned short) _1);                \
  199.         vec_st(_2, 0 * 16, (T *) ptr);                                  \
  200.         vec_st(_3, 1 * 16, (T *) ptr);                                  \
  201.         _0 = vec_mergel(x0, x1);                                        \
  202.         _1 = vec_mergel(x2, x3);                                        \
  203.         _2 = (T) vec_mergeh((vector unsigned short) _0,                 \
  204.                             (vector unsigned short) _1);                \
  205.         _3 = (T) vec_mergel((vector unsigned short) _0,                 \
  206.                             (vector unsigned short) _1);                \
  207.         vec_st(_2, 2 * 16, (T *) ptr);                                  \
  208.         vec_st(_3, 3 * 16, (T *) ptr);                                  \
  209.         ptr += 4;                                                       \
  210.     } while (0)
  211.  
  212. /*
  213.  * 1     0       1.4021   | | Y |
  214.  * 1    -0.3441 -0.7142   |x| Cb|
  215.  * 1     1.7718  0        | | Cr|
  216.  *
  217.  *
  218.  * Y:      [-128 127]
  219.  * Cb/Cr : [-128 127]
  220.  *
  221.  * typical YUV conversion works on Y: 0-255 this version has been
  222.  * optimized for JPEG decoding.
  223.  */
  224.  
  225. #if HAVE_BIGENDIAN
  226. #define vec_unh(x)                                                      \
  227.     (vector signed short)                                               \
  228.         vec_perm(x, (__typeof__(x)) { 0 },                              \
  229.                  ((vector unsigned char) {                              \
  230.                      0x10, 0x00, 0x10, 0x01, 0x10, 0x02, 0x10, 0x03,    \
  231.                      0x10, 0x04, 0x10, 0x05, 0x10, 0x06, 0x10, 0x07 }))
  232.  
  233. #define vec_unl(x)                                                      \
  234.     (vector signed short)                                               \
  235.         vec_perm(x, (__typeof__(x)) { 0 },                              \
  236.                  ((vector unsigned char) {                              \
  237.                      0x10, 0x08, 0x10, 0x09, 0x10, 0x0A, 0x10, 0x0B,    \
  238.                      0x10, 0x0C, 0x10, 0x0D, 0x10, 0x0E, 0x10, 0x0F }))
  239. #else
  240. #define vec_unh(x)(vector signed short) vec_mergeh(x,(__typeof__(x)) { 0 })
  241. #define vec_unl(x)(vector signed short) vec_mergel(x,(__typeof__(x)) { 0 })
  242. #endif
  243.  
  244. #define vec_clip_s16(x)                                                 \
  245.     vec_max(vec_min(x, ((vector signed short) {                         \
  246.                     235, 235, 235, 235, 235, 235, 235, 235 })),         \
  247.             ((vector signed short) { 16, 16, 16, 16, 16, 16, 16, 16 }))
  248.  
  249. #define vec_packclp(x, y)                                               \
  250.     (vector unsigned char)                                              \
  251.         vec_packs((vector unsigned short)                               \
  252.                       vec_max(x, ((vector signed short) { 0 })),        \
  253.                   (vector unsigned short)                               \
  254.                       vec_max(y, ((vector signed short) { 0 })))
  255.  
  256. static inline void cvtyuvtoRGB(SwsContext *c, vector signed short Y,
  257.                                vector signed short U, vector signed short V,
  258.                                vector signed short *R, vector signed short *G,
  259.                                vector signed short *B)
  260. {
  261.     vector signed short vx, ux, uvx;
  262.  
  263.     Y = vec_mradds(Y, c->CY, c->OY);
  264.     U = vec_sub(U, (vector signed short)
  265.                        vec_splat((vector signed short) { 128 }, 0));
  266.     V = vec_sub(V, (vector signed short)
  267.                        vec_splat((vector signed short) { 128 }, 0));
  268.  
  269.     // ux  = (CBU * (u << c->CSHIFT) + 0x4000) >> 15;
  270.     ux = vec_sl(U, c->CSHIFT);
  271.     *B = vec_mradds(ux, c->CBU, Y);
  272.  
  273.     // vx  = (CRV * (v << c->CSHIFT) + 0x4000) >> 15;
  274.     vx = vec_sl(V, c->CSHIFT);
  275.     *R = vec_mradds(vx, c->CRV, Y);
  276.  
  277.     // uvx = ((CGU * u) + (CGV * v)) >> 15;
  278.     uvx = vec_mradds(U, c->CGU, Y);
  279.     *G  = vec_mradds(V, c->CGV, uvx);
  280. }
  281.  
  282. /*
  283.  * ------------------------------------------------------------------------------
  284.  * CS converters
  285.  * ------------------------------------------------------------------------------
  286.  */
  287.  
  288. #define DEFCSP420_CVT(name, out_pixels)                                       \
  289. static int altivec_ ## name(SwsContext *c, const unsigned char **in,          \
  290.                             int *instrides, int srcSliceY, int srcSliceH,     \
  291.                             unsigned char **oplanes, int *outstrides)         \
  292. {                                                                             \
  293.     int w = c->srcW;                                                          \
  294.     int h = srcSliceH;                                                        \
  295.     int i, j;                                                                 \
  296.     int instrides_scl[3];                                                     \
  297.     vector unsigned char y0, y1;                                              \
  298.                                                                               \
  299.     vector signed char u, v;                                                  \
  300.                                                                               \
  301.     vector signed short Y0, Y1, Y2, Y3;                                       \
  302.     vector signed short U, V;                                                 \
  303.     vector signed short vx, ux, uvx;                                          \
  304.     vector signed short vx0, ux0, uvx0;                                       \
  305.     vector signed short vx1, ux1, uvx1;                                       \
  306.     vector signed short R0, G0, B0;                                           \
  307.     vector signed short R1, G1, B1;                                           \
  308.     vector unsigned char R, G, B;                                             \
  309.                                                                               \
  310.     const vector unsigned char *y1ivP, *y2ivP, *uivP, *vivP;                  \
  311.     vector unsigned char align_perm;                                          \
  312.                                                                               \
  313.     vector signed short lCY       = c->CY;                                    \
  314.     vector signed short lOY       = c->OY;                                    \
  315.     vector signed short lCRV      = c->CRV;                                   \
  316.     vector signed short lCBU      = c->CBU;                                   \
  317.     vector signed short lCGU      = c->CGU;                                   \
  318.     vector signed short lCGV      = c->CGV;                                   \
  319.     vector unsigned short lCSHIFT = c->CSHIFT;                                \
  320.                                                                               \
  321.     const ubyte *y1i = in[0];                                                 \
  322.     const ubyte *y2i = in[0] + instrides[0];                                  \
  323.     const ubyte *ui  = in[1];                                                 \
  324.     const ubyte *vi  = in[2];                                                 \
  325.                                                                               \
  326.     vector unsigned char *oute, *outo;                                        \
  327.                                                                               \
  328.     /* loop moves y{1, 2}i by w */                                            \
  329.     instrides_scl[0] = instrides[0] * 2 - w;                                  \
  330.     /* loop moves ui by w / 2 */                                              \
  331.     instrides_scl[1] = instrides[1] - w / 2;                                  \
  332.     /* loop moves vi by w / 2 */                                              \
  333.     instrides_scl[2] = instrides[2] - w / 2;                                  \
  334.                                                                               \
  335.     for (i = 0; i < h / 2; i++) {                                             \
  336.         oute = (vector unsigned char *)(oplanes[0] + outstrides[0] *          \
  337.                                         (srcSliceY + i * 2));                 \
  338.         outo = oute + (outstrides[0] >> 4);                                   \
  339.         vec_dstst(outo, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 0);       \
  340.         vec_dstst(oute, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 1);       \
  341.                                                                               \
  342.         for (j = 0; j < w / 16; j++) {                                        \
  343.             y1ivP = (const vector unsigned char *) y1i;                       \
  344.             y2ivP = (const vector unsigned char *) y2i;                       \
  345.             uivP  = (const vector unsigned char *) ui;                        \
  346.             vivP  = (const vector unsigned char *) vi;                        \
  347.                                                                               \
  348.             align_perm = vec_lvsl(0, y1i);                                    \
  349.             y0 = (vector unsigned char)                                       \
  350.                      vec_perm(y1ivP[0], y1ivP[1], align_perm);                \
  351.                                                                               \
  352.             align_perm = vec_lvsl(0, y2i);                                    \
  353.             y1 = (vector unsigned char)                                       \
  354.                      vec_perm(y2ivP[0], y2ivP[1], align_perm);                \
  355.                                                                               \
  356.             align_perm = vec_lvsl(0, ui);                                     \
  357.             u = (vector signed char)                                          \
  358.                     vec_perm(uivP[0], uivP[1], align_perm);                   \
  359.                                                                               \
  360.             align_perm = vec_lvsl(0, vi);                                     \
  361.             v = (vector signed char)                                          \
  362.                     vec_perm(vivP[0], vivP[1], align_perm);                   \
  363.                                                                               \
  364.             u = (vector signed char)                                          \
  365.                     vec_sub(u,                                                \
  366.                             (vector signed char)                              \
  367.                                 vec_splat((vector signed char) { 128 }, 0));  \
  368.             v = (vector signed char)                                          \
  369.                     vec_sub(v,                                                \
  370.                             (vector signed char)                              \
  371.                                 vec_splat((vector signed char) { 128 }, 0));  \
  372.                                                                               \
  373.             U = vec_unpackh(u);                                               \
  374.             V = vec_unpackh(v);                                               \
  375.                                                                               \
  376.             Y0 = vec_unh(y0);                                                 \
  377.             Y1 = vec_unl(y0);                                                 \
  378.             Y2 = vec_unh(y1);                                                 \
  379.             Y3 = vec_unl(y1);                                                 \
  380.                                                                               \
  381.             Y0 = vec_mradds(Y0, lCY, lOY);                                    \
  382.             Y1 = vec_mradds(Y1, lCY, lOY);                                    \
  383.             Y2 = vec_mradds(Y2, lCY, lOY);                                    \
  384.             Y3 = vec_mradds(Y3, lCY, lOY);                                    \
  385.                                                                               \
  386.             /* ux  = (CBU * (u << CSHIFT) + 0x4000) >> 15 */                  \
  387.             ux  = vec_sl(U, lCSHIFT);                                         \
  388.             ux  = vec_mradds(ux, lCBU, (vector signed short) { 0 });          \
  389.             ux0 = vec_mergeh(ux, ux);                                         \
  390.             ux1 = vec_mergel(ux, ux);                                         \
  391.                                                                               \
  392.             /* vx  = (CRV * (v << CSHIFT) + 0x4000) >> 15; */                 \
  393.             vx  = vec_sl(V, lCSHIFT);                                         \
  394.             vx  = vec_mradds(vx, lCRV, (vector signed short) { 0 });          \
  395.             vx0 = vec_mergeh(vx, vx);                                         \
  396.             vx1 = vec_mergel(vx, vx);                                         \
  397.                                                                               \
  398.             /* uvx = ((CGU * u) + (CGV * v)) >> 15 */                         \
  399.             uvx  = vec_mradds(U, lCGU, (vector signed short) { 0 });          \
  400.             uvx  = vec_mradds(V, lCGV, uvx);                                  \
  401.             uvx0 = vec_mergeh(uvx, uvx);                                      \
  402.             uvx1 = vec_mergel(uvx, uvx);                                      \
  403.                                                                               \
  404.             R0 = vec_add(Y0, vx0);                                            \
  405.             G0 = vec_add(Y0, uvx0);                                           \
  406.             B0 = vec_add(Y0, ux0);                                            \
  407.             R1 = vec_add(Y1, vx1);                                            \
  408.             G1 = vec_add(Y1, uvx1);                                           \
  409.             B1 = vec_add(Y1, ux1);                                            \
  410.                                                                               \
  411.             R = vec_packclp(R0, R1);                                          \
  412.             G = vec_packclp(G0, G1);                                          \
  413.             B = vec_packclp(B0, B1);                                          \
  414.                                                                               \
  415.             out_pixels(R, G, B, oute);                                        \
  416.                                                                               \
  417.             R0 = vec_add(Y2, vx0);                                            \
  418.             G0 = vec_add(Y2, uvx0);                                           \
  419.             B0 = vec_add(Y2, ux0);                                            \
  420.             R1 = vec_add(Y3, vx1);                                            \
  421.             G1 = vec_add(Y3, uvx1);                                           \
  422.             B1 = vec_add(Y3, ux1);                                            \
  423.             R  = vec_packclp(R0, R1);                                         \
  424.             G  = vec_packclp(G0, G1);                                         \
  425.             B  = vec_packclp(B0, B1);                                         \
  426.                                                                               \
  427.                                                                               \
  428.             out_pixels(R, G, B, outo);                                        \
  429.                                                                               \
  430.             y1i += 16;                                                        \
  431.             y2i += 16;                                                        \
  432.             ui  += 8;                                                         \
  433.             vi  += 8;                                                         \
  434.         }                                                                     \
  435.                                                                               \
  436.         ui  += instrides_scl[1];                                              \
  437.         vi  += instrides_scl[2];                                              \
  438.         y1i += instrides_scl[0];                                              \
  439.         y2i += instrides_scl[0];                                              \
  440.     }                                                                         \
  441.     return srcSliceH;                                                         \
  442. }
  443.  
  444. #define out_abgr(a, b, c, ptr)                                          \
  445.     vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), c, b, a, ptr)
  446. #define out_bgra(a, b, c, ptr)                                          \
  447.     vec_mstrgb32(__typeof__(a), c, b, a, ((__typeof__(a)) { 255 }), ptr)
  448. #define out_rgba(a, b, c, ptr)                                          \
  449.     vec_mstrgb32(__typeof__(a), a, b, c, ((__typeof__(a)) { 255 }), ptr)
  450. #define out_argb(a, b, c, ptr)                                          \
  451.     vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), a, b, c, ptr)
  452. #define out_rgb24(a, b, c, ptr) vec_mstrgb24(a, b, c, ptr)
  453. #define out_bgr24(a, b, c, ptr) vec_mstbgr24(a, b, c, ptr)
  454.  
  455. DEFCSP420_CVT(yuv2_abgr,  out_abgr)
  456. DEFCSP420_CVT(yuv2_bgra,  out_bgra)
  457. DEFCSP420_CVT(yuv2_rgba,  out_rgba)
  458. DEFCSP420_CVT(yuv2_argb,  out_argb)
  459. DEFCSP420_CVT(yuv2_rgb24, out_rgb24)
  460. DEFCSP420_CVT(yuv2_bgr24, out_bgr24)
  461.  
  462. // uyvy|uyvy|uyvy|uyvy
  463. // 0123 4567 89ab cdef
  464. static const vector unsigned char
  465.     demux_u = { 0x10, 0x00, 0x10, 0x00,
  466.                 0x10, 0x04, 0x10, 0x04,
  467.                 0x10, 0x08, 0x10, 0x08,
  468.                 0x10, 0x0c, 0x10, 0x0c },
  469.     demux_v = { 0x10, 0x02, 0x10, 0x02,
  470.                 0x10, 0x06, 0x10, 0x06,
  471.                 0x10, 0x0A, 0x10, 0x0A,
  472.                 0x10, 0x0E, 0x10, 0x0E },
  473.     demux_y = { 0x10, 0x01, 0x10, 0x03,
  474.                 0x10, 0x05, 0x10, 0x07,
  475.                 0x10, 0x09, 0x10, 0x0B,
  476.                 0x10, 0x0D, 0x10, 0x0F };
  477.  
  478. /*
  479.  * this is so I can play live CCIR raw video
  480.  */
  481. static int altivec_uyvy_rgb32(SwsContext *c, const unsigned char **in,
  482.                               int *instrides, int srcSliceY, int srcSliceH,
  483.                               unsigned char **oplanes, int *outstrides)
  484. {
  485.     int w = c->srcW;
  486.     int h = srcSliceH;
  487.     int i, j;
  488.     vector unsigned char uyvy;
  489.     vector signed short Y, U, V;
  490.     vector signed short R0, G0, B0, R1, G1, B1;
  491.     vector unsigned char R, G, B;
  492.     vector unsigned char *out;
  493.     const ubyte *img;
  494.  
  495.     img = in[0];
  496.     out = (vector unsigned char *) (oplanes[0] + srcSliceY * outstrides[0]);
  497.  
  498.     for (i = 0; i < h; i++)
  499.         for (j = 0; j < w / 16; j++) {
  500.             uyvy = vec_ld(0, img);
  501.  
  502.             U = (vector signed short)
  503.                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
  504.             V = (vector signed short)
  505.                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
  506.             Y = (vector signed short)
  507.                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
  508.  
  509.             cvtyuvtoRGB(c, Y, U, V, &R0, &G0, &B0);
  510.  
  511.             uyvy = vec_ld(16, img);
  512.  
  513.             U = (vector signed short)
  514.                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
  515.             V = (vector signed short)
  516.                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
  517.             Y = (vector signed short)
  518.                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
  519.  
  520.             cvtyuvtoRGB(c, Y, U, V, &R1, &G1, &B1);
  521.  
  522.             R = vec_packclp(R0, R1);
  523.             G = vec_packclp(G0, G1);
  524.             B = vec_packclp(B0, B1);
  525.  
  526.             // vec_mstbgr24 (R,G,B, out);
  527.             out_rgba(R, G, B, out);
  528.  
  529.             img += 32;
  530.         }
  531.     return srcSliceH;
  532. }
  533.  
  534. #endif /* HAVE_ALTIVEC */
  535.  
  536. /* Ok currently the acceleration routine only supports
  537.  * inputs of widths a multiple of 16
  538.  * and heights a multiple 2
  539.  *
  540.  * So we just fall back to the C codes for this.
  541.  */
  542. av_cold SwsFunc ff_yuv2rgb_init_ppc(SwsContext *c)
  543. {
  544. #if HAVE_ALTIVEC
  545.     if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  546.         return NULL;
  547.  
  548.     /*
  549.      * and this seems not to matter too much I tried a bunch of
  550.      * videos with abnormal widths and MPlayer crashes elsewhere.
  551.      * mplayer -vo x11 -rawvideo on:w=350:h=240 raw-350x240.eyuv
  552.      * boom with X11 bad match.
  553.      *
  554.      */
  555.     if ((c->srcW & 0xf) != 0)
  556.         return NULL;
  557.  
  558.     switch (c->srcFormat) {
  559.     case AV_PIX_FMT_YUV410P:
  560.     case AV_PIX_FMT_YUV420P:
  561.     /*case IMGFMT_CLPL:        ??? */
  562.     case AV_PIX_FMT_GRAY8:
  563.     case AV_PIX_FMT_NV12:
  564.     case AV_PIX_FMT_NV21:
  565.         if ((c->srcH & 0x1) != 0)
  566.             return NULL;
  567.  
  568.         switch (c->dstFormat) {
  569.         case AV_PIX_FMT_RGB24:
  570.             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGB24\n");
  571.             return altivec_yuv2_rgb24;
  572.         case AV_PIX_FMT_BGR24:
  573.             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGR24\n");
  574.             return altivec_yuv2_bgr24;
  575.         case AV_PIX_FMT_ARGB:
  576.             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ARGB\n");
  577.             return altivec_yuv2_argb;
  578.         case AV_PIX_FMT_ABGR:
  579.             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ABGR\n");
  580.             return altivec_yuv2_abgr;
  581.         case AV_PIX_FMT_RGBA:
  582.             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGBA\n");
  583.             return altivec_yuv2_rgba;
  584.         case AV_PIX_FMT_BGRA:
  585.             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGRA\n");
  586.             return altivec_yuv2_bgra;
  587.         default: return NULL;
  588.         }
  589.         break;
  590.  
  591.     case AV_PIX_FMT_UYVY422:
  592.         switch (c->dstFormat) {
  593.         case AV_PIX_FMT_BGR32:
  594.             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space UYVY -> RGB32\n");
  595.             return altivec_uyvy_rgb32;
  596.         default: return NULL;
  597.         }
  598.         break;
  599.     }
  600. #endif /* HAVE_ALTIVEC */
  601.  
  602.     return NULL;
  603. }
  604.  
  605. av_cold void ff_yuv2rgb_init_tables_ppc(SwsContext *c,
  606.                                         const int inv_table[4],
  607.                                         int brightness,
  608.                                         int contrast,
  609.                                         int saturation)
  610. {
  611. #if HAVE_ALTIVEC
  612.     union {
  613.         DECLARE_ALIGNED(16, signed short, tmp)[8];
  614.         vector signed short vec;
  615.     } buf;
  616.  
  617.     if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
  618.         return;
  619.  
  620.     buf.tmp[0] = ((0xffffLL) * contrast >> 8) >> 9;                               // cy
  621.     buf.tmp[1] = -256 * brightness;                                               // oy
  622.     buf.tmp[2] =   (inv_table[0] >> 3) * (contrast >> 16) * (saturation >> 16);   // crv
  623.     buf.tmp[3] =   (inv_table[1] >> 3) * (contrast >> 16) * (saturation >> 16);   // cbu
  624.     buf.tmp[4] = -((inv_table[2] >> 1) * (contrast >> 16) * (saturation >> 16));  // cgu
  625.     buf.tmp[5] = -((inv_table[3] >> 1) * (contrast >> 16) * (saturation >> 16));  // cgv
  626.  
  627.     c->CSHIFT = (vector unsigned short) vec_splat_u16(2);
  628.     c->CY     = vec_splat((vector signed short) buf.vec, 0);
  629.     c->OY     = vec_splat((vector signed short) buf.vec, 1);
  630.     c->CRV    = vec_splat((vector signed short) buf.vec, 2);
  631.     c->CBU    = vec_splat((vector signed short) buf.vec, 3);
  632.     c->CGU    = vec_splat((vector signed short) buf.vec, 4);
  633.     c->CGV    = vec_splat((vector signed short) buf.vec, 5);
  634.     return;
  635. #endif /* HAVE_ALTIVEC */
  636. }
  637.  
  638. #if HAVE_ALTIVEC
  639.  
  640. static av_always_inline void yuv2packedX_altivec(SwsContext *c,
  641.                                                  const int16_t *lumFilter,
  642.                                                  const int16_t **lumSrc,
  643.                                                  int lumFilterSize,
  644.                                                  const int16_t *chrFilter,
  645.                                                  const int16_t **chrUSrc,
  646.                                                  const int16_t **chrVSrc,
  647.                                                  int chrFilterSize,
  648.                                                  const int16_t **alpSrc,
  649.                                                  uint8_t *dest,
  650.                                                  int dstW, int dstY,
  651.                                                  enum AVPixelFormat target)
  652. {
  653.     int i, j;
  654.     vector signed short X, X0, X1, Y0, U0, V0, Y1, U1, V1, U, V;
  655.     vector signed short R0, G0, B0, R1, G1, B1;
  656.  
  657.     vector unsigned char R, G, B;
  658.     vector unsigned char *out, *nout;
  659.  
  660.     vector signed short RND   = vec_splat_s16(1 << 3);
  661.     vector unsigned short SCL = vec_splat_u16(4);
  662.     DECLARE_ALIGNED(16, unsigned int, scratch)[16];
  663.  
  664.     vector signed short *YCoeffs, *CCoeffs;
  665.  
  666.     YCoeffs = c->vYCoeffsBank + dstY * lumFilterSize;
  667.     CCoeffs = c->vCCoeffsBank + dstY * chrFilterSize;
  668.  
  669.     out = (vector unsigned char *) dest;
  670.  
  671.     for (i = 0; i < dstW; i += 16) {
  672.         Y0 = RND;
  673.         Y1 = RND;
  674.         /* extract 16 coeffs from lumSrc */
  675.         for (j = 0; j < lumFilterSize; j++) {
  676.             X0 = vec_ld(0, &lumSrc[j][i]);
  677.             X1 = vec_ld(16, &lumSrc[j][i]);
  678.             Y0 = vec_mradds(X0, YCoeffs[j], Y0);
  679.             Y1 = vec_mradds(X1, YCoeffs[j], Y1);
  680.         }
  681.  
  682.         U = RND;
  683.         V = RND;
  684.         /* extract 8 coeffs from U,V */
  685.         for (j = 0; j < chrFilterSize; j++) {
  686.             X = vec_ld(0, &chrUSrc[j][i / 2]);
  687.             U = vec_mradds(X, CCoeffs[j], U);
  688.             X = vec_ld(0, &chrVSrc[j][i / 2]);
  689.             V = vec_mradds(X, CCoeffs[j], V);
  690.         }
  691.  
  692.         /* scale and clip signals */
  693.         Y0 = vec_sra(Y0, SCL);
  694.         Y1 = vec_sra(Y1, SCL);
  695.         U  = vec_sra(U, SCL);
  696.         V  = vec_sra(V, SCL);
  697.  
  698.         Y0 = vec_clip_s16(Y0);
  699.         Y1 = vec_clip_s16(Y1);
  700.         U  = vec_clip_s16(U);
  701.         V  = vec_clip_s16(V);
  702.  
  703.         /* now we have
  704.          * Y0 = y0 y1 y2 y3 y4 y5 y6 y7    Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  705.          * U  = u0 u1 u2 u3 u4 u5 u6 u7    V  = v0 v1 v2 v3 v4 v5 v6 v7
  706.          *
  707.          * Y0 = y0 y1 y2 y3 y4 y5 y6 y7    Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  708.          * U0 = u0 u0 u1 u1 u2 u2 u3 u3    U1 = u4 u4 u5 u5 u6 u6 u7 u7
  709.          * V0 = v0 v0 v1 v1 v2 v2 v3 v3    V1 = v4 v4 v5 v5 v6 v6 v7 v7
  710.          */
  711.  
  712.         U0 = vec_mergeh(U, U);
  713.         V0 = vec_mergeh(V, V);
  714.  
  715.         U1 = vec_mergel(U, U);
  716.         V1 = vec_mergel(V, V);
  717.  
  718.         cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
  719.         cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
  720.  
  721.         R = vec_packclp(R0, R1);
  722.         G = vec_packclp(G0, G1);
  723.         B = vec_packclp(B0, B1);
  724.  
  725.         switch (target) {
  726.         case AV_PIX_FMT_ABGR:
  727.             out_abgr(R, G, B, out);
  728.             break;
  729.         case AV_PIX_FMT_BGRA:
  730.             out_bgra(R, G, B, out);
  731.             break;
  732.         case AV_PIX_FMT_RGBA:
  733.             out_rgba(R, G, B, out);
  734.             break;
  735.         case AV_PIX_FMT_ARGB:
  736.             out_argb(R, G, B, out);
  737.             break;
  738.         case AV_PIX_FMT_RGB24:
  739.             out_rgb24(R, G, B, out);
  740.             break;
  741.         case AV_PIX_FMT_BGR24:
  742.             out_bgr24(R, G, B, out);
  743.             break;
  744.         default:
  745.         {
  746.             /* If this is reached, the caller should have called yuv2packedXinC
  747.              * instead. */
  748.             static int printed_error_message;
  749.             if (!printed_error_message) {
  750.                 av_log(c, AV_LOG_ERROR,
  751.                        "altivec_yuv2packedX doesn't support %s output\n",
  752.                        av_get_pix_fmt_name(c->dstFormat));
  753.                 printed_error_message = 1;
  754.             }
  755.             return;
  756.         }
  757.         }
  758.     }
  759.  
  760.     if (i < dstW) {
  761.         i -= 16;
  762.  
  763.         Y0 = RND;
  764.         Y1 = RND;
  765.         /* extract 16 coeffs from lumSrc */
  766.         for (j = 0; j < lumFilterSize; j++) {
  767.             X0 = vec_ld(0, &lumSrc[j][i]);
  768.             X1 = vec_ld(16, &lumSrc[j][i]);
  769.             Y0 = vec_mradds(X0, YCoeffs[j], Y0);
  770.             Y1 = vec_mradds(X1, YCoeffs[j], Y1);
  771.         }
  772.  
  773.         U = RND;
  774.         V = RND;
  775.         /* extract 8 coeffs from U,V */
  776.         for (j = 0; j < chrFilterSize; j++) {
  777.             X = vec_ld(0, &chrUSrc[j][i / 2]);
  778.             U = vec_mradds(X, CCoeffs[j], U);
  779.             X = vec_ld(0, &chrVSrc[j][i / 2]);
  780.             V = vec_mradds(X, CCoeffs[j], V);
  781.         }
  782.  
  783.         /* scale and clip signals */
  784.         Y0 = vec_sra(Y0, SCL);
  785.         Y1 = vec_sra(Y1, SCL);
  786.         U  = vec_sra(U, SCL);
  787.         V  = vec_sra(V, SCL);
  788.  
  789.         Y0 = vec_clip_s16(Y0);
  790.         Y1 = vec_clip_s16(Y1);
  791.         U  = vec_clip_s16(U);
  792.         V  = vec_clip_s16(V);
  793.  
  794.         /* now we have
  795.          * Y0 = y0 y1 y2 y3 y4 y5 y6 y7    Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  796.          * U  = u0 u1 u2 u3 u4 u5 u6 u7    V  = v0 v1 v2 v3 v4 v5 v6 v7
  797.          *
  798.          * Y0 = y0 y1 y2 y3 y4 y5 y6 y7    Y1 = y8 y9 y10 y11 y12 y13 y14 y15
  799.          * U0 = u0 u0 u1 u1 u2 u2 u3 u3    U1 = u4 u4 u5 u5 u6 u6 u7 u7
  800.          * V0 = v0 v0 v1 v1 v2 v2 v3 v3    V1 = v4 v4 v5 v5 v6 v6 v7 v7
  801.          */
  802.  
  803.         U0 = vec_mergeh(U, U);
  804.         V0 = vec_mergeh(V, V);
  805.  
  806.         U1 = vec_mergel(U, U);
  807.         V1 = vec_mergel(V, V);
  808.  
  809.         cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
  810.         cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
  811.  
  812.         R = vec_packclp(R0, R1);
  813.         G = vec_packclp(G0, G1);
  814.         B = vec_packclp(B0, B1);
  815.  
  816.         nout = (vector unsigned char *) scratch;
  817.         switch (target) {
  818.         case AV_PIX_FMT_ABGR:
  819.             out_abgr(R, G, B, nout);
  820.             break;
  821.         case AV_PIX_FMT_BGRA:
  822.             out_bgra(R, G, B, nout);
  823.             break;
  824.         case AV_PIX_FMT_RGBA:
  825.             out_rgba(R, G, B, nout);
  826.             break;
  827.         case AV_PIX_FMT_ARGB:
  828.             out_argb(R, G, B, nout);
  829.             break;
  830.         case AV_PIX_FMT_RGB24:
  831.             out_rgb24(R, G, B, nout);
  832.             break;
  833.         case AV_PIX_FMT_BGR24:
  834.             out_bgr24(R, G, B, nout);
  835.             break;
  836.         default:
  837.             /* Unreachable, I think. */
  838.             av_log(c, AV_LOG_ERROR,
  839.                    "altivec_yuv2packedX doesn't support %s output\n",
  840.                    av_get_pix_fmt_name(c->dstFormat));
  841.             return;
  842.         }
  843.  
  844.         memcpy(&((uint32_t *) dest)[i], scratch, (dstW - i) / 4);
  845.     }
  846. }
  847.  
  848. #define YUV2PACKEDX_WRAPPER(suffix, pixfmt)                             \
  849. void ff_yuv2 ## suffix ## _X_altivec(SwsContext *c,                     \
  850.                                      const int16_t *lumFilter,          \
  851.                                      const int16_t **lumSrc,            \
  852.                                      int lumFilterSize,                 \
  853.                                      const int16_t *chrFilter,          \
  854.                                      const int16_t **chrUSrc,           \
  855.                                      const int16_t **chrVSrc,           \
  856.                                      int chrFilterSize,                 \
  857.                                      const int16_t **alpSrc,            \
  858.                                      uint8_t *dest, int dstW, int dstY) \
  859. {                                                                       \
  860.     yuv2packedX_altivec(c, lumFilter, lumSrc, lumFilterSize,            \
  861.                         chrFilter, chrUSrc, chrVSrc,                    \
  862.                         chrFilterSize, alpSrc,                          \
  863.                         dest, dstW, dstY, pixfmt);                      \
  864. }
  865.  
  866. YUV2PACKEDX_WRAPPER(abgr,  AV_PIX_FMT_ABGR);
  867. YUV2PACKEDX_WRAPPER(bgra,  AV_PIX_FMT_BGRA);
  868. YUV2PACKEDX_WRAPPER(argb,  AV_PIX_FMT_ARGB);
  869. YUV2PACKEDX_WRAPPER(rgba,  AV_PIX_FMT_RGBA);
  870. YUV2PACKEDX_WRAPPER(rgb24, AV_PIX_FMT_RGB24);
  871. YUV2PACKEDX_WRAPPER(bgr24, AV_PIX_FMT_BGR24);
  872.  
  873. #endif /* HAVE_ALTIVEC */
  874.