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
27 #include "config.h"
28
31
38
42
46
48 int nb_codes,
const uint8_t (*
tab)[2])
49 {
51 &
tab[0][1], 2, &
tab[0][0], 2, 1,
52 0, 0);
53 }
54
55 /**
56 * Initialize all tables.
57 */
59 {
62
66 // Every tenth VLC table is empty
67 if((
i % 10) == 9)
continue;
71 }
74
78 if (HAVE_BIGENDIAN)
79 syms[j] = (
first << 8) | second;
80 else
81 syms[j] =
first | (second << 8);
82 }
86 syms, 2, 2, 0, 0);
87 }
92 }
97 }
98 }
99
100 /**
101 * Get stored dimension from bitstream.
102 *
103 * If the width/height is the standard one then it's coded as a 3-bit index.
104 * Otherwise it is coded as escaped 8-bit portions.
105 */
107 {
113 do{
118 }while(t == 0xFF);
119 }
121 }
122
123 /**
124 * Get encoded picture size - usually this is called from rv40_parse_slice_header.
125 */
127 {
130 }
131
133 {
134 int w =
r->s.width,
h =
r->s.height;
135 int mb_size;
137
155 mb_size = ((
w + 15) >> 4) * ((
h + 15) >> 4);
157
158 return 0;
159 }
160
161 /**
162 * Decode 4x4 intra types array.
163 */
165 {
169 int pattern;
170 int8_t *ptr;
171
172 for(
i = 0;
i < 4;
i++,
dst +=
r->intra_types_stride){
173 if(!
i &&
s->first_slice_line){
175 dst[0] = (pattern >> 2) & 2;
176 dst[1] = (pattern >> 1) & 2;
177 dst[2] = pattern & 2;
178 dst[3] = (pattern << 1) & 2;
179 continue;
180 }
182 for(j = 0; j < 4; j++){
183 /* Coefficients are read using VLC chosen by the prediction pattern
184 * The first one (used for retrieving a pair of coefficients) is
185 * constructed from the top, top right and left coefficients
186 * The second one (used for retrieving only one coefficient) is
187 * top + 10 * left.
188 */
189 A = ptr[-
r->intra_types_stride + 1];
// it won't be used for the last coefficient in a row
190 B = ptr[-
r->intra_types_stride];
192 pattern =
A +
B * (1 << 4) +
C * (1 << 8);
195 break;
198 ptr += 2;
199 j++;
200 }else{
201 if(
B != -1 &&
C != -1)
203 else{ // tricky decoding
204 v = 0;
206 case -1: // code 0 -> 1, 1 -> 0
209 break;
210 case 0:
211 case 2: // code 0 -> 2, 1 -> 0
213 break;
214 }
215 }
216 *ptr++ = v;
217 }
218 }
219 }
220 return 0;
221 }
222
223 /**
224 * Decode macroblock information.
225 */
227 {
231 int prev_type = 0;
232 int mb_pos =
s->mb_x +
s->mb_y *
s->mb_stride;
233
234 if (!
r->mb_skip_run) {
236 if (
r->mb_skip_run > (
unsigned)
s->mb_num)
237 return -1;
238 }
239
240 if (--
r->mb_skip_run)
242
243 if(
r->avail_cache[6-4]){
245 int count = 0;
246 if(
r->avail_cache[6-1])
247 blocks[
r->mb_type[mb_pos - 1]]++;
248 blocks[
r->mb_type[mb_pos -
s->mb_stride]]++;
249 if(
r->avail_cache[6-2])
250 blocks[
r->mb_type[mb_pos -
s->mb_stride + 1]]++;
251 if(
r->avail_cache[6-5])
252 blocks[
r->mb_type[mb_pos -
s->mb_stride - 1]]++;
254 if(blocks[
i] > count){
257 if(count>1)
258 break;
259 }
260 }
261 }
else if (
r->avail_cache[6-1])
262 prev_type =
r->mb_type[mb_pos - 1];
263
268 return q;
271 }else{
275 return q;
278 }
279 return 0;
280 }
281
287 };
288
289 #define MASK_CUR 0x0001
290 #define MASK_RIGHT 0x0008
291 #define MASK_BOTTOM 0x0010
292 #define MASK_TOP 0x1000
293 #define MASK_Y_TOP_ROW 0x000F
294 #define MASK_Y_LAST_ROW 0xF000
295 #define MASK_Y_LEFT_COL 0x1111
296 #define MASK_Y_RIGHT_COL 0x8888
297 #define MASK_C_TOP_ROW 0x0003
298 #define MASK_C_LAST_ROW 0x000C
299 #define MASK_C_LEFT_COL 0x0005
300 #define MASK_C_RIGHT_COL 0x000A
301
304
307 int lim_q1, int lim_p1,
308 int alpha,
int beta,
int beta2,
309 int chroma,
int edge,
int dir)
310 {
311 int filter_p1, filter_q1;
312 int strong;
313 int lims;
314
316 edge, &filter_p1, &filter_q1);
317
318 lims = filter_p1 + filter_q1 + ((lim_q1 + lim_p1) >> 1) + 1;
319
320 if (strong) {
323 } else if (filter_p1 & filter_q1) {
325 lims, lim_q1, lim_p1);
326 } else if (filter_p1 | filter_q1) {
328 alpha, beta, lims >> 1, lim_q1 >> 1,
329 lim_p1 >> 1);
330 }
331 }
332
333 /**
334 * RV40 loop filtering function
335 */
337 {
339 int mb_pos, mb_x;
342 int alpha, beta, betaY, betaC;
343 int q;
344 int mbtype[4]; ///< current macroblock and its neighbours types
345 /**
346 * flags indicating that macroblock can be filtered with strong filter
347 * it is set only for intra coded MB and MB with DCs coded separately
348 */
349 int mb_strong[4];
350 int clip[4];
///< MB filter clipping value calculated from filtering strength
351 /**
352 * coded block patterns for luma part of current macroblock and its neighbours
353 * Format:
354 * LSB corresponds to the top left block,
355 * each nibble represents one row of subblocks.
356 */
357 int cbp[4];
358 /**
359 * coded block patterns for chroma part of current macroblock and its neighbours
360 * Format is the same as for luma with two subblocks in a row.
361 */
362 int uvcbp[4][2];
363 /**
364 * This mask represents the pattern of luma subblocks that should be filtered
365 * in addition to the coded ones because they lie at the edge of
366 * 8x8 block with different enough motion vectors
367 */
368 unsigned mvmasks[4];
369
370 mb_pos = row *
s->mb_stride;
371 for(mb_x = 0; mb_x <
s->mb_width; mb_x++, mb_pos++){
372 int mbtype =
s->cur_pic.mb_type[mb_pos];
374 r->cbp_luma [mb_pos] =
r->deblock_coefs[mb_pos] = 0xFFFF;
376 r->cbp_chroma[mb_pos] = 0xFF;
377 }
378 mb_pos = row *
s->mb_stride;
379 for(mb_x = 0; mb_x <
s->mb_width; mb_x++, mb_pos++){
380 int y_h_deblock, y_v_deblock;
381 int c_v_deblock[2], c_h_deblock[2];
382 int clip_left;
383 int avail[4];
384 unsigned y_to_deblock;
385 int c_to_deblock[2];
386
387 q =
s->cur_pic.qscale_table[mb_pos];
390 betaY = betaC = beta * 3;
391 if(
s->width *
s->height <= 176*144)
392 betaY += beta;
393
394 avail[0] = 1;
395 avail[1] = row;
396 avail[2] = mb_x;
397 avail[3] = row <
s->mb_height - 1;
398 for(
i = 0;
i < 4;
i++){
401 mvmasks[
i] =
r->deblock_coefs[
pos];
402 mbtype [
i] =
s->cur_pic.mb_type[
pos];
403 cbp [
i] =
r->cbp_luma[
pos];
404 uvcbp[
i][0] =
r->cbp_chroma[
pos] & 0xF;
405 uvcbp[
i][1] =
r->cbp_chroma[
pos] >> 4;
406 }else{
408 mbtype [
i] = mbtype[0];
410 uvcbp[
i][0] = uvcbp[
i][1] = 0;
411 }
414 }
415 y_to_deblock = mvmasks[
POS_CUR]
417 /* This pattern contains bits signalling that horizontal edges of
418 * the current block can be filtered.
419 * That happens when either of adjacent subblocks is coded or lies on
420 * the edge of 8x8 blocks with motion vectors differing by more than
421 * 3/4 pel in any component (any edge orientation for some reason).
422 */
423 y_h_deblock = y_to_deblock
426 /* This pattern contains bits signalling that vertical edges of
427 * the current block can be filtered.
428 * That happens when either of adjacent subblocks is coded or lies on
429 * the edge of 8x8 blocks with motion vectors differing by more than
430 * 3/4 pel in any component (any edge orientation for some reason).
431 */
432 y_v_deblock = y_to_deblock
435 if(!mb_x)
437 if(!row)
441 /* Calculating chroma patterns is similar and easier since there is
442 * no motion vector pattern for them.
443 */
444 for(
i = 0;
i < 2;
i++){
446 c_v_deblock[
i] = c_to_deblock[
i]
449 c_h_deblock[
i] = c_to_deblock[
i]
452 if(!mb_x)
454 if(!row)
458 }
459
460 for(j = 0; j < 16; j += 4){
461 Y =
s->cur_pic.data[0] + mb_x*16 + (row*16 + j) *
s->linesize;
462 for(
i = 0;
i < 4;
i++,
Y += 4){
466
467 // if bottom block is coded then we can filter its top edge
468 // (or bottom edge of this block, which is the same)
473 clip_cur,
alpha, beta, betaY,
474 0, 0, 0);
475 }
476 // filter left block edge in ordinary mode (with low filtering strength)
480 else
483 clip_cur,
484 clip_left,
485 alpha, beta, betaY, 0, 0, 1);
486 }
487 // filter top edge of the current macroblock when filtering strength is high
490 clip_cur,
492 alpha, beta, betaY, 0, 1, 0);
493 }
494 // filter left block edge in edge mode (with high filtering strength)
498 clip_cur,
499 clip_left,
500 alpha, beta, betaY, 0, 1, 1);
501 }
502 }
503 }
504 for(k = 0; k < 2; k++){
505 for(j = 0; j < 2; j++){
506 C =
s->cur_pic.data[k + 1] + mb_x*8 + (row*8 + j*4) *
s->uvlinesize;
507 for(
i = 0;
i < 2;
i++,
C += 4){
510 if(c_h_deblock[k] & (
MASK_CUR << (ij+2))){
513 clip_bot,
514 clip_cur,
515 alpha, beta, betaC, 1, 0, 0);
516 }
520 else
523 clip_cur,
524 clip_left,
525 alpha, beta, betaC, 1, 0, 1);
526 }
530 clip_cur,
531 clip_top,
532 alpha, beta, betaC, 1, 1, 0);
533 }
537 clip_cur,
538 clip_left,
539 alpha, beta, betaC, 1, 1, 1);
540 }
541 }
542 }
543 }
544 }
545 }
546
547 /**
548 * Initialize decoder.
549 */
551 {
555
567 return 0;
568 }
569
584 };