Brief analysis of FFmpeg Kuibu--Thor "YUV code for H.

Source: Internet
Author: User

The first tribute to Thor. This blog is based on Thor's "simplest ffmpeg-based video encoder (YUV code for H. a)"

wrote, and studied it for two days at last to understand the example of the Thor. Now analysis to everyone, if there is wrong place to hope that everyone can

Correct it.

You can understand Thor's code based on the relationship of the object shown in this diagram, and here are a few areas that are not well understood:

1) avdictionary struct, this struct is used to set some context options in the FFmpeg, key corresponds to value, and the Thor code has the following:

    Avdictionary *param = 0;
    H
    : (pcodecctx->codec_id = = av_codec_id_h264) {
        av_dict_set (param, "preset", "slow", 0);
        Av_dict_set (param, "tune", "Zerolatency", 0);
        Av_dict_set (param, "profile", "main", 0);
    }

Next use the Param setting:

    if (Avcodec_open2 (Pcodecctx, Pcodec,param) < 0) {
        printf ("Failed to open encoder! \ n ");
        return-1;
    }

When using h264,h265 standard encoding in ffmpeg, it may be necessary to set the preset, tune, and Profile,ffmpeg implementations in the Avdictionary incoming avcodec_open2 () function that require additional parameters. Avcodec_open2 is used to initialize the Avcodeccontext. You can find the following statement in the X264.h file:

static const char * Const x264_tune_names[] = {"Film", "animation", \
 "Grain", "stillimage", "Psnr", "Ssim", "Fastdeco De "," zerolatency ", 0};

static const char * Const x264_preset_names[] = {"Ultrafast", "Superfast", "veryfast", "faster", \
"Fast", "medium", "Slow", "slower", "Veryslow", "Placebo", 0};

2) The relationship between the structure in FFmpeg is deliberately embodied in the Code of Thor.

int Flush_encoder (avformatcontext *fmt_ctx,unsigned int stream_index) {
   ...
    if (! ( Fmt_ctx->streams[stream_index]->codec->codec->capabilities &
        Codec_cap_delay))
        return 0;
 
    }

The correspondence between the AVFORMATCONTEXT structure and the relevant encoder throughout the program, Avformatcontext->avstream **streams[index]->avcodeccontext *codec- >const struct Avcodec *codec->. The correspondence between Avformatcontext and Avcodeccontext is: Avformatcontext->avstream **streams[index]->avcodeccontext codec.

3) There are two calls to Avcodec_encode_video2 in the code, one in main, and the other in Flush_encoder. I took the Flush_encoder call commented out after the document "dsbk.h264" and the Non-annotated document "ds.h264" to compare, as shown:


It is clear that the comments out of the execution results will be much smaller than the non-comment, and then the file made a comparison can be seen at the end of the "ds.h264" more than "dsbk.h264" a lot more data, combined with Flush_encoder (scour coding) and pairs (Capabilites & Codec_cap_delay) understanding that the encoder in the continuous input of the original frame output encoding package is delayed, that is, the output packet is not immediately obtained, so after the original frame input encoder end, you need to wash out the remaining data in the encoder.


    at the end of this article, I want to say something unrelated to this. First of all, the spirit of Thor is worth all of us to learn, I am only a small white, even the introduction is not counted. But often want to give up when you think of the thunder God kind of not seeking return, selfless dedication of the spirit, and the technology and knowledge of love, obsessed with the beginner's mind, often think of these my soul is shocked, we live in a peaceful era of peace in the country, for each of our engineers may have a good living conditions, In such circumstances may also be life or work of the trivial annoyance, for a moment of balance, treatment affects the thought of thinking, this is not very ridiculous, treat the people around, but also treat themselves, do not forget the beginner's mind because of technical knowledge and joy.

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.