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.

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