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.

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