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.

277 lines
8.5KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. #include "xvididct.h"
  27. av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st,
  28. const uint8_t *src_scantable)
  29. {
  30. int i, end;
  31. st->scantable = src_scantable;
  32. for (i = 0; i < 64; i++) {
  33. int j = src_scantable[i];
  34. st->permutated[i] = permutation[j];
  35. }
  36. end = -1;
  37. for (i = 0; i < 64; i++) {
  38. int j = st->permutated[i];
  39. if (j > end)
  40. end = j;
  41. st->raster_end[i] = end;
  42. }
  43. }
  44. av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
  45. enum idct_permutation_type perm_type)
  46. {
  47. int i;
  48. if (ARCH_X86)
  49. if (ff_init_scantable_permutation_x86(idct_permutation,
  50. perm_type))
  51. return;
  52. switch (perm_type) {
  53. case FF_IDCT_PERM_NONE:
  54. for (i = 0; i < 64; i++)
  55. idct_permutation[i] = i;
  56. break;
  57. case FF_IDCT_PERM_LIBMPEG2:
  58. for (i = 0; i < 64; i++)
  59. idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
  60. break;
  61. case FF_IDCT_PERM_TRANSPOSE:
  62. for (i = 0; i < 64; i++)
  63. idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
  64. break;
  65. case FF_IDCT_PERM_PARTTRANS:
  66. for (i = 0; i < 64; i++)
  67. idct_permutation[i] = (i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3);
  68. break;
  69. default:
  70. av_log(NULL, AV_LOG_ERROR,
  71. "Internal error, IDCT permutation not set\n");
  72. }
  73. }
  74. static void put_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
  75. int line_size)
  76. {
  77. int i;
  78. /* read the pixels */
  79. for(i=0;i<4;i++) {
  80. pixels[0] = av_clip_uint8(block[0]);
  81. pixels[1] = av_clip_uint8(block[1]);
  82. pixels[2] = av_clip_uint8(block[2]);
  83. pixels[3] = av_clip_uint8(block[3]);
  84. pixels += line_size;
  85. block += 8;
  86. }
  87. }
  88. static void put_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
  89. int line_size)
  90. {
  91. int i;
  92. /* read the pixels */
  93. for(i=0;i<2;i++) {
  94. pixels[0] = av_clip_uint8(block[0]);
  95. pixels[1] = av_clip_uint8(block[1]);
  96. pixels += line_size;
  97. block += 8;
  98. }
  99. }
  100. static void put_signed_pixels_clamped_c(const int16_t *block,
  101. uint8_t *av_restrict pixels,
  102. int line_size)
  103. {
  104. int i, j;
  105. for (i = 0; i < 8; i++) {
  106. for (j = 0; j < 8; j++) {
  107. if (*block < -128)
  108. *pixels = 0;
  109. else if (*block > 127)
  110. *pixels = 255;
  111. else
  112. *pixels = (uint8_t) (*block + 128);
  113. block++;
  114. pixels++;
  115. }
  116. pixels += (line_size - 8);
  117. }
  118. }
  119. static void add_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
  120. int line_size)
  121. {
  122. int i;
  123. /* read the pixels */
  124. for(i=0;i<4;i++) {
  125. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  126. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  127. pixels[2] = av_clip_uint8(pixels[2] + block[2]);
  128. pixels[3] = av_clip_uint8(pixels[3] + block[3]);
  129. pixels += line_size;
  130. block += 8;
  131. }
  132. }
  133. static void add_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
  134. int line_size)
  135. {
  136. int i;
  137. /* read the pixels */
  138. for(i=0;i<2;i++) {
  139. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  140. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  141. pixels += line_size;
  142. block += 8;
  143. }
  144. }
  145. static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
  146. {
  147. ff_j_rev_dct(block);
  148. put_pixels_clamped_c(block, dest, line_size);
  149. }
  150. static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
  151. {
  152. ff_j_rev_dct(block);
  153. add_pixels_clamped_c(block, dest, line_size);
  154. }
  155. static void ff_jref_idct4_put(uint8_t *dest, int line_size, int16_t *block)
  156. {
  157. ff_j_rev_dct4 (block);
  158. put_pixels_clamped4_c(block, dest, line_size);
  159. }
  160. static void ff_jref_idct4_add(uint8_t *dest, int line_size, int16_t *block)
  161. {
  162. ff_j_rev_dct4 (block);
  163. add_pixels_clamped4_c(block, dest, line_size);
  164. }
  165. static void ff_jref_idct2_put(uint8_t *dest, int line_size, int16_t *block)
  166. {
  167. ff_j_rev_dct2 (block);
  168. put_pixels_clamped2_c(block, dest, line_size);
  169. }
  170. static void ff_jref_idct2_add(uint8_t *dest, int line_size, int16_t *block)
  171. {
  172. ff_j_rev_dct2 (block);
  173. add_pixels_clamped2_c(block, dest, line_size);
  174. }
  175. static void ff_jref_idct1_put(uint8_t *dest, int line_size, int16_t *block)
  176. {
  177. dest[0] = av_clip_uint8((block[0] + 4)>>3);
  178. }
  179. static void ff_jref_idct1_add(uint8_t *dest, int line_size, int16_t *block)
  180. {
  181. dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
  182. }
  183. av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
  184. {
  185. const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
  186. if (avctx->lowres==1) {
  187. c->idct_put = ff_jref_idct4_put;
  188. c->idct_add = ff_jref_idct4_add;
  189. c->idct = ff_j_rev_dct4;
  190. c->perm_type = FF_IDCT_PERM_NONE;
  191. } else if (avctx->lowres==2) {
  192. c->idct_put = ff_jref_idct2_put;
  193. c->idct_add = ff_jref_idct2_add;
  194. c->idct = ff_j_rev_dct2;
  195. c->perm_type = FF_IDCT_PERM_NONE;
  196. } else if (avctx->lowres==3) {
  197. c->idct_put = ff_jref_idct1_put;
  198. c->idct_add = ff_jref_idct1_add;
  199. c->idct = ff_j_rev_dct1;
  200. c->perm_type = FF_IDCT_PERM_NONE;
  201. } else {
  202. if (avctx->bits_per_raw_sample == 10) {
  203. c->idct_put = ff_simple_idct_put_10;
  204. c->idct_add = ff_simple_idct_add_10;
  205. c->idct = ff_simple_idct_10;
  206. c->perm_type = FF_IDCT_PERM_NONE;
  207. } else if (avctx->bits_per_raw_sample == 12) {
  208. c->idct_put = ff_simple_idct_put_12;
  209. c->idct_add = ff_simple_idct_add_12;
  210. c->idct = ff_simple_idct_12;
  211. c->perm_type = FF_IDCT_PERM_NONE;
  212. } else {
  213. if (avctx->idct_algo == FF_IDCT_INT) {
  214. c->idct_put = jref_idct_put;
  215. c->idct_add = jref_idct_add;
  216. c->idct = ff_j_rev_dct;
  217. c->perm_type = FF_IDCT_PERM_LIBMPEG2;
  218. } else if (avctx->idct_algo == FF_IDCT_FAAN) {
  219. c->idct_put = ff_faanidct_put;
  220. c->idct_add = ff_faanidct_add;
  221. c->idct = ff_faanidct;
  222. c->perm_type = FF_IDCT_PERM_NONE;
  223. } else { // accurate/default
  224. c->idct_put = ff_simple_idct_put_8;
  225. c->idct_add = ff_simple_idct_add_8;
  226. c->idct = ff_simple_idct_8;
  227. c->perm_type = FF_IDCT_PERM_NONE;
  228. }
  229. }
  230. }
  231. c->put_pixels_clamped = put_pixels_clamped_c;
  232. c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
  233. c->add_pixels_clamped = add_pixels_clamped_c;
  234. if (CONFIG_MPEG4_DECODER && avctx->idct_algo == FF_IDCT_XVID)
  235. ff_xvididct_init(c, avctx);
  236. if (ARCH_ALPHA)
  237. ff_idctdsp_init_alpha(c, avctx, high_bit_depth);
  238. if (ARCH_ARM)
  239. ff_idctdsp_init_arm(c, avctx, high_bit_depth);
  240. if (ARCH_PPC)
  241. ff_idctdsp_init_ppc(c, avctx, high_bit_depth);
  242. if (ARCH_X86)
  243. ff_idctdsp_init_x86(c, avctx, high_bit_depth);
  244. ff_init_scantable_permutation(c->idct_permutation,
  245. c->perm_type);
  246. }