关于FFMPEG里读取的h264的AVPACKET存储nalu成为h264问题

转载http://blog.csdn.net/leixiaohua1020/article/details/11800877经过试验确实是这样

      在使用FFMPEG的类库进行编程的过程中,可以直接输出解复用之后的的视频数据码流。只需要在每次调用av_read_frame()之后将得到的视频的AVPacket存为本地文件即可。

经试验,在分离MPEG2码流的时候,直接存储AVPacket即可。

在分离H.264码流的时候,直接存储AVPacket后的文件可能是不能播放的。

如果视音频复用格式是TS(MPEG2 Transport Stream),直接存储后的文件是可以播放的。

复用格式是FLV,MP4则不行。

经过长时间资料搜索发现,FLV,MP4这些属于“特殊容器”,需要经过以下处理才能得到可播放的H.264码流:

1.第一次存储AVPacket之前需要在前面加上H.264的SPS和PPS。这些信息存储在AVCodecContext的extradata里面。

并且需要使用FFMPEG中的名为"h264_mp4toannexb"的bitstream filter 进行处理。

然后将处理后的extradata存入文件

具体代码如下:

[cpp]  view plain copy
  1. FILE *fp=fopen("test.264","ab");
  2. AVCodecContext *pCodecCtx=...  
[cpp]  view plain copy
  1. unsigned char *dummy=NULL;   //输入的指针  
  2. int dummy_len;  
  3. AVBitStreamFilterContext* bsfc =  av_bitstream_filter_init("h264_mp4toannexb");    
  4. av_bitstream_filter_filter(bsfc, pCodecCtx, NULL, &dummy, &dummy_len, NULL, 0, 0);  
  5. fwrite(pCodecCtx->extradata,pCodecCtx-->extradata_size,1,fp);  
  6. av_bitstream_filter_close(bsfc);    
  7. free(dummy);  

2.通过查看FFMPEG源代码我们发现,AVPacket中的数据起始处没有分隔符(0x00000001), 也不是0x65、0x67、0x68、0x41等字节,所以可以AVPacket肯定这不是标准的nalu。其实,AVPacket前4个字表示的是nalu的长度,从第5个字节开始才是nalu的数据。所以直接将AVPacket前4个字节替换为0x00000001即可得到标准的nalu数据。

具体代码如下:

[cpp]  view plain copy
  1. char nal_start[]={0,0,0,1};  
  2. fwrite(nal_start,4,1,fp);  
  3. fwrite(pkt->data+4,pkt->size-4,1,fp);  
  4. fclose(fp);  

经过以上两步处理之后,我们就得到了可以正常播放的H.264码流

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用FFmpeg读取RTP H.264视频流并解码的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <fcntl.h> #include <sys/time.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/avutil.h> #include <libavutil/opt.h> #include <libavutil/imgutils.h> #include <libswscale/swscale.h> } int main(int argc, char **argv) { AVCodec *codec = NULL; AVCodecContext *codec_ctx = NULL; AVPacket packet; AVFrame *frame = NULL; int ret, got_frame; int frame_count = 0; int video_width, video_height; struct timeval start_time, end_time; if (argc < 2) { printf("Usage: %s <rtp_address>\n", argv[0]); return -1; } avcodec_register_all(); av_register_all(); avformat_network_init(); // 打开RTP流并读取视频流信息 AVFormatContext *format_ctx = NULL; if (avformat_open_input(&format_ctx, argv[1], NULL, NULL) != 0) { printf("Couldn't open input file\n"); return -1; } if (avformat_find_stream_info(format_ctx, NULL) < 0) { printf("Couldn't find stream information\n"); return -1; } // 查找视频流,并初始化解码器 int video_stream_index = -1; for (int i = 0; i < format_ctx->nb_streams; i++) { if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; codec_ctx = avcodec_alloc_context3(codec); avcodec_parameters_to_context(codec_ctx, format_ctx->streams[i]->codecpar); codec = avcodec_find_decoder(codec_ctx->codec_id); if (!codec) { printf("Unsupported codec\n"); return -1; } if (avcodec_open2(codec_ctx, codec, NULL) < 0) { printf("Could not open codec\n"); return -1; } video_width = codec_ctx->width; video_height = codec_ctx->height; break; } } // 初始化AVFrame并分配内存 frame = av_frame_alloc(); if (!frame) { printf("Could not allocate frame\n"); return -1; } gettimeofday(&start_time, NULL); // 读取RTP流并解码 while (1) { ret = av_read_frame(format_ctx, &packet); if (ret < 0) { printf("Error reading packet\n"); break; } if (packet.stream_index == video_stream_index) { ret = avcodec_send_packet(codec_ctx, &packet); if (ret < 0) { printf("Error sending packet for decoding\n"); break; } while (ret >= 0) { ret = avcodec_receive_frame(codec_ctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { printf("Error during decoding\n"); break; } printf("Decoded frame %d\n", frame_count++); // 在这可以处理解码后的帧,例如渲染到屏幕上 } } av_packet_unref(&packet); } gettimeofday(&end_time, NULL); double elapsed_time = (end_time.tv_sec - start_time.tv_sec) + (end_time.tv_usec - start_time.tv_usec) / 1000000.0; printf("Decoded %d frames in %f seconds (average fps: %f)\n", frame_count, elapsed_time, frame_count / elapsed_time); avcodec_free_context(&codec_ctx); avformat_close_input(&format_ctx); av_frame_free(&frame); return 0; } ``` 这段代码打开指定的RTP流并读取视频流信息,查找视频流并初始化解码器,然后循环读取RTP流并解码每个视频帧。在解码每个帧后,您可以将其渲染到屏幕上或进行其他处理。最后,它将打印解码帧的数量和解码时间,然后释放所有资源。 请注意,为了简化代码,这个示例忽略了错误处理和内存释放。在实际应用中,您需要确保正确地处理和释放所有资源,以避免内存泄漏和其他问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值