libaom 源码分析:scalable_decoder.c 文件

server/2025/3/28 15:44:09/

AOM.html" title=libaom>libaom_0">AOM.html" title=libaom>libaom

  1. 基本特性

    • 开放和免版税:AOM.html" title=libaom>libaom 提供了一个开放源代码的编码器,任何个人和组织都可以免费使用,无需支付版税,这促进了它在各种应用中的广泛采用。
    • 高效的编码:旨在提供高效的视频压缩,以适应不同的网络条件和设备性能。AV1 编解码器通常比 HEVC(H.265)提供约 30% 更好的压缩效率,在相同质量下可以减少带宽消耗。
    • 先进的压缩技术:实现了 AV1 编解码器,使用了一系列先进的压缩技术,如 CDEF(Constrained Directional Enhancement Filtering)、CIC(Compound Internal Coding)和 PAET(Probabilistic Angular Early Termination)等。
    • 可配置性:提供了多种配置选项,允许开发者根据应用需求调整编码参数。
    • 实时编码支持:支持实时编码,适用于直播和实时通信应用。
    • 社区支持:作为一个开源项目,得到了活跃的社区支持,不断有新功能和改进被加入。
  2. 应用场景
    AOM.html" title=libaom>libaom 可以用于各种需要视频编解码的场景,如在线视频播放、视频会议、视频存储、流媒体、网页视频等。许多视频播放器、浏览器和视频服务提供商都采用了 AOM.html" title=libaom>libaom 来实现 AV1 视频编码标准。

  3. 性能提升

    • 压缩性能:经过团队的研究和尝试,AV1 的压缩性能在过去几年中有了显著的提高,相对于 VP9 而言,在以 PSNR 或 SSIM 这样的客观指标条件下能提高 36% 以上。
    • 编码器速度和内存需求:相对于 2018 年 bit - stream finalization 的时候,Libaom 编码器的速度提升 150 倍以上,在 4K 的视频压缩条件下对内存的需求下降了 80%。同时,Libaom 支持不同的 speed setting,用户可以根据实际需求进行选择,在速度和压缩性能之间进行平衡。

scalable_decoder.c 介绍

  1. 功能描述:该文件是一个可伸缩解码器的示例,输入文件包含两层空间层的压缩数据,格式为 OBU,数据通过解码器处理,解码后的帧会写入磁盘,基本层和增强层分别存储为 out_lyr0.yuvout_lyr1.yuv 文件。
  2. 文件位置AOM.html" title=libaom>libaom/examples/scalable_decoder.c
  3. main 函数流程梳理
