Mercurial > libavcodec.hg
annotate msmpeg4.c @ 222:4f83fb0c957a libavcodec
workaround gcc bug "m" (0)
avoiding useage of ebx
author | arpi_esp |
---|---|
date | Mon, 28 Jan 2002 19:34:16 +0000 |
parents | fe243b4aec02 |
children | ae145876789d |
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" | |
24 | |
25 /* | |
26 * You can also call this codec : MPEG4 with a twist ! | |
27 * | |
28 * TODO: | |
29 * - (encoding) select best mv table (two choices) | |
30 * - (encoding) select best vlc/dc table | |
31 * - (decoding) handle slice indication | |
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 | |
46 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n); | |
47 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
48 int n, int coded); | |
49 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr); | |
50 static int msmpeg4_decode_motion(MpegEncContext * s, | |
51 int *mx_ptr, int *my_ptr); | |
52 | |
53 #ifdef DEBUG | |
54 int intra_count = 0; | |
55 int frame_count = 0; | |
56 #endif | |
57 /* XXX: move it to mpegvideo.h */ | |
58 | |
59 static int init_done = 0; | |
60 | |
61 #include "msmpeg4data.h" | |
62 | |
63 #ifdef STATS | |
64 | |
65 const char *st_names[ST_NB] = { | |
66 "unknown", | |
67 "dc", | |
68 "intra_ac", | |
69 "inter_ac", | |
70 "intra_mb", | |
71 "inter_mb", | |
72 "mv", | |
73 }; | |
74 | |
75 int st_current_index = 0; | |
76 unsigned int st_bit_counts[ST_NB]; | |
77 unsigned int st_out_bit_counts[ST_NB]; | |
78 | |
79 #define set_stat(var) st_current_index = var; | |
80 | |
81 void print_stats(void) | |
82 { | |
83 unsigned int total; | |
84 int i; | |
85 | |
86 printf("Input:\n"); | |
87 total = 0; | |
88 for(i=0;i<ST_NB;i++) | |
89 total += st_bit_counts[i]; | |
90 if (total == 0) | |
91 total = 1; | |
92 for(i=0;i<ST_NB;i++) { | |
93 printf("%-10s : %10.1f %5.1f%%\n", | |
94 st_names[i], | |
95 (double)st_bit_counts[i] / 8.0, | |
96 (double)st_bit_counts[i] * 100.0 / total); | |
97 } | |
98 printf("%-10s : %10.1f %5.1f%%\n", | |
99 "total", | |
100 (double)total / 8.0, | |
101 100.0); | |
102 | |
103 printf("Output:\n"); | |
104 total = 0; | |
105 for(i=0;i<ST_NB;i++) | |
106 total += st_out_bit_counts[i]; | |
107 if (total == 0) | |
108 total = 1; | |
109 for(i=0;i<ST_NB;i++) { | |
110 printf("%-10s : %10.1f %5.1f%%\n", | |
111 st_names[i], | |
112 (double)st_out_bit_counts[i] / 8.0, | |
113 (double)st_out_bit_counts[i] * 100.0 / total); | |
114 } | |
115 printf("%-10s : %10.1f %5.1f%%\n", | |
116 "total", | |
117 (double)total / 8.0, | |
118 100.0); | |
119 } | |
120 | |
121 #else | |
122 | |
123 #define set_stat(var) | |
124 | |
125 #endif | |
126 | |
127 /* build the table which associate a (x,y) motion vector to a vlc */ | |
128 static void init_mv_table(MVTable *tab) | |
129 { | |
130 int i, x, y; | |
131 | |
132 tab->table_mv_index = malloc(sizeof(UINT16) * 4096); | |
133 /* mark all entries as not used */ | |
134 for(i=0;i<4096;i++) | |
135 tab->table_mv_index[i] = tab->n; | |
136 | |
137 for(i=0;i<tab->n;i++) { | |
138 x = tab->table_mvx[i]; | |
139 y = tab->table_mvy[i]; | |
140 tab->table_mv_index[(x << 6) | y] = i; | |
141 } | |
142 } | |
143 | |
144 static void code012(PutBitContext *pb, int n) | |
145 { | |
146 if (n == 0) { | |
147 put_bits(pb, 1, 0); | |
148 } else { | |
149 put_bits(pb, 1, 1); | |
150 put_bits(pb, 1, (n >= 2)); | |
151 } | |
152 } | |
153 | |
154 /* write MSMPEG4 V3 compatible frame header */ | |
155 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number) | |
156 { | |
157 int i; | |
158 | |
159 align_put_bits(&s->pb); | |
160 | |
161 put_bits(&s->pb, 2, s->pict_type - 1); | |
162 | |
163 put_bits(&s->pb, 5, s->qscale); | |
164 | |
165 s->rl_table_index = 2; | |
166 s->rl_chroma_table_index = 1; /* only for I frame */ | |
167 s->dc_table_index = 1; | |
168 s->mv_table_index = 1; /* only if P frame */ | |
169 s->use_skip_mb_code = 1; /* only if P frame */ | |
170 | |
171 if (s->pict_type == I_TYPE) { | |
172 put_bits(&s->pb, 5, 0x17); /* indicate only one "slice" */ | |
173 | |
174 code012(&s->pb, s->rl_chroma_table_index); | |
175 code012(&s->pb, s->rl_table_index); | |
176 | |
177 put_bits(&s->pb, 1, s->dc_table_index); | |
178 s->no_rounding = 1; | |
179 } else { | |
180 put_bits(&s->pb, 1, s->use_skip_mb_code); | |
181 | |
182 s->rl_chroma_table_index = s->rl_table_index; | |
183 code012(&s->pb, s->rl_table_index); | |
184 | |
185 put_bits(&s->pb, 1, s->dc_table_index); | |
186 | |
187 put_bits(&s->pb, 1, s->mv_table_index); | |
208 | 188 |
189 if(s->flipflop_rounding){ | |
190 s->no_rounding ^= 1; | |
191 }else{ | |
192 s->no_rounding = 0; | |
193 } | |
0 | 194 } |
195 | |
196 if (!init_done) { | |
197 /* init various encoding tables */ | |
198 init_done = 1; | |
199 init_mv_table(&mv_tables[0]); | |
200 init_mv_table(&mv_tables[1]); | |
201 for(i=0;i<NB_RL_TABLES;i++) | |
202 init_rl(&rl_table[i]); | |
203 } | |
204 | |
205 #ifdef DEBUG | |
206 intra_count = 0; | |
207 printf("*****frame %d:\n", frame_count++); | |
208 #endif | |
209 } | |
210 | |
208 | 211 void msmpeg4_encode_ext_header(MpegEncContext * s) |
212 { | |
213 if(s->pict_type == P_TYPE) | |
214 { | |
215 return; // P-Frames dont seem to have them and not even a 0 bit | |
216 } | |
217 else | |
218 { | |
219 s->flipflop_rounding=1; | |
220 s->bitrate= 910; | |
221 | |
222 put_bits(&s->pb, 1, 1); // ext header indicator | |
223 | |
224 put_bits(&s->pb, 4, 7); // ? | |
225 | |
226 put_bits(&s->pb, 11, s->bitrate); | |
227 | |
228 put_bits(&s->pb, 1, s->flipflop_rounding); | |
229 } | |
230 } | |
231 | |
0 | 232 /* predict coded block */ |
233 static inline int coded_block_pred(MpegEncContext * s, int n, UINT8 **coded_block_ptr) | |
234 { | |
235 int x, y, wrap, pred, a, b, c; | |
236 | |
237 x = 2 * s->mb_x + 1 + (n & 1); | |
238 y = 2 * s->mb_y + 1 + ((n & 2) >> 1); | |
239 wrap = s->mb_width * 2 + 2; | |
240 | |
241 /* B C | |
242 * A X | |
243 */ | |
244 a = s->coded_block[(x - 1) + (y) * wrap]; | |
245 b = s->coded_block[(x - 1) + (y - 1) * wrap]; | |
246 c = s->coded_block[(x) + (y - 1) * wrap]; | |
247 | |
248 if (b == c) { | |
249 pred = a; | |
250 } else { | |
251 pred = c; | |
252 } | |
253 | |
254 /* store value */ | |
255 *coded_block_ptr = &s->coded_block[(x) + (y) * wrap]; | |
256 | |
257 return pred; | |
258 } | |
259 | |
260 static void msmpeg4_encode_motion(MpegEncContext * s, | |
261 int mx, int my) | |
262 { | |
263 int code; | |
264 MVTable *mv; | |
265 | |
266 /* modulo encoding */ | |
267 /* WARNING : you cannot reach all the MVs even with the modulo | |
268 encoding. This is a somewhat strange compromise they took !!! */ | |
269 if (mx <= -64) | |
270 mx += 64; | |
271 else if (mx >= 64) | |
272 mx -= 64; | |
273 if (my <= -64) | |
274 my += 64; | |
275 else if (my >= 64) | |
276 my -= 64; | |
277 | |
278 mx += 32; | |
279 my += 32; | |
280 #if 0 | |
281 if ((unsigned)mx >= 64 || | |
282 (unsigned)my >= 64) | |
283 fprintf(stderr, "error mx=%d my=%d\n", mx, my); | |
284 #endif | |
285 mv = &mv_tables[s->mv_table_index]; | |
286 | |
287 code = mv->table_mv_index[(mx << 6) | my]; | |
288 set_stat(ST_MV); | |
289 put_bits(&s->pb, | |
290 mv->table_mv_bits[code], | |
291 mv->table_mv_code[code]); | |
292 if (code == mv->n) { | |
293 /* escape : code litterally */ | |
294 put_bits(&s->pb, 6, mx); | |
295 put_bits(&s->pb, 6, my); | |
296 } | |
297 } | |
298 | |
299 void msmpeg4_encode_mb(MpegEncContext * s, | |
300 DCTELEM block[6][64], | |
301 int motion_x, int motion_y) | |
302 { | |
303 int cbp, coded_cbp, i; | |
304 int pred_x, pred_y; | |
305 UINT8 *coded_block; | |
306 | |
307 if (!s->mb_intra) { | |
308 /* compute cbp */ | |
309 set_stat(ST_INTER_MB); | |
310 cbp = 0; | |
311 for (i = 0; i < 6; i++) { | |
312 if (s->block_last_index[i] >= 0) | |
313 cbp |= 1 << (5 - i); | |
314 } | |
315 if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) { | |
316 /* skip macroblock */ | |
317 put_bits(&s->pb, 1, 1); | |
318 return; | |
319 } | |
320 if (s->use_skip_mb_code) | |
321 put_bits(&s->pb, 1, 0); /* mb coded */ | |
322 | |
323 put_bits(&s->pb, | |
324 table_mb_non_intra[cbp + 64][1], | |
325 table_mb_non_intra[cbp + 64][0]); | |
326 | |
327 /* motion vector */ | |
328 h263_pred_motion(s, 0, &pred_x, &pred_y); | |
329 msmpeg4_encode_motion(s, motion_x - pred_x, | |
330 motion_y - pred_y); | |
331 } else { | |
332 /* compute cbp */ | |
333 cbp = 0; | |
334 coded_cbp = 0; | |
335 for (i = 0; i < 6; i++) { | |
336 int val, pred; | |
337 val = (s->block_last_index[i] >= 1); | |
338 cbp |= val << (5 - i); | |
339 if (i < 4) { | |
340 /* predict value for close blocks only for luma */ | |
341 pred = coded_block_pred(s, i, &coded_block); | |
342 *coded_block = val; | |
343 val = val ^ pred; | |
344 } | |
345 coded_cbp |= val << (5 - i); | |
346 } | |
347 #if 0 | |
348 if (coded_cbp) | |
349 printf("cbp=%x %x\n", cbp, coded_cbp); | |
350 #endif | |
351 | |
352 if (s->pict_type == I_TYPE) { | |
353 set_stat(ST_INTRA_MB); | |
354 put_bits(&s->pb, | |
355 table_mb_intra[coded_cbp][1], table_mb_intra[coded_cbp][0]); | |
356 } else { | |
357 if (s->use_skip_mb_code) | |
358 put_bits(&s->pb, 1, 0); /* mb coded */ | |
359 put_bits(&s->pb, | |
360 table_mb_non_intra[cbp][1], | |
361 table_mb_non_intra[cbp][0]); | |
362 } | |
363 set_stat(ST_INTRA_MB); | |
364 put_bits(&s->pb, 1, 0); /* no AC prediction yet */ | |
365 } | |
366 | |
367 for (i = 0; i < 6; i++) { | |
368 msmpeg4_encode_block(s, block[i], i); | |
369 } | |
370 } | |
371 | |
372 | |
373 /* strongly inspirated from MPEG4, but not exactly the same ! */ | |
374 void msmpeg4_dc_scale(MpegEncContext * s) | |
375 { | |
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
|
376 if (s->qscale < 5){ |
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
|
377 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
|
378 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
|
379 // 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
|
380 }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
|
381 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
|
382 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
|
383 }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
|
384 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
|
385 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
|
386 } |
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
|
387 // this differs for quant >24 from mpeg4 |
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
|
388 |
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
|
389 // if(s->qscale==13) s->c_dc_scale=14; |
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
|
390 |
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
|
391 // if(s->qscale>=6) |
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
|
392 // printf("%d", 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
|
393 |
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
|
394 /* s->c_dc_scale values (found by Michael Nidermayer) |
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
|
395 qscale=2 -> 8 (yes iam sure about that) |
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
|
396 qscale=3 -> 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
|
397 qscale=4 -> 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
|
398 qscale=5 -> 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
|
399 qscale=6 -> 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
|
400 qscale=7 -> 10 |
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
|
401 qscale=8 -> 10 |
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
|
402 qscale=9 -> 11 |
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
|
403 qscale=10-> 11 |
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
|
404 */ |
0 | 405 } |
406 | |
407 /* dir = 0: left, dir = 1: top prediction */ | |
408 static int msmpeg4_pred_dc(MpegEncContext * s, int n, | |
25 | 409 INT16 **dc_val_ptr, int *dir_ptr) |
0 | 410 { |
411 int a, b, c, x, y, wrap, pred, scale; | |
25 | 412 INT16 *dc_val; |
0 | 413 |
414 /* find prediction */ | |
415 if (n < 4) { | |
416 x = 2 * s->mb_x + 1 + (n & 1); | |
417 y = 2 * s->mb_y + 1 + ((n & 2) >> 1); | |
418 wrap = s->mb_width * 2 + 2; | |
419 dc_val = s->dc_val[0]; | |
420 scale = s->y_dc_scale; | |
421 } else { | |
422 x = s->mb_x + 1; | |
423 y = s->mb_y + 1; | |
424 wrap = s->mb_width + 2; | |
425 dc_val = s->dc_val[n - 4 + 1]; | |
426 scale = s->c_dc_scale; | |
427 } | |
428 | |
429 /* B C | |
430 * A X | |
431 */ | |
432 a = dc_val[(x - 1) + (y) * wrap]; | |
433 b = dc_val[(x - 1) + (y - 1) * wrap]; | |
434 c = dc_val[(x) + (y - 1) * wrap]; | |
435 | |
436 /* XXX: the following solution consumes divisions, but it does not | |
437 necessitate to modify mpegvideo.c. The problem comes from the | |
438 fact they decided to store the quantized DC (which would lead | |
439 to problems if Q could vary !) */ | |
221 | 440 #if defined ARCH_X86 && !defined PIC |
204 | 441 /* using 16bit divisions as they are large enough and 2x as fast */ |
442 asm volatile( | |
443 "movl %3, %%eax \n\t" | |
444 "shrl $1, %%eax \n\t" | |
445 "addl %%eax, %2 \n\t" | |
446 "addl %%eax, %1 \n\t" | |
447 "addl %0, %%eax \n\t" | |
448 "xorl %%edx, %%edx \n\t" | |
449 "divw %w3 \n\t" | |
450 "movzwl %%ax, %0 \n\t" | |
451 "movl %1, %%eax \n\t" | |
452 "xorl %%edx, %%edx \n\t" | |
453 "divw %w3 \n\t" | |
454 "movzwl %%ax, %1 \n\t" | |
455 "movl %2, %%eax \n\t" | |
456 "xorl %%edx, %%edx \n\t" | |
457 "divw %w3 \n\t" | |
458 "movzwl %%ax, %2 \n\t" | |
459 : "+r" (a), "+r" (b), "+r" (c) | |
460 : "r" (scale) | |
461 : "%eax", "%edx" | |
462 ); | |
221 | 463 #else |
464 /* #elif defined (ARCH_ALPHA) */ | |
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
465 /* Divisions are extremely costly on Alpha; optimize the most |
221 | 466 common case. But they are costly everywhere... |
467 */ | |
214
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
468 if (scale == 8) { |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
469 a = (a + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
470 b = (b + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
471 c = (c + (8 >> 1)) / 8; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
472 } else { |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
473 a = (a + (scale >> 1)) / scale; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
474 b = (b + (scale >> 1)) / scale; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
475 c = (c + (scale >> 1)) / scale; |
73df666cacc7
Alpha optimizations by Falk Hueffner <falk.hueffner@student.uni-tuebingen.de>
nickols_k
parents:
208
diff
changeset
|
476 } |
204 | 477 #endif |
0 | 478 /* XXX: WARNING: they did not choose the same test as MPEG4. This |
479 is very important ! */ | |
480 if (abs(a - b) <= abs(b - c)) { | |
481 pred = c; | |
482 *dir_ptr = 1; | |
483 } else { | |
484 pred = a; | |
485 *dir_ptr = 0; | |
486 } | |
487 | |
488 /* update predictor */ | |
489 *dc_val_ptr = &dc_val[(x) + (y) * wrap]; | |
490 return pred; | |
491 } | |
492 | |
493 #define DC_MAX 119 | |
494 | |
495 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) | |
496 { | |
497 int sign, code; | |
498 int pred; | |
25 | 499 INT16 *dc_val; |
0 | 500 |
501 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
502 | |
503 /* update predictor */ | |
504 if (n < 4) { | |
505 *dc_val = level * s->y_dc_scale; | |
506 } else { | |
507 *dc_val = level * s->c_dc_scale; | |
508 } | |
509 | |
510 /* do the prediction */ | |
511 level -= pred; | |
512 | |
513 sign = 0; | |
514 if (level < 0) { | |
515 level = -level; | |
516 sign = 1; | |
517 } | |
518 | |
519 code = level; | |
520 if (code > DC_MAX) | |
521 code = DC_MAX; | |
522 | |
523 if (s->dc_table_index == 0) { | |
524 if (n < 4) { | |
525 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]); | |
526 } else { | |
527 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]); | |
528 } | |
529 } else { | |
530 if (n < 4) { | |
531 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]); | |
532 } else { | |
533 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]); | |
534 } | |
535 } | |
536 | |
537 if (code == DC_MAX) | |
538 put_bits(&s->pb, 8, level); | |
539 | |
540 if (level != 0) { | |
541 put_bits(&s->pb, 1, sign); | |
542 } | |
543 } | |
544 | |
545 /* Encoding of a block. Very similar to MPEG4 except for a different | |
546 escape coding (same as H263) and more vlc tables. | |
547 */ | |
548 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n) | |
549 { | |
550 int level, run, last, i, j, last_index; | |
551 int last_non_zero, sign, slevel; | |
552 int code, run_diff, dc_pred_dir; | |
553 const RLTable *rl; | |
554 | |
555 if (s->mb_intra) { | |
556 set_stat(ST_DC); | |
557 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir); | |
558 i = 1; | |
559 if (n < 4) { | |
560 rl = &rl_table[s->rl_table_index]; | |
561 } else { | |
562 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
563 } | |
564 run_diff = 0; | |
565 set_stat(ST_INTRA_AC); | |
566 } else { | |
567 i = 0; | |
568 rl = &rl_table[3 + s->rl_table_index]; | |
569 run_diff = 1; | |
570 set_stat(ST_INTER_AC); | |
571 } | |
572 | |
573 /* AC coefs */ | |
574 last_index = s->block_last_index[n]; | |
575 last_non_zero = i - 1; | |
576 for (; i <= last_index; i++) { | |
577 j = zigzag_direct[i]; | |
578 level = block[j]; | |
579 if (level) { | |
580 run = i - last_non_zero - 1; | |
581 last = (i == last_index); | |
582 sign = 0; | |
583 slevel = level; | |
584 if (level < 0) { | |
585 sign = 1; | |
586 level = -level; | |
587 } | |
588 code = get_rl_index(rl, last, run, level); | |
589 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
590 if (code == rl->n) { | |
591 int level1, run1; | |
592 | |
593 level1 = level - rl->max_level[last][run]; | |
594 if (level1 < 1) | |
595 goto esc2; | |
596 code = get_rl_index(rl, last, run, level1); | |
597 if (code == rl->n) { | |
598 esc2: | |
599 put_bits(&s->pb, 1, 0); | |
600 if (level > MAX_LEVEL) | |
601 goto esc3; | |
602 run1 = run - rl->max_run[last][level] - run_diff; | |
603 if (run1 < 0) | |
604 goto esc3; | |
605 code = get_rl_index(rl, last, run1, level); | |
606 if (code == rl->n) { | |
607 esc3: | |
608 /* third escape */ | |
609 put_bits(&s->pb, 1, 0); | |
610 put_bits(&s->pb, 1, last); | |
611 put_bits(&s->pb, 6, run); | |
612 put_bits(&s->pb, 8, slevel & 0xff); | |
613 } else { | |
614 /* second escape */ | |
615 put_bits(&s->pb, 1, 1); | |
616 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
617 put_bits(&s->pb, 1, sign); | |
618 } | |
619 } else { | |
620 /* first escape */ | |
621 put_bits(&s->pb, 1, 1); | |
622 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
623 put_bits(&s->pb, 1, sign); | |
624 } | |
625 } else { | |
626 put_bits(&s->pb, 1, sign); | |
627 } | |
628 last_non_zero = i; | |
629 } | |
630 } | |
631 } | |
632 | |
633 /****************************************/ | |
634 /* decoding stuff */ | |
635 | |
636 static VLC mb_non_intra_vlc; | |
637 static VLC mb_intra_vlc; | |
638 static VLC dc_lum_vlc[2]; | |
639 static VLC dc_chroma_vlc[2]; | |
640 | |
641 /* init all vlc decoding tables */ | |
642 int msmpeg4_decode_init_vlc(MpegEncContext *s) | |
643 { | |
644 int i; | |
645 MVTable *mv; | |
646 | |
647 for(i=0;i<NB_RL_TABLES;i++) { | |
648 init_rl(&rl_table[i]); | |
649 init_vlc_rl(&rl_table[i]); | |
650 } | |
651 for(i=0;i<2;i++) { | |
652 mv = &mv_tables[i]; | |
653 init_vlc(&mv->vlc, 9, mv->n + 1, | |
654 mv->table_mv_bits, 1, 1, | |
655 mv->table_mv_code, 2, 2); | |
656 } | |
657 | |
658 init_vlc(&dc_lum_vlc[0], 9, 120, | |
659 &table0_dc_lum[0][1], 8, 4, | |
660 &table0_dc_lum[0][0], 8, 4); | |
661 init_vlc(&dc_chroma_vlc[0], 9, 120, | |
662 &table0_dc_chroma[0][1], 8, 4, | |
663 &table0_dc_chroma[0][0], 8, 4); | |
664 init_vlc(&dc_lum_vlc[1], 9, 120, | |
665 &table1_dc_lum[0][1], 8, 4, | |
666 &table1_dc_lum[0][0], 8, 4); | |
667 init_vlc(&dc_chroma_vlc[1], 9, 120, | |
668 &table1_dc_chroma[0][1], 8, 4, | |
669 &table1_dc_chroma[0][0], 8, 4); | |
670 | |
671 init_vlc(&mb_non_intra_vlc, 9, 128, | |
672 &table_mb_non_intra[0][1], 8, 4, | |
673 &table_mb_non_intra[0][0], 8, 4); | |
48 | 674 init_vlc(&mb_intra_vlc, 9, 64, |
0 | 675 &table_mb_intra[0][1], 4, 2, |
676 &table_mb_intra[0][0], 4, 2); | |
677 return 0; | |
678 } | |
679 | |
680 static int decode012(GetBitContext *gb) | |
681 { | |
682 int n; | |
21 | 683 n = get_bits1(gb); |
0 | 684 if (n == 0) |
685 return 0; | |
686 else | |
21 | 687 return get_bits1(gb) + 1; |
0 | 688 } |
689 | |
690 int msmpeg4_decode_picture_header(MpegEncContext * s) | |
691 { | |
692 int code; | |
693 | |
694 s->pict_type = get_bits(&s->gb, 2) + 1; | |
695 if (s->pict_type != I_TYPE && | |
696 s->pict_type != P_TYPE) | |
697 return -1; | |
698 | |
699 s->qscale = get_bits(&s->gb, 5); | |
700 | |
701 if (s->pict_type == I_TYPE) { | |
702 code = get_bits(&s->gb, 5); | |
703 /* 0x17: one slice, 0x18: three slices */ | |
704 /* XXX: implement it */ | |
200 | 705 //printf("%d %d %d\n", code, s->slice_height, s->first_slice_line); |
0 | 706 if (code < 0x17) |
707 return -1; | |
708 s->slice_height = s->mb_height / (code - 0x16); | |
709 s->rl_chroma_table_index = decode012(&s->gb); | |
710 s->rl_table_index = decode012(&s->gb); | |
711 | |
21 | 712 s->dc_table_index = get_bits1(&s->gb); |
0 | 713 s->no_rounding = 1; |
200 | 714 /* printf(" %d %d %d %d \n", |
715 s->qscale, | |
716 s->rl_chroma_table_index, | |
717 s->rl_table_index, | |
718 s->dc_table_index);*/ | |
0 | 719 } else { |
21 | 720 s->use_skip_mb_code = get_bits1(&s->gb); |
0 | 721 |
722 s->rl_table_index = decode012(&s->gb); | |
723 s->rl_chroma_table_index = s->rl_table_index; | |
724 | |
21 | 725 s->dc_table_index = get_bits1(&s->gb); |
0 | 726 |
21 | 727 s->mv_table_index = get_bits1(&s->gb); |
200 | 728 /* printf(" %d %d %d %d %d \n", |
729 s->use_skip_mb_code, | |
730 s->rl_table_index, | |
731 s->rl_chroma_table_index, | |
732 s->dc_table_index, | |
733 s->mv_table_index);*/ | |
208 | 734 if(s->flipflop_rounding){ |
735 s->no_rounding ^= 1; | |
736 }else{ | |
737 s->no_rounding = 0; | |
738 } | |
739 // printf("%d", s->no_rounding); | |
0 | 740 } |
208 | 741 |
742 | |
0 | 743 #ifdef DEBUG |
744 printf("*****frame %d:\n", frame_count++); | |
745 #endif | |
746 return 0; | |
747 } | |
748 | |
208 | 749 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) |
750 { | |
751 int firstBit=0; | |
752 | |
753 /* the alt_bitstream reader could read over the end so we need to check it */ | |
754 if(get_bits_count(&s->gb) < buf_size*8) firstBit= get_bits1(&s->gb); | |
755 | |
756 if(s->pict_type == P_TYPE) | |
757 { | |
758 if(firstBit) return -1; // havnt seen ext headers in P-Frames yet ;) | |
759 } | |
760 else | |
761 { | |
762 int unk; | |
763 if(!firstBit) // no header found | |
764 { | |
765 s->flipflop_rounding= 0; | |
766 s->bitrate= 0; | |
767 return 0; | |
768 } | |
769 | |
770 unk= get_bits(&s->gb, 4); | |
771 s->bitrate= get_bits(&s->gb, 11); | |
772 | |
773 // printf("%2d %4d ;; %1X\n", unk,s->bitrate, unk); | |
774 | |
775 s->flipflop_rounding= get_bits1(&s->gb); | |
776 } | |
777 | |
778 return 0; | |
779 } | |
780 | |
0 | 781 void memsetw(short *tab, int val, int n) |
782 { | |
783 int i; | |
784 for(i=0;i<n;i++) | |
785 tab[i] = val; | |
786 } | |
787 | |
788 int msmpeg4_decode_mb(MpegEncContext *s, | |
789 DCTELEM block[6][64]) | |
790 { | |
791 int cbp, code, i; | |
792 int pred, val; | |
793 UINT8 *coded_val; | |
794 | |
795 /* special slice handling */ | |
796 if (s->mb_x == 0) { | |
122 | 797 if (s->slice_height && (s->mb_y % s->slice_height) == 0) { |
0 | 798 int wrap; |
799 /* reset DC pred (set previous line to 1024) */ | |
800 wrap = 2 * s->mb_width + 2; | |
801 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap], | |
802 1024, 2 * s->mb_width); | |
803 wrap = s->mb_width + 2; | |
804 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap], | |
805 1024, s->mb_width); | |
806 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap], | |
807 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
|
808 |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
809 /* 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
|
810 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
|
811 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
|
812 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
|
813 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
|
814 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
|
815 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
|
816 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
|
817 0, s->mb_width*16); |
0 | 818 |
819 s->first_slice_line = 1; | |
820 } else { | |
821 s->first_slice_line = 0; | |
822 } | |
823 } | |
824 | |
825 if (s->pict_type == P_TYPE) { | |
826 set_stat(ST_INTER_MB); | |
827 if (s->use_skip_mb_code) { | |
21 | 828 if (get_bits1(&s->gb)) { |
0 | 829 /* skip mb */ |
830 s->mb_intra = 0; | |
831 for(i=0;i<6;i++) | |
832 s->block_last_index[i] = -1; | |
833 s->mv_dir = MV_DIR_FORWARD; | |
834 s->mv_type = MV_TYPE_16X16; | |
835 s->mv[0][0][0] = 0; | |
836 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
|
837 s->mb_skiped = 1; |
0 | 838 return 0; |
839 } | |
840 } | |
841 | |
842 code = get_vlc(&s->gb, &mb_non_intra_vlc); | |
843 if (code < 0) | |
844 return -1; | |
845 if (code & 0x40) | |
846 s->mb_intra = 0; | |
847 else | |
848 s->mb_intra = 1; | |
849 | |
850 cbp = code & 0x3f; | |
851 } else { | |
852 set_stat(ST_INTRA_MB); | |
853 s->mb_intra = 1; | |
854 code = get_vlc(&s->gb, &mb_intra_vlc); | |
855 if (code < 0) | |
856 return -1; | |
857 /* predict coded block pattern */ | |
858 cbp = 0; | |
859 for(i=0;i<6;i++) { | |
860 val = ((code >> (5 - i)) & 1); | |
861 if (i < 4) { | |
862 pred = coded_block_pred(s, i, &coded_val); | |
863 val = val ^ pred; | |
864 *coded_val = val; | |
865 } | |
866 cbp |= val << (5 - i); | |
867 } | |
868 } | |
869 | |
870 if (!s->mb_intra) { | |
871 int mx, my; | |
872 set_stat(ST_MV); | |
873 h263_pred_motion(s, 0, &mx, &my); | |
874 if (msmpeg4_decode_motion(s, &mx, &my) < 0) | |
875 return -1; | |
876 s->mv_dir = MV_DIR_FORWARD; | |
877 s->mv_type = MV_TYPE_16X16; | |
878 s->mv[0][0][0] = mx; | |
879 s->mv[0][0][1] = my; | |
880 } else { | |
881 set_stat(ST_INTRA_MB); | |
21 | 882 s->ac_pred = get_bits1(&s->gb); |
0 | 883 } |
884 | |
885 for (i = 0; i < 6; i++) { | |
886 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
887 return -1; | |
888 } | |
889 return 0; | |
890 } | |
891 | |
892 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
893 int n, int coded) | |
894 { | |
895 int code, level, i, j, last, run, run_diff; | |
896 int dc_pred_dir; | |
897 RLTable *rl; | |
898 const UINT8 *scan_table; | |
200 | 899 int qmul, qadd; |
0 | 900 |
901 if (s->mb_intra) { | |
200 | 902 qmul=1; |
903 qadd=0; | |
904 | |
0 | 905 /* DC coef */ |
906 set_stat(ST_DC); | |
907 level = msmpeg4_decode_dc(s, n, &dc_pred_dir); | |
908 if (level < 0) | |
909 return -1; | |
910 block[0] = level; | |
911 if (n < 4) { | |
912 rl = &rl_table[s->rl_table_index]; | |
913 } else { | |
914 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
915 } | |
200 | 916 |
0 | 917 run_diff = 0; |
918 i = 1; | |
919 if (!coded) { | |
920 goto not_coded; | |
921 } | |
922 if (s->ac_pred) { | |
923 if (dc_pred_dir == 0) | |
924 scan_table = ff_alternate_vertical_scan; /* left */ | |
925 else | |
926 scan_table = ff_alternate_horizontal_scan; /* top */ | |
927 } else { | |
928 scan_table = zigzag_direct; | |
929 } | |
930 set_stat(ST_INTRA_AC); | |
931 } else { | |
200 | 932 qmul = s->qscale << 1; |
933 qadd = (s->qscale - 1) | 1; | |
0 | 934 i = 0; |
935 rl = &rl_table[3 + s->rl_table_index]; | |
936 run_diff = 1; | |
937 if (!coded) { | |
938 s->block_last_index[n] = i - 1; | |
939 return 0; | |
940 } | |
941 scan_table = zigzag_direct; | |
942 set_stat(ST_INTER_AC); | |
943 } | |
944 | |
945 for(;;) { | |
946 code = get_vlc(&s->gb, &rl->vlc); | |
947 if (code < 0) | |
948 return -1; | |
949 if (code == rl->n) { | |
950 /* escape */ | |
21 | 951 if (get_bits1(&s->gb) == 0) { |
952 if (get_bits1(&s->gb) == 0) { | |
0 | 953 /* third escape */ |
21 | 954 last = get_bits1(&s->gb); |
0 | 955 run = get_bits(&s->gb, 6); |
956 level = get_bits(&s->gb, 8); | |
957 level = (level << 24) >> 24; /* sign extend */ | |
200 | 958 if(level>0) level= level * qmul + qadd; |
959 else level= level * qmul - qadd; | |
0 | 960 } else { |
961 /* second escape */ | |
962 code = get_vlc(&s->gb, &rl->vlc); | |
963 if (code < 0 || code >= rl->n) | |
964 return -1; | |
965 run = rl->table_run[code]; | |
201 | 966 level = rl->table_level[code]; |
0 | 967 last = code >= rl->last; |
968 run += rl->max_run[last][level] + run_diff; | |
201 | 969 level= level * qmul + qadd; |
21 | 970 if (get_bits1(&s->gb)) |
0 | 971 level = -level; |
972 } | |
973 } else { | |
974 /* first escape */ | |
975 code = get_vlc(&s->gb, &rl->vlc); | |
976 if (code < 0 || code >= rl->n) | |
977 return -1; | |
978 run = rl->table_run[code]; | |
979 level = rl->table_level[code]; | |
980 last = code >= rl->last; | |
981 level += rl->max_level[last][run]; | |
200 | 982 level= level * qmul + qadd; |
21 | 983 if (get_bits1(&s->gb)) |
0 | 984 level = -level; |
985 } | |
986 } else { | |
987 run = rl->table_run[code]; | |
200 | 988 level = rl->table_level[code] * qmul + qadd; |
0 | 989 last = code >= rl->last; |
21 | 990 if (get_bits1(&s->gb)) |
0 | 991 level = -level; |
992 } | |
993 i += run; | |
994 if (i >= 64) | |
995 return -1; | |
996 j = scan_table[i]; | |
997 block[j] = level; | |
998 i++; | |
999 if (last) | |
1000 break; | |
1001 } | |
1002 not_coded: | |
1003 if (s->mb_intra) { | |
1004 mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
1005 if (s->ac_pred) { | |
1006 i = 64; /* XXX: not optimal */ | |
1007 } | |
1008 } | |
1009 s->block_last_index[n] = i - 1; | |
1010 | |
1011 return 0; | |
1012 } | |
1013 | |
1014 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | |
1015 { | |
1016 int level, pred; | |
25 | 1017 INT16 *dc_val; |
0 | 1018 |
1019 if (n < 4) { | |
1020 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]); | |
1021 } else { | |
1022 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]); | |
1023 } | |
1024 if (level < 0) | |
1025 return -1; | |
1026 | |
1027 if (level == DC_MAX) { | |
1028 level = get_bits(&s->gb, 8); | |
21 | 1029 if (get_bits1(&s->gb)) |
0 | 1030 level = -level; |
1031 } else if (level != 0) { | |
21 | 1032 if (get_bits1(&s->gb)) |
0 | 1033 level = -level; |
1034 } | |
1035 | |
1036 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
1037 level += pred; | |
1038 | |
1039 /* update predictor */ | |
1040 if (n < 4) { | |
1041 *dc_val = level * s->y_dc_scale; | |
1042 } else { | |
1043 *dc_val = level * s->c_dc_scale; | |
1044 } | |
1045 | |
1046 return level; | |
1047 } | |
1048 | |
1049 static int msmpeg4_decode_motion(MpegEncContext * s, | |
1050 int *mx_ptr, int *my_ptr) | |
1051 { | |
1052 MVTable *mv; | |
1053 int code, mx, my; | |
1054 | |
1055 mv = &mv_tables[s->mv_table_index]; | |
1056 | |
1057 code = get_vlc(&s->gb, &mv->vlc); | |
1058 if (code < 0) | |
1059 return -1; | |
1060 if (code == mv->n) { | |
1061 mx = get_bits(&s->gb, 6); | |
1062 my = get_bits(&s->gb, 6); | |
1063 } else { | |
1064 mx = mv->table_mvx[code]; | |
1065 my = mv->table_mvy[code]; | |
1066 } | |
1067 | |
1068 mx += *mx_ptr - 32; | |
1069 my += *my_ptr - 32; | |
1070 /* WARNING : they do not do exactly modulo encoding */ | |
1071 if (mx <= -64) | |
1072 mx += 64; | |
1073 else if (mx >= 64) | |
1074 mx -= 64; | |
1075 | |
1076 if (my <= -64) | |
1077 my += 64; | |
1078 else if (my >= 64) | |
1079 my -= 64; | |
1080 *mx_ptr = mx; | |
1081 *my_ptr = my; | |
1082 return 0; | |
1083 } |