Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/ituh263dec.c
Line
Count
Source
1
/*
2
 * ITU H.263 bitstream decoder
3
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 * H.263+ support.
5
 * Copyright (c) 2001 Juan J. Sierralta P
6
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
7
 *
8
 * This file is part of FFmpeg.
9
 *
10
 * FFmpeg is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * FFmpeg is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with FFmpeg; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
 */
24
25
/**
26
 * @file
27
 * H.263 decoder.
28
 */
29
30
#define UNCHECKED_BITSTREAM_READER 1
31
32
#include "config_components.h"
33
34
#include "libavutil/attributes.h"
35
#include "libavutil/imgutils.h"
36
#include "libavutil/internal.h"
37
#include "libavutil/mathematics.h"
38
#include "libavutil/mem_internal.h"
39
#include "libavutil/thread.h"
40
#include "avcodec.h"
41
#include "mpegvideo.h"
42
#include "h263.h"
43
#include "h263data.h"
44
#include "h263dec.h"
45
#include "mathops.h"
46
#include "mpegutils.h"
47
#include "unary.h"
48
#include "rv10dec.h"
49
#include "mpeg4video.h"
50
#include "mpegvideodata.h"
51
#include "mpegvideodec.h"
52
#include "mpeg4videodec.h"
53
#include "mpeg4videodefs.h"
54
55
// The defines below define the number of bits that are read at once for
56
// reading vlc values. Changing these may improve speed and data cache needs
57
// be aware though that decreasing them may need the number of stages that is
58
// passed to get_vlc* to be increased.
59
408k
#define H263_MBTYPE_B_VLC_BITS 6
60
109k
#define CBPC_B_VLC_BITS 3
61
62
static const int16_t h263_mb_type_b_map[15]= {
63
    MB_TYPE_DIRECT2 | MB_TYPE_BIDIR_MV,
64
    MB_TYPE_DIRECT2 | MB_TYPE_BIDIR_MV    | MB_TYPE_CBP,
65
    MB_TYPE_DIRECT2 | MB_TYPE_BIDIR_MV    | MB_TYPE_CBP | MB_TYPE_QUANT,
66
                      MB_TYPE_FORWARD_MV                                | MB_TYPE_16x16,
67
                      MB_TYPE_FORWARD_MV  | MB_TYPE_CBP                 | MB_TYPE_16x16,
68
                      MB_TYPE_FORWARD_MV  | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
69
                      MB_TYPE_BACKWARD_MV                               | MB_TYPE_16x16,
70
                      MB_TYPE_BACKWARD_MV | MB_TYPE_CBP                 | MB_TYPE_16x16,
71
                      MB_TYPE_BACKWARD_MV | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
72
                      MB_TYPE_BIDIR_MV                                  | MB_TYPE_16x16,
73
                      MB_TYPE_BIDIR_MV    | MB_TYPE_CBP                 | MB_TYPE_16x16,
74
                      MB_TYPE_BIDIR_MV    | MB_TYPE_CBP | MB_TYPE_QUANT | MB_TYPE_16x16,
75
    0, //stuffing
76
    MB_TYPE_INTRA4x4                | MB_TYPE_CBP,
77
    MB_TYPE_INTRA4x4                | MB_TYPE_CBP | MB_TYPE_QUANT,
78
};
79
80
void ff_h263_show_pict_info(H263DecContext *const h, int h263_plus)
81
333k
{
82
333k
    if (h->c.avctx->debug&FF_DEBUG_PICT_INFO) {
83
0
        av_log(h->c.avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s%s%s%s%s %d/%d\n",
84
0
               h->c.qscale, av_get_picture_type_char(h->c.pict_type),
85
0
               h->gb.size_in_bits, 1-h->c.no_rounding,
86
0
               h->c.obmc ? " AP" : "",
87
0
               h->umvplus ? " UMV" : "",
88
0
               h->h263_long_vectors ? " LONG" : "",
89
0
               h263_plus ? " +" : "",
90
0
               h->c.h263_aic ? " AIC" : "",
91
0
               h->alt_inter_vlc ? " AIV" : "",
92
0
               h->modified_quant ? " MQ" : "",
93
0
               h->loop_filter ? " LOOP" : "",
94
0
               h->h263_slice_structured ? " SS" : "",
95
0
               h->c.avctx->framerate.num, h->c.avctx->framerate.den);
96
0
    }
97
333k
}
98
99
/***********************************************/
100
/* decoding */
101
102
VLCElem ff_h263_intra_MCBPC_vlc[72];
103
VLCElem ff_h263_inter_MCBPC_vlc[198];
104
VLCElem ff_h263_cbpy_vlc[64];
105
VLCElem ff_h263_mv_vlc[538];
106
static VLCElem h263_mbtype_b_vlc[80];
107
static VLCElem cbpc_b_vlc[8];
108
109
/* init vlcs */
110
111
static av_cold void h263_decode_init_vlc(void)
112
12
{
113
12
    VLC_INIT_STATIC_TABLE(ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
114
12
                          ff_h263_intra_MCBPC_bits, 1, 1,
115
12
                          ff_h263_intra_MCBPC_code, 1, 1, 0);
116
12
    VLC_INIT_STATIC_TABLE(ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
117
12
                          ff_h263_inter_MCBPC_bits, 1, 1,
118
12
                          ff_h263_inter_MCBPC_code, 1, 1, 0);
119
12
    VLC_INIT_STATIC_TABLE(ff_h263_cbpy_vlc, CBPY_VLC_BITS, 16,
120
12
                          &ff_h263_cbpy_tab[0][1], 2, 1,
121
12
                          &ff_h263_cbpy_tab[0][0], 2, 1, 0);
122
12
    VLC_INIT_STATIC_TABLE(ff_h263_mv_vlc, H263_MV_VLC_BITS, 33,
123
12
                          &ff_mvtab[0][1], 2, 1,
124
12
                          &ff_mvtab[0][0], 2, 1, 0);
125
12
    VLC_INIT_RL(ff_h263_rl_inter, 554);
126
12
    INIT_FIRST_VLC_RL(ff_rl_intra_aic, 554);
127
12
    VLC_INIT_STATIC_SPARSE_TABLE(h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
128
12
                                 &ff_h263_mbtype_b_tab[0][1], 2, 1,
129
12
                                 &ff_h263_mbtype_b_tab[0][0], 2, 1,
130
12
                                 h263_mb_type_b_map, 2, 2, 0);
131
12
    VLC_INIT_STATIC_TABLE(cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
132
12
                          &ff_cbpc_b_tab[0][1], 2, 1,
133
12
                          &ff_cbpc_b_tab[0][0], 2, 1, 0);
134
12
}
135
136
av_cold void ff_h263_decode_init_vlc(void)
137
48.4k
{
138
48.4k
    static AVOnce init_static_once = AV_ONCE_INIT;
139
48.4k
    ff_thread_once(&init_static_once, h263_decode_init_vlc);
140
48.4k
}
141
142
int ff_h263_decode_mba(H263DecContext *const h)
143
93.5k
{
144
93.5k
    int i, mb_pos;
145
146
252k
    for (i = 0; i < 6; i++)
147
250k
        if (h->c.mb_num - 1 <= ff_mba_max[i])
148
91.4k
            break;
149
93.5k
    mb_pos    = get_bits(&h->gb, ff_mba_length[i]);
150
93.5k
    h->c.mb_x = mb_pos % h->c.mb_width;
151
93.5k
    h->c.mb_y = mb_pos / h->c.mb_width;
152
153
93.5k
    return mb_pos;
154
93.5k
}
155
156
/**
157
 * Decode the group of blocks header or slice header.
158
 * @return <0 if an error occurred
159
 */
