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.

110 lines
2.8KB

  1. /*
  2. * CRC decoder (for codec/format testing)
  3. * Copyright (c) 2002 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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. static UINT32 adler32(UINT32 adler, UINT8 *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. typedef struct CRCState {
  54. UINT32 crcval;
  55. } CRCState;
  56. /* simple formats */
  57. static int crc_write_header(struct AVFormatContext *s)
  58. {
  59. CRCState *crc;
  60. crc = av_malloc(sizeof(CRCState));
  61. if (!crc)
  62. return -1;
  63. s->priv_data = crc;
  64. /* init CRC */
  65. crc->crcval = adler32(0, NULL, 0);
  66. return 0;
  67. }
  68. static int crc_write_packet(struct AVFormatContext *s,
  69. int stream_index,
  70. unsigned char *buf, int size, int force_pts)
  71. {
  72. CRCState *crc = s->priv_data;
  73. crc->crcval = adler32(crc->crcval, buf, size);
  74. return 0;
  75. }
  76. static int crc_write_trailer(struct AVFormatContext *s)
  77. {
  78. CRCState *crc = s->priv_data;
  79. char buf[64];
  80. snprintf(buf, sizeof(buf), "CRC=%08x\n", crc->crcval);
  81. put_buffer(&s->pb, buf, strlen(buf));
  82. put_flush_packet(&s->pb);
  83. return 0;
  84. }
  85. AVFormat crc_format = {
  86. "crc",
  87. "crc testing format",
  88. NULL,
  89. "",
  90. CODEC_ID_PCM_S16LE,
  91. CODEC_ID_RAWVIDEO,
  92. crc_write_header,
  93. crc_write_packet,
  94. crc_write_trailer,
  95. };