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.

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