Mercurial > libavcodec.hg
annotate msmpeg4.c @ 422:aa4a1c6209bd libavcodec
* baling 8 seems to have the same speed
author | kabi |
---|---|
date | Thu, 23 May 2002 16:19:07 +0000 |
parents | 5c8b3a717929 |
children | 718a22dc121f |
rev | line source |
---|---|
0 | 1 /* |
2 * MSMPEG4 backend for ffmpeg encoder and decoder | |
3 * Copyright (c) 2001 Gerard Lantau. | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
18 * |
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
19 * msmpeg4v2 stuff by Michael Niedermayer <michaelni@gmx.at> |
0 | 20 */ |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
347
diff
changeset
|
21 #include "avcodec.h" |
0 | 22 #include "dsputil.h" |
23 #include "mpegvideo.h" | |
24 | |
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
25 |
0 | 26 /* |
27 * You can also call this codec : MPEG4 with a twist ! | |
28 * | |
29 * TODO: | |
30 * - (encoding) select best mv table (two choices) | |
31 * - (encoding) select best vlc/dc table | |
32 */ | |
33 //#define DEBUG | |
34 | |
35 /* motion vector table */ | |
36 typedef struct MVTable { | |
37 int n; | |
38 const UINT16 *table_mv_code; | |
39 const UINT8 *table_mv_bits; | |
40 const UINT8 *table_mvx; | |
41 const UINT8 *table_mvy; | |
42 UINT16 *table_mv_index; /* encoding: convert mv to index in table_mv */ | |
43 VLC vlc; /* decoding: vlc */ | |
44 } MVTable; | |
45 | |
307 | 46 static UINT32 v2_dc_lum_table[512][2]; |
47 static UINT32 v2_dc_chroma_table[512][2]; | |
48 | |
0 | 49 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n); |
50 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
51 int n, int coded); | |
52 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr); | |
53 static int msmpeg4_decode_motion(MpegEncContext * s, | |
54 int *mx_ptr, int *my_ptr); | |
310 | 55 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val); |
56 static void init_h263_dc_for_msmpeg4(); | |
57 | |
0 | 58 |
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
59 extern UINT32 inverse[256]; |
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
60 |
0 | 61 #ifdef DEBUG |
62 int intra_count = 0; | |
63 int frame_count = 0; | |
64 #endif | |
65 /* XXX: move it to mpegvideo.h */ | |
66 | |
67 static int init_done = 0; | |
68 | |
69 #include "msmpeg4data.h" | |
70 | |
71 #ifdef STATS | |
72 | |
73 const char *st_names[ST_NB] = { | |
74 "unknown", | |
75 "dc", | |
76 "intra_ac", | |
77 "inter_ac", | |
78 "intra_mb", | |
79 "inter_mb", | |
80 "mv", | |
81 }; | |
82 | |
83 int st_current_index = 0; | |
84 unsigned int st_bit_counts[ST_NB]; | |
85 unsigned int st_out_bit_counts[ST_NB]; | |
86 | |
87 #define set_stat(var) st_current_index = var; | |
88 | |
89 void print_stats(void) | |
90 { | |
91 unsigned int total; | |
92 int i; | |
93 | |
94 printf("Input:\n"); | |
95 total = 0; | |
96 for(i=0;i<ST_NB;i++) | |
97 total += st_bit_counts[i]; | |
98 if (total == 0) | |
99 total = 1; | |
100 for(i=0;i<ST_NB;i++) { | |
101 printf("%-10s : %10.1f %5.1f%%\n", | |
102 st_names[i], | |
103 (double)st_bit_counts[i] / 8.0, | |
104 (double)st_bit_counts[i] * 100.0 / total); | |
105 } | |
106 printf("%-10s : %10.1f %5.1f%%\n", | |
107 "total", | |
108 (double)total / 8.0, | |
109 100.0); | |
110 | |
111 printf("Output:\n"); | |
112 total = 0; | |
113 for(i=0;i<ST_NB;i++) | |
114 total += st_out_bit_counts[i]; | |
115 if (total == 0) | |
116 total = 1; | |
117 for(i=0;i<ST_NB;i++) { | |
118 printf("%-10s : %10.1f %5.1f%%\n", | |
119 st_names[i], | |
120 (double)st_out_bit_counts[i] / 8.0, | |
121 (double)st_out_bit_counts[i] * 100.0 / total); | |
122 } | |
123 printf("%-10s : %10.1f %5.1f%%\n", | |
124 "total", | |
125 (double)total / 8.0, | |
126 100.0); | |
127 } | |
128 | |
129 #else | |
130 | |
131 #define set_stat(var) | |
132 | |
133 #endif | |
134 | |
135 /* build the table which associate a (x,y) motion vector to a vlc */ | |
136 static void init_mv_table(MVTable *tab) | |
137 { | |
138 int i, x, y; | |
139 | |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
347
diff
changeset
|
140 tab->table_mv_index = av_malloc(sizeof(UINT16) * 4096); |
0 | 141 /* mark all entries as not used */ |
142 for(i=0;i<4096;i++) | |
143 tab->table_mv_index[i] = tab->n; | |
144 | |
145 for(i=0;i<tab->n;i++) { | |
146 x = tab->table_mvx[i]; | |
147 y = tab->table_mvy[i]; | |
148 tab->table_mv_index[(x << 6) | y] = i; | |
149 } | |
150 } | |
151 | |
152 static void code012(PutBitContext *pb, int n) | |
153 { | |
154 if (n == 0) { | |
155 put_bits(pb, 1, 0); | |
156 } else { | |
157 put_bits(pb, 1, 1); | |
158 put_bits(pb, 1, (n >= 2)); | |
159 } | |
160 } | |
161 | |
162 /* write MSMPEG4 V3 compatible frame header */ | |
163 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) | |
164 { | |
165 int i; | |
166 | |
167 align_put_bits(&s->pb); | |
168 | |
169 put_bits(&s->pb, 2, s->pict_type - 1); | |
170 | |
171 put_bits(&s->pb, 5, s->qscale); | |
172 | |
173 s->rl_table_index = 2; | |
310 | 174 if(s->msmpeg4_version==2) |
175 s->rl_chroma_table_index = 2; /* only for I frame */ | |
176 else | |
177 s->rl_chroma_table_index = 1; /* only for I frame */ | |
178 | |
0 | 179 s->dc_table_index = 1; |
180 s->mv_table_index = 1; /* only if P frame */ | |
181 s->use_skip_mb_code = 1; /* only if P frame */ | |
182 | |
183 if (s->pict_type == I_TYPE) { | |
184 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */ | |
185 | |
310 | 186 if(s->msmpeg4_version!=2){ |
187 code012(&s->pb, s->rl_chroma_table_index); | |
188 code012(&s->pb, s->rl_table_index); | |
0 | 189 |
310 | 190 put_bits(&s->pb, 1, s->dc_table_index); |
191 } | |
0 | 192 s->no_rounding = 1; |
193 } else { | |
194 put_bits(&s->pb, 1, s->use_skip_mb_code); | |
195 | |
196 s->rl_chroma_table_index = s->rl_table_index; | |
310 | 197 if(s->msmpeg4_version!=2){ |
198 code012(&s->pb, s->rl_table_index); | |
0 | 199 |
310 | 200 put_bits(&s->pb, 1, s->dc_table_index); |
0 | 201 |
310 | 202 put_bits(&s->pb, 1, s->mv_table_index); |
203 } | |
204 | |
208 | 205 if(s->flipflop_rounding){ |
206 s->no_rounding ^= 1; | |
207 }else{ | |
208 s->no_rounding = 0; | |
209 } | |
0 | 210 } |
211 | |
212 if (!init_done) { | |
213 /* init various encoding tables */ | |
214 init_done = 1; | |
215 init_mv_table(&mv_tables[0]); | |
216 init_mv_table(&mv_tables[1]); | |
217 for(i=0;i<NB_RL_TABLES;i++) | |
218 init_rl(&rl_table[i]); | |
310 | 219 |
220 init_h263_dc_for_msmpeg4(); | |
0 | 221 } |
222 | |
223 #ifdef DEBUG | |
224 intra_count = 0; | |
225 printf("*****frame %d:\n", frame_count++); | |
226 #endif | |
227 } | |
228 | |
208 | 229 void msmpeg4_encode_ext_header(MpegEncContext * s) |
230 { | |
231 s->flipflop_rounding=1; | |
251
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
232 s->bitrate= 910; // FIXME |
208 | 233 |
251
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
234 put_bits(&s->pb, 5, s->frame_rate / FRAME_RATE_BASE); //yes 29.97 -> 29 |
208 | 235 |
236 put_bits(&s->pb, 11, s->bitrate); | |
237 | |
238 put_bits(&s->pb, 1, s->flipflop_rounding); | |
239 } | |
240 | |
0 | 241 /* predict coded block */ |
242 static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr) | |
243 { | |
299 | 244 int xy, wrap, pred, a, b, c; |
0 | 245 |
299 | 246 xy = s->block_index[n]; |
247 wrap = s->block_wrap[0]; | |
0 | 248 |
249 /* B C | |
250 * A X | |
251 */ | |
299 | 252 a = s->coded_block[xy - 1 ]; |
253 b = s->coded_block[xy - 1 - wrap]; | |
254 c = s->coded_block[xy - wrap]; | |
0 | 255 |
256 if (b == c) { | |
257 pred = a; | |
258 } else { | |
259 pred = c; | |
260 } | |
261 | |
262 /* store value */ | |
299 | 263 *coded_block_ptr = &s->coded_block[xy]; |
0 | 264 |
265 return pred; | |
266 } | |
267 | |
268 static void msmpeg4_encode_motion(MpegEncContext * s, | |
269 int mx, int my) | |
270 { | |
271 int code; | |
272 MVTable *mv; | |
273 | |
274 /* modulo encoding */ | |
275 /* WARNING : you cannot reach all the MVs even with the modulo | |
276 encoding. This is a somewhat strange compromise they took !!! */ | |
277 if (mx <= -64) | |
278 mx += 64; | |
279 else if (mx >= 64) | |
280 mx -= 64; | |
281 if (my <= -64) | |
282 my += 64; | |
283 else if (my >= 64) | |
284 my -= 64; | |
285 | |
286 mx += 32; | |
287 my += 32; | |
288 #if 0 | |
289 if ((unsigned)mx >= 64 || | |
290 (unsigned)my >= 64) | |
291 fprintf(stderr, "error mx=%d my=%d\n", mx, my); | |
292 #endif | |
293 mv = &mv_tables[s->mv_table_index]; | |
294 | |
295 code = mv->table_mv_index[(mx << 6) | my]; | |
296 set_stat(ST_MV); | |
297 put_bits(&s->pb, | |
298 mv->table_mv_bits[code], | |
299 mv->table_mv_code[code]); | |
300 if (code == mv->n) { | |
301 /* escape : code litterally */ | |
302 put_bits(&s->pb, 6, mx); | |
303 put_bits(&s->pb, 6, my); | |
304 } | |
305 } | |
306 | |
307 void msmpeg4_encode_mb(MpegEncContext * s, | |
308 DCTELEM block[6][64], | |
309 int motion_x, int motion_y) | |
310 { | |
311 int cbp, coded_cbp, i; | |
312 int pred_x, pred_y; | |
313 UINT8 *coded_block; | |
314 | |
315 if (!s->mb_intra) { | |
316 /* compute cbp */ | |
317 set_stat(ST_INTER_MB); | |
318 cbp = 0; | |
319 for (i = 0; i < 6; i++) { | |
320 if (s->block_last_index[i] >= 0) | |
321 cbp |= 1 << (5 - i); | |
322 } | |
323 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) { | |
324 /* skip macroblock */ | |
325 put_bits(&s->pb, 1, 1); | |
326 return; | |
327 } | |
328 if (s->use_skip_mb_code) | |
329 put_bits(&s->pb, 1, 0); /* mb coded */ | |
330 | |
310 | 331 if(s->msmpeg4_version==2){ |
332 put_bits(&s->pb, | |
333 v2_mb_type[cbp&3][1], | |
334 v2_mb_type[cbp&3][0]); | |
335 if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C; | |
336 else coded_cbp= cbp; | |
0 | 337 |
310 | 338 put_bits(&s->pb, |
339 cbpy_tab[coded_cbp>>2][1], | |
340 cbpy_tab[coded_cbp>>2][0]); | |
341 | |
342 h263_pred_motion(s, 0, &pred_x, &pred_y); | |
343 msmpeg4v2_encode_motion(s, motion_x - pred_x); | |
344 msmpeg4v2_encode_motion(s, motion_y - pred_y); | |
345 }else{ | |
346 put_bits(&s->pb, | |
347 table_mb_non_intra[cbp + 64][1], | |
348 table_mb_non_intra[cbp + 64][0]); | |
349 | |
350 /* motion vector */ | |
351 h263_pred_motion(s, 0, &pred_x, &pred_y); | |
352 msmpeg4_encode_motion(s, motion_x - pred_x, | |
353 motion_y - pred_y); | |
354 } | |
0 | 355 } else { |
356 /* compute cbp */ | |
357 cbp = 0; | |
358 coded_cbp = 0; | |
359 for (i = 0; i < 6; i++) { | |
360 int val, pred; | |
361 val = (s->block_last_index[i] >= 1); | |
362 cbp |= val << (5 - i); | |
363 if (i < 4) { | |
364 /* predict value for close blocks only for luma */ | |
365 pred = coded_block_pred(s, i, &coded_block); | |
366 *coded_block = val; | |
367 val = val ^ pred; | |
368 } | |
369 coded_cbp |= val << (5 - i); | |
370 } | |
371 #if 0 | |
372 if (coded_cbp) | |
373 printf("cbp=%x %x\n", cbp, coded_cbp); | |
374 #endif | |
375 | |
310 | 376 if(s->msmpeg4_version==2){ |
377 if (s->pict_type == I_TYPE) { | |
378 put_bits(&s->pb, | |
379 v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]); | |
380 } else { | |
381 if (s->use_skip_mb_code) | |
382 put_bits(&s->pb, 1, 0); /* mb coded */ | |
383 put_bits(&s->pb, | |
384 v2_mb_type[(cbp&3) + 4][1], | |
385 v2_mb_type[(cbp&3) + 4][0]); | |
386 } | |
387 put_bits(&s->pb, 1, 0); /* no AC prediction yet */ | |
0 | 388 put_bits(&s->pb, |
310 | 389 cbpy_tab[cbp>>2][1], |
390 cbpy_tab[cbp>>2][0]); | |
391 }else{ | |
392 if (s->pict_type == I_TYPE) { | |
393 set_stat(ST_INTRA_MB); | |
394 put_bits(&s->pb, | |
395 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]); | |
396 } else { | |
397 if (s->use_skip_mb_code) | |
398 put_bits(&s->pb, 1, 0); /* mb coded */ | |
399 put_bits(&s->pb, | |
400 table_mb_non_intra[cbp][1], | |
401 table_mb_non_intra[cbp][0]); | |
402 } | |
403 set_stat(ST_INTRA_MB); | |
404 put_bits(&s->pb, 1, 0); /* no AC prediction yet */ | |
0 | 405 } |
406 } | |
407 | |
408 for (i = 0; i < 6; i++) { | |
409 msmpeg4_encode_block(s, block[i], i); | |
410 } | |
411 } | |
412 | |
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
413 /* old ffmpeg msmpeg4v3 mode */ |
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
414 void ff_old_msmpeg4_dc_scale(MpegEncContext * s) |
0 | 415 { |
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
416 if (s->qscale < 5){ |
195
92f726205082
s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
186
diff
changeset
|
417 s->y_dc_scale = 8; |
92f726205082
s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
186
diff
changeset
|
418 s->c_dc_scale = 8; |
92f726205082
s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
186
diff
changeset
|
419 }else if (s->qscale < 9){ |
92f726205082
s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
186
diff
changeset
|
420 s->y_dc_scale = 2 * s->qscale; |
92f726205082
s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
186
diff
changeset
|
421 s->c_dc_scale = (s->qscale + 13)>>1; |
411
5c8b3a717929
workaround dc_scale bug in old ffmpeg msmpeg4v3 encoder (set workaround_bugs=1 for this)
michaelni
parents:
396
diff
changeset
|
422 }else{ |
195
92f726205082
s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
186
diff
changeset
|
423 s->y_dc_scale = s->qscale + 8; |
92f726205082
s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
186
diff
changeset
|
424 s->c_dc_scale = (s->qscale + 13)>>1; |
92f726205082
s->c_dc_scale was 7 if s->qscale==2 but should be 8 (the bug is visible in deep red areas in high bitrate clips) - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
186
diff
changeset
|
425 } |
0 | 426 } |
427 | |
428 /* dir = 0: left, dir = 1: top prediction */ | |
429 static int msmpeg4_pred_dc(MpegEncContext * s, int n, | |
25 | 430 INT16 **dc_val_ptr, int *dir_ptr) |
0 | 431 { |
299 | 432 int a, b, c, wrap, pred, scale; |
25 | 433 INT16 *dc_val; |
0 | 434 |
435 /* find prediction */ | |
436 if (n < 4) { | |
437 scale = s->y_dc_scale; | |
438 } else { | |
439 scale = s->c_dc_scale; | |
440 } | |
299 | 441 wrap = s->block_wrap[n]; |
442 dc_val= s->dc_val[0] + s->block_index[n]; | |
0 | 443 |
444 /* B C | |
445 * A X | |
446 */ | |
299 | 447 a = dc_val[ - 1]; |
448 b = dc_val[ - 1 - wrap]; | |
449 c = dc_val[ - wrap]; | |
0 | 450 |
451 /* XXX: the following solution consumes divisions, but it does not | |
452 necessitate to modify mpegvideo.c. The problem comes from the | |
453 fact they decided to store the quantized DC (which would lead | |
454 to problems if Q could vary !) */ | |
221 | 455 #if defined ARCH_X86 && !defined PIC |
204 | 456 asm volatile( |
457 "movl %3, %%eax \n\t" | |
458 "shrl $1, %%eax \n\t" | |
459 "addl %%eax, %2 \n\t" | |
460 "addl %%eax, %1 \n\t" | |
461 "addl %0, %%eax \n\t" | |
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
462 "mull %4 \n\t" |
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
463 "movl %%edx, %0 \n\t" |
204 | 464 "movl %1, %%eax \n\t" |
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
465 "mull %4 \n\t" |
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
466 "movl %%edx, %1 \n\t" |
204 | 467 "movl %2, %%eax \n\t" |
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
468 "mull %4 \n\t" |
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
469 "movl %%edx, %2 \n\t" |
228 | 470 : "+b" (a), "+c" (b), "+D" (c) |
471 : "g" (scale), "S" (inverse[scale]) | |
204 | 472 : "%eax", "%edx" |
473 ); | |
221 | 474 #else |
475 /* #elif defined (ARCH_ALPHA) */ | |
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
476 /* Divisions are extremely costly on Alpha; optimize the most |
221 | 477 common case. But they are costly everywhere... |
478 */ | |
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
479 if (scale == 8) { |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
480 a = (a + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
481 b = (b + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
482 c = (c + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
483 } else { |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
484 a = (a + (scale >> 1)) / scale; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
485 b = (b + (scale >> 1)) / scale; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
486 c = (c + (scale >> 1)) / scale; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
487 } |
204 | 488 #endif |
0 | 489 /* XXX: WARNING: they did not choose the same test as MPEG4. This |
490 is very important ! */ | |
491 if (abs(a - b) <= abs(b - c)) { | |
492 pred = c; | |
493 *dir_ptr = 1; | |
494 } else { | |
495 pred = a; | |
496 *dir_ptr = 0; | |
497 } | |
498 | |
499 /* update predictor */ | |
299 | 500 *dc_val_ptr = &dc_val[0]; |
0 | 501 return pred; |
502 } | |
503 | |
504 #define DC_MAX 119 | |
505 | |
506 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) | |
507 { | |
508 int sign, code; | |
509 int pred; | |
25 | 510 INT16 *dc_val; |
0 | 511 |
512 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
513 | |
514 /* update predictor */ | |
515 if (n < 4) { | |
516 *dc_val = level * s->y_dc_scale; | |
517 } else { | |
518 *dc_val = level * s->c_dc_scale; | |
519 } | |
520 | |
521 /* do the prediction */ | |
522 level -= pred; | |
523 | |
310 | 524 if(s->msmpeg4_version==2){ |
525 if (n < 4) { | |
526 put_bits(&s->pb, | |
527 v2_dc_lum_table[level+256][1], | |
528 v2_dc_lum_table[level+256][0]); | |
529 }else{ | |
530 put_bits(&s->pb, | |
531 v2_dc_chroma_table[level+256][1], | |
532 v2_dc_chroma_table[level+256][0]); | |
533 } | |
534 }else{ | |
535 sign = 0; | |
536 if (level < 0) { | |
537 level = -level; | |
538 sign = 1; | |
539 } | |
540 code = level; | |
541 if (code > DC_MAX) | |
542 code = DC_MAX; | |
0 | 543 |
310 | 544 if (s->dc_table_index == 0) { |
545 if (n < 4) { | |
546 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]); | |
547 } else { | |
548 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]); | |
549 } | |
0 | 550 } else { |
310 | 551 if (n < 4) { |
552 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]); | |
553 } else { | |
554 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]); | |
555 } | |
0 | 556 } |
310 | 557 |
558 if (code == DC_MAX) | |
559 put_bits(&s->pb, 8, level); | |
560 | |
561 if (level != 0) { | |
562 put_bits(&s->pb, 1, sign); | |
563 } | |
0 | 564 } |
565 } | |
566 | |
567 /* Encoding of a block. Very similar to MPEG4 except for a different | |
568 escape coding (same as H263) and more vlc tables. | |
569 */ | |
570 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n) | |
571 { | |
572 int level, run, last, i, j, last_index; | |
573 int last_non_zero, sign, slevel; | |
574 int code, run_diff, dc_pred_dir; | |
575 const RLTable *rl; | |
576 | |
577 if (s->mb_intra) { | |
578 set_stat(ST_DC); | |
579 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir); | |
580 i = 1; | |
581 if (n < 4) { | |
582 rl = &rl_table[s->rl_table_index]; | |
583 } else { | |
584 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
585 } | |
586 run_diff = 0; | |
587 set_stat(ST_INTRA_AC); | |
588 } else { | |
589 i = 0; | |
590 rl = &rl_table[3 + s->rl_table_index]; | |
310 | 591 if(s->msmpeg4_version==2) |
592 run_diff = 0; | |
593 else | |
594 run_diff = 1; | |
0 | 595 set_stat(ST_INTER_AC); |
596 } | |
597 | |
598 /* AC coefs */ | |
599 last_index = s->block_last_index[n]; | |
600 last_non_zero = i - 1; | |
601 for (; i <= last_index; i++) { | |
602 j = zigzag_direct[i]; | |
603 level = block[j]; | |
604 if (level) { | |
605 run = i - last_non_zero - 1; | |
606 last = (i == last_index); | |
607 sign = 0; | |
608 slevel = level; | |
609 if (level < 0) { | |
610 sign = 1; | |
611 level = -level; | |
612 } | |
613 code = get_rl_index(rl, last, run, level); | |
614 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
615 if (code == rl->n) { | |
616 int level1, run1; | |
617 | |
618 level1 = level - rl->max_level[last][run]; | |
619 if (level1 < 1) | |
620 goto esc2; | |
621 code = get_rl_index(rl, last, run, level1); | |
622 if (code == rl->n) { | |
623 esc2: | |
624 put_bits(&s->pb, 1, 0); | |
625 if (level > MAX_LEVEL) | |
626 goto esc3; | |
627 run1 = run - rl->max_run[last][level] - run_diff; | |
628 if (run1 < 0) | |
629 goto esc3; | |
630 code = get_rl_index(rl, last, run1, level); | |
631 if (code == rl->n) { | |
632 esc3: | |
633 /* third escape */ | |
634 put_bits(&s->pb, 1, 0); | |
635 put_bits(&s->pb, 1, last); | |
636 put_bits(&s->pb, 6, run); | |
637 put_bits(&s->pb, 8, slevel & 0xff); | |
638 } else { | |
639 /* second escape */ | |
640 put_bits(&s->pb, 1, 1); | |
641 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
642 put_bits(&s->pb, 1, sign); | |
643 } | |
644 } else { | |
645 /* first escape */ | |
646 put_bits(&s->pb, 1, 1); | |
647 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
648 put_bits(&s->pb, 1, sign); | |
649 } | |
650 } else { | |
651 put_bits(&s->pb, 1, sign); | |
652 } | |
653 last_non_zero = i; | |
654 } | |
655 } | |
656 } | |
657 | |
658 /****************************************/ | |
659 /* decoding stuff */ | |
660 | |
661 static VLC mb_non_intra_vlc; | |
662 static VLC mb_intra_vlc; | |
663 static VLC dc_lum_vlc[2]; | |
664 static VLC dc_chroma_vlc[2]; | |
307 | 665 static VLC v2_dc_lum_vlc; |
666 static VLC v2_dc_chroma_vlc; | |
667 static VLC cbpy_vlc; | |
668 static VLC v2_intra_cbpc_vlc; | |
669 static VLC v2_mb_type_vlc; | |
670 static VLC v2_mv_vlc; | |
671 | |
672 /* this table is practically identical to the one from h263 except that its inverted */ | |
673 static void init_h263_dc_for_msmpeg4() | |
674 { | |
675 static int inited=0; | |
676 | |
677 if(!inited){ | |
678 int level, uni_code, uni_len; | |
679 inited=1; | |
680 | |
309 | 681 for(level=-256; level<256; level++){ |
307 | 682 int size, v, l; |
683 /* find number of bits */ | |
684 size = 0; | |
685 v = abs(level); | |
686 while (v) { | |
687 v >>= 1; | |
688 size++; | |
689 } | |
690 | |
691 if (level < 0) | |
692 l= (-level) ^ ((1 << size) - 1); | |
693 else | |
694 l= level; | |
695 | |
696 /* luminance h263 */ | |
697 uni_code= DCtab_lum[size][0]; | |
698 uni_len = DCtab_lum[size][1]; | |
699 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
700 | |
701 if (size > 0) { | |
702 uni_code<<=size; uni_code|=l; | |
703 uni_len+=size; | |
704 if (size > 8){ | |
705 uni_code<<=1; uni_code|=1; | |
706 uni_len++; | |
707 } | |
708 } | |
709 v2_dc_lum_table[level+256][0]= uni_code; | |
710 v2_dc_lum_table[level+256][1]= uni_len; | |
711 | |
712 /* chrominance h263 */ | |
713 uni_code= DCtab_chrom[size][0]; | |
714 uni_len = DCtab_chrom[size][1]; | |
715 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
716 | |
717 if (size > 0) { | |
718 uni_code<<=size; uni_code|=l; | |
719 uni_len+=size; | |
720 if (size > 8){ | |
721 uni_code<<=1; uni_code|=1; | |
722 uni_len++; | |
723 } | |
724 } | |
725 v2_dc_chroma_table[level+256][0]= uni_code; | |
726 v2_dc_chroma_table[level+256][1]= uni_len; | |
727 | |
728 } | |
729 } | |
730 } | |
0 | 731 |
732 /* init all vlc decoding tables */ | |
733 int msmpeg4_decode_init_vlc(MpegEncContext *s) | |
734 { | |
735 int i; | |
736 MVTable *mv; | |
737 | |
738 for(i=0;i<NB_RL_TABLES;i++) { | |
739 init_rl(&rl_table[i]); | |
740 init_vlc_rl(&rl_table[i]); | |
741 } | |
742 for(i=0;i<2;i++) { | |
743 mv = &mv_tables[i]; | |
744 init_vlc(&mv->vlc, 9, mv->n + 1, | |
745 mv->table_mv_bits, 1, 1, | |
746 mv->table_mv_code, 2, 2); | |
747 } | |
748 | |
749 init_vlc(&dc_lum_vlc[0], 9, 120, | |
750 &table0_dc_lum[0][1], 8, 4, | |
751 &table0_dc_lum[0][0], 8, 4); | |
752 init_vlc(&dc_chroma_vlc[0], 9, 120, | |
753 &table0_dc_chroma[0][1], 8, 4, | |
754 &table0_dc_chroma[0][0], 8, 4); | |
755 init_vlc(&dc_lum_vlc[1], 9, 120, | |
756 &table1_dc_lum[0][1], 8, 4, | |
757 &table1_dc_lum[0][0], 8, 4); | |
758 init_vlc(&dc_chroma_vlc[1], 9, 120, | |
759 &table1_dc_chroma[0][1], 8, 4, | |
760 &table1_dc_chroma[0][0], 8, 4); | |
307 | 761 |
762 init_h263_dc_for_msmpeg4(); | |
763 init_vlc(&v2_dc_lum_vlc, 9, 512, | |
764 &v2_dc_lum_table[0][1], 8, 4, | |
765 &v2_dc_lum_table[0][0], 8, 4); | |
766 init_vlc(&v2_dc_chroma_vlc, 9, 512, | |
767 &v2_dc_chroma_table[0][1], 8, 4, | |
768 &v2_dc_chroma_table[0][0], 8, 4); | |
769 | |
770 init_vlc(&cbpy_vlc, 6, 16, | |
771 &cbpy_tab[0][1], 2, 1, | |
772 &cbpy_tab[0][0], 2, 1); | |
773 init_vlc(&v2_intra_cbpc_vlc, 3, 4, | |
774 &v2_intra_cbpc[0][1], 2, 1, | |
775 &v2_intra_cbpc[0][0], 2, 1); | |
776 init_vlc(&v2_mb_type_vlc, 5, 8, | |
777 &v2_mb_type[0][1], 2, 1, | |
778 &v2_mb_type[0][0], 2, 1); | |
779 init_vlc(&v2_mv_vlc, 9, 33, | |
780 &mvtab[0][1], 2, 1, | |
781 &mvtab[0][0], 2, 1); | |
0 | 782 |
783 init_vlc(&mb_non_intra_vlc, 9, 128, | |
784 &table_mb_non_intra[0][1], 8, 4, | |
785 &table_mb_non_intra[0][0], 8, 4); | |
48 | 786 init_vlc(&mb_intra_vlc, 9, 64, |
0 | 787 &table_mb_intra[0][1], 4, 2, |
788 &table_mb_intra[0][0], 4, 2); | |
789 return 0; | |
790 } | |
791 | |
792 static int decode012(GetBitContext *gb) | |
793 { | |
794 int n; | |
21 | 795 n = get_bits1(gb); |
0 | 796 if (n == 0) |
797 return 0; | |
798 else | |
21 | 799 return get_bits1(gb) + 1; |
0 | 800 } |
801 | |
307 | 802 int msmpeg4_decode_picture_header(MpegEncContext * s) |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
803 { |
311 | 804 int code, code2; |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
805 |
311 | 806 #if 0 |
807 { | |
808 int i; | |
809 for(i=0; i<s->gb.size*8; i++) | |
810 printf("%d", get_bits1(&s->gb)); | |
811 // get_bits1(&s->gb); | |
812 printf("END\n"); | |
813 return -1; | |
814 } | |
815 #endif | |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
816 s->pict_type = get_bits(&s->gb, 2) + 1; |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
817 if (s->pict_type != I_TYPE && |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
818 s->pict_type != P_TYPE) |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
819 return -1; |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
820 |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
821 s->qscale = get_bits(&s->gb, 5); |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
822 |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
823 if (s->pict_type == I_TYPE) { |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
824 code = get_bits(&s->gb, 5); |
307 | 825 /* 0x17: one slice, 0x18: two slices */ |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
826 if (code < 0x17) |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
827 return -1; |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
828 s->slice_height = s->mb_height / (code - 0x16); |
311 | 829 |
830 switch(s->msmpeg4_version){ | |
831 case 2: | |
307 | 832 s->rl_chroma_table_index = 2; |
833 s->rl_table_index = 2; | |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
834 |
307 | 835 s->dc_table_index = 0; //not used |
311 | 836 break; |
837 case 3: | |
307 | 838 s->rl_chroma_table_index = decode012(&s->gb); |
839 s->rl_table_index = decode012(&s->gb); | |
0 | 840 |
307 | 841 s->dc_table_index = get_bits1(&s->gb); |
311 | 842 break; |
843 case 4: | |
844 msmpeg4_decode_ext_header(s, 999 /* bufer size (useless here) */); | |
845 printf("%X\n", show_bits(&s->gb, 24)); | |
846 code= get_bits(&s->gb, 2); | |
847 if(code==1){ | |
848 code2= get_bits(&s->gb, 3); | |
849 if(code2==7) skip_bits(&s->gb, 1); | |
850 } | |
851 printf("%X\n", show_bits(&s->gb, 24)); | |
852 s->rl_chroma_table_index = 2; | |
853 s->rl_table_index = 2; | |
854 | |
855 s->dc_table_index = 0; | |
856 break; | |
307 | 857 } |
0 | 858 s->no_rounding = 1; |
200 | 859 /* printf(" %d %d %d %d \n", |
860 s->qscale, | |
861 s->rl_chroma_table_index, | |
862 s->rl_table_index, | |
863 s->dc_table_index);*/ | |
0 | 864 } else { |
21 | 865 s->use_skip_mb_code = get_bits1(&s->gb); |
0 | 866 |
307 | 867 if(s->msmpeg4_version==2){ |
868 s->rl_table_index = 2; | |
869 s->rl_chroma_table_index = s->rl_table_index; | |
870 | |
871 s->dc_table_index = 0; //not used | |
0 | 872 |
307 | 873 s->mv_table_index = 0; |
874 }else{ | |
875 s->rl_table_index = decode012(&s->gb); | |
876 s->rl_chroma_table_index = s->rl_table_index; | |
0 | 877 |
307 | 878 s->dc_table_index = get_bits1(&s->gb); |
879 | |
880 s->mv_table_index = get_bits1(&s->gb); | |
881 } | |
200 | 882 /* printf(" %d %d %d %d %d \n", |
883 s->use_skip_mb_code, | |
884 s->rl_table_index, | |
885 s->rl_chroma_table_index, | |
886 s->dc_table_index, | |
887 s->mv_table_index);*/ | |
208 | 888 if(s->flipflop_rounding){ |
889 s->no_rounding ^= 1; | |
890 }else{ | |
891 s->no_rounding = 0; | |
892 } | |
893 // printf("%d", s->no_rounding); | |
311 | 894 //return -1; |
0 | 895 } |
208 | 896 |
307 | 897 #if 0 |
898 if(s->msmpeg4_version==2) | |
899 { | |
900 int i; | |
901 for(i=0; i<s->gb.size*8; i++) | |
902 // printf("%d", get_bits1(&s->gb)); | |
903 get_bits1(&s->gb); | |
904 printf("END\n"); | |
905 return -1; | |
906 } | |
907 #endif | |
908 | |
0 | 909 #ifdef DEBUG |
910 printf("*****frame %d:\n", frame_count++); | |
911 #endif | |
912 return 0; | |
913 } | |
914 | |
208 | 915 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) |
916 { | |
917 /* the alt_bitstream reader could read over the end so we need to check it */ | |
251
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
918 if(get_bits_count(&s->gb) + 16 < buf_size*8) |
208 | 919 { |
251
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
920 int fps; |
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
921 |
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
922 fps= get_bits(&s->gb, 5); |
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
923 s->bitrate= get_bits(&s->gb, 11); |
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
924 s->flipflop_rounding= get_bits1(&s->gb); |
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
925 |
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
926 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bitrate, s->flipflop_rounding); |
208 | 927 } |
928 else | |
929 { | |
251
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
930 s->flipflop_rounding= 0; |
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
931 s->bitrate= 0; |
208 | 932 } |
251
75091bfc577b
fixing msmpeg4 decoding if fps < 16 (i thought it was a indicator for the ext header, its the fps indeed)
michaelni
parents:
246
diff
changeset
|
933 |
208 | 934 return 0; |
935 } | |
936 | |
246 | 937 static inline void memsetw(short *tab, int val, int n) |
0 | 938 { |
939 int i; | |
940 for(i=0;i<n;i++) | |
941 tab[i] = val; | |
942 } | |
943 | |
310 | 944 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val) |
945 { | |
946 int range, bit_size, sign, code, bits; | |
947 | |
948 if (val == 0) { | |
949 /* zero vector */ | |
950 code = 0; | |
951 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]); | |
952 } else { | |
953 bit_size = s->f_code - 1; | |
954 range = 1 << bit_size; | |
955 if (val <= -64) | |
956 val += 64; | |
957 else if (val >= 64) | |
958 val -= 64; | |
959 | |
960 if (val >= 0) { | |
961 sign = 0; | |
962 } else { | |
963 val = -val; | |
964 sign = 1; | |
965 } | |
966 val--; | |
967 code = (val >> bit_size) + 1; | |
968 bits = val & (range - 1); | |
969 | |
970 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); | |
971 if (bit_size > 0) { | |
972 put_bits(&s->pb, bit_size, bits); | |
973 } | |
974 } | |
975 } | |
976 | |
307 | 977 /* this is identical to h263 except that its range is multiplied by 2 */ |
978 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) | |
979 { | |
980 int code, val, sign, shift; | |
981 | |
982 code = get_vlc(&s->gb, &v2_mv_vlc); | |
983 if (code < 0) | |
984 return 0xffff; | |
985 | |
986 if (code == 0) | |
987 return pred; | |
988 sign = get_bits1(&s->gb); | |
989 shift = f_code - 1; | |
990 val = (code - 1) << shift; | |
991 if (shift > 0) | |
992 val |= get_bits(&s->gb, shift); | |
993 val++; | |
994 if (sign) | |
995 val = -val; | |
996 val += pred; | |
997 | |
998 if (val <= -64) | |
999 val += 64; | |
1000 else if (val >= 64) | |
1001 val -= 64; | |
1002 | |
1003 return val; | |
1004 } | |
1005 | |
1006 | |
1007 int msmpeg4v2_decode_mb(MpegEncContext *s, | |
1008 DCTELEM block[6][64]) | |
1009 { | |
1010 int cbp, code, i; | |
1011 if (s->pict_type == P_TYPE) { | |
1012 if (s->use_skip_mb_code) { | |
1013 if (get_bits1(&s->gb)) { | |
1014 /* skip mb */ | |
1015 s->mb_intra = 0; | |
1016 for(i=0;i<6;i++) | |
1017 s->block_last_index[i] = -1; | |
1018 s->mv_dir = MV_DIR_FORWARD; | |
1019 s->mv_type = MV_TYPE_16X16; | |
1020 s->mv[0][0][0] = 0; | |
1021 s->mv[0][0][1] = 0; | |
1022 s->mb_skiped = 1; | |
1023 return 0; | |
1024 } | |
1025 } | |
1026 | |
1027 code = get_vlc(&s->gb, &v2_mb_type_vlc); | |
1028 s->mb_intra = code >>2; | |
1029 | |
1030 cbp = code & 0x3; | |
1031 } else { | |
1032 s->mb_intra = 1; | |
1033 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc); | |
1034 } | |
1035 | |
1036 if (!s->mb_intra) { | |
1037 int mx, my; | |
1038 | |
1039 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; | |
1040 if((cbp&3) != 3) cbp^= 0x3C; | |
1041 | |
1042 h263_pred_motion(s, 0, &mx, &my); | |
1043 mx= msmpeg4v2_decode_motion(s, mx, 1); | |
1044 my= msmpeg4v2_decode_motion(s, my, 1); | |
1045 | |
1046 s->mv_dir = MV_DIR_FORWARD; | |
1047 s->mv_type = MV_TYPE_16X16; | |
1048 s->mv[0][0][0] = mx; | |
1049 s->mv[0][0][1] = my; | |
1050 } else { | |
1051 s->ac_pred = get_bits1(&s->gb); | |
1052 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; | |
1053 } | |
1054 | |
1055 for (i = 0; i < 6; i++) { | |
1056 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
1057 { | |
1058 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
1059 return -1; | |
1060 } | |
1061 } | |
1062 return 0; | |
1063 } | |
1064 | |
0 | 1065 int msmpeg4_decode_mb(MpegEncContext *s, |
1066 DCTELEM block[6][64]) | |
1067 { | |
1068 int cbp, code, i; | |
1069 UINT8 *coded_val; | |
1070 | |
1071 /* special slice handling */ | |
1072 if (s->mb_x == 0) { | |
122 | 1073 if (s->slice_height && (s->mb_y % s->slice_height) == 0) { |
0 | 1074 int wrap; |
1075 /* reset DC pred (set previous line to 1024) */ | |
1076 wrap = 2 * s->mb_width + 2; | |
1077 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap], | |
1078 1024, 2 * s->mb_width); | |
1079 wrap = s->mb_width + 2; | |
1080 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap], | |
1081 1024, s->mb_width); | |
1082 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap], | |
1083 1024, s->mb_width); | |
186
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
1084 |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
1085 /* reset AC pred (set previous line to 0) */ |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
1086 wrap = s->mb_width * 2 + 2; |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
1087 memsetw(s->ac_val[0][0] + (1 + (2 * s->mb_y) * wrap)*16, |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
1088 0, 2 * s->mb_width*16); |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
1089 wrap = s->mb_width + 2; |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
1090 memsetw(s->ac_val[1][0] + (1 + (s->mb_y) * wrap)*16, |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
1091 0, s->mb_width*16); |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
1092 memsetw(s->ac_val[2][0] + (1 + (s->mb_y) * wrap)*16, |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
1093 0, s->mb_width*16); |
0 | 1094 |
1095 s->first_slice_line = 1; | |
1096 } else { | |
1097 s->first_slice_line = 0; | |
1098 } | |
1099 } | |
1100 | |
307 | 1101 if(s->msmpeg4_version==2) return msmpeg4v2_decode_mb(s, block); //FIXME merge if possible |
1102 | |
0 | 1103 if (s->pict_type == P_TYPE) { |
1104 set_stat(ST_INTER_MB); | |
1105 if (s->use_skip_mb_code) { | |
21 | 1106 if (get_bits1(&s->gb)) { |
0 | 1107 /* skip mb */ |
1108 s->mb_intra = 0; | |
1109 for(i=0;i<6;i++) | |
1110 s->block_last_index[i] = -1; | |
1111 s->mv_dir = MV_DIR_FORWARD; | |
1112 s->mv_type = MV_TYPE_16X16; | |
1113 s->mv[0][0][0] = 0; | |
1114 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
|
1115 s->mb_skiped = 1; |
0 | 1116 return 0; |
1117 } | |
1118 } | |
1119 | |
1120 code = get_vlc(&s->gb, &mb_non_intra_vlc); | |
1121 if (code < 0) | |
1122 return -1; | |
246 | 1123 //s->mb_intra = (code & 0x40) ? 0 : 1; |
1124 s->mb_intra = (~code & 0x40) >> 6; | |
0 | 1125 |
1126 cbp = code & 0x3f; | |
1127 } else { | |
1128 set_stat(ST_INTRA_MB); | |
1129 s->mb_intra = 1; | |
1130 code = get_vlc(&s->gb, &mb_intra_vlc); | |
1131 if (code < 0) | |
1132 return -1; | |
1133 /* predict coded block pattern */ | |
1134 cbp = 0; | |
1135 for(i=0;i<6;i++) { | |
246 | 1136 int val = ((code >> (5 - i)) & 1); |
0 | 1137 if (i < 4) { |
246 | 1138 int pred = coded_block_pred(s, i, &coded_val); |
0 | 1139 val = val ^ pred; |
1140 *coded_val = val; | |
1141 } | |
1142 cbp |= val << (5 - i); | |
1143 } | |
1144 } | |
1145 | |
1146 if (!s->mb_intra) { | |
1147 int mx, my; | |
1148 set_stat(ST_MV); | |
1149 h263_pred_motion(s, 0, &mx, &my); | |
1150 if (msmpeg4_decode_motion(s, &mx, &my) < 0) | |
1151 return -1; | |
1152 s->mv_dir = MV_DIR_FORWARD; | |
1153 s->mv_type = MV_TYPE_16X16; | |
1154 s->mv[0][0][0] = mx; | |
1155 s->mv[0][0][1] = my; | |
1156 } else { | |
1157 set_stat(ST_INTRA_MB); | |
21 | 1158 s->ac_pred = get_bits1(&s->gb); |
0 | 1159 } |
1160 | |
1161 for (i = 0; i < 6; i++) { | |
1162 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
246 | 1163 { |
1164 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
1165 // return -1; | |
1166 } | |
0 | 1167 } |
1168 return 0; | |
1169 } | |
1170 | |
1171 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
1172 int n, int coded) | |
1173 { | |
1174 int code, level, i, j, last, run, run_diff; | |
1175 int dc_pred_dir; | |
1176 RLTable *rl; | |
1177 const UINT8 *scan_table; | |
200 | 1178 int qmul, qadd; |
0 | 1179 |
1180 if (s->mb_intra) { | |
200 | 1181 qmul=1; |
1182 qadd=0; | |
1183 | |
0 | 1184 /* DC coef */ |
1185 set_stat(ST_DC); | |
1186 level = msmpeg4_decode_dc(s, n, &dc_pred_dir); | |
1187 if (level < 0) | |
1188 return -1; | |
1189 block[0] = level; | |
1190 if (n < 4) { | |
1191 rl = &rl_table[s->rl_table_index]; | |
1192 } else { | |
1193 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
1194 } | |
200 | 1195 |
0 | 1196 run_diff = 0; |
1197 i = 1; | |
1198 if (!coded) { | |
1199 goto not_coded; | |
1200 } | |
1201 if (s->ac_pred) { | |
1202 if (dc_pred_dir == 0) | |
1203 scan_table = ff_alternate_vertical_scan; /* left */ | |
1204 else | |
1205 scan_table = ff_alternate_horizontal_scan; /* top */ | |
1206 } else { | |
1207 scan_table = zigzag_direct; | |
1208 } | |
1209 set_stat(ST_INTRA_AC); | |
1210 } else { | |
200 | 1211 qmul = s->qscale << 1; |
1212 qadd = (s->qscale - 1) | 1; | |
0 | 1213 i = 0; |
1214 rl = &rl_table[3 + s->rl_table_index]; | |
307 | 1215 |
1216 if(s->msmpeg4_version==2) | |
1217 run_diff = 0; | |
1218 else | |
1219 run_diff = 1; | |
1220 | |
0 | 1221 if (!coded) { |
1222 s->block_last_index[n] = i - 1; | |
1223 return 0; | |
1224 } | |
1225 scan_table = zigzag_direct; | |
1226 set_stat(ST_INTER_AC); | |
1227 } | |
1228 | |
1229 for(;;) { | |
1230 code = get_vlc(&s->gb, &rl->vlc); | |
1231 if (code < 0) | |
1232 return -1; | |
1233 if (code == rl->n) { | |
1234 /* escape */ | |
21 | 1235 if (get_bits1(&s->gb) == 0) { |
1236 if (get_bits1(&s->gb) == 0) { | |
0 | 1237 /* third escape */ |
21 | 1238 last = get_bits1(&s->gb); |
0 | 1239 run = get_bits(&s->gb, 6); |
1240 level = get_bits(&s->gb, 8); | |
1241 level = (level << 24) >> 24; /* sign extend */ | |
246 | 1242 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; |
1243 if (level>0) level= level * qmul + qadd; | |
200 | 1244 else level= level * qmul - qadd; |
0 | 1245 } else { |
1246 /* second escape */ | |
1247 code = get_vlc(&s->gb, &rl->vlc); | |
1248 if (code < 0 || code >= rl->n) | |
1249 return -1; | |
1250 run = rl->table_run[code]; | |
201 | 1251 level = rl->table_level[code]; |
0 | 1252 last = code >= rl->last; |
1253 run += rl->max_run[last][level] + run_diff; | |
201 | 1254 level= level * qmul + qadd; |
21 | 1255 if (get_bits1(&s->gb)) |
0 | 1256 level = -level; |
1257 } | |
1258 } else { | |
1259 /* first escape */ | |
1260 code = get_vlc(&s->gb, &rl->vlc); | |
1261 if (code < 0 || code >= rl->n) | |
1262 return -1; | |
1263 run = rl->table_run[code]; | |
1264 level = rl->table_level[code]; | |
1265 last = code >= rl->last; | |
1266 level += rl->max_level[last][run]; | |
200 | 1267 level= level * qmul + qadd; |
21 | 1268 if (get_bits1(&s->gb)) |
0 | 1269 level = -level; |
1270 } | |
1271 } else { | |
1272 run = rl->table_run[code]; | |
200 | 1273 level = rl->table_level[code] * qmul + qadd; |
0 | 1274 last = code >= rl->last; |
21 | 1275 if (get_bits1(&s->gb)) |
0 | 1276 level = -level; |
1277 } | |
1278 i += run; | |
1279 if (i >= 64) | |
1280 return -1; | |
311 | 1281 //printf("RL:%d %d %d ", run, level, last); |
0 | 1282 j = scan_table[i]; |
1283 block[j] = level; | |
1284 i++; | |
1285 if (last) | |
1286 break; | |
1287 } | |
1288 not_coded: | |
1289 if (s->mb_intra) { | |
1290 mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
1291 if (s->ac_pred) { | |
1292 i = 64; /* XXX: not optimal */ | |
1293 } | |
1294 } | |
1295 s->block_last_index[n] = i - 1; | |
1296 | |
1297 return 0; | |
1298 } | |
1299 | |
1300 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | |
1301 { | |
1302 int level, pred; | |
25 | 1303 INT16 *dc_val; |
0 | 1304 |
307 | 1305 if(s->msmpeg4_version==2){ |
1306 if (n < 4) { | |
1307 level = get_vlc(&s->gb, &v2_dc_lum_vlc); | |
1308 } else { | |
1309 level = get_vlc(&s->gb, &v2_dc_chroma_vlc); | |
1310 } | |
309 | 1311 if (level < 0) |
307 | 1312 return -1; |
1313 level-=256; | |
1314 }else{ //FIXME optimize use unified tables & index | |
1315 if (n < 4) { | |
1316 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]); | |
1317 } else { | |
1318 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]); | |
1319 } | |
1320 if (level < 0) | |
1321 return -1; | |
1322 | |
1323 if (level == DC_MAX) { | |
1324 level = get_bits(&s->gb, 8); | |
1325 if (get_bits1(&s->gb)) | |
1326 level = -level; | |
1327 } else if (level != 0) { | |
1328 if (get_bits1(&s->gb)) | |
1329 level = -level; | |
1330 } | |
0 | 1331 } |
1332 | |
1333 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
1334 level += pred; | |
1335 | |
1336 /* update predictor */ | |
1337 if (n < 4) { | |
1338 *dc_val = level * s->y_dc_scale; | |
1339 } else { | |
1340 *dc_val = level * s->c_dc_scale; | |
1341 } | |
1342 | |
1343 return level; | |
1344 } | |
1345 | |
1346 static int msmpeg4_decode_motion(MpegEncContext * s, | |
1347 int *mx_ptr, int *my_ptr) | |
1348 { | |
1349 MVTable *mv; | |
1350 int code, mx, my; | |
1351 | |
1352 mv = &mv_tables[s->mv_table_index]; | |
1353 | |
1354 code = get_vlc(&s->gb, &mv->vlc); | |
1355 if (code < 0) | |
1356 return -1; | |
1357 if (code == mv->n) { | |
1358 mx = get_bits(&s->gb, 6); | |
1359 my = get_bits(&s->gb, 6); | |
1360 } else { | |
1361 mx = mv->table_mvx[code]; | |
1362 my = mv->table_mvy[code]; | |
1363 } | |
1364 | |
1365 mx += *mx_ptr - 32; | |
1366 my += *my_ptr - 32; | |
1367 /* WARNING : they do not do exactly modulo encoding */ | |
1368 if (mx <= -64) | |
1369 mx += 64; | |
1370 else if (mx >= 64) | |
1371 mx -= 64; | |
1372 | |
1373 if (my <= -64) | |
1374 my += 64; | |
1375 else if (my >= 64) | |
1376 my -= 64; | |
1377 *mx_ptr = mx; | |
1378 *my_ptr = my; | |
1379 return 0; | |
1380 } |