首页 > 其他分享 >av_samples_fill_arrays

av_samples_fill_arrays

时间:2024-02-21 17:44:48浏览次数:32  
标签:size arrays buf nb samples audio data fill

av_samples_fill_arrays是FFmpeg中一个非常重要的函数,用于填充音频数据的指针数组。在音视频处理中,经常需要处理音频数据,而av_samples_fill_arrays可以正确地组织音频数据,以便后续处理和编解码。

av_samples_fill_arrays函数的原型:

/**
 * Fill plane data pointers and linesize for samples with sample
 * format sample_fmt.
 *
 * The audio_data array is filled with the pointers to the samples data planes:
 * for planar, set the start point of each channel's data within the buffer,
 * for packed, set the start point of the entire buffer only.
 *
 * The value pointed to by linesize is set to the aligned size of each
 * channel's data buffer for planar layout, or to the aligned size of the
 * buffer for all channels for packed layout.
 *
 * The buffer in buf must be big enough to contain all the samples
 * (use av_samples_get_buffer_size() to compute its minimum size),
 * otherwise the audio_data pointers will point to invalid data.
 *
 * @see enum AVSampleFormat
 * The documentation for AVSampleFormat describes the data layout.
 *
 * @param[out] audio_data  array to be filled with the pointer for each channel
 * @param[out] linesize    calculated linesize, may be NULL
 * @param buf              the pointer to a buffer containing the samples
 * @param nb_channels      the number of channels
 * @param nb_samples       the number of samples in a single channel
 * @param sample_fmt       the sample format
 * @param align            buffer size alignment (0 = default, 1 = no alignment)
 * @return                 minimum size in bytes required for the buffer on success,
 *                         or a negative error code on failure
 */
int av_samples_fill_arrays(uint8_t **audio_data, int *linesize,
                           const uint8_t *buf,
                           int nb_channels, int nb_samples,
                           enum AVSampleFormat sample_fmt, int align)
{
    int ch, planar, buf_size, line_size;

    planar   = av_sample_fmt_is_planar(sample_fmt);
    buf_size = av_samples_get_buffer_size(&line_size, nb_channels, nb_samples,
                                          sample_fmt, align);
    if (buf_size < 0)
        return buf_size;

    if (linesize)
        *linesize = line_size;

    memset(audio_data, 0, planar
                          ? sizeof(*audio_data) * nb_channels
                          : sizeof(*audio_data));

    if (!buf)
        return buf_size;

    audio_data[0] = (uint8_t *)buf;
    for (ch = 1; planar && ch < nb_channels; ch++)
        audio_data[ch] = audio_data[ch-1] + line_size;

    return buf_size;
}

参数解释:

  • audio_data:指向一个指针数组,用于存储音频数据的指针。/AVFrame.data/
  • linesize:存储每个音频数据指针的大小。/AVFrame.linesize/
  • buf:指向包含音频数据的缓冲区。
  • nb_channels:音频通道数。/AVFrame.channels/
  • nb_samples:每个通道的样本数。/AVFrame.nb_samples/
  • sample_fmt:音频样本格式。/AVFrame.format/
  • align:对齐方式。

正确使用av_samples_fill_arrays函数的关键在于正确理解参数的含义和传入正确的参数值。以下是一个简单的示例代码,演示如何使用av_samples_fill_arrays函数:

#include <libavutil/samplefmt.h>
#include <libavutil/mem.h>
#include <libavutil/opt.h>

void fill_audio_data(uint8_t **audio_data, int *linesize, uint8_t *buf, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt) {
    int ret = av_samples_fill_arrays(audio_data, linesize, buf, nb_channels, nb_samples, sample_fmt, 0);
    if (ret < 0) {
        // 错误处理
        printf("Error filling audio data arrays\n");
    }
}

int main() {
    int nb_samples = 1024;
    int nb_channels = 2;
    enum AVSampleFormat sample_fmt = AV_SAMPLE_FMT_FLTP;
    uint8_t *buf = (uint8_t *)av_malloc(av_samples_get_buffer_size(NULL, nb_channels, nb_samples, sample_fmt, 1));
    uint8_t *audio_data[8]; // 假设最多支持8个通道
    int linesize[8];

    fill_audio_data(audio_data, linesize, buf, nb_channels, nb_samples, sample_fmt);

    // 使用填充好的音频数据进行后续处理

    av_free(buf);
    return 0;
}

