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