160
static int h263_decode_gob_header(H263DecContext *const h)
161
8.08M
{
162
8.08M
    unsigned int val, gob_number;
163
8.08M
    int left;
164
165
    /* Check for GOB Start Code */
166
8.08M
    val = show_bits(&h->gb, 16);
167
8.08M
    if(val)
168
0
        return -1;
169
170
        /* We have a GBSC probably with GSTUFF */
171
8.08M
    skip_bits(&h->gb, 16); /* Drop the zeros */
172
8.08M
    left = get_bits_left(&h->gb);
173
8.08M
    left = FFMIN(left, 32);
174
    //MN: we must check the bits left or we might end in an infinite loop (or segfault)
175
131M
    for(;left>13; left--){
176
125M
        if (get_bits1(&h->gb))
177
1.66M
            break; /* Seek the '1' bit */
178
125M
    }
179
8.08M
    if(left<=13)
180
6.42M
        return -1;
181
182
1.66M
    if (h->h263_slice_structured) {
183
33.8k
        if (check_marker(h->c.avctx, &h->gb, "before MBA")==0)
184
17.3k
            return -1;
185
186
16.5k
        ff_h263_decode_mba(h);
187
188
16.5k
        if (h->c.mb_num > 1583)
189
8.75k
            if (check_marker(h->c.avctx, &h->gb, "after MBA")==0)
190
5.40k
                return -1;
191
192
11.1k
        h->c.qscale = get_bits(&h->gb, 5); /* SQUANT */
193
11.1k
        if (check_marker(h->c.avctx, &h->gb, "after SQUANT")==0)
194
5.06k
            return -1;
195
6.08k
        skip_bits(&h->gb, 2); /* GFID */
196
1.63M
    }else{
197
1.63M
        gob_number = get_bits(&h->gb, 5); /* GN */
198
1.63M
        h->c.mb_x = 0;
199
1.63M
        h->c.mb_y = h->gob_index* gob_number;
200
1.63M
        skip_bits(&h->gb, 2); /* GFID */
201
1.63M
        h->c.qscale = get_bits(&h->gb, 5); /* GQUANT */
202
1.63M
    }
203
204
1.63M
    if (h->c.mb_y >= h->c.mb_height)
205
358k
        return -1;
206
207
1.28M
    if (h->c.qscale==0)
208
168k
        return -1;
209
210
1.11M
    return 0;
211
1.28M
}
212
213
/**
214
 * Decode the group of blocks / video packet header / slice header (MPEG-4 Studio).
215
 * @return bit position of the resync_marker, or <0 if none was found
216
 */
217
int ff_h263_resync(H263DecContext *const h)
218
1.65M
{
219
1.65M
    int left, pos, ret;
220
221
    /* In MPEG-4 studio mode look for a new slice startcode
222
     * and decode slice header */
223
1.65M
    if (h->c.codec_id==AV_CODEC_ID_MPEG4 && h->c.studio_profile) {
224
35.9k
        align_get_bits(&h->gb);
225
226
3.44M
        while (get_bits_left(&h->gb) >= 32 && show_bits_long(&h->gb, 32) != SLICE_STARTCODE) {
227
3.41M
            get_bits(&h->gb, 8);
228
3.41M
        }
229
230
35.9k
        if (get_bits_left(&h->gb) >= 32 && show_bits_long(&h->gb, 32) == SLICE_STARTCODE)
231
28.0k
            return get_bits_count(&h->gb);
232
7.98k
        else
233
7.98k
            return -1;
234
35.9k
    }
235
236
1.62M
    if (h->c.codec_id==AV_CODEC_ID_MPEG4){
237
180k
        skip_bits1(&h->gb);
238
180k
        align_get_bits(&h->gb);
239
180k
    }
240
241
1.62M
    if (show_bits(&h->gb, 16) ==0) {
242
953k
        pos = get_bits_count(&h->gb);
243
953k
#if CONFIG_MPEG4_DECODER
244
953k
        if (h->c.codec_id == AV_CODEC_ID_MPEG4)
245
87.0k
            ret = ff_mpeg4_decode_video_packet_header(h);
246
866k
        else
247
866k
#endif
248
866k
            ret = h263_decode_gob_header(h);
249
953k
        if(ret>=0)
250
481k
            return pos;
251
953k
    }
252
    //OK, it's not where it is supposed to be ...
253
1.14M
    h->gb = h->last_resync_gb;
254
1.14M
    align_get_bits(&h->gb);
255
1.14M
    left = get_bits_left(&h->gb);
256
257
45.0M
    for(;left>16+1+5+5; left-=8){
258
44.6M
        if (show_bits(&h->gb, 16) == 0){
259
8.63M
            GetBitContext bak = h->gb;
260
261
8.63M
            pos = get_bits_count(&h->gb);
262
8.63M
#if CONFIG_MPEG4_DECODER
263
8.63M
            if (h->c.codec_id == AV_CODEC_ID_MPEG4)
264
1.41M
                ret = ff_mpeg4_decode_video_packet_header(h);
265
7.22M
            else
266
7.22M
#endif
267
7.22M
                ret = h263_decode_gob_header(h);
268
8.63M
            if(ret>=0)
269
706k
                return pos;
270
271
7.92M
            h->gb = bak;
272
7.92M
        }
273
43.9M
        skip_bits(&h->gb, 8);
274
43.9M
    }
275
276
434k
    return -1;
277
1.14M
}
278
279
int ff_h263_decode_motion(H263DecContext *const h, int pred, int f_code)
280
17.6M
{
281
17.6M
    int code, val, sign, shift;
282
17.6M
    code = get_vlc2(&h->gb, ff_h263_mv_vlc, H263_MV_VLC_BITS, 2);
283
284
17.6M
    if (code == 0)
285
5.81M
        return pred;
286
11.8M
    if (code < 0)
287
230k
        return 0xffff;
288
289
11.5M
    sign = get_bits1(&h->gb);
290
11.5M
    shift = f_code - 1;
291
11.5M
    val = code;
292
11.5M
    if (shift) {
293
191k
        val = (val - 1) << shift;
294
191k
        val |= get_bits(&h->gb, shift);
295
191k
        val++;
296
191k
    }
297
11.5M
    if (sign)
298
4.96M
        val = -val;
299
11.5M
    val += pred;
300
301
    /* modulo decoding */
302
11.5M
    if (!h->h263_long_vectors) {
303
8.76M
        val = sign_extend(val, 5 + f_code);
304
8.76M
    } else {
305
        /* horrible H.263 long vector mode */
306
2.80M
        if (pred < -31 && val < -63)
307
3.90k
            val += 64;
308
2.80M
        if (pred > 32 && val > 63)
309
5.84k
            val -= 64;
310
311
2.80M
    }
312
11.5M
    return val;
313
11.8M
}
314
315
316
/* Decode RVLC of H.263+ UMV */
317
static int h263p_decode_umotion(H263DecContext *const h, int pred)
318
1.74M
{
319
1.74M
   int code = 0, sign;
320
321
1.74M
   if (get_bits1(&h->gb)) /* Motion difference = 0 */
322
559k
      return pred;
323
324
1.18M
   code = 2 + get_bits1(&h->gb);
325
326
1.85M
   while (get_bits1(&h->gb))
327
666k
   {
328
666k
      code <<= 1;
329
666k
      code += get_bits1(&h->gb);
330
666k
      if (code >= 32768) {
331
897
          avpriv_request_sample(h->c.avctx, "Huge DMV");
332
897
          return 0xffff;
333
897
      }
334
666k
   }
335
1.18M
   sign = code & 1;
336
1.18M
   code >>= 1;
337
338
1.18M
   code = (sign) ? (pred - code) : (pred + code);
339
1.18M
   ff_tlog(h->c.avctx,"H.263+ UMV Motion = %d\n", code);
340
1.18M
   return code;
341
342
1.18M
}
343
344
/**
345
 * read the next MVs for OBMC. yes this is an ugly hack, feel free to send a patch :)
346
 */
347
static void preview_obmc(H263DecContext *const h)
348
681k
{
349
681k
    GetBitContext gb = h->gb;
350
351
681k
    int cbpc, i, pred_x, pred_y, mx, my;
352
681k
    int16_t *mot_val;
353
681k
    const int xy     = h->c.mb_x + 1 + h->c.mb_y * h->c.mb_stride;
354
681k
    const int stride = h->c.b8_stride * 2;
355
356
3.40M
    for(i=0; i<4; i++)
357
2.72M
        h->c.block_index[i] += 2;
358
2.04M
    for(i=4; i<6; i++)
359
1.36M
        h->c.block_index[i] += 1;
360
681k
    h->c.mb_x++;
361
362
681k
    av_assert2(h->c.pict_type == AV_PICTURE_TYPE_P);
363
364
707k
    do{
365
707k
        if (get_bits1(&h->gb)) {
366
            /* skip mb */
367
296k
            mot_val = h->c.cur_pic.motion_val[0][h->c.block_index[0]];
368
296k
            mot_val[0       ]= mot_val[2       ]=
369
296k
            mot_val[0+stride]= mot_val[2+stride]= 0;
370
296k
            mot_val[1       ]= mot_val[3       ]=
371
296k
            mot_val[1+stride]= mot_val[3+stride]= 0;
372
373
296k
            h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
374
296k
            goto end;
375
296k
        }
376
410k
        cbpc = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2);
377
410k
    }while(cbpc == 20);
