Coverage Report

Created: 2026-01-16 07:48

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
339k
#define H263_MBTYPE_B_VLC_BITS 6
60
87.2k
#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
389k
{
82
389k
    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
389k
}
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
50.9k
{
138
50.9k
    static AVOnce init_static_once = AV_ONCE_INIT;
139
50.9k
    ff_thread_once(&init_static_once, h263_decode_init_vlc);
140
50.9k
}
141
142
int ff_h263_decode_mba(H263DecContext *const h)
143
109k
{
144
109k
    int i, mb_pos;
145
146
402k
    for (i = 0; i < 6; i++)
147
400k
        if (h->c.mb_num - 1 <= ff_mba_max[i])
148
107k
            break;
149
109k
    mb_pos    = get_bits(&h->gb, ff_mba_length[i]);
150
109k
    h->c.mb_x = mb_pos % h->c.mb_width;
151
109k
    h->c.mb_y = mb_pos / h->c.mb_width;
152
153
109k
    return mb_pos;
154
109k
}
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
7.49M
{
162
7.49M
    unsigned int val, gob_number;
163
7.49M
    int left;
164
165
    /* Check for GOB Start Code */
166
7.49M
    val = show_bits(&h->gb, 16);
167
7.49M
    if(val)
168
0
        return -1;
169
170
        /* We have a GBSC probably with GSTUFF */
171
7.49M
    skip_bits(&h->gb, 16); /* Drop the zeros */
172
7.49M
    left = get_bits_left(&h->gb);
173
7.49M
    left = FFMIN(left, 32);
174
    //MN: we must check the bits left or we might end in an infinite loop (or segfault)
175
118M
    for(;left>13; left--){
176
113M
        if (get_bits1(&h->gb))
177
1.96M
            break; /* Seek the '1' bit */
178
113M
    }
179
7.49M
    if(left<=13)
180
5.52M
        return -1;
181
182
1.96M
    if (h->h263_slice_structured) {
183
73.7k
        if (check_marker(h->c.avctx, &h->gb, "before MBA")==0)
184
32.2k
            return -1;
185
186
41.5k
        ff_h263_decode_mba(h);
187
188
41.5k
        if (h->c.mb_num > 1583)
189
33.0k
            if (check_marker(h->c.avctx, &h->gb, "after MBA")==0)
190
20.8k
                return -1;
191
192
20.7k
        h->c.qscale = get_bits(&h->gb, 5); /* SQUANT */
193
20.7k
        if (check_marker(h->c.avctx, &h->gb, "after SQUANT")==0)
194
5.86k
            return -1;
195
14.8k
        skip_bits(&h->gb, 2); /* GFID */
196
1.89M
    }else{
197
1.89M
        gob_number = get_bits(&h->gb, 5); /* GN */
198
1.89M
        h->c.mb_x = 0;
199
1.89M
        h->c.mb_y = h->gob_index* gob_number;
200
1.89M
        skip_bits(&h->gb, 2); /* GFID */
201
1.89M
        h->c.qscale = get_bits(&h->gb, 5); /* GQUANT */
202
1.89M
    }
203
204
1.90M
    if (h->c.mb_y >= h->c.mb_height)
205
510k
        return -1;
206
207
1.39M
    if (h->c.qscale==0)
208
543k
        return -1;
209
210
855k
    return 0;
211
1.39M
}
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.47M
{
219
1.47M
    int left, pos, ret;
220
221
    /* In MPEG-4 studio mode look for a new slice startcode
222
     * and decode slice header */
223
1.47M
    if (h->c.codec_id==AV_CODEC_ID_MPEG4 && h->c.studio_profile) {
224
21.7k
        align_get_bits(&h->gb);
225
226
1.39M
        while (get_bits_left(&h->gb) >= 32 && show_bits_long(&h->gb, 32) != SLICE_STARTCODE) {
227
1.37M
            get_bits(&h->gb, 8);
228
1.37M
        }
229
230
21.7k
        if (get_bits_left(&h->gb) >= 32 && show_bits_long(&h->gb, 32) == SLICE_STARTCODE)
231
16.0k
            return get_bits_count(&h->gb);
232
5.64k
        else
233
5.64k
            return -1;
234
21.7k
    }
235
236
1.44M
    if (h->c.codec_id==AV_CODEC_ID_MPEG4){
237
214k
        skip_bits1(&h->gb);
238
214k
        align_get_bits(&h->gb);
239
214k
    }
240
241
1.44M
    if (show_bits(&h->gb, 16) ==0) {
242
799k
        pos = get_bits_count(&h->gb);
243
799k
#if CONFIG_MPEG4_DECODER
244
799k
        if (h->c.codec_id == AV_CODEC_ID_MPEG4)
245
44.1k
            ret = ff_mpeg4_decode_video_packet_header(h);
246
755k
        else
247
755k
#endif
248
755k
            ret = h263_decode_gob_header(h);
249
799k
        if(ret>=0)
250
282k
            return pos;
251
799k
    }
252
    //OK, it's not where it is supposed to be ...
253
1.16M
    h->gb = h->last_resync_gb;
254
1.16M
    align_get_bits(&h->gb);
255
1.16M
    left = get_bits_left(&h->gb);
256
257
46.3M
    for(;left>16+1+5+5; left-=8){
258
45.8M
        if (show_bits(&h->gb, 16) == 0){
259
7.38M
            GetBitContext bak = h->gb;
260
261
7.38M
            pos = get_bits_count(&h->gb);
262
7.38M
#if CONFIG_MPEG4_DECODER
263
7.38M
            if (h->c.codec_id == AV_CODEC_ID_MPEG4)
264
642k
                ret = ff_mpeg4_decode_video_packet_header(h);
265
6.74M
            else
266
6.74M
#endif
267
6.74M
                ret = h263_decode_gob_header(h);
268
7.38M
            if(ret>=0)
269
657k
                return pos;
270
271
6.72M
            h->gb = bak;
272
6.72M
        }
273
45.1M
        skip_bits(&h->gb, 8);
274
45.1M
    }
275
276
508k
    return -1;
277
1.16M
}
278
279
int ff_h263_decode_motion(H263DecContext *const h, int pred, int f_code)
280
15.1M
{
281
15.1M
    int code, val, sign, shift;
282
15.1M
    code = get_vlc2(&h->gb, ff_h263_mv_vlc, H263_MV_VLC_BITS, 2);
283
284
15.1M
    if (code == 0)
285
5.26M
        return pred;
286
9.90M
    if (code < 0)
287
179k
        return 0xffff;
288
289
9.72M
    sign = get_bits1(&h->gb);
290
9.72M
    shift = f_code - 1;
291
9.72M
    val = code;
292
9.72M
    if (shift) {
293
290k
        val = (val - 1) << shift;
294
290k
        val |= get_bits(&h->gb, shift);
295
290k
        val++;
296
290k
    }
297
9.72M
    if (sign)
298
4.16M
        val = -val;
299
9.72M
    val += pred;
300
301
    /* modulo decoding */
302
9.72M
    if (!h->h263_long_vectors) {
303
7.64M
        val = sign_extend(val, 5 + f_code);
304
7.64M
    } else {
305
        /* horrible H.263 long vector mode */
306
2.07M
        if (pred < -31 && val < -63)
307
3.67k
            val += 64;
308
2.07M
        if (pred > 32 && val > 63)
309
7.05k
            val -= 64;
310
311
2.07M
    }
312
9.72M
    return val;
313
9.90M
}
314
315
316
/* Decode RVLC of H.263+ UMV */
317
static int h263p_decode_umotion(H263DecContext *const h, int pred)
318
2.10M
{
319
2.10M
   int code = 0, sign;
320
321
2.10M
   if (get_bits1(&h->gb)) /* Motion difference = 0 */
322
747k
      return pred;
323
324
1.35M
   code = 2 + get_bits1(&h->gb);
325
326
2.12M
   while (get_bits1(&h->gb))
327
772k
   {
328
772k
      code <<= 1;
329
772k
      code += get_bits1(&h->gb);
330
772k
      if (code >= 32768) {
331
977
          avpriv_request_sample(h->c.avctx, "Huge DMV");
332
977
          return 0xffff;
333
977
      }
334
772k
   }
335
1.35M
   sign = code & 1;
336
1.35M
   code >>= 1;
337
338
1.35M
   code = (sign) ? (pred - code) : (pred + code);
339
1.35M
   ff_tlog(h->c.avctx,"H.263+ UMV Motion = %d\n", code);
340
1.35M
   return code;
341
342
1.35M
}
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
782k
{
349
782k
    GetBitContext gb = h->gb;
350
351
782k
    int cbpc, i, pred_x, pred_y, mx, my;
352
782k
    int16_t *mot_val;
353
782k
    const int xy     = h->c.mb_x + 1 + h->c.mb_y * h->c.mb_stride;
354
782k
    const int stride = h->c.b8_stride * 2;
355
356
3.91M
    for(i=0; i<4; i++)
357
3.13M
        h->c.block_index[i] += 2;
358
2.34M
    for(i=4; i<6; i++)
359
1.56M
        h->c.block_index[i] += 1;
360
782k
    h->c.mb_x++;
361
362
782k
    av_assert2(h->c.pict_type == AV_PICTURE_TYPE_P);
363
364
799k
    do{
365
799k
        if (get_bits1(&h->gb)) {
366
            /* skip mb */
367
320k
            mot_val = h->c.cur_pic.motion_val[0][h->c.block_index[0]];
368
320k
            mot_val[0       ]= mot_val[2       ]=
369
320k
            mot_val[0+stride]= mot_val[2+stride]= 0;
370
320k
            mot_val[1       ]= mot_val[3       ]=
371
320k
            mot_val[1+stride]= mot_val[3+stride]= 0;
372
373
320k
            h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
374
320k
            goto end;
375
320k
        }
376
479k
        cbpc = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2);
377
479k
    }while(cbpc == 20);
