luat_lib_vtool.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "luat_base.h"
  2. #include "luat_vtool_mp4box.h"
  3. #include "luat_malloc.h"
  4. #include "luat_msgbus.h"
  5. #include "luat_timer.h"
  6. #include "luat_fs.h"
  7. #define LUAT_LOG_TAG "vtool"
  8. #include "luat_log.h"
  9. #include "rotable.h"
  10. static int l_vtool_mp4create(lua_State *L) {
  11. const char* filename = luaL_checkstring(L, 1);
  12. size_t w = luaL_checkinteger(L, 2);
  13. size_t h = luaL_checkinteger(L, 3);
  14. size_t fps = luaL_checkinteger(L, 4);
  15. mp4_ctx_t* ctx = luat_vtool_mp4box_create(filename, w, h, fps);
  16. if (ctx == NULL) {
  17. return 0;
  18. }
  19. lua_pushlightuserdata(L, ctx);
  20. return 1;
  21. }
  22. static int l_vtool_mp4write(lua_State *L) {
  23. mp4_ctx_t* ctx = lua_touserdata(L, 1);
  24. size_t len = 0;
  25. const char* data = luaL_checklstring(L, 2, &len);
  26. if (len == 0) {
  27. return 0;
  28. }
  29. int ret = luat_vtool_mp4box_write_frame(ctx, (uint8_t*)data, len);
  30. lua_pushinteger(L, ret);
  31. return 1;
  32. }
  33. static int l_vtool_mp4close(lua_State *L) {
  34. mp4_ctx_t* ctx = lua_touserdata(L, 1);
  35. int ret = luat_vtool_mp4box_close(ctx);
  36. lua_pushinteger(L, ret);
  37. return 1;
  38. }
  39. // 扩展支持, 写入jpeg文件为mp4的一帧
  40. static int l_vtool_mp4write_jpeg(lua_State *L) {
  41. mp4_ctx_t* ctx = lua_touserdata(L, 1);
  42. const char* jpeg_path = luaL_checkstring(L, 2);
  43. int save_tmp = lua_toboolean(L, 3);
  44. // 首先, 解码jpeg成yuv422
  45. int ret = luat_vtool_jpeg_add(jpeg_path);
  46. lua_pushinteger(L, ret);
  47. return 1;
  48. }
  49. static int l_vtool_h264_encoder_init(lua_State *L) {
  50. luat_vtool_h264_encoder_init();
  51. lua_pushboolean(L, 1);
  52. return 1;
  53. }
  54. static int l_vtool_h264_encoder_start(lua_State *L) {
  55. luat_vtool_h264_encoder_start();
  56. lua_pushboolean(L, 1);
  57. return 1;
  58. }
  59. static int l_vtool_h264_encoder_deinit(lua_State *L) {
  60. luat_vtool_h264_encoder_deinit();
  61. lua_pushboolean(L, 1);
  62. return 1;
  63. }
  64. static const rotable_Reg_t reg_vtool[] =
  65. {
  66. { "mp4create" , ROREG_FUNC(l_vtool_mp4create)},
  67. { "mp4write" , ROREG_FUNC(l_vtool_mp4write)},
  68. { "mp4close" , ROREG_FUNC(l_vtool_mp4close)},
  69. #ifdef __BK72XX__
  70. { "mp4write_jpeg" , ROREG_FUNC(l_vtool_mp4write_jpeg)},
  71. { "h264_encoder_init", ROREG_FUNC(l_vtool_h264_encoder_init)},
  72. { "h264_encoder_start", ROREG_FUNC(l_vtool_h264_encoder_start)},
  73. { "h264_encoder_deinit",ROREG_FUNC(l_vtool_h264_encoder_deinit)},
  74. #endif
  75. { NULL, ROREG_INT(0) }
  76. };
  77. LUAMOD_API int luaopen_vtool( lua_State *L ) {
  78. luat_newlib2(L, reg_vtool);
  79. return 1;
  80. }