Mercurial > libavcodec.hg
annotate msmpeg4.c @ 318:21697f35a9ca libavcodec
- Fixed AC3 decoding for 5:1 AC3 streams. Now when calling av_audio_decode for
AC3 set avcodec_context->channels to the desired number channels, if the
setting is 0 AC3 decoder will set it to the channels found in the
stream.
- Changed ffmpeg to cope with the new "way" of AC3 decoding.
- ASF muxer now uses Tickers for PTS calculations.
author | pulento |
---|---|
date | Tue, 09 Apr 2002 04:52:49 +0000 |
parents | ac677a84d5df |
children | 3c5266cda02d |
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. | |
18 */ | |
19 #include <stdlib.h> | |
20 #include <stdio.h> | |
21 #include "common.h" | |
22 #include "dsputil.h" | |
23 #include "mpegvideo.h" | |
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
|
24 #include "avcodec.h" |
0 | 25 |
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 | |
140 tab->table_mv_index = malloc(sizeof(UINT16) * 4096); | |
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 | |
413 | |
414 /* strongly inspirated from MPEG4, but not exactly the same ! */ | |
415 void msmpeg4_dc_scale(MpegEncContext * s) | |
416 { | |
309 | 417 if (s->qscale < 5 || s->msmpeg4_version==2){ |
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
|
418 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
|
419 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
|
420 }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
|
421 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
|
422 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
|
423 }else{ |
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->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
|
425 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
|
426 } |
0 | 427 } |
428 | |
429 /* dir = 0: left, dir = 1: top prediction */ | |
430 static int msmpeg4_pred_dc(MpegEncContext * s, int n, | |
25 | 431 INT16 **dc_val_ptr, int *dir_ptr) |
0 | 432 { |
299 | 433 int a, b, c, wrap, pred, scale; |
25 | 434 INT16 *dc_val; |
0 | 435 |
436 /* find prediction */ | |
437 if (n < 4) { | |
438 scale = s->y_dc_scale; | |
439 } else { | |
440 scale = s->c_dc_scale; | |
441 } | |
299 | 442 wrap = s->block_wrap[n]; |
443 dc_val= s->dc_val[0] + s->block_index[n]; | |
0 | 444 |
445 /* B C | |
446 * A X | |
447 */ | |
299 | 448 a = dc_val[ - 1]; |
449 b = dc_val[ - 1 - wrap]; | |
450 c = dc_val[ - wrap]; | |
0 | 451 |
452 /* XXX: the following solution consumes divisions, but it does not | |
453 necessitate to modify mpegvideo.c. The problem comes from the | |
454 fact they decided to store the quantized DC (which would lead | |
455 to problems if Q could vary !) */ | |
221 | 456 #if defined ARCH_X86 && !defined PIC |
204 | 457 asm volatile( |
458 "movl %3, %%eax \n\t" | |
459 "shrl $1, %%eax \n\t" | |
460 "addl %%eax, %2 \n\t" | |
461 "addl %%eax, %1 \n\t" | |
462 "addl %0, %%eax \n\t" | |
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
463 "mull %4 \n\t" |
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
464 "movl %%edx, %0 \n\t" |
204 | 465 "movl %1, %%eax \n\t" |
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
466 "mull %4 \n\t" |
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
467 "movl %%edx, %1 \n\t" |
204 | 468 "movl %2, %%eax \n\t" |
225
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
469 "mull %4 \n\t" |
ae145876789d
use multiply instead of divides for DC prediction on X86
michaelni
parents:
221
diff
changeset
|
470 "movl %%edx, %2 \n\t" |
228 | 471 : "+b" (a), "+c" (b), "+D" (c) |
472 : "g" (scale), "S" (inverse[scale]) | |
204 | 473 : "%eax", "%edx" |
474 ); | |
221 | 475 #else |
476 /* #elif defined (ARCH_ALPHA) */ | |
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
477 /* Divisions are extremely costly on Alpha; optimize the most |
221 | 478 common case. But they are costly everywhere... |
479 */ | |
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
480 if (scale == 8) { |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
481 a = (a + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
482 b = (b + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
483 c = (c + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
484 } else { |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
485 a = (a + (scale >> 1)) / scale; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
486 b = (b + (scale >> 1)) / scale; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
487 c = (c + (scale >> 1)) / scale; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
488 } |
204 | 489 #endif |
0 | 490 /* XXX: WARNING: they did not choose the same test as MPEG4. This |
491 is very important ! */ | |
492 if (abs(a - b) <= abs(b - c)) { | |
493 pred = c; | |
494 *dir_ptr = 1; | |
495 } else { | |
496 pred = a; | |
497 *dir_ptr = 0; | |
498 } | |
499 | |
500 /* update predictor */ | |
299 | 501 *dc_val_ptr = &dc_val[0]; |
0 | 502 return pred; |
503 } | |
504 | |
505 #define DC_MAX 119 | |
506 | |
507 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) | |
508 { | |
509 int sign, code; | |
510 int pred; | |
25 | 511 INT16 *dc_val; |
0 | 512 |
513 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
514 | |
515 /* update predictor */ | |
516 if (n < 4) { | |
517 *dc_val = level * s->y_dc_scale; | |
518 } else { | |
519 *dc_val = level * s->c_dc_scale; | |
520 } | |
521 | |
522 /* do the prediction */ | |
523 level -= pred; | |
524 | |
310 | 525 if(s->msmpeg4_version==2){ |
526 if (n < 4) { | |
527 put_bits(&s->pb, | |
528 v2_dc_lum_table[level+256][1], | |
529 v2_dc_lum_table[level+256][0]); | |
530 }else{ | |
531 put_bits(&s->pb, | |
532 v2_dc_chroma_table[level+256][1], | |
533 v2_dc_chroma_table[level+256][0]); | |
534 } | |
535 }else{ | |
536 sign = 0; | |
537 if (level < 0) { | |
538 level = -level; | |
539 sign = 1; | |
540 } | |
541 code = level; | |
542 if (code > DC_MAX) | |
543 code = DC_MAX; | |
0 | 544 |
310 | 545 if (s->dc_table_index == 0) { |
546 if (n < 4) { | |
547 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]); | |
548 } else { | |
549 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]); | |
550 } | |
0 | 551 } else { |
310 | 552 if (n < 4) { |
553 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]); | |
554 } else { | |
555 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]); | |
556 } | |
0 | 557 } |
310 | 558 |
559 if (code == DC_MAX) | |
560 put_bits(&s->pb, 8, level); | |
561 | |
562 if (level != 0) { | |
563 put_bits(&s->pb, 1, sign); | |
564 } | |
0 | 565 } |
566 } | |
567 | |
568 /* Encoding of a block. Very similar to MPEG4 except for a different | |
569 escape coding (same as H263) and more vlc tables. | |
570 */ | |
571 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n) | |
572 { | |
573 int level, run, last, i, j, last_index; | |
574 int last_non_zero, sign, slevel; | |
575 int code, run_diff, dc_pred_dir; | |
576 const RLTable *rl; | |
577 | |
578 if (s->mb_intra) { | |
579 set_stat(ST_DC); | |
580 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir); | |
581 i = 1; | |
582 if (n < 4) { | |
583 rl = &rl_table[s->rl_table_index]; | |
584 } else { | |
585 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
586 } | |
587 run_diff = 0; | |
588 set_stat(ST_INTRA_AC); | |
589 } else { | |
590 i = 0; | |
591 rl = &rl_table[3 + s->rl_table_index]; | |
310 | 592 if(s->msmpeg4_version==2) |
593 run_diff = 0; | |
594 else | |
595 run_diff = 1; | |
0 | 596 set_stat(ST_INTER_AC); |
597 } | |
598 | |
599 /* AC coefs */ | |
600 last_index = s->block_last_index[n]; | |
601 last_non_zero = i - 1; | |
602 for (; i <= last_index; i++) { | |
603 j = zigzag_direct[i]; | |
604 level = block[j]; | |
605 if (level) { | |
606 run = i - last_non_zero - 1; | |
607 last = (i == last_index); | |
608 sign = 0; | |
609 slevel = level; | |
610 if (level < 0) { | |
611 sign = 1; | |
612 level = -level; | |
613 } | |
614 code = get_rl_index(rl, last, run, level); | |
615 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
616 if (code == rl->n) { | |
617 int level1, run1; | |
618 | |
619 level1 = level - rl->max_level[last][run]; | |
620 if (level1 < 1) | |
621 goto esc2; | |
622 code = get_rl_index(rl, last, run, level1); | |
623 if (code == rl->n) { | |
624 esc2: | |
625 put_bits(&s->pb, 1, 0); | |
626 if (level > MAX_LEVEL) | |
627 goto esc3; | |
628 run1 = run - rl->max_run[last][level] - run_diff; | |
629 if (run1 < 0) | |
630 goto esc3; | |
631 code = get_rl_index(rl, last, run1, level); | |
632 if (code == rl->n) { | |
633 esc3: | |
634 /* third escape */ | |
635 put_bits(&s->pb, 1, 0); | |
636 put_bits(&s->pb, 1, last); | |
637 put_bits(&s->pb, 6, run); | |
638 put_bits(&s->pb, 8, slevel & 0xff); | |
639 } else { | |
640 /* second escape */ | |
641 put_bits(&s->pb, 1, 1); | |
642 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
643 put_bits(&s->pb, 1, sign); | |
644 } | |
645 } else { | |
646 /* first escape */ | |
647 put_bits(&s->pb, 1, 1); | |
648 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
649 put_bits(&s->pb, 1, sign); | |
650 } | |
651 } else { | |
652 put_bits(&s->pb, 1, sign); | |
653 } | |
654 last_non_zero = i; | |
655 } | |
656 } | |
657 } | |
658 | |
659 /****************************************/ | |
660 /* decoding stuff */ | |
661 | |
662 static VLC mb_non_intra_vlc; | |
663 static VLC mb_intra_vlc; | |
664 static VLC dc_lum_vlc[2]; | |
665 static VLC dc_chroma_vlc[2]; | |
307 | 666 static VLC v2_dc_lum_vlc; |
667 static VLC v2_dc_chroma_vlc; | |
668 static VLC cbpy_vlc; | |
669 static VLC v2_intra_cbpc_vlc; | |
670 static VLC v2_mb_type_vlc; | |
671 static VLC v2_mv_vlc; | |
672 | |
673 /* this table is practically identical to the one from h263 except that its inverted */ | |
674 static void init_h263_dc_for_msmpeg4() | |
675 { | |
676 static int inited=0; | |
677 | |
678 if(!inited){ | |
679 int level, uni_code, uni_len; | |
680 inited=1; | |
681 | |
309 | 682 for(level=-256; level<256; level++){ |
307 | 683 int size, v, l; |
684 /* find number of bits */ | |
685 size = 0; | |
686 v = abs(level); | |
687 while (v) { | |
688 v >>= 1; | |
689 size++; | |
690 } | |
691 | |
692 if (level < 0) | |
693 l= (-level) ^ ((1 << size) - 1); | |
694 else | |
695 l= level; | |
696 | |
697 /* luminance h263 */ | |
698 uni_code= DCtab_lum[size][0]; | |
699 uni_len = DCtab_lum[size][1]; | |
700 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
701 | |
702 if (size > 0) { | |
703 uni_code<<=size; uni_code|=l; | |
704 uni_len+=size; | |
705 if (size > 8){ | |
706 uni_code<<=1; uni_code|=1; | |
707 uni_len++; | |
708 } | |
709 } | |
710 v2_dc_lum_table[level+256][0]= uni_code; | |
711 v2_dc_lum_table[level+256][1]= uni_len; | |
712 | |
713 /* chrominance h263 */ | |
714 uni_code= DCtab_chrom[size][0]; | |
715 uni_len = DCtab_chrom[size][1]; | |
716 uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility | |
717 | |
718 if (size > 0) { | |
719 uni_code<<=size; uni_code|=l; | |
720 uni_len+=size; | |
721 if (size > 8){ | |
722 uni_code<<=1; uni_code|=1; | |
723 uni_len++; | |
724 } | |
725 } | |
726 v2_dc_chroma_table[level+256][0]= uni_code; | |
727 v2_dc_chroma_table[level+256][1]= uni_len; | |
728 | |
729 } | |
730 } | |
731 } | |
0 | 732 |
733 /* init all vlc decoding tables */ | |
734 int msmpeg4_decode_init_vlc(MpegEncContext *s) | |
735 { | |
736 int i; | |
737 MVTable *mv; | |
738 | |
739 for(i=0;i<NB_RL_TABLES;i++) { | |
740 init_rl(&rl_table[i]); | |
741 init_vlc_rl(&rl_table[i]); | |
742 } | |
743 for(i=0;i<2;i++) { | |
744 mv = &mv_tables[i]; | |
745 init_vlc(&mv->vlc, 9, mv->n + 1, | |
746 mv->table_mv_bits, 1, 1, | |
747 mv->table_mv_code, 2, 2); | |
748 } | |
749 | |
750 init_vlc(&dc_lum_vlc[0], 9, 120, | |
751 &table0_dc_lum[0][1], 8, 4, | |
752 &table0_dc_lum[0][0], 8, 4); | |
753 init_vlc(&dc_chroma_vlc[0], 9, 120, | |
754 &table0_dc_chroma[0][1], 8, 4, | |
755 &table0_dc_chroma[0][0], 8, 4); | |
756 init_vlc(&dc_lum_vlc[1], 9, 120, | |
757 &table1_dc_lum[0][1], 8, 4, | |
758 &table1_dc_lum[0][0], 8, 4); | |
759 init_vlc(&dc_chroma_vlc[1], 9, 120, | |
760 &table1_dc_chroma[0][1], 8, 4, | |
761 &table1_dc_chroma[0][0], 8, 4); | |
307 | 762 |
763 init_h263_dc_for_msmpeg4(); | |
764 init_vlc(&v2_dc_lum_vlc, 9, 512, | |
765 &v2_dc_lum_table[0][1], 8, 4, | |
766 &v2_dc_lum_table[0][0], 8, 4); | |
767 init_vlc(&v2_dc_chroma_vlc, 9, 512, | |
768 &v2_dc_chroma_table[0][1], 8, 4, | |
769 &v2_dc_chroma_table[0][0], 8, 4); | |
770 | |
771 init_vlc(&cbpy_vlc, 6, 16, | |
772 &cbpy_tab[0][1], 2, 1, | |
773 &cbpy_tab[0][0], 2, 1); | |
774 init_vlc(&v2_intra_cbpc_vlc, 3, 4, | |
775 &v2_intra_cbpc[0][1], 2, 1, | |
776 &v2_intra_cbpc[0][0], 2, 1); | |
777 init_vlc(&v2_mb_type_vlc, 5, 8, | |
778 &v2_mb_type[0][1], 2, 1, | |
779 &v2_mb_type[0][0], 2, 1); | |
780 init_vlc(&v2_mv_vlc, 9, 33, | |
781 &mvtab[0][1], 2, 1, | |
782 &mvtab[0][0], 2, 1); | |
0 | 783 |
784 init_vlc(&mb_non_intra_vlc, 9, 128, | |
785 &table_mb_non_intra[0][1], 8, 4, | |
786 &table_mb_non_intra[0][0], 8, 4); | |
48 | 787 init_vlc(&mb_intra_vlc, 9, 64, |
0 | 788 &table_mb_intra[0][1], 4, 2, |
789 &table_mb_intra[0][0], 4, 2); | |
790 return 0; | |
791 } | |
792 | |
793 static int decode012(GetBitContext *gb) | |
794 { | |
795 int n; | |
21 | 796 n = get_bits1(gb); |
0 | 797 if (n == 0) |
798 return 0; | |
799 else | |
21 | 800 return get_bits1(gb) + 1; |
0 | 801 } |
802 | |
307 | 803 int msmpeg4_decode_picture_header(MpegEncContext * s) |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
804 { |
311 | 805 int code, code2; |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
806 |
311 | 807 #if 0 |
808 { | |
809 int i; | |
810 for(i=0; i<s->gb.size*8; i++) | |
811 printf("%d", get_bits1(&s->gb)); | |
812 // get_bits1(&s->gb); | |
813 printf("END\n"); | |
814 return -1; | |
815 } | |
816 #endif | |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
817 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
|
818 if (s->pict_type != I_TYPE && |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
819 s->pict_type != P_TYPE) |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
820 return -1; |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
821 |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
822 s->qscale = get_bits(&s->gb, 5); |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
823 |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
824 if (s->pict_type == I_TYPE) { |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
825 code = get_bits(&s->gb, 5); |
307 | 826 /* 0x17: one slice, 0x18: two slices */ |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
827 if (code < 0x17) |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
828 return -1; |
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
829 s->slice_height = s->mb_height / (code - 0x16); |
311 | 830 |
831 switch(s->msmpeg4_version){ | |
832 case 2: | |
307 | 833 s->rl_chroma_table_index = 2; |
834 s->rl_table_index = 2; | |
300
d874359e58f1
msmpeg4v2 header parser & some dump bits code behind #if 0
michaelni
parents:
299
diff
changeset
|
835 |
307 | 836 s->dc_table_index = 0; //not used |
311 | 837 break; |
838 case 3: | |
307 | 839 s->rl_chroma_table_index = decode012(&s->gb); |
840 s->rl_table_index = decode012(&s->gb); | |
0 | 841 |
307 | 842 s->dc_table_index = get_bits1(&s->gb); |
311 | 843 break; |
844 case 4: | |
845 msmpeg4_decode_ext_header(s, 999 /* bufer size (useless here) */); | |
846 printf("%X\n", show_bits(&s->gb, 24)); | |
847 code= get_bits(&s->gb, 2); | |
848 if(code==1){ | |
849 code2= get_bits(&s->gb, 3); | |
850 if(code2==7) skip_bits(&s->gb, 1); | |
851 } | |
852 printf("%X\n", show_bits(&s->gb, 24)); | |
853 s->rl_chroma_table_index = 2; | |
854 s->rl_table_index = 2; | |
855 | |
856 s->dc_table_index = 0; | |
857 break; | |
307 | 858 } |
0 | 859 s->no_rounding = 1; |
200 | 860 /* printf(" %d %d %d %d \n", |
861 s->qscale, | |
862 s->rl_chroma_table_index, | |
863 s->rl_table_index, | |
864 s->dc_table_index);*/ | |
0 | 865 } else { |
21 | 866 s->use_skip_mb_code = get_bits1(&s->gb); |
0 | 867 |
307 | 868 if(s->msmpeg4_version==2){ |
869 s->rl_table_index = 2; | |
870 s->rl_chroma_table_index = s->rl_table_index; | |
871 | |
872 s->dc_table_index = 0; //not used | |
0 | 873 |
307 | 874 s->mv_table_index = 0; |
875 }else{ | |
876 s->rl_table_index = decode012(&s->gb); | |
877 s->rl_chroma_table_index = s->rl_table_index; | |
0 | 878 |
307 | 879 s->dc_table_index = get_bits1(&s->gb); |
880 | |
881 s->mv_table_index = get_bits1(&s->gb); | |
882 } | |
200 | 883 /* printf(" %d %d %d %d %d \n", |
884 s->use_skip_mb_code, | |
885 s->rl_table_index, | |
886 s->rl_chroma_table_index, | |
887 s->dc_table_index, | |
888 s->mv_table_index);*/ | |
208 | 889 if(s->flipflop_rounding){ |
890 s->no_rounding ^= 1; | |
891 }else{ | |
892 s->no_rounding = 0; | |
893 } | |
894 // printf("%d", s->no_rounding); | |
311 | 895 //return -1; |
0 | 896 } |
208 | 897 |
307 | 898 #if 0 |
899 if(s->msmpeg4_version==2) | |
900 { | |
901 int i; | |
902 for(i=0; i<s->gb.size*8; i++) | |
903 // printf("%d", get_bits1(&s->gb)); | |
904 get_bits1(&s->gb); | |
905 printf("END\n"); | |
906 return -1; | |
907 } | |
908 #endif | |
909 | |
0 | 910 #ifdef DEBUG |
911 printf("*****frame %d:\n", frame_count++); | |
912 #endif | |
913 return 0; | |
914 } | |
915 | |
208 | 916 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) |
917 { | |
918 /* 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
|
919 if(get_bits_count(&s->gb) + 16 < buf_size*8) |
208 | 920 { |
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
|
921 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
|
922 |
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 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
|
924 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
|
925 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
|
926 |
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
|
927 // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bitrate, s->flipflop_rounding); |
208 | 928 } |
929 else | |
930 { | |
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
|
931 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
|
932 s->bitrate= 0; |
208 | 933 } |
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
|
934 |
208 | 935 return 0; |
936 } | |
937 | |
246 | 938 static inline void memsetw(short *tab, int val, int n) |
0 | 939 { |
940 int i; | |
941 for(i=0;i<n;i++) | |
942 tab[i] = val; | |
943 } | |
944 | |
310 | 945 static void msmpeg4v2_encode_motion(MpegEncContext * s, int val) |
946 { | |
947 int range, bit_size, sign, code, bits; | |
948 | |
949 if (val == 0) { | |
950 /* zero vector */ | |
951 code = 0; | |
952 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]); | |
953 } else { | |
954 bit_size = s->f_code - 1; | |
955 range = 1 << bit_size; | |
956 if (val <= -64) | |
957 val += 64; | |
958 else if (val >= 64) | |
959 val -= 64; | |
960 | |
961 if (val >= 0) { | |
962 sign = 0; | |
963 } else { | |
964 val = -val; | |
965 sign = 1; | |
966 } | |
967 val--; | |
968 code = (val >> bit_size) + 1; | |
969 bits = val & (range - 1); | |
970 | |
971 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign); | |
972 if (bit_size > 0) { | |
973 put_bits(&s->pb, bit_size, bits); | |
974 } | |
975 } | |
976 } | |
977 | |
307 | 978 /* this is identical to h263 except that its range is multiplied by 2 */ |
979 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code) | |
980 { | |
981 int code, val, sign, shift; | |
982 | |
983 code = get_vlc(&s->gb, &v2_mv_vlc); | |
984 if (code < 0) | |
985 return 0xffff; | |
986 | |
987 if (code == 0) | |
988 return pred; | |
989 sign = get_bits1(&s->gb); | |
990 shift = f_code - 1; | |
991 val = (code - 1) << shift; | |
992 if (shift > 0) | |
993 val |= get_bits(&s->gb, shift); | |
994 val++; | |
995 if (sign) | |
996 val = -val; | |
997 val += pred; | |
998 | |
999 if (val <= -64) | |
1000 val += 64; | |
1001 else if (val >= 64) | |
1002 val -= 64; | |
1003 | |
1004 return val; | |
1005 } | |
1006 | |
1007 | |
1008 int msmpeg4v2_decode_mb(MpegEncContext *s, | |
1009 DCTELEM block[6][64]) | |
1010 { | |
1011 int cbp, code, i; | |
1012 if (s->pict_type == P_TYPE) { | |
1013 if (s->use_skip_mb_code) { | |
1014 if (get_bits1(&s->gb)) { | |
1015 /* skip mb */ | |
1016 s->mb_intra = 0; | |
1017 for(i=0;i<6;i++) | |
1018 s->block_last_index[i] = -1; | |
1019 s->mv_dir = MV_DIR_FORWARD; | |
1020 s->mv_type = MV_TYPE_16X16; | |
1021 s->mv[0][0][0] = 0; | |
1022 s->mv[0][0][1] = 0; | |
1023 s->mb_skiped = 1; | |
1024 return 0; | |
1025 } | |
1026 } | |
1027 | |
1028 code = get_vlc(&s->gb, &v2_mb_type_vlc); | |
1029 s->mb_intra = code >>2; | |
1030 | |
1031 cbp = code & 0x3; | |
1032 } else { | |
1033 s->mb_intra = 1; | |
1034 cbp= get_vlc(&s->gb, &v2_intra_cbpc_vlc); | |
1035 } | |
1036 | |
1037 if (!s->mb_intra) { | |
1038 int mx, my; | |
1039 | |
1040 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; | |
1041 if((cbp&3) != 3) cbp^= 0x3C; | |
1042 | |
1043 h263_pred_motion(s, 0, &mx, &my); | |
1044 mx= msmpeg4v2_decode_motion(s, mx, 1); | |
1045 my= msmpeg4v2_decode_motion(s, my, 1); | |
1046 | |
1047 s->mv_dir = MV_DIR_FORWARD; | |
1048 s->mv_type = MV_TYPE_16X16; | |
1049 s->mv[0][0][0] = mx; | |
1050 s->mv[0][0][1] = my; | |
1051 } else { | |
1052 s->ac_pred = get_bits1(&s->gb); | |
1053 cbp|= get_vlc(&s->gb, &cbpy_vlc)<<2; | |
1054 } | |
1055 | |
1056 for (i = 0; i < 6; i++) { | |
1057 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
1058 { | |
1059 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
1060 return -1; | |
1061 } | |
1062 } | |
1063 return 0; | |
1064 } | |
1065 | |
0 | 1066 int msmpeg4_decode_mb(MpegEncContext *s, |
1067 DCTELEM block[6][64]) | |
1068 { | |
1069 int cbp, code, i; | |
1070 UINT8 *coded_val; | |
1071 | |
1072 /* special slice handling */ | |
1073 if (s->mb_x == 0) { | |
122 | 1074 if (s->slice_height && (s->mb_y % s->slice_height) == 0) { |
0 | 1075 int wrap; |
1076 /* reset DC pred (set previous line to 1024) */ | |
1077 wrap = 2 * s->mb_width + 2; | |
1078 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap], | |
1079 1024, 2 * s->mb_width); | |
1080 wrap = s->mb_width + 2; | |
1081 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap], | |
1082 1024, s->mb_width); | |
1083 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap], | |
1084 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
|
1085 |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
1086 /* 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
|
1087 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
|
1088 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
|
1089 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
|
1090 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
|
1091 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
|
1092 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
|
1093 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
|
1094 0, s->mb_width*16); |
0 | 1095 |
1096 s->first_slice_line = 1; | |
1097 } else { | |
1098 s->first_slice_line = 0; | |
1099 } | |
1100 } | |
1101 | |
307 | 1102 if(s->msmpeg4_version==2) return msmpeg4v2_decode_mb(s, block); //FIXME merge if possible |
1103 | |
0 | 1104 if (s->pict_type == P_TYPE) { |
1105 set_stat(ST_INTER_MB); | |
1106 if (s->use_skip_mb_code) { | |
21 | 1107 if (get_bits1(&s->gb)) { |
0 | 1108 /* skip mb */ |
1109 s->mb_intra = 0; | |
1110 for(i=0;i<6;i++) | |
1111 s->block_last_index[i] = -1; | |
1112 s->mv_dir = MV_DIR_FORWARD; | |
1113 s->mv_type = MV_TYPE_16X16; | |
1114 s->mv[0][0][0] = 0; | |
1115 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
|
1116 s->mb_skiped = 1; |
0 | 1117 return 0; |
1118 } | |
1119 } | |
1120 | |
1121 code = get_vlc(&s->gb, &mb_non_intra_vlc); | |
1122 if (code < 0) | |
1123 return -1; | |
246 | 1124 //s->mb_intra = (code & 0x40) ? 0 : 1; |
1125 s->mb_intra = (~code & 0x40) >> 6; | |
0 | 1126 |
1127 cbp = code & 0x3f; | |
1128 } else { | |
1129 set_stat(ST_INTRA_MB); | |
1130 s->mb_intra = 1; | |
1131 code = get_vlc(&s->gb, &mb_intra_vlc); | |
1132 if (code < 0) | |
1133 return -1; | |
1134 /* predict coded block pattern */ | |
1135 cbp = 0; | |
1136 for(i=0;i<6;i++) { | |
246 | 1137 int val = ((code >> (5 - i)) & 1); |
0 | 1138 if (i < 4) { |
246 | 1139 int pred = coded_block_pred(s, i, &coded_val); |
0 | 1140 val = val ^ pred; |
1141 *coded_val = val; | |
1142 } | |
1143 cbp |= val << (5 - i); | |
1144 } | |
1145 } | |
1146 | |
1147 if (!s->mb_intra) { | |
1148 int mx, my; | |
1149 set_stat(ST_MV); | |
1150 h263_pred_motion(s, 0, &mx, &my); | |
1151 if (msmpeg4_decode_motion(s, &mx, &my) < 0) | |
1152 return -1; | |
1153 s->mv_dir = MV_DIR_FORWARD; | |
1154 s->mv_type = MV_TYPE_16X16; | |
1155 s->mv[0][0][0] = mx; | |
1156 s->mv[0][0][1] = my; | |
1157 } else { | |
1158 set_stat(ST_INTRA_MB); | |
21 | 1159 s->ac_pred = get_bits1(&s->gb); |
0 | 1160 } |
1161 | |
1162 for (i = 0; i < 6; i++) { | |
1163 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
246 | 1164 { |
1165 fprintf(stderr,"\nIgnoring error while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i); | |
1166 // return -1; | |
1167 } | |
0 | 1168 } |
1169 return 0; | |
1170 } | |
1171 | |
1172 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
1173 int n, int coded) | |
1174 { | |
1175 int code, level, i, j, last, run, run_diff; | |
1176 int dc_pred_dir; | |
1177 RLTable *rl; | |
1178 const UINT8 *scan_table; | |
200 | 1179 int qmul, qadd; |
0 | 1180 |
1181 if (s->mb_intra) { | |
200 | 1182 qmul=1; |
1183 qadd=0; | |
1184 | |
0 | 1185 /* DC coef */ |
1186 set_stat(ST_DC); | |
1187 level = msmpeg4_decode_dc(s, n, &dc_pred_dir); | |
1188 if (level < 0) | |
1189 return -1; | |
1190 block[0] = level; | |
1191 if (n < 4) { | |
1192 rl = &rl_table[s->rl_table_index]; | |
1193 } else { | |
1194 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
1195 } | |
200 | 1196 |
0 | 1197 run_diff = 0; |
1198 i = 1; | |
1199 if (!coded) { | |
1200 goto not_coded; | |
1201 } | |
1202 if (s->ac_pred) { | |
1203 if (dc_pred_dir == 0) | |
1204 scan_table = ff_alternate_vertical_scan; /* left */ | |
1205 else | |
1206 scan_table = ff_alternate_horizontal_scan; /* top */ | |
1207 } else { | |
1208 scan_table = zigzag_direct; | |
1209 } | |
1210 set_stat(ST_INTRA_AC); | |
1211 } else { | |
200 | 1212 qmul = s->qscale << 1; |
1213 qadd = (s->qscale - 1) | 1; | |
0 | 1214 i = 0; |
1215 rl = &rl_table[3 + s->rl_table_index]; | |
307 | 1216 |
1217 if(s->msmpeg4_version==2) | |
1218 run_diff = 0; | |
1219 else | |
1220 run_diff = 1; | |
1221 | |
0 | 1222 if (!coded) { |
1223 s->block_last_index[n] = i - 1; | |
1224 return 0; | |
1225 } | |
1226 scan_table = zigzag_direct; | |
1227 set_stat(ST_INTER_AC); | |
1228 } | |
1229 | |
1230 for(;;) { | |
1231 code = get_vlc(&s->gb, &rl->vlc); | |
1232 if (code < 0) | |
1233 return -1; | |
1234 if (code == rl->n) { | |
1235 /* escape */ | |
21 | 1236 if (get_bits1(&s->gb) == 0) { |
1237 if (get_bits1(&s->gb) == 0) { | |
0 | 1238 /* third escape */ |
21 | 1239 last = get_bits1(&s->gb); |
0 | 1240 run = get_bits(&s->gb, 6); |
1241 level = get_bits(&s->gb, 8); | |
1242 level = (level << 24) >> 24; /* sign extend */ | |
246 | 1243 //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ; |
1244 if (level>0) level= level * qmul + qadd; | |
200 | 1245 else level= level * qmul - qadd; |
0 | 1246 } else { |
1247 /* second escape */ | |
1248 code = get_vlc(&s->gb, &rl->vlc); | |
1249 if (code < 0 || code >= rl->n) | |
1250 return -1; | |
1251 run = rl->table_run[code]; | |
201 | 1252 level = rl->table_level[code]; |
0 | 1253 last = code >= rl->last; |
1254 run += rl->max_run[last][level] + run_diff; | |
201 | 1255 level= level * qmul + qadd; |
21 | 1256 if (get_bits1(&s->gb)) |
0 | 1257 level = -level; |
1258 } | |
1259 } else { | |
1260 /* first escape */ | |
1261 code = get_vlc(&s->gb, &rl->vlc); | |
1262 if (code < 0 || code >= rl->n) | |
1263 return -1; | |
1264 run = rl->table_run[code]; | |
1265 level = rl->table_level[code]; | |
1266 last = code >= rl->last; | |
1267 level += rl->max_level[last][run]; | |
200 | 1268 level= level * qmul + qadd; |
21 | 1269 if (get_bits1(&s->gb)) |
0 | 1270 level = -level; |
1271 } | |
1272 } else { | |
1273 run = rl->table_run[code]; | |
200 | 1274 level = rl->table_level[code] * qmul + qadd; |
0 | 1275 last = code >= rl->last; |
21 | 1276 if (get_bits1(&s->gb)) |
0 | 1277 level = -level; |
1278 } | |
1279 i += run; | |
1280 if (i >= 64) | |
1281 return -1; | |
311 | 1282 //printf("RL:%d %d %d ", run, level, last); |
0 | 1283 j = scan_table[i]; |
1284 block[j] = level; | |
1285 i++; | |
1286 if (last) | |
1287 break; | |
1288 } | |
1289 not_coded: | |
1290 if (s->mb_intra) { | |
1291 mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
1292 if (s->ac_pred) { | |
1293 i = 64; /* XXX: not optimal */ | |
1294 } | |
1295 } | |
1296 s->block_last_index[n] = i - 1; | |
1297 | |
1298 return 0; | |
1299 } | |
1300 | |
1301 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | |
1302 { | |
1303 int level, pred; | |
25 | 1304 INT16 *dc_val; |
0 | 1305 |
307 | 1306 if(s->msmpeg4_version==2){ |
1307 if (n < 4) { | |
1308 level = get_vlc(&s->gb, &v2_dc_lum_vlc); | |
1309 } else { | |
1310 level = get_vlc(&s->gb, &v2_dc_chroma_vlc); | |
1311 } | |
309 | 1312 if (level < 0) |
307 | 1313 return -1; |
1314 level-=256; | |
1315 }else{ //FIXME optimize use unified tables & index | |
1316 if (n < 4) { | |
1317 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]); | |
1318 } else { | |
1319 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]); | |
1320 } | |
1321 if (level < 0) | |
1322 return -1; | |
1323 | |
1324 if (level == DC_MAX) { | |
1325 level = get_bits(&s->gb, 8); | |
1326 if (get_bits1(&s->gb)) | |
1327 level = -level; | |
1328 } else if (level != 0) { | |
1329 if (get_bits1(&s->gb)) | |
1330 level = -level; | |
1331 } | |
0 | 1332 } |
1333 | |
1334 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
1335 level += pred; | |
1336 | |
1337 /* update predictor */ | |
1338 if (n < 4) { | |
1339 *dc_val = level * s->y_dc_scale; | |
1340 } else { | |
1341 *dc_val = level * s->c_dc_scale; | |
1342 } | |
1343 | |
1344 return level; | |
1345 } | |
1346 | |
1347 static int msmpeg4_decode_motion(MpegEncContext * s, | |
1348 int *mx_ptr, int *my_ptr) | |
1349 { | |
1350 MVTable *mv; | |
1351 int code, mx, my; | |
1352 | |
1353 mv = &mv_tables[s->mv_table_index]; | |
1354 | |
1355 code = get_vlc(&s->gb, &mv->vlc); | |
1356 if (code < 0) | |
1357 return -1; | |
1358 if (code == mv->n) { | |
1359 mx = get_bits(&s->gb, 6); | |
1360 my = get_bits(&s->gb, 6); | |
1361 } else { | |
1362 mx = mv->table_mvx[code]; | |
1363 my = mv->table_mvy[code]; | |
1364 } | |
1365 | |
1366 mx += *mx_ptr - 32; | |
1367 my += *my_ptr - 32; | |
1368 /* WARNING : they do not do exactly modulo encoding */ | |
1369 if (mx <= -64) | |
1370 mx += 64; | |
1371 else if (mx >= 64) | |
1372 mx -= 64; | |
1373 | |
1374 if (my <= -64) | |
1375 my += 64; | |
1376 else if (my >= 64) | |
1377 my -= 64; | |
1378 *mx_ptr = mx; | |
1379 *my_ptr = my; | |
1380 return 0; | |
1381 } |