~ubuntu-branches/ubuntu/utopic/libav/utopic

« back to all changes in this revision

Viewing changes to libavcodec/h261dec.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2014-05-11 12:28:45 UTC
  • mfrom: (1.3.42 sid)
  • Revision ID: package-import@ubuntu.com-20140511122845-zxdom35pimot5eat
Tags: 6:10.1-1
* New upstream release 10:
   - pcm-dvd: Fix 20bit decoding (bug/592)
   - avi: Improve non-interleaved detection (bug/666)
   - arm: hpeldsp: fix put_pixels8_y2_{,no_rnd_}armv6
   - arm: hpeldsp: prevent overreads in armv6 asm (bug/646)
   - avfilter: Add missing emms_c when needed
   - rtmpproto: Check the buffer sizes when copying app/playpath strings
   - swscale: Fix an undefined behaviour
   - vp9: Read the frame size as unsigned
   - dcadec: Use correct channel count in stereo downmix check
   - dcadec: Do not decode the XCh extension when downmixing to stereo
   - matroska: add the Opus mapping
   - matroskadec: read the CodecDelay element
   - rtmpproto: Make sure to pass on the error code if read_connect failed
   - lavr: allocate the resampling buffer with a positive size
   - mp3enc: Properly write bitrate value in XING header (Closes: #736088)
   - golomb: Fix the implementation of get_se_golomb_long
* Drop debian/libav-tools.maintscript. ffserver is no longer found in
  stable, and this seems to cause other problems today (Closes: #742676)

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 * H.261 decoder.
26
26
 */
27
27
 
28
 
#include "dsputil.h"
29
28
#include "avcodec.h"
30
29
#include "mpegvideo.h"
31
30
#include "h263.h"
32
31
#include "h261.h"
33
 
#include "h261data.h"
 
32
#include "internal.h"
34
33
 
35
34
#define H261_MBA_VLC_BITS 9
36
35
#define H261_MTYPE_VLC_BITS 6
40
39
#define MBA_STUFFING 33
41
40
#define MBA_STARTCODE 34
42
41
 
43
 
extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
44
 
 
45
42
static VLC h261_mba_vlc;
46
43
static VLC h261_mtype_vlc;
47
44
static VLC h261_mv_vlc;
48
45
static VLC h261_cbp_vlc;
49
46
 
50
 
static int h261_decode_block(H261Context * h, DCTELEM * block, int n, int coded);
51
 
 
52
 
static av_cold void h261_decode_init_vlc(H261Context *h){
 
47
static av_cold void h261_decode_init_vlc(H261Context *h)
 
48
{
53
49
    static int done = 0;
54
50
 
55
 
    if(!done){
 
51
    if (!done) {
56
52
        done = 1;
57
53
        INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
58
 
                 h261_mba_bits, 1, 1,
59
 
                 h261_mba_code, 1, 1, 662);
 
54
                        ff_h261_mba_bits, 1, 1,
 
55
                        ff_h261_mba_code, 1, 1, 662);
60
56
        INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
61
 
                 h261_mtype_bits, 1, 1,
62
 
                 h261_mtype_code, 1, 1, 80);
 
57
                        ff_h261_mtype_bits, 1, 1,
 
58
                        ff_h261_mtype_code, 1, 1, 80);
63
59
        INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
64
 
                 &h261_mv_tab[0][1], 2, 1,
65
 
                 &h261_mv_tab[0][0], 2, 1, 144);
 
60
                        &ff_h261_mv_tab[0][1], 2, 1,
 
61
                        &ff_h261_mv_tab[0][0], 2, 1, 144);
66
62
        INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
67
 
                 &h261_cbp_tab[0][1], 2, 1,
68
 
                 &h261_cbp_tab[0][0], 2, 1, 512);
69
 
        ff_init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
70
 
        INIT_VLC_RL(h261_rl_tcoeff, 552);
 
63
                        &ff_h261_cbp_tab[0][1], 2, 1,
 
64
                        &ff_h261_cbp_tab[0][0], 2, 1, 512);
 
65
        INIT_VLC_RL(ff_h261_rl_tcoeff, 552);
71
66
    }
72
67
}
73
68
 
74
 
