Browse Source

add: vtool,添加在air8101环境下,通过jpg文件生成mp4文件的函数,并添加供研发使用的demo文件

Wendal Chen 1 month ago
parent
commit
f29da74f9d

+ 40 - 5
components/multimedia/vtool/binding/luat_lib_vtool.c

@@ -16,7 +16,7 @@ static int l_vtool_mp4create(lua_State *L) {
     size_t w = luaL_checkinteger(L, 2);
     size_t h = luaL_checkinteger(L, 3);
     size_t fps = luaL_checkinteger(L, 4);
-    mp4_ctx_t* ctx = luat_vtool_mp4box_creare(filename, w, h, fps);
+    mp4_ctx_t* ctx = luat_vtool_mp4box_create(filename, w, h, fps);
     if (ctx == NULL) {
         return 0;
     }
@@ -43,12 +43,47 @@ static int l_vtool_mp4close(lua_State *L) {
     return 1;
 }
 
+// 扩展支持, 写入jpeg文件为mp4的一帧
+static int l_vtool_mp4write_jpeg(lua_State *L) {
+    mp4_ctx_t* ctx = lua_touserdata(L, 1);
+    const char* jpeg_path = luaL_checkstring(L, 2);
+    int save_tmp = lua_toboolean(L, 3);
+    // 首先, 解码jpeg成yuv422
+    int ret = luat_vtool_jpeg_add(jpeg_path);
+    lua_pushinteger(L, ret);
+    return 1;
+}
+
+static int l_vtool_h264_encoder_init(lua_State *L) {
+    luat_vtool_h264_encoder_init();
+    lua_pushboolean(L, 1);
+    return 1;
+}
+
+static int l_vtool_h264_encoder_start(lua_State *L) {
+    luat_vtool_h264_encoder_start();
+    lua_pushboolean(L, 1);
+    return 1;
+}
+
+static int l_vtool_h264_encoder_deinit(lua_State *L) {
+    luat_vtool_h264_encoder_deinit();
+    lua_pushboolean(L, 1);
+    return 1;
+}
+
 static const rotable_Reg_t reg_vtool[] =
 {
-    { "mp4create" ,     ROREG_FUNC(l_vtool_mp4create)},
-    { "mp4write" ,      ROREG_FUNC(l_vtool_mp4write)},
-    { "mp4close" ,      ROREG_FUNC(l_vtool_mp4close)},
-	{ NULL,             ROREG_INT(0) }
+    { "mp4create" ,         ROREG_FUNC(l_vtool_mp4create)},
+    { "mp4write" ,          ROREG_FUNC(l_vtool_mp4write)},
+    { "mp4close" ,          ROREG_FUNC(l_vtool_mp4close)},
+    #ifdef __BK72XX__
+    { "mp4write_jpeg" ,     ROREG_FUNC(l_vtool_mp4write_jpeg)},
+    { "h264_encoder_init",  ROREG_FUNC(l_vtool_h264_encoder_init)},
+    { "h264_encoder_start", ROREG_FUNC(l_vtool_h264_encoder_start)},
+    { "h264_encoder_deinit",ROREG_FUNC(l_vtool_h264_encoder_deinit)},
+    #endif
+	{ NULL,                 ROREG_INT(0) }
 };
 
 

+ 10 - 1
components/multimedia/vtool/include/luat_vtool_mp4box.h

@@ -88,10 +88,19 @@ typedef struct mp4_ctx {
     uint64_t last_frame_tms; // 最后一帧的时间戳, 单位ms
 }mp4_ctx_t;
 
-mp4_ctx_t* luat_vtool_mp4box_creare(const char* path, uint32_t frame_w, uint32_t frame_h, uint32_t frame_fps);
+mp4_ctx_t* luat_vtool_mp4box_create(const char* path, uint32_t frame_w, uint32_t frame_h, uint32_t frame_fps);
 int luat_vtool_mp4box_write_frame(mp4_ctx_t* ctx, uint8_t* data, size_t len);
 int luat_vtool_mp4box_close(mp4_ctx_t* ctx);
 
+// int luat_vtool_jpeg_decoder_init(void);
+// int luat_vtool_jpeg_decoder_deinit(void);
+
+int luat_vtool_jpeg_add(const char* path);
+
+int luat_vtool_h264_encoder_init(void);
+int luat_vtool_h264_encoder_start(void);
+int luat_vtool_h264_encoder_deinit(void);
+
 #include "luat_mcu.h"
 
 #endif

+ 113 - 0
olddemo/vtool/main.lua

@@ -0,0 +1,113 @@
+
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "vtool"
+VERSION = "1.0.0"
+
+httpplus = require "httpplus"
+
+
+local rtos_bsp = rtos.bsp()
+
+-- spi_id,pin_cs
+local function fatfs_spi_pin()     
+    if rtos_bsp == "AIR101" then
+        return 0, pin.PB04
+    elseif rtos_bsp == "AIR103" then
+        return 0, pin.PB04
+    elseif rtos_bsp == "AIR105" then
+        return 2, pin.PB03
+    elseif rtos_bsp == "ESP32C3" then
+        return 2, 7
+    elseif rtos_bsp == "ESP32S3" then
+        return 2, 14
+    elseif rtos_bsp == "EC618" then
+        return 0, 8
+    elseif string.find(rtos_bsp,"EC718") then
+        return 0, 8
+    elseif string.find(rtos_bsp,"Air810") then
+        gpio.setup(13, 1, gpio.PULLUP)
+        return 0, 3, fatfs.SDIO
+    else
+        log.info("main", "bsp not support")
+        return
+    end
+end
+
+sys.taskInit(function()
+    sys.wait(1000)
+    -- fatfs.debug(1) -- 若挂载失败,可以尝试打开调试信息,查找原因
+
+    gpio.set(13, 1)
+
+    -- 此为spi方式
+    local spi_id, pin_cs,tp = fatfs_spi_pin() 
+    if tp and tp == fatfs.SPI then
+        -- 仅SPI方式需要自行初始化spi, sdio不需要
+        spi.setup(spi_id, nil, 0, 0, 8, 400 * 1000)
+        gpio.setup(pin_cs, 1)
+    end
+    log.info("fatfs", "mounting...")
+    fatfs.mount(tp or fatfs.SPI, "/sd", spi_id, pin_cs, 24 * 1000 * 1000)
+    log.info("fatfs", "mounted")
+
+    local data, err = fatfs.getfree("/sd")
+    if data then
+        log.info("fatfs", "getfree", json.encode(data))
+    else
+        log.info("fatfs", "err", err)
+    end
+
+    local pnum = 27
+    local init_qp = 40
+    local i_qp_max = 28
+    local p_qp_max = 28
+    local imb_bits = 3
+    local pmb_bits = 3
+
+    camera.config(0, camera.CONF_H264_QP_INIT, init_qp)
+    camera.config(0, camera.CONF_H264_QP_I_MAX, i_qp_max)
+    camera.config(0, camera.CONF_H264_QP_P_MAX, p_qp_max)
+    camera.config(0, camera.CONF_H264_IMB_BITS, imb_bits)
+    camera.config(0, camera.CONF_H264_PMB_BITS, pmb_bits)
+
+    log.info("开始测试==================")
+    vtool.h264_encoder_init()
+    vtool.h264_encoder_start()
+    local dst = "/sd/test.mp4"
+    local mp4 = vtool.mp4create(dst, 1280, 720, 15)
+
+    log.info("vtool", "开始写入jpeg文件")
+    -- 从0.jpg, 写到 4.jpg
+    for i = 0, 4 do
+        local filename = string.format("/sd/C8C2C68CACB8_904_1.mp4.%d.jpg", i)
+        log.info("vtool", "写入文件", filename)
+        local ret = vtool.mp4write_jpeg(mp4, filename)
+        log.info("vtool", "写入结果", ret)
+        sys.wait(70)
+    end
+
+    sys.wait(1000)
+
+    vtool.mp4close(mp4)
+    log.info("vtool", "测试完成, 文件保存在", dst)
+    -- 打印文件大小
+    log.info("vtool", "test.mp4 size", io.fileSize(dst))
+
+    vtool.h264_encoder_deinit()
+
+    wlan.init()
+    wlan.connect("luatos1234", "12341234")
+
+    sys.waitUntil("IP_READY")
+    httpplus.request({
+        url = "http://upload.air32.cn/api/upload/mp4",
+        method = "POST",
+        bodyfile = dst
+    })
+    log.info("执行结束------")
+end)
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!