About the Avpacket storage of H264 read in ffmpeg Nalu becomes h264 problem

Source: Internet
Author: User


Reprint http://blog.csdn.net/leixiaohua1020/article/details/11800877 After the test is really like this



In the process of programming with the class library of FFmpeg, the video data stream after the re-use can be output directly. You only need to save the avpacket of the resulting video to a local file after each call to Av_read_frame ().



After the test, in the separation of MPEG2 code flow, the direct storage of avpacket can be.



The files that are stored directly after Avpacket may not be able to be played back when the code stream is detached.



If the AV multiplexing format is TS (MPEG2 Transport Stream), the files that are stored directly can be played back.



The reuse format is flv,mp4.



After a long time data search, Flv,mp4 these are "special containers" and need to undergo the following processing to get a playable H-coded stream:



1. Before storing the avpacket for the first time, you need to precede the SPS and PPS with H. This information is stored in the extradata of Avcodeccontext.



And it needs to be processed using the bitstream filter named "H264_mp4toannexb" in FFmpeg.



The processed extradata are then deposited into the file



The specific code is as follows:


FILE *fp=fopen("test.264","ab");
AVCodecContext *pCodecCtx=...  
unsigned char *dummy=NULL;   //Input pointer
int dummy_len;  
AVBitStreamFilterContext* bsfc =  av_bitstream_filter_init("h264_mp4toannexb");    
av_bitstream_filter_filter(bsfc, pCodecCtx, NULL, &dummy, &dummy_len, NULL, 0, 0);  
fwrite(pCodecCtx->extradata,pCodecCtx-->extradata_size,1,fp);  
av_bitstream_filter_close(bsfc);    
free(dummy);  




2. By looking at the FFmpeg source code, we find that there is no delimiter (0x00000001) at the beginning of the data in Avpacket, nor is it 0x65, 0x67, 0x68, 0x41, and so on, so you can avpacket that this is not a standard Nalu. In fact, avpacket the first 4 words is the length of the Nalu, starting from the 5th byte is the Nalu data. Therefore, the standard NALU data can be obtained directly by replacing the first 4 bytes of avpacket with the 0x00000001.



The specific code is as follows:


char nal_start[]={0,0,0,1};  
fwrite(nal_start,4,1,fp);  
fwrite(pkt->data+4,pkt->size-4,1,fp);  
fclose(fp); 



After the above two steps, we can get the normal playback of the H-code stream


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.