1 /*
2 * RV40 decoder
3 * Copyright (c) 2007 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 * RV40 decoder
25 */
26
28
33
37
41
43 0, 614, 1222, 1794, 2410, 3014, 3586, 4202, 4792, 5382, 5966, 6542,
44 7138, 7716, 8292, 8864, 9444, 10030, 10642, 11212, 11814
45 };
46
47 /**
48 * Initialize all tables.
49 */
51 {
52 int i;
55 static VLC_TYPE aic_mode2_table[11814][2];
58
59 aic_top_vlc.
table = aic_table;
65 // Every tenth VLC table is empty
66 if((i % 10) == 9) continue;
72 }
79 }
87 }
95 }
96 }
97
98 /**
99 * Get stored dimension from bitstream.
100 *
101 * If the width/height is the standard one then it's coded as a 3-bit index.
102 * Otherwise it is coded as escaped 8-bit portions.
103 */
105 {
108 if(val < 0)
110 if(!val){
111 do{
113 val += t << 2;
114 }while(t == 0xFF);
115 }
117 }
118
119 /**
120 * Get encoded picture size - usually this is called from rv40_parse_slice_header.
121 */
123 {
126 }
127
129 {
130 int mb_bits;
132 int mb_size;
134
152 mb_size = ((w + 15) >> 4) * ((h + 15) >> 4);
155
156 return 0;
157 }
158
159 /**
160 * Decode 4x4 intra types array.
161 */
163 {
167 int pattern;
168 int8_t *ptr;
169
173 dst[0] = (pattern >> 2) & 2;
174 dst[1] = (pattern >> 1) & 2;
175 dst[2] = pattern & 2;
176 dst[3] = (pattern << 1) & 2;
177 continue;
178 }
179 ptr = dst;
180 for(j = 0; j < 4; j++){
181 /* Coefficients are read using VLC chosen by the prediction pattern
182 * The first one (used for retrieving a pair of coefficients) is
183 * constructed from the top, top right and left coefficients
184 * The second one (used for retrieving only one coefficient) is
185 * top + 10 * left.
186 */
187 A = ptr[-r->
intra_types_stride + 1];
// it won't be used for the last coefficient in a row
189 C = ptr[-1];
190 pattern = A + (B << 4) + (C << 8);
193 break;
194 if(j < 3 && k < MODE2_PATTERNS_NUM){ //pattern is found, decoding 2 coefficients
196 *ptr++ = v/9;
197 *ptr++ = v%9;
198 j++;
199 }else{
200 if(B != -1 && C != -1)
202 else{ // tricky decoding
203 v = 0;
204 switch(C){
205 case -1: // code 0 -> 1, 1 -> 0
206 if(B < 2)
208 break;
209 case 0:
210 case 2: // code 0 -> 2, 1 -> 0
212 break;
213 }
214 }
216 }
217 }
218 }
219 return 0;
220 }
221
222 /**
223 * Decode macroblock information.
224 */
226 {
229 int q, i;
230 int prev_type = 0;
232
236 return -1;
237 }
238
241
246 blocks[r->
mb_type[mb_pos - 1]]++;
253 if(blocks[i] > count){
254 count = blocks[i];
255 prev_type = i;
256 if(count>1)
257 break;
258 }
259 }
261 prev_type = r->
mb_type[mb_pos - 1];
262
267 return q;
270 }else{
274 return q;
277 }
278 return 0;
279 }
280
286 };
287
288 #define MASK_CUR 0x0001
289 #define MASK_RIGHT 0x0008
290 #define MASK_BOTTOM 0x0010
291 #define MASK_TOP 0x1000
292 #define MASK_Y_TOP_ROW 0x000F
293 #define MASK_Y_LAST_ROW 0xF000
294 #define MASK_Y_LEFT_COL 0x1111
295 #define MASK_Y_RIGHT_COL 0x8888
296 #define MASK_C_TOP_ROW 0x0003
297 #define MASK_C_LAST_ROW 0x000C
298 #define MASK_C_LEFT_COL 0x0005
299 #define MASK_C_RIGHT_COL 0x000A
300
303
306 int lim_q1, int lim_p1,
307 int alpha,
int beta,
int beta2,
308 int chroma, int edge, int dir)
309 {
310 int filter_p1, filter_q1;
311 int strong;
312 int lims;
313
315 edge, &filter_p1, &filter_q1);
316
317 lims = filter_p1 + filter_q1 + ((lim_q1 + lim_p1) >> 1) + 1;
318
319 if (strong) {
321 lims, dmode, chroma);
322 } else if (filter_p1 & filter_q1) {
324 lims, lim_q1, lim_p1);
325 } else if (filter_p1 | filter_q1) {
327 alpha, beta, lims >> 1, lim_q1 >> 1,
328 lim_p1 >> 1);
329 }
330 }
331
332 /**
333 * RV40 loop filtering function
334 */
336 {
338 int mb_pos, mb_x;
339 int i, j, k;
341 int alpha, beta, betaY, betaC;
342 int q;
343 int mbtype[4]; ///< current macroblock and its neighbours types
344 /**
345 * flags indicating that macroblock can be filtered with strong filter
346 * it is set only for intra coded MB and MB with DCs coded separately
347 */
348 int mb_strong[4];
349 int clip[4];
///< MB filter clipping value calculated from filtering strength
350 /**
351 * coded block patterns for luma part of current macroblock and its neighbours
352 * Format:
353 * LSB corresponds to the top left block,
354 * each nibble represents one row of subblocks.
355 */
356 int cbp[4];
357 /**
358 * coded block patterns for chroma part of current macroblock and its neighbours
359 * Format is the same as for luma with two subblocks in a row.
360 */
361 int uvcbp[4][2];
362 /**
363 * This mask represents the pattern of luma subblocks that should be filtered
364 * in addition to the coded ones because they lie at the edge of
365 * 8x8 block with different enough motion vectors
366 */
367 unsigned mvmasks[4];
368
370 for(mb_x = 0; mb_x < s->
mb_width; mb_x++, mb_pos++){
376 }
378 for(mb_x = 0; mb_x < s->
mb_width; mb_x++, mb_pos++){
379 int y_h_deblock, y_v_deblock;
380 int c_v_deblock[2], c_h_deblock[2];
381 int clip_left;
382 int avail[4];
383 unsigned y_to_deblock;
384 int c_to_deblock[2];
385
389 betaY = betaC = beta * 3;
391 betaY += beta;
392
393 avail[0] = 1;
394 avail[1] = row;
395 avail[2] = mb_x;
397 for(i = 0; i < 4; i++){
398 if(avail[i]){
405 }else{
406 mvmasks[i] = 0;
407 mbtype [i] = mbtype[0];
408 cbp [i] = 0;
409 uvcbp[i][0] = uvcbp[i][1] = 0;
410 }
413 }
414 y_to_deblock = mvmasks[
POS_CUR]
416 /* This pattern contains bits signalling that horizontal edges of
417 * the current block can be filtered.
418 * That happens when either of adjacent subblocks is coded or lies on
419 * the edge of 8x8 blocks with motion vectors differing by more than
420 * 3/4 pel in any component (any edge orientation for some reason).
421 */
422 y_h_deblock = y_to_deblock
425 /* This pattern contains bits signalling that vertical edges of
426 * the current block can be filtered.
427 * That happens when either of adjacent subblocks is coded or lies on
428 * the edge of 8x8 blocks with motion vectors differing by more than
429 * 3/4 pel in any component (any edge orientation for some reason).
430 */
431 y_v_deblock = y_to_deblock
434 if(!mb_x)
436 if(!row)
440 /* Calculating chroma patterns is similar and easier since there is
441 * no motion vector pattern for them.
442 */
443 for(i = 0; i < 2; i++){
445 c_v_deblock[i] = c_to_deblock[i]
448 c_h_deblock[i] = c_to_deblock[i]
451 if(!mb_x)
453 if(!row)
457 }
458
459 for(j = 0; j < 16; j += 4){
461 for(i = 0; i < 4; i++, Y += 4){
462 int ij = i + j;
464 int dither = j ? ij : i*4;
465
466 // if bottom block is coded then we can filter its top edge
467 // (or bottom edge of this block, which is the same)
472 clip_cur, alpha, beta, betaY,
473 0, 0, 0);
474 }
475 // filter left block edge in ordinary mode (with low filtering strength)
477 if(!i)
479 else
482 clip_cur,
483 clip_left,
484 alpha, beta, betaY, 0, 0, 1);
485 }
486 // filter top edge of the current macroblock when filtering strength is high
489 clip_cur,
490 mvmasks[POS_TOP] & (
MASK_TOP << i) ? clip[POS_TOP] : 0,
491 alpha, beta, betaY, 0, 1, 0);
492 }
493 // filter left block edge in edge mode (with high filtering strength)
494 if(y_v_deblock & (
MASK_CUR << ij) && !i && (mb_strong[
POS_CUR] | mb_strong[POS_LEFT])){
497 clip_cur,
498 clip_left,
499 alpha, beta, betaY, 0, 1, 1);
500 }
501 }
502 }
503 for(k = 0; k < 2; k++){
504 for(j = 0; j < 2; j++){
506 for(i = 0; i < 2; i++, C += 4){
507 int ij = i + j*2;
509 if(c_h_deblock[k] & (
MASK_CUR << (ij+2))){
510 int clip_bot = c_to_deblock[k] & (
MASK_CUR << (ij+2)) ? clip[
POS_CUR] : 0;
512 clip_bot,
513 clip_cur,
514 alpha, beta, betaC, 1, 0, 0);
515 }
517 if(!i)
519 else
520 clip_left = c_to_deblock[k] & (
MASK_CUR << (ij-1)) ? clip[
POS_CUR] : 0;
522 clip_cur,
523 clip_left,
524 alpha, beta, betaC, 1, 0, 1);
525 }
529 clip_cur,
530 clip_top,
531 alpha, beta, betaC, 1, 1, 0);
532 }
533 if(c_v_deblock[k] & (
MASK_CUR << ij) && !i && (mb_strong[
POS_CUR] | mb_strong[POS_LEFT])){
536 clip_cur,
537 clip_left,
538 alpha, beta, betaC, 1, 1, 1);
539 }
540 }
541 }
542 }
543 }
544 }
545
546 /**
547 * Initialize decoder.
548 */
550 {
553
557 if(!aic_top_vlc.
bits)
565 return 0;
566 }
567
583 },
586 };