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.

381 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. MpegEncContext *const s = &h->s;
  39. const int mb_x = s->mb_x;
  40. const int mb_y = s->mb_y;
  41. const int mb_xy = h->mb_xy;
  42. const int mb_type = s->current_picture.f.mb_type[mb_xy];
  43. uint8_t *dest_y, *dest_cb, *dest_cr;
  44. int linesize, uvlinesize /*dct_offset*/;
  45. int i, j;
  46. int *block_offset = &h->block_offset[0];
  47. const int transform_bypass = !SIMPLE && (s->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 || s->codec_id == CODEC_ID_H264;
  50. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  51. const int block_h = 16 >> s->chroma_y_shift;
  52. const int chroma422 = CHROMA422;
  53. dest_y = s->current_picture.f.data[0] + ((mb_x << PIXEL_SHIFT) + mb_y * s->linesize) * 16;
  54. dest_cb = s->current_picture.f.data[1] + (mb_x << PIXEL_SHIFT) * 8 + mb_y * s->uvlinesize * block_h;
  55. dest_cr = s->current_picture.f.data[2] + (mb_x << PIXEL_SHIFT) * 8 + mb_y * s->uvlinesize * block_h;
  56. s->dsp.prefetch(dest_y + (s->mb_x & 3) * 4 * s->linesize + (64 << PIXEL_SHIFT), s->linesize, 4);
  57. s->dsp.prefetch(dest_cb + (s->mb_x & 7) * s->uvlinesize + (64 << PIXEL_SHIFT), dest_cr - dest_cb, 2);
  58. h->list_counts[mb_xy] = h->list_count;
  59. if (!SIMPLE && MB_FIELD) {
  60. linesize = h->mb_linesize = s->linesize * 2;
  61. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  62. block_offset = &h->block_offset[48];
  63. if (mb_y & 1) { // FIXME move out of this function?
  64. dest_y -= s->linesize * 15;
  65. dest_cb -= s->uvlinesize * (block_h - 1);
  66. dest_cr -= s->uvlinesize * (block_h - 1);
  67. }
  68. if (FRAME_MBAFF) {
  69. int list;
  70. for (list = 0; list < h->list_count; list++) {
  71. if (!USES_LIST(mb_type, list))
  72. continue;
  73. if (IS_16X16(mb_type)) {
  74. int8_t *ref = &h->ref_cache[list][scan8[0]];
  75. fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (s->mb_y & 1), 1);
  76. } else {
  77. for (i = 0; i < 16; i += 4) {
  78. int ref = h->ref_cache[list][scan8[i]];
  79. if (ref >= 0)
  80. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
  81. 8, (16 + ref) ^ (s->mb_y & 1), 1);
  82. }
  83. }
  84. }
  85. }
  86. } else {
  87. linesize = h->mb_linesize = s->linesize;
  88. uvlinesize = h->mb_uvlinesize = s->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, (uint8_t *)h->mb,
  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 || !(s->flags & 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, (uint8_t *)h->mb + i * 16, 16);
  131. if (SIMPLE || !CONFIG_GRAY || !(s->flags & 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. uint8_t *src_cb = (uint8_t *)h->mb + 256;
  139. uint8_t *src_cr = (uint8_t *)h->mb + 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 (h->deblocking_filter)
  150. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
  151. uvlinesize, 1, 0, SIMPLE, PIXEL_SHIFT);
  152. if (SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  153. h->hpc.pred8x8[h->chroma_pred_mode](dest_cb, uvlinesize);
  154. h->hpc.pred8x8[h->chroma_pred_mode](dest_cr, uvlinesize);
  155. }
  156. hl_decode_mb_predict_luma(h, mb_type, is_h264, SIMPLE,
  157. transform_bypass, PIXEL_SHIFT,
  158. block_offset, linesize, dest_y, 0);
  159. if (h->deblocking_filter)
  160. xchg_mb_border(h, 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, dest_y, dest_cb, dest_cr,
  165. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  166. s->me.qpel_avg, s->dsp.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, dest_y, dest_cb, dest_cr,
  171. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  172. s->me.qpel_avg, s->dsp.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, mb_type, is_h264, SIMPLE, transform_bypass,
  178. PIXEL_SHIFT, block_offset, linesize, dest_y, 0);
  179. if ((SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) &&
  180. (h->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. (h->chroma_pred_mode == VERT_PRED8x8 ||
  185. h->chroma_pred_mode == HOR_PRED8x8)) {
  186. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0],
  187. block_offset + 16,
  188. h->mb + (16 * 16 * 1 << PIXEL_SHIFT),
  189. uvlinesize);
  190. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1],
  191. block_offset + 32,
  192. h->mb + (16 * 16 * 2 << PIXEL_SHIFT),
  193. uvlinesize);
  194. } else {
  195. idct_add = s->dsp.add_pixels4;
  196. for (j = 1; j < 3; j++) {
  197. for (i = j * 16; i < j * 16 + 4; i++)
  198. if (h->non_zero_count_cache[scan8[i]] ||
  199. dctcoef_get(h->mb, PIXEL_SHIFT, i * 16))
  200. idct_add(dest[j - 1] + block_offset[i],
  201. h->mb + (i * 16 << PIXEL_SHIFT),
  202. uvlinesize);
  203. if (chroma422) {
  204. for (i = j * 16 + 4; i < j * 16 + 8; i++)
  205. if (h->non_zero_count_cache[scan8[i + 4]] ||
  206. dctcoef_get(h->mb, PIXEL_SHIFT, i * 16))
  207. idct_add(dest[j - 1] + block_offset[i + 4],
  208. h->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] = h->chroma_qp[0] + 3;
  218. qp[1] = h->chroma_qp[1] + 3;
  219. } else {
  220. qp[0] = h->chroma_qp[0];
  221. qp[1] = h->chroma_qp[1];
  222. }
  223. if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 0]])
  224. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 1 << PIXEL_SHIFT),
  225. h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][qp[0]][0]);
  226. if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 1]])
  227. h->h264dsp.h264_chroma_dc_dequant_idct(h->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. h->mb, uvlinesize,
  231. h->non_zero_count_cache);
  232. } else if (CONFIG_SVQ3_DECODER) {
  233. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 1,
  234. h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][h->chroma_qp[0]][0]);
  235. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 2,
  236. h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][h->chroma_qp[1]][0]);
  237. for (j = 1; j < 3; j++) {
  238. for (i = j * 16; i < j * 16 + 4; i++)
  239. if (h->non_zero_count_cache[scan8[i]] || h->mb[i * 16]) {
  240. uint8_t *const ptr = dest[j - 1] + block_offset[i];
  241. ff_svq3_add_idct_c(ptr, h->mb + i * 16,
  242. uvlinesize,
  243. ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. if (h->cbp || IS_INTRA(mb_type)) {
  251. s->dsp.clear_blocks(h->mb);
  252. s->dsp.clear_blocks(h->mb + (24 * 16 << PIXEL_SHIFT));
  253. }
  254. }
  255. #if !SIMPLE || BITS == 8
  256. #undef CHROMA_IDC
  257. #define CHROMA_IDC 3
  258. #include "h264_mc_template.c"
  259. static av_noinline void FUNC(hl_decode_mb_444)(H264Context *h)
  260. {
  261. MpegEncContext *const s = &h->s;
  262. const int mb_x = s->mb_x;
  263. const int mb_y = s->mb_y;
  264. const int mb_xy = h->mb_xy;
  265. const int mb_type = s->current_picture.f.mb_type[mb_xy];
  266. uint8_t *dest[3];
  267. int linesize;
  268. int i, j, p;
  269. int *block_offset = &h->block_offset[0];
  270. const int transform_bypass = !SIMPLE && (s->qscale == 0 && h->sps.transform_bypass);
  271. const int plane_count = (SIMPLE || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) ? 3 : 1;
  272. for (p = 0; p < plane_count; p++) {
  273. dest[p] = s->current_picture.f.data[p] +
  274. ((mb_x << PIXEL_SHIFT) + mb_y * s->linesize) * 16;
  275. s->dsp.prefetch(dest[p] + (s->mb_x & 3) * 4 * s->linesize + (64 << PIXEL_SHIFT),
  276. s->linesize, 4);
  277. }
  278. h->list_counts[mb_xy] = h->list_count;
  279. if (!SIMPLE && MB_FIELD) {
  280. linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;
  281. block_offset = &h->block_offset[48];
  282. if (mb_y & 1) // FIXME move out of this function?
  283. for (p = 0; p < 3; p++)
  284. dest[p] -= s->linesize * 15;
  285. if (FRAME_MBAFF) {
  286. int list;
  287. for (list = 0; list < h->list_count; list++) {
  288. if (!USES_LIST(mb_type, list))
  289. continue;
  290. if (IS_16X16(mb_type)) {
  291. int8_t *ref = &h->ref_cache[list][scan8[0]];
  292. fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (s->mb_y & 1), 1);
  293. } else {
  294. for (i = 0; i < 16; i += 4) {
  295. int ref = h->ref_cache[list][scan8[i]];
  296. if (ref >= 0)
  297. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
  298. 8, (16 + ref) ^ (s->mb_y & 1), 1);
  299. }
  300. }
  301. }
  302. }
  303. } else {
  304. linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize;
  305. }
  306. if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
  307. if (PIXEL_SHIFT) {
  308. const int bit_depth = h->sps.bit_depth_luma;
  309. GetBitContext gb;
  310. init_get_bits(&gb, (uint8_t *)h->mb, 768 * bit_depth);
  311. for (p = 0; p < plane_count; p++)
  312. for (i = 0; i < 16; i++) {
  313. uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);
  314. for (j = 0; j < 16; j++)
  315. tmp[j] = get_bits(&gb, bit_depth);
  316. }
  317. } else {
  318. for (p = 0; p < plane_count; p++)
  319. for (i = 0; i < 16; i++)
  320. memcpy(dest[p] + i * linesize,
  321. (uint8_t *)h->mb + p * 256 + i * 16, 16);
  322. }
  323. } else {
  324. if (IS_INTRA(mb_type)) {
  325. if (h->deblocking_filter)
  326. xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
  327. linesize, 1, 1, SIMPLE, PIXEL_SHIFT);
  328. for (p = 0; p < plane_count; p++)
  329. hl_decode_mb_predict_luma(h, mb_type, 1, SIMPLE,
  330. transform_bypass, PIXEL_SHIFT,
  331. block_offset, linesize, dest[p], p);
  332. if (h->deblocking_filter)
  333. xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
  334. linesize, 0, 1, SIMPLE, PIXEL_SHIFT);
  335. } else {
  336. FUNC(hl_motion_444)(h, dest[0], dest[1], dest[2],
  337. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  338. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  339. h->h264dsp.weight_h264_pixels_tab,
  340. h->h264dsp.biweight_h264_pixels_tab);
  341. }
  342. for (p = 0; p < plane_count; p++)
  343. hl_decode_mb_idct_luma(h, mb_type, 1, SIMPLE, transform_bypass,
  344. PIXEL_SHIFT, block_offset, linesize,
  345. dest[p], p);
  346. }
  347. if (h->cbp || IS_INTRA(mb_type)) {
  348. s->dsp.clear_blocks(h->mb);
  349. s->dsp.clear_blocks(h->mb + (24 * 16 << PIXEL_SHIFT));
  350. }
  351. }
  352. #endif