winlin

avc/aac demux the flv samples

@@ -28,12 +28,14 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -28,12 +28,14 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <srs_core_error.hpp> 28 #include <srs_core_error.hpp>
29 #include <srs_core_stream.hpp> 29 #include <srs_core_stream.hpp>
30 #include <srs_core_log.hpp> 30 #include <srs_core_log.hpp>
  31 +#include <srs_core_autofree.hpp>
31 32
32 SrsCodec::SrsCodec() 33 SrsCodec::SrsCodec()
33 { 34 {
34 width = 0; 35 width = 0;
35 height = 0; 36 height = 0;
36 duration = 0; 37 duration = 0;
  38 + NAL_unit_length = 0;
37 frame_rate = 0; 39 frame_rate = 0;
38 video_data_rate = 0; 40 video_data_rate = 0;
39 video_codec_id = 0; 41 video_codec_id = 0;
@@ -60,7 +62,7 @@ SrsCodec::~SrsCodec() @@ -60,7 +62,7 @@ SrsCodec::~SrsCodec()
60 srs_freep(stream); 62 srs_freep(stream);
61 } 63 }
62 64
63 -int SrsCodec::parse_audio_codec(int8_t* data, int size) 65 +int SrsCodec::audio_aac_demux(int8_t* data, int size)
64 { 66 {
65 int ret = ERROR_SUCCESS; 67 int ret = ERROR_SUCCESS;
66 68
@@ -115,6 +117,7 @@ int SrsCodec::parse_audio_codec(int8_t* data, int size) @@ -115,6 +117,7 @@ int SrsCodec::parse_audio_codec(int8_t* data, int size)
115 } 117 }
116 } else if (aac_packet_type == SrsCodecAudioTypeRawData) { 118 } else if (aac_packet_type == SrsCodecAudioTypeRawData) {
117 // Raw AAC frame data in UI8 [] 119 // Raw AAC frame data in UI8 []
  120 + // 6.3 Raw Data, aac-iso-13818-7.pdf, page 28
118 } else { 121 } else {
119 // ignored. 122 // ignored.
120 } 123 }
@@ -125,7 +128,7 @@ int SrsCodec::parse_audio_codec(int8_t* data, int size) @@ -125,7 +128,7 @@ int SrsCodec::parse_audio_codec(int8_t* data, int size)
125 return ret; 128 return ret;
126 } 129 }
127 130
128 -int SrsCodec::parse_video_codec(int8_t* data, int size) 131 +int SrsCodec::video_avc_demux(int8_t* data, int size)
129 { 132 {
130 int ret = ERROR_SUCCESS; 133 int ret = ERROR_SUCCESS;
131 134
@@ -177,9 +180,62 @@ int SrsCodec::parse_video_codec(int8_t* data, int size) @@ -177,9 +180,62 @@ int SrsCodec::parse_video_codec(int8_t* data, int size)
177 avc_extra_data = new char[avc_extra_size]; 180 avc_extra_data = new char[avc_extra_size];
178 memcpy(avc_extra_data, data + stream->pos(), avc_extra_size); 181 memcpy(avc_extra_data, data + stream->pos(), avc_extra_size);
179 } 182 }
  183 +
  184 + if (!stream->require(6)) {
  185 + ret = ERROR_HLS_DECODE_ERROR;
  186 + srs_error("hls decode video avc sequenc header failed. ret=%d", ret);
  187 + return ret;
  188 + }
  189 + //int8_t configurationVersion = stream->read_1bytes();
  190 + //int8_t AVCProfileIndication = stream->read_1bytes();
  191 + //int8_t profile_compatibility = stream->read_1bytes();
  192 + //int8_t AVCLevelIndication = stream->read_1bytes();
  193 + stream->skip(4);
  194 + // parse the NALU size.
  195 + int8_t lengthSizeMinusOne = stream->read_1bytes();
  196 + lengthSizeMinusOne &= 0x03;
  197 + NAL_unit_length = lengthSizeMinusOne;
  198 + /**
  199 + * skip the following:
  200 + * numOfSequenceParameterSets
  201 + * donot parse the following:
  202 + * sequenceParameterSetLength
  203 + * sequenceParameterSetNALUnit
  204 + * numOfPictureParameterSets
  205 + * pictureParameterSetLength
  206 + * pictureParameterSetNALUnit
  207 + */
