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.

353 lines
15KB

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