从音视频文件中读取数据,抽取其中的h264视频数据,并保存在文件中,如果想要此文件被播放器正常解码播放,还需要添加在每个帧之前添加start code,在每个关键帧前添加sps/pps。
播放器需要知道一帧的开始位置和起始位置。
start code是一个四个字节的标识码,一般是0x 00 00 00 01,如果这帧有关键帧可能是三个字节,比如0x 00 00 01用于区分关键帧,此时后面还会跟几个字节的sps和pps数据。
pps和sps中含有视频的参数,比如宽高等,如果是以文件存储,可以只含一次就可以。当视频格参数改变时,在改变之前再添加一个pps和sps即可。
但如果要网络传输的话,考虑到丢帧,因此在每个关键帧前都需要加入sps和pps。
因为sps和pps只有几个字节,并不会增加网络负载,因此不管是文件还是实时流,一般都会在每个关键帧前加入sps和pps。
从文件中分离视频数据并单独保存在一个文件中。当从文件中读取好一个h264 packet时,在写入文件前,在每帧前加入start code,在关键帧前加入sps和pps,代码如下。
推荐一些音视频免费讲解,笔者听完了,nice!
连接

#include <stdio.h>
#include <libavutil/log.h>
#include <libavformat/avio.h>
#include <libavformat/avformat.h>#ifndef AV_WB32
#   define AV_WB32(p, val) do {                 \uint32_t d = (val);                     \((uint8_t*)(p))[3] = (d);               \((uint8_t*)(p))[2] = (d)>>8;            \((uint8_t*)(p))[1] = (d)>>16;           \((uint8_t*)(p))[0] = (d)>>24;           \} while(0)
#endif#ifndef AV_RB16
#   define AV_RB16(x)                           \((((const uint8_t*)(x))[0] << 8) |          \((const uint8_t*)(x))[1])
#endifstatic int alloc_and_copy(AVPacket *out,const uint8_t *sps_pps, uint32_t sps_pps_size,const uint8_t *in, uint32_t in_size)
{uint32_t offset         = out->size;uint8_t nal_header_size = offset ? 3 : 4;int err;err = av_grow_packet(out, sps_pps_size + in_size + nal_header_size);if (err < 0)return err;if (sps_pps)memcpy(out->data + offset, sps_pps, sps_pps_size);memcpy(out->data + sps_pps_size + nal_header_size + offset, in, in_size);if (!offset) {AV_WB32(out->data + sps_pps_size, 1);} else {(out->data + offset + sps_pps_size)[0] =(out->data + offset + sps_pps_size)[1] = 0;(out->data + offset + sps_pps_size)[2] = 1;}return 0;
}int h264_extradata_to_annexb(const uint8_t *codec_extradata, const int codec_extradata_size, AVPacket *out_extradata, int padding)
{uint16_t unit_size;uint64_t total_size                 = 0;uint8_t *out                        = NULL, unit_nb, sps_done = 0,sps_seen                   = 0, pps_seen = 0, sps_offset = 0, pps_offset = 0;const uint8_t *extradata            = codec_extradata + 4;static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };int length_size = (*extradata++ & 0x3) + 1; // retrieve length coded size, 用于指示表示编码数据长度所需字节数sps_offset = pps_offset = -1;/* retrieve sps and pps unit(s) */unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */if (!unit_nb) {goto pps;}else {sps_offset = 0;sps_seen = 1;}while (unit_nb--) {int err;unit_size   = AV_RB16(extradata);total_size += unit_size + 4;if (total_size > INT_MAX - padding) {av_log(NULL, AV_LOG_ERROR,"Too big extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");av_free(out);return AVERROR(EINVAL);}if (extradata + 2 + unit_size > codec_extradata + codec_extradata_size) {av_log(NULL, AV_LOG_ERROR, "Packet header is not contained in global extradata, ""corrupted stream or invalid MP4/AVCC bitstream\n");av_free(out);return AVERROR(EINVAL);}if ((err = av_reallocp(&out, total_size + padding)) < 0)return err;memcpy(out + total_size - unit_size - 4, nalu_header, 4);memcpy(out + total_size - unit_size, extradata + 2, unit_size);extradata += 2 + unit_size;
pps:if (!unit_nb && !sps_done++) {unit_nb = *extradata++; /* number of pps unit(s) */if (unit_nb) {pps_offset = total_size;pps_seen = 1;}}}if (out)memset(out + total_size, 0, padding);if (!sps_seen)av_log(NULL, AV_LOG_WARNING,"Warning: SPS NALU missing or invalid. ""The resulting stream may not play.\n");if (!pps_seen)av_log(NULL, AV_LOG_WARNING,"Warning: PPS NALU missing or invalid. ""The resulting stream may not play.\n");out_extradata->data      = out;out_extradata->size      = total_size;return length_size;
}int h264_mp4toannexb(AVFormatContext *fmt_ctx, AVPacket *in, FILE *dst_fd)
{AVPacket *out = NULL;AVPacket spspps_pkt;int len;uint8_t unit_type;int32_t nal_size;uint32_t cumul_size    = 0;const uint8_t *buf;const uint8_t *buf_end;int            buf_size;int ret = 0, i;out = av_packet_alloc();buf      = in->data;buf_size = in->size;buf_end  = in->data + in->size;do {ret= AVERROR(EINVAL);if (buf + 4 /*s->length_size*/ > buf_end)goto fail;for (nal_size = 0, i = 0; i<4/*s->length_size*/; i++)nal_size = (nal_size << 8) | buf[i];buf += 4; /*s->length_size;*/unit_type = *buf & 0x1f;if (nal_size > buf_end - buf || nal_size < 0)goto fail;/*if (unit_type == 7)s->idr_sps_seen = s->new_idr = 1;else if (unit_type == 8) {s->idr_pps_seen = s->new_idr = 1;*//* if SPS has not been seen yet, prepend the AVCC one to PPS *//*if (!s->idr_sps_seen) {if (s->sps_offset == -1)av_log(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n");else {if ((ret = alloc_and_copy(out,ctx->par_out->extradata + s->sps_offset,s->pps_offset != -1 ? s->pps_offset : ctx->par_out->extradata_size - s->sps_offset,buf, nal_size)) < 0)goto fail;s->idr_sps_seen = 1;goto next_nal;}}}*//* if this is a new IDR picture following an IDR picture, reset the idr flag.* Just check first_mb_in_slice to be 0 as this is the simplest solution.* This could be checking idr_pic_id instead, but would complexify the parsing. *//*if (!s->new_idr && unit_type == 5 && (buf[1] & 0x80))s->new_idr = 1;*//* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */if (/*s->new_idr && */unit_type == 5 /*&& !s->idr_sps_seen && !s->idr_pps_seen*/) {h264_extradata_to_annexb( fmt_ctx->streams[in->stream_index]->codec->extradata,fmt_ctx->streams[in->stream_index]->codec->extradata_size,&spspps_pkt,AV_INPUT_BUFFER_PADDING_SIZE);if ((ret=alloc_and_copy(out,spspps_pkt.data, spspps_pkt.size,buf, nal_size)) < 0)goto fail;/*s->new_idr = 0;*//* if only SPS has been seen, also insert PPS */}/*else if (s->new_idr && unit_type == 5 && s->idr_sps_seen && !s->idr_pps_seen) {if (s->pps_offset == -1) {av_log(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n");if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)goto fail;} else if ((ret = alloc_and_copy(out,ctx->par_out->extradata + s->pps_offset, ctx->par_out->extradata_size - s->pps_offset,buf, nal_size)) < 0)goto fail;}*/ else {if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)goto fail;/*if (!s->new_idr && unit_type == 1) {s->new_idr = 1;s->idr_sps_seen = 0;s->idr_pps_seen = 0;}*/}len = fwrite( out->data, 1, out->size, dst_fd);if(len != out->size){av_log(NULL, AV_LOG_DEBUG, "warning, length of writed data isn't equal pkt.size(%d, %d)\n",len,out->size);}fflush(dst_fd);next_nal:buf        += nal_size;cumul_size += nal_size + 4;//s->length_size;} while (cumul_size < buf_size);/*ret = av_packet_copy_props(out, in);if (ret < 0)goto fail;*/
fail:av_packet_free(&out);return ret;
}int main(int argc, char *argv[])
{int err_code;char errors[1024];char *src_filename = NULL;char *dst_filename = NULL;FILE *dst_fd = NULL;int video_stream_index = -1;//AVFormatContext *ofmt_ctx = NULL;//AVOutputFormat *output_fmt = NULL;//AVStream *out_stream = NULL;AVFormatContext *fmt_ctx = NULL;AVPacket pkt;//AVFrame *frame = NULL;av_log_set_level(AV_LOG_DEBUG);if(argc < 3){av_log(NULL, AV_LOG_DEBUG, "the count of parameters should be more than three!\n");return -1;}src_filename = argv[1];dst_filename = argv[2];if(src_filename == NULL || dst_filename == NULL){av_log(NULL, AV_LOG_ERROR, "src or dts file is null, plz check them!\n");return -1;}/*register all formats and codec*/av_register_all();dst_fd = fopen(dst_filename, "wb");if (!dst_fd) {av_log(NULL, AV_LOG_DEBUG, "Could not open destination file %s\n", dst_filename);return -1;}/*open input media file, and allocate format context*/if((err_code = avformat_open_input(&fmt_ctx, src_filename, NULL, NULL)) < 0){av_strerror(err_code, errors, 1024);av_log(NULL, AV_LOG_DEBUG, "Could not open source file: %s, %d(%s)\n",src_filename,err_code,errors);return -1;}/*dump input information*/av_dump_format(fmt_ctx, 0, src_filename, 0);/*initialize packet*/av_init_packet(&pkt);pkt.data = NULL;pkt.size = 0;/*find best video stream*/video_stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);if(video_stream_index < 0){av_log(NULL, AV_LOG_DEBUG, "Could not find %s stream in input file %s\n",av_get_media_type_string(AVMEDIA_TYPE_VIDEO),src_filename);return AVERROR(EINVAL);}/*if (avformat_write_header(ofmt_ctx, NULL) < 0) {av_log(NULL, AV_LOG_DEBUG, "Error occurred when opening output file");exit(1);}*//*read frames from media file*/while(av_read_frame(fmt_ctx, &pkt) >=0 ){if(pkt.stream_index == video_stream_index){/*pkt.stream_index = 0;av_write_frame(ofmt_ctx, &pkt);av_free_packet(&pkt);*/h264_mp4toannexb(fmt_ctx, &pkt, dst_fd);}//release pkt->dataav_packet_unref(&pkt);}//av_write_trailer(ofmt_ctx);/*close input media file*/avformat_close_input(&fmt_ctx);if(dst_fd) {fclose(dst_fd);}//avio_close(ofmt_ctx->pb);return 0;
}