378
379
462k
    if(cbpc & 4){
380
49.9k
        h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA;
381
412k
    }else{
382
412k
        get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
383
412k
        if (cbpc & 8) {
384
35.6k
            skip_bits(&h->gb, h->modified_quant ? (get_bits1(&h->gb) ? 1 : 5) : 2);
385
35.6k
        }
386
387
412k
        if ((cbpc & 16) == 0) {
388
281k
                h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
389
                /* 16x16 motion prediction */
390
281k
                mot_val= ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
391
281k
                if (h->umvplus)
392
66.2k
                    mx = h263p_decode_umotion(h, pred_x);
393
215k
                else
394
215k
                    mx = ff_h263_decode_motion(h, pred_x, 1);
395
396
281k
                if (h->umvplus)
397
66.2k
                    my = h263p_decode_umotion(h, pred_y);
398
215k
                else
399
215k
                    my = ff_h263_decode_motion(h, pred_y, 1);
400
401
281k
                mot_val[0       ]= mot_val[2       ]=
402
281k
                mot_val[0+stride]= mot_val[2+stride]= mx;
403
281k
                mot_val[1       ]= mot_val[3       ]=
404
281k
                mot_val[1+stride]= mot_val[3+stride]= my;
405
281k
        } else {
406
131k
            h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_FORWARD_MV;
407
656k
            for(i=0;i<4;i++) {
408
525k
                mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y);
409
525k
                if (h->umvplus)
410
174k
                    mx = h263p_decode_umotion(h, pred_x);
411
351k
                else
412
351k
                    mx = ff_h263_decode_motion(h, pred_x, 1);
413
414
525k
                if (h->umvplus)
415
174k
                    my = h263p_decode_umotion(h, pred_y);
416
351k
                else
417
351k
                    my = ff_h263_decode_motion(h, pred_y, 1);
418
525k
                if (h->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
419
29.9k
                    skip_bits1(&h->gb); /* Bit stuffing to prevent PSC */
420
525k
                mot_val[0] = mx;
421
525k
                mot_val[1] = my;
422
525k
            }
423
131k
        }
424
412k
    }
425
782k
end:
426
427
3.91M
    for(i=0; i<4; i++)
428
3.13M
        h->c.block_index[i] -= 2;
429
2.34M
    for(i=4; i<6; i++)
430
1.56M
        h->c.block_index[i] -= 1;
431
782k
    h->c.mb_x--;
432
433
782k
    h->gb = gb;
434
782k
}
435
436
static void h263_decode_dquant(H263DecContext *const h)
437
803k
{
438
803k
    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
439
803k
    int qscale;
440
441
803k
    if (h->modified_quant) {
442
190k
        if (get_bits1(&h->gb))
443
40.9k
            qscale = ff_modified_quant_tab[get_bits1(&h->gb)][h->c.qscale];
444
149k
        else
445
149k
            qscale = get_bits(&h->gb, 5);
446
190k
    }else
447
612k
        qscale = h->c.qscale + quant_tab[get_bits(&h->gb, 2)];
448
803k
    ff_set_qscale(&h->c, qscale);
449
803k
}
450
451
static void h263_pred_acdc(MpegEncContext * s, int16_t *block, int n)
452
1.36M
{
453
1.36M
    int wrap, a, c, pred_dc, scale;
454
1.36M
    const int xy = s->block_index[n];
455
1.36M
    int16_t *const dc_val =  s->dc_val + xy;
456
1.36M
    int16_t *const ac_val = (s->ac_val + xy)[0];
457
458
    /* find prediction */
459
1.36M
    if (n < 4) {
460
921k
        wrap = s->b8_stride;
461
921k
        scale = s->y_dc_scale;
462
921k
    } else {
463
445k
        wrap = s->mb_stride;
464
445k
        scale = s->c_dc_scale;
465
445k
    }
466
467
    /* B C
468
     * A X
469
     */
470
1.36M
    a = dc_val[-1];
471
1.36M
    c = dc_val[-wrap];
472
473
    /* No prediction outside GOB boundary */
474
1.36M
    if (s->first_slice_line && n != 3) {
475
717k
        if (n != 2) c= 1024;
476
717k
        if (n != 1 && s->mb_x == s->resync_mb_x) a= 1024;
477
717k
    }
478
479
1.36M
    if (s->ac_pred) {
480
795k
        pred_dc = 1024;
481
795k
        if (s->h263_aic_dir) {
482
            /* left prediction */
483
445k
            if (a != 1024) {
484
399k
                int16_t *const ac_val2 = ac_val - 16;
485
3.19M
                for (int i = 1; i < 8; i++) {
486
2.79M
                    block[s->idsp.idct_permutation[i << 3]] += ac_val2[i];
487
2.79M
                }
488
399k
                pred_dc = a;
489
399k
            }
490
445k
        } else {
491
            /* top prediction */
492
349k
            if (c != 1024) {
493
249k
                int16_t *const ac_val2 = ac_val - 16 * wrap;
494
1.99M
                for (int i = 1; i < 8; i++) {
495
1.74M
                    block[s->idsp.idct_permutation[i]] += ac_val2[i + 8];
496
1.74M
                }
497
249k
                pred_dc = c;
498
249k
            }
499
349k
        }
500
795k
    } else {
501
        /* just DC prediction */
502
571k
        if (a != 1024 && c != 1024)
503
291k
            pred_dc = (a + c) >> 1;
504
280k
        else if (a != 1024)
505
210k
            pred_dc = a;
506
69.8k
        else
507
69.8k
            pred_dc = c;
508
571k
    }
509
510
    /* we assume pred is positive */
511
1.36M
    block[0] = block[0] * scale + pred_dc;
512
513
1.36M
    if (block[0] < 0)
514
7.16k
        block[0] = 0;
515
1.35M
    else
516
1.35M
        block[0] |= 1;
517
518
    /* Update AC/DC tables */
519
1.36M
    *dc_val = block[0];
520
521
    /* left copy */
522
10.9M
    for (int i = 1; i < 8; i++)
523
9.56M
        ac_val[i]     = block[s->idsp.idct_permutation[i << 3]];
524
    /* top copy */
525
10.9M
    for (int i = 1; i < 8; i++)
526
9.56M
        ac_val[8 + i] = block[s->idsp.idct_permutation[i]];
527
1.36M
}
528
529
static int h263_decode_block(H263DecContext *const h, int16_t block[64],
530
                             int n, int coded)
