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.

911 lines
33KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... reference picture handling
  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 / MPEG-4 part10 reference picture handling.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include <inttypes.h>
  27. #include "libavutil/avassert.h"
  28. #include "internal.h"
  29. #include "avcodec.h"
  30. #include "h264.h"
  31. #include "golomb.h"
  32. #include "mpegutils.h"
  33. #include <assert.h>
  34. static void pic_as_field(H264Ref *pic, const int parity)
  35. {
  36. int i;
  37. for (i = 0; i < FF_ARRAY_ELEMS(pic->data); ++i) {
  38. if (parity == PICT_BOTTOM_FIELD)
  39. pic->data[i] += pic->linesize[i];
  40. pic->reference = parity;
  41. pic->linesize[i] *= 2;
  42. }
  43. pic->poc = pic->parent->field_poc[parity == PICT_BOTTOM_FIELD];
  44. }
  45. static void ref_from_h264pic(H264Ref *dst, H264Picture *src)
  46. {
  47. memcpy(dst->data, src->f->data, sizeof(dst->data));
  48. memcpy(dst->linesize, src->f->linesize, sizeof(dst->linesize));
  49. dst->reference = src->reference;
  50. dst->poc = src->poc;
  51. dst->pic_id = src->pic_id;
  52. dst->parent = src;
  53. }
  54. static int split_field_copy(H264Ref *dest, H264Picture *src, int parity, int id_add)
  55. {
  56. int match = !!(src->reference & parity);
  57. if (match) {
  58. ref_from_h264pic(dest, src);
  59. if (parity != PICT_FRAME) {
  60. pic_as_field(dest, parity);
  61. dest->pic_id *= 2;
  62. dest->pic_id += id_add;
  63. }
  64. }
  65. return match;
  66. }
  67. static int build_def_list(H264Ref *def, int def_len,
  68. H264Picture * const *in, int len, int is_long, int sel)
  69. {
  70. int i[2] = { 0 };
  71. int index = 0;
  72. while (i[0] < len || i[1] < len) {
  73. while (i[0] < len && !(in[i[0]] && (in[i[0]]->reference & sel)))
  74. i[0]++;
  75. while (i[1] < len && !(in[i[1]] && (in[i[1]]->reference & (sel ^ 3))))
  76. i[1]++;
  77. if (i[0] < len) {
  78. av_assert0(index < def_len);
  79. in[i[0]]->pic_id = is_long ? i[0] : in[i[0]]->frame_num;
  80. split_field_copy(&def[index++], in[i[0]++], sel, 1);
  81. }
  82. if (i[1] < len) {
  83. av_assert0(index < def_len);
  84. in[i[1]]->pic_id = is_long ? i[1] : in[i[1]]->frame_num;
  85. split_field_copy(&def[index++], in[i[1]++], sel ^ 3, 0);
  86. }
  87. }
  88. return index;
  89. }
  90. static int add_sorted(H264Picture **sorted, H264Picture * const *src,
  91. int len, int limit, int dir)
  92. {
  93. int i, best_poc;
  94. int out_i = 0;
  95. for (;;) {
  96. best_poc = dir ? INT_MIN : INT_MAX;
  97. for (i = 0; i < len; i++) {
  98. const int poc = src[i]->poc;
  99. if (((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)) {
  100. best_poc = poc;
  101. sorted[out_i] = src[i];
  102. }
  103. }
  104. if (best_poc == (dir ? INT_MIN : INT_MAX))
  105. break;
  106. limit = sorted[out_i++]->poc - dir;
  107. }
  108. return out_i;
  109. }
  110. static int mismatches_ref(const H264Context *h, const H264Picture *pic)
  111. {
  112. const AVFrame *f = pic->f;
  113. return (h->cur_pic_ptr->f->width != f->width ||
  114. h->cur_pic_ptr->f->height != f->height ||
  115. h->cur_pic_ptr->f->format != f->format);
  116. }
  117. static void h264_initialise_ref_list(H264Context *h, H264SliceContext *sl)
  118. {
  119. int i, len;
  120. int j;
  121. if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
  122. H264Picture *sorted[32];
  123. int cur_poc, list;
  124. int lens[2];
  125. if (FIELD_PICTURE(h))
  126. cur_poc = h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];
  127. else
  128. cur_poc = h->cur_pic_ptr->poc;
  129. for (list = 0; list < 2; list++) {
  130. len = add_sorted(sorted, h->short_ref, h->short_ref_count, cur_poc, 1 ^ list);
  131. len += add_sorted(sorted + len, h->short_ref, h->short_ref_count, cur_poc, 0 ^ list);
  132. av_assert0(len <= 32);
  133. len = build_def_list(sl->ref_list[list], FF_ARRAY_ELEMS(sl->ref_list[0]),
  134. sorted, len, 0, h->picture_structure);
  135. len += build_def_list(sl->ref_list[list] + len,
  136. FF_ARRAY_ELEMS(sl->ref_list[0]) - len,
  137. h->long_ref, 16, 1, h->picture_structure);
  138. av_assert0(len <= 32);
  139. if (len < sl->ref_count[list])
  140. memset(&sl->ref_list[list][len], 0, sizeof(H264Ref) * (sl->ref_count[list] - len));
  141. lens[list] = len;
  142. }
  143. if (lens[0] == lens[1] && lens[1] > 1) {
  144. for (i = 0; i < lens[0] &&
  145. sl->ref_list[0][i].parent->f->buf[0]->buffer ==
  146. sl->ref_list[1][i].parent->f->buf[0]->buffer; i++);
  147. if (i == lens[0]) {
  148. FFSWAP(H264Ref, sl->ref_list[1][0], sl->ref_list[1][1]);
  149. }
  150. }
  151. } else {
  152. len = build_def_list(sl->ref_list[0], FF_ARRAY_ELEMS(sl->ref_list[0]),
  153. h->short_ref, h->short_ref_count, 0, h->picture_structure);
  154. len += build_def_list(sl->ref_list[0] + len,
  155. FF_ARRAY_ELEMS(sl->ref_list[0]) - len,
  156. h-> long_ref, 16, 1, h->picture_structure);
  157. av_assert0(len <= 32);
  158. if (len < sl->ref_count[0])
  159. memset(&sl->ref_list[0][len], 0, sizeof(H264Ref) * (sl->ref_count[0] - len));
  160. }
  161. #ifdef TRACE
  162. for (i = 0; i < sl->ref_count[0]; i++) {
  163. ff_tlog(h->avctx, "List0: %s fn:%d 0x%p\n",
  164. (sl->ref_list[0][i].parent ? (sl->ref_list[0][i].parent->long_ref ? "LT" : "ST") : "??"),
  165. sl->ref_list[0][i].pic_id,
  166. sl->ref_list[0][i].data[0]);
  167. }
  168. if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
  169. for (i = 0; i < sl->ref_count[1]; i++) {
  170. ff_tlog(h->avctx, "List1: %s fn:%d 0x%p\n",
  171. (sl->ref_list[1][i].parent ? (sl->ref_list[1][i].parent->long_ref ? "LT" : "ST") : "??"),
  172. sl->ref_list[1][i].pic_id,
  173. sl->ref_list[1][i].data[0]);
  174. }
  175. }
  176. #endif
  177. for (j = 0; j<1+(sl->slice_type_nos == AV_PICTURE_TYPE_B); j++) {
  178. for (i = 0; i < sl->ref_count[j]; i++) {
  179. if (sl->ref_list[j][i].parent) {
  180. if (mismatches_ref(h, sl->ref_list[j][i].parent)) {
  181. av_log(h->avctx, AV_LOG_ERROR, "Discarding mismatching reference\n");
  182. memset(&sl->ref_list[j][i], 0, sizeof(sl->ref_list[j][i]));
  183. }
  184. }
  185. }
  186. }
  187. for (i = 0; i < sl->list_count; i++)
  188. h->default_ref[i] = sl->ref_list[i][0];
  189. }
  190. /**
  191. * print short term list
  192. */
  193. static void print_short_term(const H264Context *h)
  194. {
  195. uint32_t i;
  196. if (h->avctx->debug & FF_DEBUG_MMCO) {
  197. av_log(h->avctx, AV_LOG_DEBUG, "short term list:\n");
  198. for (i = 0; i < h->short_ref_count; i++) {
  199. H264Picture *pic = h->short_ref[i];
  200. av_log(h->avctx, AV_LOG_DEBUG, "%"PRIu32" fn:%d poc:%d %p\n",
  201. i, pic->frame_num, pic->poc, pic->f->data[0]);
  202. }
  203. }
  204. }
  205. /**
  206. * print long term list
  207. */
  208. static void print_long_term(const H264Context *h)
  209. {
  210. uint32_t i;
  211. if (h->avctx->debug & FF_DEBUG_MMCO) {
  212. av_log(h->avctx, AV_LOG_DEBUG, "long term list:\n");
  213. for (i = 0; i < 16; i++) {
  214. H264Picture *pic = h->long_ref[i];
  215. if (pic) {
  216. av_log(h->avctx, AV_LOG_DEBUG, "%"PRIu32" fn:%d poc:%d %p\n",
  217. i, pic->frame_num, pic->poc, pic->f->data[0]);
  218. }
  219. }
  220. }
  221. }
  222. /**
  223. * Extract structure information about the picture described by pic_num in
  224. * the current decoding context (frame or field). Note that pic_num is
  225. * picture number without wrapping (so, 0<=pic_num<max_pic_num).
  226. * @param pic_num picture number for which to extract structure information
  227. * @param structure one of PICT_XXX describing structure of picture
  228. * with pic_num
  229. * @return frame number (short term) or long term index of picture
  230. * described by pic_num
  231. */
  232. static int pic_num_extract(const H264Context *h, int pic_num, int *structure)
  233. {
  234. *structure = h->picture_structure;
  235. if (FIELD_PICTURE(h)) {
  236. if (!(pic_num & 1))
  237. /* opposite field */
  238. *structure ^= PICT_FRAME;
  239. pic_num >>= 1;
  240. }
  241. return pic_num;
  242. }
  243. int ff_h264_decode_ref_pic_list_reordering(H264Context *h, H264SliceContext *sl)
  244. {
  245. int list, index, pic_structure;
  246. print_short_term(h);
  247. print_long_term(h);
  248. h264_initialise_ref_list(h, sl);
  249. for (list = 0; list < sl->list_count; list++) {
  250. if (get_bits1(&sl->gb)) { // ref_pic_list_modification_flag_l[01]
  251. int pred = h->curr_pic_num;
  252. for (index = 0; ; index++) {
  253. unsigned int modification_of_pic_nums_idc = get_ue_golomb_31(&sl->gb);
  254. unsigned int pic_id;
  255. int i;
  256. H264Picture *ref = NULL;
  257. if (modification_of_pic_nums_idc == 3)
  258. break;
  259. if (index >= sl->ref_count[list]) {
  260. av_log(h->avctx, AV_LOG_ERROR, "reference count overflow\n");
  261. return -1;
  262. }
  263. switch (modification_of_pic_nums_idc) {
  264. case 0:
  265. case 1: {
  266. const unsigned int abs_diff_pic_num = get_ue_golomb_long(&sl->gb) + 1;
  267. int frame_num;
  268. if (abs_diff_pic_num > h->max_pic_num) {
  269. av_log(h->avctx, AV_LOG_ERROR,
  270. "abs_diff_pic_num overflow\n");
  271. return AVERROR_INVALIDDATA;
  272. }
  273. if (modification_of_pic_nums_idc == 0)
  274. pred -= abs_diff_pic_num;
  275. else
  276. pred += abs_diff_pic_num;
  277. pred &= h->max_pic_num - 1;
  278. frame_num = pic_num_extract(h, pred, &pic_structure);
  279. for (i = h->short_ref_count - 1; i >= 0; i--) {
  280. ref = h->short_ref[i];
  281. assert(ref->reference);
  282. assert(!ref->long_ref);
  283. if (ref->frame_num == frame_num &&
  284. (ref->reference & pic_structure))
  285. break;
  286. }
  287. if (i >= 0)
  288. ref->pic_id = pred;
  289. break;
  290. }
  291. case 2: {
  292. int long_idx;
  293. pic_id = get_ue_golomb(&sl->gb); // long_term_pic_idx
  294. long_idx = pic_num_extract(h, pic_id, &pic_structure);
  295. if (long_idx > 31U) {
  296. av_log(h->avctx, AV_LOG_ERROR,
  297. "long_term_pic_idx overflow\n");
  298. return AVERROR_INVALIDDATA;
  299. }
  300. ref = h->long_ref[long_idx];
  301. assert(!(ref && !ref->reference));
  302. if (ref && (ref->reference & pic_structure) && !mismatches_ref(h, ref)) {
  303. ref->pic_id = pic_id;
  304. assert(ref->long_ref);
  305. i = 0;
  306. } else {
  307. i = -1;
  308. }
  309. break;
  310. }
  311. default:
  312. av_log(h->avctx, AV_LOG_ERROR,
  313. "illegal modification_of_pic_nums_idc %u\n",
  314. modification_of_pic_nums_idc);
  315. return AVERROR_INVALIDDATA;
  316. }
  317. if (i < 0) {
  318. av_log(h->avctx, AV_LOG_ERROR,
  319. "reference picture missing during reorder\n");
  320. memset(&sl->ref_list[list][index], 0, sizeof(sl->ref_list[0][0])); // FIXME
  321. } else {
  322. for (i = index; i + 1 < sl->ref_count[list]; i++) {
  323. if (sl->ref_list[list][i].parent &&
  324. ref->long_ref == sl->ref_list[list][i].parent->long_ref &&
  325. ref->pic_id == sl->ref_list[list][i].pic_id)
  326. break;
  327. }
  328. for (; i > index; i--) {
  329. sl->ref_list[list][i] = sl->ref_list[list][i - 1];
  330. }
  331. ref_from_h264pic(&sl->ref_list[list][index], ref);
  332. if (FIELD_PICTURE(h)) {
  333. pic_as_field(&sl->ref_list[list][index], pic_structure);
  334. }
  335. }
  336. }
  337. }
  338. }
  339. for (list = 0; list < sl->list_count; list++) {
  340. for (index = 0; index < sl->ref_count[list]; index++) {
  341. if ( !sl->ref_list[list][index].parent
  342. || (!FIELD_PICTURE(h) && (sl->ref_list[list][index].reference&3) != 3)) {
  343. int i;
  344. av_log(h->avctx, AV_LOG_ERROR, "Missing reference picture, default is %d\n", h->default_ref[list].poc);
  345. for (i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
  346. h->last_pocs[i] = INT_MIN;
  347. if (h->default_ref[list].parent
  348. && !(!FIELD_PICTURE(h) && (h->default_ref[list].reference&3) != 3))
  349. sl->ref_list[list][index] = h->default_ref[list];
  350. else
  351. return -1;
  352. }
  353. av_assert0(av_buffer_get_ref_count(sl->ref_list[list][index].parent->f->buf[0]) > 0);
  354. }
  355. }
  356. return 0;
  357. }
  358. void ff_h264_fill_mbaff_ref_list(H264SliceContext *sl)
  359. {
  360. int list, i, j;
  361. for (list = 0; list < sl->list_count; list++) {
  362. for (i = 0; i < sl->ref_count[list]; i++) {
  363. H264Ref *frame = &sl->ref_list[list][i];
  364. H264Ref *field = &sl->ref_list[list][16 + 2 * i];
  365. field[0] = *frame;
  366. for (j = 0; j < 3; j++)
  367. field[0].linesize[j] <<= 1;
  368. field[0].reference = PICT_TOP_FIELD;
  369. field[0].poc = field[0].parent->field_poc[0];
  370. field[1] = field[0];
  371. for (j = 0; j < 3; j++)
  372. field[1].data[j] += frame->parent->f->linesize[j];
  373. field[1].reference = PICT_BOTTOM_FIELD;
  374. field[1].poc = field[1].parent->field_poc[1];
  375. sl->pwt.luma_weight[16 + 2 * i][list][0] = sl->pwt.luma_weight[16 + 2 * i + 1][list][0] = sl->pwt.luma_weight[i][list][0];
  376. sl->pwt.luma_weight[16 + 2 * i][list][1] = sl->pwt.luma_weight[16 + 2 * i + 1][list][1] = sl->pwt.luma_weight[i][list][1];
  377. for (j = 0; j < 2; j++) {
  378. sl->pwt.chroma_weight[16 + 2 * i][list][j][0] = sl->pwt.chroma_weight[16 + 2 * i + 1][list][j][0] = sl->pwt.chroma_weight[i][list][j][0];
  379. sl->pwt.chroma_weight[16 + 2 * i][list][j][1] = sl->pwt.chroma_weight[16 + 2 * i + 1][list][j][1] = sl->pwt.chroma_weight[i][list][j][1];
  380. }
  381. }
  382. }
  383. }
  384. /**
  385. * Mark a picture as no longer needed for reference. The refmask
  386. * argument allows unreferencing of individual fields or the whole frame.
  387. * If the picture becomes entirely unreferenced, but is being held for
  388. * display purposes, it is marked as such.
  389. * @param refmask mask of fields to unreference; the mask is bitwise
  390. * anded with the reference marking of pic
  391. * @return non-zero if pic becomes entirely unreferenced (except possibly
  392. * for display purposes) zero if one of the fields remains in
  393. * reference
  394. */
  395. static inline int unreference_pic(H264Context *h, H264Picture *pic, int refmask)
  396. {
  397. int i;
  398. if (pic->reference &= refmask) {
  399. return 0;
  400. } else {
  401. for(i = 0; h->delayed_pic[i]; i++)
  402. if(pic == h->delayed_pic[i]){
  403. pic->reference = DELAYED_PIC_REF;
  404. break;
  405. }
  406. return 1;
  407. }
  408. }
  409. /**
  410. * Find a H264Picture in the short term reference list by frame number.
  411. * @param frame_num frame number to search for
  412. * @param idx the index into h->short_ref where returned picture is found
  413. * undefined if no picture found.
  414. * @return pointer to the found picture, or NULL if no pic with the provided
  415. * frame number is found
  416. */
  417. static H264Picture *find_short(H264Context *h, int frame_num, int *idx)
  418. {
  419. int i;
  420. for (i = 0; i < h->short_ref_count; i++) {
  421. H264Picture *pic = h->short_ref[i];
  422. if (h->avctx->debug & FF_DEBUG_MMCO)
  423. av_log(h->avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  424. if (pic->frame_num == frame_num) {
  425. *idx = i;
  426. return pic;
  427. }
  428. }
  429. return NULL;
  430. }
  431. /**
  432. * Remove a picture from the short term reference list by its index in
  433. * that list. This does no checking on the provided index; it is assumed
  434. * to be valid. Other list entries are shifted down.
  435. * @param i index into h->short_ref of picture to remove.
  436. */
  437. static void remove_short_at_index(H264Context *h, int i)
  438. {
  439. assert(i >= 0 && i < h->short_ref_count);
  440. h->short_ref[i] = NULL;
  441. if (--h->short_ref_count)
  442. memmove(&h->short_ref[i], &h->short_ref[i + 1],
  443. (h->short_ref_count - i) * sizeof(H264Picture*));
  444. }
  445. /**
  446. * @return the removed picture or NULL if an error occurs
  447. */
  448. static H264Picture *remove_short(H264Context *h, int frame_num, int ref_mask)
  449. {
  450. H264Picture *pic;
  451. int i;
  452. if (h->avctx->debug & FF_DEBUG_MMCO)
  453. av_log(h->avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  454. pic = find_short(h, frame_num, &i);
  455. if (pic) {
  456. if (unreference_pic(h, pic, ref_mask))
  457. remove_short_at_index(h, i);
  458. }
  459. return pic;
  460. }
  461. /**
  462. * Remove a picture from the long term reference list by its index in
  463. * that list.
  464. * @return the removed picture or NULL if an error occurs
  465. */
  466. static H264Picture *remove_long(H264Context *h, int i, int ref_mask)
  467. {
  468. H264Picture *pic;
  469. pic = h->long_ref[i];
  470. if (pic) {
  471. if (unreference_pic(h, pic, ref_mask)) {
  472. assert(h->long_ref[i]->long_ref == 1);
  473. h->long_ref[i]->long_ref = 0;
  474. h->long_ref[i] = NULL;
  475. h->long_ref_count--;
  476. }
  477. }
  478. return pic;
  479. }
  480. void ff_h264_remove_all_refs(H264Context *h)
  481. {
  482. int i;
  483. for (i = 0; i < 16; i++) {
  484. remove_long(h, i, 0);
  485. }
  486. assert(h->long_ref_count == 0);
  487. if (h->short_ref_count && !h->last_pic_for_ec.f->data[0]) {
  488. ff_h264_unref_picture(h, &h->last_pic_for_ec);
  489. if (h->short_ref[0]->f->buf[0])
  490. ff_h264_ref_picture(h, &h->last_pic_for_ec, h->short_ref[0]);
  491. }
  492. for (i = 0; i < h->short_ref_count; i++) {
  493. unreference_pic(h, h->short_ref[i], 0);
  494. h->short_ref[i] = NULL;
  495. }
  496. h->short_ref_count = 0;
  497. memset(h->default_ref, 0, sizeof(h->default_ref));
  498. for (i = 0; i < h->nb_slice_ctx; i++) {
  499. H264SliceContext *sl = &h->slice_ctx[i];
  500. sl->list_count = sl->ref_count[0] = sl->ref_count[1] = 0;
  501. memset(sl->ref_list, 0, sizeof(sl->ref_list));
  502. }
  503. }
  504. static int check_opcodes(MMCO *mmco1, MMCO *mmco2, int n_mmcos)
  505. {
  506. int i;
  507. for (i = 0; i < n_mmcos; i++) {
  508. if (mmco1[i].opcode != mmco2[i].opcode) {
  509. av_log(NULL, AV_LOG_ERROR, "MMCO opcode [%d, %d] at %d mismatches between slices\n",
  510. mmco1[i].opcode, mmco2[i].opcode, i);
  511. return -1;
  512. }
  513. }
  514. return 0;
  515. }
  516. int ff_generate_sliding_window_mmcos(H264Context *h, int first_slice)
  517. {
  518. MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
  519. int mmco_index = 0, i = 0;
  520. if (h->short_ref_count &&
  521. h->long_ref_count + h->short_ref_count >= h->ps.sps->ref_frame_count &&
  522. !(FIELD_PICTURE(h) && !h->first_field && h->cur_pic_ptr->reference)) {
  523. mmco[0].opcode = MMCO_SHORT2UNUSED;
  524. mmco[0].short_pic_num = h->short_ref[h->short_ref_count - 1]->frame_num;
  525. mmco_index = 1;
  526. if (FIELD_PICTURE(h)) {
  527. mmco[0].short_pic_num *= 2;
  528. mmco[1].opcode = MMCO_SHORT2UNUSED;
  529. mmco[1].short_pic_num = mmco[0].short_pic_num + 1;
  530. mmco_index = 2;
  531. }
  532. }
  533. if (first_slice) {
  534. h->mmco_index = mmco_index;
  535. } else if (!first_slice && mmco_index >= 0 &&
  536. (mmco_index != h->mmco_index ||
  537. (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
  538. av_log(h->avctx, AV_LOG_ERROR,
  539. "Inconsistent MMCO state between slices [%d, %d]\n",
  540. mmco_index, h->mmco_index);
  541. return AVERROR_INVALIDDATA;
  542. }
  543. return 0;
  544. }
  545. int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count)
  546. {
  547. int i, av_uninit(j);
  548. int pps_ref_count[2] = {0};
  549. int current_ref_assigned = 0, err = 0;
  550. H264Picture *av_uninit(pic);
  551. if ((h->avctx->debug & FF_DEBUG_MMCO) && mmco_count == 0)
  552. av_log(h->avctx, AV_LOG_DEBUG, "no mmco here\n");
  553. for (i = 0; i < mmco_count; i++) {
  554. int av_uninit(structure), av_uninit(frame_num);
  555. if (h->avctx->debug & FF_DEBUG_MMCO)
  556. av_log(h->avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode,
  557. h->mmco[i].short_pic_num, h->mmco[i].long_arg);
  558. if (mmco[i].opcode == MMCO_SHORT2UNUSED ||
  559. mmco[i].opcode == MMCO_SHORT2LONG) {
  560. frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
  561. pic = find_short(h, frame_num, &j);
  562. if (!pic) {
  563. if (mmco[i].opcode != MMCO_SHORT2LONG ||
  564. !h->long_ref[mmco[i].long_arg] ||
  565. h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
  566. av_log(h->avctx, h->short_ref_count ? AV_LOG_ERROR : AV_LOG_DEBUG, "mmco: unref short failure\n");
  567. err = AVERROR_INVALIDDATA;
  568. }
  569. continue;
  570. }
  571. }
  572. switch (mmco[i].opcode) {
  573. case MMCO_SHORT2UNUSED:
  574. if (h->avctx->debug & FF_DEBUG_MMCO)
  575. av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n",
  576. h->mmco[i].short_pic_num, h->short_ref_count);
  577. remove_short(h, frame_num, structure ^ PICT_FRAME);
  578. break;
  579. case MMCO_SHORT2LONG:
  580. if (h->long_ref[mmco[i].long_arg] != pic)
  581. remove_long(h, mmco[i].long_arg, 0);
  582. remove_short_at_index(h, j);
  583. h->long_ref[ mmco[i].long_arg ] = pic;
  584. if (h->long_ref[mmco[i].long_arg]) {
  585. h->long_ref[mmco[i].long_arg]->long_ref = 1;
  586. h->long_ref_count++;
  587. }
  588. break;
  589. case MMCO_LONG2UNUSED:
  590. j = pic_num_extract(h, mmco[i].long_arg, &structure);
  591. pic = h->long_ref[j];
  592. if (pic) {
  593. remove_long(h, j, structure ^ PICT_FRAME);
  594. } else if (h->avctx->debug & FF_DEBUG_MMCO)
  595. av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
  596. break;
  597. case MMCO_LONG:
  598. // Comment below left from previous code as it is an interesting note.
  599. /* First field in pair is in short term list or
  600. * at a different long term index.
  601. * This is not allowed; see 7.4.3.3, notes 2 and 3.
  602. * Report the problem and keep the pair where it is,
  603. * and mark this field valid.
  604. */
  605. if (h->short_ref[0] == h->cur_pic_ptr) {
  606. av_log(h->avctx, AV_LOG_ERROR, "mmco: cannot assign current picture to short and long at the same time\n");
  607. remove_short_at_index(h, 0);
  608. }
  609. /* make sure the current picture is not already assigned as a long ref */
  610. if (h->cur_pic_ptr->long_ref) {
  611. for (j = 0; j < FF_ARRAY_ELEMS(h->long_ref); j++) {
  612. if (h->long_ref[j] == h->cur_pic_ptr) {
  613. if (j != mmco[i].long_arg)
  614. av_log(h->avctx, AV_LOG_ERROR, "mmco: cannot assign current picture to 2 long term references\n");
  615. remove_long(h, j, 0);
  616. }
  617. }
  618. }
  619. if (h->long_ref[mmco[i].long_arg] != h->cur_pic_ptr) {
  620. av_assert0(!h->cur_pic_ptr->long_ref);
  621. remove_long(h, mmco[i].long_arg, 0);
  622. h->long_ref[mmco[i].long_arg] = h->cur_pic_ptr;
  623. h->long_ref[mmco[i].long_arg]->long_ref = 1;
  624. h->long_ref_count++;
  625. }
  626. h->cur_pic_ptr->reference |= h->picture_structure;
  627. current_ref_assigned = 1;
  628. break;
  629. case MMCO_SET_MAX_LONG:
  630. assert(mmco[i].long_arg <= 16);
  631. // just remove the long term which index is greater than new max
  632. for (j = mmco[i].long_arg; j < 16; j++) {
  633. remove_long(h, j, 0);
  634. }
  635. break;
  636. case MMCO_RESET:
  637. while (h->short_ref_count) {
  638. remove_short(h, h->short_ref[0]->frame_num, 0);
  639. }
  640. for (j = 0; j < 16; j++) {
  641. remove_long(h, j, 0);
  642. }
  643. h->poc.frame_num = h->cur_pic_ptr->frame_num = 0;
  644. h->mmco_reset = 1;
  645. h->cur_pic_ptr->mmco_reset = 1;
  646. for (j = 0; j < MAX_DELAYED_PIC_COUNT; j++)
  647. h->last_pocs[j] = INT_MIN;
  648. break;
  649. default: assert(0);
  650. }
  651. }
  652. if (!current_ref_assigned) {
  653. /* Second field of complementary field pair; the first field of
  654. * which is already referenced. If short referenced, it
  655. * should be first entry in short_ref. If not, it must exist
  656. * in long_ref; trying to put it on the short list here is an
  657. * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
  658. */
  659. if (h->short_ref_count && h->short_ref[0] == h->cur_pic_ptr) {
  660. /* Just mark the second field valid */
  661. h->cur_pic_ptr->reference |= h->picture_structure;
  662. } else if (h->cur_pic_ptr->long_ref) {
  663. av_log(h->avctx, AV_LOG_ERROR, "illegal short term reference "
  664. "assignment for second field "
  665. "in complementary field pair "
  666. "(first field is long term)\n");
  667. err = AVERROR_INVALIDDATA;
  668. } else {
  669. pic = remove_short(h, h->cur_pic_ptr->frame_num, 0);
  670. if (pic) {
  671. av_log(h->avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  672. err = AVERROR_INVALIDDATA;
  673. }
  674. if (h->short_ref_count)
  675. memmove(&h->short_ref[1], &h->short_ref[0],
  676. h->short_ref_count * sizeof(H264Picture*));
  677. h->short_ref[0] = h->cur_pic_ptr;
  678. h->short_ref_count++;
  679. h->cur_pic_ptr->reference |= h->picture_structure;
  680. }
  681. }
  682. if (h->long_ref_count + h->short_ref_count > FFMAX(h->ps.sps->ref_frame_count, 1)) {
  683. /* We have too many reference frames, probably due to corrupted
  684. * stream. Need to discard one frame. Prevents overrun of the
  685. * short_ref and long_ref buffers.
  686. */
  687. av_log(h->avctx, AV_LOG_ERROR,
  688. "number of reference frames (%d+%d) exceeds max (%d; probably "
  689. "corrupt input), discarding one\n",
  690. h->long_ref_count, h->short_ref_count, h->ps.sps->ref_frame_count);
  691. err = AVERROR_INVALIDDATA;
  692. if (h->long_ref_count && !h->short_ref_count) {
  693. for (i = 0; i < 16; ++i)
  694. if (h->long_ref[i])
  695. break;
  696. assert(i < 16);
  697. remove_long(h, i, 0);
  698. } else {
  699. pic = h->short_ref[h->short_ref_count - 1];
  700. remove_short(h, pic->frame_num, 0);
  701. }
  702. }
  703. for (i = 0; i<h->short_ref_count; i++) {
  704. pic = h->short_ref[i];
  705. if (pic->invalid_gap) {
  706. int d = av_mod_uintp2(h->cur_pic_ptr->frame_num - pic->frame_num, h->ps.sps->log2_max_frame_num);
  707. if (d > h->ps.sps->ref_frame_count)
  708. remove_short(h, pic->frame_num, 0);
  709. }
  710. }
  711. print_short_term(h);
  712. print_long_term(h);
  713. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++) {
  714. if (h->ps.pps_list[i]) {
  715. const PPS *pps = (const PPS *)h->ps.pps_list[i]->data;
  716. pps_ref_count[0] = FFMAX(pps_ref_count[0], pps->ref_count[0]);
  717. pps_ref_count[1] = FFMAX(pps_ref_count[1], pps->ref_count[1]);
  718. }
  719. }
  720. if ( err >= 0
  721. && h->long_ref_count==0
  722. && ( h->short_ref_count<=2
  723. || pps_ref_count[0] <= 1 + (h->picture_structure != PICT_FRAME) && pps_ref_count[1] <= 1)
  724. && pps_ref_count[0]<=2 + (h->picture_structure != PICT_FRAME) + (2*!h->has_recovery_point)
  725. && h->cur_pic_ptr->f->pict_type == AV_PICTURE_TYPE_I){
  726. h->cur_pic_ptr->recovered |= 1;
  727. if(!h->avctx->has_b_frames)
  728. h->frame_recovered |= FRAME_RECOVERED_SEI;
  729. }
  730. return (h->avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
  731. }
  732. int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb,
  733. int first_slice)
  734. {
  735. int i, ret;
  736. MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = mmco_temp;
  737. int mmco_index = 0;
  738. if (h->nal_unit_type == NAL_IDR_SLICE) { // FIXME fields
  739. skip_bits1(gb); // broken_link
  740. if (get_bits1(gb)) {
  741. mmco[0].opcode = MMCO_LONG;
  742. mmco[0].long_arg = 0;
  743. mmco_index = 1;
  744. }
  745. } else {
  746. if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
  747. for (i = 0; i < MAX_MMCO_COUNT; i++) {
  748. MMCOOpcode opcode = get_ue_golomb_31(gb);
  749. mmco[i].opcode = opcode;
  750. if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG) {
  751. mmco[i].short_pic_num =
  752. (h->curr_pic_num - get_ue_golomb_long(gb) - 1) &
  753. (h->max_pic_num - 1);
  754. #if 0
  755. if (mmco[i].short_pic_num >= h->short_ref_count ||
  756. !h->short_ref[mmco[i].short_pic_num]) {
  757. av_log(s->avctx, AV_LOG_ERROR,
  758. "illegal short ref in memory management control "
  759. "operation %d\n", mmco);
  760. return -1;
  761. }
  762. #endif
  763. }
  764. if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
  765. opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG) {
  766. unsigned int long_arg = get_ue_golomb_31(gb);
  767. if (long_arg >= 32 ||
  768. (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG &&
  769. long_arg == 16) &&
  770. !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE(h)))) {
  771. av_log(h->avctx, AV_LOG_ERROR,
  772. "illegal long ref in memory management control "
  773. "operation %d\n", opcode);
  774. return -1;
  775. }
  776. mmco[i].long_arg = long_arg;
  777. }
  778. if (opcode > (unsigned) MMCO_LONG) {
  779. av_log(h->avctx, AV_LOG_ERROR,
  780. "illegal memory management control operation %d\n",
  781. opcode);
  782. return -1;
  783. }
  784. if (opcode == MMCO_END)
  785. break;
  786. }
  787. mmco_index = i;
  788. } else {
  789. if (first_slice) {
  790. ret = ff_generate_sliding_window_mmcos(h, first_slice);
  791. if (ret < 0 && h->avctx->err_recognition & AV_EF_EXPLODE)
  792. return ret;
  793. }
  794. mmco_index = -1;
  795. }
  796. }
  797. if (first_slice && mmco_index != -1) {
  798. memcpy(h->mmco, mmco_temp, sizeof(h->mmco));
  799. h->mmco_index = mmco_index;
  800. } else if (!first_slice && mmco_index >= 0 &&
  801. (mmco_index != h->mmco_index ||
  802. check_opcodes(h->mmco, mmco_temp, mmco_index))) {
  803. av_log(h->avctx, AV_LOG_ERROR,
  804. "Inconsistent MMCO state between slices [%d, %d]\n",
  805. mmco_index, h->mmco_index);
  806. return AVERROR_INVALIDDATA;
  807. }
  808. return 0;
  809. }