Revision be3492ec

View differences:

libavcodec/vc1.c
1 1
/*
2 2
 * VC-1 and WMV3 decoder
3
 * Copyright (c) 2005 Anonymous
4
 * Copyright (c) 2005 Alex Beregszaszi
5
 * Copyright (c) 2005 Michael Niedermayer
3
 * Copyright (c) 2006 Konstantin Shishkov
4
 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 5
 *
7 6
 * This library is free software; you can redistribute it and/or
8 7
 * modify it under the terms of the GNU Lesser General Public
......
24 23
 * @file vc1.c
25 24
 * VC-1 and WMV3 decoder
26 25
 *
27
 * TODO: most AP stuff, optimize, most of MB layer, transform, filtering and motion compensation, etc
28
 * TODO: use MPV_ !!
29 26
 */
30 27
#include "common.h"
31 28
#include "dsputil.h"
32 29
#include "avcodec.h"
33 30
#include "mpegvideo.h"
34 31
#include "vc1data.h"
32
#include "vc1acdata.h"
35 33

  
36 34
#undef NDEBUG
37 35
#include <assert.h>
......
41 39
extern VLC ff_msmp4_dc_luma_vlc[2], ff_msmp4_dc_chroma_vlc[2];
42 40
#define MB_INTRA_VLC_BITS 9
43 41
extern VLC ff_msmp4_mb_i_vlc;
42
extern const uint16_t ff_msmp4_mb_i_table[64][2];
44 43
#define DC_VLC_BITS 9
44
#define AC_VLC_BITS 9
45 45
static const uint16_t table_mb_intra[64][2];
46 46

  
47
/* Some inhibiting stuff */
48
#define HAS_ADVANCED_PROFILE   0
49
#define TRACE                  1
50

  
51
#if TRACE
52
#  define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
53
                   codes, codes_wrap, codes_size, use_static)          \
54
  if (init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size,     \
55
               codes, codes_wrap, codes_size, use_static) < 0)         \
56
  {                                                                    \
57
    av_log(v->s.avctx, AV_LOG_ERROR, "Error for " # vlc " (%i)\n", i);   \
58
    return -1;                                                         \
59
  }
60
#else
61
#  define INIT_VLC(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size, \
62
                   codes, codes_wrap, codes_size, use_static)          \
63
  init_vlc(vlc, nb_bits, nb_codes, bits, bits_wrap, bits_size,         \
64
           codes, codes_wrap, codes_size, use_static)
65
#endif
66 47

  
67 48
/** Available Profiles */
68 49
//@{
69
#define PROFILE_SIMPLE   0
70
#define PROFILE_MAIN     1
71
#define PROFILE_COMPLEX  2 ///< TODO: WMV9 specific
72
#define PROFILE_ADVANCED 3
50
enum Profile {
51
    PROFILE_SIMPLE,
52
    PROFILE_MAIN,
53
    PROFILE_COMPLEX, ///< TODO: WMV9 specific
54
    PROFILE_ADVANCED
55
};
73 56
//@}
74 57

  
75 58
/** Sequence quantizer mode */
76 59
//@{
77
#define QUANT_FRAME_IMPLICIT   0 ///< Implicitly specified at frame level
78
#define QUANT_FRAME_EXPLICIT   1 ///< Explicitly specified at frame level
79
#define QUANT_NON_UNIFORM      2 ///< Non-uniform quant used for all frames
80
#define QUANT_UNIFORM          3 ///< Uniform quant used for all frames
60
enum QuantMode {
61
    QUANT_FRAME_IMPLICIT,    ///< Implicitly specified at frame level
62
    QUANT_FRAME_EXPLICIT,    ///< Explicitly specified at frame level
63
    QUANT_NON_UNIFORM,       ///< Non-uniform quant used for all frames
64
    QUANT_UNIFORM            ///< Uniform quant used for all frames
65
};
81 66
//@}
82 67

  
83 68
/** Where quant can be changed */
84 69
//@{
85
#define DQPROFILE_FOUR_EDGES   0
86
#define DQPROFILE_DOUBLE_EDGES 1
87
#define DQPROFILE_SINGLE_EDGE  2
88
#define DQPROFILE_ALL_MBS      3
70
enum DQProfile {
71
    DQPROFILE_FOUR_EDGES,
72
    DQPROFILE_DOUBLE_EDGES,
73
    DQPROFILE_SINGLE_EDGE,
74
    DQPROFILE_ALL_MBS
75
};
89 76
//@}
90 77

  
91 78
/** @name Where quant can be changed
92 79
 */
93 80
//@{
94
#define DQPROFILE_FOUR_EDGES   0
95
#define DQSINGLE_BEDGE_LEFT   0
96
#define DQSINGLE_BEDGE_TOP    1
97
#define DQSINGLE_BEDGE_RIGHT  2
98
#define DQSINGLE_BEDGE_BOTTOM 3
81
enum DQSingleEdge {
82
    DQSINGLE_BEDGE_LEFT,
83
    DQSINGLE_BEDGE_TOP,
84
    DQSINGLE_BEDGE_RIGHT,
85
    DQSINGLE_BEDGE_BOTTOM
86
};
99 87
//@}
100 88

  
101 89
/** Which pair of edges is quantized with ALTPQUANT */
102 90
//@{
103
#define DQDOUBLE_BEDGE_TOPLEFT     0
104
#define DQDOUBLE_BEDGE_TOPRIGHT    1
105
#define DQDOUBLE_BEDGE_BOTTOMRIGHT 2
106
#define DQDOUBLE_BEDGE_BOTTOMLEFT  3
91
enum DQDoubleEdge {
92
    DQDOUBLE_BEDGE_TOPLEFT,
93
    DQDOUBLE_BEDGE_TOPRIGHT,
94
    DQDOUBLE_BEDGE_BOTTOMRIGHT,
95
    DQDOUBLE_BEDGE_BOTTOMLEFT
96
};
107 97
//@}
108 98

  
109 99
/** MV modes for P frames */
110 100
//@{
111
#define MV_PMODE_1MV_HPEL_BILIN   0
112
#define MV_PMODE_1MV              1
113
#define MV_PMODE_1MV_HPEL         2
114
#define MV_PMODE_MIXED_MV         3
115
#define MV_PMODE_INTENSITY_COMP   4
101
enum MVModes {
102
    MV_PMODE_1MV_HPEL_BILIN,
103
    MV_PMODE_1MV,
104
    MV_PMODE_1MV_HPEL,
105
    MV_PMODE_MIXED_MV,
106
    MV_PMODE_INTENSITY_COMP
107
};
116 108
//@}
117 109

  
118 110
/** @name MV types for B frames */
119 111
//@{
120
#define BMV_TYPE_BACKWARD          0
121
#define BMV_TYPE_BACKWARD          0
122
#define BMV_TYPE_FORWARD           1
123
#define BMV_TYPE_INTERPOLATED      3
112
enum BMVTypes {
113
    BMV_TYPE_BACKWARD,
114
    BMV_TYPE_FORWARD,
115
    BMV_TYPE_INTERPOLATED = 3 //XXX: ??
116
};
117
//@}
118

  
119
/** @name Block types for P/B frames */
120
//@{
121
enum TransformTypes {
122
    TT_8X8,
123
    TT_8X4_BOTTOM,
124
    TT_8X4_TOP,
125
    TT_8X4, //Both halves
126
    TT_4X8_RIGHT,
127
    TT_4X8_LEFT,
128
    TT_4X8, //Both halves
129
    TT_4X4
130
};
124 131
//@}
125 132

  
133
/** Table for conversion between TTBLK and TTMB */
134
static const int ttblk_to_tt[3][8] = {
135
  { TT_8X4, TT_4X8, TT_8X8, TT_4X4, TT_8X4_TOP, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT },
136
  { TT_8X8, TT_4X8_RIGHT, TT_4X8_LEFT, TT_4X4, TT_8X4, TT_4X8, TT_8X4_BOTTOM, TT_8X4_TOP },
137
  { TT_8X8, TT_4X8, TT_4X4, TT_8X4_BOTTOM, TT_4X8_RIGHT, TT_4X8_LEFT, TT_8X4, TT_8X4_TOP }
138
};
139

  
126 140
/** MV P mode - the 5th element is only used for mode 1 */
127 141
static const uint8_t mv_pmode_table[2][5] = {
128
  { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_MIXED_MV, MV_PMODE_INTENSITY_COMP },
129
  { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_INTENSITY_COMP }
142
  { MV_PMODE_1MV_HPEL_BILIN, MV_PMODE_1MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_MIXED_MV },
143
  { MV_PMODE_1MV, MV_PMODE_MIXED_MV, MV_PMODE_1MV_HPEL, MV_PMODE_INTENSITY_COMP, MV_PMODE_1MV_HPEL_BILIN }
130 144
};
131 145

  
132 146
/** One more frame type */
......
174 188
static VLC vc1_ttblk_vlc[3];
175 189
#define VC1_SUBBLKPAT_VLC_BITS 6
176 190
static VLC vc1_subblkpat_vlc[3];
191

  
192
static VLC vc1_ac_coeff_table[8];
177 193
//@}
178 194

  
195
enum CodingSet {
196
    CS_HIGH_MOT_INTRA = 0,
197
    CS_HIGH_MOT_INTER,
198
    CS_LOW_MOT_INTRA,
199
    CS_LOW_MOT_INTER,
200
    CS_MID_RATE_INTRA,
201
    CS_MID_RATE_INTER,
202
    CS_HIGH_RATE_INTRA,
203
    CS_HIGH_RATE_INTER
204
};
205

  
179 206
/** Bitplane struct
180 207
 * We mainly need data and is_raw, so this struct could be avoided
181 208
 * to save a level of indirection; feel free to modify
......
190 217
    uint8_t is_raw;     ///< Bit values must be read at MB level
191 218
} BitPlane;
192 219

  
220

  
221
/** Block data for DC/AC prediction
222
*/
223
typedef struct Block {
224
    uint16_t dc;
225
    int16_t hor_ac[7];
226
    int16_t vert_ac[7];
227
    int16_t dcstep, step;
228
} Block;
229

  
193 230
/** The VC1 Context
194 231
 * @fixme Change size wherever another size is more efficient
195 232
 * Many members are only used for Advanced Profile
......
197 234
typedef struct VC1Context{
198 235
    MpegEncContext s;
199 236

  
237
    int bits;
238

  
200 239
    /** Simple/Main Profile sequence header */
