title: H.264 over RTP - Identify SPS and PPS Frames
body: <p>I have a raw H.264 Stream from an IP Camera packed in RTP frames. I want to get raw H.264 data into a file so I can convert it with <code>ffmpeg</code>.</p>
  
  <p>So when I want to write the data into my raw H.264 file I found out it has to look like this:</p>
  
  <pre><code>00 00 01 [SPS] 
  00 00 01 [PPS]
  00 00 01 [NALByte]
  [PAYLOAD RTP Frame 1]     // Payload always without the first 2 Bytes -&gt; NAL
  [PAYLOAD RTP Frame 2]
  [... until PAYLOAD Frame with Mark Bit received]  // From here its a new Video Frame
  00 00 01 [NAL BYTE]
  [PAYLOAD RTP Frame 1]
  ....
  </code></pre>
  
  <p>So I get the <code>SPS</code> and the <code>PPS</code> from the <code>Session Description Protocol</code> out of my preceding <code>RTSP</code> communication. Additionally the camera sends the <code>SPS</code> and the <code>PPS</code>in two single messages before starting with the video stream itself. </p>
  
  <p>So I capture the messages in this order:</p>
  
  <pre><code>1. Preceding RTSP Communication here ( including SDP with SPS and PPS )
  2. RTP Frame with Payload: 67 42 80 28 DA 01 40 16 C4    // This is the SPS 
  3. RTP Frame with Payload: 68 CE 3C 80                   // This is the PPS
  4. RTP Frame with Payload: ...  // Video Data
  </code></pre>
  
  <p>Then there come some Frames with Payload and at some point a RTP Frame with the <code>Marker Bit = 1</code>. This means ( if I got it right) that I have a complete video frame. Afer this I write the Prefix Sequence ( <code>00 00 01</code> ) and the <code>NAL</code>from the payload again and go on with the same procedure.</p>
  
  <p>Now my camera sends me after every 8 complete Video Frames the <code>SPS</code> and the <code>PPS</code> again. ( Again in two RTP Frames, as seen in the example above ). I know that especially the <code>PPS</code> can change in between streaming but that's not the problem.</p>
  
  <p>My questions are now:</p>
  
  <p><strong>1. Do I need to write the SPS/PPS every 8th Video Frame?</strong></p>
  
  <p>If my <code>SPS</code> and my <code>PPS</code> don't change it should be enough to have them written at the very beginning of my file and nothing more?</p>
  
  <p><strong>2. How to distinguish between SPS/PPS and normal RTP Frames?</strong></p>
  
  <p>In my C++ Code which parses the transmitted data I need make a difference between the RTP Frames with normal Payload an the ones carrying the <code>SPS/PPS</code>. How can I distinguish them? Okay the <code>SPS/PPS</code> frames are usually way smaller, but that's not a save call to rely on. Because if I ignore them I need to know which data I can throw away, or if I need to write them I need to put the <code>00 00 01</code> Prefix in front of them. ? Or is it a fixed rule that they occur every 8th Video Frame?</p>