Browse Source

add:添加G711音频编解码
https://gitee.com/openLuat/LuatOS/issues/ICMKRP?from=project-issue&search_text=g71://gitee.com/openLuat/LuatOS/issues/ICMKRP?from=project-issue&search_text=g711

liustu 5 months ago
parent
commit
433bcfce0b

+ 526 - 0
components/multimedia/g711_codec/g711_codec.c

@@ -0,0 +1,526 @@
+// 刘斌修改 start - G711编解码实现
+// 内存优化:使用动态分配查找表
+// 支持μ-law和A-law编解码
+
+#include "../../luat/include/luat_base.h"
+#include "../luat_multimedia_codec.h"
+#include "../../luat/include/luat_mem.h"
+#include <stdbool.h>
+
+#define LUAT_LOG_TAG "g711"
+#include "luat_log.h"
+
+// G711编解码器结构体 - 包含动态分配的查找表
+typedef struct {
+    uint8_t type;        // ULAW or ALAW
+    uint8_t is_encoder;  // 是否为编码器
+    uint32_t sample_rate; // 采样率
+    uint32_t frame_count; // 处理的帧数统计
+
+    // 动态分配的查找表
+    uint8_t* linear_to_alaw;    // A-law编码表
+    uint8_t* linear_to_ulaw;    // μ-law编码表
+    int16_t* ulaw_decode_table; // μ-law解码表
+    int16_t* alaw_decode_table; // A-law解码表
+    bool tables_initialized;    // 查找表是否已初始化
+} g711_codec_t;
+
+// μ-law编码函数 - 用于生成查找表
+static int ulaw2linear(unsigned char u_val)
+{
+    int t;
+    u_val = ~u_val;
+    t = ((u_val & 0x0F) << 3) + 0x84;
+    t <<= ((unsigned)u_val & 0x70) >> 4;
+    return (u_val & 0x80) ? (0x84 - t) : (t - 0x84);
+}
+
+// A-law编码函数 - 用于生成查找表
+static int alaw2linear(unsigned char a_val)
+{
+    int t;
+    int seg;
+    a_val ^= 0x55;
+    t = a_val & 0x0F;
+    seg = ((unsigned)a_val & 0x70) >> 4;
+    if(seg)
+        t = (t + t + 1 + 32) << (seg + 2);
+    else
+        t = (t + t + 1) << 3;
+    return (a_val & 0x80) ? t : -t;
+}
+
+// 生成μ-law编码查找表
+static void build_ulaw_table(uint8_t* linear_to_ulaw)
+{
+    int i, j, v, v1, v2;
+
+    j = 1;
+    linear_to_ulaw[8192] = 0xFF;
+
+    for(i = 0; i < 127; i++) {
+        v1 = ulaw2linear(i ^ 0xFF);
+        v2 = ulaw2linear((i + 1) ^ 0xFF);
+        v = (v1 + v2 + 4) >> 3;
+
+        for(; j < v; j += 1) {
+            linear_to_ulaw[8192 - j] = (i ^ (0xFF ^ 0x80));
+            linear_to_ulaw[8192 + j] = (i ^ 0xFF);
+        }
+    }
+
+    for(; j < 8192; j++) {
+        linear_to_ulaw[8192 - j] = (127 ^ (0xFF ^ 0x80));
+        linear_to_ulaw[8192 + j] = (127 ^ 0xFF);
+    }
+
+    linear_to_ulaw[0] = linear_to_ulaw[1];
+}
+
+// 生成A-law编码查找表
+static void build_alaw_table(uint8_t* linear_to_alaw)
+{
+    int i, j, v, v1, v2;
+
+    j = 1;
+    linear_to_alaw[8192] = 0xD5;
+
+    for(i = 0; i < 127; i++) {
+        v1 = alaw2linear(i ^ 0xD5);
+        v2 = alaw2linear((i + 1) ^ 0xD5);
+        v = (v1 + v2 + 4) >> 3;
+
+        for(; j < v; j += 1) {
+            linear_to_alaw[8192 - j] = (i ^ (0xD5 ^ 0x80));
+            linear_to_alaw[8192 + j] = (i ^ 0xD5);
+        }
+    }
+
+    for(; j < 8192; j++) {
+        linear_to_alaw[8192 - j] = (127 ^ (0xD5 ^ 0x80));
+        linear_to_alaw[8192 + j] = (127 ^ 0xD5);
+    }
+
+    linear_to_alaw[0] = linear_to_alaw[1];
+}
+
+// 初始化μ-law解码查找表
+static void build_ulaw_decode_table(int16_t* ulaw_decode_table)
+{
+    // 标准的μ-law解码表数据
+    const int16_t ulaw_data[256] = {
+        -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
+        -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
+        -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
+        -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
+        -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
+        -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
+        -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
+        -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
+        -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
+        -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
+        -876, -844, -812, -780, -748, -716, -684, -652,
+        -620, -588, -556, -524, -492, -460, -428, -396,
+        -372, -356, -340, -324, -308, -292, -276, -260,
+        -244, -228, -212, -196, -180, -164, -148, -132,
+        -120, -112, -104, -96, -88, -80, -72, -64,
+        -56, -48, -40, -32, -24, -16, -8, 0,
+        32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
+        23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
+        15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
+        11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
+        7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
+        5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
+        3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
+        2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
+        1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
+        1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
+        876, 844, 812, 780, 748, 716, 684, 652,
+        620, 588, 556, 524, 492, 460, 428, 396,
+        372, 356, 340, 324, 308, 292, 276, 260,
+        244, 228, 212, 196, 180, 164, 148, 132,
+        120, 112, 104, 96, 88, 80, 72, 64,
+        56, 48, 40, 32, 24, 16, 8, 0
+    };
+
+    // 复制数据到动态分配的表
+    for(int i = 0; i < 256; i++) {
+        ulaw_decode_table[i] = ulaw_data[i];
+    }
+}
+
+// 初始化A-law解码查找表
+static void build_alaw_decode_table(int16_t* alaw_decode_table)
+{
+    // 标准的A-law解码表数据
+    const int16_t alaw_data[256] = {
+        -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
+        -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
+        -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
+        -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
+        -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
+        -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
+        -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
+        -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
+        -344, -328, -376, -360, -280, -264, -312, -296,
+        -472, -456, -504, -488, -408, -392, -440, -424,
+        -88, -72, -120, -104, -24, -8, -56, -40,
+        -216, -200, -248, -232, -152, -136, -184, -168,
+        -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
+        -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
+        -688, -656, -752, -720, -560, -528, -624, -592,
+        -944, -912, -1008, -976, -816, -784, -880, -848,
+        5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
+        7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
+        2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
+        3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
+        22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
+        30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
+        11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
+        15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
+        344, 328, 376, 360, 280, 264, 312, 296,
+        472, 456, 504, 488, 408, 392, 440, 424,
+        88, 72, 120, 104, 24, 8, 56, 40,
+        216, 200, 248, 232, 152, 136, 184, 168,
+        1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
+        1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
+        688, 656, 752, 720, 560, 528, 624, 592,
+        944, 912, 1008, 976, 816, 784, 880, 848
+    };
+
+    // 复制数据到动态分配的表
+    for(int i = 0; i < 256; i++) {
+        alaw_decode_table[i] = alaw_data[i];
+    }
+}
+
+// 初始化所有查找表
+static int g711_tables_init(g711_codec_t* codec)
+{
+    if (!codec || codec->tables_initialized) {
+        return 0;
+    }
+
+    // 分配编码查找表内存
+    codec->linear_to_alaw = (uint8_t*)luat_heap_malloc(16384);
+    codec->linear_to_ulaw = (uint8_t*)luat_heap_malloc(16384);
+
+    // 分配解码查找表内存
+    codec->ulaw_decode_table = (int16_t*)luat_heap_malloc(256 * sizeof(int16_t));
+    codec->alaw_decode_table = (int16_t*)luat_heap_malloc(256 * sizeof(int16_t));
+
+    // 检查内存分配是否成功
+    if (!codec->linear_to_alaw || !codec->linear_to_ulaw ||
+        !codec->ulaw_decode_table || !codec->alaw_decode_table) {
+        // 内存分配失败,清理已分配的内存
+        if (codec->linear_to_alaw) luat_heap_free(codec->linear_to_alaw);
+        if (codec->linear_to_ulaw) luat_heap_free(codec->linear_to_ulaw);
+        if (codec->ulaw_decode_table) luat_heap_free(codec->ulaw_decode_table);
+        if (codec->alaw_decode_table) luat_heap_free(codec->alaw_decode_table);
+
+        codec->linear_to_alaw = NULL;
+        codec->linear_to_ulaw = NULL;
+        codec->ulaw_decode_table = NULL;
+        codec->alaw_decode_table = NULL;
+
+        LLOGE("G711 tables memory allocation failed");
+        return -1;
+    }
+
+    // 生成查找表
+    build_ulaw_table(codec->linear_to_ulaw);
+    build_alaw_table(codec->linear_to_alaw);
+    build_ulaw_decode_table(codec->ulaw_decode_table);
+    build_alaw_decode_table(codec->alaw_decode_table);
+
+    codec->tables_initialized = true;
+    return 0;
+}
+
+// 释放查找表内存
+static void g711_tables_cleanup(g711_codec_t* codec)
+{
+    if (codec) {
+        if (codec->linear_to_alaw) {
+            luat_heap_free(codec->linear_to_alaw);
+            codec->linear_to_alaw = NULL;
+        }
+        if (codec->linear_to_ulaw) {
+            luat_heap_free(codec->linear_to_ulaw);
+            codec->linear_to_ulaw = NULL;
+        }
+        if (codec->ulaw_decode_table) {
+            luat_heap_free(codec->ulaw_decode_table);
+            codec->ulaw_decode_table = NULL;
+        }
+        if (codec->alaw_decode_table) {
+            luat_heap_free(codec->alaw_decode_table);
+            codec->alaw_decode_table = NULL;
+        }
+        codec->tables_initialized = false;
+    }
+}
+
+// 优化的G711 μ-law编码函数 - 使用动态查找表
+static inline uint8_t g711_ulaw_encode_optimized(g711_codec_t* codec, int16_t pcm_sample)
+{
+    int index = (pcm_sample + 32768) >> 2;
+    if(index < 0) index = 0;
+    if(index >= 16384) index = 16383;
+    return codec->linear_to_ulaw[index];
+}
+
+// 优化的G711 A-law编码函数 - 使用动态查找表
+static inline uint8_t g711_alaw_encode_optimized(g711_codec_t* codec, int16_t pcm_sample)
+{
+    int index = (pcm_sample + 32768) >> 2;
+    if(index < 0) index = 0;
+    if(index >= 16384) index = 16383;
+    return codec->linear_to_alaw[index];
+}
+
+// 标准G711 μ-law解码函数 - 使用动态查找表
+static inline int16_t g711_ulaw_decode_optimized(g711_codec_t* codec, uint8_t ulaw_byte) {
+    return codec->ulaw_decode_table[ulaw_byte];
+}
+
+// 标准G711 A-law解码函数 - 使用动态查找表
+static inline int16_t g711_alaw_decode_optimized(g711_codec_t* codec, uint8_t alaw_byte) {
+    return codec->alaw_decode_table[alaw_byte];
+}
+
+// 批量μ-law编码 - 使用循环展开优化
+static void g711_ulaw_encode_batch(g711_codec_t* codec, const int16_t* pcm, uint8_t* output, uint32_t len) {
+    uint32_t i;
+    // 循环展开优化,每次处理4个样本
+    for (i = 0; i < len - 3; i += 4) {
+        output[i] = g711_ulaw_encode_optimized(codec, pcm[i]);
+        output[i+1] = g711_ulaw_encode_optimized(codec, pcm[i+1]);
+        output[i+2] = g711_ulaw_encode_optimized(codec, pcm[i+2]);
+        output[i+3] = g711_ulaw_encode_optimized(codec, pcm[i+3]);
+    }
+    // 处理剩余样本
+    for (; i < len; i++) {
+        output[i] = g711_ulaw_encode_optimized(codec, pcm[i]);
+    }
+}
+
+// 批量A-law编码 - 使用循环展开优化
+static void g711_alaw_encode_batch(g711_codec_t* codec, const int16_t* pcm, uint8_t* output, uint32_t len) {
+    uint32_t i;
+    // 循环展开优化,每次处理4个样本
+    for (i = 0; i < len - 3; i += 4) {
+        output[i] = g711_alaw_encode_optimized(codec, pcm[i]);
+        output[i+1] = g711_alaw_encode_optimized(codec, pcm[i+1]);
+        output[i+2] = g711_alaw_encode_optimized(codec, pcm[i+2]);
+        output[i+3] = g711_alaw_encode_optimized(codec, pcm[i+3]);
+    }
+    // 处理剩余样本
+    for (; i < len; i++) {
+        output[i] = g711_alaw_encode_optimized(codec, pcm[i]);
+    }
+}
+
+// 批量μ-law解码 - 使用循环展开优化
+static void g711_ulaw_decode_batch(g711_codec_t* codec, const uint8_t* input, int16_t* pcm, uint32_t len) {
+    uint32_t i;
+    // 循环展开优化,每次处理4个样本
+    for (i = 0; i < len - 3; i += 4) {
+        pcm[i] = g711_ulaw_decode_optimized(codec, input[i]);
+        pcm[i+1] = g711_ulaw_decode_optimized(codec, input[i+1]);
+        pcm[i+2] = g711_ulaw_decode_optimized(codec, input[i+2]);
+        pcm[i+3] = g711_ulaw_decode_optimized(codec, input[i+3]);
+    }
+    // 处理剩余样本
+    for (; i < len; i++) {
+        pcm[i] = g711_ulaw_decode_optimized(codec, input[i]);
+    }
+}
+
+// 批量A-law解码 - 使用循环展开优化
+static void g711_alaw_decode_batch(g711_codec_t* codec, const uint8_t* input, int16_t* pcm, uint32_t len) {
+    uint32_t i;
+    // 循环展开优化,每次处理4个样本
+    for (i = 0; i < len - 3; i += 4) {
+        pcm[i] = g711_alaw_decode_optimized(codec, input[i]);
+        pcm[i+1] = g711_alaw_decode_optimized(codec, input[i+1]);
+        pcm[i+2] = g711_alaw_decode_optimized(codec, input[i+2]);
+        pcm[i+3] = g711_alaw_decode_optimized(codec, input[i+3]);
+    }
+    // 处理剩余样本
+    for (; i < len; i++) {
+        pcm[i] = g711_alaw_decode_optimized(codec, input[i]);
+    }
+}
+
+// 创建G711解码器 - 优化内存分配
+void* g711_decoder_create(uint8_t type) {
+    // 验证类型
+    if (type != LUAT_MULTIMEDIA_DATA_TYPE_ULAW && type != LUAT_MULTIMEDIA_DATA_TYPE_ALAW) {
+        return NULL;
+    }
+
+    g711_codec_t* decoder = (g711_codec_t*)luat_heap_malloc(sizeof(g711_codec_t));
+    if (decoder) {
+        // 初始化结构体
+        decoder->type = type;
+        decoder->is_encoder = 0;
+        decoder->sample_rate = 8000; // 默认采样率
+        decoder->frame_count = 0;
+        decoder->linear_to_alaw = NULL;
+        decoder->linear_to_ulaw = NULL;
+        decoder->ulaw_decode_table = NULL;
+        decoder->alaw_decode_table = NULL;
+        decoder->tables_initialized = false;
+
+        // 初始化查找表
+        if (g711_tables_init(decoder) != 0) {
+            luat_heap_free(decoder);
+            return NULL;
+        }
+    } else {
+        LLOGE("G711 decoder memory allocation failed");
+    }
+    return decoder;
+}
+
+// 销毁G711解码器 - 安全释放内存
+void g711_decoder_destroy(void* decoder) {
+    if (decoder) {
+        g711_codec_t* g711 = (g711_codec_t*)decoder;
+        g711_tables_cleanup(g711);
+        luat_heap_free(decoder);
+    }
+}
+
+// G711解码数据 - 批量处理提高效率
+int g711_decoder_get_data(void* decoder, const uint8_t* input, uint32_t len,
+                           int16_t* pcm, uint32_t* out_len, uint32_t* used) {
+    g711_codec_t* g711 = (g711_codec_t*)decoder;
+
+    // 参数验证
+    if (!g711 || !input || !pcm || !out_len || !used) {
+        LLOGE("G711 decoder invalid parameters");
+        return -1;
+    }
+
+    if (len == 0) {
+        *out_len = 0;
+        *used = 0;
+        return 0;
+    }
+
+    *out_len = 0;
+    *used = len;
+
+    // 批量解码G711数据为PCM
+    if (g711->type == LUAT_MULTIMEDIA_DATA_TYPE_ULAW) {
+        g711_ulaw_decode_batch(g711, input, pcm, len);
+    } else if (g711->type == LUAT_MULTIMEDIA_DATA_TYPE_ALAW) {
+        g711_alaw_decode_batch(g711, input, pcm, len);
+    } else {
+        return -1;
+    }
+
+    *out_len = len * 2;  // 16位PCM,每样本2字节
+
+    return 1;
+}
+
+// 创建G711编码器 - 优化内存分配
+void* g711_encoder_create(uint8_t type) {
+    // 验证类型
+    if (type != LUAT_MULTIMEDIA_DATA_TYPE_ULAW && type != LUAT_MULTIMEDIA_DATA_TYPE_ALAW) {
+        return NULL;
+    }
+
+    g711_codec_t* encoder = (g711_codec_t*)luat_heap_malloc(sizeof(g711_codec_t));
+    if (encoder) {
+        // 初始化结构体
+        encoder->type = type;
+        encoder->is_encoder = 1;
+        encoder->sample_rate = 8000; // 默认采样率
+        encoder->frame_count = 0;
+        encoder->linear_to_alaw = NULL;
+        encoder->linear_to_ulaw = NULL;
+        encoder->ulaw_decode_table = NULL;
+        encoder->alaw_decode_table = NULL;
+        encoder->tables_initialized = false;
+
+        // 初始化查找表
+        if (g711_tables_init(encoder) != 0) {
+            luat_heap_free(encoder);
+            return NULL;
+        }
+    } else {
+        LLOGE("G711 encoder memory allocation failed");
+    }
+    return encoder;
+}
+
+// 销毁G711编码器 - 安全释放内存
+void g711_encoder_destroy(void* encoder) {
+    if (encoder) {
+        g711_codec_t* g711 = (g711_codec_t*)encoder;
+        g711_tables_cleanup(g711);
+        luat_heap_free(encoder);
+    }
+}
+
+// G711编码数据 - 批量处理提高效率,仅支持16位PCM输入
+int g711_encoder_get_data(void* encoder, const int16_t* pcm, uint32_t len,
+                           uint8_t* output, uint32_t* out_len) {
+    g711_codec_t* g711 = (g711_codec_t*)encoder;
+
+    // 参数验证
+    if (!g711 || !pcm || !output || !out_len) {
+        return -1;
+    }
+
+    if (len == 0) {
+        *out_len = 0;
+        return 0;
+    }
+
+    *out_len = 0;
+
+    // 批量编码PCM数据为G711
+    if (g711->type == LUAT_MULTIMEDIA_DATA_TYPE_ULAW) {
+        g711_ulaw_encode_batch(g711, pcm, output, len);
+    } else if (g711->type == LUAT_MULTIMEDIA_DATA_TYPE_ALAW) {
+        g711_alaw_encode_batch(g711, pcm, output, len);
+    } else {
+        return -1;
+    }
+
+    *out_len = len;  // 8位G711,每样本1字节
+
+    return 1;
+}
+
+// 性能统计函数
+void g711_get_stats(void* codec, uint32_t* sample_rate, uint32_t* frame_count) {
+    g711_codec_t* g711 = (g711_codec_t*)codec;
+    if (g711) {
+        if (sample_rate) *sample_rate = g711->sample_rate;
+        if (frame_count) *frame_count = g711->frame_count;
+    }
+}
+
+// 重置统计信息
+void g711_reset_stats(void* codec) {
+    g711_codec_t* g711 = (g711_codec_t*)codec;
+    if (g711) {
+        g711->frame_count = 0;
+    }
+}
+
+// 设置采样率
+void g711_set_sample_rate(void* codec, uint32_t sample_rate) {
+    g711_codec_t* g711 = (g711_codec_t*)codec;
+    if (g711) {
+        g711->sample_rate = sample_rate;
+    }
+}
+
+// 刘斌修改 end

