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.

artificial_camera.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "mmal.h"
  26. #include "core/mmal_component_private.h"
  27. #include "core/mmal_port_private.h"
  28. #include "mmal_logging.h"
  29. #define ARTIFICIAL_CAMERA_PORTS_NUM 3
  30. /* Buffering requirements */
  31. #define OUTPUT_MIN_BUFFER_NUM 1
  32. #define OUTPUT_RECOMMENDED_BUFFER_NUM 4
  33. #define DEFAULT_WIDTH 320
  34. #define DEFAULT_HEIGHT 240
  35. /*****************************************************************************/
  36. typedef struct MMAL_PORT_MODULE_T
  37. {
  38. MMAL_BUFFER_HEADER_VIDEO_SPECIFIC_T frame;
  39. unsigned int frame_size;
  40. int count;
  41. MMAL_QUEUE_T *queue;
  42. } MMAL_PORT_MODULE_T;
  43. typedef struct MMAL_COMPONENT_MODULE_T
  44. {
  45. MMAL_STATUS_T status;
  46. } MMAL_COMPONENT_MODULE_T;
  47. /*****************************************************************************/
  48. static void artificial_camera_do_processing(MMAL_COMPONENT_T *component)
  49. {
  50. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  51. MMAL_BUFFER_HEADER_T *buffer;
  52. unsigned int i;
  53. if (module->status != MMAL_SUCCESS)
  54. return;
  55. /* Loop through all the ports */
  56. for (i = 0; i < component->output_num; i++)
  57. {
  58. MMAL_PORT_T *port = component->output[i];
  59. buffer = mmal_queue_get(port->priv->module->queue);
  60. if (!buffer)
  61. continue;
  62. /* Sanity check the buffer size */
  63. if (buffer->alloc_size < port->priv->module->frame_size)
  64. {
  65. LOG_ERROR("buffer too small (%i/%i)",
  66. buffer->alloc_size, port->priv->module->frame_size);
  67. module->status = MMAL_EINVAL;
  68. mmal_queue_put_back(port->priv->module->queue, buffer);
  69. mmal_event_error_send(component, module->status);
  70. return;
  71. }
  72. module->status = mmal_buffer_header_mem_lock(buffer);
  73. if (module->status != MMAL_SUCCESS)
  74. {
  75. LOG_ERROR("invalid buffer (%p, %p)", buffer, buffer->data);
  76. mmal_queue_put_back(port->priv->module->queue, buffer);
  77. mmal_event_error_send(component, module->status);
  78. return;
  79. }
  80. buffer->offset = 0;
  81. buffer->length = port->priv->module->frame_size;
  82. buffer->type->video = port->priv->module->frame;
  83. memset(buffer->data, 0xff, buffer->length);
  84. if (buffer->type->video.planes > 1)
  85. memset(buffer->data + buffer->type->video.offset[1],
  86. 0x7f - port->priv->module->count++,
  87. buffer->length - buffer->type->video.offset[1]);
  88. mmal_buffer_header_mem_unlock(buffer);
  89. mmal_port_buffer_header_callback(port, buffer);
  90. }
  91. vcos_sleep(10); /* Make sure we don't peg all the resources */
  92. }
  93. /** Destroy a previously created component */
  94. static MMAL_STATUS_T artificial_camera_component_destroy(MMAL_COMPONENT_T *component)
  95. {
  96. unsigned int i;
  97. for (i = 0; i < component->output_num; i++)
  98. if (component->output[i]->priv->module->queue)
  99. mmal_queue_destroy(component->output[i]->priv->module->queue);
  100. if(component->output_num)
  101. mmal_ports_free(component->output, component->output_num);
  102. vcos_free(component->priv->module);
  103. return MMAL_SUCCESS;
  104. }
  105. /** Enable processing on a port */
  106. static MMAL_STATUS_T artificial_camera_port_enable(MMAL_PORT_T *port, MMAL_PORT_BH_CB_T cb)
  107. {
  108. MMAL_PARAM_UNUSED(port);
  109. MMAL_PARAM_UNUSED(cb);
  110. return MMAL_SUCCESS;
  111. }
  112. /** Flush a port */
  113. static MMAL_STATUS_T artificial_camera_port_flush(MMAL_PORT_T *port)
  114. {
  115. MMAL_PARAM_UNUSED(port);
  116. return MMAL_SUCCESS;
  117. }
  118. /** Disable processing on a port */
  119. static MMAL_STATUS_T artificial_camera_port_disable(MMAL_PORT_T *port)
  120. {
  121. MMAL_PARAM_UNUSED(port);
  122. return MMAL_SUCCESS;
  123. }
  124. /** Send a buffer header to a port */
  125. static MMAL_STATUS_T artificial_camera_port_send(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  126. {
  127. /* Just queue the buffer */
  128. mmal_queue_put(port->priv->module->queue, buffer);
  129. mmal_component_action_trigger(port->component);
  130. return MMAL_SUCCESS;
  131. }
  132. /** Set format on a port */
  133. static MMAL_STATUS_T artificial_camera_port_format_commit(MMAL_PORT_T *port)
  134. {
  135. MMAL_PORT_MODULE_T *port_module = port->priv->module;
  136. unsigned int width = port->format->es->video.width;
  137. unsigned int height = port->format->es->video.height;
  138. width = (width + 31) & ~31;
  139. height = (height + 15) & ~15;
  140. /* We only support a few formats */
  141. switch(port->format->encoding)
  142. {
  143. case MMAL_ENCODING_I420:
  144. port_module->frame_size = width * height * 3 / 2;
  145. port_module->frame.planes = 3;
  146. port_module->frame.pitch[0] = width;
  147. port_module->frame.offset[1] = port_module->frame.pitch[0] * height;
  148. port_module->frame.pitch[1] = width / 2;
  149. port_module->frame.offset[2] = port_module->frame.offset[1] + port_module->frame.pitch[1] * height / 2;
  150. port_module->frame.pitch[2] = width / 2;
  151. break;
  152. case MMAL_ENCODING_NV21:
  153. port_module->frame_size = width * height * 3 / 2;
  154. port_module->frame.planes = 2;
  155. port_module->frame.pitch[0] = width;
  156. port_module->frame.offset[1] = port_module->frame.pitch[0] * height;
  157. port_module->frame.pitch[1] = width;
  158. break;
  159. case MMAL_ENCODING_I422:
  160. port_module->frame_size = width * height * 2;
  161. port_module->frame.planes = 3;
  162. port_module->frame.pitch[0] = width;
  163. port_module->frame.offset[1] = port_module->frame.pitch[0] * height;
  164. port_module->frame.pitch[1] = width / 2;
  165. port_module->frame.offset[2] = port_module->frame.offset[1] + port_module->frame.pitch[1] * height;
  166. port_module->frame.pitch[2] = width / 2;
  167. break;
  168. default:
  169. return MMAL_ENOSYS;
  170. }
  171. port->buffer_size_min = port->buffer_size_recommended = port_module->frame_size;
  172. return MMAL_SUCCESS;
  173. }
  174. /** Set parameter on a port */
  175. static MMAL_STATUS_T artificial_port_parameter_set(MMAL_PORT_T *port, const MMAL_PARAMETER_HEADER_T *param)
  176. {
  177. MMAL_PARAM_UNUSED(port);
  178. switch (param->id)
  179. {
  180. default:
  181. return MMAL_ENOSYS;
  182. }
  183. }
  184. /** Get parameter on a port */
  185. static MMAL_STATUS_T artificial_port_parameter_get(MMAL_PORT_T *port, MMAL_PARAMETER_HEADER_T *param)
  186. {
  187. MMAL_PARAM_UNUSED(port);
  188. switch (param->id)
  189. {
  190. default:
  191. return MMAL_ENOSYS;
  192. }
  193. }
  194. /** Create an instance of a component */
  195. static MMAL_STATUS_T mmal_component_create_artificial_camera(const char *name, MMAL_COMPONENT_T *component)
  196. {
  197. MMAL_STATUS_T status = MMAL_ENOMEM;
  198. unsigned int i;
  199. MMAL_PARAM_UNUSED(name);
  200. /* Allocate our module context */
  201. component->priv->module = vcos_calloc(1, sizeof(*component->priv->module), "mmal module");
  202. if (!component->priv->module)
  203. return MMAL_ENOMEM;
  204. component->priv->pf_destroy = artificial_camera_component_destroy;
  205. /* Allocate all the ports for this component */
  206. component->output = mmal_ports_alloc(component, ARTIFICIAL_CAMERA_PORTS_NUM, MMAL_PORT_TYPE_OUTPUT,
  207. sizeof(MMAL_PORT_MODULE_T));
  208. if(!component->output)
  209. goto error;
  210. component->output_num = ARTIFICIAL_CAMERA_PORTS_NUM;
  211. for (i = 0; i < component->output_num; i++)
  212. {
  213. component->output[i]->priv->pf_enable = artificial_camera_port_enable;
  214. component->output[i]->priv->pf_disable = artificial_camera_port_disable;
  215. component->output[i]->priv->pf_flush = artificial_camera_port_flush;
  216. component->output[i]->priv->pf_send = artificial_camera_port_send;
  217. component->output[i]->priv->pf_send = artificial_camera_port_send;
  218. component->output[i]->priv->pf_set_format = artificial_camera_port_format_commit;
  219. component->output[i]->priv->pf_parameter_set = artificial_port_parameter_set;
  220. component->output[i]->priv->pf_parameter_get = artificial_port_parameter_get;
  221. component->output[i]->format->type = MMAL_ES_TYPE_VIDEO;
  222. component->output[i]->format->encoding = MMAL_ENCODING_I420;
  223. component->output[i]->format->es->video.width = DEFAULT_WIDTH;
  224. component->output[i]->format->es->video.height = DEFAULT_HEIGHT;
  225. component->output[i]->buffer_num_min = OUTPUT_MIN_BUFFER_NUM;
  226. component->output[i]->buffer_num_recommended = OUTPUT_RECOMMENDED_BUFFER_NUM;
  227. artificial_camera_port_format_commit(component->output[i]);
  228. component->output[i]->priv->module->queue = mmal_queue_create();
  229. if (!component->output[i]->priv->module->queue)
  230. goto error;
  231. }
  232. status = mmal_component_action_register(component, artificial_camera_do_processing);
  233. if (status != MMAL_SUCCESS)
  234. goto error;
  235. return MMAL_SUCCESS;
  236. error:
  237. artificial_camera_component_destroy(component);
  238. return status;
  239. }
  240. MMAL_CONSTRUCTOR(mmal_register_component_artificial_camera);
  241. void mmal_register_component_artificial_camera(void)
  242. {
  243. mmal_component_supplier_register("artificial_camera", mmal_component_create_artificial_camera);
  244. }