2 * CRC decoder (for codec/format testing)
3 * Copyright (c) 2002 Fabrice Bellard.
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.
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.
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
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
26 #define BASE 65521L /* largest prime smaller than 65536 */
28 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
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);
36 unsigned long update_adler32(unsigned long adler
, const uint8_t *buf
, unsigned int len
)
38 unsigned long s1
= adler
& 0xffff;
39 unsigned long s2
= (adler
>> 16) & 0xffff;
42 if (buf
== NULL
) return 1L;
45 k
= len
< NMAX ? len
: NMAX
;
57 return (s2
<< 16) | s1
;
61 typedef struct CRCState
{
65 static int crc_write_header(struct AVFormatContext
*s
)
67 CRCState
*crc
= s
->priv_data
;
70 crc
->crcval
= update_adler32(0, NULL
, 0);
75 static int crc_write_packet(struct AVFormatContext
*s
, AVPacket
*pkt
)
77 CRCState
*crc
= s
->priv_data
;
78 crc
->crcval
= update_adler32(crc
->crcval
, pkt
->data
, pkt
->size
);
82 static int crc_write_trailer(struct AVFormatContext
*s
)
84 CRCState
*crc
= s
->priv_data
;
87 snprintf(buf
, sizeof(buf
), "CRC=0x%08x\n", crc
->crcval
);
88 put_buffer(&s
->pb
, buf
, strlen(buf
));
89 put_flush_packet(&s
->pb
);
93 static int framecrc_write_packet(struct AVFormatContext
*s
, AVPacket
*pkt
)
95 uint32_t crc
= update_adler32(0, pkt
->data
, pkt
->size
);
98 snprintf(buf
, sizeof(buf
), "%d, %"PRId64
", %d, 0x%08x\n", pkt
->stream_index
, pkt
->dts
, pkt
->size
, crc
);
99 put_buffer(&s
->pb
, buf
, strlen(buf
));
100 put_flush_packet(&s
->pb
);
104 static AVOutputFormat crc_format
= {
106 "crc testing format",
117 static AVOutputFormat framecrc_format
= {
119 "framecrc testing format",
126 framecrc_write_packet
,
132 av_register_output_format(&crc_format
);
133 av_register_output_format(&framecrc_format
);
136 #endif /* CONFIG_MUXERS */