531
28.0M
{
532
28.0M
    int level, i, j, run;
533
28.0M
    const RLTable *rl = &ff_h263_rl_inter;
534
28.0M
    const uint8_t *scan_table;
535
28.0M
    GetBitContext gb = h->gb;
536
537
28.0M
    scan_table = h->c.intra_scantable.permutated;
538
28.0M
    if (h->c.h263_aic && h->c.mb_intra) {
539
1.40M
        i = 0;
540
1.40M
        if (!coded)
541
768k
            goto not_coded;
542
633k
        rl = &ff_rl_intra_aic;
543
633k
        if (h->c.ac_pred) {
544
405k
            if (h->c.h263_aic_dir)
545
199k
                scan_table = h->permutated_intra_v_scantable; /* left */
546
205k
            else
547
205k
                scan_table = h->permutated_intra_h_scantable; /* top */
548
405k
        }
549
26.6M
    } else if (h->c.mb_intra) {
550
        /* DC coef */
551
4.17M
        if (CONFIG_RV10_DECODER && h->c.codec_id == AV_CODEC_ID_RV10) {
552
766k
            if (h->rv10_version == 3 && h->c.pict_type == AV_PICTURE_TYPE_I) {
553
722k
                int component = (n <= 3 ? 0 : n - 4 + 1);
554
722k
                level = h->last_dc[component];
555
722k
                if (h->rv10_first_dc_coded[component]) {
556
698k
                    int diff = ff_rv_decode_dc(h, n);
557
698k
                    if (diff < 0)
558
272
                        return -1;
559
698k
                    level += diff;
560
698k
                    level = level & 0xff; /* handle wrap round */
561
698k
                    h->last_dc[component] = level;
562
698k
                } else {
563
23.3k
                    h->rv10_first_dc_coded[component] = 1;
564
23.3k
                }
565
722k
            } else {
566
44.4k
                level = get_bits(&h->gb, 8);
567
44.4k
                if (level == 255)
568
959
                    level = 128;
569
44.4k
            }
570
3.40M
        }else{
571
3.40M
            level = get_bits(&h->gb, 8);
572
3.40M
            if((level&0x7F) == 0){
573
501k
                av_log(h->c.avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
574
501k
                       level, h->c.mb_x, h->c.mb_y);
575
501k
                if (h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT))
576
19.0k
                    return -1;
577
501k
            }
578
3.38M
            if (level == 255)
579
97.5k
                level = 128;
580
3.38M
        }
581
4.15M
        block[0] = level;
582
4.15M
        i = 1;
583
22.4M
    } else {
584
22.4M
        i = 0;
585
22.4M
    }
586
27.2M
    if (!coded) {
587
21.4M
        h->c.block_last_index[n] = i - 1;
588
21.4M
        return 0;
589
21.4M
    }
590
5.81M
retry:
591
5.81M
    {
592
5.81M
    OPEN_READER(re, &h->gb);
593
5.81M
    i--; // offset by -1 to allow direct indexing of scan_table
594
17.3M
    for(;;) {
595
17.3M
        UPDATE_CACHE(re, &h->gb);
596
17.3M
        GET_RL_VLC(level, run, re, &h->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
597
17.3M
        if (run == 66) {
598
812k
            if (level){
599
648k
                CLOSE_READER(re, &h->gb);
600
648k
                av_log(h->c.avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
601
648k
                       h->c.mb_x, h->c.mb_y);
602
648k
                return -1;
603
648k
            }
604
            /* escape */
605
163k
            if (CONFIG_FLV_DECODER && h->flv) {
606
6.35k
                int is11 = SHOW_UBITS(re, &h->gb, 1);
607
6.35k
                SKIP_CACHE(re, &h->gb, 1);
608
6.35k
                run = SHOW_UBITS(re, &h->gb, 7) + 1;
609
6.35k
                if (is11) {
610
4.51k
                    SKIP_COUNTER(re, &h->gb, 1 + 7);
611
4.51k
                    UPDATE_CACHE(re, &h->gb);
612
4.51k
                    level = SHOW_SBITS(re, &h->gb, 11);
613
4.51k
                    SKIP_COUNTER(re, &h->gb, 11);
614
4.51k
                } else {
615
1.84k
                    SKIP_CACHE(re, &h->gb, 7);
616
1.84k
                    level = SHOW_SBITS(re, &h->gb, 7);
617
1.84k
                    SKIP_COUNTER(re, &h->gb, 1 + 7 + 7);
618
1.84k
                }
619
157k
            } else {
620
157k
                run = SHOW_UBITS(re, &h->gb, 7) + 1;
621
157k
                SKIP_CACHE(re, &h->gb, 7);
622
157k
                level = (int8_t)SHOW_UBITS(re, &h->gb, 8);
623
157k
                SKIP_COUNTER(re, &h->gb, 7 + 8);
624
157k
                if(level == -128){
625
18.9k
                    UPDATE_CACHE(re, &h->gb);
626
18.9k
                    if (h->c.codec_id == AV_CODEC_ID_RV10) {
627
                        /* XXX: should patch encoder too */
628
312
                        level = SHOW_SBITS(re, &h->gb, 12);
629
312
                        SKIP_COUNTER(re, &h->gb, 12);
630
18.6k
                    }else{
631
18.6k
                        level = SHOW_UBITS(re, &h->gb, 5);
632
18.6k
                        SKIP_CACHE(re, &h->gb, 5);
633
18.6k
                        level |= SHOW_SBITS(re, &h->gb, 6) * (1<<5);
634
18.6k
                        SKIP_COUNTER(re, &h->gb, 5 + 6);
635
18.6k
                    }
636
18.9k
                }
637
157k
            }
638
16.4M
        } else {
639
16.4M
            if (SHOW_UBITS(re, &h->gb, 1))
640
7.25M
                level = -level;
641
16.4M
            SKIP_COUNTER(re, &h->gb, 1);
642
16.4M
        }
643
16.6M
        i += run;
644
16.6M
        if (i >= 64){
645
5.16M
            CLOSE_READER(re, &h->gb);
646
            // redo update without last flag, revert -1 offset
647
5.16M
            i = i - run + ((run-1)&63) + 1;
648
5.16M
            if (i < 64) {
649
                // only last marker, no overrun
650
5.11M
                block[scan_table[i]] = level;
651
5.11M
                break;
652
5.11M
            }
653
45.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.64k
                rl = &ff_rl_intra_aic;
656
4.64k
                i = 0;
657
4.64k
                h->gb = gb;
658
4.64k
                h->c.bdsp.clear_block(block);
659
4.64k
                goto retry;
660
4.64k
            }
661
40.6k
            av_log(h->c.avctx, AV_LOG_ERROR, "run overflow at %dx%d i:%d\n",
662
40.6k
                   h->c.mb_x, h->c.mb_y, h->c.mb_intra);
663
40.6k
            return -1;
664
45.3k
        }
665
11.4M
        j = scan_table[i];
666
11.4M
        block[j] = level;
667
11.4M
    }
668
5.81M
    }