+ 44 - 0
components/multimedia/g711_codec/g711_codec.h

@@ -0,0 +1,44 @@
+// 刘斌修改 - G711编解码器头文件
+// 定义G711编解码器的接口和数据结构
+
+#ifndef G711_CODEC_H
+#define G711_CODEC_H
+
+#include "luat_base.h"
+#include "luat_mem.h"
+#include "luat_log.h"
+
+// G711编解码器类型
+typedef enum {
+    G711_TYPE_ULAW = 0,  // μ-law编码
+    G711_TYPE_ALAW = 1   // A-law编码
+} g711_codec_type_t;
+
+// G711编解码器结构体
+typedef struct {
+    g711_codec_type_t type;  // 编解码器类型
+    uint32_t sample_rate;    // 采样率(固定为8000Hz)
+    uint32_t channels;       // 声道数(固定为1)
+    uint32_t bits_per_sample; // 位深度(固定为16位PCM)
+} g711_codec_t;
+
+// 编解码器创建和销毁函数
+void* g711_decoder_create(uint8_t type);
+void g711_decoder_destroy(void* decoder);
+void* g711_encoder_create(uint8_t type);
+void g711_encoder_destroy(void* encoder);
+
+// 编解码数据函数
+int g711_decoder_get_data(void* decoder, const uint8_t* input, uint32_t len,
+                          int16_t* pcm, uint32_t* out_len, uint32_t* used);
+int g711_encoder_get_data(void* encoder, const int16_t* pcm, uint32_t len,
+                          uint8_t* output, uint32_t* out_len);
+
+// 底层编解码函数
+uint8_t g711_ulaw_encode(int16_t pcm_sample);
+uint8_t g711_alaw_encode(int16_t pcm_sample);
+int16_t g711_ulaw_decode(uint8_t ulaw_byte);
+int16_t g711_alaw_decode(uint8_t alaw_byte);
+
+#endif // G711_CODEC_H
+// 刘斌修改

