Mercurial > libavcodec.hg
annotate h263.c @ 144:cb5dabd00ba2 libavcodec
- Bug fix on inter MCBPC table for inter+q.
- H.263/H.263+ decoder now knows GOB start codes.
- H.263/H.263+ decoder now returns the size of the stream on the first call.
- Added show_bits() functions to see the buffer without loosing the bits.
- TODO: H.263v1 UMV parsing is buggy.
author | pulento |
---|---|
date | Sat, 03 Nov 2001 00:49:53 +0000 |
parents | e00e5d93457c |
children | f914f710b8d0 |
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 */ | |
255 if (y == 1 || s->first_slice_line) { | |
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 }; |
144 | 782 |
783 /* Check for GOB Start Code */ | |
784 val = show_bits(&s->gb, 16); | |
785 | |
786 if (val == 0) { | |
787 /* We have a GBSC probably with GSTUFF */ | |
788 #ifdef DEBUG | |
789 unsigned int gn, gfid; | |
790 #endif | |
791 //skip_bits(&s->gb, 16); /* Drop the zeros */ | |
792 while (get_bits1(&s->gb) == 0); /* Seek the '1' bit */ | |
793 #ifdef DEBUG | |
794 fprintf(stderr,"\nGOB Start Code at MB %d\n", | |
795 (s->mb_y * s->mb_width) + s->mb_x); | |
796 gn = get_bits(&s->gb, 5); /* GN */ | |
797 gfid = get_bits(&s->gb, 2); /* GFID */ | |
798 #else | |
799 skip_bits(&s->gb, 5); /* GN */ | |
800 skip_bits(&s->gb, 2); /* GFID */ | |
801 #endif | |
802 s->qscale = get_bits(&s->gb, 5); /* GQUANT */ | |
803 #ifdef DEBUG | |
804 fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", gn, gfid, s->qscale); | |
805 #endif | |
806 } | |
807 | |
0 | 808 if (s->pict_type == P_TYPE) { |
21 | 809 if (get_bits1(&s->gb)) { |
0 | 810 /* skip mb */ |
811 s->mb_intra = 0; | |
812 for(i=0;i<6;i++) | |
813 s->block_last_index[i] = -1; | |
814 s->mv_dir = MV_DIR_FORWARD; | |
815 s->mv_type = MV_TYPE_16X16; | |
816 s->mv[0][0][0] = 0; | |
817 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
|
818 s->mb_skiped = 1; |
0 | 819 return 0; |
820 } | |
821 cbpc = get_vlc(&s->gb, &inter_MCBPC_vlc); | |
144 | 822 //fprintf(stderr, "\tCBPC: %d", cbpc); |
0 | 823 if (cbpc < 0) |
824 return -1; | |
144 | 825 |
0 | 826 dquant = cbpc & 8; |
827 s->mb_intra = ((cbpc & 4) != 0); | |
828 } else { | |
829 cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc); | |
830 if (cbpc < 0) | |
831 return -1; | |
832 dquant = cbpc & 4; | |
833 s->mb_intra = 1; | |
834 } | |
835 | |
836 if (!s->mb_intra) { | |
837 cbpy = get_vlc(&s->gb, &cbpy_vlc); | |
838 cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2); | |
839 if (dquant) { | |
840 s->qscale += quant_tab[get_bits(&s->gb, 2)]; | |
841 if (s->qscale < 1) | |
842 s->qscale = 1; | |
843 else if (s->qscale > 31) | |
844 s->qscale = 31; | |
845 } | |
846 s->mv_dir = MV_DIR_FORWARD; | |
847 if ((cbpc & 16) == 0) { | |
848 /* 16x16 motion prediction */ | |
849 s->mv_type = MV_TYPE_16X16; | |
850 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
|
851 if (s->umvplus_dec) |
78 | 852 mx = h263p_decode_umotion(s, pred_x); |
853 else | |
854 mx = h263_decode_motion(s, pred_x); | |
0 | 855 if (mx >= 0xffff) |
856 return -1; | |
78 | 857 |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
858 if (s->umvplus_dec) |
78 | 859 my = h263p_decode_umotion(s, pred_y); |
860 else | |
861 my = h263_decode_motion(s, pred_y); | |
0 | 862 if (my >= 0xffff) |
863 return -1; | |
864 s->mv[0][0][0] = mx; | |
865 s->mv[0][0][1] = my; | |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
866 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1) |
78 | 867 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */ |
868 | |
0 | 869 } else { |
870 s->mv_type = MV_TYPE_8X8; | |
871 for(i=0;i<4;i++) { | |
872 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
|
873 if (s->umvplus_dec) |
78 | 874 mx = h263p_decode_umotion(s, pred_x); |
875 else | |
876 mx = h263_decode_motion(s, pred_x); | |
0 | 877 if (mx >= 0xffff) |
878 return -1; | |
78 | 879 |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
880 if (s->umvplus_dec) |
78 | 881 my = h263p_decode_umotion(s, pred_y); |
882 else | |
883 my = h263_decode_motion(s, pred_y); | |
0 | 884 if (my >= 0xffff) |
885 return -1; | |
886 s->mv[0][i][0] = mx; | |
887 s->mv[0][i][1] = my; | |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
888 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1) |
78 | 889 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */ |
0 | 890 mot_val[0] = mx; |
891 mot_val[1] = my; | |
892 } | |
893 } | |
894 } else { | |
895 s->ac_pred = 0; | |
144 | 896 if (s->h263_pred) { |
21 | 897 s->ac_pred = get_bits1(&s->gb); |
0 | 898 } |
899 cbpy = get_vlc(&s->gb, &cbpy_vlc); | |
900 cbp = (cbpc & 3) | (cbpy << 2); | |
901 if (dquant) { | |
902 s->qscale += quant_tab[get_bits(&s->gb, 2)]; | |
903 if (s->qscale < 1) | |
904 s->qscale = 1; | |
905 else if (s->qscale > 31) | |
906 s->qscale = 31; | |
907 } | |
908 } | |
909 | |
910 /* decode each block */ | |
911 if (s->h263_pred) { | |
912 for (i = 0; i < 6; i++) { | |
913 if (mpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
914 return -1; | |
915 } | |
916 } else { | |
917 for (i = 0; i < 6; i++) { | |
918 if (h263_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
919 return -1; | |
920 } | |
921 } | |
922 return 0; | |
923 } | |
924 | |
925 static int h263_decode_motion(MpegEncContext * s, int pred) | |
926 { | |
927 int code, val, sign, shift, l, m; | |
928 | |
929 code = get_vlc(&s->gb, &mv_vlc); | |
930 if (code < 0) | |
931 return 0xffff; | |
932 | |
933 if (code == 0) | |
934 return pred; | |
21 | 935 sign = get_bits1(&s->gb); |
0 | 936 shift = s->f_code - 1; |
937 val = (code - 1) << shift; | |
938 if (shift > 0) | |
939 val |= get_bits(&s->gb, shift); | |
940 val++; | |
941 if (sign) | |
942 val = -val; | |
943 val += pred; | |
944 | |
945 /* modulo decoding */ | |
946 if (!s->h263_long_vectors) { | |
947 l = (1 << (s->f_code - 1)) * 32; | |
948 m = 2 * l; | |
949 if (val < -l) { | |
950 val += m; | |
951 } else if (val >= l) { | |
952 val -= m; | |
953 } | |
954 } else { | |
955 /* horrible h263 long vector mode */ | |
956 if (pred < -31 && val < -63) | |
957 val += 64; | |
958 if (pred > 32 && val > 63) | |
959 val -= 64; | |
960 } | |
961 return val; | |
962 } | |
963 | |
78 | 964 /* Decodes RVLC of H.263+ UMV */ |
965 static int h263p_decode_umotion(MpegEncContext * s, int pred) | |
966 { | |
967 int code = 0, sign; | |
968 | |
969 if (get_bits1(&s->gb)) /* Motion difference = 0 */ | |
970 return pred; | |
971 | |
972 code = 2 + get_bits1(&s->gb); | |
973 | |
974 while (get_bits1(&s->gb)) | |
975 { | |
976 code <<= 1; | |
977 code += get_bits1(&s->gb); | |
978 } | |
979 sign = code & 1; | |
980 code >>= 1; | |
981 | |
982 code = (sign) ? (pred - code) : (pred + code); | |
983 #ifdef DEBUG | |
984 fprintf(stderr,"H.263+ UMV Motion = %d\n", code); | |
985 #endif | |
986 return code; | |
987 | |
988 } | |
989 | |
0 | 990 static int h263_decode_block(MpegEncContext * s, DCTELEM * block, |
991 int n, int coded) | |
992 { | |
993 int code, level, i, j, last, run; | |
994 RLTable *rl = &rl_inter; | |
995 | |
996 if (s->mb_intra) { | |
997 /* DC coef */ | |
998 if (s->h263_rv10 && s->rv10_version == 3 && s->pict_type == I_TYPE) { | |
999 int component, diff; | |
1000 component = (n <= 3 ? 0 : n - 4 + 1); | |
1001 level = s->last_dc[component]; | |
1002 if (s->rv10_first_dc_coded[component]) { | |
1003 diff = rv_decode_dc(s, n); | |
1004 if (diff == 0xffff) | |
1005 return -1; | |
1006 level += diff; | |
1007 level = level & 0xff; /* handle wrap round */ | |
1008 s->last_dc[component] = level; | |
1009 } else { | |
1010 s->rv10_first_dc_coded[component] = 1; | |
1011 } | |
1012 } else { | |
1013 level = get_bits(&s->gb, 8); | |
1014 if (level == 255) | |
1015 level = 128; | |
1016 } | |
1017 block[0] = level; | |
1018 i = 1; | |
1019 } else { | |
1020 i = 0; | |
1021 } | |
1022 if (!coded) { | |
1023 s->block_last_index[n] = i - 1; | |
1024 return 0; | |
1025 } | |
1026 | |
1027 for(;;) { | |
1028 code = get_vlc(&s->gb, &rl->vlc); | |
1029 if (code < 0) | |
1030 return -1; | |
1031 if (code == rl->n) { | |
1032 /* escape */ | |
21 | 1033 last = get_bits1(&s->gb); |
0 | 1034 run = get_bits(&s->gb, 6); |
1035 level = (INT8)get_bits(&s->gb, 8); | |
1036 if (s->h263_rv10 && level == -128) { | |
1037 /* XXX: should patch encoder too */ | |
1038 level = get_bits(&s->gb, 12); | |
1039 level = (level << 20) >> 20; | |
1040 } | |
1041 } else { | |
1042 run = rl->table_run[code]; | |
1043 level = rl->table_level[code]; | |
1044 last = code >= rl->last; | |
21 | 1045 if (get_bits1(&s->gb)) |
0 | 1046 level = -level; |
1047 } | |
1048 i += run; | |
1049 if (i >= 64) | |
1050 return -1; | |
1051 j = zigzag_direct[i]; | |
1052 block[j] = level; | |
1053 if (last) | |
1054 break; | |
1055 i++; | |
1056 } | |
1057 s->block_last_index[n] = i; | |
1058 return 0; | |
1059 } | |
1060 | |
1061 static int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | |
1062 { | |
1063 int level, pred, code; | |
1064 UINT16 *dc_val; | |
1065 | |
1066 if (n < 4) | |
1067 code = get_vlc(&s->gb, &dc_lum); | |
1068 else | |
1069 code = get_vlc(&s->gb, &dc_chrom); | |
1070 if (code < 0) | |
1071 return -1; | |
1072 if (code == 0) { | |
1073 level = 0; | |
1074 } else { | |
1075 level = get_bits(&s->gb, code); | |
1076 if ((level >> (code - 1)) == 0) /* if MSB not set it is negative*/ | |
1077 level = - (level ^ ((1 << code) - 1)); | |
1078 if (code > 8) | |
21 | 1079 skip_bits1(&s->gb); /* marker */ |
0 | 1080 } |
1081 | |
1082 pred = mpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
1083 level += pred; | |
1084 if (level < 0) | |
1085 level = 0; | |
1086 if (n < 4) { | |
1087 *dc_val = level * s->y_dc_scale; | |
1088 } else { | |
1089 *dc_val = level * s->c_dc_scale; | |
1090 } | |
1091 return level; | |
1092 } | |
1093 | |
1094 static int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
1095 int n, int coded) | |
1096 { | |
1097 int code, level, i, j, last, run; | |
1098 int dc_pred_dir; | |
1099 RLTable *rl; | |
1100 const UINT8 *scan_table; | |
1101 | |
1102 if (s->mb_intra) { | |
1103 /* DC coef */ | |
1104 level = mpeg4_decode_dc(s, n, &dc_pred_dir); | |
1105 if (level < 0) | |
1106 return -1; | |
1107 block[0] = level; | |
1108 i = 1; | |
1109 if (!coded) | |
1110 goto not_coded; | |
1111 rl = &rl_intra; | |
1112 if (s->ac_pred) { | |
1113 if (dc_pred_dir == 0) | |
1114 scan_table = ff_alternate_vertical_scan; /* left */ | |
1115 else | |
1116 scan_table = ff_alternate_horizontal_scan; /* top */ | |
1117 } else { | |
1118 scan_table = zigzag_direct; | |
1119 } | |
1120 } else { | |
1121 i = 0; | |
1122 if (!coded) { | |
1123 s->block_last_index[n] = i - 1; | |
1124 return 0; | |
1125 } | |
1126 rl = &rl_inter; | |
1127 scan_table = zigzag_direct; | |
1128 } | |
1129 | |
1130 for(;;) { | |
1131 code = get_vlc(&s->gb, &rl->vlc); | |
1132 if (code < 0) | |
1133 return -1; | |
1134 if (code == rl->n) { | |
1135 /* escape */ | |
21 | 1136 if (get_bits1(&s->gb) != 0) { |
1137 if (get_bits1(&s->gb) != 0) { | |
0 | 1138 /* third escape */ |
21 | 1139 last = get_bits1(&s->gb); |
0 | 1140 run = get_bits(&s->gb, 6); |
21 | 1141 get_bits1(&s->gb); /* marker */ |
0 | 1142 level = get_bits(&s->gb, 12); |
1143 level = (level << 20) >> 20; /* sign extend */ | |
21 | 1144 skip_bits1(&s->gb); /* marker */ |
0 | 1145 } else { |
1146 /* second escape */ | |
1147 code = get_vlc(&s->gb, &rl->vlc); | |
1148 if (code < 0 || code >= rl->n) | |
1149 return -1; | |
1150 run = rl->table_run[code]; | |
1151 level = rl->table_level[code]; | |
1152 last = code >= rl->last; | |
1153 run += rl->max_run[last][level] + 1; | |
21 | 1154 if (get_bits1(&s->gb)) |
0 | 1155 level = -level; |
1156 } | |
1157 } else { | |
1158 /* first escape */ | |
1159 code = get_vlc(&s->gb, &rl->vlc); | |
1160 if (code < 0 || code >= rl->n) | |
1161 return -1; | |
1162 run = rl->table_run[code]; | |
1163 level = rl->table_level[code]; | |
1164 last = code >= rl->last; | |
1165 level += rl->max_level[last][run]; | |
21 | 1166 if (get_bits1(&s->gb)) |
0 | 1167 level = -level; |
1168 } | |
1169 } else { | |
1170 run = rl->table_run[code]; | |
1171 level = rl->table_level[code]; | |
1172 last = code >= rl->last; | |
21 | 1173 if (get_bits1(&s->gb)) |
0 | 1174 level = -level; |
1175 } | |
1176 i += run; | |
1177 if (i >= 64) | |
1178 return -1; | |
1179 j = scan_table[i]; | |
1180 block[j] = level; | |
1181 i++; | |
1182 if (last) | |
1183 break; | |
1184 } | |
1185 not_coded: | |
1186 if (s->mb_intra) { | |
1187 mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
1188 if (s->ac_pred) { | |
1189 i = 64; /* XXX: not optimal */ | |
1190 } | |
1191 } | |
1192 s->block_last_index[n] = i - 1; | |
1193 return 0; | |
1194 } | |
1195 | |
1196 /* most is hardcoded. should extend to handle all h263 streams */ | |
1197 int h263_decode_picture_header(MpegEncContext *s) | |
1198 { | |
1199 int format, width, height; | |
1200 | |
1201 /* picture header */ | |
1202 if (get_bits(&s->gb, 22) != 0x20) | |
1203 return -1; | |
21 | 1204 skip_bits(&s->gb, 8); /* picture timestamp */ |
0 | 1205 |
21 | 1206 if (get_bits1(&s->gb) != 1) |
0 | 1207 return -1; /* marker */ |
21 | 1208 if (get_bits1(&s->gb) != 0) |
0 | 1209 return -1; /* h263 id */ |
21 | 1210 skip_bits1(&s->gb); /* split screen off */ |
1211 skip_bits1(&s->gb); /* camera off */ | |
1212 skip_bits1(&s->gb); /* freeze picture release off */ | |
0 | 1213 |
1214 format = get_bits(&s->gb, 3); | |
1215 | |
1216 if (format != 7) { | |
1217 s->h263_plus = 0; | |
1218 /* H.263v1 */ | |
1219 width = h263_format[format][0]; | |
1220 height = h263_format[format][1]; | |
1221 if (!width) | |
1222 return -1; | |
1223 | |
21 | 1224 s->pict_type = I_TYPE + get_bits1(&s->gb); |
0 | 1225 |
21 | 1226 s->unrestricted_mv = get_bits1(&s->gb); |
0 | 1227 s->h263_long_vectors = s->unrestricted_mv; |
1228 | |
21 | 1229 if (get_bits1(&s->gb) != 0) |
0 | 1230 return -1; /* SAC: off */ |
21 | 1231 if (get_bits1(&s->gb) != 0) |
0 | 1232 return -1; /* advanced prediction mode: off */ |
21 | 1233 if (get_bits1(&s->gb) != 0) |
0 | 1234 return -1; /* not PB frame */ |
1235 | |
1236 s->qscale = get_bits(&s->gb, 5); | |
21 | 1237 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */ |
0 | 1238 } else { |
1239 s->h263_plus = 1; | |
1240 /* H.263v2 */ | |
78 | 1241 /* OPPTYPE */ |
1242 | |
1243 if (get_bits(&s->gb, 3) != 1) /* Update Full Extended PTYPE */ | |
0 | 1244 return -1; |
78 | 1245 format = get_bits(&s->gb, 3); |
1246 | |
1247 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
|
1248 s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */ |
78 | 1249 skip_bits(&s->gb, 10); |
1250 skip_bits(&s->gb, 3); /* Reserved */ | |
1251 | |
1252 /* MPPTYPE */ | |
0 | 1253 s->pict_type = get_bits(&s->gb, 3) + 1; |
1254 if (s->pict_type != I_TYPE && | |
1255 s->pict_type != P_TYPE) | |
1256 return -1; | |
21 | 1257 skip_bits(&s->gb, 7); |
78 | 1258 |
1259 /* Get the picture dimensions */ | |
1260 if (format == 6) { | |
1261 /* Custom Picture Format (CPFMT) */ | |
1262 skip_bits(&s->gb, 4); /* aspect ratio */ | |
1263 width = (get_bits(&s->gb, 9) + 1) * 4; | |
1264 skip_bits1(&s->gb); | |
1265 height = get_bits(&s->gb, 9) * 4; | |
1266 #ifdef DEBUG | |
1267 fprintf(stderr,"\nH.263+ Custom picture: %dx%d\n",width,height); | |
1268 #endif | |
1269 } | |
1270 else { | |
1271 width = h263_format[format][0]; | |
1272 height = h263_format[format][1]; | |
1273 } | |
1274 | |
1275 if ((width == 0) || (height == 0)) | |
0 | 1276 return -1; |
78 | 1277 |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
1278 if (s->umvplus_dec) { |
78 | 1279 skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */ |
1280 } | |
1281 | |
0 | 1282 s->qscale = get_bits(&s->gb, 5); |
1283 } | |
1284 /* PEI */ | |
21 | 1285 while (get_bits1(&s->gb) != 0) { |
1286 skip_bits(&s->gb, 8); | |
0 | 1287 } |
1288 s->f_code = 1; | |
1289 s->width = width; | |
1290 s->height = height; | |
144 | 1291 |
0 | 1292 return 0; |
1293 } | |
1294 | |
1295 /* decode mpeg4 VOP header */ | |
1296 int mpeg4_decode_picture_header(MpegEncContext * s) | |
1297 { | |
1298 int time_incr, startcode, state, v; | |
1299 | |
1300 redo: | |
1301 /* search next start code */ | |
1302 align_get_bits(&s->gb); | |
1303 state = 0xff; | |
1304 for(;;) { | |
1305 v = get_bits(&s->gb, 8); | |
1306 if (state == 0x000001) { | |
1307 state = ((state << 8) | v) & 0xffffff; | |
1308 startcode = state; | |
1309 break; | |
1310 } | |
1311 state = ((state << 8) | v) & 0xffffff; | |
1312 /* XXX: really detect end of frame */ | |
1313 if (state == 0) | |
1314 return -1; | |
1315 } | |
1316 | |
1317 if (startcode == 0x120) { | |
63 | 1318 int time_increment_resolution, width, height, vo_ver_id; |
0 | 1319 |
1320 /* vol header */ | |
21 | 1321 skip_bits(&s->gb, 1); /* random access */ |
1322 skip_bits(&s->gb, 8); /* vo_type */ | |
63 | 1323 if (get_bits1(&s->gb) != 0) { /* is_ol_id */ |
1324 vo_ver_id = get_bits(&s->gb, 4); /* vo_ver_id */ | |
1325 skip_bits(&s->gb, 3); /* vo_priority */ | |
1326 } else { | |
1327 vo_ver_id = 1; | |
1328 } | |
0 | 1329 |
21 | 1330 skip_bits(&s->gb, 4); /* aspect_ratio_info */ |
63 | 1331 skip_bits1(&s->gb); /* vol control parameter */ |
1332 s->shape = get_bits(&s->gb, 2); /* vol shape */ | |
21 | 1333 skip_bits1(&s->gb); /* marker */ |
0 | 1334 |
1335 time_increment_resolution = get_bits(&s->gb, 16); | |
70 | 1336 s->time_increment_bits = av_log2(time_increment_resolution - 1) + 1; |
63 | 1337 if (s->time_increment_bits < 1) |
1338 s->time_increment_bits = 1; | |
21 | 1339 skip_bits1(&s->gb); /* marker */ |
0 | 1340 |
63 | 1341 if (get_bits1(&s->gb) != 0) { /* fixed_vop_rate */ |
1342 skip_bits(&s->gb, s->time_increment_bits); | |
1343 } | |
0 | 1344 |
63 | 1345 if (s->shape != 2) { |
1346 if (s->shape == 0) { | |
1347 skip_bits1(&s->gb); /* marker */ | |
1348 width = get_bits(&s->gb, 13); | |
1349 skip_bits1(&s->gb); /* marker */ | |
1350 height = get_bits(&s->gb, 13); | |
1351 skip_bits1(&s->gb); /* marker */ | |
1352 } | |
1353 | |
1354 skip_bits1(&s->gb); /* interlaced */ | |
1355 skip_bits1(&s->gb); /* OBMC */ | |
1356 if (vo_ver_id == 1) { | |
1357 s->vol_sprite_usage = get_bits1(&s->gb); /* vol_sprite_usage */ | |
1358 } else { | |
1359 s->vol_sprite_usage = get_bits(&s->gb, 2); /* vol_sprite_usage */ | |
1360 } | |
1361 if (get_bits1(&s->gb) == 1) { /* not_8_bit */ | |
1362 s->quant_precision = get_bits(&s->gb, 4); /* quant_precision */ | |
1363 skip_bits(&s->gb, 4); /* bits_per_pixel */ | |
1364 } else { | |
1365 s->quant_precision = 5; | |
1366 } | |
1367 | |
1368 skip_bits1(&s->gb); /* vol_quant_type */ | |
1369 skip_bits1(&s->gb); /* vol_quarter_pixel */ | |
1370 skip_bits1(&s->gb); /* complexity_estimation_disabled */ | |
1371 skip_bits1(&s->gb); /* resync_marker_disabled */ | |
1372 skip_bits1(&s->gb); /* data_partioning_enabled */ | |
1373 if (get_bits1(&s->gb) != 0) { /* scalability */ | |
1374 printf("bad scalability!!!\n"); | |
1375 return -1; | |
1376 } | |
1377 } | |
0 | 1378 goto redo; |
1379 } else if (startcode != 0x1b6) { | |
1380 goto redo; | |
1381 } | |
1382 | |
1383 s->pict_type = get_bits(&s->gb, 2) + 1; /* pict type: I = 0 , P = 1 */ | |
1384 if (s->pict_type != I_TYPE && | |
1385 s->pict_type != P_TYPE) | |
1386 return -1; | |
1387 | |
1388 /* XXX: parse time base */ | |
1389 time_incr = 0; | |
21 | 1390 while (get_bits1(&s->gb) != 0) |
0 | 1391 time_incr++; |
1392 | |
21 | 1393 skip_bits1(&s->gb); /* marker */ |
1394 skip_bits(&s->gb, s->time_increment_bits); | |
1395 skip_bits1(&s->gb); /* marker */ | |
0 | 1396 /* vop coded */ |
21 | 1397 if (get_bits1(&s->gb) != 1) |
63 | 1398 goto redo; |
0 | 1399 |
63 | 1400 if (s->shape != 2 && s->pict_type == P_TYPE) { |
0 | 1401 /* rounding type for motion estimation */ |
21 | 1402 s->no_rounding = get_bits1(&s->gb); |
63 | 1403 } else { |
1404 s->no_rounding = 0; | |
0 | 1405 } |
63 | 1406 |
1407 if (s->shape != 0) { | |
1408 if (s->vol_sprite_usage != 1 || s->pict_type != I_TYPE) { | |
1409 int width, height, hor_spat_ref, ver_spat_ref; | |
1410 | |
1411 width = get_bits(&s->gb, 13); | |
1412 skip_bits1(&s->gb); /* marker */ | |
1413 height = get_bits(&s->gb, 13); | |
1414 skip_bits1(&s->gb); /* marker */ | |
1415 hor_spat_ref = get_bits(&s->gb, 13); /* hor_spat_ref */ | |
1416 skip_bits1(&s->gb); /* marker */ | |
1417 ver_spat_ref = get_bits(&s->gb, 13); /* ver_spat_ref */ | |
1418 } | |
1419 skip_bits1(&s->gb); /* change_CR_disable */ | |
1420 | |
1421 if (get_bits1(&s->gb) != 0) { | |
1422 skip_bits(&s->gb, 8); /* constant_alpha_value */ | |
1423 } | |
1424 } | |
0 | 1425 |
63 | 1426 if (s->shape != 2) { |
1427 skip_bits(&s->gb, 3); /* intra dc VLC threshold */ | |
1428 | |
1429 /* note: we do not use quant_precision to avoid problem if no | |
1430 MPEG4 vol header as it is found on some old opendivx | |
1431 movies */ | |
1432 s->qscale = get_bits(&s->gb, 5); | |
1433 | |
1434 if (s->pict_type != I_TYPE) { | |
1435 s->f_code = get_bits(&s->gb, 3); /* fcode_for */ | |
1436 } | |
1437 if (s->shape && (s->pict_type != I_TYPE)) { | |
1438 skip_bits1(&s->gb); // vop shape coding type | |
1439 } | |
1440 } | |
1441 return 0; | |
0 | 1442 } |
1443 | |
1444 /* don't understand why they choose a different header ! */ | |
1445 int intel_h263_decode_picture_header(MpegEncContext *s) | |
1446 { | |
1447 int format; | |
1448 | |
1449 /* picture header */ | |
1450 if (get_bits(&s->gb, 22) != 0x20) | |
1451 return -1; | |
21 | 1452 skip_bits(&s->gb, 8); /* picture timestamp */ |
0 | 1453 |
21 | 1454 if (get_bits1(&s->gb) != 1) |
0 | 1455 return -1; /* marker */ |
21 | 1456 if (get_bits1(&s->gb) != 0) |
0 | 1457 return -1; /* h263 id */ |
21 | 1458 skip_bits1(&s->gb); /* split screen off */ |
1459 skip_bits1(&s->gb); /* camera off */ | |
1460 skip_bits1(&s->gb); /* freeze picture release off */ | |
0 | 1461 |
1462 format = get_bits(&s->gb, 3); | |
1463 if (format != 7) | |
1464 return -1; | |
1465 | |
1466 s->h263_plus = 0; | |
1467 | |
21 | 1468 s->pict_type = I_TYPE + get_bits1(&s->gb); |
0 | 1469 |
21 | 1470 s->unrestricted_mv = get_bits1(&s->gb); |
0 | 1471 s->h263_long_vectors = s->unrestricted_mv; |
1472 | |
21 | 1473 if (get_bits1(&s->gb) != 0) |
0 | 1474 return -1; /* SAC: off */ |
21 | 1475 if (get_bits1(&s->gb) != 0) |
0 | 1476 return -1; /* advanced prediction mode: off */ |
21 | 1477 if (get_bits1(&s->gb) != 0) |
0 | 1478 return -1; /* not PB frame */ |
1479 | |
1480 /* skip unknown header garbage */ | |
21 | 1481 skip_bits(&s->gb, 41); |
0 | 1482 |
1483 s->qscale = get_bits(&s->gb, 5); | |
21 | 1484 skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */ |
0 | 1485 |
1486 /* PEI */ | |
21 | 1487 while (get_bits1(&s->gb) != 0) { |
1488 skip_bits(&s->gb, 8); | |
0 | 1489 } |
1490 s->f_code = 1; | |
1491 return 0; | |
1492 } | |
144 | 1493 |