参考文献 : 视音频数据处理入门:H.264视频码流解析

测试文件:H264文件

链接:https://pan.baidu.com/s/1eRTJwTsXTgHf2Ez8Inab1A 
提取码:1c7q 

原理

H.264原始码流(又称为“裸流”)是由一个一个的NALU组成的。他们的结构如下图所示。

其中每个NALU之间通过startcode(起始码)进行分隔,起始码分成两种:0x000001(3Byte)或者0x00000001(4Byte)。如果NALU对应的Slice为一帧的开始就用0x00000001,否则就用0x000001。
H.264码流解析的步骤就是首先从码流中搜索0x000001和0x00000001,分离出NALU;然后再分析NALU的各个字段。本文的程序即实现了上述的两个步骤。

代码

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;public class Test {//NALU结构static class NALU{int startcodeprefix_len;      //! 4 for parameter sets and first slice in picture, 3 for everything else (suggested)int len;                         //! Length of the NAL unit (Excluding the start code, which does not belong to the NALU)int max_size;                   //! Nal Unit Buffer sizeint forbidden_bit;            //! should be always FALSEint nal_reference_idc;        //! NALU_PRIORITY_xxxxint nal_unit_type;            //! NALU_TYPE_xxxx    @Overridepublic String toString() {return "NALU [len=" + len + ", forbidden_bit=" + forbidden_bit + ", nal_reference_idc=" + nal_reference_idc+ ", nal_unit_type=" + nal_unit_type + "]";}}private static RandomAccessFile in;private static List<NALU> list = new ArrayList<NALU>();private static List<Long> NALUIndexs = new ArrayList<>() ;public static void main(String[] args) throws Exception {String fileName = "F:/testFile/video/vid.h264";if(args.length>0) {fileName = args[0];}in = new RandomAccessFile(fileName, "r");parseIndexs();System.out.println(NALUIndexs);getNALU();System.out.println(list);in.close();}public static int parseNALU() throws IOException {int head = in.readInt();if(head==1) {//0x00000001?return 4;}else if(head>>1 == 1) {//0x000001?in.seek(in.getFilePointer()-1);return 3;}return -1;}/** 获取每一帧NALU 并存入集合*/public static void getNALU() throws IOException, InterruptedException {for(int i=0;i<NALUIndexs.size();i++) {NALU n = new NALU();if(i!=NALUIndexs.size()-1) {n.len = (int) (NALUIndexs.get(i+1)-NALUIndexs.get(i));}else {n.len = (int) (in.length() - NALUIndexs.get(i));}in.seek(NALUIndexs.get(i));int head = in.read();n.forbidden_bit = head >> 7 & 1;n.nal_reference_idc = head >> 5 & 3;n.nal_unit_type = head & 0x1f;list.add(n);}}/** 获取所有NAUL的起始位置*/public static void parseIndexs() throws IOException {while(true) {if(parseNALU()>0) {NALUIndexs.add(in.getFilePointer());}if(in.getFilePointer()==in.length()) {break;}in.seek(in.getFilePointer()-4);in.readByte();}}}

结果:

视音频数据处理入门:H.264视频码流解析(java)相关推荐

  1. 视音频数据处理入门:H.264视频码流解析

    ===================================================== 视音频数据处理入门系列文章:视音频数据处理入门:RGB.YUV像素数据处理视音频数据处理入门 ...

  2. H.264/H265码流解析

    H.264/H265码流解析 一.H.264码流解析 一个原始的H.264 NALU 单元常由 [StartCode] [NALU Header] [NALU Payload] 三部分组成 一个原始的 ...

  3. 【H.264】码流解析 annexb vs avcc

    H264码流解析及NALU AVCC和ANNEXB 前者是FLV容器.mp4 常用的. 后者 是实时传输使用,所以是TS 一类的标准. VLC显示AVC1就是AVCC AVCC格式 也叫AVC1格式, ...

  4. 视音频数据处理入门:AAC音频码流解析

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  5. 视音频数据处理入门:RGB、YUV像素数据处理【转】

    转自:http://blog.csdn.net/leixiaohua1020/article/details/50534150 ==================================== ...

  6. 视音频数据处理入门:UDP-RTP协议解析

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  7. 视音频数据处理入门:PCM音频采样数据处理

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

  8. H.264基础知识及视频码流解析

    H.264基础知识及视频码流解析 目录 H.264概述 H264相关概念 H264压缩方式 H264分层结构 H264码流结构 H264的NAL单元 H.264视频码流解析及代码实现 1. H.264 ...

  9. 视音频数据处理入门:RGB、YUV像素数据处理

    ===================================================== 视音频数据处理入门系列文章: 视音频数据处理入门:RGB.YUV像素数据处理 视音频数据处理 ...

最新文章

  1. 二、通过工厂方法来配置bean
  2. Linux查看内存使用情况
  3. 使用Android Studio运行项目出现 UnsupportedMethodException 错误的解决办法
  4. 【Kafka】Kafka No serviceName defined in either JAAS or Kafka config
  5. python之集合操作
  6. 如何做相册_今天才知道,原来长按微信相册,还隐藏着一个实用功能
  7. Vue中watch监听数据变化以及watch中各属性详解
  8. C++设计模式 - 适配器模式(Adapter)
  9. 百度API查询经纬度小页面
  10. Netty 的零拷贝
  11. 手机对红外探头发送数据和接受
  12. 计算机科学技术职业道德,计算机职业道德与学术技能
  13. 10019---CSS Grouping Selectors(分组和嵌套)
  14. 将doc文档生成html页面
  15. Uncaught TypeError: date.getDay is not a function at getDate
  16. Word打开文件时,提示文件扩展名和文件格式不匹配,无法打开
  17. 个人中心页面的UI设计知识点
  18. 程序员,请不要天天加班
  19. RIoTBoard开发板系列笔记(三)—— 移植Gstreamer
  20. win7文件共享(四步)

热门文章

  1. 白话firefox扩展插件权限索取的说明(专业版)
  2. VLC插件在浏览器下(IE11)实现播放rtsp视频直播流
  3. 计算机二级MS选择题题库百度云,计算机二级msoffice题库选择题集(精选版).pdf
  4. 2021-Java面试题(03-29已更新)
  5. 路由器、交换机、集线器三剑客有什么区别?分别是用来干什么的?
  6. 简单循迹小车实验心得_新人刚刚接触单片机,求教一个简单的循迹小车程序(实在看不懂)...
  7. usb驱动的基本结构和函数简介
  8. OpenWRT学习笔记-45 mt76x2e闭源驱动移植(上)
  9. docker安装python依赖包
  10. 判断445端口是否已经关闭的方法