201 240
    //@{
202 241
    int res_sm;           ///< reserved, 2b
......
210 249
    int reserved;         ///< reserved
211 250
    //@}
212 251

  
213
#if HAS_ADVANCED_PROFILE
214 252
    /** Advanced Profile */
215 253
    //@{
216 254
    int level;            ///< 3bits, for Advanced/Simple Profile, provided by TS layer
......
227 265
    int hrd_param_flag;   ///< Presence of Hypothetical Reference
228 266
                          ///< Decoder parameters
229 267
    //@}
230
#endif
231

  
232 268

  
233 269
    /** Sequence header data for all Profiles
234 270
     * TODO: choose between ints, uint8_ts and monobit flags
......
252 288
    uint8_t mv_mode2;     ///< Secondary MV coding mode (B frames)
253 289
    int k_x;              ///< Number of bits for MVs (depends on MV range)
254 290
    int k_y;              ///< Number of bits for MVs (depends on MV range)
291
    int range_x, range_y; ///< MV range
255 292
    uint8_t pq, altpq;    ///< Current/alternate frame quantizer scale
256 293
    /** pquant parameters */
257 294
    //@{
......
271 308
    uint8_t ttmbf;        ///< Transform type flag
272 309
    int ttmb;             ///< Transform type
273 310
    uint8_t ttblk4x4;     ///< Value of ttblk which indicates a 4x4 transform
311
    int codingset;        ///< index of current table set from 11.8 to use for luma block decoding
312
    int codingset2;       ///< index of current table set from 11.8 to use for chroma block decoding
313
    int pqindex;          ///< raw pqindex used in coding set selection
314

  
315

  
274 316
    /** Luma compensation parameters */
275 317
    //@{
276 318
    uint8_t lumscale;
......
301 343
    uint8_t interpfrm;
302 344
    //@}
303 345

  
304
#if HAS_ADVANCED_PROFILE
305 346
    /** Frame decoding info for Advanced profile */
306 347
    //@{
307 348
    uint8_t fcm; ///< 0->Progressive, 2->Frame-Interlace, 3->Field-Interlace
......
327 368
    uint8_t range_mapy;
328 369
    uint8_t range_mapuv;
329 370
    //@}
330
#endif
331 371
} VC1Context;
332 372

  
333 373
/**
......
341 381
static int get_prefix(GetBitContext *gb, int stop, int len)
342 382
{
343 383
#if 1
344
  int i = 0, tmp = !stop;
384
    int i;
385

  
386
    for(i = 0; i < len && get_bits1(gb) != stop; i++);
387
    return i;
388
/*  int i = 0, tmp = !stop;
345 389

  
346 390
  while (i != len && tmp != stop)
347 391
  {
......
349 393
    i++;
350 394
  }
351 395
  if (i == len && tmp != stop) return len+1;
352
  return i;
396
  return i;*/
353 397
#else
354 398
  unsigned int buf;
355 399
  int log;
......
372 416
#endif
373 417
}
374 418

  
419
static inline int decode210(GetBitContext *gb){
420
    int n;
421
    n = get_bits1(gb);
422
    if (n == 1)
423
        return 0;
424
    else
425
        return 2 - get_bits1(gb);
426
}
427

  
375 428
/**
376 429
 * Init VC-1 specific tables and VC1Context members
377 430
 * @param v The VC1Context to initialize
......
386 439
    v->mv_type_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
387 440
    v->direct_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
388 441
    v->skip_mb_plane = (struct BitPlane) { NULL, 0, 0, 0 };
389
#if HAS_ADVANCED_PROFILE
390 442
    v->ac_pred_plane = v->over_flags_plane = (struct BitPlane) { NULL, 0, 0, 0 };
391 443
    v->hrd_rate = v->hrd_buffer = NULL;
392
#endif
393 444

  
394 445
    /* VLC tables */
395
#if 0 // spec -> actual tables converter
396
    for(i=0; i<64; i++){
397
        int code= (vc1_norm6_spec[i][1] << vc1_norm6_spec[i][4]) + vc1_norm6_spec[i][3];
398
        av_log(NULL, AV_LOG_DEBUG, "0x%03X, ", code);
399
        if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
400
    }
401
    for(i=0; i<64; i++){
402
        int code= vc1_norm6_spec[i][2] + vc1_norm6_spec[i][4];
403
        av_log(NULL, AV_LOG_DEBUG, "%2d, ", code);
404
        if(i%16==15) av_log(NULL, AV_LOG_DEBUG, "\n");
405
    }
406
#endif
407 446
    if(!done)
