简介: CSDN博客专家、《Android系统多媒体进阶实战》作者
博主新书推荐:《Android系统多媒体进阶实战》🚀
Android Audio工程师专栏地址:Audio工程师进阶系列【原创干货持续更新中...... 】🚀
Android多媒体专栏地址:多媒体系统工程师系列【原创干货持续更新中...... 】🚀
专题一 二:AAOS车载系统+AOSP14系统攻城狮入门视频实战课 🚀
专题三:Android14 Binder之HIDL与AIDL通信实战课 🚀
专题四:Android15快速自定义与集成音效实战课 🚀
专题五:Android15音频策略实战课 🚀
专题六:Android15音频性能实战课(无声/杂音/断音/爆音实战案例) 🚀
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
更多原创,欢迎关注:Android系统攻城狮

🍉🍉🍉文章目录🍉🍉🍉
🌻1.前言
本篇目的:理解
av_frame_get_plane_buffer()如何根据视频平面数据指针查找其所属的AVBufferRef,并说明返回引用与帧对象之间的生命周期关系。
AVFrame的data[]保存各个数据平面的起始指针,buf[]和extended_buf[]保存底层缓冲区引用。一个Plane不一定对应一个独立Buffer,多个平面可能位于同一个Buffer中。
av_frame_get_plane_buffer()根据Plane的数据指针查找包含它的Buffer,不会创建新的引用,也不会复制平面数据。
#mermaid-svg-RpXU5IuLKG3xqTuT{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-RpXU5IuLKG3xqTuT .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-RpXU5IuLKG3xqTuT .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-RpXU5IuLKG3xqTuT .error-icon{fill:#552222;}#mermaid-svg-RpXU5IuLKG3xqTuT .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-RpXU5IuLKG3xqTuT .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-RpXU5IuLKG3xqTuT .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-RpXU5IuLKG3xqTuT .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-RpXU5IuLKG3xqTuT .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-RpXU5IuLKG3xqTuT .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-RpXU5IuLKG3xqTuT .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-RpXU5IuLKG3xqTuT .marker{fill:#333333;stroke:#333333;}#mermaid-svg-RpXU5IuLKG3xqTuT .marker.cross{stroke:#333333;}#mermaid-svg-RpXU5IuLKG3xqTuT svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-RpXU5IuLKG3xqTuT p{margin:0;}#mermaid-svg-RpXU5IuLKG3xqTuT .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-RpXU5IuLKG3xqTuT .cluster-label text{fill:#333;}#mermaid-svg-RpXU5IuLKG3xqTuT .cluster-label span{color:#333;}#mermaid-svg-RpXU5IuLKG3xqTuT .cluster-label span p{background-color:transparent;}#mermaid-svg-RpXU5IuLKG3xqTuT .label text,#mermaid-svg-RpXU5IuLKG3xqTuT span{fill:#333;color:#333;}#mermaid-svg-RpXU5IuLKG3xqTuT .node rect,#mermaid-svg-RpXU5IuLKG3xqTuT .node circle,#mermaid-svg-RpXU5IuLKG3xqTuT .node ellipse,#mermaid-svg-RpXU5IuLKG3xqTuT .node polygon,#mermaid-svg-RpXU5IuLKG3xqTuT .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-RpXU5IuLKG3xqTuT .rough-node .label text,#mermaid-svg-RpXU5IuLKG3xqTuT .node .label text,#mermaid-svg-RpXU5IuLKG3xqTuT .image-shape .label,#mermaid-svg-RpXU5IuLKG3xqTuT .icon-shape .label{text-anchor:middle;}#mermaid-svg-RpXU5IuLKG3xqTuT .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-RpXU5IuLKG3xqTuT .rough-node .label,#mermaid-svg-RpXU5IuLKG3xqTuT .node .label,#mermaid-svg-RpXU5IuLKG3xqTuT .image-shape .label,#mermaid-svg-RpXU5IuLKG3xqTuT .icon-shape .label{text-align:center;}#mermaid-svg-RpXU5IuLKG3xqTuT .node.clickable{cursor:pointer;}#mermaid-svg-RpXU5IuLKG3xqTuT .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-RpXU5IuLKG3xqTuT .arrowheadPath{fill:#333333;}#mermaid-svg-RpXU5IuLKG3xqTuT .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-RpXU5IuLKG3xqTuT .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-RpXU5IuLKG3xqTuT .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RpXU5IuLKG3xqTuT .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-RpXU5IuLKG3xqTuT .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RpXU5IuLKG3xqTuT .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-RpXU5IuLKG3xqTuT .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-RpXU5IuLKG3xqTuT .cluster text{fill:#333;}#mermaid-svg-RpXU5IuLKG3xqTuT .cluster span{color:#333;}#mermaid-svg-RpXU5IuLKG3xqTuT div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-RpXU5IuLKG3xqTuT .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-RpXU5IuLKG3xqTuT rect.text{fill:none;stroke-width:0;}#mermaid-svg-RpXU5IuLKG3xqTuT .icon-shape,#mermaid-svg-RpXU5IuLKG3xqTuT .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RpXU5IuLKG3xqTuT .icon-shape p,#mermaid-svg-RpXU5IuLKG3xqTuT .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-RpXU5IuLKG3xqTuT .icon-shape .label rect,#mermaid-svg-RpXU5IuLKG3xqTuT .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RpXU5IuLKG3xqTuT .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-RpXU5IuLKG3xqTuT .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-RpXU5IuLKG3xqTuT :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} AVFrame的Plane指针
av_frame_get_plane_buffer
帧持有的Buffer引用
查找包含Plane数据的Buffer
返回所属AVBufferRef
🌻2.应用场景和用法
2.1定位视频Plane所属的Buffer
该接口常用于检查平面内存归属、计算平面在Buffer中的偏移,或在独立管理Buffer时延长底层存储生命周期。
| 场景 | 使用方式 | 注意事项 |
|---|---|---|
| 确认Plane有Buffer管理 | 对指定Plane调用接口 | 返回NULL时不能假设数据受该引用管理 |
| 计算Plane偏移 | 用frame->data[plane] - buffer->data计算 |
只有确认平面指针属于返回Buffer后才计算 |
| 延长Buffer生命周期 | 对返回值调用av_buffer_ref() |
返回值本身是借用引用,不能直接释放 |
| 检查自定义Frame | 确认平面指针是否落在帧的Buffer范围内 | 数据指针可能由外部管理而不在帧引用数组中 |
对于常见YUV420P软件帧,Y、U、V是三个数据平面,但底层可能由一个连续Buffer统一管理。Plane编号表示extended_data中的数据平面索引,不是buf[]中的Buffer编号。
2.2函数原型与返回语义
c
AVBufferRef *
av_frame_get_plane_buffer (const AVFrame *frame, int plane);
| 参数或返回值 | 含义 |
|---|---|
frame |
已有视频或音频数据平面的帧对象 |
plane |
extended_data中的数据平面索引 |
| 返回值 | 包含该平面数据指针的AVBufferRef,找不到时返回NULL |
实现会先检查Plane索引和对应数据指针,再依次检查frame->buf[]和frame->extended_buf[],判断平面起始指针是否位于某个Buffer的数据范围内。
返回的AVBufferRef *指向帧内部持有的引用,函数不会增加引用计数。若只在帧存活期间检查或读取,可以直接使用;若需要在帧释放后继续持有底层存储,应先用av_buffer_ref()增加引用,之后释放自己持有的新引用。
🌻3.调用流程剖析
3.1确定Plane索引范围和数据指针
函数根据帧类型决定平面数量。视频帧按固定的平面索引范围检查,音频帧则依据声道数量以及采样格式是否为Planar确定Plane数量。
如果索引越界,或者对应Plane的数据指针为空,函数返回NULL。通过检查后,函数从extended_data[plane]取得Plane的起始地址。
#mermaid-svg-89KGn2NyrjD5mkSG{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-89KGn2NyrjD5mkSG .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-89KGn2NyrjD5mkSG .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-89KGn2NyrjD5mkSG .error-icon{fill:#552222;}#mermaid-svg-89KGn2NyrjD5mkSG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-89KGn2NyrjD5mkSG .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-89KGn2NyrjD5mkSG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-89KGn2NyrjD5mkSG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-89KGn2NyrjD5mkSG .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-89KGn2NyrjD5mkSG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-89KGn2NyrjD5mkSG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-89KGn2NyrjD5mkSG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-89KGn2NyrjD5mkSG .marker.cross{stroke:#333333;}#mermaid-svg-89KGn2NyrjD5mkSG svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-89KGn2NyrjD5mkSG p{margin:0;}#mermaid-svg-89KGn2NyrjD5mkSG .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-89KGn2NyrjD5mkSG .cluster-label text{fill:#333;}#mermaid-svg-89KGn2NyrjD5mkSG .cluster-label span{color:#333;}#mermaid-svg-89KGn2NyrjD5mkSG .cluster-label span p{background-color:transparent;}#mermaid-svg-89KGn2NyrjD5mkSG .label text,#mermaid-svg-89KGn2NyrjD5mkSG span{fill:#333;color:#333;}#mermaid-svg-89KGn2NyrjD5mkSG .node rect,#mermaid-svg-89KGn2NyrjD5mkSG .node circle,#mermaid-svg-89KGn2NyrjD5mkSG .node ellipse,#mermaid-svg-89KGn2NyrjD5mkSG .node polygon,#mermaid-svg-89KGn2NyrjD5mkSG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-89KGn2NyrjD5mkSG .rough-node .label text,#mermaid-svg-89KGn2NyrjD5mkSG .node .label text,#mermaid-svg-89KGn2NyrjD5mkSG .image-shape .label,#mermaid-svg-89KGn2NyrjD5mkSG .icon-shape .label{text-anchor:middle;}#mermaid-svg-89KGn2NyrjD5mkSG .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-89KGn2NyrjD5mkSG .rough-node .label,#mermaid-svg-89KGn2NyrjD5mkSG .node .label,#mermaid-svg-89KGn2NyrjD5mkSG .image-shape .label,#mermaid-svg-89KGn2NyrjD5mkSG .icon-shape .label{text-align:center;}#mermaid-svg-89KGn2NyrjD5mkSG .node.clickable{cursor:pointer;}#mermaid-svg-89KGn2NyrjD5mkSG .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-89KGn2NyrjD5mkSG .arrowheadPath{fill:#333333;}#mermaid-svg-89KGn2NyrjD5mkSG .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-89KGn2NyrjD5mkSG .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-89KGn2NyrjD5mkSG .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-89KGn2NyrjD5mkSG .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-89KGn2NyrjD5mkSG .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-89KGn2NyrjD5mkSG .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-89KGn2NyrjD5mkSG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-89KGn2NyrjD5mkSG .cluster text{fill:#333;}#mermaid-svg-89KGn2NyrjD5mkSG .cluster span{color:#333;}#mermaid-svg-89KGn2NyrjD5mkSG div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-89KGn2NyrjD5mkSG .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-89KGn2NyrjD5mkSG rect.text{fill:none;stroke-width:0;}#mermaid-svg-89KGn2NyrjD5mkSG .icon-shape,#mermaid-svg-89KGn2NyrjD5mkSG .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-89KGn2NyrjD5mkSG .icon-shape p,#mermaid-svg-89KGn2NyrjD5mkSG .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-89KGn2NyrjD5mkSG .icon-shape .label rect,#mermaid-svg-89KGn2NyrjD5mkSG .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-89KGn2NyrjD5mkSG .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-89KGn2NyrjD5mkSG .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-89KGn2NyrjD5mkSG :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 否
是
输入AVFrame和Plane索引
判断帧类型与Plane范围
索引有效且数据指针存在
返回NULL
读取Plane起始地址
检查帧持有的Buffer范围
3.2在Buffer数组中查找所属引用
函数遍历frame->buf[],再遍历frame->extended_buf[]。当Plane指针落在某个AVBufferRef的data到data + size范围内,就返回该引用。没有任何引用覆盖Plane指针时,返回NULL。
这个函数返回的是Buffer归属信息,不会复制数据、调整Frame布局或增加引用计数。调用者如果要独立保留该Buffer,需要明确调用av_buffer_ref(),并在使用完后调用av_buffer_unref()。
🌻4.实战案例
4.1分配YUV420P视频帧
案例创建一帧640×360的YUV420P图像,并调用av_frame_get_buffer()分配对齐内存。随后检查U平面所属的Buffer,并计算U平面数据相对于Buffer起始地址的偏移。
4.2增加独立Buffer引用
av_frame_get_plane_buffer()返回的引用由AVFrame持有。代码计算偏移后调用av_buffer_ref()创建独立引用,再释放Frame。独立引用仍然有效,释放时由调用者负责调用av_buffer_unref()。
4.3完整代码
c
#include <libavutil/buffer.h>
#include <libavutil/frame.h>
#include <libavutil/pixfmt.h>
#include <stddef.h>
int
main (void)
{
AVFrame *frame = NULL;
AVBufferRef *plane_buffer;
AVBufferRef *held_buffer = NULL;
ptrdiff_t plane_offset;
int ret = 1;
frame = av_frame_alloc ();
if (!frame)
goto end;
/* 创建一帧YUV420P视频并分配平面存储 */
frame->format = AV_PIX_FMT_YUV420P;
frame->width = 640;
frame->height = 360;
if (av_frame_get_buffer (frame, 32) < 0)
goto end;
/* 查询U平面所属的底层Buffer */
plane_buffer = av_frame_get_plane_buffer (frame, 1);
if (!plane_buffer)
goto end;
/* 记录U平面在Buffer中的偏移,之后不再依赖借用指针 */
plane_offset = frame->data[1] - plane_buffer->data;
/* 增加引用,使底层存储可以独立于AVFrame继续存在 */
held_buffer = av_buffer_ref (plane_buffer);
if (!held_buffer)
goto end;
av_frame_free (&frame);
/* 检查独立引用仍覆盖原U平面所在位置 */
if (plane_offset < 0 || (size_t) plane_offset >= held_buffer->size)
goto end;
ret = 0;
end:
av_frame_free (&frame);
av_buffer_unref (&held_buffer);
return ret;
}
🌻5.总结
av_frame_get_plane_buffer()根据Plane数据指针返回帧所持有的所属Buffer引用,但不会增加引用计数或复制数据。