Commit | Line | Data |
---|---|---|
1c098b2f FB |
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 | ||
21 | /* adler32.c -- compute the Adler-32 checksum of a data stream | |
22 | * Copyright (C) 1995 Mark Adler | |
23 | * For conditions of distribution and use, see copyright notice in zlib.h | |
24 | */ | |
25 | ||
26 | #define BASE 65521L /* largest prime smaller than 65536 */ | |
27 | #define NMAX 5552 | |
28 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ | |
29 | ||
30 | #define DO1(buf) {s1 += *buf++; s2 += s1;} | |
31 | #define DO2(buf) DO1(buf); DO1(buf); | |
32 | #define DO4(buf) DO2(buf); DO2(buf); | |
33 | #define DO8(buf) DO4(buf); DO4(buf); | |
34 | #define DO16(buf) DO8(buf); DO8(buf); | |
35 | ||
36 | static UINT32 adler32(UINT32 adler, UINT8 *buf, unsigned int len) | |
37 | { | |
38 | unsigned long s1 = adler & 0xffff; | |
39 | unsigned long s2 = (adler >> 16) & 0xffff; | |
40 | int k; | |
41 | ||
42 | if (buf == NULL) return 1L; | |
43 | ||
44 | while (len > 0) { | |
45 | k = len < NMAX ? len : NMAX; | |
46 | len -= k; | |
47 | while (k >= 16) { | |
48 | DO16(buf); | |
49 | k -= 16; | |
50 | } | |
51 | if (k != 0) do { | |
52 | DO1(buf); | |
53 | } while (--k); | |
54 | s1 %= BASE; | |
55 | s2 %= BASE; | |
56 | } | |
57 | return (s2 << 16) | s1; | |
58 | } | |
59 | ||
60 | typedef struct CRCState { | |
61 | UINT32 crcval; | |
62 | } CRCState; | |
63 | ||
1c098b2f FB |
64 | static int crc_write_header(struct AVFormatContext *s) |
65 | { | |
c9a65ca8 FB |
66 | CRCState *crc = s->priv_data; |
67 | ||
1c098b2f FB |
68 | /* init CRC */ |
69 | crc->crcval = adler32(0, NULL, 0); | |
70 | ||
71 | return 0; | |
72 | } | |
73 | ||
74 | static int crc_write_packet(struct AVFormatContext *s, | |
75 | int stream_index, | |
76 | unsigned char *buf, int size, int force_pts) | |
77 | { | |
78 | CRCState *crc = s->priv_data; | |
79 | crc->crcval = adler32(crc->crcval, buf, size); | |
80 | return 0; | |
81 | } | |
82 | ||
83 | static int crc_write_trailer(struct AVFormatContext *s) | |
84 | { | |
85 | CRCState *crc = s->priv_data; | |
86 | char buf[64]; | |
87 | ||
88 | snprintf(buf, sizeof(buf), "CRC=%08x\n", crc->crcval); | |
89 | put_buffer(&s->pb, buf, strlen(buf)); | |
90 | put_flush_packet(&s->pb); | |
91 | return 0; | |
92 | } | |
93 | ||
c9a65ca8 | 94 | AVOutputFormat crc_format = { |
1c098b2f FB |
95 | "crc", |
96 | "crc testing format", | |
97 | NULL, | |
98 | "", | |
c9a65ca8 | 99 | sizeof(CRCState), |
1c098b2f FB |
100 | CODEC_ID_PCM_S16LE, |
101 | CODEC_ID_RAWVIDEO, | |
102 | crc_write_header, | |
103 | crc_write_packet, | |
104 | crc_write_trailer, | |
105 | }; | |
c9a65ca8 FB |
106 | |
107 | int crc_init(void) | |
108 | { | |
109 | av_register_output_format(&crc_format); | |
110 | return 0; | |
111 | } |