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.

clock.c 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 "util/mmal_util_rational.h"
  29. #include "util/mmal_list.h"
  30. #include "mmal_logging.h"
  31. #define CLOCK_PORTS_NUM 5
  32. #define MAX_CLOCK_EVENT_SLOTS 16
  33. #define DEFAULT_FRAME_RATE 30 /* frames per second */
  34. #define DEFAULT_CLOCK_LATENCY 60000 /* microseconds */
  35. #define FILTER_DURATION 2 /* seconds */
  36. #define MAX_FILTER_LENGTH 180 /* samples */
  37. #define MAX_TIME (~(1LL << 63)) /* microseconds */
  38. #define MIN_TIME (1LL << 63) /* microseconds */
  39. #define ABS(a) ((a) < 0 ? -(a) : (a))
  40. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  41. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  42. /** Set to 1 to enable additional stream timing log
  43. * messages used for debugging the clock algorithm */
  44. #define ENABLE_ADDITIONAL_LOGGING 0
  45. static int clock_additional_logging = ENABLE_ADDITIONAL_LOGGING;
  46. /*****************************************************************************/
  47. typedef int64_t TIME_T;
  48. typedef struct FILTER_T
  49. {
  50. uint32_t first; /**< index to the oldest sample */
  51. uint32_t last; /**< index to the most recent sample*/
  52. uint32_t count; /**< total number of samples in the filter */
  53. uint32_t length; /**< maximum number of samples */
  54. TIME_T sum; /**< sum of all samples currently in the filter */
  55. TIME_T h[MAX_FILTER_LENGTH]; /**< filter history */
  56. } FILTER_T;
  57. /** Frame statistics for a stream */
  58. typedef struct CLOCK_STREAM_T
  59. {
  60. uint32_t id; /**< for debug purposes */
  61. MMAL_BOOL_T started; /**< TRUE at least one frame has been received */
  62. TIME_T pts; /**< most recent time-stamp seen */
  63. TIME_T stc; /**< most recent wall-time seen */
  64. TIME_T mt_off; /**< offset of the current time stamp from the
  65. arrival time, i.e. PTS - STC */
  66. TIME_T mt_off_avg; /**< rolling average of the media time offset */
  67. TIME_T mt_off_std; /**< approximate standard deviation of the media
  68. time offset */
  69. FILTER_T avg_filter; /**< moving average filter */
  70. FILTER_T std_filter; /**< (approximate) standard deviation filter */
  71. } CLOCK_STREAM_T;
  72. /** Clock stream events */
  73. typedef enum CLOCK_STREAM_EVENT_T
  74. {
  75. CLOCK_STREAM_EVENT_NONE,
  76. CLOCK_STREAM_EVENT_STARTED, /**< first data received */
  77. CLOCK_STREAM_EVENT_DISCONT, /**< discontinuity detected */
  78. CLOCK_STREAM_EVENT_FRAME_COMPLETE, /**< complete frame received */
  79. } CLOCK_STREAM_EVENT_T;
  80. /** Clock port event */
  81. typedef struct CLOCK_PORT_EVENT_T
  82. {
  83. MMAL_LIST_ELEMENT_T link; /**< must be first */
  84. MMAL_PORT_T *port; /**< clock port where the event occurred */
  85. MMAL_CLOCK_EVENT_T event; /**< event data */
  86. } CLOCK_PORT_EVENT_T;
  87. /** Clock component context */
  88. typedef struct MMAL_COMPONENT_MODULE_T
  89. {
  90. MMAL_STATUS_T status; /**< current status of the component */
  91. MMAL_BOOL_T clock_discont; /**< TRUE -> clock discontinuity detected */
  92. uint32_t stream_min_id; /**< id of selected minimum stream (debugging only) */
  93. uint32_t stream_max_id; /**< if of selected maximum stream (debugging only) */
  94. TIME_T mt_off_target; /**< target clock media time offset */
  95. TIME_T mt_off_clk; /**< current clock media time offset */
  96. TIME_T adj_p; /**< proportional clock adjustment */
  97. TIME_T adj_m; /**< clock adjustment factor (between 1 and 0) */
  98. TIME_T adj; /**< final clock adjustment */
  99. TIME_T stc_at_update; /**< the value of the STC the last time the clocks
  100. were updated */
  101. TIME_T frame_duration; /**< one frame period (microseconds) */
  102. MMAL_RATIONAL_T frame_rate; /**< frame rate set by the client */
  103. uint32_t frame_rate_log2; /**< frame rate expressed as a power of two */
  104. MMAL_RATIONAL_T scale; /**< current clock scale factor */
  105. MMAL_BOOL_T pending_scale; /**< TRUE -> scale change is pending */
  106. MMAL_CLOCK_LATENCY_T latency;
  107. MMAL_CLOCK_UPDATE_THRESHOLD_T update_threshold;
  108. MMAL_CLOCK_DISCONT_THRESHOLD_T discont_threshold;
  109. MMAL_CLOCK_REQUEST_THRESHOLD_T request_threshold;
  110. /** Clock port events */
  111. struct
  112. {
  113. MMAL_LIST_T* queue; /**< pending events */
  114. MMAL_LIST_T* free; /**< available event slots */
  115. CLOCK_PORT_EVENT_T pool[MAX_CLOCK_EVENT_SLOTS];
  116. } events;
  117. } MMAL_COMPONENT_MODULE_T;
  118. /** Clock port context */
  119. typedef struct MMAL_PORT_MODULE_T
  120. {
  121. CLOCK_STREAM_T *stream; /**< stream associated with this clock port */
  122. } MMAL_PORT_MODULE_T;
  123. /*****************************************************************************/
  124. /** Round x up to the next power of two */
  125. static uint32_t next_pow2(uint32_t x)
  126. {
  127. x--;
  128. x = (x >> 1) | x;
  129. x = (x >> 2) | x;
  130. x = (x >> 4) | x;
  131. x = (x >> 8) | x;
  132. x = (x >> 16) | x;
  133. return ++x;
  134. }
  135. /** Given a power of 2 value, return the number of bit shifts */
  136. static uint32_t pow2_shift(uint32_t x)
  137. {
  138. static const uint32_t BIT_POSITIONS[32] =
  139. {
  140. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  141. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  142. };
  143. return BIT_POSITIONS[((x & -x) * 0x077CB531U) >> 27];
  144. }
  145. /** Add 2 values with saturation */
  146. static inline TIME_T saturate_add(TIME_T a, TIME_T b)
  147. {
  148. TIME_T sum = a + b;
  149. if (a > 0 && b > 0 && sum < 0)
  150. sum = MAX_TIME;
  151. else if (a < 0 && b < 0 && sum > 0)
  152. sum = MIN_TIME;
  153. return sum;
  154. }
  155. /*****************************************************************************/
  156. /** Filter reset */
  157. static void filter_init(FILTER_T *filter, uint32_t length)
  158. {
  159. memset(filter, 0, sizeof(*filter));
  160. filter->last = length - 1;
  161. filter->length = length;
  162. };
  163. /** Increment filter index modulo the length */
  164. static inline uint32_t filter_index_wrap(uint32_t index, uint32_t length)
  165. {
  166. return (++index < length) ? index : 0;
  167. }
  168. /** Remove the oldest sample from the filter */
  169. static void filter_drop(FILTER_T *filter)
  170. {
  171. if (!filter->count)
  172. return;
  173. filter->sum -= filter->h[filter->first];
  174. filter->first = filter_index_wrap(filter->first, filter->length);
  175. filter->count--;
  176. }
  177. /** Add a new sample (and drop the oldest when full) */
  178. static void filter_insert(FILTER_T *filter, TIME_T sample)
  179. {
  180. if (filter->count == filter->length)
  181. filter_drop(filter);
  182. filter->last = filter_index_wrap(filter->last, filter->length);
  183. filter->h[filter->last] = sample;
  184. filter->sum = saturate_add(filter->sum, sample);
  185. filter->count++;
  186. }
  187. /*****************************************************************************/
  188. /** Create and initialise a clock stream */
  189. static MMAL_BOOL_T clock_create_stream(CLOCK_STREAM_T **stream, uint32_t id, uint32_t filter_length)
  190. {
  191. CLOCK_STREAM_T *s = vcos_calloc(1, sizeof(CLOCK_STREAM_T), "clock stream");
  192. if (!s)
  193. {
  194. LOG_ERROR("failed to allocate stream");
  195. return MMAL_FALSE;
  196. }
  197. s->id = id;
  198. filter_init(&s->avg_filter, filter_length);
  199. filter_init(&s->std_filter, filter_length);
  200. *stream = s;
  201. return MMAL_TRUE;
  202. }
  203. /** Flag this stream as started */
  204. static void clock_start_stream(CLOCK_STREAM_T *stream, TIME_T stc, TIME_T pts)
  205. {
  206. stream->started = MMAL_TRUE;
  207. stream->pts = pts;
  208. stream->stc = stc;
  209. }
  210. /** Reset the internal state of a stream */
  211. static void clock_reset_stream(CLOCK_STREAM_T *stream)
  212. {
  213. if (!stream)
  214. return;
  215. stream->pts = 0;
  216. stream->stc = 0;
  217. stream->mt_off = 0;
  218. stream->mt_off_avg = 0;
  219. stream->mt_off_std = 0;
  220. stream->started = MMAL_FALSE;
  221. filter_init(&stream->avg_filter, stream->avg_filter.length);
  222. filter_init(&stream->std_filter, stream->std_filter.length);
  223. }
  224. /** Update the internal state of a stream */
  225. static CLOCK_STREAM_EVENT_T clock_update_stream(CLOCK_STREAM_T *stream, TIME_T stc, TIME_T pts,
  226. TIME_T discont_threshold)
  227. {
  228. CLOCK_STREAM_EVENT_T event = CLOCK_STREAM_EVENT_NONE;
  229. TIME_T pts_delta, stc_delta;
  230. if (pts == MMAL_TIME_UNKNOWN)
  231. {
  232. LOG_TRACE("ignoring invalid timestamp received at %"PRIi64, stc);
  233. return CLOCK_STREAM_EVENT_NONE;
  234. }
  235. if (!stream->started)
  236. {
  237. LOG_TRACE("stream %d started %"PRIi64" %"PRIi64, stream->id, stc, pts);
  238. clock_start_stream(stream, stc, pts);
  239. return CLOCK_STREAM_EVENT_STARTED;
  240. }
  241. /* XXX: This should really use the buffer flags to determine if a complete
  242. * frame has been received. However, not all clients set MMAL buffer flags
  243. * correctly (if at all). */
  244. pts_delta = pts - stream->pts;
  245. stc_delta = stc - stream->stc;
  246. /* Check for discontinuities. */
  247. if ((ABS(pts_delta) > discont_threshold) || (ABS(stc_delta) > discont_threshold))
  248. {
  249. LOG_ERROR("discontinuity detected on stream %d %"PRIi64" %"PRIi64" %"PRIi64,
  250. stream->id, pts_delta, stc_delta, discont_threshold);
  251. return CLOCK_STREAM_EVENT_DISCONT;
  252. }
  253. if (pts_delta)
  254. {
  255. /* A complete frame has now been received, so update the stream's notion of media time */
  256. stream->mt_off = stream->pts - stream->stc;
  257. filter_insert(&stream->avg_filter, stream->mt_off);
  258. stream->mt_off_avg = stream->avg_filter.sum / stream->avg_filter.count;
  259. filter_insert(&stream->std_filter, ABS(stream->mt_off - stream->mt_off_avg));
  260. stream->mt_off_std = stream->std_filter.sum / stream->std_filter.count;
  261. LOG_TRACE("stream %d %"PRIi64" %"PRIi64" %"PRIi64" %"PRIi64,
  262. stream->id, stream->stc, stream->pts, stream->mt_off_avg, stream->mt_off);
  263. event = CLOCK_STREAM_EVENT_FRAME_COMPLETE;
  264. }
  265. stream->pts = pts;
  266. stream->stc = stc;
  267. return event;
  268. }
  269. /*****************************************************************************/
  270. /** Start all enabled clock ports, making sure all use the same thresholds */
  271. static void clock_start_clocks(MMAL_COMPONENT_T *component, TIME_T media_time)
  272. {
  273. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  274. unsigned i;
  275. for (i = 0; i < component->clock_num; ++i)
  276. {
  277. MMAL_PORT_T *port = component->clock[i];
  278. if (port->is_enabled)
  279. {
  280. LOG_TRACE("starting clock %d with time %"PRIi64, port->index, media_time);
  281. mmal_port_clock_reference_set(port, MMAL_TRUE);
  282. mmal_port_clock_media_time_set(port, media_time);
  283. mmal_port_clock_update_threshold_set(port, &module->update_threshold);
  284. mmal_port_clock_discont_threshold_set(port, &module->discont_threshold);
  285. mmal_port_clock_request_threshold_set(port, &module->request_threshold);
  286. mmal_port_clock_active_set(port, MMAL_TRUE);
  287. }
  288. }
  289. }
  290. /** Stop (and flush) all enabled clock ports */
  291. static void clock_stop_clocks(MMAL_COMPONENT_T *component)
  292. {
  293. unsigned i;
  294. for (i = 0; i < component->clock_num; ++i)
  295. {
  296. MMAL_PORT_T *port = component->clock[i];
  297. if (port->is_enabled)
  298. {
  299. LOG_TRACE("stopping clock %d", port->index);
  300. mmal_port_clock_request_flush(port);
  301. mmal_port_clock_active_set(port, MMAL_FALSE);
  302. }
  303. }
  304. }
  305. /** Reset the internal state of all streams in order to rebase clock
  306. * adjustment calculations */
  307. static void clock_reset_clocks(MMAL_COMPONENT_T *component)
  308. {
  309. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  310. unsigned i;
  311. for (i = 0; i < component->clock_num; ++i)
  312. clock_reset_stream(component->clock[i]->priv->module->stream);
  313. module->clock_discont = MMAL_TRUE;
  314. }
  315. /** Change the media-time for all enabled clock ports */
  316. static void clock_set_media_time(MMAL_COMPONENT_T *component, TIME_T media_time)
  317. {
  318. unsigned i;
  319. for (i = 0; i < component->clock_num; ++i)
  320. {
  321. MMAL_PORT_T *port = component->clock[i];
  322. if (port->is_enabled)
  323. mmal_port_clock_media_time_set(port, media_time);
  324. }
  325. }
  326. /** Change the scale for all clock ports */
  327. static void clock_set_scale(MMAL_COMPONENT_T *component, MMAL_RATIONAL_T scale)
  328. {
  329. unsigned i;
  330. for (i = 0; i < component->clock_num; ++i)
  331. mmal_port_clock_scale_set(component->clock[i], scale);
  332. component->priv->module->pending_scale = 0;
  333. }
  334. /** Update the average and standard deviation calculations for all streams
  335. * (dropping samples where necessary) and return the minimum and maximum
  336. * streams */
  337. static MMAL_BOOL_T clock_get_mt_off_avg(MMAL_COMPONENT_T *component, TIME_T stc,
  338. CLOCK_STREAM_T **minimum, CLOCK_STREAM_T **maximum)
  339. {
  340. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  341. TIME_T drop_threshold = 6 * module->frame_duration;
  342. TIME_T reset_threshold = module->latency.target << 1;
  343. TIME_T avg_min = MAX_TIME;
  344. TIME_T avg_max = MIN_TIME;
  345. TIME_T avg_bias;
  346. TIME_T stc_delta;
  347. unsigned i;
  348. *minimum = 0;
  349. *maximum = 0;
  350. for (i = 0; i < component->clock_num; ++i)
  351. {
  352. CLOCK_STREAM_T *stream = component->clock[i]->priv->module->stream;
  353. if (stream)
  354. {
  355. stc_delta = stc - stream->stc;
  356. /* Drop samples from the moving average and standard deviation filters */
  357. if (stc_delta > reset_threshold)
  358. {
  359. filter_init(&stream->avg_filter, stream->avg_filter.length);
  360. filter_init(&stream->std_filter, stream->std_filter.length);
  361. LOG_TRACE("reset stream %d filters due to stc_delta %"PRIi64, stream->id, stc_delta);
  362. }
  363. else if (stc_delta > drop_threshold)
  364. {
  365. filter_drop(&stream->avg_filter);
  366. filter_drop(&stream->std_filter);
  367. LOG_TRACE("drop stream %d filter samples due to stc_delta %"PRIi64, stream->id, stc_delta);
  368. }
  369. /* No point in continuing if filters are empty */
  370. if (!stream->avg_filter.count)
  371. continue;
  372. /* Calculate new average and standard deviation for the stream */
  373. stream->mt_off_avg = stream->avg_filter.sum / stream->avg_filter.count;
  374. stream->mt_off_std = stream->std_filter.sum / stream->std_filter.count;
  375. /* Select the minimum and maximum average between all active streams */
  376. avg_bias = (stream->avg_filter.length - stream->avg_filter.count) * ABS(stream->mt_off_avg) / stream->avg_filter.length;
  377. if ((stream->mt_off_avg + avg_bias) < avg_min)
  378. {
  379. avg_min = stream->mt_off_avg;
  380. *minimum = stream;
  381. LOG_TRACE("found min on %d mt_off_avg %"PRIi64" mt_off_std %"PRIi64" avg_bias %"PRIi64" count %d",
  382. stream->id, stream->mt_off_avg, stream->mt_off_std, avg_bias, stream->avg_filter.count);
  383. }
  384. if ((stream->mt_off_avg - avg_bias) > avg_max)
  385. {
  386. avg_max = stream->mt_off_avg;
  387. *maximum = stream;
  388. LOG_TRACE("found max on %d mt_off_avg %"PRIi64" mt_off_std %"PRIi64" avg_bias %"PRIi64" count %d",
  389. stream->id, stream->mt_off_avg, stream->mt_off_std, avg_bias, stream->avg_filter.count);
  390. }
  391. }
  392. }
  393. return (*minimum) && (*maximum);
  394. }
  395. /** Adjust the media-time of the playback clocks based on current timing statistics */
  396. static void clock_adjust_clocks(MMAL_COMPONENT_T *component, TIME_T stc)
  397. {
  398. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  399. CLOCK_STREAM_T *stream_min;
  400. CLOCK_STREAM_T *stream_max;
  401. TIME_T mt_off_clk;
  402. TIME_T stc_prev;
  403. if (!clock_get_mt_off_avg(component, stc, &stream_min, &stream_max))
  404. return;
  405. module->stream_min_id = stream_min->id;
  406. module->stream_max_id = stream_max->id;
  407. /* Calculate the actual media-time offset seen by the clock */
  408. mt_off_clk = mmal_port_clock_media_time_get(component->clock[0]) - stc;
  409. stc_prev = module->stc_at_update;
  410. module->stc_at_update = stc;
  411. /* If there has been a discontinuity, restart the clock,
  412. * else use the clock control loop to apply a clock adjustment */
  413. if (module->clock_discont)
  414. {
  415. module->clock_discont = MMAL_FALSE;
  416. module->mt_off_clk = stream_min->mt_off_avg - module->latency.target;
  417. module->mt_off_target = module->mt_off_clk;
  418. clock_stop_clocks(component);
  419. clock_start_clocks(component, module->mt_off_clk + stc);
  420. }
  421. else
  422. {
  423. /* Determine the new clock target */
  424. TIME_T mt_off_target_max = stream_max->mt_off_avg - module->latency.target;
  425. TIME_T mt_off_target_min = stream_min->mt_off_avg - module->frame_duration;
  426. module->mt_off_target = MIN(mt_off_target_max, mt_off_target_min);
  427. /* Calculate the proportional adjustment, capped by the attack rate
  428. * set by the client */
  429. TIME_T stc_delta = (stc > stc_prev) ? (stc - stc_prev) : 0;
  430. TIME_T adj_p_max = stc_delta * module->latency.attack_rate / module->latency.attack_period;
  431. module->adj_p = module->mt_off_target - module->mt_off_clk;
  432. if (module->adj_p < -adj_p_max)
  433. module->adj_p = -adj_p_max;
  434. else if (module->adj_p > adj_p_max)
  435. module->adj_p = adj_p_max;
  436. /* Calculate the confidence of the adjustment using the approximate
  437. * standard deviation for the selected stream:
  438. *
  439. * adj_m = 1.0 - STD * FPS / 4
  440. *
  441. * The adjustment factor is scaled up by 2^20 which is an approximation
  442. * of 1000000 (microseconds per second) and the frame rate is assumed
  443. * to be either 32 or 64 which are approximations for 24/25/30 and 60
  444. * fps to avoid divisions. This has a lower limit of 0. */
  445. module->adj_m =
  446. MAX((1 << 20) - ((stream_min->mt_off_std << module->frame_rate_log2) >> 2), 0);
  447. /* Modulate the proportional adjustment by the sample confidence
  448. * and apply the adjustment to the current clock */
  449. module->adj = (module->adj_p * module->adj_m) >> 20;
  450. module->adj = (module->adj * (stream_min->avg_filter.count << 8) / stream_min->avg_filter.length) >> 8;
  451. module->mt_off_clk += module->adj;
  452. clock_set_media_time(component, module->mt_off_clk + stc);
  453. }
  454. /* Any pending clock scale changes can now be applied */
  455. if (component->priv->module->pending_scale)
  456. clock_set_scale(component, component->priv->module->scale);
  457. }
  458. /*****************************************************************************/
  459. static void clock_process_stream_event(MMAL_COMPONENT_T *component, CLOCK_STREAM_T *stream,
  460. CLOCK_STREAM_EVENT_T event, TIME_T stc)
  461. {
  462. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  463. switch (event)
  464. {
  465. case CLOCK_STREAM_EVENT_FRAME_COMPLETE:
  466. clock_adjust_clocks(component, stc);
  467. if (clock_additional_logging)
  468. {
  469. VCOS_ALERT("STRM_%d %"PRIi64" %"PRIi64" %"PRIi64" %"PRIi64" %"PRIi64" %"PRIi64" %d %"
  470. PRIi64" %"PRIi64" %"PRIi64" %"PRIi64" %"PRIi64" %u %u",
  471. stream->id, stream->stc, stream->pts, stream->mt_off_avg, stream->mt_off,
  472. stream->mt_off_std, ABS(stream->mt_off - stream->mt_off_avg), stream->avg_filter.count,
  473. module->mt_off_clk, module->mt_off_target, module->adj_p, module->adj_m, module->adj,
  474. module->stream_min_id, module->stream_max_id);
  475. }
  476. break;
  477. case CLOCK_STREAM_EVENT_DISCONT:
  478. clock_reset_clocks(component);
  479. break;
  480. default:
  481. /* ignore all other events */
  482. break;
  483. }
  484. }
  485. /** Handler for input buffer events */
  486. static void clock_process_input_buffer_info_event(MMAL_COMPONENT_T *component, MMAL_PORT_T *port,
  487. const MMAL_CLOCK_BUFFER_INFO_T *info)
  488. {
  489. CLOCK_STREAM_EVENT_T stream_event = CLOCK_STREAM_EVENT_NONE;
  490. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  491. MMAL_PORT_MODULE_T *port_module = port->priv->module;
  492. TIME_T stc = (TIME_T)((uint64_t)info->arrival_time);
  493. TIME_T pts = info->time_stamp;
  494. LOG_TRACE("port %d %"PRIi64" %"PRIi64, port->index, stc, pts);
  495. if (!port_module->stream)
  496. {
  497. /* First data received for this stream */
  498. uint32_t filter_length = module->frame_rate.num * FILTER_DURATION /
  499. module->frame_rate.den;
  500. if (!clock_create_stream(&port_module->stream, port->index, filter_length))
  501. return;
  502. }
  503. stream_event = clock_update_stream(port_module->stream, stc, pts, module->discont_threshold.threshold);
  504. clock_process_stream_event(component, port_module->stream, stream_event, stc);
  505. }
  506. /** Handler for clock scale events */
  507. static void clock_process_scale_event(MMAL_COMPONENT_T *component, MMAL_RATIONAL_T scale)
  508. {
  509. /* When pausing the clock (i.e. scale = 0.0), apply the scale change
  510. * immediately. However, when resuming the clock (i.e. scale = 1.0),
  511. * the scale change can only be applied the next time new buffer timing
  512. * information is received. This ensures that clocks resume with the
  513. * correct media-time. */
  514. if (scale.num == 0)
  515. {
  516. component->priv->module->scale = scale;
  517. clock_set_scale(component, scale);
  518. }
  519. else
  520. {
  521. /* Only support scale == 1.0 */
  522. if (!mmal_rational_equal(component->priv->module->scale, scale) &&
  523. (scale.num == scale.den))
  524. {
  525. component->priv->module->scale = scale;
  526. component->priv->module->pending_scale = 1;
  527. clock_reset_clocks(component);
  528. }
  529. }
  530. }
  531. /** Handler for update threshold events */
  532. static void clock_process_update_threshold_event(MMAL_COMPONENT_T *component, const MMAL_CLOCK_UPDATE_THRESHOLD_T *threshold)
  533. {
  534. unsigned i;
  535. component->priv->module->update_threshold = *threshold;
  536. for (i = 0; i < component->clock_num; ++i)
  537. mmal_port_clock_update_threshold_set(component->clock[i], threshold);
  538. }
  539. /** Handler for discontinuity threshold events */
  540. static void clock_process_discont_threshold_event(MMAL_COMPONENT_T *component, const MMAL_CLOCK_DISCONT_THRESHOLD_T *threshold)
  541. {
  542. unsigned i;
  543. component->priv->module->discont_threshold = *threshold;
  544. for (i = 0; i < component->clock_num; ++i)
  545. mmal_port_clock_discont_threshold_set(component->clock[i], threshold);
  546. }
  547. /** Handler for request threshold events */
  548. static void clock_process_request_threshold_event(MMAL_COMPONENT_T *component, const MMAL_CLOCK_REQUEST_THRESHOLD_T *threshold)
  549. {
  550. unsigned i;
  551. component->priv->module->request_threshold = *threshold;
  552. for (i = 0; i < component->clock_num; ++i)
  553. mmal_port_clock_request_threshold_set(component->clock[i], threshold);
  554. }
  555. /** Handler for latency events */
  556. static void clock_process_latency_event(MMAL_COMPONENT_T *component, const MMAL_CLOCK_LATENCY_T *latency)
  557. {
  558. component->priv->module->latency = *latency;
  559. clock_reset_clocks(component);
  560. }
  561. /** Add a clock port event to the queue and trigger the action thread */
  562. static MMAL_STATUS_T clock_event_queue(MMAL_COMPONENT_T *component, MMAL_PORT_T *port, const MMAL_CLOCK_EVENT_T *event)
  563. {
  564. CLOCK_PORT_EVENT_T *slot = (CLOCK_PORT_EVENT_T*)mmal_list_pop_front(component->priv->module->events.free);
  565. if (!slot)
  566. {
  567. LOG_ERROR("no event slots available");
  568. return MMAL_ENOSPC;
  569. }
  570. slot->port = port;
  571. slot->event = *event;
  572. mmal_list_push_back(component->priv->module->events.queue, &slot->link);
  573. return mmal_component_action_trigger(component);
  574. }
  575. /** Get the next clock port event in the queue */
  576. static MMAL_STATUS_T clock_event_dequeue(MMAL_COMPONENT_T *component, CLOCK_PORT_EVENT_T *port_event)
  577. {
  578. CLOCK_PORT_EVENT_T *slot = (CLOCK_PORT_EVENT_T*)mmal_list_pop_front(component->priv->module->events.queue);
  579. if (!slot)
  580. return MMAL_EINVAL;
  581. port_event->port = slot->port;
  582. port_event->event = slot->event;
  583. mmal_list_push_back(component->priv->module->events.free, &slot->link);
  584. if (port_event->event.buffer)
  585. {
  586. port_event->event.buffer->length = 0;
  587. mmal_port_buffer_header_callback(port_event->port, port_event->event.buffer);
  588. }
  589. return MMAL_SUCCESS;
  590. }
  591. /** Event callback from a clock port */
  592. static void clock_event_cb(MMAL_PORT_T *port, const MMAL_CLOCK_EVENT_T *event)
  593. {
  594. clock_event_queue(port->component, port, event);
  595. }
  596. /*****************************************************************************/
  597. /** Actual processing function */
  598. static MMAL_BOOL_T clock_do_processing(MMAL_COMPONENT_T *component)
  599. {
  600. CLOCK_PORT_EVENT_T port_event;
  601. if (clock_event_dequeue(component, &port_event) != MMAL_SUCCESS)
  602. return MMAL_FALSE; /* No more external events to process */
  603. /* Process external events (coming from clock ports) */
  604. switch (port_event.event.id)
  605. {
  606. case MMAL_CLOCK_EVENT_SCALE:
  607. clock_process_scale_event(component, port_event.event.data.scale);
  608. break;
  609. case MMAL_CLOCK_EVENT_UPDATE_THRESHOLD:
  610. clock_process_update_threshold_event(component, &port_event.event.data.update_threshold);
  611. break;
  612. case MMAL_CLOCK_EVENT_DISCONT_THRESHOLD:
  613. clock_process_discont_threshold_event(component, &port_event.event.data.discont_threshold);
  614. break;
  615. case MMAL_CLOCK_EVENT_REQUEST_THRESHOLD:
  616. clock_process_request_threshold_event(component, &port_event.event.data.request_threshold);
  617. break;
  618. case MMAL_CLOCK_EVENT_LATENCY:
  619. clock_process_latency_event(component, &port_event.event.data.latency);
  620. break;
  621. case MMAL_CLOCK_EVENT_INPUT_BUFFER_INFO:
  622. clock_process_input_buffer_info_event(component, port_event.port, &port_event.event.data.buffer);
  623. break;
  624. default:
  625. break;
  626. }
  627. return MMAL_TRUE;
  628. }
  629. /** Component action thread */
  630. static void clock_do_processing_loop(MMAL_COMPONENT_T *component)
  631. {
  632. while (clock_do_processing(component));
  633. }
  634. /*****************************************************************************/
  635. /** Set a parameter on the clock component's control port */
  636. static MMAL_STATUS_T clock_control_parameter_set(MMAL_PORT_T *port, const MMAL_PARAMETER_HEADER_T *param)
  637. {
  638. MMAL_COMPONENT_T *component = port->component;
  639. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  640. MMAL_STATUS_T status = MMAL_SUCCESS;
  641. switch (param->id)
  642. {
  643. case MMAL_PARAMETER_CLOCK_FRAME_RATE:
  644. {
  645. const MMAL_PARAMETER_FRAME_RATE_T *p = (const MMAL_PARAMETER_FRAME_RATE_T *)param;
  646. module->frame_rate = p->frame_rate;
  647. /* XXX: take frame_rate.den into account */
  648. module->frame_rate_log2 = pow2_shift(next_pow2(module->frame_rate.num));
  649. module->frame_duration = p->frame_rate.den * 1000000 / p->frame_rate.num;
  650. LOG_TRACE("frame rate %d/%d (%u) duration %"PRIi64,
  651. module->frame_rate.num, module->frame_rate.den,
  652. module->frame_rate_log2, module->frame_duration);
  653. }
  654. break;
  655. case MMAL_PARAMETER_CLOCK_LATENCY:
  656. {
  657. /* Changing the latency setting requires a reset of the clock algorithm, but
  658. * that can only be safely done from within the component's worker thread.
  659. * So, queue the new latency setting as a clock event. */
  660. const MMAL_PARAMETER_CLOCK_LATENCY_T *p = (const MMAL_PARAMETER_CLOCK_LATENCY_T *)param;
  661. MMAL_CLOCK_EVENT_T event = { MMAL_CLOCK_EVENT_LATENCY, MMAL_CLOCK_EVENT_MAGIC };
  662. LOG_TRACE("latency target %"PRIi64" attack %"PRIi64"/%"PRIi64,
  663. p->value.target, p->value.attack_rate, p->value.attack_period);
  664. event.data.latency = p->value;
  665. status = clock_event_queue(port->component, port, &event);
  666. }
  667. break;
  668. default:
  669. LOG_ERROR("parameter not supported (0x%x)", param->id);
  670. status = MMAL_ENOSYS;
  671. break;
  672. }
  673. return status;
  674. }
  675. /** Destroy a previously created component */
  676. static MMAL_STATUS_T clock_component_destroy(MMAL_COMPONENT_T *component)
  677. {
  678. MMAL_COMPONENT_MODULE_T *module = component->priv->module;
  679. unsigned int i;
  680. if (module->events.free)
  681. mmal_list_destroy(module->events.free);
  682. if (module->events.queue)
  683. mmal_list_destroy(module->events.queue);
  684. if (component->clock_num)
  685. {
  686. for (i = 0; i < component->clock_num; ++i)
  687. vcos_free(component->clock[i]->priv->module->stream);
  688. mmal_ports_clock_free(component->clock, component->clock_num);
  689. }
  690. vcos_free(module);
  691. return MMAL_SUCCESS;
  692. }
  693. /** Create an instance of a clock component */
  694. static MMAL_STATUS_T mmal_component_create_clock(const char *name, MMAL_COMPONENT_T *component)
  695. {
  696. int i;
  697. MMAL_COMPONENT_MODULE_T *module;
  698. MMAL_STATUS_T status = MMAL_ENOMEM;
  699. MMAL_PARAM_UNUSED(name);
  700. /* Allocate the context for our module */
  701. component->priv->module = module = vcos_malloc(sizeof(*module), "mmal module");
  702. if (!module)
  703. return MMAL_ENOMEM;
  704. memset(module, 0, sizeof(*module));
  705. component->priv->pf_destroy = clock_component_destroy;
  706. /* Create the clock ports (clock ports are managed by the framework) */
  707. component->clock = mmal_ports_clock_alloc(component, CLOCK_PORTS_NUM,
  708. sizeof(MMAL_PORT_MODULE_T), clock_event_cb);
  709. if (!component->clock)
  710. goto error;
  711. component->clock_num = CLOCK_PORTS_NUM;
  712. component->control->priv->pf_parameter_set = clock_control_parameter_set;
  713. /* Setup event slots */
  714. module->events.free = mmal_list_create();
  715. module->events.queue = mmal_list_create();
  716. if (!module->events.free || !module->events.queue)
  717. {
  718. LOG_ERROR("failed to create list %p %p", module->events.free, module->events.queue);
  719. goto error;
  720. }
  721. for (i = 0; i < MAX_CLOCK_EVENT_SLOTS; ++i)
  722. mmal_list_push_back(module->events.free, &module->events.pool[i].link);
  723. component->priv->priority = VCOS_THREAD_PRI_REALTIME;
  724. status = mmal_component_action_register(component, clock_do_processing_loop);
  725. module->clock_discont = MMAL_TRUE;
  726. module->frame_rate.num = DEFAULT_FRAME_RATE;
  727. module->frame_rate.den = 1;
  728. module->scale = mmal_port_clock_scale_get(component->clock[0]);
  729. memset(&module->latency, 0, sizeof(module->latency));
  730. module->latency.target = DEFAULT_CLOCK_LATENCY;
  731. mmal_port_clock_update_threshold_get(component->clock[0], &module->update_threshold);
  732. mmal_port_clock_discont_threshold_get(component->clock[0], &module->discont_threshold);
  733. mmal_port_clock_request_threshold_get(component->clock[0], &module->request_threshold);
  734. return status;
  735. error:
  736. clock_component_destroy(component);
  737. return status;
  738. }
  739. /*****************************************************************************/
  740. MMAL_CONSTRUCTOR(mmal_register_component_clock);
  741. void mmal_register_component_clock(void)
  742. {
  743. mmal_component_supplier_register("clock", mmal_component_create_clock);
  744. }