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