annotate crc.c @ 37:8f76666c71c2 libavformat

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