Put muxer-specific code parts in #ifdef CONFIG_MUXERS.
[libav.git] / libavformat / crc.c
CommitLineData
115329f1 1/*
1c098b2f 2 * CRC decoder (for codec/format testing)
19720f15 3 * Copyright (c) 2002 Fabrice Bellard.
1c098b2f 4 *
19720f15
FB
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.
1c098b2f 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
1c098b2f 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
1c098b2f 14 *
19720f15
FB
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
5509bffa 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1c098b2f
FB
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
ee9f36a8 36unsigned long update_adler32(unsigned long adler, const uint8_t *buf, unsigned int len)
1c098b2f
FB
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}
8228bff5 59#ifdef CONFIG_MUXERS
1c098b2f
FB
60
61typedef struct CRCState {
0c1a9eda 62 uint32_t crcval;
1c098b2f
FB
63} CRCState;
64
1c098b2f
FB
65static int crc_write_header(struct AVFormatContext *s)
66{
c9a65ca8
FB
67 CRCState *crc = s->priv_data;
68
1c098b2f 69 /* init CRC */
ee9f36a8 70 crc->crcval = update_adler32(0, NULL, 0);
1c098b2f
FB
71
72 return 0;
73}
74
e928649b 75static int crc_write_packet(struct AVFormatContext *s, AVPacket *pkt)
1c098b2f
FB
76{
77 CRCState *crc = s->priv_data;
e928649b 78 crc->crcval = update_adler32(crc->crcval, pkt->data, pkt->size);
1c098b2f
FB
79 return 0;
80}
81
82static int crc_write_trailer(struct AVFormatContext *s)
83{
84 CRCState *crc = s->priv_data;
85 char buf[64];
86
840fb7a7
MN
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);
90 return 0;
91}
92
93static int framecrc_write_packet(struct AVFormatContext *s, AVPacket *pkt)
94{
95 uint32_t crc = update_adler32(0, pkt->data, pkt->size);
96 char buf[256];
97
4733abcb 98 snprintf(buf, sizeof(buf), "%d, %"PRId64", %d, 0x%08x\n", pkt->stream_index, pkt->dts, pkt->size, crc);
1c098b2f
FB
99 put_buffer(&s->pb, buf, strlen(buf));
100 put_flush_packet(&s->pb);
101 return 0;
102}
103
c18a2692 104static AVOutputFormat crc_format = {
1c098b2f
FB
105 "crc",
106 "crc testing format",
107 NULL,
108 "",
c9a65ca8 109 sizeof(CRCState),
1c098b2f
FB
110 CODEC_ID_PCM_S16LE,
111 CODEC_ID_RAWVIDEO,
112 crc_write_header,
113 crc_write_packet,
114 crc_write_trailer,
115};
c9a65ca8 116
840fb7a7
MN
117static AVOutputFormat framecrc_format = {
118 "framecrc",
119 "framecrc testing format",
120 NULL,
121 "",
122 0,
123 CODEC_ID_PCM_S16LE,
124 CODEC_ID_RAWVIDEO,
125 NULL,
126 framecrc_write_packet,
127 NULL,
128};
129
c9a65ca8
FB
130int crc_init(void)
131{
132 av_register_output_format(&crc_format);
840fb7a7 133 av_register_output_format(&framecrc_format);
c9a65ca8
FB
134 return 0;
135}
8228bff5 136#endif /* CONFIG_MUXERS */