180 } else if (avc_packet_type == SrsCodecVideoAVCTypeNALU){ 208 } else if (avc_packet_type == SrsCodecVideoAVCTypeNALU){
181 // One or more NALUs (Full frames are required) 209 // One or more NALUs (Full frames are required)
182 // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 20 210 // 5.3.4.2.1 Syntax, H.264-AVC-ISO_IEC_14496-15.pdf, page 20
  211 + int PictureLength = size - stream->pos();
  212 + for (int i = 0; i < PictureLength;) {
  213 + if (!stream->require(NAL_unit_length + 1)) {
  214 + ret = ERROR_HLS_DECODE_ERROR;
  215 + srs_error("hls decode video avc NALU size failed. ret=%d", ret);
  216 + return ret;
  217 + }
  218 + int32_t NALUnitLength = 0;
  219 + if (NAL_unit_length == 3) {
  220 + NALUnitLength = stream->read_4bytes();
  221 + } else if (NALUnitLength == 2) {
  222 + NALUnitLength = stream->read_3bytes();
  223 + } else if (NALUnitLength == 1) {
  224 + NALUnitLength = stream->read_2bytes();
  225 + } else {
  226 + NALUnitLength = stream->read_1bytes();
  227 + }
  228 + // NALUnit
  229 + if (!stream->require(NALUnitLength)) {
  230 + ret = ERROR_HLS_DECODE_ERROR;
  231 + srs_error("hls decode video avc NALU data failed. ret=%d", ret);
  232 + return ret;
  233 + }
  234 + // 7.3.1 NAL unit syntax, H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
  235 + stream->skip(NALUnitLength);
  236 +
  237 + i += NAL_unit_length + 1 + NALUnitLength;
  238 + }
183 } else { 239 } else {
184 // ignored. 240 // ignored.
185 } 241 }
@@ -174,13 +174,17 @@ public: @@ -174,13 +174,17 @@ public:
174 */ 174 */
175 // @see: SrsCodecVideo 175 // @see: SrsCodecVideo
176 int video_codec_id; 176 int video_codec_id;
177 - u_int8_t profile; // profile_idc, page 45.  
178 - u_int8_t level; // level_idc, page 45. 177 + // profile_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
  178 + u_int8_t profile;
  179 + // level_idc, H.264-AVC-ISO_IEC_14496-10.pdf, page 45.
  180 + u_int8_t level;
179 int width; 181 int width;
180 int height; 182 int height;
181 int video_data_rate; // in bps 183 int video_data_rate; // in bps
182 int frame_rate; 184 int frame_rate;
183 int duration; 185 int duration;
  186 + // lengthSizeMinusOne, H.264-AVC-ISO_IEC_14496-15.pdf, page 16
  187 + int8_t NAL_unit_length;
184 /** 188 /**
185 * audio specified 189 * audio specified
186 */ 190 */
@@ -208,8 +212,8 @@ public: @@ -208,8 +212,8 @@ public:
208 virtual ~SrsCodec(); 212 virtual ~SrsCodec();
209 // the following function used for hls to build the codec info. 213 // the following function used for hls to build the codec info.
210 public: 214 public:
211 - virtual int parse_audio_codec(int8_t* data, int size);  
212 - virtual int parse_video_codec(int8_t* data, int size); 215 + virtual int audio_aac_demux(int8_t* data, int size);
  216 + virtual int video_avc_demux(int8_t* data, int size);
213 // the following function used to finger out the flv/rtmp packet detail. 217 // the following function used to finger out the flv/rtmp packet detail.
214 public: 218 public:
215 /** 219 /**
@@ -134,7 +134,7 @@ int SrsHLS::on_audio(SrsCommonMessage* audio) @@ -134,7 +134,7 @@ int SrsHLS::on_audio(SrsCommonMessage* audio)
134 { 134 {
135 int ret = ERROR_SUCCESS; 135 int ret = ERROR_SUCCESS;
136 136
137 - if ((ret = codec->parse_audio_codec(audio->payload, audio->size)) != ERROR_SUCCESS) { 137 + if ((ret = codec->audio_aac_demux(audio->payload, audio->size)) != ERROR_SUCCESS) {
138 return ret; 138 return ret;
139 } 139 }
140 140
@@ -145,7 +145,7 @@ int SrsHLS::on_video(SrsCommonMessage* video) @@ -145,7 +145,7 @@ int SrsHLS::on_video(SrsCommonMessage* video)
145 { 145 {
146 int ret = ERROR_SUCCESS; 146 int ret = ERROR_SUCCESS;
147 147
148 - if ((ret = codec->parse_video_codec(video->payload, video->size)) != ERROR_SUCCESS) { 148 + if ((ret = codec->video_avc_demux(video->payload, video->size)) != ERROR_SUCCESS) {
149 return ret; 149 return ret;
150 } 150 }
151 151