fifo.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "libavutil/fifo.h"
  21. int main(void)
  22. {
  23. /* create a FIFO buffer */
  24. AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int));
  25. int i, j, n, *p;
  26. /* fill data */
  27. for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
  28. av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
  29. /* peek at FIFO */
  30. n = av_fifo_size(fifo) / sizeof(int);
  31. for (i = -n + 1; i < n; i++) {
  32. int *v = (int *)av_fifo_peek2(fifo, i * sizeof(int));
  33. printf("%d: %d\n", i, *v);
  34. }
  35. printf("\n");
  36. /* peek_at at FIFO */
  37. n = av_fifo_size(fifo) / sizeof(int);
  38. for (i = 0; i < n; i++) {
  39. av_fifo_generic_peek_at(fifo, &j, i * sizeof(int), sizeof(j), NULL);
  40. printf("%d: %d\n", i, j);
  41. }
  42. printf("\n");
  43. /* generic peek at FIFO */
  44. n = av_fifo_size(fifo);
  45. p = malloc(n);
  46. if (p == NULL) {
  47. fprintf(stderr, "failed to allocate memory.\n");
  48. exit(1);
  49. }
  50. (void) av_fifo_generic_peek(fifo, p, n, NULL);
  51. /* read data at p */
  52. n /= sizeof(int);
  53. for(i = 0; i < n; ++i)
  54. printf("%d: %d\n", i, p[i]);
  55. putchar('\n');
  56. /* read data */
  57. for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) {
  58. av_fifo_generic_read(fifo, &j, sizeof(int), NULL);
  59. printf("%d ", j);
  60. }
  61. printf("\n");
  62. /* test *ndx overflow */
  63. av_fifo_reset(fifo);
  64. fifo->rndx = fifo->wndx = ~(uint32_t)0 - 5;
  65. /* fill data */
  66. for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
  67. av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
  68. /* peek_at at FIFO */
  69. n = av_fifo_size(fifo) / sizeof(int);
  70. for (i = 0; i < n; i++) {
  71. av_fifo_generic_peek_at(fifo, &j, i * sizeof(int), sizeof(j), NULL);
  72. printf("%d: %d\n", i, j);
  73. }
  74. putchar('\n');
  75. /* test fifo_grow */
  76. (void) av_fifo_grow(fifo, 15 * sizeof(int));
  77. /* fill data */
  78. n = av_fifo_size(fifo) / sizeof(int);
  79. for (i = n; av_fifo_space(fifo) >= sizeof(int); ++i)
  80. av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
  81. /* peek_at at FIFO */
  82. n = av_fifo_size(fifo) / sizeof(int);
  83. for (i = 0; i < n; i++) {
  84. av_fifo_generic_peek_at(fifo, &j, i * sizeof(int), sizeof(j), NULL);
  85. printf("%d: %d\n", i, j);
  86. }
  87. av_fifo_free(fifo);
  88. free(p);
  89. return 0;
  90. }