A pitfall in the process of using FFMPEG Video Encoding

Source: Internet
Author: User

Today, I am talking about a pitfall that uses FFMPEG to write Video Encoding programs. It took me a lot of time to think about it. It is very difficult to break through the mindset in many cases. The following are incorrect encoding results:
When using FFMPEG for video encoding, you must first create a new data frame and allocate the corresponding memory for the data frame to save the image data. To allocate memory for the data frame, you must use the av_image_alloc () function, this function allocates memory based on the number of bytes, such as the input image width, height, image format, and Data Alignment base. The number of records may be confusing, that is, the number of records set by the Data Alignment base? By the way, the reason why Data Alignment is required is mainly to improve the data reading efficiency. Assume that the demo provided by FFmpeg is usually set to 32, so in my project, I also set it to 32, as a result, I thought it was a problem with the original data in the replica set and thought I was not completely familiar with it. Yv12 The data format is always incorrect due to program changes. Assume that the resolution of the raw data is 480*480, the width of the Y component row is 480, and that of the UV component row is 240, If the Data Alignment base is set to 32, the row width of the image frames allocated by FFmpeg is a multiple of 32. For data with a width of 480, the row width of the Y component is 480, the row width of the UV component is 256, but the actual row width of the UV component is only 240. In this way, a row contains 16 more bytes, and the data is misplaced when it is copied in sequence, in this way, the result after encoding will be wrong. If it is set to 16, the row width of the Y component is 480, And the row width of the UV component is 240, which is consistent with the width of each row of the original YUV component of the replica set. The data is correct during copy, the video encoded file will also be correct. That is to say, the Data Alignment base should be calculated based on the actual data resolution.,You cannot take it for granted,Calculation method: first, the data row width must be able to divide the base number. At the same time, the alignment base number must be the N power of 2. Of course, the smaller the value is, for example, set it to 1, there is no problem, but the data reading efficiency is lower. Is the correct video encoding result.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.