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.

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