title: H.264 conversion with FFmpeg (from a RTP stream)
body: <p><strong>Environment:</strong></p>
  
  <p>I have an IP Camera, which is capable of streaming it's data over RTP in a H.264 encoded format. This raw stream is recorded from the ethernet. With that data I have to work.</p>
  
  <p><strong>Goal:</strong></p>
  
  <p>In the end I want to have a *.mp4 file, which I can play with common Media Players (like VLC or Windows MP).</p>
  
  <p><strong>What have I done so far:</strong></p>
  
  <p>I take that raw stream data I have and parse it. Since the data has been transmitted via RTP I need to take care of the NAL Bytes, SPS and PPS. </p>
  
  <p><em><strong>1. Write a raw file</em></strong></p>
  
  <p>First I determine the type of each frame received over Ethernet. To do so, I parse the first two bytes of every RTP Payload, so I can get the 8 NAL Unit Bit, the Fragment Type Bits and the Start, Reserved and End Bit. In the payload, they're arranged like this:</p>
  
  <pre><code>Byte 1: [          3 NAL Unit Bits          | 5 Fragment Type Bits]
  Byte 2: [Start Bit | Reserved Bit | End Bit | 5 NAL Unit Bits]
  </code></pre>
  
  <p>From this I can determine:</p>
  
  <ul>
  <li>Start and End of 
  a Video Frame -> Start Bit and End Bit</li>
  <li>Type of the Payload -> 5 Fragment Type Bits</li>
  <li>NAL Unit Byte</li>
  </ul>
  
  <p>The Fragment types which are necessary in my case are:</p>
  
  <pre><code>Fragment Type  7 = SPS
  Fragment Type  8 = PPS
  Fragment Type 28 = Video Fragment
  </code></pre>
  
  <p>The NAL Byte is created by putting the NAL Unit Bits from Byte 1 and 2 together.</p>
  
  <p>Now depending on the fragmentation type I do the following:</p>
  
  <p><em>SPS/PPS:</em></p>
  
  <ol>
  <li>Write the NAL Prefix (<code>0x00 0x00 0x01</code>) and then the SPS or PPS data</li>
  </ol>
  
  <p><em>Fragmentation with Start Bit</em></p>
  
  <ol>
  <li>Write NAL Prefix</li>
  <li>Write NAL Unit Byte</li>
  <li>Write remaining raw data</li>
  </ol>
  
  <p><em>Fragmentation without Start Bit</em></p>
  
  <ol>
  <li>Write raw data</li>
  </ol>
  
  <p>This means my raw file looks something like this:</p>
  
  <pre><code>[NAL Prefix][SPS][NAL Prefix][PPS][NAL Prefix][NAL Unit Byte][Raw Video Data][Raw Video Data]....[NAL Prefix][NAL Unit Byte][Raw Video Data]...
  </code></pre>
  
  <p>For every PPS and SPS I find in my stream data, I just write a NAL Prefix ( 0x00 0x00 0x01 ) and then the SPS/PPS itself.</p>
  
  <p>Now I can't play this data with some media player, which leads me to :</p>
  
  <p><em><strong>2. Convert the file</em></strong></p>
  
  <p>Since I wanted to avoid working much with codecs I just went to use an existing application -> FFmpeg. This I am calling with those parameters:</p>
  
  <p><code>ffmpeg.exe -f h264 -i &lt;RawInputFile&gt; -vcodec copy -r 25 &lt;OutPutFilename&gt;.mp4</code></p>
  
  <p><code>-f h264</code>: This should tell ffmpeg I have a h264 coded stream</p>
  
  <p><code>-vcodec copy</code>: Quote from the manpage:</p>
  
  <p><code>Force video codec to codec. Use the "copy" special value to tell that the raw codec data must be copied as is.</code></p>
  
  <p><code>-r 25</code>: Sets the framerate to 25 FPS.</p>
  
  <p>When I call ffmpeg with those parameters I get an .mp4 File, which I can play with VLC and Windows MP,  so it actually works. But the file now looks a bit different from my raw file.</p>
  
  <p>This leads me to my question:</p>
  
  <p><strong>What did I actually do?</strong></p>
  
  <p>My problem is not that it is not working. I just want/need to know what I have actually done with calling ffmpeg. I had a raw H264 file which I <strong>could not</strong> play. After using FFmpeg I <strong>can</strong> play it.</p>
  
  <p>There are the following differences between the original raw file (which I have written) and the one written by FFmpeg:</p>
  
  <ol>
  <li>Header: The FFmpeg File has like about 0x30 Bytes of Header</li>
  <li>Footer: The FFmpeg File also has a footer</li>
  <li>Changed Prefix and 2 new Bytes:</li>
  </ol>
  
  <p>While a new Video Frame from the Raw File started like
  <code>[NAL Prefix][NAL Unit Byte][Raw Video Data]</code> in the new file it looks like this:</p>
  
  <pre><code>[0x00 0x00][2 "Random" Bytes][NAL Unit Byte][Raw Video Data].....[0x00 0x00[2 other "Random" Bytes][NAL Unit Byte][Raw Video Data]...
  </code></pre>
  
  <p>I understand that the Video Stream needs a container format (correct me if I am wrong but I assume that the new header and footer are responsible for that). But why does it change actually some Bytes in the raw data? It can't be some decoding since the stream itself should get decoded by the player and not ffmpeg.</p>
  
  <p>As you can see I don't need a new solution for my problem as far more an explanation (so I can explain it by myself). What does ffmpeg actually do? And why does it change some bytes within the video data? </p>