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.

322 lines
10KB

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