File: encoder-cellb.cc

package info (click to toggle)
vic 2.8ucl4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,864 kB
  • ctags: 9,033
  • sloc: ansic: 56,989; cpp: 44,560; tcl: 5,550; sh: 1,382; perl: 1,329; makefile: 357
file content (368 lines) | stat: -rw-r--r-- 10,501 bytes parent folder | download
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright (c) 1994 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the University of
 *      California, Berkeley and the Network Research Group at
 *      Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/* This code was originally derived from a cellb reference encoder from
 * Sun Microsystems with the following copyright:
 *
 * Copyright (c) Sun Microsystems, Inc.  1992, 1993. All rights reserved. 
 *
 * License is granted to copy, to use, and to make and to use derivative 
 * works for research and evaluation purposes, provided that Sun Microsystems is
 * acknowledged in all documentation pertaining to any such copy or derivative
 * work. Sun Microsystems grants no other licenses expressed or implied. The
 * Sun Microsystems  trade name should not be used in any advertising without
 * its written permission.
 *
 * SUN MICROSYSTEMS MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF
 * THIS SOFTWARE FOR ANY PARTICULAR PURPOSE.  The software is provided "as is"
 * without express or implied warranty of any kind.
 *
 * These notices must be retained in any copies of any part of this software.
 */
static const char rcsid[] =
    "@(#) $Header: /cs/research/mice/starship/src/local/CVS_repository/vic/encoder-cellb.cc,v 1.1.1.1 1998/02/18 18:03:26 ucacsva Exp $ (LBL)";

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "inet.h"
#include "net.h"
#include "rtp.h"
#include "bsd-endian.h"
#include "crdef.h"
#include "transmitter.h"
#include "module.h"

#define MAXSKIP		32

extern "C" u_char cellb_yyremap[];
extern "C" u_char cellb_uvremap[];

#define HLEN (sizeof(rtphdr) + sizeof(cellbhdr))

class CellbEncoder : public TransmitterModule {
 public:
	CellbEncoder();
	~CellbEncoder();
	virtual int consume(const VideoFrame*);
 private:
	void size(int w, int h);
	int diff(const u_char blk1[4], const u_char blk2[4]) const;
	void send(Transmitter::pktbuf* pb, int sync, int x, int y, int cc);
	u_int encode_cell(const u_char* lum, const u_char* chm, int stride);

	int nw_;
	int nh_;

	/*
	 * Tables for CellB.
	 */
	u_char* divtable_[17];/*XXX*/
	u_char dtbl_[34816];
};

static class CellbEncoderMatcher : public Matcher {
public:
	CellbEncoderMatcher() : Matcher("module") {}
	TclObject* match(const char* fmt) {
		if (strcasecmp(fmt, "cellb") == 0)
			return (new CellbEncoder);
		return (0);
	}
} encoder_matcher_cellb;

void CellbEncoder::send(Transmitter::pktbuf* pb, int sync,
			int x, int y, int cc)
{
	pb->iov[0].iov_len = HLEN;
	pb->iov[1].iov_len = cc;
	rtphdr* rh = (rtphdr*)pb->hdr;
	if (sync)
		rh->rh_flags |= htons(RTP_M);

	cellbhdr* ch = (cellbhdr*)(rh + 1);
	ch->width = nw_;
	ch->height = nh_;
	ch->x = htons(x);
	ch->y = htons(y);
	tx_->send(pb);
}

/*
 * Returns 32-bit encoded cell in host order.
 */
