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.

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