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.

368 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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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;
  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) {
  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) {
  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. const int bit_depth = h->sps.bit_depth_luma;
  92. if (PIXEL_SHIFT) {
  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. uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
  107. for (j = 0; j < 8; j++) {
  108. tmp_cb[j] = tmp_cr[j] = 1 << (bit_depth - 1);
  109. }
  110. }
  111. } else {
  112. for (i = 0; i < block_h; i++) {
  113. uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
  114. for (j = 0; j < 8; j++)
  115. tmp_cb[j] = get_bits(&gb, bit_depth);
  116. }
  117. for (i = 0; i < block_h; i++) {
  118. uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
  119. for (j = 0; j < 8; j++)
  120. tmp_cr[j] = get_bits(&gb, bit_depth);
  121. }
  122. }
  123. }
  124. } else {
  125. for (i = 0; i < 16; i++)
  126. memcpy(dest_y + i * linesize, h->intra_pcm_ptr + i * 16, 16);
  127. if (SIMPLE || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  128. if (!h->sps.chroma_format_idc) {
  129. for (i = 0; i < 8; i++) {
  130. memset(dest_cb + i*uvlinesize, 1 << (bit_depth - 1), 8);
  131. memset(dest_cr + i*uvlinesize, 1 << (bit_depth - 1), 8);
  132. }
  133. } else {
  134. const uint8_t *src_cb = h->intra_pcm_ptr + 256;
  135. const uint8_t *src_cr = h->intra_pcm_ptr + 256 + block_h * 8;
  136. for (i = 0; i < block_h; i++) {
  137. memcpy(dest_cb + i * uvlinesize, src_cb + i * 8, 8);
  138. memcpy(dest_cr + i * uvlinesize, src_cr + i * 8, 8);
  139. }
  140. }
  141. }
  142. }
  143. } else {
  144. if (IS_INTRA(mb_type)) {
  145. if (h->deblocking_filter)
  146. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
  147. uvlinesize, 1, 0, SIMPLE, PIXEL_SHIFT);
  148. if (SIMPLE || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) {
  149. h->hpc.pred8x8[h->chroma_pred_mode](dest_cb, uvlinesize);
  150. h->hpc.pred8x8[h->chroma_pred_mode](dest_cr, uvlinesize);
  151. }
  152. hl_decode_mb_predict_luma(h, mb_type, is_h264, SIMPLE,
  153. transform_bypass, PIXEL_SHIFT,
  154. block_offset, linesize, dest_y, 0);
  155. if (h->deblocking_filter)
  156. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
  157. uvlinesize, 0, 0, SIMPLE, PIXEL_SHIFT);
  158. } else if (is_h264) {
  159. if (chroma422) {
  160. FUNC(hl_motion_422)(h, dest_y, dest_cb, dest_cr,
  161. h->me.qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
  162. h->me.qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
  163. h->h264dsp.weight_h264_pixels_tab,
  164. h->h264dsp.biweight_h264_pixels_tab);
  165. } else {
  166. FUNC(hl_motion_420)(h, dest_y, dest_cb, dest_cr,
  167. h->me.qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
  168. h->me.qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
  169. h->h264dsp.weight_h264_pixels_tab,
  170. h->h264dsp.biweight_h264_pixels_tab);
  171. }
  172. }
  173. hl_decode_mb_idct_luma(h, mb_type, is_h264, SIMPLE, transform_bypass,
  174. PIXEL_SHIFT, block_offset, linesize, dest_y, 0);
  175. if ((SIMPLE || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) &&
  176. (h->cbp & 0x30)) {
  177. uint8_t *dest[2] = { dest_cb, dest_cr };
  178. if (transform_bypass) {
  179. if (IS_INTRA(mb_type) && h->sps.profile_idc == 244 &&
  180. (h->chroma_pred_mode == VERT_PRED8x8 ||
  181. h->chroma_pred_mode == HOR_PRED8x8)) {
  182. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0],
  183. block_offset + 16,
  184. h->mb + (16 * 16 * 1 << PIXEL_SHIFT),
  185. uvlinesize);
  186. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1],
  187. block_offset + 32,
  188. h->mb + (16 * 16 * 2 << PIXEL_SHIFT),
  189. uvlinesize);
  190. } else {
  191. idct_add = h->h264dsp.h264_add_pixels4_clear;
  192. for (j = 1; j < 3; j++) {
  193. for (i = j * 16; i < j * 16 + 4; i++)
  194. if (h->non_zero_count_cache[scan8[i]] ||
  195. dctcoef_get(h->mb, PIXEL_SHIFT, i * 16))
  196. idct_add(dest[j - 1] + block_offset[i],
  197. h->mb + (i * 16 << PIXEL_SHIFT),
  198. uvlinesize);
  199. if (chroma422) {
  200. for (i = j * 16 + 4; i < j * 16 + 8; i++)
  201. if (h->non_zero_count_cache[scan8[i + 4]] ||
  202. dctcoef_get(h->mb, PIXEL_SHIFT, i * 16))
  203. idct_add(dest[j - 1] + block_offset[i + 4],
  204. h->mb + (i * 16 << PIXEL_SHIFT),
  205. uvlinesize);
  206. }
  207. }
  208. }
  209. } else {
  210. if (is_h264) {
  211. int qp[2];
  212. if (chroma422) {
  213. qp[0] = h->chroma_qp[0] + 3;
  214. qp[1] = h->chroma_qp[1] + 3;
  215. } else {
  216. qp[0] = h->chroma_qp[0];
  217. qp[1] = h->chroma_qp[1];
  218. }
  219. if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 0]])
  220. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 1 << PIXEL_SHIFT),
  221. h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][qp[0]][0]);
  222. if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 1]])
  223. h->h264dsp.h264_chroma_dc_dequant_idct(h->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. h->mb, uvlinesize,
  227. h->non_zero_count_cache);
  228. } else if (CONFIG_SVQ3_DECODER) {
  229. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 1,
  230. h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][h->chroma_qp[0]][0]);
  231. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 2,
  232. h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][h->chroma_qp[1]][0]);
  233. for (j = 1; j < 3; j++) {
  234. for (i = j * 16; i < j * 16 + 4; i++)
  235. if (h->non_zero_count_cache[scan8[i]] || h->mb[i * 16]) {
  236. uint8_t *const ptr = dest[j - 1] + block_offset[i];
  237. ff_svq3_add_idct_c(ptr, h->mb + i * 16,
  238. uvlinesize,
  239. ff_h264_chroma_qp[0][h->qscale + 12] - 12, 2);
  240. }
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. #if !SIMPLE || BITS == 8
  248. #undef CHROMA_IDC
  249. #define CHROMA_IDC 3
  250. #include "h264_mc_template.c"
  251. static av_noinline void FUNC(hl_decode_mb_444)(H264Context *h)
  252. {
  253. const int mb_x = h->mb_x;
  254. const int mb_y = h->mb_y;
  255. const int mb_xy = h->mb_xy;
  256. const int mb_type = h->cur_pic.mb_type[mb_xy];
  257. uint8_t *dest[3];
  258. int linesize;
  259. int i, j, p;
  260. int *block_offset = &h->block_offset[0];
  261. const int transform_bypass = !SIMPLE && (h->qscale == 0 && h->sps.transform_bypass);
  262. const int plane_count = (SIMPLE || !CONFIG_GRAY || !(h->flags & CODEC_FLAG_GRAY)) ? 3 : 1;
  263. for (p = 0; p < plane_count; p++) {
  264. dest[p] = h->cur_pic.f.data[p] +
  265. ((mb_x << PIXEL_SHIFT) + mb_y * h->linesize) * 16;
  266. h->vdsp.prefetch(dest[p] + (h->mb_x & 3) * 4 * h->linesize + (64 << PIXEL_SHIFT),
  267. h->linesize, 4);
  268. }
  269. h->list_counts[mb_xy] = h->list_count;
  270. if (!SIMPLE && MB_FIELD) {
  271. linesize = h->mb_linesize = h->mb_uvlinesize = h->linesize * 2;
  272. block_offset = &h->block_offset[48];
  273. if (mb_y & 1) // FIXME move out of this function?
  274. for (p = 0; p < 3; p++)
  275. dest[p] -= h->linesize * 15;
  276. if (FRAME_MBAFF) {
  277. int list;
  278. for (list = 0; list < h->list_count; list++) {
  279. if (!USES_LIST(mb_type, list))
  280. continue;
  281. if (IS_16X16(mb_type)) {
  282. int8_t *ref = &h->ref_cache[list][scan8[0]];
  283. fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (h->mb_y & 1), 1);
  284. } else {
  285. for (i = 0; i < 16; i += 4) {
  286. int ref = h->ref_cache[list][scan8[i]];
  287. if (ref >= 0)
  288. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
  289. 8, (16 + ref) ^ (h->mb_y & 1), 1);
  290. }
  291. }
  292. }
  293. }
  294. } else {
  295. linesize = h->mb_linesize = h->mb_uvlinesize = h->linesize;
  296. }
  297. if (!SIMPLE && IS_INTRA_PCM(mb_type)) {
  298. if (PIXEL_SHIFT) {
  299. const int bit_depth = h->sps.bit_depth_luma;
  300. GetBitContext gb;
  301. init_get_bits(&gb, h->intra_pcm_ptr, 768 * bit_depth);
  302. for (p = 0; p < plane_count; p++)
  303. for (i = 0; i < 16; i++) {
  304. uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);
  305. for (j = 0; j < 16; j++)
  306. tmp[j] = get_bits(&gb, bit_depth);
  307. }
  308. } else {
  309. for (p = 0; p < plane_count; p++)
  310. for (i = 0; i < 16; i++)
  311. memcpy(dest[p] + i * linesize,
  312. h->intra_pcm_ptr + p * 256 + i * 16, 16);
  313. }
  314. } else {
  315. if (IS_INTRA(mb_type)) {
  316. if (h->deblocking_filter)
  317. xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
  318. linesize, 1, 1, SIMPLE, PIXEL_SHIFT);
  319. for (p = 0; p < plane_count; p++)
  320. hl_decode_mb_predict_luma(h, mb_type, 1, SIMPLE,
  321. transform_bypass, PIXEL_SHIFT,
  322. block_offset, linesize, dest[p], p);
  323. if (h->deblocking_filter)
  324. xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
  325. linesize, 0, 1, SIMPLE, PIXEL_SHIFT);
  326. } else {
  327. FUNC(hl_motion_444)(h, dest[0], dest[1], dest[2],
  328. h->me.qpel_put, h->h264chroma.put_h264_chroma_pixels_tab,
  329. h->me.qpel_avg, h->h264chroma.avg_h264_chroma_pixels_tab,
  330. h->h264dsp.weight_h264_pixels_tab,
  331. h->h264dsp.biweight_h264_pixels_tab);
  332. }
  333. for (p = 0; p < plane_count; p++)
  334. hl_decode_mb_idct_luma(h, mb_type, 1, SIMPLE, transform_bypass,
  335. PIXEL_SHIFT, block_offset, linesize,
  336. dest[p], p);
  337. }
  338. }
  339. #endif