static av_cold int h261_decode_init(AVCodecContext *avctx){
75
 
    H261Context *h= avctx->priv_data;
76
 
    MpegEncContext * const s = &h->s;
 
69
static av_cold int h261_decode_init(AVCodecContext *avctx)
 
70
{
 
71
    H261Context *h          = avctx->priv_data;
 
72
    MpegEncContext *const s = &h->s;
77
73
 
78
74
    // set defaults
79
75
    ff_MPV_decode_defaults(s);
80
 
    s->avctx = avctx;
81
 
 
82
 
    s->width  = s->avctx->coded_width;
83
 
    s->height = s->avctx->coded_height;
84
 
    s->codec_id = s->avctx->codec->id;
85
 
 
86
 
    s->out_format = FMT_H261;
87
 
    s->low_delay= 1;
88
 
    avctx->pix_fmt= AV_PIX_FMT_YUV420P;
89
 
 
90
 
    s->codec_id= avctx->codec->id;
91
 
 
 
76
    s->avctx       = avctx;
 
77
    s->width       = s->avctx->coded_width;
 
78
    s->height      = s->avctx->coded_height;
 
79
    s->codec_id    = s->avctx->codec->id;
 
80
    s->out_format  = FMT_H261;
 
81
    s->low_delay   = 1;
 
82
    avctx->pix_fmt = AV_PIX_FMT_YUV420P;
 
83
    s->codec_id    = avctx->codec->id;
 
84
 
 
85
    ff_h261_common_init();
92
86
    h261_decode_init_vlc(h);
93
87
 
94
88
    h->gob_start_code_skipped = 0;
100
94
 * Decode the group of blocks header or slice header.
101
95
 * @return <0 if an error occurred
102
96
 */
103
 
static int h261_decode_gob_header(H261Context *h){
 
97
static int h261_decode_gob_header(H261Context *h)
 
98
{
104
99
    unsigned int val;
105
 
    MpegEncContext * const s = &h->s;
 
100
    MpegEncContext *const s = &h->s;
106
101
 
107
 
    if ( !h->gob_start_code_skipped ){
 
102
    if (!h->gob_start_code_skipped) {
108
103
        /* Check for GOB Start Code */
109
104
        val = show_bits(&s->gb, 15);
110
 
        if(val)
 
105
        if (val)
111
106
            return -1;
112
107
 
113
108
        /* We have a GBSC */
117
112
    h->gob_start_code_skipped = 0;
118
113
 
119
114
    h->gob_number = get_bits(&s->gb, 4); /* GN */
120
 
    s->qscale = get_bits(&s->gb, 5); /* GQUANT */
 
115
    s->qscale     = get_bits(&s->gb, 5); /* GQUANT */
121
116
 
122
117
    /* Check if gob_number is valid */
123
 
    if (s->mb_height==18){ //cif
124
 
        if ((h->gob_number<=0) || (h->gob_number>12))
 
118
    if (s->mb_height == 18) { // CIF
 
119
        if ((h->gob_number <= 0) || (h->gob_number > 12))
125
120
            return -1;
126
 
    }
127
 
    else{ //qcif
128
 
        if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
 
121
    } else { // QCIF
 
122
        if ((h->gob_number != 1) && (h->gob_number != 3) &&
 
123
            (h->gob_number != 5))
129
124
            return -1;
130
125
    }
131
126
 
132
127
    /* GEI */
133
 
    while (get_bits1(&s->gb) != 0) {
 
128
    while (get_bits1(&s->gb) != 0)
134
129
        skip_bits(&s->gb, 8);
135
 
    }
136
130
 
137
 
    if(s->qscale==0) {
 
131
    if (s->qscale == 0) {
138
132
        av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
139
133
        if (s->avctx->err_recognition & AV_EF_BITSTREAM)
140
134
            return -1;
141
135
    }
142
136
 
143
 
    // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
144
 
    // subsequent macroblocks, MBA is the difference between the absolute addresses of
145
 
    // the macroblock and the last transmitted macroblock.
 
137
    /* For the first transmitted macroblock in a GOB, MBA is the absolute
 
138
     * address. For subsequent macroblocks, MBA is the difference between
 
139
     * the absolute addresses of the macroblock and the last transmitted
 
140
     * macroblock. */
146
141
    h->current_mba = 0;
147
 
    h->mba_diff = 0;
 
142
    h->mba_diff    = 0;
148
143
 
149
144
    return 0;
150
145
}
153
148
 * Decode the group of blocks / video packet header.
154
149
 * @return <0 if no resync found
155
150
 */
156
 
static int ff_h261_resync(H261Context *h){
157
 
    MpegEncContext * const s = &h->s;
 
151
static int h261_resync(H261Context *h)
 
152
{
 
153
    MpegEncContext *const s = &h->s;
158
154
    int left, ret;
159
155
 
160
 
    if ( h->gob_start_code_skipped ){
161
 
        ret= h261_decode_gob_header(h);
162
 
        if(ret>=0)
 
156
    if (h->gob_start_code_skipped) {
 
157
        ret = h261_decode_gob_header(h);
 
158
        if (ret >= 0)
163
159
            return 0;
164
 
    }
165
 
    else{
166
 
        if(show_bits(&s->gb, 15)==0){
167
 
            ret= h261_decode_gob_header(h);
168
 
            if(ret>=0)
 
160
    } else {
 
161
        if (show_bits(&s->gb, 15) == 0) {
 
162
            ret = h261_decode_gob_header(h);
 
163
            if (ret >= 0)
169
164
                return 0;
170
165
        }
171
 
        //OK, it is not where it is supposed to be ...
172
 
        s->gb= s->last_resync_gb;
 
166
        // OK, it is not where it is supposed to be ...
 
167
        s->gb = s->last_resync_gb;
173
168
        align_get_bits(&s->gb);
174
 
        left= get_bits_left(&s->gb);
175
 
 
176
 
        for(;left>15+1+4+5; left-=8){
177
 
            if(show_bits(&s->gb, 15)==0){
178
 
                GetBitContext bak= s->gb;
179
 
 
180
 
                ret= h261_decode_gob_header(h);
181
 
                if(ret>=0)
 
169
        left = get_bits_left(&s->gb);
 
170
 
 
171
        for (; left > 15 + 1 + 4 + 5; left -= 8) {
 
172
            if (show_bits(&s->gb, 15) == 0) {
 
173
                GetBitContext bak = s->gb;
 
174
 
 
175
                ret = h261_decode_gob_header(h);
 
176
                if (ret >= 0)
182
177
                    return 0;
183
178
 
184
 
                s->gb= bak;
 
179
                s->gb = bak;
185
180
            }
186
181
            skip_bits(&s->gb, 8);
187
182
        }
194
189
 * Decode skipped macroblocks.
195
190
 * @return 0
196
191
 */
197
 
static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
 
192
static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
198
193
{
199
 
    MpegEncContext * const s = &h->s;
 
194
    MpegEncContext *const s = &h->s;
200
195
    int i;
201
196
 
202
197
    s->mb_intra = 0;
203
198
 
204
 
    for(i=mba1; i<mba2; i++){
 
199
    for (i = mba1; i < mba2; i++) {
205
200
        int j, xy;
206
201
 
207
 
        s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
208
 
        s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
209
 
        xy = s->mb_x + s->mb_y * s->mb_stride;
 
202
        s->mb_x = ((h->gob_number - 1) % 2) * 11 + i % 11;
 
203
        s->mb_y = ((h->gob_number - 1) / 2) * 3 + i / 11;
 
204
        xy      = s->mb_x + s->mb_y * s->mb_stride;
210
205
        ff_init_block_index(s);
211
206
        ff_update_block_index(s);
212
207
 
213
 
        for(j=0;j<6;j++)
 
208
        for (j = 0; j < 6; j++)
214
209
            s->block_last_index[j] = -1;
215
210
 
216
 
        s->mv_dir = MV_DIR_FORWARD;
217
 
        s->mv_type = MV_TYPE_16X16;
218
 
        s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
219
 
        s->mv[0][0][0] = 0;
220
 
        s->mv[0][0][1] = 0;
221
 
        s->mb_skipped = 1;
222
 
        h->mtype &= ~MB_TYPE_H261_FIL;
 
211
        s->mv_dir                      = MV_DIR_FORWARD;
 
212
        s->mv_type                     = MV_TYPE_16X16;
 
213
        s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
 
214
        s->mv[0][0][0]                 = 0;
 
215
        s->mv[0][0][1]                 = 0;
 
216
        s->mb_skipped                  = 1;
 
217
        h->mtype                      &= ~MB_TYPE_H261_FIL;
223
218
 
224
219
        ff_MPV_decode_mb(s, s->block);
225
220
    }
227
222
    return 0;
228
223
}
229
224
 
230
 
static int decode_mv_component(GetBitContext *gb, int v){
 
225
static const int mvmap[17] = {
 
226
    0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
 
227
};
 
228
 
 
229
static int decode_mv_component(GetBitContext *gb, int v)
 
230
{
231
231
    int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
232
232
 
233
233
    /* check if mv_diff is valid */
234
 
    if ( mv_diff < 0 )
 
234
    if (mv_diff < 0)
235
235
        return v;
236
236
 
237
237
    mv_diff = mvmap[mv_diff];
238
238
 
239
 
    if(mv_diff && !get_bits1(gb))
240
 
        mv_diff= -mv_diff;
 
239
    if (mv_diff && !get_bits1(gb))
 
240
        mv_diff = -mv_diff;
241
241
 
242
242
    v += mv_diff;
243
 
    if     (v <=-16) v+= 32;
244
 
    else if(v >= 16) v-= 32;
 
243
    if (v <= -16)
 
244
        v += 32;
 
245
    else if (v >= 16)
 
246
        v -= 32;
245
247
 
246
248
    return v;
247
249
}
248
250
 
249
 
static int h261_decode_mb(H261Context *h){
250
 
    MpegEncContext * const s = &h->s;
 
251
/**
 
252
 * Decode a macroblock.
 
253
 * @return <0 if an error occurred
 
254
 */
 
255
static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
 
256
{
 
257
    MpegEncContext *const s = &h->s;
 
258
    int code, level, i, j, run;
 
259
    RLTable *rl = &ff_h261_rl_tcoeff;
 
260
    const uint8_t *scan_table;
 
261
 
 
262
    /* For the variable length encoding there are two code tables, one being
 
263
     * used for the first transmitted LEVEL in INTER, INTER + MC and
 
264
     * INTER + MC + FIL blocks, the second for all other LEVELs except the
 
265
     * first one in INTRA blocks which is fixed length coded with 8 bits.
 
266
     * NOTE: The two code tables only differ in one VLC so we handle that
 
267
     * manually. */
 
268
    scan_table = s->intra_scantable.permutated;
 
269
    if (s->mb_intra) {
 
270
        /* DC coef */
 
271
        level = get_bits(&s->gb, 8);
 
272
        // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
 
273
        if ((level & 0x7F) == 0) {
 
274
            av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
 
275
                   level, s->mb_x, s->mb_y);
 
276
            return -1;
 
277
        }
 
278
        /* The code 1000 0000 is not used, the reconstruction level of 1024
 
279
         * being coded as 1111 1111. */
 
280
        if (level == 255)
 
281
            level = 128;
 
282
        block[0] = level;
 
283
        i        = 1;
 
284
    } else if (coded) {
 
285
        // Run  Level   Code
 
286
        // EOB          Not possible for first level when cbp is available (that's why the table is different)
 
287
        // 0    1       1s
 
288
        // *    *       0*
 
289
        int check = show_bits(&s->gb, 2);
 
290
        i = 0;
 
291
        if (check & 0x2) {
 
292
            skip_bits(&s->gb, 2);
 
293
            block[0] = (check & 0x1) ? -1 : 1;
 
294
            i        = 1;
 
295
        }
 
296
    } else {
 
297
        i = 0;
 
298
    }
 
299
    if (!coded) {
 
300
        s->block_last_index[n] = i - 1;
 
301
        return 0;
 
302
    }
 
303
    for (;;) {
 
304
        code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
 
305
        if (code < 0) {
 
306
            av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
 
307
                   s->mb_x, s->mb_y);
 
308
            return -1;
 
309
        }
 
310
        if (code == rl->n) {
 
311
            /* escape */
 
312
            /* The remaining combinations of (run, level) are encoded with a
 
313
             * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
 
314
             * level. */
 
315
            run   = get_bits(&s->gb, 6);
 
316
            level = get_sbits(&s->gb, 8);
 
317
        } else if (code == 0) {
 
318
            break;
 
319
        } else {
 
320
            run   = rl->table_run[code];
 
321
            level = rl->table_level[code];
 
322
            if (get_bits1(&s->gb))
 
323
                level = -level;
 
324
        }
 
325
        i += run;
 
326
        if (i >= 64) {
 
327
            av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n",
 
328
                   s->mb_x, s->mb_y);
 
329
            return -1;
 
330
        }
 
331
        j        = scan_table[i];
 
332
        block[j] = level;
 
333
        i++;
 
334
    }
 
335
    s->block_last_index[n] = i - 1;
 
336
    return 0;
 
337
}
 
338
 
 
339
static int h261_decode_mb(H261Context *h)
 
340
{
 
341
    MpegEncContext *const s = &h->s;
251
342
    int i, cbp, xy;
252
343
 
253
344
    cbp = 63;
254
345
    // Read mba
255
 
    do{
256
 
        h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
 
346
    do {
 
347
        h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table,
 
348
                               H261_MBA_VLC_BITS, 2);
257
349
 
258
350
        /* Check for slice end */
259
351
        /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
260
 
        if (h->mba_diff == MBA_STARTCODE){ // start code
 
352
        if (h->mba_diff == MBA_STARTCODE) { // start code
261
353
            h->gob_start_code_skipped = 1;
262
354
            return SLICE_END;
263
355
        }
264
 
    }
265
 
    while( h->mba_diff == MBA_STUFFING ); // stuffing
 
356
    } while (h->mba_diff == MBA_STUFFING); // stuffing
266
357
 
267
 
    if ( h->mba_diff < 0 ){
 
358
    if (h->mba_diff < 0) {
268
359
        if (get_bits_left(&s->gb) <= 7)
269
360
            return SLICE_END;
270
361
 
272
363
        return SLICE_ERROR;
273
364
    }
274
365
 
275
 
    h->mba_diff += 1;
 
366
    h->mba_diff    += 1;
276
367
    h->current_mba += h->mba_diff;
277
368
 
278
 
    if ( h->current_mba > MBA_STUFFING )
 
369
    if (h->current_mba > MBA_STUFFING)
279
370
        return SLICE_ERROR;
280
371
 
281
 
    s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
282
 
    s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
283
 
    xy = s->mb_x + s->mb_y * s->mb_stride;
 
372
    s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11);
 
373
    s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11);
 
374
    xy      = s->mb_x + s->mb_y * s->mb_stride;
284
375
    ff_init_block_index(s);
285
376
    ff_update_block_index(s);
286
377
 
287
378
    // Read mtype
288
379
    h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
289
 
    if (h->mtype < 0 || h->mtype >= FF_ARRAY_ELEMS(h261_mtype_map)) {
 
380
    if (h->mtype < 0 || h->mtype >= FF_ARRAY_ELEMS(ff_h261_mtype_map)) {
290
381
        av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n",
291
382
               h->mtype);
292
383
        return SLICE_ERROR;
293
384
    }
294
 
    h->mtype = h261_mtype_map[h->mtype];
 
385
    h->mtype = ff_h261_mtype_map[h->mtype];
295
386
 
296
387
    // Read mquant
297
 
    if ( IS_QUANT ( h->mtype ) ){
 
388
    if (IS_QUANT(h->mtype))
298
389
        ff_set_qscale(s, get_bits(&s->gb, 5));
299
 
    }
300
390
 
301
391
    s->mb_intra = IS_INTRA4x4(h->mtype);
302
392
 
303
393
    // Read mv
304
 
    if ( IS_16X16 ( h->mtype ) ){
305
 
        // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
306
 
        // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
307
 
        // following three situations:
308
 
        // 1) evaluating MVD for macroblocks 1, 12 and 23;
309
 
        // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
310
 
        // 3) MTYPE of the previous macroblock was not MC.
311
 
        if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
312
 
             ( h->mba_diff != 1))
313
 
        {
 
394
    if (IS_16X16(h->mtype)) {
 
395
        /* Motion vector data is included for all MC macroblocks. MVD is
 
396
         * obtained from the macroblock vector by subtracting the vector
 
397
         * of the preceding macroblock. For this calculation the vector
 
398
         * of the preceding macroblock is regarded as zero in the
 
399
         * following three situations:
 
400
         * 1) evaluating MVD for macroblocks 1, 12 and 23;
 
401
         * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
 
402
         * 3) MTYPE of the previous macroblock was not MC. */
 
403
        if ((h->current_mba ==  1) || (h->current_mba == 12) ||
 
404
            (h->current_mba == 23) || (h->mba_diff != 1)) {
314
405
            h->current_mv_x = 0;
315
406
            h->current_mv_y = 0;
316
407
        }
317
408
 
318
 
        h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
319
 
        h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
320
 
    }else{
 
409
        h->current_mv_x = decode_mv_component(&s->gb, h->current_mv_x);
 
410
        h->current_mv_y = decode_mv_component(&s->gb, h->current_mv_y);
 
411
    } else {
321
412
        h->current_mv_x = 0;
322
413
        h->current_mv_y = 0;
323
414
    }
324
415
 
325
416
    // Read cbp
326
 
    if ( HAS_CBP( h->mtype ) ){
 
417
    if (HAS_CBP(h->mtype))
327
418
        cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
328
 
    }
329
419
 
330
 
    if(s->mb_intra){
331
 
        s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
 
420
    if (s->mb_intra) {
 
421
        s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
332
422
        goto intra;
333
423
    }
334
424
 
335
425
    //set motion vectors
336
 
    s->mv_dir = MV_DIR_FORWARD;
337
 
    s->mv_type = MV_TYPE_16X16;
338
 
    s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
339
 
    s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
340
 
    s->mv[0][0][1] = h->current_mv_y * 2;
 
426
    s->mv_dir                      = MV_DIR_FORWARD;
 
427
    s->mv_type                     = MV_TYPE_16X16;
 
428
    s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
 
429
    s->mv[0][0][0]                 = h->current_mv_x * 2; // gets divided by 2 in motion compensation
 
430
    s->mv[0][0][1]                 = h->current_mv_y * 2;
341
431
 
342
432
intra:
343
433
    /* decode each block */
344
 
    if(s->mb_intra || HAS_CBP(h->mtype)){
 
434
    if (s->mb_intra || HAS_CBP(h->mtype)) {
345
435
        s->dsp.clear_blocks(s->block[0]);
346
436
        for (i = 0; i < 6; i++) {
347
 
            if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
 
437
            if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0)
348
438
                return SLICE_ERROR;
349
 
            }
350
 
            cbp+=cbp;
 
439
            cbp += cbp;
351
440
        }
352
 
    }else{
 
441
    } else {
353
442
        for (i = 0; i < 6; i++)
354
 
            s->block_last_index[i]= -1;
 
443
            s->block_last_index[i] = -1;
355
444
    }
356
445
 
357
446
    ff_MPV_decode_mb(s, s->block);
360
449
}
361
450
 
362
451
/**
363
 
 * Decode a macroblock.
364
 
 * @return <0 if an error occurred
365
 
 */
366
 
static int h261_decode_block(H261Context * h, DCTELEM * block,
367
 
                             int n, int coded)
368
 
{
369
 
    MpegEncContext * const s = &h->s;
370
 
    int code, level, i, j, run;
371
 
    RLTable *rl = &h261_rl_tcoeff;
372
 
    const uint8_t *scan_table;
373
 
 
374
 
    // For the variable length encoding there are two code tables, one being used for
375
 
    // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
376
 
    // for all other LEVELs except the first one in INTRA blocks which is fixed length
377
 
    // coded with 8 bits.
378
 
    // NOTE: the two code tables only differ in one VLC so we handle that manually.
379
 
    scan_table = s->intra_scantable.permutated;
380
 
    if (s->mb_intra){
381
 
        /* DC coef */
382
 
        level = get_bits(&s->gb, 8);
383
 
        // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
384
 
        if((level&0x7F) == 0){
385
 
            av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
386
 
            return -1;
387
 
        }
388
 
        // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
389
 
        if (level == 255)
390
 
            level = 128;
391
 
        block[0] = level;
392
 
        i = 1;
393
 
    }else if(coded){
394
 
        // Run  Level   Code
395
 
        // EOB                  Not possible for first level when cbp is available (that's why the table is different)
396
 
        // 0    1               1s
397
 
        // *    *               0*
398
 
        int check = show_bits(&s->gb, 2);
399
 
        i = 0;
400
 
        if ( check & 0x2 ){
401
 
            skip_bits(&s->gb, 2);
402
 
            block[0] = ( check & 0x1 ) ? -1 : 1;
403
 
            i = 1;
404
 
        }
405
 
    }else{
406
 
        i = 0;
407
 
    }
408
 
    if(!coded){
409
 
        s->block_last_index[n] = i - 1;
410
 
        return 0;
411
 
    }
412
 
    for(;;){
413
 
        code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
414
 
        if (code < 0){
415
 
            av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
416
 
            return -1;
417
 
        }
418
 
        if (code == rl->n) {
419
 
            /* escape */
420
 
            // The remaining combinations of (run, level) are encoded with a 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits level.
421
 
            run = get_bits(&s->gb, 6);
422
 
            level = get_sbits(&s->gb, 8);
423
 
        }else if(code == 0){
424
 
            break;
425
 
        }else{
426
 
            run = rl->table_run[code];
427
 
            level = rl->table_level[code];
428
 
            if (get_bits1(&s->gb))
429
 
                level = -level;
430
 
        }
431
 
        i += run;
432
 
        if (i >= 64){
433
 
            av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
434
 
            return -1;
435
 
        }
436
 
        j = scan_table[i];
437
 
        block[j] = level;
438
 
        i++;
439
 
    }
440
 
    s->block_last_index[n] = i-1;
441
 
    return 0;
442
 
}
443
 
 
444
 
/**
445
452
 * Decode the H.261 picture header.
446
453
 * @return <0 if no startcode found
447
454
 */
448
 
static int h261_decode_picture_header(H261Context *h){
449
 
    MpegEncContext * const s = &h->s;
 
455
static int h261_decode_picture_header(H261Context *h)
 
456
{
 
457
    MpegEncContext *const s = &h->s;
450
458
    int format, i;
451
 
    uint32_t startcode= 0;
 
459
    uint32_t startcode = 0;
452
460
 
453
 
    for(i= get_bits_left(&s->gb); i>24; i-=1){
 
461
    for (i = get_bits_left(&s->gb); i > 24; i -= 1) {
454
462
        startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
455
463
 
456
 
        if(startcode == 0x10)
 
464
        if (startcode == 0x10)
457
465
            break;
458
466
    }
459
467
 
460
 
    if (startcode != 0x10){
 
468
    if (startcode != 0x10) {
461
469
        av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
462
470
        return -1;
463
471
    }
464
472
 
465
473
    /* temporal reference */
466
 
    i= get_bits(&s->gb, 5); /* picture timestamp */
467
 
    if(i < (s->picture_number&31))
 
474
    i = get_bits(&s->gb, 5); /* picture timestamp */
 
475
    if (i < (s->picture_number & 31))
468
476
        i += 32;
469
 
    s->picture_number = (s->picture_number&~31) + i;
470
 
 
471
 
    s->avctx->time_base= (AVRational){1001, 30000};
472
 
    s->current_picture.f.pts = s->picture_number;
473
 
 
 
477
    s->picture_number = (s->picture_number & ~31) + i;
 
478
 
 
479
    s->avctx->time_base      = (AVRational) { 1001, 30000 };
474
480
 
475
481
    /* PTYPE starts here */
476
482
    skip_bits1(&s->gb); /* split screen off */
479
485
 
480
486
    format = get_bits1(&s->gb);
481
487
 
482
 
    //only 2 formats possible
483
 
    if (format == 0){//QCIF
484
 
        s->width = 176;
485
 
        s->height = 144;
486
 
        s->mb_width = 11;
 
488
    // only 2 formats possible
 
489
    if (format == 0) { // QCIF
 
490
        s->width     = 176;
 
491
        s->height    = 144;
 
492
        s->mb_width  = 11;
487
493
        s->mb_height = 9;
488
 
    }else{//CIF
489
 
        s->width = 352;
490
 
        s->height = 288;
491
 
        s->mb_width = 22;
 
494
    } else { // CIF
 
495
        s->width     = 352;
 
496
        s->height    = 288;
 
497
        s->mb_width  = 22;
492
498
        s->mb_height = 18;
493
499
    }
494
500
 
498
504
    skip_bits1(&s->gb); /* Reserved */
499
505
 
500
506
    /* PEI */
501
 
    while (get_bits1(&s->gb) != 0){
 
507
    while (get_bits1(&s->gb) != 0)
502
508
        skip_bits(&s->gb, 8);
503
 
    }
504
509
 
505
 
    // h261 has no I-FRAMES, but if we pass AV_PICTURE_TYPE_I for the first frame, the codec crashes if it does
506
 
    // not contain all I-blocks (e.g. when a packet is lost)
 
510
    /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
 
511
     * frame, the codec crashes if it does not contain all I-blocks
 
512
     * (e.g. when a packet is lost). */
507
513
    s->pict_type = AV_PICTURE_TYPE_P;
508
514
 
509
515
    h->gob_number = 0;
510
516
    return 0;
511
517
}
512
518
 
513
 
static int h261_decode_gob(H261Context *h){
514
 
    MpegEncContext * const s = &h->s;
 
519
static int h261_decode_gob(H261Context *h)
 
520
{
 
521
    MpegEncContext *const s = &h->s;
515
522
 
516
523
    ff_set_qscale(s, s->qscale);
517
524
 
518
525
    /* decode mb's */
519
 
    while(h->current_mba <= MBA_STUFFING)
520
 
    {
 
526
    while (h->current_mba <= MBA_STUFFING) {
521
527
        int ret;
522
528
        /* DCT & quantize */
523
 
        ret= h261_decode_mb(h);
524
 
        if(ret<0){
525
 
            if(ret==SLICE_END){
 
529
        ret = h261_decode_mb(h);
 
530
        if (ret < 0) {
 
531
            if (ret == SLICE_END) {
526
532
                h261_decode_mb_skipped(h, h->current_mba, 33);
527
533
                return 0;
528
534
            }
529
 
            av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
 
535
            av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n",
 
536
                   s->mb_x + s->mb_y * s->mb_stride);
530
537
            return -1;
531
538
        }
532
539
 
533
 
        h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
 
540
        h261_decode_mb_skipped(h,
 
541
                               h->current_mba - h->mba_diff,
 
542
                               h->current_mba - 1);
534
543
    }
535
544
 
536
545
    return -1;
539
548
/**
540
549
 * returns the number of bytes consumed for building the current frame
541
550
 */
542
 
static int get_consumed_bytes(MpegEncContext *s, int buf_size){
543
 
    int pos= get_bits_count(&s->gb)>>3;
544
 
    if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
545
 
    if(pos+10>buf_size) pos=buf_size; // oops ;)
 
551
static int get_consumed_bytes(MpegEncContext *s, int buf_size)
 
552
{
 
553
    int pos = get_bits_count(&s->gb) >> 3;
 
554
    if (pos == 0)
 
555
        pos = 1;      // avoid infinite loops (i doubt that is needed but ...)
 
556
    if (pos + 10 > buf_size)
 
557
        pos = buf_size;               // oops ;)
546
558
 
547
559
    return pos;
548
560
}
549
561
 
550
 
static int h261_decode_frame(AVCodecContext *avctx,
551
 
                             void *data, int *got_frame,
552
 
                             AVPacket *avpkt)
 
562
static int h261_decode_frame(AVCodecContext *avctx, void *data,
 
563
                             int *got_frame, AVPacket *avpkt)
553
564
{
554
565
    const uint8_t *buf = avpkt->data;
555
 
    int buf_size = avpkt->size;
556
 
    H261Context *h= avctx->priv_data;
557
 
    MpegEncContext *s = &h->s;
 
566
    int buf_size       = avpkt->size;
 
567
    H261Context *h     = avctx->priv_data;
 
568
    MpegEncContext *s  = &h->s;
558
569
    int ret;
559
570
    AVFrame *pict = data;
560
571
 
561
572
    av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
562
573
    av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
563
 
    s->flags= avctx->flags;
564
 
    s->flags2= avctx->flags2;
 
574
    s->flags  = avctx->flags;
 
575
    s->flags2 = avctx->flags2;
565
576
 
566
 
    h->gob_start_code_skipped=0;
 
577
    h->gob_start_code_skipped = 0;
567
578
 
568
579
retry:
569
 
 
570
 
    init_get_bits(&s->gb, buf, buf_size*8);
571
 
 
572
 
    if(!s->context_initialized){
573
 
        if (ff_MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
 
580
    init_get_bits(&s->gb, buf, buf_size * 8);
 
581
 
 
582
    if (!s->context_initialized)
 
583
        // we need the IDCT permutaton for reading a custom matrix
 
584
        if (ff_MPV_common_init(s) < 0)
574
585
            return -1;
575
 
    }
576
 
 
577
 
    //we need to set current_picture_ptr before reading the header, otherwise we cannot store anyting im there
578
 
    if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
579
 
        int i= ff_find_unused_picture(s, 0);
580
 
        if (i < 0)
581
 
            return i;
582
 
        s->current_picture_ptr= &s->picture[i];
583
 
    }
584
586
 
585
587
    ret = h261_decode_picture_header(h);
586
588
 
587
589
    /* skip if the header was thrashed */
588
 
    if (ret < 0){
 
590
    if (ret < 0) {
589
591
        av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
590
592
        return -1;
591
593
    }
592
594
 
593
 
    if (s->width != avctx->coded_width || s->height != avctx->coded_height){
594
 
        ParseContext pc= s->parse_context; //FIXME move this demuxing hack to libavformat
595
 
        s->parse_context.buffer=0;
 
595
    if (s->width != avctx->coded_width || s->height != avctx->coded_height) {
 
596
        ParseContext pc = s->parse_context; // FIXME move this demuxing hack to libavformat
 
597
        s->parse_context.buffer = 0;
596
598
        ff_MPV_common_end(s);
597
 
        s->parse_context= pc;
 
599
        s->parse_context = pc;
598
600
    }
599
601
    if (!s->context_initialized) {
600
 
        avcodec_set_dimensions(avctx, s->width, s->height);
 
602
        ret = ff_set_dimensions(avctx, s->width, s->height);
 
603
        if (ret < 0)
 
604
            return ret;
601
605
 
602
606
        goto retry;
603
607
    }
606
610
    s->current_picture.f.pict_type = s->pict_type;
607
611
    s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
608
612
 
609
 
    if(  (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
610
 
       ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
611
 
       || avctx->skip_frame >= AVDISCARD_ALL)
 
613
    if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
 
614
        (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
 
615
         avctx->skip_frame >= AVDISCARD_ALL)
612
616
        return get_consumed_bytes(s, buf_size);
613
617
 
614
 
    if(ff_MPV_frame_start(s, avctx) < 0)
 
618
    if (ff_MPV_frame_start(s, avctx) < 0)
615
619
        return -1;
616
620
 
617
 
    ff_er_frame_start(s);
 
621
    ff_mpeg_er_frame_start(s);
618
622
 
619
623
    /* decode each macroblock */
620
 
    s->mb_x=0;
621
 
    s->mb_y=0;
 
624
    s->mb_x = 0;
 
625
    s->mb_y = 0;
622
626
 
623
 
    while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
624
 
        if(ff_h261_resync(h)<0)
 
627
    while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) {
 
628
        if (h261_resync(h) < 0)
625
629
            break;
626
630
        h261_decode_gob(h);
627
631
    }
628
632
    ff_MPV_frame_end(s);
629
633
 
630
 
assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
631
 
assert(s->current_picture.f.pict_type == s->pict_type);
 
634
    assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
 
635
    assert(s->current_picture.f.pict_type == s->pict_type);
632
636
 
633
 
    *pict = s->current_picture_ptr->f;
634
 
    ff_print_debug_info(s, pict);
 
637
    if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
 
638
        return ret;
 
639
    ff_print_debug_info(s, s->current_picture_ptr);
635
640
 
636
641
    *got_frame = 1;
637
642
 
640
645
 
641
646
static av_cold int h261_decode_end(AVCodecContext *avctx)
642
647
{
643
 
    H261Context *h= avctx->priv_data;
 
648
    H261Context *h    = avctx->priv_data;
644
649
    MpegEncContext *s = &h->s;
645
650
 
646
651
    ff_MPV_common_end(s);
649
654
 
650
655
AVCodec ff_h261_decoder = {
651
656
    .name           = "h261",
 
657
    .long_name      = NULL_IF_CONFIG_SMALL("H.261"),
652
658
    .type           = AVMEDIA_TYPE_VIDEO,
653
659
    .id             = AV_CODEC_ID_H261,
654
660
    .priv_data_size = sizeof(H261Context),
656
662
    .close          = h261_decode_end,
657
663
    .decode         = h261_decode_frame,
658
664
    .capabilities   = CODEC_CAP_DR1,
659
 
    .long_name      = NULL_IF_CONFIG_SMALL("H.261"),
660
665
};