在这段示例代码中,首先分配了一个足够大的缓冲区用于存储音频数据,然后调用fill_audio_data函数填充音频数据指针数组。最后可以使用填充好的音频数据进行后续的音频处理操作。

标签:size,arrays,buf,nb,samples,audio,data,fill
From: https://www.cnblogs.com/faithlocus/p/18025852

相关文章

  • [Rust] Arrays in Rust
    InthislessonwetakealookatArraysandthedifferentwaysofcreatingthem.ArraysinRustarecollectionsofvaluesofthesametypethatcannotchangeinsize.Inotherwords,thenumberoffields(orelements)hastobeknownatcompiletime.#[al......
  • C1. Good Subarrays (Easy Version)
    找子数组的个数双指针#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;constintN=2e5+10;inta[N];voidsolve(){ intn; cin>>n; for(inti=1;i<=n;i++)cin>>a[i]; intl=1,r=1; intans=0; while(l<=r){ if(l>n||r>......
  • Educational Codeforces Round 145 (Rated for Div. 2)C. Sum on Subarrays(构造)
    很意思的一道构造题题意:给一个\(n、k\),让构造长度为n的数组满足,子数组为整数的个数为k个,负数的为\(k-(n+1)*n/2\),每个数的范围为\([-1000,1000]\)这种构造题可以考虑就是前一段可以一直用一样的、最小的。我们观察可以发现\(k+k-(n+1)*n/2=(n+1)*n/2\)也就是所有子数组......
  • offline 2 online | 重要性采样,把 offline + online 数据化为 on-policy samples
    论文标题:Offline-to-OnlineReinforcementLearningviaBalancedReplayandPessimisticQ-EnsembleCoRL2021,4个weakaccept。pdf:https://arxiv.org/pdf/2107.00591.pdfhtml:https://ar5iv.labs.arxiv.org/html/2107.00591openreview:https://openreview.net/forum?id=......
  • #排列组合#CF1550D Excellent Arrays
    洛谷传送门CF1550D分析对于excellent的\(a\)来说\(|a_i-i|=x\)的值是固定的,考虑枚举它一半正一半负时函数值是最大的,当\(n\)为奇数时要分为两种情况(不过可以通过杨辉三角合并)问题是,由于\(l,r\)的范围,并不能做到所有位置都能可正可负,不过不超过\(mn=\min\{1-l,r-n\}......
  • 无涯教程-Passing arrays to 函数s
    下面的示例更好地解释了此概念。Passingarraystofunctions-示例varnames=newArray("Mary","Tom","Jack","Jill")functiondisp(arr_names){for(vari=0;i<arr_names.length;i++){console.log(names[i])......
  • [LeetCode] 2966. Divide Array Into Arrays With Max Difference
    Youaregivenanintegerarraynumsofsizenandapositiveintegerk.Dividethearrayintooneormorearraysofsize3satisfyingthefollowingconditions:Eachelementofnumsshouldbeinexactlyonearray.Thedifferencebetweenanytwoelementsin......
  • 无涯教程-Scala - 数组(Arrays)
    Scala提供了一种数据结构数组,它存储了相同类型元素的固定大小的顺序集合。声明数组要在程序中使用数组,必须声明一个变量以引用该数组,并且必须指定该变量可以引用的数组的类型。varz:Array[String]=newArray[String](3)orvarz=newArray[String](3)在此,z被声明为可容......
  • 15 Friendly Arrays
    FriendlyArrays打表#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;voidsolve(){ intn,m; cin>>n>>m; vector<int>a(n+1); vector<int>b(m+1); for(inti=1;i<=n;i++)cin>>a[i]; for(inti=1;i<......
  • C++中setw和setfill函数的结合应用
    一、头文件头文件为#include<iomanip>其中io代表输入输出,manip是manipulator(操纵器)的缩写iomanip的作用:主要是对cin,cout之类的一些操纵运算子,比如setfill,setw,setbase,setprecision等等。它是I/O流控制头文件,就像C里面的格式化输出一样。二、setw函数s......