35ecb15c2e620548501292d070977d9b11c05c1d
[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 return 0;
742 }
743 cbpc = get_vlc(&s->gb, &inter_MCBPC_vlc);
744 if (cbpc < 0)
745 return -1;
746 dquant = cbpc & 8;
747 s->mb_intra = ((cbpc & 4) != 0);
748 } else {
749 cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc);
750 if (cbpc < 0)
751 return -1;
752 dquant = cbpc & 4;
753 s->mb_intra = 1;
754 }
755
756 if (!s->mb_intra) {
757 cbpy = get_vlc(&s->gb, &cbpy_vlc);
758 cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2);
759 if (dquant) {
760 s->qscale += quant_tab[get_bits(&s->gb, 2)];
761 if (s->qscale < 1)
762 s->qscale = 1;
763 else if (s->qscale > 31)
764 s->qscale = 31;
765 }
766 s->mv_dir = MV_DIR_FORWARD;
767 if ((cbpc & 16) == 0) {
768 /* 16x16 motion prediction */
769 s->mv_type = MV_TYPE_16X16;
770 h263_pred_motion(s, 0, &pred_x, &pred_y);
771 mx = h263_decode_motion(s, pred_x);
772 if (mx >= 0xffff)
773 return -1;
774 my = h263_decode_motion(s, pred_y);
775 if (my >= 0xffff)
776 return -1;
777 s->mv[0][0][0] = mx;
778 s->mv[0][0][1] = my;
779 } else {
780 s->mv_type = MV_TYPE_8X8;
781 for(i=0;i<4;i++) {
782 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
783 mx = h263_decode_motion(s, pred_x);
784 if (mx >= 0xffff)
785 return -1;
786 my = h263_decode_motion(s, pred_y);
787 if (my >= 0xffff)
788 return -1;
789 s->mv[0][i][0] = mx;
790 s->mv[0][i][1] = my;
791 mot_val[0] = mx;
792 mot_val[1] = my;
793 }
794 }
795 } else {
796 s->ac_pred = 0;
797 if (s->h263_pred) {
798 s->ac_pred = get_bits(&s->gb, 1);
799 }
800 cbpy = get_vlc(&s->gb, &cbpy_vlc);
801 cbp = (cbpc & 3) | (cbpy << 2);
802 if (dquant) {
803 s->qscale += quant_tab[get_bits(&s->gb, 2)];
804 if (s->qscale < 1)
805 s->qscale = 1;
806 else if (s->qscale > 31)
807 s->qscale = 31;
808 }
809 }
810
811 /* decode each block */
812 if (s->h263_pred) {
813 for (i = 0; i < 6; i++) {
814 if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
815 return -1;
816 }
817 } else {
818 for (i = 0; i < 6; i++) {
819 if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0)
820 return -1;
821 }
822 }
823 return 0;
824 }
825
826 static int h263_decode_motion(MpegEncContext * s, int pred)
827 {
828 int code, val, sign, shift, l, m;
829
830 code = get_vlc(&s->gb, &mv_vlc);
831 if (code < 0)
832 return 0xffff;
833
834 if (code == 0)
835 return pred;
836 sign = get_bits(&s->gb, 1);
837 shift = s->f_code - 1;
838 val = (code - 1) << shift;
839 if (shift > 0)
840 val |= get_bits(&s->gb, shift);
841 val++;
842 if (sign)
843 val = -val;
844 val += pred;
845
846 /* modulo decoding */
847 if (!s->h263_long_vectors) {
848 l = (1 << (s->f_code - 1)) * 32;
849 m = 2 * l;
850 if (val < -l) {
851 val += m;
852 } else if (val >= l) {
853 val -= m;
854 }
855 } else {
856 /* horrible h263 long vector mode */
857 if (pred < -31 && val < -63)
858 val += 64;
859 if (pred > 32 && val > 63)
860 val -= 64;
861 }
862 return val;
863 }
864
865 static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
866 int n, int coded)
867 {
868 int code, level, i, j, last, run;
869 RLTable *rl = &rl_inter;
870
871 if (s->mb_intra) {
872 /* DC coef */
873 if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) {
874 int component, diff;
875 component = (n <= 3 ? 0 : n - 4 + 1);
876 level = s->last_dc[component];
877 if (s->rv10_first_dc_coded[component]) {
878 diff = rv_decode_dc(s, n);
879 if (diff == 0xffff)
880 return -1;
881 level += diff;
882 level = level & 0xff; /* handle wrap round */
883 s->last_dc[component] = level;
884 } else {
885 s->rv10_first_dc_coded[component] = 1;
886 }
887 } else {
888 level = get_bits(&s->gb, 8);
889 if (level == 255)
890 level = 128;
891 }
892 block[0] = level;
893 i = 1;
894 } else {
895 i = 0;
896 }
897 if (!coded) {
898 s->block_last_index[n] = i - 1;
899 return 0;
900 }
901
902 for(;;) {
903 code = get_vlc(&s->gb, &rl->vlc);
904 if (code < 0)
905 return -1;
906 if (code == rl->n) {
907 /* escape */
908 last = get_bits(&s->gb, 1);
909 run = get_bits(&s->gb, 6);
910 level = (INT8)get_bits(&s->gb, 8);
911 if (s->h263_rv10 && level == -128) {
912 /* XXX: should patch encoder too */
913 level = get_bits(&s->gb, 12);
914 level = (level << 20) >> 20;
915 }
916 } else {
917 run = rl->table_run[code];
918 level = rl->table_level[code];
919 last = code >= rl->last;
920 if (get_bits(&s->gb, 1))
921 level = -level;
922 }
923 i += run;
924 if (i >= 64)
925 return -1;
926 j = zigzag_direct[i];
927 block[j] = level;
928 if (last)
929 break;
930 i++;
931 }
932 s->block_last_index[n] = i;
933 return 0;
934 }
935
936 static int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
937 {
938 int level, pred, code;
939 UINT16 *dc_val;
940
941 if (n < 4)
942 code = get_vlc(&s->gb, &dc_lum);
943 else
944 code = get_vlc(&s->gb, &dc_chrom);
945 if (code < 0)
946 return -1;
947 if (code == 0) {
948 level = 0;
949 } else {
950 level = get_bits(&s->gb, code);
951 if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/
952 level = - (level ^ ((1 << code) - 1));
953 if (code > 8)
954 get_bits(&s->gb, 1); /* marker */
955 }
956
957 pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr);
958 level += pred;
959 if (level < 0)
960 level = 0;
961 if (n < 4) {
962 *dc_val = level * s->y_dc_scale;
963 } else {
964 *dc_val = level * s->c_dc_scale;
965 }
966 return level;
967 }
968
969 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
970 int n, int coded)
971 {
972 int code, level, i, j, last, run;
973 int dc_pred_dir;
974 RLTable *rl;
975 const UINT8 *scan_table;
976
977 if (s->mb_intra) {
978 /* DC coef */
979 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
980 if (level < 0)
981 return -1;
982 block[0] = level;
983 i = 1;
984 if (!coded)
985 goto not_coded;
986 rl = &rl_intra;
987 if (s->ac_pred) {
988 if (dc_pred_dir == 0)
989 scan_table = ff_alternate_vertical_scan; /* left */
990 else
991 scan_table = ff_alternate_horizontal_scan; /* top */
992 } else {
993 scan_table = zigzag_direct;
994 }
995 } else {
996 i = 0;
997 if (!coded) {
998 s->block_last_index[n] = i - 1;
999 return 0;
1000 }
1001 rl = &rl_inter;
1002 scan_table = zigzag_direct;
1003 }
1004
1005 for(;;) {
1006 code = get_vlc(&s->gb, &rl->vlc);
1007 if (code < 0)
1008 return -1;
1009 if (code == rl->n) {
1010 /* escape */
1011 if (get_bits(&s->gb, 1) != 0) {
1012 if (get_bits(&s->gb, 1) != 0) {
1013 /* third escape */
1014 last = get_bits(&s->gb, 1);
1015 run = get_bits(&s->gb, 6);
1016 get_bits(&s->gb, 1); /* marker */
1017 level = get_bits(&s->gb, 12);
1018 level = (level << 20) >> 20; /* sign extend */
1019 get_bits(&s->gb, 1); /* marker */
1020 } else {
1021 /* second escape */
1022 code = get_vlc(&s->gb, &rl->vlc);
1023 if (code < 0 || code >= rl->n)
1024 return -1;
1025 run = rl->table_run[code];
1026 level = rl->table_level[code];
1027 last = code >= rl->last;
1028 run += rl->max_run[last][level] + 1;
1029 if (get_bits(&s->gb, 1))
1030 level = -level;
1031 }
1032 } else {
1033 /* first escape */
1034 code = get_vlc(&s->gb, &rl->vlc);
1035 if (code < 0 || code >= rl->n)
1036 return -1;
1037 run = rl->table_run[code];
1038 level = rl->table_level[code];
1039 last = code >= rl->last;
1040 level += rl->max_level[last][run];
1041 if (get_bits(&s->gb, 1))
1042 level = -level;
1043 }
1044 } else {
1045 run = rl->table_run[code];
1046 level = rl->table_level[code];
1047 last = code >= rl->last;
1048 if (get_bits(&s->gb, 1))
1049 level = -level;
1050 }
1051 i += run;
1052 if (i >= 64)
1053 return -1;
1054 j = scan_table[i];
1055 block[j] = level;
1056 i++;
1057 if (last)
1058 break;
1059 }
1060 not_coded:
1061 if (s->mb_intra) {
1062 mpeg4_pred_ac(s, block, n, dc_pred_dir);
1063 if (s->ac_pred) {
1064 i = 64; /* XXX: not optimal */
1065 }
1066 }
1067 s->block_last_index[n] = i - 1;
1068 return 0;
1069 }
1070
1071 /* most is hardcoded. should extend to handle all h263 streams */
1072 int h263_decode_picture_header(MpegEncContext *s)
1073 {
1074 int format, width, height;
1075
1076 /* picture header */
1077 if (get_bits(&s->gb, 22) != 0x20)
1078 return -1;
1079 get_bits(&s->gb, 8); /* picture timestamp */
1080
1081 if (get_bits(&s->gb, 1) != 1)
1082 return -1; /* marker */
1083 if (get_bits(&s->gb, 1) != 0)
1084 return -1; /* h263 id */
1085 get_bits(&s->gb, 1); /* split screen off */
1086 get_bits(&s->gb, 1); /* camera off */
1087 get_bits(&s->gb, 1); /* freeze picture release off */
1088
1089 format = get_bits(&s->gb, 3);
1090
1091 if (format != 7) {
1092 s->h263_plus = 0;
1093 /* H.263v1 */
1094 width = h263_format[format][0];
1095 height = h263_format[format][1];
1096 if (!width)
1097 return -1;
1098
1099 s->pict_type = I_TYPE + get_bits(&s->gb, 1);
1100
1101 s->unrestricted_mv = get_bits(&s->gb, 1);
1102 s->h263_long_vectors = s->unrestricted_mv;
1103
1104 if (get_bits(&s->gb, 1) != 0)
1105 return -1; /* SAC: off */
1106 if (get_bits(&s->gb, 1) != 0)
1107 return -1; /* advanced prediction mode: off */
1108 if (get_bits(&s->gb, 1) != 0)
1109 return -1; /* not PB frame */
1110
1111 s->qscale = get_bits(&s->gb, 5);
1112 get_bits(&s->gb, 1); /* Continuous Presence Multipoint mode: off */
1113 } else {
1114 s->h263_plus = 1;
1115 /* H.263v2 */
1116 if (get_bits(&s->gb, 3) != 1)
1117 return -1;
1118 if (get_bits(&s->gb, 3) != 6) /* custom source format */
1119 return -1;
1120 get_bits(&s->gb, 12);
1121 get_bits(&s->gb, 3);
1122 s->pict_type = get_bits(&s->gb, 3) + 1;
1123 if (s->pict_type != I_TYPE &&
1124 s->pict_type != P_TYPE)
1125 return -1;
1126 get_bits(&s->gb, 7);
1127 get_bits(&s->gb, 4); /* aspect ratio */
1128 width = (get_bits(&s->gb, 9) + 1) * 4;
1129 get_bits(&s->gb, 1);
1130 height = get_bits(&s->gb, 9) * 4;
1131 if (height == 0)
1132 return -1;
1133 s->qscale = get_bits(&s->gb, 5);
1134 }
1135 /* PEI */
1136 while (get_bits(&s->gb, 1) != 0) {
1137 get_bits(&s->gb, 8);
1138 }
1139 s->f_code = 1;
1140 s->width = width;
1141 s->height = height;
1142 return 0;
1143 }
1144
1145 /* decode mpeg4 VOP header */
1146 int mpeg4_decode_picture_header(MpegEncContext * s)
1147 {
1148 int time_incr, startcode, state, v;
1149
1150 redo:
1151 /* search next start code */
1152 align_get_bits(&s->gb);
1153 state = 0xff;
1154 for(;;) {
1155 v = get_bits(&s->gb, 8);
1156 if (state == 0x000001) {
1157 state = ((state << 8) | v) & 0xffffff;
1158 startcode = state;
1159 break;
1160 }
1161 state = ((state << 8) | v) & 0xffffff;
1162 /* XXX: really detect end of frame */
1163 if (state == 0)
1164 return -1;
1165 }
1166
1167 if (startcode == 0x120) {
1168 int time_increment_resolution, width, height;
1169
1170 /* vol header */
1171 get_bits(&s->gb, 1); /* random access */
1172 get_bits(&s->gb, 8); /* vo_type */
1173 get_bits(&s->gb, 1); /* is_ol_id */
1174 get_bits(&s->gb, 4); /* vo_ver_id */
1175 get_bits(&s->gb, 3); /* vo_priority */
1176
1177 get_bits(&s->gb, 4); /* aspect_ratio_info */
1178 get_bits(&s->gb, 1); /* vol control parameter */
1179 get_bits(&s->gb, 2); /* vol shape */
1180 get_bits(&s->gb, 1); /* marker */
1181
1182 time_increment_resolution = get_bits(&s->gb, 16);
1183 s->time_increment_bits = log2(time_increment_resolution - 1) + 1;
1184 get_bits(&s->gb, 1); /* marker */
1185
1186 get_bits(&s->gb, 1); /* vop rate */
1187 get_bits(&s->gb, s->time_increment_bits);
1188 get_bits(&s->gb, 1); /* marker */
1189
1190 width = get_bits(&s->gb, 13);
1191 get_bits(&s->gb, 1); /* marker */
1192 height = get_bits(&s->gb, 13);
1193 get_bits(&s->gb, 1); /* marker */
1194
1195 get_bits(&s->gb, 1); /* interfaced */
1196 get_bits(&s->gb, 1); /* OBMC */
1197 get_bits(&s->gb, 2); /* vol_sprite_usage */
1198 get_bits(&s->gb, 1); /* not_8_bit */
1199
1200 get_bits(&s->gb, 1); /* vol_quant_type */
1201 get_bits(&s->gb, 1); /* vol_quarter_pixel */
1202 get_bits(&s->gb, 1); /* complexity_estimation_disabled */
1203 get_bits(&s->gb, 1); /* resync_marker_disabled */
1204 get_bits(&s->gb, 1); /* data_partioning_enabled */
1205 goto redo;
1206 } else if (startcode != 0x1b6) {
1207 goto redo;
1208 }
1209
1210 s->pict_type = get_bits(&s->gb, 2) + 1; /* pict type: I = 0 , P = 1 */
1211 if (s->pict_type != I_TYPE &&
1212 s->pict_type != P_TYPE)
1213 return -1;
1214
1215 /* XXX: parse time base */
1216 time_incr = 0;
1217 while (get_bits(&s->gb, 1) != 0)
1218 time_incr++;
1219
1220 get_bits(&s->gb, 1); /* marker */
1221 get_bits(&s->gb, s->time_increment_bits);
1222 get_bits(&s->gb, 1); /* marker */
1223 /* vop coded */
1224 if (get_bits(&s->gb, 1) != 1)
1225 return -1;
1226
1227 if (s->pict_type == P_TYPE) {
1228 /* rounding type for motion estimation */
1229 s->no_rounding = get_bits(&s->gb, 1);
1230 }
1231
1232 if (get_bits(&s->gb, 3) != 0)
1233 return -1; /* intra dc VLC threshold */
1234
1235 s->qscale = get_bits(&s->gb, 5);
1236
1237 if (s->pict_type != I_TYPE) {
1238 s->f_code = get_bits(&s->gb, 3); /* fcode_for */
1239 }
1240 return 0;
1241 }
1242
1243 /* don't understand why they choose a different header ! */
1244 int intel_h263_decode_picture_header(MpegEncContext *s)
1245 {
1246 int format;
1247
1248 /* picture header */
1249 if (get_bits(&s->gb, 22) != 0x20)
1250 return -1;
1251 get_bits(&s->gb, 8); /* picture timestamp */
1252
1253 if (get_bits(&s->gb, 1) != 1)
1254 return -1; /* marker */
1255 if (get_bits(&s->gb, 1) != 0)
1256 return -1; /* h263 id */
1257 get_bits(&s->gb, 1); /* split screen off */
1258 get_bits(&s->gb, 1); /* camera off */
1259 get_bits(&s->gb, 1); /* freeze picture release off */
1260
1261 format = get_bits(&s->gb, 3);
1262 if (format != 7)
1263 return -1;
1264
1265 s->h263_plus = 0;
1266
1267 s->pict_type = I_TYPE + get_bits(&s->gb, 1);
1268
1269 s->unrestricted_mv = get_bits(&s->gb, 1);
1270 s->h263_long_vectors = s->unrestricted_mv;
1271
1272 if (get_bits(&s->gb, 1) != 0)
1273 return -1; /* SAC: off */
1274 if (get_bits(&s->gb, 1) != 0)
1275 return -1; /* advanced prediction mode: off */
1276 if (get_bits(&s->gb, 1) != 0)
1277 return -1; /* not PB frame */
1278
1279 /* skip unknown header garbage */
1280 get_bits(&s->gb, 41);
1281
1282 s->qscale = get_bits(&s->gb, 5);
1283 get_bits(&s->gb, 1); /* Continuous Presence Multipoint mode: off */
1284
1285 /* PEI */
1286 while (get_bits(&s->gb, 1) != 0) {
1287 get_bits(&s->gb, 8);
1288 }
1289 s->f_code = 1;
1290 return 0;
1291 }