H264 获取SPS与PPS

2024-06-19 16:18
文章标签 获取 sps h264 pps

本文主要是介绍H264 获取SPS与PPS,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载地址

H264 获取SPS与PPS

在用Android手机进行h264硬编码的时候如果要进行视频流的实时传输与播放,就需要知道视频流的Sequence Parameter Sets (SPS) 和Picture Parameter Set (PPS)。

今天算是看明白如何获取SPS和PPS,在这里记录下来,希望有需要的朋友可以在这里获取到一些些的帮助。

首先说一下大前提,我设置的视频录制参数为:

mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

为了让大家更加明白,我先贴出avcC的数据结构:

    aligned(8) class AVCDecoderConfigurationRecord {  unsigned int(8) configurationVersion = 1;  unsigned int(8) AVCProfileIndication;  unsigned int(8) profile_compatibility;  unsigned int(8) AVCLevelIndication;  bit(6) reserved = '111111'b;  unsigned int(2) lengthSizeMinusOne;  bit(3) reserved = '111'b;  unsigned int(5) numOfSequenceParameterSets;  for (i=0; i< numOfSequenceParameterSets; i++) {  unsigned int(16) sequenceParameterSetLength ;  bit(8*sequenceParameterSetLength) sequenceParameterSetNALUnit;  }  unsigned int(8) numOfPictureParameterSets;  for (i=0; i< numOfPictureParameterSets; i++) {  unsigned int(16) pictureParameterSetLength;  bit(8*pictureParameterSetLength) pictureParameterSetNALUnit;  }  }  

ok,数据结构贴出来了,我再贴出录制的3gp输出类型的h264码流片断。

这里写图片描述

阴影部分就是avcC的全部数据了。

其中: 0x61 0x76 0x63 0x43 就是字符avcC

0x01 是configurationVersion

0x42 是AVCProfileIndication

0x00 是profile_compatibility

0x1F是AVCLevelIndication

0xFF 是6bit的reserved 和2bit的lengthSizeMinusOne

0xE1 是3bit的reserved 和5bit的numOfSequenceParameterSets

0x00 0x09是sps的长度为9个字节。

故SPS的内容为接下来的9个字节:67 42 00 1f e9 02 c1 2c 80

接下来的:01为numOfPictureParameterSets

0x00和0x04是pps的长度为4个字节。

故PPS的内容为接下来的4个字节:68 ce 06 f2

通过这段数据片断,就可以获取到SPS和PPS了。

下面我将贴上用java代码获取输出格式为3gp的h264码流的SPS与PPS代码:

    package cn.edu.xmu.zgy;  import java.io.File;  import java.io.FileInputStream;  import java.io.IOException;  public class ObtainSPSAndPPS {  public void getSPSAndPPS(String fileName) throws IOException {  File file = new File(fileName);  FileInputStream fis = new FileInputStream(file);  int fileLength = (int) file.length();  byte[] fileData = new byte[fileLength];  fis.read(fileData);  // 'a'=0x61, 'v'=0x76, 'c'=0x63, 'C'=0x43  byte[] avcC = new byte[] { 0x61, 0x76, 0x63, 0x43 };  // avcC的起始位置  int avcRecord = 0;  for (int ix = 0; ix < fileLength; ++ix) {  if (fileData[ix] == avcC[0] && fileData[ix + 1] == avcC[1]  && fileData[ix + 2] == avcC[2]  && fileData[ix + 3] == avcC[3]) {  // 找到avcC,则记录avcRecord起始位置,然后退出循环。  avcRecord = ix + 4;  break;  }  }  if (0 == avcRecord) {  System.out.println("没有找到avcC,请检查文件格式是否正确");  return;  }  // 加6的目的是为了跳过  // (1)8字节的 configurationVersion  // (2)8字节的 AVCProfileIndication  // (3)8字节的 profile_compatibility  // (4)8 字节的 AVCLevelIndication  // (5)6 bit 的 reserved  // (6)2 bit 的 lengthSizeMinusOne  // (7)3 bit 的 reserved  // (8)5 bit 的numOfSequenceParameterSets  // 共6个字节,然后到达sequenceParameterSetLength的位置  int spsStartPos = avcRecord + 6;  byte[] spsbt = new byte[] { fileData[spsStartPos],  fileData[spsStartPos + 1] };  int spsLength = bytes2Int(spsbt);  byte[] SPS = new byte[spsLength];  // 跳过2个字节的 sequenceParameterSetLength  spsStartPos += 2;  System.arraycopy(fileData, spsStartPos, SPS, 0, spsLength);  printResult("SPS", SPS, spsLength);  // 底下部分为获取PPS  // spsStartPos + spsLength 可以跳到pps位置  // 再加1的目的是跳过1字节的 numOfPictureParameterSets  int ppsStartPos = spsStartPos + spsLength + 1;  byte[] ppsbt = new byte[] { fileData[ppsStartPos],  fileData[ppsStartPos + 1] };  int ppsLength = bytes2Int(ppsbt);  byte[] PPS = new byte[ppsLength];  ppsStartPos += 2;  System.arraycopy(fileData, ppsStartPos, PPS, 0, ppsLength);  printResult("PPS", PPS, ppsLength);  }  private int bytes2Int(byte[] bt) {  int ret = bt[0];  ret <<= 8;  ret |= bt[1];  return ret;  }  private void printResult(String type, byte[] bt, int len) {  System.out.println(type + "长度为:" + len);  String cont = type + "的内容为:";  System.out.print(cont);  for (int ix = 0; ix < len; ++ix) {  System.out.printf("%02x ", bt[ix]);  }  System.out.println("\n----------");  }  public static void main(String[] args) throws IOException {  new ObtainSPSAndPPS().getSPSAndPPS("c:\\zgy.h264");  }  }  

运行结果如下:

    SPS长度为:9  SPS的内容为:67 42 00 1f e9 02 c1 2c 80   ----------  
    PPS长度为:4  PPS的内容为:68 ce 06 f2   ----------  

这篇关于H264 获取SPS与PPS的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1075525

相关文章

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

使用Python实现获取屏幕像素颜色值

《使用Python实现获取屏幕像素颜色值》这篇文章主要为大家详细介绍了如何使用Python实现获取屏幕像素颜色值,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、一个小工具,按住F10键,颜色值会跟着显示。完整代码import tkinter as tkimport pyau

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

使用Python获取JS加载的数据的多种实现方法

《使用Python获取JS加载的数据的多种实现方法》在当今的互联网时代,网页数据的动态加载已经成为一种常见的技术手段,许多现代网站通过JavaScript(JS)动态加载内容,这使得传统的静态网页爬取... 目录引言一、动态 网页与js加载数据的原理二、python爬取JS加载数据的方法(一)分析网络请求1

通过cmd获取网卡速率的代码

《通过cmd获取网卡速率的代码》今天从群里看到通过bat获取网卡速率两段代码,感觉还不错,学习bat的朋友可以参考一下... 1、本机有线网卡支持的最高速度:%v%@echo off & setlocal enabledelayedexpansionecho 代码开始echo 65001编码获取: >