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.

4235 lines
164KB

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