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.

4257 lines
165KB

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