PCM转PCMA(pcm_alaw,G711.A率)转换表 && PCM转PCMU(pcm_ulaw,G711.U率)转换表

PCM转PCMA(pcm_alaw,G711.A率)转换表 && PCM转PCMU(pcm_ulaw,G711.U率)转换表

文章目录

ffmpeg源码生成转码表

最近要做一个功能,int16_t的PCM数据转为uint8的PCMA数据,

网上的不太可信,找ffmpeg库,发现ffmpeg库使用的是查表发实现的,

而且还有现成的生成表数据的源代码,可信度非常高

c 复制代码
/*
 * Generate a header file for hardcoded PCM tables
 *
 * Copyright (c) 2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdlib.h>
#define CONFIG_HARDCODED_TABLES 0
#include "pcm_tablegen.h"
#include "tableprint.h"

int main(void)
{
    pcm_alaw_tableinit();
    pcm_ulaw_tableinit();
    pcm_vidc_tableinit();

    write_fileheader();

    WRITE_ARRAY("static const", uint8_t, linear_to_alaw);
    WRITE_ARRAY("static const", uint8_t, linear_to_ulaw);
    WRITE_ARRAY("static const", uint8_t, linear_to_vidc);

    return 0;
}

编译上有些小问题,没太关注,直接使用了注释大法,

没有用的统统干掉,编译通过,执行生成转换映射表。

不用再去研究转换公式,直接对照着表查询结果即可。

拿走不谢

使用方式

可参照ffmpeg源代码pcm.c使用

c 复制代码
    case AV_CODEC_ID_PCM_ALAW:
        for (; n > 0; n--) {
            v      = *samples++;
            *dst++ = linear_to_alaw[(v + 32768) >> 2];
        }
        break;
    case AV_CODEC_ID_PCM_MULAW:
        for (; n > 0; n--) {
            v      = *samples++;
            *dst++ = linear_to_ulaw[(v + 32768) >> 2];
        }
        break;

PCM有符号,加上32768变成正数除以4查表即可。

生成源码

表数据太大,CSDN不让保存,放到我个人服务器上了(大神手下留情不要攻击)。
http://118.126.97.61:8084/src_libs/ffmpeg_pcm_table/

相关推荐
郭涤生2 天前
PCM 详解
pcm
xmRao3 天前
Qt+FFmpeg 实现 PCM 转 WAV
qt·ffmpeg·pcm
deep_drink4 天前
【论文精读(二十五)】PCM:Mamba 首次杀入 3D 点云,线性复杂度吊打 PTv3(ArXiv 2024)
深度学习·神经网络·计算机视觉·3d·pcm·point cloud
xmRao5 天前
Qt 结合 SDL2 实现 PCM 音频文件播放
开发语言·qt·pcm
PGCCC5 天前
PCM认证大师专访 | 马天源
pcm
viqjeee14 天前
SI32178芯片FXS与FXO共用PCM通道的关键技术要点
pcm·si32178
杰瑞不懂代码21 天前
PCM均匀量化与μ-law非均匀量化的仿真对比:误差特性与SNR分析
人工智能·matlab·语音识别·pcm·均匀量化·非均匀量化
Android系统攻城狮21 天前
Android ALSA进阶之处理PCM的ioctl命令snd_pcm_lib_ioctl:用法实例(一百)
android·pcm·alsa·音频进阶
Android系统攻城狮1 个月前
Android ALSA驱动进阶之设置共享内存snd_pcm_lib_mmap_iomem:用法实例(九十九)
android·pcm·音频进阶·alsa驱动·android驱动
扶尔魔ocy1 个月前
【QT window】multimedia+ffmpeg实现(PCM和MP4)录音功能
qt·ffmpeg·pcm