. 初始化阶段- 解析命令行参数,检查输入文件- 打开输入文件并初始化 OBU 解码上下文- 获取 AV1 解码器接口- 初始化解码器上下文- 设置解码器输出所有层的控制参数. 文件验证- 读取文件头信息,验证是否为有效的 OBU 文件- 获取视频流的空间层数量. 输出文件准备- 打开基本层输出文件 `out_lyr0.yuv`- 根据空间层数量打开对应的增强层输出文件. 解码循环- 读取时间单元数据- 解码每一帧- 获取解码后的图像- 对图像进行位深转换- 根据空间层 ID 将图像写入对应的输出文件- 更新帧计数器. 清理阶段- 销毁解码器上下文- 关闭所有输出文件- 关闭输入文件
  1. 文件内部调用的 API 解析
  • 实例化解码器:get_aom_decoder_by_index(0) 通过索引 0 获取 AV1 解码器接口,aom_codec_iface_name(decoder) 获取解码器的名称;
  aom_codec_iface_t *decoder = get_aom_decoder_by_index(0);printf("Using %s\n", aom_codec_iface_name(decoder));
  • 初始化:aom_codec_ctx_t codec声明一个解码器上下文变量 codec ,用于存储解码器的状态信息;aom_codec_dec_init() 初始化解码器;
  aom_codec_ctx_t codec;if (aom_codec_dec_init(&codec, decoder, NULL, 0))die("Failed to initialize decoder.");
  • 编码器能力控制:aom_codec_control() 用于设置解码器的控制参数;
    AV1D_SET_OUTPUT_ALL_LAYERS 控制参数,表示是否输出所有层;
 if (aom_codec_control(&codec, AV1D_SET_OUTPUT_ALL_LAYERS, 1)) {die_codec(&codec, "Failed to set output_all_layers control.");}
  • 输入流的OBU头信息解析:aom_codec_peek_stream_info解析空间层配置信息;
  // peak sequence header OBU to get number of spatial layersconst size_t ret = fread(tmpbuf, 1, 32, inputfile);if (ret != 32) die_codec(&codec, "Input is not a valid obu file");si.is_annexb = 0;if (aom_codec_peek_stream_info(decoder, tmpbuf, 32, &si)) {die_codec(&codec, "Input is not a valid obu file");}fseek(inputfile, -32, SEEK_CUR);
  • 文件校验和输出文件准备
 if (!file_is_obu(&obu_ctx))die_codec(&codec, "Input is not a valid obu file");// open base layer output yuv filesnprintf(filename, sizeof(filename), "out_lyr%d.yuv", 0);if (!(outfile[0] = fopen(filename, "wb")))die("Failed top open output for writing.");// open any enhancement layer output yuv filesfor (i = 1; i < si.number_spatial_layers; i++) {snprintf(filename, sizeof(filename), "out_lyr%u.yuv", i);if (!(outfile[i] = fopen(filename, "wb")))die("Failed to open output for writing.");}
  • 解码核心逻辑:obudec_read_temporal_unit一直读取OBU单元,aom_codec_decode解码的核心函数,aom_codec_get_frame获取解码帧,aom_img_downshift将原图像进行下采样,aom_img_write根据不同的spatial_id写入输出视频文件;
  while (!obudec_read_temporal_unit(&obu_ctx, &buf, &bytes_in_buffer,&buffer_size)) {aom_codec_iter_t iter = NULL;aom_image_t *img = NULL;if (aom_codec_decode(&codec, buf, bytes_in_buffer, NULL))die_codec(&codec, "Failed to decode frame.");while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {aom_image_t *img_shifted =aom_img_alloc(NULL, AOM_IMG_FMT_I420, img->d_w, img->d_h, 16);img_shifted->bit_depth = 8;aom_img_downshift(img_shifted, img,img->bit_depth - img_shifted->bit_depth);if (img->spatial_id == 0) {printf("Writing        base layer 0 %d\n", frame_cnt);aom_img_write(img_shifted, outfile[0]);} else if (img->spatial_id <= (int)(si.number_spatial_layers - 1)) {printf("Writing enhancement layer %d %d\n", img->spatial_id, frame_cnt);aom_img_write(img_shifted, outfile[img->spatial_id]);} else {die_codec(&codec, "Invalid bitstream. Layer id exceeds layer count");}if (img->spatial_id == (int)(si.number_spatial_layers - 1)) ++frame_cnt;}}
  • 资源销毁和回收清理工作
  printf("Processed %d frames.\n", frame_cnt);if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");for (i = 0; i < si.number_spatial_layers; i++) fclose(outfile[i]);fclose(inputfile);
  1. 源码:
