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.

4296 lines
164KB

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