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.

317 lines
9.9KB

  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. void (*ff_put_pixels_clamped)(const int16_t *block, uint8_t *pixels, ptrdiff_t line_size);
  75. void (*ff_add_pixels_clamped)(const int16_t *block, uint8_t *pixels, ptrdiff_t line_size);
  76. static void put_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
  77. ptrdiff_t line_size)
  78. {
  79. int i;
  80. /* read the pixels */
  81. for (i = 0; i < 8; i++) {
  82. pixels[0] = av_clip_uint8(block[0]);
  83. pixels[1] = av_clip_uint8(block[1]);
  84. pixels[2] = av_clip_uint8(block[2]);
  85. pixels[3] = av_clip_uint8(block[3]);
  86. pixels[4] = av_clip_uint8(block[4]);
  87. pixels[5] = av_clip_uint8(block[5]);
  88. pixels[6] = av_clip_uint8(block[6]);
  89. pixels[7] = av_clip_uint8(block[7]);
  90. pixels += line_size;
  91. block += 8;
  92. }
  93. }
  94. static void put_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
  95. int line_size)
  96. {
  97. int i;
  98. /* read the pixels */
  99. for(i=0;i<4;i++) {
  100. pixels[0] = av_clip_uint8(block[0]);
  101. pixels[1] = av_clip_uint8(block[1]);
  102. pixels[2] = av_clip_uint8(block[2]);
  103. pixels[3] = av_clip_uint8(block[3]);
  104. pixels += line_size;
  105. block += 8;
  106. }
  107. }
  108. static void put_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
  109. int line_size)
  110. {
  111. int i;
  112. /* read the pixels */
  113. for(i=0;i<2;i++) {
  114. pixels[0] = av_clip_uint8(block[0]);
  115. pixels[1] = av_clip_uint8(block[1]);
  116. pixels += line_size;
  117. block += 8;
  118. }
  119. }
  120. static void put_signed_pixels_clamped_c(const int16_t *block,
  121. uint8_t *av_restrict pixels,
  122. ptrdiff_t line_size)
  123. {
  124. int i, j;
  125. for (i = 0; i < 8; i++) {
  126. for (j = 0; j < 8; j++) {
  127. if (*block < -128)
  128. *pixels = 0;
  129. else if (*block > 127)
  130. *pixels = 255;
  131. else
  132. *pixels = (uint8_t) (*block + 128);
  133. block++;
  134. pixels++;
  135. }
  136. pixels += (line_size - 8);
  137. }
  138. }
  139. static void add_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
  140. ptrdiff_t line_size)
  141. {
  142. int i;
  143. /* read the pixels */
  144. for (i = 0; i < 8; i++) {
  145. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  146. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  147. pixels[2] = av_clip_uint8(pixels[2] + block[2]);
  148. pixels[3] = av_clip_uint8(pixels[3] + block[3]);
  149. pixels[4] = av_clip_uint8(pixels[4] + block[4]);
  150. pixels[5] = av_clip_uint8(pixels[5] + block[5]);
  151. pixels[6] = av_clip_uint8(pixels[6] + block[6]);
  152. pixels[7] = av_clip_uint8(pixels[7] + block[7]);
  153. pixels += line_size;
  154. block += 8;
  155. }
  156. }
  157. static void add_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
  158. int line_size)
  159. {
  160. int i;
  161. /* read the pixels */
  162. for(i=0;i<4;i++) {
  163. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  164. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  165. pixels[2] = av_clip_uint8(pixels[2] + block[2]);
  166. pixels[3] = av_clip_uint8(pixels[3] + block[3]);
  167. pixels += line_size;
  168. block += 8;
  169. }
  170. }
  171. static void add_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
  172. int line_size)
  173. {
  174. int i;
  175. /* read the pixels */
  176. for(i=0;i<2;i++) {
  177. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  178. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  179. pixels += line_size;
  180. block += 8;
  181. }
  182. }
  183. static void ff_jref_idct4_put(uint8_t *dest, int line_size, int16_t *block)
  184. {
  185. ff_j_rev_dct4 (block);
  186. put_pixels_clamped4_c(block, dest, line_size);
  187. }
  188. static void ff_jref_idct4_add(uint8_t *dest, int line_size, int16_t *block)
  189. {
  190. ff_j_rev_dct4 (block);
  191. add_pixels_clamped4_c(block, dest, line_size);
  192. }
  193. static void ff_jref_idct2_put(uint8_t *dest, int line_size, int16_t *block)
  194. {
  195. ff_j_rev_dct2 (block);
  196. put_pixels_clamped2_c(block, dest, line_size);
  197. }
  198. static void ff_jref_idct2_add(uint8_t *dest, int line_size, int16_t *block)
  199. {
  200. ff_j_rev_dct2 (block);
  201. add_pixels_clamped2_c(block, dest, line_size);
  202. }
  203. static void ff_jref_idct1_put(uint8_t *dest, int line_size, int16_t *block)
  204. {
  205. dest[0] = av_clip_uint8((block[0] + 4)>>3);
  206. }
  207. static void ff_jref_idct1_add(uint8_t *dest, int line_size, int16_t *block)
  208. {
  209. dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
  210. }
  211. av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
  212. {
  213. const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
  214. if (avctx->lowres==1) {
  215. c->idct_put = ff_jref_idct4_put;
  216. c->idct_add = ff_jref_idct4_add;
  217. c->idct = ff_j_rev_dct4;
  218. c->perm_type = FF_IDCT_PERM_NONE;
  219. } else if (avctx->lowres==2) {
  220. c->idct_put = ff_jref_idct2_put;
  221. c->idct_add = ff_jref_idct2_add;
  222. c->idct = ff_j_rev_dct2;
  223. c->perm_type = FF_IDCT_PERM_NONE;
  224. } else if (avctx->lowres==3) {
  225. c->idct_put = ff_jref_idct1_put;
  226. c->idct_add = ff_jref_idct1_add;
  227. c->idct = ff_j_rev_dct1;
  228. c->perm_type = FF_IDCT_PERM_NONE;
  229. } else {
  230. if (avctx->bits_per_raw_sample == 10 || avctx->bits_per_raw_sample == 9) {
  231. c->idct_put = ff_simple_idct_put_10;
  232. c->idct_add = ff_simple_idct_add_10;
  233. c->idct = ff_simple_idct_10;
  234. c->perm_type = FF_IDCT_PERM_NONE;
  235. } else if (avctx->bits_per_raw_sample == 12) {
  236. c->idct_put = ff_simple_idct_put_12;
  237. c->idct_add = ff_simple_idct_add_12;
  238. c->idct = ff_simple_idct_12;
  239. c->perm_type = FF_IDCT_PERM_NONE;
  240. } else {
  241. if (avctx->idct_algo == FF_IDCT_INT) {
  242. c->idct_put = ff_jref_idct_put;
  243. c->idct_add = ff_jref_idct_add;
  244. c->idct = ff_j_rev_dct;
  245. c->perm_type = FF_IDCT_PERM_LIBMPEG2;
  246. #if CONFIG_FAANIDCT
  247. } else if (avctx->idct_algo == FF_IDCT_FAAN) {
  248. c->idct_put = ff_faanidct_put;
  249. c->idct_add = ff_faanidct_add;
  250. c->idct = ff_faanidct;
  251. c->perm_type = FF_IDCT_PERM_NONE;
  252. #endif /* CONFIG_FAANIDCT */
  253. } else { // accurate/default
  254. c->idct_put = ff_simple_idct_put_8;
  255. c->idct_add = ff_simple_idct_add_8;
  256. c->idct = ff_simple_idct_8;
  257. c->perm_type = FF_IDCT_PERM_NONE;
  258. }
  259. }
  260. }
  261. c->put_pixels_clamped = put_pixels_clamped_c;
  262. c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
  263. c->add_pixels_clamped = add_pixels_clamped_c;
  264. if (CONFIG_MPEG4_DECODER && avctx->idct_algo == FF_IDCT_XVID)
  265. ff_xvid_idct_init(c, avctx);
  266. if (ARCH_ALPHA)
  267. ff_idctdsp_init_alpha(c, avctx, high_bit_depth);
  268. if (ARCH_ARM)
  269. ff_idctdsp_init_arm(c, avctx, high_bit_depth);
  270. if (ARCH_PPC)
  271. ff_idctdsp_init_ppc(c, avctx, high_bit_depth);
  272. if (ARCH_X86)
  273. ff_idctdsp_init_x86(c, avctx, high_bit_depth);
  274. if (ARCH_MIPS)
  275. ff_idctdsp_init_mips(c, avctx, high_bit_depth);
  276. ff_put_pixels_clamped = c->put_pixels_clamped;
  277. ff_add_pixels_clamped = c->add_pixels_clamped;
  278. ff_init_scantable_permutation(c->idct_permutation,
  279. c->perm_type);
  280. }