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.

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