/** Copyright (c) 2018, Alliance for Open Media. All rights reserved.** This source code is subject to the terms of the BSD 2 Clause License and* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License* was not distributed with this source code in the LICENSE file, you can* obtain it at www.aomedia.org/license/software. If the Alliance for Open* Media Patent License 1.0 was not distributed with this source code in the* PATENTS file, you can obtain it at www.aomedia.org/license/patent.*/// Scalable Decoder
// ==============
//
// This is an example of a scalable decoder loop. It takes a 2-spatial-layer
// input file
// containing the compressed data (in OBU format), passes it through the
// decoder, and writes the decompressed frames to disk. The base layer and
// enhancement layers are stored as separate files, out_lyr0.yuv and
// out_lyr1.yuv, respectively.
//
// Standard Includes
// -----------------
// For decoders, you only have to include `aom_decoder.h` and then any
// header files for the specific codecs you use. In this case, we're using
// av1.
//
// Initializing The Codec
// ----------------------
// The AOM.html" title=libaom>libaom decoder is initialized by the call to aom_codec_dec_init().
// Determining the codec interface to use is handled by AvxVideoReader and the
// functions prefixed with aom_video_reader_. Discussion of those functions is
// beyond the scope of this example, but the main gist is to open the input file
// and parse just enough of it to determine if it's a AVx file and which AVx
// codec is contained within the file.
// Note the NULL pointer passed to aom_codec_dec_init(). We do that in this
// example because we want the algorithm to determine the stream configuration
// (width/height) and allocate memory automatically.
//
// Decoding A Frame
// ----------------
// Once the frame has been read into memory, it is decoded using the
// `aom_codec_decode` function. The call takes a pointer to the data
// (`frame`) and the length of the data (`frame_size`). No application data
// is associated with the frame in this example, so the `user_priv`
// parameter is NULL. The `deadline` parameter is left at zero for this
// example. This parameter is generally only used when doing adaptive post
// processing.
//
// Codecs may produce a variable number of output frames for every call to
// `aom_codec_decode`. These frames are retrieved by the
// `aom_codec_get_frame` iterator function. The iterator variable `iter` is
// initialized to NULL each time `aom_codec_decode` is called.
// `aom_codec_get_frame` is called in a loop, returning a pointer to a
// decoded image or NULL to indicate the end of list.
//
// Processing The Decoded Data
// ---------------------------
// In this example, we simply write the encoded data to disk. It is
// important to honor the image's `stride` values.
//
// Cleanup
// -------
// The `aom_codec_destroy` call frees any memory allocated by the codec.
//
// Error Handling
// --------------
// This example does not special case any error return codes. If there was
// an error, a descriptive message is printed and the program exits. With
// few exceptions, aom_codec functions return an enumerated error status,
// with the value `0` indicating success.#include <stdio.h>
#include <stdlib.h>
#include <string.h>#include "aom/aom_decoder.h"
#include "aom/aomdx.h"
#include "common/obudec.h"
#include "common/tools_common.h"
#include "common/video_reader.h"static const char *exec_name;#define MAX_LAYERS 5void usage_exit(void) {fprintf(stderr, "Usage: %s <infile>\n", exec_name);exit(EXIT_FAILURE);
}int main(int argc, char **argv) {int frame_cnt = 0;FILE *outfile[MAX_LAYERS];char filename[80];FILE *inputfile = NULL;uint8_t *buf = NULL;size_t bytes_in_buffer = 0;size_t buffer_size = 0;struct AvxInputContext aom_input_ctx;struct ObuDecInputContext obu_ctx = { &aom_input_ctx, NULL, 0, 0, 0 };aom_codec_stream_info_t si;uint8_t tmpbuf[32];unsigned int i;exec_name = argv[0];if (argc != 2) die("Invalid number of arguments.");if (!(inputfile = fopen(argv[1], "rb")))die("Failed to open %s for read.", argv[1]);obu_ctx.avx_ctx->file = inputfile;obu_ctx.avx_ctx->filename = argv[1];aom_codec_iface_t *decoder = get_aom_decoder_by_index(0);printf("Using %s\n", aom_codec_iface_name(decoder));aom_codec_ctx_t codec;if (aom_codec_dec_init(&codec, decoder, NULL, 0))die("Failed to initialize decoder.");if (aom_codec_control(&codec, AV1D_SET_OUTPUT_ALL_LAYERS, 1)) {die_codec(&codec, "Failed to set output_all_layers control.");}// peak sequence header OBU to get number of spatial layersconst size_t ret = fread(tmpbuf, 1, 32, inputfile);if (ret != 32) die_codec(&codec, "Input is not a valid obu file");si.is_annexb = 0;if (aom_codec_peek_stream_info(decoder, tmpbuf, 32, &si)) {die_codec(&codec, "Input is not a valid obu file");}fseek(inputfile, -32, SEEK_CUR);if (!file_is_obu(&obu_ctx))die_codec(&codec, "Input is not a valid obu file");// open base layer output yuv filesnprintf(filename, sizeof(filename), "out_lyr%d.yuv", 0);if (!(outfile[0] = fopen(filename, "wb")))die("Failed top open output for writing.");// open any enhancement layer output yuv filesfor (i = 1; i < si.number_spatial_layers; i++) {snprintf(filename, sizeof(filename), "out_lyr%u.yuv", i);if (!(outfile[i] = fopen(filename, "wb")))die("Failed to open output for writing.");}while (!obudec_read_temporal_unit(&obu_ctx, &buf, &bytes_in_buffer,&buffer_size)) {aom_codec_iter_t iter = NULL;aom_image_t *img = NULL;if (aom_codec_decode(&codec, buf, bytes_in_buffer, NULL))die_codec(&codec, "Failed to decode frame.");while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {aom_image_t *img_shifted =aom_img_alloc(NULL, AOM_IMG_FMT_I420, img->d_w, img->d_h, 16);img_shifted->bit_depth = 8;aom_img_downshift(img_shifted, img,img->bit_depth - img_shifted->bit_depth);if (img->spatial_id == 0) {printf("Writing        base layer 0 %d\n", frame_cnt);aom_img_write(img_shifted, outfile[0]);} else if (img->spatial_id <= (int)(si.number_spatial_layers - 1)) {printf("Writing enhancement layer %d %d\n", img->spatial_id, frame_cnt);aom_img_write(img_shifted, outfile[img->spatial_id]);} else {die_codec(&codec, "Invalid bitstream. Layer id exceeds layer count");}if (img->spatial_id == (int)(si.number_spatial_layers - 1)) ++frame_cnt;}}printf("Processed %d frames.\n", frame_cnt);if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");for (i = 0; i < si.number_spatial_layers; i++) fclose(outfile[i]);fclose(inputfile);return EXIT_SUCCESS;
}

