Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mmalomx_buffer.c 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. Copyright (c) 2012, Broadcom Europe Ltd
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "mmalomx.h"
  26. #include "mmalomx_buffer.h"
  27. #include "mmalomx_commands.h"
  28. #include "mmalomx_marks.h"
  29. #include "mmalomx_logging.h"
  30. #include <util/mmal_util.h>
  31. /*****************************************************************************/
  32. OMX_ERRORTYPE mmalomx_buffer_send(
  33. MMALOMX_COMPONENT_T *component,
  34. OMX_BUFFERHEADERTYPE *omx_buffer,
  35. OMX_DIRTYPE direction)
  36. {
  37. OMX_ERRORTYPE status = OMX_ErrorNone;
  38. MMAL_BUFFER_HEADER_T *mmal_buffer;
  39. MMAL_STATUS_T mmal_status;
  40. MMALOMX_PORT_T *port;
  41. unsigned int index;
  42. /* Sanity checks */
  43. if (!component)
  44. return OMX_ErrorInvalidComponent;
  45. if (component->state == OMX_StateInvalid)
  46. return OMX_ErrorInvalidState;
  47. if (!omx_buffer || omx_buffer->nSize != sizeof(OMX_BUFFERHEADERTYPE) ||
  48. omx_buffer->nOffset + omx_buffer->nFilledLen > omx_buffer->nAllocLen)
  49. return OMX_ErrorBadParameter;
  50. index = direction == OMX_DirInput ? omx_buffer->nInputPortIndex : omx_buffer->nOutputPortIndex;
  51. if (index >= component->ports_num)
  52. return OMX_ErrorBadPortIndex;
  53. port = &component->ports[index];
  54. if (port->direction != direction)
  55. return OMX_ErrorBadPortIndex;
  56. MMALOMX_LOCK_PORT(component, port);
  57. if (component->state != OMX_StatePause && component->state != OMX_StateExecuting)
  58. status = OMX_ErrorIncorrectStateOperation;
  59. if (!port->enabled /* FIXME: || flushing || pending idle */)
  60. status = OMX_ErrorIncorrectStateOperation;
  61. if (status != OMX_ErrorNone)
  62. goto error;
  63. mmal_buffer = mmal_queue_get( port->pool->queue );
  64. if (!vcos_verify(mmal_buffer)) /* Should never happen */
  65. {
  66. status = OMX_ErrorUndefined;
  67. goto error;
  68. }
  69. mmalomx_mark_process_incoming(component, port, omx_buffer);
  70. mmal_buffer->user_data = (void *)omx_buffer;
  71. mmalil_buffer_header_to_mmal(mmal_buffer, omx_buffer);
  72. mmal_status = mmal_port_send_buffer(port->mmal, mmal_buffer);
  73. if (!vcos_verify(mmal_status == MMAL_SUCCESS))
  74. {
  75. LOG_ERROR("failed to send buffer on %s", port->mmal->name);
  76. mmal_queue_put_back( port->pool->queue, mmal_buffer );
  77. status = mmalil_error_to_omx(mmal_status);
  78. }
  79. else
  80. {
  81. port->buffers_in_transit++;
  82. }
  83. error:
  84. MMALOMX_UNLOCK_PORT(component, port);
  85. return status;
  86. }
  87. /*****************************************************************************/
  88. static void mmalomx_buffer_event(
  89. MMALOMX_PORT_T *port,
  90. MMAL_BUFFER_HEADER_T *mmal_buffer)
  91. {
  92. MMALOMX_COMPONENT_T *component = port->component;
  93. MMAL_EVENT_FORMAT_CHANGED_T *event;
  94. LOG_TRACE("hComponent %p, port %i, event %4.4s", component, port->index,
  95. (char *)&mmal_buffer->cmd);
  96. if (mmal_buffer->cmd == MMAL_EVENT_ERROR )
  97. {
  98. mmalomx_callback_event_handler(component, OMX_EventError,
  99. mmalil_error_to_omx(*(MMAL_STATUS_T *)mmal_buffer->data), 0, NULL);
  100. return;
  101. }
  102. event = mmal_event_format_changed_get(mmal_buffer);
  103. if (event && port->mmal->type == MMAL_PORT_TYPE_OUTPUT &&
  104. port->mmal->format->type == MMAL_ES_TYPE_VIDEO)
  105. {
  106. uint32_t diff = mmal_format_compare(event->format, port->mmal->format);
  107. MMAL_ES_FORMAT_T *format = port->mmal->format;
  108. MMAL_VIDEO_FORMAT_T video = format->es->video;
  109. /* Update the port settings with the new values */
  110. mmal_format_copy(format, event->format);
  111. port->mmal->buffer_num_min = event->buffer_num_min;
  112. port->mmal->buffer_size_min = event->buffer_size_min;
  113. port->format_changed = MMAL_TRUE;
  114. if ((diff & MMAL_ES_FORMAT_COMPARE_FLAG_VIDEO_ASPECT_RATIO) &&
  115. /* Do not report a change if going from unspecified to 1:1 */
  116. !(format->es->video.par.num == format->es->video.par.den && !video.par.num))
  117. {
  118. LOG_DEBUG("aspect ratio change %ix%i->%ix%i", (int)video.par.num, (int)video.par.den,
  119. (int)format->es->video.par.num, (int)format->es->video.par.den);
  120. mmalomx_callback_event_handler(component, OMX_EventPortSettingsChanged,
  121. port->index, OMX_IndexParamBrcmPixelAspectRatio, NULL);
  122. }
  123. if (diff & (MMAL_ES_FORMAT_COMPARE_FLAG_ENCODING|
  124. MMAL_ES_FORMAT_COMPARE_FLAG_VIDEO_RESOLUTION|
  125. MMAL_ES_FORMAT_COMPARE_FLAG_VIDEO_CROPPING))
  126. {
  127. LOG_DEBUG("format change %ix%i(%ix%i) -> %ix%i(%ix%i)",
  128. (int)video.width, (int)video.height,
  129. (int)video.crop.width, (int)video.crop.height,
  130. (int)format->es->video.width, (int)format->es->video.height,
  131. (int)format->es->video.crop.width, (int)format->es->video.crop.height);
  132. mmalomx_callback_event_handler(component, OMX_EventPortSettingsChanged,
  133. port->index, 0, NULL);
  134. }
  135. }
  136. else if (event && port->mmal->type == MMAL_PORT_TYPE_OUTPUT &&
  137. port->mmal->format->type == MMAL_ES_TYPE_AUDIO)
  138. {
  139. uint32_t diff = mmal_format_compare(event->format, port->mmal->format);
  140. MMAL_ES_FORMAT_T *format = port->mmal->format;
  141. MMAL_AUDIO_FORMAT_T audio = format->es->audio;
  142. /* Update the port settings with the new values */
  143. mmal_format_copy(format, event->format);
  144. port->mmal->buffer_num_min = event->buffer_num_min;
  145. port->mmal->buffer_size_min = event->buffer_size_min;
  146. port->format_changed = MMAL_TRUE;
  147. if (diff)
  148. {
  149. LOG_DEBUG("format change %ich, %iHz, %ibps -> %ich, %iHz, %ibps",
  150. (int)audio.channels, (int)audio.sample_rate,
  151. (int)audio.bits_per_sample,
  152. (int)format->es->audio.channels,
  153. (int)format->es->audio.sample_rate,
  154. (int)format->es->audio.bits_per_sample);
  155. mmalomx_callback_event_handler(component, OMX_EventPortSettingsChanged,
  156. port->index, 0, NULL);
  157. }
  158. }
  159. }
  160. /*****************************************************************************/
  161. OMX_ERRORTYPE mmalomx_buffer_return(
  162. MMALOMX_PORT_T *port,
  163. MMAL_BUFFER_HEADER_T *mmal_buffer)
  164. {
  165. MMALOMX_COMPONENT_T *component = port->component;
  166. OMX_BUFFERHEADERTYPE *omx_buffer = (OMX_BUFFERHEADERTYPE *)mmal_buffer->user_data;
  167. MMAL_BOOL_T signal;
  168. if (mmal_buffer->cmd)
  169. {
  170. mmalomx_buffer_event(port, mmal_buffer);
  171. mmal_buffer_header_release(mmal_buffer);
  172. return OMX_ErrorNone;
  173. }
  174. if (ENABLE_MMAL_EXTRA_LOGGING)
  175. LOG_TRACE("hComponent %p, port %i, pBuffer %p", component,
  176. port->index, omx_buffer);
  177. vcos_assert(omx_buffer->pBuffer == mmal_buffer->data);
  178. mmalil_buffer_header_to_omx(omx_buffer, mmal_buffer);
  179. mmal_buffer_header_release(mmal_buffer);
  180. if ((omx_buffer->nFlags & OMX_BUFFERFLAG_EOS) && port->direction == OMX_DirOutput)
  181. {
  182. mmalomx_callback_event_handler(component, OMX_EventBufferFlag,
  183. port->index, omx_buffer->nFlags, NULL);
  184. }
  185. mmalomx_mark_process_outgoing(component, port, omx_buffer);
  186. if (port->direction == OMX_DirInput)
  187. component->callbacks.EmptyBufferDone((OMX_HANDLETYPE)&component->omx,
  188. component->callbacks_data, omx_buffer );
  189. else
  190. component->callbacks.FillBufferDone((OMX_HANDLETYPE)&component->omx,
  191. component->callbacks_data, omx_buffer );
  192. MMALOMX_LOCK_PORT(component, port);
  193. signal = port->actions & MMALOMX_ACTION_CHECK_FLUSHED;
  194. port->buffers_in_transit--;
  195. MMALOMX_UNLOCK_PORT(component, port);
  196. if (signal)
  197. mmalomx_commands_actions_signal(component);
  198. return OMX_ErrorNone;
  199. }