音视频基础
视频、图像像素点数据格式
- GRAY 色彩空间
这一模式为 8 位展示的灰度,取值 0 至 255,表示明暗程度,0 为最黑暗的模式,255 为最亮的模式。 - YUV 色彩空间
在视频领域,通常以 YUV 的格式来存储和显示图像。其中 Y 表示视频的灰阶值,也可以理解为亮度值,而 UV 表示色彩度。
大多数 YUV 格式平均使用的每像素位数都少于 24 位。主要的色彩采样格式有 YCbCr 4:2:0、YCbCr 4:2:2、YCbCr 4:1:1 和 YCbCr 4:4:4。YUV 的表示法也称为 A:B:C 表示法。 - RGB 色彩空间
使用三个 8 位无符号整数(0 到 255)表示红色、绿色和蓝色的强度。
RGB 常见的展现方式分为 16 位模式和 32 位模式(32 位模式中主要用其中 24 位来表示 RGB)。 - HSL 和 HSV 色彩空间
HSL,就是色相(Hue)、饱和度( Saturation)、亮度( Lightness)。HSV 是色相(Hue)、饱和度( Saturation)和明度(Value)。色相(H)是色彩的基本属性,就是平常我们所说的颜色名称,如红色、黄色等;饱和度(S)是指色彩的纯度,越高色彩越纯,低则逐渐变灰,取 0~100% 的数值;明度(V)和亮度(L),同样取 0~100% 的数值。
图像的色彩空间
用相同的数据格式不一定能输出颜色完全一样的图像,跟图像显示设备的色彩空间(也叫色域)有关。
隔行扫描与逐行扫描
隔行扫描(Interlaced)是一种将图像隔行显示在扫描式显示设备上的方法,例如早期的 CRT 电脑显示器。非隔行扫描的扫描方法,即逐行扫描(Progressive),通常从上到下地扫描每帧图像。
720i、1080i,“i”就是 Interlaced。隔行扫描
720p、1080p。逐行扫描。
帧率fps
就是我们平时提到的帧率(FrameRate),指一秒钟刷新的视频图像帧数(Frames Per Second),视频一秒钟可以刷新多少帧,取决于显示设备的刷新能力。
图像分辨率与比例
分辨率通常由宽、高与像素点占用的位数组成,计算方式为图像的宽乘以高。在提到显示分辨率的时候,人们还常常会提到宽高比,即 DAR。DAR 是显示宽高比率(display aspect ratio),表示不同分辨率的图像的差别。
I、P、B 帧
一个 I 帧可以不依赖其他帧就解码出一幅完整的图像,而 P 帧、B 帧不行。
P 帧需要依赖视频流中排在它前面的帧才能解码出图像。
B 帧则需要依赖视频流中排在它前面或后面的帧才能解码出图像。
DTS、PTS
DTS(Decoding Time Stamp):即解码时间戳,这个时间戳的意义在于告诉播放器该在什么时候解码这一帧的数据。
PTS(Presentation Time Stamp):即显示时间戳,这个时间戳用来告诉播放器该在什么时候显示这一帧的数据。
音视频的采集
AudioRecoder、MediaRecoder
音视频编码
有损编码、无损编码。
编码的目的是为了压缩。
音频数据流
PCM格式(裸数据)--数字信号
编码压缩后生成 aac/mp3
map4/flv格式中包含mp3/aac数据
人类的听觉范围 20Hz~20kHz
以拨动尺子为例
音调:音频的快慢 儿童>女生>男生
音量:振动的幅度
音色:谐波
模数转换

采样率越高,失真越小。
声音量化的基本概念
采样大小:一个采样用多少bit存放。常用16bit
采样率:采样频率8k,16k,32k,44k,48k
声道数:单声道、双声道、多声道
音频原始数据
PCM裸数据
WAV格式=PCM+文件头


PCM码率计算

音频采集
Android 采集
MediaRecoder
AudioRecoder
IOS
Windows
ffmpeg采集
命令方式
ffmpeg-f avdoundation -i :0 out.wav
ffmplay out.wav
api方式
音频采集 https://blog.csdn.net/qq_15255121/article/details/115301516
打开音频设备
采集步骤
注册设备
->设置采集方式avdoundation/dshow/alsa
->打开音频设备
读取音频数据
av_read_frame
第七章 音频编码原理
有损编码
无损编码
有损压缩是去除冗余信息,需要界定什么是冗余信息。
人耳频率之外的。20~2whz
遮蔽效应


