annotate crc.c @ 893:1a50f1735214 libavformat

TrueSpeech compatible audio decoder by Konstantin Shishkov
author diego
date Tue, 03 Jan 2006 02:30:38 +0000
parents da1d5db0ce5c
children edbe5c3717f9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 881
diff changeset
1 /*
0
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
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 241
diff changeset
36 unsigned long update_adler32(unsigned long adler, const uint8_t *buf, unsigned int len)
0
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 {
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 0
diff changeset
61 uint32_t crcval;
0
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 */
418
41da3366d341 checksuming for nut & nice checksum API for libavformat
michael
parents: 241
diff changeset
69 crc->crcval = update_adler32(0, NULL, 0);
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
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 418
diff changeset
74 static int crc_write_packet(struct AVFormatContext *s, AVPacket *pkt)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
75 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
76 CRCState *crc = s->priv_data;
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 418
diff changeset
77 crc->crcval = update_adler32(crc->crcval, pkt->data, pkt->size);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
78 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
79 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
80
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
81 static int crc_write_trailer(struct AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
82 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
83 CRCState *crc = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
84 char buf[64];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
85
870
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
86 snprintf(buf, sizeof(buf), "CRC=0x%08x\n", crc->crcval);
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
87 put_buffer(&s->pb, buf, strlen(buf));
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
88 put_flush_packet(&s->pb);
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
89 return 0;
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
90 }
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
91
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
92 static int framecrc_write_packet(struct AVFormatContext *s, AVPacket *pkt)
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
93 {
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
94 uint32_t crc = update_adler32(0, pkt->data, pkt->size);
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
95 char buf[256];
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
96
881
91dcb9da9be6 use PRIxN, %zd, %td formats where needed
mru
parents: 870
diff changeset
97 snprintf(buf, sizeof(buf), "%d, %"PRId64", %d, 0x%08x\n", pkt->stream_index, pkt->dts, pkt->size, crc);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
98 put_buffer(&s->pb, buf, strlen(buf));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 put_flush_packet(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
100 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
102
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
103 static AVOutputFormat crc_format = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
104 "crc",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
105 "crc testing format",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
106 NULL,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
107 "",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108 sizeof(CRCState),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109 CODEC_ID_PCM_S16LE,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
110 CODEC_ID_RAWVIDEO,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
111 crc_write_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
112 crc_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
113 crc_write_trailer,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
114 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
115
870
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
116 static AVOutputFormat framecrc_format = {
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
117 "framecrc",
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
118 "framecrc testing format",
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
119 NULL,
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
120 "",
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
121 0,
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
122 CODEC_ID_PCM_S16LE,
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
123 CODEC_ID_RAWVIDEO,
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
124 NULL,
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
125 framecrc_write_packet,
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
126 NULL,
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
127 };
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
128
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
129 int crc_init(void)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
130 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
131 av_register_output_format(&crc_format);
870
16df90bf46fd per frame crc support
michael
parents: 468
diff changeset
132 av_register_output_format(&framecrc_format);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
133 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
134 }