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.

4317 lines
164KB

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