给h264帧增加start code和sps/pps相关推荐

  1. 【Android RTMP】x264 编码器初始化及设置 ( 获取 x264 编码参数 | 编码规格 | 码率 | 帧率 | B帧个数 | 关键帧间隔 | 关键帧解码数据 SPS PPS )

    文章目录 安卓直播推流专栏博客总结 一. x264 编码器参数设置引入 二. 获取 x264 编码器参数 三. 设置 x264 编码器编码规格 四. 设置 x264 编码器编码图像数据格式 五. 设置 ...

  2. h264 sei信息 解码_关于H264编码数据中SPS,PPS,SEI,IDR等内容的问题

    群内的朋友们好! 我使用的平台是ipnc rdk 3.8 , 我在h264编码的有关NALU参数设置如下: staticParams->nalUnitControlParams.naluCont ...

  3. FFmpeg SPS/PPS剖析

    场景说明             在解码过程中,需要设置SPS/PPS等解码信息,才能够初始化解码器.有两种方式可以设置SPS/PPS,一种是手动指定SPS/PPS内容,指定AVCodecContex ...

  4. H264帧的分析sps pps

    帧格式 H264帧由NALU头和NALU主体组成. NALU头由一个字节组成,它的语法如下: +---------------+       |0|1|2|3|4|5|6|7|       +-+-+ ...

  5. H264 帧、pps 、sps

    H264帧 对于H.264而言,每帧的界定符为00 00 00 01 或者00 00 01. 例如下面是一个H264的文件片段 00 00 00 01 67 42 C0 28 DA 01 E0 08 ...

  6. 音视频学习-H264帧基础知识

    一组图像 GOP 所谓GOP就是1组图像Group of Picture,在这一组图像中有且只有1个I帧,多个P帧或B帧,两个I帧之间的帧数,就是一个GOP. GOP一般设置为编码器每秒输出的帧数,即 ...

  7. H264—帧,片,参数集,NALU等概念

    h264是一个编码压缩的格式,可以使用x264库进行编码,源码开放,可下载编译使用. --------------------------------------------------------- ...

  8. RTP中H264封装NALU(SPS,PPS等)

    NAL的英文全称为Network  Abstract Layer,即网络抽象层,在H264/AVC视频编解码标准中,整个系统框架分为两个层面,视频编解码层面(VCL)和网络抽象层面(NAL).VCL负 ...

  9. 从nginx-rtmp中提取一帧h264帧

    摘要:一为什么要提取h264帧?  因为我们经常需要从事实流中截取一些画面,用于变动的封面,安全,鉴黄等用处.二从nginx_rtmp中怎么提取一帧h264帧呢?  前面我们讲过如何提取sps和pps ...

