MppDecoder.cpp 5.9 KB

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