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.

example_basic_2.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "util/mmal_default_components.h"
  27. #include "util/mmal_util_params.h"
  28. #include "interface/vcos/vcos.h"
  29. #include <stdio.h>
  30. #define CHECK_STATUS(status, msg) if (status != MMAL_SUCCESS) { fprintf(stderr, msg"\n"); goto error; }
  31. static uint8_t codec_header_bytes[512];
  32. static unsigned int codec_header_bytes_size = sizeof(codec_header_bytes);
  33. static FILE *source_file;
  34. /* Macros abstracting the I/O, just to make the example code clearer */
  35. #define SOURCE_OPEN(uri) \
  36. source_file = fopen(uri, "rb"); if (!source_file) goto error;
  37. #define SOURCE_READ_CODEC_CONFIG_DATA(bytes, size) \
  38. size = fread(bytes, 1, size, source_file); rewind(source_file)
  39. #define SOURCE_READ_DATA_INTO_BUFFER(a) \
  40. a->length = fread(a->data, 1, a->alloc_size - 128, source_file); \
  41. a->offset = 0
  42. #define SOURCE_CLOSE() \
  43. if (source_file) fclose(source_file)
  44. /** Context for our application */
  45. static struct CONTEXT_T {
  46. VCOS_SEMAPHORE_T semaphore;
  47. MMAL_QUEUE_T *queue;
  48. MMAL_STATUS_T status;
  49. } context;
  50. /** Callback from the control port.
  51. * Component is sending us an event. */
  52. static void control_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  53. {
  54. struct CONTEXT_T *ctx = (struct CONTEXT_T *)port->userdata;
  55. switch (buffer->cmd)
  56. {
  57. case MMAL_EVENT_EOS:
  58. /* Only sink component generate EOS events */
  59. break;
  60. case MMAL_EVENT_ERROR:
  61. /* Something went wrong. Signal this to the application */
  62. ctx->status = *(MMAL_STATUS_T *)buffer->data;
  63. break;
  64. default:
  65. break;
  66. }
  67. /* Done with the event, recycle it */
  68. mmal_buffer_header_release(buffer);
  69. /* Kick the processing thread */
  70. vcos_semaphore_post(&ctx->semaphore);
  71. }
  72. /** Callback from the input port.
  73. * Buffer has been consumed and is available to be used again. */
  74. static void input_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  75. {
  76. struct CONTEXT_T *ctx = (struct CONTEXT_T *)port->userdata;
  77. /* The decoder is done with the data, just recycle the buffer header into its pool */
  78. mmal_buffer_header_release(buffer);
  79. /* Kick the processing thread */
  80. vcos_semaphore_post(&ctx->semaphore);
  81. }
  82. /** Callback from the output port.
  83. * Buffer has been produced by the port and is available for processing. */
  84. static void output_callback(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  85. {
  86. struct CONTEXT_T *ctx = (struct CONTEXT_T *)port->userdata;
  87. /* Queue the decoded video frame */
  88. mmal_queue_put(ctx->queue, buffer);
  89. /* Kick the processing thread */
  90. vcos_semaphore_post(&ctx->semaphore);
  91. }
  92. int main(int argc, char **argv)
  93. {
  94. MMAL_STATUS_T status = MMAL_EINVAL;
  95. MMAL_COMPONENT_T *decoder = 0;
  96. MMAL_POOL_T *pool_in = 0, *pool_out = 0;
  97. MMAL_BOOL_T eos_sent = MMAL_FALSE, eos_received = MMAL_FALSE;
  98. unsigned int count;
  99. if (argc < 2)
  100. {
  101. fprintf(stderr, "invalid arguments\n");
  102. return -1;
  103. }
  104. vcos_semaphore_create(&context.semaphore, "example", 1);
  105. SOURCE_OPEN(argv[1]);
  106. /* Create the decoder component.
  107. * This specific component exposes 2 ports (1 input and 1 output). Like most components
  108. * its expects the format of its input port to be set by the client in order for it to
  109. * know what kind of data it will be fed. */
  110. status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_DECODER, &decoder);
  111. CHECK_STATUS(status, "failed to create decoder");
  112. /* Enable control port so we can receive events from the component */
  113. decoder->control->userdata = (void *)&context;
  114. status = mmal_port_enable(decoder->control, control_callback);
  115. CHECK_STATUS(status, "failed to enable control port");
  116. /* Get statistics on the input port */
  117. MMAL_PARAMETER_CORE_STATISTICS_T stats = {{0}};
  118. stats.hdr.id = MMAL_PARAMETER_CORE_STATISTICS;
  119. stats.hdr.size = sizeof(MMAL_PARAMETER_CORE_STATISTICS_T);
  120. status = mmal_port_parameter_get(decoder->input[0], &stats.hdr);
  121. CHECK_STATUS(status, "failed to get stats");
  122. fprintf(stderr, "stats: %i, %i", stats.stats.buffer_count, stats.stats.max_delay);
  123. /* Set the zero-copy parameter on the input port */
  124. MMAL_PARAMETER_BOOLEAN_T zc = {{MMAL_PARAMETER_ZERO_COPY, sizeof(zc)}, MMAL_TRUE};
  125. status = mmal_port_parameter_set(decoder->input[0], &zc.hdr);
  126. fprintf(stderr, "status: %i\n", status);
  127. /* Set the zero-copy parameter on the output port */
  128. status = mmal_port_parameter_set_boolean(decoder->output[0], MMAL_PARAMETER_ZERO_COPY, MMAL_TRUE);
  129. fprintf(stderr, "status: %i\n", status);
  130. /* Set format of video decoder input port */
  131. MMAL_ES_FORMAT_T *format_in = decoder->input[0]->format;
  132. format_in->type = MMAL_ES_TYPE_VIDEO;
  133. format_in->encoding = MMAL_ENCODING_H264;
  134. format_in->es->video.width = 1280;
  135. format_in->es->video.height = 720;
  136. format_in->es->video.frame_rate.num = 30;
  137. format_in->es->video.frame_rate.den = 1;
  138. format_in->es->video.par.num = 1;
  139. format_in->es->video.par.den = 1;
  140. /* If the data is known to be framed then the following flag should be set:
  141. * format_in->flags |= MMAL_ES_FORMAT_FLAG_FRAMED; */
  142. SOURCE_READ_CODEC_CONFIG_DATA(codec_header_bytes, codec_header_bytes_size);
  143. status = mmal_format_extradata_alloc(format_in, codec_header_bytes_size);
  144. CHECK_STATUS(status, "failed to allocate extradata");
  145. format_in->extradata_size = codec_header_bytes_size;
  146. if (format_in->extradata_size)
  147. memcpy(format_in->extradata, codec_header_bytes, format_in->extradata_size);
  148. status = mmal_port_format_commit(decoder->input[0]);
  149. CHECK_STATUS(status, "failed to commit format");
  150. /* Our decoder can do internal colour conversion, ask for a conversion to RGB565 */
  151. MMAL_ES_FORMAT_T *format_out = decoder->output[0]->format;
  152. format_out->encoding = MMAL_ENCODING_RGB16;
  153. status = mmal_port_format_commit(decoder->output[0]);
  154. CHECK_STATUS(status, "failed to commit format");
  155. /* Display the output port format */
  156. fprintf(stderr, "%s\n", decoder->output[0]->name);
  157. fprintf(stderr, " type: %i, fourcc: %4.4s\n", format_out->type, (char *)&format_out->encoding);
  158. fprintf(stderr, " bitrate: %i, framed: %i\n", format_out->bitrate,
  159. !!(format_out->flags & MMAL_ES_FORMAT_FLAG_FRAMED));
  160. fprintf(stderr, " extra data: %i, %p\n", format_out->extradata_size, format_out->extradata);
  161. fprintf(stderr, " width: %i, height: %i, (%i,%i,%i,%i)\n",
  162. format_out->es->video.width, format_out->es->video.height,
  163. format_out->es->video.crop.x, format_out->es->video.crop.y,
  164. format_out->es->video.crop.width, format_out->es->video.crop.height);
  165. /* The format of both ports is now set so we can get their buffer requirements and create
  166. * our buffer headers. We use the buffer pool API to create these. */
  167. decoder->input[0]->buffer_num = decoder->input[0]->buffer_num_min;
  168. decoder->input[0]->buffer_size = decoder->input[0]->buffer_size_min;
  169. decoder->output[0]->buffer_num = decoder->output[0]->buffer_num_min;
  170. decoder->output[0]->buffer_size = decoder->output[0]->buffer_size_min;
  171. pool_in = mmal_pool_create(decoder->input[0]->buffer_num,
  172. decoder->input[0]->buffer_size);
  173. pool_out = mmal_pool_create(decoder->output[0]->buffer_num,
  174. decoder->output[0]->buffer_size);
  175. /* Create a queue to store our decoded video frames. The callback we will get when
  176. * a frame has been decoded will put the frame into this queue. */
  177. context.queue = mmal_queue_create();
  178. /* Store a reference to our context in each port (will be used during callbacks) */
  179. decoder->input[0]->userdata = (void *)&context;
  180. decoder->output[0]->userdata = (void *)&context;
  181. /* Enable all the input port and the output port.
  182. * The callback specified here is the function which will be called when the buffer header
  183. * we sent to the component has been processed. */
  184. status = mmal_port_enable(decoder->input[0], input_callback);
  185. CHECK_STATUS(status, "failed to enable input port");
  186. status = mmal_port_enable(decoder->output[0], output_callback);
  187. CHECK_STATUS(status, "failed to enable output port");
  188. /* Component won't start processing data until it is enabled. */
  189. status = mmal_component_enable(decoder);
  190. CHECK_STATUS(status, "failed to enable component");
  191. /* Start decoding */
  192. fprintf(stderr, "start decoding\n");
  193. /* This is the main processing loop */
  194. for (count = 0; !eos_received && count < 500; count++)
  195. {
  196. MMAL_BUFFER_HEADER_T *buffer;
  197. /* Wait for buffer headers to be available on either of the decoder ports */
  198. vcos_semaphore_wait(&context.semaphore);
  199. /* Check for errors */
  200. if (context.status != MMAL_SUCCESS)
  201. break;
  202. /* Send data to decode to the input port of the video decoder */
  203. if (!eos_sent && (buffer = mmal_queue_get(pool_in->queue)) != NULL)
  204. {
  205. SOURCE_READ_DATA_INTO_BUFFER(buffer);
  206. if(!buffer->length) eos_sent = MMAL_TRUE;
  207. buffer->flags = buffer->length ? 0 : MMAL_BUFFER_HEADER_FLAG_EOS;
  208. buffer->pts = buffer->dts = MMAL_TIME_UNKNOWN;
  209. fprintf(stderr, "sending %i bytes\n", (int)buffer->length);
  210. status = mmal_port_send_buffer(decoder->input[0], buffer);
  211. CHECK_STATUS(status, "failed to send buffer");
  212. }
  213. /* Get our decoded frames */
  214. while ((buffer = mmal_queue_get(context.queue)) != NULL)
  215. {
  216. /* We have a frame, do something with it (why not display it for instance?).
  217. * Once we're done with it, we release it. It will automatically go back
  218. * to its original pool so it can be reused for a new video frame.
  219. */
  220. eos_received = buffer->flags & MMAL_BUFFER_HEADER_FLAG_EOS;
  221. if (buffer->cmd)
  222. fprintf(stderr, "received event %4.4s", (char *)&buffer->cmd);
  223. else
  224. fprintf(stderr, "decoded frame (flags %x)\n", buffer->flags);
  225. mmal_buffer_header_release(buffer);
  226. }
  227. /* Send empty buffers to the output port of the decoder */
  228. while ((buffer = mmal_queue_get(pool_out->queue)) != NULL)
  229. {
  230. status = mmal_port_send_buffer(decoder->output[0], buffer);
  231. CHECK_STATUS(status, "failed to send buffer");
  232. }
  233. }
  234. /* Stop decoding */
  235. fprintf(stderr, "stop decoding\n");
  236. /* Stop everything. Not strictly necessary since mmal_component_destroy()
  237. * will do that anyway */
  238. mmal_port_disable(decoder->input[0]);
  239. mmal_port_disable(decoder->output[0]);
  240. mmal_component_disable(decoder);
  241. error:
  242. /* Cleanup everything */
  243. if (decoder)
  244. mmal_component_destroy(decoder);
  245. if (pool_in)
  246. mmal_pool_destroy(pool_in);
  247. if (pool_out)
  248. mmal_pool_destroy(pool_out);
  249. if (context.queue)
  250. mmal_queue_destroy(context.queue);
  251. SOURCE_CLOSE();
  252. vcos_semaphore_delete(&context.semaphore);
  253. return status == MMAL_SUCCESS ? 0 : -1;
  254. }