+ 162 - 2
components/multimedia/luat_lib_multimedia_codec.c

@@ -19,6 +19,15 @@
 #include "interf_enc.h"
 #include "interf_dec.h"
 #endif
+
+#ifdef LUAT_USE_AUDIO_G711
+#include "g711_codec/g711_codec.h"
+#endif
+
+#ifndef G711_PCM_SAMPLES
+#define G711_PCM_SAMPLES 160
+#endif
+
 #ifndef MINIMP3_MAX_SAMPLES_PER_FRAME
 #define MINIMP3_MAX_SAMPLES_PER_FRAME (2*1152)
 #endif
@@ -60,6 +69,17 @@ static int l_codec_create(lua_State *L) {
             		return 1;
             	}
             	break;
+#ifdef LUAT_USE_AUDIO_G711
+         	case LUAT_MULTIMEDIA_DATA_TYPE_ULAW:
+         	case LUAT_MULTIMEDIA_DATA_TYPE_ALAW:
+             	// 使用专门的g711_codec字段存储G711解码器
+             	coder->g711_codec = g711_decoder_create(type);
+             	if (!coder->g711_codec) {
+             		lua_pushnil(L);
+             		return 1;
+             	}
+             	break;
+ #endif
         	}
 
     	}
@@ -89,8 +109,19 @@ static int l_codec_create(lua_State *L) {
             		lua_pushnil(L);
             		return 1;
             	}
