Path: blob/21.2-virgl/src/util/format/u_format_yuv.c
7160 views
/**************************************************************************1*2* Copyright 2010 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,17* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19* USE OR OTHER DEALINGS IN THE SOFTWARE.20*21* The above copyright notice and this permission notice (including the22* next paragraph) shall be included in all copies or substantial portions23* of the Software.24*25**************************************************************************/262728/**29* @file30* YUV and RGB subsampled formats conversion.31*32* @author Jose Fonseca <[email protected]>33*/343536#include "util/u_debug.h"37#include "util/format/u_format_yuv.h"383940void41util_format_r8g8_b8g8_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,42const uint8_t *restrict src_row, unsigned src_stride,43unsigned width, unsigned height)44{45unsigned x, y;4647for (y = 0; y < height; y += 1) {48float *dst = dst_row;49const uint32_t *src = (const uint32_t *)src_row;50uint32_t value;51float r, g0, g1, b;5253for (x = 0; x + 1 < width; x += 2) {54value = util_cpu_to_le32(*src++);5556r = ubyte_to_float((value >> 0) & 0xff);57g0 = ubyte_to_float((value >> 8) & 0xff);58b = ubyte_to_float((value >> 16) & 0xff);59g1 = ubyte_to_float((value >> 24) & 0xff);6061dst[0] = r; /* r */62dst[1] = g0; /* g */63dst[2] = b; /* b */64dst[3] = 1.0f; /* a */65dst += 4;6667dst[0] = r; /* r */68dst[1] = g1; /* g */69dst[2] = b; /* b */70dst[3] = 1.0f; /* a */71dst += 4;72}7374if (x < width) {75value = util_cpu_to_le32(*src);7677r = ubyte_to_float((value >> 0) & 0xff);78g0 = ubyte_to_float((value >> 8) & 0xff);79b = ubyte_to_float((value >> 16) & 0xff);80g1 = ubyte_to_float((value >> 24) & 0xff);8182dst[0] = r; /* r */83dst[1] = g0; /* g */84dst[2] = b; /* b */85dst[3] = 1.0f; /* a */86}8788src_row = (uint8_t *)src_row + src_stride;89dst_row = (uint8_t *)dst_row + dst_stride;90}91}929394void95util_format_r8g8_b8g8_unorm_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,96const uint8_t *restrict src_row, unsigned src_stride,97unsigned width, unsigned height)98{99unsigned x, y;100101for (y = 0; y < height; y += 1) {102uint8_t *dst = dst_row;103const uint32_t *src = (const uint32_t *)src_row;104uint32_t value;105uint8_t r, g0, g1, b;106107for (x = 0; x + 1 < width; x += 2) {108value = util_cpu_to_le32(*src++);109110r = (value >> 0) & 0xff;111g0 = (value >> 8) & 0xff;112b = (value >> 16) & 0xff;113g1 = (value >> 24) & 0xff;114115dst[0] = r; /* r */116dst[1] = g0; /* g */117dst[2] = b; /* b */118dst[3] = 0xff; /* a */119dst += 4;120121dst[0] = r; /* r */122dst[1] = g1; /* g */123dst[2] = b; /* b */124dst[3] = 0xff; /* a */125dst += 4;126}127128if (x < width) {129value = util_cpu_to_le32(*src);130131r = (value >> 0) & 0xff;132g0 = (value >> 8) & 0xff;133b = (value >> 16) & 0xff;134g1 = (value >> 24) & 0xff;135136dst[0] = r; /* r */137dst[1] = g0; /* g */138dst[2] = b; /* b */139dst[3] = 0xff; /* a */140}141142src_row += src_stride/sizeof(*src_row);143dst_row += dst_stride/sizeof(*dst_row);144}145}146147148void149util_format_r8g8_b8g8_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,150const float *restrict src_row, unsigned src_stride,151unsigned width, unsigned height)152{153unsigned x, y;154155for (y = 0; y < height; y += 1) {156const float *src = src_row;157uint32_t *dst = (uint32_t *)dst_row;158float r, g0, g1, b;159uint32_t value;160161for (x = 0; x + 1 < width; x += 2) {162r = 0.5f*(src[0] + src[4]);163g0 = src[1];164g1 = src[5];165b = 0.5f*(src[2] + src[6]);166167value = (uint32_t)float_to_ubyte(r);168value |= (uint32_t)float_to_ubyte(g0) << 8;169value |= (uint32_t)float_to_ubyte(b) << 16;170value |= (uint32_t)float_to_ubyte(g1) << 24;171172*dst++ = util_le32_to_cpu(value);173174src += 8;175}176177if (x < width) {178r = src[0];179g0 = src[1];180g1 = 0;181b = src[2];182183value = (uint32_t)float_to_ubyte(r);184value |= (uint32_t)float_to_ubyte(g0) << 8;185value |= (uint32_t)float_to_ubyte(b) << 16;186value |= (uint32_t)float_to_ubyte(g1) << 24;187188*dst = util_le32_to_cpu(value);189}190191dst_row += dst_stride/sizeof(*dst_row);192src_row += src_stride/sizeof(*src_row);193}194}195196197void198util_format_r8g8_b8g8_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,199const uint8_t *restrict src_row, unsigned src_stride,200unsigned width, unsigned height)201{202unsigned x, y;203204for (y = 0; y < height; y += 1) {205const uint8_t *src = src_row;206uint32_t *dst = (uint32_t *)dst_row;207uint32_t r, g0, g1, b;208uint32_t value;209210for (x = 0; x + 1 < width; x += 2) {211r = (src[0] + src[4] + 1) >> 1;212g0 = src[1];213g1 = src[5];214b = (src[2] + src[6] + 1) >> 1;215216value = r;217value |= (uint32_t)g0 << 8;218value |= (uint32_t)b << 16;219value |= (uint32_t)g1 << 24;220221*dst++ = util_le32_to_cpu(value);222223src += 8;224}225226if (x < width) {227r = src[0];228g0 = src[1];229g1 = 0;230b = src[2];231232value = r;233value |= (uint32_t)g0 << 8;234value |= (uint32_t)b << 16;235value |= (uint32_t)g1 << 24;236237*dst = util_le32_to_cpu(value);238}239240dst_row += dst_stride/sizeof(*dst_row);241src_row += src_stride/sizeof(*src_row);242}243}244245246void247util_format_r8g8_b8g8_unorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src,248unsigned i, ASSERTED unsigned j)249{250float *dst = in_dst;251252assert(i < 2);253assert(j < 1);254255dst[0] = ubyte_to_float(src[0]); /* r */256dst[1] = ubyte_to_float(src[1 + 2*i]); /* g */257dst[2] = ubyte_to_float(src[2]); /* b */258dst[3] = 1.0f; /* a */259}260261262void263util_format_g8r8_g8b8_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,264const uint8_t *restrict src_row, unsigned src_stride,265unsigned width, unsigned height)266{267unsigned x, y;268269for (y = 0; y < height; y += 1) {270float *dst = dst_row;271const uint32_t *src = (const uint32_t *)src_row;272uint32_t value;273float r, g0, g1, b;274275for (x = 0; x + 1 < width; x += 2) {276value = util_cpu_to_le32(*src++);277278g0 = ubyte_to_float((value >> 0) & 0xff);279r = ubyte_to_float((value >> 8) & 0xff);280g1 = ubyte_to_float((value >> 16) & 0xff);281b = ubyte_to_float((value >> 24) & 0xff);282283dst[0] = r; /* r */284dst[1] = g0; /* g */285dst[2] = b; /* b */286dst[3] = 1.0f; /* a */287dst += 4;288289dst[0] = r; /* r */290dst[1] = g1; /* g */291dst[2] = b; /* b */292dst[3] = 1.0f; /* a */293dst += 4;294}295296if (x < width) {297value = util_cpu_to_le32(*src);298299g0 = ubyte_to_float((value >> 0) & 0xff);300r = ubyte_to_float((value >> 8) & 0xff);301g1 = ubyte_to_float((value >> 16) & 0xff);302b = ubyte_to_float((value >> 24) & 0xff);303304dst[0] = r; /* r */305dst[1] = g0; /* g */306dst[2] = b; /* b */307dst[3] = 1.0f; /* a */308}309310src_row = (uint8_t *)src_row + src_stride;311dst_row = (uint8_t *)dst_row + dst_stride;312}313}314315316void317util_format_g8r8_g8b8_unorm_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,318const uint8_t *restrict src_row, unsigned src_stride,319unsigned width, unsigned height)320{321unsigned x, y;322323for (y = 0; y < height; y += 1) {324uint8_t *dst = dst_row;325const uint32_t *src = (const uint32_t *)src_row;326uint32_t value;327uint8_t r, g0, g1, b;328329for (x = 0; x + 1 < width; x += 2) {330value = util_cpu_to_le32(*src++);331332g0 = (value >> 0) & 0xff;333r = (value >> 8) & 0xff;334g1 = (value >> 16) & 0xff;335b = (value >> 24) & 0xff;336337dst[0] = r; /* r */338dst[1] = g0; /* g */339dst[2] = b; /* b */340dst[3] = 0xff; /* a */341dst += 4;342343dst[0] = r; /* r */344dst[1] = g1; /* g */345dst[2] = b; /* b */346dst[3] = 0xff; /* a */347dst += 4;348}349350if (x < width) {351value = util_cpu_to_le32(*src);352353g0 = (value >> 0) & 0xff;354r = (value >> 8) & 0xff;355g1 = (value >> 16) & 0xff;356b = (value >> 24) & 0xff;357358dst[0] = r; /* r */359dst[1] = g0; /* g */360dst[2] = b; /* b */361dst[3] = 0xff; /* a */362}363364src_row += src_stride/sizeof(*src_row);365dst_row += dst_stride/sizeof(*dst_row);366}367}368369370void371util_format_g8r8_g8b8_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,372const float *restrict src_row, unsigned src_stride,373unsigned width, unsigned height)374{375unsigned x, y;376377for (y = 0; y < height; y += 1) {378const float *src = src_row;379uint32_t *dst = (uint32_t *)dst_row;380float r, g0, g1, b;381uint32_t value;382383for (x = 0; x + 1 < width; x += 2) {384r = 0.5f*(src[0] + src[4]);385g0 = src[1];386g1 = src[5];387b = 0.5f*(src[2] + src[6]);388389value = (uint32_t)float_to_ubyte(g0);390value |= (uint32_t)float_to_ubyte(r) << 8;391value |= (uint32_t)float_to_ubyte(g1) << 16;392value |= (uint32_t)float_to_ubyte(b) << 24;393394*dst++ = util_le32_to_cpu(value);395396src += 8;397}398399if (x < width) {400r = src[0];401g0 = src[1];402g1 = 0;403b = src[2];404405value = (uint32_t)float_to_ubyte(g0);406value |= (uint32_t)float_to_ubyte(r) << 8;407value |= (uint32_t)float_to_ubyte(g1) << 16;408value |= (uint32_t)float_to_ubyte(b) << 24;409410*dst = util_le32_to_cpu(value);411}412413dst_row += dst_stride/sizeof(*dst_row);414src_row += src_stride/sizeof(*src_row);415}416}417418419void420util_format_g8r8_g8b8_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,421const uint8_t *restrict src_row, unsigned src_stride,422unsigned width, unsigned height)423{424unsigned x, y;425426for (y = 0; y < height; y += 1) {427const uint8_t *src = src_row;428uint32_t *dst = (uint32_t *)dst_row;429uint32_t r, g0, g1, b;430uint32_t value;431432for (x = 0; x + 1 < width; x += 2) {433r = (src[0] + src[4] + 1) >> 1;434g0 = src[1];435g1 = src[5];436b = (src[2] + src[6] + 1) >> 1;437438value = g0;439value |= (uint32_t)r << 8;440value |= (uint32_t)g1 << 16;441value |= (uint32_t)b << 24;442443*dst++ = util_le32_to_cpu(value);444445src += 8;446}447448if (x < width) {449r = src[0];450g0 = src[1];451g1 = 0;452b = src[2];453454value = g0;455value |= (uint32_t)r << 8;456value |= (uint32_t)g1 << 16;457value |= (uint32_t)b << 24;458459*dst = util_le32_to_cpu(value);460}461462dst_row += dst_stride/sizeof(*dst_row);463src_row += src_stride/sizeof(*src_row);464}465}466467468void469util_format_g8r8_g8b8_unorm_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src,470unsigned i, ASSERTED unsigned j)471{472float *dst = in_dst;473474assert(i < 2);475assert(j < 1);476477dst[0] = ubyte_to_float(src[1]); /* r */478dst[1] = ubyte_to_float(src[0 + 2*i]); /* g */479dst[2] = ubyte_to_float(src[3]); /* b */480dst[3] = 1.0f; /* a */481}482483484void485util_format_uyvy_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,486const uint8_t *restrict src_row, unsigned src_stride,487unsigned width, unsigned height)488{489unsigned x, y;490491for (y = 0; y < height; y += 1) {492float *dst = dst_row;493const uint32_t *src = (const uint32_t *)src_row;494uint32_t value;495uint8_t y0, y1, u, v;496497for (x = 0; x + 1 < width; x += 2) {498value = util_cpu_to_le32(*src++);499500u = (value >> 0) & 0xff;501y0 = (value >> 8) & 0xff;502v = (value >> 16) & 0xff;503y1 = (value >> 24) & 0xff;504505util_format_yuv_to_rgb_float(y0, u, v, &dst[0], &dst[1], &dst[2]);506dst[3] = 1.0f; /* a */507dst += 4;508509util_format_yuv_to_rgb_float(y1, u, v, &dst[0], &dst[1], &dst[2]);510dst[3] = 1.0f; /* a */511dst += 4;512}513514if (x < width) {515value = util_cpu_to_le32(*src);516517u = (value >> 0) & 0xff;518y0 = (value >> 8) & 0xff;519v = (value >> 16) & 0xff;520y1 = (value >> 24) & 0xff;521522util_format_yuv_to_rgb_float(y0, u, v, &dst[0], &dst[1], &dst[2]);523dst[3] = 1.0f; /* a */524}525526src_row = (uint8_t *)src_row + src_stride;527dst_row = (uint8_t *)dst_row + dst_stride;528}529}530531532void533util_format_uyvy_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,534const uint8_t *restrict src_row, unsigned src_stride,535unsigned width, unsigned height)536{537unsigned x, y;538539for (y = 0; y < height; y += 1) {540uint8_t *dst = dst_row;541const uint32_t *src = (const uint32_t *)src_row;542uint32_t value;543uint8_t y0, y1, u, v;544545for (x = 0; x + 1 < width; x += 2) {546value = util_cpu_to_le32(*src++);547548u = (value >> 0) & 0xff;549y0 = (value >> 8) & 0xff;550v = (value >> 16) & 0xff;551y1 = (value >> 24) & 0xff;552553util_format_yuv_to_rgb_8unorm(y0, u, v, &dst[0], &dst[1], &dst[2]);554dst[3] = 0xff; /* a */555dst += 4;556557util_format_yuv_to_rgb_8unorm(y1, u, v, &dst[0], &dst[1], &dst[2]);558dst[3] = 0xff; /* a */559dst += 4;560}561562if (x < width) {563value = util_cpu_to_le32(*src);564565u = (value >> 0) & 0xff;566y0 = (value >> 8) & 0xff;567v = (value >> 16) & 0xff;568y1 = (value >> 24) & 0xff;569570util_format_yuv_to_rgb_8unorm(y0, u, v, &dst[0], &dst[1], &dst[2]);571dst[3] = 0xff; /* a */572}573574src_row += src_stride/sizeof(*src_row);575dst_row += dst_stride/sizeof(*dst_row);576}577}578579580void581util_format_uyvy_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,582const float *restrict src_row, unsigned src_stride,583unsigned width, unsigned height)584{585unsigned x, y;586587for (y = 0; y < height; y += 1) {588const float *src = src_row;589uint32_t *dst = (uint32_t *)dst_row;590uint8_t y0, y1, u, v;591uint32_t value;592593for (x = 0; x + 1 < width; x += 2) {594uint8_t y0, y1, u0, u1, v0, v1, u, v;595596util_format_rgb_float_to_yuv(src[0], src[1], src[2],597&y0, &u0, &v0);598util_format_rgb_float_to_yuv(src[4], src[5], src[6],599&y1, &u1, &v1);600601u = (u0 + u1 + 1) >> 1;602v = (v0 + v1 + 1) >> 1;603604value = u;605value |= (uint32_t)y0 << 8;606value |= (uint32_t)v << 16;607value |= (uint32_t)y1 << 24;608609*dst++ = util_le32_to_cpu(value);610611src += 8;612}613614if (x < width) {615util_format_rgb_float_to_yuv(src[0], src[1], src[2],616&y0, &u, &v);617y1 = 0;618619value = u;620value |= (uint32_t)y0 << 8;621value |= (uint32_t)v << 16;622value |= (uint32_t)y1 << 24;623624*dst = util_le32_to_cpu(value);625}626627dst_row += dst_stride/sizeof(*dst_row);628src_row += src_stride/sizeof(*src_row);629}630}631632633void634util_format_uyvy_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,635const uint8_t *restrict src_row, unsigned src_stride,636unsigned width, unsigned height)637{638unsigned x, y;639640for (y = 0; y < height; y += 1) {641const uint8_t *src = src_row;642uint32_t *dst = (uint32_t *)dst_row;643uint8_t y0, y1, u, v;644uint32_t value;645646for (x = 0; x + 1 < width; x += 2) {647uint8_t y0, y1, u0, u1, v0, v1, u, v;648649util_format_rgb_8unorm_to_yuv(src[0], src[1], src[2],650&y0, &u0, &v0);651util_format_rgb_8unorm_to_yuv(src[4], src[5], src[6],652&y1, &u1, &v1);653654u = (u0 + u1 + 1) >> 1;655v = (v0 + v1 + 1) >> 1;656657value = u;658value |= (uint32_t)y0 << 8;659value |= (uint32_t)v << 16;660value |= (uint32_t)y1 << 24;661662*dst++ = util_le32_to_cpu(value);663664src += 8;665}666667if (x < width) {668util_format_rgb_8unorm_to_yuv(src[0], src[1], src[2],669&y0, &u, &v);670y1 = 0;671672value = u;673value |= (uint32_t)y0 << 8;674value |= (uint32_t)v << 16;675value |= (uint32_t)y1 << 24;676677*dst = util_le32_to_cpu(value);678}679680dst_row += dst_stride/sizeof(*dst_row);681src_row += src_stride/sizeof(*src_row);682}683}684685686void687util_format_uyvy_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src,688unsigned i, ASSERTED unsigned j)689{690float *dst = in_dst;691uint8_t y, u, v;692693assert(i < 2);694assert(j < 1);695696y = src[1 + i*2];697u = src[0];698v = src[2];699700util_format_yuv_to_rgb_float(y, u, v, &dst[0], &dst[1], &dst[2]);701702dst[3] = 1.0f;703}704705706void707util_format_yuyv_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,708const uint8_t *restrict src_row, unsigned src_stride,709unsigned width, unsigned height)710{711unsigned x, y;712713for (y = 0; y < height; y += 1) {714float *dst = dst_row;715const uint32_t *src = (const uint32_t *)src_row;716uint32_t value;717uint8_t y0, y1, u, v;718719for (x = 0; x + 1 < width; x += 2) {720value = util_cpu_to_le32(*src++);721722y0 = (value >> 0) & 0xff;723u = (value >> 8) & 0xff;724y1 = (value >> 16) & 0xff;725v = (value >> 24) & 0xff;726727util_format_yuv_to_rgb_float(y0, u, v, &dst[0], &dst[1], &dst[2]);728dst[3] = 1.0f; /* a */729dst += 4;730731util_format_yuv_to_rgb_float(y1, u, v, &dst[0], &dst[1], &dst[2]);732dst[3] = 1.0f; /* a */733dst += 4;734}735736if (x < width) {737value = util_cpu_to_le32(*src);738739y0 = (value >> 0) & 0xff;740u = (value >> 8) & 0xff;741y1 = (value >> 16) & 0xff;742v = (value >> 24) & 0xff;743744util_format_yuv_to_rgb_float(y0, u, v, &dst[0], &dst[1], &dst[2]);745dst[3] = 1.0f; /* a */746}747748src_row = (uint8_t *)src_row + src_stride;749dst_row = (uint8_t *)dst_row + dst_stride;750}751}752753754void755util_format_yuyv_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,756const uint8_t *restrict src_row, unsigned src_stride,757unsigned width, unsigned height)758{759unsigned x, y;760761for (y = 0; y < height; y += 1) {762uint8_t *dst = dst_row;763const uint32_t *src = (const uint32_t *)src_row;764uint32_t value;765uint8_t y0, y1, u, v;766767for (x = 0; x + 1 < width; x += 2) {768value = util_cpu_to_le32(*src++);769770y0 = (value >> 0) & 0xff;771u = (value >> 8) & 0xff;772y1 = (value >> 16) & 0xff;773v = (value >> 24) & 0xff;774775util_format_yuv_to_rgb_8unorm(y0, u, v, &dst[0], &dst[1], &dst[2]);776dst[3] = 0xff; /* a */777dst += 4;778779util_format_yuv_to_rgb_8unorm(y1, u, v, &dst[0], &dst[1], &dst[2]);780dst[3] = 0xff; /* a */781dst += 4;782}783784if (x < width) {785value = util_cpu_to_le32(*src);786787y0 = (value >> 0) & 0xff;788u = (value >> 8) & 0xff;789y1 = (value >> 16) & 0xff;790v = (value >> 24) & 0xff;791792util_format_yuv_to_rgb_8unorm(y0, u, v, &dst[0], &dst[1], &dst[2]);793dst[3] = 0xff; /* a */794}795796src_row += src_stride/sizeof(*src_row);797dst_row += dst_stride/sizeof(*dst_row);798}799}800801802void803util_format_yuyv_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,804const float *restrict src_row, unsigned src_stride,805unsigned width, unsigned height)806{807unsigned x, y;808809for (y = 0; y < height; y += 1) {810const float *src = src_row;811uint32_t *dst = (uint32_t *)dst_row;812uint8_t y0, y1, u, v;813uint32_t value;814815for (x = 0; x + 1 < width; x += 2) {816uint8_t y0, y1, u0, u1, v0, v1, u, v;817818util_format_rgb_float_to_yuv(src[0], src[1], src[2],819&y0, &u0, &v0);820util_format_rgb_float_to_yuv(src[4], src[5], src[6],821&y1, &u1, &v1);822823u = (u0 + u1 + 1) >> 1;824v = (v0 + v1 + 1) >> 1;825826value = y0;827value |= (uint32_t)u << 8;828value |= (uint32_t)y1 << 16;829value |= (uint32_t)v << 24;830831*dst++ = util_le32_to_cpu(value);832833src += 8;834}835836if (x < width) {837util_format_rgb_float_to_yuv(src[0], src[1], src[2],838&y0, &u, &v);839y1 = 0;840841value = y0;842value |= (uint32_t)u << 8;843value |= (uint32_t)y1 << 16;844value |= (uint32_t)v << 24;845846*dst = util_le32_to_cpu(value);847}848849dst_row += dst_stride/sizeof(*dst_row);850src_row += src_stride/sizeof(*src_row);851}852}853854855void856util_format_yuyv_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,857const uint8_t *restrict src_row, unsigned src_stride,858unsigned width, unsigned height)859{860unsigned x, y;861862for (y = 0; y < height; y += 1) {863const uint8_t *src = src_row;864uint32_t *dst = (uint32_t *)dst_row;865uint8_t y0, y1, u, v;866uint32_t value;867868for (x = 0; x + 1 < width; x += 2) {869uint8_t y0, y1, u0, u1, v0, v1, u, v;870871util_format_rgb_8unorm_to_yuv(src[0], src[1], src[2],872&y0, &u0, &v0);873util_format_rgb_8unorm_to_yuv(src[4], src[5], src[6],874&y1, &u1, &v1);875876u = (u0 + u1 + 1) >> 1;877v = (v0 + v1 + 1) >> 1;878879value = y0;880value |= (uint32_t)u << 8;881value |= (uint32_t)y1 << 16;882value |= (uint32_t)v << 24;883884*dst++ = util_le32_to_cpu(value);885886src += 8;887}888889if (x < width) {890util_format_rgb_8unorm_to_yuv(src[0], src[1], src[2],891&y0, &u, &v);892y1 = 0;893894value = y0;895value |= (uint32_t)u << 8;896value |= (uint32_t)y1 << 16;897value |= (uint32_t)v << 24;898899*dst = util_le32_to_cpu(value);900}901902dst_row += dst_stride/sizeof(*dst_row);903src_row += src_stride/sizeof(*src_row);904}905}906907908void909util_format_yuyv_fetch_rgba(void *restrict in_dst, const uint8_t *restrict src,910unsigned i, ASSERTED unsigned j)911{912float *dst = in_dst;913uint8_t y, u, v;914915assert(i < 2);916assert(j < 1);917918y = src[0 + i*2];919u = src[1];920v = src[3];921922util_format_yuv_to_rgb_float(y, u, v, &dst[0], &dst[1], &dst[2]);923924dst[3] = 1.0f;925}926927928