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.

4243 lines
164KB

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