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