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.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. /** \file
  26. * Multi-Media Abstraction Layer API
  27. */
  28. #ifndef MMAL_H
  29. #define MMAL_H
  30. /**
  31. *
  32. * \mainpage Multi-Media Abstraction Layer (MMAL). Draft Version 0.1.
  33. *
  34. * \par Contents
  35. * - \ref intro_sec
  36. * - \ref features
  37. * - \ref concepts
  38. * - \ref comp
  39. * - \ref create
  40. * - \ref port
  41. * - \ref buf
  42. * - \ref metadata
  43. * - \ref queue
  44. * - \ref pool
  45. * - \ref param
  46. * - \ref events
  47. * - \ref version
  48. * - \ref example
  49. *
  50. * \section intro_sec Introduction
  51. *
  52. * MMAL (Multi-Media Abstraction Layer) is a framework which is used to provide a host-side,
  53. * simple and relatively low-level interface to multimedia components running on VideoCore.
  54. * It also provides a component interface so that new components can be easily created and
  55. * integrated into the framework.
  56. *
  57. * There is no requirement that all the components be running on VideoCore as MMAL doesn't
  58. * put any restriction on where components live. The current implementation for instance
  59. * provides some components which can be run on both host-side or VideoCore (e.g. the splitter
  60. * component).
  61. *
  62. * \section features Features
  63. *
  64. * The MMAL API has been designed to support all the following features:
  65. * - Sufficiently generic to support different kinds of multimedia component.
  66. * - Simple to use from client side (mostly synchronous except where it matters).
  67. * - Straightforward API for designing components (e.g. avoids multiple data paths, as found in RIL).
  68. * - Allows for fully-optimised implementation of components (e.g. zero-copy buffer passing).
  69. * - Portability (API is self-contained).
  70. * - Supports multiple instances (e.g. of VideoCore).
  71. * - Extensible without breaking source or binary backward compatibility.
  72. *
  73. * \section concepts API concepts
  74. *
  75. * The MMAL API is based on the concept of components, ports and buffer headers.
  76. * Clients create MMAL components which expose ports for each individual
  77. * elementary stream of data they support (e.g. audio/video). Components expose
  78. * input ports to receive data from the client, and expose output ports
  79. * to return data to the client.
  80. *
  81. * Data sent to or received from the component needs to be attached to a buffer header.
  82. * Buffer headers are necessary because they contain buffer specific ancillary data which is
  83. * necessary for the component and client to do their processing (e.g timestamps).
  84. *
  85. * \section comp Components
  86. *
  87. * MMAL lets clients create multi-media components (video encoders,
  88. * video decoders, camera, and so-on) using a common API. Clients exchange
  89. * data with components using buffer headers. A buffer header
  90. * has a pointer to the payload data and optional metadata.
  91. * Buffer headers are sent to and received from ports that are provided by components.
  92. *
  93. * A typical decoder component would have a single input port and a
  94. * single output port, but the same architecture could also be used
  95. * for components with different layouts (e.g. a camera with a
  96. * capture and preview port, or a debugging component with just an input port).
  97. *
  98. * \subsection create Component Creation
  99. *
  100. * Each component is identified by a unique name. To create a specific component
  101. * the client needs to call \ref mmal_component_create with the desired component's
  102. * name as an argument.
  103. * This call will return a context (\ref MMAL_COMPONENT_T) to the component. This
  104. * context will expose the input and output ports (\ref MMAL_PORT_T) supported
  105. * by this specific component.
  106. *
  107. * \note All VideoCore components have a name starting with the "vc." prefix (this prefix
  108. * is used to distinguish when a creation request needs to be forwarded to VideoCore).
  109. *
  110. * \section port Component Ports
  111. *
  112. * A port (\ref MMAL_PORT_T) is the entity which exposes an elementary stream
  113. * (\ref MMAL_ES_FORMAT_T) on a component. It is also the entity to which buffer headers
  114. * (\ref MMAL_BUFFER_HEADER_T) are sent or from which they are received.
  115. *
  116. * Clients do not need to create ports. They are created automatically by
  117. * the component when this one is created but the format of a port might need to
  118. * be set by the client depending on the type of component the client is using.
  119. *
  120. * For example, for a video decoding component, one input port and one output port
  121. * will be available. The format of the input port must be set by the
  122. * client (using \ref mmal_port_format_commit) while the format of the output port
  123. * will be automatically set by the component once the component has enough information
  124. * to find out what its format should be.
  125. *
  126. * If the input port format contains enough information for the component to determine
  127. * the format of the output port straight away, then the output port will be set to the proper
  128. * format when \ref mmal_port_format_commit returns. Otherwise the output format will be set to
  129. * \ref MMAL_ENCODING_UNKNOWN until the component is fed enough data to determine the format
  130. * of the output port.
  131. * When this happens, the client will receive an event on the output port, signalling
  132. * that its format has changed.
  133. *
  134. * \section buf Buffer Headers
  135. *
  136. * Buffer headers (\ref MMAL_BUFFER_HEADER_T) are used to exchange data with components.
  137. * They do not contain the data directly but instead contain a pointer to the data being
  138. * transferred.
  139. *
  140. * Separating the buffer headers from the payload means that the memory for the data can
  141. * be allocated outside of MMAL (e.g. if it is supplied by an external library) while still
  142. * providing a consistent way to exchange data between clients and components.
  143. *
  144. * Buffer headers are allocated from pools and are reference counted. The refcount
  145. * will drop when \ref mmal_buffer_header_release is called and the buffer header
  146. * will be recycled to its pool when it reaches zero.
  147. * The client can be notified when the buffer header is recycled so that it can recycle the
  148. * associated payload memory as well.
  149. *
  150. * A pool of buffer headers should be created after committing the format of the port. When
  151. * the format is changed, the minimum and recommended size and number of buffers may change.
  152. *
  153. * \note The current number of buffers and their size (\ref MMAL_PORT_T::buffer_num and \ref
  154. * MMAL_PORT_T::buffer_size) are not modified by MMAL, and must be updated by the client as
  155. * required after changes to a port's format.
  156. *
  157. * \subsection metadata Buffer Metadata
  158. *
  159. * The API provides a way for clients or components to associate metadata with buffer headers.
  160. * A camera component could for example store information like exposure time or focal length
  161. * as metadata within the buffer header containing the frame just captured.
  162. * \note This area needs more work
  163. *
  164. * \subsection queue Queues of Buffer Headers
  165. *
  166. * Queues (\ref MMAL_QUEUE_T) are a facility that allows thread-safe processing of buffer headers
  167. * from the client. Callbacks triggered by a MMAL component when it sends a buffer header to the
  168. * client can simply put the buffer in a queue and let the main processing thread of the client
  169. * get its data from the queue.
  170. *
  171. * \subsection pool Pools of Buffer Headers
  172. *
  173. * Pools (\ref MMAL_POOL_T) let clients allocate a fixed number of buffer headers, and
  174. * a queue (\ref MMAL_QUEUE_T). They are used for buffer header allocation.
  175. * Optionally a pool can also allocate the payload memory for the client.
  176. *
  177. * Pools can also be resized after creation, for example, if the port format is changed leading
  178. * to a new number or size of buffers being required.
  179. *
  180. * \section param Port Parameters
  181. *
  182. * Components support setting and getting component specific parameters using
  183. * \ref mmal_port_parameter_set and \ref mmal_port_parameter_get. Parameters
  184. * are identified using an integer index; parameter data is binary. See the \ref MMAL_PARAMETER_IDS
  185. * "Pre-defined MMAL parameter IDs" page for more information on the pre-defined parameters.
  186. *
  187. * \section events Port Events
  188. *
  189. * Components can generate events on their ports. Events are sent to clients
  190. * as buffer headers and thus when the client receives a buffer header on one
  191. * of the component's port it should check if the buffer header is an event
  192. * and in which case process it and then release it (\ref mmal_buffer_header_release).
  193. * The reason for transmitting events in-band with the actual data is that it
  194. * is often very valuable to know exactly when the event happens relative to the
  195. * the actual data (e.g. with a focus event, from which video frame are we in focus).\n
  196. * Buffer headers used to transmit events are allocated internally by the framework
  197. * so it is important to release the buffer headers with \ref mmal_buffer_header_release
  198. * so the buffer headers make it back to their actual owner.
  199. *
  200. * Event buffer headers are allocated when the component is created, based on the
  201. * minimum number and size of control port buffers set by the component. Component
  202. * wide events (not port specific) are sent to the control port callback when that
  203. * port is enabled. Port events are sent to the port callback, the same as data
  204. * buffers, but the 'cmd' field is non-zero.
  205. *
  206. * \section version Versioning
  207. *
  208. * The API requires that the MMAL core be the same or more recent version
  209. * as the components and clients. Clients and components can be older and
  210. * the API will still work both at compile-time and run-time.
  211. *
  212. * \section example Example Code
  213. *
  214. * The following code is a simple example on how to do video decoding using MMAL. Note that
  215. * the code is intended to be clear and illustrate how to use MMAL at its most fundamental
  216. * level, not necessarily the most efficient way to achieve the same result. Use of opaque
  217. * images, tunneling and zero-copy inter-processor buffers can all improve the performance
  218. * or reduce the load.
  219. *
  220. * The \ref MmalConnectionUtility "Port Connection Utility" functions can also be used to
  221. * replace much of the common "boilerplate" code, especially when a pipeline of several
  222. * components needs to be processed.
  223. *
  224. * \code
  225. * #include <mmal.h>
  226. * ...
  227. * static void input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  228. * {
  229. * // The decoder is done with the data, just recycle the buffer header into its pool
  230. * mmal_buffer_header_release(buffer);
  231. * }
  232. * static void output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  233. * {
  234. * MMAL_QUEUE_T *queue = (MMAL_QUEUE_T *)port->userdata;
  235. * mmal_queue_put(queue, buffer); // Queue the decoded video frame
  236. * }
  237. * ...
  238. *
  239. * MMAL_COMPONENT_T *decoder = 0;
  240. * MMAL_STATUS_T status;
  241. *
  242. * // Create the video decoder component on VideoCore
  243. * status = mmal_component_create("vc.ril.video_decoder", &decoder);
  244. * ABORT_IF_ERROR(status);
  245. *
  246. * // Set format of video decoder input port
  247. * MMAL_ES_FORMAT_T *format_in = decoder->input[0]->format;
  248. * format_in->type = MMAL_ES_TYPE_VIDEO;
  249. * format_in->encoding = MMAL_ENCODING_H264;
  250. * format_in->es->video.width = 1280;
  251. * format_in->es->video.height = 720;
  252. * format_in->es->video.frame_rate.num = 30;
  253. * format_in->es->video.frame_rate.den = 1;
  254. * format_in->es->video.par.num = 1;
  255. * format_in->es->video.par.den = 1;
  256. * format_in->flags = MMAL_ES_FORMAT_FLAG_FRAMED;
  257. * status = mmal_format_extradata_alloc(format_in, YOUR_H264_CODEC_HEADER_BYTES_SIZE);
  258. * ABORT_IF_ERROR(status);
  259. * format_in->extradata_size = YOUR_H264_CODEC_HEADER_BYTES_SIZE;
  260. * memcpy(format_in->extradata, YOUR_H264_CODEC_HEADER_BYTES, format_in->extradata_size);
  261. *
  262. * status = mmal_port_format_commit(decoder->input[0]);
  263. * ABORT_IF_ERROR(status);
  264. *
  265. * // Once the call to mmal_port_format_commit() on the input port returns, the decoder will
  266. * // have set the format of the output port.
  267. * // If the decoder still doesn t have enough information to determine the format of the
  268. * // output port, the encoding will be set to unknown. As soon as the decoder receives
  269. * // enough stream data to determine the format of the output port it will send an event
  270. * // to the client to signal that the format of the port has changed.
  271. * // However, for the sake of simplicity this example assumes that the decoder was given
  272. * // all the necessary information right at the start (i.e. video format and codec header bytes)
  273. * MMAL_FORMAT_T *format_out = decoder->output[0]->format;
  274. * if (format_out->encoding == MMAL_ENCODING_UNKNOWN)
  275. * ABORT();
  276. *
  277. * // Now we know the format of both ports and the requirements of the decoder, we can create
  278. * // our buffer headers and their associated memory buffers. We use the buffer pool API for this.
  279. * decoder->input[0]->buffer_num = decoder->input[0]->buffer_num_min;
  280. * decoder->input[0]->buffer_size = decoder->input[0]->buffer_size_min;
  281. * MMAL_POOL_T *pool_in = mmal_pool_create(decoder->input[0]->buffer_num,
  282. * decoder->input[0]->buffer_size);
  283. * decoder->output[0]->buffer_num = decoder->output[0]->buffer_num_min;
  284. * decoder->output[0]->buffer_size = decoder->output[0]->buffer_size_min;
  285. * MMAL_POOL_T *pool_out = mmal_pool_create(decoder->output[0]->buffer_num,
  286. * decoder->output[0]->buffer_size);
  287. *
  288. * // Create a queue to store our decoded video frames. The callback we will get when
  289. * // a frame has been decoded will put the frame into this queue.
  290. * MMAL_QUEUE_T *queue_decoded_frames = mmal_queue_create();
  291. * decoder->output[0]->userdata = (void)queue_decoded_frames;
  292. *
  293. * // Enable all the input port and the output port.
  294. * // The callback specified here is the function which will be called when the buffer header
  295. * // we sent to the component has been processed.
  296. * status = mmal_port_enable(decoder->input[0], input_callback);
  297. * ABORT_IF_ERROR(status);
  298. * status = mmal_port_enable(decoder->output[0], output_callback);
  299. * ABORT_IF_ERROR(status);
  300. *
  301. * // Enable the component. Components will only process data when they are enabled.
  302. * status = mmal_component_enable(decoder);
  303. * ABORT_IF_ERROR(status);
  304. *
  305. * // Data processing loop
  306. * while (1)
  307. * {
  308. * MMAL_BUFFER_HEADER_T *header;
  309. *
  310. * // The client needs to implement its own blocking code.
  311. * // (e.g. a semaphore which is posted when a buffer header is put in one of the queues)
  312. * WAIT_FOR_QUEUES_TO_HAVE_BUFFERS();
  313. *
  314. * // Send empty buffers to the output port of the decoder to allow the decoder to start
  315. * // producing frames as soon as it gets input data
  316. * while ((buffer = mmal_queue_get(pool_out->queue)) != NULL)
  317. * {
  318. * status = mmal_port_send_buffer(decoder->output[0], buffer);
  319. * ABORT_IF_ERROR(status);
  320. * }
  321. *
  322. * // Send data to decode to the input port of the video decoder
  323. * if ((buffer = mmal_queue_get(pool_in->queue)) != NULL)
  324. * {
  325. * READ_DATA_INTO_BUFFER(buffer);
  326. *
  327. * status = mmal_port_send_buffer(decoder->input[0], buffer);
  328. * ABORT_IF_ERROR(status);
  329. * }
  330. *
  331. * // Get our decoded frames. We also need to cope with events
  332. * // generated from the component here.
  333. * while ((buffer = mmal_queue_get(queue_decoded_frames)) != NULL)
  334. * {
  335. * if (buffer->cmd)
  336. * {
  337. * // This is an event. Do something with it and release the buffer.
  338. * mmal_buffer_header_release(buffer);
  339. * continue;
  340. * }
  341. *
  342. * // We have a frame, do something with it (why not display it for instance?).
  343. * // Once we're done with it, we release it. It will magically go back
  344. * // to its original pool so it can be reused for a new video frame.
  345. * mmal_buffer_header_release(buffer);
  346. * }
  347. * }
  348. *
  349. * // Cleanup everything
  350. * mmal_component_destroy(decoder);
  351. * mmal_pool_destroy(pool_in);
  352. * mmal_pool_destroy(pool_out);
  353. * mmal_queue_destroy(queue_decode_frames);
  354. *
  355. * \endcode
  356. */
  357. #include "mmal_common.h"
  358. #include "mmal_types.h"
  359. #include "mmal_port.h"
  360. #include "mmal_component.h"
  361. #include "mmal_parameters.h"
  362. #include "mmal_metadata.h"
  363. #include "mmal_queue.h"
  364. #include "mmal_pool.h"
  365. #include "mmal_events.h"
  366. /**/
  367. /** \name API Version
  368. * The following define the version number of the API */
  369. /* @{ */
  370. /** Major version number.
  371. * This changes when the API breaks in a way which is not backward compatible. */
  372. #define MMAL_VERSION_MAJOR 0
  373. /** Minor version number.
  374. * This changes each time the API is extended in a way which is still source and
  375. * binary compatible. */
  376. #define MMAL_VERSION_MINOR 1
  377. #define MMAL_VERSION (MMAL_VERSION_MAJOR << 16 | MMAL_VERSION_MINOR)
  378. #define MMAL_VERSION_TO_MAJOR(a) (a >> 16)
  379. #define MMAL_VERSION_TO_MINOR(a) (a & 0xFFFF)
  380. /* @} */
  381. #endif /* MMAL_H */