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.

4321 lines
165KB

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