http://www.ppmy.cn/server/177118.html

相关文章

doris:审计日志

Doris 提供了对于数据库操作的审计能力&#xff0c;可以记录用户对数据库的登陆、查询、修改操作。在 Doris 中&#xff0c;可以直接通过内置系统表查询审计日志&#xff0c;也可以直接查看 Doris 的审计日志文件。 开启审计日志​ 通过全局变量 enable_audit_plugin 可以随时…

GCC 预定义宏:解锁编译器的隐藏信息

GCC 预定义宏&#xff1a;解锁编译器的隐藏信息 在 GCC 编译器中&#xff0c;有许多内置的预定义宏&#xff0c;它们可以提供编译环境的信息&#xff0c;如文件名、行号、时间、版本等。这些宏在调试、日志记录、条件编译等场景中非常有用。本文将介绍常见的 GCC 预定义宏&…

SpringSecurity——前后端分离登录认证

SpringSecurity——前后端分离登录认证的整个过程 前端&#xff1a; 使用Axios向后端发送请求 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>登录</title><script src"https://cdn…

基于DeepSeek的智能体搭建

智能体是什么&#xff1f; 智能体是一种生成式人工智能应用程 序&#xff0c;其核心是通过观察环境并利用工 具自主采取行动以实现特定目标。 智能体架构 •大模型&#xff08;Language Model&#xff09;&#xff1a;作为agent流程的集中决策者使用的语言模型&#xff0c;能…

网络华为HCIA+HCIP WLAN

目录 WLAN 敏捷分布式AP架构 有线侧组网概念&#xff1a;CAPWAP协议 CAPWAP隧道 功能 有线侧组网概念&#xff1a;AP-AC组网方式 有线侧组网概念&#xff1a;AC连接方式 无线侧组网概念&#xff1a;无线通信系统 无线侧组网概念&#xff1a;无线电磁波 无线侧组网概…

Breeze 25A FOC 电调:无人机动力系统的智能心脏|精准控制 × 高效输出 × 多重防护

—— 以工业级技术重构无人机飞行性能 —— ▌核心技术突破 ✓ 创新 Vfast 观测器算法&#xff1a;实现 0.5ms 级响应速度&#xff0c;动态补偿误差&#xff1c;0.1% ✓ 矢量控制&#xff08;FOC&#xff09;技术&#xff1a;相比传统方波驱动效率提升 18%&#xff0c;力效比达…

基于SpringBoot+Vue3实现的宠物领养管理平台功能一

一、前言介绍&#xff1a; 1.1 项目摘要 随着社会经济的发展和人们生活水平的提高&#xff0c;越来越多的人开始关注并参与到宠物领养中。宠物已经成为许多家庭的重要成员&#xff0c;人们对于宠物的关爱和照顾也日益增加。然而&#xff0c;传统的宠物领养流程存在诸多不便&a…

【设计模式】装饰模式

六、装饰模式 装饰(Decorator) 模式也称为装饰器模式/包装模式&#xff0c;是一种结构型模式。这是一个非常有趣和值得学习的设计模式&#xff0c;该模式展现出了运行时的一种扩展能力&#xff0c;以及比继承更强大和灵活的设计视角和设计能力&#xff0c;甚至在有些场合下&am…