Browse code

Split H263 encoder and decoder from common code.

Originally committed as revision 21109 to svn://svn.ffmpeg.org/ffmpeg/trunk

Michael Niedermayer authored on 2010/01/09 23:59:06
Showing 15 changed files
... ...
@@ -124,11 +124,11 @@ OBJS-$(CONFIG_H261_ENCODER)            += h261enc.o h261.o             \
124 124
                                           mpegvideo_enc.o motion_est.o \
125 125
                                           ratecontrol.o mpeg12data.o   \
126 126
                                           mpegvideo.o
127
-OBJS-$(CONFIG_H263_DECODER)            += h263dec.o h263.o mpeg4video.o mpeg4videodec.o flvdec.o intelh263dec.o \
127
+OBJS-$(CONFIG_H263_DECODER)            += h263dec.o h263.o ituh263dec.o mpeg4video.o mpeg4videodec.o flvdec.o intelh263dec.o \
128 128
                                           mpegvideo.o error_resilience.o
129 129
 OBJS-$(CONFIG_H263_VAAPI_HWACCEL)      += vaapi_mpeg4.o
130 130
 OBJS-$(CONFIG_H263_ENCODER)            += mpegvideo_enc.o mpeg4video.o mpeg4videoenc.o motion_est.o      \
131
-                                          ratecontrol.o h263.o flvenc.o mpeg12data.o \
131
+                                          ratecontrol.o h263.o ituh263enc.o flvenc.o mpeg12data.o \
132 132
                                           mpegvideo.o error_resilience.o
133 133
 OBJS-$(CONFIG_H264_DECODER)            += h264.o h264idct.o h264pred.o cabac.o \
134 134
                                           mpegvideo.o error_resilience.o
... ...
@@ -18,6 +18,7 @@
18 18
  */
19 19
 
20 20
 #include "mpegvideo.h"
21
+#include "h263.h"
21 22
 #include "flv.h"
22 23
 
23 24
 void ff_flv2_decode_ac_esc(GetBitContext *gb, int *level, int *run, int *last){
... ...
@@ -18,6 +18,7 @@
18 18
  */
19 19
 
20 20
 #include "mpegvideo.h"
21
+#include "h263.h"
21 22
 #include "flv.h"
22 23
 
23 24
 void ff_flv_encode_picture_header(MpegEncContext * s, int picture_number)
... ...
@@ -5,10 +5,6 @@
5 5
  * Copyright (c) 2001 Juan J. Sierralta P
6 6
  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
7 7
  *
8
- * ac prediction encoding, B-frame support, error resilience, optimizations,
9
- * qpel decoding, gmc decoding, interlaced decoding
10
- * by Michael Niedermayer <michaelni@gmx.at>
11
- *
12 8
  * This file is part of FFmpeg.
13 9
  *
14 10
  * FFmpeg is free software; you can redistribute it and/or
... ...
@@ -47,299 +43,8 @@
47 47
 //#undef NDEBUG
48 48
 //#include <assert.h>
49 49
 
50
-// The defines below define the number of bits that are read at once for
51
-// reading vlc values. Changing these may improve speed and data cache needs
52
-// be aware though that decreasing them may need the number of stages that is
53
-// passed to get_vlc* to be increased.
54
-#define MV_VLC_BITS 9
55
-#define H263_MBTYPE_B_VLC_BITS 6
56
-#define CBPC_B_VLC_BITS 3
57
-
58
-
59
-#if CONFIG_ENCODERS
60
-/**
61
- * Table of number of bits a motion vector component needs.
62
- */
63
-static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
64
-
65
-/**
66
- * Minimal fcode that a motion vector component would need.
67
- */
68
-static uint8_t fcode_tab[MAX_MV*2+1];
69
-
70
-/**
71
- * Minimal fcode that a motion vector component would need in umv.
72
- * All entries in this table are 1.
73
- */
74
-static uint8_t umv_fcode_tab[MAX_MV*2+1];
75
-
76
-//unified encoding tables for run length encoding of coefficients
77
-//unified in the sense that the specification specifies the encoding in several steps.
78
-static uint8_t  uni_h263_intra_aic_rl_len [64*64*2*2];
79
-static uint8_t  uni_h263_inter_rl_len [64*64*2*2];
80
-//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
81
-//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
82
-#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
83
-
84
-#endif
85
-
86
-static uint8_t static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
87
-
88
-
89
-int h263_get_picture_format(int width, int height)
90
-{
91
-    if (width == 128 && height == 96)
92
-        return 1;
93
-    else if (width == 176 && height == 144)
94
-        return 2;
95
-    else if (width == 352 && height == 288)
96
-        return 3;
97
-    else if (width == 704 && height == 576)
98
-        return 4;
99
-    else if (width == 1408 && height == 1152)
100
-        return 5;
101
-    else
102
-        return 7;
103
-}
104
-
105
-void ff_h263_show_pict_info(MpegEncContext *s){
106
-    if(s->avctx->debug&FF_DEBUG_PICT_INFO){
107
-    av_log(s->avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s%s%s%s%s %d/%d\n",
108
-         s->qscale, av_get_pict_type_char(s->pict_type),
109
-         s->gb.size_in_bits, 1-s->no_rounding,
110
-         s->obmc ? " AP" : "",
111
-         s->umvplus ? " UMV" : "",
112
-         s->h263_long_vectors ? " LONG" : "",
113
-         s->h263_plus ? " +" : "",
114
-         s->h263_aic ? " AIC" : "",
115
-         s->alt_inter_vlc ? " AIV" : "",
116
-         s->modified_quant ? " MQ" : "",
117
-         s->loop_filter ? " LOOP" : "",
118
-         s->h263_slice_structured ? " SS" : "",
119
-         s->avctx->time_base.den, s->avctx->time_base.num
120
-    );
121
-    }
122
-}
123
-
124
-#if CONFIG_ENCODERS
125
-
126
-/**
127
- * Returns the 4 bit value that specifies the given aspect ratio.
128
- * This may be one of the standard aspect ratios or it specifies
129
- * that the aspect will be stored explicitly later.
130
- */
131
-av_const int ff_h263_aspect_to_info(AVRational aspect){
132
-    int i;
133
-
134
-    if(aspect.num==0) aspect= (AVRational){1,1};
135
-
136
-    for(i=1; i<6; i++){
137
-        if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){
138
-            return i;
139
-        }
140
-    }
141
-
142
-    return FF_ASPECT_EXTENDED;
143
-}
144
-
145
-void h263_encode_picture_header(MpegEncContext * s, int picture_number)
146
-{
147
-    int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
148
-    int best_clock_code=1;
149
-    int best_divisor=60;
150
-    int best_error= INT_MAX;
151
-
152
-    if(s->h263_plus){
153
-        for(i=0; i<2; i++){
154
-            int div, error;
155
-            div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den);
156
-            div= av_clip(div, 1, 127);
157
-            error= FFABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div);
158
-            if(error < best_error){
159
-                best_error= error;
160
-                best_divisor= div;
161
-                best_clock_code= i;
162
-            }
163
-        }
164
-    }
165
-    s->custom_pcf= best_clock_code!=1 || best_divisor!=60;
166
-    coded_frame_rate= 1800000;
167
-    coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
168
-
169
-    align_put_bits(&s->pb);
170
-
171
-    /* Update the pointer to last GOB */
172
-    s->ptr_lastgob = put_bits_ptr(&s->pb);
173
-    put_bits(&s->pb, 22, 0x20); /* PSC */
174
-    temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->time_base.num / //FIXME use timestamp
175
-                         (coded_frame_rate_base * (int64_t)s->avctx->time_base.den);
176
-    put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */
177
-
178
-    put_bits(&s->pb, 1, 1);     /* marker */
179
-    put_bits(&s->pb, 1, 0);     /* h263 id */
180
-    put_bits(&s->pb, 1, 0);     /* split screen off */
181
-    put_bits(&s->pb, 1, 0);     /* camera  off */
182
-    put_bits(&s->pb, 1, 0);     /* freeze picture release off */
183
-
184
-    format = h263_get_picture_format(s->width, s->height);
185
-    if (!s->h263_plus) {
186
-        /* H.263v1 */
187
-        put_bits(&s->pb, 3, format);
188
-        put_bits(&s->pb, 1, (s->pict_type == FF_P_TYPE));
189
-        /* By now UMV IS DISABLED ON H.263v1, since the restrictions
190
-        of H.263v1 UMV implies to check the predicted MV after
191
-        calculation of the current MB to see if we're on the limits */
192
-        put_bits(&s->pb, 1, 0);         /* Unrestricted Motion Vector: off */
193
-        put_bits(&s->pb, 1, 0);         /* SAC: off */
194
-        put_bits(&s->pb, 1, s->obmc);   /* Advanced Prediction */
195
-        put_bits(&s->pb, 1, 0);         /* only I/P frames, no PB frame */
196
-        put_bits(&s->pb, 5, s->qscale);
197
-        put_bits(&s->pb, 1, 0);         /* Continuous Presence Multipoint mode: off */
198
-    } else {
199
-        int ufep=1;
200
-        /* H.263v2 */
201
-        /* H.263 Plus PTYPE */
202
-
203
-        put_bits(&s->pb, 3, 7);
204
-        put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */
205
-        if (format == 7)
206
-            put_bits(&s->pb,3,6); /* Custom Source Format */
207
-        else
208
-            put_bits(&s->pb, 3, format);
209
-
210
-        put_bits(&s->pb,1, s->custom_pcf);
211
-        put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
212
-        put_bits(&s->pb,1,0); /* SAC: off */
213
-        put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
214
-        put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
215
-        put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
216
-        put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
217
-        put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
218
-        put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
219
-        put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
220
-        put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
221
-        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
222
-        put_bits(&s->pb,3,0); /* Reserved */
223
-
224
-        put_bits(&s->pb, 3, s->pict_type == FF_P_TYPE);
225
-
226
-        put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
227
-        put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
228
-        put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
229
-        put_bits(&s->pb,2,0); /* Reserved */
230
-        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
50
+uint8_t ff_h263_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
231 51
 
232
-        /* This should be here if PLUSPTYPE */
233
-        put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
234
-
235
-                if (format == 7) {
236
-            /* Custom Picture Format (CPFMT) */
237
-            s->aspect_ratio_info= ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
238
-
239
-            put_bits(&s->pb,4,s->aspect_ratio_info);
240
-            put_bits(&s->pb,9,(s->width >> 2) - 1);
241
-            put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
242
-            put_bits(&s->pb,9,(s->height >> 2));
243
-            if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
244
-                put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
245
-                put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
246
-            }
247
-        }
248
-        if(s->custom_pcf){
249
-            if(ufep){
250
-                put_bits(&s->pb, 1, best_clock_code);
251
-                put_bits(&s->pb, 7, best_divisor);
252
-            }
253
-            put_sbits(&s->pb, 2, temp_ref>>8);
254
-        }
255
-
256
-        /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
257
-        if (s->umvplus)
258
-//            put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
259
-//FIXME check actual requested range
260
-            put_bits(&s->pb,2,1); /* unlimited */
261
-        if(s->h263_slice_structured)
262
-            put_bits(&s->pb,2,0); /* no weird submodes */
263
-
264
-        put_bits(&s->pb, 5, s->qscale);
265
-    }
266
-
267
-    put_bits(&s->pb, 1, 0);     /* no PEI */
268
-
269
-    if(s->h263_slice_structured){
270
-        put_bits(&s->pb, 1, 1);
271
-
272
-        assert(s->mb_x == 0 && s->mb_y == 0);
273
-        ff_h263_encode_mba(s);
274
-
275
-        put_bits(&s->pb, 1, 1);
276
-    }
277
-
278
-    if(s->h263_aic){
279
-         s->y_dc_scale_table=
280
-         s->c_dc_scale_table= ff_aic_dc_scale_table;
281
-    }else{
282
-        s->y_dc_scale_table=
283
-        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
284
-    }
285
-}
286
-
287
-/**
288
- * Encodes a group of blocks header.
289
- */
290
-void h263_encode_gob_header(MpegEncContext * s, int mb_line)
291
-{
292
-    put_bits(&s->pb, 17, 1); /* GBSC */
293
-
294
-    if(s->h263_slice_structured){
295
-        put_bits(&s->pb, 1, 1);
296
-
297
-        ff_h263_encode_mba(s);
298
-
299
-        if(s->mb_num > 1583)
300
-            put_bits(&s->pb, 1, 1);
301
-        put_bits(&s->pb, 5, s->qscale); /* GQUANT */
302
-        put_bits(&s->pb, 1, 1);
303
-        put_bits(&s->pb, 2, s->pict_type == FF_I_TYPE); /* GFID */
304
-    }else{
305
-        int gob_number= mb_line / s->gob_index;
306
-
307
-        put_bits(&s->pb, 5, gob_number); /* GN */
308
-        put_bits(&s->pb, 2, s->pict_type == FF_I_TYPE); /* GFID */
309
-        put_bits(&s->pb, 5, s->qscale); /* GQUANT */
310
-    }
311
-}
312
-
313
-/**
314
- * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2)
315
- */
316
-void ff_clean_h263_qscales(MpegEncContext *s){
317
-    int i;
318
-    int8_t * const qscale_table= s->current_picture.qscale_table;
319
-
320
-    ff_init_qscale_tab(s);
321
-
322
-    for(i=1; i<s->mb_num; i++){
323
-        if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
324
-            qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
325
-    }
326
-    for(i=s->mb_num-2; i>=0; i--){
327
-        if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
328
-            qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
329
-    }
330
-
331
-    if(s->codec_id != CODEC_ID_H263P){
332
-        for(i=1; i<s->mb_num; i++){
333
-            int mb_xy= s->mb_index2xy[i];
334
-
335
-            if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){
336
-                s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
337
-            }
338
-        }
339
-    }
340
-}
341
-
342
-#endif //CONFIG_ENCODERS
343 52
 
344 53
 void ff_h263_update_motion_val(MpegEncContext * s){
345 54
     const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
... ...
@@ -393,164 +98,7 @@ void ff_h263_update_motion_val(MpegEncContext * s){
393 393
     }
394 394
 }
395 395
 
396
-#if CONFIG_ENCODERS
397
-
398
-static const int dquant_code[5]= {1,0,9,2,3};
399
-
400
-/**
401
- * encodes a 8x8 block.
402
- * @param block the 8x8 block
403
- * @param n block index (0-3 are luma, 4-5 are chroma)
404
- */
405
-static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
406
-{
407
-    int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
408
-    RLTable *rl;
409
-
410
-    rl = &ff_h263_rl_inter;
411
-    if (s->mb_intra && !s->h263_aic) {
412
-        /* DC coef */
413
-        level = block[0];
414
-        /* 255 cannot be represented, so we clamp */
415
-        if (level > 254) {
416
-            level = 254;
417
-            block[0] = 254;
418
-        }
419
-        /* 0 cannot be represented also */
420
-        else if (level < 1) {
421
-            level = 1;
422
-            block[0] = 1;
423
-        }
424
-        if (level == 128) //FIXME check rv10
425
-            put_bits(&s->pb, 8, 0xff);
426
-        else
427
-            put_bits(&s->pb, 8, level);
428
-        i = 1;
429
-    } else {
430
-        i = 0;
431
-        if (s->h263_aic && s->mb_intra)
432
-            rl = &rl_intra_aic;
433
-
434
-        if(s->alt_inter_vlc && !s->mb_intra){
435
-            int aic_vlc_bits=0;
436
-            int inter_vlc_bits=0;
437
-            int wrong_pos=-1;
438
-            int aic_code;
439
-
440
-            last_index = s->block_last_index[n];
441
-            last_non_zero = i - 1;
442
-            for (; i <= last_index; i++) {
443
-                j = s->intra_scantable.permutated[i];
444
-                level = block[j];
445
-                if (level) {
446
-                    run = i - last_non_zero - 1;
447
-                    last = (i == last_index);
448
-
449
-                    if(level<0) level= -level;
450
-
451
-                    code = get_rl_index(rl, last, run, level);
452
-                    aic_code = get_rl_index(&rl_intra_aic, last, run, level);
453
-                    inter_vlc_bits += rl->table_vlc[code][1]+1;
454
-                    aic_vlc_bits   += rl_intra_aic.table_vlc[aic_code][1]+1;
455
-
456
-                    if (code == rl->n) {
457
-                        inter_vlc_bits += 1+6+8-1;
458
-                    }
459
-                    if (aic_code == rl_intra_aic.n) {
460
-                        aic_vlc_bits += 1+6+8-1;
461
-                        wrong_pos += run + 1;
462
-                    }else
463
-                        wrong_pos += wrong_run[aic_code];
464
-                    last_non_zero = i;
465
-                }
466
-            }
467
-            i = 0;
468
-            if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
469
-                rl = &rl_intra_aic;
470
-        }
471
-    }
472
-
473
-    /* AC coefs */
474
-    last_index = s->block_last_index[n];
475
-    last_non_zero = i - 1;
476
-    for (; i <= last_index; i++) {
477
-        j = s->intra_scantable.permutated[i];
478
-        level = block[j];
479
-        if (level) {
480
-            run = i - last_non_zero - 1;
481
-            last = (i == last_index);
482
-            sign = 0;
483
-            slevel = level;
484
-            if (level < 0) {
485
-                sign = 1;
486
-                level = -level;
487
-            }
488
-            code = get_rl_index(rl, last, run, level);
489
-            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
490
-            if (code == rl->n) {
491
-              if(!CONFIG_FLV_ENCODER || s->h263_flv <= 1){
492
-                put_bits(&s->pb, 1, last);
493
-                put_bits(&s->pb, 6, run);
494
-
495
-                assert(slevel != 0);
496
-
497
-                if(level < 128)
498
-                    put_sbits(&s->pb, 8, slevel);
499
-                else{
500
-                    put_bits(&s->pb, 8, 128);
501
-                    put_sbits(&s->pb, 5, slevel);
502
-                    put_sbits(&s->pb, 6, slevel>>5);
503
-                }
504
-              }else{
505
-                    ff_flv2_encode_ac_esc(&s->pb, slevel, level, run, last);
506
-              }
507
-            } else {
508
-                put_bits(&s->pb, 1, sign);
509
-            }
510
-            last_non_zero = i;
511
-        }
512
-    }
513
-}
514
-
515
-/* Encode MV differences on H.263+ with Unrestricted MV mode */
516
-static void h263p_encode_umotion(MpegEncContext * s, int val)
517
-{
518
-    short sval = 0;
519
-    short i = 0;
520
-    short n_bits = 0;
521
-    short temp_val;
522
-    int code = 0;
523
-    int tcode;
524
-
525
-    if ( val == 0)
526
-        put_bits(&s->pb, 1, 1);
527
-    else if (val == 1)
528
-        put_bits(&s->pb, 3, 0);
529
-    else if (val == -1)
530
-        put_bits(&s->pb, 3, 2);
531
-    else {
532
-
533
-        sval = ((val < 0) ? (short)(-val):(short)val);
534
-        temp_val = sval;
535
-
536
-        while (temp_val != 0) {
537
-            temp_val = temp_val >> 1;
538
-            n_bits++;
539
-        }
540
-
541
-        i = n_bits - 1;
542
-        while (i > 0) {
543
-            tcode = (sval & (1 << (i-1))) >> (i-1);
544
-            tcode = (tcode << 1) | 1;
545
-            code = (code << 2) | tcode;
546
-            i--;
547
-        }
548
-        code = ((code << 1) | (val < 0)) << 1;
549
-        put_bits(&s->pb, (2*n_bits)+1, code);
550
-    }
551
-}
552
-
553
-static int h263_pred_dc(MpegEncContext * s, int n, int16_t **dc_val_ptr)
396
+int h263_pred_dc(MpegEncContext * s, int n, int16_t **dc_val_ptr)
554 397
 {
555 398
     int x, y, wrap, a, c, pred_dc;
556 399
     int16_t *dc_val;
... ...
@@ -591,208 +139,6 @@ static int h263_pred_dc(MpegEncContext * s, int n, int16_t **dc_val_ptr)
591 591
     return pred_dc;
592 592
 }
593 593
 
