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.

4698 lines
186KB

  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. /**
  22. * @file
  23. * H.264 / AVC / MPEG4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #define UNCHECKED_BITSTREAM_READER 1
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/opt.h"
  29. #include "internal.h"
  30. #include "cabac.h"
  31. #include "cabac_functions.h"
  32. #include "dsputil.h"
  33. #include "avcodec.h"
  34. #include "mpegvideo.h"
  35. #include "h264.h"
  36. #include "h264data.h"
  37. #include "h264_mvpred.h"
  38. #include "golomb.h"
  39. #include "mathops.h"
  40. #include "rectangle.h"
  41. #include "thread.h"
  42. #include "vdpau_internal.h"
  43. #include "libavutil/avassert.h"
  44. // #undef NDEBUG
  45. #include <assert.h>
  46. static const uint8_t rem6[QP_MAX_NUM + 1] = {
  47. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2,
  48. 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
  49. 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
  50. };
  51. static const uint8_t div6[QP_MAX_NUM + 1] = {
  52. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3,
  53. 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
  54. 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
  55. };
  56. static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
  57. PIX_FMT_DXVA2_VLD,
  58. PIX_FMT_VAAPI_VLD,
  59. PIX_FMT_VDA_VLD,
  60. PIX_FMT_YUVJ420P,
  61. PIX_FMT_NONE
  62. };
  63. /**
  64. * Check if the top & left blocks are available if needed and
  65. * change the dc mode so it only uses the available blocks.
  66. */
  67. int ff_h264_check_intra4x4_pred_mode(H264Context *h)
  68. {
  69. MpegEncContext *const s = &h->s;
  70. static const int8_t top[12] = {
  71. -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
  72. };
  73. static const int8_t left[12] = {
  74. 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
  75. };
  76. int i;
  77. if (!(h->top_samples_available & 0x8000)) {
  78. for (i = 0; i < 4; i++) {
  79. int status = top[h->intra4x4_pred_mode_cache[scan8[0] + i]];
  80. if (status < 0) {
  81. av_log(h->s.avctx, AV_LOG_ERROR,
  82. "top block unavailable for requested intra4x4 mode %d at %d %d\n",
  83. status, s->mb_x, s->mb_y);
  84. return -1;
  85. } else if (status) {
  86. h->intra4x4_pred_mode_cache[scan8[0] + i] = status;
  87. }
  88. }
  89. }
  90. if ((h->left_samples_available & 0x8888) != 0x8888) {
  91. static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
  92. for (i = 0; i < 4; i++)
  93. if (!(h->left_samples_available & mask[i])) {
  94. int status = left[h->intra4x4_pred_mode_cache[scan8[0] + 8 * i]];
  95. if (status < 0) {
  96. av_log(h->s.avctx, AV_LOG_ERROR,
  97. "left block unavailable for requested intra4x4 mode %d at %d %d\n",
  98. status, s->mb_x, s->mb_y);
  99. return -1;
  100. } else if (status) {
  101. h->intra4x4_pred_mode_cache[scan8[0] + 8 * i] = status;
  102. }
  103. }
  104. }
  105. return 0;
  106. } // FIXME cleanup like ff_h264_check_intra_pred_mode
  107. /**
  108. * Check if the top & left blocks are available if needed and
  109. * change the dc mode so it only uses the available blocks.
  110. */
  111. int ff_h264_check_intra_pred_mode(H264Context *h, int mode, int is_chroma)
  112. {
  113. MpegEncContext *const s = &h->s;
  114. static const int8_t top[7] = { LEFT_DC_PRED8x8, 1, -1, -1 };
  115. static const int8_t left[7] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
  116. if (mode > 6U) {
  117. av_log(h->s.avctx, AV_LOG_ERROR,
  118. "out of range intra chroma pred mode at %d %d\n",
  119. s->mb_x, s->mb_y);
  120. return -1;
  121. }
  122. if (!(h->top_samples_available & 0x8000)) {
  123. mode = top[mode];
  124. if (mode < 0) {
  125. av_log(h->s.avctx, AV_LOG_ERROR,
  126. "top block unavailable for requested intra mode at %d %d\n",
  127. s->mb_x, s->mb_y);
  128. return -1;
  129. }
  130. }
  131. if ((h->left_samples_available & 0x8080) != 0x8080) {
  132. mode = left[mode];
  133. if (is_chroma && (h->left_samples_available & 0x8080)) {
  134. // mad cow disease mode, aka MBAFF + constrained_intra_pred
  135. mode = ALZHEIMER_DC_L0T_PRED8x8 +
  136. (!(h->left_samples_available & 0x8000)) +
  137. 2 * (mode == DC_128_PRED8x8);
  138. }
  139. if (mode < 0) {
  140. av_log(h->s.avctx, AV_LOG_ERROR,
  141. "left block unavailable for requested intra mode at %d %d\n",
  142. s->mb_x, s->mb_y);
  143. return -1;
  144. }
  145. }
  146. return mode;
  147. }
  148. const uint8_t *ff_h264_decode_nal(H264Context *h, const uint8_t *src,
  149. int *dst_length, int *consumed, int length)
  150. {
  151. int i, si, di;
  152. uint8_t *dst;
  153. int bufidx;
  154. // src[0]&0x80; // forbidden bit
  155. h->nal_ref_idc = src[0] >> 5;
  156. h->nal_unit_type = src[0] & 0x1F;
  157. src++;
  158. length--;
  159. #if HAVE_FAST_UNALIGNED
  160. #if HAVE_FAST_64BIT
  161. #define RS 7
  162. for (i = 0; i + 1 < length; i += 9) {
  163. if (!((~AV_RN64A(src + i) &
  164. (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
  165. 0x8000800080008080ULL))
  166. #else
  167. #define RS 3
  168. for (i = 0; i + 1 < length; i += 5) {
  169. if (!((~AV_RN32A(src + i) &
  170. (AV_RN32A(src + i) - 0x01000101U)) &
  171. 0x80008080U))
  172. #endif
  173. continue;
  174. if (i > 0 && !src[i])
  175. i--;
  176. while (src[i])
  177. i++;
  178. #else
  179. #define RS 0
  180. for (i = 0; i + 1 < length; i += 2) {
  181. if (src[i])
  182. continue;
  183. if (i > 0 && src[i - 1] == 0)
  184. i--;
  185. #endif
  186. if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {
  187. if (src[i + 2] != 3) {
  188. /* startcode, so we must be past the end */
  189. length = i;
  190. }
  191. break;
  192. }
  193. i -= RS;
  194. }
  195. // use second escape buffer for inter data
  196. bufidx = h->nal_unit_type == NAL_DPC ? 1 : 0;
  197. si = h->rbsp_buffer_size[bufidx];
  198. av_fast_padded_malloc(&h->rbsp_buffer[bufidx], &h->rbsp_buffer_size[bufidx], length+MAX_MBPAIR_SIZE);
  199. dst = h->rbsp_buffer[bufidx];
  200. if (dst == NULL)
  201. return NULL;
  202. if(i>=length-1){ //no escaped 0
  203. *dst_length= length;
  204. *consumed= length+1; //+1 for the header
  205. if(h->s.avctx->flags2 & CODEC_FLAG2_FAST){
  206. return src;
  207. }else{
  208. memcpy(dst, src, length);
  209. return dst;
  210. }
  211. }
  212. // printf("decoding esc\n");
  213. memcpy(dst, src, i);
  214. si = di = i;
  215. while (si + 2 < length) {
  216. // remove escapes (very rare 1:2^22)
  217. if (src[si + 2] > 3) {
  218. dst[di++] = src[si++];
  219. dst[di++] = src[si++];
  220. } else if (src[si] == 0 && src[si + 1] == 0) {
  221. if (src[si + 2] == 3) { // escape
  222. dst[di++] = 0;
  223. dst[di++] = 0;
  224. si += 3;
  225. continue;
  226. } else // next start code
  227. goto nsc;
  228. }
  229. dst[di++] = src[si++];
  230. }
  231. while (si < length)
  232. dst[di++] = src[si++];
  233. nsc:
  234. memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  235. *dst_length = di;
  236. *consumed = si + 1; // +1 for the header
  237. /* FIXME store exact number of bits in the getbitcontext
  238. * (it is needed for decoding) */
  239. return dst;
  240. }
  241. /**
  242. * Identify the exact end of the bitstream
  243. * @return the length of the trailing, or 0 if damaged
  244. */
  245. static int decode_rbsp_trailing(H264Context *h, const uint8_t *src)
  246. {
  247. int v = *src;
  248. int r;
  249. tprintf(h->s.avctx, "rbsp trailing %X\n", v);
  250. for (r = 1; r < 9; r++) {
  251. if (v & 1)
  252. return r;
  253. v >>= 1;
  254. }
  255. return 0;
  256. }
  257. static inline int get_lowest_part_list_y(H264Context *h, Picture *pic, int n,
  258. int height, int y_offset, int list)
  259. {
  260. int raw_my = h->mv_cache[list][scan8[n]][1];
  261. int filter_height = (raw_my & 3) ? 2 : 0;
  262. int full_my = (raw_my >> 2) + y_offset;
  263. int top = full_my - filter_height;
  264. int bottom = full_my + filter_height + height;
  265. return FFMAX(abs(top), bottom);
  266. }
  267. static inline void get_lowest_part_y(H264Context *h, int refs[2][48], int n,
  268. int height, int y_offset, int list0,
  269. int list1, int *nrefs)
  270. {
  271. MpegEncContext *const s = &h->s;
  272. int my;
  273. y_offset += 16 * (s->mb_y >> MB_FIELD);
  274. if (list0) {
  275. int ref_n = h->ref_cache[0][scan8[n]];
  276. Picture *ref = &h->ref_list[0][ref_n];
  277. // Error resilience puts the current picture in the ref list.
  278. // Don't try to wait on these as it will cause a deadlock.
  279. // Fields can wait on each other, though.
  280. if (ref->f.thread_opaque != s->current_picture.f.thread_opaque ||
  281. (ref->f.reference & 3) != s->picture_structure) {
  282. my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
  283. if (refs[0][ref_n] < 0)
  284. nrefs[0] += 1;
  285. refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
  286. }
  287. }
  288. if (list1) {
  289. int ref_n = h->ref_cache[1][scan8[n]];
  290. Picture *ref = &h->ref_list[1][ref_n];
  291. if (ref->f.thread_opaque != s->current_picture.f.thread_opaque ||
  292. (ref->f.reference & 3) != s->picture_structure) {
  293. my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
  294. if (refs[1][ref_n] < 0)
  295. nrefs[1] += 1;
  296. refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
  297. }
  298. }
  299. }
  300. /**
  301. * Wait until all reference frames are available for MC operations.
  302. *
  303. * @param h the H264 context
  304. */
  305. static void await_references(H264Context *h)
  306. {
  307. MpegEncContext *const s = &h->s;
  308. const int mb_xy = h->mb_xy;
  309. const int mb_type = s->current_picture.f.mb_type[mb_xy];
  310. int refs[2][48];
  311. int nrefs[2] = { 0 };
  312. int ref, list;
  313. memset(refs, -1, sizeof(refs));
  314. if (IS_16X16(mb_type)) {
  315. get_lowest_part_y(h, refs, 0, 16, 0,
  316. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  317. } else if (IS_16X8(mb_type)) {
  318. get_lowest_part_y(h, refs, 0, 8, 0,
  319. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  320. get_lowest_part_y(h, refs, 8, 8, 8,
  321. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  322. } else if (IS_8X16(mb_type)) {
  323. get_lowest_part_y(h, refs, 0, 16, 0,
  324. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1), nrefs);
  325. get_lowest_part_y(h, refs, 4, 16, 0,
  326. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1), nrefs);
  327. } else {
  328. int i;
  329. assert(IS_8X8(mb_type));
  330. for (i = 0; i < 4; i++) {
  331. const int sub_mb_type = h->sub_mb_type[i];
  332. const int n = 4 * i;
  333. int y_offset = (i & 2) << 2;
  334. if (IS_SUB_8X8(sub_mb_type)) {
  335. get_lowest_part_y(h, refs, n, 8, y_offset,
  336. IS_DIR(sub_mb_type, 0, 0),
  337. IS_DIR(sub_mb_type, 0, 1),
  338. nrefs);
  339. } else if (IS_SUB_8X4(sub_mb_type)) {
  340. get_lowest_part_y(h, refs, n, 4, y_offset,
  341. IS_DIR(sub_mb_type, 0, 0),
  342. IS_DIR(sub_mb_type, 0, 1),
  343. nrefs);
  344. get_lowest_part_y(h, refs, n + 2, 4, y_offset + 4,
  345. IS_DIR(sub_mb_type, 0, 0),
  346. IS_DIR(sub_mb_type, 0, 1),
  347. nrefs);
  348. } else if (IS_SUB_4X8(sub_mb_type)) {
  349. get_lowest_part_y(h, refs, n, 8, y_offset,
  350. IS_DIR(sub_mb_type, 0, 0),
  351. IS_DIR(sub_mb_type, 0, 1),
  352. nrefs);
  353. get_lowest_part_y(h, refs, n + 1, 8, y_offset,
  354. IS_DIR(sub_mb_type, 0, 0),
  355. IS_DIR(sub_mb_type, 0, 1),
  356. nrefs);
  357. } else {
  358. int j;
  359. assert(IS_SUB_4X4(sub_mb_type));
  360. for (j = 0; j < 4; j++) {
  361. int sub_y_offset = y_offset + 2 * (j & 2);
  362. get_lowest_part_y(h, refs, n + j, 4, sub_y_offset,
  363. IS_DIR(sub_mb_type, 0, 0),
  364. IS_DIR(sub_mb_type, 0, 1),
  365. nrefs);
  366. }
  367. }
  368. }
  369. }
  370. for (list = h->list_count - 1; list >= 0; list--)
  371. for (ref = 0; ref < 48 && nrefs[list]; ref++) {
  372. int row = refs[list][ref];
  373. if (row >= 0) {
  374. Picture *ref_pic = &h->ref_list[list][ref];
  375. int ref_field = ref_pic->f.reference - 1;
  376. int ref_field_picture = ref_pic->field_picture;
  377. int pic_height = 16 * s->mb_height >> ref_field_picture;
  378. row <<= MB_MBAFF;
  379. nrefs[list]--;
  380. if (!FIELD_PICTURE && ref_field_picture) { // frame referencing two fields
  381. ff_thread_await_progress(&ref_pic->f,
  382. FFMIN((row >> 1) - !(row & 1),
  383. pic_height - 1),
  384. 1);
  385. ff_thread_await_progress(&ref_pic->f,
  386. FFMIN((row >> 1), pic_height - 1),
  387. 0);
  388. } else if (FIELD_PICTURE && !ref_field_picture) { // field referencing one field of a frame
  389. ff_thread_await_progress(&ref_pic->f,
  390. FFMIN(row * 2 + ref_field,
  391. pic_height - 1),
  392. 0);
  393. } else if (FIELD_PICTURE) {
  394. ff_thread_await_progress(&ref_pic->f,
  395. FFMIN(row, pic_height - 1),
  396. ref_field);
  397. } else {
  398. ff_thread_await_progress(&ref_pic->f,
  399. FFMIN(row, pic_height - 1),
  400. 0);
  401. }
  402. }
  403. }
  404. }
  405. static av_always_inline void mc_dir_part(H264Context *h, Picture *pic,
  406. int n, int square, int height,
  407. int delta, int list,
  408. uint8_t *dest_y, uint8_t *dest_cb,
  409. uint8_t *dest_cr,
  410. int src_x_offset, int src_y_offset,
  411. qpel_mc_func *qpix_op,
  412. h264_chroma_mc_func chroma_op,
  413. int pixel_shift, int chroma_idc)
  414. {
  415. MpegEncContext *const s = &h->s;
  416. const int mx = h->mv_cache[list][scan8[n]][0] + src_x_offset * 8;
  417. int my = h->mv_cache[list][scan8[n]][1] + src_y_offset * 8;
  418. const int luma_xy = (mx & 3) + ((my & 3) << 2);
  419. int offset = ((mx >> 2) << pixel_shift) + (my >> 2) * h->mb_linesize;
  420. uint8_t *src_y = pic->f.data[0] + offset;
  421. uint8_t *src_cb, *src_cr;
  422. int extra_width = h->emu_edge_width;
  423. int extra_height = h->emu_edge_height;
  424. int emu = 0;
  425. const int full_mx = mx >> 2;
  426. const int full_my = my >> 2;
  427. const int pic_width = 16 * s->mb_width;
  428. const int pic_height = 16 * s->mb_height >> MB_FIELD;
  429. int ysh;
  430. if (mx & 7)
  431. extra_width -= 3;
  432. if (my & 7)
  433. extra_height -= 3;
  434. if (full_mx < 0 - extra_width ||
  435. full_my < 0 - extra_height ||
  436. full_mx + 16 /*FIXME*/ > pic_width + extra_width ||
  437. full_my + 16 /*FIXME*/ > pic_height + extra_height) {
  438. s->dsp.emulated_edge_mc(s->edge_emu_buffer,
  439. src_y - (2 << pixel_shift) - 2 * h->mb_linesize,
  440. h->mb_linesize,
  441. 16 + 5, 16 + 5 /*FIXME*/, full_mx - 2,
  442. full_my - 2, pic_width, pic_height);
  443. src_y = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  444. emu = 1;
  445. }
  446. qpix_op[luma_xy](dest_y, src_y, h->mb_linesize); // FIXME try variable height perhaps?
  447. if (!square)
  448. qpix_op[luma_xy](dest_y + delta, src_y + delta, h->mb_linesize);
  449. if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY)
  450. return;
  451. if (chroma_idc == 3 /* yuv444 */) {
  452. src_cb = pic->f.data[1] + offset;
  453. if (emu) {
  454. s->dsp.emulated_edge_mc(s->edge_emu_buffer,
  455. src_cb - (2 << pixel_shift) - 2 * h->mb_linesize,
  456. h->mb_linesize,
  457. 16 + 5, 16 + 5 /*FIXME*/,
  458. full_mx - 2, full_my - 2,
  459. pic_width, pic_height);
  460. src_cb = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  461. }
  462. qpix_op[luma_xy](dest_cb, src_cb, h->mb_linesize); // FIXME try variable height perhaps?
  463. if (!square)
  464. qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
  465. src_cr = pic->f.data[2] + offset;
  466. if (emu) {
  467. s->dsp.emulated_edge_mc(s->edge_emu_buffer,
  468. src_cr - (2 << pixel_shift) - 2 * h->mb_linesize,
  469. h->mb_linesize,
  470. 16 + 5, 16 + 5 /*FIXME*/,
  471. full_mx - 2, full_my - 2,
  472. pic_width, pic_height);
  473. src_cr = s->edge_emu_buffer + (2 << pixel_shift) + 2 * h->mb_linesize;
  474. }
  475. qpix_op[luma_xy](dest_cr, src_cr, h->mb_linesize); // FIXME try variable height perhaps?
  476. if (!square)
  477. qpix_op[luma_xy](dest_cr + delta, src_cr + delta, h->mb_linesize);
  478. return;
  479. }
  480. ysh = 3 - (chroma_idc == 2 /* yuv422 */);
  481. if (chroma_idc == 1 /* yuv420 */ && MB_FIELD) {
  482. // chroma offset when predicting from a field of opposite parity
  483. my += 2 * ((s->mb_y & 1) - (pic->f.reference - 1));
  484. emu |= (my >> 3) < 0 || (my >> 3) + 8 >= (pic_height >> 1);
  485. }
  486. src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) +
  487. (my >> ysh) * h->mb_uvlinesize;
  488. src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) +
  489. (my >> ysh) * h->mb_uvlinesize;
  490. if (emu) {
  491. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize,
  492. 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
  493. pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
  494. src_cb = s->edge_emu_buffer;
  495. }
  496. chroma_op(dest_cb, src_cb, h->mb_uvlinesize,
  497. height >> (chroma_idc == 1 /* yuv420 */),
  498. mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
  499. if (emu) {
  500. s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize,
  501. 9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
  502. pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
  503. src_cr = s->edge_emu_buffer;
  504. }
  505. chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
  506. mx & 7, (my << (chroma_idc == 2 /* yuv422 */)) & 7);
  507. }
  508. static av_always_inline void mc_part_std(H264Context *h, int n, int square,
  509. int height, int delta,
  510. uint8_t *dest_y, uint8_t *dest_cb,
  511. uint8_t *dest_cr,
  512. int x_offset, int y_offset,
  513. qpel_mc_func *qpix_put,
  514. h264_chroma_mc_func chroma_put,
  515. qpel_mc_func *qpix_avg,
  516. h264_chroma_mc_func chroma_avg,
  517. int list0, int list1,
  518. int pixel_shift, int chroma_idc)
  519. {
  520. MpegEncContext *const s = &h->s;
  521. qpel_mc_func *qpix_op = qpix_put;
  522. h264_chroma_mc_func chroma_op = chroma_put;
  523. dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  524. if (chroma_idc == 3 /* yuv444 */) {
  525. dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  526. dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  527. } else if (chroma_idc == 2 /* yuv422 */) {
  528. dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  529. dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  530. } else { /* yuv420 */
  531. dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  532. dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  533. }
  534. x_offset += 8 * s->mb_x;
  535. y_offset += 8 * (s->mb_y >> MB_FIELD);
  536. if (list0) {
  537. Picture *ref = &h->ref_list[0][h->ref_cache[0][scan8[n]]];
  538. mc_dir_part(h, ref, n, square, height, delta, 0,
  539. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  540. qpix_op, chroma_op, pixel_shift, chroma_idc);
  541. qpix_op = qpix_avg;
  542. chroma_op = chroma_avg;
  543. }
  544. if (list1) {
  545. Picture *ref = &h->ref_list[1][h->ref_cache[1][scan8[n]]];
  546. mc_dir_part(h, ref, n, square, height, delta, 1,
  547. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  548. qpix_op, chroma_op, pixel_shift, chroma_idc);
  549. }
  550. }
  551. static av_always_inline void mc_part_weighted(H264Context *h, int n, int square,
  552. int height, int delta,
  553. uint8_t *dest_y, uint8_t *dest_cb,
  554. uint8_t *dest_cr,
  555. int x_offset, int y_offset,
  556. qpel_mc_func *qpix_put,
  557. h264_chroma_mc_func chroma_put,
  558. h264_weight_func luma_weight_op,
  559. h264_weight_func chroma_weight_op,
  560. h264_biweight_func luma_weight_avg,
  561. h264_biweight_func chroma_weight_avg,
  562. int list0, int list1,
  563. int pixel_shift, int chroma_idc)
  564. {
  565. MpegEncContext *const s = &h->s;
  566. int chroma_height;
  567. dest_y += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  568. if (chroma_idc == 3 /* yuv444 */) {
  569. chroma_height = height;
  570. chroma_weight_avg = luma_weight_avg;
  571. chroma_weight_op = luma_weight_op;
  572. dest_cb += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  573. dest_cr += (2 * x_offset << pixel_shift) + 2 * y_offset * h->mb_linesize;
  574. } else if (chroma_idc == 2 /* yuv422 */) {
  575. chroma_height = height;
  576. dest_cb += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  577. dest_cr += (x_offset << pixel_shift) + 2 * y_offset * h->mb_uvlinesize;
  578. } else { /* yuv420 */
  579. chroma_height = height >> 1;
  580. dest_cb += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  581. dest_cr += (x_offset << pixel_shift) + y_offset * h->mb_uvlinesize;
  582. }
  583. x_offset += 8 * s->mb_x;
  584. y_offset += 8 * (s->mb_y >> MB_FIELD);
  585. if (list0 && list1) {
  586. /* don't optimize for luma-only case, since B-frames usually
  587. * use implicit weights => chroma too. */
  588. uint8_t *tmp_cb = s->obmc_scratchpad;
  589. uint8_t *tmp_cr = s->obmc_scratchpad + (16 << pixel_shift);
  590. uint8_t *tmp_y = s->obmc_scratchpad + 16 * h->mb_uvlinesize;
  591. int refn0 = h->ref_cache[0][scan8[n]];
  592. int refn1 = h->ref_cache[1][scan8[n]];
  593. mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
  594. dest_y, dest_cb, dest_cr,
  595. x_offset, y_offset, qpix_put, chroma_put,
  596. pixel_shift, chroma_idc);
  597. mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
  598. tmp_y, tmp_cb, tmp_cr,
  599. x_offset, y_offset, qpix_put, chroma_put,
  600. pixel_shift, chroma_idc);
  601. if (h->use_weight == 2) {
  602. int weight0 = h->implicit_weight[refn0][refn1][s->mb_y & 1];
  603. int weight1 = 64 - weight0;
  604. luma_weight_avg(dest_y, tmp_y, h->mb_linesize,
  605. height, 5, weight0, weight1, 0);
  606. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
  607. chroma_height, 5, weight0, weight1, 0);
  608. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
  609. chroma_height, 5, weight0, weight1, 0);
  610. } else {
  611. luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height,
  612. h->luma_log2_weight_denom,
  613. h->luma_weight[refn0][0][0],
  614. h->luma_weight[refn1][1][0],
  615. h->luma_weight[refn0][0][1] +
  616. h->luma_weight[refn1][1][1]);
  617. chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height,
  618. h->chroma_log2_weight_denom,
  619. h->chroma_weight[refn0][0][0][0],
  620. h->chroma_weight[refn1][1][0][0],
  621. h->chroma_weight[refn0][0][0][1] +
  622. h->chroma_weight[refn1][1][0][1]);
  623. chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height,
  624. h->chroma_log2_weight_denom,
  625. h->chroma_weight[refn0][0][1][0],
  626. h->chroma_weight[refn1][1][1][0],
  627. h->chroma_weight[refn0][0][1][1] +
  628. h->chroma_weight[refn1][1][1][1]);
  629. }
  630. } else {
  631. int list = list1 ? 1 : 0;
  632. int refn = h->ref_cache[list][scan8[n]];
  633. Picture *ref = &h->ref_list[list][refn];
  634. mc_dir_part(h, ref, n, square, height, delta, list,
  635. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  636. qpix_put, chroma_put, pixel_shift, chroma_idc);
  637. luma_weight_op(dest_y, h->mb_linesize, height,
  638. h->luma_log2_weight_denom,
  639. h->luma_weight[refn][list][0],
  640. h->luma_weight[refn][list][1]);
  641. if (h->use_weight_chroma) {
  642. chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height,
  643. h->chroma_log2_weight_denom,
  644. h->chroma_weight[refn][list][0][0],
  645. h->chroma_weight[refn][list][0][1]);
  646. chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height,
  647. h->chroma_log2_weight_denom,
  648. h->chroma_weight[refn][list][1][0],
  649. h->chroma_weight[refn][list][1][1]);
  650. }
  651. }
  652. }
  653. static av_always_inline void mc_part(H264Context *h, int n, int square,
  654. int height, int delta,
  655. uint8_t *dest_y, uint8_t *dest_cb,
  656. uint8_t *dest_cr,
  657. int x_offset, int y_offset,
  658. qpel_mc_func *qpix_put,
  659. h264_chroma_mc_func chroma_put,
  660. qpel_mc_func *qpix_avg,
  661. h264_chroma_mc_func chroma_avg,
  662. h264_weight_func *weight_op,
  663. h264_biweight_func *weight_avg,
  664. int list0, int list1,
  665. int pixel_shift, int chroma_idc)
  666. {
  667. if ((h->use_weight == 2 && list0 && list1 &&
  668. (h->implicit_weight[h->ref_cache[0][scan8[n]]][h->ref_cache[1][scan8[n]]][h->s.mb_y & 1] != 32)) ||
  669. h->use_weight == 1)
  670. mc_part_weighted(h, n, square, height, delta, dest_y, dest_cb, dest_cr,
  671. x_offset, y_offset, qpix_put, chroma_put,
  672. weight_op[0], weight_op[1], weight_avg[0],
  673. weight_avg[1], list0, list1, pixel_shift, chroma_idc);
  674. else
  675. mc_part_std(h, n, square, height, delta, dest_y, dest_cb, dest_cr,
  676. x_offset, y_offset, qpix_put, chroma_put, qpix_avg,
  677. chroma_avg, list0, list1, pixel_shift, chroma_idc);
  678. }
  679. static av_always_inline void prefetch_motion(H264Context *h, int list,
  680. int pixel_shift, int chroma_idc)
  681. {
  682. /* fetch pixels for estimated mv 4 macroblocks ahead
  683. * optimized for 64byte cache lines */
  684. MpegEncContext *const s = &h->s;
  685. const int refn = h->ref_cache[list][scan8[0]];
  686. if (refn >= 0) {
  687. const int mx = (h->mv_cache[list][scan8[0]][0] >> 2) + 16 * s->mb_x + 8;
  688. const int my = (h->mv_cache[list][scan8[0]][1] >> 2) + 16 * s->mb_y;
  689. uint8_t **src = h->ref_list[list][refn].f.data;
  690. int off = (mx << pixel_shift) +
  691. (my + (s->mb_x & 3) * 4) * h->mb_linesize +
  692. (64 << pixel_shift);
  693. s->dsp.prefetch(src[0] + off, s->linesize, 4);
  694. if (chroma_idc == 3 /* yuv444 */) {
  695. s->dsp.prefetch(src[1] + off, s->linesize, 4);
  696. s->dsp.prefetch(src[2] + off, s->linesize, 4);
  697. } else {
  698. off= (((mx>>1)+64)<<pixel_shift) + ((my>>1) + (s->mb_x&7))*s->uvlinesize;
  699. s->dsp.prefetch(src[1] + off, src[2] - src[1], 2);
  700. }
  701. }
  702. }
  703. static av_always_inline void hl_motion(H264Context *h, uint8_t *dest_y,
  704. uint8_t *dest_cb, uint8_t *dest_cr,
  705. qpel_mc_func(*qpix_put)[16],
  706. h264_chroma_mc_func(*chroma_put),
  707. qpel_mc_func(*qpix_avg)[16],
  708. h264_chroma_mc_func(*chroma_avg),
  709. h264_weight_func *weight_op,
  710. h264_biweight_func *weight_avg,
  711. int pixel_shift, int chroma_idc)
  712. {
  713. MpegEncContext *const s = &h->s;
  714. const int mb_xy = h->mb_xy;
  715. const int mb_type = s->current_picture.f.mb_type[mb_xy];
  716. assert(IS_INTER(mb_type));
  717. if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
  718. await_references(h);
  719. prefetch_motion(h, 0, pixel_shift, chroma_idc);
  720. if (IS_16X16(mb_type)) {
  721. mc_part(h, 0, 1, 16, 0, dest_y, dest_cb, dest_cr, 0, 0,
  722. qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
  723. weight_op, weight_avg,
  724. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
  725. pixel_shift, chroma_idc);
  726. } else if (IS_16X8(mb_type)) {
  727. mc_part(h, 0, 0, 8, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 0,
  728. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  729. weight_op, weight_avg,
  730. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
  731. pixel_shift, chroma_idc);
  732. mc_part(h, 8, 0, 8, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 4,
  733. qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
  734. weight_op, weight_avg,
  735. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),
  736. pixel_shift, chroma_idc);
  737. } else if (IS_8X16(mb_type)) {
  738. mc_part(h, 0, 0, 16, 8 * h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
  739. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  740. &weight_op[1], &weight_avg[1],
  741. IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
  742. pixel_shift, chroma_idc);
  743. mc_part(h, 4, 0, 16, 8 * h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
  744. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  745. &weight_op[1], &weight_avg[1],
  746. IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),
  747. pixel_shift, chroma_idc);
  748. } else {
  749. int i;
  750. assert(IS_8X8(mb_type));
  751. for (i = 0; i < 4; i++) {
  752. const int sub_mb_type = h->sub_mb_type[i];
  753. const int n = 4 * i;
  754. int x_offset = (i & 1) << 2;
  755. int y_offset = (i & 2) << 1;
  756. if (IS_SUB_8X8(sub_mb_type)) {
  757. mc_part(h, n, 1, 8, 0, dest_y, dest_cb, dest_cr,
  758. x_offset, y_offset,
  759. qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
  760. &weight_op[1], &weight_avg[1],
  761. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
  762. pixel_shift, chroma_idc);
  763. } else if (IS_SUB_8X4(sub_mb_type)) {
  764. mc_part(h, n, 0, 4, 4 << pixel_shift, dest_y, dest_cb, dest_cr,
  765. x_offset, y_offset,
  766. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  767. &weight_op[1], &weight_avg[1],
  768. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
  769. pixel_shift, chroma_idc);
  770. mc_part(h, n + 2, 0, 4, 4 << pixel_shift,
  771. dest_y, dest_cb, dest_cr, x_offset, y_offset + 2,
  772. qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
  773. &weight_op[1], &weight_avg[1],
  774. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
  775. pixel_shift, chroma_idc);
  776. } else if (IS_SUB_4X8(sub_mb_type)) {
  777. mc_part(h, n, 0, 8, 4 * h->mb_linesize,
  778. dest_y, dest_cb, dest_cr, x_offset, y_offset,
  779. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  780. &weight_op[2], &weight_avg[2],
  781. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
  782. pixel_shift, chroma_idc);
  783. mc_part(h, n + 1, 0, 8, 4 * h->mb_linesize,
  784. dest_y, dest_cb, dest_cr, x_offset + 2, y_offset,
  785. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  786. &weight_op[2], &weight_avg[2],
  787. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
  788. pixel_shift, chroma_idc);
  789. } else {
  790. int j;
  791. assert(IS_SUB_4X4(sub_mb_type));
  792. for (j = 0; j < 4; j++) {
  793. int sub_x_offset = x_offset + 2 * (j & 1);
  794. int sub_y_offset = y_offset + (j & 2);
  795. mc_part(h, n + j, 1, 4, 0,
  796. dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
  797. qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
  798. &weight_op[2], &weight_avg[2],
  799. IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
  800. pixel_shift, chroma_idc);
  801. }
  802. }
  803. }
  804. }
  805. prefetch_motion(h, 1, pixel_shift, chroma_idc);
  806. }
  807. static av_always_inline void hl_motion_420(H264Context *h, uint8_t *dest_y,
  808. uint8_t *dest_cb, uint8_t *dest_cr,
  809. qpel_mc_func(*qpix_put)[16],
  810. h264_chroma_mc_func(*chroma_put),
  811. qpel_mc_func(*qpix_avg)[16],
  812. h264_chroma_mc_func(*chroma_avg),
  813. h264_weight_func *weight_op,
  814. h264_biweight_func *weight_avg,
  815. int pixel_shift)
  816. {
  817. hl_motion(h, dest_y, dest_cb, dest_cr, qpix_put, chroma_put,
  818. qpix_avg, chroma_avg, weight_op, weight_avg, pixel_shift, 1);
  819. }
  820. static av_always_inline void hl_motion_422(H264Context *h, uint8_t *dest_y,
  821. uint8_t *dest_cb, uint8_t *dest_cr,
  822. qpel_mc_func(*qpix_put)[16],
  823. h264_chroma_mc_func(*chroma_put),
  824. qpel_mc_func(*qpix_avg)[16],
  825. h264_chroma_mc_func(*chroma_avg),
  826. h264_weight_func *weight_op,
  827. h264_biweight_func *weight_avg,
  828. int pixel_shift)
  829. {
  830. hl_motion(h, dest_y, dest_cb, dest_cr, qpix_put, chroma_put,
  831. qpix_avg, chroma_avg, weight_op, weight_avg, pixel_shift, 2);
  832. }
  833. static void free_tables(H264Context *h, int free_rbsp)
  834. {
  835. int i;
  836. H264Context *hx;
  837. av_freep(&h->intra4x4_pred_mode);
  838. av_freep(&h->chroma_pred_mode_table);
  839. av_freep(&h->cbp_table);
  840. av_freep(&h->mvd_table[0]);
  841. av_freep(&h->mvd_table[1]);
  842. av_freep(&h->direct_table);
  843. av_freep(&h->non_zero_count);
  844. av_freep(&h->slice_table_base);
  845. h->slice_table = NULL;
  846. av_freep(&h->list_counts);
  847. av_freep(&h->mb2b_xy);
  848. av_freep(&h->mb2br_xy);
  849. for (i = 0; i < MAX_THREADS; i++) {
  850. hx = h->thread_context[i];
  851. if (!hx)
  852. continue;
  853. av_freep(&hx->top_borders[1]);
  854. av_freep(&hx->top_borders[0]);
  855. av_freep(&hx->s.obmc_scratchpad);
  856. if (free_rbsp) {
  857. av_freep(&hx->rbsp_buffer[1]);
  858. av_freep(&hx->rbsp_buffer[0]);
  859. hx->rbsp_buffer_size[0] = 0;
  860. hx->rbsp_buffer_size[1] = 0;
  861. }
  862. if (i)
  863. av_freep(&h->thread_context[i]);
  864. }
  865. }
  866. static void init_dequant8_coeff_table(H264Context *h)
  867. {
  868. int i, j, q, x;
  869. const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
  870. for (i = 0; i < 6; i++) {
  871. h->dequant8_coeff[i] = h->dequant8_buffer[i];
  872. for (j = 0; j < i; j++)
  873. if (!memcmp(h->pps.scaling_matrix8[j], h->pps.scaling_matrix8[i],
  874. 64 * sizeof(uint8_t))) {
  875. h->dequant8_coeff[i] = h->dequant8_buffer[j];
  876. break;
  877. }
  878. if (j < i)
  879. continue;
  880. for (q = 0; q < max_qp + 1; q++) {
  881. int shift = div6[q];
  882. int idx = rem6[q];
  883. for (x = 0; x < 64; x++)
  884. h->dequant8_coeff[i][q][(x >> 3) | ((x & 7) << 3)] =
  885. ((uint32_t)dequant8_coeff_init[idx][dequant8_coeff_init_scan[((x >> 1) & 12) | (x & 3)]] *
  886. h->pps.scaling_matrix8[i][x]) << shift;
  887. }
  888. }
  889. }
  890. static void init_dequant4_coeff_table(H264Context *h)
  891. {
  892. int i, j, q, x;
  893. const int max_qp = 51 + 6 * (h->sps.bit_depth_luma - 8);
  894. for (i = 0; i < 6; i++) {
  895. h->dequant4_coeff[i] = h->dequant4_buffer[i];
  896. for (j = 0; j < i; j++)
  897. if (!memcmp(h->pps.scaling_matrix4[j], h->pps.scaling_matrix4[i],
  898. 16 * sizeof(uint8_t))) {
  899. h->dequant4_coeff[i] = h->dequant4_buffer[j];
  900. break;
  901. }
  902. if (j < i)
  903. continue;
  904. for (q = 0; q < max_qp + 1; q++) {
  905. int shift = div6[q] + 2;
  906. int idx = rem6[q];
  907. for (x = 0; x < 16; x++)
  908. h->dequant4_coeff[i][q][(x >> 2) | ((x << 2) & 0xF)] =
  909. ((uint32_t)dequant4_coeff_init[idx][(x & 1) + ((x >> 2) & 1)] *
  910. h->pps.scaling_matrix4[i][x]) << shift;
  911. }
  912. }
  913. }
  914. static void init_dequant_tables(H264Context *h)
  915. {
  916. int i, x;
  917. init_dequant4_coeff_table(h);
  918. if (h->pps.transform_8x8_mode)
  919. init_dequant8_coeff_table(h);
  920. if (h->sps.transform_bypass) {
  921. for (i = 0; i < 6; i++)
  922. for (x = 0; x < 16; x++)
  923. h->dequant4_coeff[i][0][x] = 1 << 6;
  924. if (h->pps.transform_8x8_mode)
  925. for (i = 0; i < 6; i++)
  926. for (x = 0; x < 64; x++)
  927. h->dequant8_coeff[i][0][x] = 1 << 6;
  928. }
  929. }
  930. int ff_h264_alloc_tables(H264Context *h)
  931. {
  932. MpegEncContext *const s = &h->s;
  933. const int big_mb_num = s->mb_stride * (s->mb_height + 1);
  934. const int row_mb_num = 2*s->mb_stride*FFMAX(s->avctx->thread_count, 1);
  935. int x, y;
  936. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->intra4x4_pred_mode,
  937. row_mb_num * 8 * sizeof(uint8_t), fail)
  938. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->non_zero_count,
  939. big_mb_num * 48 * sizeof(uint8_t), fail)
  940. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->slice_table_base,
  941. (big_mb_num + s->mb_stride) * sizeof(*h->slice_table_base), fail)
  942. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->cbp_table,
  943. big_mb_num * sizeof(uint16_t), fail)
  944. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->chroma_pred_mode_table,
  945. big_mb_num * sizeof(uint8_t), fail)
  946. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[0],
  947. 16 * row_mb_num * sizeof(uint8_t), fail);
  948. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mvd_table[1],
  949. 16 * row_mb_num * sizeof(uint8_t), fail);
  950. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->direct_table,
  951. 4 * big_mb_num * sizeof(uint8_t), fail);
  952. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->list_counts,
  953. big_mb_num * sizeof(uint8_t), fail)
  954. memset(h->slice_table_base, -1,
  955. (big_mb_num + s->mb_stride) * sizeof(*h->slice_table_base));
  956. h->slice_table = h->slice_table_base + s->mb_stride * 2 + 1;
  957. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2b_xy,
  958. big_mb_num * sizeof(uint32_t), fail);
  959. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->mb2br_xy,
  960. big_mb_num * sizeof(uint32_t), fail);
  961. for (y = 0; y < s->mb_height; y++)
  962. for (x = 0; x < s->mb_width; x++) {
  963. const int mb_xy = x + y * s->mb_stride;
  964. const int b_xy = 4 * x + 4 * y * h->b_stride;
  965. h->mb2b_xy[mb_xy] = b_xy;
  966. h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * s->mb_stride)));
  967. }
  968. s->obmc_scratchpad = NULL;
  969. if (!h->dequant4_coeff[0])
  970. init_dequant_tables(h);
  971. return 0;
  972. fail:
  973. free_tables(h, 1);
  974. return -1;
  975. }
  976. /**
  977. * Mimic alloc_tables(), but for every context thread.
  978. */
  979. static void clone_tables(H264Context *dst, H264Context *src, int i)
  980. {
  981. MpegEncContext *const s = &src->s;
  982. dst->intra4x4_pred_mode = src->intra4x4_pred_mode + i * 8 * 2 * s->mb_stride;
  983. dst->non_zero_count = src->non_zero_count;
  984. dst->slice_table = src->slice_table;
  985. dst->cbp_table = src->cbp_table;
  986. dst->mb2b_xy = src->mb2b_xy;
  987. dst->mb2br_xy = src->mb2br_xy;
  988. dst->chroma_pred_mode_table = src->chroma_pred_mode_table;
  989. dst->mvd_table[0] = src->mvd_table[0] + i * 8 * 2 * s->mb_stride;
  990. dst->mvd_table[1] = src->mvd_table[1] + i * 8 * 2 * s->mb_stride;
  991. dst->direct_table = src->direct_table;
  992. dst->list_counts = src->list_counts;
  993. dst->s.obmc_scratchpad = NULL;
  994. ff_h264_pred_init(&dst->hpc, src->s.codec_id, src->sps.bit_depth_luma,
  995. src->sps.chroma_format_idc);
  996. }
  997. /**
  998. * Init context
  999. * Allocate buffers which are not shared amongst multiple threads.
  1000. */
  1001. static int context_init(H264Context *h)
  1002. {
  1003. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[0],
  1004. h->s.mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
  1005. FF_ALLOCZ_OR_GOTO(h->s.avctx, h->top_borders[1],
  1006. h->s.mb_width * 16 * 3 * sizeof(uint8_t) * 2, fail)
  1007. h->ref_cache[0][scan8[5] + 1] =
  1008. h->ref_cache[0][scan8[7] + 1] =
  1009. h->ref_cache[0][scan8[13] + 1] =
  1010. h->ref_cache[1][scan8[5] + 1] =
  1011. h->ref_cache[1][scan8[7] + 1] =
  1012. h->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
  1013. return 0;
  1014. fail:
  1015. return -1; // free_tables will clean up for us
  1016. }
  1017. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size);
  1018. static av_cold void common_init(H264Context *h)
  1019. {
  1020. MpegEncContext *const s = &h->s;
  1021. s->width = s->avctx->width;
  1022. s->height = s->avctx->height;
  1023. s->codec_id = s->avctx->codec->id;
  1024. s->avctx->bits_per_raw_sample = 8;
  1025. h->cur_chroma_format_idc = 1;
  1026. ff_h264dsp_init(&h->h264dsp,
  1027. s->avctx->bits_per_raw_sample, h->cur_chroma_format_idc);
  1028. ff_h264_pred_init(&h->hpc, s->codec_id,
  1029. s->avctx->bits_per_raw_sample, h->cur_chroma_format_idc);
  1030. h->dequant_coeff_pps = -1;
  1031. s->unrestricted_mv = 1;
  1032. s->dsp.dct_bits = 16;
  1033. /* needed so that IDCT permutation is known early */
  1034. ff_dsputil_init(&s->dsp, s->avctx);
  1035. memset(h->pps.scaling_matrix4, 16, 6 * 16 * sizeof(uint8_t));
  1036. memset(h->pps.scaling_matrix8, 16, 2 * 64 * sizeof(uint8_t));
  1037. }
  1038. int ff_h264_decode_extradata(H264Context *h, const uint8_t *buf, int size)
  1039. {
  1040. AVCodecContext *avctx = h->s.avctx;
  1041. if (!buf || size <= 0)
  1042. return -1;
  1043. if (buf[0] == 1) {
  1044. int i, cnt, nalsize;
  1045. const unsigned char *p = buf;
  1046. h->is_avc = 1;
  1047. if (size < 7) {
  1048. av_log(avctx, AV_LOG_ERROR, "avcC too short\n");
  1049. return -1;
  1050. }
  1051. /* sps and pps in the avcC always have length coded with 2 bytes,
  1052. * so put a fake nal_length_size = 2 while parsing them */
  1053. h->nal_length_size = 2;
  1054. // Decode sps from avcC
  1055. cnt = *(p + 5) & 0x1f; // Number of sps
  1056. p += 6;
  1057. for (i = 0; i < cnt; i++) {
  1058. nalsize = AV_RB16(p) + 2;
  1059. if(nalsize > size - (p-buf))
  1060. return -1;
  1061. if (decode_nal_units(h, p, nalsize) < 0) {
  1062. av_log(avctx, AV_LOG_ERROR,
  1063. "Decoding sps %d from avcC failed\n", i);
  1064. return -1;
  1065. }
  1066. p += nalsize;
  1067. }
  1068. // Decode pps from avcC
  1069. cnt = *(p++); // Number of pps
  1070. for (i = 0; i < cnt; i++) {
  1071. nalsize = AV_RB16(p) + 2;
  1072. if(nalsize > size - (p-buf))
  1073. return -1;
  1074. if (decode_nal_units(h, p, nalsize) < 0) {
  1075. av_log(avctx, AV_LOG_ERROR,
  1076. "Decoding pps %d from avcC failed\n", i);
  1077. return -1;
  1078. }
  1079. p += nalsize;
  1080. }
  1081. // Now store right nal length size, that will be used to parse all other nals
  1082. h->nal_length_size = (buf[4] & 0x03) + 1;
  1083. } else {
  1084. h->is_avc = 0;
  1085. if (decode_nal_units(h, buf, size) < 0)
  1086. return -1;
  1087. }
  1088. return size;
  1089. }
  1090. av_cold int ff_h264_decode_init(AVCodecContext *avctx)
  1091. {
  1092. H264Context *h = avctx->priv_data;
  1093. MpegEncContext *const s = &h->s;
  1094. int i;
  1095. ff_MPV_decode_defaults(s);
  1096. s->avctx = avctx;
  1097. common_init(h);
  1098. s->out_format = FMT_H264;
  1099. s->workaround_bugs = avctx->workaround_bugs;
  1100. /* set defaults */
  1101. // s->decode_mb = ff_h263_decode_mb;
  1102. s->quarter_sample = 1;
  1103. if (!avctx->has_b_frames)
  1104. s->low_delay = 1;
  1105. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  1106. ff_h264_decode_init_vlc();
  1107. h->pixel_shift = 0;
  1108. h->sps.bit_depth_luma = avctx->bits_per_raw_sample = 8;
  1109. h->thread_context[0] = h;
  1110. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  1111. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  1112. h->last_pocs[i] = INT_MIN;
  1113. h->prev_poc_msb = 1 << 16;
  1114. h->prev_frame_num = -1;
  1115. h->x264_build = -1;
  1116. ff_h264_reset_sei(h);
  1117. if (avctx->codec_id == CODEC_ID_H264) {
  1118. if (avctx->ticks_per_frame == 1)
  1119. s->avctx->time_base.den *= 2;
  1120. avctx->ticks_per_frame = 2;
  1121. }
  1122. if (avctx->extradata_size > 0 && avctx->extradata &&
  1123. ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size) < 0)
  1124. return -1;
  1125. if (h->sps.bitstream_restriction_flag &&
  1126. s->avctx->has_b_frames < h->sps.num_reorder_frames) {
  1127. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  1128. s->low_delay = 0;
  1129. }
  1130. return 0;
  1131. }
  1132. #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
  1133. static void copy_picture_range(Picture **to, Picture **from, int count,
  1134. MpegEncContext *new_base,
  1135. MpegEncContext *old_base)
  1136. {
  1137. int i;
  1138. for (i = 0; i < count; i++) {
  1139. assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
  1140. IN_RANGE(from[i], old_base->picture,
  1141. sizeof(Picture) * old_base->picture_count) ||
  1142. !from[i]));
  1143. to[i] = REBASE_PICTURE(from[i], new_base, old_base);
  1144. }
  1145. }
  1146. static void copy_parameter_set(void **to, void **from, int count, int size)
  1147. {
  1148. int i;
  1149. for (i = 0; i < count; i++) {
  1150. if (to[i] && !from[i])
  1151. av_freep(&to[i]);
  1152. else if (from[i] && !to[i])
  1153. to[i] = av_malloc(size);
  1154. if (from[i])
  1155. memcpy(to[i], from[i], size);
  1156. }
  1157. }
  1158. static int decode_init_thread_copy(AVCodecContext *avctx)
  1159. {
  1160. H264Context *h = avctx->priv_data;
  1161. if (!avctx->internal->is_copy)
  1162. return 0;
  1163. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  1164. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  1165. return 0;
  1166. }
  1167. #define copy_fields(to, from, start_field, end_field) \
  1168. memcpy(&to->start_field, &from->start_field, \
  1169. (char *)&to->end_field - (char *)&to->start_field)
  1170. static int decode_update_thread_context(AVCodecContext *dst,
  1171. const AVCodecContext *src)
  1172. {
  1173. H264Context *h = dst->priv_data, *h1 = src->priv_data;
  1174. MpegEncContext *const s = &h->s, *const s1 = &h1->s;
  1175. int inited = s->context_initialized, err;
  1176. int i;
  1177. if (dst == src)
  1178. return 0;
  1179. err = ff_mpeg_update_thread_context(dst, src);
  1180. if (err)
  1181. return err;
  1182. // FIXME handle width/height changing
  1183. if (!inited) {
  1184. for (i = 0; i < MAX_SPS_COUNT; i++)
  1185. av_freep(h->sps_buffers + i);
  1186. for (i = 0; i < MAX_PPS_COUNT; i++)
  1187. av_freep(h->pps_buffers + i);
  1188. // copy all fields after MpegEnc
  1189. memcpy(&h->s + 1, &h1->s + 1,
  1190. sizeof(H264Context) - sizeof(MpegEncContext));
  1191. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  1192. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  1193. if (s1->context_initialized) {
  1194. if (ff_h264_alloc_tables(h) < 0) {
  1195. av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
  1196. return AVERROR(ENOMEM);
  1197. }
  1198. context_init(h);
  1199. /* frame_start may not be called for the next thread (if it's decoding
  1200. * a bottom field) so this has to be allocated here */
  1201. h->s.obmc_scratchpad = av_malloc(16 * 6 * s->linesize);
  1202. }
  1203. for (i = 0; i < 2; i++) {
  1204. h->rbsp_buffer[i] = NULL;
  1205. h->rbsp_buffer_size[i] = 0;
  1206. }
  1207. h->thread_context[0] = h;
  1208. s->dsp.clear_blocks(h->mb);
  1209. s->dsp.clear_blocks(h->mb + (24 * 16 << h->pixel_shift));
  1210. }
  1211. // extradata/NAL handling
  1212. h->is_avc = h1->is_avc;
  1213. // SPS/PPS
  1214. copy_parameter_set((void **)h->sps_buffers, (void **)h1->sps_buffers,
  1215. MAX_SPS_COUNT, sizeof(SPS));
  1216. h->sps = h1->sps;
  1217. copy_parameter_set((void **)h->pps_buffers, (void **)h1->pps_buffers,
  1218. MAX_PPS_COUNT, sizeof(PPS));
  1219. h->pps = h1->pps;
  1220. // Dequantization matrices
  1221. // FIXME these are big - can they be only copied when PPS changes?
  1222. copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
  1223. for (i = 0; i < 6; i++)
  1224. h->dequant4_coeff[i] = h->dequant4_buffer[0] +
  1225. (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
  1226. for (i = 0; i < 6; i++)
  1227. h->dequant8_coeff[i] = h->dequant8_buffer[0] +
  1228. (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
  1229. h->dequant_coeff_pps = h1->dequant_coeff_pps;
  1230. // POC timing
  1231. copy_fields(h, h1, poc_lsb, redundant_pic_count);
  1232. // reference lists
  1233. copy_fields(h, h1, ref_count, list_count);
  1234. copy_fields(h, h1, ref_list, intra_gb);
  1235. copy_fields(h, h1, short_ref, cabac_init_idc);
  1236. copy_picture_range(h->short_ref, h1->short_ref, 32, s, s1);
  1237. copy_picture_range(h->long_ref, h1->long_ref, 32, s, s1);
  1238. copy_picture_range(h->delayed_pic, h1->delayed_pic,
  1239. MAX_DELAYED_PIC_COUNT + 2, s, s1);
  1240. h->last_slice_type = h1->last_slice_type;
  1241. h->sync = h1->sync;
  1242. if (!s->current_picture_ptr)
  1243. return 0;
  1244. if (!s->dropable) {
  1245. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1246. h->prev_poc_msb = h->poc_msb;
  1247. h->prev_poc_lsb = h->poc_lsb;
  1248. }
  1249. h->prev_frame_num_offset = h->frame_num_offset;
  1250. h->prev_frame_num = h->frame_num;
  1251. h->outputed_poc = h->next_outputed_poc;
  1252. return err;
  1253. }
  1254. int ff_h264_frame_start(H264Context *h)
  1255. {
  1256. MpegEncContext *const s = &h->s;
  1257. int i;
  1258. const int pixel_shift = h->pixel_shift;
  1259. if (ff_MPV_frame_start(s, s->avctx) < 0)
  1260. return -1;
  1261. ff_er_frame_start(s);
  1262. /*
  1263. * ff_MPV_frame_start uses pict_type to derive key_frame.
  1264. * This is incorrect for H.264; IDR markings must be used.
  1265. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  1266. * See decode_nal_units().
  1267. */
  1268. s->current_picture_ptr->f.key_frame = 0;
  1269. s->current_picture_ptr->sync = 0;
  1270. s->current_picture_ptr->mmco_reset = 0;
  1271. assert(s->linesize && s->uvlinesize);
  1272. for (i = 0; i < 16; i++) {
  1273. h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * s->linesize * ((scan8[i] - scan8[0]) >> 3);
  1274. h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * s->linesize * ((scan8[i] - scan8[0]) >> 3);
  1275. }
  1276. for (i = 0; i < 16; i++) {
  1277. h->block_offset[16 + i] =
  1278. h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * s->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1279. h->block_offset[48 + 16 + i] =
  1280. h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * s->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1281. }
  1282. /* can't be in alloc_tables because linesize isn't known there.
  1283. * FIXME: redo bipred weight to not require extra buffer? */
  1284. for (i = 0; i < s->slice_context_count; i++)
  1285. if (h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
  1286. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16 * 6 * s->linesize);
  1287. /* Some macroblocks can be accessed before they're available in case
  1288. * of lost slices, MBAFF or threading. */
  1289. memset(h->slice_table, -1,
  1290. (s->mb_height * s->mb_stride - 1) * sizeof(*h->slice_table));
  1291. // s->decode = (s->flags & CODEC_FLAG_PSNR) || !s->encoding ||
  1292. // s->current_picture.f.reference /* || h->contains_intra */ || 1;
  1293. /* We mark the current picture as non-reference after allocating it, so
  1294. * that if we break out due to an error it can be released automatically
  1295. * in the next ff_MPV_frame_start().
  1296. * SVQ3 as well as most other codecs have only last/next/current and thus
  1297. * get released even with set reference, besides SVQ3 and others do not
  1298. * mark frames as reference later "naturally". */
  1299. if (s->codec_id != CODEC_ID_SVQ3)
  1300. s->current_picture_ptr->f.reference = 0;
  1301. s->current_picture_ptr->field_poc[0] =
  1302. s->current_picture_ptr->field_poc[1] = INT_MAX;
  1303. h->next_output_pic = NULL;
  1304. assert(s->current_picture_ptr->long_ref == 0);
  1305. return 0;
  1306. }
  1307. /**
  1308. * Run setup operations that must be run after slice header decoding.
  1309. * This includes finding the next displayed frame.
  1310. *
  1311. * @param h h264 master context
  1312. * @param setup_finished enough NALs have been read that we can call
  1313. * ff_thread_finish_setup()
  1314. */
  1315. static void decode_postinit(H264Context *h, int setup_finished)
  1316. {
  1317. MpegEncContext *const s = &h->s;
  1318. Picture *out = s->current_picture_ptr;
  1319. Picture *cur = s->current_picture_ptr;
  1320. int i, pics, out_of_order, out_idx;
  1321. s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_H264;
  1322. s->current_picture_ptr->f.pict_type = s->pict_type;
  1323. if (h->next_output_pic)
  1324. return;
  1325. if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
  1326. /* FIXME: if we have two PAFF fields in one packet, we can't start
  1327. * the next thread here. If we have one field per packet, we can.
  1328. * The check in decode_nal_units() is not good enough to find this
  1329. * yet, so we assume the worst for now. */
  1330. // if (setup_finished)
  1331. // ff_thread_finish_setup(s->avctx);
  1332. return;
  1333. }
  1334. cur->f.interlaced_frame = 0;
  1335. cur->f.repeat_pict = 0;
  1336. /* Signal interlacing information externally. */
  1337. /* Prioritize picture timing SEI information over used
  1338. * decoding process if it exists. */
  1339. if (h->sps.pic_struct_present_flag) {
  1340. switch (h->sei_pic_struct) {
  1341. case SEI_PIC_STRUCT_FRAME:
  1342. break;
  1343. case SEI_PIC_STRUCT_TOP_FIELD:
  1344. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  1345. cur->f.interlaced_frame = 1;
  1346. break;
  1347. case SEI_PIC_STRUCT_TOP_BOTTOM:
  1348. case SEI_PIC_STRUCT_BOTTOM_TOP:
  1349. if (FIELD_OR_MBAFF_PICTURE)
  1350. cur->f.interlaced_frame = 1;
  1351. else
  1352. // try to flag soft telecine progressive
  1353. cur->f.interlaced_frame = h->prev_interlaced_frame;
  1354. break;
  1355. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  1356. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  1357. /* Signal the possibility of telecined film externally
  1358. * (pic_struct 5,6). From these hints, let the applications
  1359. * decide if they apply deinterlacing. */
  1360. cur->f.repeat_pict = 1;
  1361. break;
  1362. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  1363. // Force progressive here, doubling interlaced frame is a bad idea.
  1364. cur->f.repeat_pict = 2;
  1365. break;
  1366. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  1367. cur->f.repeat_pict = 4;
  1368. break;
  1369. }
  1370. if ((h->sei_ct_type & 3) &&
  1371. h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  1372. cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
  1373. } else {
  1374. /* Derive interlacing flag from used decoding process. */
  1375. cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  1376. }
  1377. h->prev_interlaced_frame = cur->f.interlaced_frame;
  1378. if (cur->field_poc[0] != cur->field_poc[1]) {
  1379. /* Derive top_field_first from field pocs. */
  1380. cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
  1381. } else {
  1382. if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
  1383. /* Use picture timing SEI information. Even if it is a
  1384. * information of a past frame, better than nothing. */
  1385. if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
  1386. h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  1387. cur->f.top_field_first = 1;
  1388. else
  1389. cur->f.top_field_first = 0;
  1390. } else {
  1391. /* Most likely progressive */
  1392. cur->f.top_field_first = 0;
  1393. }
  1394. }
  1395. cur->mmco_reset = h->mmco_reset;
  1396. h->mmco_reset = 0;
  1397. // FIXME do something with unavailable reference frames
  1398. /* Sort B-frames into display order */
  1399. if (h->sps.bitstream_restriction_flag &&
  1400. s->avctx->has_b_frames < h->sps.num_reorder_frames) {
  1401. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  1402. s->low_delay = 0;
  1403. }
  1404. if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
  1405. !h->sps.bitstream_restriction_flag) {
  1406. s->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
  1407. s->low_delay = 0;
  1408. }
  1409. for (i = 0; 1; i++) {
  1410. if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
  1411. if(i)
  1412. h->last_pocs[i-1] = cur->poc;
  1413. break;
  1414. } else if(i) {
  1415. h->last_pocs[i-1]= h->last_pocs[i];
  1416. }
  1417. }
  1418. out_of_order = MAX_DELAYED_PIC_COUNT - i;
  1419. if( cur->f.pict_type == AV_PICTURE_TYPE_B
  1420. || (h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > INT_MIN && h->last_pocs[MAX_DELAYED_PIC_COUNT-1] - h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > 2))
  1421. out_of_order = FFMAX(out_of_order, 1);
  1422. if(s->avctx->has_b_frames < out_of_order && !h->sps.bitstream_restriction_flag){
  1423. av_log(s->avctx, AV_LOG_WARNING, "Increasing reorder buffer to %d\n", out_of_order);
  1424. s->avctx->has_b_frames = out_of_order;
  1425. s->low_delay = 0;
  1426. }
  1427. pics = 0;
  1428. while (h->delayed_pic[pics])
  1429. pics++;
  1430. av_assert0(pics <= MAX_DELAYED_PIC_COUNT);
  1431. h->delayed_pic[pics++] = cur;
  1432. if (cur->f.reference == 0)
  1433. cur->f.reference = DELAYED_PIC_REF;
  1434. out = h->delayed_pic[0];
  1435. out_idx = 0;
  1436. for (i = 1; h->delayed_pic[i] &&
  1437. !h->delayed_pic[i]->f.key_frame &&
  1438. !h->delayed_pic[i]->mmco_reset;
  1439. i++)
  1440. if (h->delayed_pic[i]->poc < out->poc) {
  1441. out = h->delayed_pic[i];
  1442. out_idx = i;
  1443. }
  1444. if (s->avctx->has_b_frames == 0 &&
  1445. (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset))
  1446. h->next_outputed_poc = INT_MIN;
  1447. out_of_order = out->poc < h->next_outputed_poc;
  1448. if (out_of_order || pics > s->avctx->has_b_frames) {
  1449. out->f.reference &= ~DELAYED_PIC_REF;
  1450. // for frame threading, the owner must be the second field's thread or
  1451. // else the first thread can release the picture and reuse it unsafely
  1452. out->owner2 = s;
  1453. for (i = out_idx; h->delayed_pic[i]; i++)
  1454. h->delayed_pic[i] = h->delayed_pic[i + 1];
  1455. }
  1456. if (!out_of_order && pics > s->avctx->has_b_frames) {
  1457. h->next_output_pic = out;
  1458. if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset)) {
  1459. h->next_outputed_poc = INT_MIN;
  1460. } else
  1461. h->next_outputed_poc = out->poc;
  1462. } else {
  1463. av_log(s->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
  1464. }
  1465. if (h->next_output_pic && h->next_output_pic->sync) {
  1466. h->sync |= 2;
  1467. }
  1468. if (setup_finished)
  1469. ff_thread_finish_setup(s->avctx);
  1470. }
  1471. static av_always_inline void backup_mb_border(H264Context *h, uint8_t *src_y,
  1472. uint8_t *src_cb, uint8_t *src_cr,
  1473. int linesize, int uvlinesize,
  1474. int simple)
  1475. {
  1476. MpegEncContext *const s = &h->s;
  1477. uint8_t *top_border;
  1478. int top_idx = 1;
  1479. const int pixel_shift = h->pixel_shift;
  1480. int chroma444 = CHROMA444;
  1481. int chroma422 = CHROMA422;
  1482. src_y -= linesize;
  1483. src_cb -= uvlinesize;
  1484. src_cr -= uvlinesize;
  1485. if (!simple && FRAME_MBAFF) {
  1486. if (s->mb_y & 1) {
  1487. if (!MB_MBAFF) {
  1488. top_border = h->top_borders[0][s->mb_x];
  1489. AV_COPY128(top_border, src_y + 15 * linesize);
  1490. if (pixel_shift)
  1491. AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
  1492. if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1493. if (chroma444) {
  1494. if (pixel_shift) {
  1495. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  1496. AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
  1497. AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
  1498. AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
  1499. } else {
  1500. AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
  1501. AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
  1502. }
  1503. } else if (chroma422) {
  1504. if (pixel_shift) {
  1505. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  1506. AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
  1507. } else {
  1508. AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
  1509. AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
  1510. }
  1511. } else {
  1512. if (pixel_shift) {
  1513. AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
  1514. AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
  1515. } else {
  1516. AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
  1517. AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
  1518. }
  1519. }
  1520. }
  1521. }
  1522. } else if (MB_MBAFF) {
  1523. top_idx = 0;
  1524. } else
  1525. return;
  1526. }
  1527. top_border = h->top_borders[top_idx][s->mb_x];
  1528. /* There are two lines saved, the line above the the top macroblock
  1529. * of a pair, and the line above the bottom macroblock. */
  1530. AV_COPY128(top_border, src_y + 16 * linesize);
  1531. if (pixel_shift)
  1532. AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
  1533. if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1534. if (chroma444) {
  1535. if (pixel_shift) {
  1536. AV_COPY128(top_border + 32, src_cb + 16 * linesize);
  1537. AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
  1538. AV_COPY128(top_border + 64, src_cr + 16 * linesize);
  1539. AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
  1540. } else {
  1541. AV_COPY128(top_border + 16, src_cb + 16 * linesize);
  1542. AV_COPY128(top_border + 32, src_cr + 16 * linesize);
  1543. }
  1544. } else if (chroma422) {
  1545. if (pixel_shift) {
  1546. AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
  1547. AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
  1548. } else {
  1549. AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
  1550. AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
  1551. }
  1552. } else {
  1553. if (pixel_shift) {
  1554. AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
  1555. AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
  1556. } else {
  1557. AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
  1558. AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
  1559. }
  1560. }
  1561. }
  1562. }
  1563. static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
  1564. uint8_t *src_cb, uint8_t *src_cr,
  1565. int linesize, int uvlinesize,
  1566. int xchg, int chroma444,
  1567. int simple, int pixel_shift)
  1568. {
  1569. MpegEncContext *const s = &h->s;
  1570. int deblock_topleft;
  1571. int deblock_top;
  1572. int top_idx = 1;
  1573. uint8_t *top_border_m1;
  1574. uint8_t *top_border;
  1575. if (!simple && FRAME_MBAFF) {
  1576. if (s->mb_y & 1) {
  1577. if (!MB_MBAFF)
  1578. return;
  1579. } else {
  1580. top_idx = MB_MBAFF ? 0 : 1;
  1581. }
  1582. }
  1583. if (h->deblocking_filter == 2) {
  1584. deblock_topleft = h->slice_table[h->mb_xy - 1 - s->mb_stride] == h->slice_num;
  1585. deblock_top = h->top_type;
  1586. } else {
  1587. deblock_topleft = (s->mb_x > 0);
  1588. deblock_top = (s->mb_y > !!MB_FIELD);
  1589. }
  1590. src_y -= linesize + 1 + pixel_shift;
  1591. src_cb -= uvlinesize + 1 + pixel_shift;
  1592. src_cr -= uvlinesize + 1 + pixel_shift;
  1593. top_border_m1 = h->top_borders[top_idx][s->mb_x - 1];
  1594. top_border = h->top_borders[top_idx][s->mb_x];
  1595. #define XCHG(a, b, xchg) \
  1596. if (pixel_shift) { \
  1597. if (xchg) { \
  1598. AV_SWAP64(b + 0, a + 0); \
  1599. AV_SWAP64(b + 8, a + 8); \
  1600. } else { \
  1601. AV_COPY128(b, a); \
  1602. } \
  1603. } else if (xchg) \
  1604. AV_SWAP64(b, a); \
  1605. else \
  1606. AV_COPY64(b, a);
  1607. if (deblock_top) {
  1608. if (deblock_topleft) {
  1609. XCHG(top_border_m1 + (8 << pixel_shift),
  1610. src_y - (7 << pixel_shift), 1);
  1611. }
  1612. XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
  1613. XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
  1614. if (s->mb_x + 1 < s->mb_width) {
  1615. XCHG(h->top_borders[top_idx][s->mb_x + 1],
  1616. src_y + (17 << pixel_shift), 1);
  1617. }
  1618. }
  1619. if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1620. if (chroma444) {
  1621. if (deblock_topleft) {
  1622. XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  1623. XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  1624. }
  1625. XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
  1626. XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
  1627. XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
  1628. XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
  1629. if (s->mb_x + 1 < s->mb_width) {
  1630. XCHG(h->top_borders[top_idx][s->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
  1631. XCHG(h->top_borders[top_idx][s->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
  1632. }
  1633. } else {
  1634. if (deblock_top) {
  1635. if (deblock_topleft) {
  1636. XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  1637. XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  1638. }
  1639. XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
  1640. XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
  1641. }
  1642. }
  1643. }
  1644. }
  1645. static av_always_inline int dctcoef_get(DCTELEM *mb, int high_bit_depth,
  1646. int index)
  1647. {
  1648. if (high_bit_depth) {
  1649. return AV_RN32A(((int32_t *)mb) + index);
  1650. } else
  1651. return AV_RN16A(mb + index);
  1652. }
  1653. static av_always_inline void dctcoef_set(DCTELEM *mb, int high_bit_depth,
  1654. int index, int value)
  1655. {
  1656. if (high_bit_depth) {
  1657. AV_WN32A(((int32_t *)mb) + index, value);
  1658. } else
  1659. AV_WN16A(mb + index, value);
  1660. }
  1661. static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,
  1662. int mb_type, int is_h264,
  1663. int simple,
  1664. int transform_bypass,
  1665. int pixel_shift,
  1666. int *block_offset,
  1667. int linesize,
  1668. uint8_t *dest_y, int p)
  1669. {
  1670. MpegEncContext *const s = &h->s;
  1671. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1672. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  1673. int i;
  1674. int qscale = p == 0 ? s->qscale : h->chroma_qp[p - 1];
  1675. block_offset += 16 * p;
  1676. if (IS_INTRA4x4(mb_type)) {
  1677. if (simple || !s->encoding) {
  1678. if (IS_8x8DCT(mb_type)) {
  1679. if (transform_bypass) {
  1680. idct_dc_add =
  1681. idct_add = s->dsp.add_pixels8;
  1682. } else {
  1683. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  1684. idct_add = h->h264dsp.h264_idct8_add;
  1685. }
  1686. for (i = 0; i < 16; i += 4) {
  1687. uint8_t *const ptr = dest_y + block_offset[i];
  1688. const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
  1689. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  1690. h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1691. } else {
  1692. const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
  1693. h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
  1694. (h->topright_samples_available << i) & 0x4000, linesize);
  1695. if (nnz) {
  1696. if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  1697. idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1698. else
  1699. idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1700. }
  1701. }
  1702. }
  1703. } else {
  1704. if (transform_bypass) {
  1705. idct_dc_add =
  1706. idct_add = s->dsp.add_pixels4;
  1707. } else {
  1708. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  1709. idct_add = h->h264dsp.h264_idct_add;
  1710. }
  1711. for (i = 0; i < 16; i++) {
  1712. uint8_t *const ptr = dest_y + block_offset[i];
  1713. const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
  1714. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  1715. h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1716. } else {
  1717. uint8_t *topright;
  1718. int nnz, tr;
  1719. uint64_t tr_high;
  1720. if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
  1721. const int topright_avail = (h->topright_samples_available << i) & 0x8000;
  1722. assert(s->mb_y || linesize <= block_offset[i]);
  1723. if (!topright_avail) {
  1724. if (pixel_shift) {
  1725. tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
  1726. topright = (uint8_t *)&tr_high;
  1727. } else {
  1728. tr = ptr[3 - linesize] * 0x01010101u;
  1729. topright = (uint8_t *)&tr;
  1730. }
  1731. } else
  1732. topright = ptr + (4 << pixel_shift) - linesize;
  1733. } else
  1734. topright = NULL;
  1735. h->hpc.pred4x4[dir](ptr, topright, linesize);
  1736. nnz = h->non_zero_count_cache[scan8[i + p * 16]];
  1737. if (nnz) {
  1738. if (is_h264) {
  1739. if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  1740. idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1741. else
  1742. idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1743. } else if (CONFIG_SVQ3_DECODER)
  1744. ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
  1745. }
  1746. }
  1747. }
  1748. }
  1749. }
  1750. } else {
  1751. h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
  1752. if (is_h264) {
  1753. if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
  1754. if (!transform_bypass)
  1755. h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
  1756. h->mb_luma_dc[p],
  1757. h->dequant4_coeff[p][qscale][0]);
  1758. else {
  1759. static const uint8_t dc_mapping[16] = {
  1760. 0 * 16, 1 * 16, 4 * 16, 5 * 16,
  1761. 2 * 16, 3 * 16, 6 * 16, 7 * 16,
  1762. 8 * 16, 9 * 16, 12 * 16, 13 * 16,
  1763. 10 * 16, 11 * 16, 14 * 16, 15 * 16 };
  1764. for (i = 0; i < 16; i++)
  1765. dctcoef_set(h->mb + (p * 256 << pixel_shift),
  1766. pixel_shift, dc_mapping[i],
  1767. dctcoef_get(h->mb_luma_dc[p],
  1768. pixel_shift, i));
  1769. }
  1770. }
  1771. } else if (CONFIG_SVQ3_DECODER)
  1772. ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
  1773. h->mb_luma_dc[p], qscale);
  1774. }
  1775. }
  1776. static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type,
  1777. int is_h264, int simple,
  1778. int transform_bypass,
  1779. int pixel_shift,
  1780. int *block_offset,
  1781. int linesize,
  1782. uint8_t *dest_y, int p)
  1783. {
  1784. MpegEncContext *const s = &h->s;
  1785. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1786. int i;
  1787. block_offset += 16 * p;
  1788. if (!IS_INTRA4x4(mb_type)) {
  1789. if (is_h264) {
  1790. if (IS_INTRA16x16(mb_type)) {
  1791. if (transform_bypass) {
  1792. if (h->sps.profile_idc == 244 &&
  1793. (h->intra16x16_pred_mode == VERT_PRED8x8 ||
  1794. h->intra16x16_pred_mode == HOR_PRED8x8)) {
  1795. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
  1796. h->mb + (p * 256 << pixel_shift),
  1797. linesize);
  1798. } else {
  1799. for (i = 0; i < 16; i++)
  1800. if (h->non_zero_count_cache[scan8[i + p * 16]] ||
  1801. dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  1802. s->dsp.add_pixels4(dest_y + block_offset[i],
  1803. h->mb + (i * 16 + p * 256 << pixel_shift),
  1804. linesize);
  1805. }
  1806. } else {
  1807. h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
  1808. h->mb + (p * 256 << pixel_shift),
  1809. linesize,
  1810. h->non_zero_count_cache + p * 5 * 8);
  1811. }
  1812. } else if (h->cbp & 15) {
  1813. if (transform_bypass) {
  1814. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  1815. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8
  1816. : s->dsp.add_pixels4;
  1817. for (i = 0; i < 16; i += di)
  1818. if (h->non_zero_count_cache[scan8[i + p * 16]])
  1819. idct_add(dest_y + block_offset[i],
  1820. h->mb + (i * 16 + p * 256 << pixel_shift),
  1821. linesize);
  1822. } else {
  1823. if (IS_8x8DCT(mb_type))
  1824. h->h264dsp.h264_idct8_add4(dest_y, block_offset,
  1825. h->mb + (p * 256 << pixel_shift),
  1826. linesize,
  1827. h->non_zero_count_cache + p * 5 * 8);
  1828. else
  1829. h->h264dsp.h264_idct_add16(dest_y, block_offset,
  1830. h->mb + (p * 256 << pixel_shift),
  1831. linesize,
  1832. h->non_zero_count_cache + p * 5 * 8);
  1833. }
  1834. }
  1835. } else if (CONFIG_SVQ3_DECODER) {
  1836. for (i = 0; i < 16; i++)
  1837. if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
  1838. // FIXME benchmark weird rule, & below
  1839. uint8_t *const ptr = dest_y + block_offset[i];
  1840. ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
  1841. s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1842. }
  1843. }
  1844. }
  1845. }
  1846. static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple,
  1847. int pixel_shift)
  1848. {
  1849. MpegEncContext *const s = &h->s;
  1850. const int mb_x = s->mb_x;
  1851. const int mb_y = s->mb_y;
  1852. const int mb_xy = h->mb_xy;
  1853. const int mb_type = s->current_picture.f.mb_type[mb_xy];
  1854. uint8_t *dest_y, *dest_cb, *dest_cr;
  1855. int linesize, uvlinesize /*dct_offset*/;
  1856. int i, j;
  1857. int *block_offset = &h->block_offset[0];
  1858. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  1859. /* is_h264 should always be true if SVQ3 is disabled. */
  1860. const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
  1861. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1862. const int block_h = 16 >> s->chroma_y_shift;
  1863. const int chroma422 = CHROMA422;
  1864. dest_y = s->current_picture.f.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
  1865. dest_cb = s->current_picture.f.data[1] + (mb_x << pixel_shift) * 8 + mb_y * s->uvlinesize * block_h;
  1866. dest_cr = s->current_picture.f.data[2] + (mb_x << pixel_shift) * 8 + mb_y * s->uvlinesize * block_h;
  1867. s->dsp.prefetch(dest_y + (s->mb_x & 3) * 4 * s->linesize + (64 << pixel_shift), s->linesize, 4);
  1868. s->dsp.prefetch(dest_cb + (s->mb_x & 7) * s->uvlinesize + (64 << pixel_shift), dest_cr - dest_cb, 2);
  1869. h->list_counts[mb_xy] = h->list_count;
  1870. if (!simple && MB_FIELD) {
  1871. linesize = h->mb_linesize = s->linesize * 2;
  1872. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  1873. block_offset = &h->block_offset[48];
  1874. if (mb_y & 1) { // FIXME move out of this function?
  1875. dest_y -= s->linesize * 15;
  1876. dest_cb -= s->uvlinesize * (block_h - 1);
  1877. dest_cr -= s->uvlinesize * (block_h - 1);
  1878. }
  1879. if (FRAME_MBAFF) {
  1880. int list;
  1881. for (list = 0; list < h->list_count; list++) {
  1882. if (!USES_LIST(mb_type, list))
  1883. continue;
  1884. if (IS_16X16(mb_type)) {
  1885. int8_t *ref = &h->ref_cache[list][scan8[0]];
  1886. fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (s->mb_y & 1), 1);
  1887. } else {
  1888. for (i = 0; i < 16; i += 4) {
  1889. int ref = h->ref_cache[list][scan8[i]];
  1890. if (ref >= 0)
  1891. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
  1892. 8, (16 + ref) ^ (s->mb_y & 1), 1);
  1893. }
  1894. }
  1895. }
  1896. }
  1897. } else {
  1898. linesize = h->mb_linesize = s->linesize;
  1899. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  1900. // dct_offset = s->linesize * 16;
  1901. }
  1902. if (!simple && IS_INTRA_PCM(mb_type)) {
  1903. const int bit_depth = h->sps.bit_depth_luma;
  1904. if (pixel_shift) {
  1905. int j;
  1906. GetBitContext gb;
  1907. init_get_bits(&gb, (uint8_t *)h->mb, 384 * bit_depth);
  1908. for (i = 0; i < 16; i++) {
  1909. uint16_t *tmp_y = (uint16_t *)(dest_y + i * linesize);
  1910. for (j = 0; j < 16; j++)
  1911. tmp_y[j] = get_bits(&gb, bit_depth);
  1912. }
  1913. if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1914. if (!h->sps.chroma_format_idc) {
  1915. for (i = 0; i < block_h; i++) {
  1916. uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
  1917. uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
  1918. for (j = 0; j < 8; j++) {
  1919. tmp_cb[j] = tmp_cr[j] = 1 << (bit_depth - 1);
  1920. }
  1921. }
  1922. } else {
  1923. for (i = 0; i < block_h; i++) {
  1924. uint16_t *tmp_cb = (uint16_t *)(dest_cb + i * uvlinesize);
  1925. for (j = 0; j < 8; j++)
  1926. tmp_cb[j] = get_bits(&gb, bit_depth);
  1927. }
  1928. for (i = 0; i < block_h; i++) {
  1929. uint16_t *tmp_cr = (uint16_t *)(dest_cr + i * uvlinesize);
  1930. for (j = 0; j < 8; j++)
  1931. tmp_cr[j] = get_bits(&gb, bit_depth);
  1932. }
  1933. }
  1934. }
  1935. } else {
  1936. for (i = 0; i < 16; i++)
  1937. memcpy(dest_y + i * linesize, h->mb + i * 8, 16);
  1938. if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1939. if (!h->sps.chroma_format_idc) {
  1940. for (i = 0; i < 8; i++) {
  1941. memset(dest_cb + i*uvlinesize, 1 << (bit_depth - 1), 8);
  1942. memset(dest_cr + i*uvlinesize, 1 << (bit_depth - 1), 8);
  1943. }
  1944. } else {
  1945. for (i = 0; i < block_h; i++) {
  1946. memcpy(dest_cb + i * uvlinesize, h->mb + 128 + i * 4, 8);
  1947. memcpy(dest_cr + i * uvlinesize, h->mb + 160 + i * 4, 8);
  1948. }
  1949. }
  1950. }
  1951. }
  1952. } else {
  1953. if (IS_INTRA(mb_type)) {
  1954. if (h->deblocking_filter)
  1955. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
  1956. uvlinesize, 1, 0, simple, pixel_shift);
  1957. if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1958. if (CHROMA) {
  1959. h->hpc.pred8x8[h->chroma_pred_mode](dest_cb, uvlinesize);
  1960. h->hpc.pred8x8[h->chroma_pred_mode](dest_cr, uvlinesize);
  1961. }
  1962. }
  1963. hl_decode_mb_predict_luma(h, mb_type, is_h264, simple,
  1964. transform_bypass, pixel_shift,
  1965. block_offset, linesize, dest_y, 0);
  1966. if (h->deblocking_filter)
  1967. xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
  1968. uvlinesize, 0, 0, simple, pixel_shift);
  1969. } else if (is_h264) {
  1970. if (chroma422) {
  1971. hl_motion_422(h, dest_y, dest_cb, dest_cr,
  1972. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1973. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1974. h->h264dsp.weight_h264_pixels_tab,
  1975. h->h264dsp.biweight_h264_pixels_tab,
  1976. pixel_shift);
  1977. } else {
  1978. hl_motion_420(h, dest_y, dest_cb, dest_cr,
  1979. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  1980. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  1981. h->h264dsp.weight_h264_pixels_tab,
  1982. h->h264dsp.biweight_h264_pixels_tab,
  1983. pixel_shift);
  1984. }
  1985. }
  1986. hl_decode_mb_idct_luma(h, mb_type, is_h264, simple, transform_bypass,
  1987. pixel_shift, block_offset, linesize, dest_y, 0);
  1988. if ((simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) &&
  1989. (h->cbp & 0x30)) {
  1990. uint8_t *dest[2] = { dest_cb, dest_cr };
  1991. if (transform_bypass) {
  1992. if (IS_INTRA(mb_type) && h->sps.profile_idc == 244 &&
  1993. (h->chroma_pred_mode == VERT_PRED8x8 ||
  1994. h->chroma_pred_mode == HOR_PRED8x8)) {
  1995. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[0],
  1996. block_offset + 16,
  1997. h->mb + (16 * 16 * 1 << pixel_shift),
  1998. uvlinesize);
  1999. h->hpc.pred8x8_add[h->chroma_pred_mode](dest[1],
  2000. block_offset + 32,
  2001. h->mb + (16 * 16 * 2 << pixel_shift),
  2002. uvlinesize);
  2003. } else {
  2004. idct_add = s->dsp.add_pixels4;
  2005. for (j = 1; j < 3; j++) {
  2006. for (i = j * 16; i < j * 16 + 4; i++)
  2007. if (h->non_zero_count_cache[scan8[i]] ||
  2008. dctcoef_get(h->mb, pixel_shift, i * 16))
  2009. idct_add(dest[j - 1] + block_offset[i],
  2010. h->mb + (i * 16 << pixel_shift),
  2011. uvlinesize);
  2012. if (chroma422) {
  2013. for (i = j * 16 + 4; i < j * 16 + 8; i++)
  2014. if (h->non_zero_count_cache[scan8[i + 4]] ||
  2015. dctcoef_get(h->mb, pixel_shift, i * 16))
  2016. idct_add(dest[j - 1] + block_offset[i + 4],
  2017. h->mb + (i * 16 << pixel_shift),
  2018. uvlinesize);
  2019. }
  2020. }
  2021. }
  2022. } else {
  2023. if (is_h264) {
  2024. int qp[2];
  2025. if (chroma422) {
  2026. qp[0] = h->chroma_qp[0] + 3;
  2027. qp[1] = h->chroma_qp[1] + 3;
  2028. } else {
  2029. qp[0] = h->chroma_qp[0];
  2030. qp[1] = h->chroma_qp[1];
  2031. }
  2032. if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 0]])
  2033. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 1 << pixel_shift),
  2034. h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][qp[0]][0]);
  2035. if (h->non_zero_count_cache[scan8[CHROMA_DC_BLOCK_INDEX + 1]])
  2036. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16 * 16 * 2 << pixel_shift),
  2037. h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][qp[1]][0]);
  2038. h->h264dsp.h264_idct_add8(dest, block_offset,
  2039. h->mb, uvlinesize,
  2040. h->non_zero_count_cache);
  2041. } else if (CONFIG_SVQ3_DECODER) {
  2042. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 1,
  2043. h->dequant4_coeff[IS_INTRA(mb_type) ? 1 : 4][h->chroma_qp[0]][0]);
  2044. h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16 * 16 * 2,
  2045. h->dequant4_coeff[IS_INTRA(mb_type) ? 2 : 5][h->chroma_qp[1]][0]);
  2046. for (j = 1; j < 3; j++) {
  2047. for (i = j * 16; i < j * 16 + 4; i++)
  2048. if (h->non_zero_count_cache[scan8[i]] || h->mb[i * 16]) {
  2049. uint8_t *const ptr = dest[j - 1] + block_offset[i];
  2050. ff_svq3_add_idct_c(ptr, h->mb + i * 16,
  2051. uvlinesize,
  2052. ff_h264_chroma_qp[0][s->qscale + 12] - 12, 2);
  2053. }
  2054. }
  2055. }
  2056. }
  2057. }
  2058. }
  2059. if (h->cbp || IS_INTRA(mb_type)) {
  2060. s->dsp.clear_blocks(h->mb);
  2061. s->dsp.clear_blocks(h->mb + (24 * 16 << pixel_shift));
  2062. }
  2063. }
  2064. static av_always_inline void hl_decode_mb_444_internal(H264Context *h,
  2065. int simple,
  2066. int pixel_shift)
  2067. {
  2068. MpegEncContext *const s = &h->s;
  2069. const int mb_x = s->mb_x;
  2070. const int mb_y = s->mb_y;
  2071. const int mb_xy = h->mb_xy;
  2072. const int mb_type = s->current_picture.f.mb_type[mb_xy];
  2073. uint8_t *dest[3];
  2074. int linesize;
  2075. int i, j, p;
  2076. int *block_offset = &h->block_offset[0];
  2077. const int transform_bypass = !simple && (s->qscale == 0 && h->sps.transform_bypass);
  2078. const int plane_count = (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) ? 3 : 1;
  2079. for (p = 0; p < plane_count; p++) {
  2080. dest[p] = s->current_picture.f.data[p] +
  2081. ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
  2082. s->dsp.prefetch(dest[p] + (s->mb_x & 3) * 4 * s->linesize + (64 << pixel_shift),
  2083. s->linesize, 4);
  2084. }
  2085. h->list_counts[mb_xy] = h->list_count;
  2086. if (!simple && MB_FIELD) {
  2087. linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize * 2;
  2088. block_offset = &h->block_offset[48];
  2089. if (mb_y & 1) // FIXME move out of this function?
  2090. for (p = 0; p < 3; p++)
  2091. dest[p] -= s->linesize * 15;
  2092. if (FRAME_MBAFF) {
  2093. int list;
  2094. for (list = 0; list < h->list_count; list++) {
  2095. if (!USES_LIST(mb_type, list))
  2096. continue;
  2097. if (IS_16X16(mb_type)) {
  2098. int8_t *ref = &h->ref_cache[list][scan8[0]];
  2099. fill_rectangle(ref, 4, 4, 8, (16 + *ref) ^ (s->mb_y & 1), 1);
  2100. } else {
  2101. for (i = 0; i < 16; i += 4) {
  2102. int ref = h->ref_cache[list][scan8[i]];
  2103. if (ref >= 0)
  2104. fill_rectangle(&h->ref_cache[list][scan8[i]], 2, 2,
  2105. 8, (16 + ref) ^ (s->mb_y & 1), 1);
  2106. }
  2107. }
  2108. }
  2109. }
  2110. } else {
  2111. linesize = h->mb_linesize = h->mb_uvlinesize = s->linesize;
  2112. }
  2113. if (!simple && IS_INTRA_PCM(mb_type)) {
  2114. if (pixel_shift) {
  2115. const int bit_depth = h->sps.bit_depth_luma;
  2116. GetBitContext gb;
  2117. init_get_bits(&gb, (uint8_t *)h->mb, 768 * bit_depth);
  2118. for (p = 0; p < plane_count; p++)
  2119. for (i = 0; i < 16; i++) {
  2120. uint16_t *tmp = (uint16_t *)(dest[p] + i * linesize);
  2121. for (j = 0; j < 16; j++)
  2122. tmp[j] = get_bits(&gb, bit_depth);
  2123. }
  2124. } else {
  2125. for (p = 0; p < plane_count; p++)
  2126. for (i = 0; i < 16; i++)
  2127. memcpy(dest[p] + i * linesize, h->mb + p * 128 + i * 8, 16);
  2128. }
  2129. } else {
  2130. if (IS_INTRA(mb_type)) {
  2131. if (h->deblocking_filter)
  2132. xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
  2133. linesize, 1, 1, simple, pixel_shift);
  2134. for (p = 0; p < plane_count; p++)
  2135. hl_decode_mb_predict_luma(h, mb_type, 1, simple,
  2136. transform_bypass, pixel_shift,
  2137. block_offset, linesize, dest[p], p);
  2138. if (h->deblocking_filter)
  2139. xchg_mb_border(h, dest[0], dest[1], dest[2], linesize,
  2140. linesize, 0, 1, simple, pixel_shift);
  2141. } else {
  2142. hl_motion(h, dest[0], dest[1], dest[2],
  2143. s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
  2144. s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
  2145. h->h264dsp.weight_h264_pixels_tab,
  2146. h->h264dsp.biweight_h264_pixels_tab, pixel_shift, 3);
  2147. }
  2148. for (p = 0; p < plane_count; p++)
  2149. hl_decode_mb_idct_luma(h, mb_type, 1, simple, transform_bypass,
  2150. pixel_shift, block_offset, linesize,
  2151. dest[p], p);
  2152. }
  2153. if (h->cbp || IS_INTRA(mb_type)) {
  2154. s->dsp.clear_blocks(h->mb);
  2155. s->dsp.clear_blocks(h->mb + (24 * 16 << pixel_shift));
  2156. }
  2157. }
  2158. /**
  2159. * Process a macroblock; this case avoids checks for expensive uncommon cases.
  2160. */
  2161. #define hl_decode_mb_simple(sh, bits) \
  2162. static void hl_decode_mb_simple_ ## bits(H264Context *h) \
  2163. { \
  2164. hl_decode_mb_internal(h, 1, sh); \
  2165. }
  2166. hl_decode_mb_simple(0, 8)
  2167. hl_decode_mb_simple(1, 16)
  2168. /**
  2169. * Process a macroblock; this handles edge cases, such as interlacing.
  2170. */
  2171. static void av_noinline hl_decode_mb_complex(H264Context *h)
  2172. {
  2173. hl_decode_mb_internal(h, 0, h->pixel_shift);
  2174. }
  2175. static void av_noinline hl_decode_mb_444_complex(H264Context *h)
  2176. {
  2177. hl_decode_mb_444_internal(h, 0, h->pixel_shift);
  2178. }
  2179. static void av_noinline hl_decode_mb_444_simple(H264Context *h)
  2180. {
  2181. hl_decode_mb_444_internal(h, 1, 0);
  2182. }
  2183. void ff_h264_hl_decode_mb(H264Context *h)
  2184. {
  2185. MpegEncContext *const s = &h->s;
  2186. const int mb_xy = h->mb_xy;
  2187. const int mb_type = s->current_picture.f.mb_type[mb_xy];
  2188. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  2189. if (CHROMA444) {
  2190. if (is_complex || h->pixel_shift)
  2191. hl_decode_mb_444_complex(h);
  2192. else
  2193. hl_decode_mb_444_simple(h);
  2194. } else if (is_complex) {
  2195. hl_decode_mb_complex(h);
  2196. } else if (h->pixel_shift) {
  2197. hl_decode_mb_simple_16(h);
  2198. } else
  2199. hl_decode_mb_simple_8(h);
  2200. }
  2201. static int pred_weight_table(H264Context *h)
  2202. {
  2203. MpegEncContext *const s = &h->s;
  2204. int list, i;
  2205. int luma_def, chroma_def;
  2206. h->use_weight = 0;
  2207. h->use_weight_chroma = 0;
  2208. h->luma_log2_weight_denom = get_ue_golomb(&s->gb);
  2209. if (h->sps.chroma_format_idc)
  2210. h->chroma_log2_weight_denom = get_ue_golomb(&s->gb);
  2211. luma_def = 1 << h->luma_log2_weight_denom;
  2212. chroma_def = 1 << h->chroma_log2_weight_denom;
  2213. for (list = 0; list < 2; list++) {
  2214. h->luma_weight_flag[list] = 0;
  2215. h->chroma_weight_flag[list] = 0;
  2216. for (i = 0; i < h->ref_count[list]; i++) {
  2217. int luma_weight_flag, chroma_weight_flag;
  2218. luma_weight_flag = get_bits1(&s->gb);
  2219. if (luma_weight_flag) {
  2220. h->luma_weight[i][list][0] = get_se_golomb(&s->gb);
  2221. h->luma_weight[i][list][1] = get_se_golomb(&s->gb);
  2222. if (h->luma_weight[i][list][0] != luma_def ||
  2223. h->luma_weight[i][list][1] != 0) {
  2224. h->use_weight = 1;
  2225. h->luma_weight_flag[list] = 1;
  2226. }
  2227. } else {
  2228. h->luma_weight[i][list][0] = luma_def;
  2229. h->luma_weight[i][list][1] = 0;
  2230. }
  2231. if (h->sps.chroma_format_idc) {
  2232. chroma_weight_flag = get_bits1(&s->gb);
  2233. if (chroma_weight_flag) {
  2234. int j;
  2235. for (j = 0; j < 2; j++) {
  2236. h->chroma_weight[i][list][j][0] = get_se_golomb(&s->gb);
  2237. h->chroma_weight[i][list][j][1] = get_se_golomb(&s->gb);
  2238. if (h->chroma_weight[i][list][j][0] != chroma_def ||
  2239. h->chroma_weight[i][list][j][1] != 0) {
  2240. h->use_weight_chroma = 1;
  2241. h->chroma_weight_flag[list] = 1;
  2242. }
  2243. }
  2244. } else {
  2245. int j;
  2246. for (j = 0; j < 2; j++) {
  2247. h->chroma_weight[i][list][j][0] = chroma_def;
  2248. h->chroma_weight[i][list][j][1] = 0;
  2249. }
  2250. }
  2251. }
  2252. }
  2253. if (h->slice_type_nos != AV_PICTURE_TYPE_B)
  2254. break;
  2255. }
  2256. h->use_weight = h->use_weight || h->use_weight_chroma;
  2257. return 0;
  2258. }
  2259. /**
  2260. * Initialize implicit_weight table.
  2261. * @param field 0/1 initialize the weight for interlaced MBAFF
  2262. * -1 initializes the rest
  2263. */
  2264. static void implicit_weight_table(H264Context *h, int field)
  2265. {
  2266. MpegEncContext *const s = &h->s;
  2267. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  2268. for (i = 0; i < 2; i++) {
  2269. h->luma_weight_flag[i] = 0;
  2270. h->chroma_weight_flag[i] = 0;
  2271. }
  2272. if (field < 0) {
  2273. if (s->picture_structure == PICT_FRAME) {
  2274. cur_poc = s->current_picture_ptr->poc;
  2275. } else {
  2276. cur_poc = s->current_picture_ptr->field_poc[s->picture_structure - 1];
  2277. }
  2278. if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF &&
  2279. h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
  2280. h->use_weight = 0;
  2281. h->use_weight_chroma = 0;
  2282. return;
  2283. }
  2284. ref_start = 0;
  2285. ref_count0 = h->ref_count[0];
  2286. ref_count1 = h->ref_count[1];
  2287. } else {
  2288. cur_poc = s->current_picture_ptr->field_poc[field];
  2289. ref_start = 16;
  2290. ref_count0 = 16 + 2 * h->ref_count[0];
  2291. ref_count1 = 16 + 2 * h->ref_count[1];
  2292. }
  2293. h->use_weight = 2;
  2294. h->use_weight_chroma = 2;
  2295. h->luma_log2_weight_denom = 5;
  2296. h->chroma_log2_weight_denom = 5;
  2297. for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
  2298. int poc0 = h->ref_list[0][ref0].poc;
  2299. for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
  2300. int w = 32;
  2301. if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
  2302. int poc1 = h->ref_list[1][ref1].poc;
  2303. int td = av_clip(poc1 - poc0, -128, 127);
  2304. if (td) {
  2305. int tb = av_clip(cur_poc - poc0, -128, 127);
  2306. int tx = (16384 + (FFABS(td) >> 1)) / td;
  2307. int dist_scale_factor = (tb * tx + 32) >> 8;
  2308. if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
  2309. w = 64 - dist_scale_factor;
  2310. }
  2311. }
  2312. if (field < 0) {
  2313. h->implicit_weight[ref0][ref1][0] =
  2314. h->implicit_weight[ref0][ref1][1] = w;
  2315. } else {
  2316. h->implicit_weight[ref0][ref1][field] = w;
  2317. }
  2318. }
  2319. }
  2320. }
  2321. /**
  2322. * instantaneous decoder refresh.
  2323. */
  2324. static void idr(H264Context *h)
  2325. {
  2326. int i;
  2327. ff_h264_remove_all_refs(h);
  2328. h->prev_frame_num = 0;
  2329. h->prev_frame_num_offset = 0;
  2330. h->prev_poc_msb = 1<<16;
  2331. h->prev_poc_lsb = 0;
  2332. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  2333. h->last_pocs[i] = INT_MIN;
  2334. }
  2335. /* forget old pics after a seek */
  2336. static void flush_dpb(AVCodecContext *avctx)
  2337. {
  2338. H264Context *h = avctx->priv_data;
  2339. int i;
  2340. for (i=0; i<=MAX_DELAYED_PIC_COUNT; i++) {
  2341. if (h->delayed_pic[i])
  2342. h->delayed_pic[i]->f.reference = 0;
  2343. h->delayed_pic[i] = NULL;
  2344. }
  2345. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  2346. h->prev_interlaced_frame = 1;
  2347. idr(h);
  2348. h->prev_frame_num = -1;
  2349. if (h->s.current_picture_ptr)
  2350. h->s.current_picture_ptr->f.reference = 0;
  2351. h->s.first_field = 0;
  2352. ff_h264_reset_sei(h);
  2353. ff_mpeg_flush(avctx);
  2354. h->recovery_frame= -1;
  2355. h->sync= 0;
  2356. }
  2357. static int init_poc(H264Context *h)
  2358. {
  2359. MpegEncContext *const s = &h->s;
  2360. const int max_frame_num = 1 << h->sps.log2_max_frame_num;
  2361. int field_poc[2];
  2362. Picture *cur = s->current_picture_ptr;
  2363. h->frame_num_offset = h->prev_frame_num_offset;
  2364. if (h->frame_num < h->prev_frame_num)
  2365. h->frame_num_offset += max_frame_num;
  2366. if (h->sps.poc_type == 0) {
  2367. const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
  2368. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
  2369. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  2370. else if (h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
  2371. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  2372. else
  2373. h->poc_msb = h->prev_poc_msb;
  2374. // printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
  2375. field_poc[0] =
  2376. field_poc[1] = h->poc_msb + h->poc_lsb;
  2377. if (s->picture_structure == PICT_FRAME)
  2378. field_poc[1] += h->delta_poc_bottom;
  2379. } else if (h->sps.poc_type == 1) {
  2380. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  2381. int i;
  2382. if (h->sps.poc_cycle_length != 0)
  2383. abs_frame_num = h->frame_num_offset + h->frame_num;
  2384. else
  2385. abs_frame_num = 0;
  2386. if (h->nal_ref_idc == 0 && abs_frame_num > 0)
  2387. abs_frame_num--;
  2388. expected_delta_per_poc_cycle = 0;
  2389. for (i = 0; i < h->sps.poc_cycle_length; i++)
  2390. // FIXME integrate during sps parse
  2391. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
  2392. if (abs_frame_num > 0) {
  2393. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  2394. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  2395. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  2396. for (i = 0; i <= frame_num_in_poc_cycle; i++)
  2397. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
  2398. } else
  2399. expectedpoc = 0;
  2400. if (h->nal_ref_idc == 0)
  2401. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  2402. field_poc[0] = expectedpoc + h->delta_poc[0];
  2403. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  2404. if (s->picture_structure == PICT_FRAME)
  2405. field_poc[1] += h->delta_poc[1];
  2406. } else {
  2407. int poc = 2 * (h->frame_num_offset + h->frame_num);
  2408. if (!h->nal_ref_idc)
  2409. poc--;
  2410. field_poc[0] = poc;
  2411. field_poc[1] = poc;
  2412. }
  2413. if (s->picture_structure != PICT_BOTTOM_FIELD)
  2414. s->current_picture_ptr->field_poc[0] = field_poc[0];
  2415. if (s->picture_structure != PICT_TOP_FIELD)
  2416. s->current_picture_ptr->field_poc[1] = field_poc[1];
  2417. cur->poc = FFMIN(cur->field_poc[0], cur->field_poc[1]);
  2418. return 0;
  2419. }
  2420. /**
  2421. * initialize scan tables
  2422. */
  2423. static void init_scan_tables(H264Context *h)
  2424. {
  2425. int i;
  2426. for (i = 0; i < 16; i++) {
  2427. #define T(x) (x >> 2) | ((x << 2) & 0xF)
  2428. h->zigzag_scan[i] = T(zigzag_scan[i]);
  2429. h->field_scan[i] = T(field_scan[i]);
  2430. #undef T
  2431. }
  2432. for (i = 0; i < 64; i++) {
  2433. #define T(x) (x >> 3) | ((x & 7) << 3)
  2434. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  2435. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  2436. h->field_scan8x8[i] = T(field_scan8x8[i]);
  2437. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  2438. #undef T
  2439. }
  2440. if (h->sps.transform_bypass) { // FIXME same ugly
  2441. h->zigzag_scan_q0 = zigzag_scan;
  2442. h->zigzag_scan8x8_q0 = ff_zigzag_direct;
  2443. h->zigzag_scan8x8_cavlc_q0 = zigzag_scan8x8_cavlc;
  2444. h->field_scan_q0 = field_scan;
  2445. h->field_scan8x8_q0 = field_scan8x8;
  2446. h->field_scan8x8_cavlc_q0 = field_scan8x8_cavlc;
  2447. } else {
  2448. h->zigzag_scan_q0 = h->zigzag_scan;
  2449. h->zigzag_scan8x8_q0 = h->zigzag_scan8x8;
  2450. h->zigzag_scan8x8_cavlc_q0 = h->zigzag_scan8x8_cavlc;
  2451. h->field_scan_q0 = h->field_scan;
  2452. h->field_scan8x8_q0 = h->field_scan8x8;
  2453. h->field_scan8x8_cavlc_q0 = h->field_scan8x8_cavlc;
  2454. }
  2455. }
  2456. static int field_end(H264Context *h, int in_setup)
  2457. {
  2458. MpegEncContext *const s = &h->s;
  2459. AVCodecContext *const avctx = s->avctx;
  2460. int err = 0;
  2461. s->mb_y = 0;
  2462. if (!in_setup && !s->dropable)
  2463. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX,
  2464. s->picture_structure == PICT_BOTTOM_FIELD);
  2465. if (CONFIG_H264_VDPAU_DECODER &&
  2466. s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  2467. ff_vdpau_h264_set_reference_frames(s);
  2468. if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  2469. if (!s->dropable) {
  2470. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  2471. h->prev_poc_msb = h->poc_msb;
  2472. h->prev_poc_lsb = h->poc_lsb;
  2473. }
  2474. h->prev_frame_num_offset = h->frame_num_offset;
  2475. h->prev_frame_num = h->frame_num;
  2476. h->outputed_poc = h->next_outputed_poc;
  2477. }
  2478. if (avctx->hwaccel) {
  2479. if (avctx->hwaccel->end_frame(avctx) < 0)
  2480. av_log(avctx, AV_LOG_ERROR,
  2481. "hardware accelerator failed to decode picture\n");
  2482. }
  2483. if (CONFIG_H264_VDPAU_DECODER &&
  2484. s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  2485. ff_vdpau_h264_picture_complete(s);
  2486. /*
  2487. * FIXME: Error handling code does not seem to support interlaced
  2488. * when slices span multiple rows
  2489. * The ff_er_add_slice calls don't work right for bottom
  2490. * fields; they cause massive erroneous error concealing
  2491. * Error marking covers both fields (top and bottom).
  2492. * This causes a mismatched s->error_count
  2493. * and a bad error table. Further, the error count goes to
  2494. * INT_MAX when called for bottom field, because mb_y is
  2495. * past end by one (callers fault) and resync_mb_y != 0
  2496. * causes problems for the first MB line, too.
  2497. */
  2498. if (!FIELD_PICTURE)
  2499. ff_er_frame_end(s);
  2500. ff_MPV_frame_end(s);
  2501. h->current_slice = 0;
  2502. return err;
  2503. }
  2504. /**
  2505. * Replicate H264 "master" context to thread contexts.
  2506. */
  2507. static void clone_slice(H264Context *dst, H264Context *src)
  2508. {
  2509. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  2510. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  2511. dst->s.current_picture = src->s.current_picture;
  2512. dst->s.linesize = src->s.linesize;
  2513. dst->s.uvlinesize = src->s.uvlinesize;
  2514. dst->s.first_field = src->s.first_field;
  2515. dst->prev_poc_msb = src->prev_poc_msb;
  2516. dst->prev_poc_lsb = src->prev_poc_lsb;
  2517. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  2518. dst->prev_frame_num = src->prev_frame_num;
  2519. dst->short_ref_count = src->short_ref_count;
  2520. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  2521. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  2522. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  2523. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  2524. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  2525. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  2526. }
  2527. /**
  2528. * Compute profile from profile_idc and constraint_set?_flags.
  2529. *
  2530. * @param sps SPS
  2531. *
  2532. * @return profile as defined by FF_PROFILE_H264_*
  2533. */
  2534. int ff_h264_get_profile(SPS *sps)
  2535. {
  2536. int profile = sps->profile_idc;
  2537. switch (sps->profile_idc) {
  2538. case FF_PROFILE_H264_BASELINE:
  2539. // constraint_set1_flag set to 1
  2540. profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  2541. break;
  2542. case FF_PROFILE_H264_HIGH_10:
  2543. case FF_PROFILE_H264_HIGH_422:
  2544. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  2545. // constraint_set3_flag set to 1
  2546. profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
  2547. break;
  2548. }
  2549. return profile;
  2550. }
  2551. /**
  2552. * Decode a slice header.
  2553. * This will also call ff_MPV_common_init() and frame_start() as needed.
  2554. *
  2555. * @param h h264context
  2556. * @param h0 h264 master context (differs from 'h' when doing sliced based
  2557. * parallel decoding)
  2558. *
  2559. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  2560. */
  2561. static int decode_slice_header(H264Context *h, H264Context *h0)
  2562. {
  2563. MpegEncContext *const s = &h->s;
  2564. MpegEncContext *const s0 = &h0->s;
  2565. unsigned int first_mb_in_slice;
  2566. unsigned int pps_id;
  2567. int num_ref_idx_active_override_flag;
  2568. unsigned int slice_type, tmp, i, j;
  2569. int default_ref_list_done = 0;
  2570. int last_pic_structure, last_pic_dropable;
  2571. int must_reinit;
  2572. /* FIXME: 2tap qpel isn't implemented for high bit depth. */
  2573. if ((s->avctx->flags2 & CODEC_FLAG2_FAST) &&
  2574. !h->nal_ref_idc && !h->pixel_shift) {
  2575. s->me.qpel_put = s->dsp.put_2tap_qpel_pixels_tab;
  2576. s->me.qpel_avg = s->dsp.avg_2tap_qpel_pixels_tab;
  2577. } else {
  2578. s->me.qpel_put = s->dsp.put_h264_qpel_pixels_tab;
  2579. s->me.qpel_avg = s->dsp.avg_h264_qpel_pixels_tab;
  2580. }
  2581. first_mb_in_slice = get_ue_golomb_long(&s->gb);
  2582. if (first_mb_in_slice == 0) { // FIXME better field boundary detection
  2583. if (h0->current_slice && FIELD_PICTURE) {
  2584. field_end(h, 1);
  2585. }
  2586. h0->current_slice = 0;
  2587. if (!s0->first_field) {
  2588. if (s->current_picture_ptr && !s->dropable &&
  2589. s->current_picture_ptr->owner2 == s) {
  2590. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX,
  2591. s->picture_structure == PICT_BOTTOM_FIELD);
  2592. }
  2593. s->current_picture_ptr = NULL;
  2594. }
  2595. }
  2596. slice_type = get_ue_golomb_31(&s->gb);
  2597. if (slice_type > 9) {
  2598. av_log(h->s.avctx, AV_LOG_ERROR,
  2599. "slice type too large (%d) at %d %d\n",
  2600. h->slice_type, s->mb_x, s->mb_y);
  2601. return -1;
  2602. }
  2603. if (slice_type > 4) {
  2604. slice_type -= 5;
  2605. h->slice_type_fixed = 1;
  2606. } else
  2607. h->slice_type_fixed = 0;
  2608. slice_type = golomb_to_pict_type[slice_type];
  2609. if (slice_type == AV_PICTURE_TYPE_I ||
  2610. (h0->current_slice != 0 && slice_type == h0->last_slice_type)) {
  2611. default_ref_list_done = 1;
  2612. }
  2613. h->slice_type = slice_type;
  2614. h->slice_type_nos = slice_type & 3;
  2615. // to make a few old functions happy, it's wrong though
  2616. s->pict_type = h->slice_type;
  2617. pps_id = get_ue_golomb(&s->gb);
  2618. if (pps_id >= MAX_PPS_COUNT) {
  2619. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id %d out of range\n", pps_id);
  2620. return -1;
  2621. }
  2622. if (!h0->pps_buffers[pps_id]) {
  2623. av_log(h->s.avctx, AV_LOG_ERROR,
  2624. "non-existing PPS %u referenced\n",
  2625. pps_id);
  2626. return -1;
  2627. }
  2628. h->pps = *h0->pps_buffers[pps_id];
  2629. if (!h0->sps_buffers[h->pps.sps_id]) {
  2630. av_log(h->s.avctx, AV_LOG_ERROR,
  2631. "non-existing SPS %u referenced\n",
  2632. h->pps.sps_id);
  2633. return -1;
  2634. }
  2635. h->sps = *h0->sps_buffers[h->pps.sps_id];
  2636. s->avctx->profile = ff_h264_get_profile(&h->sps);
  2637. s->avctx->level = h->sps.level_idc;
  2638. s->avctx->refs = h->sps.ref_frame_count;
  2639. must_reinit = (s->context_initialized &&
  2640. ( 16*h->sps.mb_width != s->avctx->coded_width
  2641. || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != s->avctx->coded_height
  2642. || s->avctx->bits_per_raw_sample != h->sps.bit_depth_luma
  2643. || h->cur_chroma_format_idc != h->sps.chroma_format_idc
  2644. || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio)));
  2645. if(must_reinit && (h != h0 || (s->avctx->active_thread_type & FF_THREAD_FRAME))) {
  2646. av_log_missing_feature(s->avctx,
  2647. "Width/height/bit depth/chroma idc changing with threads is", 0);
  2648. return -1; // width / height changed during parallelized decoding
  2649. }
  2650. s->mb_width = h->sps.mb_width;
  2651. s->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  2652. h->b_stride = s->mb_width * 4;
  2653. s->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
  2654. s->width = 16 * s->mb_width;
  2655. s->height = 16 * s->mb_height;
  2656. if(must_reinit) {
  2657. free_tables(h, 0);
  2658. flush_dpb(s->avctx);
  2659. ff_MPV_common_end(s);
  2660. h->list_count = 0;
  2661. }
  2662. if (!s->context_initialized) {
  2663. if (h != h0) {
  2664. av_log(h->s.avctx, AV_LOG_ERROR,
  2665. "Cannot (re-)initialize context during parallel decoding.\n");
  2666. return -1;
  2667. }
  2668. avcodec_set_dimensions(s->avctx, s->width, s->height);
  2669. s->avctx->width -= (2>>CHROMA444)*FFMIN(h->sps.crop_right, (8<<CHROMA444)-1);
  2670. s->avctx->height -= (1<<s->chroma_y_shift)*FFMIN(h->sps.crop_bottom, (16>>s->chroma_y_shift)-1) * (2 - h->sps.frame_mbs_only_flag);
  2671. s->avctx->sample_aspect_ratio = h->sps.sar;
  2672. av_assert0(s->avctx->sample_aspect_ratio.den);
  2673. if (s->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
  2674. h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
  2675. if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10 &&
  2676. (h->sps.bit_depth_luma != 9 || !CHROMA422)) {
  2677. s->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  2678. h->cur_chroma_format_idc = h->sps.chroma_format_idc;
  2679. h->pixel_shift = h->sps.bit_depth_luma > 8;
  2680. ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
  2681. ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
  2682. s->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;
  2683. ff_dsputil_init(&s->dsp, s->avctx);
  2684. } else {
  2685. av_log(s->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d chroma_idc: %d\n",
  2686. h->sps.bit_depth_luma, h->sps.chroma_format_idc);
  2687. return -1;
  2688. }
  2689. }
  2690. if (h->sps.video_signal_type_present_flag) {
  2691. s->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG
  2692. : AVCOL_RANGE_MPEG;
  2693. if (h->sps.colour_description_present_flag) {
  2694. s->avctx->color_primaries = h->sps.color_primaries;
  2695. s->avctx->color_trc = h->sps.color_trc;
  2696. s->avctx->colorspace = h->sps.colorspace;
  2697. }
  2698. }
  2699. if (h->sps.timing_info_present_flag) {
  2700. int64_t den = h->sps.time_scale;
  2701. if (h->x264_build < 44U)
  2702. den *= 2;
  2703. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  2704. h->sps.num_units_in_tick, den, 1 << 30);
  2705. }
  2706. switch (h->sps.bit_depth_luma) {
  2707. case 9:
  2708. if (CHROMA444) {
  2709. if (s->avctx->colorspace == AVCOL_SPC_RGB) {
  2710. s->avctx->pix_fmt = PIX_FMT_GBRP9;
  2711. } else
  2712. s->avctx->pix_fmt = PIX_FMT_YUV444P9;
  2713. } else if (CHROMA422)
  2714. s->avctx->pix_fmt = PIX_FMT_YUV422P9;
  2715. else
  2716. s->avctx->pix_fmt = PIX_FMT_YUV420P9;
  2717. break;
  2718. case 10:
  2719. if (CHROMA444) {
  2720. if (s->avctx->colorspace == AVCOL_SPC_RGB) {
  2721. s->avctx->pix_fmt = PIX_FMT_GBRP10;
  2722. } else
  2723. s->avctx->pix_fmt = PIX_FMT_YUV444P10;
  2724. } else if (CHROMA422)
  2725. s->avctx->pix_fmt = PIX_FMT_YUV422P10;
  2726. else
  2727. s->avctx->pix_fmt = PIX_FMT_YUV420P10;
  2728. break;
  2729. case 8:
  2730. if (CHROMA444) {
  2731. s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ444P
  2732. : PIX_FMT_YUV444P;
  2733. if (s->avctx->colorspace == AVCOL_SPC_RGB) {
  2734. s->avctx->pix_fmt = PIX_FMT_GBR24P;
  2735. av_log(h->s.avctx, AV_LOG_DEBUG, "Detected GBR colorspace.\n");
  2736. } else if (s->avctx->colorspace == AVCOL_SPC_YCGCO) {
  2737. av_log(h->s.avctx, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n");
  2738. }
  2739. } else if (CHROMA422) {
  2740. s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ422P
  2741. : PIX_FMT_YUV422P;
  2742. } else {
  2743. s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
  2744. s->avctx->codec->pix_fmts ?
  2745. s->avctx->codec->pix_fmts :
  2746. s->avctx->color_range == AVCOL_RANGE_JPEG ?
  2747. hwaccel_pixfmt_list_h264_jpeg_420 :
  2748. ff_hwaccel_pixfmt_list_420);
  2749. }
  2750. break;
  2751. default:
  2752. av_log(s->avctx, AV_LOG_ERROR,
  2753. "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
  2754. return AVERROR_INVALIDDATA;
  2755. }
  2756. s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id,
  2757. s->avctx->pix_fmt);
  2758. if (ff_MPV_common_init(s) < 0) {
  2759. av_log(h->s.avctx, AV_LOG_ERROR, "ff_MPV_common_init() failed.\n");
  2760. return -1;
  2761. }
  2762. s->first_field = 0;
  2763. h->prev_interlaced_frame = 1;
  2764. init_scan_tables(h);
  2765. if (ff_h264_alloc_tables(h) < 0) {
  2766. av_log(h->s.avctx, AV_LOG_ERROR,
  2767. "Could not allocate memory for h264\n");
  2768. return AVERROR(ENOMEM);
  2769. }
  2770. if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_SLICE)) {
  2771. if (context_init(h) < 0) {
  2772. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
  2773. return -1;
  2774. }
  2775. } else {
  2776. for (i = 1; i < s->slice_context_count; i++) {
  2777. H264Context *c;
  2778. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  2779. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  2780. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  2781. c->h264dsp = h->h264dsp;
  2782. c->sps = h->sps;
  2783. c->pps = h->pps;
  2784. c->pixel_shift = h->pixel_shift;
  2785. c->cur_chroma_format_idc = h->cur_chroma_format_idc;
  2786. init_scan_tables(c);
  2787. clone_tables(c, h, i);
  2788. }
  2789. for (i = 0; i < s->slice_context_count; i++)
  2790. if (context_init(h->thread_context[i]) < 0) {
  2791. av_log(h->s.avctx, AV_LOG_ERROR,
  2792. "context_init() failed.\n");
  2793. return -1;
  2794. }
  2795. }
  2796. }
  2797. if (h == h0 && h->dequant_coeff_pps != pps_id) {
  2798. h->dequant_coeff_pps = pps_id;
  2799. init_dequant_tables(h);
  2800. }
  2801. h->frame_num = get_bits(&s->gb, h->sps.log2_max_frame_num);
  2802. h->mb_mbaff = 0;
  2803. h->mb_aff_frame = 0;
  2804. last_pic_structure = s0->picture_structure;
  2805. last_pic_dropable = s->dropable;
  2806. s->dropable = h->nal_ref_idc == 0;
  2807. if (h->sps.frame_mbs_only_flag) {
  2808. s->picture_structure = PICT_FRAME;
  2809. } else {
  2810. if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
  2811. av_log(h->s.avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
  2812. return -1;
  2813. }
  2814. if (get_bits1(&s->gb)) { // field_pic_flag
  2815. s->picture_structure = PICT_TOP_FIELD + get_bits1(&s->gb); // bottom_field_flag
  2816. } else {
  2817. s->picture_structure = PICT_FRAME;
  2818. h->mb_aff_frame = h->sps.mb_aff;
  2819. }
  2820. }
  2821. h->mb_field_decoding_flag = s->picture_structure != PICT_FRAME;
  2822. if (h0->current_slice != 0) {
  2823. if (last_pic_structure != s->picture_structure ||
  2824. last_pic_dropable != s->dropable) {
  2825. av_log(h->s.avctx, AV_LOG_ERROR,
  2826. "Changing field mode (%d -> %d) between slices is not allowed\n",
  2827. last_pic_structure, s->picture_structure);
  2828. s->picture_structure = last_pic_structure;
  2829. s->dropable = last_pic_dropable;
  2830. return AVERROR_INVALIDDATA;
  2831. }
  2832. } else {
  2833. /* Shorten frame num gaps so we don't have to allocate reference
  2834. * frames just to throw them away */
  2835. if (h->frame_num != h->prev_frame_num && h->prev_frame_num >= 0) {
  2836. int unwrap_prev_frame_num = h->prev_frame_num;
  2837. int max_frame_num = 1 << h->sps.log2_max_frame_num;
  2838. if (unwrap_prev_frame_num > h->frame_num)
  2839. unwrap_prev_frame_num -= max_frame_num;
  2840. if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
  2841. unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
  2842. if (unwrap_prev_frame_num < 0)
  2843. unwrap_prev_frame_num += max_frame_num;
  2844. h->prev_frame_num = unwrap_prev_frame_num;
  2845. }
  2846. }
  2847. /* See if we have a decoded first field looking for a pair...
  2848. * Here, we're using that to see if we should mark previously
  2849. * decode frames as "finished".
  2850. * We have to do that before the "dummy" in-between frame allocation,
  2851. * since that can modify s->current_picture_ptr. */
  2852. if (s0->first_field) {
  2853. assert(s0->current_picture_ptr);
  2854. assert(s0->current_picture_ptr->f.data[0]);
  2855. assert(s0->current_picture_ptr->f.reference != DELAYED_PIC_REF);
  2856. /* Mark old field/frame as completed */
  2857. if (!last_pic_dropable && s0->current_picture_ptr->owner2 == s0) {
  2858. ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
  2859. last_pic_structure == PICT_BOTTOM_FIELD);
  2860. }
  2861. /* figure out if we have a complementary field pair */
  2862. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  2863. /* Previous field is unmatched. Don't display it, but let it
  2864. * remain for reference if marked as such. */
  2865. if (!last_pic_dropable && last_pic_structure != PICT_FRAME) {
  2866. ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
  2867. last_pic_structure == PICT_TOP_FIELD);
  2868. }
  2869. } else {
  2870. if (s0->current_picture_ptr->frame_num != h->frame_num) {
  2871. /* This and previous field were reference, but had
  2872. * different frame_nums. Consider this field first in
  2873. * pair. Throw away previous field except for reference
  2874. * purposes. */
  2875. if (!last_pic_dropable && last_pic_structure != PICT_FRAME) {
  2876. ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
  2877. last_pic_structure == PICT_TOP_FIELD);
  2878. }
  2879. } else {
  2880. /* Second field in complementary pair */
  2881. if (!((last_pic_structure == PICT_TOP_FIELD &&
  2882. s->picture_structure == PICT_BOTTOM_FIELD) ||
  2883. (last_pic_structure == PICT_BOTTOM_FIELD &&
  2884. s->picture_structure == PICT_TOP_FIELD))) {
  2885. av_log(s->avctx, AV_LOG_ERROR,
  2886. "Invalid field mode combination %d/%d\n",
  2887. last_pic_structure, s->picture_structure);
  2888. s->picture_structure = last_pic_structure;
  2889. s->dropable = last_pic_dropable;
  2890. return AVERROR_INVALIDDATA;
  2891. } else if (last_pic_dropable != s->dropable) {
  2892. av_log(s->avctx, AV_LOG_ERROR,
  2893. "Cannot combine reference and non-reference fields in the same frame\n");
  2894. av_log_ask_for_sample(s->avctx, NULL);
  2895. s->picture_structure = last_pic_structure;
  2896. s->dropable = last_pic_dropable;
  2897. return AVERROR_INVALIDDATA;
  2898. }
  2899. /* Take ownership of this buffer. Note that if another thread owned
  2900. * the first field of this buffer, we're not operating on that pointer,
  2901. * so the original thread is still responsible for reporting progress
  2902. * on that first field (or if that was us, we just did that above).
  2903. * By taking ownership, we assign responsibility to ourselves to
  2904. * report progress on the second field. */
  2905. s0->current_picture_ptr->owner2 = s0;
  2906. }
  2907. }
  2908. }
  2909. while (h->frame_num != h->prev_frame_num && h->prev_frame_num >= 0 &&
  2910. h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
  2911. Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  2912. av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
  2913. h->frame_num, h->prev_frame_num);
  2914. if (ff_h264_frame_start(h) < 0)
  2915. return -1;
  2916. h->prev_frame_num++;
  2917. h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
  2918. s->current_picture_ptr->frame_num = h->prev_frame_num;
  2919. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
  2920. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 1);
  2921. ff_generate_sliding_window_mmcos(h);
  2922. if (ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index) < 0 &&
  2923. (s->avctx->err_recognition & AV_EF_EXPLODE))
  2924. return AVERROR_INVALIDDATA;
  2925. /* Error concealment: if a ref is missing, copy the previous ref in its place.
  2926. * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
  2927. * about there being no actual duplicates.
  2928. * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
  2929. * concealing a lost frame, this probably isn't noticeable by comparison, but it should
  2930. * be fixed. */
  2931. if (h->short_ref_count) {
  2932. if (prev) {
  2933. av_image_copy(h->short_ref[0]->f.data, h->short_ref[0]->f.linesize,
  2934. (const uint8_t **)prev->f.data, prev->f.linesize,
  2935. s->avctx->pix_fmt, s->mb_width * 16, s->mb_height * 16);
  2936. h->short_ref[0]->poc = prev->poc + 2;
  2937. }
  2938. h->short_ref[0]->frame_num = h->prev_frame_num;
  2939. }
  2940. }
  2941. /* See if we have a decoded first field looking for a pair...
  2942. * We're using that to see whether to continue decoding in that
  2943. * frame, or to allocate a new one. */
  2944. if (s0->first_field) {
  2945. assert(s0->current_picture_ptr);
  2946. assert(s0->current_picture_ptr->f.data[0]);
  2947. assert(s0->current_picture_ptr->f.reference != DELAYED_PIC_REF);
  2948. /* figure out if we have a complementary field pair */
  2949. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  2950. /* Previous field is unmatched. Don't display it, but let it
  2951. * remain for reference if marked as such. */
  2952. s0->current_picture_ptr = NULL;
  2953. s0->first_field = FIELD_PICTURE;
  2954. } else {
  2955. if (s0->current_picture_ptr->frame_num != h->frame_num) {
  2956. ff_thread_report_progress((AVFrame*)s0->current_picture_ptr, INT_MAX,
  2957. s0->picture_structure==PICT_BOTTOM_FIELD);
  2958. /* This and the previous field had different frame_nums.
  2959. * Consider this field first in pair. Throw away previous
  2960. * one except for reference purposes. */
  2961. s0->first_field = 1;
  2962. s0->current_picture_ptr = NULL;
  2963. } else {
  2964. /* Second field in complementary pair */
  2965. s0->first_field = 0;
  2966. }
  2967. }
  2968. } else {
  2969. /* Frame or first field in a potentially complementary pair */
  2970. assert(!s0->current_picture_ptr);
  2971. s0->first_field = FIELD_PICTURE;
  2972. }
  2973. if (!FIELD_PICTURE || s0->first_field) {
  2974. if (ff_h264_frame_start(h) < 0) {
  2975. s0->first_field = 0;
  2976. return -1;
  2977. }
  2978. } else {
  2979. ff_release_unused_pictures(s, 0);
  2980. }
  2981. }
  2982. if (h != h0)
  2983. clone_slice(h, h0);
  2984. s->current_picture_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
  2985. assert(s->mb_num == s->mb_width * s->mb_height);
  2986. if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  2987. first_mb_in_slice >= s->mb_num) {
  2988. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  2989. return -1;
  2990. }
  2991. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  2992. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  2993. if (s->picture_structure == PICT_BOTTOM_FIELD)
  2994. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  2995. assert(s->mb_y < s->mb_height);
  2996. if (s->picture_structure == PICT_FRAME) {
  2997. h->curr_pic_num = h->frame_num;
  2998. h->max_pic_num = 1 << h->sps.log2_max_frame_num;
  2999. } else {
  3000. h->curr_pic_num = 2 * h->frame_num + 1;
  3001. h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
  3002. }
  3003. if (h->nal_unit_type == NAL_IDR_SLICE)
  3004. get_ue_golomb(&s->gb); /* idr_pic_id */
  3005. if (h->sps.poc_type == 0) {
  3006. h->poc_lsb = get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  3007. if (h->pps.pic_order_present == 1 && s->picture_structure == PICT_FRAME)
  3008. h->delta_poc_bottom = get_se_golomb(&s->gb);
  3009. }
  3010. if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
  3011. h->delta_poc[0] = get_se_golomb(&s->gb);
  3012. if (h->pps.pic_order_present == 1 && s->picture_structure == PICT_FRAME)
  3013. h->delta_poc[1] = get_se_golomb(&s->gb);
  3014. }
  3015. init_poc(h);
  3016. if (h->pps.redundant_pic_cnt_present)
  3017. h->redundant_pic_count = get_ue_golomb(&s->gb);
  3018. // set defaults, might be overridden a few lines later
  3019. h->ref_count[0] = h->pps.ref_count[0];
  3020. h->ref_count[1] = h->pps.ref_count[1];
  3021. if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
  3022. unsigned max = s->picture_structure == PICT_FRAME ? 15 : 31;
  3023. if (h->slice_type_nos == AV_PICTURE_TYPE_B)
  3024. h->direct_spatial_mv_pred = get_bits1(&s->gb);
  3025. num_ref_idx_active_override_flag = get_bits1(&s->gb);
  3026. if (num_ref_idx_active_override_flag) {
  3027. h->ref_count[0] = get_ue_golomb(&s->gb) + 1;
  3028. if (h->slice_type_nos == AV_PICTURE_TYPE_B)
  3029. h->ref_count[1] = get_ue_golomb(&s->gb) + 1;
  3030. }
  3031. if (h->ref_count[0]-1 > max || h->ref_count[1]-1 > max){
  3032. av_log(h->s.avctx, AV_LOG_ERROR, "reference overflow\n");
  3033. h->ref_count[0] = h->ref_count[1] = 1;
  3034. return AVERROR_INVALIDDATA;
  3035. }
  3036. if (h->slice_type_nos == AV_PICTURE_TYPE_B)
  3037. h->list_count = 2;
  3038. else
  3039. h->list_count = 1;
  3040. } else
  3041. h->ref_count[1]= h->ref_count[0]= h->list_count= 0;
  3042. if (!default_ref_list_done)
  3043. ff_h264_fill_default_ref_list(h);
  3044. if (h->slice_type_nos != AV_PICTURE_TYPE_I &&
  3045. ff_h264_decode_ref_pic_list_reordering(h) < 0) {
  3046. h->ref_count[1] = h->ref_count[0] = 0;
  3047. return -1;
  3048. }
  3049. if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
  3050. s->last_picture_ptr = &h->ref_list[0][0];
  3051. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  3052. }
  3053. if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
  3054. s->next_picture_ptr = &h->ref_list[1][0];
  3055. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  3056. }
  3057. if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
  3058. (h->pps.weighted_bipred_idc == 1 &&
  3059. h->slice_type_nos == AV_PICTURE_TYPE_B))
  3060. pred_weight_table(h);
  3061. else if (h->pps.weighted_bipred_idc == 2 &&
  3062. h->slice_type_nos == AV_PICTURE_TYPE_B) {
  3063. implicit_weight_table(h, -1);
  3064. } else {
  3065. h->use_weight = 0;
  3066. for (i = 0; i < 2; i++) {
  3067. h->luma_weight_flag[i] = 0;
  3068. h->chroma_weight_flag[i] = 0;
  3069. }
  3070. }
  3071. if (h->nal_ref_idc && ff_h264_decode_ref_pic_marking(h0, &s->gb) < 0 &&
  3072. (s->avctx->err_recognition & AV_EF_EXPLODE))
  3073. return AVERROR_INVALIDDATA;
  3074. if (FRAME_MBAFF) {
  3075. ff_h264_fill_mbaff_ref_list(h);
  3076. if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {
  3077. implicit_weight_table(h, 0);
  3078. implicit_weight_table(h, 1);
  3079. }
  3080. }
  3081. if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
  3082. ff_h264_direct_dist_scale_factor(h);
  3083. ff_h264_direct_ref_list_init(h);
  3084. if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
  3085. tmp = get_ue_golomb_31(&s->gb);
  3086. if (tmp > 2) {
  3087. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  3088. return -1;
  3089. }
  3090. h->cabac_init_idc = tmp;
  3091. }
  3092. h->last_qscale_diff = 0;
  3093. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  3094. if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
  3095. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  3096. return -1;
  3097. }
  3098. s->qscale = tmp;
  3099. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  3100. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  3101. // FIXME qscale / qp ... stuff
  3102. if (h->slice_type == AV_PICTURE_TYPE_SP)
  3103. get_bits1(&s->gb); /* sp_for_switch_flag */
  3104. if (h->slice_type == AV_PICTURE_TYPE_SP ||
  3105. h->slice_type == AV_PICTURE_TYPE_SI)
  3106. get_se_golomb(&s->gb); /* slice_qs_delta */
  3107. h->deblocking_filter = 1;
  3108. h->slice_alpha_c0_offset = 52;
  3109. h->slice_beta_offset = 52;
  3110. if (h->pps.deblocking_filter_parameters_present) {
  3111. tmp = get_ue_golomb_31(&s->gb);
  3112. if (tmp > 2) {
  3113. av_log(s->avctx, AV_LOG_ERROR,
  3114. "deblocking_filter_idc %u out of range\n", tmp);
  3115. return -1;
  3116. }
  3117. h->deblocking_filter = tmp;
  3118. if (h->deblocking_filter < 2)
  3119. h->deblocking_filter ^= 1; // 1<->0
  3120. if (h->deblocking_filter) {
  3121. h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
  3122. h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
  3123. if (h->slice_alpha_c0_offset > 104U ||
  3124. h->slice_beta_offset > 104U) {
  3125. av_log(s->avctx, AV_LOG_ERROR,
  3126. "deblocking filter parameters %d %d out of range\n",
  3127. h->slice_alpha_c0_offset, h->slice_beta_offset);
  3128. return -1;
  3129. }
  3130. }
  3131. }
  3132. if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  3133. (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
  3134. h->slice_type_nos != AV_PICTURE_TYPE_I) ||
  3135. (s->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
  3136. h->slice_type_nos == AV_PICTURE_TYPE_B) ||
  3137. (s->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
  3138. h->nal_ref_idc == 0))
  3139. h->deblocking_filter = 0;
  3140. if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
  3141. if (s->avctx->flags2 & CODEC_FLAG2_FAST) {
  3142. /* Cheat slightly for speed:
  3143. * Do not bother to deblock across slices. */
  3144. h->deblocking_filter = 2;
  3145. } else {
  3146. h0->max_contexts = 1;
  3147. if (!h0->single_decode_warning) {
  3148. av_log(s->avctx, AV_LOG_INFO,
  3149. "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  3150. h0->single_decode_warning = 1;
  3151. }
  3152. if (h != h0) {
  3153. av_log(h->s.avctx, AV_LOG_ERROR,
  3154. "Deblocking switched inside frame.\n");
  3155. return 1;
  3156. }
  3157. }
  3158. }
  3159. h->qp_thresh = 15 + 52 -
  3160. FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) -
  3161. FFMAX3(0,
  3162. h->pps.chroma_qp_index_offset[0],
  3163. h->pps.chroma_qp_index_offset[1]) +
  3164. 6 * (h->sps.bit_depth_luma - 8);
  3165. h0->last_slice_type = slice_type;
  3166. h->slice_num = ++h0->current_slice;
  3167. if (h->slice_num)
  3168. h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= s->resync_mb_y;
  3169. if ( h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= s->resync_mb_y
  3170. && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= s->resync_mb_y
  3171. && h->slice_num >= MAX_SLICES) {
  3172. //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
  3173. av_log(s->avctx, AV_LOG_WARNING, "Possibly too many slices (%d >= %d), increase MAX_SLICES and recompile if there are artifacts\n", h->slice_num, MAX_SLICES);
  3174. }
  3175. for (j = 0; j < 2; j++) {
  3176. int id_list[16];
  3177. int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
  3178. for (i = 0; i < 16; i++) {
  3179. id_list[i] = 60;
  3180. if (h->ref_list[j][i].f.data[0]) {
  3181. int k;
  3182. uint8_t *base = h->ref_list[j][i].f.base[0];
  3183. for (k = 0; k < h->short_ref_count; k++)
  3184. if (h->short_ref[k]->f.base[0] == base) {
  3185. id_list[i] = k;
  3186. break;
  3187. }
  3188. for (k = 0; k < h->long_ref_count; k++)
  3189. if (h->long_ref[k] && h->long_ref[k]->f.base[0] == base) {
  3190. id_list[i] = h->short_ref_count + k;
  3191. break;
  3192. }
  3193. }
  3194. }
  3195. ref2frm[0] =
  3196. ref2frm[1] = -1;
  3197. for (i = 0; i < 16; i++)
  3198. ref2frm[i + 2] = 4 * id_list[i] +
  3199. (h->ref_list[j][i].f.reference & 3);
  3200. ref2frm[18 + 0] =
  3201. ref2frm[18 + 1] = -1;
  3202. for (i = 16; i < 48; i++)
  3203. ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
  3204. (h->ref_list[j][i].f.reference & 3);
  3205. }
  3206. // FIXME: fix draw_edges + PAFF + frame threads
  3207. h->emu_edge_width = (s->flags & CODEC_FLAG_EMU_EDGE ||
  3208. (!h->sps.frame_mbs_only_flag &&
  3209. s->avctx->active_thread_type))
  3210. ? 0 : 16;
  3211. h->emu_edge_height = (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  3212. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  3213. av_log(h->s.avctx, AV_LOG_DEBUG,
  3214. "slice:%d %s mb:%d %c%s%s pps:%u frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d:%d:%d weight:%d%s %s\n",
  3215. h->slice_num,
  3216. (s->picture_structure == PICT_FRAME ? "F" : s->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
  3217. first_mb_in_slice,
  3218. av_get_picture_type_char(h->slice_type),
  3219. h->slice_type_fixed ? " fix" : "",
  3220. h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  3221. pps_id, h->frame_num,
  3222. s->current_picture_ptr->field_poc[0],
  3223. s->current_picture_ptr->field_poc[1],
  3224. h->ref_count[0], h->ref_count[1],
  3225. s->qscale,
  3226. h->deblocking_filter,
  3227. h->slice_alpha_c0_offset / 2 - 26, h->slice_beta_offset / 2 - 26,
  3228. h->use_weight,
  3229. h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
  3230. h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
  3231. }
  3232. return 0;
  3233. }
  3234. int ff_h264_get_slice_type(const H264Context *h)
  3235. {
  3236. switch (h->slice_type) {
  3237. case AV_PICTURE_TYPE_P:
  3238. return 0;
  3239. case AV_PICTURE_TYPE_B:
  3240. return 1;
  3241. case AV_PICTURE_TYPE_I:
  3242. return 2;
  3243. case AV_PICTURE_TYPE_SP:
  3244. return 3;
  3245. case AV_PICTURE_TYPE_SI:
  3246. return 4;
  3247. default:
  3248. return -1;
  3249. }
  3250. }
  3251. static av_always_inline void fill_filter_caches_inter(H264Context *h,
  3252. MpegEncContext *const s,
  3253. int mb_type, int top_xy,
  3254. int left_xy[LEFT_MBS],
  3255. int top_type,
  3256. int left_type[LEFT_MBS],
  3257. int mb_xy, int list)
  3258. {
  3259. int b_stride = h->b_stride;
  3260. int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
  3261. int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
  3262. if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
  3263. if (USES_LIST(top_type, list)) {
  3264. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  3265. const int b8_xy = 4 * top_xy + 2;
  3266. int (*ref2frm)[64] = h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2);
  3267. AV_COPY128(mv_dst - 1 * 8, s->current_picture.f.motion_val[list][b_xy + 0]);
  3268. ref_cache[0 - 1 * 8] =
  3269. ref_cache[1 - 1 * 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 0]];
  3270. ref_cache[2 - 1 * 8] =
  3271. ref_cache[3 - 1 * 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 1]];
  3272. } else {
  3273. AV_ZERO128(mv_dst - 1 * 8);
  3274. AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3275. }
  3276. if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
  3277. if (USES_LIST(left_type[LTOP], list)) {
  3278. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  3279. const int b8_xy = 4 * left_xy[LTOP] + 1;
  3280. int (*ref2frm)[64] = h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2);
  3281. AV_COPY32(mv_dst - 1 + 0, s->current_picture.f.motion_val[list][b_xy + b_stride * 0]);
  3282. AV_COPY32(mv_dst - 1 + 8, s->current_picture.f.motion_val[list][b_xy + b_stride * 1]);
  3283. AV_COPY32(mv_dst - 1 + 16, s->current_picture.f.motion_val[list][b_xy + b_stride * 2]);
  3284. AV_COPY32(mv_dst - 1 + 24, s->current_picture.f.motion_val[list][b_xy + b_stride * 3]);
  3285. ref_cache[-1 + 0] =
  3286. ref_cache[-1 + 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2 * 0]];
  3287. ref_cache[-1 + 16] =
  3288. ref_cache[-1 + 24] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2 * 1]];
  3289. } else {
  3290. AV_ZERO32(mv_dst - 1 + 0);
  3291. AV_ZERO32(mv_dst - 1 + 8);
  3292. AV_ZERO32(mv_dst - 1 + 16);
  3293. AV_ZERO32(mv_dst - 1 + 24);
  3294. ref_cache[-1 + 0] =
  3295. ref_cache[-1 + 8] =
  3296. ref_cache[-1 + 16] =
  3297. ref_cache[-1 + 24] = LIST_NOT_USED;
  3298. }
  3299. }
  3300. }
  3301. if (!USES_LIST(mb_type, list)) {
  3302. fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
  3303. AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3304. AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3305. AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3306. AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  3307. return;
  3308. }
  3309. {
  3310. int8_t *ref = &s->current_picture.f.ref_index[list][4 * mb_xy];
  3311. int (*ref2frm)[64] = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2);
  3312. uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
  3313. uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
  3314. AV_WN32A(&ref_cache[0 * 8], ref01);
  3315. AV_WN32A(&ref_cache[1 * 8], ref01);
  3316. AV_WN32A(&ref_cache[2 * 8], ref23);
  3317. AV_WN32A(&ref_cache[3 * 8], ref23);
  3318. }
  3319. {
  3320. int16_t(*mv_src)[2] = &s->current_picture.f.motion_val[list][4 * s->mb_x + 4 * s->mb_y * b_stride];
  3321. AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
  3322. AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
  3323. AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
  3324. AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
  3325. }
  3326. }
  3327. /**
  3328. *
  3329. * @return non zero if the loop filter can be skipped
  3330. */
  3331. static int fill_filter_caches(H264Context *h, int mb_type)
  3332. {
  3333. MpegEncContext *const s = &h->s;
  3334. const int mb_xy = h->mb_xy;
  3335. int top_xy, left_xy[LEFT_MBS];
  3336. int top_type, left_type[LEFT_MBS];
  3337. uint8_t *nnz;
  3338. uint8_t *nnz_cache;
  3339. top_xy = mb_xy - (s->mb_stride << MB_FIELD);
  3340. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  3341. * stuff, I can't imagine that these complex rules are worth it. */
  3342. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  3343. if (FRAME_MBAFF) {
  3344. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.f.mb_type[mb_xy - 1]);
  3345. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  3346. if (s->mb_y & 1) {
  3347. if (left_mb_field_flag != curr_mb_field_flag)
  3348. left_xy[LTOP] -= s->mb_stride;
  3349. } else {
  3350. if (curr_mb_field_flag)
  3351. top_xy += s->mb_stride &
  3352. (((s->current_picture.f.mb_type[top_xy] >> 7) & 1) - 1);
  3353. if (left_mb_field_flag != curr_mb_field_flag)
  3354. left_xy[LBOT] += s->mb_stride;
  3355. }
  3356. }
  3357. h->top_mb_xy = top_xy;
  3358. h->left_mb_xy[LTOP] = left_xy[LTOP];
  3359. h->left_mb_xy[LBOT] = left_xy[LBOT];
  3360. {
  3361. /* For sufficiently low qp, filtering wouldn't do anything.
  3362. * This is a conservative estimate: could also check beta_offset
  3363. * and more accurate chroma_qp. */
  3364. int qp_thresh = h->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
  3365. int qp = s->current_picture.f.qscale_table[mb_xy];
  3366. if (qp <= qp_thresh &&
  3367. (left_xy[LTOP] < 0 ||
  3368. ((qp + s->current_picture.f.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
  3369. (top_xy < 0 ||
  3370. ((qp + s->current_picture.f.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
  3371. if (!FRAME_MBAFF)
  3372. return 1;
  3373. if ((left_xy[LTOP] < 0 ||
  3374. ((qp + s->current_picture.f.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
  3375. (top_xy < s->mb_stride ||
  3376. ((qp + s->current_picture.f.qscale_table[top_xy - s->mb_stride] + 1) >> 1) <= qp_thresh))
  3377. return 1;
  3378. }
  3379. }
  3380. top_type = s->current_picture.f.mb_type[top_xy];
  3381. left_type[LTOP] = s->current_picture.f.mb_type[left_xy[LTOP]];
  3382. left_type[LBOT] = s->current_picture.f.mb_type[left_xy[LBOT]];
  3383. if (h->deblocking_filter == 2) {
  3384. if (h->slice_table[top_xy] != h->slice_num)
  3385. top_type = 0;
  3386. if (h->slice_table[left_xy[LBOT]] != h->slice_num)
  3387. left_type[LTOP] = left_type[LBOT] = 0;
  3388. } else {
  3389. if (h->slice_table[top_xy] == 0xFFFF)
  3390. top_type = 0;
  3391. if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
  3392. left_type[LTOP] = left_type[LBOT] = 0;
  3393. }
  3394. h->top_type = top_type;
  3395. h->left_type[LTOP] = left_type[LTOP];
  3396. h->left_type[LBOT] = left_type[LBOT];
  3397. if (IS_INTRA(mb_type))
  3398. return 0;
  3399. fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy,
  3400. top_type, left_type, mb_xy, 0);
  3401. if (h->list_count == 2)
  3402. fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy,
  3403. top_type, left_type, mb_xy, 1);
  3404. nnz = h->non_zero_count[mb_xy];
  3405. nnz_cache = h->non_zero_count_cache;
  3406. AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
  3407. AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
  3408. AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
  3409. AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
  3410. h->cbp = h->cbp_table[mb_xy];
  3411. if (top_type) {
  3412. nnz = h->non_zero_count[top_xy];
  3413. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
  3414. }
  3415. if (left_type[LTOP]) {
  3416. nnz = h->non_zero_count[left_xy[LTOP]];
  3417. nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
  3418. nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
  3419. nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
  3420. nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
  3421. }
  3422. /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
  3423. * from what the loop filter needs */
  3424. if (!CABAC && h->pps.transform_8x8_mode) {
  3425. if (IS_8x8DCT(top_type)) {
  3426. nnz_cache[4 + 8 * 0] =
  3427. nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
  3428. nnz_cache[6 + 8 * 0] =
  3429. nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
  3430. }
  3431. if (IS_8x8DCT(left_type[LTOP])) {
  3432. nnz_cache[3 + 8 * 1] =
  3433. nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
  3434. }
  3435. if (IS_8x8DCT(left_type[LBOT])) {
  3436. nnz_cache[3 + 8 * 3] =
  3437. nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
  3438. }
  3439. if (IS_8x8DCT(mb_type)) {
  3440. nnz_cache[scan8[0]] =
  3441. nnz_cache[scan8[1]] =
  3442. nnz_cache[scan8[2]] =
  3443. nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;
  3444. nnz_cache[scan8[0 + 4]] =
  3445. nnz_cache[scan8[1 + 4]] =
  3446. nnz_cache[scan8[2 + 4]] =
  3447. nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;
  3448. nnz_cache[scan8[0 + 8]] =
  3449. nnz_cache[scan8[1 + 8]] =
  3450. nnz_cache[scan8[2 + 8]] =
  3451. nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;
  3452. nnz_cache[scan8[0 + 12]] =
  3453. nnz_cache[scan8[1 + 12]] =
  3454. nnz_cache[scan8[2 + 12]] =
  3455. nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;
  3456. }
  3457. }
  3458. return 0;
  3459. }
  3460. static void loop_filter(H264Context *h, int start_x, int end_x)
  3461. {
  3462. MpegEncContext *const s = &h->s;
  3463. uint8_t *dest_y, *dest_cb, *dest_cr;
  3464. int linesize, uvlinesize, mb_x, mb_y;
  3465. const int end_mb_y = s->mb_y + FRAME_MBAFF;
  3466. const int old_slice_type = h->slice_type;
  3467. const int pixel_shift = h->pixel_shift;
  3468. const int block_h = 16 >> s->chroma_y_shift;
  3469. if (h->deblocking_filter) {
  3470. for (mb_x = start_x; mb_x < end_x; mb_x++)
  3471. for (mb_y = end_mb_y - FRAME_MBAFF; mb_y <= end_mb_y; mb_y++) {
  3472. int mb_xy, mb_type;
  3473. mb_xy = h->mb_xy = mb_x + mb_y * s->mb_stride;
  3474. h->slice_num = h->slice_table[mb_xy];
  3475. mb_type = s->current_picture.f.mb_type[mb_xy];
  3476. h->list_count = h->list_counts[mb_xy];
  3477. if (FRAME_MBAFF)
  3478. h->mb_mbaff =
  3479. h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  3480. s->mb_x = mb_x;
  3481. s->mb_y = mb_y;
  3482. dest_y = s->current_picture.f.data[0] +
  3483. ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
  3484. dest_cb = s->current_picture.f.data[1] +
  3485. (mb_x << pixel_shift) * (8 << CHROMA444) +
  3486. mb_y * s->uvlinesize * block_h;
  3487. dest_cr = s->current_picture.f.data[2] +
  3488. (mb_x << pixel_shift) * (8 << CHROMA444) +
  3489. mb_y * s->uvlinesize * block_h;
  3490. // FIXME simplify above
  3491. if (MB_FIELD) {
  3492. linesize = h->mb_linesize = s->linesize * 2;
  3493. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3494. if (mb_y & 1) { // FIXME move out of this function?
  3495. dest_y -= s->linesize * 15;
  3496. dest_cb -= s->uvlinesize * (block_h - 1);
  3497. dest_cr -= s->uvlinesize * (block_h - 1);
  3498. }
  3499. } else {
  3500. linesize = h->mb_linesize = s->linesize;
  3501. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3502. }
  3503. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
  3504. uvlinesize, 0);
  3505. if (fill_filter_caches(h, mb_type))
  3506. continue;
  3507. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.f.qscale_table[mb_xy]);
  3508. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.f.qscale_table[mb_xy]);
  3509. if (FRAME_MBAFF) {
  3510. ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
  3511. linesize, uvlinesize);
  3512. } else {
  3513. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb,
  3514. dest_cr, linesize, uvlinesize);
  3515. }
  3516. }
  3517. }
  3518. h->slice_type = old_slice_type;
  3519. s->mb_x = end_x;
  3520. s->mb_y = end_mb_y - FRAME_MBAFF;
  3521. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  3522. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  3523. }
  3524. static void predict_field_decoding_flag(H264Context *h)
  3525. {
  3526. MpegEncContext *const s = &h->s;
  3527. const int mb_xy = s->mb_x + s->mb_y * s->mb_stride;
  3528. int mb_type = (h->slice_table[mb_xy - 1] == h->slice_num) ?
  3529. s->current_picture.f.mb_type[mb_xy - 1] :
  3530. (h->slice_table[mb_xy - s->mb_stride] == h->slice_num) ?
  3531. s->current_picture.f.mb_type[mb_xy - s->mb_stride] : 0;
  3532. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  3533. }
  3534. /**
  3535. * Draw edges and report progress for the last MB row.
  3536. */
  3537. static void decode_finish_row(H264Context *h)
  3538. {
  3539. MpegEncContext *const s = &h->s;
  3540. int top = 16 * (s->mb_y >> FIELD_PICTURE);
  3541. int pic_height = 16 * s->mb_height >> FIELD_PICTURE;
  3542. int height = 16 << FRAME_MBAFF;
  3543. int deblock_border = (16 + 4) << FRAME_MBAFF;
  3544. if (h->deblocking_filter) {
  3545. if ((top + height) >= pic_height)
  3546. height += deblock_border;
  3547. top -= deblock_border;
  3548. }
  3549. if (top >= pic_height || (top + height) < h->emu_edge_height)
  3550. return;
  3551. height = FFMIN(height, pic_height - top);
  3552. if (top < h->emu_edge_height) {
  3553. height = top + height;
  3554. top = 0;
  3555. }
  3556. ff_draw_horiz_band(s, top, height);
  3557. if (s->dropable)
  3558. return;
  3559. ff_thread_report_progress(&s->current_picture_ptr->f, top + height - 1,
  3560. s->picture_structure == PICT_BOTTOM_FIELD);
  3561. }
  3562. static int decode_slice(struct AVCodecContext *avctx, void *arg)
  3563. {
  3564. H264Context *h = *(void **)arg;
  3565. MpegEncContext *const s = &h->s;
  3566. const int part_mask = s->partitioned_frame ? (ER_AC_END | ER_AC_ERROR)
  3567. : 0x7F;
  3568. int lf_x_start = s->mb_x;
  3569. s->mb_skip_run = -1;
  3570. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME ||
  3571. s->codec_id != CODEC_ID_H264 ||
  3572. (CONFIG_GRAY && (s->flags & CODEC_FLAG_GRAY));
  3573. if (h->pps.cabac) {
  3574. /* realign */
  3575. align_get_bits(&s->gb);
  3576. /* init cabac */
  3577. ff_init_cabac_states(&h->cabac);
  3578. ff_init_cabac_decoder(&h->cabac,
  3579. s->gb.buffer + get_bits_count(&s->gb) / 8,
  3580. (get_bits_left(&s->gb) + 7) / 8);
  3581. ff_h264_init_cabac_states(h);
  3582. for (;;) {
  3583. // START_TIMER
  3584. int ret = ff_h264_decode_mb_cabac(h);
  3585. int eos;
  3586. // STOP_TIMER("decode_mb_cabac")
  3587. if (ret >= 0)
  3588. ff_h264_hl_decode_mb(h);
  3589. // FIXME optimal? or let mb_decode decode 16x32 ?
  3590. if (ret >= 0 && FRAME_MBAFF) {
  3591. s->mb_y++;
  3592. ret = ff_h264_decode_mb_cabac(h);
  3593. if (ret >= 0)
  3594. ff_h264_hl_decode_mb(h);
  3595. s->mb_y--;
  3596. }
  3597. eos = get_cabac_terminate(&h->cabac);
  3598. if ((s->workaround_bugs & FF_BUG_TRUNCATED) &&
  3599. h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  3600. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x - 1,
  3601. s->mb_y, ER_MB_END & part_mask);
  3602. if (s->mb_x >= lf_x_start)
  3603. loop_filter(h, lf_x_start, s->mb_x + 1);
  3604. return 0;
  3605. }
  3606. if (ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
  3607. av_log(h->s.avctx, AV_LOG_ERROR,
  3608. "error while decoding MB %d %d, bytestream (%td)\n",
  3609. s->mb_x, s->mb_y,
  3610. h->cabac.bytestream_end - h->cabac.bytestream);
  3611. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x,
  3612. s->mb_y, ER_MB_ERROR & part_mask);
  3613. return -1;
  3614. }
  3615. if (++s->mb_x >= s->mb_width) {
  3616. loop_filter(h, lf_x_start, s->mb_x);
  3617. s->mb_x = lf_x_start = 0;
  3618. decode_finish_row(h);
  3619. ++s->mb_y;
  3620. if (FIELD_OR_MBAFF_PICTURE) {
  3621. ++s->mb_y;
  3622. if (FRAME_MBAFF && s->mb_y < s->mb_height)
  3623. predict_field_decoding_flag(h);
  3624. }
  3625. }
  3626. if (eos || s->mb_y >= s->mb_height) {
  3627. tprintf(s->avctx, "slice end %d %d\n",
  3628. get_bits_count(&s->gb), s->gb.size_in_bits);
  3629. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x - 1,
  3630. s->mb_y, ER_MB_END & part_mask);
  3631. if (s->mb_x > lf_x_start)
  3632. loop_filter(h, lf_x_start, s->mb_x);
  3633. return 0;
  3634. }
  3635. }
  3636. } else {
  3637. for (;;) {
  3638. int ret = ff_h264_decode_mb_cavlc(h);
  3639. if (ret >= 0)
  3640. ff_h264_hl_decode_mb(h);
  3641. // FIXME optimal? or let mb_decode decode 16x32 ?
  3642. if (ret >= 0 && FRAME_MBAFF) {
  3643. s->mb_y++;
  3644. ret = ff_h264_decode_mb_cavlc(h);
  3645. if (ret >= 0)
  3646. ff_h264_hl_decode_mb(h);
  3647. s->mb_y--;
  3648. }
  3649. if (ret < 0) {
  3650. av_log(h->s.avctx, AV_LOG_ERROR,
  3651. "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
  3652. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x,
  3653. s->mb_y, ER_MB_ERROR & part_mask);
  3654. return -1;
  3655. }
  3656. if (++s->mb_x >= s->mb_width) {
  3657. loop_filter(h, lf_x_start, s->mb_x);
  3658. s->mb_x = lf_x_start = 0;
  3659. decode_finish_row(h);
  3660. ++s->mb_y;
  3661. if (FIELD_OR_MBAFF_PICTURE) {
  3662. ++s->mb_y;
  3663. if (FRAME_MBAFF && s->mb_y < s->mb_height)
  3664. predict_field_decoding_flag(h);
  3665. }
  3666. if (s->mb_y >= s->mb_height) {
  3667. tprintf(s->avctx, "slice end %d %d\n",
  3668. get_bits_count(&s->gb), s->gb.size_in_bits);
  3669. if ( get_bits_left(&s->gb) == 0
  3670. || get_bits_left(&s->gb) > 0 && !(s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
  3671. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y,
  3672. s->mb_x - 1, s->mb_y,
  3673. ER_MB_END & part_mask);
  3674. return 0;
  3675. } else {
  3676. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y,
  3677. s->mb_x, s->mb_y,
  3678. ER_MB_END & part_mask);
  3679. return -1;
  3680. }
  3681. }
  3682. }
  3683. if (get_bits_left(&s->gb) <= 0 && s->mb_skip_run <= 0) {
  3684. tprintf(s->avctx, "slice end %d %d\n",
  3685. get_bits_count(&s->gb), s->gb.size_in_bits);
  3686. if (get_bits_left(&s->gb) == 0) {
  3687. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y,
  3688. s->mb_x - 1, s->mb_y,
  3689. ER_MB_END & part_mask);
  3690. if (s->mb_x > lf_x_start)
  3691. loop_filter(h, lf_x_start, s->mb_x);
  3692. return 0;
  3693. } else {
  3694. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x,
  3695. s->mb_y, ER_MB_ERROR & part_mask);
  3696. return -1;
  3697. }
  3698. }
  3699. }
  3700. }
  3701. }
  3702. /**
  3703. * Call decode_slice() for each context.
  3704. *
  3705. * @param h h264 master context
  3706. * @param context_count number of contexts to execute
  3707. */
  3708. static int execute_decode_slices(H264Context *h, int context_count)
  3709. {
  3710. MpegEncContext *const s = &h->s;
  3711. AVCodecContext *const avctx = s->avctx;
  3712. H264Context *hx;
  3713. int i;
  3714. if (s->avctx->hwaccel ||
  3715. s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  3716. return 0;
  3717. if (context_count == 1) {
  3718. return decode_slice(avctx, &h);
  3719. } else {
  3720. for (i = 1; i < context_count; i++) {
  3721. hx = h->thread_context[i];
  3722. hx->s.err_recognition = avctx->err_recognition;
  3723. hx->s.error_count = 0;
  3724. hx->x264_build = h->x264_build;
  3725. }
  3726. avctx->execute(avctx, decode_slice, h->thread_context,
  3727. NULL, context_count, sizeof(void *));
  3728. /* pull back stuff from slices to master context */
  3729. hx = h->thread_context[context_count - 1];
  3730. s->mb_x = hx->s.mb_x;
  3731. s->mb_y = hx->s.mb_y;
  3732. s->dropable = hx->s.dropable;
  3733. s->picture_structure = hx->s.picture_structure;
  3734. for (i = 1; i < context_count; i++)
  3735. h->s.error_count += h->thread_context[i]->s.error_count;
  3736. }
  3737. return 0;
  3738. }
  3739. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
  3740. {
  3741. MpegEncContext *const s = &h->s;
  3742. AVCodecContext *const avctx = s->avctx;
  3743. H264Context *hx; ///< thread context
  3744. int buf_index;
  3745. int context_count;
  3746. int next_avc;
  3747. int pass = !(avctx->active_thread_type & FF_THREAD_FRAME);
  3748. int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
  3749. int nal_index;
  3750. h->nal_unit_type= 0;
  3751. if(!s->slice_context_count)
  3752. s->slice_context_count= 1;
  3753. h->max_contexts = s->slice_context_count;
  3754. if (!(s->flags2 & CODEC_FLAG2_CHUNKS)) {
  3755. h->current_slice = 0;
  3756. if (!s->first_field)
  3757. s->current_picture_ptr = NULL;
  3758. ff_h264_reset_sei(h);
  3759. }
  3760. for (; pass <= 1; pass++) {
  3761. buf_index = 0;
  3762. context_count = 0;
  3763. next_avc = h->is_avc ? 0 : buf_size;
  3764. nal_index = 0;
  3765. for (;;) {
  3766. int consumed;
  3767. int dst_length;
  3768. int bit_length;
  3769. const uint8_t *ptr;
  3770. int i, nalsize = 0;
  3771. int err;
  3772. if (buf_index >= next_avc) {
  3773. if (buf_index >= buf_size - h->nal_length_size)
  3774. break;
  3775. nalsize = 0;
  3776. for (i = 0; i < h->nal_length_size; i++)
  3777. nalsize = (nalsize << 8) | buf[buf_index++];
  3778. if (nalsize <= 0 || nalsize > buf_size - buf_index) {
  3779. av_log(h->s.avctx, AV_LOG_ERROR,
  3780. "AVC: nal size %d\n", nalsize);
  3781. break;
  3782. }
  3783. next_avc = buf_index + nalsize;
  3784. } else {
  3785. // start code prefix search
  3786. for (; buf_index + 3 < next_avc; buf_index++)
  3787. // This should always succeed in the first iteration.
  3788. if (buf[buf_index] == 0 &&
  3789. buf[buf_index + 1] == 0 &&
  3790. buf[buf_index + 2] == 1)
  3791. break;
  3792. if (buf_index + 3 >= buf_size)
  3793. break;
  3794. buf_index += 3;
  3795. if (buf_index >= next_avc)
  3796. continue;
  3797. }
  3798. hx = h->thread_context[context_count];
  3799. ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
  3800. &consumed, next_avc - buf_index);
  3801. if (ptr == NULL || dst_length < 0) {
  3802. buf_index = -1;
  3803. goto end;
  3804. }
  3805. i = buf_index + consumed;
  3806. if ((s->workaround_bugs & FF_BUG_AUTODETECT) && i + 3 < next_avc &&
  3807. buf[i] == 0x00 && buf[i + 1] == 0x00 &&
  3808. buf[i + 2] == 0x01 && buf[i + 3] == 0xE0)
  3809. s->workaround_bugs |= FF_BUG_TRUNCATED;
  3810. if (!(s->workaround_bugs & FF_BUG_TRUNCATED))
  3811. while(dst_length > 0 && ptr[dst_length - 1] == 0)
  3812. dst_length--;
  3813. bit_length = !dst_length ? 0
  3814. : (8 * dst_length -
  3815. decode_rbsp_trailing(h, ptr + dst_length - 1));
  3816. if (s->avctx->debug & FF_DEBUG_STARTCODE)
  3817. av_log(h->s.avctx, AV_LOG_DEBUG, "NAL %d/%d at %d/%d length %d pass %d\n", hx->nal_unit_type, hx->nal_ref_idc, buf_index, buf_size, dst_length, pass);
  3818. if (h->is_avc && (nalsize != consumed) && nalsize)
  3819. av_log(h->s.avctx, AV_LOG_DEBUG,
  3820. "AVC: Consumed only %d bytes instead of %d\n",
  3821. consumed, nalsize);
  3822. buf_index += consumed;
  3823. nal_index++;
  3824. if (pass == 0) {
  3825. /* packets can sometimes contain multiple PPS/SPS,
  3826. * e.g. two PAFF field pictures in one packet, or a demuxer
  3827. * which splits NALs strangely if so, when frame threading we
  3828. * can't start the next thread until we've read all of them */
  3829. switch (hx->nal_unit_type) {
  3830. case NAL_SPS:
  3831. case NAL_PPS:
  3832. nals_needed = nal_index;
  3833. break;
  3834. case NAL_IDR_SLICE:
  3835. case NAL_SLICE:
  3836. init_get_bits(&hx->s.gb, ptr, bit_length);
  3837. if (!get_ue_golomb(&hx->s.gb))
  3838. nals_needed = nal_index;
  3839. }
  3840. continue;
  3841. }
  3842. // FIXME do not discard SEI id
  3843. if (avctx->skip_frame >= AVDISCARD_NONREF && h->nal_ref_idc == 0)
  3844. continue;
  3845. again:
  3846. err = 0;
  3847. switch (hx->nal_unit_type) {
  3848. case NAL_IDR_SLICE:
  3849. if (h->nal_unit_type != NAL_IDR_SLICE) {
  3850. av_log(h->s.avctx, AV_LOG_ERROR,
  3851. "Invalid mix of idr and non-idr slices\n");
  3852. buf_index = -1;
  3853. goto end;
  3854. }
  3855. idr(h); // FIXME ensure we don't lose some frames if there is reordering
  3856. case NAL_SLICE:
  3857. init_get_bits(&hx->s.gb, ptr, bit_length);
  3858. hx->intra_gb_ptr =
  3859. hx->inter_gb_ptr = &hx->s.gb;
  3860. hx->s.data_partitioning = 0;
  3861. if ((err = decode_slice_header(hx, h)))
  3862. break;
  3863. if ( h->sei_recovery_frame_cnt >= 0
  3864. && ( h->recovery_frame<0
  3865. || ((h->recovery_frame - h->frame_num) & ((1 << h->sps.log2_max_frame_num)-1)) > h->sei_recovery_frame_cnt)) {
  3866. h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) %
  3867. (1 << h->sps.log2_max_frame_num);
  3868. }
  3869. s->current_picture_ptr->f.key_frame |=
  3870. (hx->nal_unit_type == NAL_IDR_SLICE);
  3871. if (h->recovery_frame == h->frame_num) {
  3872. s->current_picture_ptr->sync |= 1;
  3873. h->recovery_frame = -1;
  3874. }
  3875. h->sync |= !!s->current_picture_ptr->f.key_frame;
  3876. h->sync |= 3*!!(s->flags2 & CODEC_FLAG2_SHOW_ALL);
  3877. s->current_picture_ptr->sync |= h->sync;
  3878. if (h->current_slice == 1) {
  3879. if (!(s->flags2 & CODEC_FLAG2_CHUNKS))
  3880. decode_postinit(h, nal_index >= nals_needed);
  3881. if (s->avctx->hwaccel &&
  3882. s->avctx->hwaccel->start_frame(s->avctx, NULL, 0) < 0)
  3883. return -1;
  3884. if (CONFIG_H264_VDPAU_DECODER &&
  3885. s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  3886. ff_vdpau_h264_picture_start(s);
  3887. }
  3888. if (hx->redundant_pic_count == 0 &&
  3889. (avctx->skip_frame < AVDISCARD_NONREF ||
  3890. hx->nal_ref_idc) &&
  3891. (avctx->skip_frame < AVDISCARD_BIDIR ||
  3892. hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
  3893. (avctx->skip_frame < AVDISCARD_NONKEY ||
  3894. hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
  3895. avctx->skip_frame < AVDISCARD_ALL) {
  3896. if (avctx->hwaccel) {
  3897. if (avctx->hwaccel->decode_slice(avctx,
  3898. &buf[buf_index - consumed],
  3899. consumed) < 0)
  3900. return -1;
  3901. } else if (CONFIG_H264_VDPAU_DECODER &&
  3902. s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
  3903. static const uint8_t start_code[] = {
  3904. 0x00, 0x00, 0x01 };
  3905. ff_vdpau_add_data_chunk(s, start_code,
  3906. sizeof(start_code));
  3907. ff_vdpau_add_data_chunk(s, &buf[buf_index - consumed],
  3908. consumed);
  3909. } else
  3910. context_count++;
  3911. }
  3912. break;
  3913. case NAL_DPA:
  3914. init_get_bits(&hx->s.gb, ptr, bit_length);
  3915. hx->intra_gb_ptr =
  3916. hx->inter_gb_ptr = NULL;
  3917. if ((err = decode_slice_header(hx, h)) < 0)
  3918. break;
  3919. hx->s.data_partitioning = 1;
  3920. break;
  3921. case NAL_DPB:
  3922. init_get_bits(&hx->intra_gb, ptr, bit_length);
  3923. hx->intra_gb_ptr = &hx->intra_gb;
  3924. break;
  3925. case NAL_DPC:
  3926. init_get_bits(&hx->inter_gb, ptr, bit_length);
  3927. hx->inter_gb_ptr = &hx->inter_gb;
  3928. if (hx->redundant_pic_count == 0 &&
  3929. hx->intra_gb_ptr &&
  3930. hx->s.data_partitioning &&
  3931. s->context_initialized &&
  3932. (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc) &&
  3933. (avctx->skip_frame < AVDISCARD_BIDIR ||
  3934. hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
  3935. (avctx->skip_frame < AVDISCARD_NONKEY ||
  3936. hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
  3937. avctx->skip_frame < AVDISCARD_ALL)
  3938. context_count++;
  3939. break;
  3940. case NAL_SEI:
  3941. init_get_bits(&s->gb, ptr, bit_length);
  3942. ff_h264_decode_sei(h);
  3943. break;
  3944. case NAL_SPS:
  3945. init_get_bits(&s->gb, ptr, bit_length);
  3946. if (ff_h264_decode_seq_parameter_set(h) < 0 && (h->is_avc ? (nalsize != consumed) && nalsize : 1)) {
  3947. av_log(h->s.avctx, AV_LOG_DEBUG,
  3948. "SPS decoding failure, trying alternative mode\n");
  3949. if (h->is_avc)
  3950. av_assert0(next_avc - buf_index + consumed == nalsize);
  3951. init_get_bits(&s->gb, &buf[buf_index + 1 - consumed],
  3952. 8*(next_avc - buf_index + consumed - 1));
  3953. ff_h264_decode_seq_parameter_set(h);
  3954. }
  3955. if (s->flags & CODEC_FLAG_LOW_DELAY ||
  3956. (h->sps.bitstream_restriction_flag &&
  3957. !h->sps.num_reorder_frames))
  3958. s->low_delay = 1;
  3959. if (avctx->has_b_frames < 2)
  3960. avctx->has_b_frames = !s->low_delay;
  3961. break;
  3962. case NAL_PPS:
  3963. init_get_bits(&s->gb, ptr, bit_length);
  3964. ff_h264_decode_picture_parameter_set(h, bit_length);
  3965. break;
  3966. case NAL_AUD:
  3967. case NAL_END_SEQUENCE:
  3968. case NAL_END_STREAM:
  3969. case NAL_FILLER_DATA:
  3970. case NAL_SPS_EXT:
  3971. case NAL_AUXILIARY_SLICE:
  3972. break;
  3973. default:
  3974. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
  3975. hx->nal_unit_type, bit_length);
  3976. }
  3977. if (context_count == h->max_contexts) {
  3978. execute_decode_slices(h, context_count);
  3979. context_count = 0;
  3980. }
  3981. if (err < 0)
  3982. av_log(h->s.avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  3983. else if (err == 1) {
  3984. /* Slice could not be decoded in parallel mode, copy down
  3985. * NAL unit stuff to context 0 and restart. Note that
  3986. * rbsp_buffer is not transferred, but since we no longer
  3987. * run in parallel mode this should not be an issue. */
  3988. h->nal_unit_type = hx->nal_unit_type;
  3989. h->nal_ref_idc = hx->nal_ref_idc;
  3990. hx = h;
  3991. goto again;
  3992. }
  3993. }
  3994. }
  3995. if (context_count)
  3996. execute_decode_slices(h, context_count);
  3997. end:
  3998. /* clean up */
  3999. if (s->current_picture_ptr && s->current_picture_ptr->owner2 == s &&
  4000. !s->dropable) {
  4001. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX,
  4002. s->picture_structure == PICT_BOTTOM_FIELD);
  4003. }
  4004. return buf_index;
  4005. }
  4006. /**
  4007. * Return the number of bytes consumed for building the current frame.
  4008. */
  4009. static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size)
  4010. {
  4011. if (pos == 0)
  4012. pos = 1; // avoid infinite loops (i doubt that is needed but ...)
  4013. if (pos + 10 > buf_size)
  4014. pos = buf_size; // oops ;)
  4015. return pos;
  4016. }
  4017. static int decode_frame(AVCodecContext *avctx, void *data,
  4018. int *data_size, AVPacket *avpkt)
  4019. {
  4020. const uint8_t *buf = avpkt->data;
  4021. int buf_size = avpkt->size;
  4022. H264Context *h = avctx->priv_data;
  4023. MpegEncContext *s = &h->s;
  4024. AVFrame *pict = data;
  4025. int buf_index = 0;
  4026. Picture *out;
  4027. int i, out_idx;
  4028. s->flags = avctx->flags;
  4029. s->flags2 = avctx->flags2;
  4030. /* end of stream, output what is still in the buffers */
  4031. if (buf_size == 0) {
  4032. out:
  4033. s->current_picture_ptr = NULL;
  4034. // FIXME factorize this with the output code below
  4035. out = h->delayed_pic[0];
  4036. out_idx = 0;
  4037. for (i = 1;
  4038. h->delayed_pic[i] &&
  4039. !h->delayed_pic[i]->f.key_frame &&
  4040. !h->delayed_pic[i]->mmco_reset;
  4041. i++)
  4042. if (h->delayed_pic[i]->poc < out->poc) {
  4043. out = h->delayed_pic[i];
  4044. out_idx = i;
  4045. }
  4046. for (i = out_idx; h->delayed_pic[i]; i++)
  4047. h->delayed_pic[i] = h->delayed_pic[i + 1];
  4048. if (out) {
  4049. *data_size = sizeof(AVFrame);
  4050. *pict = out->f;
  4051. }
  4052. return buf_index;
  4053. }
  4054. if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
  4055. int cnt= buf[5]&0x1f;
  4056. const uint8_t *p= buf+6;
  4057. while(cnt--){
  4058. int nalsize= AV_RB16(p) + 2;
  4059. if(nalsize > buf_size - (p-buf) || p[2]!=0x67)
  4060. goto not_extra;
  4061. p += nalsize;
  4062. }
  4063. cnt = *(p++);
  4064. if(!cnt)
  4065. goto not_extra;
  4066. while(cnt--){
  4067. int nalsize= AV_RB16(p) + 2;
  4068. if(nalsize > buf_size - (p-buf) || p[2]!=0x68)
  4069. goto not_extra;
  4070. p += nalsize;
  4071. }
  4072. return ff_h264_decode_extradata(h, buf, buf_size);
  4073. }
  4074. not_extra:
  4075. buf_index = decode_nal_units(h, buf, buf_size);
  4076. if (buf_index < 0)
  4077. return -1;
  4078. if (!s->current_picture_ptr && h->nal_unit_type == NAL_END_SEQUENCE) {
  4079. av_assert0(buf_index <= buf_size);
  4080. goto out;
  4081. }
  4082. if (!(s->flags2 & CODEC_FLAG2_CHUNKS) && !s->current_picture_ptr) {
  4083. if (avctx->skip_frame >= AVDISCARD_NONREF ||
  4084. buf_size >= 4 && !memcmp("Q264", buf, 4))
  4085. return buf_size;
  4086. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  4087. return -1;
  4088. }
  4089. if (!(s->flags2 & CODEC_FLAG2_CHUNKS) ||
  4090. (s->mb_y >= s->mb_height && s->mb_height)) {
  4091. if (s->flags2 & CODEC_FLAG2_CHUNKS)
  4092. decode_postinit(h, 1);
  4093. field_end(h, 0);
  4094. /* Wait for second field. */
  4095. *data_size = 0;
  4096. if (h->next_output_pic && (h->next_output_pic->sync || h->sync>1)) {
  4097. *data_size = sizeof(AVFrame);
  4098. *pict = h->next_output_pic->f;
  4099. }
  4100. }
  4101. assert(pict->data[0] || !*data_size);
  4102. ff_print_debug_info(s, pict);
  4103. // printf("out %d\n", (int)pict->data[0]);
  4104. return get_consumed_bytes(s, buf_index, buf_size);
  4105. }
  4106. av_cold void ff_h264_free_context(H264Context *h)
  4107. {
  4108. int i;
  4109. free_tables(h, 1); // FIXME cleanup init stuff perhaps
  4110. for (i = 0; i < MAX_SPS_COUNT; i++)
  4111. av_freep(h->sps_buffers + i);
  4112. for (i = 0; i < MAX_PPS_COUNT; i++)
  4113. av_freep(h->pps_buffers + i);
  4114. }
  4115. static av_cold int h264_decode_end(AVCodecContext *avctx)
  4116. {
  4117. H264Context *h = avctx->priv_data;
  4118. MpegEncContext *s = &h->s;
  4119. ff_h264_remove_all_refs(h);
  4120. ff_h264_free_context(h);
  4121. ff_MPV_common_end(s);
  4122. // memset(h, 0, sizeof(H264Context));
  4123. return 0;
  4124. }
  4125. static const AVProfile profiles[] = {
  4126. { FF_PROFILE_H264_BASELINE, "Baseline" },
  4127. { FF_PROFILE_H264_CONSTRAINED_BASELINE, "Constrained Baseline" },
  4128. { FF_PROFILE_H264_MAIN, "Main" },
  4129. { FF_PROFILE_H264_EXTENDED, "Extended" },
  4130. { FF_PROFILE_H264_HIGH, "High" },
  4131. { FF_PROFILE_H264_HIGH_10, "High 10" },
  4132. { FF_PROFILE_H264_HIGH_10_INTRA, "High 10 Intra" },
  4133. { FF_PROFILE_H264_HIGH_422, "High 4:2:2" },
  4134. { FF_PROFILE_H264_HIGH_422_INTRA, "High 4:2:2 Intra" },
  4135. { FF_PROFILE_H264_HIGH_444, "High 4:4:4" },
  4136. { FF_PROFILE_H264_HIGH_444_PREDICTIVE, "High 4:4:4 Predictive" },
  4137. { FF_PROFILE_H264_HIGH_444_INTRA, "High 4:4:4 Intra" },
  4138. { FF_PROFILE_H264_CAVLC_444, "CAVLC 4:4:4" },
  4139. { FF_PROFILE_UNKNOWN },
  4140. };
  4141. static const AVOption h264_options[] = {
  4142. {"is_avc", "is avc", offsetof(H264Context, is_avc), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, 0},
  4143. {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 4, 0},
  4144. {NULL}
  4145. };
  4146. static const AVClass h264_class = {
  4147. "H264 Decoder",
  4148. av_default_item_name,
  4149. h264_options,
  4150. LIBAVUTIL_VERSION_INT,
  4151. };
  4152. static const AVClass h264_vdpau_class = {
  4153. "H264 VDPAU Decoder",
  4154. av_default_item_name,
  4155. h264_options,
  4156. LIBAVUTIL_VERSION_INT,
  4157. };
  4158. AVCodec ff_h264_decoder = {
  4159. .name = "h264",
  4160. .type = AVMEDIA_TYPE_VIDEO,
  4161. .id = CODEC_ID_H264,
  4162. .priv_data_size = sizeof(H264Context),
  4163. .init = ff_h264_decode_init,
  4164. .close = h264_decode_end,
  4165. .decode = decode_frame,
  4166. .capabilities = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 |
  4167. CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS |
  4168. CODEC_CAP_FRAME_THREADS,
  4169. .flush = flush_dpb,
  4170. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  4171. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  4172. .update_thread_context = ONLY_IF_THREADS_ENABLED(decode_update_thread_context),
  4173. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  4174. .priv_class = &h264_class,
  4175. };
  4176. #if CONFIG_H264_VDPAU_DECODER
  4177. AVCodec ff_h264_vdpau_decoder = {
  4178. .name = "h264_vdpau",
  4179. .type = AVMEDIA_TYPE_VIDEO,
  4180. .id = CODEC_ID_H264,
  4181. .priv_data_size = sizeof(H264Context),
  4182. .init = ff_h264_decode_init,
  4183. .close = h264_decode_end,
  4184. .decode = decode_frame,
  4185. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
  4186. .flush = flush_dpb,
  4187. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  4188. .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_VDPAU_H264,
  4189. PIX_FMT_NONE},
  4190. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  4191. .priv_class = &h264_vdpau_class,
  4192. };
  4193. #endif