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.

4303 lines
164KB

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