u_int CellbEncoder::encode_cell(const u_char* lum,
				const u_char* chm,
				int stride)
{
	u_int mask = 0xff00ff;
	u_int q0 = *(u_int *)lum;
	u_int q1 = *(u_int *)&lum[stride];
	u_int q2 = *(u_int *)&lum[stride << 1];
	u_int q3 = *(u_int *)&lum[(stride << 1) + stride];

	/*
	 * Compute the mean of the luminance component.
	 * Do some of the adds in parallel.
	 */
	u_int ymean = q0 & mask;
	ymean += (q0 >> 8) & mask;
	ymean += q1 & mask;
	ymean += (q1 >> 8) & mask;
	ymean += q2 & mask;
	ymean += (q2 >> 8) & mask;
	ymean += q3 & mask;
	ymean += (q3 >> 8) & mask;
	ymean = (ymean & 0xffff) + (ymean >> 16);
	u_int sum = ymean;
	ymean >>= 4;

	/*
	  .... next the bit mask for the tile is generated. for
	  the first fifteen bits this is done by comparing each
	  luminance value to the mean. if that value is greater
	  than the mean the correnponding bit in the mask is set,
	  otherwise the counter for the number of zeros in the mask,
	  tmp, is incremented. also, the values of those pixels less
	  than the mean are accumulted in ylo ....
	  */

	mask = 0;
	int ylo = 0;
	int nb = 0;

#define ACCUMULATE(pix, mean, n, slow, mask, pos) \
	{ \
		int t = (pix) & 0xff; \
		t -= (mean); \
		int neg = t >> 31; \
		(slow) += neg & t; \
		(slow) += neg & (mean); \
		neg &= 1; \
		(n) += neg; \
		(mask) |= neg << (pos); \
	}
#if BYTE_ORDER == LITTLE_ENDIAN
#define BYTE(b, n) ((b) >> (8 * (3 - (n))))
#else
#define BYTE(b, n) ((b) >> (8 * (n)))
#endif
	ACCUMULATE(BYTE(q3, 0), ymean, nb, ylo, mask, 0);
	ACCUMULATE(BYTE(q3, 1), ymean, nb, ylo, mask, 1);
	ACCUMULATE(BYTE(q3, 2), ymean, nb, ylo, mask, 2);
	ACCUMULATE(BYTE(q3, 3), ymean, nb, ylo, mask, 3);

	ACCUMULATE(BYTE(q2, 0), ymean, nb, ylo, mask, 4);
	ACCUMULATE(BYTE(q2, 1), ymean, nb, ylo, mask, 5);
	ACCUMULATE(BYTE(q2, 2), ymean, nb, ylo, mask, 6);
	ACCUMULATE(BYTE(q2, 3), ymean, nb, ylo, mask, 7);

	ACCUMULATE(BYTE(q1, 0), ymean, nb, ylo, mask, 8);
	ACCUMULATE(BYTE(q1, 1), ymean, nb, ylo, mask, 9);
	ACCUMULATE(BYTE(q1, 2), ymean, nb, ylo, mask, 10);
	ACCUMULATE(BYTE(q1, 3), ymean, nb, ylo, mask, 11);

	ACCUMULATE(BYTE(q0, 0), ymean, nb, ylo, mask, 12);
	ACCUMULATE(BYTE(q0, 1), ymean, nb, ylo, mask, 13);
	ACCUMULATE(BYTE(q0, 2), ymean, nb, ylo, mask, 14);
	ACCUMULATE(BYTE(q0, 3), ymean, nb, ylo, mask, 15);
#undef BYTE
#undef ACCUMULATE

	int yhi = sum - ylo;
	yhi = divtable_[16 - nb][yhi];
	ylo = divtable_[nb][ylo];
	/*
	 * Normalize bitmask.  Note mask is
	 * computed in negative logic.
	 */
	if (mask & 0x8000) {
		mask ^= 0xffff;
		ylo <<= 8;
		ylo |= yhi;
	} else
		ylo |= yhi << 8;

	mask <<= 16;

	/*
	  .... the high mean and low mean values are finally quantized
	  by the cellb_yyremap[] table, the Y/Y vector codeword byte, the
	  U/V codeword byte and the bitmask are then assembled into
	  a Normal Cell bytecode and returned form the routine ....
	  */
	mask |= cellb_yyremap[ylo];

	/*
	 * Simply downsample the chroma components, then
	 * vector quantize according to the CellB spec.
	 * (The encoder in nv computes a mean of the 16
	 * chroma components.  This is probably not a good
	 * idea first because it's computationally more
	 * expensive and second because it doesn't buy
	 * much.  This is equivalent to placing a low-pass
	 * (rectangular) filter in front of the downsampling
	 * stage.  But the chroma signal in most cases
	 * doesn't have much of a high frequency component.
	 * Try visually comparing this coder with nv's.
	 * You will probably see little difference.)
	 */
	int u = chm[0];
	int v = chm[framesize_ >> 1];
	mask |= cellb_uvremap[u >> 2 << 6 | v >> 2] << 8;

	return (mask);
}

