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.

4704 lines
186KB

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