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.

892 lines
31KB

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