1 | /*
|
---|
2 | * MSMPEG4 backend for ffmpeg encoder and decoder
|
---|
3 | * Copyright (c) 2001 Fabrice Bellard.
|
---|
4 | * Copyright (c) 2002-2004 Michael Niedermayer <[email protected]>
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
19 | *
|
---|
20 | * msmpeg4v1 & v2 stuff by Michael Niedermayer <[email protected]>
|
---|
21 | */
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * @file msmpeg4.c
|
---|
25 | * MSMPEG4 backend for ffmpeg encoder and decoder.
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "avcodec.h"
|
---|
29 | #include "dsputil.h"
|
---|
30 | #include "mpegvideo.h"
|
---|
31 |
|
---|
32 | /*
|
---|
33 | * You can also call this codec : MPEG4 with a twist !
|
---|
34 | *
|
---|
35 | * TODO:
|
---|
36 | * - (encoding) select best mv table (two choices)
|
---|
37 | * - (encoding) select best vlc/dc table
|
---|
38 | */
|
---|
39 | //#define DEBUG
|
---|
40 |
|
---|
41 | #define DC_VLC_BITS 9
|
---|
42 | #define CBPY_VLC_BITS 6
|
---|
43 | #define INTER_INTRA_VLC_BITS 3
|
---|
44 | #define V1_INTRA_CBPC_VLC_BITS 6
|
---|
45 | #define V1_INTER_CBPC_VLC_BITS 6
|
---|
46 | #define V2_INTRA_CBPC_VLC_BITS 3
|
---|
47 | #define V2_MB_TYPE_VLC_BITS 7
|
---|
48 | #define MV_VLC_BITS 9
|
---|
49 | #define V2_MV_VLC_BITS 9
|
---|
50 | #define TEX_VLC_BITS 9
|
---|
51 | #define MB_NON_INTRA_VLC_BITS 9
|
---|
52 | #define MB_INTRA_VLC_BITS 9
|
---|
53 |
|
---|
54 | #define II_BITRATE 128*1024
|
---|
55 | #define MBAC_BITRATE 50*1024
|
---|
56 |
|
---|
57 | #define DEFAULT_INTER_INDEX 3
|
---|
58 |
|
---|
59 | static uint32_t v2_dc_lum_table[512][2];
|
---|
60 | static uint32_t v2_dc_chroma_table[512][2];
|
---|
61 |
|
---|
62 | static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
|
---|
63 | static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
|
---|
64 | int n, int coded, const uint8_t *scantable);
|
---|
65 | static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
|
---|
66 | static int msmpeg4_decode_motion(MpegEncContext * s,
|
---|
67 | int *mx_ptr, int *my_ptr);
|
---|
68 | static void msmpeg4v2_encode_motion(MpegEncContext * s, int val);
|
---|
69 | static void init_h263_dc_for_msmpeg4(void);
|
---|
70 | static inline void msmpeg4_memsetw(short *tab, int val, int n);
|
---|
71 | #ifdef CONFIG_ENCODERS
|
---|
72 | static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra);
|
---|
73 | #endif //CONFIG_ENCODERS
|
---|
74 | static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
|
---|
75 | static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
|
---|
76 | static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
|
---|
77 |
|
---|
78 | /* vc1 externs */
|
---|
79 | extern uint8_t wmv3_dc_scale_table[32];
|
---|
80 |
|
---|
81 | #ifdef DEBUG
|
---|
82 | int intra_count = 0;
|
---|
83 | int frame_count = 0;
|
---|
84 | #endif
|
---|
85 |
|
---|
86 | #include "msmpeg4data.h"
|
---|
87 |
|
---|
88 | #ifdef CONFIG_ENCODERS //strangely gcc includes this even if its not references
|
---|
89 | static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
|
---|
90 | #endif //CONFIG_ENCODERS
|
---|
91 |
|
---|
92 | static void common_init(MpegEncContext * s)
|
---|
93 | {
|
---|
94 | static int inited=0;
|
---|
95 |
|
---|
96 | switch(s->msmpeg4_version){
|
---|
97 | case 1:
|
---|
98 | case 2:
|
---|
99 | s->y_dc_scale_table=
|
---|
100 | s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
|
---|
101 | break;
|
---|
102 | case 3:
|
---|
103 | if(s->workaround_bugs){
|
---|
104 | s->y_dc_scale_table= old_ff_y_dc_scale_table;
|
---|
105 | s->c_dc_scale_table= old_ff_c_dc_scale_table;
|
---|
106 | } else{
|
---|
107 | s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
|
---|
108 | s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
|
---|
109 | }
|
---|
110 | break;
|
---|
111 | case 4:
|
---|
112 | case 5:
|
---|
113 | s->y_dc_scale_table= wmv1_y_dc_scale_table;
|
---|
114 | s->c_dc_scale_table= wmv1_c_dc_scale_table;
|
---|
115 | break;
|
---|
116 | #if defined(CONFIG_WMV3_DECODER)||defined(CONFIG_VC1_DECODER)
|
---|
117 | case 6:
|
---|
118 | s->y_dc_scale_table= wmv3_dc_scale_table;
|
---|
119 | s->c_dc_scale_table= wmv3_dc_scale_table;
|
---|
120 | break;
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | if(s->msmpeg4_version>=4){
|
---|
127 | ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , wmv1_scantable[1]);
|
---|
128 | ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, wmv1_scantable[2]);
|
---|
129 | ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, wmv1_scantable[3]);
|
---|
130 | ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , wmv1_scantable[0]);
|
---|
131 | }
|
---|
132 | //Note the default tables are set in common_init in mpegvideo.c
|
---|
133 |
|
---|
134 | if(!inited){
|
---|
135 | inited=1;
|
---|
136 |
|
---|
137 | init_h263_dc_for_msmpeg4();
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | #ifdef CONFIG_ENCODERS
|
---|
142 |
|
---|
143 | /* build the table which associate a (x,y) motion vector to a vlc */
|
---|
144 | static void init_mv_table(MVTable *tab)
|
---|
145 | {
|
---|
146 | int i, x, y;
|
---|
147 |
|
---|
148 | tab->table_mv_index = av_malloc(sizeof(uint16_t) * 4096);
|
---|
149 | /* mark all entries as not used */
|
---|
150 | for(i=0;i<4096;i++)
|
---|
151 | tab->table_mv_index[i] = tab->n;
|
---|
152 |
|
---|
153 | for(i=0;i<tab->n;i++) {
|
---|
154 | x = tab->table_mvx[i];
|
---|
155 | y = tab->table_mvy[i];
|
---|
156 | tab->table_mv_index[(x << 6) | y] = i;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | static void code012(PutBitContext *pb, int n)
|
---|
161 | {
|
---|
162 | if (n == 0) {
|
---|
163 | put_bits(pb, 1, 0);
|
---|
164 | } else {
|
---|
165 | put_bits(pb, 1, 1);
|
---|
166 | put_bits(pb, 1, (n >= 2));
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | void ff_msmpeg4_encode_init(MpegEncContext *s)
|
---|
171 | {
|
---|
172 | static int init_done=0;
|
---|
173 | int i;
|
---|
174 |
|
---|
175 | common_init(s);
|
---|
176 | if(s->msmpeg4_version>=4){
|
---|
177 | s->min_qcoeff= -255;
|
---|
178 | s->max_qcoeff= 255;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (!init_done) {
|
---|
182 | /* init various encoding tables */
|
---|
183 | init_done = 1;
|
---|
184 | init_mv_table(&mv_tables[0]);
|
---|
185 | init_mv_table(&mv_tables[1]);
|
---|
186 | for(i=0;i<NB_RL_TABLES;i++)
|
---|
187 | init_rl(&rl_table[i], 1);
|
---|
188 |
|
---|
189 | for(i=0; i<NB_RL_TABLES; i++){
|
---|
190 | int level;
|
---|
191 | for(level=0; level<=MAX_LEVEL; level++){
|
---|
192 | int run;
|
---|
193 | for(run=0; run<=MAX_RUN; run++){
|
---|
194 | int last;
|
---|
195 | for(last=0; last<2; last++){
|
---|
196 | rl_length[i][level][run][last]= get_size_of_code(s, &rl_table[ i], last, run, level, 0);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run, int level, int intra){
|
---|
205 | int size=0;
|
---|
206 | int code;
|
---|
207 | int run_diff= intra ? 0 : 1;
|
---|
208 |
|
---|
209 | code = get_rl_index(rl, last, run, level);
|
---|
210 | size+= rl->table_vlc[code][1];
|
---|
211 | if (code == rl->n) {
|
---|
212 | int level1, run1;
|
---|
213 |
|
---|
214 | level1 = level - rl->max_level[last][run];
|
---|
215 | if (level1 < 1)
|
---|
216 | goto esc2;
|
---|
217 | code = get_rl_index(rl, last, run, level1);
|
---|
218 | if (code == rl->n) {
|
---|
219 | esc2:
|
---|
220 | size++;
|
---|
221 | if (level > MAX_LEVEL)
|
---|
222 | goto esc3;
|
---|
223 | run1 = run - rl->max_run[last][level] - run_diff;
|
---|
224 | if (run1 < 0)
|
---|
225 | goto esc3;
|
---|
226 | code = get_rl_index(rl, last, run1, level);
|
---|
227 | if (code == rl->n) {
|
---|
228 | esc3:
|
---|
229 | /* third escape */
|
---|
230 | size+=1+1+6+8;
|
---|
231 | } else {
|
---|
232 | /* second escape */
|
---|
233 | size+= 1+1+ rl->table_vlc[code][1];
|
---|
234 | }
|
---|
235 | } else {
|
---|
236 | /* first escape */
|
---|
237 | size+= 1+1+ rl->table_vlc[code][1];
|
---|
238 | }
|
---|
239 | } else {
|
---|
240 | size++;
|
---|
241 | }
|
---|
242 | return size;
|
---|
243 | }
|
---|
244 |
|
---|
245 | static void find_best_tables(MpegEncContext * s)
|
---|
246 | {
|
---|
247 | int i;
|
---|
248 | int best =-1, best_size =9999999;
|
---|
249 | int chroma_best=-1, best_chroma_size=9999999;
|
---|
250 |
|
---|
251 | for(i=0; i<3; i++){
|
---|
252 | int level;
|
---|
253 | int chroma_size=0;
|
---|
254 | int size=0;
|
---|
255 |
|
---|
256 | if(i>0){// ;)
|
---|
257 | size++;
|
---|
258 | chroma_size++;
|
---|
259 | }
|
---|
260 | for(level=0; level<=MAX_LEVEL; level++){
|
---|
261 | int run;
|
---|
262 | for(run=0; run<=MAX_RUN; run++){
|
---|
263 | int last;
|
---|
264 | const int last_size= size + chroma_size;
|
---|
265 | for(last=0; last<2; last++){
|
---|
266 | int inter_count = s->ac_stats[0][0][level][run][last] + s->ac_stats[0][1][level][run][last];
|
---|
267 | int intra_luma_count = s->ac_stats[1][0][level][run][last];
|
---|
268 | int intra_chroma_count= s->ac_stats[1][1][level][run][last];
|
---|
269 |
|
---|
270 | if(s->pict_type==I_TYPE){
|
---|
271 | size += intra_luma_count *rl_length[i ][level][run][last];
|
---|
272 | chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
|
---|
273 | }else{
|
---|
274 | size+= intra_luma_count *rl_length[i ][level][run][last]
|
---|
275 | +intra_chroma_count*rl_length[i+3][level][run][last]
|
---|
276 | +inter_count *rl_length[i+3][level][run][last];
|
---|
277 | }
|
---|
278 | }
|
---|
279 | if(last_size == size+chroma_size) break;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | if(size<best_size){
|
---|
283 | best_size= size;
|
---|
284 | best= i;
|
---|
285 | }
|
---|
286 | if(chroma_size<best_chroma_size){
|
---|
287 | best_chroma_size= chroma_size;
|
---|
288 | chroma_best= i;
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | // printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n",
|
---|
293 | // s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
|
---|
294 |
|
---|
295 | if(s->pict_type==P_TYPE) chroma_best= best;
|
---|
296 |
|
---|
297 | memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
|
---|
298 |
|
---|
299 | s->rl_table_index = best;
|
---|
300 | s->rl_chroma_table_index= chroma_best;
|
---|
301 |
|
---|
302 | if(s->pict_type != s->last_non_b_pict_type){
|
---|
303 | s->rl_table_index= 2;
|
---|
304 | if(s->pict_type==I_TYPE)
|
---|
305 | s->rl_chroma_table_index= 1;
|
---|
306 | else
|
---|
307 | s->rl_chroma_table_index= 2;
|
---|
308 | }
|
---|
309 |
|
---|
310 | }
|
---|
311 |
|
---|
312 | /* write MSMPEG4 compatible frame header */
|
---|
313 | void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
|
---|
314 | {
|
---|
315 | find_best_tables(s);
|
---|
316 |
|
---|
317 | align_put_bits(&s->pb);
|
---|
318 | put_bits(&s->pb, 2, s->pict_type - 1);
|
---|
319 |
|
---|
320 | put_bits(&s->pb, 5, s->qscale);
|
---|
321 | if(s->msmpeg4_version<=2){
|
---|
322 | s->rl_table_index = 2;
|
---|
323 | s->rl_chroma_table_index = 2;
|
---|
324 | }
|
---|
325 |
|
---|
326 | s->dc_table_index = 1;
|
---|
327 | s->mv_table_index = 1; /* only if P frame */
|
---|
328 | s->use_skip_mb_code = 1; /* only if P frame */
|
---|
329 | s->per_mb_rl_table = 0;
|
---|
330 | if(s->msmpeg4_version==4)
|
---|
331 | s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE);
|
---|
332 | //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
|
---|
333 |
|
---|
334 | if (s->pict_type == I_TYPE) {
|
---|
335 | s->slice_height= s->mb_height/1;
|
---|
336 | put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
|
---|
337 |
|
---|
338 | if(s->msmpeg4_version==4){
|
---|
339 | msmpeg4_encode_ext_header(s);
|
---|
340 | if(s->bit_rate>MBAC_BITRATE)
|
---|
341 | put_bits(&s->pb, 1, s->per_mb_rl_table);
|
---|
342 | }
|
---|
343 |
|
---|
344 | if(s->msmpeg4_version>2){
|
---|
345 | if(!s->per_mb_rl_table){
|
---|
346 | code012(&s->pb, s->rl_chroma_table_index);
|
---|
347 | code012(&s->pb, s->rl_table_index);
|
---|
348 | }
|
---|
349 |
|
---|
350 | put_bits(&s->pb, 1, s->dc_table_index);
|
---|
351 | }
|
---|
352 | } else {
|
---|
353 | put_bits(&s->pb, 1, s->use_skip_mb_code);
|
---|
354 |
|
---|
355 | if(s->msmpeg4_version==4 && s->bit_rate>MBAC_BITRATE)
|
---|
356 | put_bits(&s->pb, 1, s->per_mb_rl_table);
|
---|
357 |
|
---|
358 | if(s->msmpeg4_version>2){
|
---|
359 | if(!s->per_mb_rl_table)
|
---|
360 | code012(&s->pb, s->rl_table_index);
|
---|
361 |
|
---|
362 | put_bits(&s->pb, 1, s->dc_table_index);
|
---|
363 |
|
---|
364 | put_bits(&s->pb, 1, s->mv_table_index);
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | s->esc3_level_length= 0;
|
---|
369 | s->esc3_run_length= 0;
|
---|
370 |
|
---|
371 | #ifdef DEBUG
|
---|
372 | intra_count = 0;
|
---|
373 | av_log(s->avctx, AV_LOG_DEBUG, "*****frame %d:\n", frame_count++);
|
---|
374 | #endif
|
---|
375 | }
|
---|
376 |
|
---|
377 | void msmpeg4_encode_ext_header(MpegEncContext * s)
|
---|
378 | {
|
---|
379 | put_bits(&s->pb, 5, s->avctx->time_base.den / s->avctx->time_base.num); //yes 29.97 -> 29
|
---|
380 |
|
---|
381 | put_bits(&s->pb, 11, FFMIN(s->bit_rate/1024, 2047));
|
---|
382 |
|
---|
383 | if(s->msmpeg4_version>=3)
|
---|
384 | put_bits(&s->pb, 1, s->flipflop_rounding);
|
---|
385 | else
|
---|
386 | assert(s->flipflop_rounding==0);
|
---|
387 | }
|
---|
388 |
|
---|
389 | #endif //CONFIG_ENCODERS
|
---|
390 |
|
---|
391 | /* predict coded block */
|
---|
392 | static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
|
---|
393 | {
|
---|
394 | int xy, wrap, pred, a, b, c;
|
---|
395 |
|
---|
396 | xy = s->block_index[n];
|
---|
397 | wrap = s->b8_stride;
|
---|
398 |
|
---|
399 | /* B C
|
---|
400 | * A X
|
---|
401 | */
|
---|
402 | a = s->coded_block[xy - 1 ];
|
---|
403 | b = s->coded_block[xy - 1 - wrap];
|
---|
404 | c = s->coded_block[xy - wrap];
|
---|
405 |
|
---|
406 | if (b == c) {
|
---|
407 | pred = a;
|
---|
408 | } else {
|
---|
409 | pred = c;
|
---|
410 | }
|
---|
411 |
|
---|
412 | /* store value */
|
---|
413 | *coded_block_ptr = &s->coded_block[xy];
|
---|
414 |
|
---|
415 | return pred;
|
---|
416 | }
|
---|
417 |
|
---|
418 | #ifdef CONFIG_ENCODERS
|
---|
419 |
|
---|
420 | static void msmpeg4_encode_motion(MpegEncContext * s,
|
---|
421 | int mx, int my)
|
---|
422 | {
|
---|
423 | int code;
|
---|
424 | MVTable *mv;
|
---|
425 |
|
---|
426 | /* modulo encoding */
|
---|
427 | /* WARNING : you cannot reach all the MVs even with the modulo
|
---|
428 | encoding. This is a somewhat strange compromise they took !!! */
|
---|
429 | if (mx <= -64)
|
---|
430 | mx += 64;
|
---|
431 | else if (mx >= 64)
|
---|
432 | mx -= 64;
|
---|
433 | if (my <= -64)
|
---|
434 | my += 64;
|
---|
435 | else if (my >= 64)
|
---|
436 | my -= 64;
|
---|
437 |
|
---|
438 | mx += 32;
|
---|
439 | my += 32;
|
---|
440 | #if 0
|
---|
441 | if ((unsigned)mx >= 64 ||
|
---|
442 | (unsigned)my >= 64)
|
---|
443 | av_log(s->avctx, AV_LOG_ERROR, "error mx=%d my=%d\n", mx, my);
|
---|
444 | #endif
|
---|
445 | mv = &mv_tables[s->mv_table_index];
|
---|
446 |
|
---|
447 | code = mv->table_mv_index[(mx << 6) | my];
|
---|
448 | put_bits(&s->pb,
|
---|
449 | mv->table_mv_bits[code],
|
---|
450 | mv->table_mv_code[code]);
|
---|
451 | if (code == mv->n) {
|
---|
452 | /* escape : code litterally */
|
---|
453 | put_bits(&s->pb, 6, mx);
|
---|
454 | put_bits(&s->pb, 6, my);
|
---|
455 | }
|
---|
456 | }
|
---|
457 |
|
---|
458 | static inline void handle_slices(MpegEncContext *s){
|
---|
459 | if (s->mb_x == 0) {
|
---|
460 | if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
|
---|
461 | if(s->msmpeg4_version < 4){
|
---|
462 | ff_mpeg4_clean_buffers(s);
|
---|
463 | }
|
---|
464 | s->first_slice_line = 1;
|
---|
465 | } else {
|
---|
466 | s->first_slice_line = 0;
|
---|
467 | }
|
---|
468 | }
|
---|
469 | }
|
---|
470 |
|
---|
471 | void msmpeg4_encode_mb(MpegEncContext * s,
|
---|
472 | DCTELEM block[6][64],
|
---|
473 | int motion_x, int motion_y)
|
---|
474 | {
|
---|
475 | int cbp, coded_cbp, i;
|
---|
476 | int pred_x, pred_y;
|
---|
477 | uint8_t *coded_block;
|
---|
478 |
|
---|
479 | handle_slices(s);
|
---|
480 |
|
---|
481 | if (!s->mb_intra) {
|
---|
482 | /* compute cbp */
|
---|
483 | cbp = 0;
|
---|
484 | for (i = 0; i < 6; i++) {
|
---|
485 | if (s->block_last_index[i] >= 0)
|
---|
486 | cbp |= 1 << (5 - i);
|
---|
487 | }
|
---|
488 | if (s->use_skip_mb_code && (cbp | motion_x | motion_y) == 0) {
|
---|
489 | /* skip macroblock */
|
---|
490 | put_bits(&s->pb, 1, 1);
|
---|
491 | s->last_bits++;
|
---|
492 | s->misc_bits++;
|
---|
493 | s->skip_count++;
|
---|
494 |
|
---|
495 | return;
|
---|
496 | }
|
---|
497 | if (s->use_skip_mb_code)
|
---|
498 | put_bits(&s->pb, 1, 0); /* mb coded */
|
---|
499 |
|
---|
500 | if(s->msmpeg4_version<=2){
|
---|
501 | put_bits(&s->pb,
|
---|
502 | v2_mb_type[cbp&3][1],
|
---|
503 | v2_mb_type[cbp&3][0]);
|
---|
504 | if((cbp&3) != 3) coded_cbp= cbp ^ 0x3C;
|
---|
505 | else coded_cbp= cbp;
|
---|
506 |
|
---|
507 | put_bits(&s->pb,
|
---|
508 | cbpy_tab[coded_cbp>>2][1],
|
---|
509 | cbpy_tab[coded_cbp>>2][0]);
|
---|
510 |
|
---|
511 | s->misc_bits += get_bits_diff(s);
|
---|
512 |
|
---|
513 | h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
|
---|
514 | msmpeg4v2_encode_motion(s, motion_x - pred_x);
|
---|
515 | msmpeg4v2_encode_motion(s, motion_y - pred_y);
|
---|
516 | }else{
|
---|
517 | put_bits(&s->pb,
|
---|
518 | table_mb_non_intra[cbp + 64][1],
|
---|
519 | table_mb_non_intra[cbp + 64][0]);
|
---|
520 |
|
---|
521 | s->misc_bits += get_bits_diff(s);
|
---|
522 |
|
---|
523 | /* motion vector */
|
---|
524 | h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
|
---|
525 | msmpeg4_encode_motion(s, motion_x - pred_x,
|
---|
526 | motion_y - pred_y);
|
---|
527 | }
|
---|
528 |
|
---|
529 | s->mv_bits += get_bits_diff(s);
|
---|
530 |
|
---|
531 | for (i = 0; i < 6; i++) {
|
---|
532 | msmpeg4_encode_block(s, block[i], i);
|
---|
533 | }
|
---|
534 | s->p_tex_bits += get_bits_diff(s);
|
---|
535 | } else {
|
---|
536 | /* compute cbp */
|
---|
537 | cbp = 0;
|
---|
538 | coded_cbp = 0;
|
---|
539 | for (i = 0; i < 6; i++) {
|
---|
540 | int val, pred;
|
---|
541 | val = (s->block_last_index[i] >= 1);
|
---|
542 | cbp |= val << (5 - i);
|
---|
543 | if (i < 4) {
|
---|
544 | /* predict value for close blocks only for luma */
|
---|
545 | pred = coded_block_pred(s, i, &coded_block);
|
---|
546 | *coded_block = val;
|
---|
547 | val = val ^ pred;
|
---|
548 | }
|
---|
549 | coded_cbp |= val << (5 - i);
|
---|
550 | }
|
---|
551 | #if 0
|
---|
552 | if (coded_cbp)
|
---|
553 | printf("cbp=%x %x\n", cbp, coded_cbp);
|
---|
554 | #endif
|
---|
555 |
|
---|
556 | if(s->msmpeg4_version<=2){
|
---|
557 | if (s->pict_type == I_TYPE) {
|
---|
558 | put_bits(&s->pb,
|
---|
559 | v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
|
---|
560 | } else {
|
---|
561 | if (s->use_skip_mb_code)
|
---|
562 | put_bits(&s->pb, 1, 0); /* mb coded */
|
---|
563 | put_bits(&s->pb,
|
---|
564 | v2_mb_type[(cbp&3) + 4][1],
|
---|
565 | v2_mb_type[(cbp&3) + 4][0]);
|
---|
566 | }
|
---|
567 | put_bits(&s->pb, 1, 0); /* no AC prediction yet */
|
---|
568 | put_bits(&s->pb,
|
---|
569 | cbpy_tab[cbp>>2][1],
|
---|
570 | cbpy_tab[cbp>>2][0]);
|
---|
571 | }else{
|
---|
572 | if (s->pict_type == I_TYPE) {
|
---|
573 | put_bits(&s->pb,
|
---|
574 | ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
|
---|
575 | } else {
|
---|
576 | if (s->use_skip_mb_code)
|
---|
577 | put_bits(&s->pb, 1, 0); /* mb coded */
|
---|
578 | put_bits(&s->pb,
|
---|
579 | table_mb_non_intra[cbp][1],
|
---|
580 | table_mb_non_intra[cbp][0]);
|
---|
581 | }
|
---|
582 | put_bits(&s->pb, 1, 0); /* no AC prediction yet */
|
---|
583 | if(s->inter_intra_pred){
|
---|
584 | s->h263_aic_dir=0;
|
---|
585 | put_bits(&s->pb, table_inter_intra[s->h263_aic_dir][1], table_inter_intra[s->h263_aic_dir][0]);
|
---|
586 | }
|
---|
587 | }
|
---|
588 | s->misc_bits += get_bits_diff(s);
|
---|
589 |
|
---|
590 | for (i = 0; i < 6; i++) {
|
---|
591 | msmpeg4_encode_block(s, block[i], i);
|
---|
592 | }
|
---|
593 | s->i_tex_bits += get_bits_diff(s);
|
---|
594 | s->i_count++;
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | #endif //CONFIG_ENCODERS
|
---|
599 |
|
---|
600 | static inline int msmpeg4v1_pred_dc(MpegEncContext * s, int n,
|
---|
601 | int32_t **dc_val_ptr)
|
---|
602 | {
|
---|
603 | int i;
|
---|
604 |
|
---|
605 | if (n < 4) {
|
---|
606 | i= 0;
|
---|
607 | } else {
|
---|
608 | i= n-3;
|
---|
609 | }
|
---|
610 |
|
---|
611 | *dc_val_ptr= &s->last_dc[i];
|
---|
612 | return s->last_dc[i];
|
---|
613 | }
|
---|
614 |
|
---|
615 | static int get_dc(uint8_t *src, int stride, int scale)
|
---|
616 | {
|
---|
617 | int y;
|
---|
618 | int sum=0;
|
---|
619 | for(y=0; y<8; y++){
|
---|
620 | int x;
|
---|
621 | for(x=0; x<8; x++){
|
---|
622 | sum+=src[x + y*stride];
|
---|
623 | }
|
---|
624 | }
|
---|
625 | return FASTDIV((sum + (scale>>1)), scale);
|
---|
626 | }
|
---|
627 |
|
---|
628 | /* dir = 0: left, dir = 1: top prediction */
|
---|
629 | static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
|
---|
630 | uint16_t **dc_val_ptr, int *dir_ptr)
|
---|
631 | {
|
---|
632 | int a, b, c, wrap, pred, scale;
|
---|
633 | int16_t *dc_val;
|
---|
634 |
|
---|
635 | /* find prediction */
|
---|
636 | if (n < 4) {
|
---|
637 | scale = s->y_dc_scale;
|
---|
638 | } else {
|
---|
639 | scale = s->c_dc_scale;
|
---|
640 | }
|
---|
641 |
|
---|
642 | wrap = s->block_wrap[n];
|
---|
643 | dc_val= s->dc_val[0] + s->block_index[n];
|
---|
644 |
|
---|
645 | /* B C
|
---|
646 | * A X
|
---|
647 | */
|
---|
648 | a = dc_val[ - 1];
|
---|
649 | b = dc_val[ - 1 - wrap];
|
---|
650 | c = dc_val[ - wrap];
|
---|
651 |
|
---|
652 | if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
|
---|
653 | b=c=1024;
|
---|
654 | }
|
---|
655 |
|
---|
656 | /* XXX: the following solution consumes divisions, but it does not
|
---|
657 | necessitate to modify mpegvideo.c. The problem comes from the
|
---|
658 | fact they decided to store the quantized DC (which would lead
|
---|
659 | to problems if Q could vary !) */
|
---|
660 | #if (defined(ARCH_X86) || defined(ARCH_X86_64)) && !defined PIC
|
---|
661 | asm volatile(
|
---|
662 | "movl %3, %%eax \n\t"
|
---|
663 | "shrl $1, %%eax \n\t"
|
---|
664 | "addl %%eax, %2 \n\t"
|
---|
665 | "addl %%eax, %1 \n\t"
|
---|
666 | "addl %0, %%eax \n\t"
|
---|
667 | "mull %4 \n\t"
|
---|
668 | "movl %%edx, %0 \n\t"
|
---|
669 | "movl %1, %%eax \n\t"
|
---|
670 | "mull %4 \n\t"
|
---|
671 | "movl %%edx, %1 \n\t"
|
---|
672 | "movl %2, %%eax \n\t"
|
---|
673 | "mull %4 \n\t"
|
---|
674 | "movl %%edx, %2 \n\t"
|
---|
675 | : "+b" (a), "+c" (b), "+D" (c)
|
---|
676 | : "g" (scale), "S" (inverse[scale])
|
---|
677 | : "%eax", "%edx"
|
---|
678 | );
|
---|
679 | #else
|
---|
680 | /* #elif defined (ARCH_ALPHA) */
|
---|
681 | /* Divisions are extremely costly on Alpha; optimize the most
|
---|
682 | common case. But they are costly everywhere...
|
---|
683 | */
|
---|
684 | if (scale == 8) {
|
---|
685 | a = (a + (8 >> 1)) / 8;
|
---|
686 | b = (b + (8 >> 1)) / 8;
|
---|
687 | c = (c + (8 >> 1)) / 8;
|
---|
688 | } else {
|
---|
689 | a = FASTDIV((a + (scale >> 1)), scale);
|
---|
690 | b = FASTDIV((b + (scale >> 1)), scale);
|
---|
691 | c = FASTDIV((c + (scale >> 1)), scale);
|
---|
692 | }
|
---|
693 | #endif
|
---|
694 | /* XXX: WARNING: they did not choose the same test as MPEG4. This
|
---|
695 | is very important ! */
|
---|
696 | if(s->msmpeg4_version>3){
|
---|
697 | if(s->inter_intra_pred){
|
---|
698 | uint8_t *dest;
|
---|
699 | int wrap;
|
---|
700 |
|
---|
701 | if(n==1){
|
---|
702 | pred=a;
|
---|
703 | *dir_ptr = 0;
|
---|
704 | }else if(n==2){
|
---|
705 | pred=c;
|
---|
706 | *dir_ptr = 1;
|
---|
707 | }else if(n==3){
|
---|
708 | if (abs(a - b) < abs(b - c)) {
|
---|
709 | pred = c;
|
---|
710 | *dir_ptr = 1;
|
---|
711 | } else {
|
---|
712 | pred = a;
|
---|
713 | *dir_ptr = 0;
|
---|
714 | }
|
---|
715 | }else{
|
---|
716 | if(n<4){
|
---|
717 | wrap= s->linesize;
|
---|
718 | dest= s->current_picture.data[0] + (((n>>1) + 2*s->mb_y) * 8* wrap ) + ((n&1) + 2*s->mb_x) * 8;
|
---|
719 | }else{
|
---|
720 | wrap= s->uvlinesize;
|
---|
721 | dest= s->current_picture.data[n-3] + (s->mb_y * 8 * wrap) + s->mb_x * 8;
|
---|
722 | }
|
---|
723 | if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
|
---|
724 | else a= get_dc(dest-8, wrap, scale*8);
|
---|
725 | if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
|
---|
726 | else c= get_dc(dest-8*wrap, wrap, scale*8);
|
---|
727 |
|
---|
728 | if (s->h263_aic_dir==0) {
|
---|
729 | pred= a;
|
---|
730 | *dir_ptr = 0;
|
---|
731 | }else if (s->h263_aic_dir==1) {
|
---|
732 | if(n==0){
|
---|
733 | pred= c;
|
---|
734 | *dir_ptr = 1;
|
---|
735 | }else{
|
---|
736 | pred= a;
|
---|
737 | *dir_ptr = 0;
|
---|
738 | }
|
---|
739 | }else if (s->h263_aic_dir==2) {
|
---|
740 | if(n==0){
|
---|
741 | pred= a;
|
---|
742 | *dir_ptr = 0;
|
---|
743 | }else{
|
---|
744 | pred= c;
|
---|
745 | *dir_ptr = 1;
|
---|
746 | }
|
---|
747 | } else {
|
---|
748 | pred= c;
|
---|
749 | *dir_ptr = 1;
|
---|
750 | }
|
---|
751 | }
|
---|
752 | }else{
|
---|
753 | if (abs(a - b) < abs(b - c)) {
|
---|
754 | pred = c;
|
---|
755 | *dir_ptr = 1;
|
---|
756 | } else {
|
---|
757 | pred = a;
|
---|
758 | *dir_ptr = 0;
|
---|
759 | }
|
---|
760 | }
|
---|
761 | }else{
|
---|
762 | if (abs(a - b) <= abs(b - c)) {
|
---|
763 | pred = c;
|
---|
764 | *dir_ptr = 1;
|
---|
765 | } else {
|
---|
766 | pred = a;
|
---|
767 | *dir_ptr = 0;
|
---|
768 | }
|
---|
769 | }
|
---|
770 |
|
---|
771 | /* update predictor */
|
---|
772 | *dc_val_ptr = &dc_val[0];
|
---|
773 | return pred;
|
---|
774 | }
|
---|
775 |
|
---|
776 | #define DC_MAX 119
|
---|
777 |
|
---|
778 | static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
|
---|
779 | {
|
---|
780 | int sign, code;
|
---|
781 | int pred;
|
---|
782 |
|
---|
783 | if(s->msmpeg4_version==1){
|
---|
784 | int32_t *dc_val;
|
---|
785 | pred = msmpeg4v1_pred_dc(s, n, &dc_val);
|
---|
786 |
|
---|
787 | /* update predictor */
|
---|
788 | *dc_val= level;
|
---|
789 | }else{
|
---|
790 | uint16_t *dc_val;
|
---|
791 | pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
|
---|
792 |
|
---|
793 | /* update predictor */
|
---|
794 | if (n < 4) {
|
---|
795 | *dc_val = level * s->y_dc_scale;
|
---|
796 | } else {
|
---|
797 | *dc_val = level * s->c_dc_scale;
|
---|
798 | }
|
---|
799 | }
|
---|
800 |
|
---|
801 | /* do the prediction */
|
---|
802 | level -= pred;
|
---|
803 |
|
---|
804 | if(s->msmpeg4_version<=2){
|
---|
805 | if (n < 4) {
|
---|
806 | put_bits(&s->pb,
|
---|
807 | v2_dc_lum_table[level+256][1],
|
---|
808 | v2_dc_lum_table[level+256][0]);
|
---|
809 | }else{
|
---|
810 | put_bits(&s->pb,
|
---|
811 | v2_dc_chroma_table[level+256][1],
|
---|
812 | v2_dc_chroma_table[level+256][0]);
|
---|
813 | }
|
---|
814 | }else{
|
---|
815 | sign = 0;
|
---|
816 | if (level < 0) {
|
---|
817 | level = -level;
|
---|
818 | sign = 1;
|
---|
819 | }
|
---|
820 | code = level;
|
---|
821 | if (code > DC_MAX)
|
---|
822 | code = DC_MAX;
|
---|
823 |
|
---|
824 | if (s->dc_table_index == 0) {
|
---|
825 | if (n < 4) {
|
---|
826 | put_bits(&s->pb, ff_table0_dc_lum[code][1], ff_table0_dc_lum[code][0]);
|
---|
827 | } else {
|
---|
828 | put_bits(&s->pb, ff_table0_dc_chroma[code][1], ff_table0_dc_chroma[code][0]);
|
---|
829 | }
|
---|
830 | } else {
|
---|
831 | if (n < 4) {
|
---|
832 | put_bits(&s->pb, ff_table1_dc_lum[code][1], ff_table1_dc_lum[code][0]);
|
---|
833 | } else {
|
---|
834 | put_bits(&s->pb, ff_table1_dc_chroma[code][1], ff_table1_dc_chroma[code][0]);
|
---|
835 | }
|
---|
836 | }
|
---|
837 |
|
---|
838 | if (code == DC_MAX)
|
---|
839 | put_bits(&s->pb, 8, level);
|
---|
840 |
|
---|
841 | if (level != 0) {
|
---|
842 | put_bits(&s->pb, 1, sign);
|
---|
843 | }
|
---|
844 | }
|
---|
845 | }
|
---|
846 |
|
---|
847 | /* Encoding of a block. Very similar to MPEG4 except for a different
|
---|
848 | escape coding (same as H263) and more vlc tables.
|
---|
849 | */
|
---|
850 | static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
|
---|
851 | {
|
---|
852 | int level, run, last, i, j, last_index;
|
---|
853 | int last_non_zero, sign, slevel;
|
---|
854 | int code, run_diff, dc_pred_dir;
|
---|
855 | const RLTable *rl;
|
---|
856 | const uint8_t *scantable;
|
---|
857 |
|
---|
858 | if (s->mb_intra) {
|
---|
859 | msmpeg4_encode_dc(s, block[0], n, &dc_pred_dir);
|
---|
860 | i = 1;
|
---|
861 | if (n < 4) {
|
---|
862 | rl = &rl_table[s->rl_table_index];
|
---|
863 | } else {
|
---|
864 | rl = &rl_table[3 + s->rl_chroma_table_index];
|
---|
865 | }
|
---|
866 | run_diff = 0;
|
---|
867 | scantable= s->intra_scantable.permutated;
|
---|
868 | } else {
|
---|
869 | i = 0;
|
---|
870 | rl = &rl_table[3 + s->rl_table_index];
|
---|
871 | if(s->msmpeg4_version<=2)
|
---|
872 | run_diff = 0;
|
---|
873 | else
|
---|
874 | run_diff = 1;
|
---|
875 | scantable= s->inter_scantable.permutated;
|
---|
876 | }
|
---|
877 |
|
---|
878 | /* recalculate block_last_index for M$ wmv1 */
|
---|
879 | if(s->msmpeg4_version>=4 && s->block_last_index[n]>0){
|
---|
880 | for(last_index=63; last_index>=0; last_index--){
|
---|
881 | if(block[scantable[last_index]]) break;
|
---|
882 | }
|
---|
883 | s->block_last_index[n]= last_index;
|
---|
884 | }else
|
---|
885 | last_index = s->block_last_index[n];
|
---|
886 | /* AC coefs */
|
---|
887 | last_non_zero = i - 1;
|
---|
888 | for (; i <= last_index; i++) {
|
---|
889 | j = scantable[i];
|
---|
890 | level = block[j];
|
---|
891 | if (level) {
|
---|
892 | run = i - last_non_zero - 1;
|
---|
893 | last = (i == last_index);
|
---|
894 | sign = 0;
|
---|
895 | slevel = level;
|
---|
896 | if (level < 0) {
|
---|
897 | sign = 1;
|
---|
898 | level = -level;
|
---|
899 | }
|
---|
900 |
|
---|
901 | if(level<=MAX_LEVEL && run<=MAX_RUN){
|
---|
902 | s->ac_stats[s->mb_intra][n>3][level][run][last]++;
|
---|
903 | }
|
---|
904 | #if 0
|
---|
905 | else
|
---|
906 | s->ac_stats[s->mb_intra][n>3][40][63][0]++; //esc3 like
|
---|
907 | #endif
|
---|
908 | code = get_rl_index(rl, last, run, level);
|
---|
909 | put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
|
---|
910 | if (code == rl->n) {
|
---|
911 | int level1, run1;
|
---|
912 |
|
---|
913 | level1 = level - rl->max_level[last][run];
|
---|
914 | if (level1 < 1)
|
---|
915 | goto esc2;
|
---|
916 | code = get_rl_index(rl, last, run, level1);
|
---|
917 | if (code == rl->n) {
|
---|
918 | esc2:
|
---|
919 | put_bits(&s->pb, 1, 0);
|
---|
920 | if (level > MAX_LEVEL)
|
---|
921 | goto esc3;
|
---|
922 | run1 = run - rl->max_run[last][level] - run_diff;
|
---|
923 | if (run1 < 0)
|
---|
924 | goto esc3;
|
---|
925 | code = get_rl_index(rl, last, run1, level);
|
---|
926 | if (code == rl->n) {
|
---|
927 | esc3:
|
---|
928 | /* third escape */
|
---|
929 | put_bits(&s->pb, 1, 0);
|
---|
930 | put_bits(&s->pb, 1, last);
|
---|
931 | if(s->msmpeg4_version>=4){
|
---|
932 | if(s->esc3_level_length==0){
|
---|
933 | s->esc3_level_length=8;
|
---|
934 | s->esc3_run_length= 6;
|
---|
935 | if(s->qscale<8)
|
---|
936 | put_bits(&s->pb, 6, 3);
|
---|
937 | else
|
---|
938 | put_bits(&s->pb, 8, 3);
|
---|
939 | }
|
---|
940 | put_bits(&s->pb, s->esc3_run_length, run);
|
---|
941 | put_bits(&s->pb, 1, sign);
|
---|
942 | put_bits(&s->pb, s->esc3_level_length, level);
|
---|
943 | }else{
|
---|
944 | put_bits(&s->pb, 6, run);
|
---|
945 | put_bits(&s->pb, 8, slevel & 0xff);
|
---|
946 | }
|
---|
947 | } else {
|
---|
948 | /* second escape */
|
---|
949 | put_bits(&s->pb, 1, 1);
|
---|
950 | put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
|
---|
951 | put_bits(&s->pb, 1, sign);
|
---|
952 | }
|
---|
953 | } else {
|
---|
954 | /* first escape */
|
---|
955 | put_bits(&s->pb, 1, 1);
|
---|
956 | put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
|
---|
957 | put_bits(&s->pb, 1, sign);
|
---|
958 | }
|
---|
959 | } else {
|
---|
960 | put_bits(&s->pb, 1, sign);
|
---|
961 | }
|
---|
962 | last_non_zero = i;
|
---|
963 | }
|
---|
964 | }
|
---|
965 | }
|
---|
966 |
|
---|
967 | /****************************************/
|
---|
968 | /* decoding stuff */
|
---|
969 |
|
---|
970 | static VLC mb_non_intra_vlc[4];
|
---|
971 | VLC ff_msmp4_mb_i_vlc;
|
---|
972 | VLC ff_msmp4_dc_luma_vlc[2];
|
---|
973 | VLC ff_msmp4_dc_chroma_vlc[2];
|
---|
974 | static VLC v2_dc_lum_vlc;
|
---|
975 | static VLC v2_dc_chroma_vlc;
|
---|
976 | static VLC cbpy_vlc;
|
---|
977 | static VLC v2_intra_cbpc_vlc;
|
---|
978 | static VLC v2_mb_type_vlc;
|
---|
979 | static VLC v2_mv_vlc;
|
---|
980 | static VLC v1_intra_cbpc_vlc;
|
---|
981 | static VLC v1_inter_cbpc_vlc;
|
---|
982 | static VLC inter_intra_vlc;
|
---|
983 |
|
---|
984 | /* this table is practically identical to the one from h263 except that its inverted */
|
---|
985 | static void init_h263_dc_for_msmpeg4(void)
|
---|
986 | {
|
---|
987 | int level, uni_code, uni_len;
|
---|
988 |
|
---|
989 | for(level=-256; level<256; level++){
|
---|
990 | int size, v, l;
|
---|
991 | /* find number of bits */
|
---|
992 | size = 0;
|
---|
993 | v = abs(level);
|
---|
994 | while (v) {
|
---|
995 | v >>= 1;
|
---|
996 | size++;
|
---|
997 | }
|
---|
998 |
|
---|
999 | if (level < 0)
|
---|
1000 | l= (-level) ^ ((1 << size) - 1);
|
---|
1001 | else
|
---|
1002 | l= level;
|
---|
1003 |
|
---|
1004 | /* luminance h263 */
|
---|
1005 | uni_code= DCtab_lum[size][0];
|
---|
1006 | uni_len = DCtab_lum[size][1];
|
---|
1007 | uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
|
---|
1008 |
|
---|
1009 | if (size > 0) {
|
---|
1010 | uni_code<<=size; uni_code|=l;
|
---|
1011 | uni_len+=size;
|
---|
1012 | if (size > 8){
|
---|
1013 | uni_code<<=1; uni_code|=1;
|
---|
1014 | uni_len++;
|
---|
1015 | }
|
---|
1016 | }
|
---|
1017 | v2_dc_lum_table[level+256][0]= uni_code;
|
---|
1018 | v2_dc_lum_table[level+256][1]= uni_len;
|
---|
1019 |
|
---|
1020 | /* chrominance h263 */
|
---|
1021 | uni_code= DCtab_chrom[size][0];
|
---|
1022 | uni_len = DCtab_chrom[size][1];
|
---|
1023 | uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
|
---|
1024 |
|
---|
1025 | if (size > 0) {
|
---|
1026 | uni_code<<=size; uni_code|=l;
|
---|
1027 | uni_len+=size;
|
---|
1028 | if (size > 8){
|
---|
1029 | uni_code<<=1; uni_code|=1;
|
---|
1030 | uni_len++;
|
---|
1031 | }
|
---|
1032 | }
|
---|
1033 | v2_dc_chroma_table[level+256][0]= uni_code;
|
---|
1034 | v2_dc_chroma_table[level+256][1]= uni_len;
|
---|
1035 |
|
---|
1036 | }
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | /* init all vlc decoding tables */
|
---|
1040 | int ff_msmpeg4_decode_init(MpegEncContext *s)
|
---|
1041 | {
|
---|
1042 | static int done = 0;
|
---|
1043 | int i;
|
---|
1044 | MVTable *mv;
|
---|
1045 |
|
---|
1046 | common_init(s);
|
---|
1047 |
|
---|
1048 | if (!done) {
|
---|
1049 | done = 1;
|
---|
1050 |
|
---|
1051 | for(i=0;i<NB_RL_TABLES;i++) {
|
---|
1052 | init_rl(&rl_table[i], 1);
|
---|
1053 | init_vlc_rl(&rl_table[i], 1);
|
---|
1054 | }
|
---|
1055 | for(i=0;i<2;i++) {
|
---|
1056 | mv = &mv_tables[i];
|
---|
1057 | init_vlc(&mv->vlc, MV_VLC_BITS, mv->n + 1,
|
---|
1058 | mv->table_mv_bits, 1, 1,
|
---|
1059 | mv->table_mv_code, 2, 2, 1);
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | init_vlc(&ff_msmp4_dc_luma_vlc[0], DC_VLC_BITS, 120,
|
---|
1063 | &ff_table0_dc_lum[0][1], 8, 4,
|
---|
1064 | &ff_table0_dc_lum[0][0], 8, 4, 1);
|
---|
1065 | init_vlc(&ff_msmp4_dc_chroma_vlc[0], DC_VLC_BITS, 120,
|
---|
1066 | &ff_table0_dc_chroma[0][1], 8, 4,
|
---|
1067 | &ff_table0_dc_chroma[0][0], 8, 4, 1);
|
---|
1068 | init_vlc(&ff_msmp4_dc_luma_vlc[1], DC_VLC_BITS, 120,
|
---|
1069 | &ff_table1_dc_lum[0][1], 8, 4,
|
---|
1070 | &ff_table1_dc_lum[0][0], 8, 4, 1);
|
---|
1071 | init_vlc(&ff_msmp4_dc_chroma_vlc[1], DC_VLC_BITS, 120,
|
---|
1072 | &ff_table1_dc_chroma[0][1], 8, 4,
|
---|
1073 | &ff_table1_dc_chroma[0][0], 8, 4, 1);
|
---|
1074 |
|
---|
1075 | init_vlc(&v2_dc_lum_vlc, DC_VLC_BITS, 512,
|
---|
1076 | &v2_dc_lum_table[0][1], 8, 4,
|
---|
1077 | &v2_dc_lum_table[0][0], 8, 4, 1);
|
---|
1078 | init_vlc(&v2_dc_chroma_vlc, DC_VLC_BITS, 512,
|
---|
1079 | &v2_dc_chroma_table[0][1], 8, 4,
|
---|
1080 | &v2_dc_chroma_table[0][0], 8, 4, 1);
|
---|
1081 |
|
---|
1082 | init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
|
---|
1083 | &cbpy_tab[0][1], 2, 1,
|
---|
1084 | &cbpy_tab[0][0], 2, 1, 1);
|
---|
1085 | init_vlc(&v2_intra_cbpc_vlc, V2_INTRA_CBPC_VLC_BITS, 4,
|
---|
1086 | &v2_intra_cbpc[0][1], 2, 1,
|
---|
1087 | &v2_intra_cbpc[0][0], 2, 1, 1);
|
---|
1088 | init_vlc(&v2_mb_type_vlc, V2_MB_TYPE_VLC_BITS, 8,
|
---|
1089 | &v2_mb_type[0][1], 2, 1,
|
---|
1090 | &v2_mb_type[0][0], 2, 1, 1);
|
---|
1091 | init_vlc(&v2_mv_vlc, V2_MV_VLC_BITS, 33,
|
---|
1092 | &mvtab[0][1], 2, 1,
|
---|
1093 | &mvtab[0][0], 2, 1, 1);
|
---|
1094 |
|
---|
1095 | for(i=0; i<4; i++){
|
---|
1096 | init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128,
|
---|
1097 | &wmv2_inter_table[i][0][1], 8, 4,
|
---|
1098 | &wmv2_inter_table[i][0][0], 8, 4, 1); //FIXME name?
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
|
---|
1102 | &ff_msmp4_mb_i_table[0][1], 4, 2,
|
---|
1103 | &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
|
---|
1104 |
|
---|
1105 | init_vlc(&v1_intra_cbpc_vlc, V1_INTRA_CBPC_VLC_BITS, 8,
|
---|
1106 | intra_MCBPC_bits, 1, 1,
|
---|
1107 | intra_MCBPC_code, 1, 1, 1);
|
---|
1108 | init_vlc(&v1_inter_cbpc_vlc, V1_INTER_CBPC_VLC_BITS, 25,
|
---|
1109 | inter_MCBPC_bits, 1, 1,
|
---|
1110 | inter_MCBPC_code, 1, 1, 1);
|
---|
1111 |
|
---|
1112 | init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
|
---|
1113 | &table_inter_intra[0][1], 2, 1,
|
---|
1114 | &table_inter_intra[0][0], 2, 1, 1);
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | switch(s->msmpeg4_version){
|
---|
1118 | case 1:
|
---|
1119 | case 2:
|
---|
1120 | s->decode_mb= msmpeg4v12_decode_mb;
|
---|
1121 | break;
|
---|
1122 | case 3:
|
---|
1123 | case 4:
|
---|
1124 | s->decode_mb= msmpeg4v34_decode_mb;
|
---|
1125 | break;
|
---|
1126 | case 5:
|
---|
1127 | s->decode_mb= wmv2_decode_mb;
|
---|
1128 | case 6:
|
---|
1129 | //FIXME + TODO VC1 decode mb
|
---|
1130 | break;
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | s->slice_height= s->mb_height; //to avoid 1/0 if the first frame isnt a keyframe
|
---|
1134 |
|
---|
1135 | return 0;
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | int msmpeg4_decode_picture_header(MpegEncContext * s)
|
---|
1139 | {
|
---|
1140 | int code;
|
---|
1141 |
|
---|
1142 | #if 0
|
---|
1143 | {
|
---|
1144 | int i;
|
---|
1145 | for(i=0; i<s->gb.size_in_bits; i++)
|
---|
1146 | av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
|
---|
1147 | // get_bits1(&s->gb);
|
---|
1148 | av_log(s->avctx, AV_LOG_DEBUG, "END\n");
|
---|
1149 | return -1;
|
---|
1150 | }
|
---|
1151 | #endif
|
---|
1152 |
|
---|
1153 | if(s->msmpeg4_version==1){
|
---|
1154 | int start_code, num;
|
---|
1155 | start_code = (get_bits(&s->gb, 16)<<16) | get_bits(&s->gb, 16);
|
---|
1156 | if(start_code!=0x00000100){
|
---|
1157 | av_log(s->avctx, AV_LOG_ERROR, "invalid startcode\n");
|
---|
1158 | return -1;
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | num= get_bits(&s->gb, 5); // frame number */
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | s->pict_type = get_bits(&s->gb, 2) + 1;
|
---|
1165 | if (s->pict_type != I_TYPE &&
|
---|
1166 | s->pict_type != P_TYPE){
|
---|
1167 | av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
|
---|
1168 | return -1;
|
---|
1169 | }
|
---|
1170 | #if 0
|
---|
1171 | {
|
---|
1172 | static int had_i=0;
|
---|
1173 | if(s->pict_type == I_TYPE) had_i=1;
|
---|
1174 | if(!had_i) return -1;
|
---|
1175 | }
|
---|
1176 | #endif
|
---|
1177 | s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
|
---|
1178 | if(s->qscale==0){
|
---|
1179 | av_log(s->avctx, AV_LOG_ERROR, "invalid qscale\n");
|
---|
1180 | return -1;
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | if (s->pict_type == I_TYPE) {
|
---|
1184 | code = get_bits(&s->gb, 5);
|
---|
1185 | if(s->msmpeg4_version==1){
|
---|
1186 | if(code==0 || code>s->mb_height){
|
---|
1187 | av_log(s->avctx, AV_LOG_ERROR, "invalid slice height %d\n", code);
|
---|
1188 | return -1;
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | s->slice_height = code;
|
---|
1192 | }else{
|
---|
1193 | /* 0x17: one slice, 0x18: two slices, ... */
|
---|
1194 | if (code < 0x17){
|
---|
1195 | av_log(s->avctx, AV_LOG_ERROR, "error, slice code was %X\n", code);
|
---|
1196 | return -1;
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | s->slice_height = s->mb_height / (code - 0x16);
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | switch(s->msmpeg4_version){
|
---|
1203 | case 1:
|
---|
1204 | case 2:
|
---|
1205 | s->rl_chroma_table_index = 2;
|
---|
1206 | s->rl_table_index = 2;
|
---|
1207 |
|
---|
1208 | s->dc_table_index = 0; //not used
|
---|
1209 | break;
|
---|
1210 | case 3:
|
---|
1211 | s->rl_chroma_table_index = decode012(&s->gb);
|
---|
1212 | s->rl_table_index = decode012(&s->gb);
|
---|
1213 |
|
---|
1214 | s->dc_table_index = get_bits1(&s->gb);
|
---|
1215 | break;
|
---|
1216 | case 4:
|
---|
1217 | msmpeg4_decode_ext_header(s, (2+5+5+17+7)/8);
|
---|
1218 |
|
---|
1219 | if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
|
---|
1220 | else s->per_mb_rl_table= 0;
|
---|
1221 |
|
---|
1222 | if(!s->per_mb_rl_table){
|
---|
1223 | s->rl_chroma_table_index = decode012(&s->gb);
|
---|
1224 | s->rl_table_index = decode012(&s->gb);
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | s->dc_table_index = get_bits1(&s->gb);
|
---|
1228 | s->inter_intra_pred= 0;
|
---|
1229 | break;
|
---|
1230 | }
|
---|
1231 | s->no_rounding = 1;
|
---|
1232 | if(s->avctx->debug&FF_DEBUG_PICT_INFO)
|
---|
1233 | av_log(s->avctx, AV_LOG_DEBUG, "qscale:%d rlc:%d rl:%d dc:%d mbrl:%d slice:%d \n",
|
---|
1234 | s->qscale,
|
---|
1235 | s->rl_chroma_table_index,
|
---|
1236 | s->rl_table_index,
|
---|
1237 | s->dc_table_index,
|
---|
1238 | s->per_mb_rl_table,
|
---|
1239 | s->slice_height);
|
---|
1240 | } else {
|
---|
1241 | switch(s->msmpeg4_version){
|
---|
1242 | case 1:
|
---|
1243 | case 2:
|
---|
1244 | if(s->msmpeg4_version==1)
|
---|
1245 | s->use_skip_mb_code = 1;
|
---|
1246 | else
|
---|
1247 | s->use_skip_mb_code = get_bits1(&s->gb);
|
---|
1248 | s->rl_table_index = 2;
|
---|
1249 | s->rl_chroma_table_index = s->rl_table_index;
|
---|
1250 | s->dc_table_index = 0; //not used
|
---|
1251 | s->mv_table_index = 0;
|
---|
1252 | break;
|
---|
1253 | case 3:
|
---|
1254 | s->use_skip_mb_code = get_bits1(&s->gb);
|
---|
1255 | s->rl_table_index = decode012(&s->gb);
|
---|
1256 | s->rl_chroma_table_index = s->rl_table_index;
|
---|
1257 |
|
---|
1258 | s->dc_table_index = get_bits1(&s->gb);
|
---|
1259 |
|
---|
1260 | s->mv_table_index = get_bits1(&s->gb);
|
---|
1261 | break;
|
---|
1262 | case 4:
|
---|
1263 | s->use_skip_mb_code = get_bits1(&s->gb);
|
---|
1264 |
|
---|
1265 | if(s->bit_rate > MBAC_BITRATE) s->per_mb_rl_table= get_bits1(&s->gb);
|
---|
1266 | else s->per_mb_rl_table= 0;
|
---|
1267 |
|
---|
1268 | if(!s->per_mb_rl_table){
|
---|
1269 | s->rl_table_index = decode012(&s->gb);
|
---|
1270 | s->rl_chroma_table_index = s->rl_table_index;
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | s->dc_table_index = get_bits1(&s->gb);
|
---|
1274 |
|
---|
1275 | s->mv_table_index = get_bits1(&s->gb);
|
---|
1276 | s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE);
|
---|
1277 | break;
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | if(s->avctx->debug&FF_DEBUG_PICT_INFO)
|
---|
1281 | av_log(s->avctx, AV_LOG_DEBUG, "skip:%d rl:%d rlc:%d dc:%d mv:%d mbrl:%d qp:%d \n",
|
---|
1282 | s->use_skip_mb_code,
|
---|
1283 | s->rl_table_index,
|
---|
1284 | s->rl_chroma_table_index,
|
---|
1285 | s->dc_table_index,
|
---|
1286 | s->mv_table_index,
|
---|
1287 | s->per_mb_rl_table,
|
---|
1288 | s->qscale);
|
---|
1289 |
|
---|
1290 | if(s->flipflop_rounding){
|
---|
1291 | s->no_rounding ^= 1;
|
---|
1292 | }else{
|
---|
1293 | s->no_rounding = 0;
|
---|
1294 | }
|
---|
1295 | }
|
---|
1296 | //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
|
---|
1297 |
|
---|
1298 | s->esc3_level_length= 0;
|
---|
1299 | s->esc3_run_length= 0;
|
---|
1300 |
|
---|
1301 | #ifdef DEBUG
|
---|
1302 | av_log(s->avctx, AV_LOG_DEBUG, "*****frame %d:\n", frame_count++);
|
---|
1303 | #endif
|
---|
1304 | return 0;
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | int msmpeg4_decode_ext_header(MpegEncContext * s, int buf_size)
|
---|
1308 | {
|
---|
1309 | int left= buf_size*8 - get_bits_count(&s->gb);
|
---|
1310 | int length= s->msmpeg4_version>=3 ? 17 : 16;
|
---|
1311 | /* the alt_bitstream reader could read over the end so we need to check it */
|
---|
1312 | if(left>=length && left<length+8)
|
---|
1313 | {
|
---|
1314 | int fps;
|
---|
1315 |
|
---|
1316 | fps= get_bits(&s->gb, 5);
|
---|
1317 | s->bit_rate= get_bits(&s->gb, 11)*1024;
|
---|
1318 | if(s->msmpeg4_version>=3)
|
---|
1319 | s->flipflop_rounding= get_bits1(&s->gb);
|
---|
1320 | else
|
---|
1321 | s->flipflop_rounding= 0;
|
---|
1322 |
|
---|
1323 | // printf("fps:%2d bps:%2d roundingType:%1d\n", fps, s->bit_rate/1024, s->flipflop_rounding);
|
---|
1324 | }
|
---|
1325 | else if(left<length+8)
|
---|
1326 | {
|
---|
1327 | s->flipflop_rounding= 0;
|
---|
1328 | if(s->msmpeg4_version != 2)
|
---|
1329 | av_log(s->avctx, AV_LOG_ERROR, "ext header missing, %d left\n", left);
|
---|
1330 | }
|
---|
1331 | else
|
---|
1332 | {
|
---|
1333 | av_log(s->avctx, AV_LOG_ERROR, "I frame too long, ignoring ext header\n");
|
---|
1334 | }
|
---|
1335 |
|
---|
1336 | return 0;
|
---|
1337 | }
|
---|
1338 |
|
---|
1339 | static inline void msmpeg4_memsetw(short *tab, int val, int n)
|
---|
1340 | {
|
---|
1341 | int i;
|
---|
1342 | for(i=0;i<n;i++)
|
---|
1343 | tab[i] = val;
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
|
---|
1347 | {
|
---|
1348 | int range, bit_size, sign, code, bits;
|
---|
1349 |
|
---|
1350 | if (val == 0) {
|
---|
1351 | /* zero vector */
|
---|
1352 | code = 0;
|
---|
1353 | put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
|
---|
1354 | } else {
|
---|
1355 | bit_size = s->f_code - 1;
|
---|
1356 | range = 1 << bit_size;
|
---|
1357 | if (val <= -64)
|
---|
1358 | val += 64;
|
---|
1359 | else if (val >= 64)
|
---|
1360 | val -= 64;
|
---|
1361 |
|
---|
1362 | if (val >= 0) {
|
---|
1363 | sign = 0;
|
---|
1364 | } else {
|
---|
1365 | val = -val;
|
---|
1366 | sign = 1;
|
---|
1367 | }
|
---|
1368 | val--;
|
---|
1369 | code = (val >> bit_size) + 1;
|
---|
1370 | bits = val & (range - 1);
|
---|
1371 |
|
---|
1372 | put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
|
---|
1373 | if (bit_size > 0) {
|
---|
1374 | put_bits(&s->pb, bit_size, bits);
|
---|
1375 | }
|
---|
1376 | }
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 | /* this is identical to h263 except that its range is multiplied by 2 */
|
---|
1380 | static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
|
---|
1381 | {
|
---|
1382 | int code, val, sign, shift;
|
---|
1383 |
|
---|
1384 | code = get_vlc2(&s->gb, v2_mv_vlc.table, V2_MV_VLC_BITS, 2);
|
---|
1385 | // printf("MV code %d at %d %d pred: %d\n", code, s->mb_x,s->mb_y, pred);
|
---|
1386 | if (code < 0)
|
---|
1387 | return 0xffff;
|
---|
1388 |
|
---|
1389 | if (code == 0)
|
---|
1390 | return pred;
|
---|
1391 | sign = get_bits1(&s->gb);
|
---|
1392 | shift = f_code - 1;
|
---|
1393 | val = code;
|
---|
1394 | if (shift) {
|
---|
1395 | val = (val - 1) << shift;
|
---|
1396 | val |= get_bits(&s->gb, shift);
|
---|
1397 | val++;
|
---|
1398 | }
|
---|
1399 | if (sign)
|
---|
1400 | val = -val;
|
---|
1401 |
|
---|
1402 | val += pred;
|
---|
1403 | if (val <= -64)
|
---|
1404 | val += 64;
|
---|
1405 | else if (val >= 64)
|
---|
1406 | val -= 64;
|
---|
1407 |
|
---|
1408 | return val;
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
|
---|
1412 | {
|
---|
1413 | int cbp, code, i;
|
---|
1414 |
|
---|
1415 | if (s->pict_type == P_TYPE) {
|
---|
1416 | if (s->use_skip_mb_code) {
|
---|
1417 | if (get_bits1(&s->gb)) {
|
---|
1418 | /* skip mb */
|
---|
1419 | s->mb_intra = 0;
|
---|
1420 | for(i=0;i<6;i++)
|
---|
1421 | s->block_last_index[i] = -1;
|
---|
1422 | s->mv_dir = MV_DIR_FORWARD;
|
---|
1423 | s->mv_type = MV_TYPE_16X16;
|
---|
1424 | s->mv[0][0][0] = 0;
|
---|
1425 | s->mv[0][0][1] = 0;
|
---|
1426 | s->mb_skipped = 1;
|
---|
1427 | return 0;
|
---|
1428 | }
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | if(s->msmpeg4_version==2)
|
---|
1432 | code = get_vlc2(&s->gb, v2_mb_type_vlc.table, V2_MB_TYPE_VLC_BITS, 1);
|
---|
1433 | else
|
---|
1434 | code = get_vlc2(&s->gb, v1_inter_cbpc_vlc.table, V1_INTER_CBPC_VLC_BITS, 3);
|
---|
1435 | if(code<0 || code>7){
|
---|
1436 | av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", code, s->mb_x, s->mb_y);
|
---|
1437 | return -1;
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | s->mb_intra = code >>2;
|
---|
1441 |
|
---|
1442 | cbp = code & 0x3;
|
---|
1443 | } else {
|
---|
1444 | s->mb_intra = 1;
|
---|
1445 | if(s->msmpeg4_version==2)
|
---|
1446 | cbp= get_vlc2(&s->gb, v2_intra_cbpc_vlc.table, V2_INTRA_CBPC_VLC_BITS, 1);
|
---|
1447 | else
|
---|
1448 | cbp= get_vlc2(&s->gb, v1_intra_cbpc_vlc.table, V1_INTRA_CBPC_VLC_BITS, 1);
|
---|
1449 | if(cbp<0 || cbp>3){
|
---|
1450 | av_log(s->avctx, AV_LOG_ERROR, "cbpc %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
|
---|
1451 | return -1;
|
---|
1452 | }
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | if (!s->mb_intra) {
|
---|
1456 | int mx, my, cbpy;
|
---|
1457 |
|
---|
1458 | cbpy= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
|
---|
1459 | if(cbpy<0){
|
---|
1460 | av_log(s->avctx, AV_LOG_ERROR, "cbpy %d invalid at %d %d\n", cbp, s->mb_x, s->mb_y);
|
---|
1461 | return -1;
|
---|
1462 | }
|
---|
1463 |
|
---|
1464 | cbp|= cbpy<<2;
|
---|
1465 | if(s->msmpeg4_version==1 || (cbp&3) != 3) cbp^= 0x3C;
|
---|
1466 |
|
---|
1467 | h263_pred_motion(s, 0, 0, &mx, &my);
|
---|
1468 | mx= msmpeg4v2_decode_motion(s, mx, 1);
|
---|
1469 | my= msmpeg4v2_decode_motion(s, my, 1);
|
---|
1470 |
|
---|
1471 | s->mv_dir = MV_DIR_FORWARD;
|
---|
1472 | s->mv_type = MV_TYPE_16X16;
|
---|
1473 | s->mv[0][0][0] = mx;
|
---|
1474 | s->mv[0][0][1] = my;
|
---|
1475 | } else {
|
---|
1476 | if(s->msmpeg4_version==2){
|
---|
1477 | s->ac_pred = get_bits1(&s->gb);
|
---|
1478 | cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
|
---|
1479 | } else{
|
---|
1480 | s->ac_pred = 0;
|
---|
1481 | cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
|
---|
1482 | if(s->pict_type==P_TYPE) cbp^=0x3C;
|
---|
1483 | }
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | s->dsp.clear_blocks(s->block[0]);
|
---|
1487 | for (i = 0; i < 6; i++) {
|
---|
1488 | if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
|
---|
1489 | {
|
---|
1490 | av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
|
---|
1491 | return -1;
|
---|
1492 | }
|
---|
1493 | }
|
---|
1494 | return 0;
|
---|
1495 | }
|
---|
1496 |
|
---|
1497 | static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
|
---|
1498 | {
|
---|
1499 | int cbp, code, i;
|
---|
1500 | uint8_t *coded_val;
|
---|
1501 | uint32_t * const mb_type_ptr= &s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ];
|
---|
1502 |
|
---|
1503 | if (s->pict_type == P_TYPE) {
|
---|
1504 | if (s->use_skip_mb_code) {
|
---|
1505 | if (get_bits1(&s->gb)) {
|
---|
1506 | /* skip mb */
|
---|
1507 | s->mb_intra = 0;
|
---|
1508 | for(i=0;i<6;i++)
|
---|
1509 | s->block_last_index[i] = -1;
|
---|
1510 | s->mv_dir = MV_DIR_FORWARD;
|
---|
1511 | s->mv_type = MV_TYPE_16X16;
|
---|
1512 | s->mv[0][0][0] = 0;
|
---|
1513 | s->mv[0][0][1] = 0;
|
---|
1514 | s->mb_skipped = 1;
|
---|
1515 | *mb_type_ptr = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
|
---|
1516 |
|
---|
1517 | return 0;
|
---|
1518 | }
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
|
---|
1522 | if (code < 0)
|
---|
1523 | return -1;
|
---|
1524 | //s->mb_intra = (code & 0x40) ? 0 : 1;
|
---|
1525 | s->mb_intra = (~code & 0x40) >> 6;
|
---|
1526 |
|
---|
1527 | cbp = code & 0x3f;
|
---|
1528 | } else {
|
---|
1529 | s->mb_intra = 1;
|
---|
1530 | code = get_vlc2(&s->gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
|
---|
1531 | if (code < 0)
|
---|
1532 | return -1;
|
---|
1533 | /* predict coded block pattern */
|
---|
1534 | cbp = 0;
|
---|
1535 | for(i=0;i<6;i++) {
|
---|
1536 | int val = ((code >> (5 - i)) & 1);
|
---|
1537 | if (i < 4) {
|
---|
1538 | int pred = coded_block_pred(s, i, &coded_val);
|
---|
1539 | val = val ^ pred;
|
---|
1540 | *coded_val = val;
|
---|
1541 | }
|
---|
1542 | cbp |= val << (5 - i);
|
---|
1543 | }
|
---|
1544 | }
|
---|
1545 |
|
---|
1546 | if (!s->mb_intra) {
|
---|
1547 | int mx, my;
|
---|
1548 | //printf("P at %d %d\n", s->mb_x, s->mb_y);
|
---|
1549 | if(s->per_mb_rl_table && cbp){
|
---|
1550 | s->rl_table_index = decode012(&s->gb);
|
---|
1551 | s->rl_chroma_table_index = s->rl_table_index;
|
---|
1552 | }
|
---|
1553 | h263_pred_motion(s, 0, 0, &mx, &my);
|
---|
1554 | if (msmpeg4_decode_motion(s, &mx, &my) < 0)
|
---|
1555 | return -1;
|
---|
1556 | s->mv_dir = MV_DIR_FORWARD;
|
---|
1557 | s->mv_type = MV_TYPE_16X16;
|
---|
1558 | s->mv[0][0][0] = mx;
|
---|
1559 | s->mv[0][0][1] = my;
|
---|
1560 | *mb_type_ptr = MB_TYPE_L0 | MB_TYPE_16x16;
|
---|
1561 | } else {
|
---|
1562 | //printf("I at %d %d %d %06X\n", s->mb_x, s->mb_y, ((cbp&3)? 1 : 0) +((cbp&0x3C)? 2 : 0), show_bits(&s->gb, 24));
|
---|
1563 | s->ac_pred = get_bits1(&s->gb);
|
---|
1564 | *mb_type_ptr = MB_TYPE_INTRA;
|
---|
1565 | if(s->inter_intra_pred){
|
---|
1566 | s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
|
---|
1567 | // printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
|
---|
1568 | }
|
---|
1569 | if(s->per_mb_rl_table && cbp){
|
---|
1570 | s->rl_table_index = decode012(&s->gb);
|
---|
1571 | s->rl_chroma_table_index = s->rl_table_index;
|
---|
1572 | }
|
---|
1573 | }
|
---|
1574 |
|
---|
1575 | s->dsp.clear_blocks(s->block[0]);
|
---|
1576 | for (i = 0; i < 6; i++) {
|
---|
1577 | if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
|
---|
1578 | {
|
---|
1579 | av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
|
---|
1580 | return -1;
|
---|
1581 | }
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | return 0;
|
---|
1585 | }
|
---|
1586 | //#define ERROR_DETAILS
|
---|
1587 | static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
|
---|
1588 | int n, int coded, const uint8_t *scan_table)
|
---|
1589 | {
|
---|
1590 | int level, i, last, run, run_diff;
|
---|
1591 | int dc_pred_dir;
|
---|
1592 | RLTable *rl;
|
---|
1593 | RL_VLC_ELEM *rl_vlc;
|
---|
1594 | int qmul, qadd;
|
---|
1595 |
|
---|
1596 | if (s->mb_intra) {
|
---|
1597 | qmul=1;
|
---|
1598 | qadd=0;
|
---|
1599 |
|
---|
1600 | /* DC coef */
|
---|
1601 | level = msmpeg4_decode_dc(s, n, &dc_pred_dir);
|
---|
1602 |
|
---|
1603 | if (level < 0){
|
---|
1604 | av_log(s->avctx, AV_LOG_ERROR, "dc overflow- block: %d qscale: %d//\n", n, s->qscale);
|
---|
1605 | if(s->inter_intra_pred) level=0;
|
---|
1606 | else return -1;
|
---|
1607 | }
|
---|
1608 | if (n < 4) {
|
---|
1609 | rl = &rl_table[s->rl_table_index];
|
---|
1610 | if(level > 256*s->y_dc_scale){
|
---|
1611 | av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ L qscale: %d//\n", s->qscale);
|
---|
1612 | if(!s->inter_intra_pred) return -1;
|
---|
1613 | }
|
---|
1614 | } else {
|
---|
1615 | rl = &rl_table[3 + s->rl_chroma_table_index];
|
---|
1616 | if(level > 256*s->c_dc_scale){
|
---|
1617 | av_log(s->avctx, AV_LOG_ERROR, "dc overflow+ C qscale: %d//\n", s->qscale);
|
---|
1618 | if(!s->inter_intra_pred) return -1;
|
---|
1619 | }
|
---|
1620 | }
|
---|
1621 | block[0] = level;
|
---|
1622 |
|
---|
1623 | run_diff = 0;
|
---|
1624 | i = 0;
|
---|
1625 | if (!coded) {
|
---|
1626 | goto not_coded;
|
---|
1627 | }
|
---|
1628 | if (s->ac_pred) {
|
---|
1629 | if (dc_pred_dir == 0)
|
---|
1630 | scan_table = s->intra_v_scantable.permutated; /* left */
|
---|
1631 | else
|
---|
1632 | scan_table = s->intra_h_scantable.permutated; /* top */
|
---|
1633 | } else {
|
---|
1634 | scan_table = s->intra_scantable.permutated;
|
---|
1635 | }
|
---|
1636 | rl_vlc= rl->rl_vlc[0];
|
---|
1637 | } else {
|
---|
1638 | qmul = s->qscale << 1;
|
---|
1639 | qadd = (s->qscale - 1) | 1;
|
---|
1640 | i = -1;
|
---|
1641 | rl = &rl_table[3 + s->rl_table_index];
|
---|
1642 |
|
---|
1643 | if(s->msmpeg4_version==2)
|
---|
1644 | run_diff = 0;
|
---|
1645 | else
|
---|
1646 | run_diff = 1;
|
---|
1647 |
|
---|
1648 | if (!coded) {
|
---|
1649 | s->block_last_index[n] = i;
|
---|
1650 | return 0;
|
---|
1651 | }
|
---|
1652 | if(!scan_table)
|
---|
1653 | scan_table = s->inter_scantable.permutated;
|
---|
1654 | rl_vlc= rl->rl_vlc[s->qscale];
|
---|
1655 | }
|
---|
1656 | {
|
---|
1657 | OPEN_READER(re, &s->gb);
|
---|
1658 | for(;;) {
|
---|
1659 | UPDATE_CACHE(re, &s->gb);
|
---|
1660 | GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
|
---|
1661 | if (level==0) {
|
---|
1662 | int cache;
|
---|
1663 | cache= GET_CACHE(re, &s->gb);
|
---|
1664 | /* escape */
|
---|
1665 | if (s->msmpeg4_version==1 || (cache&0x80000000)==0) {
|
---|
1666 | if (s->msmpeg4_version==1 || (cache&0x40000000)==0) {
|
---|
1667 | /* third escape */
|
---|
1668 | if(s->msmpeg4_version!=1) LAST_SKIP_BITS(re, &s->gb, 2);
|
---|
1669 | UPDATE_CACHE(re, &s->gb);
|
---|
1670 | if(s->msmpeg4_version<=3){
|
---|
1671 | last= SHOW_UBITS(re, &s->gb, 1); SKIP_CACHE(re, &s->gb, 1);
|
---|
1672 | run= SHOW_UBITS(re, &s->gb, 6); SKIP_CACHE(re, &s->gb, 6);
|
---|
1673 | level= SHOW_SBITS(re, &s->gb, 8); LAST_SKIP_CACHE(re, &s->gb, 8);
|
---|
1674 | SKIP_COUNTER(re, &s->gb, 1+6+8);
|
---|
1675 | }else{
|
---|
1676 | int sign;
|
---|
1677 | last= SHOW_UBITS(re, &s->gb, 1); SKIP_BITS(re, &s->gb, 1);
|
---|
1678 | if(!s->esc3_level_length){
|
---|
1679 | int ll;
|
---|
1680 | //printf("ESC-3 %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
|
---|
1681 | if(s->qscale<8){
|
---|
1682 | ll= SHOW_UBITS(re, &s->gb, 3); SKIP_BITS(re, &s->gb, 3);
|
---|
1683 | if(ll==0){
|
---|
1684 | if(SHOW_UBITS(re, &s->gb, 1)) av_log(s->avctx, AV_LOG_ERROR, "cool a new vlc code ,contact the ffmpeg developers and upload the file\n");
|
---|
1685 | SKIP_BITS(re, &s->gb, 1);
|
---|
1686 | ll=8;
|
---|
1687 | }
|
---|
1688 | }else{
|
---|
1689 | ll=2;
|
---|
1690 | while(ll<8 && SHOW_UBITS(re, &s->gb, 1)==0){
|
---|
1691 | ll++;
|
---|
1692 | SKIP_BITS(re, &s->gb, 1);
|
---|
1693 | }
|
---|
1694 | if(ll<8) SKIP_BITS(re, &s->gb, 1);
|
---|
1695 | }
|
---|
1696 |
|
---|
1697 | s->esc3_level_length= ll;
|
---|
1698 | s->esc3_run_length= SHOW_UBITS(re, &s->gb, 2) + 3; SKIP_BITS(re, &s->gb, 2);
|
---|
1699 | //printf("level length:%d, run length: %d\n", ll, s->esc3_run_length);
|
---|
1700 | UPDATE_CACHE(re, &s->gb);
|
---|
1701 | }
|
---|
1702 | run= SHOW_UBITS(re, &s->gb, s->esc3_run_length);
|
---|
1703 | SKIP_BITS(re, &s->gb, s->esc3_run_length);
|
---|
1704 |
|
---|
1705 | sign= SHOW_UBITS(re, &s->gb, 1);
|
---|
1706 | SKIP_BITS(re, &s->gb, 1);
|
---|
1707 |
|
---|
1708 | level= SHOW_UBITS(re, &s->gb, s->esc3_level_length);
|
---|
1709 | SKIP_BITS(re, &s->gb, s->esc3_level_length);
|
---|
1710 | if(sign) level= -level;
|
---|
1711 | }
|
---|
1712 | //printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
|
---|
1713 | #if 0 // waste of time / this will detect very few errors
|
---|
1714 | {
|
---|
1715 | const int abs_level= ABS(level);
|
---|
1716 | const int run1= run - rl->max_run[last][abs_level] - run_diff;
|
---|
1717 | if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
|
---|
1718 | if(abs_level <= rl->max_level[last][run]){
|
---|
1719 | av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
|
---|
1720 | return DECODING_AC_LOST;
|
---|
1721 | }
|
---|
1722 | if(abs_level <= rl->max_level[last][run]*2){
|
---|
1723 | av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
|
---|
1724 | return DECODING_AC_LOST;
|
---|
1725 | }
|
---|
1726 | if(run1>=0 && abs_level <= rl->max_level[last][run1]){
|
---|
1727 | av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
|
---|
1728 | return DECODING_AC_LOST;
|
---|
1729 | }
|
---|
1730 | }
|
---|
1731 | }
|
---|
1732 | #endif
|
---|
1733 | //level = level * qmul + (level>0) * qadd - (level<=0) * qadd ;
|
---|
1734 | if (level>0) level= level * qmul + qadd;
|
---|
1735 | else level= level * qmul - qadd;
|
---|
1736 | #if 0 // waste of time too :(
|
---|
1737 | if(level>2048 || level<-2048){
|
---|
1738 | av_log(s->avctx, AV_LOG_ERROR, "|level| overflow in 3. esc\n");
|
---|
1739 | return DECODING_AC_LOST;
|
---|
1740 | }
|
---|
1741 | #endif
|
---|
1742 | i+= run + 1;
|
---|
1743 | if(last) i+=192;
|
---|
1744 | #ifdef ERROR_DETAILS
|
---|
1745 | if(run==66)
|
---|
1746 | av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC3 level=%d\n", level);
|
---|
1747 | else if((i>62 && i<192) || i>192+63)
|
---|
1748 | av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC3 i=%d run=%d level=%d\n", i, run, level);
|
---|
1749 | #endif
|
---|
1750 | } else {
|
---|
1751 | /* second escape */
|
---|
1752 | #if MIN_CACHE_BITS < 23
|
---|
1753 | LAST_SKIP_BITS(re, &s->gb, 2);
|
---|
1754 | UPDATE_CACHE(re, &s->gb);
|
---|
1755 | #else
|
---|
1756 | SKIP_BITS(re, &s->gb, 2);
|
---|
1757 | #endif
|
---|
1758 | GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
|
---|
1759 | i+= run + rl->max_run[run>>7][level/qmul] + run_diff; //FIXME opt indexing
|
---|
1760 | level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
|
---|
1761 | LAST_SKIP_BITS(re, &s->gb, 1);
|
---|
1762 | #ifdef ERROR_DETAILS
|
---|
1763 | if(run==66)
|
---|
1764 | av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC2 level=%d\n", level);
|
---|
1765 | else if((i>62 && i<192) || i>192+63)
|
---|
1766 | av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC2 i=%d run=%d level=%d\n", i, run, level);
|
---|
1767 | #endif
|
---|
1768 | }
|
---|
1769 | } else {
|
---|
1770 | /* first escape */
|
---|
1771 | #if MIN_CACHE_BITS < 22
|
---|
1772 | LAST_SKIP_BITS(re, &s->gb, 1);
|
---|
1773 | UPDATE_CACHE(re, &s->gb);
|
---|
1774 | #else
|
---|
1775 | SKIP_BITS(re, &s->gb, 1);
|
---|
1776 | #endif
|
---|
1777 | GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
|
---|
1778 | i+= run;
|
---|
1779 | level = level + rl->max_level[run>>7][(run-1)&63] * qmul;//FIXME opt indexing
|
---|
1780 | level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
|
---|
1781 | LAST_SKIP_BITS(re, &s->gb, 1);
|
---|
1782 | #ifdef ERROR_DETAILS
|
---|
1783 | if(run==66)
|
---|
1784 | av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code in ESC1 level=%d\n", level);
|
---|
1785 | else if((i>62 && i<192) || i>192+63)
|
---|
1786 | av_log(s->avctx, AV_LOG_ERROR, "run overflow in ESC1 i=%d run=%d level=%d\n", i, run, level);
|
---|
1787 | #endif
|
---|
1788 | }
|
---|
1789 | } else {
|
---|
1790 | i+= run;
|
---|
1791 | level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
|
---|
1792 | LAST_SKIP_BITS(re, &s->gb, 1);
|
---|
1793 | #ifdef ERROR_DETAILS
|
---|
1794 | if(run==66)
|
---|
1795 | av_log(s->avctx, AV_LOG_ERROR, "illegal vlc code level=%d\n", level);
|
---|
1796 | else if((i>62 && i<192) || i>192+63)
|
---|
1797 | av_log(s->avctx, AV_LOG_ERROR, "run overflow i=%d run=%d level=%d\n", i, run, level);
|
---|
1798 | #endif
|
---|
1799 | }
|
---|
1800 | if (i > 62){
|
---|
1801 | i-= 192;
|
---|
1802 | if(i&(~63)){
|
---|
1803 | const int left= s->gb.size_in_bits - get_bits_count(&s->gb);
|
---|
1804 | if(((i+192 == 64 && level/qmul==-1) || s->error_resilience<=1) && left>=0){
|
---|
1805 | av_log(s->avctx, AV_LOG_ERROR, "ignoring overflow at %d %d\n", s->mb_x, s->mb_y);
|
---|
1806 | break;
|
---|
1807 | }else{
|
---|
1808 | av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
|
---|
1809 | return -1;
|
---|
1810 | }
|
---|
1811 | }
|
---|
1812 |
|
---|
1813 | block[scan_table[i]] = level;
|
---|
1814 | break;
|
---|
1815 | }
|
---|
1816 |
|
---|
1817 | block[scan_table[i]] = level;
|
---|
1818 | }
|
---|
1819 | CLOSE_READER(re, &s->gb);
|
---|
1820 | }
|
---|
1821 | not_coded:
|
---|
1822 | if (s->mb_intra) {
|
---|
1823 | mpeg4_pred_ac(s, block, n, dc_pred_dir);
|
---|
1824 | if (s->ac_pred) {
|
---|
1825 | i = 63; /* XXX: not optimal */
|
---|
1826 | }
|
---|
1827 | }
|
---|
1828 | if(s->msmpeg4_version>=4 && i>0) i=63; //FIXME/XXX optimize
|
---|
1829 | s->block_last_index[n] = i;
|
---|
1830 |
|
---|
1831 | return 0;
|
---|
1832 | }
|
---|
1833 |
|
---|
1834 | static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
|
---|
1835 | {
|
---|
1836 | int level, pred;
|
---|
1837 |
|
---|
1838 | if(s->msmpeg4_version<=2){
|
---|
1839 | if (n < 4) {
|
---|
1840 | level = get_vlc2(&s->gb, v2_dc_lum_vlc.table, DC_VLC_BITS, 3);
|
---|
1841 | } else {
|
---|
1842 | level = get_vlc2(&s->gb, v2_dc_chroma_vlc.table, DC_VLC_BITS, 3);
|
---|
1843 | }
|
---|
1844 | if (level < 0)
|
---|
1845 | return -1;
|
---|
1846 | level-=256;
|
---|
1847 | }else{ //FIXME optimize use unified tables & index
|
---|
1848 | if (n < 4) {
|
---|
1849 | level = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
|
---|
1850 | } else {
|
---|
1851 | level = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
|
---|
1852 | }
|
---|
1853 | if (level < 0){
|
---|
1854 | av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
|
---|
1855 | return -1;
|
---|
1856 | }
|
---|
1857 |
|
---|
1858 | if (level == DC_MAX) {
|
---|
1859 | level = get_bits(&s->gb, 8);
|
---|
1860 | if (get_bits1(&s->gb))
|
---|
1861 | level = -level;
|
---|
1862 | } else if (level != 0) {
|
---|
1863 | if (get_bits1(&s->gb))
|
---|
1864 | level = -level;
|
---|
1865 | }
|
---|
1866 | }
|
---|
1867 |
|
---|
1868 | if(s->msmpeg4_version==1){
|
---|
1869 | int32_t *dc_val;
|
---|
1870 | pred = msmpeg4v1_pred_dc(s, n, &dc_val);
|
---|
1871 | level += pred;
|
---|
1872 |
|
---|
1873 | /* update predictor */
|
---|
1874 | *dc_val= level;
|
---|
1875 | }else{
|
---|
1876 | uint16_t *dc_val;
|
---|
1877 | pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
|
---|
1878 | level += pred;
|
---|
1879 |
|
---|
1880 | /* update predictor */
|
---|
1881 | if (n < 4) {
|
---|
1882 | *dc_val = level * s->y_dc_scale;
|
---|
1883 | } else {
|
---|
1884 | *dc_val = level * s->c_dc_scale;
|
---|
1885 | }
|
---|
1886 | }
|
---|
1887 |
|
---|
1888 | return level;
|
---|
1889 | }
|
---|
1890 |
|
---|
1891 | static int msmpeg4_decode_motion(MpegEncContext * s,
|
---|
1892 | int *mx_ptr, int *my_ptr)
|
---|
1893 | {
|
---|
1894 | MVTable *mv;
|
---|
1895 | int code, mx, my;
|
---|
1896 |
|
---|
1897 | mv = &mv_tables[s->mv_table_index];
|
---|
1898 |
|
---|
1899 | code = get_vlc2(&s->gb, mv->vlc.table, MV_VLC_BITS, 2);
|
---|
1900 | if (code < 0){
|
---|
1901 | av_log(s->avctx, AV_LOG_ERROR, "illegal MV code at %d %d\n", s->mb_x, s->mb_y);
|
---|
1902 | return -1;
|
---|
1903 | }
|
---|
1904 | if (code == mv->n) {
|
---|
1905 | //printf("MV ESC %X at %d %d\n", show_bits(&s->gb, 24), s->mb_x, s->mb_y);
|
---|
1906 | mx = get_bits(&s->gb, 6);
|
---|
1907 | my = get_bits(&s->gb, 6);
|
---|
1908 | } else {
|
---|
1909 | mx = mv->table_mvx[code];
|
---|
1910 | my = mv->table_mvy[code];
|
---|
1911 | }
|
---|
1912 |
|
---|
1913 | mx += *mx_ptr - 32;
|
---|
1914 | my += *my_ptr - 32;
|
---|
1915 | /* WARNING : they do not do exactly modulo encoding */
|
---|
1916 | if (mx <= -64)
|
---|
1917 | mx += 64;
|
---|
1918 | else if (mx >= 64)
|
---|
1919 | mx -= 64;
|
---|
1920 |
|
---|
1921 | if (my <= -64)
|
---|
1922 | my += 64;
|
---|
1923 | else if (my >= 64)
|
---|
1924 | my -= 64;
|
---|
1925 | *mx_ptr = mx;
|
---|
1926 | *my_ptr = my;
|
---|
1927 | return 0;
|
---|
1928 | }
|
---|
1929 |
|
---|
1930 | /* cleanest way to support it
|
---|
1931 | * there is too much shared between versions so that we cant have 1 file per version & 1 common
|
---|
1932 | * as allmost everything would be in the common file
|
---|
1933 | */
|
---|
1934 | #include "wmv2.c"
|
---|