1 /*
2 * encoder-h261.cc --
3 *
4 * H.261 video encoder
5 *
6 * Copyright (c) 1994-2002 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * A. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * B. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * C. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
22 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 static const char rcsid[] =
35 "@(#) $Header: /usr/mash/src/repository/mash/mash-1/codec/encoder-h261.cc,v 1.32 2003/11/19 19:20:17 aswan Exp $";
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 #include "rtp/inet.h"
42 #include "rtp/rtp.h"
43 #include "codec/dct.h"
44 #include "codec/p64/p64-huff.h"
45 #include "misc/bsd-endian.h"
46 #include "tclcl.h"
47 #include "codec/crdef.h"
48 #include "rtp/pktbuf-rtp.h"
49 #include "codec/module.h"
50 #include "codec/module-list.h"
51
52 #include "codec/encoder-h261.h"
53
54 #ifdef __PSVP_ENABLED__
55 #include "psvp/vidreps.h"
56 #endif
57
58 #define HLEN (sizeof(rtphdr) + 4)
59 #define CIF_WIDTH 352
60 #define CIF_HEIGHT 288
61 #define QCIF_WIDTH 176
62 #define QCIF_HEIGHT 144
63 #define BMB 6 /* # blocks in a MB */
64 #define MBPERGOB 33 /* # of Macroblocks per GOB */
65
66
67 #if BYTE_ORDER == LITTLE_ENDIAN
68 #if NBIT == 64
69 #define STORE_BITS(bb, bc) \
70 bc[0] = bb >> 56; \
71 bc[1] = bb >> 48; \
72 bc[2] = bb >> 40; \
73 bc[3] = bb >> 32; \
74 bc[4] = bb >> 24; \
75 bc[5] = bb >> 16; \
76 bc[6] = bb >> 8; \
77 bc[7] = bb;
78 #define LOAD_BITS(bc) \
79 ((BB_INT)bc[0] << 56 | \
80 (BB_INT)bc[1] << 48 | \
81 (BB_INT)bc[2] << 40 | \
82 (BB_INT)bc[3] << 32 | \
83 (BB_INT)bc[4] << 24 | \
84 (BB_INT)bc[5] << 16 | \
85 (BB_INT)bc[6] << 8 | \
86 (BB_INT)bc[7])
87 #else
88 #define STORE_BITS(bb, bc) \
89 bc[0] = bb >> 24; \
90 bc[1] = bb >> 16; \
91 bc[2] = bb >> 8; \
92 bc[3] = bb;
93 #define LOAD_BITS(bc) (ntohl(*(BB_INT*)(bc)))
94 #endif
95 #else
96 #define STORE_BITS(bb, bc) *(BB_INT*)bc = (bb);
97 #define LOAD_BITS(bc) (*(BB_INT*)(bc))
98 #endif
99
100 #define PUT_BITS(bits, n, nbb, bb, bc) \
101 { \
102 nbb += (n); \
103 if (nbb > NBIT) { \
104 u_int extra = (nbb) - NBIT; \
105 bb |= (BB_INT)(bits) >> extra; \
106 STORE_BITS(bb, bc) \
107 bc += sizeof(BB_INT); \
108 bb = (BB_INT)(bits) << (NBIT - extra); \
109 nbb = extra; \
110 } else \
111 bb |= (BB_INT)(bits) << (NBIT - (nbb)); \
112 }
113
114
115
116 H261Encoder::H261Encoder() :
117 bs_(0), bc_(0), ngob_(12)
118 {
119 for (int q = 0; q < 32; ++q) {
120 llm_[q] = 0;
121 clm_[q] = 0;
122 }
123 }
124
125 H261Encoder::~H261Encoder()
126 {
127 for (int q = 0; q < 32; ++q) {
128 if (llm_[q] != 0) delete[] llm_[q];
129 if (clm_[q] != 0) delete[] clm_[q];
130 }
131 }
132
133 H261PixelEncoder::H261PixelEncoder() : H261Encoder()
134 {
135 quant_required_ = 0;
136 setq(10);
137 }
138
139 H261DCTEncoder::H261DCTEncoder() : H261Encoder()
140 {
141 quant_required_ = 1;
142 setq(10);
143 }
144
145 /*
146 * Set up the forward DCT quantization table for
147 * INTRA mode operation.
148 */
149 void
150 H261Encoder::setquantizers(int lq, int mq, int hq)
151 {
152 int qt[64];
153 if (lq > 31)
154 lq = 31;
155 if (lq <= 0)
156 lq = 1;
157 lq_ = lq;
158
159 if (mq > 31)
160 mq = 31;
161 if (mq <= 0)
162 mq = 1;
163 mq_ = mq;
164
165 if (hq > 31)
166 hq = 31;
167 if (hq <= 0)
168 hq = 1;
169 hq_ = hq;
170
171 /*
172 * quant_required_ indicates quantization is not folded
173 * into fdct [because fdct is not performed]
174 */
175 if (quant_required_ == 0) {
176 /*
177 * Set the DC quantizer to 1, since we want to do this
178 * coefficient differently (i.e., the DC is rounded while
179 * the AC terms are truncated).
180 */
181 qt[0] = 1;
182 int i;
183 for (i = 1; i < 64; ++i)
184 qt[i] = lq_ << 1;
185 fdct_fold_q(qt, lqt_);
186
187 qt[0] = 1;
188 for (i = 1; i < 64; ++i)
189 qt[i] = mq_ << 1;
190 fdct_fold_q(qt, mqt_);
191
192 qt[0] = 1;
193 for (i = 1; i < 64; ++i)
194 qt[i] = hq_ << 1;
195 fdct_fold_q(qt, hqt_);
196 }
197 }
198
199 void
200 H261Encoder::setq(int q)
201 {
202 setquantizers(q, q / 2, 1);
203 }
204
205 void
206 H261PixelEncoder::size(int w, int h)
207 {
208 FrameModule::size(w, h);
209 if (w == CIF_WIDTH && h == CIF_HEIGHT) {
210 /* CIF */
211 cif_ = 1;
212 ngob_ = 12;
213 bstride_ = 11;
214 lstride_ = 16 * CIF_WIDTH - CIF_WIDTH / 2;
215 cstride_ = 8 * 176 - 176 / 2;
216 loffsize_ = 16;
217 coffsize_ = 8;
218 bloffsize_ = 1;
219 } else if (w == QCIF_WIDTH && h == QCIF_HEIGHT) {
220 /* QCIF */
221 cif_ = 0;
222 ngob_ = 6; /* not really number of GOBs, just loop limit */
223 bstride_ = 0;
224 lstride_ = 16 * QCIF_WIDTH - QCIF_WIDTH;
225 cstride_ = 8 * 88 - 88;
226 loffsize_ = 16;
227 coffsize_ = 8;
228 bloffsize_ = 1;
229 } else {
230 /*FIXME*/
231 fprintf(stderr, "H261PixelEncoder: H.261 bad geometry: %dx%d\n",
232 w, h);
233 exit(1);
234 }
235 u_int loff = 0;
236 u_int coff = 0;
237 u_int blkno = 0;
238 for (u_int gob = 0; gob < ngob_; gob += 2) {
239 loff_[gob] = loff;
240 coff_[gob] = coff;
241 blkno_[gob] = blkno;
242 /* width of a GOB (these aren't ref'd in QCIF case) */
243 loff_[gob + 1] = loff + 11 * 16;
244 coff_[gob + 1] = coff + 11 * 8;
245 blkno_[gob + 1] = blkno + 11;
246
247 /* advance to next GOB row */
248 loff += (16 * 16 * MBPERGOB) << cif_;
249 coff += (8 * 8 * MBPERGOB) << cif_;
250 blkno += MBPERGOB << cif_;
251 }
252 }
253
254 void
255 H261DCTEncoder::size(int w, int h)
256 {
257 FrameModule::size(w, h);
258 if (w == CIF_WIDTH && h == CIF_HEIGHT) {
259 /* CIF */
260 cif_ = 1;
261 ngob_ = 12;
262 bstride_ = 11;
263 lstride_ = - (11 * (64*BMB)) + 2 * 11 * 64 * BMB;
264 cstride_ = - (11 * (64*BMB)) + 2 * 11 * 64 * BMB;
265 loffsize_ = 64 * BMB;
266 coffsize_ = 64 * BMB;
267 bloffsize_ = 1;
268 } else if (w == QCIF_WIDTH && h == QCIF_HEIGHT) {
269 /* QCIF */
270 cif_ = 0;
271 ngob_ = 6; /* not really number of GOBs, just loop limit */
272 bstride_ = 0;
273 lstride_ = 0;
274 cstride_ = 0;
275 loffsize_ = 64 * BMB;
276 coffsize_ = 64 * BMB;
277 bloffsize_ = 1;
278 } else {
279 /*FIXME*/
280 fprintf(stderr, "H261DCTEncoder: H.261 bad geometry: %dx%d\n",
281 w, h);
282 exit(1);
283 }
284
285 u_int gob;
286 for (gob = 0; gob < ngob_; gob += 2) {
287
288 if (gob != 0) {
289 loff_[gob] = loff_[gob-2] +
290 (MBPERGOB << cif_) * BMB * 64;
291 coff_[gob] = coff_[gob-2] +
292 (MBPERGOB << cif_) * BMB * 64;
293 blkno_[gob] = blkno_[gob-2] +
294 (MBPERGOB << cif_);
295 } else {
296 loff_[0] = 0;
297 coff_[0] = loff_[0] + 4 * 64; // 4 Y's
298 blkno_[0] = 0;
299 }
300
301 loff_[gob + 1] = loff_[gob] + 11 * BMB * 64;
302 coff_[gob + 1] = coff_[gob] + 11 * BMB * 64;
303 blkno_[gob + 1] = blkno_[gob] + 11;
304 }
305 }
306
307
308 int
309 H261Encoder::command(int argc, const char*const* argv)
310 {
311 // let the encoder choose the color-subsampling scheme
312 if (argc == 2 && strcmp(argv[1], "frame-format") == 0) {
313 Tcl& tcl = Tcl::instance();
314 tcl.result("cif");
315 //tcl.result("420");
316 return (TCL_OK);
317 }
318
319 if (argc == 3 && strcmp(argv[1], "q") == 0) {
320 setq(atoi(argv[2]));
321 return (TCL_OK);
322 }
323 return (EncoderModule::command(argc, argv));
324 }
325
326 /*
327 * Make a map to go from a 12 bit dct value to an 8 bit quantized
328 * 'level' number. The 'map' includes both the quantizer (for the
329 * dct encoder) and the perceptual filter 'threshold' (for both
330 * the pixel & dct encoders). The first 4k of the map is for the
331 * unfiltered coeff (the first 20 in zigzag order; roughly the
332 * upper left quadrant) and the next 4k of the map are for the
333 * filtered coef.
334 */
335 char*
336 H261Encoder::make_level_map(int q, u_int fthresh)
337 {
338 /* make the luminance map */
339 char* lm = new char[0x2000];
340 char* flm = lm + 0x1000;
341 int i;
342 lm[0] = 0;
343 flm[0] = 0;
344 q = quant_required_? q << 1 : 0;
345 for (i = 1; i < 0x800; ++i) {
346 int l = i;
347 if (q)
348 l /= q;
349 lm[i] = l;
350 lm[-i & 0xfff] = -l;
351
352 if ((u_int)l <= fthresh)
353 l = 0;
354 flm[i] = l;
355 flm[-i & 0xfff] = -l;
356 }
357 return (lm);
358 }
359
360 /*
361 * encode_blk:
362 * encode a block of DCT coef's
363 */
364 void
365 H261Encoder::encode_blk(const short* blk, const char* lm)
366 {
367 BB_INT bb = bb_;
368 u_int nbb = nbb_;
369 u_char* bc = bc_;
370
371 /*
372 * Quantize DC. Round instead of truncate.
373 */
374 int dc = (blk[0] + 4) >> 3;
375
376 if (dc <= 0)
377 /* shouldn't happen with CCIR 601 black (level 16) */
378 dc = 1;
379 else if (dc > 254)
380 dc = 254;
381 else if (dc == 128)
382 /* per Table 6/H.261 */
383 dc = 255;
384 /* Code DC */
385 PUT_BITS(dc, 8, nbb, bb, bc);
386 int run = 0;
387 const u_char* colzag = &COLZAG[0];
388 for (int zag; (zag = *++colzag) != 0; ) {
389 if (colzag == &COLZAG[20])
390 lm += 0x1000;
391 int level = lm[((const u_short*)blk)[zag] & 0xfff];
392 if (level != 0) {
393 int val, nb;
394 huffent* he;
395 if (u_int(level + 15) <= 30 &&
396 (nb = (he = &hte_tc[((level&0x1f) << 6)|run])->nb))
397 /* we can use a VLC. */
398 val = he->val;
399 else {
400 /* Can't use a VLC. Escape it. */
401 val = (1 << 14) | (run << 8) | (level & 0xff);
402 nb = 20;
403 }
404 PUT_BITS(val, nb, nbb, bb, bc);
405 run = 0;
406 } else
407 ++run;
408 }
409 /* EOB */
410 PUT_BITS(2, 2, nbb, bb, bc);
411
412 bb_ = bb;
413 nbb_ = nbb;
414 bc_ = bc;
415 }
416
417 /*
418 * H261PixelEncoder::encode_mb
419 * encode a macroblock given a set of input YUV pixels
420 */
421 void
422 H261PixelEncoder::encode_mb(u_int mba, const u_char* frm,
423 u_int loff, u_int coff, int how)
424 {
425 register int q;
426 float* qt;
427 if (how == CR_LQ) {
428 q = lq_;
429 qt = lqt_;
430 } else if (how == CR_HQ) {
431 q = hq_;
432 qt = hqt_;
433 } else {
434 /* must be medium quality */
435 q = mq_;
436 qt = mqt_;
437 }
438
439 /*
440 * encode all 6 blocks of the macro block to find the largest
441 * coef (so we can pick a new quantizer if gquant doesn't have
442 * enough range).
443 */
444 /*FIXME this can be u_char instead of short but need smarts in fdct */
445 short blk[64 * 6];
446 register int stride = width_;
447 /* luminance */
448 const u_char* p = &frm[loff];
449 fdct(p, stride, blk + 0, qt);
450 fdct(p + 8, stride, blk + 64, qt);
451 fdct(p + 8 * stride, stride, blk + 128, qt);
452 fdct(p + (8 * stride + 8), stride, blk + 192, qt);
453 /* chominance */
454 int fs = framesize_;
455 p = &frm[fs + coff];
456 stride >>= 1;
457 fdct(p, stride, blk + 256, qt);
458 fdct(p + (fs >> 2), stride, blk + 320, qt);
459
460 /*
461 * if the default quantizer is too small to handle the coef.
462 * dynamic range, spin through the blocks and see if any
463 * coef. would significantly overflow.
464 */
465 if (q < 8) {
466 register int cmin = 0, cmax = 0;
467 register short* bp = blk;
468 for (register int i = 6; --i >= 0; ) {
469 ++bp; // ignore dc coef
470 for (register int j = 63; --j >= 0; ) {
471 register int v = *bp++;
472 if (v < cmin)
473 cmin = v;
474 else if (v > cmax)
475 cmax = v;
476 }
477 }
478 if (cmax < -cmin)
479 cmax = -cmin;
480 if (cmax >= 128) {
481 /* need to re-quantize */
482 register int s;
483 for (s = 1; cmax >= (128 << s); ++s) {
484 }
485 q <<= s;
486 register short* bp = blk;
487 for (register int i = 6; --i >= 0; ) {
488 ++bp; // ignore dc coef
489 for (register int j = 63; --j >= 0; ) {
490 register int v = *bp;
491 *bp++ = v >> s;
492 }
493 }
494 }
495 }
496
497 u_int m = mba - mba_;
498 mba_ = mba;
499 huffent* he = &hte_mba[m - 1];
500 /* MBA */
501 PUT_BITS(he->val, he->nb, nbb_, bb_, bc_);
502 if (q != mquant_) {
503 /* MTYPE = INTRA + TC + MQUANT */
504 PUT_BITS(1, 7, nbb_, bb_, bc_);
505 PUT_BITS(q, 5, nbb_, bb_, bc_);
506 mquant_ = q;
507 } else {
508 /* MTYPE = INTRA + TC (no quantizer) */
509 PUT_BITS(1, 4, nbb_, bb_, bc_);
510 }
511
512 /* luminance */
513 /*const*/ char* lm = llm_[q];
514 if (lm == 0) {
515 lm = make_level_map(q, 1);
516 llm_[q] = lm;
517 clm_[q] = make_level_map(q, 2);
518 }
519 encode_blk(blk + 0, lm);
520 encode_blk(blk + 64, lm);
521 encode_blk(blk + 128, lm);
522 encode_blk(blk + 192, lm);
523 /* chominance */
524 lm = clm_[q];
525 encode_blk(blk + 256, lm);
526 encode_blk(blk + 320, lm);
527 }
528
529
530 /*
531 * H261DCTEncoder::encode_mb
532 * encode a macroblock given a set of input DCT coefs
533 * each coef is stored as a short
534 */
535 void
536 H261DCTEncoder::encode_mb(u_int mba, const u_char* frm,
537 u_int loff, u_int coff, int how)
538 {
539 short *lblk = (short *)frm + loff;
540 short *ublk = (short *)frm + coff;
541 short *vblk = (short *)frm + coff + 64;
542
543 register u_int q;
544 if (how == CR_LQ)
545 q = lq_;
546 else if (how == CR_HQ)
547 q = hq_;
548 else
549 /* must be medium quality */
550 q = mq_;
551
552 /*
553 * if the default quantizer is too small to handle the coef.
554 * dynamic range, spin through the blocks and see if any
555 * coef. would significantly overflow.
556 */
557 if (q < 8) {
558 register int cmin = 0, cmax = 0;
559 register short* bp = lblk;
560 register int i, j;
561
562 // Y U and V blocks
563 for (i = 6; --i >= 0; ) {
564 ++bp; // ignore dc coef
565 for (j = 63; --j >= 0; ) {
566 register int v = *bp++;
567 if (v < cmin)
568 cmin = v;
569 else if (v > cmax)
570 cmax = v;
571 }
572 }
573
574 if (cmax < -cmin)
575 cmax = -cmin;
576 cmax /= (q << 1);
577 if (cmax >= 128) {
578 /* need to re-quantize */
579 register int s;
580
581 for (s = 1; cmax >= (128 << s); ++s) {
582 }
583 q <<= s;
584
585 }
586 }
587
588 u_int m = mba - mba_;
589 mba_ = mba;
590 huffent* he = &hte_mba[m - 1];
591 /* MBA */
592 PUT_BITS(he->val, he->nb, nbb_, bb_, bc_);
593 if (q != mquant_) {
594 /* MTYPE = INTRA + TC + MQUANT */
595 PUT_BITS(1, 7, nbb_, bb_, bc_);
596 PUT_BITS(q, 5, nbb_, bb_, bc_);
597 mquant_ = q;
598 } else {
599 /* MTYPE = INTRA + TC (no quantizer) */
600 PUT_BITS(1, 4, nbb_, bb_, bc_);
601 }
602
603 /* luminance */
604 /*const*/ char* lm = llm_[q];
605 if (lm == 0) {
606 /*
607 * the filter thresh is 0 since we assume the jpeg percept.
608 * quantizer already did the filtering.
609 */
610 lm = make_level_map(q, 0);
611 llm_[q] = lm;
612 clm_[q] = make_level_map(q, 0);
613 }
614 encode_blk(lblk + 0, lm);
615 encode_blk(lblk + 64, lm);
616 encode_blk(lblk + 128, lm);
617 encode_blk(lblk + 192, lm);
618 /* chominance */
619 lm = clm_[q];
620 encode_blk(ublk, lm);
621 encode_blk(vblk, lm);
622 }
623
624 int
625 H261Encoder::flush(pktbuf* pb, int nbit, pktbuf* npb)
626 {
627 /* flush bit buffer */
628 STORE_BITS(bb_, bc_);
629
630 int cc = (nbit + 7) >> 3;
631 int ebit = (cc << 3) - nbit;
632
633 /*FIXME*/
634 if (cc == 0 && npb != 0)
635 abort();
636
637 pb->len = cc + HLEN;
638 rtphdr* rh = (rtphdr*)pb->data;
639 if (npb == 0)
640 rh->rh_flags |= htons(RTP_M);
641
642 int h = *(u_int*)(rh + 1) | ebit << 26 | sbit_ << 29;
643 *(u_int*)(rh + 1) = htonl(h);
644
645 if (npb != 0) {
646 u_char* nbs = &npb->data[HLEN];
647 u_int bc = (bc_ - bs_) << 3;
648 int tbit = bc + nbb_;
649 int extra = ((tbit + 7) >> 3) - (nbit >> 3);
650 if (extra > 0)
651 memcpy(nbs, bs_ + (nbit >> 3), extra);
652 bs_ = nbs;
653 sbit_ = nbit & 7;
654 tbit -= nbit &~ 7;
655 bc = tbit &~ (NBIT - 1);
656 nbb_ = tbit - bc;
657 bc_ = bs_ + (bc >> 3);
658 /*
659 * Prime the bit buffer. Be careful to set bits that
660 * are not yet in use to 0, since output bits are later
661 * or'd into the buffer.
662 */
663 if (nbb_ > 0) {
664 u_int n = NBIT - nbb_;
665 bb_ = (LOAD_BITS(bc_) >> n) << n;
666 } else
667 bb_ = 0;
668 }
669 pb->attach();
670 target_->recv(pb);
671 Module * target;
672 target = target_list_->first_;
673 while (target != NULL) {
674 pb->attach();
675 target->recv(pb);
676 target = target->next();
677 }
678 pb->release();
679
680 return (cc + HLEN);
681 }
682
683 void H261DCTEncoder::recv(Buffer* bp)
684 {
685 const VideoFrame *vf = (VideoFrame*)bp;
686
687 if (!samesize(vf))
688 size(vf->width_, vf->height_);
689
690 DCTFrame* df = (DCTFrame *)vf;
691
692 encode(df, df->crvec_);
693 }
694
695 void H261PixelEncoder::recv(Buffer* bp)
696 {
697 const VideoFrame *vf = (VideoFrame*)bp;
698
699 if (!samesize(vf))
700 size(vf->width_, vf->height_);
701
702 YuvFrame* p = (YuvFrame*)vf;
703 encode(p, p->crvec_);
704 }
705
706 void H261Encoder::encode(const VideoFrame* vf, const u_int8_t *crvec)
707 {
708 pktbuf* pb = pool_->alloc(vf->ts_, RTP_PT_H261);
709 bs_ = &pb->data[HLEN];
710 bc_ = bs_;
711 u_int ec = (mtu_ - HLEN) << 3;
712 bb_ = 0;
713 nbb_ = 0;
714 sbit_ = 0;
715 /* RTP/H.261 header */
716 rtphdr* rh = (rtphdr*)pb->data;
717 *(u_int*)(rh + 1) = 1 << 25 | lq_ << 10;
718
719 /* PSC */
720 PUT_BITS(0x0001, 16, nbb_, bb_, bc_);
721 /* GOB 0 -> picture header */
722 PUT_BITS(0, 4, nbb_, bb_, bc_);
723 /* TR (FIXME should do this right) */
724 PUT_BITS(0, 5, nbb_, bb_, bc_);
725 /* PTYPE = CIF */
726 int pt = cif_ ? 6 : 2;
727 PUT_BITS(pt, 6, nbb_, bb_, bc_);
728 /* PEI */
729 PUT_BITS(0, 1, nbb_, bb_, bc_);
730
731 int step = cif_ ? 1 : 2;
732
733 u_int8_t* frm = vf->bp_;
734 for (u_int gob = 0; gob < ngob_; gob += step) {
735 u_int loff = loff_[gob];
736 u_int coff = coff_[gob];
737 u_int blkno = blkno_[gob];
738 u_int nbit = ((bc_ - bs_) << 3) + nbb_;
739
740 /* GSC/GN */
741 PUT_BITS(0x10 | (gob + 1), 20, nbb_, bb_, bc_);
742 /* GQUANT/GEI */
743 mquant_ = lq_;
744 PUT_BITS(mquant_ << 1, 6, nbb_, bb_, bc_);
745
746 mba_ = 0;
747 int line = 11;
748 for (u_int mba = 1; mba <= 33; ++mba) {
749 /*
750 * If the conditional replenishment algorithm
751 * has decided to send any of the blocks of
752 * this macroblock, code it.
753 */
754 u_int s = crvec[blkno];
755 if ((s & CR_SEND) != 0) {
756 u_int mbpred = mba_;
757 encode_mb(mba, frm, loff, coff, CR_QUALITY(s));
758 u_int cbits = ((bc_ - bs_) << 3) + nbb_;
759 if (cbits > ec) {
760 pktbuf* npb;
761 npb = pool_->alloc(vf->ts_, RTP_PT_H261);
762 nb_ += flush(pb, nbit, npb);
763 cbits -= nbit;
764 pb = npb;
765 /* RTP/H.261 header */
766 u_int m = mbpred;
767 u_int g;
768 if (m != 0) {
769 g = gob + 1;
770 m -= 1;
771 } else
772 g = 0;
773
774 rh = (rtphdr*)pb->data;
775 *(u_int*)(rh + 1) =
776 1 << 25 |
777 m << 15 |
778 g << 20 |
779 mquant_ << 10;
780 }
781 nbit = cbits;
782 }
783
784 loff += loffsize_;
785 coff += coffsize_;
786 blkno += bloffsize_;
787 if (--line <= 0) {
788 line = 11;
789 blkno += bstride_;
790 loff += lstride_;
791 coff += cstride_;
792 }
793
794 }
795 }
796 nb_ += flush(pb, ((bc_ - bs_) << 3) + nbb_, 0);
797 }
798
799 #ifdef __PSVP_ENABLED__
800
801 class UncompressedToH261Encoder : public H261PixelEncoder, public ConditionalReplenisher {
802 public:
803 UncompressedToH261Encoder() : H261PixelEncoder(), ConditionalReplenisher(), frame_data_(0) {frame_ = new YuvFrame(0,0,0,0,0, 420);};
804 void recv(Buffer *buffer);
805 void recv(Uncompressed *fb);
806 virtual int command(int argc, const char*const* argv);
807
808 private:
809 YuvFrame *frame_;
810 u_int8_t* frame_data_;
811 };
812
813 static class UncompressedToH261EncoderClass : public TclClass {
814 public:
815 UncompressedToH261EncoderClass() : TclClass("Module/VideoEncoder/UncompressedToH261") {}
816 TclObject* create(int argc, const char*const* argv) {
817 return (new UncompressedToH261Encoder);
818 }
819 } uncompressed_to_h261_class_;
820
821
822 void
823 UncompressedToH261Encoder::recv(Uncompressed *fb)
824 {
825 int y;
826
827 if ((fb->w_ != frame_->width_) ||
828 (fb->h_ != frame_->height_)) {
829
830 /* Frame size changed. Reallocate frame data space and reinit crvec */
831
832 if (frame_data_ != 0) {
833 delete [] frame_data_;
834 }
835 frame_data_ = new u_int8_t[fb->w_*fb->h_*3/2];
836
837 if (fb->lum_ != 0) {
838 if (fb->lum_->firstByte != 0) {
839 memcpy(frame_data_, fb->lum_->firstByte,
840 fb->w_*fb->h_);
841 }
842 }
843
844 if (fb->cr_ != 0) {
845 if (fb->cr_->firstByte != 0) {
846 memcpy(frame_data_+fb->w_*fb->h_,
847 fb->cr_->firstByte, fb->w_*fb->h_/4);
848 }
849 }
850
851 if (fb->cb_ != 0) {
852 if (fb->cb_->firstByte != 0) {
853 memcpy(frame_data_+fb->w_*fb->h_*5/4,
854 fb->cb_->firstByte, fb->w_*fb->h_/4);
855 }
856 }
857
858 crinit(fb->w_, fb->h_);
859
860 frame_->crvec_ = crvec_;
861 frame_->bp_ = frame_data_;
862 frame_->width_ = fb->w_;
863 frame_->height_ = fb->h_;
864 frame_->layer_ = 0;
865 } else {
866 /* Frame size is the same, so do conditional replenishment. */
867
868 int mark = age_blocks() | CR_MOTION_BIT | CR_LQ;
869
870 register int _stride = fb->w_;
871
872 const u_char* rb = &(frame_data_[scan_ * _stride]);
873 const u_char* lb = &(fb->lum_->firstByte[scan_ * _stride]);
874
875 u_char* crv = crvec_;
876
877 int bw = frame_->width_/16;
878 int bh = frame_->height_/16;
879
880 for (y = 0; y < bh; y++) {
881 const u_char* nrb = rb;
882 const u_char* nlb = lb;
883 u_char* ncrv = crv;
884
885 for (int x = 0; x < bw; x++) {
886 int tl = 0;
887 int tc1 = 0;
888 int tc2 = 0;
889 int tr = 0;
890 int bl = 0;
891 int bc1 = 0;
892 int bc2 = 0;
893 int br = 0;
894
895 tl = lb[0] - rb[0] + lb[1] - rb[1] + lb[2] - rb[2] + lb[3] - rb[3];
896 if (tl < 0) tl = -tl;
897
898 tc1 = lb[4] - rb[4] + lb[5] - rb[5] + lb[6] - rb[6] + lb[7] - rb[7];
899 if (tc1 < 0) tc1 = -tc1;
900
901 tc2 = lb[8] - rb[8] + lb[9] - rb[9] + lb[10] - rb[10] + lb[11] -rb[11];
902 if (tc2 < 0) tc2 = -tc2;
903
904 tr = lb[12] - rb[12] + lb[13] - rb[13] + lb[14] - rb[14] +
905 lb[15] - rb[15];
906 if (tr < 0) tr = -tr;
907
908 lb += _stride << 3;
909 rb += _stride << 3;
910
911 bl = lb[0] - rb[0] + lb[1] - rb[1] + lb[2] - rb[2] + lb[3] - rb[3];
912 if (bl < 0) bl = -bl;
913
914 bc1 = lb[4] - rb[4] + lb[5] - rb[5] + lb[6] - rb[6] + lb[7] - rb[7];
915 if (bc1 < 0) bc1 = -bc1;
916
917 bc2 = lb[8] - rb[8] + lb[9] - rb[9] + lb[10] - rb[10] + lb[11] -rb[11];
918 if (bc2 < 0) bc2 = -bc2;
919
920 br = lb[12] - rb[12] + lb[13] - rb[13] + lb[14] - rb[14] +
921 lb[15] - rb[15];
922 if (br < 0) br = -br;
923
924 lb -= _stride << 3;
925 rb -= _stride << 3;
926
927 if (scan_ < 4) {
928 /* north-west */
929 if ((tl >= 24) && (x > 0) && (y > 0)) {
930 crv[-bw-1] = mark;
931 }
932 /* north */
933 if (((tl >= 24) || (tc1 >= 24) || (tc2 >= 24) || (tr >= 24)) &&
934 (y > 0)) {
935 crv[-bw] = mark;
936 }
937 /* north-east */
938 if ((tr >= 24) && (x < bw - 1) && (y > 0)) {
939 crv[-bw+1] = mark;
940 }
941 /* west */
942 if (((tl >= 24) || (bl >= 24)) && (x > 0)) {
943 crv[-1] = mark;
944 }
945 /* middle */
946 if ((tl >= 24) || (tc1 >= 24) || (tc2 >= 24) || (tr >= 24) ||
947 (bl >= 24) || (bc1 >= 24) || (bc2 >= 24) || (br >= 24)) {
948 crv[0] = mark;
949 }
950 /* east */
951 if (((tr >= 24) || (br >=24)) && (x < bw - 1)) {
952 crv[1] = 0;
953 }
954 } else {
955 /* south-west */
956 if ((bl >= 24) && (x > 0) && (y < bh-1)) {
957 crv[bw-1] = mark;
958 }
959 /* south */
960 if (((bl >= 24) || (bc1 >= 24) || (bc2 >= 24) || (br >= 24)) &&
961 (y < bh-1)) {
962 crv[bw] = mark;
963 }
964 /* south-east */
965 if ((br >= 24) && (x < bw - 1) && (y < bh - 1)) {
966 crv[bw+1] = mark;
967 }
968 /* west */
969 if (((bl >= 24) || (tl >= 24)) && (x > 0)) {
970 crv[-1] = mark;
971 }
972 /* middle */
973 if ((bl >= 24) || (bc1 >= 24) || (bc2 >= 24) || (br >= 24) ||
974 (tl >= 24) || (tc1 >= 24) || (tc2 >= 24) || (tr >= 24)) {
975 crv[0] = mark;
976 }
977 /* east */
978 if (((br >= 24) || (tr >=24)) && (x < bw - 1)) {
979 crv[1] = 0;
980 }
981 }
982 lb += 16;
983 rb += 16;
984 crv++;
985 }
986 lb = nlb + (_stride << 4);
987 rb = nrb + (_stride << 4);
988 crv = ncrv + bw;
989 }
990
991 /* Copy blocks into frame based on conditional replenishment */
992
993 crv = crvec_;
994 int off = frame_->width_ * frame_->height_;
995 u_char* dest_lum = frame_data_;
996 u_char* dest_cr = frame_data_+off;
997 u_char* dest_cb = frame_data_+off+(off/4);
998 u_char* src_lum = fb->lum_->firstByte;
999 u_char* src_cr = fb->cr_->firstByte;
1000 u_char* src_cb = fb->cb_->firstByte;
1001
1002 for (y = 0; y < bh; y++) {
1003 int i;
1004 for (int x = 0; x < bw; x++) {
1005 int s = *crv++;
1006 if ((s & CR_SEND) != 0) {
1007 int idx = y*_stride*16+x*16;
1008 u_int32_t* sl = (u_int32_t*) &(src_lum[idx]);
1009 u_int32_t* dl = (u_int32_t*) &(dest_lum[idx]);
1010 for(i=0; i<16; i++) {
1011 dl[0] = sl[0];
1012 dl[1] = sl[1];
1013 dl[2] = sl[2];
1014 dl[3] = sl[3];
1015 dl += (_stride / 4);
1016 sl += (_stride / 4);
1017 }
1018
1019 idx = y*(_stride/2)*8+x*8;
1020 u_int32_t* scr = (u_int32_t*) &(src_cr[idx]);
1021 u_int32_t* scb = (u_int32_t*) &(src_cb[idx]);
1022 u_int32_t* dcr = (u_int32_t*) &(dest_cr[idx]);
1023 u_int32_t* dcb = (u_int32_t*) &(dest_cb[idx]);
1024 for(i=0; i<8; i++) {
1025 dcr[0] = scr[0];
1026 dcr[1] = scr[1];
1027 dcb[0] = scb[0];
1028 dcb[1] = scb[1];
1029 dcr += _stride / 8;
1030 dcb += _stride / 8;
1031 scr += _stride / 8;
1032 scb += _stride / 8;
1033 }
1034 }
1035 }
1036 }
1037 }
1038 frame_->ts_ = fb->ts_;
1039
1040 H261PixelEncoder::recv((Buffer *) frame_);
1041 }
1042
1043 void
1044 UncompressedToH261Encoder::recv(Buffer *buffer)
1045 {
1046 recv((Uncompressed *) buffer);
1047 return;
1048 }
1049
1050 int
1051 UncompressedToH261Encoder::command(int argc, const char*const* argv)
1052 {
1053 if (argc == 3 && strcmp(argv[1], "recv") == 0) {
1054 Uncompressed *input = (Uncompressed *) TclObject::lookup(argv[2]);
1055 if (input !=0 ) {
1056 recv(input);
1057 }
1058 return (TCL_OK);
1059 }
1060 return (H261PixelEncoder::command(argc, argv));
1061 }
1062
1063 #endif
1064
1065
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.