(original) parse out H264 and AAC from mp4,flv file, decoder failed

Source: Internet
Author: User

  Reprint Please specify source :http://www.cnblogs.com/lihaiping/p/5285166.html

Today in the local file decoding test, found from the mp4,flv file read out the frame data, H264 and AAC frame Direct decoder decoding, found that the decoding failed, but the file on the PC with Ffplay and VLC can be played, And this test video file is used Ffmpeg.exe to transcode out, so there should be no decoding problem, the problem?

Baidu, someone on the internet said that MP4 files in the package of H264 have two formats: H264 and AVC1:

The differences between the two formats are:

AVC1 Description: H. bitstream without start codes. The video generated by the FFmpeg transcoding is not 0x00000001 with the starting code.
H264 Description: H. bitstream with start codes. Generally, the suppression format of Hdvd and other films is with the starting code 0x00000001.

So I used VLC play to see the next, the original really is AVC1, this only found that they have been exposed to so long stream media, even avc1 have not heard, hey, sad.

So how do we decode it? At the same time I looked at the ffmpeg in the h264 of the AVC1 did not set a separate decoder, only a h264 decoder Codeid, then it is how to implement decoding AVC1? And how do you differentiate it?

Question 1: How do I decode it?

Here in fact, someone has the same problem as me: Http://stackoverflow.com/questions/11330764/ffmpeg-cant-decode-h264-stream-frame-data

At the same time, there are people in the forum to discuss how to solve: http://bbs.csdn.net/topics/390538510

Some people say that AVC1 is the original NAL packaging format, that is, the beginning of a number of bytes (1,2,4 bytes) is the length of nal, rather than Start_code, at this time must rely on a global data to obtain the encoder Profile,level,pps,sps and other information can be decoded.

In that case, how can I get pps,sps and other information? Someone wrote: http://blog.csdn.net/gavinr/article/details/7183499, in this article, you need to pay attention to a few of the new places that were not carefully understood before:

    1) PPS and SPS are not available from packet, but are stored in the Avcodeccontext extradata data domain

    2) How to resolve the SPS and PPS from the Extradata? A flow Filter "H264_mp4toannexb" is available in the FFmpeg to complete

The workaround is: Use the H264_mp4toannexb flow filter provided by FFmpeg to resolve: The specific method can be consulted: http://blog.csdn.net/leixiaohua1020/article/details/11800877

The problem 2:ffmpeg does not use a separate decoder for AVC1, but instead uses the same decoder as the h264? So how does it make a distinction?

This problem, you need to carefully look at the FFmpeg source code, in the Ff_h264_decode_init function has such a piece of code:

  

if 0 && avctx->extradata) {        ff_h264_decode_extradata (H, Avctx->extradata, avctx->extradata_size);         if 0 ) {            ff_h264_free_context (h);             return ret;        }    }

Keep looking down and see what you've done in the Ff_h264_decode_extradata function.

intFf_h264_decode_extradata (H264context *h,Constuint8_t *buf,intsize) {Avcodeccontext*avctx = h->Avctx; intret; if(!buf | | size <=0)        return-1; if (buf[0] = = 1) {        intI, CNT, nalsize; ConstUnsignedChar*p =buf; h ->IS_AVC = 1; if(Size <7) {Av_log (Avctx, Av_log_error,"AvcC%d too short\n", size); returnAverror_invaliddata; }        /*SPS and PPS in the AvcC always has length coded with 2 bytes, * So put a fake nal_length_size = 2 while pars ing them*/h->nal_length_size =2; //Decode SPS from AvcCCNT = * (P +5) &0x1f;//Number of SPSp + =6;  for(i =0; I < CNT; i++) {nalsize= Av_rb16 (P) +2; if(Nalsize > Size-(P-buf)) returnAverror_invaliddata; RET= Decode_nal_units (h, p, Nalsize,1); if(Ret <0) {Av_log (Avctx, Av_log_error,"decoding SPS%d from AvcC failed\n", i); returnret; } P+=nalsize; }        //Decode pps from AvcCCNT = * (p++);//Number of pps         for(i =0; I < CNT; i++) {nalsize= Av_rb16 (P) +2; if(Nalsize > Size-(P-buf)) returnAverror_invaliddata; RET= Decode_nal_units (h, p, Nalsize,1); if(Ret <0) {Av_log (Avctx, Av_log_error,"decoding pps%d from AvcC failed\n", i); returnret; } P+=nalsize; }        //Store right nal length size that would be used to parse all other nalsH->nal_length_size = (buf[4] &0x03) +1; } Else{h-&GT;IS_AVC =0; RET= Decode_nal_units (h, buf, size,1); if(Ret <0)            returnret; }    returnsize;}

So again here H264 decoder is through the Avcodeccontext extradata in the ff_h264_decode_init time, the distinction, but also the initial resolution code.

=================================================================

Problem with AAC decoding failure:

http://blog.csdn.net/leixiaohua1020/article/details/39767055 This article says that the av Splitter (Demuxer) does not apply to some formats. There is no problem with MP3 encoded audio. However, in the separation of mp4/flv/mkv and other formats of AAC encoded stream, the resulting AAC stream can not be played. The reason is that the data in the Avpacket data field that stores the AAC is not a "header" data that contains a 7-byte Adts file header, and it cannot be decoded directly (if, of course, it can be played by manually adding a 7-byte Adts file header before the data).

Adts is a new concept, how to solve? Google is not asked Niang, http://blog.csdn.net/tx3344/article/details/7414543, this article introduced the concept of Adts.

Originally wanted to solve with h264 AVC1 solution, but found that using AAC_ADTSTOASC flow filter is not feasible, because he has been returned 0, so I looked at the source of this function in FFmpeg, the original source of this function is to return 0. Test results are also not decoded.

Later found an article: http://blog.chinaunix.net/uid-24922718-id-3692670.html, originally wanted to refer to the method in this implementation:

                Charbits[7] = {0}; intSample_index =0, channel =0; Chartemp =0; intLength =7+audiopack.size; Sample_index= (audiocodecctx->extradata[0] &0x07) <<1; Temp= (audiocodecctx->extradata[1]&0x80); Switch(audiocodecctx->sample_rate) {                     Case 44100: {sample_index=0x7; } Break; default: {sample_index= Sample_index + (temp>>7); } Break; } Channel= ((audiocodecctx->extradata[1]-Temp) &0xFF) >>3; bits[0] =0xFF; bits[1] =0xf1; bits[2] =0x40| (sample_index<<2) | (channel>>2); bits[3] = ((channel&0x3) <<6) | (Length >> One); bits[4] = (length>>3) &0xFF; bits[5] = ((length<<5) &0xFF) |0x1f; bits[6] =0XFC; Fwrite (Bits,1,7, f);

The result has failed, adding these own Adts head still the same cannot decode the data.

Finally reference http://blog.itpub.net/30168498/viewspace-1576794/ This article code, carries on the application, smoothly decodes the AAC.

  

(original) parse out H264 and AAC from mp4,flv file, decoder failed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.