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.

spdif.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 SPDIF_AC3_FRAME_SIZE 6144
  30. #define SPDIF_EAC3_FRAME_SIZE (6144*4)
  31. #define SPDIF_FRAME_SIZE SPDIF_EAC3_FRAME_SIZE
  32. /* Buffering requirements */
  33. #define INPUT_MIN_BUFFER_SIZE SPDIF_FRAME_SIZE
  34. #define INPUT_MIN_BUFFER_NUM 2
  35. #define OUTPUT_MIN_BUFFER_SIZE SPDIF_FRAME_SIZE
  36. #define OUTPUT_MIN_BUFFER_NUM 2
  37. /*****************************************************************************/
  38. typedef struct MMAL_COMPONENT_MODULE_T
  39. {
  40. MMAL_STATUS_T status; /**< current status of the component */
  41. } MMAL_COMPONENT_MODULE_T;
  42. typedef struct MMAL_PORT_MODULE_T
  43. {
  44. MMAL_QUEUE_T *queue; /**< queue for the buffers sent to the ports */
  45. MMAL_BOOL_T needs_configuring; /**< port is waiting for a format commit */
  46. } MMAL_PORT_MODULE_T;
  47. /*****************************************************************************/
  48. /*****************************************************************************/
  49. static MMAL_STATUS_T spdif_send_event_format_changed(MMAL_COMPONENT_T *component, MMAL_PORT_T *port,
  50. MMAL_ES_FORMAT_T *format)
  51. {
  52. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  53. MMAL_BUFFER_HEADER_T *buffer = NULL;
  54. MMAL_EVENT_FORMAT_CHANGED_T *event;
  55. /* Get an event buffer */
  56. module->status = mmal_port_event_get(port, &buffer, MMAL_EVENT_FORMAT_CHANGED);
  57. if (module->status != MMAL_SUCCESS)
  58. {
  59. LOG_ERROR("unable to get an event buffer");
  60. return module->status;
  61. }
  62. /* coverity[returned_null] Can't return null or call above would have failed */
  63. event = mmal_event_format_changed_get(buffer);
  64. /* Fill in the new format */
  65. if (port->format->encoding == MMAL_ENCODING_PCM_SIGNED)
  66. mmal_format_copy(event->format, port->format);
  67. else
  68. mmal_format_copy(event->format, format);
  69. event->format->es->audio.sample_rate = format->es->audio.sample_rate;
  70. /* Pass on the buffer requirements */
  71. event->buffer_num_min = port->buffer_num_min;
  72. event->buffer_size_min = port->buffer_size_min;
  73. event->buffer_size_recommended = event->buffer_size_min;
  74. event->buffer_num_recommended = port->buffer_num_recommended;
  75. port->priv->module->needs_configuring = 1;
  76. mmal_port_event_send(port, buffer);
  77. return MMAL_SUCCESS;
  78. }
  79. /** Actual processing function */
  80. static MMAL_BOOL_T spdif_do_processing(MMAL_COMPONENT_T *component)
  81. {
  82. static const uint8_t ac3_spdif_header[6] = {0x72,0xF8,0x1F,0x4E,0x1, 0};
  83. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  84. MMAL_PORT_T *port_in = component->input[0];
  85. MMAL_PORT_T *port_out = component->output[0];
  86. MMAL_BUFFER_HEADER_T *in, *out;
  87. unsigned int i, sample_rate, frame_size, spdif_frame_size;
  88. uint8_t *in_data;
  89. if (port_out->priv->module->needs_configuring)
  90. return 0;
  91. in = mmal_queue_get(port_in->priv->module->queue);
  92. if (!in)
  93. return 0;
  94. /* Handle event buffers */
  95. if (in->cmd)
  96. {
  97. MMAL_EVENT_FORMAT_CHANGED_T *event = mmal_event_format_changed_get(in);
  98. if (event)
  99. {
  100. module->status = mmal_format_full_copy(port_in->format, event->format);
  101. if (module->status == MMAL_SUCCESS)
  102. module->status = port_in->priv->pf_set_format(port_in);
  103. if (module->status != MMAL_SUCCESS)
  104. {
  105. LOG_ERROR("format not set on port %s %p (%i)", port_in->name, port_in, module->status);
  106. if (mmal_event_error_send(component, module->status) != MMAL_SUCCESS)
  107. LOG_ERROR("unable to send an error event buffer");
  108. }
  109. }
  110. else
  111. {
  112. LOG_ERROR("discarding event %i on port %s %p", (int)in->cmd, port_in->name, port_in);
  113. }
  114. in->length = 0;
  115. mmal_port_buffer_header_callback(port_in, in);
  116. return 1;
  117. }
  118. /* Don't do anything if we've already seen an error */
  119. if (module->status != MMAL_SUCCESS)
  120. {
  121. mmal_queue_put_back(port_in->priv->module->queue, in);
  122. return 0;
  123. }
  124. /* Discard empty buffers */
  125. if (!in->length && !in->flags)
  126. {
  127. mmal_port_buffer_header_callback(port_in, in);
  128. return 1;
  129. }
  130. /* Discard codec config data as it's not needed */
  131. if (in->flags & MMAL_BUFFER_HEADER_FLAG_CONFIG)
  132. {
  133. LOG_DEBUG("config buffer %ibytes", in->length);
  134. in->length = 0;
  135. mmal_port_buffer_header_callback(port_in, in);
  136. return 1;
  137. }
  138. out = mmal_queue_get(port_out->priv->module->queue);
  139. if (!out)
  140. {
  141. mmal_queue_put_back(port_in->priv->module->queue, in);
  142. return 0;
  143. }
  144. spdif_frame_size = SPDIF_AC3_FRAME_SIZE;
  145. if (port_out->format->encoding == MMAL_ENCODING_EAC3)
  146. spdif_frame_size = SPDIF_EAC3_FRAME_SIZE;
  147. /* Sanity check the output buffer is big enough */
  148. if (out->alloc_size < spdif_frame_size)
  149. {
  150. module->status = MMAL_EINVAL;
  151. if (mmal_event_error_send(component, module->status) != MMAL_SUCCESS)
  152. LOG_ERROR("unable to send an error event buffer");
  153. mmal_queue_put_back(port_in->priv->module->queue, in);
  154. mmal_queue_put_back(port_out->priv->module->queue, out);
  155. return 0;
  156. }
  157. /* Special case for empty buffers carrying a flag */
  158. if (!in->length && in->flags)
  159. {
  160. out->length = 0;
  161. goto end;
  162. }
  163. LOG_DEBUG("frame: %lld, size %i", in->pts, in->length);
  164. mmal_buffer_header_mem_lock(out);
  165. mmal_buffer_header_mem_lock(in);
  166. in_data = in->data + in->offset;
  167. /* Sanity check we're dealing with an AC3 frame */
  168. if (in->length < 5)
  169. {
  170. LOG_ERROR("invalid data size (%i bytes)", in->length);
  171. goto discard;
  172. }
  173. if (!(in_data[0] == 0x0B || in_data[1] == 0x77) &&
  174. !(in_data[0] == 0x77 || in_data[1] == 0x0B))
  175. {
  176. LOG_ERROR("invalid data (%i bytes): %2.2x,%2.2x,%2.2x,%2.2x",
  177. in->length, in_data[0], in_data[1], in_data[2], in_data[3]);
  178. goto discard;
  179. }
  180. /* We need to make sure we use the right sample rate
  181. * to be able to play this at the right rate */
  182. if ((in_data[4] & 0xC0) == 0x40) sample_rate = 44100;
  183. else if ((in_data[4] & 0xC0) == 0x80) sample_rate = 32000;
  184. else sample_rate = 48000;
  185. /* If the sample rate changes, stop everything we're doing
  186. * and signal the format change. */
  187. if (sample_rate != port_out->format->es->audio.sample_rate)
  188. {
  189. LOG_INFO("format change: %i->%i",
  190. port_out->format->es->audio.sample_rate, sample_rate);
  191. port_in->format->es->audio.sample_rate = sample_rate;
  192. spdif_send_event_format_changed(component, port_out, port_in->format);
  193. mmal_buffer_header_mem_unlock(in);
  194. mmal_buffer_header_mem_unlock(out);
  195. mmal_queue_put_back(port_in->priv->module->queue, in);
  196. mmal_queue_put_back(port_out->priv->module->queue, out);
  197. return 0;
  198. }
  199. /* Build our S/PDIF frame. We assume that we need to send
  200. * little endian S/PDIF data. */
  201. if (in->length > spdif_frame_size - 8)
  202. LOG_ERROR("frame too big, truncating (%i/%i bytes)",
  203. in->length, spdif_frame_size - 8);
  204. frame_size = MMAL_MIN(in->length, spdif_frame_size - 8) / 2;
  205. memcpy(out->data, ac3_spdif_header, sizeof(ac3_spdif_header));
  206. out->data[5] = in_data[5] & 0x7; /* bsmod */
  207. out->data[6] = frame_size & 0xFF;
  208. out->data[7] = frame_size >> 8;
  209. /* Copy the AC3 data, reverting the endianness if required */
  210. if (in_data[0] == 0x0B)
  211. {
  212. for (i = 0; i < frame_size; i++)
  213. {
  214. out->data[8+i*2] = in_data[in->offset+i*2+1];
  215. out->data[8+i*2+1] = in_data[in->offset+i*2];
  216. }
  217. }
  218. else
  219. memcpy(out->data + 8, in_data, in->length);
  220. /* The S/PDIF frame needs to be padded */
  221. memset(out->data + 8 + frame_size * 2, 0,
  222. spdif_frame_size - frame_size * 2 - 8);
  223. mmal_buffer_header_mem_unlock(in);
  224. mmal_buffer_header_mem_unlock(out);
  225. out->length = spdif_frame_size;
  226. end:
  227. out->offset = 0;
  228. out->flags = in->flags;
  229. out->pts = in->pts;
  230. out->dts = in->dts;
  231. /* Send buffers back */
  232. in->length = 0;
  233. mmal_port_buffer_header_callback(port_in, in);
  234. mmal_port_buffer_header_callback(port_out, out);
  235. return 1;
  236. discard:
  237. mmal_buffer_header_mem_unlock(in);
  238. mmal_buffer_header_mem_unlock(out);
  239. in->length = 0;
  240. mmal_queue_put_back(port_out->priv->module->queue, out);
  241. mmal_port_buffer_header_callback(port_in, in);
  242. return 1;
  243. }
  244. /*****************************************************************************/
  245. static void spdif_do_processing_loop(MMAL_COMPONENT_T *component)
  246. {
  247. while (spdif_do_processing(component));
  248. }
  249. /** Destroy a previously created component */
  250. static MMAL_STATUS_T spdif_component_destroy(MMAL_COMPONENT_T *component)
  251. {
  252. unsigned int i;
  253. for(i = 0; i < component->input_num; i++)
  254. if(component->input[i]->priv->module->queue)
  255. mmal_queue_destroy(component->input[i]->priv->module->queue);
  256. if(component->input_num)
  257. mmal_ports_free(component->input, component->input_num);
  258. for(i = 0; i < component->output_num; i++)
  259. if(component->output[i]->priv->module->queue)
  260. mmal_queue_destroy(component->output[i]->priv->module->queue);
  261. if(component->output_num)
  262. mmal_ports_free(component->output, component->output_num);
  263. vcos_free(component->priv->module);
  264. return MMAL_SUCCESS;
  265. }
  266. /** Enable processing on a port */
  267. static MMAL_STATUS_T spdif_port_enable(MMAL_PORT_T *port, MMAL_PORT_BH_CB_T cb)
  268. {
  269. MMAL_PARAM_UNUSED(cb);
  270. /* We need to propagate the buffer requirements when the input port is
  271. * enabled */
  272. if (port->type == MMAL_PORT_TYPE_INPUT)
  273. return port->priv->pf_set_format(port);
  274. return MMAL_SUCCESS;
  275. }
  276. /** Flush a port */
  277. static MMAL_STATUS_T spdif_port_flush(MMAL_PORT_T *port)
  278. {
  279. MMAL_PORT_MODULE_T *port_module = port->priv->module;
  280. MMAL_BUFFER_HEADER_T *buffer;
  281. /* Flush buffers that our component is holding on to */
  282. buffer = mmal_queue_get(port_module->queue);
  283. while(buffer)
  284. {
  285. mmal_port_buffer_header_callback(port, buffer);
  286. buffer = mmal_queue_get(port_module->queue);
  287. }
  288. return MMAL_SUCCESS;
  289. }
  290. /** Disable processing on a port */
  291. static MMAL_STATUS_T spdif_port_disable(MMAL_PORT_T *port)
  292. {
  293. /* We just need to flush our internal queue */
  294. return spdif_port_flush(port);
  295. }
  296. /** Send a buffer header to a port */
  297. static MMAL_STATUS_T spdif_port_send(MMAL_PORT_T *port, MMAL_BUFFER_HEADER_T *buffer)
  298. {
  299. mmal_queue_put(port->priv->module->queue, buffer);
  300. mmal_component_action_trigger(port->component);
  301. return MMAL_SUCCESS;
  302. }
  303. /** Set format on input port */
  304. static MMAL_STATUS_T spdif_input_port_format_commit(MMAL_PORT_T *in)
  305. {
  306. MMAL_COMPONENT_T *component = in->component;
  307. MMAL_PORT_T *out = component->output[0];
  308. /* Sanity check we cope with this format */
  309. if (in->format->encoding != MMAL_ENCODING_AC3 &&
  310. in->format->encoding != MMAL_ENCODING_EAC3)
  311. return MMAL_ENXIO;
  312. LOG_INFO("%4.4s, %iHz, %ichan, %ibps", (char *)&in->format->encoding,
  313. in->format->es->audio.sample_rate, in->format->es->audio.channels,
  314. in->format->bitrate);
  315. /* TODO: should we check the bitrate to see if that fits in an S/PDIF
  316. * frame? */
  317. /* Check if there's anything to propagate to the output port */
  318. if (!mmal_format_compare(in->format, out->format))
  319. return MMAL_SUCCESS;
  320. if (out->format->encoding == MMAL_ENCODING_PCM_SIGNED &&
  321. in->format->es->audio.sample_rate ==
  322. out->format->es->audio.sample_rate)
  323. return MMAL_SUCCESS;
  324. /* If the output port is not enabled we just need to update its format.
  325. * Otherwise we'll have to trigger a format changed event for it. */
  326. if (!out->is_enabled)
  327. {
  328. if (out->format->encoding != MMAL_ENCODING_PCM_SIGNED)
  329. mmal_format_copy(out->format, in->format);
  330. out->format->es->audio.sample_rate = in->format->es->audio.sample_rate;
  331. return MMAL_SUCCESS;
  332. }
  333. /* Send an event on the output port */
  334. return spdif_send_event_format_changed(component, out, in->format);
  335. }
  336. /** Set format on output port */
  337. static MMAL_STATUS_T spdif_output_port_format_commit(MMAL_PORT_T *out)
  338. {
  339. int supported = 0;
  340. if (out->format->type == MMAL_ES_TYPE_AUDIO &&
  341. out->format->encoding == MMAL_ENCODING_PCM_SIGNED &&
  342. out->format->es->audio.channels == 2 &&
  343. out->format->es->audio.bits_per_sample == 16)
  344. supported = 1;
  345. if (out->format->type == MMAL_ES_TYPE_AUDIO &&
  346. (out->format->encoding == MMAL_ENCODING_AC3 ||
  347. out->format->encoding == MMAL_ENCODING_EAC3))
  348. supported = 1;
  349. if (!supported)
  350. {
  351. LOG_ERROR("invalid format %4.4s, %ichan, %ibps",
  352. (char *)&out->format->encoding, out->format->es->audio.channels,
  353. out->format->es->audio.bits_per_sample);
  354. return MMAL_EINVAL;
  355. }
  356. out->priv->module->needs_configuring = 0;
  357. mmal_component_action_trigger(out->component);
  358. return MMAL_SUCCESS;
  359. }
  360. /** Create an instance of a component */
  361. static MMAL_STATUS_T mmal_component_create_spdif(const char *name, MMAL_COMPONENT_T *component)
  362. {
  363. MMAL_COMPONENT_MODULE_T *module;
  364. MMAL_STATUS_T status = MMAL_ENOMEM;
  365. MMAL_PARAM_UNUSED(name);
  366. /* Allocate the context for our module */
  367. component->priv->module = module = vcos_malloc(sizeof(*module), "mmal module");
  368. if (!module)
  369. return MMAL_ENOMEM;
  370. memset(module, 0, sizeof(*module));
  371. component->priv->pf_destroy = spdif_component_destroy;
  372. /* Allocate and initialise all the ports for this component */
  373. component->input = mmal_ports_alloc(component, 1, MMAL_PORT_TYPE_INPUT, sizeof(MMAL_PORT_MODULE_T));
  374. if(!component->input)
  375. goto error;
  376. component->input_num = 1;
  377. component->input[0]->priv->pf_enable = spdif_port_enable;
  378. component->input[0]->priv->pf_disable = spdif_port_disable;
  379. component->input[0]->priv->pf_flush = spdif_port_flush;
  380. component->input[0]->priv->pf_send = spdif_port_send;
  381. component->input[0]->priv->pf_set_format = spdif_input_port_format_commit;
  382. component->input[0]->priv->module->queue = mmal_queue_create();
  383. if(!component->input[0]->priv->module->queue)
  384. goto error;
  385. component->output = mmal_ports_alloc(component, 1, MMAL_PORT_TYPE_OUTPUT, sizeof(MMAL_PORT_MODULE_T));
  386. if(!component->output)
  387. goto error;
  388. component->output_num = 1;
  389. component->output[0]->priv->pf_enable = spdif_port_enable;
  390. component->output[0]->priv->pf_disable = spdif_port_disable;
  391. component->output[0]->priv->pf_flush = spdif_port_flush;
  392. component->output[0]->priv->pf_send = spdif_port_send;
  393. component->output[0]->priv->pf_set_format = spdif_output_port_format_commit;
  394. component->output[0]->priv->module->queue = mmal_queue_create();
  395. if(!component->output[0]->priv->module->queue)
  396. goto error;
  397. status = mmal_component_action_register(component, spdif_do_processing_loop);
  398. if (status != MMAL_SUCCESS)
  399. goto error;
  400. component->input[0]->format->type = MMAL_ES_TYPE_AUDIO;
  401. component->input[0]->format->encoding = MMAL_ENCODING_AC3;
  402. component->input[0]->buffer_size_min =
  403. component->input[0]->buffer_size_recommended = INPUT_MIN_BUFFER_SIZE;
  404. component->input[0]->buffer_num_min =
  405. component->input[0]->buffer_num_recommended = INPUT_MIN_BUFFER_NUM;
  406. component->output[0]->format->type = MMAL_ES_TYPE_AUDIO;
  407. component->output[0]->format->encoding = MMAL_ENCODING_AC3;
  408. component->output[0]->format->es->audio.channels = 2;
  409. component->output[0]->format->es->audio.bits_per_sample = 16;
  410. component->output[0]->buffer_size_min =
  411. component->output[0]->buffer_size_recommended = OUTPUT_MIN_BUFFER_SIZE;
  412. component->output[0]->buffer_num_min =
  413. component->output[0]->buffer_num_recommended = OUTPUT_MIN_BUFFER_NUM;
  414. return MMAL_SUCCESS;
  415. error:
  416. spdif_component_destroy(component);
  417. return status;
  418. }
  419. MMAL_CONSTRUCTOR(mmal_register_component_spdif);
  420. void mmal_register_component_spdif(void)
  421. {
  422. mmal_component_supplier_register("spdif", mmal_component_create_spdif);
  423. }