594
-void h263_encode_mb(MpegEncContext * s,
595
-                    DCTELEM block[6][64],
596
-                    int motion_x, int motion_y)
597
-{
598
-    int cbpc, cbpy, i, cbp, pred_x, pred_y;
599
-    int16_t pred_dc;
600
-    int16_t rec_intradc[6];
601
-    int16_t *dc_ptr[6];
602
-    const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1);
603
-
604
-    if (!s->mb_intra) {
605
-        /* compute cbp */
606
-        cbp= get_p_cbp(s, block, motion_x, motion_y);
607
-
608
-        if ((cbp | motion_x | motion_y | s->dquant | (s->mv_type - MV_TYPE_16X16)) == 0) {
609
-            /* skip macroblock */
610
-            put_bits(&s->pb, 1, 1);
611
-            if(interleaved_stats){
612
-                s->misc_bits++;
613
-                s->last_bits++;
614
-            }
615
-            s->skip_count++;
616
-
617
-            return;
618
-        }
619
-        put_bits(&s->pb, 1, 0);         /* mb coded */
620
-
621
-        cbpc = cbp & 3;
622
-        cbpy = cbp >> 2;
623
-        if(s->alt_inter_vlc==0 || cbpc!=3)
624
-            cbpy ^= 0xF;
625
-        if(s->dquant) cbpc+= 8;
626
-        if(s->mv_type==MV_TYPE_16X16){
627
-            put_bits(&s->pb,
628
-                    ff_h263_inter_MCBPC_bits[cbpc],
629
-                    ff_h263_inter_MCBPC_code[cbpc]);
630
-
631
-            put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
632
-            if(s->dquant)
633
-                put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
634
-
635
-            if(interleaved_stats){
636
-                s->misc_bits+= get_bits_diff(s);
637
-            }
638
-
639
-            /* motion vectors: 16x16 mode */
640
-            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
641
-
642
-            if (!s->umvplus) {
643
-                ff_h263_encode_motion_vector(s, motion_x - pred_x,
644
-                                                motion_y - pred_y, 1);
645
-            }
646
-            else {
647
-                h263p_encode_umotion(s, motion_x - pred_x);
648
-                h263p_encode_umotion(s, motion_y - pred_y);
649
-                if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
650
-                    /* To prevent Start Code emulation */
651
-                    put_bits(&s->pb,1,1);
652
-            }
653
-        }else{
654
-            put_bits(&s->pb,
655
-                    ff_h263_inter_MCBPC_bits[cbpc+16],
656
-                    ff_h263_inter_MCBPC_code[cbpc+16]);
657
-            put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
658
-            if(s->dquant)
659
-                put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
660
-
661
-            if(interleaved_stats){
662
-                s->misc_bits+= get_bits_diff(s);
663
-            }
664
-
665
-            for(i=0; i<4; i++){
666
-                /* motion vectors: 8x8 mode*/
667
-                h263_pred_motion(s, i, 0, &pred_x, &pred_y);
668
-
669
-                motion_x= s->current_picture.motion_val[0][ s->block_index[i] ][0];
670
-                motion_y= s->current_picture.motion_val[0][ s->block_index[i] ][1];
671
-                if (!s->umvplus) {
672
-                    ff_h263_encode_motion_vector(s, motion_x - pred_x,
673
-                                                    motion_y - pred_y, 1);
674
-                }
675
-                else {
676
-                    h263p_encode_umotion(s, motion_x - pred_x);
677
-                    h263p_encode_umotion(s, motion_y - pred_y);
678
-                    if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
679
-                        /* To prevent Start Code emulation */
680
-                        put_bits(&s->pb,1,1);
681
-                }
682
-            }
683
-        }
684
-
685
-        if(interleaved_stats){
686
-            s->mv_bits+= get_bits_diff(s);
687
-        }
688
-    } else {
689
-        assert(s->mb_intra);
690
-
691
-        cbp = 0;
692
-        if (s->h263_aic) {
693
-            /* Predict DC */
694
-            for(i=0; i<6; i++) {
695
-                int16_t level = block[i][0];
696
-                int scale;
697
-
698
-                if(i<4) scale= s->y_dc_scale;
699
-                else    scale= s->c_dc_scale;
700
-
701
-                pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
702
-                level -= pred_dc;
703
-                /* Quant */
704
-                if (level >= 0)
705
-                    level = (level + (scale>>1))/scale;
706
-                else
707
-                    level = (level - (scale>>1))/scale;
708
-
709
-                /* AIC can change CBP */
710
-                if (level == 0 && s->block_last_index[i] == 0)
711
-                    s->block_last_index[i] = -1;
712
-
713
-                if(!s->modified_quant){
714
-                    if (level < -127)
715
-                        level = -127;
716
-                    else if (level > 127)
717
-                        level = 127;
718
-                }
719
-
720
-                block[i][0] = level;
721
-                /* Reconstruction */
722
-                rec_intradc[i] = scale*level + pred_dc;
723
-                /* Oddify */
724
-                rec_intradc[i] |= 1;
725
-                //if ((rec_intradc[i] % 2) == 0)
726
-                //    rec_intradc[i]++;
727
-                /* Clipping */
728
-                if (rec_intradc[i] < 0)
729
-                    rec_intradc[i] = 0;
730
-                else if (rec_intradc[i] > 2047)
731
-                    rec_intradc[i] = 2047;
732
-
733
-                /* Update AC/DC tables */
734
-                *dc_ptr[i] = rec_intradc[i];
735
-                if (s->block_last_index[i] >= 0)
736
-                    cbp |= 1 << (5 - i);
737
-            }
738
-        }else{
739
-            for(i=0; i<6; i++) {
740
-                /* compute cbp */
741
-                if (s->block_last_index[i] >= 1)
742
-                    cbp |= 1 << (5 - i);
743
-            }
744
-        }
745
-
746
-        cbpc = cbp & 3;
747
-        if (s->pict_type == FF_I_TYPE) {
748
-            if(s->dquant) cbpc+=4;
749
-            put_bits(&s->pb,
750
-                ff_h263_intra_MCBPC_bits[cbpc],
751
-                ff_h263_intra_MCBPC_code[cbpc]);
752
-        } else {
753
-            if(s->dquant) cbpc+=8;
754
-            put_bits(&s->pb, 1, 0);     /* mb coded */
755
-            put_bits(&s->pb,
756
-                ff_h263_inter_MCBPC_bits[cbpc + 4],
757
-                ff_h263_inter_MCBPC_code[cbpc + 4]);
758
-        }
759
-        if (s->h263_aic) {
760
-            /* XXX: currently, we do not try to use ac prediction */
761
-            put_bits(&s->pb, 1, 0);     /* no AC prediction */
762
-        }
763
-        cbpy = cbp >> 2;
764
-        put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
765
-        if(s->dquant)
766
-            put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
767
-
768
-        if(interleaved_stats){
769
-            s->misc_bits+= get_bits_diff(s);
770
-        }
771
-    }
772
-
773
-    for(i=0; i<6; i++) {
774
-        /* encode each block */
775
-        h263_encode_block(s, block[i], i);
776
-
777
-        /* Update INTRADC for decoding */
778
-        if (s->h263_aic && s->mb_intra) {
779
-            block[i][0] = rec_intradc[i];
780
-
781
-        }
782
-    }
783
-
784
-    if(interleaved_stats){
785
-        if (!s->mb_intra) {
786
-            s->p_tex_bits+= get_bits_diff(s);
787
-            s->f_count++;
788
-        }else{
789
-            s->i_tex_bits+= get_bits_diff(s);
790
-            s->i_count++;
791
-        }
792
-    }
793
-}
794
-#endif
795
-
796 594
 void ff_h263_loop_filter(MpegEncContext * s){
797 595
     int qp_c;
798 596
     const int linesize  = s->linesize;
... ...
@@ -880,7 +226,7 @@ void ff_h263_loop_filter(MpegEncContext * s){
880 880
     }
881 881
 }
882 882
 
883
-static void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n)
883
+void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n)
884 884
 {
885 885
     int x, y, wrap, a, c, pred_dc, scale, i;
886 886
     int16_t *dc_val, *ac_val, *ac_val1;
... ...
@@ -1025,229 +371,6 @@ int16_t *h263_pred_motion(MpegEncContext * s, int block, int dir,
1025 1025
     return *mot_val;
1026 1026
 }
1027 1027
 
1028
-#if CONFIG_ENCODERS
1029
-
1030
-/***************************************************/
1031
-void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
1032
-{
1033
-    int range, l, bit_size, sign, code, bits;
1034
-
1035
-    if (val == 0) {
1036
-        /* zero vector */
1037
-        code = 0;
1038
-        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1039
-    } else {
1040
-        bit_size = f_code - 1;
1041
-        range = 1 << bit_size;
1042
-        /* modulo encoding */
1043
-        l= INT_BIT - 6 - bit_size;
1044
-        val = (val<<l)>>l;
1045
-        sign = val>>31;
1046
-        val= (val^sign)-sign;
1047
-        sign&=1;
1048
-
1049
-        val--;
1050
-        code = (val >> bit_size) + 1;
1051
-        bits = val & (range - 1);
1052
-
1053
-        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
1054
-        if (bit_size > 0) {
1055
-            put_bits(&s->pb, bit_size, bits);
1056
-        }
1057
-    }
1058
-}
1059
-
1060
-static void init_mv_penalty_and_fcode(MpegEncContext *s)
1061
-{
1062
-    int f_code;
1063
-    int mv;
1064
-
1065
-    for(f_code=1; f_code<=MAX_FCODE; f_code++){
1066
-        for(mv=-MAX_MV; mv<=MAX_MV; mv++){
1067
-            int len;
1068
-
1069
-            if(mv==0) len= mvtab[0][1];
1070
-            else{
1071
-                int val, bit_size, code;
1072
-
1073
-                bit_size = f_code - 1;
1074
-
1075
-                val=mv;
1076
-                if (val < 0)
1077
-                    val = -val;
1078
-                val--;
1079
-                code = (val >> bit_size) + 1;
1080
-                if(code<33){
1081
-                    len= mvtab[code][1] + 1 + bit_size;
1082
-                }else{
1083
-                    len= mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
1084
-                }
1085
-            }
1086
-
1087
-            mv_penalty[f_code][mv+MAX_MV]= len;
1088
-        }
1089
-    }
1090
-
1091
-    for(f_code=MAX_FCODE; f_code>0; f_code--){
1092
-        for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
1093
-            fcode_tab[mv+MAX_MV]= f_code;
1094
-        }
1095
-    }
1096
-
1097
-    for(mv=0; mv<MAX_MV*2+1; mv++){
1098
-        umv_fcode_tab[mv]= 1;
1099
-    }
1100
-}
1101
-
1102
-static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
1103
-    int slevel, run, last;
1104
-
1105
-    assert(MAX_LEVEL >= 64);
1106
-    assert(MAX_RUN   >= 63);
1107
-
1108
-    for(slevel=-64; slevel<64; slevel++){
1109
-        if(slevel==0) continue;
1110
-        for(run=0; run<64; run++){
1111
-            for(last=0; last<=1; last++){
1112
-                const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
1113
-                int level= slevel < 0 ? -slevel : slevel;
1114
-                int sign= slevel < 0 ? 1 : 0;
1115
-                int bits, len, code;
1116
-
1117
-                len_tab[index]= 100;
1118
-
1119
-                /* ESC0 */
1120
-                code= get_rl_index(rl, last, run, level);
1121
-                bits= rl->table_vlc[code][0];
1122
-                len=  rl->table_vlc[code][1];
1123
-                bits=bits*2+sign; len++;
1124
-
1125
-                if(code!=rl->n && len < len_tab[index]){
1126
-                    if(bits_tab) bits_tab[index]= bits;
1127
-                    len_tab [index]= len;
1128
-                }
1129
-                /* ESC */
1130
-                bits= rl->table_vlc[rl->n][0];
1131
-                len = rl->table_vlc[rl->n][1];
1132
-                bits=bits*2+last; len++;
1133
-                bits=bits*64+run; len+=6;
1134
-                bits=bits*256+(level&0xff); len+=8;
1135
-
1136
-                if(len < len_tab[index]){
1137
-                    if(bits_tab) bits_tab[index]= bits;
1138
-                    len_tab [index]= len;
1139
-                }
1140
-            }
1141
-        }
1142
-    }
1143
-}
1144
-
1145
-void h263_encode_init(MpegEncContext *s)
1146
-{
1147
-    static int done = 0;
1148
-
1149
-    if (!done) {
1150
-        done = 1;
1151
-
1152
-        init_rl(&ff_h263_rl_inter, static_rl_table_store[0]);
1153
-        init_rl(&rl_intra_aic, static_rl_table_store[1]);
1154
-
1155
-        init_uni_h263_rl_tab(&rl_intra_aic, NULL, uni_h263_intra_aic_rl_len);
1156
-        init_uni_h263_rl_tab(&ff_h263_rl_inter    , NULL, uni_h263_inter_rl_len);
1157
-
1158
-        init_mv_penalty_and_fcode(s);
1159
-    }
1160
-    s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
1161
-
1162
-    s->intra_ac_vlc_length     =s->inter_ac_vlc_length     = uni_h263_inter_rl_len;
1163
-    s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
1164
-    if(s->h263_aic){
1165
-        s->intra_ac_vlc_length     = uni_h263_intra_aic_rl_len;
1166
-        s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
1167
-    }
1168
-    s->ac_esc_length= 7+1+6+8;
1169
-
1170
-    // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
1171
-    switch(s->codec_id){
1172
-    case CODEC_ID_MPEG4:
1173
-        s->fcode_tab= fcode_tab;
1174
-        break;
1175
-    case CODEC_ID_H263P:
1176
-        if(s->umvplus)
1177
-            s->fcode_tab= umv_fcode_tab;
1178
-        if(s->modified_quant){
1179
-            s->min_qcoeff= -2047;
1180
-            s->max_qcoeff=  2047;
1181
-        }else{
1182
-            s->min_qcoeff= -127;
1183
-            s->max_qcoeff=  127;
1184
-        }
1185
-        break;
1186
-        //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
1187
-    case CODEC_ID_FLV1:
1188
-        if (s->h263_flv > 1) {
1189
-            s->min_qcoeff= -1023;
1190
-            s->max_qcoeff=  1023;
1191
-        } else {
1192
-            s->min_qcoeff= -127;
1193
-            s->max_qcoeff=  127;
1194
-        }
1195
-        s->y_dc_scale_table=
1196
-        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1197
-        break;
1198
-    default: //nothing needed - default table already set in mpegvideo.c
1199
-        s->min_qcoeff= -127;
1200
-        s->max_qcoeff=  127;
1201
-        s->y_dc_scale_table=
1202
-        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1203
-    }
1204
-}
1205
-#endif //CONFIG_ENCODERS
1206
-
1207
-/***********************************************/
1208
-/* decoding */
1209
-
1210
-VLC ff_h263_intra_MCBPC_vlc;
1211
-VLC ff_h263_inter_MCBPC_vlc;
1212
-VLC ff_h263_cbpy_vlc;
1213
-static VLC mv_vlc;
1214
-static VLC h263_mbtype_b_vlc;
1215
-static VLC cbpc_b_vlc;
1216
-
1217
-/* init vlcs */
1218
-
1219
-/* XXX: find a better solution to handle static init */
1220
-void h263_decode_init_vlc(MpegEncContext *s)
1221
-{
1222
-    static int done = 0;
1223
-
1224
-    if (!done) {
1225
-        done = 1;
1226
-
1227
-        INIT_VLC_STATIC(&ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
1228
-                 ff_h263_intra_MCBPC_bits, 1, 1,
1229
-                 ff_h263_intra_MCBPC_code, 1, 1, 72);
1230
-        INIT_VLC_STATIC(&ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
1231
-                 ff_h263_inter_MCBPC_bits, 1, 1,
1232
-                 ff_h263_inter_MCBPC_code, 1, 1, 198);
1233
-        INIT_VLC_STATIC(&ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
1234
-                 &ff_h263_cbpy_tab[0][1], 2, 1,
1235
-                 &ff_h263_cbpy_tab[0][0], 2, 1, 64);
1236
-        INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33,
1237
-                 &mvtab[0][1], 2, 1,
1238
-                 &mvtab[0][0], 2, 1, 538);
1239
-        init_rl(&ff_h263_rl_inter, static_rl_table_store[0]);
1240
-        init_rl(&rl_intra_aic, static_rl_table_store[1]);
1241
-        INIT_VLC_RL(ff_h263_rl_inter, 554);
1242
-        INIT_VLC_RL(rl_intra_aic, 554);
1243
-        INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
1244
-                 &h263_mbtype_b_tab[0][1], 2, 1,
1245
-                 &h263_mbtype_b_tab[0][0], 2, 1, 80);
1246
-        INIT_VLC_STATIC(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
1247
-                 &cbpc_b_tab[0][1], 2, 1,
1248
-                 &cbpc_b_tab[0][0], 2, 1, 8);
1249
-    }
1250
-}
1251 1028
 
