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