type
status
date
slug
summary
tags
category
icon
password
Property
Mar 25, 2023 12:51 PM
前置知识
H.264 基础知识
- 视频为什么需要进行编码压缩?
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fdeca9ce0-f7bf-4520-adc7-f74f653efd8a%2FUntitled.png?table=block&id=5d82daaf-c273-45f4-bde5-49de13708c06)
- 为什么压缩的原始数据一般采用YUV格式?
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fbdf0b125-d957-4487-a1e2-e573f7993ee9%2FUntitled.png?table=block&id=e8cbda3d-ea3b-49ba-95e6-488d9d5db067)
- 视频压缩原理-数据冗余
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F4193f1c2-cf8f-467e-925a-ead0d1d5b5b8%2FUntitled.png?table=block&id=ce21f4c3-15a1-4420-bf18-dc04e217dfda)
- 图像帧的类型 (I帧、P帧和B帧)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F278bca0c-c37b-477c-abba-320641fd40b3%2FUntitled.png?table=block&id=1826db75-aed3-4329-9505-88e278c8ff45)
- GOP(一组图像,Group of Pictures)和GOP长度
- GOP之Closed GOP和Open GOP
- GOP间隔
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F9a56f2bd-9083-4909-ad5e-241743bcf21f%2FUntitled.png?table=block&id=8cd4ac5d-a458-462b-8092-be65818bfb41)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fc1b2957c-d34a-486f-95c1-745fad0b258e%2FUntitled.png?table=block&id=dcdcd418-0b12-4d36-85b4-53074ac4c24a)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F8154227e-f813-4871-9a62-429d72fd7edf%2FUntitled.png?table=block&id=da888ca8-9d79-40e7-8104-f4097d3e8ee5)
- H264编码原理
- 宏块扫描
- 宏块扫描是视频编码中的一个重要概念,用于将视频图像分割成更小的块,并对每个块进行编码。在视频编码中,图像通常被分割为一系列的宏块,每个宏块包含 16x16 个像素。
- 宏块扫描算法决定了在对图像进行编码时,应如何对宏块进行排列和扫描。常见的宏块扫描算法包括行扫描、逐行扫描、斜向扫描等。行扫描算法是指在每一行上依次扫描每个宏块;逐行扫描算法是指在每一列上依次扫描每个宏块;斜向扫描算法是指按照一定的斜线方向依次扫描每个宏块。
- 宏块扫描算法的选择会影响编码的效率和压缩率。例如,行扫描算法可以更好地利用视频压缩中的局部性原理,因为宏块之间通常具有很高的相似性。而斜向扫描算法可以减少图像中的局部冗余,从而提高压缩率。因此,不同的宏块扫描算法在不同的场景下具有不同的优劣。
- 残缺块
- 帧内预测
- 帧间预测
- DCT 变换和量化
- DCT变换
- 量化步长
- 量化步长表
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F580aa233-aa5f-4aa7-a2ae-3f6dfb710979%2FUntitled.png?table=block&id=a67e7132-8368-4e06-a49f-2b99222a033e)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Ff989bbc3-4a76-4fd0-bdae-aaa7bfb8f4cd%2FUntitled.png?table=block&id=a6c332cf-5faf-4282-a080-e6db3da810ce)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fb65e7094-4bcd-4814-a02e-4e8eb72beb48%2FUntitled.png?table=block&id=373bf97a-f643-4819-ace3-44d8ffde484c)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F12f9b95f-b3d3-4313-893b-abaa489e4825%2FUntitled.png?table=block&id=77916e2b-ce22-4127-8e8c-c71104fe2972)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Ff58bcd62-a356-47b9-900d-af64f3c8f189%2FUntitled.png?table=block&id=e681ad79-3fb9-4e3a-870a-8214dd3fc7cc)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fa0bccab3-b008-4986-85c2-bca1c9b11c3e%2FUntitled.png?table=block&id=b5ddae87-81f3-4bfc-821a-e047126f6e7f)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F2912368a-4333-4b57-a493-6dae3ac5a09a%2FUntitled.png?table=block&id=3aba7836-2429-457f-a882-c3a5876cb8ce)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F3e486a73-e9ab-455d-a85f-bbc1cfedd59a%2FUntitled.png?table=block&id=ccd85844-5f1a-4629-8d9f-67a0d45edac9)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F3d6b9507-8c85-4c57-9d8e-7d9fa8cbd5d7%2FUntitled.png?table=block&id=13022502-8c1e-4e5b-862e-5a50ebd08e65)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fa2081e3c-aad3-4f5b-a6b1-a1afeb81de0d%2FUntitled.png?table=block&id=bc71833e-5466-436f-9ef3-05a1414662fd)
编码原理总结
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F4ca59d79-8e7c-4902-99d0-35f17635c8ac%2FUntitled.png?table=block&id=94288789-5c87-4640-8291-b201280636ec)
FFmpeg 流程
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fcc7ac633-d463-438b-bc89-80168287c38e%2FUntitled.png?table=block&id=5507cd30-b013-4b96-9dce-e09cf10836eb)
相关 API
av_image_get_buffer_size()
: 用于计算给定图像参数的图像数据所需的缓冲区大小。具体来说,它可以计算使用给定图像格式、宽度、高度和对齐方式的图像所需的最小缓冲区大小。pix_fmt
:表示图像的像素格式width
:表示图像的宽度height
:表示图像的高度align
:表示缓冲区对齐方式,此参数是设定内存对⻬的对⻬数,也就是按多⼤的字节进⾏内存对⻬- ⽐如设置为1,表示按1字节对⻬,那么得到的结果就是与实际的内存⼤⼩⼀样。
- 再⽐如设置为4,表示按4字节对⻬。也就是内存的起始地址必须是4的整倍数。
- 返回值表示所需的缓冲区大小,以字节为单位
函数原型如下:
av_image_alloc()
:用于分配给定图像参数的图像数据缓冲区。具体来说,它可以根据给定图像格式、宽度、高度和对齐方式分配缓冲区,并将缓冲区指针、每行数据大小和总体数据大小返回给调用方。pointers
是一个指针数组,用于返回分配的缓冲区指针;linesizes
是一个整型数组,用于返回每行数据的大小;w
和h
分别表示图像的宽度和高度;pix_fmt
表示图像的像素格式;align
表示缓冲区对齐方式。- 返回值:所申请的内存空间的总⼤⼩。如果是负值,表示申请失败。
函数原型如下:
av_image_fill_arrays()
:用于填充给定图像参数的图像数据缓冲区。具体来说,它可以根据给定的缓冲区指针、每行数据大小和图像格式,将图像数据缓冲区分割成不同平面,并将平面数据指针、每行数据大小和对应的高度返回给调用方。dst_data
是一个指针数组,用于返回图像数据缓冲区的平面指针;dst_linesize
是一个整型数组,用于返回每行数据的大小;src
是源数据缓冲区的指针;pix_fmt
表示图像的像素格式;width
和height
分别表示图像的宽度和高度;align
表示缓冲区对齐方式。
av_image_fill_arrays()函数⾃身不具备内存申请的功能,此函数类似于格式化已经申请的内存,即通过av_malloc()函数申请的内存空间,或者av_frame_get_buffer()函数申请的内存空间。
函数原型如下:
H.264 码率设置
- 什么是视频码率?
视频码率是视频数据(包含视频⾊彩量、亮度量、像素量)每秒输出的位数。⼀般⽤的单位是kbps。
- 设置视频码率的必要性
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fec96129d-6cd9-446e-a535-7c4739a927fd%2FUntitled.png?table=block&id=275aa206-5204-4965-8438-55e3ddac78fb)
- 手机设置码率建议
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F9b0bf50a-6ee9-4163-8ebb-97d1c3b83d80%2FUntitled.png?table=block&id=7e26b57e-2a30-4bf3-8329-ef55996f214b)
FFmpeg 与 H.264 编码指南
鉴于x264的参数众多,各种参数的配合复杂,为了使⽤者⽅便,x264建议如⽆特别需要可使
⽤preset和tune设置。这套开发者推荐的参数较为合理,可在此基础上在调整⼀些具体参数以
符合⾃⼰需要,⼿动设定的参数会覆盖preset和tune⾥的参数。
- 使⽤ffmpeg -h encoder=libx264 命令查询相关⽀持的参数
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fb1d90e7a-f914-409a-90d5-a3a616cc6c80%2FUntitled.png?table=block&id=7558638f-e141-4333-9804-6f54460db2c1)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F73799e46-e2c7-4c90-b955-f9dbd5246f7a%2FUntitled.png?table=block&id=388eec30-1204-4610-9822-b8d2523cd859)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F228d4ec3-b97c-4f6d-9660-951cb9123fc0%2FUntitled.png?table=block&id=5e1c41cd-3227-4b76-9613-bdbc1147b9bd)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F173b5814-0b1c-48bb-8c42-4db05be87c90%2FUntitled.png?table=block&id=eb8aa3d8-3646-47a9-bacc-02a88903c70c)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F26572f8a-c774-4546-aa2d-abe65143076e%2FUntitled.png?table=block&id=344106f1-7eed-4925-a1b7-d6341d1a7658)
英⽂地址:https://trac.ffmpeg.org/wiki/Encode/H.264 。内容有⼀定出⼊,但是可以借鉴学习。
- x264是⼀个 H.264/MPEG4 AVC 编码器,本指南将指导新⼿如何创建⾼质量的H.264视频。 对于普通⽤户通常有两种码率控制模式:CRF(Constant Rate Factor)和Two pass ABR。码率控制是⼀种决定为每⼀个视频帧分配多少⽐特数的⽅法,它将决定⽂件的⼤⼩和质量的分配。 如果你在编译和安装libx264 ⽅⾯需要帮助,请查看ffmpeg和x264编译指南: http://ffmpeg.org/trac/ffmpeg/wiki/CompilationGuide
CRF(Constant Rate Factor):
选择⼀个CRF值
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fe92aaaab-2b73-45ed-86b2-7597b2f7f65e%2FUntitled.png?table=block&id=d1eebdeb-8670-4ba9-8e3d-8d5562ecd5b8)
选择⼀个preset和tune
- preset
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F7f90055c-d944-411f-b9d6-42587a7cfd52%2FUntitled.png?table=block&id=aa5fab4f-2a20-4c15-9caf-84564d8c7782)
- tune
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F29bd85df-7f92-46c3-9ae9-c3cd94c92dae%2FUntitled.png?table=block&id=52e1c474-da58-425a-a805-7f239e1ecca4)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F47406331-467a-4513-a476-13432848f0af%2FUntitled.png?table=block&id=a491a43f-13d8-4329-b0a2-6ae3a4ba7a61)
- profile
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F83f9f814-4bd3-48af-86a1-7ee943b21bb7%2FUntitled.png?table=block&id=b08bd629-0396-456d-be2d-28cf03b2b6b4)
低延迟
x264提供了⼀个 -tune zerolatency 选项。
兼容性
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F335e0397-c47f-461e-ab97-654c67841d5b%2FUntitled.png?table=block&id=2af0a48d-3656-4fdb-847e-005c73ddc558)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F5dbc25a7-9d53-4a88-a1ff-2d36f08a9439%2FUntitled.png?table=block&id=760bfa1b-10c5-4d18-b6fd-76112cb3291b)
X264 参数之 zerolatency 的分析
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fa829f20c-fa15-4300-bcb5-352b0300abf8%2FUntitled.png?table=block&id=e8c6ceec-b03a-429a-af00-82e901afbde8)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F80ad3915-50ed-46aa-8b4c-7240b93f8b0b%2FUntitled.png?table=block&id=71ac0ec4-343f-4b98-8c21-4b8a7c8b1e67)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Ffc9a70b5-898e-4181-97b5-7dcfc0848627%2FUntitled.png?table=block&id=a54af4c8-cc89-4487-a650-eb3747048f54)
YUV 编码为 H.264 代码
视频花屏、绿屏、黑屏黄屏的排查思路
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Fbf537f7e-38a4-4104-99d2-6d2c81f5f85b%2FUntitled.png?table=block&id=9ce21f47-8994-49b3-a3f0-d7808187ff63)
![notion image](https://www.notion.so/image/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F683dce04-8fcd-43ed-96a2-231eb09eb02c%2FUntitled.png?table=block&id=21da5857-5494-49bc-ae81-2958a5582b83)
- 作者:OctalZero
- 链接:https://octalzero.com/article/e5d3e585-936a-4ae8-a974-0e85879e814d
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章