1252 1029
 /**
1253 1030
  * Get the GOB height based on picture height.
... ...
@@ -1260,1021 +383,3 @@ int ff_h263_get_gob_height(MpegEncContext *s){
1260 1260
     else
1261 1261
         return 4;
1262 1262
 }
1263
-
1264
-int ff_h263_decode_mba(MpegEncContext *s)
1265
-{
1266
-    int i, mb_pos;
1267
-
1268
-    for(i=0; i<6; i++){
1269
-        if(s->mb_num-1 <= ff_mba_max[i]) break;
1270
-    }
1271
-    mb_pos= get_bits(&s->gb, ff_mba_length[i]);
1272
-    s->mb_x= mb_pos % s->mb_width;
1273
-    s->mb_y= mb_pos / s->mb_width;
1274
-
1275
-    return mb_pos;
1276
-}
1277
-
1278
-void ff_h263_encode_mba(MpegEncContext *s)
1279
-{
1280
-    int i, mb_pos;
1281
-
1282
-    for(i=0; i<6; i++){
1283
-        if(s->mb_num-1 <= ff_mba_max[i]) break;
1284
-    }
1285
-    mb_pos= s->mb_x + s->mb_width*s->mb_y;
1286
-    put_bits(&s->pb, ff_mba_length[i], mb_pos);
1287
-}
1288
-
1289
-/**
1290
- * decodes the group of blocks header or slice header.
1291
- * @return <0 if an error occurred
1292
- */
1293
-static int h263_decode_gob_header(MpegEncContext *s)
1294
-{
1295
-    unsigned int val, gfid, gob_number;
1296
-    int left;
1297
-
1298
-    /* Check for GOB Start Code */
1299
-    val = show_bits(&s->gb, 16);
1300
-    if(val)
1301
-        return -1;
1302
-
1303
-        /* We have a GBSC probably with GSTUFF */
1304
-    skip_bits(&s->gb, 16); /* Drop the zeros */
1305
-    left= get_bits_left(&s->gb);
1306
-    //MN: we must check the bits left or we might end in a infinite loop (or segfault)
1307
-    for(;left>13; left--){
1308
-        if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
1309
-    }
1310
-    if(left<=13)
1311
-        return -1;
1312
-
1313
-    if(s->h263_slice_structured){
1314
-        if(get_bits1(&s->gb)==0)
1315
-            return -1;
1316
-
1317
-        ff_h263_decode_mba(s);
1318
-
1319
-        if(s->mb_num > 1583)
1320
-            if(get_bits1(&s->gb)==0)
1321
-                return -1;
1322
-
1323
-        s->qscale = get_bits(&s->gb, 5); /* SQUANT */
1324
-        if(get_bits1(&s->gb)==0)
1325
-            return -1;
1326
-        gfid = get_bits(&s->gb, 2); /* GFID */
1327
-    }else{
1328
-        gob_number = get_bits(&s->gb, 5); /* GN */
1329
-        s->mb_x= 0;
1330
-        s->mb_y= s->gob_index* gob_number;
1331
-        gfid = get_bits(&s->gb, 2); /* GFID */
1332
-        s->qscale = get_bits(&s->gb, 5); /* GQUANT */
1333
-    }
1334
-
1335
-    if(s->mb_y >= s->mb_height)
1336
-        return -1;
1337
-
1338
-    if(s->qscale==0)
1339
-        return -1;
1340
-
1341
-    return 0;
1342
-}
1343
-
1344
-static inline void memsetw(short *tab, int val, int n)
1345
-{
1346
-    int i;
1347
-    for(i=0;i<n;i++)
1348
-        tab[i] = val;
1349
-}
1350
-
1351
-/**
1352
- * finds the next resync_marker
1353
- * @param p pointer to buffer to scan
1354
- * @param end pointer to the end of the buffer
1355
- * @return pointer to the next resync_marker, or end if none was found
1356
- */
1357
-const uint8_t *ff_h263_find_resync_marker(const uint8_t *restrict p, const uint8_t * restrict end)
1358
-{
1359
-    assert(p < end);
1360
-
1361
-    end-=2;
1362
-    p++;
1363
-    for(;p<end; p+=2){
1364
-        if(!*p){
1365
-            if     (!p[-1] && p[1]) return p - 1;
1366
-            else if(!p[ 1] && p[2]) return p;
1367
-        }
1368
-    }
1369
-    return end+2;
1370
-}
1371
-
1372
-/**
1373
- * decodes the group of blocks / video packet header.
1374
- * @return bit position of the resync_marker, or <0 if none was found
1375
- */
1376
-int ff_h263_resync(MpegEncContext *s){
1377
-    int left, pos, ret;
1378
-
1379
-    if(s->codec_id==CODEC_ID_MPEG4){
1380
-        skip_bits1(&s->gb);
1381
-        align_get_bits(&s->gb);
1382
-    }
1383
-
1384
-    if(show_bits(&s->gb, 16)==0){
1385
-        pos= get_bits_count(&s->gb);
1386
-        if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4)
1387
-            ret= mpeg4_decode_video_packet_header(s);
1388
-        else
1389
-            ret= h263_decode_gob_header(s);
1390
-        if(ret>=0)
1391
-            return pos;
1392
-    }
1393
-    //OK, it's not where it is supposed to be ...
1394
-    s->gb= s->last_resync_gb;
1395
-    align_get_bits(&s->gb);
1396
-    left= get_bits_left(&s->gb);
1397
-
1398
-    for(;left>16+1+5+5; left-=8){
1399
-        if(show_bits(&s->gb, 16)==0){
1400
-            GetBitContext bak= s->gb;
1401
-
1402
-            pos= get_bits_count(&s->gb);
1403
-            if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4)
1404
-                ret= mpeg4_decode_video_packet_header(s);
1405
-            else
1406
-                ret= h263_decode_gob_header(s);
1407
-            if(ret>=0)
1408
-                return pos;
1409
-
1410
-            s->gb= bak;
1411
-        }
1412
-        skip_bits(&s->gb, 8);
1413
-    }
1414
-
1415
-    return -1;
1416
-}
1417
-
1418
-int h263_decode_motion(MpegEncContext * s, int pred, int f_code)
1419
-{
1420
-    int code, val, sign, shift, l;
1421
-    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
1422
-
1423
-    if (code == 0)
1424
-        return pred;
1425
-    if (code < 0)
1426
-        return 0xffff;
1427
-
1428
-    sign = get_bits1(&s->gb);
1429
-    shift = f_code - 1;
1430
-    val = code;
1431
-    if (shift) {
1432
-        val = (val - 1) << shift;
1433
-        val |= get_bits(&s->gb, shift);
1434
-        val++;
1435
-    }
1436
-    if (sign)
1437
-        val = -val;
1438
-    val += pred;
1439
-
1440
-    /* modulo decoding */
1441
-    if (!s->h263_long_vectors) {
1442
-        l = INT_BIT - 5 - f_code;
1443
-        val = (val<<l)>>l;
1444
-    } else {
1445
-        /* horrible h263 long vector mode */
1446
-        if (pred < -31 && val < -63)
1447
-            val += 64;
1448
-        if (pred > 32 && val > 63)
1449
-            val -= 64;
1450
-
1451
-    }
1452
-    return val;
1453
-}
1454
-
1455
-
1456
-/* Decodes RVLC of H.263+ UMV */
1457
-static int h263p_decode_umotion(MpegEncContext * s, int pred)
1458
-{
1459
-   int code = 0, sign;
1460
-
1461
-   if (get_bits1(&s->gb)) /* Motion difference = 0 */
1462
-      return pred;
1463
-
1464
-   code = 2 + get_bits1(&s->gb);
1465
-
1466
-   while (get_bits1(&s->gb))
1467
-   {
1468
-      code <<= 1;
1469
-      code += get_bits1(&s->gb);
1470
-   }
1471
-   sign = code & 1;
1472
-   code >>= 1;
1473
-
1474
-   code = (sign) ? (pred - code) : (pred + code);
1475
-   dprintf(s->avctx,"H.263+ UMV Motion = %d\n", code);
1476
-   return code;
1477
-
1478
-}
1479
-
1480
-/**
1481
- * read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :)
1482
- */
1483
-static void preview_obmc(MpegEncContext *s){
1484
-    GetBitContext gb= s->gb;
1485
-
1486
-    int cbpc, i, pred_x, pred_y, mx, my;
1487
-    int16_t *mot_val;
1488
-    const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
1489
-    const int stride= s->b8_stride*2;
1490
-
1491
-    for(i=0; i<4; i++)
1492
-        s->block_index[i]+= 2;
1493
-    for(i=4; i<6; i++)
1494
-        s->block_index[i]+= 1;
1495
-    s->mb_x++;
1496
-
1497
-    assert(s->pict_type == FF_P_TYPE);
1498
-
1499
-    do{
1500
-        if (get_bits1(&s->gb)) {
1501
-            /* skip mb */
1502
-            mot_val = s->current_picture.motion_val[0][ s->block_index[0] ];
1503
-            mot_val[0       ]= mot_val[2       ]=
1504
-            mot_val[0+stride]= mot_val[2+stride]= 0;
1505
-            mot_val[1       ]= mot_val[3       ]=
1506
-            mot_val[1+stride]= mot_val[3+stride]= 0;
1507
-
1508
-            s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
1509
-            goto end;
1510
-        }
1511
-        cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1512
-    }while(cbpc == 20);
1513
-
1514
-    if(cbpc & 4){
1515
-        s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
1516
-    }else{
1517
-        get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1518
-        if (cbpc & 8) {
1519
-            if(s->modified_quant){
1520
-                if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
1521
-                else                  skip_bits(&s->gb, 5);
1522
-            }else
1523
-                skip_bits(&s->gb, 2);
1524
-        }
1525
-
1526
-        if ((cbpc & 16) == 0) {
1527
-                s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
1528
-                /* 16x16 motion prediction */
1529
-                mot_val= h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1530
-                if (s->umvplus)
1531
-                   mx = h263p_decode_umotion(s, pred_x);
1532
-                else
1533
-                   mx = h263_decode_motion(s, pred_x, 1);
1534
-
1535
-                if (s->umvplus)
1536
-                   my = h263p_decode_umotion(s, pred_y);
1537
-                else
1538
-                   my = h263_decode_motion(s, pred_y, 1);
1539
-
1540
-                mot_val[0       ]= mot_val[2       ]=
1541
-                mot_val[0+stride]= mot_val[2+stride]= mx;
1542
-                mot_val[1       ]= mot_val[3       ]=
1543
-                mot_val[1+stride]= mot_val[3+stride]= my;
1544
-        } else {
1545
-            s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
1546
-            for(i=0;i<4;i++) {
1547
-                mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1548
-                if (s->umvplus)
1549
-                  mx = h263p_decode_umotion(s, pred_x);
1550
-                else
1551
-                  mx = h263_decode_motion(s, pred_x, 1);
1552
-
1553
-                if (s->umvplus)
1554
-                  my = h263p_decode_umotion(s, pred_y);
1555
-                else
1556
-                  my = h263_decode_motion(s, pred_y, 1);
1557
-                if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
1558
-                  skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
1559
-                mot_val[0] = mx;
1560
-                mot_val[1] = my;
1561
-            }
1562
-        }
1563
-    }
1564
-end:
1565
-
1566
-    for(i=0; i<4; i++)
1567
-        s->block_index[i]-= 2;
1568
-    for(i=4; i<6; i++)
1569
-        s->block_index[i]-= 1;
1570
-    s->mb_x--;
1571
-
1572
-    s->gb= gb;
1573
-}
1574
-
1575
-static void h263_decode_dquant(MpegEncContext *s){
1576
-    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1577
-
1578
-    if(s->modified_quant){
1579
-        if(get_bits1(&s->gb))
1580
-            s->qscale= modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
1581
-        else
1582
-            s->qscale= get_bits(&s->gb, 5);
1583
-    }else
1584
-        s->qscale += quant_tab[get_bits(&s->gb, 2)];
1585
-    ff_set_qscale(s, s->qscale);
1586
-}
1587
-
1588
-static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
1589
-                             int n, int coded)
1590
-{
1591
-    int code, level, i, j, last, run;
1592
-    RLTable *rl = &ff_h263_rl_inter;
1593
-    const uint8_t *scan_table;
1594
-    GetBitContext gb= s->gb;
1595
-
1596
-    scan_table = s->intra_scantable.permutated;
1597
-    if (s->h263_aic && s->mb_intra) {
1598
-        rl = &rl_intra_aic;
1599
-        i = 0;
1600
-        if (s->ac_pred) {
1601
-            if (s->h263_aic_dir)
1602
-                scan_table = s->intra_v_scantable.permutated; /* left */
1603
-            else
1604
-                scan_table = s->intra_h_scantable.permutated; /* top */
1605
-        }
1606
-    } else if (s->mb_intra) {
1607
-        /* DC coef */
1608
-        if(s->codec_id == CODEC_ID_RV10){
1609
-#if CONFIG_RV10_DECODER
1610
-          if (s->rv10_version == 3 && s->pict_type == FF_I_TYPE) {
1611
-            int component, diff;
1612
-            component = (n <= 3 ? 0 : n - 4 + 1);
1613
-            level = s->last_dc[component];
1614
-            if (s->rv10_first_dc_coded[component]) {
1615
-                diff = rv_decode_dc(s, n);
1616
-                if (diff == 0xffff)
1617
-                    return -1;
1618
-                level += diff;
1619
-                level = level & 0xff; /* handle wrap round */
1620
-                s->last_dc[component] = level;
1621
-            } else {
1622
-                s->rv10_first_dc_coded[component] = 1;
1623
-            }
1624
-          } else {
1625
-                level = get_bits(&s->gb, 8);
1626
-                if (level == 255)
1627
-                    level = 128;
1628
-          }
1629
-#endif
1630
-        }else{
1631
-            level = get_bits(&s->gb, 8);
1632
-            if((level&0x7F) == 0){
1633
-                av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
1634
-                if(s->error_recognition >= FF_ER_COMPLIANT)
1635
-                    return -1;
1636
-            }
1637
-            if (level == 255)
1638
-                level = 128;
1639
-        }
1640
-        block[0] = level;
1641
-        i = 1;
1642
-    } else {
1643
-        i = 0;
1644
-    }
1645
-    if (!coded) {
1646
-        if (s->mb_intra && s->h263_aic)
1647
-            goto not_coded;
1648
-        s->block_last_index[n] = i - 1;
1649
-        return 0;
1650
-    }
1651
-retry:
1652
-    for(;;) {
1653
-        code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
1654
-        if (code < 0){
1655
-            av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
1656
-            return -1;
1657
-        }
1658
-        if (code == rl->n) {
1659
-            /* escape */
1660
-            if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
1661
-                ff_flv2_decode_ac_esc(&s->gb, &level, &run, &last);
1662
-            } else {
1663
-                last = get_bits1(&s->gb);
1664
-                run = get_bits(&s->gb, 6);
1665
-                level = (int8_t)get_bits(&s->gb, 8);
1666
-                if(level == -128){
1667
-                    if (s->codec_id == CODEC_ID_RV10) {
1668
-                        /* XXX: should patch encoder too */
1669
-                        level = get_sbits(&s->gb, 12);
1670
-                    }else{
1671
-                        level = get_bits(&s->gb, 5);
1672
-                        level |= get_sbits(&s->gb, 6)<<5;
1673
-                    }
1674
-                }
1675
-            }
1676
-        } else {
1677
-            run = rl->table_run[code];
1678
-            level = rl->table_level[code];
1679
-            last = code >= rl->last;
1680
-            if (get_bits1(&s->gb))
1681
-                level = -level;
1682
-        }
1683
-        i += run;
1684
-        if (i >= 64){
1685
-            if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
1686
-                //Looks like a hack but no, it's the way it is supposed to work ...
1687
-                rl = &rl_intra_aic;
1688
-                i = 0;
1689
-                s->gb= gb;
1690
-                s->dsp.clear_block(block);
1691
-                goto retry;
1692
-            }
1693
-            av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
1694
-            return -1;
1695
-        }
1696
-        j = scan_table[i];
1697
-        block[j] = level;
1698
-        if (last)
1699
-            break;
1700
-        i++;
1701
-    }
1702
-not_coded:
1703
-    if (s->mb_intra && s->h263_aic) {
1704
-        h263_pred_acdc(s, block, n);
1705
-        i = 63;
1706
-    }
1707
-    s->block_last_index[n] = i;
1708
-    return 0;
1709
-}
1710
-
1711
-static int h263_skip_b_part(MpegEncContext *s, int cbp)
1712
-{
1713
-    DECLARE_ALIGNED(16, DCTELEM, dblock[64]);
1714
-    int i, mbi;
1715
-
1716
-    /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
1717
-     * but real value should be restored in order to be used later (in OBMC condition)
1718
-     */
1719
-    mbi = s->mb_intra;
1720
-    s->mb_intra = 0;
1721
-    for (i = 0; i < 6; i++) {
1722
-        if (h263_decode_block(s, dblock, i, cbp&32) < 0)
1723
-            return -1;
1724
-        cbp+=cbp;
1725
-    }
1726
-    s->mb_intra = mbi;
1727
-    return 0;
1728
-}
1729
-
1730
-static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
1731
-{
1732
-    int c, mv = 1;
1733
-
1734
-    if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
1735
-        c = get_bits1(gb);
1736
-        if (pb_frame == 2 && c)
1737
-            mv = !get_bits1(gb);
1738
-    } else { // h.263 Annex M improved PB-frame
1739
-        mv = get_unary(gb, 0, 4) + 1;
1740
-        c = mv & 1;
1741
-        mv = !!(mv & 2);
1742
-    }
1743
-    if(c)
1744
-        *cbpb = get_bits(gb, 6);
1745
-    return mv;
1746
-}
1747
-
1748
-int ff_h263_decode_mb(MpegEncContext *s,
1749
-                      DCTELEM block[6][64])
1750
-{
1751
-    int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1752
-    int16_t *mot_val;
1753
-    const int xy= s->mb_x + s->mb_y * s->mb_stride;
1754
-    int cbpb = 0, pb_mv_count = 0;
1755
-
1756
-    assert(!s->h263_pred);
1757
-
1758
-    if (s->pict_type == FF_P_TYPE) {
1759
-        do{
1760
-            if (get_bits1(&s->gb)) {
1761
-                /* skip mb */
1762
-                s->mb_intra = 0;
1763
-                for(i=0;i<6;i++)
1764
-                    s->block_last_index[i] = -1;
1765
-                s->mv_dir = MV_DIR_FORWARD;
1766
-                s->mv_type = MV_TYPE_16X16;
1767
-                s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
1768
-                s->mv[0][0][0] = 0;
1769
-                s->mv[0][0][1] = 0;
1770
-                s->mb_skipped = !(s->obmc | s->loop_filter);
1771
-                goto end;
1772
-            }
1773
-            cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1774
-            if (cbpc < 0){
1775
-                av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1776
-                return -1;
1777
-            }
1778
-        }while(cbpc == 20);
1779
-
1780
-        s->dsp.clear_blocks(s->block[0]);
1781
-
1782
-        dquant = cbpc & 8;
1783
-        s->mb_intra = ((cbpc & 4) != 0);
1784
-        if (s->mb_intra) goto intra;
1785
-
1786
-        if(s->pb_frame && get_bits1(&s->gb))
1787
-            pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
1788
-        cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1789
-
1790
-        if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
1791
-            cbpy ^= 0xF;
1792
-
1793
-        cbp = (cbpc & 3) | (cbpy << 2);
1794
-        if (dquant) {
1795
-            h263_decode_dquant(s);
1796
-        }
1797
-
1798
-        s->mv_dir = MV_DIR_FORWARD;
1799
-        if ((cbpc & 16) == 0) {
1800
-            s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
1801
-            /* 16x16 motion prediction */
1802
-            s->mv_type = MV_TYPE_16X16;
1803
-            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1804
-            if (s->umvplus)
1805
-               mx = h263p_decode_umotion(s, pred_x);
1806
-            else
1807
-               mx = h263_decode_motion(s, pred_x, 1);
1808
-
1809
-            if (mx >= 0xffff)
1810
-                return -1;
1811
-
1812
-            if (s->umvplus)
1813
-               my = h263p_decode_umotion(s, pred_y);
1814
-            else
1815
-               my = h263_decode_motion(s, pred_y, 1);
1816
-
1817
-            if (my >= 0xffff)
1818
-                return -1;
1819
-            s->mv[0][0][0] = mx;
1820
-            s->mv[0][0][1] = my;
1821
-
1822
-            if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
1823
-               skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
1824
-        } else {
1825
-            s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
1826
-            s->mv_type = MV_TYPE_8X8;
1827
-            for(i=0;i<4;i++) {
1828
-                mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1829
-                if (s->umvplus)
1830
-                  mx = h263p_decode_umotion(s, pred_x);
1831
-                else
1832
-                  mx = h263_decode_motion(s, pred_x, 1);
1833
-                if (mx >= 0xffff)
1834
-                    return -1;
1835
-
1836
-                if (s->umvplus)
1837
-                  my = h263p_decode_umotion(s, pred_y);
1838
-                else
1839
-                  my = h263_decode_motion(s, pred_y, 1);
1840
-                if (my >= 0xffff)
1841
-                    return -1;
1842
-                s->mv[0][i][0] = mx;
1843
-                s->mv[0][i][1] = my;
1844
-                if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
1845
-                  skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
1846
-                mot_val[0] = mx;
1847
-                mot_val[1] = my;
1848
-            }
1849
-        }
1850
-    } else if(s->pict_type==FF_B_TYPE) {
1851
-        int mb_type;
1852
-        const int stride= s->b8_stride;
1853
-        int16_t *mot_val0 = s->current_picture.motion_val[0][ 2*(s->mb_x + s->mb_y*stride) ];
1854
-        int16_t *mot_val1 = s->current_picture.motion_val[1][ 2*(s->mb_x + s->mb_y*stride) ];
1855
-//        const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
1856
-
1857
-        //FIXME ugly
1858
-        mot_val0[0       ]= mot_val0[2       ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
1859
-        mot_val0[1       ]= mot_val0[3       ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
1860
-        mot_val1[0       ]= mot_val1[2       ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
1861
-        mot_val1[1       ]= mot_val1[3       ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
1862
-
1863
-        do{
1864
-            mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
1865
-            if (mb_type < 0){
1866
-                av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
1867
-                return -1;
1868
-            }
1869
-
1870
-            mb_type= h263_mb_type_b_map[ mb_type ];
1871
-        }while(!mb_type);
1872
-
1873
-        s->mb_intra = IS_INTRA(mb_type);
1874
-        if(HAS_CBP(mb_type)){
1875
-            s->dsp.clear_blocks(s->block[0]);
1876
-            cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
1877
-            if(s->mb_intra){
1878
-                dquant = IS_QUANT(mb_type);
1879
-                goto intra;
1880
-            }
1881
-
1882
-            cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1883
-
1884
-            if (cbpy < 0){
1885
-                av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1886
-                return -1;
1887
-            }
1888
-
1889
-            if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
1890
-                cbpy ^= 0xF;
1891
-
1892
-            cbp = (cbpc & 3) | (cbpy << 2);
1893
-        }else
1894
-            cbp=0;
1895
-
1896
-        assert(!s->mb_intra);
1897
-
1898
-        if(IS_QUANT(mb_type)){
1899
-            h263_decode_dquant(s);
1900
-        }
1901
-
1902
-        if(IS_DIRECT(mb_type)){
1903
-            s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1904
-            mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
1905
-        }else{
1906
-            s->mv_dir = 0;
1907
-            s->mv_type= MV_TYPE_16X16;
1908
-//FIXME UMV
1909
-
1910
-            if(USES_LIST(mb_type, 0)){
1911
-                int16_t *mot_val= h263_pred_motion(s, 0, 0, &mx, &my);
1912
-                s->mv_dir = MV_DIR_FORWARD;
1913
-
1914
-                mx = h263_decode_motion(s, mx, 1);
1915
-                my = h263_decode_motion(s, my, 1);
1916
-
1917
-                s->mv[0][0][0] = mx;
1918
-                s->mv[0][0][1] = my;
1919
-                mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
1920
-                mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
1921
-            }
1922
-
1923
-            if(USES_LIST(mb_type, 1)){
1924
-                int16_t *mot_val= h263_pred_motion(s, 0, 1, &mx, &my);
1925
-                s->mv_dir |= MV_DIR_BACKWARD;
1926
-
1927
-                mx = h263_decode_motion(s, mx, 1);
1928
-                my = h263_decode_motion(s, my, 1);
1929
-
1930
-                s->mv[1][0][0] = mx;
1931
-                s->mv[1][0][1] = my;
1932
-                mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
1933
-                mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
1934
-            }
1935
-        }
1936
-
1937
-        s->current_picture.mb_type[xy]= mb_type;
1938
-    } else { /* I-Frame */
1939
-        do{
1940
-            cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1941
-            if (cbpc < 0){
1942
-                av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1943
-                return -1;
1944
-            }
1945
-        }while(cbpc == 8);
1946
-
1947
-        s->dsp.clear_blocks(s->block[0]);
1948
-
1949
-        dquant = cbpc & 4;
1950
-        s->mb_intra = 1;
1951
-intra:
1952
-        s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
1953
-        if (s->h263_aic) {
1954
-            s->ac_pred = get_bits1(&s->gb);
1955
-            if(s->ac_pred){
1956
-                s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED;
1957
-
1958
-                s->h263_aic_dir = get_bits1(&s->gb);
1959
-            }
1960
-        }else
1961
-            s->ac_pred = 0;
1962
-
1963
-        if(s->pb_frame && get_bits1(&s->gb))
1964
-            pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
1965
-        cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1966
-        if(cbpy<0){
1967
-            av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1968
-            return -1;
1969
-        }
1970
-        cbp = (cbpc & 3) | (cbpy << 2);
1971
-        if (dquant) {
1972
-            h263_decode_dquant(s);
1973
-        }
1974
-
1975
-        pb_mv_count += !!s->pb_frame;
1976
-    }
1977
-
1978
-    while(pb_mv_count--){
1979
-        h263_decode_motion(s, 0, 1);
1980
-        h263_decode_motion(s, 0, 1);
1981
-    }
1982
-
1983
-    /* decode each block */
1984
-    for (i = 0; i < 6; i++) {
1985
-        if (h263_decode_block(s, block[i], i, cbp&32) < 0)
1986
-            return -1;
1987
-        cbp+=cbp;
1988
-    }
1989
-
1990
-    if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
1991
-        return -1;
1992
-    if(s->obmc && !s->mb_intra){
1993
-        if(s->pict_type == FF_P_TYPE && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
1994
-            preview_obmc(s);
1995
-    }
1996
-end:
1997
-
1998
-        /* per-MB end of slice check */
1999
-    {
2000
-        int v= show_bits(&s->gb, 16);
2001
-
2002
-        if(get_bits_count(&s->gb) + 16 > s->gb.size_in_bits){
2003
-            v>>= get_bits_count(&s->gb) + 16 - s->gb.size_in_bits;
2004
-        }
2005
-
2006
-        if(v==0)
2007
-            return SLICE_END;
2008
-    }
2009
-
2010
-    return SLICE_OK;
2011
-}
2012
-
2013
-/* most is hardcoded. should extend to handle all h263 streams */
2014
-int h263_decode_picture_header(MpegEncContext *s)
2015
-{
2016
-    int format, width, height, i;
2017
-    uint32_t startcode;
2018
-
2019
-    align_get_bits(&s->gb);
2020
-
2021
-    startcode= get_bits(&s->gb, 22-8);
2022
-
2023
-    for(i= get_bits_left(&s->gb); i>24; i-=8) {
2024
-        startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
2025
-
2026
-        if(startcode == 0x20)
2027
-            break;
2028
-    }
2029
-
2030
-    if (startcode != 0x20) {
2031
-        av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
2032
-        return -1;
2033
-    }
2034
-    /* temporal reference */
2035
-    i = get_bits(&s->gb, 8); /* picture timestamp */
2036
-    if( (s->picture_number&~0xFF)+i < s->picture_number)
2037
-        i+= 256;
2038
-    s->current_picture_ptr->pts=
2039
-    s->picture_number= (s->picture_number&~0xFF) + i;
2040
-
2041
-    /* PTYPE starts here */
2042
-    if (get_bits1(&s->gb) != 1) {
2043
-        /* marker */
2044
-        av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
2045
-        return -1;
2046
-    }
2047
-    if (get_bits1(&s->gb) != 0) {
2048
-        av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
2049
-        return -1;      /* h263 id */
2050
-    }
2051
-    skip_bits1(&s->gb);         /* split screen off */
2052
-    skip_bits1(&s->gb);         /* camera  off */
2053
-    skip_bits1(&s->gb);         /* freeze picture release off */
2054
-
2055
-    format = get_bits(&s->gb, 3);
2056
-    /*
2057
-        0    forbidden
2058
-        1    sub-QCIF
2059
-        10   QCIF
2060
-        7       extended PTYPE (PLUSPTYPE)
2061
-    */
2062
-
2063
-    if (format != 7 && format != 6) {
2064
-        s->h263_plus = 0;
2065
-        /* H.263v1 */
2066
-        width = h263_format[format][0];
2067
-        height = h263_format[format][1];
2068
-        if (!width)
2069
-            return -1;
2070
-
2071
-        s->pict_type = FF_I_TYPE + get_bits1(&s->gb);
2072
-
2073
-        s->h263_long_vectors = get_bits1(&s->gb);
2074
-
2075
-        if (get_bits1(&s->gb) != 0) {
2076
-            av_log(s->avctx, AV_LOG_ERROR, "H263 SAC not supported\n");
2077
-            return -1; /* SAC: off */
2078
-        }
2079
-        s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
2080
-        s->unrestricted_mv = s->h263_long_vectors || s->obmc;
2081
-
2082
-        s->pb_frame = get_bits1(&s->gb);
2083
-        s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
2084
-        skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
2085
-
2086
-        s->width = width;
2087
-        s->height = height;
2088
-        s->avctx->sample_aspect_ratio= (AVRational){12,11};
2089
-        s->avctx->time_base= (AVRational){1001, 30000};
2090
-    } else {
2091
-        int ufep;
2092
-
2093
-        /* H.263v2 */
2094
-        s->h263_plus = 1;
2095
-        ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
2096
-
2097
-        /* ufep other than 0 and 1 are reserved */
2098
-        if (ufep == 1) {
2099
-            /* OPPTYPE */
2100
-            format = get_bits(&s->gb, 3);
2101
-            dprintf(s->avctx, "ufep=1, format: %d\n", format);
2102
-            s->custom_pcf= get_bits1(&s->gb);
2103
-            s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
2104
-            if (get_bits1(&s->gb) != 0) {
2105
-                av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
2106
-            }
2107
-            s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
2108
-            s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
2109
-            s->loop_filter= get_bits1(&s->gb);
2110
-            s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
2111
-
2112
-            s->h263_slice_structured= get_bits1(&s->gb);
2113
-            if (get_bits1(&s->gb) != 0) {
2114
-                av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
2115
-            }
2116
-            if (get_bits1(&s->gb) != 0) {
2117
-                av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
2118
-            }
2119
-            s->alt_inter_vlc= get_bits1(&s->gb);
2120
-            s->modified_quant= get_bits1(&s->gb);
2121
-            if(s->modified_quant)
2122
-                s->chroma_qscale_table= ff_h263_chroma_qscale_table;
2123
-
2124
-            skip_bits(&s->gb, 1); /* Prevent start code emulation */
2125
-
2126
-            skip_bits(&s->gb, 3); /* Reserved */
2127
-        } else if (ufep != 0) {
2128
-            av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
2129
-            return -1;
2130
-        }
2131
-
2132
-        /* MPPTYPE */
2133
-        s->pict_type = get_bits(&s->gb, 3);
2134
-        switch(s->pict_type){
2135
-        case 0: s->pict_type= FF_I_TYPE;break;
2136
-        case 1: s->pict_type= FF_P_TYPE;break;
2137
-        case 2: s->pict_type= FF_P_TYPE;s->pb_frame = 3;break;
2138
-        case 3: s->pict_type= FF_B_TYPE;break;
2139
-        case 7: s->pict_type= FF_I_TYPE;break; //ZYGO
2140
-        default:
2141
-            return -1;
2142
-        }
2143
-        skip_bits(&s->gb, 2);
2144
-        s->no_rounding = get_bits1(&s->gb);
2145
-        skip_bits(&s->gb, 4);
2146
-
2147
-        /* Get the picture dimensions */
2148
-        if (ufep) {
2149
-            if (format == 6) {
2150
-                /* Custom Picture Format (CPFMT) */
2151
-                s->aspect_ratio_info = get_bits(&s->gb, 4);
2152
-                dprintf(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
2153
-                /* aspect ratios:
2154
-                0 - forbidden
2155
-                1 - 1:1
2156
-                2 - 12:11 (CIF 4:3)
2157
-                3 - 10:11 (525-type 4:3)
2158
-                4 - 16:11 (CIF 16:9)
2159
-                5 - 40:33 (525-type 16:9)
2160
-                6-14 - reserved
2161
-                */
2162
-                width = (get_bits(&s->gb, 9) + 1) * 4;
2163
-                skip_bits1(&s->gb);
2164
-                height = get_bits(&s->gb, 9) * 4;
2165
-                dprintf(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
2166
-                if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
2167
-                    /* aspected dimensions */
2168
-                    s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
2169
-                    s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
2170
-                }else{
2171
-                    s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
2172
-                }
2173
-            } else {
2174
-                width = h263_format[format][0];
2175
-                height = h263_format[format][1];
2176
-                s->avctx->sample_aspect_ratio= (AVRational){12,11};
2177
-            }
2178
-            if ((width == 0) || (height == 0))
2179
-                return -1;
2180
-            s->width = width;
2181
-            s->height = height;
2182
-
2183
-            if(s->custom_pcf){
2184
-                int gcd;
2185
-                s->avctx->time_base.den= 1800000;
2186
-                s->avctx->time_base.num= 1000 + get_bits1(&s->gb);
2187
-                s->avctx->time_base.num*= get_bits(&s->gb, 7);
2188
-                if(s->avctx->time_base.num == 0){
2189
-                    av_log(s, AV_LOG_ERROR, "zero framerate\n");
2190
-                    return -1;
2191
-                }
2192
-                gcd= av_gcd(s->avctx->time_base.den, s->avctx->time_base.num);
2193
-                s->avctx->time_base.den /= gcd;
2194
-                s->avctx->time_base.num /= gcd;
2195
-            }else{
2196
-                s->avctx->time_base= (AVRational){1001, 30000};
2197
-            }
2198
-        }
2199
-
2200
-        if(s->custom_pcf){
2201
-            skip_bits(&s->gb, 2); //extended Temporal reference
2202
-        }
2203
-
2204
-        if (ufep) {
2205
-            if (s->umvplus) {
2206
-                if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
2207
-                    skip_bits1(&s->gb);
2208
-            }
2209
-            if(s->h263_slice_structured){
2210
-                if (get_bits1(&s->gb) != 0) {
2211
-                    av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
2212
-                }
2213
-                if (get_bits1(&s->gb) != 0) {
2214
-                    av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
2215
-                }
2216
-            }
2217
-        }
2218
-
2219
-        s->qscale = get_bits(&s->gb, 5);
2220
-    }
2221
-
2222
-    s->mb_width = (s->width  + 15) / 16;
2223
-    s->mb_height = (s->height  + 15) / 16;
2224
-    s->mb_num = s->mb_width * s->mb_height;
2225
-
2226
-    if (s->pb_frame) {
2227
-        skip_bits(&s->gb, 3); /* Temporal reference for B-pictures */
2228
-        if (s->custom_pcf)
2229
-            skip_bits(&s->gb, 2); //extended Temporal reference
2230
-        skip_bits(&s->gb, 2); /* Quantization information for B-pictures */
2231
-    }
2232
-
2233
-    /* PEI */
2234
-    while (get_bits1(&s->gb) != 0) {
2235
-        skip_bits(&s->gb, 8);
2236
-    }
2237
-
2238
-    if(s->h263_slice_structured){
2239
-        if (get_bits1(&s->gb) != 1) {
2240
-            av_log(s->avctx, AV_LOG_ERROR, "SEPB1 marker missing\n");
2241
-            return -1;
2242
-        }
2243
-
2244
-        ff_h263_decode_mba(s);
2245
-
2246
-        if (get_bits1(&s->gb) != 1) {
2247
-            av_log(s->avctx, AV_LOG_ERROR, "SEPB2 marker missing\n");
2248
-            return -1;
2249
-        }
2250
-    }
2251
-    s->f_code = 1;
2252
-
2253
-    if(s->h263_aic){
2254
-         s->y_dc_scale_table=
2255
-         s->c_dc_scale_table= ff_aic_dc_scale_table;
2256
-    }else{
2257
-        s->y_dc_scale_table=
2258
-        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
2259
-    }
2260
-
2261
-        ff_h263_show_pict_info(s);
2262
-    if (s->pict_type == FF_I_TYPE && s->codec_tag == AV_RL32("ZYGO")){
2263
-        int i,j;
2264
-        for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
2265
-        av_log(s->avctx, AV_LOG_DEBUG, "\n");
2266
-        for(i=0; i<13; i++){
2267
-            for(j=0; j<3; j++){
2268
-                int v= get_bits(&s->gb, 8);
2269
-                v |= get_sbits(&s->gb, 8)<<8;
2270
-                av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
2271
-            }
2272
-            av_log(s->avctx, AV_LOG_DEBUG, "\n");
2273
-        }
2274
-        for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
2275
-    }
2276
-
2277
-    return 0;
2278
-}
2279
-
2280
-
... ...
@@ -32,6 +32,8 @@
32 32
 extern const AVRational ff_h263_pixel_aspect[16];
33 33
 extern const uint8_t ff_h263_cbpy_tab[16][2];
34 34
 
35
+extern const uint8_t cbpc_b_tab[4][2];
36
+
35 37
 extern const uint8_t mvtab[33][2];
36 38
 
37 39
 extern const uint8_t ff_h263_intra_MCBPC_code[9];
... ...
@@ -39,6 +41,7 @@ extern const uint8_t ff_h263_intra_MCBPC_bits[9];
39 39
 
40 40
 extern const uint8_t ff_h263_inter_MCBPC_code[28];
41 41
 extern const uint8_t ff_h263_inter_MCBPC_bits[28];
42
+extern const uint8_t h263_mbtype_b_tab[15][2];
42 43
 
43 44
 extern VLC ff_h263_intra_MCBPC_vlc;
44 45
 extern VLC ff_h263_inter_MCBPC_vlc;
... ...
@@ -46,8 +49,66 @@ extern VLC ff_h263_cbpy_vlc;
46 46
 
47 47
 extern RLTable ff_h263_rl_inter;
48 48
 
49
+extern RLTable rl_intra_aic;
50
+
51
+extern const uint16_t h263_format[8][2];
52
+extern const uint8_t modified_quant_tab[2][32];
53
+extern uint16_t ff_mba_max[6];
54
+extern uint8_t ff_mba_length[7];
55
+
56
+extern uint8_t ff_h263_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
57
+
58
+
49 59
 int h263_decode_motion(MpegEncContext * s, int pred, int f_code);
50 60
 av_const int ff_h263_aspect_to_info(AVRational aspect);
61
+int ff_h263_decode_init(AVCodecContext *avctx);
62
+int ff_h263_decode_frame(AVCodecContext *avctx,
63
+                             void *data, int *data_size,
64
+                             AVPacket *avpkt);
65
+int ff_h263_decode_end(AVCodecContext *avctx);
66
+void h263_encode_mb(MpegEncContext *s,
67
+                    DCTELEM block[6][64],
68
+                    int motion_x, int motion_y);
69
+void h263_encode_picture_header(MpegEncContext *s, int picture_number);
70
+void h263_encode_gob_header(MpegEncContext * s, int mb_line);
71
+int16_t *h263_pred_motion(MpegEncContext * s, int block, int dir,
72
+                        int *px, int *py);
73
+void h263_encode_init(MpegEncContext *s);
74
+void h263_decode_init_vlc(MpegEncContext *s);
75
+int h263_decode_picture_header(MpegEncContext *s);
76
+int ff_h263_decode_gob_header(MpegEncContext *s);
77
+void ff_h263_update_motion_val(MpegEncContext * s);
78
+void ff_h263_loop_filter(MpegEncContext * s);
79
+void ff_set_qscale(MpegEncContext * s, int qscale);
80
+int ff_h263_decode_mba(MpegEncContext *s);
81
+void ff_h263_encode_mba(MpegEncContext *s);
82
+void ff_init_qscale_tab(MpegEncContext *s);
83
+int h263_pred_dc(MpegEncContext * s, int n, int16_t **dc_val_ptr);
84
+void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n);
85
+
86
+
87
+/**
88
+ * Prints picture info if FF_DEBUG_PICT_INFO is set.
89
+ */
90
+void ff_h263_show_pict_info(MpegEncContext *s);
91
+
92
+int ff_intel_h263_decode_picture_header(MpegEncContext *s);
93
+int ff_h263_decode_mb(MpegEncContext *s,
94
+                      DCTELEM block[6][64]);
95
+
96
+/**
97
+ * Returns the value of the 3bit "source format" syntax element.
98
+ * that represents some standard picture dimensions or indicates that
99
+ * width&height are explicitly stored later.
100
+ */
101
+int av_const h263_get_picture_format(int width, int height);
102
+
103
+void ff_clean_h263_qscales(MpegEncContext *s);
104
+int ff_h263_resync(MpegEncContext *s);
105
+const uint8_t *ff_h263_find_resync_marker(const uint8_t *p, const uint8_t *end);
106
+int ff_h263_get_gob_height(MpegEncContext *s);
107
+void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code);
108
+
51 109
 
52 110
 static inline int h263_get_motion_length(MpegEncContext * s, int val, int f_code){
53 111
     int l, bit_size, code;
... ...
@@ -175,4 +236,11 @@ static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64],
175 175
     }
176 176
     return cbp;
177 177
 }
178
+
179
+static inline void memsetw(short *tab, int val, int n)
180
+{
181
+    int i;
182
+    for(i=0;i<n;i++)
183
+        tab[i] = val;
184
+}
178 185
 #endif
... ...
@@ -57,7 +57,7 @@ const uint8_t ff_h263_inter_MCBPC_bits[28] = {
57 57
     11, 13, 13, 13,/* inter4Q*/
58 58
 };
59 59
 
60
-static const uint8_t h263_mbtype_b_tab[15][2] = {
60
+const uint8_t h263_mbtype_b_tab[15][2] = {
61 61
  {1, 1},
62 62
  {3, 3},
63 63
  {1, 5},
... ...
@@ -75,25 +75,7 @@ static const uint8_t h263_mbtype_b_tab[15][2] = {
75 75
  {1, 8},
76 76
 };
77 77
 
78
-static const int h263_mb_type_b_map[15]= {
79
-    MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
80
-    MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP,
81
-    MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT,
82
-                      MB_TYPE_L0                                 | MB_TYPE_16x16,
83
-                      MB_TYPE_L0   | MB_TYPE_CBP                 | MB_TYPE_16x16,
84
-                      MB_TYPE_L0   | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
85
-                      MB_TYPE_L1                                 | MB_TYPE_16x16,
86
-                      MB_TYPE_L1   | MB_TYPE_CBP                 | MB_TYPE_16x16,
87
-                      MB_TYPE_L1   | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
88
-                      MB_TYPE_L0L1                               | MB_TYPE_16x16,
89
-                      MB_TYPE_L0L1 | MB_TYPE_CBP                 | MB_TYPE_16x16,
90
-                      MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
91
-    0, //stuffing
92
-    MB_TYPE_INTRA4x4                | MB_TYPE_CBP,
93
-    MB_TYPE_INTRA4x4                | MB_TYPE_CBP | MB_TYPE_QUANT,
94
-};
95
-
96
-static const uint8_t cbpc_b_tab[4][2] = {
78
+const uint8_t cbpc_b_tab[4][2] = {
97 79
 {0, 1},
98 80
 {2, 2},
99 81
 {7, 3},
... ...
@@ -246,7 +228,7 @@ static const int8_t intra_level_aic[102] = {
246 246
  1,  1,  1,  1,  1,  1,
247 247
 };
248 248
 
249
-static RLTable rl_intra_aic = {
249
+RLTable rl_intra_aic = {
250 250
     102,
251 251
     58,
252 252
     intra_vlc_aic,
... ...
@@ -254,25 +236,7 @@ static RLTable rl_intra_aic = {
254 254
     intra_level_aic,
255 255
 };
256 256
 
257
-#if CONFIG_ENCODERS
258
-static const uint8_t wrong_run[102] = {
259
- 1,  2,  3,  5,  4, 10,  9,  8,
260
-11, 15, 17, 16, 23, 22, 21, 20,
261
-19, 18, 25, 24, 27, 26, 11,  7,
262
- 6,  1,  2, 13,  2,  2,  2,  2,
263
- 6, 12,  3,  9,  1,  3,  4,  3,
264
- 7,  4,  1,  1,  5,  5, 14,  6,
265
- 1,  7,  1,  8,  1,  1,  1,  1,
266
-10,  1,  1,  5,  9, 17, 25, 24,
267
-29, 33, 32, 41,  2, 23, 28, 31,
268
- 3, 22, 30,  4, 27, 40,  8, 26,
269
- 6, 39,  7, 38, 16, 37, 15, 10,
270
-11, 12, 13, 14,  1, 21, 20, 18,
271
-19,  2,  1, 34, 35, 36
272
-};
273
-#endif
274
-
275
-static const uint16_t h263_format[8][2] = {
257
+const uint16_t h263_format[8][2] = {
276 258
     { 0, 0 },
277 259
     { 128, 96 },
278 260
     { 176, 144 },
... ...
@@ -286,7 +250,7 @@ const uint8_t ff_aic_dc_scale_table[32]={
286 286
     0, 2, 4, 6, 8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62
287 287
 };
288 288
 
289
-static const uint8_t modified_quant_tab[2][32]={
289
+const uint8_t modified_quant_tab[2][32]={
290 290
 //  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
291 291
 {
292 292
     0, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9,10,11,12,13,14,15,16,17,18,18,19,20,21,22,23,24,25,26,27,28
... ...
@@ -300,11 +264,11 @@ const uint8_t ff_h263_chroma_qscale_table[32]={
300 300
     0, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 9,10,10,11,11,12,12,12,13,13,13,14,14,14,14,14,15,15,15,15,15
301 301
 };
302 302
 
303
-const uint16_t ff_mba_max[6]={
303
+uint16_t ff_mba_max[6]={
304 304
      47,  98, 395,1583,6335,9215
305 305
 };
306 306
 
307
-const uint8_t ff_mba_length[7]={
307
+uint8_t ff_mba_length[7]={
308 308
       6,   7,   9,  11,  13,  14,  14
309 309
 };
310 310
 
... ...
@@ -29,6 +29,7 @@
29 29
 #include "avcodec.h"
30 30
 #include "dsputil.h"
31 31
 #include "mpegvideo.h"
32
+#include "h263.h"
32 33
 #include "h263_parser.h"
33 34
 #include "mpeg4video_parser.h"
34 35
 #include "msmpeg4.h"
... ...
@@ -19,6 +19,7 @@
19 19
  */
20 20
 
21 21
 #include "mpegvideo.h"
22
+#include "h263.h"
22 23
 
23 24
 /* don't understand why they choose a different header ! */
24 25
 int ff_intel_h263_decode_picture_header(MpegEncContext *s)
25 26
new file mode 100644
... ...
@@ -0,0 +1,1131 @@
0
+/*
1
+ * ITU H263 bitstream decoder
2
+ * Copyright (c) 2000,2001 Fabrice Bellard
3
+ * H263+ support.
4
+ * Copyright (c) 2001 Juan J. Sierralta P
5
+ * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6
+ *
7
+ * This file is part of FFmpeg.
8
+ *
9
+ * FFmpeg is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU Lesser General Public
11
+ * License as published by the Free Software Foundation; either
12
+ * version 2.1 of the License, or (at your option) any later version.
13
+ *
14
+ * FFmpeg is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
+ * Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with FFmpeg; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ */
23
+
24
+/**
25
+ * @file libavcodec/ituh263dec.c
26
+ * h263 decoder.
27
+ */
28
+
29
+//#define DEBUG
30
+#include <limits.h>
31
+
32
+#include "dsputil.h"
33
+#include "avcodec.h"
34
+#include "mpegvideo.h"
35
+#include "h263.h"
36
+#include "mathops.h"
37
+#include "unary.h"
38
+#include "flv.h"
39
+#include "mpeg4video.h"
40
+
41
+//#undef NDEBUG
42
+//#include <assert.h>
43
+
44
+// The defines below define the number of bits that are read at once for
45
+// reading vlc values. Changing these may improve speed and data cache needs
46
+// be aware though that decreasing them may need the number of stages that is
47
+// passed to get_vlc* to be increased.
48
+#define MV_VLC_BITS 9
49
+#define H263_MBTYPE_B_VLC_BITS 6
50
+#define CBPC_B_VLC_BITS 3
51
+
52
+static const int h263_mb_type_b_map[15]= {
53
+    MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
54
+    MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP,
55
+    MB_TYPE_DIRECT2 | MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT,
56
+                      MB_TYPE_L0                                 | MB_TYPE_16x16,
57
+                      MB_TYPE_L0   | MB_TYPE_CBP                 | MB_TYPE_16x16,
58
+                      MB_TYPE_L0   | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
59
+                      MB_TYPE_L1                                 | MB_TYPE_16x16,
60
+                      MB_TYPE_L1   | MB_TYPE_CBP                 | MB_TYPE_16x16,
61
+                      MB_TYPE_L1   | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
62
+                      MB_TYPE_L0L1                               | MB_TYPE_16x16,
63
+                      MB_TYPE_L0L1 | MB_TYPE_CBP                 | MB_TYPE_16x16,
64
+                      MB_TYPE_L0L1 | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
65
+    0, //stuffing
66
+    MB_TYPE_INTRA4x4                | MB_TYPE_CBP,
67
+    MB_TYPE_INTRA4x4                | MB_TYPE_CBP | MB_TYPE_QUANT,
68
+};
69
+
70
+void ff_h263_show_pict_info(MpegEncContext *s){
71
+    if(s->avctx->debug&FF_DEBUG_PICT_INFO){
72
+    av_log(s->avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s%s%s%s%s %d/%d\n",
73
+         s->qscale, av_get_pict_type_char(s->pict_type),
74
+         s->gb.size_in_bits, 1-s->no_rounding,
75
+         s->obmc ? " AP" : "",
76
+         s->umvplus ? " UMV" : "",
77
+         s->h263_long_vectors ? " LONG" : "",
78
+         s->h263_plus ? " +" : "",
79
+         s->h263_aic ? " AIC" : "",
80
+         s->alt_inter_vlc ? " AIV" : "",
81
+         s->modified_quant ? " MQ" : "",
82
+         s->loop_filter ? " LOOP" : "",
83
+         s->h263_slice_structured ? " SS" : "",
84
+         s->avctx->time_base.den, s->avctx->time_base.num
85
+    );
86
+    }
87
+}
88
+
89
+/***********************************************/
90
+/* decoding */
91
+
92
+VLC ff_h263_intra_MCBPC_vlc;
93
+VLC ff_h263_inter_MCBPC_vlc;
94
+VLC ff_h263_cbpy_vlc;
95
+static VLC mv_vlc;
96
+static VLC h263_mbtype_b_vlc;
97
+static VLC cbpc_b_vlc;
98
+
99
+/* init vlcs */
100
+
101
+/* XXX: find a better solution to handle static init */
102
+void h263_decode_init_vlc(MpegEncContext *s)
103
+{
104
+    static int done = 0;
105
+
106
+    if (!done) {
107
+        done = 1;
108
+
109
+        INIT_VLC_STATIC(&ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
110
+                 ff_h263_intra_MCBPC_bits, 1, 1,
111
+                 ff_h263_intra_MCBPC_code, 1, 1, 72);
112
+        INIT_VLC_STATIC(&ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
113
+                 ff_h263_inter_MCBPC_bits, 1, 1,
114
+                 ff_h263_inter_MCBPC_code, 1, 1, 198);
115
+        INIT_VLC_STATIC(&ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
116
+                 &ff_h263_cbpy_tab[0][1], 2, 1,
117
+                 &ff_h263_cbpy_tab[0][0], 2, 1, 64);
118
+        INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 33,
119
+                 &mvtab[0][1], 2, 1,
120
+                 &mvtab[0][0], 2, 1, 538);
121
+        init_rl(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
122
+        init_rl(&rl_intra_aic, ff_h263_static_rl_table_store[1]);
123
+        INIT_VLC_RL(ff_h263_rl_inter, 554);
124
+        INIT_VLC_RL(rl_intra_aic, 554);
125
+        INIT_VLC_STATIC(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
126
+                 &h263_mbtype_b_tab[0][1], 2, 1,
127
+                 &h263_mbtype_b_tab[0][0], 2, 1, 80);
128
+        INIT_VLC_STATIC(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
129
+                 &cbpc_b_tab[0][1], 2, 1,
130
+                 &cbpc_b_tab[0][0], 2, 1, 8);
131
+    }
132
+}
133
+
134
+int ff_h263_decode_mba(MpegEncContext *s)
135
+{
136
+    int i, mb_pos;
137
+
138
+    for(i=0; i<6; i++){
139
+        if(s->mb_num-1 <= ff_mba_max[i]) break;
140
+    }
141
+    mb_pos= get_bits(&s->gb, ff_mba_length[i]);
142
+    s->mb_x= mb_pos % s->mb_width;
143
+    s->mb_y= mb_pos / s->mb_width;
144
+
145
+    return mb_pos;
146
+}
147
+
148
+/**
149
+ * decodes the group of blocks header or slice header.
150
+ * @return <0 if an error occurred
151
+ */
152
+static int h263_decode_gob_header(MpegEncContext *s)
153
+{
154
+    unsigned int val, gfid, gob_number;
155
+    int left;
156
+
157
+    /* Check for GOB Start Code */
158
+    val = show_bits(&s->gb, 16);
159
+    if(val)
160
+        return -1;
161
+
162
+        /* We have a GBSC probably with GSTUFF */
163
+    skip_bits(&s->gb, 16); /* Drop the zeros */
164
+    left= get_bits_left(&s->gb);
165
+    //MN: we must check the bits left or we might end in a infinite loop (or segfault)
166
+    for(;left>13; left--){
167
+        if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
168
+    }
169
+    if(left<=13)
170
+        return -1;
171
+
172
+    if(s->h263_slice_structured){
173
+        if(get_bits1(&s->gb)==0)
174
+            return -1;
175
+
176
+        ff_h263_decode_mba(s);
177
+
178
+        if(s->mb_num > 1583)
179
+            if(get_bits1(&s->gb)==0)
180
+                return -1;
181
+
182
+        s->qscale = get_bits(&s->gb, 5); /* SQUANT */
183
+        if(get_bits1(&s->gb)==0)
184
+            return -1;
185
+        gfid = get_bits(&s->gb, 2); /* GFID */
186
+    }else{
187
+        gob_number = get_bits(&s->gb, 5); /* GN */
188
+        s->mb_x= 0;
189
+        s->mb_y= s->gob_index* gob_number;
190
+        gfid = get_bits(&s->gb, 2); /* GFID */
191
+        s->qscale = get_bits(&s->gb, 5); /* GQUANT */
192
+    }
193
+
194
+    if(s->mb_y >= s->mb_height)
195
+        return -1;
196
+
197
+    if(s->qscale==0)
198
+        return -1;
199
+
200
+    return 0;
201
+}
202
+
203
+/**
204
+ * finds the next resync_marker
205
+ * @param p pointer to buffer to scan
206
+ * @param end pointer to the end of the buffer
207
+ * @return pointer to the next resync_marker, or end if none was found
208
+ */
209
+const uint8_t *ff_h263_find_resync_marker(const uint8_t *restrict p, const uint8_t * restrict end)
210
+{
211
+    assert(p < end);
212
+
213
+    end-=2;
214
+    p++;
215
+    for(;p<end; p+=2){
216
+        if(!*p){
217
+            if     (!p[-1] && p[1]) return p - 1;
218
+            else if(!p[ 1] && p[2]) return p;
219
+        }
220
+    }
221
+    return end+2;
222
+}
223
+
224
+/**
225
+ * decodes the group of blocks / video packet header.
226
+ * @return bit position of the resync_marker, or <0 if none was found
227
+ */
228
+int ff_h263_resync(MpegEncContext *s){
229
+    int left, pos, ret;
230
+
231
+    if(s->codec_id==CODEC_ID_MPEG4){
232
+        skip_bits1(&s->gb);
233
+        align_get_bits(&s->gb);
234
+    }
235
+
236
+    if(show_bits(&s->gb, 16)==0){
237
+        pos= get_bits_count(&s->gb);
238
+        if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4)
239
+            ret= mpeg4_decode_video_packet_header(s);
240
+        else
241
+            ret= h263_decode_gob_header(s);
242
+        if(ret>=0)
243
+            return pos;
244
+    }
245
+    //OK, it's not where it is supposed to be ...
246
+    s->gb= s->last_resync_gb;
247
+    align_get_bits(&s->gb);
248
+    left= get_bits_left(&s->gb);
249
+
250
+    for(;left>16+1+5+5; left-=8){
251
+        if(show_bits(&s->gb, 16)==0){
252
+            GetBitContext bak= s->gb;
253
+
254
+            pos= get_bits_count(&s->gb);
255
+            if(CONFIG_MPEG4_DECODER && s->codec_id==CODEC_ID_MPEG4)
256
+                ret= mpeg4_decode_video_packet_header(s);
257
+            else
258
+                ret= h263_decode_gob_header(s);
259
+            if(ret>=0)
260
+                return pos;
261
+
262
+            s->gb= bak;
263
+        }
264
+        skip_bits(&s->gb, 8);
265
+    }
266
+
267
+    return -1;
268
+}
269
+
270
+int h263_decode_motion(MpegEncContext * s, int pred, int f_code)
271
+{
272
+    int code, val, sign, shift, l;
273
+    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
274
+
275
+    if (code == 0)
276
+        return pred;
277
+    if (code < 0)
278
+        return 0xffff;
279
+
280
+    sign = get_bits1(&s->gb);
281
+    shift = f_code - 1;
282
+    val = code;
283
+    if (shift) {
284
+        val = (val - 1) << shift;
285
+        val |= get_bits(&s->gb, shift);
286
+        val++;
287
+    }
288
+    if (sign)
289
+        val = -val;
290
+    val += pred;
291
+
292
+    /* modulo decoding */
293
+    if (!s->h263_long_vectors) {
294
+        l = INT_BIT - 5 - f_code;
295
+        val = (val<<l)>>l;
296
+    } else {
297
+        /* horrible h263 long vector mode */
298
+        if (pred < -31 && val < -63)
299
+            val += 64;
300
+        if (pred > 32 && val > 63)
301
+            val -= 64;
302
+
303
+    }
304
+    return val;
305
+}
306
+
307
+
308
+/* Decodes RVLC of H.263+ UMV */
309
+static int h263p_decode_umotion(MpegEncContext * s, int pred)
310
+{
311
+   int code = 0, sign;
312
+
313
+   if (get_bits1(&s->gb)) /* Motion difference = 0 */
314
+      return pred;
315
+
316
+   code = 2 + get_bits1(&s->gb);
317
+
318
+   while (get_bits1(&s->gb))
319
+   {
320
+      code <<= 1;
321
+      code += get_bits1(&s->gb);
322
+   }
323
+   sign = code & 1;
324
+   code >>= 1;
325
+
326
+   code = (sign) ? (pred - code) : (pred + code);
327
+   dprintf(s->avctx,"H.263+ UMV Motion = %d\n", code);
328
+   return code;
329
+
330
+}
331
+
332
+/**
333
+ * read the next MVs for OBMC. yes this is a ugly hack, feel free to send a patch :)
334
+ */
335
+static void preview_obmc(MpegEncContext *s){
336
+    GetBitContext gb= s->gb;
337
+
338
+    int cbpc, i, pred_x, pred_y, mx, my;
339
+    int16_t *mot_val;
340
+    const int xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
341
+    const int stride= s->b8_stride*2;
342
+
343
+    for(i=0; i<4; i++)
344
+        s->block_index[i]+= 2;
345
+    for(i=4; i<6; i++)
346
+        s->block_index[i]+= 1;
347
+    s->mb_x++;
348
+
349
+    assert(s->pict_type == FF_P_TYPE);
350
+
351
+    do{
352
+        if (get_bits1(&s->gb)) {
353
+            /* skip mb */
354
+            mot_val = s->current_picture.motion_val[0][ s->block_index[0] ];
355
+            mot_val[0       ]= mot_val[2       ]=
356
+            mot_val[0+stride]= mot_val[2+stride]= 0;
357
+            mot_val[1       ]= mot_val[3       ]=
358
+            mot_val[1+stride]= mot_val[3+stride]= 0;
359
+
360
+            s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
361
+            goto end;
362
+        }
363
+        cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
364
+    }while(cbpc == 20);
365
+
366
+    if(cbpc & 4){
367
+        s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
368
+    }else{
369
+        get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
370
+        if (cbpc & 8) {
371
+            if(s->modified_quant){
372
+                if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
373
+                else                  skip_bits(&s->gb, 5);
374
+            }else
375
+                skip_bits(&s->gb, 2);
376
+        }
377
+
378
+        if ((cbpc & 16) == 0) {
379
+                s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
380
+                /* 16x16 motion prediction */
381
+                mot_val= h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
382
+                if (s->umvplus)
383
+                   mx = h263p_decode_umotion(s, pred_x);
384
+                else
385
+                   mx = h263_decode_motion(s, pred_x, 1);
386
+
387
+                if (s->umvplus)
388
+                   my = h263p_decode_umotion(s, pred_y);
389
+                else
390
+                   my = h263_decode_motion(s, pred_y, 1);
391
+
392
+                mot_val[0       ]= mot_val[2       ]=
393
+                mot_val[0+stride]= mot_val[2+stride]= mx;
394
+                mot_val[1       ]= mot_val[3       ]=
395
+                mot_val[1+stride]= mot_val[3+stride]= my;
396
+        } else {
397
+            s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
398
+            for(i=0;i<4;i++) {
399
+                mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
400
+                if (s->umvplus)
401
+                  mx = h263p_decode_umotion(s, pred_x);
402
+                else
403
+                  mx = h263_decode_motion(s, pred_x, 1);
404
+
405
+                if (s->umvplus)
406
+                  my = h263p_decode_umotion(s, pred_y);
407
+                else
408
+                  my = h263_decode_motion(s, pred_y, 1);
409
+                if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
410
+                  skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
411
+                mot_val[0] = mx;
412
+                mot_val[1] = my;
413
+            }
414
+        }
415
+    }
416
+end:
417
+
418
+    for(i=0; i<4; i++)
419
+        s->block_index[i]-= 2;
420
+    for(i=4; i<6; i++)
421
+        s->block_index[i]-= 1;
422
+    s->mb_x--;
423
+
424
+    s->gb= gb;
425
+}
426
+
427
+static void h263_decode_dquant(MpegEncContext *s){
428
+    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
429
+
430
+    if(s->modified_quant){
431
+        if(get_bits1(&s->gb))
432
+            s->qscale= modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
433
+        else
434
+            s->qscale= get_bits(&s->gb, 5);
435
+    }else
436
+        s->qscale += quant_tab[get_bits(&s->gb, 2)];
437
+    ff_set_qscale(s, s->qscale);
438
+}
439
+
440
+static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
441
+                             int n, int coded)
442
+{
443
+    int code, level, i, j, last, run;
444
+    RLTable *rl = &ff_h263_rl_inter;
445
+    const uint8_t *scan_table;
446
+    GetBitContext gb= s->gb;
447
+
448
+    scan_table = s->intra_scantable.permutated;
449
+    if (s->h263_aic && s->mb_intra) {
450
+        rl = &rl_intra_aic;
451
+        i = 0;
452
+        if (s->ac_pred) {
453
+            if (s->h263_aic_dir)
454
+                scan_table = s->intra_v_scantable.permutated; /* left */
455
+            else
456
+                scan_table = s->intra_h_scantable.permutated; /* top */
457
+        }
458
+    } else if (s->mb_intra) {
459
+        /* DC coef */
460
+        if(s->codec_id == CODEC_ID_RV10){
461
+#if CONFIG_RV10_DECODER
462
+          if (s->rv10_version == 3 && s->pict_type == FF_I_TYPE) {
463
+            int component, diff;
464
+            component = (n <= 3 ? 0 : n - 4 + 1);
465
+            level = s->last_dc[component];
466
+            if (s->rv10_first_dc_coded[component]) {
467
+                diff = rv_decode_dc(s, n);
468
+                if (diff == 0xffff)
469
+                    return -1;
470
+                level += diff;
471
+                level = level & 0xff; /* handle wrap round */
472
+                s->last_dc[component] = level;
473
+            } else {
474
+                s->rv10_first_dc_coded[component] = 1;
475
+            }
476
+          } else {
477
+                level = get_bits(&s->gb, 8);
478
+                if (level == 255)
479
+                    level = 128;
480
+          }
481
+#endif
482
+        }else{
483
+            level = get_bits(&s->gb, 8);
484
+            if((level&0x7F) == 0){
485
+                av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
486
+                if(s->error_recognition >= FF_ER_COMPLIANT)
487
+                    return -1;
488
+            }
489
+            if (level == 255)
490
+                level = 128;
491
+        }
492
+        block[0] = level;
493
+        i = 1;
494
+    } else {
495
+        i = 0;
496
+    }
497
+    if (!coded) {
498
+        if (s->mb_intra && s->h263_aic)
499
+            goto not_coded;
500
+        s->block_last_index[n] = i - 1;
501
+        return 0;
502
+    }
503
+retry:
504
+    for(;;) {
505
+        code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
506
+        if (code < 0){
507
+            av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
508
+            return -1;
509
+        }
510
+        if (code == rl->n) {
511
+            /* escape */
512
+            if (CONFIG_FLV_DECODER && s->h263_flv > 1) {
513
+                ff_flv2_decode_ac_esc(&s->gb, &level, &run, &last);
514
+            } else {
515
+                last = get_bits1(&s->gb);
516
+                run = get_bits(&s->gb, 6);
517
+                level = (int8_t)get_bits(&s->gb, 8);
518
+                if(level == -128){
519
+                    if (s->codec_id == CODEC_ID_RV10) {
520
+                        /* XXX: should patch encoder too */
521
+                        level = get_sbits(&s->gb, 12);
522
+                    }else{
523
+                        level = get_bits(&s->gb, 5);
524
+                        level |= get_sbits(&s->gb, 6)<<5;
525
+                    }
526
+                }
527
+            }
528
+        } else {
529
+            run = rl->table_run[code];
530
+            level = rl->table_level[code];
531
+            last = code >= rl->last;
532
+            if (get_bits1(&s->gb))
533
+                level = -level;
534
+        }
535
+        i += run;
536
+        if (i >= 64){
537
+            if(s->alt_inter_vlc && rl == &ff_h263_rl_inter && !s->mb_intra){
538
+                //Looks like a hack but no, it's the way it is supposed to work ...
539
+                rl = &rl_intra_aic;
540
+                i = 0;
541
+                s->gb= gb;
542
+                s->dsp.clear_block(block);
543
+                goto retry;
544
+            }
545
+            av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n", s->mb_x, s->mb_y, s->mb_intra);
546
+            return -1;
547
+        }
548
+        j = scan_table[i];
549
+        block[j] = level;
550
+        if (last)
551
+            break;
552
+        i++;
553
+    }
554
+not_coded:
555
+    if (s->mb_intra && s->h263_aic) {
556
+        h263_pred_acdc(s, block, n);
557
+        i = 63;
558
+    }
559
+    s->block_last_index[n] = i;
560
+    return 0;
561
+}
562
+
563
+static int h263_skip_b_part(MpegEncContext *s, int cbp)
564
+{
565
+    DECLARE_ALIGNED(16, DCTELEM, dblock[64]);
566
+    int i, mbi;
567
+
568
+    /* we have to set s->mb_intra to zero to decode B-part of PB-frame correctly
569
+     * but real value should be restored in order to be used later (in OBMC condition)
570
+     */
571
+    mbi = s->mb_intra;
572
+    s->mb_intra = 0;
573
+    for (i = 0; i < 6; i++) {
574
+        if (h263_decode_block(s, dblock, i, cbp&32) < 0)
575
+            return -1;
576
+        cbp+=cbp;
577
+    }
578
+    s->mb_intra = mbi;
579
+    return 0;
580
+}
581
+
582
+static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
583
+{
584
+    int c, mv = 1;
585
+
586
+    if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
587
+        c = get_bits1(gb);
588
+        if (pb_frame == 2 && c)
589
+            mv = !get_bits1(gb);
590
+    } else { // h.263 Annex M improved PB-frame
591
+        mv = get_unary(gb, 0, 4) + 1;
592
+        c = mv & 1;
593
+        mv = !!(mv & 2);
594
+    }
595
+    if(c)
596
+        *cbpb = get_bits(gb, 6);
597
+    return mv;
598
+}
599
+
600
+int ff_h263_decode_mb(MpegEncContext *s,
601
+                      DCTELEM block[6][64])
602
+{
603
+    int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
604
+    int16_t *mot_val;
605
+    const int xy= s->mb_x + s->mb_y * s->mb_stride;
606
+    int cbpb = 0, pb_mv_count = 0;
607
+
608
+    assert(!s->h263_pred);
609
+
610
+    if (s->pict_type == FF_P_TYPE) {
611
+        do{
612
+            if (get_bits1(&s->gb)) {
613
+                /* skip mb */
614
+                s->mb_intra = 0;
615
+                for(i=0;i<6;i++)
616
+                    s->block_last_index[i] = -1;
617
+                s->mv_dir = MV_DIR_FORWARD;
618
+                s->mv_type = MV_TYPE_16X16;
619
+                s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
620
+                s->mv[0][0][0] = 0;
621
+                s->mv[0][0][1] = 0;
622
+                s->mb_skipped = !(s->obmc | s->loop_filter);
623
+                goto end;
624
+            }
625
+            cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
626
+            if (cbpc < 0){
627
+                av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
628
+                return -1;
629
+            }
630
+        }while(cbpc == 20);
631
+
632
+        s->dsp.clear_blocks(s->block[0]);
633
+
634
+        dquant = cbpc & 8;
635
+        s->mb_intra = ((cbpc & 4) != 0);
636
+        if (s->mb_intra) goto intra;
637
+
638
+        if(s->pb_frame && get_bits1(&s->gb))
639
+            pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
640
+        cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
641
+
642
+        if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
643
+            cbpy ^= 0xF;
644
+
645
+        cbp = (cbpc & 3) | (cbpy << 2);
646
+        if (dquant) {
647
+            h263_decode_dquant(s);
648
+        }
649
+
650
+        s->mv_dir = MV_DIR_FORWARD;
651
+        if ((cbpc & 16) == 0) {
652
+            s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
653
+            /* 16x16 motion prediction */
654
+            s->mv_type = MV_TYPE_16X16;
655
+            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
656
+            if (s->umvplus)
657
+               mx = h263p_decode_umotion(s, pred_x);
658
+            else
659
+               mx = h263_decode_motion(s, pred_x, 1);
660
+
661
+            if (mx >= 0xffff)
662
+                return -1;
663
+
664
+            if (s->umvplus)
665
+               my = h263p_decode_umotion(s, pred_y);
666
+            else
667
+               my = h263_decode_motion(s, pred_y, 1);
668
+
669
+            if (my >= 0xffff)
670
+                return -1;
671
+            s->mv[0][0][0] = mx;
672
+            s->mv[0][0][1] = my;
673
+
674
+            if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
675
+               skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
676
+        } else {
677
+            s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
678
+            s->mv_type = MV_TYPE_8X8;
679
+            for(i=0;i<4;i++) {
680
+                mot_val = h263_pred_motion(s, i, 0, &pred_x, &pred_y);
681
+                if (s->umvplus)
682
+                  mx = h263p_decode_umotion(s, pred_x);
683
+                else
684
+                  mx = h263_decode_motion(s, pred_x, 1);
685
+                if (mx >= 0xffff)
686
+                    return -1;
687
+
688
+                if (s->umvplus)
689
+                  my = h263p_decode_umotion(s, pred_y);
690
+                else
691
+                  my = h263_decode_motion(s, pred_y, 1);
692
+                if (my >= 0xffff)
693
+                    return -1;
694
+                s->mv[0][i][0] = mx;
695
+                s->mv[0][i][1] = my;
696
+                if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
697
+                  skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
698
+                mot_val[0] = mx;
699
+                mot_val[1] = my;
700
+            }
701
+        }
702
+    } else if(s->pict_type==FF_B_TYPE) {
703
+        int mb_type;
704
+        const int stride= s->b8_stride;
705
+        int16_t *mot_val0 = s->current_picture.motion_val[0][ 2*(s->mb_x + s->mb_y*stride) ];
706
+        int16_t *mot_val1 = s->current_picture.motion_val[1][ 2*(s->mb_x + s->mb_y*stride) ];
707
+//        const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
708
+
709
+        //FIXME ugly
710
+        mot_val0[0       ]= mot_val0[2       ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
711
+        mot_val0[1       ]= mot_val0[3       ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
712
+        mot_val1[0       ]= mot_val1[2       ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
713
+        mot_val1[1       ]= mot_val1[3       ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
714
+
715
+        do{
716
+            mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
717
+            if (mb_type < 0){
718
+                av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
719
+                return -1;
720
+            }
721
+
722
+            mb_type= h263_mb_type_b_map[ mb_type ];
723
+        }while(!mb_type);
724
+
725
+        s->mb_intra = IS_INTRA(mb_type);
726
+        if(HAS_CBP(mb_type)){
727
+            s->dsp.clear_blocks(s->block[0]);
728
+            cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
729
+            if(s->mb_intra){
730
+                dquant = IS_QUANT(mb_type);
731
+                goto intra;
732
+            }
733
+
734
+            cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
735
+
736
+            if (cbpy < 0){
737
+                av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
738
+                return -1;
739
+            }
740
+
741
+            if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
742
+                cbpy ^= 0xF;
743
+
744
+            cbp = (cbpc & 3) | (cbpy << 2);
745
+        }else
746
+            cbp=0;
747
+
748
+        assert(!s->mb_intra);
749
+
750
+        if(IS_QUANT(mb_type)){
751
+            h263_decode_dquant(s);
752
+        }
753
+
754
+        if(IS_DIRECT(mb_type)){
755
+            s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
756
+            mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
757
+        }else{
758
+            s->mv_dir = 0;
759
+            s->mv_type= MV_TYPE_16X16;
760
+//FIXME UMV
761
+
762
+            if(USES_LIST(mb_type, 0)){
763
+                int16_t *mot_val= h263_pred_motion(s, 0, 0, &mx, &my);
764
+                s->mv_dir = MV_DIR_FORWARD;
765
+
766
+                mx = h263_decode_motion(s, mx, 1);
767
+                my = h263_decode_motion(s, my, 1);
768
+
769
+                s->mv[0][0][0] = mx;
770
+                s->mv[0][0][1] = my;
771
+                mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
772
+                mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
773
+            }
774
+
775
+            if(USES_LIST(mb_type, 1)){
776
+                int16_t *mot_val= h263_pred_motion(s, 0, 1, &mx, &my);
777
+                s->mv_dir |= MV_DIR_BACKWARD;
778
+
779
+                mx = h263_decode_motion(s, mx, 1);
780
+                my = h263_decode_motion(s, my, 1);
781
+
782
+                s->mv[1][0][0] = mx;
783
+                s->mv[1][0][1] = my;
784
+                mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
785
+                mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
786
+            }
787
+        }
788
+
789
+        s->current_picture.mb_type[xy]= mb_type;
790
+    } else { /* I-Frame */
791
+        do{
792
+            cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
793
+            if (cbpc < 0){
794
+                av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
795
+                return -1;
796
+            }
797
+        }while(cbpc == 8);
798
+
799
+        s->dsp.clear_blocks(s->block[0]);
800
+
801
+        dquant = cbpc & 4;
802
+        s->mb_intra = 1;
803
+intra:
804
+        s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
805
+        if (s->h263_aic) {
806
+            s->ac_pred = get_bits1(&s->gb);
807
+            if(s->ac_pred){
808
+                s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED;
809
+
810
+                s->h263_aic_dir = get_bits1(&s->gb);
811
+            }
812
+        }else
813
+            s->ac_pred = 0;
814
+
815
+        if(s->pb_frame && get_bits1(&s->gb))
816
+            pb_mv_count = h263_get_modb(&s->gb, s->pb_frame, &cbpb);
817
+        cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
818
+        if(cbpy<0){
819
+            av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
820
+            return -1;
821
+        }
822
+        cbp = (cbpc & 3) | (cbpy << 2);
823
+        if (dquant) {
824
+            h263_decode_dquant(s);
825
+        }
826
+
827
+        pb_mv_count += !!s->pb_frame;
828
+    }
829
+
830
+    while(pb_mv_count--){
831
+        h263_decode_motion(s, 0, 1);
832
+        h263_decode_motion(s, 0, 1);
833
+    }
834
+
835
+    /* decode each block */
836
+    for (i = 0; i < 6; i++) {
837
+        if (h263_decode_block(s, block[i], i, cbp&32) < 0)
838
+            return -1;
839
+        cbp+=cbp;
840
+    }
841
+
842
+    if(s->pb_frame && h263_skip_b_part(s, cbpb) < 0)
843
+        return -1;
844
+    if(s->obmc && !s->mb_intra){
845
+        if(s->pict_type == FF_P_TYPE && s->mb_x+1<s->mb_width && s->mb_num_left != 1)
846
+            preview_obmc(s);
847
+    }
848
+end:
849
+
850
+        /* per-MB end of slice check */
851
+    {
852
+        int v= show_bits(&s->gb, 16);
853
+
854
+        if(get_bits_count(&s->gb) + 16 > s->gb.size_in_bits){
855
+            v>>= get_bits_count(&s->gb) + 16 - s->gb.size_in_bits;
856
+        }
857
+
858
+        if(v==0)
859
+            return SLICE_END;
860
+    }
861
+
862
+    return SLICE_OK;
863
+}
864
+
865
+/* most is hardcoded. should extend to handle all h263 streams */
866
+int h263_decode_picture_header(MpegEncContext *s)
867
+{
868
+    int format, width, height, i;
869
+    uint32_t startcode;
870
+
871
+    align_get_bits(&s->gb);
872
+
873
+    startcode= get_bits(&s->gb, 22-8);
874
+
875
+    for(i= get_bits_left(&s->gb); i>24; i-=8) {
876
+        startcode = ((startcode << 8) | get_bits(&s->gb, 8)) & 0x003FFFFF;
877
+
878
+        if(startcode == 0x20)
879
+            break;
880
+    }
881
+
882
+    if (startcode != 0x20) {
883
+        av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
884
+        return -1;
885
+    }
886
+    /* temporal reference */
887
+    i = get_bits(&s->gb, 8); /* picture timestamp */
888
+    if( (s->picture_number&~0xFF)+i < s->picture_number)
889
+        i+= 256;
890
+    s->current_picture_ptr->pts=
891
+    s->picture_number= (s->picture_number&~0xFF) + i;
892
+
893
+    /* PTYPE starts here */
894
+    if (get_bits1(&s->gb) != 1) {
895
+        /* marker */
896
+        av_log(s->avctx, AV_LOG_ERROR, "Bad marker\n");
897
+        return -1;
898
+    }
899
+    if (get_bits1(&s->gb) != 0) {
900
+        av_log(s->avctx, AV_LOG_ERROR, "Bad H263 id\n");
901
+        return -1;      /* h263 id */
902
+    }
903
+    skip_bits1(&s->gb);         /* split screen off */
904
+    skip_bits1(&s->gb);         /* camera  off */
905
+    skip_bits1(&s->gb);         /* freeze picture release off */
906
+
907
+    format = get_bits(&s->gb, 3);
908
+    /*
909
+        0    forbidden
910
+        1    sub-QCIF
911
+        10   QCIF
912
+        7       extended PTYPE (PLUSPTYPE)
913
+    */
914
+
915
+    if (format != 7 && format != 6) {
916
+        s->h263_plus = 0;
917
+        /* H.263v1 */
918
+        width = h263_format[format][0];
919
+        height = h263_format[format][1];
920
+        if (!width)
921
+            return -1;
922
+
923
+        s->pict_type = FF_I_TYPE + get_bits1(&s->gb);
924
+
925
+        s->h263_long_vectors = get_bits1(&s->gb);
926
+
927
+        if (get_bits1(&s->gb) != 0) {
928
+            av_log(s->avctx, AV_LOG_ERROR, "H263 SAC not supported\n");
929
+            return -1; /* SAC: off */
930
+        }
931
+        s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
932
+        s->unrestricted_mv = s->h263_long_vectors || s->obmc;
933
+
934
+        s->pb_frame = get_bits1(&s->gb);
935
+        s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
936
+        skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
937
+
938
+        s->width = width;
939
+        s->height = height;
940
+        s->avctx->sample_aspect_ratio= (AVRational){12,11};
941
+        s->avctx->time_base= (AVRational){1001, 30000};
942
+    } else {
943
+        int ufep;
944
+
945
+        /* H.263v2 */
946
+        s->h263_plus = 1;
947
+        ufep = get_bits(&s->gb, 3); /* Update Full Extended PTYPE */
948
+
949
+        /* ufep other than 0 and 1 are reserved */
950
+        if (ufep == 1) {
951
+            /* OPPTYPE */
952
+            format = get_bits(&s->gb, 3);
953
+            dprintf(s->avctx, "ufep=1, format: %d\n", format);
954
+            s->custom_pcf= get_bits1(&s->gb);
955
+            s->umvplus = get_bits1(&s->gb); /* Unrestricted Motion Vector */
956
+            if (get_bits1(&s->gb) != 0) {
957
+                av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
958
+            }
959
+            s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
960
+            s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
961
+            s->loop_filter= get_bits1(&s->gb);
962
+            s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
963
+
964
+            s->h263_slice_structured= get_bits1(&s->gb);
965
+            if (get_bits1(&s->gb) != 0) {
966
+                av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
967
+            }
968
+            if (get_bits1(&s->gb) != 0) {
969
+                av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
970
+            }
971
+            s->alt_inter_vlc= get_bits1(&s->gb);
972
+            s->modified_quant= get_bits1(&s->gb);
973
+            if(s->modified_quant)
974
+                s->chroma_qscale_table= ff_h263_chroma_qscale_table;
975
+
976
+            skip_bits(&s->gb, 1); /* Prevent start code emulation */
977
+
978
+            skip_bits(&s->gb, 3); /* Reserved */
979
+        } else if (ufep != 0) {
980
+            av_log(s->avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
981
+            return -1;
982
+        }
983
+
984
+        /* MPPTYPE */
985
+        s->pict_type = get_bits(&s->gb, 3);
986
+        switch(s->pict_type){
987
+        case 0: s->pict_type= FF_I_TYPE;break;
988
+        case 1: s->pict_type= FF_P_TYPE;break;
989
+        case 2: s->pict_type= FF_P_TYPE;s->pb_frame = 3;break;
990
+        case 3: s->pict_type= FF_B_TYPE;break;
991
+        case 7: s->pict_type= FF_I_TYPE;break; //ZYGO
992
+        default:
993
+            return -1;
994
+        }
995
+        skip_bits(&s->gb, 2);
996
+        s->no_rounding = get_bits1(&s->gb);
997
+        skip_bits(&s->gb, 4);
998
+
999
+        /* Get the picture dimensions */
1000
+        if (ufep) {
1001
+            if (format == 6) {
1002
+                /* Custom Picture Format (CPFMT) */
1003
+                s->aspect_ratio_info = get_bits(&s->gb, 4);
1004
+                dprintf(s->avctx, "aspect: %d\n", s->aspect_ratio_info);
1005
+                /* aspect ratios:
1006
+                0 - forbidden
1007
+                1 - 1:1
1008
+                2 - 12:11 (CIF 4:3)
1009
+                3 - 10:11 (525-type 4:3)
1010
+                4 - 16:11 (CIF 16:9)
1011
+                5 - 40:33 (525-type 16:9)
1012
+                6-14 - reserved
1013
+                */
1014
+                width = (get_bits(&s->gb, 9) + 1) * 4;
1015
+                skip_bits1(&s->gb);
1016
+                height = get_bits(&s->gb, 9) * 4;
1017
+                dprintf(s->avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
1018
+                if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
1019
+                    /* aspected dimensions */
1020
+                    s->avctx->sample_aspect_ratio.num= get_bits(&s->gb, 8);
1021
+                    s->avctx->sample_aspect_ratio.den= get_bits(&s->gb, 8);
1022
+                }else{
1023
+                    s->avctx->sample_aspect_ratio= ff_h263_pixel_aspect[s->aspect_ratio_info];
1024
+                }
1025
+            } else {
1026
+                width = h263_format[format][0];
1027
+                height = h263_format[format][1];
1028
+                s->avctx->sample_aspect_ratio= (AVRational){12,11};
1029
+            }
1030
+            if ((width == 0) || (height == 0))
1031
+                return -1;
1032
+            s->width = width;
1033
+            s->height = height;
1034
+
1035
+            if(s->custom_pcf){
1036
+                int gcd;
1037
+                s->avctx->time_base.den= 1800000;
1038
+                s->avctx->time_base.num= 1000 + get_bits1(&s->gb);
1039
+                s->avctx->time_base.num*= get_bits(&s->gb, 7);
1040
+                if(s->avctx->time_base.num == 0){
1041
+                    av_log(s, AV_LOG_ERROR, "zero framerate\n");
1042
+                    return -1;
1043
+                }
1044
+                gcd= av_gcd(s->avctx->time_base.den, s->avctx->time_base.num);
1045
+                s->avctx->time_base.den /= gcd;
1046
+                s->avctx->time_base.num /= gcd;
1047
+            }else{
1048
+                s->avctx->time_base= (AVRational){1001, 30000};
1049
+            }
1050
+        }
1051
+
1052
+        if(s->custom_pcf){
1053
+            skip_bits(&s->gb, 2); //extended Temporal reference
1054
+        }
1055
+
1056
+        if (ufep) {
1057
+            if (s->umvplus) {
1058
+                if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
1059
+                    skip_bits1(&s->gb);
1060
+            }
1061
+            if(s->h263_slice_structured){
1062
+                if (get_bits1(&s->gb) != 0) {
1063
+                    av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
1064
+                }
1065
+                if (get_bits1(&s->gb) != 0) {
1066
+                    av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
1067
+                }
1068
+            }
1069
+        }
1070
+
1071
+        s->qscale = get_bits(&s->gb, 5);
1072
+    }
1073
+
1074
+    s->mb_width = (s->width  + 15) / 16;
1075
+    s->mb_height = (s->height  + 15) / 16;
1076
+    s->mb_num = s->mb_width * s->mb_height;
1077
+
1078
+    if (s->pb_frame) {
1079
+        skip_bits(&s->gb, 3); /* Temporal reference for B-pictures */
1080
+        if (s->custom_pcf)
1081
+            skip_bits(&s->gb, 2); //extended Temporal reference
1082
+        skip_bits(&s->gb, 2); /* Quantization information for B-pictures */
1083
+    }
1084
+
1085
+    /* PEI */
1086
+    while (get_bits1(&s->gb) != 0) {
1087
+        skip_bits(&s->gb, 8);
1088
+    }
1089
+
1090
+    if(s->h263_slice_structured){
1091
+        if (get_bits1(&s->gb) != 1) {
1092
+            av_log(s->avctx, AV_LOG_ERROR, "SEPB1 marker missing\n");
1093
+            return -1;
1094
+        }
1095
+
1096
+        ff_h263_decode_mba(s);
1097
+
1098
+        if (get_bits1(&s->gb) != 1) {
1099
+            av_log(s->avctx, AV_LOG_ERROR, "SEPB2 marker missing\n");
1100
+            return -1;
1101
+        }
1102
+    }
1103
+    s->f_code = 1;
1104
+
1105
+    if(s->h263_aic){
1106
+         s->y_dc_scale_table=
1107
+         s->c_dc_scale_table= ff_aic_dc_scale_table;
1108
+    }else{
1109
+        s->y_dc_scale_table=
1110
+        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1111
+    }
1112
+
1113
+        ff_h263_show_pict_info(s);
1114
+    if (s->pict_type == FF_I_TYPE && s->codec_tag == AV_RL32("ZYGO")){
1115
+        int i,j;
1116
+        for(i=0; i<85; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
1117
+        av_log(s->avctx, AV_LOG_DEBUG, "\n");
1118
+        for(i=0; i<13; i++){
1119
+            for(j=0; j<3; j++){
1120
+                int v= get_bits(&s->gb, 8);
1121
+                v |= get_sbits(&s->gb, 8)<<8;
1122
+                av_log(s->avctx, AV_LOG_DEBUG, " %5d", v);
1123
+            }
1124
+            av_log(s->avctx, AV_LOG_DEBUG, "\n");
1125
+        }
1126
+        for(i=0; i<50; i++) av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
1127
+    }
1128
+
1129
+    return 0;
1130
+}
0 1131
new file mode 100644
... ...
@@ -0,0 +1,857 @@
0
+/*
1
+ * ITU H263 bitstream encoder
2
+ * Copyright (c) 2000,2001 Fabrice Bellard
3
+ * H263+ support.
4
+ * Copyright (c) 2001 Juan J. Sierralta P
5
+ * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6
+ *
7
+ * This file is part of FFmpeg.
8
+ *
9
+ * FFmpeg is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU Lesser General Public
11
+ * License as published by the Free Software Foundation; either
12
+ * version 2.1 of the License, or (at your option) any later version.
13
+ *
14
+ * FFmpeg is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
+ * Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with FFmpeg; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ */
23
+
24
+/**
25
+ * @file libavcodec/ituh263enc.c
26
+ * h263 encoder.
27
+ */
28
+
29
+//#define DEBUG
30
+#include <limits.h>
31
+
32
+#include "dsputil.h"
33
+#include "avcodec.h"
34
+#include "mpegvideo.h"
35
+#include "h263.h"
36
+#include "mathops.h"
37
+#include "unary.h"
38
+#include "flv.h"
39
+#include "mpeg4video.h"
40
+
41
+//#undef NDEBUG
42
+//#include <assert.h>
43
+
44
+/**
45
+ * Table of number of bits a motion vector component needs.
46
+ */
47
+static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
48
+
49
+/**
50
+ * Minimal fcode that a motion vector component would need.
51
+ */
52
+static uint8_t fcode_tab[MAX_MV*2+1];
53
+
54
+/**
55
+ * Minimal fcode that a motion vector component would need in umv.
56
+ * All entries in this table are 1.
57
+ */
58
+static uint8_t umv_fcode_tab[MAX_MV*2+1];
59
+
60
+//unified encoding tables for run length encoding of coefficients
61
+//unified in the sense that the specification specifies the encoding in several steps.
62
+static uint8_t  uni_h263_intra_aic_rl_len [64*64*2*2];
63
+static uint8_t  uni_h263_inter_rl_len [64*64*2*2];
64
+//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
65
+//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
66
+#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
67
+
68
+static const uint8_t wrong_run[102] = {
69
+ 1,  2,  3,  5,  4, 10,  9,  8,
70
+11, 15, 17, 16, 23, 22, 21, 20,
71
+19, 18, 25, 24, 27, 26, 11,  7,
72
+ 6,  1,  2, 13,  2,  2,  2,  2,
73
+ 6, 12,  3,  9,  1,  3,  4,  3,
74
+ 7,  4,  1,  1,  5,  5, 14,  6,
75
+ 1,  7,  1,  8,  1,  1,  1,  1,
76
+10,  1,  1,  5,  9, 17, 25, 24,
77
+29, 33, 32, 41,  2, 23, 28, 31,
78
+ 3, 22, 30,  4, 27, 40,  8, 26,
79
+ 6, 39,  7, 38, 16, 37, 15, 10,
80
+11, 12, 13, 14,  1, 21, 20, 18,
81
+19,  2,  1, 34, 35, 36
82
+};
83
+
84
+int h263_get_picture_format(int width, int height)
85
+{
86
+    if (width == 128 && height == 96)
87
+        return 1;
88
+    else if (width == 176 && height == 144)
89
+        return 2;
90
+    else if (width == 352 && height == 288)
91
+        return 3;
92
+    else if (width == 704 && height == 576)
93
+        return 4;
94
+    else if (width == 1408 && height == 1152)
95
+        return 5;
96
+    else
97
+        return 7;
98
+}
99
+
100
+/**
101
+ * Returns the 4 bit value that specifies the given aspect ratio.
102
+ * This may be one of the standard aspect ratios or it specifies
103
+ * that the aspect will be stored explicitly later.
104
+ */
105
+av_const int ff_h263_aspect_to_info(AVRational aspect){
106
+    int i;
107
+
108
+    if(aspect.num==0) aspect= (AVRational){1,1};
109
+
110
+    for(i=1; i<6; i++){
111
+        if(av_cmp_q(ff_h263_pixel_aspect[i], aspect) == 0){
112
+            return i;
113
+        }
114
+    }
115
+
116
+    return FF_ASPECT_EXTENDED;
117
+}
118
+
119
+void h263_encode_picture_header(MpegEncContext * s, int picture_number)
120
+{
121
+    int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
122
+    int best_clock_code=1;
123
+    int best_divisor=60;
124
+    int best_error= INT_MAX;
125
+
126
+    if(s->h263_plus){
127
+        for(i=0; i<2; i++){
128
+            int div, error;
129
+            div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den);
130
+            div= av_clip(div, 1, 127);
131
+            error= FFABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div);
132
+            if(error < best_error){
133
+                best_error= error;
134
+                best_divisor= div;
135
+                best_clock_code= i;
136
+            }
137
+        }
138
+    }
139
+    s->custom_pcf= best_clock_code!=1 || best_divisor!=60;
140
+    coded_frame_rate= 1800000;
141
+    coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
142
+
143
+    align_put_bits(&s->pb);
144
+
145
+    /* Update the pointer to last GOB */
146
+    s->ptr_lastgob = put_bits_ptr(&s->pb);
147
+    put_bits(&s->pb, 22, 0x20); /* PSC */
148
+    temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->time_base.num / //FIXME use timestamp
149
+                         (coded_frame_rate_base * (int64_t)s->avctx->time_base.den);
150
+    put_sbits(&s->pb, 8, temp_ref); /* TemporalReference */
151
+
152
+    put_bits(&s->pb, 1, 1);     /* marker */
153
+    put_bits(&s->pb, 1, 0);     /* h263 id */
154
+    put_bits(&s->pb, 1, 0);     /* split screen off */
155
+    put_bits(&s->pb, 1, 0);     /* camera  off */
156
+    put_bits(&s->pb, 1, 0);     /* freeze picture release off */
157
+
158
+    format = h263_get_picture_format(s->width, s->height);
159
+    if (!s->h263_plus) {
160
+        /* H.263v1 */
161
+        put_bits(&s->pb, 3, format);
162
+        put_bits(&s->pb, 1, (s->pict_type == FF_P_TYPE));
163
+        /* By now UMV IS DISABLED ON H.263v1, since the restrictions
164
+        of H.263v1 UMV implies to check the predicted MV after
165
+        calculation of the current MB to see if we're on the limits */
166
+        put_bits(&s->pb, 1, 0);         /* Unrestricted Motion Vector: off */
167
+        put_bits(&s->pb, 1, 0);         /* SAC: off */
168
+        put_bits(&s->pb, 1, s->obmc);   /* Advanced Prediction */
169
+        put_bits(&s->pb, 1, 0);         /* only I/P frames, no PB frame */
170
+        put_bits(&s->pb, 5, s->qscale);
171
+        put_bits(&s->pb, 1, 0);         /* Continuous Presence Multipoint mode: off */
172
+    } else {
173
+        int ufep=1;
174
+        /* H.263v2 */
175
+        /* H.263 Plus PTYPE */
176
+
177
+        put_bits(&s->pb, 3, 7);
178
+        put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */
179
+        if (format == 7)
180
+            put_bits(&s->pb,3,6); /* Custom Source Format */
181
+        else
182
+            put_bits(&s->pb, 3, format);
183
+
184
+        put_bits(&s->pb,1, s->custom_pcf);
185
+        put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
186
+        put_bits(&s->pb,1,0); /* SAC: off */
187
+        put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
188
+        put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
189
+        put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
190
+        put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
191
+        put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
192
+        put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
193
+        put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
194
+        put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
195
+        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
196
+        put_bits(&s->pb,3,0); /* Reserved */
197
+
198
+        put_bits(&s->pb, 3, s->pict_type == FF_P_TYPE);
199
+
200
+        put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
201
+        put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
202
+        put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
203
+        put_bits(&s->pb,2,0); /* Reserved */
204
+        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
205
+
206
+        /* This should be here if PLUSPTYPE */
207
+        put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
208
+
209
+                if (format == 7) {
210
+            /* Custom Picture Format (CPFMT) */
211
+            s->aspect_ratio_info= ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
212
+
213
+            put_bits(&s->pb,4,s->aspect_ratio_info);
214
+            put_bits(&s->pb,9,(s->width >> 2) - 1);
215
+            put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
216
+            put_bits(&s->pb,9,(s->height >> 2));
217
+            if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
218
+                put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
219
+                put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
220
+            }
221
+        }
222
+        if(s->custom_pcf){
223
+            if(ufep){
224
+                put_bits(&s->pb, 1, best_clock_code);
225
+                put_bits(&s->pb, 7, best_divisor);
226
+            }
227
+            put_sbits(&s->pb, 2, temp_ref>>8);
228
+        }
229
+
230
+        /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
231
+        if (s->umvplus)
232
+//            put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
233
+//FIXME check actual requested range
234
+            put_bits(&s->pb,2,1); /* unlimited */
235
+        if(s->h263_slice_structured)
236
+            put_bits(&s->pb,2,0); /* no weird submodes */
237
+
238
+        put_bits(&s->pb, 5, s->qscale);
239
+    }
240
+
241
+    put_bits(&s->pb, 1, 0);     /* no PEI */
242
+
243
+    if(s->h263_slice_structured){
244
+        put_bits(&s->pb, 1, 1);
245
+
246
+        assert(s->mb_x == 0 && s->mb_y == 0);
247
+        ff_h263_encode_mba(s);
248
+
249
+        put_bits(&s->pb, 1, 1);
250
+    }
251
+
252
+    if(s->h263_aic){
253
+         s->y_dc_scale_table=
254
+         s->c_dc_scale_table= ff_aic_dc_scale_table;
255
+    }else{
256
+        s->y_dc_scale_table=
257
+        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
258
+    }
259
+}
260
+
261
+/**
262
+ * Encodes a group of blocks header.
263
+ */
264
+void h263_encode_gob_header(MpegEncContext * s, int mb_line)
265
+{
266
+    put_bits(&s->pb, 17, 1); /* GBSC */
267
+
268
+    if(s->h263_slice_structured){
269
+        put_bits(&s->pb, 1, 1);
270
+
271
+        ff_h263_encode_mba(s);
272
+
273
+        if(s->mb_num > 1583)
274
+            put_bits(&s->pb, 1, 1);
275
+        put_bits(&s->pb, 5, s->qscale); /* GQUANT */
276
+        put_bits(&s->pb, 1, 1);
277
+        put_bits(&s->pb, 2, s->pict_type == FF_I_TYPE); /* GFID */
278
+    }else{
279
+        int gob_number= mb_line / s->gob_index;
280
+
281
+        put_bits(&s->pb, 5, gob_number); /* GN */
282
+        put_bits(&s->pb, 2, s->pict_type == FF_I_TYPE); /* GFID */
283
+        put_bits(&s->pb, 5, s->qscale); /* GQUANT */
284
+    }
285
+}
286
+
287
+/**
288
+ * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2)
289
+ */
290
+void ff_clean_h263_qscales(MpegEncContext *s){
291
+    int i;
292
+    int8_t * const qscale_table= s->current_picture.qscale_table;
293
+
294
+    ff_init_qscale_tab(s);
295
+
296
+    for(i=1; i<s->mb_num; i++){
297
+        if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
298
+            qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
299
+    }
300
+    for(i=s->mb_num-2; i>=0; i--){
301
+        if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
302
+            qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
303
+    }
304
+
305
+    if(s->codec_id != CODEC_ID_H263P){
306
+        for(i=1; i<s->mb_num; i++){
307
+            int mb_xy= s->mb_index2xy[i];
308
+
309
+            if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){
310
+                s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
311
+            }
312
+        }
313
+    }
314
+}
315
+
316
+static const int dquant_code[5]= {1,0,9,2,3};
317
+
318
+/**
319
+ * encodes a 8x8 block.
320
+ * @param block the 8x8 block
321
+ * @param n block index (0-3 are luma, 4-5 are chroma)
322
+ */
323
+static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
324
+{
325
+    int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
326
+    RLTable *rl;
327
+
328
+    rl = &ff_h263_rl_inter;
329
+    if (s->mb_intra && !s->h263_aic) {
330
+        /* DC coef */
331
+        level = block[0];
332
+        /* 255 cannot be represented, so we clamp */
333
+        if (level > 254) {
334
+            level = 254;
335
+            block[0] = 254;
336
+        }
337
+        /* 0 cannot be represented also */
338
+        else if (level < 1) {
339
+            level = 1;
340
+            block[0] = 1;
341
+        }
342
+        if (level == 128) //FIXME check rv10
343
+            put_bits(&s->pb, 8, 0xff);
344
+        else
345
+            put_bits(&s->pb, 8, level);
346
+        i = 1;
347
+    } else {
348
+        i = 0;
349
+        if (s->h263_aic && s->mb_intra)
350
+            rl = &rl_intra_aic;
351
+
352
+        if(s->alt_inter_vlc && !s->mb_intra){
353
+            int aic_vlc_bits=0;
354
+            int inter_vlc_bits=0;
355
+            int wrong_pos=-1;
356
+            int aic_code;
357
+
358
+            last_index = s->block_last_index[n];
359
+            last_non_zero = i - 1;
360
+            for (; i <= last_index; i++) {
361
+                j = s->intra_scantable.permutated[i];
362
+                level = block[j];
363
+                if (level) {
364
+                    run = i - last_non_zero - 1;
365
+                    last = (i == last_index);
366
+
367
+                    if(level<0) level= -level;
368
+
369
+                    code = get_rl_index(rl, last, run, level);
370
+                    aic_code = get_rl_index(&rl_intra_aic, last, run, level);
371
+                    inter_vlc_bits += rl->table_vlc[code][1]+1;
372
+                    aic_vlc_bits   += rl_intra_aic.table_vlc[aic_code][1]+1;
373
+
374
+                    if (code == rl->n) {
375
+                        inter_vlc_bits += 1+6+8-1;
376
+                    }
377
+                    if (aic_code == rl_intra_aic.n) {
378
+                        aic_vlc_bits += 1+6+8-1;
379
+                        wrong_pos += run + 1;
380
+                    }else
381
+                        wrong_pos += wrong_run[aic_code];
382
+                    last_non_zero = i;
383
+                }
384
+            }
385
+            i = 0;
386
+            if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
387
+                rl = &rl_intra_aic;
388
+        }
389
+    }
390
+
391
+    /* AC coefs */
392
+    last_index = s->block_last_index[n];
393
+    last_non_zero = i - 1;
394
+    for (; i <= last_index; i++) {
395
+        j = s->intra_scantable.permutated[i];
396
+        level = block[j];
397
+        if (level) {
398
+            run = i - last_non_zero - 1;
399
+            last = (i == last_index);
400
+            sign = 0;
401
+            slevel = level;
402
+            if (level < 0) {
403
+                sign = 1;
404
+                level = -level;
405
+            }
406
+            code = get_rl_index(rl, last, run, level);
407
+            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
408
+            if (code == rl->n) {
409
+              if(!CONFIG_FLV_ENCODER || s->h263_flv <= 1){
410
+                put_bits(&s->pb, 1, last);
411
+                put_bits(&s->pb, 6, run);
412
+
413
+                assert(slevel != 0);
414
+
415
+                if(level < 128)
416
+                    put_sbits(&s->pb, 8, slevel);
417
+                else{
418
+                    put_bits(&s->pb, 8, 128);
419
+                    put_sbits(&s->pb, 5, slevel);
420
+                    put_sbits(&s->pb, 6, slevel>>5);
421
+                }
422
+              }else{
423
+                    ff_flv2_encode_ac_esc(&s->pb, slevel, level, run, last);
424
+              }
425
+            } else {
426
+                put_bits(&s->pb, 1, sign);
427
+            }
428
+            last_non_zero = i;
429
+        }
430
+    }
431
+}
432
+
433
+/* Encode MV differences on H.263+ with Unrestricted MV mode */
434
+static void h263p_encode_umotion(MpegEncContext * s, int val)
435
+{
436
+    short sval = 0;
437
+    short i = 0;
438
+    short n_bits = 0;
439
+    short temp_val;
440
+    int code = 0;
441
+    int tcode;
442
+
443
+    if ( val == 0)
444
+        put_bits(&s->pb, 1, 1);
445
+    else if (val == 1)
446
+        put_bits(&s->pb, 3, 0);
447
+    else if (val == -1)
448
+        put_bits(&s->pb, 3, 2);
449
+    else {
450
+
451
+        sval = ((val < 0) ? (short)(-val):(short)val);
452
+        temp_val = sval;
453
+
454
+        while (temp_val != 0) {
455
+            temp_val = temp_val >> 1;
456
+            n_bits++;
457
+        }
458
+
459
+        i = n_bits - 1;
460
+        while (i > 0) {
461
+            tcode = (sval & (1 << (i-1))) >> (i-1);
462
+            tcode = (tcode << 1) | 1;
463
+            code = (code << 2) | tcode;
464
+            i--;
465
+        }
466
+        code = ((code << 1) | (val < 0)) << 1;
467
+        put_bits(&s->pb, (2*n_bits)+1, code);
468
+    }
469
+}
470
+
471
+void h263_encode_mb(MpegEncContext * s,
472
+                    DCTELEM block[6][64],
473
+                    int motion_x, int motion_y)
474
+{
475
+    int cbpc, cbpy, i, cbp, pred_x, pred_y;
476
+    int16_t pred_dc;
477
+    int16_t rec_intradc[6];
478
+    int16_t *dc_ptr[6];
479
+    const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1);
480
+
481
+    if (!s->mb_intra) {
482
+        /* compute cbp */
483
+        cbp= get_p_cbp(s, block, motion_x, motion_y);
484
+
485
+        if ((cbp | motion_x | motion_y | s->dquant | (s->mv_type - MV_TYPE_16X16)) == 0) {
486
+            /* skip macroblock */
487
+            put_bits(&s->pb, 1, 1);
488
+            if(interleaved_stats){
489
+                s->misc_bits++;
490
+                s->last_bits++;
491
+            }
492
+            s->skip_count++;
493
+
494
+            return;
495
+        }
496
+        put_bits(&s->pb, 1, 0);         /* mb coded */
497
+
498
+        cbpc = cbp & 3;
499
+        cbpy = cbp >> 2;
500
+        if(s->alt_inter_vlc==0 || cbpc!=3)
501
+            cbpy ^= 0xF;
502
+        if(s->dquant) cbpc+= 8;
503
+        if(s->mv_type==MV_TYPE_16X16){
504
+            put_bits(&s->pb,
505
+                    ff_h263_inter_MCBPC_bits[cbpc],
506
+                    ff_h263_inter_MCBPC_code[cbpc]);
507
+
508
+            put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
509
+            if(s->dquant)
510
+                put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
511
+
512
+            if(interleaved_stats){
513
+                s->misc_bits+= get_bits_diff(s);
514
+            }
515
+
516
+            /* motion vectors: 16x16 mode */
517
+            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
518
+
519
+            if (!s->umvplus) {
520
+                ff_h263_encode_motion_vector(s, motion_x - pred_x,
521
+                                                motion_y - pred_y, 1);
522
+            }
523
+            else {
524
+                h263p_encode_umotion(s, motion_x - pred_x);
525
+                h263p_encode_umotion(s, motion_y - pred_y);
526
+                if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
527
+                    /* To prevent Start Code emulation */
528
+                    put_bits(&s->pb,1,1);
529
+            }
530
+        }else{
531
+            put_bits(&s->pb,
532
+                    ff_h263_inter_MCBPC_bits[cbpc+16],
533
+                    ff_h263_inter_MCBPC_code[cbpc+16]);
534
+            put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
535
+            if(s->dquant)
536
+                put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
537
+
538
+            if(interleaved_stats){
539
+                s->misc_bits+= get_bits_diff(s);
540
+            }
541
+
542
+            for(i=0; i<4; i++){
543
+                /* motion vectors: 8x8 mode*/
544
+                h263_pred_motion(s, i, 0, &pred_x, &pred_y);
545
+
546
+                motion_x= s->current_picture.motion_val[0][ s->block_index[i] ][0];
547
+                motion_y= s->current_picture.motion_val[0][ s->block_index[i] ][1];
548
+                if (!s->umvplus) {
549
+                    ff_h263_encode_motion_vector(s, motion_x - pred_x,
550
+                                                    motion_y - pred_y, 1);
551
+                }
552
+                else {
553
+                    h263p_encode_umotion(s, motion_x - pred_x);
554
+                    h263p_encode_umotion(s, motion_y - pred_y);
555
+                    if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
556
+                        /* To prevent Start Code emulation */
557
+                        put_bits(&s->pb,1,1);
558
+                }
559
+            }
560
+        }
561
+
562
+        if(interleaved_stats){
563
+            s->mv_bits+= get_bits_diff(s);
564
+        }
565
+    } else {
566
+        assert(s->mb_intra);
567
+
568
+        cbp = 0;
569
+        if (s->h263_aic) {
570
+            /* Predict DC */
571
+            for(i=0; i<6; i++) {
572
+                int16_t level = block[i][0];
573
+                int scale;
574
+
575
+                if(i<4) scale= s->y_dc_scale;
576
+                else    scale= s->c_dc_scale;
577
+
578
+                pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
579
+                level -= pred_dc;
580
+                /* Quant */
581
+                if (level >= 0)
582
+                    level = (level + (scale>>1))/scale;
583
+                else
584
+                    level = (level - (scale>>1))/scale;
585
+
586
+                /* AIC can change CBP */
587
+                if (level == 0 && s->block_last_index[i] == 0)
588
+                    s->block_last_index[i] = -1;
589
+
590
+                if(!s->modified_quant){
591
+                    if (level < -127)
592
+                        level = -127;
593
+                    else if (level > 127)
594
+                        level = 127;
595
+                }
596
+
597
+                block[i][0] = level;
598
+                /* Reconstruction */
599
+                rec_intradc[i] = scale*level + pred_dc;
600
+                /* Oddify */
601
+                rec_intradc[i] |= 1;
602
+                //if ((rec_intradc[i] % 2) == 0)
603
+                //    rec_intradc[i]++;
604
+                /* Clipping */
605
+                if (rec_intradc[i] < 0)
606
+                    rec_intradc[i] = 0;
607
+                else if (rec_intradc[i] > 2047)
608
+                    rec_intradc[i] = 2047;
609
+
610
+                /* Update AC/DC tables */
611
+                *dc_ptr[i] = rec_intradc[i];
612
+                if (s->block_last_index[i] >= 0)
613
+                    cbp |= 1 << (5 - i);
614
+            }
615
+        }else{
616
+            for(i=0; i<6; i++) {
617
+                /* compute cbp */
618
+                if (s->block_last_index[i] >= 1)
619
+                    cbp |= 1 << (5 - i);
620
+            }
621
+        }
622
+
623
+        cbpc = cbp & 3;
624
+        if (s->pict_type == FF_I_TYPE) {
625
+            if(s->dquant) cbpc+=4;
626
+            put_bits(&s->pb,
627
+                ff_h263_intra_MCBPC_bits[cbpc],
628
+                ff_h263_intra_MCBPC_code[cbpc]);
629
+        } else {
630
+            if(s->dquant) cbpc+=8;
631
+            put_bits(&s->pb, 1, 0);     /* mb coded */
632
+            put_bits(&s->pb,
633
+                ff_h263_inter_MCBPC_bits[cbpc + 4],
634
+                ff_h263_inter_MCBPC_code[cbpc + 4]);
635
+        }
636
+        if (s->h263_aic) {
637
+            /* XXX: currently, we do not try to use ac prediction */
638
+            put_bits(&s->pb, 1, 0);     /* no AC prediction */
639
+        }
640
+        cbpy = cbp >> 2;
641
+        put_bits(&s->pb, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
642
+        if(s->dquant)
643
+            put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
644
+
645
+        if(interleaved_stats){
646
+            s->misc_bits+= get_bits_diff(s);
647
+        }
648
+    }
649
+
650
+    for(i=0; i<6; i++) {
651
+        /* encode each block */
652
+        h263_encode_block(s, block[i], i);
653
+
654
+        /* Update INTRADC for decoding */
655
+        if (s->h263_aic && s->mb_intra) {
656
+            block[i][0] = rec_intradc[i];
657
+
658
+        }
659
+    }
660
+
661
+    if(interleaved_stats){
662
+        if (!s->mb_intra) {
663
+            s->p_tex_bits+= get_bits_diff(s);
664
+            s->f_count++;
665
+        }else{
666
+            s->i_tex_bits+= get_bits_diff(s);
667
+            s->i_count++;
668
+        }
669
+    }
670
+}
671
+
672
+void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
673
+{
674
+    int range, l, bit_size, sign, code, bits;
675
+
676
+    if (val == 0) {
677
+        /* zero vector */
678
+        code = 0;
679
+        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
680
+    } else {
681
+        bit_size = f_code - 1;
682
+        range = 1 << bit_size;
683
+        /* modulo encoding */
684
+        l= INT_BIT - 6 - bit_size;
685
+        val = (val<<l)>>l;
686
+        sign = val>>31;
687
+        val= (val^sign)-sign;
688
+        sign&=1;
689
+
690
+        val--;
691
+        code = (val >> bit_size) + 1;
692
+        bits = val & (range - 1);
693
+
694
+        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
695
+        if (bit_size > 0) {
696
+            put_bits(&s->pb, bit_size, bits);
697
+        }
698
+    }
699
+}
700
+
701
+static void init_mv_penalty_and_fcode(MpegEncContext *s)
702
+{
703
+    int f_code;
704
+    int mv;
705
+
706
+    for(f_code=1; f_code<=MAX_FCODE; f_code++){
707
+        for(mv=-MAX_MV; mv<=MAX_MV; mv++){
708
+            int len;
709
+
710
+            if(mv==0) len= mvtab[0][1];
711
+            else{
712
+                int val, bit_size, code;
713
+
714
+                bit_size = f_code - 1;
715
+
716
+                val=mv;
717
+                if (val < 0)
718
+                    val = -val;
719
+                val--;
720
+                code = (val >> bit_size) + 1;
721
+                if(code<33){
722
+                    len= mvtab[code][1] + 1 + bit_size;
723
+                }else{
724
+                    len= mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
725
+                }
726
+            }
727
+
728
+            mv_penalty[f_code][mv+MAX_MV]= len;
729
+        }
730
+    }
731
+
732
+    for(f_code=MAX_FCODE; f_code>0; f_code--){
733
+        for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
734
+            fcode_tab[mv+MAX_MV]= f_code;
735
+        }
736
+    }
737
+
738
+    for(mv=0; mv<MAX_MV*2+1; mv++){
739
+        umv_fcode_tab[mv]= 1;
740
+    }
741
+}
742
+
743
+static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
744
+    int slevel, run, last;
745
+
746
+    assert(MAX_LEVEL >= 64);
747
+    assert(MAX_RUN   >= 63);
748
+
749
+    for(slevel=-64; slevel<64; slevel++){
750
+        if(slevel==0) continue;
751
+        for(run=0; run<64; run++){
752
+            for(last=0; last<=1; last++){
753
+                const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
754
+                int level= slevel < 0 ? -slevel : slevel;
755
+                int sign= slevel < 0 ? 1 : 0;
756
+                int bits, len, code;
757
+
758
+                len_tab[index]= 100;
759
+
760
+                /* ESC0 */
761
+                code= get_rl_index(rl, last, run, level);
762
+                bits= rl->table_vlc[code][0];
763
+                len=  rl->table_vlc[code][1];
764
+                bits=bits*2+sign; len++;
765
+
766
+                if(code!=rl->n && len < len_tab[index]){
767
+                    if(bits_tab) bits_tab[index]= bits;
768
+                    len_tab [index]= len;
769
+                }
770
+                /* ESC */
771
+                bits= rl->table_vlc[rl->n][0];
772
+                len = rl->table_vlc[rl->n][1];
773
+                bits=bits*2+last; len++;
774
+                bits=bits*64+run; len+=6;
775
+                bits=bits*256+(level&0xff); len+=8;
776
+
777
+                if(len < len_tab[index]){
778
+                    if(bits_tab) bits_tab[index]= bits;
779
+                    len_tab [index]= len;
780
+                }
781
+            }
782
+        }
783
+    }
784
+}
785
+
786
+void h263_encode_init(MpegEncContext *s)
787
+{
788
+    static int done = 0;
789
+
790
+    if (!done) {
791
+        done = 1;
792
+
793
+        init_rl(&ff_h263_rl_inter, ff_h263_static_rl_table_store[0]);
794
+        init_rl(&rl_intra_aic, ff_h263_static_rl_table_store[1]);
795
+
796
+        init_uni_h263_rl_tab(&rl_intra_aic, NULL, uni_h263_intra_aic_rl_len);
797
+        init_uni_h263_rl_tab(&ff_h263_rl_inter    , NULL, uni_h263_inter_rl_len);
798
+
799
+        init_mv_penalty_and_fcode(s);
800
+    }
801
+    s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
802
+
803
+    s->intra_ac_vlc_length     =s->inter_ac_vlc_length     = uni_h263_inter_rl_len;
804
+    s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
805
+    if(s->h263_aic){
806
+        s->intra_ac_vlc_length     = uni_h263_intra_aic_rl_len;
807
+        s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
808
+    }
809
+    s->ac_esc_length= 7+1+6+8;
810
+
811
+    // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
812
+    switch(s->codec_id){
813
+    case CODEC_ID_MPEG4:
814
+        s->fcode_tab= fcode_tab;
815
+        break;
816
+    case CODEC_ID_H263P:
817
+        if(s->umvplus)
818
+            s->fcode_tab= umv_fcode_tab;
819
+        if(s->modified_quant){
820
+            s->min_qcoeff= -2047;
821
+            s->max_qcoeff=  2047;
822
+        }else{
823
+            s->min_qcoeff= -127;
824
+            s->max_qcoeff=  127;
825
+        }
826
+        break;
827
+        //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
828
+    case CODEC_ID_FLV1:
829
+        if (s->h263_flv > 1) {
830
+            s->min_qcoeff= -1023;
831
+            s->max_qcoeff=  1023;
832
+        } else {
833
+            s->min_qcoeff= -127;
834
+            s->max_qcoeff=  127;
835
+        }
836
+        s->y_dc_scale_table=
837
+        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
838
+        break;
839
+    default: //nothing needed - default table already set in mpegvideo.c
840
+        s->min_qcoeff= -127;
841
+        s->max_qcoeff=  127;
842
+        s->y_dc_scale_table=
843
+        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
844
+    }
845
+}
846
+
847
+void ff_h263_encode_mba(MpegEncContext *s)
848
+{
849
+    int i, mb_pos;
850
+
851
+    for(i=0; i<6; i++){
852
+        if(s->mb_num-1 <= ff_mba_max[i]) break;
853
+    }
854
+    mb_pos= s->mb_x + s->mb_width*s->mb_y;
855
+    put_bits(&s->pb, ff_mba_length[i], mb_pos);
856
+}
... ...
@@ -797,53 +797,6 @@ void ff_h261_encode_init(MpegEncContext *s);
797 797
 int ff_h261_get_picture_format(int width, int height);
798 798
 
799 799
 
800
-/* h263.c, h263dec.c */
801
-int ff_h263_decode_init(AVCodecContext *avctx);
802
-int ff_h263_decode_frame(AVCodecContext *avctx,
803
-                             void *data, int *data_size,
804
-                             AVPacket *avpkt);
805
-int ff_h263_decode_end(AVCodecContext *avctx);
806
-void h263_encode_mb(MpegEncContext *s,
807
-                    DCTELEM block[6][64],
808
-                    int motion_x, int motion_y);
809
-void h263_encode_picture_header(MpegEncContext *s, int picture_number);
810
-void h263_encode_gob_header(MpegEncContext * s, int mb_line);
811
-int16_t *h263_pred_motion(MpegEncContext * s, int block, int dir,
812
-                        int *px, int *py);
813
-void h263_encode_init(MpegEncContext *s);
814
-void h263_decode_init_vlc(MpegEncContext *s);
815
-int h263_decode_picture_header(MpegEncContext *s);
816
-int ff_h263_decode_gob_header(MpegEncContext *s);
817
-void ff_h263_update_motion_val(MpegEncContext * s);
818
-void ff_h263_loop_filter(MpegEncContext * s);
819
-void ff_set_qscale(MpegEncContext * s, int qscale);
820
-int ff_h263_decode_mba(MpegEncContext *s);
821
-void ff_h263_encode_mba(MpegEncContext *s);
822
-
823
-/**
824
- * Prints picture info if FF_DEBUG_PICT_INFO is set.
825
- */
826
-void ff_h263_show_pict_info(MpegEncContext *s);
827
-
828
-int ff_intel_h263_decode_picture_header(MpegEncContext *s);
829
-int ff_h263_decode_mb(MpegEncContext *s,
830
-                      DCTELEM block[6][64]);
831
-
832
-/**
833
- * Returns the value of the 3bit "source format" syntax element.
834
- * that represents some standard picture dimensions or indicates that
835
- * width&height are explicitly stored later.
836
- */
837
-int av_const h263_get_picture_format(int width, int height);
838
-
839
-void ff_clean_h263_qscales(MpegEncContext *s);
840
-int ff_h263_resync(MpegEncContext *s);
841
-const uint8_t *ff_h263_find_resync_marker(const uint8_t *p, const uint8_t *end);
842
-int ff_h263_get_gob_height(MpegEncContext *s);
843
-void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code);
844
-void ff_init_qscale_tab(MpegEncContext *s);
845
-
846
-
847 800
 /* rv10.c */
848 801
 void rv10_encode_picture_header(MpegEncContext *s, int picture_number);
849 802
 int rv_decode_dc(MpegEncContext *s, int n);
... ...
@@ -28,6 +28,7 @@
28 28
 #include "avcodec.h"
29 29
 #include "dsputil.h"
30 30
 #include "mpegvideo.h"
31
+#include "h263.h"
31 32
 
32 33
 //#define DEBUG
33 34
 
... ...
@@ -30,6 +30,7 @@
30 30
 #include "avcodec.h"
31 31
 #include "dsputil.h"
32 32
 #include "mpegvideo.h"
33
+#include "h263.h"
33 34
 
34 35
 #include "svq1.h"
35 36
 #include "svq1enc_cb.h"
... ...
@@ -21,6 +21,7 @@
21 21
 #include "avcodec.h"
22 22
 #include "dsputil.h"
23 23
 #include "mpegvideo.h"
24
+#include "h263.h"
24 25
 #include "mathops.h"
25 26
 #include "msmpeg4.h"
26 27
 #include "msmpeg4data.h"
... ...
@@ -23,6 +23,7 @@
23 23
 #include "mpegvideo.h"
24 24
 #include "msmpeg4.h"
25 25
 #include "msmpeg4data.h"
26
+#include "h263.h"
26 27
 #include "wmv2.h"
27 28
 
28 29