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.

359 lines
16KB

  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->ps.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->ps.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->ps.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->ps.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->ps.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->h264qpel.put_h264_qpel_pixels_tab,
  163. h->h264chroma.put_h264_chroma_pixels_tab,
  164. h->h264qpel.avg_h264_qpel_pixels_tab,
  165. 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, sl, dest_y, dest_cb, dest_cr,
  170. h->h264qpel.put_h264_qpel_pixels_tab,
  171. h->h264chroma.put_h264_chroma_pixels_tab,
  172. h->h264qpel.avg_h264_qpel_pixels_tab,
  173. h->h264chroma.avg_h264_chroma_pixels_tab,
  174. h->h264dsp.weight_h264_pixels_tab,
  175. h->h264dsp.biweight_h264_pixels_tab);
  176. }
  177. }
  178. hl_decode_mb_idct_luma(h, sl, mb_type, SIMPLE, transform_bypass,
  179. PIXEL_SHIFT, block_offset, linesize, dest_y, 0);
  180. if ((SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) &&
  181. (sl->cbp & 0x30)) {
  182. uint8_t *dest[2] = { dest_cb, dest_cr };
  183. if (transform_bypass) {
  184. if (IS_INTRA(mb_type) && h->ps.sps->profile_idc == 244 &&
  185. (sl->chroma_pred_mode == VERT_PRED8x8 ||
  186. sl->chroma_pred_mode == HOR_PRED8x8)) {
  187. h->hpc.pred8x8_add[sl->chroma_pred_mode](dest[0],
  188. block_offset + 16,
  189. sl->mb + (16 * 16 * 1 << PIXEL_SHIFT),
  190. uvlinesize);
  191. h->hpc.pred8x8_add[sl->chroma_pred_mode](dest[1],
  192. block_offset + 32,
  193. sl->mb + (16 * 16 * 2 << PIXEL_SHIFT),
  194. uvlinesize);
  195. } else {
  196. idct_add = h->h264dsp.h264_add_pixels4_clear;
  197. for (j = 1; j < 3; j++) {
  198. for (i = j * 16; i < j * 16 + 4; i++)
  199. if (sl->non_zero_count_cache[scan8[i]] ||
  200. dctcoef_get(sl->mb, PIXEL_SHIFT, i * 16))
  201. idct_add(dest[j - 1] + block_offset[i],
  202. sl->mb + (i * 16 << PIXEL_SHIFT),
  203. uvlinesize);
  204. if (chroma422) {
  205. for (i = j * 16 + 4; i < j * 16 + 8; i++)
  206. if (sl->non_zero_count_cache[scan8[i + 4]] ||
  207. dctcoef_get(sl->mb, PIXEL_SHIFT, i * 16))
  208. idct_add(dest[j - 1] + block_offset[i + 4],
  209. sl->mb + (i * 16 << PIXEL_SHIFT),
  210. uvlinesize);
  211. }
  212. }
  213. }
  214. } else {
  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->ps.pps->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->ps.pps->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. }
  233. }
  234. }
  235. }
  236. #if !SIMPLE || BITS == 8
  237. #undef CHROMA_IDC
  238. #define CHROMA_IDC 3
  239. #include "h264_mc_template.c"
  240. static av_noinline void FUNC(hl_decode_mb_444)(const H264Context *h, H264SliceContext *sl)
  241. {
  242. const int mb_x = sl->mb_x;
  243. const int mb_y = sl->mb_y;
  244. const int mb_xy = sl->mb_xy;
  245. const int mb_type = h->cur_pic.mb_type[mb_xy];
  246. uint8_t *dest[3];
  247. int linesize;
  248. int i, j, p;
  249. const int *block_offset = &h->block_offset[0];
  250. const int transform_bypass = !SIMPLE && (sl->qscale == 0 && h->ps.sps->transform_bypass);
  251. const int plane_count = (SIMPLE || !CONFIG_GRAY || !(h->flags & AV_CODEC_FLAG_GRAY)) ? 3 : 1;
  252. for (p = 0; p < plane_count; p++) {
  253. dest[p] = h->cur_pic.f->data[p] +
  254. ((mb_x << PIXEL_SHIFT) + mb_y * sl->linesize) * 16;
  255. h->vdsp.prefetch(dest[p] + (sl->mb_x & 3) * 4 * sl->linesize + (64 << PIXEL_SHIFT),
  256. sl->linesize, 4);
  257. }
  258. h->list_counts[mb_xy] = sl->list_count;
  259. if (!SIMPLE && MB_FIELD(sl)) {
  260. linesize = sl->mb_linesize = sl->mb_uvlinesize = sl->linesize * 2;
  261. block_offset = &h->block_offset[48];
  262. if (mb_y & 1) // FIXME move out of this function?
  263. for (p = 0; p < 3; p++)
  264. dest[p] -= sl->linesize * 15;
  265. if (FRAME_MBAFF(h)) {
  266. int list;
  267. for (list = 0; list < sl->list_count; list++) {
  268. if (!USES_LIST(mb_type, list))
  269. continue;
  270. if (IS_16X16(mb_type)) {
  271. int8_t *ref = &sl->ref_cache[list][scan8[0]];
  272. fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (sl->mb_y & 1), 1);
  273. } else {
  274. for (i = 0; i < 16; i += 4) {
  275. int ref = sl->ref_cache[list][scan8[i]];
  276. if (ref >= 0)
  277. fill_rectangle(&sl->ref_cache[list][scan8[i]], 2, 2,
  278. 8, (16 + ref) ^ (sl->mb_y & 1), 1);
  279. }
  280. }
  281. }
  282. }
  283. } else {
  284. linesize = sl->mb_linesize = sl->mb_uvlinesize = sl->linesize;
  285. }
  286. if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
  287. if (PIXEL_SHIFT) {
  288. const int bit_depth = h->ps.sps->bit_depth_luma;
  289. GetBitContext gb;
  290. init_get_bits(&gb, sl->intra_pcm_ptr, 768 * bit_depth);
  291. for (p = 0; p < plane_count; p++)
  292. for (i = 0; i < 16; i++) {
  293. uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);
  294. for (j = 0; j < 16; j++)
  295. tmp[j] = get_bits(&gb, bit_depth);
  296. }
  297. } else {
  298. for (p = 0; p < plane_count; p++)
  299. for (i = 0; i < 16; i++)
  300. memcpy(dest[p] + i * linesize,
  301. sl->intra_pcm_ptr + p * 256 + i * 16, 16);
  302. }
  303. } else {
  304. if (IS_INTRA(mb_type)) {
  305. if (sl->deblocking_filter)
  306. xchg_mb_border(h, sl, dest[0], dest[1], dest[2], linesize,
  307. linesize, 1, 1, SIMPLE, PIXEL_SHIFT);
  308. for (p = 0; p < plane_count; p++)
  309. hl_decode_mb_predict_luma(h, sl, mb_type, SIMPLE,
  310. transform_bypass, PIXEL_SHIFT,
  311. block_offset, linesize, dest[p], p);
  312. if (sl->deblocking_filter)
  313. xchg_mb_border(h, sl, dest[0], dest[1], dest[2], linesize,
  314. linesize, 0, 1, SIMPLE, PIXEL_SHIFT);
  315. } else {
  316. FUNC(hl_motion_444)(h, sl, dest[0], dest[1], dest[2],
  317. h->h264qpel.put_h264_qpel_pixels_tab,
  318. h->h264chroma.put_h264_chroma_pixels_tab,
  319. h->h264qpel.avg_h264_qpel_pixels_tab,
  320. h->h264chroma.avg_h264_chroma_pixels_tab,
  321. h->h264dsp.weight_h264_pixels_tab,
  322. h->h264dsp.biweight_h264_pixels_tab);
  323. }
  324. for (p = 0; p < plane_count; p++)
  325. hl_decode_mb_idct_luma(h, sl, mb_type, SIMPLE, transform_bypass,
  326. PIXEL_SHIFT, block_offset, linesize,
  327. dest[p], p);
  328. }
  329. }
  330. #endif