Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the small optimization from RGB to YUV?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what the small optimization of RGB to YUV is, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Do audio and video, I believe that the conversion between RGB and YUV, we are no stranger. However, because the conversion formula is floating-point operation, coupled with a large number of pixel-intensive operations, resulting in a relatively large consumption of resources, and then the efficiency needs to be further improved.

Just briefly see what can be optimized.

I. Optimization of the formula itself

First of all, let's take a look at the formula from RGB to YUV:

Y = 0.299r + 0.587g + 0.114B

U =-0.169r-0.331g + 0.5 * B

V = 0.5 * R-0.419G-0.081B

Take a look, all floating-point operations, we all know that floating-point multiplication, in the computer to go through the order code and Mantissa operation, relatively time-consuming. If we can get rid of floating-point operations, we can achieve this goal. Therefore, we can transform floating-point operations into integer operations through some mathematical transformations. The transformation process is as follows:

Y = 0.299R + 0.587g + 0.114B = 128 * (0.299R + 0.587g + 0.114B) > > 7 = (38 * R + 75 * B + 15 * B) > 7

PS:U and V are transformed in the same way, and the enlarged value is in the way of four shots and five inputs.

After such conversion, the floating point operation becomes the integer multiplication and displacement operation, and the displacement operation is very efficient, and the integer multiplication operation is also more efficient than the floating point operation, so this method is desirable.

Second, from one-on-one to group fights

Due to the processing of audio and video data, it is a typical intensive operation. That is, for each pixel, our algorithm and processing method are the same, which also makes it possible for us to do further optimization.

It's just fast.

In a more primitive way, we traverse every pixel and do conversion processing for each pixel, so there is no problem to achieve the function, but the efficiency will be relatively low. Because the algorithm is the same, we can process more than one pixel at a time, which greatly improves efficiency.

Suppose we used to process one pixel at a time, now we deal with 16 pixels at a time, so our efficiency will be mentioned 16 times in theory, of course, this is only a theoretical value, in fact, 10 times is certain and considerable. In order to achieve this operation, we can use multimedia instruction sets, such as MMX,SSE, and so on, we can achieve efficiency improvement. However, we should pay attention to the problem of alignment. If we deal with 16 pixels at a time, the width of the image must be a multiple of 16, otherwise we need to do some fault-tolerant processing, such as filling.

The following is part of the code, it is estimated that it can not be used directly, oh, according to the machine's own CPU support instruction set to make corresponding adjustments.

Int YUV420_RGB32_mmx (uint32_t* rgb, int width, int height, uint8_t* y, uint8_t* u, uint8_t* v) {_ _ asm {pushadfinitxor eax, eaxmov ebx, heightmov ecx, widthmov edx, ymov edi, vmov esi, umov ebp, rgbhloop:push ebxmov ebx, ecxwloop: push ebxxor ebx, ebxmov al, [edi] mov bl, [esi] movq mm0, [CoefficientsRGBU + 8*eax] paddw mm0, [CoefficientsRGBV + 8*ebx] mov al, [edx] mov bl, [edx + 1] movq mm1 [CoefficientsRGBY + 8 * eax] movq mm2, [CoefficientsRGBY + 8 * ebx] mov al, [edx + ecx] mov bl, [edx + ecx + 1] movq mm3, [CoefficientsRGBY + 8 * eax] movq mm4, [CoefficientsRGBY + 8 * ebx] paddw mm1, mm0paddw mm2, mm0paddw mm3, mm0paddw mm4, mm0psraw mm1, 6psraw mm2, 6psraw mm3, 6psraw mm4, 6packuswb mm1, mm2packuswb mm3, mm4movq [ebp], mm1movq [ebp + 4 * ecx], mm3add ebp, 8add edx, 2add edi, 1add esi, 1add esi, 1pop ebxsub ebx [ebp + 4*ecx] add edx, ecxpop ebxsub ebx, 2jnz hloopemmspopad}}

To sum up, the conversion from RGB to YUV is a common processing process in audio and video, and there is also some room for optimization. Optimization is mainly divided into two directions, one is to make some changes to the conversion formula itself, so as to avoid floating-point operations; the other is to realize from single processing to batch processing with the help of instruction set.

The above content is what is the small optimization of RGB to YUV? have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report