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.

312 lines
9.8KB

  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. 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. int idct_permutation_type)
  45. {
  46. int i;
  47. if (ARCH_X86)
  48. if (ff_init_scantable_permutation_x86(idct_permutation,
  49. idct_permutation_type))
  50. return;
  51. switch (idct_permutation_type) {
  52. case FF_NO_IDCT_PERM:
  53. for (i = 0; i < 64; i++)
  54. idct_permutation[i] = i;
  55. break;
  56. case FF_LIBMPEG2_IDCT_PERM:
  57. for (i = 0; i < 64; i++)
  58. idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
  59. break;
  60. case FF_TRANSPOSE_IDCT_PERM:
  61. for (i = 0; i < 64; i++)
  62. idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
  63. break;
  64. case FF_PARTTRANS_IDCT_PERM:
  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 *av_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_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
  92. int line_size)
  93. {
  94. int i;
  95. /* read the pixels */
  96. for(i=0;i<4;i++) {
  97. pixels[0] = av_clip_uint8(block[0]);
  98. pixels[1] = av_clip_uint8(block[1]);
  99. pixels[2] = av_clip_uint8(block[2]);
  100. pixels[3] = av_clip_uint8(block[3]);
  101. pixels += line_size;
  102. block += 8;
  103. }
  104. }
  105. static void put_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
  106. int line_size)
  107. {
  108. int i;
  109. /* read the pixels */
  110. for(i=0;i<2;i++) {
  111. pixels[0] = av_clip_uint8(block[0]);
  112. pixels[1] = av_clip_uint8(block[1]);
  113. pixels += line_size;
  114. block += 8;
  115. }
  116. }
  117. static void put_signed_pixels_clamped_c(const int16_t *block,
  118. uint8_t *av_restrict pixels,
  119. int line_size)
  120. {
  121. int i, j;
  122. for (i = 0; i < 8; i++) {
  123. for (j = 0; j < 8; j++) {
  124. if (*block < -128)
  125. *pixels = 0;
  126. else if (*block > 127)
  127. *pixels = 255;
  128. else
  129. *pixels = (uint8_t) (*block + 128);
  130. block++;
  131. pixels++;
  132. }
  133. pixels += (line_size - 8);
  134. }
  135. }
  136. static void add_pixels_clamped_c(const int16_t *block, uint8_t *av_restrict pixels,
  137. int line_size)
  138. {
  139. int i;
  140. /* read the pixels */
  141. for (i = 0; i < 8; i++) {
  142. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  143. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  144. pixels[2] = av_clip_uint8(pixels[2] + block[2]);
  145. pixels[3] = av_clip_uint8(pixels[3] + block[3]);
  146. pixels[4] = av_clip_uint8(pixels[4] + block[4]);
  147. pixels[5] = av_clip_uint8(pixels[5] + block[5]);
  148. pixels[6] = av_clip_uint8(pixels[6] + block[6]);
  149. pixels[7] = av_clip_uint8(pixels[7] + block[7]);
  150. pixels += line_size;
  151. block += 8;
  152. }
  153. }
  154. static void add_pixels_clamped4_c(const int16_t *block, uint8_t *av_restrict pixels,
  155. int line_size)
  156. {
  157. int i;
  158. /* read the pixels */
  159. for(i=0;i<4;i++) {
  160. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  161. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  162. pixels[2] = av_clip_uint8(pixels[2] + block[2]);
  163. pixels[3] = av_clip_uint8(pixels[3] + block[3]);
  164. pixels += line_size;
  165. block += 8;
  166. }
  167. }
  168. static void add_pixels_clamped2_c(const int16_t *block, uint8_t *av_restrict pixels,
  169. int line_size)
  170. {
  171. int i;
  172. /* read the pixels */
  173. for(i=0;i<2;i++) {
  174. pixels[0] = av_clip_uint8(pixels[0] + block[0]);
  175. pixels[1] = av_clip_uint8(pixels[1] + block[1]);
  176. pixels += line_size;
  177. block += 8;
  178. }
  179. }
  180. static void jref_idct_put(uint8_t *dest, int line_size, int16_t *block)
  181. {
  182. ff_j_rev_dct(block);
  183. put_pixels_clamped_c(block, dest, line_size);
  184. }
  185. static void jref_idct_add(uint8_t *dest, int line_size, int16_t *block)
  186. {
  187. ff_j_rev_dct(block);
  188. add_pixels_clamped_c(block, dest, line_size);
  189. }
  190. static void ff_jref_idct4_put(uint8_t *dest, int line_size, int16_t *block)
  191. {
  192. ff_j_rev_dct4 (block);
  193. put_pixels_clamped4_c(block, dest, line_size);
  194. }
  195. static void ff_jref_idct4_add(uint8_t *dest, int line_size, int16_t *block)
  196. {
  197. ff_j_rev_dct4 (block);
  198. add_pixels_clamped4_c(block, dest, line_size);
  199. }
  200. static void ff_jref_idct2_put(uint8_t *dest, int line_size, int16_t *block)
  201. {
  202. ff_j_rev_dct2 (block);
  203. put_pixels_clamped2_c(block, dest, line_size);
  204. }
  205. static void ff_jref_idct2_add(uint8_t *dest, int line_size, int16_t *block)
  206. {
  207. ff_j_rev_dct2 (block);
  208. add_pixels_clamped2_c(block, dest, line_size);
  209. }
  210. static void ff_jref_idct1_put(uint8_t *dest, int line_size, int16_t *block)
  211. {
  212. dest[0] = av_clip_uint8((block[0] + 4)>>3);
  213. }
  214. static void ff_jref_idct1_add(uint8_t *dest, int line_size, int16_t *block)
  215. {
  216. dest[0] = av_clip_uint8(dest[0] + ((block[0] + 4)>>3));
  217. }
  218. av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
  219. {
  220. const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
  221. if (avctx->lowres==1) {
  222. c->idct_put = ff_jref_idct4_put;
  223. c->idct_add = ff_jref_idct4_add;
  224. c->idct = ff_j_rev_dct4;
  225. c->idct_permutation_type = FF_NO_IDCT_PERM;
  226. } else if (avctx->lowres==2) {
  227. c->idct_put = ff_jref_idct2_put;
  228. c->idct_add = ff_jref_idct2_add;
  229. c->idct = ff_j_rev_dct2;
  230. c->idct_permutation_type = FF_NO_IDCT_PERM;
  231. } else if (avctx->lowres==3) {
  232. c->idct_put = ff_jref_idct1_put;
  233. c->idct_add = ff_jref_idct1_add;
  234. c->idct = ff_j_rev_dct1;
  235. c->idct_permutation_type = FF_NO_IDCT_PERM;
  236. } else {
  237. if (avctx->bits_per_raw_sample == 10) {
  238. c->idct_put = ff_simple_idct_put_10;
  239. c->idct_add = ff_simple_idct_add_10;
  240. c->idct = ff_simple_idct_10;
  241. c->idct_permutation_type = FF_NO_IDCT_PERM;
  242. } else if (avctx->bits_per_raw_sample == 12) {
  243. c->idct_put = ff_simple_idct_put_12;
  244. c->idct_add = ff_simple_idct_add_12;
  245. c->idct = ff_simple_idct_12;
  246. c->idct_permutation_type = FF_NO_IDCT_PERM;
  247. } else {
  248. if (avctx->idct_algo == FF_IDCT_INT) {
  249. c->idct_put = jref_idct_put;
  250. c->idct_add = jref_idct_add;
  251. c->idct = ff_j_rev_dct;
  252. c->idct_permutation_type = FF_LIBMPEG2_IDCT_PERM;
  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->idct_permutation_type = FF_NO_IDCT_PERM;
  258. } else { // accurate/default
  259. c->idct_put = ff_simple_idct_put_8;
  260. c->idct_add = ff_simple_idct_add_8;
  261. c->idct = ff_simple_idct_8;
  262. c->idct_permutation_type = FF_NO_IDCT_PERM;
  263. }
  264. }
  265. }
  266. c->put_pixels_clamped = put_pixels_clamped_c;
  267. c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
  268. c->add_pixels_clamped = add_pixels_clamped_c;
  269. if (ARCH_ARM)
  270. ff_idctdsp_init_arm(c, avctx, high_bit_depth);
  271. if (ARCH_PPC)
  272. ff_idctdsp_init_ppc(c, avctx, high_bit_depth);
  273. if (ARCH_X86)
  274. ff_idctdsp_init_x86(c, avctx, high_bit_depth);
  275. ff_init_scantable_permutation(c->idct_permutation,
  276. c->idct_permutation_type);
  277. }