669
5.11M
    if (h->c.mb_intra && h->c.h263_aic) {
670
1.36M
not_coded:
671
1.36M
        h263_pred_acdc(&h->c, block, n);
672
1.36M
    }
673
5.88M
    h->c.block_last_index[n] = i;
674
5.88M
    return 0;
675
5.11M
}
676
677
static int h263_skip_b_part(H263DecContext *const h, int cbp)
678
514k
{
679
514k
    LOCAL_ALIGNED_32(int16_t, dblock, [64]);
680
514k
    int i, mbi;
681
514k
    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
514k
    mbi = h->c.mb_intra;
687
514k
    memcpy(bli, h->c.block_last_index, sizeof(bli));
688
514k
    h->c.mb_intra = 0;
689
3.53M
    for (i = 0; i < 6; i++) {
690
3.03M
        if (h263_decode_block(h, dblock, i, cbp&32) < 0)
691
14.4k
            return -1;
692
3.01M
        cbp+=cbp;
693
3.01M
    }
694
499k
    h->c.mb_intra = mbi;
695
499k
    memcpy(h->c.block_last_index, bli, sizeof(bli));
696
499k
    return 0;
697
514k
}
698
699
static int h263_get_modb(GetBitContext *gb, int pb_frame, int *cbpb)
700
214k
{
701
214k
    int c, mv = 1;
702
703
214k
    if (pb_frame < 3) { // h.263 Annex G and i263 PB-frame
704
183k
        c = get_bits1(gb);
705
183k
        if (pb_frame == 2 && c)
706
4.52k
            mv = !get_bits1(gb);
707
183k
    } else { // h.263 Annex M improved PB-frame
708
31.2k
        mv = get_unary(gb, 0, 4) + 1;
709
31.2k
        c = mv & 1;
710
31.2k
        mv = !!(mv & 2);
711
31.2k
    }
712
214k
    if(c)
713
128k
        *cbpb = get_bits(gb, 6);
714
214k
    return mv;
715
214k
}
716
717
1.50M
#define tab_size ((signed)FF_ARRAY_ELEMS(s->direct_scale_mv[0]))
718
1.12M
#define tab_bias (tab_size / 2)
719
static inline void set_one_direct_mv(MpegEncContext *s, const MPVPicture *p, int i)
720
192k
{
721
192k
    int xy           = s->block_index[i];
722
192k
    uint16_t time_pp = s->pp_time;
723
192k
    uint16_t time_pb = s->pb_time;
724
192k
    int p_mx, p_my;
725
726
192k
    p_mx = p->motion_val[0][xy][0];
727
192k
    if ((unsigned)(p_mx + tab_bias) < tab_size) {
728
186k
        s->mv[0][i][0] = s->direct_scale_mv[0][p_mx + tab_bias];
729
186k
        s->mv[1][i][0] = s->direct_scale_mv[1][p_mx + tab_bias];
730
186k
    } else {
731
6.07k
        s->mv[0][i][0] = p_mx * time_pb / time_pp;
732
6.07k
        s->mv[1][i][0] = p_mx * (time_pb - time_pp) / time_pp;
733
6.07k
    }
734
192k
    p_my = p->motion_val[0][xy][1];
735
192k
    if ((unsigned)(p_my + tab_bias) < tab_size) {
736
181k
        s->mv[0][i][1] = s->direct_scale_mv[0][p_my + tab_bias];
737
181k
        s->mv[1][i][1] = s->direct_scale_mv[1][p_my + tab_bias];
738
181k
    } else {
739
11.3k
        s->mv[0][i][1] = p_my * time_pb / time_pp;
740
11.3k
        s->mv[1][i][1] = p_my * (time_pb - time_pp) / time_pp;
741
11.3k
    }
742
192k
}
743
744
/**
745
 * @return the mb_type
746
 */
