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