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.

373 lines
17KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "svq3.h"
  22. #undef FUNC
  23. #undef PIXEL_SHIFT
  24. #if SIMPLE
  25. # define FUNC(n) AV_JOIN(n ## _simple_, BITS)
  26. # define PIXEL_SHIFT (BITS >> 4)
  27. #else
  28. # define FUNC(n) n ## _complex
  29. # define PIXEL_SHIFT h->pixel_shift
  30. #endif
  31. #undef CHROMA_IDC
  32. #define CHROMA_IDC 1
  33. #include "h264_mc_template.c"
  34. #undef CHROMA_IDC
  35. #define CHROMA_IDC 2
  36. #include "h264_mc_template.c"
  37. static av_noinline void FUNC(hl_decode_mb)(const H264Context *h, H264SliceContext *sl)
  38. {
  39. const int mb_x = sl->mb_x;
  40. const int mb_y = sl->mb_y;
  41. const int mb_xy = sl->mb_xy;
  42. const int mb_type = h->cur_pic.mb_type[mb_xy];
  43. uint8_t *dest_y, *dest_cb, *dest_cr;
  44. int linesize, uvlinesize /*dct_offset*/;
  45. int i, j;
  46. const int *block_offset = &h->block_offset[0];
  47. const int transform_bypass = !SIMPLE && (sl->qscale == 0 && h->sps.transform_bypass);
  48. /* is_h264 should always be true if SVQ3 is disabled. */
  49. const int is_h264 = !CONFIG_SVQ3_DECODER || SIMPLE || h->avctx->codec_id == AV_CODEC_ID_H264;
  50. void (*idct_add)(uint8_t *dst, int16_t *block, int stride);
  51. const int block_h = 16 >> h->chroma_y_shift;
  52. const int chroma422 = CHROMA422(h);
  53. dest_y = h->cur_pic.f->data[0] + ((mb_x << PIXEL_SHIFT) + mb_y * sl->linesize) * 16;
  54. dest_cb = h->cur_pic.f->data[1] + (mb_x << PIXEL_SHIFT) * 8 + mb_y * sl->uvlinesize * block_h;
  55. dest_cr = h->cur_pic.f->data[2] + (mb_x << PIXEL_SHIFT) * 8 + mb_y * sl->uvlinesize * block_h;
  56. h->vdsp.prefetch(dest_y + (sl->mb_x & 3) * 4 * sl->linesize + (64 << PIXEL_SHIFT), sl->linesize, 4);
  57. h->vdsp.prefetch(dest_cb + (sl->mb_x & 7) * sl->uvlinesize + (64 << PIXEL_SHIFT), dest_cr - dest_cb, 2);
  58. h->list_counts[mb_xy] = sl->list_count;
  59. if (!SIMPLE && MB_FIELD(sl)) {
  60. linesize = sl->mb_linesize = sl->linesize * 2;
  61. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize * 2;
  62. block_offset = &h->block_offset[48];
  63. if (mb_y & 1) { // FIXME move out of this function?
  64. dest_y -= sl->linesize * 15;
  65. dest_cb -= sl->uvlinesize * (block_h - 1);
  66. dest_cr -= sl->uvlinesize * (block_h - 1);
  67. }
  68. if (FRAME_MBAFF(h)) {
  69. int list;
  70. for (list = 0; list < sl->list_count; list++) {
  71. if (!USES_LIST(mb_type, list))
  72. continue;
  73. if (IS_16X16(mb_type)) {
  74. int8_t *ref = &sl->ref_cache[list][scan8[0]];
  75. fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (sl->mb_y & 1), 1);
  76. } else {
  77. for (i = 0; i < 16; i += 4) {
  78. int ref = sl->ref_cache[list][scan8[i]];
  79. if (ref >= 0)
  80. fill_rectangle(&sl->ref_cache[list][scan8[i]], 2, 2,
  81. 8, (16 + ref) ^ (sl->mb_y & 1), 1);
  82. }
  83. }
  84. }
  85. }
  86. } else {
  87. linesize = sl->mb_linesize = sl->linesize;
  88. uvlinesize = sl->mb_uvlinesize = sl->uvlinesize;
  89. // dct_offset = s->linesize * 16;
  90. }
  91. if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
  92. if (PIXEL_SHIFT) {
  93. const int bit_depth = h->sps.bit_depth_luma;
  94. int j;
  95. GetBitContext gb;
  96. init_get_bits(&gb, sl->intra_pcm_ptr,
  97. ff_h264_mb_sizes[h->sps.chroma_format_idc] * bit_depth);
  98. for (i = 0; i < 16; i++) {
  99. uint16_t *tmp_y = (uint16_t *)(dest_y + i * linesize);
  100. for (j = 0; j < 16; j++)
  101. tmp_y[j] = get_bits(&gb, bit_depth);
  102. }
  103. if (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
  104. if (!h->sps.chroma_format_idc) {
  105. for (i = 0; i < block_h; i++) {
  106. uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
  107. for (j = 0; j < 8; j++)
  108. tmp_cb[j] = 1 << (bit_depth - 1);
  109. }
  110. for (i = 0; i < block_h; i++) {
  111. uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
  112. for (j = 0; j < 8; j++)
  113. tmp_cr[j] = 1 << (bit_depth - 1);
  114. }
  115. } else {
  116. for (i = 0; i < block_h; i++) {
  117. uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
  118. for (j = 0; j < 8; j++)
  119. tmp_cb[j] = get_bits(&gb, bit_depth);
  120. }
  121. for (i = 0; i < block_h; i++) {
  122. uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
  123. for (j = 0; j < 8; j++)
  124. tmp_cr[j] = get_bits(&gb, bit_depth);
  125. }
  126. }
  127. }
  128. } else {
  129. for (i = 0; i < 16; i++)
  130. memcpy(dest_y + i * linesize, sl->intra_pcm_ptr + i * 16, 16);
  131. if (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
  132. if (!h->sps.chroma_format_idc) {
  133. for (i = 0; i < block_h; i++) {
  134. memset(dest_cb + i * uvlinesize, 128, 8);
  135. memset(dest_cr + i * uvlinesize, 128, 8);
  136. }
  137. } else {
  138. const uint8_t *src_cb = sl->intra_pcm_ptr + 256;
  139. const uint8_t *src_cr = sl->intra_pcm_ptr + 256 + block_h * 8;
  140. for (i = 0; i < block_h; i++) {
  141. memcpy(dest_cb + i * uvlinesize, src_cb + i * 8, 8);
  142. memcpy(dest_cr + i * uvlinesize, src_cr + i * 8, 8);
  143. }
  144. }
  145. }
  146. }
  147. } else {
  148. if (IS_INTRA(mb_type)) {
  149. if (sl->deblocking_filter)
  150. xchg_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
  151. uvlinesize, 1, 0, SIMPLE, PIXEL_SHIFT);
  152. if (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) {
  153. h->hpc.pred8x8[sl->chroma_pred_mode](dest_cb, uvlinesize);
  154. h->hpc.pred8x8[sl->chroma_pred_mode](dest_cr, uvlinesize);
  155. }
  156. hl_decode_mb_predict_luma(h, sl, mb_type, is_h264, SIMPLE,
  157. transform_bypass, PIXEL_SHIFT,
  158. block_offset, linesize, dest_y, 0);
  159. if (sl->deblocking_filter)
  160. xchg_mb_border(h, sl, dest_y, dest_cb, dest_cr, linesize,
  161. uvlinesize, 0, 0, SIMPLE, PIXEL_SHIFT);
  162. } else if (is_h264) {
  163. if (chroma422) {
  164. FUNC(hl_motion_422)(h, sl, dest_y, dest_cb, dest_cr,
  165. h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
  166. h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
  167. h->h264dsp.weight_h264_pixels_tab,
  168. h->h264dsp.biweight_h264_pixels_tab);
  169. } else {
  170. FUNC(hl_motion_420)(h, sl, dest_y, dest_cb, dest_cr,
  171. h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
  172. h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
  173. h->h264dsp.weight_h264_pixels_tab,
  174. h->h264dsp.biweight_h264_pixels_tab);
  175. }
  176. }
  177. hl_decode_mb_idct_luma(h, sl, mb_type, is_h264, SIMPLE, transform_bypass,
  178. PIXEL_SHIFT, block_offset, linesize, dest_y, 0);
  179. if ((SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) &&
  180. (sl->cbp & 0x30)) {
  181. uint8_t *dest[2] = { dest_cb, dest_cr };
  182. if (transform_bypass) {
  183. if (IS_INTRA(mb_type) && h->sps.profile_idc == 244 &&
  184. (sl->chroma_pred_mode == VERT_PRED8x8 ||
  185. sl->chroma_pred_mode == HOR_PRED8x8)) {
  186. h->hpc.pred8x8_add[sl->chroma_pred_mode](dest[0],
  187. block_offset + 16,
  188. sl->mb + (16 * 16 * 1 << PIXEL_SHIFT),
  189. uvlinesize);
  190. h->hpc.pred8x8_add[sl->chroma_pred_mode](dest[1],
  191. block_offset + 32,
  192. sl->mb + (16 * 16 * 2 << PIXEL_SHIFT),
  193. uvlinesize);
  194. } else {
  195. idct_add = h->h264dsp.h264_add_pixels4_clear;
  196. for (j = 1; j < 3; j++) {
  197. for (i = j * 16; i < j * 16 + 4; i++)
  198. if (sl->non_zero_count_cache[scan8[i]] ||
  199. dctcoef_get(sl->mb, PIXEL_SHIFT, i * 16))
  200. idct_add(dest[j - 1] + block_offset[i],
  201. sl->mb + (i * 16 << PIXEL_SHIFT),
  202. uvlinesize);
  203. if (chroma422) {
  204. for (i = j * 16 + 4; i < j * 16 + 8; i++)
  205. if (sl->non_zero_count_cache[scan8[i + 4]] ||
  206. dctcoef_get(sl->mb, PIXEL_SHIFT, i * 16))
  207. idct_add(dest[j - 1] + block_offset[i + 4],
  208. sl->mb + (i * 16 << PIXEL_SHIFT),
  209. uvlinesize);
  210. }
  211. }
  212. }
  213. } else {
  214. if (is_h264) {
  215. int qp[2];
  216. if (chroma422) {
  217. qp[0] = sl->chroma_qp[0] + 3;
  218. qp[1] = sl->chroma_qp[1] + 3;
  219. } else {
  220. qp[0] = sl->chroma_qp[0];
  221. qp[1] = sl->chroma_qp[1];
  222. }
  223. if (sl->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 0]])
  224. h->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + (16 * 16 * 1 << PIXEL_SHIFT),
  225. h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][qp[0]][0]);
  226. if (sl->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 1]])
  227. h->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + (16 * 16 * 2 << PIXEL_SHIFT),
  228. h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][qp[1]][0]);
  229. h->h264dsp.h264_idct_add8(dest, block_offset,
  230. sl->mb, uvlinesize,
  231. sl->non_zero_count_cache);
  232. } else if (CONFIG_SVQ3_DECODER) {
  233. h->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + 16 * 16 * 1,
  234. h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][sl->chroma_qp[0]][0]);
  235. h->h264dsp.h264_chroma_dc_dequant_idct(sl->mb + 16 * 16 * 2,
  236. h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][sl->chroma_qp[1]][0]);
  237. for (j = 1; j < 3; j++) {
  238. for (i = j * 16; i < j * 16 + 4; i++)
  239. if (sl->non_zero_count_cache[scan8[i]] || sl->mb[i * 16]) {
  240. uint8_t *const ptr = dest[j - 1] + block_offset[i];
  241. ff_svq3_add_idct_c(ptr, sl->mb + i * 16,
  242. uvlinesize,
  243. ff_h264_chroma_qp[0][sl->qscale + 12] - 12, 2);
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. #if !SIMPLE || BITS == 8
  252. #undef CHROMA_IDC
  253. #define CHROMA_IDC 3
  254. #include "h264_mc_template.c"
  255. static av_noinline void FUNC(hl_decode_mb_444)(const H264Context *h, H264SliceContext *sl)
  256. {
  257. const int mb_x = sl->mb_x;
  258. const int mb_y = sl->mb_y;
  259. const int mb_xy = sl->mb_xy;
  260. const int mb_type = h->cur_pic.mb_type[mb_xy];
  261. uint8_t *dest[3];
  262. int linesize;
  263. int i, j, p;
  264. const int *block_offset = &h->block_offset[0];
  265. const int transform_bypass = !SIMPLE && (sl->qscale == 0 && h->sps.transform_bypass);
  266. const int plane_count = (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) ? 3 : 1;
  267. for (p = 0; p < plane_count; p++) {
  268. dest[p] = h->cur_pic.f->data[p] +
  269. ((mb_x << PIXEL_SHIFT) + mb_y * sl->linesize) * 16;
  270. h->vdsp.prefetch(dest[p] + (sl->mb_x & 3) * 4 * sl->linesize + (64 << PIXEL_SHIFT),
  271. sl->linesize, 4);
  272. }
  273. h->list_counts[mb_xy] = sl->list_count;
  274. if (!SIMPLE && MB_FIELD(sl)) {
  275. linesize = sl->mb_linesize = sl->mb_uvlinesize = sl->linesize * 2;
  276. block_offset = &h->block_offset[48];
  277. if (mb_y & 1) // FIXME move out of this function?
  278. for (p = 0; p < 3; p++)
  279. dest[p] -= sl->linesize * 15;
  280. if (FRAME_MBAFF(h)) {
  281. int list;
  282. for (list = 0; list < sl->list_count; list++) {
  283. if (!USES_LIST(mb_type, list))
  284. continue;
  285. if (IS_16X16(mb_type)) {
  286. int8_t *ref = &sl->ref_cache[list][scan8[0]];
  287. fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (sl->mb_y & 1), 1);
  288. } else {
  289. for (i = 0; i < 16; i += 4) {
  290. int ref = sl->ref_cache[list][scan8[i]];
  291. if (ref >= 0)
  292. fill_rectangle(&sl->ref_cache[list][scan8[i]], 2, 2,
  293. 8, (16 + ref) ^ (sl->mb_y & 1), 1);
  294. }
  295. }
  296. }
  297. }
  298. } else {
  299. linesize = sl->mb_linesize = sl->mb_uvlinesize = sl->linesize;
  300. }
  301. if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
  302. if (PIXEL_SHIFT) {
  303. const int bit_depth = h->sps.bit_depth_luma;
  304. GetBitContext gb;
  305. init_get_bits(&gb, sl->intra_pcm_ptr, 768 * bit_depth);
  306. for (p = 0; p < plane_count; p++)
  307. for (i = 0; i < 16; i++) {
  308. uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);
  309. for (j = 0; j < 16; j++)
  310. tmp[j] = get_bits(&gb, bit_depth);
  311. }
  312. } else {
  313. for (p = 0; p < plane_count; p++)
  314. for (i = 0; i < 16; i++)
  315. memcpy(dest[p] + i * linesize,
  316. sl->intra_pcm_ptr + p * 256 + i * 16, 16);
  317. }
  318. } else {
  319. if (IS_INTRA(mb_type)) {
  320. if (sl->deblocking_filter)
  321. xchg_mb_border(h, sl, dest[0], dest[1], dest[2], linesize,
  322. linesize, 1, 1, SIMPLE, PIXEL_SHIFT);
  323. for (p = 0; p < plane_count; p++)
  324. hl_decode_mb_predict_luma(h, sl, mb_type, 1, SIMPLE,
  325. transform_bypass, PIXEL_SHIFT,
  326. block_offset, linesize, dest[p], p);
  327. if (sl->deblocking_filter)
  328. xchg_mb_border(h, sl, dest[0], dest[1], dest[2], linesize,
  329. linesize, 0, 1, SIMPLE, PIXEL_SHIFT);
  330. } else {
  331. FUNC(hl_motion_444)(h, sl, dest[0], dest[1], dest[2],
  332. h->qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
  333. h->qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
  334. h->h264dsp.weight_h264_pixels_tab,
  335. h->h264dsp.biweight_h264_pixels_tab);
  336. }
  337. for (p = 0; p < plane_count; p++)
  338. hl_decode_mb_idct_luma(h, sl, mb_type, 1, SIMPLE, transform_bypass,
  339. PIXEL_SHIFT, block_offset, linesize,
  340. dest[p], p);
  341. }
  342. }
  343. #endif