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.

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