MppDecoder.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "MppDecoder.h"
  2. #include <string.h>
  3. #include <iostream>
  4. #include <stdexcept>
  5. #include "../LogRecorder/LogOutput.h"
  6. bool MppDecoder::init()
  7. {
  8. MppDecCfg cfg = NULL;
  9. RK_U32 need_split = 1;
  10. if (m_rgbData == nullptr)
  11. {
  12. m_rgbData = new unsigned char[m_width * m_height * 3];
  13. }
  14. MPP_RET ret = mpp_create(&m_ctx, &m_mpi);
  15. if (ret)
  16. {
  17. LOG_ERROR("mpp_create failed\n");
  18. return false;
  19. }
  20. MpiCmd cmd = MPP_CMD_BASE;
  21. MppParam param = NULL;
  22. cmd = MPP_DEC_SET_PARSER_SPLIT_MODE;
  23. param = &need_split;
  24. ret = m_mpi->control(m_ctx, cmd, param);
  25. if (ret)
  26. {
  27. LOG_ERROR("set parser split mode failed\n");
  28. return false;
  29. }
  30. ret = mpp_init(m_ctx, MPP_CTX_DEC, MPP_VIDEO_CodingMJPEG);
  31. if (ret)
  32. {
  33. LOG_ERROR("mpp_init failed\n");
  34. return false;
  35. }
  36. MppFrameFormat fmt = MPP_FMT_RGB888;
  37. param = &fmt;
  38. ret = m_mpi->control(m_ctx, MPP_DEC_SET_OUTPUT_FORMAT, param);
  39. if (ret)
  40. {
  41. LOG_ERROR("set output format failed\n");
  42. return false;
  43. }
  44. ret = mpp_frame_init(&m_frame);
  45. if (ret)
  46. {
  47. LOG_ERROR("mpp_frame_init failed\n");
  48. return false;
  49. }
  50. ret = mpp_buffer_group_get_internal(&m_frameGroup, MPP_BUFFER_TYPE_ION);
  51. if (ret)
  52. {
  53. LOG_ERROR("mpp_buffer_group_get_internal failed\n");
  54. return false;
  55. }
  56. ret = mpp_buffer_group_get_internal(&m_PacketGroup, MPP_BUFFER_TYPE_ION);
  57. if (ret)
  58. {
  59. LOG_ERROR("mpp_buffer_group_get_internal failed\n");
  60. return false;
  61. }
  62. RK_U32 hor_stride = MPP_ALIGN(m_width, 16);
  63. RK_U32 ver_stride = MPP_ALIGN(m_height, 16);
  64. ret = mpp_buffer_get(m_frameGroup, &m_frameBuffer, hor_stride * ver_stride * 4);
  65. if (ret)
  66. {
  67. LOG_ERROR("mpp_buffer_get failed\n");
  68. return false;
  69. }
  70. mpp_frame_set_buffer(m_frame, m_frameBuffer);
  71. ret = mpp_buffer_get(m_PacketGroup, &m_packetBuffer, m_width * m_height * 3);
  72. if (ret)
  73. {
  74. LOG_ERROR("mpp_buffer_get failed\n");
  75. return false;
  76. }
  77. ret = mpp_packet_init_with_buffer(&m_packet, m_packetBuffer);
  78. if (ret)
  79. {
  80. LOG_ERROR("mpp_packet_init_with_buffer failed\n");
  81. return false;
  82. }
  83. m_inputBufferPtr = (unsigned char *)mpp_buffer_get_ptr(m_packetBuffer);
  84. return m_initFlag = true;
  85. }
  86. bool MppDecoder::decodeJpegToRgb(const char *jpegData, size_t jpegSize, unsigned char **rgbData, int *width, int *height)
  87. {
  88. if (!m_initFlag)
  89. {
  90. LOG_ERROR("MppDecoder[{}]: Decoder not initialized", m_index);
  91. return false;
  92. }
  93. if (jpegData == nullptr || jpegSize == 0) {
  94. LOG_ERROR("MppDecoder[{}]: Invalid jpeg data or size", m_index);
  95. return false;
  96. }
  97. memset(m_inputBufferPtr, 0, m_width * m_height * 3);
  98. memcpy(m_inputBufferPtr, jpegData, jpegSize);
  99. mpp_packet_set_pos(m_packet, m_inputBufferPtr);
  100. mpp_packet_set_length(m_packet, jpegSize);
  101. mpp_packet_set_eos(m_packet);
  102. MPP_RET ret;
  103. ret = m_mpi->poll(m_ctx, MPP_PORT_INPUT, MPP_POLL_BLOCK);
  104. if (ret)
  105. {
  106. LOG_ERROR("MppDecoder[{}]: m_mpi->poll input failed, ret={}", m_index, int(ret));
  107. resetDecoder(); // 添加重置功能以尝试恢复
  108. return false;
  109. }
  110. ret = m_mpi->dequeue(m_ctx, MPP_PORT_INPUT, &m_task);
  111. if (ret)
  112. {
  113. LOG_ERROR("MppDecoder[{}]: m_mpi->dequeue input failed, ret={}", m_index, int(ret));
  114. resetDecoder();
  115. return false;
  116. }
  117. mpp_task_meta_set_packet(m_task, KEY_INPUT_PACKET, m_packet);
  118. mpp_task_meta_set_frame(m_task, KEY_OUTPUT_FRAME, m_frame);
  119. ret = m_mpi->enqueue(m_ctx, MPP_PORT_INPUT, m_task);
  120. if (ret)
  121. {
  122. LOG_ERROR("MppDecoder[{}]: m_mpi->enqueue input failed, ret={}", m_index, int(ret));
  123. resetDecoder();
  124. return false;
  125. }
  126. ret = m_mpi->poll(m_ctx, MPP_PORT_OUTPUT, MPP_POLL_BLOCK);
  127. if (ret)
  128. {
  129. LOG_ERROR("MppDecoder[{}]: m_mpi->poll output failed, ret={}", m_index, int(ret));
  130. resetDecoder();
  131. return false;
  132. }
  133. ret = m_mpi->dequeue(m_ctx, MPP_PORT_OUTPUT, &m_task);
  134. if (ret)
  135. {
  136. LOG_ERROR("MppDecoder[{}]: m_mpi->dequeue output failed, ret={}", m_index, int(ret));
  137. resetDecoder();
  138. return false;
  139. }
  140. if (m_task)
  141. {
  142. MppFrame Output = nullptr;
  143. mpp_task_meta_get_frame(m_task, KEY_OUTPUT_FRAME, &Output);
  144. if (Output)
  145. {
  146. MppBuffer buffer = nullptr;
  147. buffer = mpp_frame_get_buffer(Output);
  148. if (buffer)
  149. {
  150. *rgbData = (unsigned char *)mpp_buffer_get_ptr(buffer);
  151. *width = mpp_frame_get_width(Output);
  152. *height = mpp_frame_get_height(Output);
  153. }
  154. }
  155. m_mpi->enqueue(m_ctx, MPP_PORT_OUTPUT, m_task);
  156. return (Output != nullptr);
  157. }
  158. LOG_ERROR("MppDecoder[{}]: No task returned", m_index);
  159. return false;
  160. }
  161. bool MppDecoder::resetDecoder()
  162. {
  163. try {
  164. LOG_WARN("MppDecoder[{}]: Attempting to reset decoder", m_index);
  165. // reset the decoder context and MPI instance if they are valid
  166. if (m_ctx && m_mpi) {
  167. // clear the task queue and reset the decoder context
  168. m_mpi->reset(m_ctx);
  169. }
  170. LOG_INFO("MppDecoder[{}]: Decoder reset completed", m_index);
  171. return true;
  172. } catch (const std::exception& e) {
  173. LOG_ERROR("MppDecoder[{}]: Exception during reset: {}", m_index, e.what());
  174. return false;
  175. } catch (...) {
  176. LOG_ERROR("MppDecoder[{}]: Unknown exception during reset", m_index);
  177. return false;
  178. }
  179. }