added skip macroblock optimization (big perf win on black regions for example)
[libav.git] / libavcodec / h263.c
1 /*
2 * H263/MPEG4 backend for ffmpeg encoder and decoder
3 * Copyright (c) 2000,2001 Gerard Lantau.
4 * H263+ support for custom picture format.
5 * Copyright (c) 2001 Juan J. Sierralta P.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "common.h"
25 #include "dsputil.h"
26 #include "avcodec.h"
27 #include "mpegvideo.h"
28 #include "h263data.h"
29 #include "mpeg4data.h"
30
31 #define NDEBUG
32 #include <assert.h>
33
34 static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
35 int n);
36 static void h263_encode_motion(MpegEncContext * s, int val);
37 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
38 int n);
39 static int h263_decode_motion(MpegEncContext * s, int pred);
40 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
41 int n, int coded);
42 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
43 int n, int coded);
44
45 int h263_get_picture_format(int width, int height)
46 {
47 int format;
48
49 if (width == 128 && height == 96)
50 format = 1;
51 else if (width == 176 && height == 144)
52 format = 2;
53 else if (width == 352 && height == 288)
54 format = 3;
55 else if (width == 704 && height == 576)
56 format = 4;
57 else if (width == 1408 && height == 1152)
58 format = 5;
59 else
60 format = 7;
61 return format;
62 }
63
64 void h263_encode_picture_header(MpegEncContext * s, int picture_number)
65 {
66 int format, umvplus;
67
68 align_put_bits(&s->pb);
69 put_bits(&s->pb, 22, 0x20);
70 put_bits(&s->pb, 8, ((s->picture_number * 30 * FRAME_RATE_BASE) /
71 s->frame_rate) & 0xff);
72
73 put_bits(&s->pb, 1, 1); /* marker */
74 put_bits(&s->pb, 1, 0); /* h263 id */
75 put_bits(&s->pb, 1, 0); /* split screen off */
76 put_bits(&s->pb, 1, 0); /* camera off */
77 put_bits(&s->pb, 1, 0); /* freeze picture release off */
78
79 if (!s->h263_plus) {
80 /* H.263v1 */
81 format = h263_get_picture_format(s->width, s->height);
82 put_bits(&s->pb, 3, format);
83 put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
84 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
85 of H.263v1 UMV implies to check the predicted MV after
86 calculation of the current MB to see if we're on the limits */
87 put_bits(&s->pb, 1, 0); /* unrestricted motion vector: off */
88 put_bits(&s->pb, 1, 0); /* SAC: off */
89 put_bits(&s->pb, 1, 0); /* advanced prediction mode: off */
90 put_bits(&s->pb, 1, 0); /* not PB frame */
91 put_bits(&s->pb, 5, s->qscale);
92 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
93 } else {
94 /* H.263v2 */
95 /* H.263 Plus PTYPE */
96 put_bits(&s->pb, 3, 7);
97 put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */
98 put_bits(&s->pb,3,6); /* Custom Source Format */
99 put_bits(&s->pb,1,0); /* Custom PCF: off */
100 umvplus = (s->pict_type == P_TYPE) && s->unrestricted_mv;
101 put_bits(&s->pb, 1, umvplus); /* Unrestricted Motion Vector */
102 put_bits(&s->pb,1,0); /* SAC: off */
103 put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */
104 put_bits(&s->pb,1,0); /* Advanced Intra Coding: off */
105 put_bits(&s->pb,1,0); /* Deblocking Filter: off */
106 put_bits(&s->pb,1,0); /* Slice Structured: off */
107 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
108 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
109 put_bits(&s->pb,1,0); /* Alternative Inter VLC: off */
110 put_bits(&s->pb,1,0); /* Modified Quantization: off */
111 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
112 put_bits(&s->pb,3,0); /* Reserved */
113
114 put_bits(&s->pb, 3, s->pict_type == P_TYPE);
115
116 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
117 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
118 put_bits(&s->pb,1,0); /* Rounding Type */
119 put_bits(&s->pb,2,0); /* Reserved */
120 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
121
122 /* This should be here if PLUSPTYPE */
123 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
124
125 /* Custom Picture Format (CPFMT) */
126
127 put_bits(&s->pb,4,2); /* Aspect ratio: CIF 12:11 (4:3) picture */
128 put_bits(&s->pb,9,(s->width >> 2) - 1);
129 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
130 put_bits(&s->pb,9,(s->height >> 2));
131 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
132 if (umvplus)
133 put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
134 put_bits(&s->pb, 5, s->qscale);
135 }
136
137 put_bits(&s->pb, 1, 0); /* no PEI */
138 }
139
140 void h263_encode_mb(MpegEncContext * s,
141 DCTELEM block[6][64],
142 int motion_x, int motion_y)
143 {
144 int cbpc, cbpy, i, cbp, pred_x, pred_y;
145
146 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
147 if (!s->mb_intra) {
148 /* compute cbp */
149 cbp = 0;
150 for (i = 0; i < 6; i++) {
151 if (s->block_last_index[i] >= 0)
152 cbp |= 1 << (5 - i);
153 }
154 if ((cbp | motion_x | motion_y) == 0) {
155 /* skip macroblock */
156 put_bits(&s->pb, 1, 1);
157 return;
158 }
159 put_bits(&s->pb, 1, 0); /* mb coded */
160 cbpc = cbp & 3;
161 put_bits(&s->pb,
162 inter_MCBPC_bits[cbpc],
163 inter_MCBPC_code[cbpc]);
164 cbpy = cbp >> 2;
165 cbpy ^= 0xf;
166 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
167
168 /* motion vectors: 16x16 mode only now */
169 h263_pred_motion(s, 0, &pred_x, &pred_y);
170
171 h263_encode_motion(s, motion_x - pred_x);
172 h263_encode_motion(s, motion_y - pred_y);
173 } else {
174 /* compute cbp */
175 cbp = 0;
176 for (i = 0; i < 6; i++) {
177 if (s->block_last_index[i] >= 1)
178 cbp |= 1 << (5 - i);
179 }
180
181 cbpc = cbp & 3;
182 if (s->pict_type == I_TYPE) {
183 put_bits(&s->pb,
184 intra_MCBPC_bits[cbpc],
185 intra_MCBPC_code[cbpc]);
186 } else {
187 put_bits(&s->pb, 1, 0); /* mb coded */
188 put_bits(&s->pb,
189 inter_MCBPC_bits[cbpc + 4],
190 inter_MCBPC_code[cbpc + 4]);
191 }
192 if (s->h263_pred) {
193 /* XXX: currently, we do not try to use ac prediction */
194 put_bits(&s->pb, 1, 0); /* no ac prediction */
195 }
196 cbpy = cbp >> 2;
197 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
198 }
199
200 /* encode each block */
201 if (s->h263_pred) {
202 for (i = 0; i < 6; i++) {
203 mpeg4_encode_block(s, block[i], i);
204 }
205 } else {
206 for (i = 0; i < 6; i++) {
207 h263_encode_block(s, block[i], i);
208 }
209 }
210 }
211
212 static inline int mid_pred(int a, int b, int c)
213 {
214 int vmin, vmax;
215 vmin = a;
216 if (b < vmin)
217 vmin = b;
218 if (c < vmin)
219 vmin = c;
220
221 vmax = a;
222 if (b > vmax)
223 vmax = b;
224 if (c > vmax)
225 vmax = c;
226
227 return a + b + c - vmin - vmax;
228 }
229
230 INT16 *h263_pred_motion(MpegEncContext * s, int block,
231 int *px, int *py)
232 {
233 int x, y, wrap;
234 INT16 *A, *B, *C, *mot_val;
235
236 x = 2 * s->mb_x + 1 + (block & 1);
237 y = 2 * s->mb_y + 1 + ((block >> 1) & 1);
238 wrap = 2 * s->mb_width + 2;
239
240 mot_val = s->motion_val[(x) + (y) * wrap];
241
242 /* special case for first line */
243 if (y == 1 || s->first_slice_line) {
244 A = s->motion_val[(x-1) + (y) * wrap];
245 *px = A[0];
246 *py = A[1];
247 } else {
248 switch(block) {
249 default:
250 case 0:
251 A = s->motion_val[(x-1) + (y) * wrap];
252 B = s->motion_val[(x) + (y-1) * wrap];
253 C = s->motion_val[(x+2) + (y-1) * wrap];
254 break;
255 case 1:
256 case 2:
257 A = s->motion_val[(x-1) + (y) * wrap];
258 B = s->motion_val[(x) + (y-1) * wrap];
259 C = s->motion_val[(x+1) + (y-1) * wrap];
260 break;
261 case 3:
262 A = s->motion_val[(x-1) + (y) * wrap];
263 B = s->motion_val[(x-1) + (y-1) * wrap];
264 C = s->motion_val[(x) + (y-1) * wrap];
265 break;
266 }
267 *px = mid_pred(A[0], B[0], C[0]);
268 *py = mid_pred(A[1], B[1], C[1]);
269 }
270 return mot_val;
271 }
272
273
274 static void h263_encode_motion(MpegEncContext * s, int val)
275 {
276 int range, l, m, bit_size, sign, code, bits;
277
278 if (val == 0) {
279 /* zero vector */
280 code = 0;
281 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
282 } else {
283 bit_size = s->f_code - 1;
284 range = 1 << bit_size;
285 /* modulo encoding */
286 l = range * 32;
287 m = 2 * l;
288 if (val < -l) {
289 val += m;
290 } else if (val >= l) {
291 val -= m;
292 }
293
294 if (val >= 0) {
295 val--;
296 code = (val >> bit_size) + 1;
297 bits = val & (range - 1);
298 sign = 0;
299 } else {
300 val = -val;
301 val--;
302 code = (val >> bit_size) + 1;
303 bits = val & (range - 1);
304 sign = 1;
305 }
306
307 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
308 if (bit_size > 0) {
309 put_bits(&s->pb, bit_size, bits);
310 }
311 }
312 }
313
314 void h263_encode_init_vlc(MpegEncContext *s)
315 {
316 static int done = 0;
317
318 if (!done) {
319 done = 1;
320 init_rl(&rl_inter);
321 init_rl(&rl_intra);
322 }
323 }
324
325 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
326 {
327 int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
328 int code;
329 RLTable *rl = &rl_inter;
330
331 if (s->mb_intra) {
332 /* DC coef */
333 level = block[0];
334 /* 255 cannot be represented, so we clamp */
335 if (level > 254) {
336 level = 254;
337 block[0] = 254;
338 }
339 if (level == 128)
340 put_bits(&s->pb, 8, 0xff);
341 else
342 put_bits(&s->pb, 8, level & 0xff);
343 i = 1;
344 } else {
345 i = 0;
346 }
347
348 /* AC coefs */
349 last_index = s->block_last_index[n];
350 last_non_zero = i - 1;
351 for (; i <= last_index; i++) {
352 j = zigzag_direct[i];
353 level = block[j];
354 if (level) {
355 run = i - last_non_zero - 1;
356 last = (i == last_index);
357 sign = 0;
358 slevel = level;
359 if (level < 0) {
360 sign = 1;
361 level = -level;
362 }
363 code = get_rl_index(rl, last, run, level);
364 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
365 if (code == rl->n) {
366 put_bits(&s->pb, 1, last);
367 put_bits(&s->pb, 6, run);
368 put_bits(&s->pb, 8, slevel & 0xff);
369 } else {
370 put_bits(&s->pb, 1, sign);
371 }
372 last_non_zero = i;
373 }
374 }
375 }
376
377 /***************************************************/
378
379 /* write mpeg4 VOP header */
380 void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
381 {
382 align_put_bits(&s->pb);
383
384 put_bits(&s->pb, 32, 0x1B6); /* vop header */
385 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
386 /* XXX: time base + 1 not always correct */
387 put_bits(&s->pb, 1, 1);
388 put_bits(&s->pb, 1, 0);
389
390 put_bits(&s->pb, 1, 1); /* marker */
391 put_bits(&s->pb, 4, 1); /* XXX: correct time increment */
392 put_bits(&s->pb, 1, 1); /* marker */
393 put_bits(&s->pb, 1, 1); /* vop coded */
394 if (s->pict_type == P_TYPE) {
395 s->no_rounding = 0;
396 put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
397 }
398 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
399
400 put_bits(&s->pb, 5, s->qscale);
401
402 if (s->pict_type != I_TYPE)
403 put_bits(&s->pb, 3, s->f_code); /* fcode_for */
404 // printf("****frame %d\n", picture_number);
405 }
406
407 void h263_dc_scale(MpegEncContext * s)
408 {
409 int quant;
410
411 quant = s->qscale;
412 /* luminance */
413 if (quant < 5)
414 s->y_dc_scale = 8;
415 else if (quant > 4 && quant < 9)
416 s->y_dc_scale = (2 * quant);
417 else if (quant > 8 && quant < 25)
418 s->y_dc_scale = (quant + 8);
419 else
420 s->y_dc_scale = (2 * quant - 16);
421 /* chrominance */
422 if (quant < 5)
423 s->c_dc_scale = 8;
424 else if (quant > 4 && quant < 25)
425 s->c_dc_scale = ((quant + 13) / 2);
426 else
427 s->c_dc_scale = (quant - 6);
428 }
429
430 static int mpeg4_pred_dc(MpegEncContext * s, int n, UINT16 **dc_val_ptr, int *dir_ptr)
431 {
432 int a, b, c, x, y, wrap, pred, scale;
433 UINT16 *dc_val;
434
435 /* find prediction */
436 if (n < 4) {
437 x = 2 * s->mb_x + 1 + (n & 1);
438 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
439 wrap = s->mb_width * 2 + 2;
440 dc_val = s->dc_val[0];
441 scale = s->y_dc_scale;
442 } else {
443 x = s->mb_x + 1;
444 y = s->mb_y + 1;
445 wrap = s->mb_width + 2;
446 dc_val = s->dc_val[n - 4 + 1];
447 scale = s->c_dc_scale;
448 }
449
450 /* B C
451 * A X
452 */
453 a = dc_val[(x - 1) + (y) * wrap];
454 b = dc_val[(x - 1) + (y - 1) * wrap];
455 c = dc_val[(x) + (y - 1) * wrap];
456
457 if (abs(a - b) < abs(b - c)) {
458 pred = c;
459 *dir_ptr = 1; /* top */
460 } else {
461 pred = a;
462 *dir_ptr = 0; /* left */
463 }
464 /* we assume pred is positive */
465 pred = (pred + (scale >> 1)) / scale;
466
467 /* prepare address for prediction update */
468 *dc_val_ptr = &dc_val[(x) + (y) * wrap];
469
470 return pred;
471 }
472
473 void mpeg4_pred_ac(MpegEncContext * s, INT16 *block, int n,
474 int dir)
475 {
476 int x, y, wrap, i;
477 INT16 *ac_val, *ac_val1;
478
479 /* find prediction */
480 if (n < 4) {
481 x = 2 * s->mb_x + 1 + (n & 1);
482 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
483 wrap = s->mb_width * 2 + 2;
484 ac_val = s->ac_val[0][0];
485 } else {
486 x = s->mb_x + 1;
487 y = s->mb_y + 1;
488 wrap = s->mb_width + 2;
489 ac_val = s->ac_val[n - 4 + 1][0];
490 }
491 ac_val += ((y) * wrap + (x)) * 16;
492 ac_val1 = ac_val;
493 if (s->ac_pred) {
494 if (dir == 0) {
495 /* left prediction */
496 ac_val -= 16;
497 for(i=1;i<8;i++) {
498 block[i*8] += ac_val[i];
499 }
500 } else {
501 /* top prediction */
502 ac_val -= 16 * wrap;
503 for(i=1;i<8;i++) {
504 block[i] += ac_val[i + 8];
505 }
506 }
507 }
508 /* left copy */
509 for(i=1;i<8;i++)
510 ac_val1[i] = block[i * 8];
511 /* top copy */
512 for(i=1;i<8;i++)
513 ac_val1[8 + i] = block[i];
514 }
515
516 static inline void mpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
517 {
518 int size, v, pred;
519 UINT16 *dc_val;
520
521 pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
522 if (n < 4) {
523 *dc_val = level * s->y_dc_scale;
524 } else {
525 *dc_val = level * s->c_dc_scale;
526 }
527
528 /* do the prediction */
529 level -= pred;
530 /* find number of bits */
531 size = 0;
532 v = abs(level);
533 while (v) {
534 v >>= 1;
535 size++;
536 }
537
538 if (n < 4) {
539 /* luminance */
540 put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
541 } else {
542 /* chrominance */
543 put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
544 }
545
546 /* encode remaining bits */
547 if (size > 0) {
548 if (level < 0)
549 level = (-level) ^ ((1 << size) - 1);
550 put_bits(&s->pb, size, level);
551 if (size > 8)
552 put_bits(&s->pb, 1, 1);
553 }
554 }
555
556 static void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
557 {
558 int level, run, last, i, j, last_index, last_non_zero, sign, slevel;
559 int code, dc_pred_dir;
560 const RLTable *rl;
561
562 if (s->mb_intra) {
563 /* mpeg4 based DC predictor */
564 mpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
565 i = 1;
566 rl = &rl_intra;
567 } else {
568 i = 0;
569 rl = &rl_inter;
570 }
571
572 /* AC coefs */
573 last_index = s->block_last_index[n];
574 last_non_zero = i - 1;
575 for (; i <= last_index; i++) {
576 j = zigzag_direct[i];
577 level = block[j];
578 if (level) {
579 run = i - last_non_zero - 1;
580 last = (i == last_index);
581 sign = 0;
582 slevel = level;
583 if (level < 0) {
584 sign = 1;
585 level = -level;
586 }
587 code = get_rl_index(rl, last, run, level);
588 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
589 if (code == rl->n) {
590 int level1, run1;
591 level1 = level - rl->max_level[last][run];
592 if (level1 < 1)
593 goto esc2;
594 code = get_rl_index(rl, last, run, level1);
595 if (code == rl->n) {
596 esc2:
597 put_bits(&s->pb, 1, 1);
598 if (level > MAX_LEVEL)
599 goto esc3;
600 run1 = run - rl->max_run[last][level] - 1;
601 if (run1 < 0)
602 goto esc3;
603 code = get_rl_index(rl, last, run1, level);
604 if (code == rl->n) {
605 esc3:
606 /* third escape */
607 put_bits(&s->pb, 1, 1);
608 put_bits(&s->pb, 1, last);
609 put_bits(&s->pb, 6, run);
610 put_bits(&s->pb, 1, 1);
611 put_bits(&s->pb, 12, slevel & 0xfff);
612 put_bits(&s->pb, 1, 1);
613 } else {
614 /* second escape */
615 put_bits(&s->pb, 1, 0);
616 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
617 put_bits(&s->pb, 1, sign);
618 }
619 } else {
620 /* first escape */
621 put_bits(&s->pb, 1, 0);
622 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
623 put_bits(&s->pb, 1, sign);
624 }
625 } else {
626 put_bits(&s->pb, 1, sign);
627 }
628 last_non_zero = i;
629 }
630 }
631 }
632
633
634
635 /***********************************************/
636 /* decoding */
637
638 static VLC intra_MCBPC_vlc;
639 static VLC inter_MCBPC_vlc;
640 static VLC cbpy_vlc;
641 static VLC mv_vlc;
642 static VLC dc_lum, dc_chrom;
643
644 void init_rl(RLTable *rl)
645 {
646 INT8 max_level[MAX_RUN+1], max_run[MAX_LEVEL+1];
647 UINT8 index_run[MAX_RUN+1];
648 int last, run, level, start, end, i;
649
650 /* compute max_level[], max_run[] and index_run[] */
651 for(last=0;last<2;last++) {
652 if (last == 0) {
653 start = 0;
654 end = rl->last;
655 } else {
656 start = rl->last;
657 end = rl->n;
658 }
659
660 memset(max_level, 0, MAX_RUN + 1);
661 memset(max_run, 0, MAX_LEVEL + 1);
662 memset(index_run, rl->n, MAX_RUN + 1);
663 for(i=start;i<end;i++) {
664 run = rl->table_run[i];
665 level = rl->table_level[i];
666 if (index_run[run] == rl->n)
667 index_run[run] = i;
668 if (level > max_level[run])
669 max_level[run] = level;
670 if (run > max_run[level])
671 max_run[level] = run;
672 }
673 rl->max_level[last] = malloc(MAX_RUN + 1);
674 memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
675 rl->max_run[last] = malloc(MAX_LEVEL + 1);
676 memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
677 rl->index_run[last] = malloc(MAX_RUN + 1);
678 memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
679 }
680 }
681
682 void init_vlc_rl(RLTable *rl)
683 {
684 init_vlc(&rl->vlc, 9, rl->n + 1,
685 &rl->table_vlc[0][1], 4, 2,
686 &rl->table_vlc[0][0], 4, 2);
687 }
688
689 /* init vlcs */
690
691 /* XXX: find a better solution to handle static init */
692 void h263_decode_init_vlc(MpegEncContext *s)
693 {
694 static int done = 0;
695
696 if (!done) {
697 done = 1;
698
699 init_vlc(&intra_MCBPC_vlc, 6, 8,
700 intra_MCBPC_bits, 1, 1,
701 intra_MCBPC_code, 1, 1);
702 init_vlc(&inter_MCBPC_vlc, 9, 20,
703 inter_MCBPC_bits, 1, 1,
704 inter_MCBPC_code, 1, 1);
705 init_vlc(&cbpy_vlc, 6, 16,
706 &cbpy_tab[0][1], 2, 1,
707 &cbpy_tab[0][0], 2, 1);
708 init_vlc(&mv_vlc, 9, 33,
709 &mvtab[0][1], 2, 1,
710 &mvtab[0][0], 2, 1);
711 init_rl(&rl_inter);
712 init_rl(&rl_intra);
713 init_vlc_rl(&rl_inter);
714 init_vlc_rl(&rl_intra);
715 init_vlc(&dc_lum, 9, 13,
716 &DCtab_lum[0][1], 2, 1,
717 &DCtab_lum[0][0], 2, 1);
718 init_vlc(&dc_chrom, 9, 13,
719 &DCtab_chrom[0][1], 2, 1,
720 &DCtab_chrom[0][0], 2, 1);
721 }
722 }
723
724 int h263_decode_mb(MpegEncContext *s,
725 DCTELEM block[6][64])
726 {
727 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
728 INT16 *mot_val;
729 static UINT8 quant_tab[4] = { -1, -2, 1, 2 };
730
731 if (s->pict_type == P_TYPE) {
732 if (get_bits(&s->gb, 1)) {
733 /* skip mb */
734 s->mb_intra = 0;
735 for(i=0;i<6;i++)
736 s->block_last_index[i] = -1;
737 s->mv_dir = MV_DIR_FORWARD;
738 s->mv_type = MV_TYPE_16X16;
739 s->mv[0][0][0] = 0;
740 s->mv[0][0][1] = 0;
741 s->mb_skiped = 1;
742 return 0;
743 }
744 cbpc = get_vlc(&s->gb, &inter_MCBPC_vlc);
745 if (cbpc < 0)
746 return -1;
747 dquant = cbpc & 8;
748 s->mb_intra = ((cbpc & 4) != 0);
749 } else {
750 cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc);
751 if (cbpc < 0)
752 return -1;
753 dquant = cbpc & 4;
754 s->mb_intra = 1;
755 }
756
757 if (!s->mb_intra) {
758 cbpy = get_vlc(&s->gb, &cbpy_vlc);
759 cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2);
760 if (dquant) {
761 s->qscale += quant_tab[get_bits(&s->gb, 2)];
762 if (s->qscale < 1)
763 s->qscale = 1;
764 else if (s->qscale > 31)
765 s->qscale = 31;
766 }
767 s->mv_dir = MV_DIR_FORWARD;
768 if ((cbpc & 16) == 0) {
769 /* 16x16 motion prediction */
770 s->mv_type = MV_TYPE_16X16;
771 h263_pred_motion(s, 0, &pred_x, &pred_y);
772 mx = h263_decode_motion(s, pred_x);
773 if (mx >= 0xffff)
774 return -1;
775 my = h263_decode_motion(s, pred_y);
776 if (my >= 0xffff)
777 return -1;
778 s->mv[0][0][0] = mx;
779 s->mv[0][0][1] = my;
780 } else {
781 s->mv_type = MV_TYPE_8X8;
782 for(i=0;i<4;i++) {
783 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
784 mx = h263_decode_motion(s, pred_x);
785 if (mx >= 0xffff)
786 return -1;
787 my = h263_decode_motion(s, pred_y);
788 if (my >= 0xffff)
789 return -1;
790 s->mv[0][i][0] = mx;
791 s->mv[0][i][1] = my;
792 mot_val[0] = mx;
793 mot_val[1] = my;
794 }
795 }
796 } else {
797 s->ac_pred = 0;
798 if (s->h263_pred) {
799 s->ac_pred = get_bits(&s->gb, 1);
800 }
801 cbpy = get_vlc(&s->gb, &cbpy_vlc);
802 cbp = (cbpc & 3) | (cbpy << 2);
803 if (dquant) {
804 s->qscale += quant_tab[get_bits(&s->gb, 2)];
805 if (s->qscale < 1)
806 s->qscale = 1;
807 else if (s->qscale > 31)
808 s->qscale = 31;
809 }
810 }
811
812 /* decode each block */
813 if (s->h263_pred) {
814 for (i = 0; i < 6; i++) {
815 if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
816 return -1;
817 }
818 } else {
819 for (i = 0; i < 6; i++) {
820 if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
821 return -1;
822 }
823 }
824 return 0;
825 }
826
827 static int h263_decode_motion(MpegEncContext * s, int pred)
828 {
829 int code, val, sign, shift, l, m;
830
831 code = get_vlc(&s->gb, &mv_vlc);
832 if (code < 0)
833 return 0xffff;
834
835 if (code == 0)
836 return pred;
837 sign = get_bits(&s->gb, 1);
838 shift = s->f_code - 1;
839 val = (code - 1) << shift;
840 if (shift > 0)
841 val |= get_bits(&s->gb, shift);
842 val++;
843 if (sign)
844 val = -val;
845 val += pred;
846
847 /* modulo decoding */
848 if (!s->h263_long_vectors) {
849 l = (1 << (s->f_code - 1)) * 32;
850 m = 2 * l;
851 if (val < -l) {
852 val += m;
853 } else if (val >= l) {
854 val -= m;
855 }
856 } else {
857 /* horrible h263 long vector mode */
858 if (pred < -31 && val < -63)
859 val += 64;
860 if (pred > 32 && val > 63)
861 val -= 64;
862 }
863 return val;
864 }
865
866 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
867 int n, int coded)
868 {
869 int code, level, i, j, last, run;
870 RLTable *rl = &rl_inter;
871
872 if (s->mb_intra) {
873 /* DC coef */
874 if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) {
875 int component, diff;
876 component = (n <= 3 ? 0 : n - 4 + 1);
877 level = s->last_dc[component];
878 if (s->rv10_first_dc_coded[component]) {
879 diff = rv_decode_dc(s, n);
880 if (diff == 0xffff)
881 return -1;
882 level += diff;
883 level = level & 0xff; /* handle wrap round */
884 s->last_dc[component] = level;
885 } else {
886 s->rv10_first_dc_coded[component] = 1;
887 }
888 } else {
889 level = get_bits(&s->gb, 8);
890 if (level == 255)
891 level = 128;
892 }
893 block[0] = level;
894 i = 1;
895 } else {
896 i = 0;
897 }
898 if (!coded) {
899 s->block_last_index[n] = i - 1;
900 return 0;
901 }
902
903 for(;;) {
904 code = get_vlc(&s->gb, &rl->vlc);
905 if (code < 0)
906 return -1;
907 if (code == rl->n) {
908 /* escape */
909 last = get_bits(&s->gb, 1);
910 run = get_bits(&s->gb, 6);
911 level = (INT8)get_bits(&s->gb, 8);
912 if (s->h263_rv10 && level == -128) {
913 /* XXX: should patch encoder too */
914 level = get_bits(&s->gb, 12);
915 level = (level << 20) >> 20;
916 }
917 } else {
918 run = rl->table_run[code];
919 level = rl->table_level[code];
920 last = code >= rl->last;
921 if (get_bits(&s->gb, 1))
922 level = -level;
923 }
924 i += run;
925 if (i >= 64)
926 return -1;
927 j = zigzag_direct[i];
928 block[j] = level;
929 if (last)
930 break;
931 i++;
932 }
933 s->block_last_index[n] = i;
934 return 0;
935 }
936
937 static int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
938 {
939 int level, pred, code;
940 UINT16 *dc_val;
941
942 if (n < 4)
943 code = get_vlc(&s->gb, &dc_lum);
944 else
945 code = get_vlc(&s->gb, &dc_chrom);
946 if (code < 0)
947 return -1;
948 if (code == 0) {
949 level = 0;
950 } else {
951 level = get_bits(&s->gb, code);
952 if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/
953 level = - (level ^ ((1 << code) - 1));
954 if (code > 8)
955 get_bits(&s->gb, 1); /* marker */
956 }
957
958 pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
959 level += pred;
960 if (level < 0)
961 level = 0;
962 if (n < 4) {
963 *dc_val = level * s->y_dc_scale;
964 } else {
965 *dc_val = level * s->c_dc_scale;
966 }
967 return level;
968 }
969
970 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
971 int n, int coded)
972 {
973 int code, level, i, j, last, run;
974 int dc_pred_dir;
975 RLTable *rl;
976 const UINT8 *scan_table;
977
978 if (s->mb_intra) {
979 /* DC coef */
980 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
981 if (level < 0)
982 return -1;
983 block[0] = level;
984 i = 1;
985 if (!coded)
986 goto not_coded;
987 rl = &rl_intra;
988 if (s->ac_pred) {
989 if (dc_pred_dir == 0)
990 scan_table = ff_alternate_vertical_scan; /* left */
991 else
992 scan_table = ff_alternate_horizontal_scan; /* top */
993 } else {
994 scan_table = zigzag_direct;
995 }
996 } else {
997 i = 0;
998 if (!coded) {
999 s->block_last_index[n] = i - 1;
1000 return 0;
1001 }
1002 rl = &rl_inter;
1003 scan_table = zigzag_direct;
1004 }
1005
1006 for(;;) {
1007 code = get_vlc(&s->gb, &rl->vlc);
1008 if (code < 0)
1009 return -1;
1010 if (code == rl->n) {
1011 /* escape */
1012 if (get_bits(&s->gb, 1) != 0) {
1013 if (get_bits(&s->gb, 1) != 0) {
1014 /* third escape */
1015 last = get_bits(&s->gb, 1);
1016 run = get_bits(&s->gb, 6);
1017 get_bits(&s->gb, 1); /* marker */
1018 level = get_bits(&s->gb, 12);
1019 level = (level << 20) >> 20; /* sign extend */
1020 get_bits(&s->gb, 1); /* marker */
1021 } else {
1022 /* second escape */
1023 code = get_vlc(&s->gb, &rl->vlc);
1024 if (code < 0 || code >= rl->n)
1025 return -1;
1026 run = rl->table_run[code];
1027 level = rl->table_level[code];
1028 last = code >= rl->last;
1029 run += rl->max_run[last][level] + 1;
1030 if (get_bits(&s->gb, 1))
1031 level = -level;
1032 }
1033 } else {
1034 /* first escape */
1035 code = get_vlc(&s->gb, &rl->vlc);
1036 if (code < 0 || code >= rl->n)
1037 return -1;
1038 run = rl->table_run[code];
1039 level = rl->table_level[code];
1040 last = code >= rl->last;
1041 level += rl->max_level[last][run];
1042 if (get_bits(&s->gb, 1))
1043 level = -level;
1044 }
1045 } else {
1046 run = rl->table_run[code];
1047 level = rl->table_level[code];
1048 last = code >= rl->last;
1049 if (get_bits(&s->gb, 1))
1050 level = -level;
1051 }
1052 i += run;
1053 if (i >= 64)
1054 return -1;
1055 j = scan_table[i];
1056 block[j] = level;
1057 i++;
1058 if (last)
1059 break;
1060 }
1061 not_coded:
1062 if (s->mb_intra) {
1063 mpeg4_pred_ac(s, block, n, dc_pred_dir);
1064 if (s->ac_pred) {
1065 i = 64; /* XXX: not optimal */
1066 }
1067 }
1068 s->block_last_index[n] = i - 1;
1069 return 0;
1070 }
1071
1072 /* most is hardcoded. should extend to handle all h263 streams */
1073 int h263_decode_picture_header(MpegEncContext *s)
1074 {
1075 int format, width, height;
1076
1077 /* picture header */
1078 if (get_bits(&s->gb, 22) != 0x20)
1079 return -1;
1080 get_bits(&s->gb, 8); /* picture timestamp */
1081
1082 if (get_bits(&s->gb, 1) != 1)
1083 return -1; /* marker */
1084 if (get_bits(&s->gb, 1) != 0)
1085 return -1; /* h263 id */
1086 get_bits(&s->gb, 1); /* split screen off */
1087 get_bits(&s->gb, 1); /* camera off */
1088 get_bits(&s->gb, 1); /* freeze picture release off */
1089
1090 format = get_bits(&s->gb, 3);
1091
1092 if (format != 7) {
1093 s->h263_plus = 0;
1094 /* H.263v1 */
1095 width = h263_format[format][0];
1096 height = h263_format[format][1];
1097 if (!width)
1098 return -1;
1099
1100 s->pict_type = I_TYPE + get_bits(&s->gb, 1);
1101
1102 s->unrestricted_mv = get_bits(&s->gb, 1);
1103 s->h263_long_vectors = s->unrestricted_mv;
1104
1105 if (get_bits(&s->gb, 1) != 0)
1106 return -1; /* SAC: off */
1107 if (get_bits(&s->gb, 1) != 0)
1108 return -1; /* advanced prediction mode: off */
1109 if (get_bits(&s->gb, 1) != 0)
1110 return -1; /* not PB frame */
1111
1112 s->qscale = get_bits(&s->gb, 5);
1113 get_bits(&s->gb, 1); /* Continuous Presence Multipoint mode: off */
1114 } else {
1115 s->h263_plus = 1;
1116 /* H.263v2 */
1117 if (get_bits(&s->gb, 3) != 1)
1118 return -1;
1119 if (get_bits(&s->gb, 3) != 6) /* custom source format */
1120 return -1;
1121 get_bits(&s->gb, 12);
1122 get_bits(&s->gb, 3);
1123 s->pict_type = get_bits(&s->gb, 3) + 1;
1124 if (s->pict_type != I_TYPE &&
1125 s->pict_type != P_TYPE)
1126 return -1;
1127 get_bits(&s->gb, 7);
1128 get_bits(&s->gb, 4); /* aspect ratio */
1129 width = (get_bits(&s->gb, 9) + 1) * 4;
1130 get_bits(&s->gb, 1);
1131 height = get_bits(&s->gb, 9) * 4;
1132 if (height == 0)
1133 return -1;
1134 s->qscale = get_bits(&s->gb, 5);
1135 }
1136 /* PEI */
1137 while (get_bits(&s->gb, 1) != 0) {
1138 get_bits(&s->gb, 8);
1139 }
1140 s->f_code = 1;
1141 s->width = width;
1142 s->height = height;
1143 return 0;
1144 }
1145
1146 /* decode mpeg4 VOP header */
1147 int mpeg4_decode_picture_header(MpegEncContext * s)
1148 {
1149 int time_incr, startcode, state, v;
1150
1151 redo:
1152 /* search next start code */
1153 align_get_bits(&s->gb);
1154 state = 0xff;
1155 for(;;) {
1156 v = get_bits(&s->gb, 8);
1157 if (state == 0x000001) {
1158 state = ((state << 8) | v) & 0xffffff;
1159 startcode = state;
1160 break;
1161 }
1162 state = ((state << 8) | v) & 0xffffff;
1163 /* XXX: really detect end of frame */
1164 if (state == 0)
1165 return -1;
1166 }
1167
1168 if (startcode == 0x120) {
1169 int time_increment_resolution, width, height;
1170
1171 /* vol header */
1172 get_bits(&s->gb, 1); /* random access */
1173 get_bits(&s->gb, 8); /* vo_type */
1174 get_bits(&s->gb, 1); /* is_ol_id */
1175 get_bits(&s->gb, 4); /* vo_ver_id */
1176 get_bits(&s->gb, 3); /* vo_priority */
1177
1178 get_bits(&s->gb, 4); /* aspect_ratio_info */
1179 get_bits(&s->gb, 1); /* vol control parameter */
1180 get_bits(&s->gb, 2); /* vol shape */
1181 get_bits(&s->gb, 1); /* marker */
1182
1183 time_increment_resolution = get_bits(&s->gb, 16);
1184 s->time_increment_bits = log2(time_increment_resolution - 1) + 1;
1185 get_bits(&s->gb, 1); /* marker */
1186
1187 get_bits(&s->gb, 1); /* vop rate */
1188 get_bits(&s->gb, s->time_increment_bits);
1189 get_bits(&s->gb, 1); /* marker */
1190
1191 width = get_bits(&s->gb, 13);
1192 get_bits(&s->gb, 1); /* marker */
1193 height = get_bits(&s->gb, 13);
1194 get_bits(&s->gb, 1); /* marker */
1195
1196 get_bits(&s->gb, 1); /* interfaced */
1197 get_bits(&s->gb, 1); /* OBMC */
1198 get_bits(&s->gb, 2); /* vol_sprite_usage */
1199 get_bits(&s->gb, 1); /* not_8_bit */
1200
1201 get_bits(&s->gb, 1); /* vol_quant_type */
1202 get_bits(&s->gb, 1); /* vol_quarter_pixel */
1203 get_bits(&s->gb, 1); /* complexity_estimation_disabled */
1204 get_bits(&s->gb, 1); /* resync_marker_disabled */
1205 get_bits(&s->gb, 1); /* data_partioning_enabled */
1206 goto redo;
1207 } else if (startcode != 0x1b6) {
1208 goto redo;
1209 }
1210
1211 s->pict_type = get_bits(&s->gb, 2) + 1; /* pict type: I = 0 , P = 1 */
1212 if (s->pict_type != I_TYPE &&
1213 s->pict_type != P_TYPE)
1214 return -1;
1215
1216 /* XXX: parse time base */
1217 time_incr = 0;
1218 while (get_bits(&s->gb, 1) != 0)
1219 time_incr++;
1220
1221 get_bits(&s->gb, 1); /* marker */
1222 get_bits(&s->gb, s->time_increment_bits);
1223 get_bits(&s->gb, 1); /* marker */
1224 /* vop coded */
1225 if (get_bits(&s->gb, 1) != 1)
1226 return -1;
1227
1228 if (s->pict_type == P_TYPE) {
1229 /* rounding type for motion estimation */
1230 s->no_rounding = get_bits(&s->gb, 1);
1231 }
1232
1233 if (get_bits(&s->gb, 3) != 0)
1234 return -1; /* intra dc VLC threshold */
1235
1236 s->qscale = get_bits(&s->gb, 5);
1237
1238 if (s->pict_type != I_TYPE) {
1239 s->f_code = get_bits(&s->gb, 3); /* fcode_for */
1240 }
1241 return 0;
1242 }
1243
1244 /* don't understand why they choose a different header ! */
1245 int intel_h263_decode_picture_header(MpegEncContext *s)
1246 {
1247 int format;
1248
1249 /* picture header */
1250 if (get_bits(&s->gb, 22) != 0x20)
1251 return -1;
1252 get_bits(&s->gb, 8); /* picture timestamp */
1253
1254 if (get_bits(&s->gb, 1) != 1)
1255 return -1; /* marker */
1256 if (get_bits(&s->gb, 1) != 0)
1257 return -1; /* h263 id */
1258 get_bits(&s->gb, 1); /* split screen off */
1259 get_bits(&s->gb, 1); /* camera off */
1260 get_bits(&s->gb, 1); /* freeze picture release off */
1261
1262 format = get_bits(&s->gb, 3);
1263 if (format != 7)
1264 return -1;
1265
1266 s->h263_plus = 0;
1267
1268 s->pict_type = I_TYPE + get_bits(&s->gb, 1);
1269
1270 s->unrestricted_mv = get_bits(&s->gb, 1);
1271 s->h263_long_vectors = s->unrestricted_mv;
1272
1273 if (get_bits(&s->gb, 1) != 0)
1274 return -1; /* SAC: off */
1275 if (get_bits(&s->gb, 1) != 0)
1276 return -1; /* advanced prediction mode: off */
1277 if (get_bits(&s->gb, 1) != 0)
1278 return -1; /* not PB frame */
1279
1280 /* skip unknown header garbage */
1281 get_bits(&s->gb, 41);
1282
1283 s->qscale = get_bits(&s->gb, 5);
1284 get_bits(&s->gb, 1); /* Continuous Presence Multipoint mode: off */
1285
1286 /* PEI */
1287 while (get_bits(&s->gb, 1) != 0) {
1288 get_bits(&s->gb, 8);
1289 }
1290 s->f_code = 1;
1291 return 0;
1292 }