When I read the source code of the underlying stream media library on the TV forwarding server these two days, I encountered some questions when I saw the display part:
When d3d is used for display, the data format we display is specified as yv12. For the distribution of yv12 data formats in the memory, refer to yv12 articles, I will skip it for the moment.
Copy the data to the displayed locked memory for display:
For (I = 0; I Memcpy (p + I * stride, item-> output + I * w, W );
}
For (I = 0; I
Memcpy (p + stride * H + I * stride/2, item-> output + W * H/4 + I * w/2, W/2 );
}
For (I = 0; I
Memcpy (p + stride * H/4 + I * stride/2, item-> output + W * H + I * w/2, W/2 );
}
Before the display, that is, when decoding, the data is also copied:
Memcpy (& (pitemf. Head), & (m_trecentitem.head), sizeof (av_head_param ));
For (INT I = 0; I <m_trecentitem.head.height; I ++) // copy y Data
Memcpy (pyuvbuf + I * m_trecentitem.head.width, m_trecentitem.picture-> data [0] + I * m_trecentitem.picture-> linesize [0], m_trecentitem.head.width );
For (INT I = 0; I <m_trecentitem.head.height/2; I ++) // U data copy
Memcpy (pyuvbuf + m_trecentitem.head.height * Ratio + I * percent/2, m_trecentitem.picture-> data [1] + I * m_trecentitem.picture-> linesize [1], Bytes/2 );
For (INT I = 0; I <m_trecentitem.head.height/2; I ++) // v data copy
Memcpy (pyuvbuf + pai* region + m_trecentitem.head.height/2 * region/2 + I * region/2, m_trecentitem.picture-> data [2] + I * m_trecentitem.picture-> linesize [2], m_trecentitem.head.width/2 );
The above item-> output = pyuvbuf
If the data displayed is yv12, what format is the decoded data format?
--------------------------------------------------------------
The decoded data format is i420.
Mentioned in this blog: http://blog.sina.com.cn/s/blog_4ae178ba01018o7q.html
Decoding
If you only need to calculate the YUV 420i data, you only need to call it once:
Avcodec_decode_video (g_pcodecctx, g_pavfframe, (int *) & ngot, (unsigned _ int8 *) psrcdata, dwdatalen );
Here, ngot is used to return whether the decoding is successful. After avcodec_decode_video is called, if ngot is not equal to 0, the decoding is successful. Otherwise, the video frame is not decoded.
Psrcdata is a data stream encoded in h264 to be decrypted. dwdatalen indicates the length of the data stream in bytes.
The video frame (YUV data) After decoding is saved into g_pavfframe, g_pavfframe-> data [0], g_pavfframe-> data [1], g_pavfframe-> data [2] are YUV data. The following sample code places the YUV data in a memory in the following way:
YY
YY
U
V
This function has a return value: If the decoding is successful, the number of bitstream bytes used for this decoding is returned; otherwise, 0 is returned.
------------------------------------------------------------------------
Differences between yv12 and i420 generally, the video data directly collected is in rgb24 format. The size of a rgb24 frame is size = width × heigth × 3 bit, the size of rgb32 is width × heigth × 4. If it is i420 (that is, the YUV standard format is), the data size is size = width × heigth × 1. 5 bit.
After rgb24 data is collected, the data in this format needs to be compressed for the first time. The color space of the image is determined by rgb2yuv. Because the standard YUV () is required for x264 encoding ). However, it should be noted that although yv12 is also (), yv12 and i420 are different, and there are some differences in the storage space. As follows:
Yv12: brightness (row x column) + V (row x column/4) + U (row x column/4)
I420: brightness (row x column) + U (row x column/4) + V (row x column/4)
It can be seen that yv12 and i420 are basically the same, that is, the UV sequence is different. ------------------------------------------------------------------
From the above section, we can know that during the display, because the i420 format data is different from the yv12 format data UV storage order, so when we do the display, the data is re-stored once.
Decoded data format of FFMPEG