408 447
    {
409 448
        done = 1;
410
        INIT_VLC(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
449
        init_vlc(&vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
411 450
                 vc1_bfraction_bits, 1, 1,
412 451
                 vc1_bfraction_codes, 1, 1, 1);
413
        INIT_VLC(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
452
        init_vlc(&vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
414 453
                 vc1_norm2_bits, 1, 1,
415 454
                 vc1_norm2_codes, 1, 1, 1);
416
        INIT_VLC(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
455
        init_vlc(&vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
417 456
                 vc1_norm6_bits, 1, 1,
418 457
                 vc1_norm6_codes, 2, 2, 1);
419
        INIT_VLC(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
458
        init_vlc(&vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
420 459
                 vc1_imode_bits, 1, 1,
421 460
                 vc1_imode_codes, 1, 1, 1);
422 461
        for (i=0; i<3; i++)
423 462
        {
424
            INIT_VLC(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
463
            init_vlc(&vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
425 464
                     vc1_ttmb_bits[i], 1, 1,
426 465
                     vc1_ttmb_codes[i], 2, 2, 1);
427
            INIT_VLC(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
466
            init_vlc(&vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
428 467
                     vc1_ttblk_bits[i], 1, 1,
429 468
                     vc1_ttblk_codes[i], 1, 1, 1);
430
            INIT_VLC(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
469
            init_vlc(&vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
431 470
                     vc1_subblkpat_bits[i], 1, 1,
432 471
                     vc1_subblkpat_codes[i], 1, 1, 1);
433 472
        }
434 473
        for(i=0; i<4; i++)
435 474
        {
436
            INIT_VLC(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
475
            init_vlc(&vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
437 476
                     vc1_4mv_block_pattern_bits[i], 1, 1,
438 477
                     vc1_4mv_block_pattern_codes[i], 1, 1, 1);
439
            INIT_VLC(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
478
            init_vlc(&vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
440 479
                     vc1_cbpcy_p_bits[i], 1, 1,
441 480
                     vc1_cbpcy_p_codes[i], 2, 2, 1);
442
            INIT_VLC(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
481
            init_vlc(&vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
443 482
                     vc1_mv_diff_bits[i], 1, 1,
444 483
                     vc1_mv_diff_codes[i], 2, 2, 1);
445 484
        }
485
        for(i=0; i<8; i++)
486
            init_vlc(&vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
487
                     &vc1_ac_tables[i][0][1], 8, 4,
488
                     &vc1_ac_tables[i][0][0], 8, 4, 1);
489
        init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
490
                 &ff_msmp4_mb_i_table[0][1], 4, 2,
491
                 &ff_msmp4_mb_i_table[0][0], 4, 2, 1);
446 492
    }
447 493

  
448 494
    /* Other defaults */
......
452 498
    return 0;
453 499
}
454 500

  
455
#if HAS_ADVANCED_PROFILE
501
/***********************************************************************/
456 502
/**
457
 * Decode sequence header's Hypothetic Reference Decoder data
458
 * @see 6.2.1, p32
459
 * @param v The VC1Context to initialize
460
 * @param gb A GetBitContext initialized from AVCodecContext extra_data
503
 * @defgroup bitplane VC9 Bitplane decoding
504
 * @see 8.7, p56
505
 * @{
506
 */
507

  
508
/** @addtogroup bitplane
509
 * Imode types
510
 * @{
511
 */
512
enum Imode {
513
    IMODE_RAW,
514
    IMODE_NORM2,
515
    IMODE_DIFF2,
516
    IMODE_NORM6,
517
    IMODE_DIFF6,
518
    IMODE_ROWSKIP,
519
    IMODE_COLSKIP
520
};
521
/** @} */ //imode defines
522

  
523
/** Allocate the buffer from a bitplane, given its dimensions
524
 * @param bp Bitplane which buffer is to allocate
525
 * @param[in] width Width of the buffer
526
 * @param[in] height Height of the buffer
461 527
 * @return Status
528
 * @todo TODO: Take into account stride
529
 * @todo TODO: Allow use of external buffers ?
462 530
 */
463
static int decode_hrd(VC1Context *v, GetBitContext *gb)
531
static int alloc_bitplane(BitPlane *bp, int width, int height)
464 532
{
465
    int i, num;
466

  
467
    num = 1 + get_bits(gb, 5);
533
    if (!bp || bp->width<0 || bp->height<0) return -1;
534
    bp->data = (uint8_t*)av_malloc(width*height);
535
    if (!bp->data) return -1;
536
    bp->width = bp->stride = width;
537
    bp->height = height;
538
    return 0;
539
}
468 540

  
469
    /*hrd rate*/
470
    if (v->hrd_rate || num != v->hrd_num_leaky_buckets)
471
    {
472
        av_freep(&v->hrd_rate);
473
    }
474
    if (!v->hrd_rate) v->hrd_rate = av_malloc(num*sizeof(uint16_t));
475
    if (!v->hrd_rate) return -1;
541
/** Free the bitplane's buffer
542
 * @param bp Bitplane which buffer is to free
543
 */
544
static void free_bitplane(BitPlane *bp)
545
{
546
    bp->width = bp->stride = bp->height = 0;
547
    if (bp->data) av_freep(&bp->data);
548
}
476 549

  
477
    /*hrd buffer*/
478
    if (v->hrd_buffer || num != v->hrd_num_leaky_buckets)
479
    {
480
        av_freep(&v->hrd_buffer);
481
    }
482
    if (!v->hrd_buffer) v->hrd_buffer = av_malloc(num*sizeof(uint16_t));
483
    if (!v->hrd_buffer)
484
    {
485
        av_freep(&v->hrd_rate);
486
        return -1;
487
    }
550
/** Decode rows by checking if they are skipped
551
 * @param plane Buffer to store decoded bits
552
 * @param[in] width Width of this buffer
553
 * @param[in] height Height of this buffer
554
 * @param[in] stride of this buffer
555
 */
556
static void decode_rowskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
557
    int x, y;
488 558

  
489
    /*hrd fullness*/
490
    if (v->hrd_fullness || num != v->hrd_num_leaky_buckets)
491
    {
492
        av_freep(&v->hrd_buffer);
493
    }
494
    if (!v->hrd_fullness) v->hrd_fullness = av_malloc(num*sizeof(uint8_t));
495
    if (!v->hrd_fullness)
496
    {
497
        av_freep(&v->hrd_rate);
498
        av_freep(&v->hrd_buffer);
499
        return -1;
559
    for (y=0; y<height; y++){
560
        if (!get_bits(gb, 1)) //rowskip
561
            memset(plane, 0, width);
562
        else
563
            for (x=0; x<width; x++)
564
                plane[x] = get_bits(gb, 1);
565
        plane += stride;
500 566
    }
501
    v->hrd_num_leaky_buckets = num;
567
}
502 568

  
503
    //exponent in base-2 for rate
504
    v->bit_rate_exponent = 6 + get_bits(gb, 4);
505
    //exponent in base-2 for buffer_size
506
    v->buffer_size_exponent = 4 + get_bits(gb, 4);
569
/** Decode columns by checking if they are skipped
570
 * @param plane Buffer to store decoded bits
571
 * @param[in] width Width of this buffer
572
 * @param[in] height Height of this buffer
573
 * @param[in] stride of this buffer
574
 * @fixme FIXME: Optimize
575
 */
576
static void decode_colskip(uint8_t* plane, int width, int height, int stride, GetBitContext *gb){
577
    int x, y;
507 578

  
508
    for (i=0; i<num; i++)
509
    {
510
        //mantissae, ordered (if not, use a function ?
511
        v->hrd_rate[i] = 1 + get_bits(gb, 16);
512
        if (i && v->hrd_rate[i-1]>=v->hrd_rate[i])
513
        {
514
            av_log(v->s.avctx, AV_LOG_ERROR, "HDR Rates aren't strictly increasing:"
515
                   "%i vs %i\n", v->hrd_rate[i-1], v->hrd_rate[i]);
516
            return -1;
517
        }
518
        v->hrd_buffer[i] = 1 + get_bits(gb, 16);
519
        if (i && v->hrd_buffer[i-1]<v->hrd_buffer[i])
520
        {
521
            av_log(v->s.avctx, AV_LOG_ERROR, "HDR Buffers aren't decreasing:"
522
                   "%i vs %i\n", v->hrd_buffer[i-1], v->hrd_buffer[i]);
523
            return -1;
524
        }
579
    for (x=0; x<width; x++){
580
        if (!get_bits(gb, 1)) //colskip
581
            for (y=0; y<height; y++)
582
                plane[y*stride] = 0;
583
        else
584
            for (y=0; y<height; y++)
585
                plane[y*stride] = get_bits(gb, 1);
586
        plane ++;
525 587
    }
526
    return 0;
527 588
}
528 589

  
529
/**
530
 * Decode sequence header for Advanced Profile
531
 * @see Table 2, p18
532
 * @see 6.1.7, pp21-27
533
 * @param v The VC1Context to initialize
534
 * @param gb A GetBitContext initialized from AVCodecContext extra_data
590
/** Decode a bitplane's bits
591
 * @param bp Bitplane where to store the decode bits
592
 * @param v VC-1 context for bit reading and logging
535 593
 * @return Status
594
 * @fixme FIXME: Optimize
595
 * @todo TODO: Decide if a struct is needed
536 596
 */
537
static int decode_advanced_sequence_header(AVCodecContext *avctx, GetBitContext *gb)
597
static int bitplane_decoding(BitPlane *bp, VC1Context *v)
538 598
{
539
    VC1Context *v = avctx->priv_data;
540
    int nr, dr, aspect_ratio;
599
    GetBitContext *gb = &v->s.gb;
541 600

  
542
    v->postprocflag = get_bits(gb, 1);
543
    v->broadcast = get_bits(gb, 1);
544
    v->interlace = get_bits(gb, 1);
601
    int imode, x, y, code, offset;
602
    uint8_t invert, *planep = bp->data;
545 603

  
546
    v->tfcntrflag = get_bits(gb, 1);
547
    v->finterpflag = get_bits(gb, 1); //common
548
    v->panscanflag = get_bits(gb, 1);
549
    v->reserved = get_bits(gb, 1);
550
    if (v->reserved)
551
    {
552
        av_log(avctx, AV_LOG_ERROR, "RESERVED should be 0 (is %i)\n",
553
               v->reserved);
554
        return -1;
555
    }
556
    if (v->extended_mv)
557
        v->extended_dmv = get_bits(gb, 1);
604
    invert = get_bits(gb, 1);
605
    imode = get_vlc2(gb, vc1_imode_vlc.table, VC1_IMODE_VLC_BITS, 1);
558 606

  
559
    /* 6.1.7, p21 */
560
    if (get_bits(gb, 1) /* pic_size_flag */)
607
    bp->is_raw = 0;
608
    switch (imode)
561 609
    {
562
        avctx->coded_width = get_bits(gb, 12) << 1;
563
        avctx->coded_height = get_bits(gb, 12) << 1;
564
        if ( get_bits(gb, 1) /* disp_size_flag */)
610
    case IMODE_RAW:
611
        //Data is actually read in the MB layer (same for all tests == "raw")
612
        bp->is_raw = 1; //invert ignored
613
        return invert;
614
    case IMODE_DIFF2:
615
    case IMODE_NORM2:
616
        if ((bp->height * bp->width) & 1)
565 617
        {
566
            avctx->width = get_bits(gb, 14);
567
            avctx->height = get_bits(gb, 14);
618
            *planep++ = get_bits(gb, 1);
619
            offset = 1;
568 620
        }
569

  
570
        /* 6.1.7.4, p23 */
571
        if ( get_bits(gb, 1) /* aspect_ratio_flag */)
572
        {
573
            aspect_ratio = get_bits(gb, 4); //SAR
574
            if (aspect_ratio == 0x0F) //FF_ASPECT_EXTENDED
575
            {
576
                avctx->sample_aspect_ratio.num = 1 + get_bits(gb, 8);
577
                avctx->sample_aspect_ratio.den = 1 + get_bits(gb, 8);
621
        else offset = 0;
622
        // decode bitplane as one long line
623
        for (y = offset; y < bp->height * bp->width; y += 2) {
624
            code = get_vlc2(gb, vc1_norm2_vlc.table, VC1_NORM2_VLC_BITS, 1);
625
            *planep++ = code & 1;
626
            offset++;
627
            if(offset == bp->width) {
628
                offset = 0;
629
                planep += bp->stride - bp->width;
578 630
            }
579
            else if (aspect_ratio == 0x0E)
580
            {
581
                av_log(avctx, AV_LOG_DEBUG, "Reserved AR found\n");
631
            *planep++ = code >> 1;
632
            offset++;
633
            if(offset == bp->width) {
634
                offset = 0;
635
                planep += bp->stride - bp->width;
582 636
            }
583
            else
637
        }
638
        break;
639
    case IMODE_DIFF6:
640
    case IMODE_NORM6:
641
        if(!(bp->height % 3) && (bp->width % 3)) { // use 2x3 decoding
642
            for(y = 0; y < bp->height; y+= 3) {
643
                for(x = bp->width & 1; x < bp->width; x += 2) {
644
                    code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
645
                    if(code < 0){
646
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
647
                        return -1;
648
                    }
649
                    planep[x + 0] = (code >> 0) & 1;
650
                    planep[x + 1] = (code >> 1) & 1;
651
                    planep[x + 0 + bp->stride] = (code >> 2) & 1;
652
                    planep[x + 1 + bp->stride] = (code >> 3) & 1;
653
                    planep[x + 0 + bp->stride * 2] = (code >> 4) & 1;
654
                    planep[x + 1 + bp->stride * 2] = (code >> 5) & 1;
655
                }
656
                planep += bp->stride * 3;
657
            }
658
            if(bp->width & 1) decode_colskip(bp->data, 1, bp->height, bp->stride, &v->s.gb);
659
        } else { // 3x2
660
            for(y = bp->height & 1; y < bp->height; y += 2) {
661
                for(x = bp->width % 3; x < bp->width; x += 3) {
662
                    code = get_vlc2(gb, vc1_norm6_vlc.table, VC1_NORM6_VLC_BITS, 2);
663
                    if(code < 0){
664
                        av_log(v->s.avctx, AV_LOG_DEBUG, "invalid NORM-6 VLC\n");
665
                        return -1;
666
                    }
667
                    planep[x + 0] = (code >> 0) & 1;
668
                    planep[x + 1] = (code >> 1) & 1;
669
                    planep[x + 2] = (code >> 2) & 1;
670
                    planep[x + 0 + bp->stride] = (code >> 3) & 1;
671
                    planep[x + 1 + bp->stride] = (code >> 4) & 1;
672
                    planep[x + 2 + bp->stride] = (code >> 5) & 1;
673
                }
674
                planep += bp->stride * 2;
675
            }
676
            x = bp->width % 3;
677
            if(x) decode_colskip(bp->data  ,             x, bp->height    , bp->stride, &v->s.gb);
678
            if(bp->height & 1) decode_rowskip(bp->data+x, bp->width - x, bp->height & 1, bp->stride, &v->s.gb);
679
        }
680
        break;
681
    case IMODE_ROWSKIP:
682
        decode_rowskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
683
        break;
684
    case IMODE_COLSKIP:
685
        decode_colskip(bp->data, bp->width, bp->height, bp->stride, &v->s.gb);
686
        break;
687
    default: break;
688
    }
689

  
690
    /* Applying diff operator */
691
    if (imode == IMODE_DIFF2 || imode == IMODE_DIFF6)
692
    {
693
        planep = bp->data;
694
        planep[0] ^= invert;
695
        for (x=1; x<bp->width; x++)
696
            planep[x] ^= planep[x-1];
697
        for (y=1; y<bp->height; y++)
698
        {
699
            planep += bp->stride;
700
            planep[0] ^= planep[-bp->stride];
701
            for (x=1; x<bp->width; x++)
584 702
            {
585
              avctx->sample_aspect_ratio = vc1_pixel_aspect[aspect_ratio];
703
                if (planep[x-1] != planep[x-bp->stride]) planep[x] ^= invert;
704
                else                                     planep[x] ^= planep[x-1];
586 705
            }
587 706
        }
588 707
    }
589
    else
708
    else if (invert)
590 709
    {
591
        avctx->coded_width = avctx->width;
592
        avctx->coded_height = avctx->height;
710
        planep = bp->data;
711
        for (x=0; x<bp->width*bp->height; x++) planep[x] = !planep[x]; //FIXME stride
593 712
    }
713
    return (imode<<1) + invert;
714
}
715
/** @} */ //Bitplane group
594 716

  
595
    /* 6.1.8, p23 */
596
    if ( get_bits(gb, 1) /* framerateflag */)
717
/***********************************************************************/
718
/** VOP Dquant decoding
719
 * @param v VC-1 Context
720
 */
721
static int vop_dquant_decoding(VC1Context *v)
722
{
723
    GetBitContext *gb = &v->s.gb;
724
    int pqdiff;
725

  
726
    //variable size
727
    if (v->dquant == 2)
728
    {
729
        pqdiff = get_bits(gb, 3);
730
        if (pqdiff == 7) v->altpq = get_bits(gb, 5);
731
        else v->altpq = v->pq + pqdiff + 1;
732
    }
733
    else
597 734
    {
598
        if ( !get_bits(gb, 1) /* framerateind */)
735
        v->dquantfrm = get_bits(gb, 1);
736
        if ( v->dquantfrm )
599 737
        {
600
            nr = get_bits(gb, 8);
601
            dr = get_bits(gb, 4);
602
            if (nr<1)
603
            {
604
                av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATENR\n");
605
                return -1;
606
            }
607
            if (nr>5)
608
            {
609
                av_log(avctx, AV_LOG_ERROR,
610
                       "Reserved FRAMERATENR %i not handled\n", nr);
611
                nr = 5; /* overflow protection */
612
            }
613
            if (dr<1)
738
            v->dqprofile = get_bits(gb, 2);
739
            switch (v->dqprofile)
614 740
            {
615
                av_log(avctx, AV_LOG_ERROR, "0 is forbidden for FRAMERATEDR\n");
616
                return -1;
741
            case DQPROFILE_SINGLE_EDGE:
742
            case DQPROFILE_DOUBLE_EDGES:
743
                v->dqsbedge = get_bits(gb, 2);
744
                break;
745
            case DQPROFILE_ALL_MBS:
746
                v->dqbilevel = get_bits(gb, 1);
747
            default: break; //Forbidden ?
617 748
            }
618
            if (dr>2)
749
            if (!v->dqbilevel || v->dqprofile != DQPROFILE_ALL_MBS)
619 750
            {
620
                av_log(avctx, AV_LOG_ERROR,
621
                       "Reserved FRAMERATEDR %i not handled\n", dr);
622
                dr = 2; /* overflow protection */
751
                pqdiff = get_bits(gb, 3);
752
                if (pqdiff == 7) v->altpq = get_bits(gb, 5);
753
                else v->altpq = v->pq + pqdiff + 1;
623 754
            }
624
            avctx->time_base.num = fps_nr[dr - 1];
625
            avctx->time_base.den = fps_nr[nr - 1];
626
        }
627
        else
628
        {
629
            nr = get_bits(gb, 16);
630
            // 0.03125->2048Hz / 0.03125Hz
631
            avctx->time_base.den = 1000000;
632
            avctx->time_base.num = 31250*(1+nr);
633 755
        }
634 756
    }
757
    return 0;
758
}
635 759

  
636
    /* 6.1.9, p25 */
637
    if ( get_bits(gb, 1) /* color_format_flag */)
638
    {
639
        //Chromacity coordinates of color primaries
640
        //like ITU-R BT.709-2, BT.470-2, ...
641
        v->color_prim = get_bits(gb, 8);
642
        if (v->color_prim<1)
643
        {
644
            av_log(avctx, AV_LOG_ERROR, "0 for COLOR_PRIM is forbidden\n");
645
            return -1;
646
        }
647
        if (v->color_prim == 3 || v->color_prim>6)
648
        {
649
            av_log(avctx, AV_LOG_DEBUG, "Reserved COLOR_PRIM %i found\n",
650
                   v->color_prim);
651
            return -1;
652
        }
653 760

  
654
        //Opto-electronic transfer characteristics
655
        v->transfer_char = get_bits(gb, 8);
656
        if (v->transfer_char < 1)
657
        {
658
            av_log(avctx, AV_LOG_ERROR, "0 for TRAMSFER_CHAR is forbidden\n");
659
            return -1;
761
/** Do inverse transform
762
 */
763
static void vc1_inv_trans(DCTELEM block[64], int M, int N)
764
{
765
    int i;
766
    register int t1,t2,t3,t4,t5,t6,t7,t8;
767
    DCTELEM *src, *dst;
768

  
769
    src = block;
770
    dst = block;
771
    if(M==4){
772
        for(i = 0; i < N; i++){
773
            t1 = 17 * (src[0] + src[2]);
774
            t2 = 17 * (src[0] - src[2]);
775
            t3 = 22 * src[1];
776
            t4 = 22 * src[3];
777
            t5 = 10 * src[1];
778
            t6 = 10 * src[3];
779

  
780
            dst[0] = (t1 + t3 + t6 + 4) >> 3;
781
            dst[1] = (t2 - t4 + t5 + 4) >> 3;
782
            dst[2] = (t2 + t4 - t5 + 4) >> 3;
783
            dst[3] = (t1 - t3 - t6 + 4) >> 3;
784

  
785
            src += 8;
786
            dst += 8;
660 787
        }
661
        if (v->transfer_char == 3 || v->transfer_char>8)
662
        {
663
            av_log(avctx, AV_LOG_DEBUG, "Reserved TRANSFERT_CHAR %i found\n",
664
                   v->color_prim);
665
            return -1;
788
    }else{
789
        for(i = 0; i < N; i++){
790
            t1 = 12 * (src[0] + src[4]);
791
            t2 = 12 * (src[0] - src[4]);
792
            t3 = 16 * src[2] +  6 * src[6];
793
            t4 =  6 * src[2] - 16 * src[6];
794

  
795
            t5 = t1 + t3;
796
            t6 = t2 + t4;
797
            t7 = t2 - t4;
798
            t8 = t1 - t3;
799

  
800
            t1 = 16 * src[1] + 15 * src[3] +  9 * src[5] +  4 * src[7];
801
            t2 = 15 * src[1] -  4 * src[3] - 16 * src[5] -  9 * src[7];
802
            t3 =  9 * src[1] - 16 * src[3] +  4 * src[5] + 15 * src[7];
803
            t4 =  4 * src[1] -  9 * src[3] + 15 * src[5] - 16 * src[7];
804

  
805
            dst[0] = (t5 + t1 + 4) >> 3;
806
            dst[1] = (t6 + t2 + 4) >> 3;
807
            dst[2] = (t7 + t3 + 4) >> 3;
808
            dst[3] = (t8 + t4 + 4) >> 3;
809
            dst[4] = (t8 - t4 + 4) >> 3;
810
            dst[5] = (t7 - t3 + 4) >> 3;
811
            dst[6] = (t6 - t2 + 4) >> 3;
812
            dst[7] = (t5 - t1 + 4) >> 3;
813

  
814
            src += 8;
815
            dst += 8;
666 816
        }
817
    }
667 818

  
668
        //Matrix coefficient for primariev->YCbCr
669
        v->matrix_coef = get_bits(gb, 8);
670
        if (v->matrix_coef < 1)
671
        {
672
            av_log(avctx, AV_LOG_ERROR, "0 for MATRIX_COEF is forbidden\n");
673
            return -1;
819
    src = block;
820
    dst = block;
821
    if(N==4){
822
        for(i = 0; i < M; i++){
823
            t1 = 17 * (src[ 0] + src[16]);
824
            t2 = 17 * (src[ 0] - src[16]);
825
            t3 = 22 * src[ 8];
826
            t4 = 22 * src[24];
827
            t5 = 10 * src[ 8];
828
            t6 = 10 * src[24];
829

  
830
            dst[ 0] = (t1 + t3 + t6 + 64) >> 7;
831
            dst[ 8] = (t2 - t4 + t5 + 64) >> 7;
832
            dst[16] = (t2 + t4 - t5 + 64) >> 7;
833
            dst[24] = (t1 - t3 - t6 + 64) >> 7;
834

  
835
            src ++;
836
            dst ++;
674 837
        }
675
        if ((v->matrix_coef > 2 && v->matrix_coef < 6) || v->matrix_coef > 7)
676
        {
677
            av_log(avctx, AV_LOG_DEBUG, "Reserved MATRIX_COEF %i found\n",
678
                   v->color_prim);
679
            return -1;
838
    }else{
839
        for(i = 0; i < M; i++){
840
            t1 = 12 * (src[ 0] + src[32]);
841
            t2 = 12 * (src[ 0] - src[32]);
842
            t3 = 16 * src[16] +  6 * src[48];
843
            t4 =  6 * src[16] - 16 * src[48];
844

  
845
            t5 = t1 + t3;
846
            t6 = t2 + t4;
847
            t7 = t2 - t4;
848
            t8 = t1 - t3;
849

  
850
            t1 = 16 * src[ 8] + 15 * src[24] +  9 * src[40] +  4 * src[56];
851
            t2 = 15 * src[ 8] -  4 * src[24] - 16 * src[40] -  9 * src[56];
852
            t3 =  9 * src[ 8] - 16 * src[24] +  4 * src[40] + 15 * src[56];
853
            t4 =  4 * src[ 8] -  9 * src[24] + 15 * src[40] - 16 * src[56];
854

  
855
            dst[ 0] = (t5 + t1 + 64) >> 7;
856
            dst[ 8] = (t6 + t2 + 64) >> 7;
857
            dst[16] = (t7 + t3 + 64) >> 7;
858
            dst[24] = (t8 + t4 + 64) >> 7;
859
            dst[32] = (t8 - t4 + 64 + 1) >> 7;
860
            dst[40] = (t7 - t3 + 64 + 1) >> 7;
861
            dst[48] = (t6 - t2 + 64 + 1) >> 7;
862
            dst[56] = (t5 - t1 + 64 + 1) >> 7;
863

  
864
            src++;
865
            dst++;
680 866
        }
681 867
    }
868
}
682 869

  
683
    //Hypothetical reference decoder indicator flag
684
    v->hrd_param_flag = get_bits(gb, 1);
685
    if (v->hrd_param_flag)
686
    {
687
      if (decode_hrd(v, gb) < 0) return -1;
688
    }
689

  
690
    /*reset scaling ranges, 6.2.2 & 6.2.3, p33*/
691
    v->range_mapy_flag = 0;
692
    v->range_mapuv_flag = 0;
693

  
694
    av_log(avctx, AV_LOG_DEBUG, "Advanced profile not supported yet\n");
695
    return -1;
870
/** Apply overlap transform
871
 * @todo optimize
872
 * @todo move to DSPContext
873
 */
874
static void vc1_overlap_block(MpegEncContext *s, DCTELEM block[64], int n, int do_hor, int do_vert)
875
{
876
    int i;
877

  
878
    if(do_hor) { //TODO
879
    }
880
    if(do_vert) { //TODO
881
    }
882

  
883
    for(i = 0; i < 64; i++)
884
        block[i] += 128;
885
}
886

  
887

  
888
/** Put block onto picture
889
 * @todo move to DSPContext
890
 */
891
static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
892
{
893
    uint8_t *Y;
894
    int ys, us, vs;
895
    DSPContext *dsp = &v->s.dsp;
896

  
897
    ys = v->s.current_picture.linesize[0];
898
    us = v->s.current_picture.linesize[1];
899
    vs = v->s.current_picture.linesize[2];
900
    Y = v->s.dest[0];
901

  
902
    dsp->put_pixels_clamped(block[0], Y, ys);
903
    dsp->put_pixels_clamped(block[1], Y + 8, ys);
904
    Y += ys * 8;
905
    dsp->put_pixels_clamped(block[2], Y, ys);
906
    dsp->put_pixels_clamped(block[3], Y + 8, ys);
907

  
908
    dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
909
    dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
910
}
911

  
912
/** Do motion compensation over 1 macroblock
913
 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
914
 */
915
static void vc1_mc_1mv(VC1Context *v)
916
{
917
    MpegEncContext *s = &v->s;
918
    DSPContext *dsp = &v->s.dsp;
919
    uint8_t *srcY, *srcU, *srcV;
920
    int dxy, mx, my, src_x, src_y;
921
    int width = s->mb_width * 16, height = s->mb_height * 16;
922

  
923
    if(!v->s.last_picture.data[0])return;
924

  
925
    mx = s->mv[0][0][0] >> s->mspel;
926
    my = s->mv[0][0][1] >> s->mspel;
927
    srcY = s->last_picture.data[0];
928
    srcU = s->last_picture.data[1];
929
    srcV = s->last_picture.data[2];
930

  
931
    if(s->mspel) { // hpel mc
932
        dxy = ((my & 1) << 1) | (mx & 1);
933
        src_x = s->mb_x * 16 + (mx >> 1);
934
        src_y = s->mb_y * 16 + (my >> 1);
935
/*        src_x = clip(src_x, -16, width); //FIXME unneeded for emu?
936
        if (src_x == width)
937
            dxy &= ~1;
938
        src_y = clip(src_y, -16, height);
939
        if (src_y == height)
940
            dxy &= ~2;*/
941
        srcY += src_y * s->linesize + src_x;
942
        srcU += (src_y >> 1) * s->uvlinesize + (src_x >> 1);
943
        srcV += (src_y >> 1) * s->uvlinesize + (src_x >> 1);
944

  
945
        if((unsigned)src_x > s->h_edge_pos - (mx&1) - 16
946
           || (unsigned)src_y > s->v_edge_pos - (my&1) - 16){
947
            uint8_t *uvbuf= s->edge_emu_buffer + 18 * s->linesize;
948

  
949
            ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 16+1, 16+1,
950
                             src_x, src_y, s->h_edge_pos, s->v_edge_pos);
951
            srcY = s->edge_emu_buffer;
952
            ff_emulated_edge_mc(uvbuf, srcU, s->uvlinesize, 8+1, 8+1,
953
                             src_x >> 1, src_y >> 1, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
954
            ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
955
                             src_x >> 1, src_y >> 1, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
956
            srcU = uvbuf;
957
            srcV = uvbuf + 16;
958
        }
959
        dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
960
        dsp->put_no_rnd_pixels_tab[1][0](s->dest[1], srcU, s->uvlinesize, 8);
961
        dsp->put_no_rnd_pixels_tab[1][0](s->dest[2], srcV, s->uvlinesize, 8);
962
    } else {
963
        int motion_x = mx, motion_y = my, uvdxy, uvsrc_x, uvsrc_y;
964
        dxy = ((motion_y & 3) << 2) | (motion_x & 3);
965
        src_x = s->mb_x * 16 + (mx >> 2);
966
        src_y = s->mb_y * 16 + (my >> 2);
967

  
968
        mx= motion_x/2;
969
        my= motion_y/2;
970

  
971
        mx= (mx>>1)|(mx&1);
972
        my= (my>>1)|(my&1);
973

  
974
        uvdxy= (mx&1) | ((my&1)<<1);
975
        mx>>=1;
976
        my>>=1;
977

  
978
        uvsrc_x = s->mb_x * 8 + mx;
979
        uvsrc_y = s->mb_y * 8 + my;
980

  
981
        srcY = s->last_picture.data[0] +   src_y *   s->linesize +   src_x;
982
        srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
983
        srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
984

  
985
        if(   (unsigned)src_x > s->h_edge_pos - (motion_x&3) - 16
986
              || (unsigned)src_y > s->v_edge_pos - (motion_y&3) - 16  ){
987
            uint8_t *uvbuf= s->edge_emu_buffer + 18*s->linesize;
988
            ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17, 17,
989
                                src_x, src_y, s->h_edge_pos, s->v_edge_pos);
990
            srcY = s->edge_emu_buffer;
991
            ff_emulated_edge_mc(uvbuf, srcU, s->uvlinesize, 9, 9,
992
                                    uvsrc_x, uvsrc_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
993
            ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 9, 9,
994
                                    uvsrc_x, uvsrc_y, s->h_edge_pos>>1, s->v_edge_pos>>1);
995
            srcU = uvbuf;
996
            srcV = uvbuf + 16;
997
        }
998

  
999
        dsp->put_no_rnd_qpel_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize);
1000
        dsp->put_no_rnd_pixels_tab[1][uvdxy](s->dest[1], srcU, s->uvlinesize, 8);
1001
        dsp->put_no_rnd_pixels_tab[1][uvdxy](s->dest[2], srcV, s->uvlinesize, 8);
1002
    }
696 1003
}
697
#endif
698 1004

  
699 1005
/**
700 1006
 * Decode Simple/Main Profiles sequence header
......
707 1013
{
708 1014
    VC1Context *v = avctx->priv_data;
709 1015

  
710
    av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32));
1016
    av_log(avctx, AV_LOG_INFO, "Header: %0X\n", show_bits(gb, 32));
711 1017
    v->profile = get_bits(gb, 2);
712 1018
    if (v->profile == 2)
713 1019
    {
714
        av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden\n");
1020
        av_log(avctx, AV_LOG_ERROR, "Profile value 2 is forbidden (and WMV3 Complex Profile is unsupported)\n");
715 1021
        return -1;
716 1022
    }
717 1023

  
718
#if HAS_ADVANCED_PROFILE
719 1024
    if (v->profile == PROFILE_ADVANCED)
720 1025
    {
721 1026
        v->level = get_bits(gb, 3);
......
732 1037
        }
733 1038
    }
734 1039
    else
735
#endif
736 1040
    {
737 1041
        v->res_sm = get_bits(gb, 2); //reserved
738 1042
        if (v->res_sm)
......
754 1058
               "LOOPFILTER shell not be enabled in simple profile\n");
755 1059
    }
756 1060

  
757
#if HAS_ADVANCED_PROFILE
758 1061
    if (v->profile < PROFILE_ADVANCED)
759
#endif
760 1062
    {
761 1063
        v->res_x8 = get_bits(gb, 1); //reserved
762 1064
        if (v->res_x8)
......
792 1094
    v->dquant =  get_bits(gb, 2); //common
793 1095
    v->vstransform =  get_bits(gb, 1); //common
794 1096

  
795
#if HAS_ADVANCED_PROFILE
796 1097
    if (v->profile < PROFILE_ADVANCED)
797
#endif
798 1098
    {
799 1099
        v->res_transtab = get_bits(gb, 1);
800 1100
        if (v->res_transtab)
......
807 1107

  
808 1108
    v->overlap = get_bits(gb, 1); //common
809 1109

  
810
#if HAS_ADVANCED_PROFILE
811 1110
    if (v->profile < PROFILE_ADVANCED)
812
#endif
813 1111
    {
814 1112
        v->s.resync_marker = get_bits(gb, 1);
815 1113
        v->rangered = get_bits(gb, 1);
816 1114
        if (v->rangered && v->profile == PROFILE_SIMPLE)
817 1115
        {
818
            av_log(avctx, AV_LOG_DEBUG,
1116
            av_log(avctx, AV_LOG_INFO,
819 1117
                   "RANGERED should be set to 0 in simple profile\n");
820 1118
        }
821 1119
    }
......
823 1121
    v->s.max_b_frames = avctx->max_b_frames = get_bits(gb, 3); //common
824 1122
    v->quantizer_mode = get_bits(gb, 2); //common
825 1123

  
826
#if HAS_ADVANCED_PROFILE
827 1124
    if (v->profile < PROFILE_ADVANCED)
828
#endif
829 1125
    {
830 1126
        v->finterpflag = get_bits(gb, 1); //common
831 1127
        v->res_rtm_flag = get_bits(gb, 1); //reserved
......
835 1131
                   "0 for reserved RES_RTM_FLAG is forbidden\n");
836 1132
            //return -1;
837 1133
        }
838
#if TRACE
839
        av_log(avctx, AV_LOG_INFO,
1134
        av_log(avctx, AV_LOG_DEBUG,
840 1135
               "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
841 1136
               "LoopFilter=%i, MultiRes=%i, FastUVMV=%i, Extended MV=%i\n"
842 1137
               "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
......
847 1142
               v->dquant, v->quantizer_mode, avctx->max_b_frames
848 1143
               );
849 1144
        return 0;
850
#endif
851 1145
    }
852
#if HAS_ADVANCED_PROFILE
853
    else return decode_advanced_sequence_header(avctx, gb);
854
#endif
1146
    return -1;
855 1147
}
856 1148

  
857 1149

  
858
#if HAS_ADVANCED_PROFILE
859
/** Entry point decoding (Advanced Profile)
860
 * @param avctx Codec context
861
 * @param gb GetBit context initialized from avctx->extra_data
862
 * @return Status
863
 */
864
static int advanced_entry_point_process(AVCodecContext *avctx, GetBitContext *gb)
1150
static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb)
865 1151
{
866
    VC1Context *v = avctx->priv_data;
867
    int i;
868
    if (v->profile != PROFILE_ADVANCED)
869
    {
870
        av_log(avctx, AV_LOG_ERROR,
871
               "Entry point are only defined in Advanced Profile!\n");
872
        return -1; //Only for advanced profile!
873
    }
874
    if (v->hrd_param_flag)
875
    {
876
        //Update buffer fullness
877
        av_log(avctx, AV_LOG_DEBUG, "Buffer fullness update\n");
878
        assert(v->hrd_num_leaky_buckets > 0);
879
        for (i=0; i<v->hrd_num_leaky_buckets; i++)
880
            v->hrd_fullness[i] = get_bits(gb, 8);
881
    }
882
    if ((v->range_mapy_flag = get_bits(gb, 1)))
883
    {
884
        //RANGE_MAPY
885
        av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPY\n");
886
        v->range_mapy = get_bits(gb, 3);
887
    }
888
    if ((v->range_mapuv_flag = get_bits(gb, 1)))
889
    {
890
        //RANGE_MAPUV
891
        av_log(avctx, AV_LOG_DEBUG, "RANGE_MAPUV\n");
892
        v->range_mapuv = get_bits(gb, 3);
1152
    int pqindex, lowquant, status;
1153

  
1154
    if(v->finterpflag) v->interpfrm = get_bits(gb, 1);
1155
    skip_bits(gb, 2); //framecnt unused
1156
    v->rangeredfrm = 0;
1157
    if (v->rangered) v->rangeredfrm = get_bits(gb, 1);
1158
    v->s.pict_type = get_bits(gb, 1);
1159
    if (v->s.avctx->max_b_frames) {
1160
        if (!v->s.pict_type) {
1161
            if (get_bits(gb, 1)) v->s.pict_type = I_TYPE;
1162
            else v->s.pict_type = B_TYPE;
1163
        } else v->s.pict_type = P_TYPE;
1164
    } else v->s.pict_type = v->s.pict_type ? P_TYPE : I_TYPE;
1165

  
1166
    if(v->s.pict_type == I_TYPE)
1167
        get_bits(gb, 7); // skip buffer fullness
1168

  
1169
    /* Quantizer stuff */
1170
    pqindex = get_bits(gb, 5);
1171
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1172
        v->pq = pquant_table[0][pqindex];
1173
    else
1174
        v->pq = pquant_table[v->quantizer_mode-1][pqindex];
1175

  
1176
    if (v->quantizer_mode == QUANT_FRAME_IMPLICIT)
1177
        v->pquantizer = pqindex < 9;
1178
    if (v->quantizer_mode == QUANT_UNIFORM || v->quantizer_mode == QUANT_NON_UNIFORM)
1179
        v->pquantizer = v->quantizer_mode == QUANT_UNIFORM;
1180
    v->pqindex = pqindex;
1181
    if (pqindex < 9) v->halfpq = get_bits(gb, 1);
1182
    else v->halfpq = 0;
1183
    if (v->quantizer_mode == QUANT_FRAME_EXPLICIT)
1184
        v->pquantizer = get_bits(gb, 1);
1185
    v->dquantfrm = 0;
1186

  
1187
//av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1188
//        (v->s.pict_type == P_TYPE) ? 'P' : ((v->s.pict_type == I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1189

  
1190
    //TODO: complete parsing for P/B/BI frames
1191
    switch(v->s.pict_type) {
1192
    case P_TYPE:
1193
        if (v->pq < 5) v->tt_index = 0;
1194
        else if(v->pq < 13) v->tt_index = 1;
1195
        else v->tt_index = 2;
1196

  
1197
        if (v->extended_mv == 1) v->mvrange = get_prefix(gb, 0, 3);
1198
        v->k_x = v->mvrange + 9 + (v->mvrange >> 1); //k_x can be 9 10 12 13
1199
        v->k_y = v->mvrange + 8; //k_y can be 8 9 10 11
1200
        v->range_x = 1 << (v->k_x - 1);
1201
        v->range_y = 1 << (v->k_y - 1);
1202
        if (v->profile == PROFILE_ADVANCED)
1203
        {
1204
            if (v->postprocflag) v->postproc = get_bits(gb, 1);
1205
        }
1206
        else
1207
            if (v->multires) v->respic = get_bits(gb, 2);
1208
        lowquant = (v->pq > 12) ? 0 : 1;
1209
        v->mv_mode = mv_pmode_table[lowquant][get_prefix(gb, 1, 4)];
1210
        if (v->mv_mode == MV_PMODE_INTENSITY_COMP)
1211
        {
1212
            v->mv_mode2 = mv_pmode_table[lowquant][get_prefix(gb, 1, 3)];
1213
            v->lumscale = get_bits(gb, 6);
1214
            v->lumshift = get_bits(gb, 6);
1215
        }
1216
        if(v->mv_mode == MV_PMODE_1MV_HPEL || v->mv_mode == MV_PMODE_1MV_HPEL_BILIN)
1217
            v->s.mspel = 1;
1218
        else
1219
            v->s.mspel = 0;
1220

  
1221
if(v->mv_mode != MV_PMODE_1MV && v->mv_mode != MV_PMODE_1MV_HPEL && v->mv_mode != MV_PMODE_1MV_HPEL_BILIN) {
1222
    av_log(v->s.avctx, AV_LOG_ERROR, "Only 1MV P-frames are supported by now\n");
1223
    return -1;
1224
}
1225
        if ((v->mv_mode == MV_PMODE_INTENSITY_COMP &&
1226
                 v->mv_mode2 == MV_PMODE_MIXED_MV)
1227
                || v->mv_mode == MV_PMODE_MIXED_MV)
1228
        {
1229
            status = bitplane_decoding(&v->mv_type_mb_plane, v);
1230
            if (status < 0) return -1;
1231
            av_log(v->s.avctx, AV_LOG_DEBUG, "MB MV Type plane encoding: "
1232
                   "Imode: %i, Invert: %i\n", status>>1, status&1);
1233
        }
1234
        status = bitplane_decoding(&v->skip_mb_plane, v);
1235
        if (status < 0) return -1;
1236
        av_log(v->s.avctx, AV_LOG_DEBUG, "MB Skip plane encoding: "
1237
               "Imode: %i, Invert: %i\n", status>>1, status&1);
1238

  
1239
        /* Hopefully this is correct for P frames */
1240
        v->s.mv_table_index = get_bits(gb, 2); //but using vc1_ tables
1241
        v->cbpcy_vlc = &vc1_cbpcy_p_vlc[get_bits(gb, 2)];
1242

  
1243
        if (v->dquant)
1244
        {
1245
            av_log(v->s.avctx, AV_LOG_DEBUG, "VOP DQuant info\n");
1246
            vop_dquant_decoding(v);
1247
        }
1248

  
1249
        v->ttfrm = 0; //FIXME Is that so ?
1250
        if (v->vstransform)
1251
        {
1252
            v->ttmbf = get_bits(gb, 1);
1253
            if (v->ttmbf)
1254
            {
1255
                v->ttfrm = get_bits(gb, 2);
1256
            }
1257
        }
1258
        break;
1259
    case B_TYPE:
1260
        break;
893 1261
    }
894
    if (v->panscanflag)
1262

  
1263
    /* AC Syntax */
1264
    v->c_ac_table_index = decode012(gb);
1265
    if (v->s.pict_type == I_TYPE || v->s.pict_type == BI_TYPE)
895 1266
    {
896
        //NUMPANSCANWIN
897
        v->numpanscanwin = get_bits(gb, 3);
898
        av_log(avctx, AV_LOG_DEBUG, "NUMPANSCANWIN: %u\n", v->numpanscanwin);
1267
        v->y_ac_table_index = decode012(gb);
899 1268
    }
1269
    /* DC Syntax */
1270
    v->s.dc_table_index = get_bits(gb, 1);
1271

  
900 1272
    return 0;
901 1273
}
902
#endif
903 1274

  
904 1275
/***********************************************************************/
905 1276
/**
906
 * @defgroup bitplane VC1 Bitplane decoding
907
 * @see 8.7, p56
1277
 * @defgroup block VC-1 Block-level functions
1278
 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1279
 * @todo TODO: Integrate to MpegEncContext facilities
908 1280
 * @{
909 1281
 */
910 1282

  
911
/** @addtogroup bitplane
912
 * Imode types
913
 * @{
1283
/**
1284
 * @def GET_MQUANT
1285
 * @brief Get macroblock-level quantizer scale
1286
 * @warning XXX: qdiff to the frame quant, not previous quant ?
1287
 * @fixme XXX: Don't know how to initialize mquant otherwise in last case
914 1288
 */
915
#define IMODE_RAW     0
916
#define IMODE_NORM2   1
917
#define IMODE_DIFF2   2
918
#define IMODE_NORM6   3
919
#define IMODE_DIFF6   4
920
#define IMODE_ROWSKIP 5
921
#define IMODE_COLSKIP 6
922
/** @} */ //imode defines
1289
#define GET_MQUANT()                                           \
1290
  if (v->dquantfrm)                                            \
1291
  {                                                            \
1292
    if (v->dqprofile == DQPROFILE_ALL_MBS)                     \
1293
    {                                                          \
1294
      if (v->dqbilevel)                                        \
1295
      {                                                        \
1296
        mquant = (get_bits(gb, 1)) ? v->pq : v->altpq;         \
1297
      }                                                        \
1298
      else                                                     \
1299
      {                                                        \
1300
        mqdiff = get_bits(gb, 3);                              \
1301
        if (mqdiff != 7) mquant = v->pq + mqdiff;              \
1302
        else mquant = get_bits(gb, 5);                         \
1303
      }                                                        \
1304
    }                                                          \
1305
    else mquant = v->pq;                                       \
1306
  }
923 1307

  
924
/** Allocate the buffer from a bitplane, given its dimensions
925
 * @param bp Bitplane which buffer is to allocate
926
 * @param[in] width Width of the buffer
927
 * @param[in] height Height of the buffer
928
 * @return Status
929
 * @todo TODO: Take into account stride
930
 * @todo TODO: Allow use of external buffers ?
1308
/**
1309
 * @def GET_MVDATA(_dmv_x, _dmv_y)
1310
 * @brief Get MV differentials
1311
 * @see MVDATA decoding from 8.3.5.2, p(1)20
1312
 * @param _dmv_x Horizontal differential for decoded MV
1313
 * @param _dmv_y Vertical differential for decoded MV
1314
 * @todo TODO: Use MpegEncContext arrays to store them
931 1315
 */
932
static int alloc_bitplane(BitPlane *bp, int width, int height)
933
{
934
    if (!bp || bp->width<0 || bp->height<0) return -1;
935
    bp->data = (uint8_t*)av_malloc(width*height);
936
    if (!bp->data) return -1;
937
    bp->width = bp->stride = width;
938
    bp->height = height;
939
    return 0;
940
}
1316
#define GET_MVDATA(_dmv_x, _dmv_y)                                  \
1317
  index = 1 + get_vlc2(gb, vc1_mv_diff_vlc[s->mv_table_index].table,\
1318
                       VC1_MV_DIFF_VLC_BITS, 2);                    \
1319
  if (index > 36)                                                   \
1320
  {                                                                 \
1321
    mb_has_coeffs = 1;                                              \
1322
    index -= 37;                                                    \
1323
  }                                                                 \
1324
  else mb_has_coeffs = 0;                                           \
1325
  s->mb_intra = 0;                                                  \
1326
  if (!index) { _dmv_x = _dmv_y = 0; }                              \
1327
  else if (index == 35)                                             \
1328
  {                                                                 \
1329
    _dmv_x = get_bits(gb, v->k_x - s->mspel);                       \
1330
    _dmv_y = get_bits(gb, v->k_y - s->mspel);                       \
1331
  }                                                                 \
1332
  else if (index == 36)                                             \
1333
  {                                                                 \
1334
    _dmv_x = 0;                                                     \
1335
    _dmv_y = 0;                                                     \
1336
    s->mb_intra = 1;                                                \
1337
  }                                                                 \
1338
  else                                                              \
1339
  {                                                                 \
1340
    index1 = index%6;                                               \
1341
    if (s->mspel && index1 == 5) val = 1;                           \
1342
    else                         val = 0;                           \
1343
    val = get_bits(gb, size_table[index1] - val);                   \
1344
    sign = 0 - (val&1);                                             \
1345
    _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1346
                                                                    \
1347
    index1 = index/6;                                               \
1348
    if (s->mspel && index1 == 5) val = 1;                           \
1349
    else                          val = 0;                          \
1350
    val = get_bits(gb, size_table[index1] - val);                   \
1351
    sign = 0 - (val&1);                                             \
1352
    _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1353
  }
941 1354

  
942
/** Free the bitplane's buffer
943
 * @param bp Bitplane which buffer is to free
1355
/** Predict and set motion vector
944 1356
 */
945
static void free_bitplane(BitPlane *bp)
1357
static inline void vc1_pred_mv(MpegEncContext *s, int dmv_x, int dmv_y, int mv1, int r_x, int r_y)
946 1358
{
947
    bp->width = bp->stride = bp->height = 0;
948
    if (bp->data) av_freep(&bp->data);
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff