<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Carson Wu</title>
    <description>The latest articles on Forem by Carson Wu (@carsonwu).</description>
    <link>https://forem.com/carsonwu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1027101%2F6ef0d1ac-9390-4434-b3d3-53aa606e5cf9.jpeg</url>
      <title>Forem: Carson Wu</title>
      <link>https://forem.com/carsonwu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/carsonwu"/>
    <language>en</language>
    <item>
      <title>Libav (FFmpeg) C API understanding from Object-Oriented view</title>
      <dc:creator>Carson Wu</dc:creator>
      <pubDate>Mon, 27 Feb 2023 03:31:31 +0000</pubDate>
      <link>https://forem.com/carsonwu/libav-ffmpeg-c-api-understanding-from-object-oriented-view-1h9i</link>
      <guid>https://forem.com/carsonwu/libav-ffmpeg-c-api-understanding-from-object-oriented-view-1h9i</guid>
      <description>&lt;p&gt;Recently, I was developing WebAssembly based FFmpeg library, &lt;a href="https://github.com/carsonDB/frameflow" rel="noopener noreferrer"&gt;FrameFlow&lt;/a&gt;. It directly uses low-level C API of libav* folders from FFmpeg, to give more power to web browser. I want to share some development experience of using those C APIs.&lt;/p&gt;

&lt;p&gt;FFmpeg mainly has two ways to use it. Command-line way or C API. Actually Command-line program is also based on C API. Now when your first time to learn those APIs, it would be confused why there are multiple steps to create one thing. Because C language only has functions to do something. Why not use just one function to init something?&lt;br&gt;
Here is an example (C++), from &lt;a href="https://github.com/carsonDB/frameflow/blob/6681b44073a65e5ab612e0bf6f24f71742095d5d/src/cpp/encode.cpp#L14" rel="noopener noreferrer"&gt;encode.cpp&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;codec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;avcodec_find_encoder_by_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;codec_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c_str&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;codec_ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;avcodec_alloc_context3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;codec&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;set_avcodec_context_from_streamInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;codec_ctx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;avcodec_open2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;codec_ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;codec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is minimum requirements to create an encoder. Let me explain one by one.&lt;br&gt;
First &lt;code&gt;avcodec_find_encoder_by_name&lt;/code&gt; find the &lt;code&gt;Codec&lt;/code&gt; by its name. This &lt;code&gt;Codec&lt;/code&gt; is just like a class. You cannot change any value in it. It gives you some meta information about the codec (like &lt;code&gt;libx264&lt;/code&gt; codec), and also has pointers to functions to encode for example. Its type is &lt;code&gt;AVCodec&lt;/code&gt;.&lt;br&gt;
Second line &lt;code&gt;avcodec_alloc_context3&lt;/code&gt;, is just &lt;code&gt;malloc&lt;/code&gt; a memory block, with every value in the struct set to default value. It is called &lt;code&gt;codec_ctx&lt;/code&gt; (codec context). The name is a convention in FFmpeg. Because its type is &lt;code&gt;AVCodecContext&lt;/code&gt;. This is just like using &lt;code&gt;new&lt;/code&gt; to create a new object (instance).&lt;br&gt;
The third line is to set all values from &lt;code&gt;info&lt;/code&gt; which I defined before. And this function is my defined function. Don't care about it. This step is just like giving parameters to &lt;code&gt;constructor&lt;/code&gt; of the class.&lt;br&gt;
The last line &lt;code&gt;avcodec_open2&lt;/code&gt; is to initiate the object (instance). Just like calling constructor of the class.&lt;/p&gt;

&lt;p&gt;So although, FFmpeg is written in pure C language. But it actually uses some Object-oriented style to organize the codebase. You can also see other similar examples about &lt;code&gt;demuxer&lt;/code&gt;, &lt;code&gt;muxer&lt;/code&gt;, &lt;code&gt;decoder&lt;/code&gt; in my project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changes after init
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Decoder: Time_base
&lt;/h3&gt;

&lt;p&gt;In my experience of developing, there are some annoying bugs that seem weird, at first glance. Then after understanding the init process as I explained above, there is a key step that we should care about, last step &lt;code&gt;avcodec_open2&lt;/code&gt;. Because it starts a contructor function, and init. It may change some fields that you set at the previous step.&lt;br&gt;
For example, here when you call &lt;code&gt;avcodec_open2&lt;/code&gt;. It will use specifed codec algorithm to init. And often, &lt;code&gt;time_base&lt;/code&gt; will be changed to another value. That may let us surprised. So any output frames' &lt;code&gt;time_base&lt;/code&gt; is according to the new one, not the one you set. So after calling &lt;code&gt;avcodec_open2&lt;/code&gt;, you may need to retrieve current &lt;code&gt;time_base&lt;/code&gt; value from &lt;code&gt;codec_ctx&lt;/code&gt;, to do further stuff.&lt;br&gt;
By the way, you may wonder what is &lt;code&gt;time_base&lt;/code&gt; ? It might be worth to write another blog to explain. And now, simply explained, it is just a time unit, like second, microsecond, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Encoder: format (pixel format / sample format)...
&lt;/h3&gt;

&lt;p&gt;There is another example. For encoder, pixel format (video) or sample format (audio) may be changed, by specified codec algorithm, which the decoder uses. So after init, the encoder may only accept another pixel format frame. So before encoding, you need to &lt;code&gt;rescale&lt;/code&gt; video frames to the specified pixel format, or &lt;code&gt;resample&lt;/code&gt; audio frames to the specified sample format.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Overall, having an Object-oriented view would better understand those C APIs. And You can see all cpp codes in &lt;a href="https://github.com/carsonDB/frameflow/blob/6681b44073a65e5ab612e0bf6f24f71742095d5d/src/cpp/" rel="noopener noreferrer"&gt;FrameFlow-0.1.1 release&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>coding</category>
    </item>
  </channel>
</rss>