-            	break;
+            	        		break;
 #endif
+#endif
+#ifdef LUAT_USE_AUDIO_G711
+         	case LUAT_MULTIMEDIA_DATA_TYPE_ULAW:
+         	case LUAT_MULTIMEDIA_DATA_TYPE_ALAW:
+             	// 使用专门的g711_codec字段存储G711编码器
+             	coder->g711_codec = g711_encoder_create(type);
+             	if (!coder->g711_codec) {
+             		lua_pushnil(L);
+             		return 1;
+             	}
+             	break;
 #endif
 
         	default:
@@ -209,6 +240,17 @@ static int l_codec_get_audio_info(lua_State *L) {
 				LLOGD("head error");
 			}
 			break;
+#ifdef LUAT_USE_AUDIO_G711
+		case LUAT_MULTIMEDIA_DATA_TYPE_ULAW:
+		case LUAT_MULTIMEDIA_DATA_TYPE_ALAW:
+			// G711固定参数:8kHz采样率, 单声道, 8位深度
+			sample_rate = 8000;
+			num_channels = 1;
+			bits_per_sample = 8;
+			audio_format = LUAT_MULTIMEDIA_DATA_TYPE_PCM;
+			result = 1;
+			break;
+#endif
 		default:
 			break;
 		}
@@ -341,6 +383,41 @@ GET_MP3_DATA:
 			}
 
 			break;
