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.

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