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.

mmal_util.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 "interface/mmal/mmal.h"
  26. #include "mmal_encodings.h"
  27. #include "mmal_util.h"
  28. #include "mmal_logging.h"
  29. #include <string.h>
  30. #include <stdio.h>
  31. #define STATUS_TO_STR(x) { MMAL_##x, #x }
  32. static struct {
  33. MMAL_STATUS_T status;
  34. const char *str;
  35. } status_to_string_map[] =
  36. {
  37. STATUS_TO_STR(SUCCESS),
  38. STATUS_TO_STR(ENOMEM),
  39. STATUS_TO_STR(ENOSPC),
  40. STATUS_TO_STR(EINVAL),
  41. STATUS_TO_STR(ENOSYS),
  42. STATUS_TO_STR(ENOENT),
  43. STATUS_TO_STR(ENXIO),
  44. STATUS_TO_STR(EIO),
  45. STATUS_TO_STR(ESPIPE),
  46. STATUS_TO_STR(ECORRUPT),
  47. STATUS_TO_STR(ENOTREADY),
  48. STATUS_TO_STR(ECONFIG),
  49. {0, 0}
  50. };
  51. const char *mmal_status_to_string(MMAL_STATUS_T status)
  52. {
  53. unsigned i;
  54. for (i=0; status_to_string_map[i].str; i++)
  55. if (status_to_string_map[i].status == status)
  56. break;
  57. return status_to_string_map[i].str ? status_to_string_map[i].str : "UNKNOWN";
  58. }
  59. static struct {
  60. uint32_t encoding;
  61. uint32_t pitch_num;
  62. uint32_t pitch_den;
  63. } pixel_pitch[] =
  64. {
  65. {MMAL_ENCODING_I420, 1, 1},
  66. {MMAL_ENCODING_YV12, 1, 1},
  67. {MMAL_ENCODING_I422, 1, 1},
  68. {MMAL_ENCODING_NV21, 1, 1},
  69. {MMAL_ENCODING_NV12, 1, 1},
  70. {MMAL_ENCODING_ARGB, 4, 1},
  71. {MMAL_ENCODING_RGBA, 4, 1},
  72. {MMAL_ENCODING_RGB32, 4, 1},
  73. {MMAL_ENCODING_ABGR, 4, 1},
  74. {MMAL_ENCODING_BGRA, 4, 1},
  75. {MMAL_ENCODING_BGR32, 4, 1},
  76. {MMAL_ENCODING_RGB16, 2, 1},
  77. {MMAL_ENCODING_RGB24, 3, 1},
  78. {MMAL_ENCODING_BGR16, 2, 1},
  79. {MMAL_ENCODING_BGR24, 3, 1},
  80. {MMAL_ENCODING_YUYV, 2, 1},
  81. {MMAL_ENCODING_YVYU, 2, 1},
  82. {MMAL_ENCODING_UYVY, 2, 1},
  83. {MMAL_ENCODING_VYUY, 2, 1},
  84. /* {MMAL_ENCODING_YUVUV128, 1, 1}, That's a special case which must not be included */
  85. {MMAL_ENCODING_UNKNOWN, 0, 0}
  86. };
  87. uint32_t mmal_encoding_stride_to_width(uint32_t encoding, uint32_t stride)
  88. {
  89. unsigned int i;
  90. for(i = 0; pixel_pitch[i].encoding != MMAL_ENCODING_UNKNOWN; i++)
  91. if(pixel_pitch[i].encoding == encoding) break;
  92. if(pixel_pitch[i].encoding == MMAL_ENCODING_UNKNOWN)
  93. return 0;
  94. return pixel_pitch[i].pitch_den * stride / pixel_pitch[i].pitch_num;
  95. }
  96. uint32_t mmal_encoding_width_to_stride(uint32_t encoding, uint32_t width)
  97. {
  98. unsigned int i;
  99. for(i = 0; pixel_pitch[i].encoding != MMAL_ENCODING_UNKNOWN; i++)
  100. if(pixel_pitch[i].encoding == encoding) break;
  101. if(pixel_pitch[i].encoding == MMAL_ENCODING_UNKNOWN)
  102. return 0;
  103. return pixel_pitch[i].pitch_num * width / pixel_pitch[i].pitch_den;
  104. }
  105. const char* mmal_port_type_to_string(MMAL_PORT_TYPE_T type)
  106. {
  107. const char *str;
  108. switch (type)
  109. {
  110. case MMAL_PORT_TYPE_INPUT: str = "in"; break;
  111. case MMAL_PORT_TYPE_OUTPUT: str = "out"; break;
  112. case MMAL_PORT_TYPE_CLOCK: str = "clk"; break;
  113. case MMAL_PORT_TYPE_CONTROL: str = "ctr"; break;
  114. default: str = "invalid"; break;
  115. }
  116. return str;
  117. }
  118. MMAL_PARAMETER_HEADER_T *mmal_port_parameter_alloc_get(MMAL_PORT_T *port,
  119. uint32_t id, uint32_t size, MMAL_STATUS_T *p_status)
  120. {
  121. MMAL_PARAMETER_HEADER_T *param = NULL;
  122. MMAL_STATUS_T status = MMAL_ENOSYS;
  123. if (size < sizeof(MMAL_PARAMETER_HEADER_T))
  124. size = sizeof(MMAL_PARAMETER_HEADER_T);
  125. if ((param = vcos_calloc(1, size, "mmal_port_param_get")) == NULL)
  126. {
  127. status = MMAL_ENOMEM;
  128. goto error;
  129. }
  130. param->id = id;
  131. param->size = size;
  132. if ((status = mmal_port_parameter_get(port, param)) == MMAL_ENOSPC)
  133. {
  134. /* We need to reallocate to get enough space for all parameter data */
  135. size = param->size;
  136. vcos_free(param);
  137. if ((param = vcos_calloc(1, size, "mmal_port_param_get")) == NULL)
  138. {
  139. status = MMAL_ENOMEM;
  140. goto error;
  141. }
  142. /* Now retrieve it again */
  143. param->id = id;
  144. param->size = size;
  145. status = mmal_port_parameter_get(port, param);
  146. }
  147. if (status != MMAL_SUCCESS)
  148. goto error;
  149. end:
  150. if (p_status) *p_status = status;
  151. return param;
  152. error:
  153. if (param) vcos_free(param);
  154. param = NULL;
  155. goto end;
  156. }
  157. void mmal_port_parameter_free(MMAL_PARAMETER_HEADER_T *param)
  158. {
  159. vcos_free(param);
  160. }
  161. /** Copy buffer header metadata from source to dest
  162. */
  163. void mmal_buffer_header_copy_header(MMAL_BUFFER_HEADER_T *dest, const MMAL_BUFFER_HEADER_T *src)
  164. {
  165. dest->cmd = src->cmd;
  166. dest->offset = src->offset;
  167. dest->length = src->length;
  168. dest->flags = src->flags;
  169. dest->pts = src->pts;
  170. dest->dts = src->dts;
  171. *dest->type = *src->type;
  172. }
  173. /** Create a pool of MMAL_BUFFER_HEADER_T */
  174. MMAL_POOL_T *mmal_port_pool_create(MMAL_PORT_T *port, unsigned int headers, uint32_t payload_size)
  175. {
  176. if (!port || !port->priv)
  177. return NULL;
  178. LOG_TRACE("%s(%i:%i) port %p, headers %u, size %i", port->component->name,
  179. (int)port->type, (int)port->index, port, headers, (int)payload_size);
  180. /* Create a pool and ask the port for some memory */
  181. return mmal_pool_create_with_allocator(headers, payload_size, (void *)port,
  182. (mmal_pool_allocator_alloc_t)mmal_port_payload_alloc,
  183. (mmal_pool_allocator_free_t)mmal_port_payload_free);
  184. }
  185. /** Destroy a pool of MMAL_BUFFER_HEADER_T */
  186. void mmal_port_pool_destroy(MMAL_PORT_T *port, MMAL_POOL_T *pool)
  187. {
  188. if (!port || !port->priv || !pool)
  189. return;
  190. LOG_TRACE("%s(%i:%i) port %p, pool %p", port->component->name,
  191. (int)port->type, (int)port->index, port, pool);
  192. if (!vcos_verify(!port->is_enabled))
  193. {
  194. LOG_ERROR("port %p, pool %p destroyed while port enabled", port, pool);
  195. mmal_port_disable(port);
  196. }
  197. mmal_pool_destroy(pool);
  198. }
  199. /*****************************************************************************/
  200. void mmal_log_dump_port(MMAL_PORT_T *port)
  201. {
  202. if (!port)
  203. return;
  204. LOG_DEBUG("%s(%p)", port->name, port);
  205. mmal_log_dump_format(port->format);
  206. LOG_DEBUG(" buffers num: %i(opt %i, min %i), size: %i(opt %i, min: %i), align: %i",
  207. port->buffer_num, port->buffer_num_recommended, port->buffer_num_min,
  208. port->buffer_size, port->buffer_size_recommended, port->buffer_size_min,
  209. port->buffer_alignment_min);
  210. }
  211. /*****************************************************************************/
  212. void mmal_log_dump_format(MMAL_ES_FORMAT_T *format)
  213. {
  214. const char *name_type;
  215. if (!format)
  216. return;
  217. switch(format->type)
  218. {
  219. case MMAL_ES_TYPE_AUDIO: name_type = "audio"; break;
  220. case MMAL_ES_TYPE_VIDEO: name_type = "video"; break;
  221. case MMAL_ES_TYPE_SUBPICTURE: name_type = "subpicture"; break;
  222. default: name_type = "unknown"; break;
  223. }
  224. LOG_DEBUG("type: %s, fourcc: %4.4s", name_type, (char *)&format->encoding);
  225. LOG_DEBUG(" bitrate: %i, framed: %i", format->bitrate,
  226. !!(format->flags & MMAL_ES_FORMAT_FLAG_FRAMED));
  227. LOG_DEBUG(" extra data: %i, %p", format->extradata_size, format->extradata);
  228. switch(format->type)
  229. {
  230. case MMAL_ES_TYPE_AUDIO:
  231. LOG_DEBUG(" samplerate: %i, channels: %i, bps: %i, block align: %i",
  232. format->es->audio.sample_rate, format->es->audio.channels,
  233. format->es->audio.bits_per_sample, format->es->audio.block_align);
  234. break;
  235. case MMAL_ES_TYPE_VIDEO:
  236. LOG_DEBUG(" width: %i, height: %i, (%i,%i,%i,%i)",
  237. format->es->video.width, format->es->video.height,
  238. format->es->video.crop.x, format->es->video.crop.y,
  239. format->es->video.crop.width, format->es->video.crop.height);
  240. LOG_DEBUG(" pixel aspect ratio: %i/%i, frame rate: %i/%i",
  241. format->es->video.par.num, format->es->video.par.den,
  242. format->es->video.frame_rate.num, format->es->video.frame_rate.den);
  243. break;
  244. case MMAL_ES_TYPE_SUBPICTURE:
  245. break;
  246. default: break;
  247. }
  248. }
  249. MMAL_PORT_T *mmal_util_get_port(MMAL_COMPONENT_T *comp, MMAL_PORT_TYPE_T type, unsigned index)
  250. {
  251. unsigned num;
  252. MMAL_PORT_T **list;
  253. switch (type)
  254. {
  255. case MMAL_PORT_TYPE_INPUT:
  256. num = comp->input_num;
  257. list = comp->input;
  258. break;
  259. case MMAL_PORT_TYPE_OUTPUT:
  260. num = comp->output_num;
  261. list = comp->output;
  262. break;
  263. case MMAL_PORT_TYPE_CLOCK:
  264. num = comp->clock_num;
  265. list = comp->clock;
  266. break;
  267. case MMAL_PORT_TYPE_CONTROL:
  268. num = 1;
  269. list = &comp->control;
  270. break;
  271. default:
  272. vcos_assert(0);
  273. return NULL;
  274. }
  275. if (index < num)
  276. /* coverity[ptr_arith] num is 1 here */
  277. return list[index];
  278. else
  279. return NULL;
  280. }
  281. char *mmal_4cc_to_string(char *buf, size_t len, uint32_t fourcc)
  282. {
  283. char *src = (char*)&fourcc;
  284. vcos_assert(len >= 5);
  285. if (len < 5)
  286. {
  287. buf[0] = '\0';
  288. }
  289. else if (fourcc)
  290. {
  291. memcpy(buf, src, 4);
  292. buf[4] = '\0';
  293. }
  294. else
  295. {
  296. snprintf(buf, len, "<0>");
  297. }
  298. return buf;
  299. }