venc_demo1.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #define LOG_LEVEL 3
  13. #define LOG_PREFIX "venc_demo1"
  14. #include <syslog.h>
  15. #include <csi_venc.h>
  16. #define TEST_MODULE_NAME "simulator_venc0"
  17. int main(int argc, char *argv[])
  18. {
  19. int ret;
  20. csi_venc_info_s venc_info;
  21. csi_venc_dev_t encoder;
  22. csi_venc_chn_t channel;
  23. // 打印HAL接口版本号
  24. csi_api_version_u version;
  25. csi_venc_get_version(&version);
  26. printf("Video Encoder HAL version: %d.%d\n", version.major, version.minor);
  27. // 获取设备中,所有的Video Encoder
  28. struct csi_venc_infos venc_infos;
  29. csi_venc_query_list(&venc_infos);
  30. // 打印所有设备所支持的Video Encoder
  31. bool found_module = false;
  32. for (int i = 0; i < venc_infos.count; i++) {
  33. csi_venc_info_s *info;
  34. info = &(venc_infos.info[i]);
  35. printf("decoder[%d]: module_name='%s', device_name='%s', capabilities=0x%lx\n",
  36. i, info->module_name, info->device_name, info->capabilities);
  37. if (strcmp(info->module_name, TEST_MODULE_NAME) == 0) {
  38. venc_info = *info;
  39. found_module = true;
  40. }
  41. }
  42. if (!found_module) {
  43. LOG_E("Can't find module_name:'%s'\n", TEST_MODULE_NAME);
  44. exit(-1);
  45. }
  46. // 打开设备获取句柄,作为后续操对象
  47. csi_venc_open(&encoder, venc_info.device_name);
  48. // 创建视频解码通道
  49. csi_venc_chn_cfg_s chn_cfg;
  50. chn_cfg.attr.enc_vcodec_id = CSI_VCODEC_ID_H264;
  51. chn_cfg.attr.max_pic_width = 3840;
  52. chn_cfg.attr.max_pic_height = 2160;
  53. chn_cfg.attr.input_format = CSI_PIX_FMT_I420;
  54. chn_cfg.attr.pic_width = 1920;
  55. chn_cfg.attr.pic_height = 1080;
  56. chn_cfg.attr.buf_size = 1*1024*1024;
  57. chn_cfg.attr.h264_attr.profile = CSI_VENC_H264_PROFILE_HIGH;
  58. chn_cfg.attr.h264_attr.level = CSI_VENC_H264_LEVEL_4;
  59. chn_cfg.attr.h264_attr.frame_type = CSI_VCODEC_I_FRAME |
  60. CSI_VCODEC_P_FRAME |
  61. CSI_VCODEC_B_FRAME;
  62. chn_cfg.attr.h264_attr.share_buf = true;
  63. chn_cfg.gop.gop_num = 5;
  64. chn_cfg.gop.gop_mode = CSI_VENC_GOPMODE_NORMALP;
  65. chn_cfg.gop.normalp.ip_qp_delta = 10;
  66. chn_cfg.rc.rc_mode = CSI_VENC_RC_MODE_H264CBR;
  67. chn_cfg.rc.h264_vbr.stat_time = 10;
  68. chn_cfg.rc.h264_vbr.framerate_numer = 25;
  69. chn_cfg.rc.h264_vbr.framerate_denom = 1;
  70. chn_cfg.rc.h264_vbr.max_bit_rate = 16 * 1024; // kbps
  71. csi_venc_create_channel(&channel, encoder, &chn_cfg);
  72. // 配置内存分配器
  73. //csi_allocator_s *allocator = csi_allocator_get(CSI_ALLOCATOR_TYPE_DMA);
  74. //csi_venc_set_memory_allocator(channel, allocator);
  75. // 设置解码通道扩展属性
  76. csi_venc_chn_ext_property_s ext_property;
  77. ext_property.prop_id = CSI_VENC_EXT_PROPERTY_ROI;
  78. ext_property.roi_prop.index = 0;
  79. ext_property.roi_prop.enable = true;
  80. ext_property.roi_prop.abs_qp = 1;
  81. ext_property.roi_prop.qp = 51;
  82. csi_venc_set_ext_property(channel, &ext_property);
  83. // 订阅事件
  84. csi_venc_event_handle_t event_handle;
  85. csi_venc_create_event_handle(&event_handle, encoder);
  86. csi_venc_event_subscription_s subscribe_dec, subscribe_chn;
  87. subscribe_dec.type = CSI_VENC_EVENT_TYPE_DECODER; // 订阅Encoder事件
  88. subscribe_dec.id = CSI_VENC_EVENT_ID_ERROR;
  89. csi_venc_subscribe_event(event_handle, &subscribe_dec);
  90. subscribe_chn.type = CSI_VENC_EVENT_TYPE_CHANNEL; // 订阅Channel事件
  91. subscribe_chn.id = CSI_VENC_CHANNEL_EVENT_ID_FRAME_READY;
  92. csi_venc_subscribe_event(event_handle, &subscribe_chn);
  93. // 传送视频码流
  94. csi_venc_start(channel);
  95. csi_frame_s frame;
  96. int send_count = 25;
  97. for (int i = 0; i < send_count; i++) {
  98. if (i == 10) {
  99. // Just for testing
  100. csi_venc_reset(channel);
  101. }
  102. if (i %5 != 0) {
  103. // Send normal frame
  104. csi_venc_send_frame(channel, &frame, 1000);
  105. }
  106. else {
  107. // Send frame with extended properties
  108. csi_venc_frame_prop_s frame_prop[2];
  109. frame_prop[0].type = CSI_VENC_FRAME_PROP_FORCE_IDR;
  110. frame_prop[0].force_idr = true;
  111. frame_prop[1].type = CSI_VENC_FRAME_PROP_FORCE_SKIP;
  112. frame_prop[1].force_skip = false;
  113. csi_venc_send_frame_ex(channel, &frame, 1000,
  114. frame_prop, ARRAY_SIZE(frame_prop));
  115. }
  116. }
  117. // 收取解码结果
  118. csi_stream_s stream;
  119. struct csi_venc_event event;
  120. int get_count = 0;
  121. csi_venc_chn_status_s status;
  122. while (get_count < send_count) {
  123. int timeout = -1; // unit: ms, -1 means wait forever, or until error occurs
  124. csi_venc_get_event(event_handle, &event, timeout);
  125. switch (event.type) {
  126. case CSI_VENC_EVENT_TYPE_DECODER:
  127. csi_venc_query_status(channel, &status);
  128. // Dump Status
  129. break;
  130. case CSI_VENC_EVENT_TYPE_CHANNEL:
  131. switch (event.id) {
  132. case CSI_VENC_CHANNEL_EVENT_ID_FRAME_READY:
  133. csi_venc_get_stream(channel, &stream, 1000);
  134. // save stream ...
  135. // release stream: stream.release();
  136. }
  137. break;
  138. default:
  139. break;
  140. }
  141. }
  142. // 关闭与回收资源
  143. csi_venc_stop(channel);
  144. csi_venc_unsubscribe_event(event_handle, &subscribe_dec);
  145. csi_venc_unsubscribe_event(event_handle, &subscribe_chn);
  146. csi_venc_destory_channel(channel);
  147. csi_venc_close(encoder);
  148. return 0;
  149. }