747
static int set_direct_mv(MpegEncContext *s)
748
160k
{
749
160k
    const int mb_index = s->mb_x + s->mb_y * s->mb_stride;
750
160k
    const MPVPicture *p = s->next_pic.ptr;
751
160k
    int colocated_mb_type = p->mb_type[mb_index];
752
160k
    int i;
753
754
160k
    if (s->codec_tag == AV_RL32("U263") && p->f->pict_type == AV_PICTURE_TYPE_I) {
755
628
        p = s->last_pic.ptr;
756
628
        colocated_mb_type = p->mb_type[mb_index];
757
628
    }
758
759
160k
    if (IS_8X8(colocated_mb_type)) {
760
10.6k
        s->mv_type = MV_TYPE_8X8;
761
53.3k
        for (i = 0; i < 4; i++)
762
42.7k
            set_one_direct_mv(s, p, i);
763
10.6k
        return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_BIDIR_MV;
764
149k
    } else {
765
149k
        set_one_direct_mv(s, p, 0);
766
149k
        s->mv[0][1][0] =
767
149k
        s->mv[0][2][0] =
768
149k
        s->mv[0][3][0] = s->mv[0][0][0];
769
149k
        s->mv[0][1][1] =
770
149k
        s->mv[0][2][1] =
771
149k
        s->mv[0][3][1] = s->mv[0][0][1];
772
149k
        s->mv[1][1][0] =
773
149k
        s->mv[1][2][0] =
774
149k
        s->mv[1][3][0] = s->mv[1][0][0];
775
149k
        s->mv[1][1][1] =
776
149k
        s->mv[1][2][1] =
777
149k
        s->mv[1][3][1] = s->mv[1][0][1];
778
149k
        s->mv_type = MV_TYPE_8X8;
779
        // Note see prev line
780
149k
        return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_BIDIR_MV;
781
149k
    }
782
160k
}
783
784
int ff_h263_decode_mb(H263DecContext *const h)
785
17.1M
{
786
17.1M
    int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
787
17.1M
    int16_t *mot_val;
788
17.1M
    const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
789
17.1M
    int cbpb = 0, pb_mv_count = 0;
790
791
17.1M
    av_assert2(!h->c.h263_pred);
792
793
17.1M
    if (h->c.pict_type == AV_PICTURE_TYPE_P) {
794
15.9M
        do{
795
15.9M
            if (get_bits1(&h->gb)) {
796
                /* skip mb */
797
12.1M
                h->c.mb_intra = 0;
798
85.3M
                for(i=0;i<6;i++)
799
73.1M
                    h->c.block_last_index[i] = -1;
800
12.1M
                h->c.mv_dir  = MV_DIR_FORWARD;
801
12.1M
                h->c.mv_type = MV_TYPE_16X16;
802
12.1M
                h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
803
12.1M
                h->c.mv[0][0][0] = 0;
804
12.1M
                h->c.mv[0][0][1] = 0;
805
12.1M
                h->c.mb_skipped = !(h->c.obmc | h->loop_filter);
806
12.1M
                goto end;
807
12.1M
            }
808
3.80M
            cbpc = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2);
809
3.80M
            if (cbpc < 0){
810
65.1k
                av_log(h->c.avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n",
811
65.1k
                       h->c.mb_x, h->c.mb_y);
812
65.1k
                return SLICE_ERROR;
813
65.1k
            }
814
3.80M
        }while(cbpc == 20);
815
816
3.71M
        h->c.bdsp.clear_blocks(h->block[0]);
817
818
3.71M
        dquant = cbpc & 8;
819
3.71M
        h->c.mb_intra = ((cbpc & 4) != 0);
820
3.71M
        if (h->c.mb_intra)
821
359k
            goto intra;
822
823
3.35M
        if (h->pb_frame && get_bits1(&h->gb))
824
144k
            pb_mv_count = h263_get_modb(&h->gb, h->pb_frame, &cbpb);
825
3.35M
        cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
826
827
3.35M
        if (cbpy < 0) {
828
79.5k
            av_log(h->c.avctx, AV_LOG_ERROR, "cbpy damaged at %d %d\n",
829
79.5k
                   h->c.mb_x, h->c.mb_y);
830
79.5k
            return SLICE_ERROR;
831
79.5k
        }
832
833
3.27M
        if (!h->alt_inter_vlc|| (cbpc & 3)!=3)
834
3.27M
            cbpy ^= 0xF;
835
836
3.27M
        cbp = (cbpc & 3) | (cbpy << 2);
837
3.27M
        if (dquant) {
838
482k
            h263_decode_dquant(h);
839
482k
        }
840
841
3.27M
        h->c.mv_dir = MV_DIR_FORWARD;
842
3.27M
        if ((cbpc & 16) == 0) {
843
2.09M
            h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
844
            /* 16x16 motion prediction */
845
2.09M
            h->c.mv_type = MV_TYPE_16X16;
846
2.09M
            ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
847
2.09M
            if (h->umvplus)
848
392k
               mx = h263p_decode_umotion(h, pred_x);
849
1.70M
            else
850
1.70M
               mx = ff_h263_decode_motion(h, pred_x, 1);
851
852
2.09M
            if (mx >= 0xffff)
853
12.1k
                return SLICE_ERROR;
854
855
2.08M
            if (h->umvplus)
856
391k
               my = h263p_decode_umotion(h, pred_y);
857
1.69M
            else
858
1.69M
               my = ff_h263_decode_motion(h, pred_y, 1);
859
860
2.08M
            if (my >= 0xffff)
861
23.3k
                return SLICE_ERROR;
862
2.06M
            h->c.mv[0][0][0] = mx;
863
2.06M
            h->c.mv[0][0][1] = my;
864
865
2.06M
            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.06M
        } else {
868
1.17M
            h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_FORWARD_MV;
869
1.17M
            h->c.mv_type = MV_TYPE_8X8;
870
5.72M
            for(i=0;i<4;i++) {
871
4.60M
                mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y);
872
4.60M
                if (h->umvplus)
873
380k
                    mx = h263p_decode_umotion(h, pred_x);
874
4.22M
                else
875
4.22M
                    mx = ff_h263_decode_motion(h, pred_x, 1);
876
4.60M
                if (mx >= 0xffff)
877
26.0k
                    return SLICE_ERROR;
878
879
4.58M
                if (h->umvplus)
880
380k
                    my = h263p_decode_umotion(h, pred_y);
881
4.20M
                else
882
4.20M
                    my = ff_h263_decode_motion(h, pred_y, 1);
883
4.58M
                if (my >= 0xffff)
884
26.6k
                    return SLICE_ERROR;
885
4.55M
                h->c.mv[0][i][0] = mx;
886
4.55M
                h->c.mv[0][i][1] = my;
887
4.55M
                if (h->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
888
59.5k
                    skip_bits1(&h->gb); /* Bit stuffing to prevent PSC */
889
4.55M
                mot_val[0] = mx;
890
4.55M
                mot_val[1] = my;
891
4.55M
            }
892
1.17M
        }
893
3.27M
    } else if (h->c.pict_type==AV_PICTURE_TYPE_B) {
894
337k
        int mb_type;
895
337k
        const int stride  = h->c.b8_stride;
896
337k
        int16_t *mot_val0 = h->c.cur_pic.motion_val[0][2 * (h->c.mb_x + h->c.mb_y * stride)];
897
337k
        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
337k
        mot_val0[0       ]= mot_val0[2       ]= mot_val0[0+2*stride]= mot_val0[2+2*stride]=
902
337k
        mot_val0[1       ]= mot_val0[3       ]= mot_val0[1+2*stride]= mot_val0[3+2*stride]=
903
337k
        mot_val1[0       ]= mot_val1[2       ]= mot_val1[0+2*stride]= mot_val1[2+2*stride]=
904
337k
        mot_val1[1       ]= mot_val1[3       ]= mot_val1[1+2*stride]= mot_val1[3+2*stride]= 0;
905
906
339k
        do{
907
339k
            mb_type = get_vlc2(&h->gb, h263_mbtype_b_vlc,
908
339k
                               H263_MBTYPE_B_VLC_BITS, 2);
909
339k
            if (mb_type < 0){
910
3.18k
                av_log(h->c.avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n",
911
3.18k
                       h->c.mb_x, h->c.mb_y);
912
3.18k
                return SLICE_ERROR;
913
3.18k
            }
914
339k
        }while(!mb_type);
915
916
334k
        h->c.mb_intra = IS_INTRA(mb_type);
917
334k
        if(HAS_CBP(mb_type)){
918
87.2k
            h->c.bdsp.clear_blocks(h->block[0]);
919
87.2k
            cbpc = get_vlc2(&h->gb, cbpc_b_vlc, CBPC_B_VLC_BITS, 1);
920
87.2k
            if (h->c.mb_intra) {
921
2.94k
                dquant = IS_QUANT(mb_type);
922
2.94k
                goto intra;
923
2.94k
            }
924
925
84.3k
            cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
926
927
84.3k
            if (cbpy < 0){
928
4.52k
                av_log(h->c.avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n",
929
4.52k
                       h->c.mb_x, h->c.mb_y);
930
4.52k
                return SLICE_ERROR;
931
4.52k
            }
932
933
79.7k
            if (!h->alt_inter_vlc || (cbpc & 3)!=3)
934
78.3k
                cbpy ^= 0xF;
935
936
79.7k
            cbp = (cbpc & 3) | (cbpy << 2);
937
79.7k
        }else
938
247k
            cbp=0;
939
940
327k
        av_assert2(!h->c.mb_intra);
941
942
327k
        if(IS_QUANT(mb_type)){
943
22.7k
            h263_decode_dquant(h);
944
22.7k
        }
945
946
327k
        if(IS_DIRECT(mb_type)){
947
160k
            h->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
948
160k
            mb_type |= set_direct_mv(&h->c);
949
166k
        }else{
950
166k
            h->c.mv_dir  = 0;
951
166k
            h->c.mv_type = MV_TYPE_16X16;
952
//FIXME UMV
953
954
166k
            if (HAS_FORWARD_MV(mb_type)) {
955
80.7k
                int16_t *mot_val = ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
956
80.7k
                h->c.mv_dir = MV_DIR_FORWARD;
957
958
80.7k
                if (h->umvplus)
959
15.0k
                    mx = h263p_decode_umotion(h, pred_x);
960
65.6k
                else
961
65.6k
                    mx = ff_h263_decode_motion(h, pred_x, 1);
962
80.7k
                if (mx >= 0xffff)
963
882
                    return SLICE_ERROR;
964
965
79.9k
                if (h->umvplus)
966
15.0k
                    my = h263p_decode_umotion(h, pred_y);
967
64.8k
                else
968
64.8k
                    my = ff_h263_decode_motion(h, pred_y, 1);
969
79.9k
                if (my >= 0xffff)
970
843
                    return SLICE_ERROR;
971
972
79.0k
                if (h->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
973
1.81k
                    skip_bits1(&h->gb); /* Bit stuffing to prevent PSC */
974
975
79.0k
                h->c.mv[0][0][0] = mx;
976
79.0k
                h->c.mv[0][0][1] = my;
977
79.0k
                mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
978
79.0k
                mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
979
79.0k
            }
980
981
164k
            if (HAS_BACKWARD_MV(mb_type)) {
982
136k
                int16_t *mot_val = ff_h263_pred_motion(&h->c, 0, 1, &pred_x, &pred_y);
983
136k
                h->c.mv_dir |= MV_DIR_BACKWARD;
984
985
136k
                if (h->umvplus)
986
24.2k
                    mx = h263p_decode_umotion(h, pred_x);
987
112k
                else
988
112k
                    mx = ff_h263_decode_motion(h, pred_x, 1);
989
136k
                if (mx >= 0xffff)
990
1.03k
                    return SLICE_ERROR;
991
992
135k
                if (h->umvplus)
993
24.2k
                    my = h263p_decode_umotion(h, pred_y);
994
111k
                else
995
111k
                    my = ff_h263_decode_motion(h, pred_y, 1);
996
135k
                if (my >= 0xffff)
997
951
                    return SLICE_ERROR;
998
999
134k
                if (h->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
1000
2.35k
                    skip_bits1(&h->gb); /* Bit stuffing to prevent PSC */
1001
1002
134k
                h->c.mv[1][0][0] = mx;
1003
134k
                h->c.mv[1][0][1] = my;
1004
134k
                mot_val[0       ]= mot_val[2       ]= mot_val[0+2*stride]= mot_val[2+2*stride]= mx;
1005
134k
                mot_val[1       ]= mot_val[3       ]= mot_val[1+2*stride]= mot_val[3+2*stride]= my;
1006
134k
            }
1007
164k
        }
1008
1009
323k
        h->c.cur_pic.mb_type[xy] = mb_type;
1010
817k
    } else { /* I-Frame */
1011
821k
        do{
1012
821k
            cbpc = get_vlc2(&h->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2);
1013
821k
            if (cbpc < 0){
1014
55.4k
                av_log(h->c.avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n",
1015
55.4k
                       h->c.mb_x, h->c.mb_y);
1016
55.4k
                return SLICE_ERROR;
1017
55.4k
            }
1018
821k
        }while(cbpc == 8);
1019
1020
761k
        h->c.bdsp.clear_blocks(h->block[0]);
1021
1022
761k
        dquant = cbpc & 4;
1023
761k
        h->c.mb_intra = 1;
1024
1.12M
intra:
1025
1.12M
        h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA;
1026
1.12M
        if (h->c.h263_aic) {
1027
263k
            h->c.ac_pred = get_bits1(&h->gb);
1028
263k
            if (h->c.ac_pred) {
1029
146k
                h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1030
1031
146k
                h->c.h263_aic_dir = get_bits1(&h->gb);
1032
146k
            }
1033
263k
        }else
1034
860k
            h->c.ac_pred = 0;
1035
1036
1.12M
        if (h->pb_frame && get_bits1(&h->gb))
1037
70.1k
            pb_mv_count = h263_get_modb(&h->gb, h->pb_frame, &cbpb);
1038
1.12M
        cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
1039
1.12M
        if(cbpy<0){
1040
50.8k
            av_log(h->c.avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n",
1041
50.8k
                   h->c.mb_x, h->c.mb_y);
1042
50.8k
            return SLICE_ERROR;
1043
50.8k
        }
1044
1.07M
        cbp = (cbpc & 3) | (cbpy << 2);
1045
1.07M
        if (dquant) {
1046
298k
            h263_decode_dquant(h);
1047
298k
        }
1048
1049
1.07M
        pb_mv_count += !!h->pb_frame;
1050
1.07M
    }
1051
1052
4.86M
    while(pb_mv_count--){
1053
283k
        ff_h263_decode_motion(h, 0, 1);
1054
283k
        ff_h263_decode_motion(h, 0, 1);
1055
283k
    }
1056
1057
    /* decode each block */
1058
28.9M
    for (i = 0; i < 6; i++) {
1059
25.0M
        if (h263_decode_block(h, h->block[i], i, cbp&32) < 0)
1060
694k
            return -1;
1061
24.3M
        cbp+=cbp;
1062
24.3M
    }
1063
1064
3.88M
    if (h->pb_frame && h263_skip_b_part(h, cbpb) < 0)
1065
14.4k
        return -1;
1066
3.87M
    if (h->c.obmc && !h->c.mb_intra) {
1067
896k
        if (h->c.pict_type == AV_PICTURE_TYPE_P &&
1068
803k
            h->c.mb_x + 1 < h->c.mb_width && h->mb_num_left != 1)
1069
782k
            preview_obmc(h);
1070
896k
    }
1071
16.0M
end:
1072
1073
16.0M
    if (get_bits_left(&h->gb) < 0)
1074
68.4k
        return AVERROR_INVALIDDATA;
1075
1076
        /* per-MB end of slice check */
1077
15.9M
    {
1078
15.9M
        int v = show_bits(&h->gb, 16);
1079
1080
15.9M
        if (get_bits_left(&h->gb) < 16) {
1081
140k
            v >>= 16 - get_bits_left(&h->gb);
1082
140k
        }
1083
1084
15.9M
        if(v==0)
1085
155k
            return SLICE_END;
1086
15.9M
    }
1087
1088
15.8M
    return SLICE_OK;
1089
15.9M
}
1090
1091
/* Most is hardcoded; should extend to handle all H.263 streams. */
1092
int ff_h263_decode_picture_header(H263DecContext *const h)
1093
627k
{
1094
627k
    int width, height, i, ret;
1095
627k
    int h263_plus;
1096
1097
627k
    align_get_bits(&h->gb);
1098
1099
627k
    if (show_bits(&h->gb, 2) == 2 && h->c.avctx->frame_num == 0) {
1100
25.4k
         av_log(h->c.avctx, AV_LOG_WARNING, "Header looks like RTP instead of H.263\n");
1101
25.4k
    }
1102
1103
627k
    uint32_t startcode = get_bits(&h->gb, 22-8);
1104
1105
2.60M
    for (i = get_bits_left(&h->gb); i>24; i -= 8) {
1106
2.40M
        startcode = ((startcode << 8) | get_bits(&h->gb, 8)) & 0x003FFFFF;
1107
1108
2.40M
        if(startcode == 0x20)
1109
422k
            break;
1110
2.40M
    }
1111
1112
627k
    if (startcode != 0x20) {
1113
204k
        av_log(h->c.avctx, AV_LOG_ERROR, "Bad picture start code\n");
1114
204k
        return -1;
1115
204k
    }
1116
    /* temporal reference */
1117
422k
    i = get_bits(&h->gb, 8); /* picture timestamp */
1118
1119
422k
    i -= (i - (h->picture_number & 0xFF) + 128) & ~0xFF;
1120
1121
422k
    h->picture_number = (h->picture_number&~0xFF) + i;
1122
1123
    /* PTYPE starts here */
1124
422k
    if (check_marker(h->c.avctx, &h->gb, "in PTYPE") != 1) {
1125
10.5k
        return -1;
1126
10.5k
    }
1127
412k
    if (get_bits1(&h->gb) != 0) {
1128
3.28k
        av_log(h->c.avctx, AV_LOG_ERROR, "Bad H.263 id\n");
1129
3.28k
        return -1;      /* H.263 id */
1130
3.28k
    }
1131
409k
    skip_bits1(&h->gb);         /* split screen off */
1132
409k
    skip_bits1(&h->gb);         /* camera  off */
1133
409k
    skip_bits1(&h->gb);         /* freeze picture release off */
1134
1135
409k
    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
409k
    if (format != 7 && format != 6) {
1144
324k
        h263_plus = 0;
1145
        /* H.263v1 */
1146
324k
        width = ff_h263_format[format][0];
1147
324k
        height = ff_h263_format[format][1];
1148
324k
        if (!width)
1149
4.23k
            return -1;
1150
1151
320k
        h->c.pict_type = AV_PICTURE_TYPE_I + get_bits1(&h->gb);
1152
1153
320k
        h->h263_long_vectors = get_bits1(&h->gb);
1154
1155
320k
        if (get_bits1(&h->gb) != 0) {
1156
2.79k
            av_log(h->c.avctx, AV_LOG_ERROR, "H.263 SAC not supported\n");
1157
2.79k
            return -1; /* SAC: off */
1158
2.79k
        }
1159
317k
        h->c.obmc = get_bits1(&h->gb); /* Advanced prediction mode */
1160
1161
317k
        h->pb_frame = get_bits1(&h->gb);
1162
317k
        h->c.chroma_qscale = h->c.qscale = get_bits(&h->gb, 5);
1163
317k
        skip_bits1(&h->gb); /* Continuous Presence Multipoint mode: off */
1164
1165
317k
        h->c.width = width;
1166
317k
        h->c.height = height;
1167
317k
        h->c.avctx->sample_aspect_ratio= (AVRational){12,11};
1168
317k
        h->c.avctx->framerate = (AVRational){ 30000, 1001 };
1169
317k
    } else {
1170
84.6k
        int ufep;
1171
1172
        /* H.263v2 */
1173
84.6k
        h263_plus = 1;
1174
84.6k
        ufep = get_bits(&h->gb, 3); /* Update Full Extended PTYPE */
1175
1176
        /* ufep other than 0 and 1 are reserved */
1177
84.6k
        if (ufep == 1) {
1178
            /* OPPTYPE */
1179
53.1k
            format = get_bits(&h->gb, 3);
1180
53.1k
            ff_dlog(h->c.avctx, "ufep=1, format: %d\n", format);
1181
53.1k
            h->custom_pcf = get_bits1(&h->gb);
1182
53.1k
            h->umvplus    = get_bits1(&h->gb); /* Unrestricted Motion Vector */
1183
53.1k
            if (get_bits1(&h->gb) != 0) {
1184
18.6k
                av_log(h->c.avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
1185
18.6k
            }
1186
53.1k
            h->c.obmc        = get_bits1(&h->gb); /* Advanced prediction mode */
1187
53.1k
            h->c.h263_aic    = get_bits1(&h->gb); /* Advanced Intra Coding (AIC) */
1188
53.1k
            h->loop_filter = get_bits1(&h->gb);
1189
53.1k
            if (h->c.avctx->lowres)
1190
25.4k
                h->loop_filter = 0;
1191
1192
53.1k
            h->h263_slice_structured = get_bits1(&h->gb);
1193
53.1k
            if (get_bits1(&h->gb) != 0) {
1194
13.0k
                av_log(h->c.avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
1195
13.0k
            }
1196
53.1k
            if (get_bits1(&h->gb) != 0) {
1197
18.9k
                av_log(h->c.avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
1198
18.9k
            }
1199
53.1k
            h->alt_inter_vlc  = get_bits1(&h->gb);
1200
53.1k
            h->modified_quant = get_bits1(&h->gb);
1201
53.1k
            if (h->modified_quant)
1202
19.7k
                h->c.chroma_qscale_table= ff_h263_chroma_qscale_table;
1203
1204
53.1k
            skip_bits(&h->gb, 1); /* Prevent start code emulation */
1205
1206
53.1k
            skip_bits(&h->gb, 3); /* Reserved */
1207
53.1k
        } else if (ufep != 0) {
1208
2.09k
            av_log(h->c.avctx, AV_LOG_ERROR, "Bad UFEP type (%d)\n", ufep);
1209
2.09k
            return -1;
1210
2.09k
        }
1211
1212
        /* MPPTYPE */
1213
82.5k
        h->c.pict_type = get_bits(&h->gb, 3);
1214
82.5k
        switch (h->c.pict_type) {
1215
17.4k
        case 0: h->c.pict_type = AV_PICTURE_TYPE_I; break;
1216
18.7k
        case 1: h->c.pict_type = AV_PICTURE_TYPE_P; break;
1217
13.2k
        case 2: h->c.pict_type = AV_PICTURE_TYPE_P; h->pb_frame = 3; break;
1218
27.5k
        case 3: h->c.pict_type = AV_PICTURE_TYPE_B; break;
1219
3.66k
        case 7: h->c.pict_type = AV_PICTURE_TYPE_I; break; //ZYGO
1220
1.83k
        default:
1221
1.83k
            return -1;
1222
82.5k
        }
1223
80.7k
        skip_bits(&h->gb, 2);
1224
80.7k
        h->c.no_rounding = get_bits1(&h->gb);
1225
80.7k
        skip_bits(&h->gb, 4);
1226
1227
        /* Get the picture dimensions */
1228
80.7k
        if (ufep) {
1229
51.9k
            if (format == 6) {
1230
                /* Custom Picture Format (CPFMT) */
1231
27.5k
                int aspect_ratio_info = get_bits(&h->gb, 4);
1232
27.5k
                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
27.5k
                width = (get_bits(&h->gb, 9) + 1) * 4;
1243
27.5k
                check_marker(h->c.avctx, &h->gb, "in dimensions");
1244
27.5k
                height = get_bits(&h->gb, 9) * 4;
1245
27.5k
                ff_dlog(h->c.avctx, "\nH.263+ Custom picture: %dx%d\n",width,height);
1246
27.5k
                if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
1247
                    /* expected dimensions */
1248
4.40k
                    h->c.avctx->sample_aspect_ratio.num = get_bits(&h->gb, 8);
1249
4.40k
                    h->c.avctx->sample_aspect_ratio.den = get_bits(&h->gb, 8);
1250
23.1k
                }else{
1251
23.1k
                    h->c.avctx->sample_aspect_ratio= ff_h263_pixel_aspect[aspect_ratio_info];
1252
23.1k
                }
1253
27.5k
            } else {
1254
24.3k
                width = ff_h263_format[format][0];
1255
24.3k
                height = ff_h263_format[format][1];
1256
24.3k
                h->c.avctx->sample_aspect_ratio = (AVRational){12,11};
1257
24.3k
            }
1258
51.9k
            h->c.avctx->sample_aspect_ratio.den <<= h->ehc_mode;
1259
51.9k
            if ((width == 0) || (height == 0))
1260
2.43k
                return -1;
1261
49.5k
            h->c.width  = width;
1262
49.5k
            h->c.height = height;
1263
1264
49.5k
            if (h->custom_pcf) {
1265
21.3k
                h->c.avctx->framerate.num  = 1800000;
1266
21.3k
                h->c.avctx->framerate.den  = 1000 + get_bits1(&h->gb);
1267
21.3k
                h->c.avctx->framerate.den *= get_bits(&h->gb, 7);
1268
21.3k
                if (h->c.avctx->framerate.den == 0) {
1269
1.04k
                    av_log(h->c.avctx, AV_LOG_ERROR, "zero framerate\n");
1270
1.04k
                    return -1;
1271
1.04k
                }
1272
20.2k
                int gcd = av_gcd(h->c.avctx->framerate.den, h->c.avctx->framerate.num);
1273
20.2k
                h->c.avctx->framerate.den /= gcd;
1274
20.2k
                h->c.avctx->framerate.num /= gcd;
1275
28.1k
            }else{
1276
28.1k
                h->c.avctx->framerate = (AVRational){ 30000, 1001 };
1277
28.1k
            }
1278
49.5k
        }
1279
1280
77.2k
        if (h->custom_pcf)
1281
37.3k
            skip_bits(&h->gb, 2); //extended Temporal reference
1282
1283
77.2k
        if (ufep) {
1284
48.4k
            if (h->umvplus) {
1285
27.5k
                if (get_bits1(&h->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
1286
14.3k
                    skip_bits1(&h->gb);
1287
27.5k
            }
1288
48.4k
            if (h->h263_slice_structured) {
1289
7.38k
                if (get_bits1(&h->gb) != 0) {
1290
3.95k
                    av_log(h->c.avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
1291
3.95k
                }
1292
7.38k
                if (get_bits1(&h->gb) != 0) {
1293
4.08k
                    av_log(h->c.avctx, AV_LOG_ERROR, "unordered slices not supported\n");
1294
4.08k
                }
1295
7.38k
            }
1296
48.4k
            if (h->c.pict_type == AV_PICTURE_TYPE_B) {
1297
5.91k
                skip_bits(&h->gb, 4); //ELNUM
1298
5.91k
                if (ufep == 1) {
1299
5.91k
                    skip_bits(&h->gb, 4); // RLNUM
1300
5.91k
                }
1301
5.91k
            }
1302
48.4k
        }
1303
1304
77.2k
        h->c.qscale = get_bits(&h->gb, 5);
1305
77.2k
    }
1306
1307
394k
    ret = av_image_check_size(h->c.width, h->c.height, 0, h->c.avctx);
1308
394k
    if (ret < 0)
1309
685
        return ret;
1310
1311
393k
    if (!(h->c.avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
1312
393k
        if ((h->c.width * h->c.height / 256 / 8) > get_bits_left(&h->gb))
1313
22.5k
            return AVERROR_INVALIDDATA;
1314
393k
    }
1315
1316
371k
    h->c.mb_width  = (h->c.width  + 15U) / 16;
1317
371k
    h->c.mb_height = (h->c.height + 15U) / 16;
1318
371k
    h->c.mb_num    = h->c.mb_width * h->c.mb_height;
1319
1320
371k
    h->gob_index = H263_GOB_HEIGHT(h->c.height);
1321
1322
371k
    if (h->pb_frame) {
1323
42.6k
        skip_bits(&h->gb, 3); /* Temporal reference for B-pictures */
1324
42.6k
        if (h->custom_pcf)
1325
21.0k
            skip_bits(&h->gb, 2); //extended Temporal reference
1326
42.6k
        skip_bits(&h->gb, 2); /* Quantization information for B-pictures */
1327
42.6k
    }
1328
1329
371k
    if (h->c.pict_type!=AV_PICTURE_TYPE_B) {
1330
346k
        h->c.time            = h->picture_number;
1331
346k
        h->c.pp_time         = h->c.time - h->c.last_non_b_time;
1332
346k
        h->c.last_non_b_time = h->c.time;
1333
346k
    }else{
1334
25.2k
        h->c.time    = h->picture_number;
1335
25.2k
        h->c.pb_time = h->c.pp_time - (h->c.last_non_b_time - h->c.time);
1336
25.2k
        if (h->c.pp_time <= h->c.pb_time ||
1337
3.89k
            h->c.pp_time <= h->c.pp_time - h->c.pb_time ||
1338
21.8k
            h->c.pp_time <= 0) {
1339
21.8k
            h->c.pp_time = 2;
1340
21.8k
            h->c.pb_time = 1;
1341
21.8k
        }
1342
25.2k
        ff_mpeg4_init_direct_mv(&h->c);
1343
25.2k
    }
1344
1345
    /* PEI */
1346
371k
    if (skip_1stop_8data_bits(&h->gb) < 0)
1347
2.20k
        return AVERROR_INVALIDDATA;
1348
1349
369k
    if (h->h263_slice_structured) {
1350
12.8k
        if (check_marker(h->c.avctx, &h->gb, "SEPB1") != 1) {
1351
6.37k
            return -1;
1352
6.37k
        }
1353
1354
6.49k
        ff_h263_decode_mba(h);
1355
1356
6.49k
        if (check_marker(h->c.avctx, &h->gb, "SEPB2") != 1) {
1357
1.98k
            return -1;
1358
1.98k
        }
1359
6.49k
    }
1360
1361
360k
    if (h->c.pict_type == AV_PICTURE_TYPE_B)
1362
24.3k
        h->c.low_delay = 0;
1363
1364
360k
    if (h->c.h263_aic) {
1365
126k
         h->c.y_dc_scale_table =
1366
126k
         h->c.c_dc_scale_table = ff_aic_dc_scale_table;
1367
234k
    }else{
1368
234k
        h->c.y_dc_scale_table =
1369
234k
        h->c.c_dc_scale_table = ff_mpeg1_dc_scale_table;
1370
234k
    }
1371
1372
360k
    ff_h263_show_pict_info(h, h263_plus);
1373
1374
360k
    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.46k
        int i,j;
1376
212k
        for(i=0; i<85; i++) av_log(h->c.avctx, AV_LOG_DEBUG, "%d", get_bits1(&h->gb));
1377
2.46k
        av_log(h->c.avctx, AV_LOG_DEBUG, "\n");
1378
34.5k
        for(i=0; i<13; i++){
1379
128k
            for(j=0; j<3; j++){
1380
96.1k
                int v= get_bits(&h->gb, 8);
1381
96.1k
                v |= get_sbits(&h->gb, 8) * (1 << 8);
1382
96.1k
                av_log(h->c.avctx, AV_LOG_DEBUG, " %5d", v);
1383
96.1k
            }
1384
32.0k
            av_log(h->c.avctx, AV_LOG_DEBUG, "\n");
1385
32.0k
        }
1386
125k
        for(i=0; i<50; i++) av_log(h->c.avctx, AV_LOG_DEBUG, "%d", get_bits1(&h->gb));
1387
2.46k
    }
1388
1389
360k
    return 0;
1390
369k
}