1 /*
2 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
3 * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
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 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
25 * @author Stefan Gehrer <stefan.gehrer@gmx.de>
26 */
27
37
39 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3,
40 4, 4, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 18, 20,
41 22, 24, 26, 28, 30, 33, 33, 35, 35, 36, 37, 37, 39, 39, 42, 44,
42 46, 48, 50, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
43 };
44
46 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
47 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6,
48 6, 7, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 11, 12, 13, 14,
49 15, 16, 17, 18, 19, 20, 21, 22, 23, 23, 24, 24, 25, 25, 26, 27
50 };
51
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
55 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4,
56 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9
57 };
58
59 /** mark block as unavailable, i.e. out of picture
60 * or not yet decoded */
62
67
68 /*****************************************************************************
69 *
70 * in-loop deblocking filter
71 *
72 ****************************************************************************/
73
75 {
77 return 2;
78 if((abs(mvP->
x - mvQ->
x) >= 4) ||
79 (abs(mvP->
y - mvQ->
y) >= 4) ||
81 return 1;
82 if (b) {
85 if((abs(mvP->
x - mvQ->
x) >= 4) ||
86 (abs(mvP->
y - mvQ->
y) >= 4) ||
88 return 1;
89 }
90 return 0;
91 }
92
94 alpha = alpha_tab[av_clip_uintp2(qp_avg + h->alpha_offset, 6)]; \
95 beta = beta_tab[av_clip_uintp2(qp_avg + h->beta_offset, 6)]; \
96 tc = tc_tab[av_clip_uintp2(qp_avg + h->alpha_offset, 6)];
97
98 /**
99 * in-loop deblocking filter for a single macroblock
100 *
101 * boundary strength (bs) mapping:
102 *
103 * --4---5--
104 * 0 2 |
105 * | 6 | 7 |
106 * 1 3 |
107 * ---------
108 *
109 */
111 {
114 int i;
115
116 /* save un-deblocked lines */
123 for (i = 0; i < 8; i++) {
128 }
130 /* determine bs */
131 if (mb_type ==
I_8X8)
132 memset(bs, 2, 8);
133 else {
134 memset(bs, 0, 8);
138 }
142 }
147 }
157 }
162
171 }
172 }
173 }
176 }
177
178 #undef SET_PARAMS
179
180 /*****************************************************************************
181 *
182 * spatial intra prediction
183 *
184 ****************************************************************************/
185
188 {
189 int i;
190
191 switch (block) {
192 case 0:
197 top[17] = top[16];
198 top[0] = top[1];
201 break;
202 case 1:
204 for (i = 0; i < 8; i++)
211 else
212 memset(&top[9], top[8], 9);
213 top[17] = top[16];
214 top[0] = top[1];
217 break;
218 case 2:
221 top[17] = top[16];
222 top[0] = top[1];
225 break;
226 case 3:
228 for (i = 0; i < 8; i++)
231 memcpy(&top[0], h->
cy + 7 + 7 * h->
l_stride, 9);
232 memset(&top[9], top[8], 9);
233 break;
234 }
235 }
236
238 {
239 /* extend borders by one pixel */
245 } else {
248 }
252 } else {
257 }
258 }
259
261 {
264 for (y = 0; y < 8; y++)
265 *((uint64_t *)(d + y * stride)) =
a;
266 }
267
269 {
272 for (y = 0; y < 8; y++) {
273 a = left[y + 1] * 0x0101010101010101ULL;
274 *((uint64_t *)(d + y * stride)) = a;
275 }
276 }
277
279 {
281 uint64_t
a = 0x8080808080808080ULL;
282 for (y = 0; y < 8; y++)
283 *((uint64_t *)(d + y * stride)) =
a;
284 }
285
287 {
289 int ih = 0;
290 int iv = 0;
292
293 for (x = 0; x < 4; x++) {
294 ih += (x + 1) * (top[5 + x] - top[3 - x]);
295 iv += (x + 1) * (left[5 + x] - left[3 - x]);
296 }
297 ia = (top[8] + left[8]) << 4;
298 ih = (17 * ih + 16) >> 5;
299 iv = (17 * iv + 16) >> 5;
300 for (y = 0; y < 8; y++)
301 for (x = 0; x < 8; x++)
302 d[y * stride + x] = cm[(ia + (x - 3) * ih + (y - 3) * iv + 16) >> 5];
303 }
304
305 #define LOWPASS(ARRAY, INDEX) \
306 ((ARRAY[(INDEX) - 1] + 2 * ARRAY[(INDEX)] + ARRAY[(INDEX) + 1] + 2) >> 2)
307
309 {
311 for (y = 0; y < 8; y++)
312 for (x = 0; x < 8; x++)
313 d[y * stride + x] = (
LOWPASS(top, x + 1) +
LOWPASS(left, y + 1)) >> 1;
314 }
315
317 {
319 for (y = 0; y < 8; y++)
320 for (x = 0; x < 8; x++)
321 d[y * stride + x] = (
LOWPASS(top, x + y + 2) +
LOWPASS(left, x + y + 2)) >> 1;
322 }
323
325 {
327 for (y = 0; y < 8; y++)
328 for (x = 0; x < 8; x++)
329 if (x == y)
330 d[y * stride + x] = (left[1] + 2 * top[0] + top[1] + 2) >> 2;
331 else if (x > y)
332 d[y * stride + x] =
LOWPASS(top, x - y);
333 else
334 d[y * stride + x] =
LOWPASS(left, y - x);
335 }
336
338 {
340 for (y = 0; y < 8; y++)
341 for (x = 0; x < 8; x++)
342 d[y * stride + x] =
LOWPASS(left, y + 1);
343 }
344
346 {
348 for (y = 0; y < 8; y++)
349 for (x = 0; x < 8; x++)
350 d[y * stride + x] =
LOWPASS(top, x + 1);
351 }
352
353 #undef LOWPASS
354
356 {
357 *mode = mod_table[*
mode];
358 if (*mode < 0) {
360 *mode = 0;
361 }
362 }
363
365 {
366 /* save pred modes before they get modified */
371
372 /* modify pred modes according to availability of neighbour samples */
377 }
382 }
383 }
384
385 /*****************************************************************************
386 *
387 * motion compensation
388 *
389 ****************************************************************************/
390
394 int src_x_offset, int src_y_offset,
397 {
398 const int mx = mv->
x + src_x_offset * 8;
399 const int my = mv->
y + src_y_offset * 8;
400 const int luma_xy = (mx & 3) + ((my & 3) << 2);
404 int extra_width = 0;
405 int extra_height = extra_width;
406 const int full_mx = mx >> 2;
407 const int full_my = my >> 2;
408 const int pic_width = 16 * h->
mb_width;
409 const int pic_height = 16 * h->
mb_height;
410 int emu = 0;
411
413 return;
414 if (mx & 7)
415 extra_width -= 3;
416 if (my & 7)
417 extra_height -= 3;
418
419 if (full_mx < 0 - extra_width ||
420 full_my < 0 - extra_height ||
421 full_mx + 16 /* FIXME */ > pic_width + extra_width ||
422 full_my + 16 /* FIXME */ > pic_height + extra_height) {
426 16 + 5, 16 + 5 /* FIXME */,
427 full_mx - 2, full_my - 2,
428 pic_width, pic_height);
430 emu = 1;
431 }
432
433 // FIXME try variable height perhaps?
434 qpix_op[luma_xy](dest_y, src_y, h->
l_stride);
435
436 if (emu) {
439 9, 9 /* FIXME */,
440 mx >> 3, my >> 3,
441 pic_width >> 1, pic_height >> 1);
443 }
444 chroma_op(dest_cb, src_cb, h->
c_stride, chroma_height, mx & 7, my & 7);
445
446 if (emu) {
449 9, 9 /* FIXME */,
450 mx >> 3, my >> 3,
451 pic_width >> 1, pic_height >> 1);
453 }
454 chroma_op(dest_cr, src_cr, h->
c_stride, chroma_height, mx & 7, my & 7);
455 }
456
461 int x_offset, int y_offset,
467 {
470
471 dest_y += x_offset * 2 + y_offset * h->
l_stride * 2;
472 dest_cb += x_offset + y_offset * h->
c_stride;
473 dest_cr += x_offset + y_offset * h->
c_stride;
474 x_offset += 8 * h->
mbx;
475 y_offset += 8 * h->
mby;
476
480 dest_y, dest_cb, dest_cr, x_offset, y_offset,
481 qpix_op, chroma_op, mv);
482
483 qpix_op = qpix_avg;
484 chroma_op = chroma_avg;
485 }
486
490 dest_y, dest_cb, dest_cr, x_offset, y_offset,
492 }
493 }
494
496 {
504 } else {
529 }
530 }
531
532 /*****************************************************************************
533 *
534 * motion vector prediction
535 *
536 ****************************************************************************/
537
540 {
542
543 *d_x = (src->
x * distp * den + 256 +
FF_SIGNBIT(src->
x)) >> 9;
544 *d_y = (src->
y * distp * den + 256 +
FF_SIGNBIT(src->
y)) >> 9;
545 }
546
552 {
553 int ax, ay, bx, by, cx, cy;
554 int len_ab, len_bc, len_ca, len_mid;
555
556 /* scale candidates according to their temporal span */
560 /* find the geometrical median of the three candidates */
561 len_ab = abs(ax - bx) + abs(ay - by);
562 len_bc = abs(bx - cx) + abs(by - cy);
563 len_ca = abs(cx - ax) + abs(cy - ay);
564 len_mid =
mid_pred(len_ab, len_bc, len_ca);
565 if (len_mid == len_ab) {
568 } else if (len_mid == len_bc) {
571 } else {
574 }
575 }
576
579 {
585
589 mvC = &h->
mv[nP - 5];
// set to top-left (mvD)
593 (mvA->
x | mvA->
y | mvA->
ref) == 0 ||
594 (mvB->
x | mvB->
y | mvB->
ref) == 0)) {
596 /* if there is only one suitable candidate, take it */
597 }
else if (mvA->
ref >= 0 && mvB->
ref < 0 && mvC->
ref < 0) {
598 mvP2 = mvA;
599 }
else if (mvA->
ref < 0 && mvB->
ref >= 0 && mvC->
ref < 0) {
600 mvP2 = mvB;
601 }
else if (mvA->
ref < 0 && mvB->
ref < 0 && mvC->
ref >= 0) {
602 mvP2 = mvC;
604 mvP2 = mvA;
606 mvP2 = mvB;
608 mvP2 = mvC;
609 }
610 if (mvP2) {
613 } else
615
619 }
621 }
622
623 /*****************************************************************************
624 *
625 * macroblock level
626 *
627 ****************************************************************************/
628
629 /**
630 * initialise predictors for motion vectors and intra prediction
631 */
633 {
634 int i;
635
636 /* copy predictors from top line (MB B and C) into cache */
637 for (i = 0; i < 3; i++) {
640 }
643 /* clear top predictors if MB B is not available */
653 }
654 if (h->
mbx == h->
mb_width - 1)
// MB C not available
656 /* clear top-right predictors if MB C is not available */
660 }
661 /* clear top-left predictors if MB D is not available */
665 }
666 }
667
668 /**
669 * save predictors for later macroblocks and increase
670 * macroblock address
671 * @return 0 if end of frame is reached, 1 otherwise
672 */
674 {
675 int i;
676
681 /* copy mvs as predictors to the left */
682 for (i = 0; i <= 20; i += 4)
683 h->
mv[i] = h->
mv[i + 2];
684 /* copy bottom mvs from cache to top line */
689 /* next MB address */
694 /* clear left pred_modes */
696 /* clear left mv predictors */
697 for (i = 0; i <= 20; i += 4)
701 /* re-calculate sample pointers */
706 return 0;
707 }
708 }
709 return 1;
710 }
711
712 /*****************************************************************************
713 *
714 * frame level
715 *
716 ****************************************************************************/
717
719 {
720 int i;
721
722 /* clear some predictors */
723 for (i = 0; i <= 20; i += 4)
739
740 return 0;
741 }
742
743 /*****************************************************************************
744 *
745 * headers and interface
746 *
747 ****************************************************************************/
748
749 /**
750 * some predictions require data from the top-neighbouring macroblock.
751 * this data has to be stored for one complete row of macroblocks
752 * and this storage space is allocated here
753 */
755 {
756 /* alloc top line of predictors */
764
765 /* alloc space for co-located MVs and types */
770
785 }
786 return 0;
787 }
788
790 {
792
801
804
811 }
812
832 return 0;
833 }
834
836 {
838
842
854 return 0;
855 }