Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write RTP/H264 stream as a file

I couldn't find a solution for my previous question so I decided to try it step by step.

The thing that I wanna do now is to store the RTP/H264 stream as a file.

What I found so far is the below:

(firstly my RTP/H264 is FU-A which is the form of)

| RTP HEADER 12bytes long | FU INDICATOR 1byte | FU HEADER 1byte | FU payload |

As I understood RFC 6184 document, I'm starting a NAL with a packet which has '1' at the first bit of FU Header and appending following packets that set '0' at the first bit until the last packet which has '1' at the second bit of FU Header.

I think that's how to get a complete NAL before the FU-A packetization and also what I found is I need to put the 'starting bits' (0x00000001) at the front of each complete NAL.

But no luck so far. The below is part of log

 ========= the new NAL is as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]7C/1111100 [5]85/10000101 [6]B8/10111000 [7]40/1000000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]7C/1111100 [5]85/10000101 [6]B8/10111000 [7]40/1000000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]7C/1111100 [5]85/10000101 [6]B8/10111000 [7]40/1000000 ...
 ========= adding the next NAL as 716
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]7C/1111100 [5]85/10000101 [6]B8/10111000 [7]40/1000000 ...
 ========= a NAL is summed up as 4866
 ========= the new NAL is as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= adding the next NAL as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= adding the next NAL as 139
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E2/11100010 [7]20/100000 ...
 ========= a NAL is summed up as 7061
 ========= the new NAL is as 1377
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]41/1000001 [5]E4/11100100 [6]40/1000000 [7]1A/11010 ...
 ========= a NAL is summed up as 1369
 ========= the new NAL is as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E6/11100110 [7]60/1100000 ...
 ========= adding the next NAL as 94
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]E6/11100110 [7]60/1100000 ...
 ========= a NAL is summed up as 1472
 ========= the new NAL is as 447
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]41/1000001 [5]E8/11101000 [6]80/10000000 [7]16/10110 ...
 ========= a NAL is summed up as 439
 ========= the new NAL is as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]EA/11101010 [7]A0/10100000  ...
 ========= adding the next NAL as 1174
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]EA/11101010 [7]A0/10100000 ...
 ========= a NAL is summed up as 2552
 ========= the new NAL is as 1400
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]EC/11101100 [7]C0/11000000 ...
 ========= adding the next NAL as 1364
 [0]0/0 [1]0/0 [2]0/0 [3]1/1 [4]5C/1011100 [5]81/10000001 [6]EC/11101100 [7]C0/11000000 ...
 ========= a NAL is summed up as 2742
 ========= the new NAL is as 1400
 ...

my question is,

  1. if I can get the complete NALs from the fragmented packets by FU-A, how can I make it as a file which is able to run by VLC or other player?

  2. I'm still confused if I have to keep the FU indicator and FU header or not. Somebody said I need to take them only for the very first packet (starting with '1' at the FU header)

Any advice will be really appreciated.

Thanks.

like image 534
Jun Avatar asked Mar 06 '12 10:03

Jun


1 Answers

The FU, STAP, and MTAP NAL units are specific only to RTP packetization as they're designed to facilitate network transport. In other words, don't count on a decoder parsing them correctly. Ultimately, you need to reassemble NAL units as in the case of FU packets, or break them up in to multiple NAL units the case of STAP/MTAP.

Once you have a NAL unit (and this includes PPS, SPS, SEI, slice partitions, and all the other types in the 1-23 range), then you can write to disk along with the '0001' start codes per H.264 annex B.

Placing the H.264 annex B stream into a container such as MPEG-4 can be done with various command-line tools (I'm pretty sure ffmpeg can do it).

like image 151
Jeff Wallace Avatar answered Sep 18 '22 06:09

Jeff Wallace