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