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.

103 lines
3.3KB

  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 <stdint.h>
  19. #include <string.h>
  20. #include "libavutil/lfg.h"
  21. #include "libavutil/log.h"
  22. #include "libavcodec/rangecoder.h"
  23. #define SIZE 1240
  24. /**
  25. * Check if at the current position there is a valid looking termination
  26. * @param version version 0 requires the decoder to know the data size in bytes
  27. * version 1 needs about 1 bit more space but does not need to
  28. * carry the size from encoder to decoder
  29. * @returns negative AVERROR code on error or non negative.
  30. */
  31. static int rac_check_termination(RangeCoder *c, int version)
  32. {
  33. if (version == 1) {
  34. RangeCoder tmp = *c;
  35. get_rac(c, (uint8_t[]) { 129 });
  36. if (c->bytestream == tmp.bytestream && c->bytestream > c->bytestream_start)
  37. tmp.low -= *--tmp.bytestream;
  38. tmp.bytestream_end = tmp.bytestream;
  39. if (get_rac(&tmp, (uint8_t[]) { 129 }))
  40. return AVERROR_INVALIDDATA;
  41. } else {
  42. if (c->bytestream_end != c->bytestream)
  43. return AVERROR_INVALIDDATA;
  44. }
  45. return 0;
  46. }
  47. int main(void)
  48. {
  49. RangeCoder c;
  50. uint8_t b[9 * SIZE] = {0};
  51. uint8_t r[9 * SIZE];
  52. int i, p, actual_length, version;
  53. uint8_t state[10];
  54. AVLFG prng;
  55. av_lfg_init(&prng, 1);
  56. for (version = 0; version < 2; version++) {
  57. for (p = 0; p< 1024; p++) {
  58. ff_init_range_encoder(&c, b, SIZE);
  59. ff_build_rac_states(&c, (1LL << 32) / 20, 128 + 64 + 32 + 16);
  60. memset(state, 128, sizeof(state));
  61. for (i = 0; i < SIZE; i++)
  62. r[i] = av_lfg_get(&prng) % 7;
  63. for (i = 0; i < SIZE; i++)
  64. put_rac(&c, state, r[i] & 1);
  65. actual_length = ff_rac_terminate(&c, version);
  66. ff_init_range_decoder(&c, b, version ? SIZE : actual_length);
  67. memset(state, 128, sizeof(state));
  68. for (i = 0; i < SIZE; i++)
  69. if ((r[i] & 1) != get_rac(&c, state)) {
  70. av_log(NULL, AV_LOG_ERROR, "rac failure at %d pass %d version %d\n", i, p, version);
  71. return 1;
  72. }
  73. if (rac_check_termination(&c, version) < 0) {
  74. av_log(NULL, AV_LOG_ERROR, "rac failure at termination pass %d version %d\n", p, version);
  75. return 1;
  76. }
  77. if (c.bytestream - c.bytestream_start - actual_length != version) {
  78. av_log(NULL, AV_LOG_ERROR, "rac failure at pass %d version %d\n", p, version);
  79. return 1;
  80. }
  81. }
  82. }
  83. return 0;
  84. }