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.

4256 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 PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
  62. PIX_FMT_DXVA2_VLD,
  63. PIX_FMT_VAAPI_VLD,
  64. PIX_FMT_VDA_VLD,
  65. PIX_FMT_YUVJ420P,
  66. 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. assert(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. assert(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. return 0;
  994. }
  995. #define IN_RANGE(a, b, size) (((a) >= (b)) && ((a) < ((b) + (size))))
  996. static void copy_picture_range(Picture **to, Picture **from, int count,
  997. MpegEncContext *new_base,
  998. MpegEncContext *old_base)
  999. {
  1000. int i;
  1001. for (i = 0; i < count; i++) {
  1002. assert((IN_RANGE(from[i], old_base, sizeof(*old_base)) ||
  1003. IN_RANGE(from[i], old_base->picture,
  1004. sizeof(Picture) * old_base->picture_count) ||
  1005. !from[i]));
  1006. to[i] = REBASE_PICTURE(from[i], new_base, old_base);
  1007. }
  1008. }
  1009. static void copy_parameter_set(void **to, void **from, int count, int size)
  1010. {
  1011. int i;
  1012. for (i = 0; i < count; i++) {
  1013. if (to[i] && !from[i])
  1014. av_freep(&to[i]);
  1015. else if (from[i] && !to[i])
  1016. to[i] = av_malloc(size);
  1017. if (from[i])
  1018. memcpy(to[i], from[i], size);
  1019. }
  1020. }
  1021. static int decode_init_thread_copy(AVCodecContext *avctx)
  1022. {
  1023. H264Context *h = avctx->priv_data;
  1024. if (!avctx->internal->is_copy)
  1025. return 0;
  1026. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  1027. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  1028. return 0;
  1029. }
  1030. #define copy_fields(to, from, start_field, end_field) \
  1031. memcpy(&to->start_field, &from->start_field, \
  1032. (char *)&to->end_field - (char *)&to->start_field)
  1033. static int decode_update_thread_context(AVCodecContext *dst,
  1034. const AVCodecContext *src)
  1035. {
  1036. H264Context *h = dst->priv_data, *h1 = src->priv_data;
  1037. MpegEncContext *const s = &h->s, *const s1 = &h1->s;
  1038. int inited = s->context_initialized, err;
  1039. int i;
  1040. if (dst == src)
  1041. return 0;
  1042. err = ff_mpeg_update_thread_context(dst, src);
  1043. if (err)
  1044. return err;
  1045. // FIXME handle width/height changing
  1046. if (!inited) {
  1047. for (i = 0; i < MAX_SPS_COUNT; i++)
  1048. av_freep(h->sps_buffers + i);
  1049. for (i = 0; i < MAX_PPS_COUNT; i++)
  1050. av_freep(h->pps_buffers + i);
  1051. // copy all fields after MpegEnc
  1052. memcpy(&h->s + 1, &h1->s + 1,
  1053. sizeof(H264Context) - sizeof(MpegEncContext));
  1054. memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
  1055. memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
  1056. if (s1->context_initialized) {
  1057. if (ff_h264_alloc_tables(h) < 0) {
  1058. av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
  1059. return AVERROR(ENOMEM);
  1060. }
  1061. context_init(h);
  1062. /* frame_start may not be called for the next thread (if it's decoding
  1063. * a bottom field) so this has to be allocated here */
  1064. h->s.obmc_scratchpad = av_malloc(16 * 6 * s->linesize);
  1065. }
  1066. for (i = 0; i < 2; i++) {
  1067. h->rbsp_buffer[i] = NULL;
  1068. h->rbsp_buffer_size[i] = 0;
  1069. }
  1070. h->thread_context[0] = h;
  1071. s->dsp.clear_blocks(h->mb);
  1072. s->dsp.clear_blocks(h->mb + (24 * 16 << h->pixel_shift));
  1073. }
  1074. // extradata/NAL handling
  1075. h->is_avc = h1->is_avc;
  1076. // SPS/PPS
  1077. copy_parameter_set((void **)h->sps_buffers, (void **)h1->sps_buffers,
  1078. MAX_SPS_COUNT, sizeof(SPS));
  1079. h->sps = h1->sps;
  1080. copy_parameter_set((void **)h->pps_buffers, (void **)h1->pps_buffers,
  1081. MAX_PPS_COUNT, sizeof(PPS));
  1082. h->pps = h1->pps;
  1083. // Dequantization matrices
  1084. // FIXME these are big - can they be only copied when PPS changes?
  1085. copy_fields(h, h1, dequant4_buffer, dequant4_coeff);
  1086. for (i = 0; i < 6; i++)
  1087. h->dequant4_coeff[i] = h->dequant4_buffer[0] +
  1088. (h1->dequant4_coeff[i] - h1->dequant4_buffer[0]);
  1089. for (i = 0; i < 6; i++)
  1090. h->dequant8_coeff[i] = h->dequant8_buffer[0] +
  1091. (h1->dequant8_coeff[i] - h1->dequant8_buffer[0]);
  1092. h->dequant_coeff_pps = h1->dequant_coeff_pps;
  1093. // POC timing
  1094. copy_fields(h, h1, poc_lsb, redundant_pic_count);
  1095. // reference lists
  1096. copy_fields(h, h1, ref_count, list_count);
  1097. copy_fields(h, h1, ref_list, intra_gb);
  1098. copy_fields(h, h1, short_ref, cabac_init_idc);
  1099. copy_picture_range(h->short_ref, h1->short_ref, 32, s, s1);
  1100. copy_picture_range(h->long_ref, h1->long_ref, 32, s, s1);
  1101. copy_picture_range(h->delayed_pic, h1->delayed_pic,
  1102. MAX_DELAYED_PIC_COUNT + 2, s, s1);
  1103. h->last_slice_type = h1->last_slice_type;
  1104. h->sync = h1->sync;
  1105. if (!s->current_picture_ptr)
  1106. return 0;
  1107. if (!s->dropable) {
  1108. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  1109. h->prev_poc_msb = h->poc_msb;
  1110. h->prev_poc_lsb = h->poc_lsb;
  1111. }
  1112. h->prev_frame_num_offset = h->frame_num_offset;
  1113. h->prev_frame_num = h->frame_num;
  1114. h->outputed_poc = h->next_outputed_poc;
  1115. return err;
  1116. }
  1117. int ff_h264_frame_start(H264Context *h)
  1118. {
  1119. MpegEncContext *const s = &h->s;
  1120. int i;
  1121. const int pixel_shift = h->pixel_shift;
  1122. if (ff_MPV_frame_start(s, s->avctx) < 0)
  1123. return -1;
  1124. ff_er_frame_start(s);
  1125. /*
  1126. * ff_MPV_frame_start uses pict_type to derive key_frame.
  1127. * This is incorrect for H.264; IDR markings must be used.
  1128. * Zero here; IDR markings per slice in frame or fields are ORed in later.
  1129. * See decode_nal_units().
  1130. */
  1131. s->current_picture_ptr->f.key_frame = 0;
  1132. s->current_picture_ptr->sync = 0;
  1133. s->current_picture_ptr->mmco_reset = 0;
  1134. assert(s->linesize && s->uvlinesize);
  1135. for (i = 0; i < 16; i++) {
  1136. h->block_offset[i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * s->linesize * ((scan8[i] - scan8[0]) >> 3);
  1137. h->block_offset[48 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * s->linesize * ((scan8[i] - scan8[0]) >> 3);
  1138. }
  1139. for (i = 0; i < 16; i++) {
  1140. h->block_offset[16 + i] =
  1141. h->block_offset[32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 4 * s->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1142. h->block_offset[48 + 16 + i] =
  1143. h->block_offset[48 + 32 + i] = (4 * ((scan8[i] - scan8[0]) & 7) << pixel_shift) + 8 * s->uvlinesize * ((scan8[i] - scan8[0]) >> 3);
  1144. }
  1145. /* can't be in alloc_tables because linesize isn't known there.
  1146. * FIXME: redo bipred weight to not require extra buffer? */
  1147. for (i = 0; i < s->slice_context_count; i++)
  1148. if (h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
  1149. h->thread_context[i]->s.obmc_scratchpad = av_malloc(16 * 6 * s->linesize);
  1150. /* Some macroblocks can be accessed before they're available in case
  1151. * of lost slices, MBAFF or threading. */
  1152. memset(h->slice_table, -1,
  1153. (s->mb_height * s->mb_stride - 1) * sizeof(*h->slice_table));
  1154. // s->decode = (s->flags & CODEC_FLAG_PSNR) || !s->encoding ||
  1155. // s->current_picture.f.reference /* || h->contains_intra */ || 1;
  1156. /* We mark the current picture as non-reference after allocating it, so
  1157. * that if we break out due to an error it can be released automatically
  1158. * in the next ff_MPV_frame_start().
  1159. * SVQ3 as well as most other codecs have only last/next/current and thus
  1160. * get released even with set reference, besides SVQ3 and others do not
  1161. * mark frames as reference later "naturally". */
  1162. if (s->codec_id != AV_CODEC_ID_SVQ3)
  1163. s->current_picture_ptr->f.reference = 0;
  1164. s->current_picture_ptr->field_poc[0] =
  1165. s->current_picture_ptr->field_poc[1] = INT_MAX;
  1166. h->next_output_pic = NULL;
  1167. assert(s->current_picture_ptr->long_ref == 0);
  1168. return 0;
  1169. }
  1170. /**
  1171. * Run setup operations that must be run after slice header decoding.
  1172. * This includes finding the next displayed frame.
  1173. *
  1174. * @param h h264 master context
  1175. * @param setup_finished enough NALs have been read that we can call
  1176. * ff_thread_finish_setup()
  1177. */
  1178. static void decode_postinit(H264Context *h, int setup_finished)
  1179. {
  1180. MpegEncContext *const s = &h->s;
  1181. Picture *out = s->current_picture_ptr;
  1182. Picture *cur = s->current_picture_ptr;
  1183. int i, pics, out_of_order, out_idx;
  1184. s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_H264;
  1185. s->current_picture_ptr->f.pict_type = s->pict_type;
  1186. if (h->next_output_pic)
  1187. return;
  1188. if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
  1189. /* FIXME: if we have two PAFF fields in one packet, we can't start
  1190. * the next thread here. If we have one field per packet, we can.
  1191. * The check in decode_nal_units() is not good enough to find this
  1192. * yet, so we assume the worst for now. */
  1193. // if (setup_finished)
  1194. // ff_thread_finish_setup(s->avctx);
  1195. return;
  1196. }
  1197. cur->f.interlaced_frame = 0;
  1198. cur->f.repeat_pict = 0;
  1199. /* Signal interlacing information externally. */
  1200. /* Prioritize picture timing SEI information over used
  1201. * decoding process if it exists. */
  1202. if (h->sps.pic_struct_present_flag) {
  1203. switch (h->sei_pic_struct) {
  1204. case SEI_PIC_STRUCT_FRAME:
  1205. break;
  1206. case SEI_PIC_STRUCT_TOP_FIELD:
  1207. case SEI_PIC_STRUCT_BOTTOM_FIELD:
  1208. cur->f.interlaced_frame = 1;
  1209. break;
  1210. case SEI_PIC_STRUCT_TOP_BOTTOM:
  1211. case SEI_PIC_STRUCT_BOTTOM_TOP:
  1212. if (FIELD_OR_MBAFF_PICTURE)
  1213. cur->f.interlaced_frame = 1;
  1214. else
  1215. // try to flag soft telecine progressive
  1216. cur->f.interlaced_frame = h->prev_interlaced_frame;
  1217. break;
  1218. case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
  1219. case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
  1220. /* Signal the possibility of telecined film externally
  1221. * (pic_struct 5,6). From these hints, let the applications
  1222. * decide if they apply deinterlacing. */
  1223. cur->f.repeat_pict = 1;
  1224. break;
  1225. case SEI_PIC_STRUCT_FRAME_DOUBLING:
  1226. // Force progressive here, doubling interlaced frame is a bad idea.
  1227. cur->f.repeat_pict = 2;
  1228. break;
  1229. case SEI_PIC_STRUCT_FRAME_TRIPLING:
  1230. cur->f.repeat_pict = 4;
  1231. break;
  1232. }
  1233. if ((h->sei_ct_type & 3) &&
  1234. h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
  1235. cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
  1236. } else {
  1237. /* Derive interlacing flag from used decoding process. */
  1238. cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE;
  1239. }
  1240. h->prev_interlaced_frame = cur->f.interlaced_frame;
  1241. if (cur->field_poc[0] != cur->field_poc[1]) {
  1242. /* Derive top_field_first from field pocs. */
  1243. cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
  1244. } else {
  1245. if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
  1246. /* Use picture timing SEI information. Even if it is a
  1247. * information of a past frame, better than nothing. */
  1248. if (h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM ||
  1249. h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
  1250. cur->f.top_field_first = 1;
  1251. else
  1252. cur->f.top_field_first = 0;
  1253. } else {
  1254. /* Most likely progressive */
  1255. cur->f.top_field_first = 0;
  1256. }
  1257. }
  1258. cur->mmco_reset = h->mmco_reset;
  1259. h->mmco_reset = 0;
  1260. // FIXME do something with unavailable reference frames
  1261. /* Sort B-frames into display order */
  1262. if (h->sps.bitstream_restriction_flag &&
  1263. s->avctx->has_b_frames < h->sps.num_reorder_frames) {
  1264. s->avctx->has_b_frames = h->sps.num_reorder_frames;
  1265. s->low_delay = 0;
  1266. }
  1267. if (s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT &&
  1268. !h->sps.bitstream_restriction_flag) {
  1269. s->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
  1270. s->low_delay = 0;
  1271. }
  1272. for (i = 0; 1; i++) {
  1273. if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
  1274. if(i)
  1275. h->last_pocs[i-1] = cur->poc;
  1276. break;
  1277. } else if(i) {
  1278. h->last_pocs[i-1]= h->last_pocs[i];
  1279. }
  1280. }
  1281. out_of_order = MAX_DELAYED_PIC_COUNT - i;
  1282. if( cur->f.pict_type == AV_PICTURE_TYPE_B
  1283. || (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))
  1284. out_of_order = FFMAX(out_of_order, 1);
  1285. if(s->avctx->has_b_frames < out_of_order && !h->sps.bitstream_restriction_flag){
  1286. av_log(s->avctx, AV_LOG_VERBOSE, "Increasing reorder buffer to %d\n", out_of_order);
  1287. s->avctx->has_b_frames = out_of_order;
  1288. s->low_delay = 0;
  1289. }
  1290. pics = 0;
  1291. while (h->delayed_pic[pics])
  1292. pics++;
  1293. av_assert0(pics <= MAX_DELAYED_PIC_COUNT);
  1294. h->delayed_pic[pics++] = cur;
  1295. if (cur->f.reference == 0)
  1296. cur->f.reference = DELAYED_PIC_REF;
  1297. out = h->delayed_pic[0];
  1298. out_idx = 0;
  1299. for (i = 1; h->delayed_pic[i] &&
  1300. !h->delayed_pic[i]->f.key_frame &&
  1301. !h->delayed_pic[i]->mmco_reset;
  1302. i++)
  1303. if (h->delayed_pic[i]->poc < out->poc) {
  1304. out = h->delayed_pic[i];
  1305. out_idx = i;
  1306. }
  1307. if (s->avctx->has_b_frames == 0 &&
  1308. (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset))
  1309. h->next_outputed_poc = INT_MIN;
  1310. out_of_order = out->poc < h->next_outputed_poc;
  1311. if (out_of_order || pics > s->avctx->has_b_frames) {
  1312. out->f.reference &= ~DELAYED_PIC_REF;
  1313. // for frame threading, the owner must be the second field's thread or
  1314. // else the first thread can release the picture and reuse it unsafely
  1315. out->owner2 = s;
  1316. for (i = out_idx; h->delayed_pic[i]; i++)
  1317. h->delayed_pic[i] = h->delayed_pic[i + 1];
  1318. }
  1319. if (!out_of_order && pics > s->avctx->has_b_frames) {
  1320. h->next_output_pic = out;
  1321. if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f.key_frame || h->delayed_pic[0]->mmco_reset)) {
  1322. h->next_outputed_poc = INT_MIN;
  1323. } else
  1324. h->next_outputed_poc = out->poc;
  1325. } else {
  1326. av_log(s->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
  1327. }
  1328. if (h->next_output_pic && h->next_output_pic->sync) {
  1329. h->sync |= 2;
  1330. }
  1331. if (setup_finished)
  1332. ff_thread_finish_setup(s->avctx);
  1333. }
  1334. static av_always_inline void backup_mb_border(H264Context *h, uint8_t *src_y,
  1335. uint8_t *src_cb, uint8_t *src_cr,
  1336. int linesize, int uvlinesize,
  1337. int simple)
  1338. {
  1339. MpegEncContext *const s = &h->s;
  1340. uint8_t *top_border;
  1341. int top_idx = 1;
  1342. const int pixel_shift = h->pixel_shift;
  1343. int chroma444 = CHROMA444;
  1344. int chroma422 = CHROMA422;
  1345. src_y -= linesize;
  1346. src_cb -= uvlinesize;
  1347. src_cr -= uvlinesize;
  1348. if (!simple && FRAME_MBAFF) {
  1349. if (s->mb_y & 1) {
  1350. if (!MB_MBAFF) {
  1351. top_border = h->top_borders[0][s->mb_x];
  1352. AV_COPY128(top_border, src_y + 15 * linesize);
  1353. if (pixel_shift)
  1354. AV_COPY128(top_border + 16, src_y + 15 * linesize + 16);
  1355. if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1356. if (chroma444) {
  1357. if (pixel_shift) {
  1358. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  1359. AV_COPY128(top_border + 48, src_cb + 15 * uvlinesize + 16);
  1360. AV_COPY128(top_border + 64, src_cr + 15 * uvlinesize);
  1361. AV_COPY128(top_border + 80, src_cr + 15 * uvlinesize + 16);
  1362. } else {
  1363. AV_COPY128(top_border + 16, src_cb + 15 * uvlinesize);
  1364. AV_COPY128(top_border + 32, src_cr + 15 * uvlinesize);
  1365. }
  1366. } else if (chroma422) {
  1367. if (pixel_shift) {
  1368. AV_COPY128(top_border + 32, src_cb + 15 * uvlinesize);
  1369. AV_COPY128(top_border + 48, src_cr + 15 * uvlinesize);
  1370. } else {
  1371. AV_COPY64(top_border + 16, src_cb + 15 * uvlinesize);
  1372. AV_COPY64(top_border + 24, src_cr + 15 * uvlinesize);
  1373. }
  1374. } else {
  1375. if (pixel_shift) {
  1376. AV_COPY128(top_border + 32, src_cb + 7 * uvlinesize);
  1377. AV_COPY128(top_border + 48, src_cr + 7 * uvlinesize);
  1378. } else {
  1379. AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
  1380. AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
  1381. }
  1382. }
  1383. }
  1384. }
  1385. } else if (MB_MBAFF) {
  1386. top_idx = 0;
  1387. } else
  1388. return;
  1389. }
  1390. top_border = h->top_borders[top_idx][s->mb_x];
  1391. /* There are two lines saved, the line above the top macroblock
  1392. * of a pair, and the line above the bottom macroblock. */
  1393. AV_COPY128(top_border, src_y + 16 * linesize);
  1394. if (pixel_shift)
  1395. AV_COPY128(top_border + 16, src_y + 16 * linesize + 16);
  1396. if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1397. if (chroma444) {
  1398. if (pixel_shift) {
  1399. AV_COPY128(top_border + 32, src_cb + 16 * linesize);
  1400. AV_COPY128(top_border + 48, src_cb + 16 * linesize + 16);
  1401. AV_COPY128(top_border + 64, src_cr + 16 * linesize);
  1402. AV_COPY128(top_border + 80, src_cr + 16 * linesize + 16);
  1403. } else {
  1404. AV_COPY128(top_border + 16, src_cb + 16 * linesize);
  1405. AV_COPY128(top_border + 32, src_cr + 16 * linesize);
  1406. }
  1407. } else if (chroma422) {
  1408. if (pixel_shift) {
  1409. AV_COPY128(top_border + 32, src_cb + 16 * uvlinesize);
  1410. AV_COPY128(top_border + 48, src_cr + 16 * uvlinesize);
  1411. } else {
  1412. AV_COPY64(top_border + 16, src_cb + 16 * uvlinesize);
  1413. AV_COPY64(top_border + 24, src_cr + 16 * uvlinesize);
  1414. }
  1415. } else {
  1416. if (pixel_shift) {
  1417. AV_COPY128(top_border + 32, src_cb + 8 * uvlinesize);
  1418. AV_COPY128(top_border + 48, src_cr + 8 * uvlinesize);
  1419. } else {
  1420. AV_COPY64(top_border + 16, src_cb + 8 * uvlinesize);
  1421. AV_COPY64(top_border + 24, src_cr + 8 * uvlinesize);
  1422. }
  1423. }
  1424. }
  1425. }
  1426. static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
  1427. uint8_t *src_cb, uint8_t *src_cr,
  1428. int linesize, int uvlinesize,
  1429. int xchg, int chroma444,
  1430. int simple, int pixel_shift)
  1431. {
  1432. MpegEncContext *const s = &h->s;
  1433. int deblock_topleft;
  1434. int deblock_top;
  1435. int top_idx = 1;
  1436. uint8_t *top_border_m1;
  1437. uint8_t *top_border;
  1438. if (!simple && FRAME_MBAFF) {
  1439. if (s->mb_y & 1) {
  1440. if (!MB_MBAFF)
  1441. return;
  1442. } else {
  1443. top_idx = MB_MBAFF ? 0 : 1;
  1444. }
  1445. }
  1446. if (h->deblocking_filter == 2) {
  1447. deblock_topleft = h->slice_table[h->mb_xy - 1 - s->mb_stride] == h->slice_num;
  1448. deblock_top = h->top_type;
  1449. } else {
  1450. deblock_topleft = (s->mb_x > 0);
  1451. deblock_top = (s->mb_y > !!MB_FIELD);
  1452. }
  1453. src_y -= linesize + 1 + pixel_shift;
  1454. src_cb -= uvlinesize + 1 + pixel_shift;
  1455. src_cr -= uvlinesize + 1 + pixel_shift;
  1456. top_border_m1 = h->top_borders[top_idx][s->mb_x - 1];
  1457. top_border = h->top_borders[top_idx][s->mb_x];
  1458. #define XCHG(a, b, xchg) \
  1459. if (pixel_shift) { \
  1460. if (xchg) { \
  1461. AV_SWAP64(b + 0, a + 0); \
  1462. AV_SWAP64(b + 8, a + 8); \
  1463. } else { \
  1464. AV_COPY128(b, a); \
  1465. } \
  1466. } else if (xchg) \
  1467. AV_SWAP64(b, a); \
  1468. else \
  1469. AV_COPY64(b, a);
  1470. if (deblock_top) {
  1471. if (deblock_topleft) {
  1472. XCHG(top_border_m1 + (8 << pixel_shift),
  1473. src_y - (7 << pixel_shift), 1);
  1474. }
  1475. XCHG(top_border + (0 << pixel_shift), src_y + (1 << pixel_shift), xchg);
  1476. XCHG(top_border + (8 << pixel_shift), src_y + (9 << pixel_shift), 1);
  1477. if (s->mb_x + 1 < s->mb_width) {
  1478. XCHG(h->top_borders[top_idx][s->mb_x + 1],
  1479. src_y + (17 << pixel_shift), 1);
  1480. }
  1481. }
  1482. if (simple || !CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
  1483. if (chroma444) {
  1484. if (deblock_topleft) {
  1485. XCHG(top_border_m1 + (24 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  1486. XCHG(top_border_m1 + (40 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  1487. }
  1488. XCHG(top_border + (16 << pixel_shift), src_cb + (1 << pixel_shift), xchg);
  1489. XCHG(top_border + (24 << pixel_shift), src_cb + (9 << pixel_shift), 1);
  1490. XCHG(top_border + (32 << pixel_shift), src_cr + (1 << pixel_shift), xchg);
  1491. XCHG(top_border + (40 << pixel_shift), src_cr + (9 << pixel_shift), 1);
  1492. if (s->mb_x + 1 < s->mb_width) {
  1493. XCHG(h->top_borders[top_idx][s->mb_x + 1] + (16 << pixel_shift), src_cb + (17 << pixel_shift), 1);
  1494. XCHG(h->top_borders[top_idx][s->mb_x + 1] + (32 << pixel_shift), src_cr + (17 << pixel_shift), 1);
  1495. }
  1496. } else {
  1497. if (deblock_top) {
  1498. if (deblock_topleft) {
  1499. XCHG(top_border_m1 + (16 << pixel_shift), src_cb - (7 << pixel_shift), 1);
  1500. XCHG(top_border_m1 + (24 << pixel_shift), src_cr - (7 << pixel_shift), 1);
  1501. }
  1502. XCHG(top_border + (16 << pixel_shift), src_cb + 1 + pixel_shift, 1);
  1503. XCHG(top_border + (24 << pixel_shift), src_cr + 1 + pixel_shift, 1);
  1504. }
  1505. }
  1506. }
  1507. }
  1508. static av_always_inline int dctcoef_get(DCTELEM *mb, int high_bit_depth,
  1509. int index)
  1510. {
  1511. if (high_bit_depth) {
  1512. return AV_RN32A(((int32_t *)mb) + index);
  1513. } else
  1514. return AV_RN16A(mb + index);
  1515. }
  1516. static av_always_inline void dctcoef_set(DCTELEM *mb, int high_bit_depth,
  1517. int index, int value)
  1518. {
  1519. if (high_bit_depth) {
  1520. AV_WN32A(((int32_t *)mb) + index, value);
  1521. } else
  1522. AV_WN16A(mb + index, value);
  1523. }
  1524. static av_always_inline void hl_decode_mb_predict_luma(H264Context *h,
  1525. int mb_type, int is_h264,
  1526. int simple,
  1527. int transform_bypass,
  1528. int pixel_shift,
  1529. int *block_offset,
  1530. int linesize,
  1531. uint8_t *dest_y, int p)
  1532. {
  1533. MpegEncContext *const s = &h->s;
  1534. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1535. void (*idct_dc_add)(uint8_t *dst, DCTELEM *block, int stride);
  1536. int i;
  1537. int qscale = p == 0 ? s->qscale : h->chroma_qp[p - 1];
  1538. block_offset += 16 * p;
  1539. if (IS_INTRA4x4(mb_type)) {
  1540. if (simple || !s->encoding) {
  1541. if (IS_8x8DCT(mb_type)) {
  1542. if (transform_bypass) {
  1543. idct_dc_add =
  1544. idct_add = s->dsp.add_pixels8;
  1545. } else {
  1546. idct_dc_add = h->h264dsp.h264_idct8_dc_add;
  1547. idct_add = h->h264dsp.h264_idct8_add;
  1548. }
  1549. for (i = 0; i < 16; i += 4) {
  1550. uint8_t *const ptr = dest_y + block_offset[i];
  1551. const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
  1552. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  1553. h->hpc.pred8x8l_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1554. } else {
  1555. const int nnz = h->non_zero_count_cache[scan8[i + p * 16]];
  1556. h->hpc.pred8x8l[dir](ptr, (h->topleft_samples_available << i) & 0x8000,
  1557. (h->topright_samples_available << i) & 0x4000, linesize);
  1558. if (nnz) {
  1559. if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  1560. idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1561. else
  1562. idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1563. }
  1564. }
  1565. }
  1566. } else {
  1567. if (transform_bypass) {
  1568. idct_dc_add =
  1569. idct_add = s->dsp.add_pixels4;
  1570. } else {
  1571. idct_dc_add = h->h264dsp.h264_idct_dc_add;
  1572. idct_add = h->h264dsp.h264_idct_add;
  1573. }
  1574. for (i = 0; i < 16; i++) {
  1575. uint8_t *const ptr = dest_y + block_offset[i];
  1576. const int dir = h->intra4x4_pred_mode_cache[scan8[i]];
  1577. if (transform_bypass && h->sps.profile_idc == 244 && dir <= 1) {
  1578. h->hpc.pred4x4_add[dir](ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1579. } else {
  1580. uint8_t *topright;
  1581. int nnz, tr;
  1582. uint64_t tr_high;
  1583. if (dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED) {
  1584. const int topright_avail = (h->topright_samples_available << i) & 0x8000;
  1585. assert(s->mb_y || linesize <= block_offset[i]);
  1586. if (!topright_avail) {
  1587. if (pixel_shift) {
  1588. tr_high = ((uint16_t *)ptr)[3 - linesize / 2] * 0x0001000100010001ULL;
  1589. topright = (uint8_t *)&tr_high;
  1590. } else {
  1591. tr = ptr[3 - linesize] * 0x01010101u;
  1592. topright = (uint8_t *)&tr;
  1593. }
  1594. } else
  1595. topright = ptr + (4 << pixel_shift) - linesize;
  1596. } else
  1597. topright = NULL;
  1598. h->hpc.pred4x4[dir](ptr, topright, linesize);
  1599. nnz = h->non_zero_count_cache[scan8[i + p * 16]];
  1600. if (nnz) {
  1601. if (is_h264) {
  1602. if (nnz == 1 && dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  1603. idct_dc_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1604. else
  1605. idct_add(ptr, h->mb + (i * 16 + p * 256 << pixel_shift), linesize);
  1606. } else if (CONFIG_SVQ3_DECODER)
  1607. ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize, qscale, 0);
  1608. }
  1609. }
  1610. }
  1611. }
  1612. }
  1613. } else {
  1614. h->hpc.pred16x16[h->intra16x16_pred_mode](dest_y, linesize);
  1615. if (is_h264) {
  1616. if (h->non_zero_count_cache[scan8[LUMA_DC_BLOCK_INDEX + p]]) {
  1617. if (!transform_bypass)
  1618. h->h264dsp.h264_luma_dc_dequant_idct(h->mb + (p * 256 << pixel_shift),
  1619. h->mb_luma_dc[p],
  1620. h->dequant4_coeff[p][qscale][0]);
  1621. else {
  1622. static const uint8_t dc_mapping[16] = {
  1623. 0 * 16, 1 * 16, 4 * 16, 5 * 16,
  1624. 2 * 16, 3 * 16, 6 * 16, 7 * 16,
  1625. 8 * 16, 9 * 16, 12 * 16, 13 * 16,
  1626. 10 * 16, 11 * 16, 14 * 16, 15 * 16 };
  1627. for (i = 0; i < 16; i++)
  1628. dctcoef_set(h->mb + (p * 256 << pixel_shift),
  1629. pixel_shift, dc_mapping[i],
  1630. dctcoef_get(h->mb_luma_dc[p],
  1631. pixel_shift, i));
  1632. }
  1633. }
  1634. } else if (CONFIG_SVQ3_DECODER)
  1635. ff_svq3_luma_dc_dequant_idct_c(h->mb + p * 256,
  1636. h->mb_luma_dc[p], qscale);
  1637. }
  1638. }
  1639. static av_always_inline void hl_decode_mb_idct_luma(H264Context *h, int mb_type,
  1640. int is_h264, int simple,
  1641. int transform_bypass,
  1642. int pixel_shift,
  1643. int *block_offset,
  1644. int linesize,
  1645. uint8_t *dest_y, int p)
  1646. {
  1647. MpegEncContext *const s = &h->s;
  1648. void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
  1649. int i;
  1650. block_offset += 16 * p;
  1651. if (!IS_INTRA4x4(mb_type)) {
  1652. if (is_h264) {
  1653. if (IS_INTRA16x16(mb_type)) {
  1654. if (transform_bypass) {
  1655. if (h->sps.profile_idc == 244 &&
  1656. (h->intra16x16_pred_mode == VERT_PRED8x8 ||
  1657. h->intra16x16_pred_mode == HOR_PRED8x8)) {
  1658. h->hpc.pred16x16_add[h->intra16x16_pred_mode](dest_y, block_offset,
  1659. h->mb + (p * 256 << pixel_shift),
  1660. linesize);
  1661. } else {
  1662. for (i = 0; i < 16; i++)
  1663. if (h->non_zero_count_cache[scan8[i + p * 16]] ||
  1664. dctcoef_get(h->mb, pixel_shift, i * 16 + p * 256))
  1665. s->dsp.add_pixels4(dest_y + block_offset[i],
  1666. h->mb + (i * 16 + p * 256 << pixel_shift),
  1667. linesize);
  1668. }
  1669. } else {
  1670. h->h264dsp.h264_idct_add16intra(dest_y, block_offset,
  1671. h->mb + (p * 256 << pixel_shift),
  1672. linesize,
  1673. h->non_zero_count_cache + p * 5 * 8);
  1674. }
  1675. } else if (h->cbp & 15) {
  1676. if (transform_bypass) {
  1677. const int di = IS_8x8DCT(mb_type) ? 4 : 1;
  1678. idct_add = IS_8x8DCT(mb_type) ? s->dsp.add_pixels8
  1679. : s->dsp.add_pixels4;
  1680. for (i = 0; i < 16; i += di)
  1681. if (h->non_zero_count_cache[scan8[i + p * 16]])
  1682. idct_add(dest_y + block_offset[i],
  1683. h->mb + (i * 16 + p * 256 << pixel_shift),
  1684. linesize);
  1685. } else {
  1686. if (IS_8x8DCT(mb_type))
  1687. h->h264dsp.h264_idct8_add4(dest_y, block_offset,
  1688. h->mb + (p * 256 << pixel_shift),
  1689. linesize,
  1690. h->non_zero_count_cache + p * 5 * 8);
  1691. else
  1692. h->h264dsp.h264_idct_add16(dest_y, block_offset,
  1693. h->mb + (p * 256 << pixel_shift),
  1694. linesize,
  1695. h->non_zero_count_cache + p * 5 * 8);
  1696. }
  1697. }
  1698. } else if (CONFIG_SVQ3_DECODER) {
  1699. for (i = 0; i < 16; i++)
  1700. if (h->non_zero_count_cache[scan8[i + p * 16]] || h->mb[i * 16 + p * 256]) {
  1701. // FIXME benchmark weird rule, & below
  1702. uint8_t *const ptr = dest_y + block_offset[i];
  1703. ff_svq3_add_idct_c(ptr, h->mb + i * 16 + p * 256, linesize,
  1704. s->qscale, IS_INTRA(mb_type) ? 1 : 0);
  1705. }
  1706. }
  1707. }
  1708. }
  1709. #define BITS 8
  1710. #define SIMPLE 1
  1711. #include "h264_mb_template.c"
  1712. #undef BITS
  1713. #define BITS 16
  1714. #include "h264_mb_template.c"
  1715. #undef SIMPLE
  1716. #define SIMPLE 0
  1717. #include "h264_mb_template.c"
  1718. void ff_h264_hl_decode_mb(H264Context *h)
  1719. {
  1720. MpegEncContext *const s = &h->s;
  1721. const int mb_xy = h->mb_xy;
  1722. const int mb_type = s->current_picture.f.mb_type[mb_xy];
  1723. int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
  1724. if (CHROMA444) {
  1725. if (is_complex || h->pixel_shift)
  1726. hl_decode_mb_444_complex(h);
  1727. else
  1728. hl_decode_mb_444_simple_8(h);
  1729. } else if (is_complex) {
  1730. hl_decode_mb_complex(h);
  1731. } else if (h->pixel_shift) {
  1732. hl_decode_mb_simple_16(h);
  1733. } else
  1734. hl_decode_mb_simple_8(h);
  1735. }
  1736. static int pred_weight_table(H264Context *h)
  1737. {
  1738. MpegEncContext *const s = &h->s;
  1739. int list, i;
  1740. int luma_def, chroma_def;
  1741. h->use_weight = 0;
  1742. h->use_weight_chroma = 0;
  1743. h->luma_log2_weight_denom = get_ue_golomb(&s->gb);
  1744. if (h->sps.chroma_format_idc)
  1745. h->chroma_log2_weight_denom = get_ue_golomb(&s->gb);
  1746. luma_def = 1 << h->luma_log2_weight_denom;
  1747. chroma_def = 1 << h->chroma_log2_weight_denom;
  1748. for (list = 0; list < 2; list++) {
  1749. h->luma_weight_flag[list] = 0;
  1750. h->chroma_weight_flag[list] = 0;
  1751. for (i = 0; i < h->ref_count[list]; i++) {
  1752. int luma_weight_flag, chroma_weight_flag;
  1753. luma_weight_flag = get_bits1(&s->gb);
  1754. if (luma_weight_flag) {
  1755. h->luma_weight[i][list][0] = get_se_golomb(&s->gb);
  1756. h->luma_weight[i][list][1] = get_se_golomb(&s->gb);
  1757. if (h->luma_weight[i][list][0] != luma_def ||
  1758. h->luma_weight[i][list][1] != 0) {
  1759. h->use_weight = 1;
  1760. h->luma_weight_flag[list] = 1;
  1761. }
  1762. } else {
  1763. h->luma_weight[i][list][0] = luma_def;
  1764. h->luma_weight[i][list][1] = 0;
  1765. }
  1766. if (h->sps.chroma_format_idc) {
  1767. chroma_weight_flag = get_bits1(&s->gb);
  1768. if (chroma_weight_flag) {
  1769. int j;
  1770. for (j = 0; j < 2; j++) {
  1771. h->chroma_weight[i][list][j][0] = get_se_golomb(&s->gb);
  1772. h->chroma_weight[i][list][j][1] = get_se_golomb(&s->gb);
  1773. if (h->chroma_weight[i][list][j][0] != chroma_def ||
  1774. h->chroma_weight[i][list][j][1] != 0) {
  1775. h->use_weight_chroma = 1;
  1776. h->chroma_weight_flag[list] = 1;
  1777. }
  1778. }
  1779. } else {
  1780. int j;
  1781. for (j = 0; j < 2; j++) {
  1782. h->chroma_weight[i][list][j][0] = chroma_def;
  1783. h->chroma_weight[i][list][j][1] = 0;
  1784. }
  1785. }
  1786. }
  1787. }
  1788. if (h->slice_type_nos != AV_PICTURE_TYPE_B)
  1789. break;
  1790. }
  1791. h->use_weight = h->use_weight || h->use_weight_chroma;
  1792. return 0;
  1793. }
  1794. /**
  1795. * Initialize implicit_weight table.
  1796. * @param field 0/1 initialize the weight for interlaced MBAFF
  1797. * -1 initializes the rest
  1798. */
  1799. static void implicit_weight_table(H264Context *h, int field)
  1800. {
  1801. MpegEncContext *const s = &h->s;
  1802. int ref0, ref1, i, cur_poc, ref_start, ref_count0, ref_count1;
  1803. for (i = 0; i < 2; i++) {
  1804. h->luma_weight_flag[i] = 0;
  1805. h->chroma_weight_flag[i] = 0;
  1806. }
  1807. if (field < 0) {
  1808. if (s->picture_structure == PICT_FRAME) {
  1809. cur_poc = s->current_picture_ptr->poc;
  1810. } else {
  1811. cur_poc = s->current_picture_ptr->field_poc[s->picture_structure - 1];
  1812. }
  1813. if (h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF &&
  1814. h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2 * cur_poc) {
  1815. h->use_weight = 0;
  1816. h->use_weight_chroma = 0;
  1817. return;
  1818. }
  1819. ref_start = 0;
  1820. ref_count0 = h->ref_count[0];
  1821. ref_count1 = h->ref_count[1];
  1822. } else {
  1823. cur_poc = s->current_picture_ptr->field_poc[field];
  1824. ref_start = 16;
  1825. ref_count0 = 16 + 2 * h->ref_count[0];
  1826. ref_count1 = 16 + 2 * h->ref_count[1];
  1827. }
  1828. h->use_weight = 2;
  1829. h->use_weight_chroma = 2;
  1830. h->luma_log2_weight_denom = 5;
  1831. h->chroma_log2_weight_denom = 5;
  1832. for (ref0 = ref_start; ref0 < ref_count0; ref0++) {
  1833. int poc0 = h->ref_list[0][ref0].poc;
  1834. for (ref1 = ref_start; ref1 < ref_count1; ref1++) {
  1835. int w = 32;
  1836. if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
  1837. int poc1 = h->ref_list[1][ref1].poc;
  1838. int td = av_clip(poc1 - poc0, -128, 127);
  1839. if (td) {
  1840. int tb = av_clip(cur_poc - poc0, -128, 127);
  1841. int tx = (16384 + (FFABS(td) >> 1)) / td;
  1842. int dist_scale_factor = (tb * tx + 32) >> 8;
  1843. if (dist_scale_factor >= -64 && dist_scale_factor <= 128)
  1844. w = 64 - dist_scale_factor;
  1845. }
  1846. }
  1847. if (field < 0) {
  1848. h->implicit_weight[ref0][ref1][0] =
  1849. h->implicit_weight[ref0][ref1][1] = w;
  1850. } else {
  1851. h->implicit_weight[ref0][ref1][field] = w;
  1852. }
  1853. }
  1854. }
  1855. }
  1856. /**
  1857. * instantaneous decoder refresh.
  1858. */
  1859. static void idr(H264Context *h)
  1860. {
  1861. int i;
  1862. ff_h264_remove_all_refs(h);
  1863. h->prev_frame_num = 0;
  1864. h->prev_frame_num_offset = 0;
  1865. h->prev_poc_msb = 1<<16;
  1866. h->prev_poc_lsb = 0;
  1867. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  1868. h->last_pocs[i] = INT_MIN;
  1869. }
  1870. /* forget old pics after a seek */
  1871. static void flush_dpb(AVCodecContext *avctx)
  1872. {
  1873. H264Context *h = avctx->priv_data;
  1874. int i;
  1875. for (i=0; i<=MAX_DELAYED_PIC_COUNT; i++) {
  1876. if (h->delayed_pic[i])
  1877. h->delayed_pic[i]->f.reference = 0;
  1878. h->delayed_pic[i] = NULL;
  1879. }
  1880. h->outputed_poc = h->next_outputed_poc = INT_MIN;
  1881. h->prev_interlaced_frame = 1;
  1882. idr(h);
  1883. h->prev_frame_num = -1;
  1884. if (h->s.current_picture_ptr)
  1885. h->s.current_picture_ptr->f.reference = 0;
  1886. h->s.first_field = 0;
  1887. ff_h264_reset_sei(h);
  1888. ff_mpeg_flush(avctx);
  1889. h->recovery_frame= -1;
  1890. h->sync= 0;
  1891. }
  1892. static int init_poc(H264Context *h)
  1893. {
  1894. MpegEncContext *const s = &h->s;
  1895. const int max_frame_num = 1 << h->sps.log2_max_frame_num;
  1896. int field_poc[2];
  1897. Picture *cur = s->current_picture_ptr;
  1898. h->frame_num_offset = h->prev_frame_num_offset;
  1899. if (h->frame_num < h->prev_frame_num)
  1900. h->frame_num_offset += max_frame_num;
  1901. if (h->sps.poc_type == 0) {
  1902. const int max_poc_lsb = 1 << h->sps.log2_max_poc_lsb;
  1903. if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb / 2)
  1904. h->poc_msb = h->prev_poc_msb + max_poc_lsb;
  1905. else if (h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb / 2)
  1906. h->poc_msb = h->prev_poc_msb - max_poc_lsb;
  1907. else
  1908. h->poc_msb = h->prev_poc_msb;
  1909. field_poc[0] =
  1910. field_poc[1] = h->poc_msb + h->poc_lsb;
  1911. if (s->picture_structure == PICT_FRAME)
  1912. field_poc[1] += h->delta_poc_bottom;
  1913. } else if (h->sps.poc_type == 1) {
  1914. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  1915. int i;
  1916. if (h->sps.poc_cycle_length != 0)
  1917. abs_frame_num = h->frame_num_offset + h->frame_num;
  1918. else
  1919. abs_frame_num = 0;
  1920. if (h->nal_ref_idc == 0 && abs_frame_num > 0)
  1921. abs_frame_num--;
  1922. expected_delta_per_poc_cycle = 0;
  1923. for (i = 0; i < h->sps.poc_cycle_length; i++)
  1924. // FIXME integrate during sps parse
  1925. expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[i];
  1926. if (abs_frame_num > 0) {
  1927. int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
  1928. int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
  1929. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  1930. for (i = 0; i <= frame_num_in_poc_cycle; i++)
  1931. expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[i];
  1932. } else
  1933. expectedpoc = 0;
  1934. if (h->nal_ref_idc == 0)
  1935. expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
  1936. field_poc[0] = expectedpoc + h->delta_poc[0];
  1937. field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
  1938. if (s->picture_structure == PICT_FRAME)
  1939. field_poc[1] += h->delta_poc[1];
  1940. } else {
  1941. int poc = 2 * (h->frame_num_offset + h->frame_num);
  1942. if (!h->nal_ref_idc)
  1943. poc--;
  1944. field_poc[0] = poc;
  1945. field_poc[1] = poc;
  1946. }
  1947. if (s->picture_structure != PICT_BOTTOM_FIELD)
  1948. s->current_picture_ptr->field_poc[0] = field_poc[0];
  1949. if (s->picture_structure != PICT_TOP_FIELD)
  1950. s->current_picture_ptr->field_poc[1] = field_poc[1];
  1951. cur->poc = FFMIN(cur->field_poc[0], cur->field_poc[1]);
  1952. return 0;
  1953. }
  1954. /**
  1955. * initialize scan tables
  1956. */
  1957. static void init_scan_tables(H264Context *h)
  1958. {
  1959. int i;
  1960. for (i = 0; i < 16; i++) {
  1961. #define T(x) (x >> 2) | ((x << 2) & 0xF)
  1962. h->zigzag_scan[i] = T(zigzag_scan[i]);
  1963. h->field_scan[i] = T(field_scan[i]);
  1964. #undef T
  1965. }
  1966. for (i = 0; i < 64; i++) {
  1967. #define T(x) (x >> 3) | ((x & 7) << 3)
  1968. h->zigzag_scan8x8[i] = T(ff_zigzag_direct[i]);
  1969. h->zigzag_scan8x8_cavlc[i] = T(zigzag_scan8x8_cavlc[i]);
  1970. h->field_scan8x8[i] = T(field_scan8x8[i]);
  1971. h->field_scan8x8_cavlc[i] = T(field_scan8x8_cavlc[i]);
  1972. #undef T
  1973. }
  1974. if (h->sps.transform_bypass) { // FIXME same ugly
  1975. memcpy(h->zigzag_scan_q0 , zigzag_scan , sizeof(h->zigzag_scan_q0 ));
  1976. memcpy(h->zigzag_scan8x8_q0 , ff_zigzag_direct , sizeof(h->zigzag_scan8x8_q0 ));
  1977. memcpy(h->zigzag_scan8x8_cavlc_q0 , zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
  1978. memcpy(h->field_scan_q0 , field_scan , sizeof(h->field_scan_q0 ));
  1979. memcpy(h->field_scan8x8_q0 , field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
  1980. memcpy(h->field_scan8x8_cavlc_q0 , field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
  1981. } else {
  1982. memcpy(h->zigzag_scan_q0 , h->zigzag_scan , sizeof(h->zigzag_scan_q0 ));
  1983. memcpy(h->zigzag_scan8x8_q0 , h->zigzag_scan8x8 , sizeof(h->zigzag_scan8x8_q0 ));
  1984. memcpy(h->zigzag_scan8x8_cavlc_q0 , h->zigzag_scan8x8_cavlc , sizeof(h->zigzag_scan8x8_cavlc_q0));
  1985. memcpy(h->field_scan_q0 , h->field_scan , sizeof(h->field_scan_q0 ));
  1986. memcpy(h->field_scan8x8_q0 , h->field_scan8x8 , sizeof(h->field_scan8x8_q0 ));
  1987. memcpy(h->field_scan8x8_cavlc_q0 , h->field_scan8x8_cavlc , sizeof(h->field_scan8x8_cavlc_q0 ));
  1988. }
  1989. }
  1990. static int field_end(H264Context *h, int in_setup)
  1991. {
  1992. MpegEncContext *const s = &h->s;
  1993. AVCodecContext *const avctx = s->avctx;
  1994. int err = 0;
  1995. s->mb_y = 0;
  1996. if (!in_setup && !s->dropable)
  1997. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX,
  1998. s->picture_structure == PICT_BOTTOM_FIELD);
  1999. if (CONFIG_H264_VDPAU_DECODER &&
  2000. s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  2001. ff_vdpau_h264_set_reference_frames(s);
  2002. if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  2003. if (!s->dropable) {
  2004. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  2005. h->prev_poc_msb = h->poc_msb;
  2006. h->prev_poc_lsb = h->poc_lsb;
  2007. }
  2008. h->prev_frame_num_offset = h->frame_num_offset;
  2009. h->prev_frame_num = h->frame_num;
  2010. h->outputed_poc = h->next_outputed_poc;
  2011. }
  2012. if (avctx->hwaccel) {
  2013. if (avctx->hwaccel->end_frame(avctx) < 0)
  2014. av_log(avctx, AV_LOG_ERROR,
  2015. "hardware accelerator failed to decode picture\n");
  2016. }
  2017. if (CONFIG_H264_VDPAU_DECODER &&
  2018. s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
  2019. ff_vdpau_h264_picture_complete(s);
  2020. /*
  2021. * FIXME: Error handling code does not seem to support interlaced
  2022. * when slices span multiple rows
  2023. * The ff_er_add_slice calls don't work right for bottom
  2024. * fields; they cause massive erroneous error concealing
  2025. * Error marking covers both fields (top and bottom).
  2026. * This causes a mismatched s->error_count
  2027. * and a bad error table. Further, the error count goes to
  2028. * INT_MAX when called for bottom field, because mb_y is
  2029. * past end by one (callers fault) and resync_mb_y != 0
  2030. * causes problems for the first MB line, too.
  2031. */
  2032. if (!FIELD_PICTURE)
  2033. ff_er_frame_end(s);
  2034. ff_MPV_frame_end(s);
  2035. h->current_slice = 0;
  2036. return err;
  2037. }
  2038. /**
  2039. * Replicate H264 "master" context to thread contexts.
  2040. */
  2041. static void clone_slice(H264Context *dst, H264Context *src)
  2042. {
  2043. memcpy(dst->block_offset, src->block_offset, sizeof(dst->block_offset));
  2044. dst->s.current_picture_ptr = src->s.current_picture_ptr;
  2045. dst->s.current_picture = src->s.current_picture;
  2046. dst->s.linesize = src->s.linesize;
  2047. dst->s.uvlinesize = src->s.uvlinesize;
  2048. dst->s.first_field = src->s.first_field;
  2049. dst->prev_poc_msb = src->prev_poc_msb;
  2050. dst->prev_poc_lsb = src->prev_poc_lsb;
  2051. dst->prev_frame_num_offset = src->prev_frame_num_offset;
  2052. dst->prev_frame_num = src->prev_frame_num;
  2053. dst->short_ref_count = src->short_ref_count;
  2054. memcpy(dst->short_ref, src->short_ref, sizeof(dst->short_ref));
  2055. memcpy(dst->long_ref, src->long_ref, sizeof(dst->long_ref));
  2056. memcpy(dst->default_ref_list, src->default_ref_list, sizeof(dst->default_ref_list));
  2057. memcpy(dst->ref_list, src->ref_list, sizeof(dst->ref_list));
  2058. memcpy(dst->dequant4_coeff, src->dequant4_coeff, sizeof(src->dequant4_coeff));
  2059. memcpy(dst->dequant8_coeff, src->dequant8_coeff, sizeof(src->dequant8_coeff));
  2060. }
  2061. /**
  2062. * Compute profile from profile_idc and constraint_set?_flags.
  2063. *
  2064. * @param sps SPS
  2065. *
  2066. * @return profile as defined by FF_PROFILE_H264_*
  2067. */
  2068. int ff_h264_get_profile(SPS *sps)
  2069. {
  2070. int profile = sps->profile_idc;
  2071. switch (sps->profile_idc) {
  2072. case FF_PROFILE_H264_BASELINE:
  2073. // constraint_set1_flag set to 1
  2074. profile |= (sps->constraint_set_flags & 1 << 1) ? FF_PROFILE_H264_CONSTRAINED : 0;
  2075. break;
  2076. case FF_PROFILE_H264_HIGH_10:
  2077. case FF_PROFILE_H264_HIGH_422:
  2078. case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
  2079. // constraint_set3_flag set to 1
  2080. profile |= (sps->constraint_set_flags & 1 << 3) ? FF_PROFILE_H264_INTRA : 0;
  2081. break;
  2082. }
  2083. return profile;
  2084. }
  2085. /**
  2086. * Decode a slice header.
  2087. * This will also call ff_MPV_common_init() and frame_start() as needed.
  2088. *
  2089. * @param h h264context
  2090. * @param h0 h264 master context (differs from 'h' when doing sliced based
  2091. * parallel decoding)
  2092. *
  2093. * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
  2094. */
  2095. static int decode_slice_header(H264Context *h, H264Context *h0)
  2096. {
  2097. MpegEncContext *const s = &h->s;
  2098. MpegEncContext *const s0 = &h0->s;
  2099. unsigned int first_mb_in_slice;
  2100. unsigned int pps_id;
  2101. int num_ref_idx_active_override_flag;
  2102. unsigned int slice_type, tmp, i, j;
  2103. int default_ref_list_done = 0;
  2104. int last_pic_structure, last_pic_dropable;
  2105. int must_reinit;
  2106. /* FIXME: 2tap qpel isn't implemented for high bit depth. */
  2107. if ((s->avctx->flags2 & CODEC_FLAG2_FAST) &&
  2108. !h->nal_ref_idc && !h->pixel_shift) {
  2109. s->me.qpel_put = s->dsp.put_2tap_qpel_pixels_tab;
  2110. s->me.qpel_avg = s->dsp.avg_2tap_qpel_pixels_tab;
  2111. } else {
  2112. s->me.qpel_put = s->dsp.put_h264_qpel_pixels_tab;
  2113. s->me.qpel_avg = s->dsp.avg_h264_qpel_pixels_tab;
  2114. }
  2115. first_mb_in_slice = get_ue_golomb_long(&s->gb);
  2116. if (first_mb_in_slice == 0) { // FIXME better field boundary detection
  2117. if (h0->current_slice && FIELD_PICTURE) {
  2118. field_end(h, 1);
  2119. }
  2120. h0->current_slice = 0;
  2121. if (!s0->first_field) {
  2122. if (s->current_picture_ptr && !s->dropable &&
  2123. s->current_picture_ptr->owner2 == s) {
  2124. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX,
  2125. s->picture_structure == PICT_BOTTOM_FIELD);
  2126. }
  2127. s->current_picture_ptr = NULL;
  2128. }
  2129. }
  2130. slice_type = get_ue_golomb_31(&s->gb);
  2131. if (slice_type > 9) {
  2132. av_log(h->s.avctx, AV_LOG_ERROR,
  2133. "slice type too large (%d) at %d %d\n",
  2134. h->slice_type, s->mb_x, s->mb_y);
  2135. return -1;
  2136. }
  2137. if (slice_type > 4) {
  2138. slice_type -= 5;
  2139. h->slice_type_fixed = 1;
  2140. } else
  2141. h->slice_type_fixed = 0;
  2142. slice_type = golomb_to_pict_type[slice_type];
  2143. if (slice_type == AV_PICTURE_TYPE_I ||
  2144. (h0->current_slice != 0 && slice_type == h0->last_slice_type)) {
  2145. default_ref_list_done = 1;
  2146. }
  2147. h->slice_type = slice_type;
  2148. h->slice_type_nos = slice_type & 3;
  2149. // to make a few old functions happy, it's wrong though
  2150. s->pict_type = h->slice_type;
  2151. pps_id = get_ue_golomb(&s->gb);
  2152. if (pps_id >= MAX_PPS_COUNT) {
  2153. av_log(h->s.avctx, AV_LOG_ERROR, "pps_id %d out of range\n", pps_id);
  2154. return -1;
  2155. }
  2156. if (!h0->pps_buffers[pps_id]) {
  2157. av_log(h->s.avctx, AV_LOG_ERROR,
  2158. "non-existing PPS %u referenced\n",
  2159. pps_id);
  2160. return -1;
  2161. }
  2162. h->pps = *h0->pps_buffers[pps_id];
  2163. if (!h0->sps_buffers[h->pps.sps_id]) {
  2164. av_log(h->s.avctx, AV_LOG_ERROR,
  2165. "non-existing SPS %u referenced\n",
  2166. h->pps.sps_id);
  2167. return -1;
  2168. }
  2169. h->sps = *h0->sps_buffers[h->pps.sps_id];
  2170. s->avctx->profile = ff_h264_get_profile(&h->sps);
  2171. s->avctx->level = h->sps.level_idc;
  2172. s->avctx->refs = h->sps.ref_frame_count;
  2173. must_reinit = (s->context_initialized &&
  2174. ( 16*h->sps.mb_width != s->avctx->coded_width
  2175. || 16*h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag) != s->avctx->coded_height
  2176. || s->avctx->bits_per_raw_sample != h->sps.bit_depth_luma
  2177. || h->cur_chroma_format_idc != h->sps.chroma_format_idc
  2178. || av_cmp_q(h->sps.sar, s->avctx->sample_aspect_ratio)));
  2179. if(must_reinit && (h != h0 || (s->avctx->active_thread_type & FF_THREAD_FRAME))) {
  2180. av_log_missing_feature(s->avctx,
  2181. "Width/height/bit depth/chroma idc changing with threads is", 0);
  2182. return AVERROR_PATCHWELCOME; // width / height changed during parallelized decoding
  2183. }
  2184. s->mb_width = h->sps.mb_width;
  2185. s->mb_height = h->sps.mb_height * (2 - h->sps.frame_mbs_only_flag);
  2186. h->b_stride = s->mb_width * 4;
  2187. s->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
  2188. s->width = 16 * s->mb_width;
  2189. s->height = 16 * s->mb_height;
  2190. if(must_reinit) {
  2191. free_tables(h, 0);
  2192. flush_dpb(s->avctx);
  2193. ff_MPV_common_end(s);
  2194. h->list_count = 0;
  2195. h->current_slice = 0;
  2196. }
  2197. if (!s->context_initialized) {
  2198. if (h != h0) {
  2199. av_log(h->s.avctx, AV_LOG_ERROR,
  2200. "Cannot (re-)initialize context during parallel decoding.\n");
  2201. return -1;
  2202. }
  2203. if( FFALIGN(s->avctx->width , 16 ) == s->width
  2204. && FFALIGN(s->avctx->height, 16*(2 - h->sps.frame_mbs_only_flag)) == s->height
  2205. && !h->sps.crop_right && !h->sps.crop_bottom
  2206. && (s->avctx->width != s->width || s->avctx->height && s->height)
  2207. ) {
  2208. av_log(h->s.avctx, AV_LOG_DEBUG, "Using externally provided dimensions\n");
  2209. s->avctx->coded_width = s->width;
  2210. s->avctx->coded_height = s->height;
  2211. } else{
  2212. avcodec_set_dimensions(s->avctx, s->width, s->height);
  2213. s->avctx->width -= (2>>CHROMA444)*FFMIN(h->sps.crop_right, (8<<CHROMA444)-1);
  2214. 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);
  2215. }
  2216. s->avctx->sample_aspect_ratio = h->sps.sar;
  2217. av_assert0(s->avctx->sample_aspect_ratio.den);
  2218. if (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU
  2219. && (h->sps.bit_depth_luma != 8 ||
  2220. h->sps.chroma_format_idc > 1)) {
  2221. av_log(s->avctx, AV_LOG_ERROR,
  2222. "VDPAU decoding does not support video "
  2223. "colorspace\n");
  2224. return -1;
  2225. }
  2226. if (s->avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
  2227. h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
  2228. 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 &&
  2229. (h->sps.bit_depth_luma != 9 || !CHROMA422)) {
  2230. s->avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
  2231. h->cur_chroma_format_idc = h->sps.chroma_format_idc;
  2232. h->pixel_shift = h->sps.bit_depth_luma > 8;
  2233. ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
  2234. ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
  2235. s->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;
  2236. ff_dsputil_init(&s->dsp, s->avctx);
  2237. } else {
  2238. av_log(s->avctx, AV_LOG_ERROR, "Unsupported bit depth: %d chroma_idc: %d\n",
  2239. h->sps.bit_depth_luma, h->sps.chroma_format_idc);
  2240. return -1;
  2241. }
  2242. }
  2243. if (h->sps.video_signal_type_present_flag) {
  2244. s->avctx->color_range = h->sps.full_range>0 ? AVCOL_RANGE_JPEG
  2245. : AVCOL_RANGE_MPEG;
  2246. if (h->sps.colour_description_present_flag) {
  2247. s->avctx->color_primaries = h->sps.color_primaries;
  2248. s->avctx->color_trc = h->sps.color_trc;
  2249. s->avctx->colorspace = h->sps.colorspace;
  2250. }
  2251. }
  2252. if (h->sps.timing_info_present_flag) {
  2253. int64_t den = h->sps.time_scale;
  2254. if (h->x264_build < 44U)
  2255. den *= 2;
  2256. av_reduce(&s->avctx->time_base.num, &s->avctx->time_base.den,
  2257. h->sps.num_units_in_tick, den, 1 << 30);
  2258. }
  2259. switch (h->sps.bit_depth_luma) {
  2260. case 9:
  2261. if (CHROMA444) {
  2262. if (s->avctx->colorspace == AVCOL_SPC_RGB) {
  2263. s->avctx->pix_fmt = PIX_FMT_GBRP9;
  2264. } else
  2265. s->avctx->pix_fmt = PIX_FMT_YUV444P9;
  2266. } else if (CHROMA422)
  2267. s->avctx->pix_fmt = PIX_FMT_YUV422P9;
  2268. else
  2269. s->avctx->pix_fmt = PIX_FMT_YUV420P9;
  2270. break;
  2271. case 10:
  2272. if (CHROMA444) {
  2273. if (s->avctx->colorspace == AVCOL_SPC_RGB) {
  2274. s->avctx->pix_fmt = PIX_FMT_GBRP10;
  2275. } else
  2276. s->avctx->pix_fmt = PIX_FMT_YUV444P10;
  2277. } else if (CHROMA422)
  2278. s->avctx->pix_fmt = PIX_FMT_YUV422P10;
  2279. else
  2280. s->avctx->pix_fmt = PIX_FMT_YUV420P10;
  2281. break;
  2282. case 12:
  2283. if (CHROMA444) {
  2284. if (s->avctx->colorspace == AVCOL_SPC_RGB) {
  2285. s->avctx->pix_fmt = PIX_FMT_GBRP12;
  2286. } else
  2287. s->avctx->pix_fmt = PIX_FMT_YUV444P12;
  2288. } else if (CHROMA422)
  2289. s->avctx->pix_fmt = PIX_FMT_YUV422P12;
  2290. else
  2291. s->avctx->pix_fmt = PIX_FMT_YUV420P12;
  2292. break;
  2293. case 14:
  2294. if (CHROMA444) {
  2295. if (s->avctx->colorspace == AVCOL_SPC_RGB) {
  2296. s->avctx->pix_fmt = PIX_FMT_GBRP14;
  2297. } else
  2298. s->avctx->pix_fmt = PIX_FMT_YUV444P14;
  2299. } else if (CHROMA422)
  2300. s->avctx->pix_fmt = PIX_FMT_YUV422P14;
  2301. else
  2302. s->avctx->pix_fmt = PIX_FMT_YUV420P14;
  2303. break;
  2304. case 8:
  2305. if (CHROMA444) {
  2306. s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ444P
  2307. : PIX_FMT_YUV444P;
  2308. if (s->avctx->colorspace == AVCOL_SPC_RGB) {
  2309. s->avctx->pix_fmt = PIX_FMT_GBR24P;
  2310. av_log(h->s.avctx, AV_LOG_DEBUG, "Detected GBR colorspace.\n");
  2311. } else if (s->avctx->colorspace == AVCOL_SPC_YCGCO) {
  2312. av_log(h->s.avctx, AV_LOG_WARNING, "Detected unsupported YCgCo colorspace.\n");
  2313. }
  2314. } else if (CHROMA422) {
  2315. s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ422P
  2316. : PIX_FMT_YUV422P;
  2317. } else {
  2318. s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
  2319. s->avctx->codec->pix_fmts ?
  2320. s->avctx->codec->pix_fmts :
  2321. s->avctx->color_range == AVCOL_RANGE_JPEG ?
  2322. hwaccel_pixfmt_list_h264_jpeg_420 :
  2323. ff_hwaccel_pixfmt_list_420);
  2324. }
  2325. break;
  2326. default:
  2327. av_log(s->avctx, AV_LOG_ERROR,
  2328. "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
  2329. return AVERROR_INVALIDDATA;
  2330. }
  2331. s->avctx->hwaccel = ff_find_hwaccel(s->avctx->codec->id,
  2332. s->avctx->pix_fmt);
  2333. if (ff_MPV_common_init(s) < 0) {
  2334. av_log(h->s.avctx, AV_LOG_ERROR, "ff_MPV_common_init() failed.\n");
  2335. return -1;
  2336. }
  2337. s->first_field = 0;
  2338. h->prev_interlaced_frame = 1;
  2339. init_scan_tables(h);
  2340. if (ff_h264_alloc_tables(h) < 0) {
  2341. av_log(h->s.avctx, AV_LOG_ERROR,
  2342. "Could not allocate memory for h264\n");
  2343. return AVERROR(ENOMEM);
  2344. }
  2345. if (!HAVE_THREADS || !(s->avctx->active_thread_type & FF_THREAD_SLICE)) {
  2346. if (context_init(h) < 0) {
  2347. av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
  2348. return -1;
  2349. }
  2350. } else {
  2351. for (i = 1; i < s->slice_context_count; i++) {
  2352. H264Context *c;
  2353. c = h->thread_context[i] = av_malloc(sizeof(H264Context));
  2354. memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
  2355. memset(&c->s + 1, 0, sizeof(H264Context) - sizeof(MpegEncContext));
  2356. c->h264dsp = h->h264dsp;
  2357. c->sps = h->sps;
  2358. c->pps = h->pps;
  2359. c->pixel_shift = h->pixel_shift;
  2360. c->cur_chroma_format_idc = h->cur_chroma_format_idc;
  2361. init_scan_tables(c);
  2362. clone_tables(c, h, i);
  2363. }
  2364. for (i = 0; i < s->slice_context_count; i++)
  2365. if (context_init(h->thread_context[i]) < 0) {
  2366. av_log(h->s.avctx, AV_LOG_ERROR,
  2367. "context_init() failed.\n");
  2368. return -1;
  2369. }
  2370. }
  2371. }
  2372. if (h == h0 && h->dequant_coeff_pps != pps_id) {
  2373. h->dequant_coeff_pps = pps_id;
  2374. init_dequant_tables(h);
  2375. }
  2376. h->frame_num = get_bits(&s->gb, h->sps.log2_max_frame_num);
  2377. h->mb_mbaff = 0;
  2378. h->mb_aff_frame = 0;
  2379. last_pic_structure = s0->picture_structure;
  2380. last_pic_dropable = s->dropable;
  2381. s->dropable = h->nal_ref_idc == 0;
  2382. if (h->sps.frame_mbs_only_flag) {
  2383. s->picture_structure = PICT_FRAME;
  2384. } else {
  2385. if (!h->sps.direct_8x8_inference_flag && slice_type == AV_PICTURE_TYPE_B) {
  2386. av_log(h->s.avctx, AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
  2387. return -1;
  2388. }
  2389. if (get_bits1(&s->gb)) { // field_pic_flag
  2390. s->picture_structure = PICT_TOP_FIELD + get_bits1(&s->gb); // bottom_field_flag
  2391. } else {
  2392. s->picture_structure = PICT_FRAME;
  2393. h->mb_aff_frame = h->sps.mb_aff;
  2394. }
  2395. }
  2396. h->mb_field_decoding_flag = s->picture_structure != PICT_FRAME;
  2397. if (h0->current_slice != 0) {
  2398. if (last_pic_structure != s->picture_structure ||
  2399. last_pic_dropable != s->dropable) {
  2400. av_log(h->s.avctx, AV_LOG_ERROR,
  2401. "Changing field mode (%d -> %d) between slices is not allowed\n",
  2402. last_pic_structure, s->picture_structure);
  2403. s->picture_structure = last_pic_structure;
  2404. s->dropable = last_pic_dropable;
  2405. return AVERROR_INVALIDDATA;
  2406. }
  2407. } else {
  2408. /* Shorten frame num gaps so we don't have to allocate reference
  2409. * frames just to throw them away */
  2410. if (h->frame_num != h->prev_frame_num && h->prev_frame_num >= 0) {
  2411. int unwrap_prev_frame_num = h->prev_frame_num;
  2412. int max_frame_num = 1 << h->sps.log2_max_frame_num;
  2413. if (unwrap_prev_frame_num > h->frame_num)
  2414. unwrap_prev_frame_num -= max_frame_num;
  2415. if ((h->frame_num - unwrap_prev_frame_num) > h->sps.ref_frame_count) {
  2416. unwrap_prev_frame_num = (h->frame_num - h->sps.ref_frame_count) - 1;
  2417. if (unwrap_prev_frame_num < 0)
  2418. unwrap_prev_frame_num += max_frame_num;
  2419. h->prev_frame_num = unwrap_prev_frame_num;
  2420. }
  2421. }
  2422. /* See if we have a decoded first field looking for a pair...
  2423. * Here, we're using that to see if we should mark previously
  2424. * decode frames as "finished".
  2425. * We have to do that before the "dummy" in-between frame allocation,
  2426. * since that can modify s->current_picture_ptr. */
  2427. if (s0->first_field) {
  2428. assert(s0->current_picture_ptr);
  2429. assert(s0->current_picture_ptr->f.data[0]);
  2430. assert(s0->current_picture_ptr->f.reference != DELAYED_PIC_REF);
  2431. /* Mark old field/frame as completed */
  2432. if (!last_pic_dropable && s0->current_picture_ptr->owner2 == s0) {
  2433. ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
  2434. last_pic_structure == PICT_BOTTOM_FIELD);
  2435. }
  2436. /* figure out if we have a complementary field pair */
  2437. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  2438. /* Previous field is unmatched. Don't display it, but let it
  2439. * remain for reference if marked as such. */
  2440. if (!last_pic_dropable && last_pic_structure != PICT_FRAME) {
  2441. ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
  2442. last_pic_structure == PICT_TOP_FIELD);
  2443. }
  2444. } else {
  2445. if (s0->current_picture_ptr->frame_num != h->frame_num) {
  2446. /* This and previous field were reference, but had
  2447. * different frame_nums. Consider this field first in
  2448. * pair. Throw away previous field except for reference
  2449. * purposes. */
  2450. if (!last_pic_dropable && last_pic_structure != PICT_FRAME) {
  2451. ff_thread_report_progress(&s0->current_picture_ptr->f, INT_MAX,
  2452. last_pic_structure == PICT_TOP_FIELD);
  2453. }
  2454. } else {
  2455. /* Second field in complementary pair */
  2456. if (!((last_pic_structure == PICT_TOP_FIELD &&
  2457. s->picture_structure == PICT_BOTTOM_FIELD) ||
  2458. (last_pic_structure == PICT_BOTTOM_FIELD &&
  2459. s->picture_structure == PICT_TOP_FIELD))) {
  2460. av_log(s->avctx, AV_LOG_ERROR,
  2461. "Invalid field mode combination %d/%d\n",
  2462. last_pic_structure, s->picture_structure);
  2463. s->picture_structure = last_pic_structure;
  2464. s->dropable = last_pic_dropable;
  2465. return AVERROR_INVALIDDATA;
  2466. } else if (last_pic_dropable != s->dropable) {
  2467. av_log(s->avctx, AV_LOG_ERROR,
  2468. "Cannot combine reference and non-reference fields in the same frame\n");
  2469. av_log_ask_for_sample(s->avctx, NULL);
  2470. s->picture_structure = last_pic_structure;
  2471. s->dropable = last_pic_dropable;
  2472. return AVERROR_INVALIDDATA;
  2473. }
  2474. /* Take ownership of this buffer. Note that if another thread owned
  2475. * the first field of this buffer, we're not operating on that pointer,
  2476. * so the original thread is still responsible for reporting progress
  2477. * on that first field (or if that was us, we just did that above).
  2478. * By taking ownership, we assign responsibility to ourselves to
  2479. * report progress on the second field. */
  2480. s0->current_picture_ptr->owner2 = s0;
  2481. }
  2482. }
  2483. }
  2484. while (h->frame_num != h->prev_frame_num && h->prev_frame_num >= 0 &&
  2485. h->frame_num != (h->prev_frame_num + 1) % (1 << h->sps.log2_max_frame_num)) {
  2486. Picture *prev = h->short_ref_count ? h->short_ref[0] : NULL;
  2487. av_log(h->s.avctx, AV_LOG_DEBUG, "Frame num gap %d %d\n",
  2488. h->frame_num, h->prev_frame_num);
  2489. if (ff_h264_frame_start(h) < 0)
  2490. return -1;
  2491. h->prev_frame_num++;
  2492. h->prev_frame_num %= 1 << h->sps.log2_max_frame_num;
  2493. s->current_picture_ptr->frame_num = h->prev_frame_num;
  2494. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
  2495. ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 1);
  2496. ff_generate_sliding_window_mmcos(h);
  2497. if (ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index) < 0 &&
  2498. (s->avctx->err_recognition & AV_EF_EXPLODE))
  2499. return AVERROR_INVALIDDATA;
  2500. /* Error concealment: if a ref is missing, copy the previous ref in its place.
  2501. * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
  2502. * about there being no actual duplicates.
  2503. * FIXME: this doesn't copy padding for out-of-frame motion vectors. Given we're
  2504. * concealing a lost frame, this probably isn't noticeable by comparison, but it should
  2505. * be fixed. */
  2506. if (h->short_ref_count) {
  2507. if (prev) {
  2508. av_image_copy(h->short_ref[0]->f.data, h->short_ref[0]->f.linesize,
  2509. (const uint8_t **)prev->f.data, prev->f.linesize,
  2510. s->avctx->pix_fmt, s->mb_width * 16, s->mb_height * 16);
  2511. h->short_ref[0]->poc = prev->poc + 2;
  2512. }
  2513. h->short_ref[0]->frame_num = h->prev_frame_num;
  2514. }
  2515. }
  2516. /* See if we have a decoded first field looking for a pair...
  2517. * We're using that to see whether to continue decoding in that
  2518. * frame, or to allocate a new one. */
  2519. if (s0->first_field) {
  2520. assert(s0->current_picture_ptr);
  2521. assert(s0->current_picture_ptr->f.data[0]);
  2522. assert(s0->current_picture_ptr->f.reference != DELAYED_PIC_REF);
  2523. /* figure out if we have a complementary field pair */
  2524. if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
  2525. /* Previous field is unmatched. Don't display it, but let it
  2526. * remain for reference if marked as such. */
  2527. s0->current_picture_ptr = NULL;
  2528. s0->first_field = FIELD_PICTURE;
  2529. } else {
  2530. if (s0->current_picture_ptr->frame_num != h->frame_num) {
  2531. ff_thread_report_progress((AVFrame*)s0->current_picture_ptr, INT_MAX,
  2532. s0->picture_structure==PICT_BOTTOM_FIELD);
  2533. /* This and the previous field had different frame_nums.
  2534. * Consider this field first in pair. Throw away previous
  2535. * one except for reference purposes. */
  2536. s0->first_field = 1;
  2537. s0->current_picture_ptr = NULL;
  2538. } else {
  2539. /* Second field in complementary pair */
  2540. s0->first_field = 0;
  2541. }
  2542. }
  2543. } else {
  2544. /* Frame or first field in a potentially complementary pair */
  2545. s0->first_field = FIELD_PICTURE;
  2546. }
  2547. if (!FIELD_PICTURE || s0->first_field) {
  2548. if (ff_h264_frame_start(h) < 0) {
  2549. s0->first_field = 0;
  2550. return -1;
  2551. }
  2552. } else {
  2553. ff_release_unused_pictures(s, 0);
  2554. }
  2555. }
  2556. if (h != h0)
  2557. clone_slice(h, h0);
  2558. s->current_picture_ptr->frame_num = h->frame_num; // FIXME frame_num cleanup
  2559. assert(s->mb_num == s->mb_width * s->mb_height);
  2560. if (first_mb_in_slice << FIELD_OR_MBAFF_PICTURE >= s->mb_num ||
  2561. first_mb_in_slice >= s->mb_num) {
  2562. av_log(h->s.avctx, AV_LOG_ERROR, "first_mb_in_slice overflow\n");
  2563. return -1;
  2564. }
  2565. s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
  2566. s->resync_mb_y = s->mb_y = (first_mb_in_slice / s->mb_width) << FIELD_OR_MBAFF_PICTURE;
  2567. if (s->picture_structure == PICT_BOTTOM_FIELD)
  2568. s->resync_mb_y = s->mb_y = s->mb_y + 1;
  2569. assert(s->mb_y < s->mb_height);
  2570. if (s->picture_structure == PICT_FRAME) {
  2571. h->curr_pic_num = h->frame_num;
  2572. h->max_pic_num = 1 << h->sps.log2_max_frame_num;
  2573. } else {
  2574. h->curr_pic_num = 2 * h->frame_num + 1;
  2575. h->max_pic_num = 1 << (h->sps.log2_max_frame_num + 1);
  2576. }
  2577. if (h->nal_unit_type == NAL_IDR_SLICE)
  2578. get_ue_golomb(&s->gb); /* idr_pic_id */
  2579. if (h->sps.poc_type == 0) {
  2580. h->poc_lsb = get_bits(&s->gb, h->sps.log2_max_poc_lsb);
  2581. if (h->pps.pic_order_present == 1 && s->picture_structure == PICT_FRAME)
  2582. h->delta_poc_bottom = get_se_golomb(&s->gb);
  2583. }
  2584. if (h->sps.poc_type == 1 && !h->sps.delta_pic_order_always_zero_flag) {
  2585. h->delta_poc[0] = get_se_golomb(&s->gb);
  2586. if (h->pps.pic_order_present == 1 && s->picture_structure == PICT_FRAME)
  2587. h->delta_poc[1] = get_se_golomb(&s->gb);
  2588. }
  2589. init_poc(h);
  2590. if (h->pps.redundant_pic_cnt_present)
  2591. h->redundant_pic_count = get_ue_golomb(&s->gb);
  2592. // set defaults, might be overridden a few lines later
  2593. h->ref_count[0] = h->pps.ref_count[0];
  2594. h->ref_count[1] = h->pps.ref_count[1];
  2595. if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
  2596. unsigned max[2];
  2597. max[0] = max[1] = s->picture_structure == PICT_FRAME ? 15 : 31;
  2598. if (h->slice_type_nos == AV_PICTURE_TYPE_B)
  2599. h->direct_spatial_mv_pred = get_bits1(&s->gb);
  2600. num_ref_idx_active_override_flag = get_bits1(&s->gb);
  2601. if (num_ref_idx_active_override_flag) {
  2602. h->ref_count[0] = get_ue_golomb(&s->gb) + 1;
  2603. if (h->slice_type_nos == AV_PICTURE_TYPE_B)
  2604. h->ref_count[1] = get_ue_golomb(&s->gb) + 1;
  2605. else
  2606. // full range is spec-ok in this case, even for frames
  2607. max[1] = 31;
  2608. }
  2609. if (h->ref_count[0]-1 > max[0] || h->ref_count[1]-1 > max[1]){
  2610. 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]);
  2611. h->ref_count[0] = h->ref_count[1] = 1;
  2612. return AVERROR_INVALIDDATA;
  2613. }
  2614. if (h->slice_type_nos == AV_PICTURE_TYPE_B)
  2615. h->list_count = 2;
  2616. else
  2617. h->list_count = 1;
  2618. } else
  2619. h->ref_count[1]= h->ref_count[0]= h->list_count= 0;
  2620. if (!default_ref_list_done)
  2621. ff_h264_fill_default_ref_list(h);
  2622. if (h->slice_type_nos != AV_PICTURE_TYPE_I &&
  2623. ff_h264_decode_ref_pic_list_reordering(h) < 0) {
  2624. h->ref_count[1] = h->ref_count[0] = 0;
  2625. return -1;
  2626. }
  2627. if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
  2628. s->last_picture_ptr = &h->ref_list[0][0];
  2629. ff_copy_picture(&s->last_picture, s->last_picture_ptr);
  2630. }
  2631. if (h->slice_type_nos == AV_PICTURE_TYPE_B) {
  2632. s->next_picture_ptr = &h->ref_list[1][0];
  2633. ff_copy_picture(&s->next_picture, s->next_picture_ptr);
  2634. }
  2635. if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
  2636. (h->pps.weighted_bipred_idc == 1 &&
  2637. h->slice_type_nos == AV_PICTURE_TYPE_B))
  2638. pred_weight_table(h);
  2639. else if (h->pps.weighted_bipred_idc == 2 &&
  2640. h->slice_type_nos == AV_PICTURE_TYPE_B) {
  2641. implicit_weight_table(h, -1);
  2642. } else {
  2643. h->use_weight = 0;
  2644. for (i = 0; i < 2; i++) {
  2645. h->luma_weight_flag[i] = 0;
  2646. h->chroma_weight_flag[i] = 0;
  2647. }
  2648. }
  2649. if (h->nal_ref_idc && ff_h264_decode_ref_pic_marking(h0, &s->gb) < 0 &&
  2650. (s->avctx->err_recognition & AV_EF_EXPLODE))
  2651. return AVERROR_INVALIDDATA;
  2652. if (FRAME_MBAFF) {
  2653. ff_h264_fill_mbaff_ref_list(h);
  2654. if (h->pps.weighted_bipred_idc == 2 && h->slice_type_nos == AV_PICTURE_TYPE_B) {
  2655. implicit_weight_table(h, 0);
  2656. implicit_weight_table(h, 1);
  2657. }
  2658. }
  2659. if (h->slice_type_nos == AV_PICTURE_TYPE_B && !h->direct_spatial_mv_pred)
  2660. ff_h264_direct_dist_scale_factor(h);
  2661. ff_h264_direct_ref_list_init(h);
  2662. if (h->slice_type_nos != AV_PICTURE_TYPE_I && h->pps.cabac) {
  2663. tmp = get_ue_golomb_31(&s->gb);
  2664. if (tmp > 2) {
  2665. av_log(s->avctx, AV_LOG_ERROR, "cabac_init_idc overflow\n");
  2666. return -1;
  2667. }
  2668. h->cabac_init_idc = tmp;
  2669. }
  2670. h->last_qscale_diff = 0;
  2671. tmp = h->pps.init_qp + get_se_golomb(&s->gb);
  2672. if (tmp > 51 + 6 * (h->sps.bit_depth_luma - 8)) {
  2673. av_log(s->avctx, AV_LOG_ERROR, "QP %u out of range\n", tmp);
  2674. return -1;
  2675. }
  2676. s->qscale = tmp;
  2677. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  2678. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  2679. // FIXME qscale / qp ... stuff
  2680. if (h->slice_type == AV_PICTURE_TYPE_SP)
  2681. get_bits1(&s->gb); /* sp_for_switch_flag */
  2682. if (h->slice_type == AV_PICTURE_TYPE_SP ||
  2683. h->slice_type == AV_PICTURE_TYPE_SI)
  2684. get_se_golomb(&s->gb); /* slice_qs_delta */
  2685. h->deblocking_filter = 1;
  2686. h->slice_alpha_c0_offset = 52;
  2687. h->slice_beta_offset = 52;
  2688. if (h->pps.deblocking_filter_parameters_present) {
  2689. tmp = get_ue_golomb_31(&s->gb);
  2690. if (tmp > 2) {
  2691. av_log(s->avctx, AV_LOG_ERROR,
  2692. "deblocking_filter_idc %u out of range\n", tmp);
  2693. return -1;
  2694. }
  2695. h->deblocking_filter = tmp;
  2696. if (h->deblocking_filter < 2)
  2697. h->deblocking_filter ^= 1; // 1<->0
  2698. if (h->deblocking_filter) {
  2699. h->slice_alpha_c0_offset += get_se_golomb(&s->gb) << 1;
  2700. h->slice_beta_offset += get_se_golomb(&s->gb) << 1;
  2701. if (h->slice_alpha_c0_offset > 104U ||
  2702. h->slice_beta_offset > 104U) {
  2703. av_log(s->avctx, AV_LOG_ERROR,
  2704. "deblocking filter parameters %d %d out of range\n",
  2705. h->slice_alpha_c0_offset, h->slice_beta_offset);
  2706. return -1;
  2707. }
  2708. }
  2709. }
  2710. if (s->avctx->skip_loop_filter >= AVDISCARD_ALL ||
  2711. (s->avctx->skip_loop_filter >= AVDISCARD_NONKEY &&
  2712. h->slice_type_nos != AV_PICTURE_TYPE_I) ||
  2713. (s->avctx->skip_loop_filter >= AVDISCARD_BIDIR &&
  2714. h->slice_type_nos == AV_PICTURE_TYPE_B) ||
  2715. (s->avctx->skip_loop_filter >= AVDISCARD_NONREF &&
  2716. h->nal_ref_idc == 0))
  2717. h->deblocking_filter = 0;
  2718. if (h->deblocking_filter == 1 && h0->max_contexts > 1) {
  2719. if (s->avctx->flags2 & CODEC_FLAG2_FAST) {
  2720. /* Cheat slightly for speed:
  2721. * Do not bother to deblock across slices. */
  2722. h->deblocking_filter = 2;
  2723. } else {
  2724. h0->max_contexts = 1;
  2725. if (!h0->single_decode_warning) {
  2726. av_log(s->avctx, AV_LOG_INFO,
  2727. "Cannot parallelize deblocking type 1, decoding such frames in sequential order\n");
  2728. h0->single_decode_warning = 1;
  2729. }
  2730. if (h != h0) {
  2731. av_log(h->s.avctx, AV_LOG_ERROR,
  2732. "Deblocking switched inside frame.\n");
  2733. return 1;
  2734. }
  2735. }
  2736. }
  2737. h->qp_thresh = 15 + 52 -
  2738. FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) -
  2739. FFMAX3(0,
  2740. h->pps.chroma_qp_index_offset[0],
  2741. h->pps.chroma_qp_index_offset[1]) +
  2742. 6 * (h->sps.bit_depth_luma - 8);
  2743. h0->last_slice_type = slice_type;
  2744. h->slice_num = ++h0->current_slice;
  2745. if (h->slice_num)
  2746. h0->slice_row[(h->slice_num-1)&(MAX_SLICES-1)]= s->resync_mb_y;
  2747. if ( h0->slice_row[h->slice_num&(MAX_SLICES-1)] + 3 >= s->resync_mb_y
  2748. && h0->slice_row[h->slice_num&(MAX_SLICES-1)] <= s->resync_mb_y
  2749. && h->slice_num >= MAX_SLICES) {
  2750. //in case of ASO this check needs to be updated depending on how we decide to assign slice numbers in this case
  2751. 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);
  2752. }
  2753. for (j = 0; j < 2; j++) {
  2754. int id_list[16];
  2755. int *ref2frm = h->ref2frm[h->slice_num & (MAX_SLICES - 1)][j];
  2756. for (i = 0; i < 16; i++) {
  2757. id_list[i] = 60;
  2758. if (h->ref_list[j][i].f.data[0]) {
  2759. int k;
  2760. uint8_t *base = h->ref_list[j][i].f.base[0];
  2761. for (k = 0; k < h->short_ref_count; k++)
  2762. if (h->short_ref[k]->f.base[0] == base) {
  2763. id_list[i] = k;
  2764. break;
  2765. }
  2766. for (k = 0; k < h->long_ref_count; k++)
  2767. if (h->long_ref[k] && h->long_ref[k]->f.base[0] == base) {
  2768. id_list[i] = h->short_ref_count + k;
  2769. break;
  2770. }
  2771. }
  2772. }
  2773. ref2frm[0] =
  2774. ref2frm[1] = -1;
  2775. for (i = 0; i < 16; i++)
  2776. ref2frm[i + 2] = 4 * id_list[i] +
  2777. (h->ref_list[j][i].f.reference & 3);
  2778. ref2frm[18 + 0] =
  2779. ref2frm[18 + 1] = -1;
  2780. for (i = 16; i < 48; i++)
  2781. ref2frm[i + 4] = 4 * id_list[(i - 16) >> 1] +
  2782. (h->ref_list[j][i].f.reference & 3);
  2783. }
  2784. // FIXME: fix draw_edges + PAFF + frame threads
  2785. h->emu_edge_width = (s->flags & CODEC_FLAG_EMU_EDGE ||
  2786. (!h->sps.frame_mbs_only_flag &&
  2787. s->avctx->active_thread_type))
  2788. ? 0 : 16;
  2789. h->emu_edge_height = (FRAME_MBAFF || FIELD_PICTURE) ? 0 : h->emu_edge_width;
  2790. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  2791. av_log(h->s.avctx, AV_LOG_DEBUG,
  2792. "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",
  2793. h->slice_num,
  2794. (s->picture_structure == PICT_FRAME ? "F" : s->picture_structure == PICT_TOP_FIELD ? "T" : "B"),
  2795. first_mb_in_slice,
  2796. av_get_picture_type_char(h->slice_type),
  2797. h->slice_type_fixed ? " fix" : "",
  2798. h->nal_unit_type == NAL_IDR_SLICE ? " IDR" : "",
  2799. pps_id, h->frame_num,
  2800. s->current_picture_ptr->field_poc[0],
  2801. s->current_picture_ptr->field_poc[1],
  2802. h->ref_count[0], h->ref_count[1],
  2803. s->qscale,
  2804. h->deblocking_filter,
  2805. h->slice_alpha_c0_offset / 2 - 26, h->slice_beta_offset / 2 - 26,
  2806. h->use_weight,
  2807. h->use_weight == 1 && h->use_weight_chroma ? "c" : "",
  2808. h->slice_type == AV_PICTURE_TYPE_B ? (h->direct_spatial_mv_pred ? "SPAT" : "TEMP") : "");
  2809. }
  2810. return 0;
  2811. }
  2812. int ff_h264_get_slice_type(const H264Context *h)
  2813. {
  2814. switch (h->slice_type) {
  2815. case AV_PICTURE_TYPE_P:
  2816. return 0;
  2817. case AV_PICTURE_TYPE_B:
  2818. return 1;
  2819. case AV_PICTURE_TYPE_I:
  2820. return 2;
  2821. case AV_PICTURE_TYPE_SP:
  2822. return 3;
  2823. case AV_PICTURE_TYPE_SI:
  2824. return 4;
  2825. default:
  2826. return -1;
  2827. }
  2828. }
  2829. static av_always_inline void fill_filter_caches_inter(H264Context *h,
  2830. MpegEncContext *const s,
  2831. int mb_type, int top_xy,
  2832. int left_xy[LEFT_MBS],
  2833. int top_type,
  2834. int left_type[LEFT_MBS],
  2835. int mb_xy, int list)
  2836. {
  2837. int b_stride = h->b_stride;
  2838. int16_t(*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
  2839. int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
  2840. if (IS_INTER(mb_type) || IS_DIRECT(mb_type)) {
  2841. if (USES_LIST(top_type, list)) {
  2842. const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
  2843. const int b8_xy = 4 * top_xy + 2;
  2844. int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_table[top_xy] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
  2845. AV_COPY128(mv_dst - 1 * 8, s->current_picture.f.motion_val[list][b_xy + 0]);
  2846. ref_cache[0 - 1 * 8] =
  2847. ref_cache[1 - 1 * 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 0]];
  2848. ref_cache[2 - 1 * 8] =
  2849. ref_cache[3 - 1 * 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 1]];
  2850. } else {
  2851. AV_ZERO128(mv_dst - 1 * 8);
  2852. AV_WN32A(&ref_cache[0 - 1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  2853. }
  2854. if (!IS_INTERLACED(mb_type ^ left_type[LTOP])) {
  2855. if (USES_LIST(left_type[LTOP], list)) {
  2856. const int b_xy = h->mb2b_xy[left_xy[LTOP]] + 3;
  2857. const int b8_xy = 4 * left_xy[LTOP] + 1;
  2858. int (*ref2frm)[64] =(void*)( h->ref2frm[h->slice_table[left_xy[LTOP]] & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
  2859. AV_COPY32(mv_dst - 1 + 0, s->current_picture.f.motion_val[list][b_xy + b_stride * 0]);
  2860. AV_COPY32(mv_dst - 1 + 8, s->current_picture.f.motion_val[list][b_xy + b_stride * 1]);
  2861. AV_COPY32(mv_dst - 1 + 16, s->current_picture.f.motion_val[list][b_xy + b_stride * 2]);
  2862. AV_COPY32(mv_dst - 1 + 24, s->current_picture.f.motion_val[list][b_xy + b_stride * 3]);
  2863. ref_cache[-1 + 0] =
  2864. ref_cache[-1 + 8] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2 * 0]];
  2865. ref_cache[-1 + 16] =
  2866. ref_cache[-1 + 24] = ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2 * 1]];
  2867. } else {
  2868. AV_ZERO32(mv_dst - 1 + 0);
  2869. AV_ZERO32(mv_dst - 1 + 8);
  2870. AV_ZERO32(mv_dst - 1 + 16);
  2871. AV_ZERO32(mv_dst - 1 + 24);
  2872. ref_cache[-1 + 0] =
  2873. ref_cache[-1 + 8] =
  2874. ref_cache[-1 + 16] =
  2875. ref_cache[-1 + 24] = LIST_NOT_USED;
  2876. }
  2877. }
  2878. }
  2879. if (!USES_LIST(mb_type, list)) {
  2880. fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0, 0), 4);
  2881. AV_WN32A(&ref_cache[0 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  2882. AV_WN32A(&ref_cache[1 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  2883. AV_WN32A(&ref_cache[2 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  2884. AV_WN32A(&ref_cache[3 * 8], ((LIST_NOT_USED) & 0xFF) * 0x01010101u);
  2885. return;
  2886. }
  2887. {
  2888. int8_t *ref = &s->current_picture.f.ref_index[list][4 * mb_xy];
  2889. int (*ref2frm)[64] = (void*)(h->ref2frm[h->slice_num & (MAX_SLICES - 1)][0] + (MB_MBAFF ? 20 : 2));
  2890. uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]], ref2frm[list][ref[1]]) & 0x00FF00FF) * 0x0101;
  2891. uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]], ref2frm[list][ref[3]]) & 0x00FF00FF) * 0x0101;
  2892. AV_WN32A(&ref_cache[0 * 8], ref01);
  2893. AV_WN32A(&ref_cache[1 * 8], ref01);
  2894. AV_WN32A(&ref_cache[2 * 8], ref23);
  2895. AV_WN32A(&ref_cache[3 * 8], ref23);
  2896. }
  2897. {
  2898. int16_t(*mv_src)[2] = &s->current_picture.f.motion_val[list][4 * s->mb_x + 4 * s->mb_y * b_stride];
  2899. AV_COPY128(mv_dst + 8 * 0, mv_src + 0 * b_stride);
  2900. AV_COPY128(mv_dst + 8 * 1, mv_src + 1 * b_stride);
  2901. AV_COPY128(mv_dst + 8 * 2, mv_src + 2 * b_stride);
  2902. AV_COPY128(mv_dst + 8 * 3, mv_src + 3 * b_stride);
  2903. }
  2904. }
  2905. /**
  2906. *
  2907. * @return non zero if the loop filter can be skipped
  2908. */
  2909. static int fill_filter_caches(H264Context *h, int mb_type)
  2910. {
  2911. MpegEncContext *const s = &h->s;
  2912. const int mb_xy = h->mb_xy;
  2913. int top_xy, left_xy[LEFT_MBS];
  2914. int top_type, left_type[LEFT_MBS];
  2915. uint8_t *nnz;
  2916. uint8_t *nnz_cache;
  2917. top_xy = mb_xy - (s->mb_stride << MB_FIELD);
  2918. /* Wow, what a mess, why didn't they simplify the interlacing & intra
  2919. * stuff, I can't imagine that these complex rules are worth it. */
  2920. left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
  2921. if (FRAME_MBAFF) {
  2922. const int left_mb_field_flag = IS_INTERLACED(s->current_picture.f.mb_type[mb_xy - 1]);
  2923. const int curr_mb_field_flag = IS_INTERLACED(mb_type);
  2924. if (s->mb_y & 1) {
  2925. if (left_mb_field_flag != curr_mb_field_flag)
  2926. left_xy[LTOP] -= s->mb_stride;
  2927. } else {
  2928. if (curr_mb_field_flag)
  2929. top_xy += s->mb_stride &
  2930. (((s->current_picture.f.mb_type[top_xy] >> 7) & 1) - 1);
  2931. if (left_mb_field_flag != curr_mb_field_flag)
  2932. left_xy[LBOT] += s->mb_stride;
  2933. }
  2934. }
  2935. h->top_mb_xy = top_xy;
  2936. h->left_mb_xy[LTOP] = left_xy[LTOP];
  2937. h->left_mb_xy[LBOT] = left_xy[LBOT];
  2938. {
  2939. /* For sufficiently low qp, filtering wouldn't do anything.
  2940. * This is a conservative estimate: could also check beta_offset
  2941. * and more accurate chroma_qp. */
  2942. int qp_thresh = h->qp_thresh; // FIXME strictly we should store qp_thresh for each mb of a slice
  2943. int qp = s->current_picture.f.qscale_table[mb_xy];
  2944. if (qp <= qp_thresh &&
  2945. (left_xy[LTOP] < 0 ||
  2946. ((qp + s->current_picture.f.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh) &&
  2947. (top_xy < 0 ||
  2948. ((qp + s->current_picture.f.qscale_table[top_xy] + 1) >> 1) <= qp_thresh)) {
  2949. if (!FRAME_MBAFF)
  2950. return 1;
  2951. if ((left_xy[LTOP] < 0 ||
  2952. ((qp + s->current_picture.f.qscale_table[left_xy[LBOT]] + 1) >> 1) <= qp_thresh) &&
  2953. (top_xy < s->mb_stride ||
  2954. ((qp + s->current_picture.f.qscale_table[top_xy - s->mb_stride] + 1) >> 1) <= qp_thresh))
  2955. return 1;
  2956. }
  2957. }
  2958. top_type = s->current_picture.f.mb_type[top_xy];
  2959. left_type[LTOP] = s->current_picture.f.mb_type[left_xy[LTOP]];
  2960. left_type[LBOT] = s->current_picture.f.mb_type[left_xy[LBOT]];
  2961. if (h->deblocking_filter == 2) {
  2962. if (h->slice_table[top_xy] != h->slice_num)
  2963. top_type = 0;
  2964. if (h->slice_table[left_xy[LBOT]] != h->slice_num)
  2965. left_type[LTOP] = left_type[LBOT] = 0;
  2966. } else {
  2967. if (h->slice_table[top_xy] == 0xFFFF)
  2968. top_type = 0;
  2969. if (h->slice_table[left_xy[LBOT]] == 0xFFFF)
  2970. left_type[LTOP] = left_type[LBOT] = 0;
  2971. }
  2972. h->top_type = top_type;
  2973. h->left_type[LTOP] = left_type[LTOP];
  2974. h->left_type[LBOT] = left_type[LBOT];
  2975. if (IS_INTRA(mb_type))
  2976. return 0;
  2977. fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy,
  2978. top_type, left_type, mb_xy, 0);
  2979. if (h->list_count == 2)
  2980. fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy,
  2981. top_type, left_type, mb_xy, 1);
  2982. nnz = h->non_zero_count[mb_xy];
  2983. nnz_cache = h->non_zero_count_cache;
  2984. AV_COPY32(&nnz_cache[4 + 8 * 1], &nnz[0]);
  2985. AV_COPY32(&nnz_cache[4 + 8 * 2], &nnz[4]);
  2986. AV_COPY32(&nnz_cache[4 + 8 * 3], &nnz[8]);
  2987. AV_COPY32(&nnz_cache[4 + 8 * 4], &nnz[12]);
  2988. h->cbp = h->cbp_table[mb_xy];
  2989. if (top_type) {
  2990. nnz = h->non_zero_count[top_xy];
  2991. AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[3 * 4]);
  2992. }
  2993. if (left_type[LTOP]) {
  2994. nnz = h->non_zero_count[left_xy[LTOP]];
  2995. nnz_cache[3 + 8 * 1] = nnz[3 + 0 * 4];
  2996. nnz_cache[3 + 8 * 2] = nnz[3 + 1 * 4];
  2997. nnz_cache[3 + 8 * 3] = nnz[3 + 2 * 4];
  2998. nnz_cache[3 + 8 * 4] = nnz[3 + 3 * 4];
  2999. }
  3000. /* CAVLC 8x8dct requires NNZ values for residual decoding that differ
  3001. * from what the loop filter needs */
  3002. if (!CABAC && h->pps.transform_8x8_mode) {
  3003. if (IS_8x8DCT(top_type)) {
  3004. nnz_cache[4 + 8 * 0] =
  3005. nnz_cache[5 + 8 * 0] = (h->cbp_table[top_xy] & 0x4000) >> 12;
  3006. nnz_cache[6 + 8 * 0] =
  3007. nnz_cache[7 + 8 * 0] = (h->cbp_table[top_xy] & 0x8000) >> 12;
  3008. }
  3009. if (IS_8x8DCT(left_type[LTOP])) {
  3010. nnz_cache[3 + 8 * 1] =
  3011. nnz_cache[3 + 8 * 2] = (h->cbp_table[left_xy[LTOP]] & 0x2000) >> 12; // FIXME check MBAFF
  3012. }
  3013. if (IS_8x8DCT(left_type[LBOT])) {
  3014. nnz_cache[3 + 8 * 3] =
  3015. nnz_cache[3 + 8 * 4] = (h->cbp_table[left_xy[LBOT]] & 0x8000) >> 12; // FIXME check MBAFF
  3016. }
  3017. if (IS_8x8DCT(mb_type)) {
  3018. nnz_cache[scan8[0]] =
  3019. nnz_cache[scan8[1]] =
  3020. nnz_cache[scan8[2]] =
  3021. nnz_cache[scan8[3]] = (h->cbp & 0x1000) >> 12;
  3022. nnz_cache[scan8[0 + 4]] =
  3023. nnz_cache[scan8[1 + 4]] =
  3024. nnz_cache[scan8[2 + 4]] =
  3025. nnz_cache[scan8[3 + 4]] = (h->cbp & 0x2000) >> 12;
  3026. nnz_cache[scan8[0 + 8]] =
  3027. nnz_cache[scan8[1 + 8]] =
  3028. nnz_cache[scan8[2 + 8]] =
  3029. nnz_cache[scan8[3 + 8]] = (h->cbp & 0x4000) >> 12;
  3030. nnz_cache[scan8[0 + 12]] =
  3031. nnz_cache[scan8[1 + 12]] =
  3032. nnz_cache[scan8[2 + 12]] =
  3033. nnz_cache[scan8[3 + 12]] = (h->cbp & 0x8000) >> 12;
  3034. }
  3035. }
  3036. return 0;
  3037. }
  3038. static void loop_filter(H264Context *h, int start_x, int end_x)
  3039. {
  3040. MpegEncContext *const s = &h->s;
  3041. uint8_t *dest_y, *dest_cb, *dest_cr;
  3042. int linesize, uvlinesize, mb_x, mb_y;
  3043. const int end_mb_y = s->mb_y + FRAME_MBAFF;
  3044. const int old_slice_type = h->slice_type;
  3045. const int pixel_shift = h->pixel_shift;
  3046. const int block_h = 16 >> s->chroma_y_shift;
  3047. if (h->deblocking_filter) {
  3048. for (mb_x = start_x; mb_x < end_x; mb_x++)
  3049. for (mb_y = end_mb_y - FRAME_MBAFF; mb_y <= end_mb_y; mb_y++) {
  3050. int mb_xy, mb_type;
  3051. mb_xy = h->mb_xy = mb_x + mb_y * s->mb_stride;
  3052. h->slice_num = h->slice_table[mb_xy];
  3053. mb_type = s->current_picture.f.mb_type[mb_xy];
  3054. h->list_count = h->list_counts[mb_xy];
  3055. if (FRAME_MBAFF)
  3056. h->mb_mbaff =
  3057. h->mb_field_decoding_flag = !!IS_INTERLACED(mb_type);
  3058. s->mb_x = mb_x;
  3059. s->mb_y = mb_y;
  3060. dest_y = s->current_picture.f.data[0] +
  3061. ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
  3062. dest_cb = s->current_picture.f.data[1] +
  3063. (mb_x << pixel_shift) * (8 << CHROMA444) +
  3064. mb_y * s->uvlinesize * block_h;
  3065. dest_cr = s->current_picture.f.data[2] +
  3066. (mb_x << pixel_shift) * (8 << CHROMA444) +
  3067. mb_y * s->uvlinesize * block_h;
  3068. // FIXME simplify above
  3069. if (MB_FIELD) {
  3070. linesize = h->mb_linesize = s->linesize * 2;
  3071. uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
  3072. if (mb_y & 1) { // FIXME move out of this function?
  3073. dest_y -= s->linesize * 15;
  3074. dest_cb -= s->uvlinesize * (block_h - 1);
  3075. dest_cr -= s->uvlinesize * (block_h - 1);
  3076. }
  3077. } else {
  3078. linesize = h->mb_linesize = s->linesize;
  3079. uvlinesize = h->mb_uvlinesize = s->uvlinesize;
  3080. }
  3081. backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize,
  3082. uvlinesize, 0);
  3083. if (fill_filter_caches(h, mb_type))
  3084. continue;
  3085. h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.f.qscale_table[mb_xy]);
  3086. h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.f.qscale_table[mb_xy]);
  3087. if (FRAME_MBAFF) {
  3088. ff_h264_filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr,
  3089. linesize, uvlinesize);
  3090. } else {
  3091. ff_h264_filter_mb_fast(h, mb_x, mb_y, dest_y, dest_cb,
  3092. dest_cr, linesize, uvlinesize);
  3093. }
  3094. }
  3095. }
  3096. h->slice_type = old_slice_type;
  3097. s->mb_x = end_x;
  3098. s->mb_y = end_mb_y - FRAME_MBAFF;
  3099. h->chroma_qp[0] = get_chroma_qp(h, 0, s->qscale);
  3100. h->chroma_qp[1] = get_chroma_qp(h, 1, s->qscale);
  3101. }
  3102. static void predict_field_decoding_flag(H264Context *h)
  3103. {
  3104. MpegEncContext *const s = &h->s;
  3105. const int mb_xy = s->mb_x + s->mb_y * s->mb_stride;
  3106. int mb_type = (h->slice_table[mb_xy - 1] == h->slice_num) ?
  3107. s->current_picture.f.mb_type[mb_xy - 1] :
  3108. (h->slice_table[mb_xy - s->mb_stride] == h->slice_num) ?
  3109. s->current_picture.f.mb_type[mb_xy - s->mb_stride] : 0;
  3110. h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
  3111. }
  3112. /**
  3113. * Draw edges and report progress for the last MB row.
  3114. */
  3115. static void decode_finish_row(H264Context *h)
  3116. {
  3117. MpegEncContext *const s = &h->s;
  3118. int top = 16 * (s->mb_y >> FIELD_PICTURE);
  3119. int pic_height = 16 * s->mb_height >> FIELD_PICTURE;
  3120. int height = 16 << FRAME_MBAFF;
  3121. int deblock_border = (16 + 4) << FRAME_MBAFF;
  3122. if (h->deblocking_filter) {
  3123. if ((top + height) >= pic_height)
  3124. height += deblock_border;
  3125. top -= deblock_border;
  3126. }
  3127. if (top >= pic_height || (top + height) < h->emu_edge_height)
  3128. return;
  3129. height = FFMIN(height, pic_height - top);
  3130. if (top < h->emu_edge_height) {
  3131. height = top + height;
  3132. top = 0;
  3133. }
  3134. ff_draw_horiz_band(s, top, height);
  3135. if (s->dropable)
  3136. return;
  3137. ff_thread_report_progress(&s->current_picture_ptr->f, top + height - 1,
  3138. s->picture_structure == PICT_BOTTOM_FIELD);
  3139. }
  3140. static int decode_slice(struct AVCodecContext *avctx, void *arg)
  3141. {
  3142. H264Context *h = *(void **)arg;
  3143. MpegEncContext *const s = &h->s;
  3144. const int part_mask = s->partitioned_frame ? (ER_AC_END | ER_AC_ERROR)
  3145. : 0x7F;
  3146. int lf_x_start = s->mb_x;
  3147. s->mb_skip_run = -1;
  3148. h->is_complex = FRAME_MBAFF || s->picture_structure != PICT_FRAME ||
  3149. s->codec_id != AV_CODEC_ID_H264 ||
  3150. (CONFIG_GRAY && (s->flags & CODEC_FLAG_GRAY));
  3151. if (h->pps.cabac) {
  3152. /* realign */
  3153. align_get_bits(&s->gb);
  3154. /* init cabac */
  3155. ff_init_cabac_states();
  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, {.dbl = 0}, 0, 1, 0},
  3730. {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), FF_OPT_TYPE_INT, {.dbl = 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 PixelFormat[]) { PIX_FMT_VDPAU_H264,
  3776. PIX_FMT_NONE},
  3777. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  3778. .priv_class = &h264_vdpau_class,
  3779. };
  3780. #endif