title: How does FFmpeg calculate linesize alignment?
body: <p>I am new to video encoding. I am using FFmpeg library to decode <a href="https://github.com/yaskovdev/sandbox/blob/master/ffmpeg-decoder/637637760230407715" rel="nofollow noreferrer">the H.264 frame</a>. I have the below C++ code (full code is <a href="https://github.com/yaskovdev/sandbox/blob/master/ffmpeg-decoder/main.cpp" rel="nofollow noreferrer">here</a>):</p>
  <pre class="lang-cpp prettyprint-override"><code>AVCodecContext *context = create_codec_context();
  AVFrame *decoded_frame = av_frame_alloc();
  avcodec_send_packet(context, encoded_packet);
  avcodec_receive_frame(context, decoded_frame);
  
  std::cout &lt;&lt; decoded_frame-&gt;width &lt;&lt; &quot;\n&quot;; // prints 1620, as expected
  std::cout &lt;&lt; decoded_frame-&gt;linesize[0] &lt;&lt; &quot;\n&quot;; // prints 1664!
  </code></pre>
  <p>What looks strange to me is the <code>linesize</code> of the decoded frame. Although the <code>width</code> of the frame is <code>1620</code>, the <code>linesize[0]</code> is <code>1664</code>. According to <a href="https://stackoverflow.com/a/35682306/1862286">this answer</a>, the <code>linesize</code> is calculated taking alignment into consideration. In order to get <code>1664</code> from <code>1620</code>, one should apply <code>64</code> as an alignment to the initial width.</p>
  <p>My question is, where does this <code>64</code> alignment come from? Searching through FFmpeg code did not give any results. Does the encoded frame itself already have this information?</p>
  <p>If afterwards I decide to &quot;flatten&quot; the decoded frame into a one-dimensional array using <a href="https://ffmpeg.org/doxygen/3.1/group__lavu__picture.html#ga6f8576f1ef0c2d9a9f7c5ac7f9a28c52" rel="nofollow noreferrer">av_image_copy_to_buffer()</a>, what alignment should I use? Should it also be <code>64</code>?</p>