+#ifdef LUAT_USE_AUDIO_G711
+		case LUAT_MULTIMEDIA_DATA_TYPE_ULAW:
+		case LUAT_MULTIMEDIA_DATA_TYPE_ALAW:
+			// 动态分配缓冲区
+			if (!coder->buff.addr) {
+				coder->buff.addr = luat_heap_malloc(G711_PCM_SAMPLES);  // G711每帧160字节
+				coder->buff.len = G711_PCM_SAMPLES;
+				coder->buff.used = 0;
+			}
+
+			// 读取G711数据
+			if (coder->buff.used < G711_PCM_SAMPLES) {
+				read_len = luat_fs_fread((void*)(coder->buff.addr + coder->buff.used),
+									G711_PCM_SAMPLES, 1, coder->fd);
+				if (read_len > 0) {
+					coder->buff.used += read_len;
+				} else {
+					is_not_end = 0;
+				}
+			}
+
+			// 解码G711数据为PCM
+ 			if (coder->buff.used >= G711_PCM_SAMPLES) {
+ 				result = g711_decoder_get_data(coder->g711_codec, coder->buff.addr,
+ 											coder->buff.used, (int16_t*)out_buff->addr + out_buff->used,
+ 											&out_len, &used);
+				if (result > 0) {
+					out_buff->used += out_len;
+				}
+				// 移动缓冲区数据
+				memmove(coder->buff.addr, coder->buff.addr + used, coder->buff.used - used);
+				coder->buff.used -= used;
+			}
+			break;
+#endif
 		default:
 			break;
 		}
