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.

137 lines
3.5KB

  1. /*
  2. * CRC decoder (for codec/format testing)
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. /* adler32.c -- compute the Adler-32 checksum of a data stream
  21. * Copyright (C) 1995 Mark Adler
  22. * For conditions of distribution and use, see copyright notice in zlib.h
  23. */
  24. #define BASE 65521L /* largest prime smaller than 65536 */
  25. #define NMAX 5552
  26. /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  27. #define DO1(buf) {s1 += *buf++; s2 += s1;}
  28. #define DO2(buf) DO1(buf); DO1(buf);
  29. #define DO4(buf) DO2(buf); DO2(buf);
  30. #define DO8(buf) DO4(buf); DO4(buf);
  31. #define DO16(buf) DO8(buf); DO8(buf);
  32. unsigned long update_adler32(unsigned long adler, const uint8_t *buf, unsigned int len)
  33. {
  34. unsigned long s1 = adler & 0xffff;
  35. unsigned long s2 = (adler >> 16) & 0xffff;
  36. int k;
  37. if (buf == NULL) return 1L;
  38. while (len > 0) {
  39. k = len < NMAX ? len : NMAX;
  40. len -= k;
  41. while (k >= 16) {
  42. DO16(buf);
  43. k -= 16;
  44. }
  45. if (k != 0) do {
  46. DO1(buf);
  47. } while (--k);
  48. s1 %= BASE;
  49. s2 %= BASE;
  50. }
  51. return (s2 << 16) | s1;
  52. }
  53. #ifdef CONFIG_MUXERS
  54. typedef struct CRCState {
  55. uint32_t crcval;
  56. } CRCState;
  57. static int crc_write_header(struct AVFormatContext *s)
  58. {
  59. CRCState *crc = s->priv_data;
  60. /* init CRC */
  61. crc->crcval = update_adler32(0, NULL, 0);
  62. return 0;
  63. }
  64. static int crc_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  65. {
  66. CRCState *crc = s->priv_data;
  67. crc->crcval = update_adler32(crc->crcval, pkt->data, pkt->size);
  68. return 0;
  69. }
  70. static int crc_write_trailer(struct AVFormatContext *s)
  71. {
  72. CRCState *crc = s->priv_data;
  73. char buf[64];
  74. snprintf(buf, sizeof(buf), "CRC=0x%08x\n", crc->crcval);
  75. put_buffer(&s->pb, buf, strlen(buf));
  76. put_flush_packet(&s->pb);
  77. return 0;
  78. }
  79. static int framecrc_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  80. {
  81. uint32_t crc = update_adler32(0, pkt->data, pkt->size);
  82. char buf[256];
  83. snprintf(buf, sizeof(buf), "%d, %"PRId64", %d, 0x%08x\n", pkt->stream_index, pkt->dts, pkt->size, crc);
  84. put_buffer(&s->pb, buf, strlen(buf));
  85. put_flush_packet(&s->pb);
  86. return 0;
  87. }
  88. static AVOutputFormat crc_format = {
  89. "crc",
  90. "crc testing format",
  91. NULL,
  92. "",
  93. sizeof(CRCState),
  94. CODEC_ID_PCM_S16LE,
  95. CODEC_ID_RAWVIDEO,
  96. crc_write_header,
  97. crc_write_packet,
  98. crc_write_trailer,
  99. };
  100. static AVOutputFormat framecrc_format = {
  101. "framecrc",
  102. "framecrc testing format",
  103. NULL,
  104. "",
  105. 0,
  106. CODEC_ID_PCM_S16LE,
  107. CODEC_ID_RAWVIDEO,
  108. NULL,
  109. framecrc_write_packet,
  110. NULL,
  111. };
  112. int crc_init(void)
  113. {
  114. av_register_output_format(&crc_format);
  115. av_register_output_format(&framecrc_format);
  116. return 0;
  117. }
  118. #endif /* CONFIG_MUXERS */