Mercurial > libavcodec.hg
annotate msmpeg4.c @ 208:2eb04d6be309 libavcodec
(commit by michael)
bye bye weird al rounding bug ;)
author | arpi_esp |
---|---|
date | Tue, 15 Jan 2002 22:22:41 +0000 |
parents | fceb435fae6b |
children | 73df666cacc7 |
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 !) */ | |
204 | 440 #ifdef ARCH_X86 |
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 ); | |
463 #else | |
0 | 464 a = (a + (scale >> 1)) / scale; |
465 b = (b + (scale >> 1)) / scale; | |
466 c = (c + (scale >> 1)) / scale; | |
204 | 467 #endif |
0 | 468 /* XXX: WARNING: they did not choose the same test as MPEG4. This |
469 is very important ! */ | |
470 if (abs(a - b) <= abs(b - c)) { | |
471 pred = c; | |
472 *dir_ptr = 1; | |
473 } else { | |
474 pred = a; | |
475 *dir_ptr = 0; | |
476 } | |
477 | |
478 /* update predictor */ | |
479 *dc_val_ptr = &dc_val[(x) + (y) * wrap]; | |
480 return pred; | |
481 } | |
482 | |
483 #define DC_MAX 119 | |
484 | |
485 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr) | |
486 { | |
487 int sign, code; | |
488 int pred; | |
25 | 489 INT16 *dc_val; |
0 | 490 |
491 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
492 | |
493 /* update predictor */ | |
494 if (n < 4) { | |
495 *dc_val = level * s->y_dc_scale; | |
496 } else { | |
497 *dc_val = level * s->c_dc_scale; | |
498 } | |
499 | |
500 /* do the prediction */ | |
501 level -= pred; | |
502 | |
503 sign = 0; | |
504 if (level < 0) { | |
505 level = -level; | |
506 sign = 1; | |
507 } | |
508 | |
509 code = level; | |
510 if (code > DC_MAX) | |
511 code = DC_MAX; | |
512 | |
513 if (s->dc_table_index == 0) { | |
514 if (n < 4) { | |
515 put_bits(&s->pb, table0_dc_lum[code][1], table0_dc_lum[code][0]); | |
516 } else { | |
517 put_bits(&s->pb, table0_dc_chroma[code][1], table0_dc_chroma[code][0]); | |
518 } | |
519 } else { | |
520 if (n < 4) { | |
521 put_bits(&s->pb, table1_dc_lum[code][1], table1_dc_lum[code][0]); | |
522 } else { | |
523 put_bits(&s->pb, table1_dc_chroma[code][1], table1_dc_chroma[code][0]); | |
524 } | |
525 } | |
526 | |
527 if (code == DC_MAX) | |
528 put_bits(&s->pb, 8, level); | |
529 | |
530 if (level != 0) { | |
531 put_bits(&s->pb, 1, sign); | |
532 } | |
533 } | |
534 | |
535 /* Encoding of a block. Very similar to MPEG4 except for a different | |
536 escape coding (same as H263) and more vlc tables. | |
537 */ | |
538 static void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n) | |
539 { | |
540 int level, run, last, i, j, last_index; | |
541 int last_non_zero, sign, slevel; | |
542 int code, run_diff, dc_pred_dir; | |
543 const RLTable *rl; | |
544 | |
545 if (s->mb_intra) { | |
546 set_stat(ST_DC); | |
547 msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir); | |
548 i = 1; | |
549 if (n < 4) { | |
550 rl = &rl_table[s->rl_table_index]; | |
551 } else { | |
552 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
553 } | |
554 run_diff = 0; | |
555 set_stat(ST_INTRA_AC); | |
556 } else { | |
557 i = 0; | |
558 rl = &rl_table[3 + s->rl_table_index]; | |
559 run_diff = 1; | |
560 set_stat(ST_INTER_AC); | |
561 } | |
562 | |
563 /* AC coefs */ | |
564 last_index = s->block_last_index[n]; | |
565 last_non_zero = i - 1; | |
566 for (; i <= last_index; i++) { | |
567 j = zigzag_direct[i]; | |
568 level = block[j]; | |
569 if (level) { | |
570 run = i - last_non_zero - 1; | |
571 last = (i == last_index); | |
572 sign = 0; | |
573 slevel = level; | |
574 if (level < 0) { | |
575 sign = 1; | |
576 level = -level; | |
577 } | |
578 code = get_rl_index(rl, last, run, level); | |
579 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
580 if (code == rl->n) { | |
581 int level1, run1; | |
582 | |
583 level1 = level - rl->max_level[last][run]; | |
584 if (level1 < 1) | |
585 goto esc2; | |
586 code = get_rl_index(rl, last, run, level1); | |
587 if (code == rl->n) { | |
588 esc2: | |
589 put_bits(&s->pb, 1, 0); | |
590 if (level > MAX_LEVEL) | |
591 goto esc3; | |
592 run1 = run - rl->max_run[last][level] - run_diff; | |
593 if (run1 < 0) | |
594 goto esc3; | |
595 code = get_rl_index(rl, last, run1, level); | |
596 if (code == rl->n) { | |
597 esc3: | |
598 /* third escape */ | |
599 put_bits(&s->pb, 1, 0); | |
600 put_bits(&s->pb, 1, last); | |
601 put_bits(&s->pb, 6, run); | |
602 put_bits(&s->pb, 8, slevel & 0xff); | |
603 } else { | |
604 /* second escape */ | |
605 put_bits(&s->pb, 1, 1); | |
606 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
607 put_bits(&s->pb, 1, sign); | |
608 } | |
609 } else { | |
610 /* first escape */ | |
611 put_bits(&s->pb, 1, 1); | |
612 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]); | |
613 put_bits(&s->pb, 1, sign); | |
614 } | |
615 } else { | |
616 put_bits(&s->pb, 1, sign); | |
617 } | |
618 last_non_zero = i; | |
619 } | |
620 } | |
621 } | |
622 | |
623 /****************************************/ | |
624 /* decoding stuff */ | |
625 | |
626 static VLC mb_non_intra_vlc; | |
627 static VLC mb_intra_vlc; | |
628 static VLC dc_lum_vlc[2]; | |
629 static VLC dc_chroma_vlc[2]; | |
630 | |
631 /* init all vlc decoding tables */ | |
632 int msmpeg4_decode_init_vlc(MpegEncContext *s) | |
633 { | |
634 int i; | |
635 MVTable *mv; | |
636 | |
637 for(i=0;i<NB_RL_TABLES;i++) { | |
638 init_rl(&rl_table[i]); | |
639 init_vlc_rl(&rl_table[i]); | |
640 } | |
641 for(i=0;i<2;i++) { | |
642 mv = &mv_tables[i]; | |
643 init_vlc(&mv->vlc, 9, mv->n + 1, | |
644 mv->table_mv_bits, 1, 1, | |
645 mv->table_mv_code, 2, 2); | |
646 } | |
647 | |
648 init_vlc(&dc_lum_vlc[0], 9, 120, | |
649 &table0_dc_lum[0][1], 8, 4, | |
650 &table0_dc_lum[0][0], 8, 4); | |
651 init_vlc(&dc_chroma_vlc[0], 9, 120, | |
652 &table0_dc_chroma[0][1], 8, 4, | |
653 &table0_dc_chroma[0][0], 8, 4); | |
654 init_vlc(&dc_lum_vlc[1], 9, 120, | |
655 &table1_dc_lum[0][1], 8, 4, | |
656 &table1_dc_lum[0][0], 8, 4); | |
657 init_vlc(&dc_chroma_vlc[1], 9, 120, | |
658 &table1_dc_chroma[0][1], 8, 4, | |
659 &table1_dc_chroma[0][0], 8, 4); | |
660 | |
661 init_vlc(&mb_non_intra_vlc, 9, 128, | |
662 &table_mb_non_intra[0][1], 8, 4, | |
663 &table_mb_non_intra[0][0], 8, 4); | |
48 | 664 init_vlc(&mb_intra_vlc, 9, 64, |
0 | 665 &table_mb_intra[0][1], 4, 2, |
666 &table_mb_intra[0][0], 4, 2); | |
667 return 0; | |
668 } | |
669 | |
670 static int decode012(GetBitContext *gb) | |
671 { | |
672 int n; | |
21 | 673 n = get_bits1(gb); |
0 | 674 if (n == 0) |
675 return 0; | |
676 else | |
21 | 677 return get_bits1(gb) + 1; |
0 | 678 } |
679 | |
680 int msmpeg4_decode_picture_header(MpegEncContext * s) | |
681 { | |
682 int code; | |
683 | |
684 s->pict_type = get_bits(&s->gb, 2) + 1; | |
685 if (s->pict_type != I_TYPE && | |
686 s->pict_type != P_TYPE) | |
687 return -1; | |
688 | |
689 s->qscale = get_bits(&s->gb, 5); | |
690 | |
691 if (s->pict_type == I_TYPE) { | |
692 code = get_bits(&s->gb, 5); | |
693 /* 0x17: one slice, 0x18: three slices */ | |
694 /* XXX: implement it */ | |
200 | 695 //printf("%d %d %d\n", code, s->slice_height, s->first_slice_line); |
0 | 696 if (code < 0x17) |
697 return -1; | |
698 s->slice_height = s->mb_height / (code - 0x16); | |
699 s->rl_chroma_table_index = decode012(&s->gb); | |
700 s->rl_table_index = decode012(&s->gb); | |
701 | |
21 | 702 s->dc_table_index = get_bits1(&s->gb); |
0 | 703 s->no_rounding = 1; |
200 | 704 /* printf(" %d %d %d %d \n", |
705 s->qscale, | |
706 s->rl_chroma_table_index, | |
707 s->rl_table_index, | |
708 s->dc_table_index);*/ | |
0 | 709 } else { |
21 | 710 s->use_skip_mb_code = get_bits1(&s->gb); |
0 | 711 |
712 s->rl_table_index = decode012(&s->gb); | |
713 s->rl_chroma_table_index = s->rl_table_index; | |
714 | |
21 | 715 s->dc_table_index = get_bits1(&s->gb); |
0 | 716 |
21 | 717 s->mv_table_index = get_bits1(&s->gb); |
200 | 718 /* printf(" %d %d %d %d %d \n", |
719 s->use_skip_mb_code, | |
720 s->rl_table_index, | |
721 s->rl_chroma_table_index, | |
722 s->dc_table_index, | |
723 s->mv_table_index);*/ | |
208 | 724 if(s->flipflop_rounding){ |
725 s->no_rounding ^= 1; | |
726 }else{ | |
727 s->no_rounding = 0; | |
728 } | |
729 // printf("%d", s->no_rounding); | |
0 | 730 } |
208 | 731 |
732 | |
0 | 733 #ifdef DEBUG |
734 printf("*****frame %d:\n", frame_count++); | |
735 #endif | |
736 return 0; | |
737 } | |
738 | |
208 | 739 int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size) |
740 { | |
741 int firstBit=0; | |
742 | |
743 /* the alt_bitstream reader could read over the end so we need to check it */ | |
744 if(get_bits_count(&s->gb) < buf_size*8) firstBit= get_bits1(&s->gb); | |
745 | |
746 if(s->pict_type == P_TYPE) | |
747 { | |
748 if(firstBit) return -1; // havnt seen ext headers in P-Frames yet ;) | |
749 } | |
750 else | |
751 { | |
752 int unk; | |
753 if(!firstBit) // no header found | |
754 { | |
755 s->flipflop_rounding= 0; | |
756 s->bitrate= 0; | |
757 return 0; | |
758 } | |
759 | |
760 unk= get_bits(&s->gb, 4); | |
761 s->bitrate= get_bits(&s->gb, 11); | |
762 | |
763 // printf("%2d %4d ;; %1X\n", unk,s->bitrate, unk); | |
764 | |
765 s->flipflop_rounding= get_bits1(&s->gb); | |
766 } | |
767 | |
768 return 0; | |
769 } | |
770 | |
0 | 771 void memsetw(short *tab, int val, int n) |
772 { | |
773 int i; | |
774 for(i=0;i<n;i++) | |
775 tab[i] = val; | |
776 } | |
777 | |
778 int msmpeg4_decode_mb(MpegEncContext *s, | |
779 DCTELEM block[6][64]) | |
780 { | |
781 int cbp, code, i; | |
782 int pred, val; | |
783 UINT8 *coded_val; | |
784 | |
785 /* special slice handling */ | |
786 if (s->mb_x == 0) { | |
122 | 787 if (s->slice_height && (s->mb_y % s->slice_height) == 0) { |
0 | 788 int wrap; |
789 /* reset DC pred (set previous line to 1024) */ | |
790 wrap = 2 * s->mb_width + 2; | |
791 memsetw(&s->dc_val[0][(1) + (2 * s->mb_y) * wrap], | |
792 1024, 2 * s->mb_width); | |
793 wrap = s->mb_width + 2; | |
794 memsetw(&s->dc_val[1][(1) + (s->mb_y) * wrap], | |
795 1024, s->mb_width); | |
796 memsetw(&s->dc_val[2][(1) + (s->mb_y) * wrap], | |
797 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
|
798 |
cf37da86d990
fix slices when code=0x18, patch by Michael Niedermayer <michael@mplayer.dev.hu>
arpi_esp
parents:
122
diff
changeset
|
799 /* 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
|
800 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
|
801 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
|
802 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
|
803 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
|
804 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
|
805 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
|
806 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
|
807 0, s->mb_width*16); |
0 | 808 |
809 s->first_slice_line = 1; | |
810 } else { | |
811 s->first_slice_line = 0; | |
812 } | |
813 } | |
814 | |
815 if (s->pict_type == P_TYPE) { | |
816 set_stat(ST_INTER_MB); | |
817 if (s->use_skip_mb_code) { | |
21 | 818 if (get_bits1(&s->gb)) { |
0 | 819 /* skip mb */ |
820 s->mb_intra = 0; | |
821 for(i=0;i<6;i++) | |
822 s->block_last_index[i] = -1; | |
823 s->mv_dir = MV_DIR_FORWARD; | |
824 s->mv_type = MV_TYPE_16X16; | |
825 s->mv[0][0][0] = 0; | |
826 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
|
827 s->mb_skiped = 1; |
0 | 828 return 0; |
829 } | |
830 } | |
831 | |
832 code = get_vlc(&s->gb, &mb_non_intra_vlc); | |
833 if (code < 0) | |
834 return -1; | |
835 if (code & 0x40) | |
836 s->mb_intra = 0; | |
837 else | |
838 s->mb_intra = 1; | |
839 | |
840 cbp = code & 0x3f; | |
841 } else { | |
842 set_stat(ST_INTRA_MB); | |
843 s->mb_intra = 1; | |
844 code = get_vlc(&s->gb, &mb_intra_vlc); | |
845 if (code < 0) | |
846 return -1; | |
847 /* predict coded block pattern */ | |
848 cbp = 0; | |
849 for(i=0;i<6;i++) { | |
850 val = ((code >> (5 - i)) & 1); | |
851 if (i < 4) { | |
852 pred = coded_block_pred(s, i, &coded_val); | |
853 val = val ^ pred; | |
854 *coded_val = val; | |
855 } | |
856 cbp |= val << (5 - i); | |
857 } | |
858 } | |
859 | |
860 if (!s->mb_intra) { | |
861 int mx, my; | |
862 set_stat(ST_MV); | |
863 h263_pred_motion(s, 0, &mx, &my); | |
864 if (msmpeg4_decode_motion(s, &mx, &my) < 0) | |
865 return -1; | |
866 s->mv_dir = MV_DIR_FORWARD; | |
867 s->mv_type = MV_TYPE_16X16; | |
868 s->mv[0][0][0] = mx; | |
869 s->mv[0][0][1] = my; | |
870 } else { | |
871 set_stat(ST_INTRA_MB); | |
21 | 872 s->ac_pred = get_bits1(&s->gb); |
0 | 873 } |
874 | |
875 for (i = 0; i < 6; i++) { | |
876 if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1) < 0) | |
877 return -1; | |
878 } | |
879 return 0; | |
880 } | |
881 | |
882 static int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block, | |
883 int n, int coded) | |
884 { | |
885 int code, level, i, j, last, run, run_diff; | |
886 int dc_pred_dir; | |
887 RLTable *rl; | |
888 const UINT8 *scan_table; | |
200 | 889 int qmul, qadd; |
0 | 890 |
891 if (s->mb_intra) { | |
200 | 892 qmul=1; |
893 qadd=0; | |
894 | |
0 | 895 /* DC coef */ |
896 set_stat(ST_DC); | |
897 level = msmpeg4_decode_dc(s, n, &dc_pred_dir); | |
898 if (level < 0) | |
899 return -1; | |
900 block[0] = level; | |
901 if (n < 4) { | |
902 rl = &rl_table[s->rl_table_index]; | |
903 } else { | |
904 rl = &rl_table[3 + s->rl_chroma_table_index]; | |
905 } | |
200 | 906 |
0 | 907 run_diff = 0; |
908 i = 1; | |
909 if (!coded) { | |
910 goto not_coded; | |
911 } | |
912 if (s->ac_pred) { | |
913 if (dc_pred_dir == 0) | |
914 scan_table = ff_alternate_vertical_scan; /* left */ | |
915 else | |
916 scan_table = ff_alternate_horizontal_scan; /* top */ | |
917 } else { | |
918 scan_table = zigzag_direct; | |
919 } | |
920 set_stat(ST_INTRA_AC); | |
921 } else { | |
200 | 922 qmul = s->qscale << 1; |
923 qadd = (s->qscale - 1) | 1; | |
0 | 924 i = 0; |
925 rl = &rl_table[3 + s->rl_table_index]; | |
926 run_diff = 1; | |
927 if (!coded) { | |
928 s->block_last_index[n] = i - 1; | |
929 return 0; | |
930 } | |
931 scan_table = zigzag_direct; | |
932 set_stat(ST_INTER_AC); | |
933 } | |
934 | |
935 for(;;) { | |
936 code = get_vlc(&s->gb, &rl->vlc); | |
937 if (code < 0) | |
938 return -1; | |
939 if (code == rl->n) { | |
940 /* escape */ | |
21 | 941 if (get_bits1(&s->gb) == 0) { |
942 if (get_bits1(&s->gb) == 0) { | |
0 | 943 /* third escape */ |
21 | 944 last = get_bits1(&s->gb); |
0 | 945 run = get_bits(&s->gb, 6); |
946 level = get_bits(&s->gb, 8); | |
947 level = (level << 24) >> 24; /* sign extend */ | |
200 | 948 if(level>0) level= level * qmul + qadd; |
949 else level= level * qmul - qadd; | |
0 | 950 } else { |
951 /* second escape */ | |
952 code = get_vlc(&s->gb, &rl->vlc); | |
953 if (code < 0 || code >= rl->n) | |
954 return -1; | |
955 run = rl->table_run[code]; | |
201 | 956 level = rl->table_level[code]; |
0 | 957 last = code >= rl->last; |
958 run += rl->max_run[last][level] + run_diff; | |
201 | 959 level= level * qmul + qadd; |
21 | 960 if (get_bits1(&s->gb)) |
0 | 961 level = -level; |
962 } | |
963 } else { | |
964 /* first escape */ | |
965 code = get_vlc(&s->gb, &rl->vlc); | |
966 if (code < 0 || code >= rl->n) | |
967 return -1; | |
968 run = rl->table_run[code]; | |
969 level = rl->table_level[code]; | |
970 last = code >= rl->last; | |
971 level += rl->max_level[last][run]; | |
200 | 972 level= level * qmul + qadd; |
21 | 973 if (get_bits1(&s->gb)) |
0 | 974 level = -level; |
975 } | |
976 } else { | |
977 run = rl->table_run[code]; | |
200 | 978 level = rl->table_level[code] * qmul + qadd; |
0 | 979 last = code >= rl->last; |
21 | 980 if (get_bits1(&s->gb)) |
0 | 981 level = -level; |
982 } | |
983 i += run; | |
984 if (i >= 64) | |
985 return -1; | |
986 j = scan_table[i]; | |
987 block[j] = level; | |
988 i++; | |
989 if (last) | |
990 break; | |
991 } | |
992 not_coded: | |
993 if (s->mb_intra) { | |
994 mpeg4_pred_ac(s, block, n, dc_pred_dir); | |
995 if (s->ac_pred) { | |
996 i = 64; /* XXX: not optimal */ | |
997 } | |
998 } | |
999 s->block_last_index[n] = i - 1; | |
1000 | |
1001 return 0; | |
1002 } | |
1003 | |
1004 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr) | |
1005 { | |
1006 int level, pred; | |
25 | 1007 INT16 *dc_val; |
0 | 1008 |
1009 if (n < 4) { | |
1010 level = get_vlc(&s->gb, &dc_lum_vlc[s->dc_table_index]); | |
1011 } else { | |
1012 level = get_vlc(&s->gb, &dc_chroma_vlc[s->dc_table_index]); | |
1013 } | |
1014 if (level < 0) | |
1015 return -1; | |
1016 | |
1017 if (level == DC_MAX) { | |
1018 level = get_bits(&s->gb, 8); | |
21 | 1019 if (get_bits1(&s->gb)) |
0 | 1020 level = -level; |
1021 } else if (level != 0) { | |
21 | 1022 if (get_bits1(&s->gb)) |
0 | 1023 level = -level; |
1024 } | |
1025 | |
1026 pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr); | |
1027 level += pred; | |
1028 | |
1029 /* update predictor */ | |
1030 if (n < 4) { | |
1031 *dc_val = level * s->y_dc_scale; | |
1032 } else { | |
1033 *dc_val = level * s->c_dc_scale; | |
1034 } | |
1035 | |
1036 return level; | |
1037 } | |
1038 | |
1039 static int msmpeg4_decode_motion(MpegEncContext * s, | |
1040 int *mx_ptr, int *my_ptr) | |
1041 { | |
1042 MVTable *mv; | |
1043 int code, mx, my; | |
1044 | |
1045 mv = &mv_tables[s->mv_table_index]; | |
1046 | |
1047 code = get_vlc(&s->gb, &mv->vlc); | |
1048 if (code < 0) | |
1049 return -1; | |
1050 if (code == mv->n) { | |
1051 mx = get_bits(&s->gb, 6); | |
1052 my = get_bits(&s->gb, 6); | |
1053 } else { | |
1054 mx = mv->table_mvx[code]; | |
1055 my = mv->table_mvy[code]; | |
1056 } | |
1057 | |
1058 mx += *mx_ptr - 32; | |
1059 my += *my_ptr - 32; | |
1060 /* WARNING : they do not do exactly modulo encoding */ | |
1061 if (mx <= -64) | |
1062 mx += 64; | |
1063 else if (mx >= 64) | |
1064 mx -= 64; | |
1065 | |
1066 if (my <= -64) | |
1067 my += 64; | |
1068 else if (my >= 64) | |
1069 my -= 64; | |
1070 *mx_ptr = mx; | |
1071 *my_ptr = my; | |
1072 return 0; | |
1073 } |