about summary refs log tree commit diff
path: root/converter/ppm/ppmtompeg/parse_huff.pl
blob: 1fc6466c27cc5439c9e5503a51a578a6eca20cb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# 
# Copyright (c) 1993 The Regents of the University of California.
# All rights reserved.
# 
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose, without fee, and without written agreement is
# hereby granted, provided that the above copyright notice and the following
# two paragraphs appear in all copies of this software.
# 
# IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
# CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
# 

#  
#  $Header: /n/picasso/users/dwallach/vid2/mpeg_encode/RCS/parse_huff.pl,v 1.3 1993/01/18 10:56:03 dwallach Exp $
#  $Log: parse_huff.pl,v $
# Revision 1.3  1993/01/18  10:56:03  dwallach
# got RCS headers in huff.c and huff.h working
#
# Revision 1.3  1993/01/18  10:56:03  dwallach
# got RCS headers in huff.c and huff.h working
#
# Revision 1.2  1993/01/18  10:17:29  dwallach
# RCS headers installed, code indented uniformly
#
# Revision 1.2  1993/01/18  10:17:29  dwallach
# RCS headers installed, code indented uniformly
#
#

# this program's purpose in life is to parse the Huffman tables
# and output C code which is awfully useful for translation

# typical usage:
# perl parse_huff.pl huffman.table
# ---> generates huff.c and huff.h

# source format:  # is a comment character
# Otherwise, there are three tab-separated fields in a line:
#    Run, Level, and VLC Code
# Run and Level are integers, corresponding to the RLE coding.
# The VLC code is what bits to generate, and is expected to be
#    composed of 1, 0, space, and 's'.  Spaces are ignored, and
#    s corresponds to the sign bit.  In the output of this program,
#    We'll completely right-shift the data, with a 0 for the sign
#    bit.  The encoder will make appropriate changes before outputing.


open(HUFFC, "> huff.c") || die "Can't write huff.c: $!\n";
open(HUFFH, "> huff.h") || die "Can't write huff.h: $!\n";

sub encode {
    local($run, $level, $vlc) = @_;
    local($result) = 0;
    local($tmp);
    $vlc =~ s/\s//g;           # remove whitespace

    local($bits) = length($vlc);

    foreach (split(//, $vlc)) {
	$_ = 0 if $_ eq 's';
	$result = ($result << 1) + $_;
    }

    $tmp = sprintf("0x%x", $result);
    eval "\$vlc$run[$level] = \$tmp";
    eval "\$bits$run[$level] = \$bits";
}

while(<>) {
    chop;
    s/\s*#.*$//;       # remove comments
    next if /^\s*$/;   # continue if the line is purely whitespace

    ($run, $level, $vlc) = split(/\t/);
    &encode($run, $level, $vlc);

    #
    # we have the +1's to insure the sizes of C arrays are correct
    #
    $maxlevel[$run] = $level+1 if $level >= $maxlevel[$run];
    $maxlevel = $level+1 if $level >= $maxlevel;
    $maxrun = $run+1 if $run >= $maxrun;
}

#
# fix the entries that aren't defined
#
for($run = 0; $run < $maxrun; $run++) {
    eval "\$vlc$run[0] = '0x0' if \$vlc$run[0] eq ''";
    eval "\$bits$run[0] = '0' if \$bits$run[0] eq ''";
}

#
# now, output some C code
#

printf HUFFC <<'EOF', $maxrun, join(", ", @maxlevel);
/*
 * Copyright (c) 1993 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

/*  
 *  $Header: /n/picasso/users/dwallach/vid2/mpeg_encode/RCS/parse_huff.pl,v 1.3 1993/01/18 10:56:03 dwallach Exp $
 */

/*  
 *  THIS FILE IS MACHINE GENERATED!  DO NOT EDIT!
 */
#include "mtypes.h"
#include "huff.h"

int huff_maxlevel[%s] = { %s };

EOF
for($run = 0; $run < $maxrun; $run++) {
    printf HUFFC <<EOF, eval "join(', ', \@vlc$run)", eval "join(', ', \@bits$run)";
uint32 huff_table$run[$maxlevel[$run]] = { %s };
int huff_bits$run[$maxlevel[$run]] = { %s };

EOF
}

printf HUFFC "uint32 *huff_table[$maxrun] = { ";
for($run = 0; $run < $maxrun; $run++) {
    printf HUFFC "%shuff_table$run", ($run)?", ":"";
}
printf HUFFC " };\n";

printf HUFFC "int *huff_bits[$maxrun] = { ";
for($run = 0; $run < $maxrun; $run++) {
    printf HUFFC "%shuff_bits$run", ($run)?", ":"";
}
printf HUFFC " };\n";
close HUFFC;

printf HUFFH <<'EOF', $maxrun, $maxlevel;
/*
 * Copyright (c) 1993 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

/*  
 *  $Header: /n/picasso/users/dwallach/vid2/mpeg_encode/RCS/parse_huff.pl,v 1.3 1993/01/18 10:56:03 dwallach Exp $
 */

/*  
 *  THIS FILE IS MACHINE GENERATED!  DO NOT EDIT!
 */
#define HUFF_MAXRUN	%s
#define HUFF_MAXLEVEL	%s

extern int huff_maxlevel[];
extern uint32 *huff_table[];
extern int *huff_bits[];
EOF