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.

4209 lines
163KB

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