最新文章

  1. hash 建表 query 统计重复个数
  2. 微信url schema,deep link
  3. 谈谈我熟悉又陌生的cookie
  4. java ArrayList转数组
  5. 学典教育计算机二级,层次化分类的离线中文签名真伪鉴别方法-计算机工程与应用.PDF...
  6. yii2 关掉php notice,yii2关闭错误提示
  7. 【C语言】第八章 地址操作与指针 题解
  8. 深入浅出VC++串口编程--基于Win32 API
  9. 多个html页面拼接成一个页面_浏览器渲染页面机制以及如何减少DOM操作
  10. Windows下安装NetCat
  11. python爬取音乐源码_Python爬虫教程,爬取网易云的音乐
  12. selenium万能选择器
  13. Win10命令提示符快捷键汇总
  14. VMware Fusion 常用内容
  15. input标签的type属性汇总
  16. Ubuntu自己动手本地模拟搭建git服务器
  17. 嵌入式工程师,怎么不被历史洪流冲走?
  18. 开头的单词_c开头的英语单词三年级到六年级的英语单词记忆
  19. WebRTC 报错:Failed to set remote offer sdp: Called with SDP without DTLS fingerprint
  20. 解决python关于UnicodeEncodeError: 'gbk' codec can't encode character '\xa3'报错的问题

热门文章

  1. Datatable 插件出现DataTable is not a function 错误
  2. Week05手写笔记
  3. 【java】程序启动后, 可以从键盘输入接收多个整数, 直到输入quit时结束输入. 把所有输入的整数倒序排列打印.
  4. gerrit git 邮箱不匹配的问题
  5. 电赛知识补充——电机篇
  6. 发现一个好用的视频下载器(浏览器插件)Cococut
  7. 晨山资本王志飏:万物智联时代,智能企业的创新路径
  8. 【Java基础】NoClassDefFoundError 和 ClassNotFoundException的定义及其区别
  9. Hibernate学习(七)
  10. 逆袭之旅DAY.XIA.Object中常用方法