378
379
385k
    if(cbpc & 4){
380
56.6k
        h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA;
381
328k
    }else{
382
328k
        get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
383
328k
        if (cbpc & 8) {
384
30.9k
            skip_bits(&h->gb, h->modified_quant ? (get_bits1(&h->gb) ? 1 : 5) : 2);
385
30.9k
        }
386
387
328k
        if ((cbpc & 16) == 0) {
388
207k
                h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
389
                /* 16x16 motion prediction */
390
207k
                mot_val= ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
391
207k
                if (h->umvplus)
392
31.6k
                    mx = h263p_decode_umotion(h, pred_x);
393
175k
                else
394
175k
                    mx = ff_h263_decode_motion(h, pred_x, 1);
395
396
207k
                if (h->umvplus)
397
31.6k
                    my = h263p_decode_umotion(h, pred_y);
398
175k
                else
399
175k
                    my = ff_h263_decode_motion(h, pred_y, 1);
400
401
207k
                mot_val[0       ]= mot_val[2       ]=
402
207k
                mot_val[0+stride]= mot_val[2+stride]= mx;
403
207k
                mot_val[1       ]= mot_val[3       ]=
404
207k
                mot_val[1+stride]= mot_val[3+stride]= my;
405
207k
        } else {
406
121k
            h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_FORWARD_MV;
407
607k
            for(i=0;i<4;i++) {
408
486k
                mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y);
409
486k
                if (h->umvplus)
410
167k
                    mx = h263p_decode_umotion(h, pred_x);
411
318k
                else
412
318k
                    mx = ff_h263_decode_motion(h, pred_x, 1);
413
414
486k
                if (h->umvplus)
415
167k
                    my = h263p_decode_umotion(h, pred_y);
416
318k
                else
417
318k
                    my = ff_h263_decode_motion(h, pred_y, 1);
418
486k
                if (h->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
419
32.8k
                    skip_bits1(&h->gb); /* Bit stuffing to prevent PSC */
420
486k
                mot_val[0] = mx;
421
486k
                mot_val[1] = my;
422
486k
            }
423
121k
        }
424
328k
    }
425
681k
end:
426
427
3.40M
    for(i=0; i<4; i++)
428
2.72M
        h->c.block_index[i] -= 2;
429
2.04M
    for(i=4; i<6; i++)
430
1.36M
        h->c.block_index[i] -= 1;
431
681k
    h->c.mb_x--;
432
433
681k
    h->gb = gb;
434
681k
}
435
436
static void h263_decode_dquant(H263DecContext *const h)
437
807k
{
438
807k
    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
439
807k
    int qscale;
440
441
807k
    if (h->modified_quant) {
442
165k
        if (get_bits1(&h->gb))
443
49.1k
            qscale = ff_modified_quant_tab[get_bits1(&h->gb)][h->c.qscale];
444
116k
        else
445
116k
            qscale = get_bits(&h->gb, 5);
446
165k
    }else
447
641k
        qscale = h->c.qscale + quant_tab[get_bits(&h->gb, 2)];
448
807k
    ff_set_qscale(&h->c, qscale);
449
807k
}
450
451
static void h263_pred_acdc(MpegEncContext * s, int16_t *block, int n)
452
957k
{
453
957k
    int wrap, a, c, pred_dc, scale;
454
957k
    const int xy = s->block_index[n];
455
957k
    int16_t *const dc_val =  s->dc_val + xy;
456
957k
    int16_t *const ac_val = (s->ac_val + xy)[0];
457
458
    /* find prediction */
459
957k
    if (n < 4) {
460
646k
        wrap = s->b8_stride;
461
646k
        scale = s->y_dc_scale;
462
646k
    } else {
463
311k
        wrap = s->mb_stride;
464
311k
        scale = s->c_dc_scale;
465
311k
    }
466
467
    /* B C
468
     * A X
469
     */
470
957k
    a = dc_val[-1];
471
957k
    c = dc_val[-wrap];
472
473
    /* No prediction outside GOB boundary */
474
957k
    if (s->first_slice_line && n != 3) {
475
477k
        if (n != 2) c= 1024;
476
477k
        if (n != 1 && s->mb_x == s->resync_mb_x) a= 1024;
477
477k
    }
478
479
957k
    if (s->ac_pred) {
480
571k
        pred_dc = 1024;
481
571k
        if (s->h263_aic_dir) {
482
            /* left prediction */
483
266k
            if (a != 1024) {
484
234k
                int16_t *const ac_val2 = ac_val - 16;
485
1.87M
                for (int i = 1; i < 8; i++) {
486
1.64M
                    block[s->idsp.idct_permutation[i << 3]] += ac_val2[i];
487
1.64M
                }
488
234k
                pred_dc = a;
489
234k
            }
490
305k
        } else {
491
            /* top prediction */
492
305k
            if (c != 1024) {
493
231k
                int16_t *const ac_val2 = ac_val - 16 * wrap;
494
1.85M
                for (int i = 1; i < 8; i++) {
495
1.61M
                    block[s->idsp.idct_permutation[i]] += ac_val2[i + 8];
496
1.61M
                }
497
231k
                pred_dc = c;
498
231k
            }
499
305k
        }
500
571k
    } else {
501
        /* just DC prediction */
502
386k
        if (a != 1024 && c != 1024)
503
179k
            pred_dc = (a + c) >> 1;
504
206k
        else if (a != 1024)
505
145k
            pred_dc = a;
506
60.6k
        else
507
60.6k
            pred_dc = c;
508
386k
    }
509
510
    /* we assume pred is positive */
511
957k
    block[0] = block[0] * scale + pred_dc;
512
513
957k
    if (block[0] < 0)
514
3.06k
        block[0] = 0;
515
954k
    else
516
954k
        block[0] |= 1;
517
518
    /* Update AC/DC tables */
519
957k
    *dc_val = block[0];
520
521
    /* left copy */
522
7.65M
    for (int i = 1; i < 8; i++)
523
6.70M
        ac_val[i]     = block[s->idsp.idct_permutation[i << 3]];
524
    /* top copy */
525
7.65M
    for (int i = 1; i < 8; i++)
526
6.70M
        ac_val[8 + i] = block[s->idsp.idct_permutation[i]];
527
957k
}
528
529
static int h263_decode_block(H263DecContext *const h, int16_t block[64],
530
                             int n, int coded)