@@ -373,11 +450,75 @@ static int l_codec_encode_audio_data(lua_State *L) {
 		in_buff = ((luat_zbuff_t *)lua_touserdata(L, 2));
 	}
 	luat_zbuff_t *out_buff = ((luat_zbuff_t *)luaL_checkudata(L, 3, LUAT_ZBUFF_TYPE));
-	if (!coder || !in_buff || !out_buff || (coder->type != LUAT_MULTIMEDIA_DATA_TYPE_AMR_NB && coder->type != LUAT_MULTIMEDIA_DATA_TYPE_AMR_WB) || coder->is_decoder)
+	if (!coder || !in_buff || !out_buff || (coder->type != LUAT_MULTIMEDIA_DATA_TYPE_AMR_NB && coder->type != LUAT_MULTIMEDIA_DATA_TYPE_AMR_WB && coder->type != LUAT_MULTIMEDIA_DATA_TYPE_ULAW && coder->type != LUAT_MULTIMEDIA_DATA_TYPE_ALAW) || coder->is_decoder)
 	{
 		lua_pushboolean(L, 0);
 		return 1;
 	}
+#ifdef LUAT_USE_AUDIO_G711
+	if (coder->type == LUAT_MULTIMEDIA_DATA_TYPE_ULAW || coder->type == LUAT_MULTIMEDIA_DATA_TYPE_ALAW) {
+		// G711编码处理 - 使用栈上分配的临时缓冲区,避免长期占用堆内存
+		uint8_t outbuf[G711_PCM_SAMPLES];  // 栈上分配,用完即释放
+		int16_t *pcm = (int16_t *)in_buff->addr;
+		uint32_t total_len = in_buff->used >> 1;  // 16位PCM转字节数
+		uint32_t done_len = 0;
+		uint32_t frame_size = G711_PCM_SAMPLES;  // G711每帧160个PCM样本
+		uint32_t out_len;
+
+		// 处理完整的160样本帧
+		while ((total_len - done_len) >= frame_size) {
+			// 编码一帧PCM数据为G711
+			int result = g711_encoder_get_data(coder->g711_codec, &pcm[done_len], frame_size,
+											 outbuf, &out_len);
+
+			if (result > 0 && out_len > 0) {
+				// 检查输出缓冲区空间
+				if ((out_buff->len - out_buff->used) < out_len) {
+					if (__zbuff_resize(out_buff, out_buff->len * 2 + out_len)) {
+						lua_pushboolean(L, 0);
+						return 1;
+					}
+				}
+				// 复制编码后的数据到输出缓冲区
+				memcpy(out_buff->addr + out_buff->used, outbuf, out_len);
+				out_buff->used += out_len;
+			} else {
+
+			}
+			done_len += frame_size;
+		}
+
+		// 处理剩余的PCM样本(不足160个样本的部分)
+		uint32_t remaining_len = total_len - done_len;
+		if (remaining_len > 0) {
+
+			// 用零填充到160个样本
+			int16_t padded_frame[G711_PCM_SAMPLES] = {0};
+			memcpy(padded_frame, &pcm[done_len], remaining_len * sizeof(int16_t));
+
+			int result = g711_encoder_get_data(coder->g711_codec, padded_frame, frame_size,
+											 outbuf, &out_len);
+
+			if (result > 0 && out_len > 0) {
+				// 检查输出缓冲区空间
+				if ((out_buff->len - out_buff->used) < out_len) {
+					if (__zbuff_resize(out_buff, out_buff->len * 2 + out_len)) {
+						lua_pushboolean(L, 0);
+						return 1;
+					}
+				}
+				// 复制编码后的数据到输出缓冲区
+				memcpy(out_buff->addr + out_buff->used, outbuf, out_len);
+				out_buff->used += out_len;
+			}
+		}
+
+		lua_pushboolean(L, 1);
+		return 1;
+	}
+#endif
+
+	// AMR编码处理
 	uint8_t outbuf[128];
 	int16_t *pcm = (int16_t *)in_buff->addr;
 	uint32_t total_len = in_buff->used >> 1;
@@ -502,6 +643,21 @@ static int l_codec_gc(lua_State *L)
 		}
 		break;
 #endif
+#endif
+#ifdef LUAT_USE_AUDIO_G711
+ 	case LUAT_MULTIMEDIA_DATA_TYPE_ULAW:
+ 	case LUAT_MULTIMEDIA_DATA_TYPE_ALAW:
+ 		if (coder->g711_codec) {
+ 			if (coder->is_decoder) {
+ 				// 清理G711解码器
+ 				g711_decoder_destroy(coder->g711_codec);
+ 			} else {
+ 				// 清理G711编码器
+ 				g711_encoder_destroy(coder->g711_codec);
+ 			}
+ 			coder->g711_codec = NULL;
+ 		}
+ 		break;
 #endif
 	}
     return 0;