void CellbEncoder::size(int w, int h)
{
	Module::size(w, h);
	nw_ = htons(w);
	nh_ = htons(h);
}

CellbEncoder::CellbEncoder() : TransmitterModule(FT_YUV_422)
{
	/* .... initialize division table ....  */
	u_char* p = dtbl_;
	divtable_[0] = p;
	for (int i = 1; i <= 16; i++) {
		divtable_[i] = p;
		for (int j = 0; j < 256*i; j++)
			*p++ = j / i;
	}
	width_ = 0;
	height_ = 0;
	nw_ = 0;
	nh_ = 0;
}

CellbEncoder::~CellbEncoder()
{
}

/*
 * Encode a cellb frame.  this code can be improved by pruning
 * skip-code runs off the front and back of the frame.
 */
int CellbEncoder::consume(const VideoFrame* vf)
{
	if (!samesize(vf))
		size(vf->width_, vf->height_);
	YuvFrame* p = (YuvFrame*)vf;
	tx_->flush();

	Transmitter::pktbuf* pb = tx_->alloc(p->ts_, RTP_PT_CELLB);
	u_char* op = (u_char*)pb->iov[1].iov_base;
	u_char* ep = op + tx_->mtu() - HLEN;
	int x0 = 0;
	int y0 = 0;

	int cc = 0;
	int nb = 0;
	int nskip = 0;
	int stride = 3 * width_;

	int blkbase = 0;
	/*
	 * Compute width and height in cells.
	 */
	int w = width_ >> 2;
	int h = height_ >> 2;

	/*
	 * Loop over 4x4 cells, encoding those indicated by the 16x16
	 * CR vector.  Because there are 4 cells per 16x16 replenishment
	 * block, the blocks are incremented only every other interation
	 * of each loop.
	 */

	const u_int8_t* lum = p->bp_;
	const u_int8_t* chm = lum + framesize_;
	const u_int8_t* crvec = p->crvec_;
	for (int y = 0; y < h; ) {
		int blkno = blkbase;
		for (int x = 0; x < w; ) {
			if (op + 2*5 >= ep) {
				/*XXX sanity*/
				if (cc != op - (u_char*)pb->iov[1].iov_base)
					abort();

				nb += cc + HLEN;
				send(pb, 0, x0, y0, cc);
				cc = 0;
				pb = tx_->alloc(p->ts_, RTP_PT_CELLB);
				op = (u_char*)pb->iov[1].iov_base;
				ep = op + tx_->mtu() - HLEN;
				x0 = x;
				y0 = y;
			}
			if (crvec[blkno] & CR_SEND) {
				/*
				 * cell has seen recent change. send it.
				 */
				if (nskip > 0) {
					++cc;
					*op++ = 0x80 | (nskip - 1);
					nskip = 0;
				}
				int code = encode_cell(lum, chm, width_);
				op[0] = code >> 24;
				op[1] = code >> 16;
				op[2] = code >> 8;
				op[3] = code;
				op += 4;
				cc += 4;
			} else {
				if (++nskip >= MAXSKIP) {
					++cc;
					*op++ = 0x80 | (MAXSKIP - 1);
					nskip = 0;
				}
			}
			if ((++x & 3) == 0)
				++blkno;
			lum += 4;
			chm += 2;
		}
		if ((++y & 3) == 0)
			blkbase += w >> 2;

		lum += stride;
		chm += stride >> 1;
	}
	nb += cc;
	send(pb, 1, x0, y0, cc);
	return (nb);
}