无损编码
熵编码:哈夫曼。算术、香农编码
常见编码器比较
常见的音频编码器OPUS,AAC



AAC编码器介绍



ADTS格式


可以借助ffmpeg帮我们添加或者解析这个头。
通过ffmpeg命令生成AAC文件

-vn no video
-c 编码器 a audio libfdk_aac音频编码器
-ar 采样率
-channels 声道数
-profile :a 指定音频 aac规格
抽取输入的mp4文件中的音频并重新编码
8. 实战音频编码
音频重采样





创建AAC编码器


avcodec_send_frame 输入音视频数据给编码器
avcodec_receive_packet 获取编码后的数据
注意并不是输入一帧音频就输出一帧,有可能是输入若干帧,再输出。
AVFrame未编码的数据(音频输入数据)
AVPacket编码后的数据(编码后的数据)
9.视频的基础知识
h264 h265做压缩
每个像素的位深 RGB888 24位 ,RGBA 32位



360P,720P,1K,2K都是16:9



只有电视支持YUV格式,手机不支持,只支持RGB





Y分量 明度
U分量/Cb分量 blue
V分量/Cr分量 red





- YUV是分层存储
- 每四个Y对应一个U和一个V,具体对应的位置关系如上图所示



为什么有要这种编码方式
人眼对色度的敏感程度要低于对亮度的敏感程度既然看不见(或难发现差别),那么就不浪费全量444存储空间。
YUV的参考资料


-i 输入多媒体文件
-an audio null过滤掉音频
-c:v 视频的编解码器 rawvideo
-pix_fmt 指定输出的YUV的格式

-pix_fmt 指定要播放的YUV格式,默认YUV420P
-s 播放的分辨率

-vf 滤波器



注意U/V分辨率是Y的一半
第十章 H264编码原理(重要)
H264压缩码率与GOP
H264优势之一在于压缩比,约为1/100

根据场景(实时通讯<直播)+分辨率+帧率会得到一个推荐的码率。

GOP(group of picture)


IPB帧

对实时性要求高需要IP帧多,对压缩率要求高需要B帧多

IDR帧起到了防止错误传播的作用

B帧需要前面的I帧和后面的P帧解码

SPS与PPS帧一般是成对出现的,且在IDR帧之前出现
H264中的宏块

前二者是有损压缩,后两者是无损压缩。
宏块是压缩技术的基础






宏块越大,控制力弱,处理速度越快
宏块越小,控制力更强,压缩比更高(用于纹理特别精细的图)
帧内压缩技术
按宏块为单位进行比较,然后压缩存储

h264提供了9种模式做帧间预测,每个宏块的预测模式不一




帧间压缩技术
帧间压缩一定是同一个GOP之间的。






无损压缩


DCT变换后将分散在nxn中的所有数据集中在开端。

H264码流结构
H264码流是分层存储。
NAL层的意义在于知道和处理二进制码流数据丢包和乱序的问题。


一个图像由多个slice组成,一般是由一个slice组成。
码流的基本概念
SODB(String of Data Bits)编码器生成的二级制数据串,按位计算;
RBSP 按字节计算
NALU NAL Header(1B)+RBSP




宏块= 宏块的类型+宏块的预测值+残差值

文件传输需要加startCode,即Annexb格式(startCode是每个NAL单元的分隔符)
网络传输用RTP格式
第十一章 实战视频编解码
H264中的profile和level





SPS重要参数


默认的宏块是16*16

GOP最大帧数

PPS与slice header


H264分析工具
前二者收费,后者免费


推荐streameye,免费的就是雷神。
实战H264编码

编码器x264参数

preset fast/slow 编码速度
tune 质量 电影>动画>图片
这两个值是不互斥的。
帧相关的参数
keyint GOP size(想要达到更高的压缩率,可以将其设置得尽量大。想要避免丢包引起的卡顿,可以将其设置的尽量小)
min-keyint 最小key帧的值,与scenecut相关联
scenecut 达到多少不同就认为场景切换了
bframes 连续帧的最大数量
ref 参考帧的数量
no-deblock/deblock 将块进行平滑处理
no-cabac 是否使用cabac进行熵编码
流控


编码分析
p8x8,p帧宏块的划分为8x8




12章 RTMP传输
12-1RTMP协议
rtmp协议是基于tcp的。


12-2 创建rtmp流



12-3 RTMP消息
FLV文件就是由“FLV Header + RTMP数据”构成的
12-7 推流程序的骨架