@@ -538,6 +694,10 @@ static const rotable_Reg_t reg_codec[] =
 	{ "VDDA_3V3",        ROREG_INT(LUAT_CODEC_VDDA_3V3)},
 	//@const VDDA_1V8 number codec 电压: 1.8V
 	{ "VDDA_1V8",        ROREG_INT(LUAT_CODEC_VDDA_1V8)},
+	//@const ULAW number G711 μ-law格式
+	{ "ULAW",            ROREG_INT(LUAT_MULTIMEDIA_DATA_TYPE_ULAW)},
+	//@const ALAW number G711 A-law格式
+	{ "ALAW",            ROREG_INT(LUAT_MULTIMEDIA_DATA_TYPE_ALAW)},
 
 	{ NULL,              ROREG_INT(0)}
 };

+ 15 - 0
components/multimedia/luat_multimedia_codec.h

@@ -17,6 +17,8 @@
 
 #define MP3_MAX_CODED_FRAME_SIZE 1792
 
+#define G711_PCM_SAMPLES 160
+
 enum{
 	LUAT_MULTIMEDIA_DATA_TYPE_NONE,
 	LUAT_MULTIMEDIA_DATA_TYPE_PCM,
@@ -57,6 +59,7 @@ typedef struct{
 		void *mp3_decoder;
 		uint32_t read_len;
 		void *amr_coder;
+		void *g711_codec;
 	};
 	FILE* fd;
 #ifdef __LUATOS__
@@ -78,6 +81,18 @@ void mp3_decoder_set_debug(void *decoder, uint8_t onoff);
 int mp3_decoder_get_info(void *decoder, const uint8_t *input, uint32_t len, uint32_t *hz, uint8_t *channel);
 int mp3_decoder_get_data(void *decoder, const uint8_t *input, uint32_t len, int16_t *pcm, uint32_t *out_len, uint32_t *hz, uint32_t *used);
 
+void* g711_decoder_create(uint8_t type);
+void g711_decoder_destroy(void* decoder);
+int g711_decoder_get_data(void* decoder, const uint8_t* input, uint32_t len,
+                          int16_t* pcm, uint32_t* out_len, uint32_t* used);
+void* g711_encoder_create(uint8_t type);
+void g711_encoder_destroy(void* encoder);
+int g711_encoder_get_data(void* encoder, const int16_t* pcm, uint32_t len,
+                          uint8_t* output, uint32_t* out_len);
+void g711_get_stats(void* codec, uint32_t* sample_rate, uint32_t* frame_count);
+void g711_reset_stats(void* codec);
+void g711_set_sample_rate(void* codec, uint32_t sample_rate);
+
 #ifdef __LUATOS__
 int l_multimedia_raw_handler(lua_State *L, void* ptr);
 #endif

+ 219 - 0
luat/demo/g711/main.lua

@@ -0,0 +1,219 @@
+
+
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "i2sdemo"
+VERSION = "1.0.0"
+
+--[[
+本demo暂时只在air101/103/601测试过
+对于EC618系列的模块,例如Air780E/Air700E,请使用audio库进行快捷播放
+
+本demo需要外挂ES8311 codec芯片, 可使用海凌科的w800音频开发板进行测试
+
+https://detail.tmall.com/item.htm?abbucket=2&id=670202333872
+]]
+
+-- sys库是标配
+sys = require("sys")
+
+if wdt then
+    --添加硬狗防止程序卡死,在支持的设备上启用这个功能
+    wdt.init(9000)--初始化watchdog设置为9s
+    sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
+end
+
+pins.setup(58, "I2C0_SDA")
+pins.setup(57, "I2C0_SCL")
+log.info("i2c", "复用" )
+
+sys.taskInit(function()
+    sys.wait(1000)
+    log.info("开始G711编解码测试")
+
+    -- 初始化audio
+    local multimedia_id = 0
+    local i2c_id = 0
+    local i2s_id = 0
+    local i2s_mode = 0
+    local i2s_sample_rate = 44100
+    local i2s_bits_per_sample = 16
+    local i2s_channel_format = 0
+    local i2s_communication_format = 0
+    local i2s_channel_bits = 32
+
+    local pa_pin = 22
+    local pa_on_level = 1
+    local pa_delay = 20
+    local power_pin = 20
+    local power_delay = 0
+
+    i2c.setup(i2c_id,i2c.FAST)
+    i2s.setup(i2s_id, i2s_mode, i2s_sample_rate, i2s_bits_per_sample, i2s_channel_format, i2s_communication_format,i2s_channel_bits)
+
+    audio.config(multimedia_id, pa_pin, pa_on_level, power_delay, pa_delay, power_pin)
+    audio.setBus(multimedia_id, audio.BUS_I2S,{chip = "es8311",i2cid = i2c_id , i2sid = i2s_id})
+
+
+        -- 播放参数设置
+    audio.start(multimedia_id, audio.PCM, 1, 16000, 16)
+    -- 音量设置
+    audio.vol(multimedia_id, 50)
+    audio.pm(multimedia_id,audio.RESUME)
+    -- PCM播放演示, 16k采样率, 16bit采样深度
+    local file_size = fs.fsize("/luadb/test.pcm")
+    -- print("/luadb/test.pcm size",file_size)
+    local f = io.open("/luadb/test.pcm", "rb")
+    if f then
+        while 1 do
+            local data = f:read(4096)
+            -- print("-------------")
+            if not data or #data == 0 then
+                break
+            end
+            audio.write(0, data)
+            sys.wait(100)
+        end
+        f:close()
+    end
+
+
+
+    -- 第一步:对/luadb/test.pcm进行G711编码
+    log.info("第一步:对/luadb/test.pcm进行G711编码")
+
+    -- 创建G711编码器
+    local encoder = codec.create(codec.ALAW, false)
+    log.info("G711编码器创建成功:", encoder)
+
+    -- 创建输入和输出缓冲区
+    local in_buffer = zbuff.create(54594)
+    local out_buffer = zbuff.create(54594)
+
+    -- 读取PCM文件数据
+    local f = io.open("/luadb/test.pcm", "rb")
+
+    local pcm_data = f:read("*a")  -- 读取全部数据
+    f:close()
+    log.info("PCM文件大小:", #pcm_data, "字节")
+
+    -- 将PCM数据写入输入缓冲区
+    in_buffer:write(pcm_data)
+
+    -- 执行G711编码
+    log.info("开始G711编码,PCM数据大小:", #pcm_data, "字节")
+    local encode_result = codec.encode(encoder, in_buffer, out_buffer, 0)  -- 使用默认编码等级0
+    log.info("G711编码结果:", encode_result)
+
+    if encode_result then
+        local encoded_size = out_buffer:used()
+        log.info("编码成功,编码后数据大小:", encoded_size, "字节")
+
+        -- 保存编码后的G711数据到文件
+        local encoded_data = out_buffer:toStr(0, encoded_size)
+        io.writeFile("/aaa.g711", encoded_data)
+        log.info("编码数据已保存到 /aaa.g711")
+
+        -- 第二步:对编码后的文件进行解码
+        log.info("第二步:对编码后的文件进行解码")
+
+        -- 创建G711解码器
+        local decoder = codec.create(codec.ALAW)
+        if not decoder then
+            log.error("创建G711解码器失败")
+            codec.release(encoder)
+            return
+        end
+        log.info("G711解码器创建成功:", decoder)
+
+        -- 获取编码后文件的信息
+        local result, audio_format, num_channels, sample_rate, bits_per_sample, is_signed = codec.info(decoder, "/aaa.g711")
+        log.info("编码后文件信息 - 采样率:", sample_rate, "声道数:", num_channels, "位深度:", bits_per_sample)
+
+        -- 解码G711文件
+        log.info("开始解码G711文件")
+        local decoded_data = ""
+        local decode_count = 0
+        local total_decoded = 0
+
+        -- 创建解码缓冲区
+        local decode_buffer = zbuff.create(54594)
+
+        while 1 do
+            local decode_result = codec.data(decoder, decode_buffer)
+            if decode_result then
+                local data_size = decode_buffer:used()
+                if data_size > 0 then
+                    decode_count = decode_count + 1
+                    total_decoded = total_decoded + data_size
+                    decoded_data = decoded_data .. decode_buffer:toStr(0, data_size)
+                    log.info("解码数据块", decode_count, "大小:", data_size, "字节")
+                else
+                    log.info("解码完成,没有更多数据")
+                    break
+                end
+            else
+                log.info("G711解码完成或失败")
+                break
+            end
+        end
+        log.info("解码完成,总解码数据大小:", #decoded_data, "字节")
+
+
+        -- 第三步:播放解码后的音频
+        log.info("第三步:播放解码后的音频")
+
+        -- 播放参数设置
+        audio.start(multimedia_id, audio.PCM, 1, 16000, 16)
+        -- 音量设置
+        audio.vol(multimedia_id, 50)
+        audio.pm(multimedia_id,audio.RESUME)
+
+        -- 播放解码后的PCM数据
+        log.info("开始播放解码后的PCM数据")
+        local play_buffer = zbuff.create(54594)
+        play_buffer:write(decoded_data)
+        play_buffer:seek(0)
+
+        -- 分块播放音频数据
+        local offset = 0
+        while offset < #decoded_data do
+            local chunk = play_buffer:read(math.min(4096, #decoded_data - offset))
+            if chunk and #chunk > 0 then
+                audio.write(multimedia_id, chunk)
+                log.info("播放音频块,大小:", #chunk, "字节")
+                sys.wait(50)  -- 等待一小段时间
+            end
+            offset = offset + 4096
+        end
+
+        log.info("音频播放完成")
+
+        -- 清理解码器资源
+        codec.release(decoder)
+        decode_buffer:free()
+        play_buffer:free()
+    else
+        log.error("G711编码失败")
+    end
+
+    -- 清理资源
+    codec.release(encoder)
+    in_buffer:free()
+    out_buffer:free()
+
+    log.info("G711编解码测试完成")
+end)
+
+-- sys.taskInit(function()
+--     while 1 do
+--         -- 打印内存状态, 调试用
+--         sys.wait(1000)
+--         log.info("lua", rtos.meminfo())
+--         log.info("sys", rtos.meminfo("sys"))
+--     end
+-- end)
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!

BIN
luat/demo/g711/test.pcm