531
33.4M
{
532
33.4M
    int level, i, j, run;
533
33.4M
    const RLTable *rl = &ff_h263_rl_inter;
534
33.4M
    const uint8_t *scan_table;
535
33.4M
    GetBitContext gb = h->gb;
536
537
33.4M
    scan_table = h->c.intra_scantable.permutated;
538
33.4M
    if (h->c.h263_aic && h->c.mb_intra) {
539
977k
        i = 0;
540
977k
        if (!coded)
541
519k
            goto not_coded;
542
458k
        rl = &ff_rl_intra_aic;
543
458k
        if (h->c.ac_pred) {
544
301k
            if (h->c.h263_aic_dir)
545
117k
                scan_table = h->permutated_intra_v_scantable; /* left */
546
183k
            else
547
183k
                scan_table = h->permutated_intra_h_scantable; /* top */
548
301k
        }
549
32.4M
    } else if (h->c.mb_intra) {
550
        /* DC coef */
551
5.22M
        if (CONFIG_RV10_DECODER && h->c.codec_id == AV_CODEC_ID_RV10) {
552
1.01M
            if (h->rv10_version == 3 && h->c.pict_type == AV_PICTURE_TYPE_I) {
553
957k
                int component = (n <= 3 ? 0 : n - 4 + 1);
554
957k
                level = h->last_dc[component];
555
957k
                if (h->rv10_first_dc_coded[component]) {
556
847k
                    int diff = ff_rv_decode_dc(h, n);
557
847k
                    if (diff < 0)
558
329
                        return -1;
559
847k
                    level += diff;
560
847k
                    level = level & 0xff; /* handle wrap round */
561
847k
                    h->last_dc[component] = level;
562
847k
                } else {
563
110k
                    h->rv10_first_dc_coded[component] = 1;
564
110k
                }
565
957k
            } else {
566
58.7k
                level = get_bits(&h->gb, 8);
567
58.7k
                if (level == 255)
568
913
                    level = 128;
569
58.7k
            }
570
4.20M
        }else{
571
4.20M
            level = get_bits(&h->gb, 8);
572
4.20M
            if((level&0x7F) == 0){
573
441k
                av_log(h->c.avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
574
441k
                       level, h->c.mb_x, h->c.mb_y);
575
441k
                if (h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
576
25.9k
                    return -1;
577
441k
            }
578
4.17M
            if (level == 255)
579
124k
                level = 128;
580
4.17M
        }
581
5.19M
        block[0] = level;
582
5.19M
        i = 1;
583
27.2M
    } else {
584
27.2M
        i = 0;
585
27.2M
    }
586
32.9M
    if (!coded) {
587
26.2M
        h->c.block_last_index[n] = i - 1;
588
26.2M
        return 0;
589
26.2M
    }
590
6.72M
retry:
591
6.72M
    {
592
6.72M
    OPEN_READER(re, &h->gb);
593
6.72M
    i--; // offset by -1 to allow direct indexing of scan_table
594
19.7M
    for(;;) {
595
19.7M
        UPDATE_CACHE(re, &h->gb);
596
19.7M
        GET_RL_VLC(level, run, re, &h->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
597
19.7M
        if (run == 66) {
598
823k
            if (level){
599
642k
                CLOSE_READER(re, &h->gb);
600
642k
                av_log(h->c.avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
601
642k
                       h->c.mb_x, h->c.mb_y);
602
642k
                return -1;
603
642k
            }
604
            /* escape */
605
180k
            if (CONFIG_FLV_DECODER && h->flv) {
606
5.79k
                int is11 = SHOW_UBITS(re, &h->gb, 1);
607
5.79k
                SKIP_CACHE(re, &h->gb, 1);
608
5.79k
                run = SHOW_UBITS(re, &h->gb, 7) + 1;
609
5.79k
                if (is11) {
610
4.12k
                    SKIP_COUNTER(re, &h->gb, 1 + 7);
611
4.12k
                    UPDATE_CACHE(re, &h->gb);
612
4.12k
                    level = SHOW_SBITS(re, &h->gb, 11);
613
4.12k
                    SKIP_COUNTER(re, &h->gb, 11);
614
4.12k
                } else {
615
1.66k
                    SKIP_CACHE(re, &h->gb, 7);
616
1.66k
                    level = SHOW_SBITS(re, &h->gb, 7);
617
1.66k
                    SKIP_COUNTER(re, &h->gb, 1 + 7 + 7);
618
1.66k
                }
619
175k
            } else {
620
175k
                run = SHOW_UBITS(re, &h->gb, 7) + 1;
621
175k
                SKIP_CACHE(re, &h->gb, 7);
622
175k
                level = (int8_t)SHOW_UBITS(re, &h->gb, 8);
623
175k
                SKIP_COUNTER(re, &h->gb, 7 + 8);
624
175k
                if(level == -128){
625
16.9k
                    UPDATE_CACHE(re, &h->gb);
626
16.9k
                    if (h->c.codec_id == AV_CODEC_ID_RV10) {
627
                        /* XXX: should patch encoder too */
628
603
                        level = SHOW_SBITS(re, &h->gb, 12);
629
603
                        SKIP_COUNTER(re, &h->gb, 12);
630
16.3k
                    }else{
631
16.3k
                        level = SHOW_UBITS(re, &h->gb, 5);
632
16.3k
                        SKIP_CACHE(re, &h->gb, 5);
633
16.3k
                        level |= SHOW_SBITS(re, &h->gb, 6) * (1<<5);
634
16.3k
                        SKIP_COUNTER(re, &h->gb, 5 + 6);
635
16.3k
                    }
636
16.9k
                }
637
175k
            }
638
18.9M
        } else {
639
18.9M
            if (SHOW_UBITS(re, &h->gb, 1))
640
8.14M
                level = -level;
641
18.9M
            SKIP_COUNTER(re, &h->gb, 1);
642
18.9M
        }
643
19.1M
        i += run;
644
19.1M
        if (i >= 64){
645
6.08M
            CLOSE_READER(re, &h->gb);
646
            // redo update without last flag, revert -1 offset
647
6.08M
            i = i - run + ((run-1)&63) + 1;
648
6.08M
            if (i < 64) {
649
                // only last marker, no overrun
650
6.00M
                block[scan_table[i]] = level;
651
6.00M
                break;
652
6.00M
            }
653
75.3k
            if(h->alt_inter_vlc && rl == &ff_h263_rl_inter && !h->c.mb_intra){
654
                //Looks like a hack but no, it's the way it is supposed to work ...
655
4.58k
                rl = &ff_rl_intra_aic;
656
4.58k
                i = 0;
657
4.58k
                h->gb = gb;
658
4.58k
                h->c.bdsp.clear_block(block);
659
4.58k
                goto retry;
660
4.58k
            }
661
70.7k
            av_log(h->c.avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n",
662
70.7k
                   h->c.mb_x, h->c.mb_y, h->c.mb_intra);
663
70.7k
            return -1;
664
75.3k
        }
665
13.0M
        j = scan_table[i];
666
13.0M
        block[j] = level;
667
13.0M
    }
668
6.72M
    }
669
6.00M
    if (h->c.mb_intra && h->c.h263_aic) {
670
957k
not_coded:
671
957k
        h263_pred_acdc(&h->c, block, n);
672
957k
    }
673
6.52M
    h->c.block_last_index[n] = i;
674
6.52M
    return 0;
675
6.00M
}
676
677
static int h263_skip_b_part(H263DecContext *const h, int cbp)
678
739k
{
679
739k
    LOCAL_ALIGNED_32(int16_t, dblock, [64]);
680
739k
    int i, mbi;
681
739k
    int bli[6];
682
683
    /* we have to set h->c.mb_intra to zero to decode B-part of PB-frame correctly
684
     * but real value should be restored in order to be used later (in OBMC condition)
685
     */
686
739k
    mbi = h->c.mb_intra;
687
739k
    memcpy(bli, h->c.block_last_index, sizeof(bli));
688
739k
    h->c.mb_intra = 0;
689
5.09M
    for (i = 0; i < 6; i++) {
690
4.37M
        if (h263_decode_block(h, dblock, i, cbp&32) < 0)
691
16.3k
            return -1;
692
4.35M
        cbp+=cbp;
693
4.35M
    }
694
722k
    h->c.mb_intra = mbi;
695
722k
    memcpy(h->c.block_last_index, bli, sizeof(bli));
696
722k
    return 0;
697
739k
}
698
699
static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
700
294k
{
701
294k
    int c, mv = 1;
702
703
294k
    if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
704
264k
        c = get_bits1(gb);
705
264k
        if (pb_frame == 2 && c)
706
4.32k
            mv = !get_bits1(gb);
707
264k
    } else { // h.263 Annex M improved PB-frame
708
30.1k
        mv = get_unary(gb, 0, 4) + 1;
709
30.1k
        c = mv & 1;
710
30.1k
        mv = !!(mv & 2);
711
30.1k
    }
712
294k
    if(c)
713
160k
        *cbpb = get_bits(gb, 6);
714
294k
    return mv;
715
294k
}
716
717
1.96M
#define tab_size ((signed)FF_ARRAY_ELEMS(s->direct_scale_mv[0]))
718
1.46M
#define tab_bias (tab_size / 2)
719
static inline void set_one_direct_mv(MpegEncContext *s, const MPVPicture *p, int i)
720
250k
{
721
250k
    int xy           = s->block_index[i];
722
250k
    uint16_t time_pp = s->pp_time;
723
250k
    uint16_t time_pb = s->pb_time;
724
250k
    int p_mx, p_my;
725
726
250k
    p_mx = p->motion_val[0][xy][0];
727
250k
    if ((unsigned)(p_mx + tab_bias) < tab_size) {
728
243k
        s->mv[0][i][0] = s->direct_scale_mv[0][p_mx + tab_bias];
729
243k
        s->mv[1][i][0] = s->direct_scale_mv[1][p_mx + tab_bias];
730
243k
    } else {
731
7.22k
        s->mv[0][i][0] = p_mx * time_pb / time_pp;
732
7.22k
        s->mv[1][i][0] = p_mx * (time_pb - time_pp) / time_pp;
733
7.22k
    }
734
250k
    p_my = p->motion_val[0][xy][1];
735
250k
    if ((unsigned)(p_my + tab_bias) < tab_size) {
736
239k
        s->mv[0][i][1] = s->direct_scale_mv[0][p_my + tab_bias];
737
239k
        s->mv[1][i][1] = s->direct_scale_mv[1][p_my + tab_bias];
738
239k
    } else {
739
11.5k
        s->mv[0][i][1] = p_my * time_pb / time_pp;
740
11.5k
        s->mv[1][i][1] = p_my * (time_pb - time_pp) / time_pp;
741
11.5k
    }
742
250k
}
743
744
/**
745
 * @return the mb_type
746
 */
747
static int set_direct_mv(MpegEncContext *s)
748
193k
{
749
193k
    const int mb_index = s->mb_x + s->mb_y * s->mb_stride;
750
193k
    const MPVPicture *p = s->next_pic.ptr;
751
193k
    int colocated_mb_type = p->mb_type[mb_index];
752
193k
    int i;
753
754
193k
    if (s->codec_tag == AV_RL32("U263") && p->f->pict_type == AV_PICTURE_TYPE_I) {
755
840
        p = s->last_pic.ptr;
756
840
        colocated_mb_type = p->mb_type[mb_index];
757
840
    }
758
759
193k
    if (IS_8X8(colocated_mb_type)) {
760
19.0k
        s->mv_type = MV_TYPE_8X8;
761
95.0k
        for (i = 0; i < 4; i++)
762
76.0k
            set_one_direct_mv(s, p, i);
763
19.0k
        return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_BIDIR_MV;
764
174k
    } else {
765
174k
        set_one_direct_mv(s, p, 0);
766
174k
        s->mv[0][1][0] =
767
174k
        s->mv[0][2][0] =
768
174k
        s->mv[0][3][0] = s->mv[0][0][0];
769
174k
        s->mv[0][1][1] =
770
174k
        s->mv[0][2][1] =
771
174k
        s->mv[0][3][1] = s->mv[0][0][1];
772
174k
        s->mv[1][1][0] =
773
174k
        s->mv[1][2][0] =
774
174k
        s->mv[1][3][0] = s->mv[1][0][0];
775
174k
        s->mv[1][1][1] =
776
174k
        s->mv[1][2][1] =
777
174k
        s->mv[1][3][1] = s->mv[1][0][1];
778
174k
        s->mv_type = MV_TYPE_8X8;
779
        // Note see prev line
780
174k
        return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_BIDIR_MV;
781
174k
    }
782
193k
}
783
784
int ff_h263_decode_mb(H263DecContext *const h)
785
22.6M
{
786
22.6M
    int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
787
22.6M
    int16_t *mot_val;
788
22.6M
    const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
789
22.6M
    int cbpb = 0, pb_mv_count = 0;
790
791
22.6M
    av_assert2(!h->c.h263_pred);
792
793
22.6M
    if (h->c.pict_type == AV_PICTURE_TYPE_P) {
794
21.4M
        do{
795
21.4M
            if (get_bits1(&h->gb)) {
796
                /* skip mb */
797
16.9M
                h->c.mb_intra = 0;
798
118M
                for(i=0;i<6;i++)
799
101M
                    h->c.block_last_index[i] = -1;
800
16.9M
                h->c.mv_dir  = MV_DIR_FORWARD;
801
16.9M
                h->c.mv_type = MV_TYPE_16X16;
802
16.9M
                h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
803
16.9M
                h->c.mv[0][0][0] = 0;
804
16.9M
                h->c.mv[0][0][1] = 0;
805
16.9M
                h->c.mb_skipped = !(h->c.obmc | h->loop_filter);
806
16.9M
                goto end;
807
16.9M
            }
808
4.48M
            cbpc = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2);
809
4.48M
            if (cbpc < 0){
810
89.7k
                av_log(h->c.avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n",
811
89.7k
                       h->c.mb_x, h->c.mb_y);
812
89.7k
                return SLICE_ERROR;
813
89.7k
            }
814
4.48M
        }while(cbpc == 20);
815
816
4.36M
        h->c.bdsp.clear_blocks(h->block[0]);
817
818
4.36M
        dquant = cbpc & 8;
819
4.36M
        h->c.mb_intra = ((cbpc & 4) != 0);
820
4.36M
        if (h->c.mb_intra)
821
446k
            goto intra;
822
823
3.91M
        if (h->pb_frame && get_bits1(&h->gb))
824
208k
            pb_mv_count = h263_get_modb(&h->gb, h->pb_frame, &cbpb);
825
3.91M
        cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
826
827
3.91M
        if (cbpy < 0) {
828
81.1k
            av_log(h->c.avctx, AV_LOG_ERROR, "cbpy damaged at %d %d\n",
829
81.1k
                   h->c.mb_x, h->c.mb_y);
830
81.1k
            return SLICE_ERROR;
831
81.1k
        }
832
833
3.83M
        if (!h->alt_inter_vlc|| (cbpc & 3)!=3)
834
3.83M
            cbpy ^= 0xF;
835
836
3.83M
        cbp = (cbpc & 3) | (cbpy << 2);
837
3.83M
        if (dquant) {
838
553k
            h263_decode_dquant(h);
839
553k
        }
840
841
3.83M
        h->c.mv_dir = MV_DIR_FORWARD;
842
3.83M
        if ((cbpc & 16) == 0) {
843
2.43M
            h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
844
            /* 16x16 motion prediction */
845
2.43M
            h->c.mv_type = MV_TYPE_16X16;
846
2.43M
            ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
847
2.43M
            if (h->umvplus)
848
239k
               mx = h263p_decode_umotion(h, pred_x);
849
2.19M
            else
850
2.19M
               mx = ff_h263_decode_motion(h, pred_x, 1);
851
852
2.43M
            if (mx >= 0xffff)
853
20.4k
                return SLICE_ERROR;
854
855
2.41M
            if (h->umvplus)
856
239k
               my = h263p_decode_umotion(h, pred_y);
857
2.17M
            else
858
2.17M
               my = ff_h263_decode_motion(h, pred_y, 1);
859
860
2.41M
            if (my >= 0xffff)
861
35.7k
                return SLICE_ERROR;
862
2.37M
            h->c.mv[0][0][0] = mx;
863
2.37M
            h->c.mv[0][0][1] = my;
864
865
2.37M
            if (h->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
866
25.2k
                skip_bits1(&h->gb); /* Bit stuffing to prevent PSC */
867
2.37M
        } else {
868
1.39M
            h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_FORWARD_MV;
869
1.39M
            h->c.mv_type = MV_TYPE_8X8;
870
6.79M
            for(i=0;i<4;i++) {
871
5.48M
                mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y);
872
5.48M
                if (h->umvplus)
873
349k
                    mx = h263p_decode_umotion(h, pred_x);
874
5.13M
                else
875
5.13M
                    mx = ff_h263_decode_motion(h, pred_x, 1);
876
5.48M
                if (mx >= 0xffff)
877
46.9k
                    return SLICE_ERROR;
878
879
5.43M
                if (h->umvplus)
880
349k
                    my = h263p_decode_umotion(h, pred_y);
881
5.08M
                else
882
5.08M
                    my = ff_h263_decode_motion(h, pred_y, 1);
883
5.43M
                if (my >= 0xffff)
884
37.3k
                    return SLICE_ERROR;
885
5.39M
                h->c.mv[0][i][0] = mx;
886
5.39M
                h->c.mv[0][i][1] = my;
887
5.39M
                if (h->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
888
53.7k
                    skip_bits1(&h->gb); /* Bit stuffing to prevent PSC */
889
5.39M
                mot_val[0] = mx;
890
5.39M
                mot_val[1] = my;
891
5.39M
            }
892
1.39M
        }
893
3.83M
    } else if (h->c.pict_type==AV_PICTURE_TYPE_B) {
894
405k
        int mb_type;
895
405k
        const int stride  = h->c.b8_stride;
896
405k
        int16_t *mot_val0 = h->c.cur_pic.motion_val[0][2 * (h->c.mb_x + h->c.mb_y * stride)];
897
405k
        int16_t *mot_val1 = h->c.cur_pic.motion_val[1][2 * (h->c.mb_x + h->c.mb_y * stride)];
898
//        const int mv_xy = h->c.mb_x + 1 + h->c.mb_y * h->c.mb_stride;
899
900
        //FIXME ugly
901
405k
        mot_val0[0       ]= mot_val0[2       ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
902
405k
        mot_val0[1       ]= mot_val0[3       ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
903
405k
        mot_val1[0       ]= mot_val1[2       ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
904
405k
        mot_val1[1       ]= mot_val1[3       ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
905
906
408k
        do{
907
408k
            mb_type = get_vlc2(&h->gb, h263_mbtype_b_vlc,
908
408k
                               H263_MBTYPE_B_VLC_BITS, 2);
909
408k
            if (mb_type < 0){
910
3.92k
                av_log(h->c.avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n",
911
3.92k
                       h->c.mb_x, h->c.mb_y);
912
3.92k
                return SLICE_ERROR;
913
3.92k
            }
914
408k
        }while(!mb_type);
915
916
401k
        h->c.mb_intra = IS_INTRA(mb_type);
917
401k
        if(HAS_CBP(mb_type)){
918
109k
            h->c.bdsp.clear_blocks(h->block[0]);
919
109k
            cbpc = get_vlc2(&h->gb, cbpc_b_vlc, CBPC_B_VLC_BITS, 1);
920
109k
            if (h->c.mb_intra) {
921
4.74k
                dquant = IS_QUANT(mb_type);
922
4.74k
                goto intra;
923
4.74k
            }
924
925
104k
            cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
926
927
104k
            if (cbpy < 0){
928
6.70k
                av_log(h->c.avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n",
929
6.70k
                       h->c.mb_x, h->c.mb_y);
930
6.70k
                return SLICE_ERROR;
931
6.70k
            }
932
933
98.2k
            if (!h->alt_inter_vlc || (cbpc & 3)!=3)
934
95.5k
                cbpy ^= 0xF;
935
936
98.2k
            cbp = (cbpc & 3) | (cbpy << 2);
937
98.2k
        }else
938
291k
            cbp=0;
939
940
389k
        av_assert2(!h->c.mb_intra);
941
942
389k
        if(IS_QUANT(mb_type)){
943
27.0k
            h263_decode_dquant(h);
944
27.0k
        }
945
946
389k
        if(IS_DIRECT(mb_type)){
947
193k
            h->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
948
193k
            mb_type |= set_direct_mv(&h->c);
949
196k
        }else{
950
196k
            h->c.mv_dir  = 0;
951
196k
            h->c.mv_type = MV_TYPE_16X16;
952
//FIXME UMV
953
954
196k
            if (HAS_FORWARD_MV(mb_type)) {
955
89.1k
                int16_t *mot_val = ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
956
89.1k
                h->c.mv_dir = MV_DIR_FORWARD;
957
958
89.1k
                if (h->umvplus)
959
29.7k
                    mx = h263p_decode_umotion(h, pred_x);
960
59.4k
                else
961
59.4k
                    mx = ff_h263_decode_motion(h, pred_x, 1);
962
89.1k
                if (mx >= 0xffff)
963
994
                    return SLICE_ERROR;
964
965
88.1k
                if (h->umvplus)
966
29.7k
                    my = h263p_decode_umotion(h, pred_y);
967
58.4k
                else
968
58.4k
                    my = ff_h263_decode_motion(h, pred_y, 1);
969
88.1k
                if (my >= 0xffff)
970
789
                    return SLICE_ERROR;
971
972
87.3k
                if (h->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
973
2.35k
                    skip_bits1(&h->gb); /* Bit stuffing to prevent PSC */
974
975
87.3k
                h->c.mv[0][0][0] = mx;
976
87.3k
                h->c.mv[0][0][1] = my;
977
87.3k
                mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
978
87.3k
                mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
979
87.3k
            }
980
981
194k
            if (HAS_BACKWARD_MV(mb_type)) {
982
153k
                int16_t *mot_val = ff_h263_pred_motion(&h->c, 0, 1, &pred_x, &pred_y);
983
153k
                h->c.mv_dir |= MV_DIR_BACKWARD;
984
985
153k
                if (h->umvplus)
986
54.4k
                    mx = h263p_decode_umotion(h, pred_x);
987
98.9k
                else
988
98.9k
                    mx = ff_h263_decode_motion(h, pred_x, 1);
989
153k
                if (mx >= 0xffff)
990
884
                    return SLICE_ERROR;
991
992
152k
                if (h->umvplus)
993
54.3k
                    my = h263p_decode_umotion(h, pred_y);
994
98.0k
                else
995
98.0k
                    my = ff_h263_decode_motion(h, pred_y, 1);
996
152k
                if (my >= 0xffff)
997
1.01k
                    return SLICE_ERROR;
998
999
151k
                if (h->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
1000
3.56k
                    skip_bits1(&h->gb); /* Bit stuffing to prevent PSC */
1001
1002
151k
                h->c.mv[1][0][0] = mx;
1003
151k
                h->c.mv[1][0][1] = my;
1004
151k
                mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
1005
151k
                mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
1006
151k
            }
1007
194k
        }
1008
1009
386k
        h->c.cur_pic.mb_type[xy] = mb_type;
1010
829k
    } else { /* I-Frame */
1011
834k
        do{
1012
834k
            cbpc = get_vlc2(&h->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2);
1013
834k
            if (cbpc < 0){
1014
26.4k
                av_log(h->c.avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n",
1015
26.4k
                       h->c.mb_x, h->c.mb_y);
1016
26.4k
                return SLICE_ERROR;
1017
26.4k
            }
1018
834k
        }while(cbpc == 8);
1019
1020
803k
        h->c.bdsp.clear_blocks(h->block[0]);
1021
1022
803k
        dquant = cbpc & 4;
1023
803k
        h->c.mb_intra = 1;
1024
1.25M
intra:
1025
1.25M
        h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA;
1026
1.25M
        if (h->c.h263_aic) {
1027
180k
            h->c.ac_pred = get_bits1(&h->gb);
1028
180k
            if (h->c.ac_pred) {
1029
103k
                h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1030
1031
103k
                h->c.h263_aic_dir = get_bits1(&h->gb);
1032
103k
            }
1033
180k
        }else
1034
1.07M
            h->c.ac_pred = 0;
1035
1036
1.25M
        if (h->pb_frame && get_bits1(&h->gb))
1037
85.7k
            pb_mv_count = h263_get_modb(&h->gb, h->pb_frame, &cbpb);
1038
1.25M
        cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
1039
1.25M
        if(cbpy<0){
1040
68.9k
            av_log(h->c.avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n",
1041
68.9k
                   h->c.mb_x, h->c.mb_y);
1042
68.9k
            return SLICE_ERROR;
1043
68.9k
        }
1044
1.18M
        cbp = (cbpc & 3) | (cbpy << 2);
1045
1.18M
        if (dquant) {
1046
227k
            h263_decode_dquant(h);
1047
227k
        }
1048
1049
1.18M
        pb_mv_count += !!h->pb_frame;
1050
1.18M
    }
1051
1052
5.63M
    while(pb_mv_count--){
1053
375k
        ff_h263_decode_motion(h, 0, 1);
1054
375k
        ff_h263_decode_motion(h, 0, 1);
1055
375k
    }
1056
1057
    /* decode each block */
1058
33.6M
    for (i = 0; i < 6; i++) {
1059
29.1M
        if (h263_decode_block(h, h->block[i], i, cbp&32) < 0)
1060
722k
            return -1;
1061
28.3M
        cbp+=cbp;
1062
28.3M
    }
1063
1064
4.54M
    if (h->pb_frame && h263_skip_b_part(h, cbpb) < 0)
1065
16.3k
        return -1;
1066
4.52M
    if (h->c.obmc && !h->c.mb_intra) {
1067
834k
        if (h->c.pict_type == AV_PICTURE_TYPE_P &&
1068
700k
            h->c.mb_x + 1 < h->c.mb_width && h->mb_num_left != 1)
1069
681k
            preview_obmc(h);
1070
834k
    }
1071
21.5M
end:
1072
1073
21.5M
    if (get_bits_left(&h->gb) < 0)
1074
45.1k
        return AVERROR_INVALIDDATA;
1075
1076
        /* per-MB end of slice check */
1077
21.4M
    {
1078
21.4M
        int v = show_bits(&h->gb, 16);
1079
1080
21.4M
        if (get_bits_left(&h->gb) < 16) {
1081
148k
            v >>= 16 - get_bits_left(&h->gb);
1082
148k
        }
1083
1084
21.4M
        if(v==0)
1085
310k
            return SLICE_END;
1086
21.4M
    }
1087
1088
21.1M
    return SLICE_OK;
1089
21.4M
}
1090
1091
/* Most is hardcoded; should extend to handle all H.263 streams. */
1092
int ff_h263_decode_picture_header(H263DecContext *const h)
1093
658k
{
1094
658k
    int width, height, i, ret;
1095
658k
    int h263_plus;
1096
1097
658k
    align_get_bits(&h->gb);
1098
1099
658k
    if (show_bits(&h->gb, 2) == 2 && h->c.avctx->frame_num == 0) {
1100
35.4k
         av_log(h->c.avctx, AV_LOG_WARNING, "Header looks like RTP instead of H.263\n");
1101
35.4k
    }
1102
1103
658k
    uint32_t startcode = get_bits(&h->gb, 22-8);
1104
1105
3.03M
    for (i = get_bits_left(&h->gb); i>24; i -= 8) {
1106
2.70M
        startcode = ((startcode << 8) | get_bits(&h->gb, 8)) & 0x003FFFFF;
1107
1108
2.70M
        if(startcode == 0x20)
1109
330k
            break;
1110
2.70M
    }
1111
1112
658k
    if (startcode != 0x20) {
1113
327k
        av_log(h->c.avctx, AV_LOG_ERROR, "Bad picture start code\n");
1114
327k
        return -1;
1115
327k
    }
1116
    /* temporal reference */
1117
331k
    i = get_bits(&h->gb, 8); /* picture timestamp */
1118
1119
331k
    i -= (i - (h->picture_number & 0xFF) + 128) & ~0xFF;
1120
1121
331k
    h->picture_number = (h->picture_number&~0xFF) + i;
1122
1123
    /* PTYPE starts here */
1124
331k
    if (check_marker(h->c.avctx, &h->gb, "in PTYPE") != 1) {
1125
7.67k
        return -1;
1126
7.67k
    }
1127
323k
    if (get_bits1(&h->gb) != 0) {
1128
2.73k
        av_log(h->c.avctx, AV_LOG_ERROR, "Bad H.263 id\n");
1129
2.73k
        return -1;      /* H.263 id */
1130
2.73k
    }
1131
320k
    skip_bits1(&h->gb);         /* split screen off */
1132
320k
    skip_bits1(&h->gb);         /* camera  off */
1133
320k
    skip_bits1(&h->gb);         /* freeze picture release off */
1134
1135
320k
    int format = get_bits(&h->gb, 3);
1136
    /*
1137
        0    forbidden
1138
        1    sub-QCIF
1139
        10   QCIF
1140
        7       extended PTYPE (PLUSPTYPE)
1141
    */
1142
1143
320k
    if (format != 7 && format != 6) {
1144
230k
        h263_plus = 0;
1145
        /* H.263v1 */
1146
230k
        width = ff_h263_format[format][0];
1147
230k
        height = ff_h263_format[format][1];
1148
230k
        if (!width)
1149
4.37k
            return -1;
1150
1151
226k
        h->c.pict_type = AV_PICTURE_TYPE_I + get_bits1(&h->gb);
1152
1153
226k
        h->h263_long_vectors = get_bits1(&h->gb);
1154
1155
226k
        if (get_bits1(&h->gb) != 0) {
1156
1.67k
            av_log(h->c.avctx, AV_LOG_ERROR, "H.263 SAC not supported\n");
1157
1.67k
            return -1; /* SAC: off */
1158
1.67k
        }
1159
224k
        h->c.obmc = get_bits1(&h->gb); /* Advanced prediction mode */
1160
1161
224k
        h->pb_frame = get_bits1(&h->gb);
1162
224k
        h->c.chroma_qscale = h->c.qscale = get_bits(&h->gb, 5);
1163
224k
        skip_bits1(&h->gb); /* Continuous Presence Multipoint mode: off */
1164
1165
224k
        h->c.width = width;
1166
224k
        h->c.height = height;
1167
224k
        h->c.avctx->sample_aspect_ratio= (AVRational){12,11};
1168
224k
        h->c.avctx->framerate = (AVRational){ 30000, 1001 };
1169
224k
    } else {
1170
89.9k
        int ufep;
1171
1172
        /* H.263v2 */
1173
89.9k
        h263_plus = 1;
1174
89.9k
        ufep = get_bits(&h->gb, 3); /* Update Full Extended PTYPE */
1175
1176
        /* ufep other than 0 and 1 are reserved */
1177
89.9k
        if (ufep == 1) {
1178
            /* OPPTYPE */
1179
45.9k
            format = get_bits(&h->gb, 3);
1180
45.9k
            ff_dlog(h->c.avctx, "ufep=1, format: %d\n", format);
1181
45.9k
            h->custom_pcf = get_bits1(&h->gb);
1182
45.9k
            h->umvplus    = get_bits1(&h->gb); /* Unrestricted Motion Vector */
1183
45.9k
            if (get_bits1(&h->gb) != 0) {
1184
16.6k
                av_log(h->c.avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
1185
16.6k
            }
1186
45.9k
            h->c.obmc        = get_bits1(&h->gb); /* Advanced prediction mode */
1187
45.9k
            h->c.h263_aic    = get_bits1(&h->gb); /* Advanced Intra Coding (AIC) */
1188
45.9k
            h->loop_filter = get_bits1(&h->gb);
1189
45.9k
            if (h->c.avctx->lowres)
1190
21.0k
                h->loop_filter = 0;
1191
1192
45.9k
            h->h263_slice_structured = get_bits1(&h->gb);
1193
45.9k
            if (get_bits1(&h->gb) != 0) {
1194
12.4k
                av_log(h->c.avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
1195
12.4k
            }
1196
45.9k
            if (get_bits1(&h->gb) != 0) {
1197
15.5k
                av_log(h->c.avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
1198
15.5k
            }
1199
45.9k
            h->alt_inter_vlc  = get_bits1(&h->gb);
1200
45.9k
            h->modified_quant = get_bits1(&h->gb);
1201
45.9k
            if (h->modified_quant)
1202
16.1k
                h->c.chroma_qscale_table= ff_h263_chroma_qscale_table;
1203
1204
45.9k
            skip_bits(&h->gb, 1); /* Prevent start code emulation */
1205
1206
45.9k
            skip_bits(&h->gb, 3); /* Reserved */
1207
45.9k
        } else if (ufep != 0) {
1208
1.94k
            av_log(h->c.avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
1209
1.94k
            return -1;
1210
1.94k
        }
1211
1212
        /* MPPTYPE */
1213
87.9k
        h->c.pict_type = get_bits(&h->gb, 3);
1214
87.9k
        switch (h->c.pict_type) {
1215
14.6k
        case 0: h->c.pict_type = AV_PICTURE_TYPE_I; break;
1216
13.4k
        case 1: h->c.pict_type = AV_PICTURE_TYPE_P; break;
1217
14.1k
        case 2: h->c.pict_type = AV_PICTURE_TYPE_P; h->pb_frame = 3; break;
1218
40.5k
        case 3: h->c.pict_type = AV_PICTURE_TYPE_B; break;
1219
3.13k
        case 7: h->c.pict_type = AV_PICTURE_TYPE_I; break; //ZYGO
1220
2.01k
        default:
1221
2.01k
            return -1;
1222
87.9k
        }
1223
85.9k
        skip_bits(&h->gb, 2);
1224
85.9k
        h->c.no_rounding = get_bits1(&h->gb);
1225
85.9k
        skip_bits(&h->gb, 4);
1226
1227
        /* Get the picture dimensions */
1228
85.9k
        if (ufep) {
1229
45.0k
            if (format == 6) {
1230
                /* Custom Picture Format (CPFMT) */
1231
22.7k
                int aspect_ratio_info = get_bits(&h->gb, 4);
1232
22.7k
                ff_dlog(h->c.avctx, "aspect: %d\n", aspect_ratio_info);
1233
                /* aspect ratios:
1234
                0 - forbidden
1235
                1 - 1:1
1236
                2 - 12:11 (CIF 4:3)
1237
                3 - 10:11 (525-type 4:3)
1238
                4 - 16:11 (CIF 16:9)
1239
                5 - 40:33 (525-type 16:9)
1240
                6-14 - reserved
1241
                */
1242
22.7k
                width = (get_bits(&h->gb, 9) + 1) * 4;
1243
22.7k
                check_marker(h->c.avctx, &h->gb, "in dimensions");
1244
22.7k
                height = get_bits(&h->gb, 9) * 4;
1245
22.7k
                ff_dlog(h->c.avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
1246
22.7k
                if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
1247
                    /* expected dimensions */
1248
4.61k
                    h->c.avctx->sample_aspect_ratio.num = get_bits(&h->gb, 8);
1249
4.61k
                    h->c.avctx->sample_aspect_ratio.den = get_bits(&h->gb, 8);
1250
18.1k
                }else{
1251
18.1k
                    h->c.avctx->sample_aspect_ratio= ff_h263_pixel_aspect[aspect_ratio_info];
1252
18.1k
                }
1253
22.7k
            } else {
1254
22.2k
                width = ff_h263_format[format][0];
1255
22.2k
                height = ff_h263_format[format][1];
1256
22.2k
                h->c.avctx->sample_aspect_ratio = (AVRational){12,11};
1257
22.2k
            }
1258
45.0k
            h->c.avctx->sample_aspect_ratio.den <<= h->ehc_mode;
1259
45.0k
            if ((width == 0) || (height == 0))
1260
2.44k
                return -1;
1261
42.5k
            h->c.width  = width;
1262
42.5k
            h->c.height = height;
1263
1264
42.5k
            if (h->custom_pcf) {
1265
20.2k
                h->c.avctx->framerate.num  = 1800000;
1266
20.2k
                h->c.avctx->framerate.den  = 1000 + get_bits1(&h->gb);
1267
20.2k
                h->c.avctx->framerate.den *= get_bits(&h->gb, 7);
1268
20.2k
                if (h->c.avctx->framerate.den == 0) {
1269
1.15k
                    av_log(h->c.avctx, AV_LOG_ERROR, "zero framerate\n");
1270
1.15k
                    return -1;
1271
1.15k
                }
1272
19.0k
                int gcd = av_gcd(h->c.avctx->framerate.den, h->c.avctx->framerate.num);
1273
19.0k
                h->c.avctx->framerate.den /= gcd;
1274
19.0k
                h->c.avctx->framerate.num /= gcd;
1275
22.3k
            }else{
1276
22.3k
                h->c.avctx->framerate = (AVRational){ 30000, 1001 };
1277
22.3k
            }
1278
42.5k
        }
1279
1280
82.3k
        if (h->custom_pcf)
1281
27.7k
            skip_bits(&h->gb, 2); //extended Temporal reference
1282
1283
82.3k
        if (ufep) {
1284
41.4k
            if (h->umvplus) {
1285
25.5k
                if (get_bits1(&h->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
1286
13.7k
                    skip_bits1(&h->gb);
1287
25.5k
            }
1288
41.4k
            if (h->h263_slice_structured) {
1289
7.40k
                if (get_bits1(&h->gb) != 0) {
1290
3.32k
                    av_log(h->c.avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
1291
3.32k
                }
1292
7.40k
                if (get_bits1(&h->gb) != 0) {
1293
4.25k
                    av_log(h->c.avctx, AV_LOG_ERROR, "unordered slices not supported\n");
1294
4.25k
                }
1295
7.40k
            }
1296
41.4k
            if (h->c.pict_type == AV_PICTURE_TYPE_B) {
1297
5.05k
                skip_bits(&h->gb, 4); //ELNUM
1298
5.05k
                if (ufep == 1) {
1299
5.05k
                    skip_bits(&h->gb, 4); // RLNUM
1300
5.05k
                }
1301
5.05k
            }
1302
41.4k
        }
1303
1304
82.3k
        h->c.qscale = get_bits(&h->gb, 5);
1305
82.3k
    }
1306
1307
307k
    ret = av_image_check_size(h->c.width, h->c.height, 0, h->c.avctx);
1308
307k
    if (ret < 0)
1309
583
        return ret;
1310
1311
306k
    if (!(h->c.avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
1312
306k
        if ((h->c.width * h->c.height / 256 / 8) > get_bits_left(&h->gb))
1313
22.1k
            return AVERROR_INVALIDDATA;
1314
306k
    }
1315
1316
284k
    h->c.mb_width  = (h->c.width  + 15U) / 16;
1317
284k
    h->c.mb_height = (h->c.height + 15U) / 16;
1318
284k
    h->c.mb_num    = h->c.mb_width * h->c.mb_height;
1319
1320
284k
    h->gob_index = H263_GOB_HEIGHT(h->c.height);
1321
1322
284k
    if (h->pb_frame) {
1323
51.7k
        skip_bits(&h->gb, 3); /* Temporal reference for B-pictures */
1324
51.7k
        if (h->custom_pcf)
1325
16.3k
            skip_bits(&h->gb, 2); //extended Temporal reference
1326
51.7k
        skip_bits(&h->gb, 2); /* Quantization information for B-pictures */
1327
51.7k
    }
1328
1329
284k
    if (h->c.pict_type!=AV_PICTURE_TYPE_B) {
1330
246k
        h->c.time            = h->picture_number;
1331
246k
        h->c.pp_time         = h->c.time - h->c.last_non_b_time;
1332
246k
        h->c.last_non_b_time = h->c.time;
1333
246k
    }else{
1334
38.0k
        h->c.time    = h->picture_number;
1335
38.0k
        h->c.pb_time = h->c.pp_time - (h->c.last_non_b_time - h->c.time);
1336
38.0k
        if (h->c.pp_time <= h->c.pb_time ||
1337
4.24k
            h->c.pp_time <= h->c.pp_time - h->c.pb_time ||
1338
34.2k
            h->c.pp_time <= 0) {
1339
34.2k
            h->c.pp_time = 2;
1340
34.2k
            h->c.pb_time = 1;
1341
34.2k
        }
1342
38.0k
        ff_mpeg4_init_direct_mv(&h->c);
1343
38.0k
    }
1344
1345
    /* PEI */
1346
284k
    if (skip_1stop_8data_bits(&h->gb) < 0)
1347
2.45k
        return AVERROR_INVALIDDATA;
1348
1349
282k
    if (h->h263_slice_structured) {
1350
10.3k
        if (check_marker(h->c.avctx, &h->gb, "SEPB1") != 1) {
1351
4.24k
            return -1;
1352
4.24k
        }
1353
1354
6.12k
        ff_h263_decode_mba(h);
1355
1356
6.12k
        if (check_marker(h->c.avctx, &h->gb, "SEPB2") != 1) {
1357
1.79k
            return -1;
1358
1.79k
        }
1359
6.12k
    }
1360
1361
276k
    if (h->c.pict_type == AV_PICTURE_TYPE_B)
1362
36.8k
        h->c.low_delay = 0;
1363
1364
276k
    if (h->c.h263_aic) {
1365
149k
         h->c.y_dc_scale_table =
1366
149k
         h->c.c_dc_scale_table = ff_aic_dc_scale_table;
1367
149k
    }else{
1368
126k
        h->c.y_dc_scale_table =
1369
126k
        h->c.c_dc_scale_table = ff_mpeg1_dc_scale_table;
1370
126k
    }
1371
1372
276k
    ff_h263_show_pict_info(h, h263_plus);
1373
1374
276k
    if (h->c.pict_type == AV_PICTURE_TYPE_I && h->c.codec_tag == AV_RL32("ZYGO") && get_bits_left(&h->gb) >= 85 + 13*3*16 + 50){
1375
2.04k
        int i,j;
1376
175k
        for(i=0; i<85; i++) av_log(h->c.avctx, AV_LOG_DEBUG, "%d", get_bits1(&h->gb));
1377
2.04k
        av_log(h->c.avctx, AV_LOG_DEBUG, "\n");
1378
28.5k
        for(i=0; i<13; i++){
1379
106k
            for(j=0; j<3; j++){
1380
79.5k
                int v= get_bits(&h->gb, 8);
1381
79.5k
                v |= get_sbits(&h->gb, 8) * (1 << 8);
1382
79.5k
                av_log(h->c.avctx, AV_LOG_DEBUG, " %5d", v);
1383
79.5k
            }
1384
26.5k
            av_log(h->c.avctx, AV_LOG_DEBUG, "\n");
1385
26.5k
        }
1386
104k
        for(i=0; i<50; i++) av_log(h->c.avctx, AV_LOG_DEBUG, "%d", get_bits1(&h->gb));
1387
2.04k
    }
1388
1389
276k
    return 0;
1390
282k
}