]> git.sesse.net Git - ffmpeg/blob - libavcodec/rv34.c
Merge commit '5d8bea3bb2357bb304f8f771a4107039037c5549'
[ffmpeg] / libavcodec / rv34.c
1 /*
2  * RV30/40 decoder common data
3  * Copyright (c) 2007 Mike Melanson, Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * RV30/40 decoder common data
25  */
26
27 #include "libavutil/imgutils.h"
28 #include "libavutil/internal.h"
29
30 #include "avcodec.h"
31 #include "error_resilience.h"
32 #include "mpegutils.h"
33 #include "mpegvideo.h"
34 #include "golomb.h"
35 #include "internal.h"
36 #include "mathops.h"
37 #include "mpeg_er.h"
38 #include "qpeldsp.h"
39 #include "rectangle.h"
40 #include "thread.h"
41
42 #include "rv34vlc.h"
43 #include "rv34data.h"
44 #include "rv34.h"
45
46 static inline void ZERO8x2(void* dst, int stride)
47 {
48     fill_rectangle(dst,                 1, 2, stride, 0, 4);
49     fill_rectangle(((uint8_t*)(dst))+4, 1, 2, stride, 0, 4);
50 }
51
52 /** translation of RV30/40 macroblock types to lavc ones */
53 static const int rv34_mb_type_to_lavc[12] = {
54     MB_TYPE_INTRA,
55     MB_TYPE_INTRA16x16              | MB_TYPE_SEPARATE_DC,
56     MB_TYPE_16x16   | MB_TYPE_L0,
57     MB_TYPE_8x8     | MB_TYPE_L0,
58     MB_TYPE_16x16   | MB_TYPE_L0,
59     MB_TYPE_16x16   | MB_TYPE_L1,
60     MB_TYPE_SKIP,
61     MB_TYPE_DIRECT2 | MB_TYPE_16x16,
62     MB_TYPE_16x8    | MB_TYPE_L0,
63     MB_TYPE_8x16    | MB_TYPE_L0,
64     MB_TYPE_16x16   | MB_TYPE_L0L1,
65     MB_TYPE_16x16   | MB_TYPE_L0    | MB_TYPE_SEPARATE_DC
66 };
67
68
69 static RV34VLC intra_vlcs[NUM_INTRA_TABLES], inter_vlcs[NUM_INTER_TABLES];
70
71 static int rv34_decode_mv(RV34DecContext *r, int block_type);
72
73 /**
74  * @name RV30/40 VLC generating functions
75  * @{
76  */
77
78 static const int table_offs[] = {
79       0,   1818,   3622,   4144,   4698,   5234,   5804,   5868,   5900,   5932,
80    5996,   6252,   6316,   6348,   6380,   7674,   8944,  10274,  11668,  12250,
81   14060,  15846,  16372,  16962,  17512,  18148,  18180,  18212,  18244,  18308,
82   18564,  18628,  18660,  18692,  20036,  21314,  22648,  23968,  24614,  26384,
83   28190,  28736,  29366,  29938,  30608,  30640,  30672,  30704,  30768,  31024,
84   31088,  31120,  31184,  32570,  33898,  35236,  36644,  37286,  39020,  40802,
85   41368,  42052,  42692,  43348,  43380,  43412,  43444,  43476,  43604,  43668,
86   43700,  43732,  45100,  46430,  47778,  49160,  49802,  51550,  53340,  53972,
87   54648,  55348,  55994,  56122,  56154,  56186,  56218,  56346,  56410,  56442,
88   56474,  57878,  59290,  60636,  62036,  62682,  64460,  64524,  64588,  64716,
89   64844,  66076,  67466,  67978,  68542,  69064,  69648,  70296,  72010,  72074,
90   72138,  72202,  72330,  73572,  74936,  75454,  76030,  76566,  77176,  77822,
91   79582,  79646,  79678,  79742,  79870,  81180,  82536,  83064,  83672,  84242,
92   84934,  85576,  87384,  87448,  87480,  87544,  87672,  88982,  90340,  90902,
93   91598,  92182,  92846,  93488,  95246,  95278,  95310,  95374,  95502,  96878,
94   98266,  98848,  99542, 100234, 100884, 101524, 103320, 103352, 103384, 103416,
95  103480, 104874, 106222, 106910, 107584, 108258, 108902, 109544, 111366, 111398,
96  111430, 111462, 111494, 112878, 114320, 114988, 115660, 116310, 116950, 117592
97 };
98
99 static VLC_TYPE table_data[117592][2];
100
101 /**
102  * Generate VLC from codeword lengths.
103  * @param bits   codeword lengths (zeroes are accepted)
104  * @param size   length of input data
105  * @param vlc    output VLC
106  * @param insyms symbols for input codes (NULL for default ones)
107  * @param num    VLC table number (for static initialization)
108  */
109 static void rv34_gen_vlc(const uint8_t *bits, int size, VLC *vlc, const uint8_t *insyms,
110                          const int num)
111 {
112     int i;
113     int counts[17] = {0}, codes[17];
114     uint16_t cw[MAX_VLC_SIZE], syms[MAX_VLC_SIZE];
115     uint8_t bits2[MAX_VLC_SIZE];
116     int maxbits = 0, realsize = 0;
117
118     for(i = 0; i < size; i++){
119         if(bits[i]){
120             bits2[realsize] = bits[i];
121             syms[realsize] = insyms ? insyms[i] : i;
122             realsize++;
123             maxbits = FFMAX(maxbits, bits[i]);
124             counts[bits[i]]++;
125         }
126     }
127
128     codes[0] = 0;
129     for(i = 0; i < 16; i++)
130         codes[i+1] = (codes[i] + counts[i]) << 1;
131     for(i = 0; i < realsize; i++)
132         cw[i] = codes[bits2[i]]++;
133
134     vlc->table = &table_data[table_offs[num]];
135     vlc->table_allocated = table_offs[num + 1] - table_offs[num];
136     ff_init_vlc_sparse(vlc, FFMIN(maxbits, 9), realsize,
137                        bits2, 1, 1,
138                        cw,    2, 2,
139                        syms,  2, 2, INIT_VLC_USE_NEW_STATIC);
140 }
141
142 /**
143  * Initialize all tables.
144  */
145 static av_cold void rv34_init_tables(void)
146 {
147     int i, j, k;
148
149     for(i = 0; i < NUM_INTRA_TABLES; i++){
150         for(j = 0; j < 2; j++){
151             rv34_gen_vlc(rv34_table_intra_cbppat   [i][j], CBPPAT_VLC_SIZE,   &intra_vlcs[i].cbppattern[j],     NULL, 19*i + 0 + j);
152             rv34_gen_vlc(rv34_table_intra_secondpat[i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].second_pattern[j], NULL, 19*i + 2 + j);
153             rv34_gen_vlc(rv34_table_intra_thirdpat [i][j], OTHERBLK_VLC_SIZE, &intra_vlcs[i].third_pattern[j],  NULL, 19*i + 4 + j);
154             for(k = 0; k < 4; k++){
155                 rv34_gen_vlc(rv34_table_intra_cbp[i][j+k*2],  CBP_VLC_SIZE,   &intra_vlcs[i].cbp[j][k],         rv34_cbp_code, 19*i + 6 + j*4 + k);
156             }
157         }
158         for(j = 0; j < 4; j++){
159             rv34_gen_vlc(rv34_table_intra_firstpat[i][j], FIRSTBLK_VLC_SIZE, &intra_vlcs[i].first_pattern[j], NULL, 19*i + 14 + j);
160         }
161         rv34_gen_vlc(rv34_intra_coeff[i], COEFF_VLC_SIZE, &intra_vlcs[i].coefficient, NULL, 19*i + 18);
162     }
163
164     for(i = 0; i < NUM_INTER_TABLES; i++){
165         rv34_gen_vlc(rv34_inter_cbppat[i], CBPPAT_VLC_SIZE, &inter_vlcs[i].cbppattern[0], NULL, i*12 + 95);
166         for(j = 0; j < 4; j++){
167             rv34_gen_vlc(rv34_inter_cbp[i][j], CBP_VLC_SIZE, &inter_vlcs[i].cbp[0][j], rv34_cbp_code, i*12 + 96 + j);
168         }
169         for(j = 0; j < 2; j++){
170             rv34_gen_vlc(rv34_table_inter_firstpat [i][j], FIRSTBLK_VLC_SIZE, &inter_vlcs[i].first_pattern[j],  NULL, i*12 + 100 + j);
171             rv34_gen_vlc(rv34_table_inter_secondpat[i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].second_pattern[j], NULL, i*12 + 102 + j);
172             rv34_gen_vlc(rv34_table_inter_thirdpat [i][j], OTHERBLK_VLC_SIZE, &inter_vlcs[i].third_pattern[j],  NULL, i*12 + 104 + j);
173         }
174         rv34_gen_vlc(rv34_inter_coeff[i], COEFF_VLC_SIZE, &inter_vlcs[i].coefficient, NULL, i*12 + 106);
175     }
176 }
177
178 /** @} */ // vlc group
179
180 /**
181  * @name RV30/40 4x4 block decoding functions
182  * @{
183  */
184
185 /**
186  * Decode coded block pattern.
187  */
188 static int rv34_decode_cbp(GetBitContext *gb, RV34VLC *vlc, int table)
189 {
190     int pattern, code, cbp=0;
191     int ones;
192     static const int cbp_masks[3] = {0x100000, 0x010000, 0x110000};
193     static const int shifts[4] = { 0, 2, 8, 10 };
194     const int *curshift = shifts;
195     int i, t, mask;
196
197     code = get_vlc2(gb, vlc->cbppattern[table].table, 9, 2);
198     pattern = code & 0xF;
199     code >>= 4;
200
201     ones = rv34_count_ones[pattern];
202
203     for(mask = 8; mask; mask >>= 1, curshift++){
204         if(pattern & mask)
205             cbp |= get_vlc2(gb, vlc->cbp[table][ones].table, vlc->cbp[table][ones].bits, 1) << curshift[0];
206     }
207
208     for(i = 0; i < 4; i++){
209         t = (modulo_three_table[code] >> (6 - 2*i)) & 3;
210         if(t == 1)
211             cbp |= cbp_masks[get_bits1(gb)] << i;
212         if(t == 2)
213             cbp |= cbp_masks[2] << i;
214     }
215     return cbp;
216 }
217
218 /**
219  * Get one coefficient value from the bitstream and store it.
220  */
221 static inline void decode_coeff(int16_t *dst, int coef, int esc, GetBitContext *gb, VLC* vlc, int q)
222 {
223     if(coef){
224         if(coef == esc){
225             coef = get_vlc2(gb, vlc->table, 9, 2);
226             if(coef > 23){
227                 coef -= 23;
228                 coef = 22 + ((1 << coef) | get_bits(gb, coef));
229             }
230             coef += esc;
231         }
232         if(get_bits1(gb))
233             coef = -coef;
234         *dst = (coef*q + 8) >> 4;
235     }
236 }
237
238 /**
239  * Decode 2x2 subblock of coefficients.
240  */
241 static inline void decode_subblock(int16_t *dst, int code, const int is_block2, GetBitContext *gb, VLC *vlc, int q)
242 {
243     int flags = modulo_three_table[code];
244
245     decode_coeff(    dst+0*4+0, (flags >> 6)    , 3, gb, vlc, q);
246     if(is_block2){
247         decode_coeff(dst+1*4+0, (flags >> 4) & 3, 2, gb, vlc, q);
248         decode_coeff(dst+0*4+1, (flags >> 2) & 3, 2, gb, vlc, q);
249     }else{
250         decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q);
251         decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q);
252     }
253     decode_coeff(    dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q);
254 }
255
256 /**
257  * Decode a single coefficient.
258  */
259 static inline void decode_subblock1(int16_t *dst, int code, GetBitContext *gb, VLC *vlc, int q)
260 {
261     int coeff = modulo_three_table[code] >> 6;
262     decode_coeff(dst, coeff, 3, gb, vlc, q);
263 }
264
265 static inline void decode_subblock3(int16_t *dst, int code, GetBitContext *gb, VLC *vlc,
266                                     int q_dc, int q_ac1, int q_ac2)
267 {
268     int flags = modulo_three_table[code];
269
270     decode_coeff(dst+0*4+0, (flags >> 6)    , 3, gb, vlc, q_dc);
271     decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q_ac1);
272     decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q_ac1);
273     decode_coeff(dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q_ac2);
274 }
275
276 /**
277  * Decode coefficients for 4x4 block.
278  *
279  * This is done by filling 2x2 subblocks with decoded coefficients
280  * in this order (the same for subblocks and subblock coefficients):
281  *  o--o
282  *    /
283  *   /
284  *  o--o
285  */
286
287 static int rv34_decode_block(int16_t *dst, GetBitContext *gb, RV34VLC *rvlc, int fc, int sc, int q_dc, int q_ac1, int q_ac2)
288 {
289     int code, pattern, has_ac = 1;
290
291     code = get_vlc2(gb, rvlc->first_pattern[fc].table, 9, 2);
292
293     pattern = code & 0x7;
294
295     code >>= 3;
296
297     if (modulo_three_table[code] & 0x3F) {
298         decode_subblock3(dst, code, gb, &rvlc->coefficient, q_dc, q_ac1, q_ac2);
299     } else {
300         decode_subblock1(dst, code, gb, &rvlc->coefficient, q_dc);
301         if (!pattern)
302             return 0;
303         has_ac = 0;
304     }
305
306     if(pattern & 4){
307         code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
308         decode_subblock(dst + 4*0+2, code, 0, gb, &rvlc->coefficient, q_ac2);
309     }
310     if(pattern & 2){ // Looks like coefficients 1 and 2 are swapped for this block
311         code = get_vlc2(gb, rvlc->second_pattern[sc].table, 9, 2);
312         decode_subblock(dst + 4*2+0, code, 1, gb, &rvlc->coefficient, q_ac2);
313     }
314     if(pattern & 1){
315         code = get_vlc2(gb, rvlc->third_pattern[sc].table, 9, 2);
316         decode_subblock(dst + 4*2+2, code, 0, gb, &rvlc->coefficient, q_ac2);
317     }
318     return has_ac | pattern;
319 }
320
321 /**
322  * @name RV30/40 bitstream parsing
323  * @{
324  */
325
326 /**
327  * Decode starting slice position.
328  * @todo Maybe replace with ff_h263_decode_mba() ?
329  */
330 int ff_rv34_get_start_offset(GetBitContext *gb, int mb_size)
331 {
332     int i;
333     for(i = 0; i < 5; i++)
334         if(rv34_mb_max_sizes[i] >= mb_size - 1)
335             break;
336     return rv34_mb_bits_sizes[i];
337 }
338
339 /**
340  * Select VLC set for decoding from current quantizer, modifier and frame type.
341  */
342 static inline RV34VLC* choose_vlc_set(int quant, int mod, int type)
343 {
344     if(mod == 2 && quant < 19) quant += 10;
345     else if(mod && quant < 26) quant += 5;
346     return type ? &inter_vlcs[rv34_quant_to_vlc_set[1][av_clip(quant, 0, 30)]]
347                 : &intra_vlcs[rv34_quant_to_vlc_set[0][av_clip(quant, 0, 30)]];
348 }
349
350 /**
351  * Decode intra macroblock header and return CBP in case of success, -1 otherwise.
352  */
353 static int rv34_decode_intra_mb_header(RV34DecContext *r, int8_t *intra_types)
354 {
355     MpegEncContext *s = &r->s;
356     GetBitContext *gb = &s->gb;
357     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
358     int t;
359
360     r->is16 = get_bits1(gb);
361     if(r->is16){
362         s->current_picture_ptr->mb_type[mb_pos] = MB_TYPE_INTRA16x16;
363         r->block_type = RV34_MB_TYPE_INTRA16x16;
364         t = get_bits(gb, 2);
365         fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
366         r->luma_vlc   = 2;
367     }else{
368         if(!r->rv30){
369             if(!get_bits1(gb))
370                 av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
371         }
372         s->current_picture_ptr->mb_type[mb_pos] = MB_TYPE_INTRA;
373         r->block_type = RV34_MB_TYPE_INTRA;
374         if(r->decode_intra_types(r, gb, intra_types) < 0)
375             return -1;
376         r->luma_vlc   = 1;
377     }
378
379     r->chroma_vlc = 0;
380     r->cur_vlcs   = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
381
382     return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
383 }
384
385 /**
386  * Decode inter macroblock header and return CBP in case of success, -1 otherwise.
387  */
388 static int rv34_decode_inter_mb_header(RV34DecContext *r, int8_t *intra_types)
389 {
390     MpegEncContext *s = &r->s;
391     GetBitContext *gb = &s->gb;
392     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
393     int i, t;
394
395     r->block_type = r->decode_mb_info(r);
396     if(r->block_type == -1)
397         return -1;
398     s->current_picture_ptr->mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
399     r->mb_type[mb_pos] = r->block_type;
400     if(r->block_type == RV34_MB_SKIP){
401         if(s->pict_type == AV_PICTURE_TYPE_P)
402             r->mb_type[mb_pos] = RV34_MB_P_16x16;
403         if(s->pict_type == AV_PICTURE_TYPE_B)
404             r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
405     }
406     r->is16 = !!IS_INTRA16x16(s->current_picture_ptr->mb_type[mb_pos]);
407     rv34_decode_mv(r, r->block_type);
408     if(r->block_type == RV34_MB_SKIP){
409         fill_rectangle(intra_types, 4, 4, r->intra_types_stride, 0, sizeof(intra_types[0]));
410         return 0;
411     }
412     r->chroma_vlc = 1;
413     r->luma_vlc   = 0;
414
415     if(IS_INTRA(s->current_picture_ptr->mb_type[mb_pos])){
416         if(r->is16){
417             t = get_bits(gb, 2);
418             fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
419             r->luma_vlc   = 2;
420         }else{
421             if(r->decode_intra_types(r, gb, intra_types) < 0)
422                 return -1;
423             r->luma_vlc   = 1;
424         }
425         r->chroma_vlc = 0;
426         r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
427     }else{
428         for(i = 0; i < 16; i++)
429             intra_types[(i & 3) + (i>>2) * r->intra_types_stride] = 0;
430         r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
431         if(r->mb_type[mb_pos] == RV34_MB_P_MIX16x16){
432             r->is16 = 1;
433             r->chroma_vlc = 1;
434             r->luma_vlc   = 2;
435             r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
436         }
437     }
438
439     return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
440 }
441
442 /** @} */ //bitstream functions
443
444 /**
445  * @name motion vector related code (prediction, reconstruction, motion compensation)
446  * @{
447  */
448
449 /** macroblock partition width in 8x8 blocks */
450 static const uint8_t part_sizes_w[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2 };
451
452 /** macroblock partition height in 8x8 blocks */
453 static const uint8_t part_sizes_h[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2 };
454
455 /** availability index for subblocks */
456 static const uint8_t avail_indexes[4] = { 6, 7, 10, 11 };
457
458 /**
459  * motion vector prediction
460  *
461  * Motion prediction performed for the block by using median prediction of
462  * motion vectors from the left, top and right top blocks but in corner cases
463  * some other vectors may be used instead.
464  */
465 static void rv34_pred_mv(RV34DecContext *r, int block_type, int subblock_no, int dmv_no)
466 {
467     MpegEncContext *s = &r->s;
468     int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
469     int A[2] = {0}, B[2], C[2];
470     int i, j;
471     int mx, my;
472     int* avail = r->avail_cache + avail_indexes[subblock_no];
473     int c_off = part_sizes_w[block_type];
474
475     mv_pos += (subblock_no & 1) + (subblock_no >> 1)*s->b8_stride;
476     if(subblock_no == 3)
477         c_off = -1;
478
479     if(avail[-1]){
480         A[0] = s->current_picture_ptr->motion_val[0][mv_pos-1][0];
481         A[1] = s->current_picture_ptr->motion_val[0][mv_pos-1][1];
482     }
483     if(avail[-4]){
484         B[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][0];
485         B[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride][1];
486     }else{
487         B[0] = A[0];
488         B[1] = A[1];
489     }
490     if(!avail[c_off-4]){
491         if(avail[-4] && (avail[-1] || r->rv30)){
492             C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][0];
493             C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride-1][1];
494         }else{
495             C[0] = A[0];
496             C[1] = A[1];
497         }
498     }else{
499         C[0] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][0];
500         C[1] = s->current_picture_ptr->motion_val[0][mv_pos-s->b8_stride+c_off][1];
501     }
502     mx = mid_pred(A[0], B[0], C[0]);
503     my = mid_pred(A[1], B[1], C[1]);
504     mx += r->dmv[dmv_no][0];
505     my += r->dmv[dmv_no][1];
506     for(j = 0; j < part_sizes_h[block_type]; j++){
507         for(i = 0; i < part_sizes_w[block_type]; i++){
508             s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][0] = mx;
509             s->current_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][1] = my;
510         }
511     }
512 }
513
514 #define GET_PTS_DIFF(a, b) (((a) - (b) + 8192) & 0x1FFF)
515
516 /**
517  * Calculate motion vector component that should be added for direct blocks.
518  */
519 static int calc_add_mv(RV34DecContext *r, int dir, int val)
520 {
521     int mul = dir ? -r->mv_weight2 : r->mv_weight1;
522
523     return (val * mul + 0x2000) >> 14;
524 }
525
526 /**
527  * Predict motion vector for B-frame macroblock.
528  */
529 static inline void rv34_pred_b_vector(int A[2], int B[2], int C[2],
530                                       int A_avail, int B_avail, int C_avail,
531                                       int *mx, int *my)
532 {
533     if(A_avail + B_avail + C_avail != 3){
534         *mx = A[0] + B[0] + C[0];
535         *my = A[1] + B[1] + C[1];
536         if(A_avail + B_avail + C_avail == 2){
537             *mx /= 2;
538             *my /= 2;
539         }
540     }else{
541         *mx = mid_pred(A[0], B[0], C[0]);
542         *my = mid_pred(A[1], B[1], C[1]);
543     }
544 }
545
546 /**
547  * motion vector prediction for B-frames
548  */
549 static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir)
550 {
551     MpegEncContext *s = &r->s;
552     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
553     int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
554     int A[2] = { 0 }, B[2] = { 0 }, C[2] = { 0 };
555     int has_A = 0, has_B = 0, has_C = 0;
556     int mx, my;
557     int i, j;
558     Picture *cur_pic = s->current_picture_ptr;
559     const int mask = dir ? MB_TYPE_L1 : MB_TYPE_L0;
560     int type = cur_pic->mb_type[mb_pos];
561
562     if((r->avail_cache[6-1] & type) & mask){
563         A[0] = cur_pic->motion_val[dir][mv_pos - 1][0];
564         A[1] = cur_pic->motion_val[dir][mv_pos - 1][1];
565         has_A = 1;
566     }
567     if((r->avail_cache[6-4] & type) & mask){
568         B[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][0];
569         B[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][1];
570         has_B = 1;
571     }
572     if(r->avail_cache[6-4] && (r->avail_cache[6-2] & type) & mask){
573         C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][0];
574         C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][1];
575         has_C = 1;
576     }else if((s->mb_x+1) == s->mb_width && (r->avail_cache[6-5] & type) & mask){
577         C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][0];
578         C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][1];
579         has_C = 1;
580     }
581
582     rv34_pred_b_vector(A, B, C, has_A, has_B, has_C, &mx, &my);
583
584     mx += r->dmv[dir][0];
585     my += r->dmv[dir][1];
586
587     for(j = 0; j < 2; j++){
588         for(i = 0; i < 2; i++){
589             cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
590             cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
591         }
592     }
593     if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD){
594         ZERO8x2(cur_pic->motion_val[!dir][mv_pos], s->b8_stride);
595     }
596 }
597
598 /**
599  * motion vector prediction - RV3 version
600  */
601 static void rv34_pred_mv_rv3(RV34DecContext *r, int block_type, int dir)
602 {
603     MpegEncContext *s = &r->s;
604     int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
605     int A[2] = {0}, B[2], C[2];
606     int i, j, k;
607     int mx, my;
608     int* avail = r->avail_cache + avail_indexes[0];
609
610     if(avail[-1]){
611         A[0] = s->current_picture_ptr->motion_val[0][mv_pos - 1][0];
612         A[1] = s->current_picture_ptr->motion_val[0][mv_pos - 1][1];
613     }
614     if(avail[-4]){
615         B[0] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride][0];
616         B[1] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride][1];
617     }else{
618         B[0] = A[0];
619         B[1] = A[1];
620     }
621     if(!avail[-4 + 2]){
622         if(avail[-4] && (avail[-1])){
623             C[0] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride - 1][0];
624             C[1] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride - 1][1];
625         }else{
626             C[0] = A[0];
627             C[1] = A[1];
628         }
629     }else{
630         C[0] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride + 2][0];
631         C[1] = s->current_picture_ptr->motion_val[0][mv_pos - s->b8_stride + 2][1];
632     }
633     mx = mid_pred(A[0], B[0], C[0]);
634     my = mid_pred(A[1], B[1], C[1]);
635     mx += r->dmv[0][0];
636     my += r->dmv[0][1];
637     for(j = 0; j < 2; j++){
638         for(i = 0; i < 2; i++){
639             for(k = 0; k < 2; k++){
640                 s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
641                 s->current_picture_ptr->motion_val[k][mv_pos + i + j*s->b8_stride][1] = my;
642             }
643         }
644     }
645 }
646
647 static const int chroma_coeffs[3] = { 0, 3, 5 };
648
649 /**
650  * generic motion compensation function
651  *
652  * @param r decoder context
653  * @param block_type type of the current block
654  * @param xoff horizontal offset from the start of the current block
655  * @param yoff vertical offset from the start of the current block
656  * @param mv_off offset to the motion vector information
657  * @param width width of the current partition in 8x8 blocks
658  * @param height height of the current partition in 8x8 blocks
659  * @param dir motion compensation direction (i.e. from the last or the next reference frame)
660  * @param thirdpel motion vectors are specified in 1/3 of pixel
661  * @param qpel_mc a set of functions used to perform luma motion compensation
662  * @param chroma_mc a set of functions used to perform chroma motion compensation
663  */
664 static inline void rv34_mc(RV34DecContext *r, const int block_type,
665                           const int xoff, const int yoff, int mv_off,
666                           const int width, const int height, int dir,
667                           const int thirdpel, int weighted,
668                           qpel_mc_func (*qpel_mc)[16],
669                           h264_chroma_mc_func (*chroma_mc))
670 {
671     MpegEncContext *s = &r->s;
672     uint8_t *Y, *U, *V, *srcY, *srcU, *srcV;
673     int dxy, mx, my, umx, umy, lx, ly, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
674     int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride + mv_off;
675     int is16x16 = 1;
676     int emu = 0;
677
678     if(thirdpel){
679         int chroma_mx, chroma_my;
680         mx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) / 3 - (1 << 24);
681         my = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) / 3 - (1 << 24);
682         lx = (s->current_picture_ptr->motion_val[dir][mv_pos][0] + (3 << 24)) % 3;
683         ly = (s->current_picture_ptr->motion_val[dir][mv_pos][1] + (3 << 24)) % 3;
684         chroma_mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] / 2;
685         chroma_my = s->current_picture_ptr->motion_val[dir][mv_pos][1] / 2;
686         umx = (chroma_mx + (3 << 24)) / 3 - (1 << 24);
687         umy = (chroma_my + (3 << 24)) / 3 - (1 << 24);
688         uvmx = chroma_coeffs[(chroma_mx + (3 << 24)) % 3];
689         uvmy = chroma_coeffs[(chroma_my + (3 << 24)) % 3];
690     }else{
691         int cx, cy;
692         mx = s->current_picture_ptr->motion_val[dir][mv_pos][0] >> 2;
693         my = s->current_picture_ptr->motion_val[dir][mv_pos][1] >> 2;
694         lx = s->current_picture_ptr->motion_val[dir][mv_pos][0] & 3;
695         ly = s->current_picture_ptr->motion_val[dir][mv_pos][1] & 3;
696         cx = s->current_picture_ptr->motion_val[dir][mv_pos][0] / 2;
697         cy = s->current_picture_ptr->motion_val[dir][mv_pos][1] / 2;
698         umx = cx >> 2;
699         umy = cy >> 2;
700         uvmx = (cx & 3) << 1;
701         uvmy = (cy & 3) << 1;
702         //due to some flaw RV40 uses the same MC compensation routine for H2V2 and H3V3
703         if(uvmx == 6 && uvmy == 6)
704             uvmx = uvmy = 4;
705     }
706
707     if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
708         /* wait for the referenced mb row to be finished */
709         int mb_row = s->mb_y + ((yoff + my + 5 + 8 * height) >> 4);
710         ThreadFrame *f = dir ? &s->next_picture_ptr->tf : &s->last_picture_ptr->tf;
711         ff_thread_await_progress(f, mb_row, 0);
712     }
713
714     dxy = ly*4 + lx;
715     srcY = dir ? s->next_picture_ptr->f->data[0] : s->last_picture_ptr->f->data[0];
716     srcU = dir ? s->next_picture_ptr->f->data[1] : s->last_picture_ptr->f->data[1];
717     srcV = dir ? s->next_picture_ptr->f->data[2] : s->last_picture_ptr->f->data[2];
718     src_x = s->mb_x * 16 + xoff + mx;
719     src_y = s->mb_y * 16 + yoff + my;
720     uvsrc_x = s->mb_x * 8 + (xoff >> 1) + umx;
721     uvsrc_y = s->mb_y * 8 + (yoff >> 1) + umy;
722     srcY += src_y * s->linesize + src_x;
723     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
724     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
725     if(s->h_edge_pos - (width << 3) < 6 || s->v_edge_pos - (height << 3) < 6 ||
726        (unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 4 ||
727        (unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 4) {
728         srcY -= 2 + 2*s->linesize;
729         s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcY,
730                                  s->linesize, s->linesize,
731                                  (width << 3) + 6, (height << 3) + 6,
732                                  src_x - 2, src_y - 2,
733                                  s->h_edge_pos, s->v_edge_pos);
734         srcY = s->sc.edge_emu_buffer + 2 + 2*s->linesize;
735         emu = 1;
736     }
737     if(!weighted){
738         Y = s->dest[0] + xoff      + yoff     *s->linesize;
739         U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
740         V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
741     }else{
742         Y = r->tmp_b_block_y [dir]     +  xoff     +  yoff    *s->linesize;
743         U = r->tmp_b_block_uv[dir*2]   + (xoff>>1) + (yoff>>1)*s->uvlinesize;
744         V = r->tmp_b_block_uv[dir*2+1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
745     }
746
747     if(block_type == RV34_MB_P_16x8){
748         qpel_mc[1][dxy](Y, srcY, s->linesize);
749         Y    += 8;
750         srcY += 8;
751     }else if(block_type == RV34_MB_P_8x16){
752         qpel_mc[1][dxy](Y, srcY, s->linesize);
753         Y    += 8 * s->linesize;
754         srcY += 8 * s->linesize;
755     }
756     is16x16 = (block_type != RV34_MB_P_8x8) && (block_type != RV34_MB_P_16x8) && (block_type != RV34_MB_P_8x16);
757     qpel_mc[!is16x16][dxy](Y, srcY, s->linesize);
758     if (emu) {
759         uint8_t *uvbuf = s->sc.edge_emu_buffer;
760
761         s->vdsp.emulated_edge_mc(uvbuf, srcU,
762                                  s->uvlinesize, s->uvlinesize,
763                                  (width << 2) + 1, (height << 2) + 1,
764                                  uvsrc_x, uvsrc_y,
765                                  s->h_edge_pos >> 1, s->v_edge_pos >> 1);
766         srcU = uvbuf;
767         uvbuf += 9*s->uvlinesize;
768
769         s->vdsp.emulated_edge_mc(uvbuf, srcV,
770                                  s->uvlinesize, s->uvlinesize,
771                                  (width << 2) + 1, (height << 2) + 1,
772                                  uvsrc_x, uvsrc_y,
773                                  s->h_edge_pos >> 1, s->v_edge_pos >> 1);
774         srcV = uvbuf;
775     }
776     chroma_mc[2-width]   (U, srcU, s->uvlinesize, height*4, uvmx, uvmy);
777     chroma_mc[2-width]   (V, srcV, s->uvlinesize, height*4, uvmx, uvmy);
778 }
779
780 static void rv34_mc_1mv(RV34DecContext *r, const int block_type,
781                         const int xoff, const int yoff, int mv_off,
782                         const int width, const int height, int dir)
783 {
784     rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30, 0,
785             r->rdsp.put_pixels_tab,
786             r->rdsp.put_chroma_pixels_tab);
787 }
788
789 static void rv4_weight(RV34DecContext *r)
790 {
791     r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][0](r->s.dest[0],
792                                                         r->tmp_b_block_y[0],
793                                                         r->tmp_b_block_y[1],
794                                                         r->weight1,
795                                                         r->weight2,
796                                                         r->s.linesize);
797     r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][1](r->s.dest[1],
798                                                         r->tmp_b_block_uv[0],
799                                                         r->tmp_b_block_uv[2],
800                                                         r->weight1,
801                                                         r->weight2,
802                                                         r->s.uvlinesize);
803     r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][1](r->s.dest[2],
804                                                         r->tmp_b_block_uv[1],
805                                                         r->tmp_b_block_uv[3],
806                                                         r->weight1,
807                                                         r->weight2,
808                                                         r->s.uvlinesize);
809 }
810
811 static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
812 {
813     int weighted = !r->rv30 && block_type != RV34_MB_B_BIDIR && r->weight1 != 8192;
814
815     rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30, weighted,
816             r->rdsp.put_pixels_tab,
817             r->rdsp.put_chroma_pixels_tab);
818     if(!weighted){
819         rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 0,
820                 r->rdsp.avg_pixels_tab,
821                 r->rdsp.avg_chroma_pixels_tab);
822     }else{
823         rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 1,
824                 r->rdsp.put_pixels_tab,
825                 r->rdsp.put_chroma_pixels_tab);
826         rv4_weight(r);
827     }
828 }
829
830 static void rv34_mc_2mv_skip(RV34DecContext *r)
831 {
832     int i, j;
833     int weighted = !r->rv30 && r->weight1 != 8192;
834
835     for(j = 0; j < 2; j++)
836         for(i = 0; i < 2; i++){
837              rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 0, r->rv30,
838                      weighted,
839                      r->rdsp.put_pixels_tab,
840                      r->rdsp.put_chroma_pixels_tab);
841              rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 1, r->rv30,
842                      weighted,
843                      weighted ? r->rdsp.put_pixels_tab : r->rdsp.avg_pixels_tab,
844                      weighted ? r->rdsp.put_chroma_pixels_tab : r->rdsp.avg_chroma_pixels_tab);
845         }
846     if(weighted)
847         rv4_weight(r);
848 }
849
850 /** number of motion vectors in each macroblock type */
851 static const int num_mvs[RV34_MB_TYPES] = { 0, 0, 1, 4, 1, 1, 0, 0, 2, 2, 2, 1 };
852
853 /**
854  * Decode motion vector differences
855  * and perform motion vector reconstruction and motion compensation.
856  */
857 static int rv34_decode_mv(RV34DecContext *r, int block_type)
858 {
859     MpegEncContext *s = &r->s;
860     GetBitContext *gb = &s->gb;
861     int i, j, k, l;
862     int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
863     int next_bt;
864
865     memset(r->dmv, 0, sizeof(r->dmv));
866     for(i = 0; i < num_mvs[block_type]; i++){
867         r->dmv[i][0] = svq3_get_se_golomb(gb);
868         r->dmv[i][1] = svq3_get_se_golomb(gb);
869     }
870     switch(block_type){
871     case RV34_MB_TYPE_INTRA:
872     case RV34_MB_TYPE_INTRA16x16:
873         ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
874         return 0;
875     case RV34_MB_SKIP:
876         if(s->pict_type == AV_PICTURE_TYPE_P){
877             ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
878             rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
879             break;
880         }
881     case RV34_MB_B_DIRECT:
882         //surprisingly, it uses motion scheme from next reference frame
883         /* wait for the current mb row to be finished */
884         if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
885             ff_thread_await_progress(&s->next_picture_ptr->tf, FFMAX(0, s->mb_y-1), 0);
886
887         next_bt = s->next_picture_ptr->mb_type[s->mb_x + s->mb_y * s->mb_stride];
888         if(IS_INTRA(next_bt) || IS_SKIP(next_bt)){
889             ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
890             ZERO8x2(s->current_picture_ptr->motion_val[1][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
891         }else
892             for(j = 0; j < 2; j++)
893                 for(i = 0; i < 2; i++)
894                     for(k = 0; k < 2; k++)
895                         for(l = 0; l < 2; l++)
896                             s->current_picture_ptr->motion_val[l][mv_pos + i + j*s->b8_stride][k] = calc_add_mv(r, l, s->next_picture_ptr->motion_val[0][mv_pos + i + j*s->b8_stride][k]);
897         if(!(IS_16X8(next_bt) || IS_8X16(next_bt) || IS_8X8(next_bt))) //we can use whole macroblock MC
898             rv34_mc_2mv(r, block_type);
899         else
900             rv34_mc_2mv_skip(r);
901         ZERO8x2(s->current_picture_ptr->motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
902         break;
903     case RV34_MB_P_16x16:
904     case RV34_MB_P_MIX16x16:
905         rv34_pred_mv(r, block_type, 0, 0);
906         rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
907         break;
908     case RV34_MB_B_FORWARD:
909     case RV34_MB_B_BACKWARD:
910         r->dmv[1][0] = r->dmv[0][0];
911         r->dmv[1][1] = r->dmv[0][1];
912         if(r->rv30)
913             rv34_pred_mv_rv3(r, block_type, block_type == RV34_MB_B_BACKWARD);
914         else
915             rv34_pred_mv_b  (r, block_type, block_type == RV34_MB_B_BACKWARD);
916         rv34_mc_1mv     (r, block_type, 0, 0, 0, 2, 2, block_type == RV34_MB_B_BACKWARD);
917         break;
918     case RV34_MB_P_16x8:
919     case RV34_MB_P_8x16:
920         rv34_pred_mv(r, block_type, 0, 0);
921         rv34_pred_mv(r, block_type, 1 + (block_type == RV34_MB_P_16x8), 1);
922         if(block_type == RV34_MB_P_16x8){
923             rv34_mc_1mv(r, block_type, 0, 0, 0,            2, 1, 0);
924             rv34_mc_1mv(r, block_type, 0, 8, s->b8_stride, 2, 1, 0);
925         }
926         if(block_type == RV34_MB_P_8x16){
927             rv34_mc_1mv(r, block_type, 0, 0, 0, 1, 2, 0);
928             rv34_mc_1mv(r, block_type, 8, 0, 1, 1, 2, 0);
929         }
930         break;
931     case RV34_MB_B_BIDIR:
932         rv34_pred_mv_b  (r, block_type, 0);
933         rv34_pred_mv_b  (r, block_type, 1);
934         rv34_mc_2mv     (r, block_type);
935         break;
936     case RV34_MB_P_8x8:
937         for(i=0;i< 4;i++){
938             rv34_pred_mv(r, block_type, i, i);
939             rv34_mc_1mv (r, block_type, (i&1)<<3, (i&2)<<2, (i&1)+(i>>1)*s->b8_stride, 1, 1, 0);
940         }
941         break;
942     }
943
944     return 0;
945 }
946 /** @} */ // mv group
947
948 /**
949  * @name Macroblock reconstruction functions
950  * @{
951  */
952 /** mapping of RV30/40 intra prediction types to standard H.264 types */
953 static const int ittrans[9] = {
954  DC_PRED, VERT_PRED, HOR_PRED, DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_LEFT_PRED,
955  VERT_RIGHT_PRED, VERT_LEFT_PRED, HOR_UP_PRED, HOR_DOWN_PRED,
956 };
957
958 /** mapping of RV30/40 intra 16x16 prediction types to standard H.264 types */
959 static const int ittrans16[4] = {
960  DC_PRED8x8, VERT_PRED8x8, HOR_PRED8x8, PLANE_PRED8x8,
961 };
962
963 /**
964  * Perform 4x4 intra prediction.
965  */
966 static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int itype, int up, int left, int down, int right)
967 {
968     uint8_t *prev = dst - stride + 4;
969     uint32_t topleft;
970
971     if(!up && !left)
972         itype = DC_128_PRED;
973     else if(!up){
974         if(itype == VERT_PRED) itype = HOR_PRED;
975         if(itype == DC_PRED)   itype = LEFT_DC_PRED;
976     }else if(!left){
977         if(itype == HOR_PRED)  itype = VERT_PRED;
978         if(itype == DC_PRED)   itype = TOP_DC_PRED;
979         if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
980     }
981     if(!down){
982         if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
983         if(itype == HOR_UP_PRED) itype = HOR_UP_PRED_RV40_NODOWN;
984         if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;
985     }
986     if(!right && up){
987         topleft = dst[-stride + 3] * 0x01010101u;
988         prev = (uint8_t*)&topleft;
989     }
990     r->h.pred4x4[itype](dst, prev, stride);
991 }
992
993 static inline int adjust_pred16(int itype, int up, int left)
994 {
995     if(!up && !left)
996         itype = DC_128_PRED8x8;
997     else if(!up){
998         if(itype == PLANE_PRED8x8)itype = HOR_PRED8x8;
999         if(itype == VERT_PRED8x8) itype = HOR_PRED8x8;
1000         if(itype == DC_PRED8x8)   itype = LEFT_DC_PRED8x8;
1001     }else if(!left){
1002         if(itype == PLANE_PRED8x8)itype = VERT_PRED8x8;
1003         if(itype == HOR_PRED8x8)  itype = VERT_PRED8x8;
1004         if(itype == DC_PRED8x8)   itype = TOP_DC_PRED8x8;
1005     }
1006     return itype;
1007 }
1008
1009 static inline void rv34_process_block(RV34DecContext *r,
1010                                       uint8_t *pdst, int stride,
1011                                       int fc, int sc, int q_dc, int q_ac)
1012 {
1013     MpegEncContext *s = &r->s;
1014     int16_t *ptr = s->block[0];
1015     int has_ac = rv34_decode_block(ptr, &s->gb, r->cur_vlcs,
1016                                    fc, sc, q_dc, q_ac, q_ac);
1017     if(has_ac){
1018         r->rdsp.rv34_idct_add(pdst, stride, ptr);
1019     }else{
1020         r->rdsp.rv34_idct_dc_add(pdst, stride, ptr[0]);
1021         ptr[0] = 0;
1022     }
1023 }
1024
1025 static void rv34_output_i16x16(RV34DecContext *r, int8_t *intra_types, int cbp)
1026 {
1027     LOCAL_ALIGNED_16(int16_t, block16, [16]);
1028     MpegEncContext *s    = &r->s;
1029     GetBitContext  *gb   = &s->gb;
1030     int             q_dc = rv34_qscale_tab[ r->luma_dc_quant_i[s->qscale] ],
1031                     q_ac = rv34_qscale_tab[s->qscale];
1032     uint8_t        *dst  = s->dest[0];
1033     int16_t        *ptr  = s->block[0];
1034     int i, j, itype, has_ac;
1035
1036     memset(block16, 0, 16 * sizeof(*block16));
1037
1038     has_ac = rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac);
1039     if(has_ac)
1040         r->rdsp.rv34_inv_transform(block16);
1041     else
1042         r->rdsp.rv34_inv_transform_dc(block16);
1043
1044     itype = ittrans16[intra_types[0]];
1045     itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
1046     r->h.pred16x16[itype](dst, s->linesize);
1047
1048     for(j = 0; j < 4; j++){
1049         for(i = 0; i < 4; i++, cbp >>= 1){
1050             int dc = block16[i + j*4];
1051
1052             if(cbp & 1){
1053                 has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
1054             }else
1055                 has_ac = 0;
1056
1057             if(has_ac){
1058                 ptr[0] = dc;
1059                 r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
1060             }else
1061                 r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
1062         }
1063
1064         dst += 4*s->linesize;
1065     }
1066
1067     itype = ittrans16[intra_types[0]];
1068     if(itype == PLANE_PRED8x8) itype = DC_PRED8x8;
1069     itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
1070
1071     q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
1072     q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
1073
1074     for(j = 1; j < 3; j++){
1075         dst = s->dest[j];
1076         r->h.pred8x8[itype](dst, s->uvlinesize);
1077         for(i = 0; i < 4; i++, cbp >>= 1){
1078             uint8_t *pdst;
1079             if(!(cbp & 1)) continue;
1080             pdst   = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
1081
1082             rv34_process_block(r, pdst, s->uvlinesize,
1083                                r->chroma_vlc, 1, q_dc, q_ac);
1084         }
1085     }
1086 }
1087
1088 static void rv34_output_intra(RV34DecContext *r, int8_t *intra_types, int cbp)
1089 {
1090     MpegEncContext *s   = &r->s;
1091     uint8_t        *dst = s->dest[0];
1092     int      avail[6*8] = {0};
1093     int i, j, k;
1094     int idx, q_ac, q_dc;
1095
1096     // Set neighbour information.
1097     if(r->avail_cache[1])
1098         avail[0] = 1;
1099     if(r->avail_cache[2])
1100         avail[1] = avail[2] = 1;
1101     if(r->avail_cache[3])
1102         avail[3] = avail[4] = 1;
1103     if(r->avail_cache[4])
1104         avail[5] = 1;
1105     if(r->avail_cache[5])
1106         avail[8] = avail[16] = 1;
1107     if(r->avail_cache[9])
1108         avail[24] = avail[32] = 1;
1109
1110     q_ac = rv34_qscale_tab[s->qscale];
1111     for(j = 0; j < 4; j++){
1112         idx = 9 + j*8;
1113         for(i = 0; i < 4; i++, cbp >>= 1, dst += 4, idx++){
1114             rv34_pred_4x4_block(r, dst, s->linesize, ittrans[intra_types[i]], avail[idx-8], avail[idx-1], avail[idx+7], avail[idx-7]);
1115             avail[idx] = 1;
1116             if(!(cbp & 1)) continue;
1117
1118             rv34_process_block(r, dst, s->linesize,
1119                                r->luma_vlc, 0, q_ac, q_ac);
1120         }
1121         dst += s->linesize * 4 - 4*4;
1122         intra_types += r->intra_types_stride;
1123     }
1124
1125     intra_types -= r->intra_types_stride * 4;
1126
1127     q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
1128     q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
1129
1130     for(k = 0; k < 2; k++){
1131         dst = s->dest[1+k];
1132         fill_rectangle(r->avail_cache + 6, 2, 2, 4, 0, 4);
1133
1134         for(j = 0; j < 2; j++){
1135             int* acache = r->avail_cache + 6 + j*4;
1136             for(i = 0; i < 2; i++, cbp >>= 1, acache++){
1137                 int itype = ittrans[intra_types[i*2+j*2*r->intra_types_stride]];
1138                 rv34_pred_4x4_block(r, dst+4*i, s->uvlinesize, itype, acache[-4], acache[-1], !i && !j, acache[-3]);
1139                 acache[0] = 1;
1140
1141                 if(!(cbp&1)) continue;
1142
1143                 rv34_process_block(r, dst + 4*i, s->uvlinesize,
1144                                    r->chroma_vlc, 1, q_dc, q_ac);
1145             }
1146
1147             dst += 4*s->uvlinesize;
1148         }
1149     }
1150 }
1151
1152 static int is_mv_diff_gt_3(int16_t (*motion_val)[2], int step)
1153 {
1154     int d;
1155     d = motion_val[0][0] - motion_val[-step][0];
1156     if(d < -3 || d > 3)
1157         return 1;
1158     d = motion_val[0][1] - motion_val[-step][1];
1159     if(d < -3 || d > 3)
1160         return 1;
1161     return 0;
1162 }
1163
1164 static int rv34_set_deblock_coef(RV34DecContext *r)
1165 {
1166     MpegEncContext *s = &r->s;
1167     int hmvmask = 0, vmvmask = 0, i, j;
1168     int midx = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
1169     int16_t (*motion_val)[2] = &s->current_picture_ptr->motion_val[0][midx];
1170     for(j = 0; j < 16; j += 8){
1171         for(i = 0; i < 2; i++){
1172             if(is_mv_diff_gt_3(motion_val + i, 1))
1173                 vmvmask |= 0x11 << (j + i*2);
1174             if((j || s->mb_y) && is_mv_diff_gt_3(motion_val + i, s->b8_stride))
1175                 hmvmask |= 0x03 << (j + i*2);
1176         }
1177         motion_val += s->b8_stride;
1178     }
1179     if(s->first_slice_line)
1180         hmvmask &= ~0x000F;
1181     if(!s->mb_x)
1182         vmvmask &= ~0x1111;
1183     if(r->rv30){ //RV30 marks both subblocks on the edge for filtering
1184         vmvmask |= (vmvmask & 0x4444) >> 1;
1185         hmvmask |= (hmvmask & 0x0F00) >> 4;
1186         if(s->mb_x)
1187             r->deblock_coefs[s->mb_x - 1 + s->mb_y*s->mb_stride] |= (vmvmask & 0x1111) << 3;
1188         if(!s->first_slice_line)
1189             r->deblock_coefs[s->mb_x + (s->mb_y - 1)*s->mb_stride] |= (hmvmask & 0xF) << 12;
1190     }
1191     return hmvmask | vmvmask;
1192 }
1193
1194 static int rv34_decode_inter_macroblock(RV34DecContext *r, int8_t *intra_types)
1195 {
1196     MpegEncContext *s   = &r->s;
1197     GetBitContext  *gb  = &s->gb;
1198     uint8_t        *dst = s->dest[0];
1199     int16_t        *ptr = s->block[0];
1200     int          mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1201     int cbp, cbp2;
1202     int q_dc, q_ac, has_ac;
1203     int i, j;
1204     int dist;
1205
1206     // Calculate which neighbours are available. Maybe it's worth optimizing too.
1207     memset(r->avail_cache, 0, sizeof(r->avail_cache));
1208     fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
1209     dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
1210     if(s->mb_x && dist)
1211         r->avail_cache[5] =
1212         r->avail_cache[9] = s->current_picture_ptr->mb_type[mb_pos - 1];
1213     if(dist >= s->mb_width)
1214         r->avail_cache[2] =
1215         r->avail_cache[3] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride];
1216     if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
1217         r->avail_cache[4] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride + 1];
1218     if(s->mb_x && dist > s->mb_width)
1219         r->avail_cache[1] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride - 1];
1220
1221     s->qscale = r->si.quant;
1222     cbp = cbp2 = rv34_decode_inter_mb_header(r, intra_types);
1223     r->cbp_luma  [mb_pos] = cbp;
1224     r->cbp_chroma[mb_pos] = cbp >> 16;
1225     r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r) | r->cbp_luma[mb_pos];
1226     s->current_picture_ptr->qscale_table[mb_pos] = s->qscale;
1227
1228     if(cbp == -1)
1229         return -1;
1230
1231     if (IS_INTRA(s->current_picture_ptr->mb_type[mb_pos])){
1232         if(r->is16) rv34_output_i16x16(r, intra_types, cbp);
1233         else        rv34_output_intra(r, intra_types, cbp);
1234         return 0;
1235     }
1236
1237     if(r->is16){
1238         // Only for RV34_MB_P_MIX16x16
1239         LOCAL_ALIGNED_16(int16_t, block16, [16]);
1240         memset(block16, 0, 16 * sizeof(*block16));
1241         q_dc = rv34_qscale_tab[ r->luma_dc_quant_p[s->qscale] ];
1242         q_ac = rv34_qscale_tab[s->qscale];
1243         if (rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac))
1244             r->rdsp.rv34_inv_transform(block16);
1245         else
1246             r->rdsp.rv34_inv_transform_dc(block16);
1247
1248         q_ac = rv34_qscale_tab[s->qscale];
1249
1250         for(j = 0; j < 4; j++){
1251             for(i = 0; i < 4; i++, cbp >>= 1){
1252                 int      dc   = block16[i + j*4];
1253
1254                 if(cbp & 1){
1255                     has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
1256                 }else
1257                     has_ac = 0;
1258
1259                 if(has_ac){
1260                     ptr[0] = dc;
1261                     r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
1262                 }else
1263                     r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
1264             }
1265
1266             dst += 4*s->linesize;
1267         }
1268
1269         r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
1270     }else{
1271         q_ac = rv34_qscale_tab[s->qscale];
1272
1273         for(j = 0; j < 4; j++){
1274             for(i = 0; i < 4; i++, cbp >>= 1){
1275                 if(!(cbp & 1)) continue;
1276
1277                 rv34_process_block(r, dst + 4*i, s->linesize,
1278                                    r->luma_vlc, 0, q_ac, q_ac);
1279             }
1280             dst += 4*s->linesize;
1281         }
1282     }
1283
1284     q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
1285     q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
1286
1287     for(j = 1; j < 3; j++){
1288         dst = s->dest[j];
1289         for(i = 0; i < 4; i++, cbp >>= 1){
1290             uint8_t *pdst;
1291             if(!(cbp & 1)) continue;
1292             pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
1293
1294             rv34_process_block(r, pdst, s->uvlinesize,
1295                                r->chroma_vlc, 1, q_dc, q_ac);
1296         }
1297     }
1298
1299     return 0;
1300 }
1301
1302 static int rv34_decode_intra_macroblock(RV34DecContext *r, int8_t *intra_types)
1303 {
1304     MpegEncContext *s = &r->s;
1305     int cbp, dist;
1306     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1307
1308     // Calculate which neighbours are available. Maybe it's worth optimizing too.
1309     memset(r->avail_cache, 0, sizeof(r->avail_cache));
1310     fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
1311     dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
1312     if(s->mb_x && dist)
1313         r->avail_cache[5] =
1314         r->avail_cache[9] = s->current_picture_ptr->mb_type[mb_pos - 1];
1315     if(dist >= s->mb_width)
1316         r->avail_cache[2] =
1317         r->avail_cache[3] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride];
1318     if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
1319         r->avail_cache[4] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride + 1];
1320     if(s->mb_x && dist > s->mb_width)
1321         r->avail_cache[1] = s->current_picture_ptr->mb_type[mb_pos - s->mb_stride - 1];
1322
1323     s->qscale = r->si.quant;
1324     cbp = rv34_decode_intra_mb_header(r, intra_types);
1325     r->cbp_luma  [mb_pos] = cbp;
1326     r->cbp_chroma[mb_pos] = cbp >> 16;
1327     r->deblock_coefs[mb_pos] = 0xFFFF;
1328     s->current_picture_ptr->qscale_table[mb_pos] = s->qscale;
1329
1330     if(cbp == -1)
1331         return -1;
1332
1333     if(r->is16){
1334         rv34_output_i16x16(r, intra_types, cbp);
1335         return 0;
1336     }
1337
1338     rv34_output_intra(r, intra_types, cbp);
1339     return 0;
1340 }
1341
1342 static int check_slice_end(RV34DecContext *r, MpegEncContext *s)
1343 {
1344     int bits;
1345     if(s->mb_y >= s->mb_height)
1346         return 1;
1347     if(!s->mb_num_left)
1348         return 1;
1349     if(r->s.mb_skip_run > 1)
1350         return 0;
1351     bits = get_bits_left(&s->gb);
1352     if(bits <= 0 || (bits < 8 && !show_bits(&s->gb, bits)))
1353         return 1;
1354     return 0;
1355 }
1356
1357
1358 static void rv34_decoder_free(RV34DecContext *r)
1359 {
1360     av_freep(&r->intra_types_hist);
1361     r->intra_types = NULL;
1362     av_freep(&r->tmp_b_block_base);
1363     av_freep(&r->mb_type);
1364     av_freep(&r->cbp_luma);
1365     av_freep(&r->cbp_chroma);
1366     av_freep(&r->deblock_coefs);
1367 }
1368
1369
1370 static int rv34_decoder_alloc(RV34DecContext *r)
1371 {
1372     r->intra_types_stride = r->s.mb_width * 4 + 4;
1373
1374     r->cbp_chroma       = av_mallocz(r->s.mb_stride * r->s.mb_height *
1375                                     sizeof(*r->cbp_chroma));
1376     r->cbp_luma         = av_mallocz(r->s.mb_stride * r->s.mb_height *
1377                                     sizeof(*r->cbp_luma));
1378     r->deblock_coefs    = av_mallocz(r->s.mb_stride * r->s.mb_height *
1379                                     sizeof(*r->deblock_coefs));
1380     r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 *
1381                                     sizeof(*r->intra_types_hist));
1382     r->mb_type          = av_mallocz(r->s.mb_stride * r->s.mb_height *
1383                                      sizeof(*r->mb_type));
1384
1385     if (!(r->cbp_chroma       && r->cbp_luma && r->deblock_coefs &&
1386           r->intra_types_hist && r->mb_type)) {
1387         rv34_decoder_free(r);
1388         return AVERROR(ENOMEM);
1389     }
1390
1391     r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
1392
1393     return 0;
1394 }
1395
1396
1397 static int rv34_decoder_realloc(RV34DecContext *r)
1398 {
1399     rv34_decoder_free(r);
1400     return rv34_decoder_alloc(r);
1401 }
1402
1403
1404 static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
1405 {
1406     MpegEncContext *s = &r->s;
1407     GetBitContext *gb = &s->gb;
1408     int mb_pos, slice_type;
1409     int res;
1410
1411     init_get_bits(&r->s.gb, buf, buf_size*8);
1412     res = r->parse_slice_header(r, gb, &r->si);
1413     if(res < 0){
1414         av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
1415         return -1;
1416     }
1417
1418     slice_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
1419     if (slice_type != s->pict_type) {
1420         av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
1421         return AVERROR_INVALIDDATA;
1422     }
1423     if (s->width != r->si.width || s->height != r->si.height) {
1424         av_log(s->avctx, AV_LOG_ERROR, "Size mismatch\n");
1425         return AVERROR_INVALIDDATA;
1426     }
1427
1428     r->si.end = end;
1429     s->qscale = r->si.quant;
1430     s->mb_num_left = r->si.end - r->si.start;
1431     r->s.mb_skip_run = 0;
1432
1433     mb_pos = s->mb_x + s->mb_y * s->mb_width;
1434     if(r->si.start != mb_pos){
1435         av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
1436         s->mb_x = r->si.start % s->mb_width;
1437         s->mb_y = r->si.start / s->mb_width;
1438     }
1439     memset(r->intra_types_hist, -1, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
1440     s->first_slice_line = 1;
1441     s->resync_mb_x = s->mb_x;
1442     s->resync_mb_y = s->mb_y;
1443
1444     ff_init_block_index(s);
1445     while(!check_slice_end(r, s)) {
1446         ff_update_block_index(s);
1447
1448         if(r->si.type)
1449             res = rv34_decode_inter_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
1450         else
1451             res = rv34_decode_intra_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
1452         if(res < 0){
1453             ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_ERROR);
1454             return -1;
1455         }
1456         if (++s->mb_x == s->mb_width) {
1457             s->mb_x = 0;
1458             s->mb_y++;
1459             ff_init_block_index(s);
1460
1461             memmove(r->intra_types_hist, r->intra_types, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
1462             memset(r->intra_types, -1, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
1463
1464             if(r->loop_filter && s->mb_y >= 2)
1465                 r->loop_filter(r, s->mb_y - 2);
1466
1467             if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
1468                 ff_thread_report_progress(&s->current_picture_ptr->tf,
1469                                           s->mb_y - 2, 0);
1470
1471         }
1472         if(s->mb_x == s->resync_mb_x)
1473             s->first_slice_line=0;
1474         s->mb_num_left--;
1475     }
1476     ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
1477
1478     return s->mb_y == s->mb_height;
1479 }
1480
1481 /** @} */ // recons group end
1482
1483 /**
1484  * Initialize decoder.
1485  */
1486 av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
1487 {
1488     RV34DecContext *r = avctx->priv_data;
1489     MpegEncContext *s = &r->s;
1490     int ret;
1491
1492     ff_mpv_decode_defaults(s);
1493     ff_mpv_decode_init(s, avctx);
1494     s->out_format = FMT_H263;
1495
1496     avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1497     avctx->has_b_frames = 1;
1498     s->low_delay = 0;
1499
1500     ff_mpv_idct_init(s);
1501     if ((ret = ff_mpv_common_init(s)) < 0)
1502         return ret;
1503
1504     ff_h264_pred_init(&r->h, AV_CODEC_ID_RV40, 8, 1);
1505
1506 #if CONFIG_RV30_DECODER
1507     if (avctx->codec_id == AV_CODEC_ID_RV30)
1508         ff_rv30dsp_init(&r->rdsp);
1509 #endif
1510 #if CONFIG_RV40_DECODER
1511     if (avctx->codec_id == AV_CODEC_ID_RV40)
1512         ff_rv40dsp_init(&r->rdsp);
1513 #endif
1514
1515     if ((ret = rv34_decoder_alloc(r)) < 0) {
1516         ff_mpv_common_end(&r->s);
1517         return ret;
1518     }
1519
1520     if(!intra_vlcs[0].cbppattern[0].bits)
1521         rv34_init_tables();
1522
1523     avctx->internal->allocate_progress = 1;
1524
1525     return 0;
1526 }
1527
1528 int ff_rv34_decode_init_thread_copy(AVCodecContext *avctx)
1529 {
1530     int err;
1531     RV34DecContext *r = avctx->priv_data;
1532
1533     r->s.avctx = avctx;
1534
1535     if (avctx->internal->is_copy) {
1536         r->tmp_b_block_base = NULL;
1537         r->cbp_chroma       = NULL;
1538         r->cbp_luma         = NULL;
1539         r->deblock_coefs    = NULL;
1540         r->intra_types_hist = NULL;
1541         r->mb_type          = NULL;
1542
1543         ff_mpv_idct_init(&r->s);
1544
1545         if ((err = ff_mpv_common_init(&r->s)) < 0)
1546             return err;
1547         if ((err = rv34_decoder_alloc(r)) < 0) {
1548             ff_mpv_common_end(&r->s);
1549             return err;
1550         }
1551     }
1552
1553     return 0;
1554 }
1555
1556 int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1557 {
1558     RV34DecContext *r = dst->priv_data, *r1 = src->priv_data;
1559     MpegEncContext * const s = &r->s, * const s1 = &r1->s;
1560     int err;
1561
1562     if (dst == src || !s1->context_initialized)
1563         return 0;
1564
1565     if (s->height != s1->height || s->width != s1->width) {
1566         s->height = s1->height;
1567         s->width  = s1->width;
1568         if ((err = ff_mpv_common_frame_size_change(s)) < 0)
1569             return err;
1570         if ((err = rv34_decoder_realloc(r)) < 0)
1571             return err;
1572     }
1573
1574     r->cur_pts  = r1->cur_pts;
1575     r->last_pts = r1->last_pts;
1576     r->next_pts = r1->next_pts;
1577
1578     memset(&r->si, 0, sizeof(r->si));
1579
1580     // Do no call ff_mpeg_update_thread_context on a partially initialized
1581     // decoder context.
1582     if (!s1->linesize)
1583         return 0;
1584
1585     return ff_mpeg_update_thread_context(dst, src);
1586 }
1587
1588 static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
1589 {
1590     if(avctx->slice_count) return avctx->slice_offset[n];
1591     else                   return AV_RL32(buf + n*8 - 4) == 1 ? AV_RL32(buf + n*8) :  AV_RB32(buf + n*8);
1592 }
1593
1594 static int finish_frame(AVCodecContext *avctx, AVFrame *pict)
1595 {
1596     RV34DecContext *r = avctx->priv_data;
1597     MpegEncContext *s = &r->s;
1598     int got_picture = 0, ret;
1599
1600     ff_er_frame_end(&s->er);
1601     ff_mpv_frame_end(s);
1602     s->mb_num_left = 0;
1603
1604     if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
1605         ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
1606
1607     if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1608         if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
1609             return ret;
1610         ff_print_debug_info(s, s->current_picture_ptr, pict);
1611         ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
1612         got_picture = 1;
1613     } else if (s->last_picture_ptr) {
1614         if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
1615             return ret;
1616         ff_print_debug_info(s, s->last_picture_ptr, pict);
1617         ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
1618         got_picture = 1;
1619     }
1620
1621     return got_picture;
1622 }
1623
1624 static AVRational update_sar(int old_w, int old_h, AVRational sar, int new_w, int new_h)
1625 {
1626     // attempt to keep aspect during typical resolution switches
1627     if (!sar.num)
1628         sar = (AVRational){1, 1};
1629
1630     sar = av_mul_q(sar, (AVRational){new_h * old_w, new_w * old_h});
1631     return sar;
1632 }
1633
1634 int ff_rv34_decode_frame(AVCodecContext *avctx,
1635                             void *data, int *got_picture_ptr,
1636                             AVPacket *avpkt)
1637 {
1638     const uint8_t *buf = avpkt->data;
1639     int buf_size = avpkt->size;
1640     RV34DecContext *r = avctx->priv_data;
1641     MpegEncContext *s = &r->s;
1642     AVFrame *pict = data;
1643     SliceInfo si;
1644     int i, ret;
1645     int slice_count;
1646     const uint8_t *slices_hdr = NULL;
1647     int last = 0;
1648
1649     /* no supplementary picture */
1650     if (buf_size == 0) {
1651         /* special case for last picture */
1652         if (s->low_delay==0 && s->next_picture_ptr) {
1653             if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
1654                 return ret;
1655             s->next_picture_ptr = NULL;
1656
1657             *got_picture_ptr = 1;
1658         }
1659         return 0;
1660     }
1661
1662     if(!avctx->slice_count){
1663         slice_count = (*buf++) + 1;
1664         slices_hdr = buf + 4;
1665         buf += 8 * slice_count;
1666         buf_size -= 1 + 8 * slice_count;
1667     }else
1668         slice_count = avctx->slice_count;
1669
1670     //parse first slice header to check whether this frame can be decoded
1671     if(get_slice_offset(avctx, slices_hdr, 0) < 0 ||
1672        get_slice_offset(avctx, slices_hdr, 0) > buf_size){
1673         av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1674         return AVERROR_INVALIDDATA;
1675     }
1676     init_get_bits(&s->gb, buf+get_slice_offset(avctx, slices_hdr, 0), (buf_size-get_slice_offset(avctx, slices_hdr, 0))*8);
1677     if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){
1678         av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\n");
1679         return AVERROR_INVALIDDATA;
1680     }
1681     if ((!s->last_picture_ptr || !s->last_picture_ptr->f->data[0]) &&
1682         si.type == AV_PICTURE_TYPE_B) {
1683         av_log(avctx, AV_LOG_ERROR, "Invalid decoder state: B-frame without "
1684                "reference data.\n");
1685         return AVERROR_INVALIDDATA;
1686     }
1687     if(   (avctx->skip_frame >= AVDISCARD_NONREF && si.type==AV_PICTURE_TYPE_B)
1688        || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=AV_PICTURE_TYPE_I)
1689        ||  avctx->skip_frame >= AVDISCARD_ALL)
1690         return avpkt->size;
1691
1692     /* first slice */
1693     if (si.start == 0) {
1694         if (s->mb_num_left > 0 && s->current_picture_ptr) {
1695             av_log(avctx, AV_LOG_ERROR, "New frame but still %d MB left.\n",
1696                    s->mb_num_left);
1697             ff_er_frame_end(&s->er);
1698             ff_mpv_frame_end(s);
1699         }
1700
1701         if (s->width != si.width || s->height != si.height) {
1702             int err;
1703
1704             av_log(s->avctx, AV_LOG_WARNING, "Changing dimensions to %dx%d\n",
1705                    si.width, si.height);
1706
1707             if (av_image_check_size(si.width, si.height, 0, s->avctx))
1708                 return AVERROR_INVALIDDATA;
1709
1710             s->avctx->sample_aspect_ratio = update_sar(
1711                 s->width, s->height, s->avctx->sample_aspect_ratio,
1712                 si.width, si.height);
1713             s->width  = si.width;
1714             s->height = si.height;
1715
1716             err = ff_set_dimensions(s->avctx, s->width, s->height);
1717             if (err < 0)
1718                 return err;
1719
1720             if ((err = ff_mpv_common_frame_size_change(s)) < 0)
1721                 return err;
1722             if ((err = rv34_decoder_realloc(r)) < 0)
1723                 return err;
1724         }
1725         s->pict_type = si.type ? si.type : AV_PICTURE_TYPE_I;
1726         if (ff_mpv_frame_start(s, s->avctx) < 0)
1727             return -1;
1728         ff_mpeg_er_frame_start(s);
1729         if (!r->tmp_b_block_base) {
1730             int i;
1731
1732             r->tmp_b_block_base = av_malloc(s->linesize * 48);
1733             for (i = 0; i < 2; i++)
1734                 r->tmp_b_block_y[i] = r->tmp_b_block_base
1735                                       + i * 16 * s->linesize;
1736             for (i = 0; i < 4; i++)
1737                 r->tmp_b_block_uv[i] = r->tmp_b_block_base + 32 * s->linesize
1738                                        + (i >> 1) * 8 * s->uvlinesize
1739                                        + (i &  1) * 16;
1740         }
1741         r->cur_pts = si.pts;
1742         if (s->pict_type != AV_PICTURE_TYPE_B) {
1743             r->last_pts = r->next_pts;
1744             r->next_pts = r->cur_pts;
1745         } else {
1746             int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
1747             int dist0   = GET_PTS_DIFF(r->cur_pts,  r->last_pts);
1748             int dist1   = GET_PTS_DIFF(r->next_pts, r->cur_pts);
1749
1750             if(!refdist){
1751                 r->mv_weight1 = r->mv_weight2 = r->weight1 = r->weight2 = 8192;
1752                 r->scaled_weight = 0;
1753             }else{
1754                 r->mv_weight1 = (dist0 << 14) / refdist;
1755                 r->mv_weight2 = (dist1 << 14) / refdist;
1756                 if((r->mv_weight1|r->mv_weight2) & 511){
1757                     r->weight1 = r->mv_weight1;
1758                     r->weight2 = r->mv_weight2;
1759                     r->scaled_weight = 0;
1760                 }else{
1761                     r->weight1 = r->mv_weight1 >> 9;
1762                     r->weight2 = r->mv_weight2 >> 9;
1763                     r->scaled_weight = 1;
1764                 }
1765             }
1766         }
1767         s->mb_x = s->mb_y = 0;
1768         ff_thread_finish_setup(s->avctx);
1769     } else if (HAVE_THREADS &&
1770                (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
1771         av_log(s->avctx, AV_LOG_ERROR, "Decoder needs full frames in frame "
1772                "multithreading mode (start MB is %d).\n", si.start);
1773         return AVERROR_INVALIDDATA;
1774     }
1775
1776     for(i = 0; i < slice_count; i++){
1777         int offset = get_slice_offset(avctx, slices_hdr, i);
1778         int size;
1779         if(i+1 == slice_count)
1780             size = buf_size - offset;
1781         else
1782             size = get_slice_offset(avctx, slices_hdr, i+1) - offset;
1783
1784         if(offset < 0 || offset > buf_size){
1785             av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1786             break;
1787         }
1788
1789         r->si.end = s->mb_width * s->mb_height;
1790         s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;
1791
1792         if(i+1 < slice_count){
1793             if (get_slice_offset(avctx, slices_hdr, i+1) < 0 ||
1794                 get_slice_offset(avctx, slices_hdr, i+1) > buf_size) {
1795                 av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1796                 break;
1797             }
1798             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);
1799             if(r->parse_slice_header(r, &r->s.gb, &si) < 0){
1800                 if(i+2 < slice_count)
1801                     size = get_slice_offset(avctx, slices_hdr, i+2) - offset;
1802                 else
1803                     size = buf_size - offset;
1804             }else
1805                 r->si.end = si.start;
1806         }
1807         if (size < 0 || size > buf_size - offset) {
1808             av_log(avctx, AV_LOG_ERROR, "Slice size is invalid\n");
1809             break;
1810         }
1811         last = rv34_decode_slice(r, r->si.end, buf + offset, size);
1812         if(last)
1813             break;
1814     }
1815
1816     if (s->current_picture_ptr) {
1817         if (last) {
1818             if(r->loop_filter)
1819                 r->loop_filter(r, s->mb_height - 1);
1820
1821             ret = finish_frame(avctx, pict);
1822             if (ret < 0)
1823                 return ret;
1824             *got_picture_ptr = ret;
1825         } else if (HAVE_THREADS &&
1826                    (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
1827             av_log(avctx, AV_LOG_INFO, "marking unfished frame as finished\n");
1828             /* always mark the current frame as finished, frame-mt supports
1829              * only complete frames */
1830             ff_er_frame_end(&s->er);
1831             ff_mpv_frame_end(s);
1832             s->mb_num_left = 0;
1833             ff_thread_report_progress(&s->current_picture_ptr->tf, INT_MAX, 0);
1834             return AVERROR_INVALIDDATA;
1835         }
1836     }
1837
1838     return avpkt->size;
1839 }
1840
1841 av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
1842 {
1843     RV34DecContext *r = avctx->priv_data;
1844
1845     ff_mpv_common_end(&r->s);
1846     rv34_decoder_free(r);
1847
1848     return 0;
1849 }