You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.1KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include "libavutil/attributes.h"
  20. #include "libavutil/common.h"
  21. #include "avcodec.h"
  22. #include "dct.h"
  23. #include "faanidct.h"
  24. #include "idctdsp.h"
  25. #include "simple_idct.h"
  26. av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st,
  27. const uint8_t *src_scantable)
  28. {
  29. int i, end;
  30. st->scantable = src_scantable;
  31. for (i = 0; i < 64; i++) {
  32. int j = src_scantable[i];
  33. st->permutated[i] = permutation[j];
  34. }
  35. end = -1;
  36. for (i = 0; i < 64; i++) {
  37. int j = st->permutated[i];
  38. if (j > end)
  39. end = j;
  40. st->raster_end[i] = end;
  41. }
  42. }
  43. av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
  44. enum idct_permutation_type perm_type)
  45. {
  46. int i;
  47. if (ARCH_X86)
  48. if (ff_init_scantable_permutation_x86(idct_permutation,
  49. perm_type))
  50. return;
  51. switch (perm_type) {
  52. case FF_IDCT_PERM_NONE:
  53. for (i = 0; i < 64; i++)
  54. idct_permutation[i] = i;
  55. break;
  56. case FF_IDCT_PERM_LIBMPEG2:
  57. for (i = 0; i < 64; i++)
  58. idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
  59. break;
  60. case FF_IDCT_PERM_TRANSPOSE:
  61. for (i = 0; i < 64; i++)
  62. idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
  63. break;
  64. case FF_IDCT_PERM_PARTTRANS:
  65. for (i = 0; i < 64; i++)
  66. idct_permutation[i] = (i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3);
  67. break;
  68. default:
  69. av_log(NULL, AV_LOG_ERROR,
  70. "Internal error, IDCT permutation not set\n");
  71. }
  72. }
  73. static void put_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
  74. int line_size)
  75. {
  76. int i;
  77. /* read the pixels */
  78. for (i = 0; i < 8; i++) {
  79. pixels[0] = av_clip_uint8(block[0]);
  80. pixels[1] = av_clip_uint8(block[1]);
  81. pixels[2] = av_clip_uint8(block[2]);
  82. pixels[3] = av_clip_uint8(block[3]);
  83. pixels[4] = av_clip_uint8(block[4]);
  84. pixels[5] = av_clip_uint8(block[5]);
  85. pixels[6] = av_clip_uint8(block[6]);
  86. pixels[7] = av_clip_uint8(block[7]);
  87. pixels += line_size;
  88. block += 8;
  89. }
  90. }
  91. static void put_signed_pixels_clamped_c(const int16_t *block,
  92. uint8_t *restrict pixels,
  93. int line_size)
  94. {
  95. int i, j;
  96. for (i = 0; i < 8; i++) {
  97. for (j = 0; j < 8; j++) {
  98. if (*block < -128)
  99. *pixels = 0;
  100. else if (*block > 127)
  101. *pixels = 255;
  102. else
  103. *pixels = (uint8_t) (*block + 128);
  104. block++;
  105. pixels++;
  106. }
  107. pixels += (line_size - 8);
  108. }
  109. }
  110. static void add_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
  111. int line_size)
  112. {
  113. int i;
  114. /* read the pixels */
  115. for (i = 0; i < 8; i++) {
  116. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  117. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  118. pixels[2] = av_clip_uint8(pixels[2] + block[2]);
  119. pixels[3] = av_clip_uint8(pixels[3] + block[3]);
  120. pixels[4] = av_clip_uint8(pixels[4] + block[4]);
  121. pixels[5] = av_clip_uint8(pixels[5] + block[5]);
  122. pixels[6] = av_clip_uint8(pixels[6] + block[6]);
  123. pixels[7] = av_clip_uint8(pixels[7] + block[7]);
  124. pixels += line_size;
  125. block += 8;
  126. }
  127. }
  128. static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
  129. {
  130. ff_j_rev_dct(block);
  131. put_pixels_clamped_c(block, dest, line_size);
  132. }
  133. static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
  134. {
  135. ff_j_rev_dct(block);
  136. add_pixels_clamped_c(block, dest, line_size);
  137. }
  138. av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
  139. {
  140. const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
  141. if (avctx->bits_per_raw_sample == 10) {
  142. c->idct_put = ff_simple_idct_put_10;
  143. c->idct_add = ff_simple_idct_add_10;
  144. c->idct = ff_simple_idct_10;
  145. c->perm_type = FF_IDCT_PERM_NONE;
  146. } else {
  147. if (avctx->idct_algo == FF_IDCT_INT) {
  148. c->idct_put = jref_idct_put;
  149. c->idct_add = jref_idct_add;
  150. c->idct = ff_j_rev_dct;
  151. c->perm_type = FF_IDCT_PERM_LIBMPEG2;
  152. } else if (avctx->idct_algo == FF_IDCT_FAAN) {
  153. c->idct_put = ff_faanidct_put;
  154. c->idct_add = ff_faanidct_add;
  155. c->idct = ff_faanidct;
  156. c->perm_type = FF_IDCT_PERM_NONE;
  157. } else { // accurate/default
  158. c->idct_put = ff_simple_idct_put_8;
  159. c->idct_add = ff_simple_idct_add_8;
  160. c->idct = ff_simple_idct_8;
  161. c->perm_type = FF_IDCT_PERM_NONE;
  162. }
  163. }
  164. c->put_pixels_clamped = put_pixels_clamped_c;
  165. c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
  166. c->add_pixels_clamped = add_pixels_clamped_c;
  167. if (ARCH_ARM)
  168. ff_idctdsp_init_arm(c, avctx, high_bit_depth);
  169. if (ARCH_PPC)
  170. ff_idctdsp_init_ppc(c, avctx, high_bit_depth);
  171. if (ARCH_X86)
  172. ff_idctdsp_init_x86(c, avctx, high_bit_depth);
  173. ff_init_scantable_permutation(c->idct_permutation,
  174. c->perm_type);
  175. }