Browse code

RV30/40 decoding core

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

Kostya Shishkov authored on 2007/12/16 21:44:25
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,1277 @@
0
+/*
1
+ * RV30/40 decoder common data
2
+ * Copyright (c) 2007 Mike Melanson, Konstantin Shishkov
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file rv34.c
23
+ * RV30/40 decoder common data
24
+ */
25
+
26
+#include "avcodec.h"
27
+#include "dsputil.h"
28
+#include "mpegvideo.h"
29
+#include "golomb.h"
30
+#include "rectangle.h"
31
+
32
+#include "rv34vlc.h"
33
+#include "rv34data.h"
34
+#include "rv34.h"
35
+
36
+//#define DEBUG
37
+
38
+/** translation of RV30/40 macroblock types to lavc ones */
39
+static const int rv34_mb_type_to_lavc[12] = {
40
+    MB_TYPE_INTRA,
41
+    MB_TYPE_INTRA16x16,
42
+    MB_TYPE_16x16   | MB_TYPE_L0,
43
+    MB_TYPE_8x8     | MB_TYPE_L0,
44
+    MB_TYPE_16x16   | MB_TYPE_L0,
45
+    MB_TYPE_16x16   | MB_TYPE_L1,
46
+    MB_TYPE_SKIP,
47
+    MB_TYPE_DIRECT2 | MB_TYPE_16x16,
48
+    MB_TYPE_16x8    | MB_TYPE_L0,
49
+    MB_TYPE_8x16    | MB_TYPE_L0,
50
+    MB_TYPE_16x16   | MB_TYPE_L0L1,
51
+    MB_TYPE_16x16   | MB_TYPE_L0
52
+};
53
+
54
+
55
+static RV34VLC intra_vlcs[NUM_INTRA_TABLES], inter_vlcs[NUM_INTER_TABLES];
56
+
57
+/**
58
+ * @defgroup vlc RV30/40 VLC generating functions
59
+ * @{
60
+ */
61
+
62
+/**
63
+ * Generate VLC from codeword lengths.
64
+ * @param bits   codeword lengths (zeroes are accepted)
65
+ * @param size   length of input data
66
+ * @param insyms symbols for input codes (NULL for default ones)
67
+ */
68
+static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const uint8_t *insyms)
69
+{
70
+    int i;
71
+    int counts[17] = {0}, codes[17];
72
+    uint16_t cw[size], syms[size];
73
+    uint8_t bits2[size];
74
+    int maxbits = 0, realsize = 0;
75
+
76
+    for(i = 0; i < size; i++){
77
+        if(bits[i]){
78
+            bits2[realsize] = bits[i];
79
+            syms[realsize] = insyms ? insyms[i] : i;
80
+            realsize++;
81
+            maxbits = FFMAX(maxbits, bits[i]);
82
+            counts[bits[i]]++;
83
+        }
84
+    }
85
+
86
+    codes[0] = 0;
87
+    for(i = 0; i < 16; i++)
88
+        codes[i+1] = (codes[i] + counts[i]) << 1;
89
+    for(i = 0; i < realsize; i++)
90
+        cw[i] = codes[bits2[i]]++;
91
+
92
+    init_vlc_sparse(vlc, FFMIN(maxbits, 9), realsize,
93
+                    bits2, 1, 1,
94
+                    cw,    2, 2,
95
+                    syms,  2, 2, INIT_VLC_USE_STATIC);
96
+}
97
+
98
+/**
99
+ * Initialize all tables.
100
+ */
101
+static void rv34_init_tables()
102
+{
103
+    int i, j, k;
104
+
105
+    for(i = 0; i < NUM_INTRA_TABLES; i++){
106
+        for(j = 0; j < 2; j++){
107
+            rv34_gen_vlc(rv34_table_intra_cbppat   [i][j], CBPPAT_VLC_SIZE,   &intra_vlcs[i].cbppattern[j],     NULL);
108
+            rv34_gen_vlc(rv34_table_intra_secondpat[i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].second_pattern[j], NULL);
109
+            rv34_gen_vlc(rv34_table_intra_thirdpat [i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].third_pattern[j],  NULL);
110
+            for(k = 0; k < 4; k++)
111
+                rv34_gen_vlc(rv34_table_intra_cbp[i][j+k*2],  CBP_VLC_SIZE,   &intra_vlcs[i].cbp[j][k],         rv34_cbp_code);
112
+        }
113
+        for(j = 0; j < 4; j++)
114
+            rv34_gen_vlc(rv34_table_intra_firstpat[i][j], FIRSTBLK_VLC_SIZE, &intra_vlcs[i].first_pattern[j], NULL);
115
+        rv34_gen_vlc(rv34_intra_coeff[i], COEFF_VLC_SIZE, &intra_vlcs[i].coefficient, NULL);
116
+    }
117
+
118
+    for(i = 0; i < NUM_INTER_TABLES; i++){
119
+        rv34_gen_vlc(rv34_inter_cbppat[i], CBPPAT_VLC_SIZE, &inter_vlcs[i].cbppattern[0], NULL);
120
+        for(j = 0; j < 4; j++)
121
+            rv34_gen_vlc(rv34_inter_cbp[i][j], CBP_VLC_SIZE, &inter_vlcs[i].cbp[0][j], rv34_cbp_code);
122
+        for(j = 0; j < 2; j++){
123
+            rv34_gen_vlc(rv34_table_inter_firstpat [i][j], FIRSTBLK_VLC_SIZE, &inter_vlcs[i].first_pattern[j],  NULL);
124
+            rv34_gen_vlc(rv34_table_inter_secondpat[i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].second_pattern[j], NULL);
125
+            rv34_gen_vlc(rv34_table_inter_thirdpat [i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].third_pattern[j],  NULL);
126
+        }
127
+        rv34_gen_vlc(rv34_inter_coeff[i], COEFF_VLC_SIZE, &inter_vlcs[i].coefficient, NULL);
128
+    }
129
+}
130
+
131
+/** @} */ // vlc group
132
+
133
+
134
+/**
135
+ * @defgroup transform RV30/40 inverse transform functions
136
+ * @{
137
+ */
138
+
139
+static av_always_inline void rv34_row_transform(int temp[16], DCTELEM *block)
140
+{
141
+    int i;
142
+
143
+    for(i=0; i<4; i++){
144
+        const int z0= 13*(block[i+8*0] +    block[i+8*2]);
145
+        const int z1= 13*(block[i+8*0] -    block[i+8*2]);
146
+        const int z2=  7* block[i+8*1] - 17*block[i+8*3];
147
+        const int z3= 17* block[i+8*1] +  7*block[i+8*3];
148
+
149
+        temp[4*i+0]= z0+z3;
150
+        temp[4*i+1]= z1+z2;
151
+        temp[4*i+2]= z1-z2;
152
+        temp[4*i+3]= z0-z3;
153
+    }
154
+}
155
+
156
+/**
157
+ * Real Video 3.0/4.0 inverse transform
158
+ * Code is almost the same as in SVQ3, only scaling is different.
159
+ */
160
+static void rv34_inv_transform(DCTELEM *block){
161
+    int temp[16];
162
+    int i;
163
+
164
+    rv34_row_transform(temp, block);
165
+
166
+    for(i=0; i<4; i++){
167
+        const int z0= 13*(temp[4*0+i] +    temp[4*2+i]) + 0x200;
168
+        const int z1= 13*(temp[4*0+i] -    temp[4*2+i]) + 0x200;
169
+        const int z2=  7* temp[4*1+i] - 17*temp[4*3+i];
170
+        const int z3= 17* temp[4*1+i] +  7*temp[4*3+i];
171
+
172
+        block[i*8+0]= (z0 + z3)>>10;
173
+        block[i*8+1]= (z1 + z2)>>10;
174
+        block[i*8+2]= (z1 - z2)>>10;
175
+        block[i*8+3]= (z0 - z3)>>10;
176
+    }
177
+
178
+}
179
+
180
+/**
181
+ * RealVideo 3.0/4.0 inverse transform for DC block
182
+ *
183
+ * Code is almost the same as rv34_inv_transform()
184
+ * but final coefficients are multiplied by 1.5 and have no rounding.
185
+ */
186
+static void rv34_inv_transform_noround(DCTELEM *block){
187
+    int temp[16];
188
+    int i;
189
+
190
+    rv34_row_transform(temp, block);
191
+
192
+    for(i=0; i<4; i++){
193
+        const int z0= 13*(temp[4*0+i] +    temp[4*2+i]);
194
+        const int z1= 13*(temp[4*0+i] -    temp[4*2+i]);
195
+        const int z2=  7* temp[4*1+i] - 17*temp[4*3+i];
196
+        const int z3= 17* temp[4*1+i] +  7*temp[4*3+i];
197
+
198
+        block[i*8+0]= ((z0 + z3)*3)>>11;
199
+        block[i*8+1]= ((z1 + z2)*3)>>11;
200
+        block[i*8+2]= ((z1 - z2)*3)>>11;
201
+        block[i*8+3]= ((z0 - z3)*3)>>11;
202
+    }
203
+
204
+}
205
+
206
+/** @} */ // transform
207
+
208
+
209
+/**
210
+ * @defgroup block RV30/40 4x4 block decoding functions
211
+ * @{
212
+ */
213
+
214
+/**
215
+ * Decode coded block pattern.
216
+ */
217
+static int rv34_decode_cbp(GetBitContext *gb, RV34VLC *vlc, int table)
218
+{
219
+    int pattern, code, cbp=0;
220
+    int ones;
221
+    static const int cbp_masks[3] = {0x100000, 0x010000, 0x110000};
222
+    static const int shifts[4] = { 0, 2, 8, 10 };
223
+    int *curshift = shifts;
224
+    int i, t, mask;
225
+
226
+    code = get_vlc2(gb, vlc->cbppattern[table].table, 9, 2);
227
+    pattern = code & 0xF;
228
+    code >>= 4;
229
+
230
+    ones = rv34_count_ones[pattern];
231
+
232
+    for(mask = 8; mask; mask >>= 1, curshift++){
233
+        if(pattern & mask)
234
+            cbp |= get_vlc2(gb, vlc->cbp[table][ones].table, vlc->cbp[table][ones].bits, 1) << curshift[0];
235
+    }
236
+
237
+    for(i = 0; i < 4; i++){
238
+        t = modulo_three_table[code][i];
239
+        if(t == 1)
240
+            cbp |= cbp_masks[get_bits1(gb)] << i;
241
+        if(t == 2)
242
+            cbp |= cbp_masks[2] << i;
243
+    }
244
+    return cbp;
245
+}
246
+
247
+/**
248
+ * Get one coefficient value from the bistream and store it.
249
+ */
250
+static inline void decode_coeff(DCTELEM *dst, int coef, int esc, GetBitContext *gb, VLC* vlc)
251
+{
252
+    if(coef){
253
+        if(coef == esc){
254
+            coef = get_vlc2(gb, vlc->table, 9, 2);
255
+            if(coef > 23){
256
+                coef -= 23;
257
+                coef = 22 + ((1 << coef) | get_bits(gb, coef));
258
+            }
259
+            coef += esc;
260
+        }
261
+        if(get_bits1(gb))
262
+            coef = -coef;
263
+        *dst = coef;
264
+    }
265
+}
266
+
267
+/**
268
+ * Decode 2x2 subblock of coefficients.
269
+ */
270
+static inline void decode_subblock(DCTELEM *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc)
271
+{
272
+    int coeffs[4];
273
+
274
+    coeffs[0] = modulo_three_table[code][0];
275
+    coeffs[1] = modulo_three_table[code][1];
276
+    coeffs[2] = modulo_three_table[code][2];
277
+    coeffs[3] = modulo_three_table[code][3];
278
+    decode_coeff(dst  , coeffs[0], 3, gb, vlc);
279
+    if(is_block2){
280
+        decode_coeff(dst+8, coeffs[1], 2, gb, vlc);
281
+        decode_coeff(dst+1, coeffs[2], 2, gb, vlc);
282
+    }else{
283
+        decode_coeff(dst+1, coeffs[1], 2, gb, vlc);
284
+        decode_coeff(dst+8, coeffs[2], 2, gb, vlc);
285
+    }
286
+    decode_coeff(dst+9, coeffs[3], 2, gb, vlc);
287
+}
288
+
289
+/**
290
+ * Decode coefficients for 4x4 block.
291
+ *
292
+ * This is done by filling 2x2 subblocks with decoded coefficients
293
+ * in this order (the same for subblocks and subblock coefficients):
294
+ *  o--o
295
+ *    /
296
+ *   /
297
+ *  o--o
298
+ */
299
+
300
+static inline void rv34_decode_block(DCTELEM *dst, GetBitContext *gb, RV34VLC *rvlc, int fc, int sc)
301
+{
302
+    int code, pattern;
303
+
304
+    code = get_vlc2(gb, rvlc->first_pattern[fc].table, 9, 2);
305
+
306
+    pattern = code & 0x7;
307
+
308
+    code >>= 3;
309
+    decode_subblock(dst, code, 0, gb, &rvlc->coefficient);
310
+
311
+    if(pattern & 4){
312
+        code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
313
+        decode_subblock(dst + 2, code, 0, gb, &rvlc->coefficient);
314
+    }
315
+    if(pattern & 2){ // Looks like coefficients 1 and 2 are swapped for this block
316
+        code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
317
+        decode_subblock(dst + 8*2, code, 1, gb, &rvlc->coefficient);
318
+    }
319
+    if(pattern & 1){
320
+        code = get_vlc2(gb, rvlc->third_pattern[sc].table, 9, 2);
321
+        decode_subblock(dst + 8*2+2, code, 0, gb, &rvlc->coefficient);
322
+    }
323
+
324
+}
325
+
326
+/**
327
+ * Dequantize ordinary 4x4 block.
328
+ * @todo optimize
329
+ */
330
+static inline void rv34_dequant4x4(DCTELEM *block, int Qdc, int Q)
331
+{
332
+    int i, j;
333
+
334
+    block[0] = (block[0] * Qdc + 8) >> 4;
335
+    for(i = 0; i < 4; i++)
336
+        for(j = !i; j < 4; j++)
337
+            block[j + i*8] = (block[j + i*8] * Q + 8) >> 4;
338
+}
339
+
340
+/**
341
+ * Dequantize 4x4 block of DC values for 16x16 macroblock.
342
+ * @todo optimize
343
+ */
344
+static inline void rv34_dequant4x4_16x16(DCTELEM *block, int Qdc, int Q)
345
+{
346
+    int i;
347
+
348
+    for(i = 0; i < 3; i++)
349
+         block[rv34_dezigzag[i]] = (block[rv34_dezigzag[i]] * Qdc + 8) >> 4;
350
+    for(; i < 16; i++)
351
+         block[rv34_dezigzag[i]] = (block[rv34_dezigzag[i]] * Q + 8) >> 4;
352
+}
353
+/** @} */ //block functions
354
+
355
+
356
+/**
357
+ * @defgroup bitstream RV30/40 bitstream parsing
358
+ * @{
359
+ */
360
+
361
+static inline int decode210(GetBitContext *gb){
362
+    if (get_bits1(gb))
363
+        return 0;
364
+    else
365
+        return 2 - get_bits1(gb);
366
+}
367
+
368
+/**
369
+ * Decode starting slice position.
370
+ * @todo Maybe replace with ff_h263_decode_mba() ?
371
+ */
372
+int ff_rv34_get_start_offset(GetBitContext *gb, int mb_size)
373
+{
374
+    int i;
375
+    for(i = 0; i < 5; i++)
376
+        if(rv34_mb_max_sizes[i] > mb_size)
377
+            break;
378
+    return rv34_mb_bits_sizes[i];
379
+}
380
+
381
+/**
382
+ * Select VLC set for decoding from current quantizer, modifier and frame type.
383
+ */
384
+static inline RV34VLC* choose_vlc_set(int quant, int mod, int type)
385
+{
386
+    if(mod == 2 && quant < 19) quant += 10;
387
+    else if(mod && quant < 26) quant += 5;
388
+    return type ? &inter_vlcs[rv34_quant_to_vlc_set[1][av_clip(quant, 0, 30)]]
389
+                : &intra_vlcs[rv34_quant_to_vlc_set[0][av_clip(quant, 0, 30)]];
390
+}
391
+
392
+/**
393
+ * Decode quantizer difference and return modified quantizer.
394
+ */
395
+static inline int rv34_decode_dquant(GetBitContext *gb, int quant)
396
+{
397
+    if(get_bits1(gb))
398
+        return rv34_dquant_tab[get_bits1(gb)][quant];
399
+    else
400
+        return get_bits(gb, 5);
401
+}
402
+
403
+/** @} */ //bitstream functions
404
+
405
+/**
406
+ * @defgroup mv motion vector related code (prediction, reconstruction, motion compensation)
407
+ * @{
408
+ */
409
+
410
+/** macroblock partition width in 8x8 blocks */
411
+static const uint8_t part_sizes_w[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2 };
412
+
413
+/** macroblock partition height in 8x8 blocks */
414
+static const uint8_t part_sizes_h[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2 };
415
+
416
+/** availability index for subblocks */
417
+static const uint8_t avail_indexes[4] = { 5, 6, 9, 10 };
418
+
419
+/**
420
+ * motion vector prediction
421
+ *
422
+ * Motion prediction performed for the block by using median prediction of
423
+ * motion vectors from the left, top and right top blocks but in corner cases
424
+ * some other vectors may be used instead.
425
+ */
426
+static void rv34_pred_mv(RV34DecContext *r, int block_type, int subblock_no, int dmv_no)
427
+{
428
+    MpegEncContext *s = &r->s;
429
+    int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
430
+    int A[2] = {0}, B[2], C[2];
431
+    int i, j;
432
+    int mx, my;
433
+    int avail_index = avail_indexes[subblock_no];
434
+    int c_off = part_sizes_w[block_type];
435
+
436
+    mv_pos += (subblock_no & 1) + (subblock_no >> 1)*s->b8_stride;
437
+    if(subblock_no == 3)
438
+        c_off = -1;
439
+
440
+    if(r->avail_cache[avail_index - 1]){
441
+        A[0] = s->current_picture_ptr->motion_val[0][mv_pos-1][0];
442
+        A[1] = s->current_picture_ptr->motion_val[0][mv_pos-1][1];
443
+    }
444
+    if(r->avail_cache[avail_index - 4]){
445
+        B[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][0];
446
+        B[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][1];
447
+    }else{
448
+        B[0] = A[0];
449
+        B[1] = A[1];
450
+    }
451
+    if(!r->avail_cache[avail_index - 4 + c_off]){
452
+        if(r->avail_cache[avail_index - 4] && (r->avail_cache[avail_index - 1] || r->rv30)){
453
+            C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][0];
454
+            C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][1];
455
+        }else{
456
+            C[0] = A[0];
457
+            C[1] = A[1];
458
+        }
459
+    }else{
460
+        C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][0];
461
+        C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][1];
462
+    }
463
+    mx = mid_pred(A[0], B[0], C[0]);
464
+    my = mid_pred(A[1], B[1], C[1]);
465
+    mx += r->dmv[dmv_no][0];
466
+    my += r->dmv[dmv_no][1];
467
+    for(j = 0; j < part_sizes_h[block_type]; j++){
468
+        for(i = 0; i < part_sizes_w[block_type]; i++){
469
+            s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
470
+            s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
471
+        }
472
+    }
473
+}
474
+
475
+/**
476
+ * Predict motion vector for B-frame macroblock.
477
+ */
478
+static inline void rv34_pred_b_vector(int A[2], int B[2], int C[2],
479
+                                      int A_avail, int B_avail, int C_avail,
480
+                                      int *mx, int *my)
481
+{
482
+    if(A_avail + B_avail + C_avail != 3){
483
+        *mx = A[0] + B[0] + C[0];
484
+        *my = A[1] + B[1] + C[1];
485
+        if(A_avail + B_avail + C_avail == 2){
486
+            *mx /= 2;
487
+            *my /= 2;
488
+        }
489
+    }else{
490
+        *mx = mid_pred(A[0], B[0], C[0]);
491
+        *my = mid_pred(A[1], B[1], C[1]);
492
+    }
493
+}
494
+
495
+/**
496
+ * motion vector prediction for B-frames
497
+ */
498
+static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir)
499
+{
500
+    MpegEncContext *s = &r->s;
501
+    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
502
+    int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
503
+    int A[2], B[2], C[2];
504
+    int has_A = 0, has_B = 0, has_C = 0;
505
+    int mx, my;
506
+    int i, j;
507
+    Picture *cur_pic = s->current_picture_ptr;
508
+    const int mask = dir ? MB_TYPE_L1 : MB_TYPE_L0;
509
+    int type = cur_pic->mb_type[mb_pos];
510
+
511
+    memset(A, 0, sizeof(A));
512
+    memset(B, 0, sizeof(B));
513
+    memset(C, 0, sizeof(C));
514
+    if((r->avail_cache[5-1] & type) & mask){
515
+        A[0] = cur_pic->motion_val[dir][mv_pos - 1][0];
516
+        A[1] = cur_pic->motion_val[dir][mv_pos - 1][1];
517
+        has_A = 1;
518
+    }
519
+    if((r->avail_cache[5-4] & type) & mask){
520
+        B[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][0];
521
+        B[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][1];
522
+        has_B = 1;
523
+    }
524
+    if((r->avail_cache[5-2] & type) & mask){
525
+        C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][0];
526
+        C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][1];
527
+        has_C = 1;
528
+    }else if((s->mb_x+1) == s->mb_width && (r->avail_cache[5-5] & type) & mask){
529
+        C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][0];
530
+        C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][1];
531
+        has_C = 1;
532
+    }
533
+
534
+    rv34_pred_b_vector(A, B, C, has_A, has_B, has_C, &mx, &my);
535
+
536
+    mx += r->dmv[dir][0];
537
+    my += r->dmv[dir][1];
538
+    //XXX add vector for bidirectionally predicted blocks
539
+    for(j = 0; j < 2; j++){
540
+        for(i = 0; i < 2; i++){
541
+            cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
542
+            cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
543
+        }
544
+    }
545
+    if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD)
546
+        fill_rectangle(cur_pic->motion_val[!dir][mv_pos], 2, 2, s->b8_stride, 0, 4);
547
+}
548
+
549
+/**
550
+ * generic motion compensation function
551
+ *
552
+ * @param r decoder context
553
+ * @param block_type type of the current block
554
+ * @param xoff horizontal offset from the start of the current block
555
+ * @param yoff vertical offset from the start of the current block
556
+ * @param mv_off offset to the motion vector information
557
+ * @param width width of the current partition in 8x8 blocks
558
+ * @param height height of the current partition in 8x8 blocks
559
+ */
560
+static inline void rv34_mc(RV34DecContext *r, const int block_type,
561
+                          const int xoff, const int yoff, int mv_off,
562
+                          const int width, const int height, int dir,
563
+                          const int thirdpel,
564
+                          qpel_mc_func (*qpel_mc)[16],
565
+                          h264_chroma_mc_func (*chroma_mc))
566
+{
567
+    MpegEncContext *s = &r->s;
568
+    uint8_t *Y, *U, *V, *srcY, *srcU, *srcV;
569
+    int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
570
+    int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride + mv_off;
571
+    int is16x16 = 1;
572
+
573
+    if(thirdpel){
574
+#if 0 /// todo
575
+        int lx, ly;
576
+
577
+        mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] / 3;
578
+        my = s->current_picture_ptr->motion_val[dir][mv_pos][1] / 3;
579
+        lx = ((s->current_picture_ptr->motion_val[dir][mv_pos][0] % 3) + 3) % 3;
580
+        ly = ((s->current_picture_ptr->motion_val[dir][mv_pos][1] % 3) + 3) % 3;
581
+        dxy = ly*3 + lx;
582
+        uvmx =
583
+#endif
584
+        mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] >> 2;
585
+        my = s->current_picture_ptr->motion_val[dir][mv_pos][1] >> 2;
586
+        dxy = ((my & 3) << 2) | (mx & 3);
587
+        uvmx = mx & 6;
588
+        uvmy = my & 6;
589
+    }else{
590
+        mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] >> 2;
591
+        my = s->current_picture_ptr->motion_val[dir][mv_pos][1] >> 2;
592
+        dxy = ((my & 3) << 2) | (mx & 3);
593
+        uvmx = mx & 6;
594
+        uvmy = my & 6;
595
+    }
596
+    srcY = dir ? s->next_picture_ptr->data[0] : s->last_picture_ptr->data[0];
597
+    srcU = dir ? s->next_picture_ptr->data[1] : s->last_picture_ptr->data[1];
598
+    srcV = dir ? s->next_picture_ptr->data[2] : s->last_picture_ptr->data[2];
599
+    src_x = s->mb_x * 16 + xoff + mx;
600
+    src_y = s->mb_y * 16 + yoff + my;
601
+    uvsrc_x = s->mb_x * 8 + (xoff >> 1) + (mx >> 1);
602
+    uvsrc_y = s->mb_y * 8 + (yoff >> 1) + (my >> 1);
603
+    srcY += src_y * s->linesize + src_x;
604
+    srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
605
+    srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
606
+    if(   (unsigned)(src_x - !!(mx&3)*2) > s->h_edge_pos - !!(mx&3)*2 - (width <<3) - 3
607
+       || (unsigned)(src_y - !!(my&3)*2) > s->v_edge_pos - !!(my&3)*2 - (height<<3) - 3){
608
+        uint8_t *uvbuf= s->edge_emu_buffer + 20 * s->linesize;
609
+
610
+        srcY -= 2 + 2*s->linesize;
611
+        ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, (width<<3)+4, (height<<3)+4,
612
+                            src_x - 2, src_y - 2, s->h_edge_pos, s->v_edge_pos);
613
+        srcY = s->edge_emu_buffer + 2 + 2*s->linesize;
614
+        ff_emulated_edge_mc(uvbuf     , srcU, s->uvlinesize, (width<<2)+1, (height<<2)+1,
615
+                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
616
+        ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, (width<<2)+1, (height<<2)+1,
617
+                            uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
618
+        srcU = uvbuf;
619
+        srcV = uvbuf + 16;
620
+    }
621
+    Y = s->dest[0] + xoff      + yoff     *s->linesize;
622
+    U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
623
+    V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
624
+
625
+    if(block_type == RV34_MB_P_16x8){
626
+        qpel_mc[1][dxy](Y, srcY, s->linesize);
627
+        Y    += 8;
628
+        srcY += 8;
629
+    }else if(block_type == RV34_MB_P_8x16){
630
+        qpel_mc[1][dxy](Y, srcY, s->linesize);
631
+        Y    += 8 * s->linesize;
632
+        srcY += 8 * s->linesize;
633
+    }
634
+    is16x16 = (block_type != RV34_MB_P_8x8) && (block_type != RV34_MB_P_16x8) && (block_type != RV34_MB_P_8x16);
635
+    qpel_mc[!is16x16][dxy](Y, srcY, s->linesize);
636
+    chroma_mc[2-width]   (U, srcU, s->uvlinesize, height*4, uvmx, uvmy);
637
+    chroma_mc[2-width]   (V, srcV, s->uvlinesize, height*4, uvmx, uvmy);
638
+}
639
+
640
+static void rv34_mc_1mv(RV34DecContext *r, const int block_type,
641
+                        const int xoff, const int yoff, int mv_off,
642
+                        const int width, const int height, int dir)
643
+{
644
+    rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30,
645
+            r->s.dsp.put_h264_qpel_pixels_tab, r->s.dsp.put_h264_chroma_pixels_tab);
646
+}
647
+
648
+static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
649
+{
650
+    rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30,
651
+            r->s.dsp.put_h264_qpel_pixels_tab, r->s.dsp.put_h264_chroma_pixels_tab);
652
+    rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30,
653
+            r->s.dsp.avg_h264_qpel_pixels_tab, r->s.dsp.avg_h264_chroma_pixels_tab);
654
+}
655
+
656
+/** number of motion vectors in each macroblock type */
657
+static const int num_mvs[RV34_MB_TYPES] = { 0, 0, 1, 4, 1, 1, 0, 0, 2, 2, 2, 1 };
658
+
659
+/**
660
+ * Decode motion vector differences
661
+ * and perform motion vector reconstruction and motion compensation.
662
+ */
663
+static int rv34_decode_mv(RV34DecContext *r, int block_type)
664
+{
665
+    MpegEncContext *s = &r->s;
666
+    GetBitContext *gb = &s->gb;
667
+    int i;
668
+
669
+    memset(r->dmv, 0, sizeof(r->dmv));
670
+    for(i = 0; i < num_mvs[block_type]; i++){
671
+        r->dmv[i][0] = svq3_get_se_golomb(gb);
672
+        r->dmv[i][1] = svq3_get_se_golomb(gb);
673
+    }
674
+    switch(block_type){
675
+    case RV34_MB_TYPE_INTRA:
676
+    case RV34_MB_TYPE_INTRA16x16:
677
+        fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);
678
+        return 0;
679
+    case RV34_MB_SKIP:
680
+        if(s->pict_type == P_TYPE){
681
+            fill_rectangle(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], 2, 2, s->b8_stride, 0, 4);
682
+            rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
683
+            break;
684
+        }
685
+    case RV34_MB_B_DIRECT:
686
+        rv34_pred_mv_b  (r, RV34_MB_B_DIRECT, 0);
687
+        rv34_pred_mv_b  (r, RV34_MB_B_DIRECT, 1);
688
+        rv34_mc_2mv     (r, RV34_MB_B_DIRECT);
689
+        break;
690
+    case RV34_MB_P_16x16:
691
+    case RV34_MB_P_MIX16x16:
692
+        rv34_pred_mv(r, block_type, 0, 0);
693
+        rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
694
+        break;
695
+    case RV34_MB_B_FORWARD:
696
+    case RV34_MB_B_BACKWARD:
697
+        r->dmv[1][0] = r->dmv[0][0];
698
+        r->dmv[1][1] = r->dmv[0][1];
699
+        rv34_pred_mv_b  (r, block_type, block_type == RV34_MB_B_BACKWARD);
700
+        rv34_mc_1mv     (r, block_type, 0, 0, 0, 2, 2, block_type == RV34_MB_B_BACKWARD);
701
+        break;
702
+    case RV34_MB_P_16x8:
703
+    case RV34_MB_P_8x16:
704
+        rv34_pred_mv(r, block_type, 0, 0);
705
+        rv34_pred_mv(r, block_type, 1 + (block_type == RV34_MB_P_16x8), 1);
706
+        if(block_type == RV34_MB_P_16x8){
707
+            rv34_mc_1mv(r, block_type, 0, 0, 0,            2, 1, 0);
708
+            rv34_mc_1mv(r, block_type, 0, 8, s->b8_stride, 2, 1, 0);
709
+        }
710
+        if(block_type == RV34_MB_P_8x16){
711
+            rv34_mc_1mv(r, block_type, 0, 0, 0, 1, 2, 0);
712
+            rv34_mc_1mv(r, block_type, 8, 0, 1, 1, 2, 0);
713
+        }
714
+        break;
715
+    case RV34_MB_B_BIDIR:
716
+        rv34_pred_mv_b  (r, block_type, 0);
717
+        rv34_pred_mv_b  (r, block_type, 1);
718
+        rv34_mc_2mv     (r, block_type);
719
+        break;
720
+    case RV34_MB_P_8x8:
721
+        for(i=0;i< 4;i++){
722
+            rv34_pred_mv(r, block_type, i, i);
723
+            rv34_mc_1mv (r, block_type, (i&1)<<3, (i&2)<<2, (i&1)+(i>>1)*s->b8_stride, 1, 1, 0);
724
+        }
725
+        break;
726
+    }
727
+
728
+    return 0;
729
+}
730
+/** @} */ // mv group
731
+
732
+/**
733
+ * @defgroup recons Macroblock reconstruction functions
734
+ * @{
735
+ */
736
+/** mapping of RV30/40 intra prediction types to standard H.264 types */
737
+static const int ittrans[9] = {
738
+ DC_PRED, VERT_PRED, HOR_PRED, DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_LEFT_PRED,
739
+ VERT_RIGHT_PRED, VERT_LEFT_PRED, HOR_UP_PRED, HOR_DOWN_PRED,
740
+};
741
+
742
+/** mapping of RV30/40 intra 16x16 prediction types to standard H.264 types */
743
+static const int ittrans16[4] = {
744
+ DC_PRED8x8, VERT_PRED8x8, HOR_PRED8x8, PLANE_PRED8x8,
745
+};
746
+
747
+/**
748
+ * Perform 4x4 intra prediction.
749
+ */
750
+static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int itype, int up, int left, int down, int right)
751
+{
752
+    uint8_t *prev = dst - stride + 4;
753
+    uint32_t topleft;
754
+
755
+    if(!up && !left)
756
+        itype = DC_128_PRED;
757
+    else if(!up){
758
+        if(itype == VERT_PRED) itype = HOR_PRED;
759
+        if(itype == DC_PRED)   itype = LEFT_DC_PRED;
760
+    }else if(!left){
761
+        if(itype == HOR_PRED)  itype = VERT_PRED;
762
+        if(itype == DC_PRED)   itype = TOP_DC_PRED;
763
+        if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
764
+    }
765
+    if(!down){
766
+        if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
767
+        if(itype == HOR_UP_PRED) itype = HOR_UP_PRED_RV40_NODOWN;
768
+    }
769
+    if(!right && up){
770
+        topleft = dst[-stride + 3] * 0x01010101;
771
+        prev = &topleft;
772
+    }
773
+    r->h.pred4x4[itype](dst, prev, stride);
774
+}
775
+
776
+/** add_pixels_clamped for 4x4 block */
777
+static void rv34_add_4x4_block(uint8_t *dst, int stride, DCTELEM block[64], int off)
778
+{
779
+    int x, y;
780
+    for(y = 0; y < 4; y++)
781
+        for(x = 0; x < 4; x++)
782
+            dst[x + y*stride] = av_clip_uint8(dst[x + y*stride] + block[off + x+y*8]);
783
+}
784
+
785
+static inline int adjust_pred16(int itype, int up, int left)
786
+{
787
+    if(!up && !left)
788
+        itype = DC_128_PRED8x8;
789
+    else if(!up){
790
+        if(itype == PLANE_PRED8x8)itype = HOR_PRED8x8;
791
+        if(itype == VERT_PRED8x8) itype = HOR_PRED8x8;
792
+        if(itype == DC_PRED8x8)   itype = LEFT_DC_PRED8x8;
793
+    }else if(!left){
794
+        if(itype == PLANE_PRED8x8)itype = VERT_PRED8x8;
795
+        if(itype == HOR_PRED8x8)  itype = VERT_PRED8x8;
796
+        if(itype == DC_PRED8x8)   itype = TOP_DC_PRED8x8;
797
+    }
798
+    return itype;
799
+}
800
+
801
+static void rv34_output_macroblock(RV34DecContext *r, int8_t *intra_types, int cbp, int is16)
802
+{
803
+    MpegEncContext *s = &r->s;
804
+    DSPContext *dsp = &s->dsp;
805
+    int i, j;
806
+    uint8_t *Y, *U, *V;
807
+    int itype;
808
+    int avail[6*8] = {0};
809
+    int idx;
810
+
811
+    // Set neighbour information.
812
+    if(r->avail_cache[0])
813
+        avail[0] = 1;
814
+    if(r->avail_cache[1])
815
+        avail[1] = avail[2] = 1;
816
+    if(r->avail_cache[2])
817
+        avail[3] = avail[4] = 1;
818
+    if(r->avail_cache[3])
819
+        avail[5] = 1;
820
+    if(r->avail_cache[4])
821
+        avail[8] = avail[16] = 1;
822
+    if(r->avail_cache[8])
823
+        avail[24] = avail[32] = 1;
824
+
825
+    Y = s->dest[0];
826
+    U = s->dest[1];
827
+    V = s->dest[2];
828
+    if(!is16){
829
+        for(j = 0; j < 4; j++){
830
+            idx = 9 + j*8;
831
+            for(i = 0; i < 4; i++, cbp >>= 1, Y += 4, idx++){
832
+                rv34_pred_4x4_block(r, Y, s->linesize, ittrans[intra_types[i]], avail[idx-8], avail[idx-1], avail[idx+7], avail[idx-7]);
833
+                avail[idx] = 1;
834
+                if(cbp & 1)
835
+                    rv34_add_4x4_block(Y, s->linesize, s->block[(i>>1)+(j&2)], (i&1)*4+(j&1)*32);
836
+            }
837
+            Y += s->linesize * 4 - 4*4;
838
+            intra_types += s->b4_stride;
839
+        }
840
+        intra_types -= s->b4_stride * 4;
841
+        fill_rectangle(r->avail_cache + 5, 2, 2, 4, 0, 4);
842
+        for(j = 0; j < 2; j++){
843
+            idx = 5 + j*4;
844
+            for(i = 0; i < 2; i++, cbp >>= 1, idx++){
845
+                rv34_pred_4x4_block(r, U + i*4 + j*4*s->uvlinesize, s->uvlinesize, ittrans[intra_types[i*2+j*2*s->b4_stride]], r->avail_cache[idx-4], r->avail_cache[idx-1], !i && !j, r->avail_cache[idx-3]);
846
+                rv34_pred_4x4_block(r, V + i*4 + j*4*s->uvlinesize, s->uvlinesize, ittrans[intra_types[i*2+j*2*s->b4_stride]], r->avail_cache[idx-4], r->avail_cache[idx-1], !i && !j, r->avail_cache[idx-3]);
847
+                r->avail_cache[idx] = 1;
848
+                if(cbp & 0x01)
849
+                    rv34_add_4x4_block(U + i*4 + j*4*s->uvlinesize, s->uvlinesize, s->block[4], i*4+j*32);
850
+                if(cbp & 0x10)
851
+                    rv34_add_4x4_block(V + i*4 + j*4*s->uvlinesize, s->uvlinesize, s->block[5], i*4+j*32);
852
+            }
853
+        }
854
+    }else{
855
+        itype = ittrans16[intra_types[0]];
856
+        itype = adjust_pred16(itype, r->avail_cache[5-4], r->avail_cache[5-1]);
857
+        r->h.pred16x16[itype](Y, s->linesize);
858
+        dsp->add_pixels_clamped(s->block[0], Y,     s->current_picture.linesize[0]);
859
+        dsp->add_pixels_clamped(s->block[1], Y + 8, s->current_picture.linesize[0]);
860
+        Y += s->current_picture.linesize[0] * 8;
861
+        dsp->add_pixels_clamped(s->block[2], Y,     s->current_picture.linesize[0]);
862
+        dsp->add_pixels_clamped(s->block[3], Y + 8, s->current_picture.linesize[0]);
863
+
864
+        itype = ittrans16[intra_types[0]];
865
+        if(itype == PLANE_PRED8x8) itype = DC_PRED8x8;
866
+        itype = adjust_pred16(itype, r->avail_cache[5-4], r->avail_cache[5-1]);
867
+        r->h.pred8x8[itype](U, s->uvlinesize);
868
+        dsp->add_pixels_clamped(s->block[4], U, s->uvlinesize);
869
+        r->h.pred8x8[itype](V, s->uvlinesize);
870
+        dsp->add_pixels_clamped(s->block[5], V, s->uvlinesize);
871
+    }
872
+}
873
+
874
+/** @} */ // recons group
875
+
876
+/**
877
+ * @addtogroup bitstream
878
+ * Decode macroblock header and return CBP in case of success, -1 otherwise.
879
+ */
880
+static int rv34_decode_mb_header(RV34DecContext *r, int8_t *intra_types)
881
+{
882
+    MpegEncContext *s = &r->s;
883
+    GetBitContext *gb = &s->gb;
884
+    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
885
+    int i, t;
886
+
887
+    if(!r->si.type){
888
+        r->is16 = get_bits1(gb);
889
+        if(!r->is16 && !r->rv30){
890
+            if(!get_bits1(gb))
891
+                av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
892
+        }
893
+        s->current_picture_ptr->mb_type[mb_pos] = r->is16 ? MB_TYPE_INTRA16x16 : MB_TYPE_INTRA;
894
+        r->block_type = r->is16 ? RV34_MB_TYPE_INTRA16x16 : RV34_MB_TYPE_INTRA;
895
+    }else{
896
+        r->block_type = r->decode_mb_info(r);
897
+        if(r->block_type == -1)
898
+            return -1;
899
+        s->current_picture_ptr->mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
900
+        r->mb_type[mb_pos] = r->block_type;
901
+        if(r->block_type == RV34_MB_SKIP){
902
+            if(s->pict_type == P_TYPE)
903
+                r->mb_type[mb_pos] = RV34_MB_P_16x16;
904
+            if(s->pict_type == B_TYPE)
905
+                r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
906
+        }
907
+        r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->mb_type[mb_pos]);
908
+        rv34_decode_mv(r, r->block_type);
909
+        if(r->block_type == RV34_MB_SKIP){
910
+            fill_rectangle(intra_types, 4, 4, s->b4_stride, 0, sizeof(intra_types[0]));
911
+            return 0;
912
+        }
913
+        r->chroma_vlc = 1;
914
+        r->luma_vlc   = 0;
915
+    }
916
+    if(IS_INTRA(s->current_picture_ptr->mb_type[mb_pos])){
917
+        if(r->is16){
918
+            t = get_bits(gb, 2);
919
+            fill_rectangle(intra_types, 4, 4, s->b4_stride, t, sizeof(intra_types[0]));
920
+            r->luma_vlc   = 2;
921
+        }else{
922
+            if(r->decode_intra_types(r, gb, intra_types) < 0)
923
+                return -1;
924
+            r->luma_vlc   = 1;
925
+        }
926
+        r->chroma_vlc = 0;
927
+        r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
928
+    }else{
929
+        for(i = 0; i < 16; i++)
930
+            intra_types[(i & 3) + (i>>2) * s->b4_stride] = 0;
931
+        r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
932
+        if(r->mb_type[mb_pos] == RV34_MB_P_MIX16x16){
933
+            r->is16 = 1;
934
+            r->chroma_vlc = 1;
935
+            r->luma_vlc   = 2;
936
+            r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
937
+        }
938
+    }
939
+
940
+    return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
941
+}
942
+
943
+/**
944
+ * @addtogroup recons
945
+ * @{
946
+ */
947
+/**
948
+ * mask for retrieving all bits in coded block pattern
949
+ * corresponding to one 8x8 block
950
+ */
951
+#define LUMA_CBP_BLOCK_MASK 0x303
952
+
953
+#define U_CBP_MASK 0x0F0000
954
+#define V_CBP_MASK 0xF00000
955
+
956
+
957
+static void rv34_apply_differences(RV34DecContext *r, int cbp)
958
+{
959
+    static const int shifts[4] = { 0, 2, 8, 10 };
960
+    MpegEncContext *s = &r->s;
961
+    int i;
962
+
963
+    for(i = 0; i < 4; i++)
964
+        if(cbp & (LUMA_CBP_BLOCK_MASK << shifts[i]))
965
+            s->dsp.add_pixels_clamped(s->block[i], s->dest[0] + (i & 1)*8 + (i&2)*4*s->linesize, s->linesize);
966
+    if(cbp & U_CBP_MASK)
967
+        s->dsp.add_pixels_clamped(s->block[4], s->dest[1], s->uvlinesize);
968
+    if(cbp & V_CBP_MASK)
969
+        s->dsp.add_pixels_clamped(s->block[5], s->dest[2], s->uvlinesize);
970
+}
971
+
972
+static int rv34_decode_macroblock(RV34DecContext *r, int8_t *intra_types)
973
+{
974
+    MpegEncContext *s = &r->s;
975
+    GetBitContext *gb = &s->gb;
976
+    int cbp, cbp2;
977
+    int i, blknum, blkoff;
978
+    DCTELEM block16[64];
979
+    int luma_dc_quant;
980
+    int dist;
981
+    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
982
+
983
+    // Calculate which neighbours are available. Maybe it's worth optimizing too.
984
+    memset(r->avail_cache, 0, sizeof(r->avail_cache));
985
+    fill_rectangle(r->avail_cache + 5, 2, 2, 4, 1, 4);
986
+    dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
987
+    if(s->mb_x && dist)
988
+        r->avail_cache[4] =
989
+        r->avail_cache[8] = s->current_picture_ptr->mb_type[mb_pos - 1];
990
+    if(dist >= s->mb_width)
991
+        r->avail_cache[1] =
992
+        r->avail_cache[2] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride];
993
+    if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
994
+        r->avail_cache[3] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride + 1];
995
+    if(s->mb_x && dist > s->mb_width)
996
+        r->avail_cache[0] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride - 1];
997
+
998
+    s->qscale = r->si.quant;
999
+    cbp = cbp2 = rv34_decode_mb_header(r, intra_types);
1000
+
1001
+    if(cbp == -1)
1002
+        return -1;
1003
+
1004
+    luma_dc_quant = r->si.type ? r->luma_dc_quant_p[s->qscale] : r->luma_dc_quant_i[s->qscale];
1005
+    if(r->is16){
1006
+        memset(block16, 0, sizeof(block16));
1007
+        rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0);
1008
+        rv34_dequant4x4_16x16(block16, rv34_qscale_tab[luma_dc_quant],rv34_qscale_tab[s->qscale]);
1009
+        rv34_inv_transform_noround(block16);
1010
+    }
1011
+
1012
+    for(i = 0; i < 16; i++, cbp >>= 1){
1013
+        if(!r->is16 && !(cbp & 1)) continue;
1014
+        blknum = ((i & 2) >> 1) + ((i & 8) >> 2);
1015
+        blkoff = ((i & 1) << 2) + ((i & 4) << 3);
1016
+        if(cbp & 1)
1017
+            rv34_decode_block(s->block[blknum] + blkoff, gb, r->cur_vlcs, r->luma_vlc, 0);
1018
+        rv34_dequant4x4(s->block[blknum] + blkoff, rv34_qscale_tab[luma_dc_quant],rv34_qscale_tab[s->qscale]);
1019
+        if(r->is16) //FIXME: optimize
1020
+            s->block[blknum][blkoff] = block16[(i & 3) | ((i & 0xC) << 1)];
1021
+        rv34_inv_transform(s->block[blknum] + blkoff);
1022
+    }
1023
+    if(r->block_type == RV34_MB_P_MIX16x16)
1024
+        r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
1025
+    for(; i < 24; i++, cbp >>= 1){
1026
+        if(!(cbp & 1)) continue;
1027
+        blknum = ((i & 4) >> 2) + 4;
1028
+        blkoff = ((i & 1) << 2) + ((i & 2) << 4);
1029
+        rv34_decode_block(s->block[blknum] + blkoff, gb, r->cur_vlcs, r->chroma_vlc, 1);
1030
+        rv34_dequant4x4(s->block[blknum] + blkoff, rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]],rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]]);
1031
+        rv34_inv_transform(s->block[blknum] + blkoff);
1032
+    }
1033
+    if(IS_INTRA(s->current_picture_ptr->mb_type[s->mb_x + s->mb_y*s->mb_stride]))
1034
+        rv34_output_macroblock(r, intra_types, cbp2, r->is16);
1035
+    else
1036
+        rv34_apply_differences(r, cbp2);
1037
+
1038
+    return 0;
1039
+}
1040
+
1041
+static int check_slice_end(RV34DecContext *r, MpegEncContext *s)
1042
+{
1043
+    int bits;
1044
+    if(s->mb_y >= s->mb_height)
1045
+        return 1;
1046
+    if(!s->mb_num_left)
1047
+        return 1;
1048
+    if(r->s.mb_skip_run > 1)
1049
+        return 0;
1050
+    bits = r->bits - get_bits_count(&s->gb);
1051
+    if(bits < 0 || (bits < 8 && !show_bits(&s->gb, bits)))
1052
+        return 1;
1053
+    return 0;
1054
+}
1055
+
1056
+static inline int slice_compare(SliceInfo *si1, SliceInfo *si2)
1057
+{
1058
+    return si1->type   != si2->type  ||
1059
+           si1->start  >= si2->start ||
1060
+           si1->width  != si2->width ||
1061
+           si1->height != si2->height;
1062
+}
1063
+
1064
+static int rv34_decode_slice(RV34DecContext *r, int end, uint8_t* buf, int buf_size)
1065
+{
1066
+    MpegEncContext *s = &r->s;
1067
+    GetBitContext *gb = &s->gb;
1068
+    int mb_pos;
1069
+    int res;
1070
+
1071
+    init_get_bits(&r->s.gb, buf, buf_size*8);
1072
+    res = r->parse_slice_header(r, gb, &r->si);
1073
+    if(res < 0){
1074
+        av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
1075
+        return -1;
1076
+    }
1077
+
1078
+    if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) {
1079
+        if(s->width != r->si.width || s->height != r->si.height){
1080
+            av_log(s->avctx, AV_LOG_DEBUG, "Changing dimensions to %dx%d\n", r->si.width,r->si.height);
1081
+            MPV_common_end(s);
1082
+            s->width  = r->si.width;
1083
+            s->height = r->si.height;
1084
+            if(MPV_common_init(s) < 0)
1085
+                return -1;
1086
+            r->intra_types_hist = av_realloc(r->intra_types_hist, s->b4_stride * 4 * 2 * sizeof(*r->intra_types_hist));
1087
+            r->intra_types = r->intra_types_hist + s->b4_stride * 4;
1088
+            r->mb_type = av_realloc(r->mb_type, r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
1089
+        }
1090
+        s->pict_type = r->si.type ? r->si.type : I_TYPE;
1091
+        if(MPV_frame_start(s, s->avctx) < 0)
1092
+            return -1;
1093
+        ff_er_frame_start(s);
1094
+        s->current_picture_ptr = &s->current_picture;
1095
+        s->mb_x = s->mb_y = 0;
1096
+    }
1097
+
1098
+    r->si.end = end;
1099
+    s->qscale = r->si.quant;
1100
+    r->bits = buf_size*8;
1101
+    s->mb_num_left = r->si.end - r->si.start;
1102
+    r->s.mb_skip_run = 0;
1103
+
1104
+    mb_pos = s->mb_x + s->mb_y * s->mb_width;
1105
+    if(r->si.start != mb_pos){
1106
+        av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
1107
+        s->mb_x = r->si.start % s->mb_width;
1108
+        s->mb_y = r->si.start / s->mb_width;
1109
+    }
1110
+    memset(r->intra_types_hist, -1, s->b4_stride * 4 * 2 * sizeof(*r->intra_types_hist));
1111
+    s->first_slice_line = 1;
1112
+    s->resync_mb_x= s->mb_x;
1113
+    s->resync_mb_y= s->mb_y;
1114
+
1115
+    ff_init_block_index(s);
1116
+    while(!check_slice_end(r, s)) {
1117
+        ff_update_block_index(s);
1118
+        s->dsp.clear_blocks(s->block[0]);
1119
+
1120
+        if(rv34_decode_macroblock(r, r->intra_types + s->mb_x * 4 + 1) < 0){
1121
+            ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
1122
+            return -1;
1123
+        }
1124
+        if (++s->mb_x == s->mb_width) {
1125
+            s->mb_x = 0;
1126
+            s->mb_y++;
1127
+            ff_init_block_index(s);
1128
+
1129
+            memmove(r->intra_types_hist, r->intra_types, s->b4_stride * 4 * sizeof(*r->intra_types_hist));
1130
+            memset(r->intra_types, -1, s->b4_stride * 4 * sizeof(*r->intra_types_hist));
1131
+        }
1132
+        if(s->mb_x == s->resync_mb_x)
1133
+            s->first_slice_line=0;
1134
+        s->mb_num_left--;
1135
+    }
1136
+    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
1137
+
1138
+    return (s->mb_y == s->mb_height);
1139
+}
1140
+
1141
+/** @} */ // recons group end
1142
+
1143
+/**
1144
+ * Initialize decoder.
1145
+ */
1146
+int ff_rv34_decode_init(AVCodecContext *avctx)
1147
+{
1148
+    RV34DecContext *r = avctx->priv_data;
1149
+    MpegEncContext *s = &r->s;
1150
+
1151
+    MPV_decode_defaults(s);
1152
+    s->avctx= avctx;
1153
+    s->out_format = FMT_H263;
1154
+    s->codec_id= avctx->codec_id;
1155
+
1156
+    s->width = avctx->width;
1157
+    s->height = avctx->height;
1158
+
1159
+    r->s.avctx = avctx;
1160
+    avctx->flags |= CODEC_FLAG_EMU_EDGE;
1161
+    r->s.flags |= CODEC_FLAG_EMU_EDGE;
1162
+    avctx->pix_fmt = PIX_FMT_YUV420P;
1163
+    avctx->has_b_frames = 1;
1164
+    s->low_delay = 0;
1165
+
1166
+    if (MPV_common_init(s) < 0)
1167
+        return -1;
1168
+
1169
+    ff_h264_pred_init(&r->h, CODEC_ID_RV40);
1170
+
1171
+    r->intra_types_hist = av_malloc(s->b4_stride * 4 * 2 * sizeof(*r->intra_types_hist));
1172
+    r->intra_types = r->intra_types_hist + s->b4_stride * 4;
1173
+
1174
+    r->mb_type = av_mallocz(r->s.mb_stride * r->s.mb_height * sizeof(*r->mb_type));
1175
+
1176
+    if(!intra_vlcs[0].cbppattern[0].bits)
1177
+        rv34_init_tables();
1178
+
1179
+    return 0;
1180
+}
1181
+
1182
+static int get_slice_offset(AVCodecContext *avctx, uint8_t *buf, int n)
1183
+{
1184
+    if(avctx->slice_count) return avctx->slice_offset[n];
1185
+    else                   return AV_RL32(buf + n*8 - 4) == 1 ? AV_RL32(buf + n*8) :  AV_RB32(buf + n*8);
1186
+}
1187
+
1188
+int ff_rv34_decode_frame(AVCodecContext *avctx,
1189
+                            void *data, int *data_size,
1190
+                            uint8_t *buf, int buf_size)
1191
+{
1192
+    RV34DecContext *r = avctx->priv_data;
1193
+    MpegEncContext *s = &r->s;
1194
+    AVFrame *pict = data;
1195
+    SliceInfo si;
1196
+    int i;
1197
+    int slice_count;
1198
+    uint8_t *slices_hdr = NULL;
1199
+    int last = 0;
1200
+
1201
+    /* no supplementary picture */
1202
+    if (buf_size == 0) {
1203
+        /* special case for last picture */
1204
+        if (s->low_delay==0 && s->next_picture_ptr) {
1205
+            *pict= *(AVFrame*)s->next_picture_ptr;
1206
+            s->next_picture_ptr= NULL;
1207
+
1208
+            *data_size = sizeof(AVFrame);
1209
+        }
1210
+        return 0;
1211
+    }
1212
+
1213
+    if(!avctx->slice_count){
1214
+        slice_count = (*buf++) + 1;
1215
+        slices_hdr = buf + 4;
1216
+        buf += 8 * slice_count;
1217
+    }else
1218
+        slice_count = avctx->slice_count;
1219
+
1220
+    for(i=0; i<slice_count; i++){
1221
+        int offset= get_slice_offset(avctx, slices_hdr, i);
1222
+        int size;
1223
+        if(i+1 == slice_count)
1224
+            size= buf_size - offset;
1225
+        else
1226
+            size= get_slice_offset(avctx, slices_hdr, i+1) - offset;
1227
+
1228
+        r->si.end = s->mb_width * s->mb_height;
1229
+        if(i+1 < slice_count){
1230
+            init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, i+1), (buf_size-get_slice_offset(avctx, slices_hdr, i+1))*8);
1231
+            if(r->parse_slice_header(r, &r->s.gb, &si) < 0){
1232
+                if(i+2 < slice_count)
1233
+                    size = get_slice_offset(avctx, slices_hdr, i+2) - offset;
1234
+                else
1235
+                    size = buf_size - offset;
1236
+            }else
1237
+                r->si.end = si.start;
1238
+        }
1239
+        last = rv34_decode_slice(r, r->si.end, buf + offset, size);
1240
+        s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;
1241
+        if(last)
1242
+            break;
1243
+    }
1244
+
1245
+    if(last){
1246
+        if(r->loop_filter)
1247
+            r->loop_filter(r);
1248
+        ff_er_frame_end(s);
1249
+        MPV_frame_end(s);
1250
+        if (s->pict_type == B_TYPE || s->low_delay) {
1251
+            *pict= *(AVFrame*)s->current_picture_ptr;
1252
+        } else if (s->last_picture_ptr != NULL) {
1253
+            *pict= *(AVFrame*)s->last_picture_ptr;
1254
+        }
1255
+
1256
+        if(s->last_picture_ptr || s->low_delay){
1257
+            *data_size = sizeof(AVFrame);
1258
+            ff_print_debug_info(s, pict);
1259
+        }
1260
+        s->current_picture_ptr= NULL; //so we can detect if frame_end wasnt called (find some nicer solution...)
1261
+    }
1262
+    return buf_size;
1263
+}
1264
+
1265
+int ff_rv34_decode_end(AVCodecContext *avctx)
1266
+{
1267
+    RV34DecContext *r = avctx->priv_data;
1268
+
1269
+    MPV_common_end(&r->s);
1270
+
1271
+    av_freep(&r->intra_types_hist);
1272
+    r->intra_types = NULL;
1273
+    av_freep(&r->mb_type);
1274
+
1275
+    return 0;
1276
+}
0 1277
new file mode 100644
... ...
@@ -0,0 +1,119 @@
0
+/*
1
+ * RV30/40 decoder common data declarations
2
+ * Copyright (c) 2007 Mike Melanson, Konstantin Shishkov
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file rv34.h
23
+ * RV30 and RV40 decoder common data declarations
24
+ */
25
+
26
+#ifndef FFMPEG_RV34_H
27
+#define FFMPEG_RV34_H
28
+
29
+#include "avcodec.h"
30
+#include "dsputil.h"
31
+#include "mpegvideo.h"
32
+
33
+#include "h264pred.h"
34
+
35
+/**
36
+ * RV30 and RV40 Macroblock types
37
+ */
38
+enum RV40BlockTypes{
39
+    RV34_MB_TYPE_INTRA,      ///< Intra macroblock
40
+    RV34_MB_TYPE_INTRA16x16, ///< Intra macroblock with DCs in a separate 4x4 block
41
+    RV34_MB_P_16x16,         ///< P-frame macroblock, one motion frame
42
+    RV34_MB_P_8x8,           ///< P-frame macroblock, 8x8 motion compensation partitions
43
+    RV34_MB_B_FORWARD,       ///< B-frame macroblock, forward prediction
44
+    RV34_MB_B_BACKWARD,      ///< B-frame macroblock, backward prediction
45
+    RV34_MB_SKIP,            ///< Skipped block
46
+    RV34_MB_B_DIRECT,        ///< Bidirectionally predicted B-frame macroblock, no motion vectors
47
+    RV34_MB_P_16x8,          ///< P-frame macroblock, 16x8 motion compensation partitions
48
+    RV34_MB_P_8x16,          ///< P-frame macroblock, 8x16 motion compensation partitions
49
+    RV34_MB_B_BIDIR,         ///< Bidirectionally predicted B-frame macroblock, two motion vectors
50
+    RV34_MB_P_MIX16x16,      ///< P-frame macroblock with DCs in a separate 4x4 block, one motion vector
51
+    RV34_MB_TYPES
52
+};
53
+
54
+/**
55
+ * VLC tables used by the decoder
56
+ *
57
+ * Intra frame VLC sets do not contain some of those tables.
58
+ */
59
+typedef struct RV34VLC{
60
+    VLC cbppattern[2];     ///< VLCs used for pattern of coded block patterns decoding
61
+    VLC cbp[2][4];         ///< VLCs used for coded block patterns decoding
62
+    VLC first_pattern[4];  ///< VLCs used for decoding coefficients in the first subblock
63
+    VLC second_pattern[2]; ///< VLCs used for decoding coefficients in the subblocks 2 and 3
64
+    VLC third_pattern[2];  ///< VLCs used for decoding coefficients in the last subblock
65
+    VLC coefficient;       ///< VLCs used for decoding big coefficients
66
+}RV34VLC;
67
+
68
+/** essential slice information */
69
+typedef struct SliceInfo{
70
+    int type;              ///< slice type (intra, inter)
71
+    int quant;             ///< quantizer used for this slice
72
+    int vlc_set;           ///< VLCs used for this slice
73
+    int start, end;        ///< start and end macroblocks of the slice
74
+    int width;             ///< coded width
75
+    int height;            ///< coded height
76
+}SliceInfo;
77
+
78
+/** decoder context */
79
+typedef struct RV34DecContext{
80
+    MpegEncContext s;
81
+    int8_t *intra_types_hist;///< old block types, used for prediction
82
+    int8_t *intra_types;     ///< block types
83
+    uint8_t *luma_dc_quant_i;///< luma subblock DC quantizer for intraframes
84
+    uint8_t *luma_dc_quant_p;///< luma subblock DC quantizer for interframes
85
+
86
+    RV34VLC *cur_vlcs;       ///< VLC set used for current frame decoding
87
+    int bits;                ///< slice size in bits
88
+    H264PredContext h;       ///< functions for 4x4 and 16x16 intra block prediction
89
+    SliceInfo si;            ///< current slice information
90
+
91
+    int *mb_type;            ///< internal macroblock types
92
+    int block_type;          ///< current block type
93
+    int luma_vlc;            ///< which VLC set will be used for decoding of luma blocks
94
+    int chroma_vlc;          ///< which VLC set will be used for decoding of chroma blocks
95
+    int is16;                ///< current block has additional 16x16 specific features or not
96
+    int dmv[4][2];           ///< differential motion vectors for the current macroblock
97
+
98
+    int rv30;                ///< indicates which RV variasnt is currently decoded
99
+    int rpr;                 ///< one field size in RV30 slice header
100
+
101
+    /** 8x8 block available flags (for MV prediction) */
102
+    DECLARE_ALIGNED_8(uint32_t, avail_cache[3*4]);
103
+
104
+    int (*parse_slice_header)(struct RV34DecContext *r, GetBitContext *gb, SliceInfo *si);
105
+    int (*decode_mb_info)(struct RV34DecContext *r);
106
+    int (*decode_intra_types)(struct RV34DecContext *r, GetBitContext *gb, int8_t *dst);
107
+    void (*loop_filter)(struct RV34DecContext *r);
108
+}RV34DecContext;
109
+
110
+/**
111
+ * common decoding functions
112
+ */
113
+int ff_rv34_get_start_offset(GetBitContext *gb, int blocks);
114
+int ff_rv34_decode_init(AVCodecContext *avctx);
115
+int ff_rv34_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size);
116
+int ff_rv34_decode_end(AVCodecContext *avctx);
117
+
118
+#endif /* FFMPEG_RV34_H */