/libavcodec/h264_refs.c

http://github.com/FFmpeg/FFmpeg · C · 894 lines · 693 code · 105 blank · 96 comment · 227 complexity · 97ec62259517337d421fce9bed337184 MD5 · raw file

  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 || mismatches_ref(h, ref)) {
  330. av_log(h->avctx, AV_LOG_ERROR,
  331. i < 0 ? "reference picture missing during reorder\n" :
  332. "mismatching reference\n"
  333. );
  334. memset(&sl->ref_list[list][index], 0, sizeof(sl->ref_list[0][0])); // FIXME
  335. } else {
  336. for (i = index; i + 1 < sl->ref_count[list]; i++) {
  337. if (sl->ref_list[list][i].parent &&
  338. ref->long_ref == sl->ref_list[list][i].parent->long_ref &&
  339. ref->pic_id == sl->ref_list[list][i].pic_id)
  340. break;
  341. }
  342. for (; i > index; i--) {
  343. sl->ref_list[list][i] = sl->ref_list[list][i - 1];
  344. }
  345. ref_from_h264pic(&sl->ref_list[list][index], ref);
  346. if (FIELD_PICTURE(h)) {
  347. pic_as_field(&sl->ref_list[list][index], pic_structure);
  348. }
  349. }
  350. }
  351. }
  352. for (list = 0; list < sl->list_count; list++) {
  353. for (index = 0; index < sl->ref_count[list]; index++) {
  354. if ( !sl->ref_list[list][index].parent
  355. || (!FIELD_PICTURE(h) && (sl->ref_list[list][index].reference&3) != 3)) {
  356. int i;
  357. av_log(h->avctx, AV_LOG_ERROR, "Missing reference picture, default is %d\n", h->default_ref[list].poc);
  358. for (i = 0; i < FF_ARRAY_ELEMS(h->last_pocs); i++)
  359. h->last_pocs[i] = INT_MIN;
  360. if (h->default_ref[list].parent
  361. && !(!FIELD_PICTURE(h) && (h->default_ref[list].reference&3) != 3))
  362. sl->ref_list[list][index] = h->default_ref[list];
  363. else
  364. return -1;
  365. }
  366. av_assert0(av_buffer_get_ref_count(sl->ref_list[list][index].parent->f->buf[0]) > 0);
  367. }
  368. }
  369. if (FRAME_MBAFF(h))
  370. h264_fill_mbaff_ref_list(sl);
  371. return 0;
  372. }
  373. int ff_h264_decode_ref_pic_list_reordering(H264SliceContext *sl, void *logctx)
  374. {
  375. int list, index;
  376. sl->nb_ref_modifications[0] = 0;
  377. sl->nb_ref_modifications[1] = 0;
  378. for (list = 0; list < sl->list_count; list++) {
  379. if (!get_bits1(&sl->gb)) // ref_pic_list_modification_flag_l[01]
  380. continue;
  381. for (index = 0; ; index++) {
  382. unsigned int op = get_ue_golomb_31(&sl->gb);
  383. if (op == 3)
  384. break;
  385. if (index >= sl->ref_count[list]) {
  386. av_log(logctx, AV_LOG_ERROR, "reference count overflow\n");
  387. return AVERROR_INVALIDDATA;
  388. } else if (op > 2) {
  389. av_log(logctx, AV_LOG_ERROR,
  390. "illegal modification_of_pic_nums_idc %u\n",
  391. op);
  392. return AVERROR_INVALIDDATA;
  393. }
  394. sl->ref_modifications[list][index].val = get_ue_golomb_long(&sl->gb);
  395. sl->ref_modifications[list][index].op = op;
  396. sl->nb_ref_modifications[list]++;
  397. }
  398. }
  399. return 0;
  400. }
  401. /**
  402. * Mark a picture as no longer needed for reference. The refmask
  403. * argument allows unreferencing of individual fields or the whole frame.
  404. * If the picture becomes entirely unreferenced, but is being held for
  405. * display purposes, it is marked as such.
  406. * @param refmask mask of fields to unreference; the mask is bitwise
  407. * anded with the reference marking of pic
  408. * @return non-zero if pic becomes entirely unreferenced (except possibly
  409. * for display purposes) zero if one of the fields remains in
  410. * reference
  411. */
  412. static inline int unreference_pic(H264Context *h, H264Picture *pic, int refmask)
  413. {
  414. int i;
  415. if (pic->reference &= refmask) {
  416. return 0;
  417. } else {
  418. for(i = 0; h->delayed_pic[i]; i++)
  419. if(pic == h->delayed_pic[i]){
  420. pic->reference = DELAYED_PIC_REF;
  421. break;
  422. }
  423. return 1;
  424. }
  425. }
  426. /**
  427. * Find a H264Picture in the short term reference list by frame number.
  428. * @param frame_num frame number to search for
  429. * @param idx the index into h->short_ref where returned picture is found
  430. * undefined if no picture found.
  431. * @return pointer to the found picture, or NULL if no pic with the provided
  432. * frame number is found
  433. */
  434. static H264Picture *find_short(H264Context *h, int frame_num, int *idx)
  435. {
  436. int i;
  437. for (i = 0; i < h->short_ref_count; i++) {
  438. H264Picture *pic = h->short_ref[i];
  439. if (h->avctx->debug & FF_DEBUG_MMCO)
  440. av_log(h->avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  441. if (pic->frame_num == frame_num) {
  442. *idx = i;
  443. return pic;
  444. }
  445. }
  446. return NULL;
  447. }
  448. /**
  449. * Remove a picture from the short term reference list by its index in
  450. * that list. This does no checking on the provided index; it is assumed
  451. * to be valid. Other list entries are shifted down.
  452. * @param i index into h->short_ref of picture to remove.
  453. */
  454. static void remove_short_at_index(H264Context *h, int i)
  455. {
  456. assert(i >= 0 && i < h->short_ref_count);
  457. h->short_ref[i] = NULL;
  458. if (--h->short_ref_count)
  459. memmove(&h->short_ref[i], &h->short_ref[i + 1],
  460. (h->short_ref_count - i) * sizeof(H264Picture*));
  461. }
  462. /**
  463. * @return the removed picture or NULL if an error occurs
  464. */
  465. static H264Picture *remove_short(H264Context *h, int frame_num, int ref_mask)
  466. {
  467. H264Picture *pic;
  468. int i;
  469. if (h->avctx->debug & FF_DEBUG_MMCO)
  470. av_log(h->avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  471. pic = find_short(h, frame_num, &i);
  472. if (pic) {
  473. if (unreference_pic(h, pic, ref_mask))
  474. remove_short_at_index(h, i);
  475. }
  476. return pic;
  477. }
  478. /**
  479. * Remove a picture from the long term reference list by its index in
  480. * that list.
  481. * @return the removed picture or NULL if an error occurs
  482. */
  483. static H264Picture *remove_long(H264Context *h, int i, int ref_mask)
  484. {
  485. H264Picture *pic;
  486. pic = h->long_ref[i];
  487. if (pic) {
  488. if (unreference_pic(h, pic, ref_mask)) {
  489. assert(h->long_ref[i]->long_ref == 1);
  490. h->long_ref[i]->long_ref = 0;
  491. h->long_ref[i] = NULL;
  492. h->long_ref_count--;
  493. }
  494. }
  495. return pic;
  496. }
  497. void ff_h264_remove_all_refs(H264Context *h)
  498. {
  499. int i;
  500. for (i = 0; i < 16; i++) {
  501. remove_long(h, i, 0);
  502. }
  503. assert(h->long_ref_count == 0);
  504. if (h->short_ref_count && !h->last_pic_for_ec.f->data[0]) {
  505. ff_h264_unref_picture(h, &h->last_pic_for_ec);
  506. ff_h264_ref_picture(h, &h->last_pic_for_ec, h->short_ref[0]);
  507. }
  508. for (i = 0; i < h->short_ref_count; i++) {
  509. unreference_pic(h, h->short_ref[i], 0);
  510. h->short_ref[i] = NULL;
  511. }
  512. h->short_ref_count = 0;
  513. memset(h->default_ref, 0, sizeof(h->default_ref));
  514. }
  515. static void generate_sliding_window_mmcos(H264Context *h)
  516. {
  517. MMCO *mmco = h->mmco;
  518. int nb_mmco = 0;
  519. if (h->short_ref_count &&
  520. h->long_ref_count + h->short_ref_count >= h->ps.sps->ref_frame_count &&
  521. !(FIELD_PICTURE(h) && !h->first_field && h->cur_pic_ptr->reference)) {
  522. mmco[0].opcode = MMCO_SHORT2UNUSED;
  523. mmco[0].short_pic_num = h->short_ref[h->short_ref_count - 1]->frame_num;
  524. nb_mmco = 1;
  525. if (FIELD_PICTURE(h)) {
  526. mmco[0].short_pic_num *= 2;
  527. mmco[1].opcode = MMCO_SHORT2UNUSED;
  528. mmco[1].short_pic_num = mmco[0].short_pic_num + 1;
  529. nb_mmco = 2;
  530. }
  531. }
  532. h->nb_mmco = nb_mmco;
  533. }
  534. int ff_h264_execute_ref_pic_marking(H264Context *h)
  535. {
  536. MMCO *mmco = h->mmco;
  537. int mmco_count;
  538. int i, av_uninit(j);
  539. int pps_ref_count[2] = {0};
  540. int current_ref_assigned = 0, err = 0;
  541. H264Picture *av_uninit(pic);
  542. if (!h->ps.sps) {
  543. av_log(h->avctx, AV_LOG_ERROR, "SPS is unset\n");
  544. err = AVERROR_INVALIDDATA;
  545. goto out;
  546. }
  547. if (!h->explicit_ref_marking)
  548. generate_sliding_window_mmcos(h);
  549. mmco_count = h->nb_mmco;
  550. if ((h->avctx->debug & FF_DEBUG_MMCO) && mmco_count == 0)
  551. av_log(h->avctx, AV_LOG_DEBUG, "no mmco here\n");
  552. for (i = 0; i < mmco_count; i++) {
  553. int av_uninit(structure), av_uninit(frame_num);
  554. if (h->avctx->debug & FF_DEBUG_MMCO)
  555. av_log(h->avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode,
  556. h->mmco[i].short_pic_num, h->mmco[i].long_arg);
  557. if (mmco[i].opcode == MMCO_SHORT2UNUSED ||
  558. mmco[i].opcode == MMCO_SHORT2LONG) {
  559. frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
  560. pic = find_short(h, frame_num, &j);
  561. if (!pic) {
  562. if (mmco[i].opcode != MMCO_SHORT2LONG ||
  563. !h->long_ref[mmco[i].long_arg] ||
  564. h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
  565. av_log(h->avctx, h->short_ref_count ? AV_LOG_ERROR : AV_LOG_DEBUG, "mmco: unref short failure\n");
  566. err = AVERROR_INVALIDDATA;
  567. }
  568. continue;
  569. }
  570. }
  571. switch (mmco[i].opcode) {
  572. case MMCO_SHORT2UNUSED:
  573. if (h->avctx->debug & FF_DEBUG_MMCO)
  574. av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n",
  575. h->mmco[i].short_pic_num, h->short_ref_count);
  576. remove_short(h, frame_num, structure ^ PICT_FRAME);
  577. break;
  578. case MMCO_SHORT2LONG:
  579. if (h->long_ref[mmco[i].long_arg] != pic)
  580. remove_long(h, mmco[i].long_arg, 0);
  581. remove_short_at_index(h, j);
  582. h->long_ref[ mmco[i].long_arg ] = pic;
  583. if (h->long_ref[mmco[i].long_arg]) {
  584. h->long_ref[mmco[i].long_arg]->long_ref = 1;
  585. h->long_ref_count++;
  586. }
  587. break;
  588. case MMCO_LONG2UNUSED:
  589. j = pic_num_extract(h, mmco[i].long_arg, &structure);
  590. pic = h->long_ref[j];
  591. if (pic) {
  592. remove_long(h, j, structure ^ PICT_FRAME);
  593. } else if (h->avctx->debug & FF_DEBUG_MMCO)
  594. av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
  595. break;
  596. case MMCO_LONG:
  597. // Comment below left from previous code as it is an interesting note.
  598. /* First field in pair is in short term list or
  599. * at a different long term index.
  600. * This is not allowed; see 7.4.3.3, notes 2 and 3.
  601. * Report the problem and keep the pair where it is,
  602. * and mark this field valid.
  603. */
  604. if (h->short_ref[0] == h->cur_pic_ptr) {
  605. av_log(h->avctx, AV_LOG_ERROR, "mmco: cannot assign current picture to short and long at the same time\n");
  606. remove_short_at_index(h, 0);
  607. }
  608. /* make sure the current picture is not already assigned as a long ref */
  609. if (h->cur_pic_ptr->long_ref) {
  610. for (j = 0; j < FF_ARRAY_ELEMS(h->long_ref); j++) {
  611. if (h->long_ref[j] == h->cur_pic_ptr) {
  612. if (j != mmco[i].long_arg)
  613. av_log(h->avctx, AV_LOG_ERROR, "mmco: cannot assign current picture to 2 long term references\n");
  614. remove_long(h, j, 0);
  615. }
  616. }
  617. }
  618. if (h->long_ref[mmco[i].long_arg] != h->cur_pic_ptr) {
  619. av_assert0(!h->cur_pic_ptr->long_ref);
  620. remove_long(h, mmco[i].long_arg, 0);
  621. h->long_ref[mmco[i].long_arg] = h->cur_pic_ptr;
  622. h->long_ref[mmco[i].long_arg]->long_ref = 1;
  623. h->long_ref_count++;
  624. }
  625. h->cur_pic_ptr->reference |= h->picture_structure;
  626. current_ref_assigned = 1;
  627. break;
  628. case MMCO_SET_MAX_LONG:
  629. assert(mmco[i].long_arg <= 16);
  630. // just remove the long term which index is greater than new max
  631. for (j = mmco[i].long_arg; j < 16; j++) {
  632. remove_long(h, j, 0);
  633. }
  634. break;
  635. case MMCO_RESET:
  636. while (h->short_ref_count) {
  637. remove_short(h, h->short_ref[0]->frame_num, 0);
  638. }
  639. for (j = 0; j < 16; j++) {
  640. remove_long(h, j, 0);
  641. }
  642. h->poc.frame_num = h->cur_pic_ptr->frame_num = 0;
  643. h->mmco_reset = 1;
  644. h->cur_pic_ptr->mmco_reset = 1;
  645. for (j = 0; j < MAX_DELAYED_PIC_COUNT; j++)
  646. h->last_pocs[j] = INT_MIN;
  647. break;
  648. default: av_assert0(0);
  649. }
  650. }
  651. if (!current_ref_assigned) {
  652. /* Second field of complementary field pair; the first field of
  653. * which is already referenced. If short referenced, it
  654. * should be first entry in short_ref. If not, it must exist
  655. * in long_ref; trying to put it on the short list here is an
  656. * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
  657. */
  658. if (h->short_ref_count && h->short_ref[0] == h->cur_pic_ptr) {
  659. /* Just mark the second field valid */
  660. h->cur_pic_ptr->reference |= h->picture_structure;
  661. } else if (h->cur_pic_ptr->long_ref) {
  662. av_log(h->avctx, AV_LOG_ERROR, "illegal short term reference "
  663. "assignment for second field "
  664. "in complementary field pair "
  665. "(first field is long term)\n");
  666. err = AVERROR_INVALIDDATA;
  667. } else {
  668. pic = remove_short(h, h->cur_pic_ptr->frame_num, 0);
  669. if (pic) {
  670. av_log(h->avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  671. err = AVERROR_INVALIDDATA;
  672. }
  673. if (h->short_ref_count)
  674. memmove(&h->short_ref[1], &h->short_ref[0],
  675. h->short_ref_count * sizeof(H264Picture*));
  676. h->short_ref[0] = h->cur_pic_ptr;
  677. h->short_ref_count++;
  678. h->cur_pic_ptr->reference |= h->picture_structure;
  679. }
  680. }
  681. if (h->long_ref_count + h->short_ref_count > FFMAX(h->ps.sps->ref_frame_count, 1)) {
  682. /* We have too many reference frames, probably due to corrupted
  683. * stream. Need to discard one frame. Prevents overrun of the
  684. * short_ref and long_ref buffers.
  685. */
  686. av_log(h->avctx, AV_LOG_ERROR,
  687. "number of reference frames (%d+%d) exceeds max (%d; probably "
  688. "corrupt input), discarding one\n",
  689. h->long_ref_count, h->short_ref_count, h->ps.sps->ref_frame_count);
  690. err = AVERROR_INVALIDDATA;
  691. if (h->long_ref_count && !h->short_ref_count) {
  692. for (i = 0; i < 16; ++i)
  693. if (h->long_ref[i])
  694. break;
  695. assert(i < 16);
  696. remove_long(h, i, 0);
  697. } else {
  698. pic = h->short_ref[h->short_ref_count - 1];
  699. remove_short(h, pic->frame_num, 0);
  700. }
  701. }
  702. for (i = 0; i<h->short_ref_count; i++) {
  703. pic = h->short_ref[i];
  704. if (pic->invalid_gap) {
  705. int d = av_mod_uintp2(h->cur_pic_ptr->frame_num - pic->frame_num, h->ps.sps->log2_max_frame_num);
  706. if (d > h->ps.sps->ref_frame_count)
  707. remove_short(h, pic->frame_num, 0);
  708. }
  709. }
  710. print_short_term(h);
  711. print_long_term(h);
  712. for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++) {
  713. if (h->ps.pps_list[i]) {
  714. const PPS *pps = (const PPS *)h->ps.pps_list[i]->data;
  715. pps_ref_count[0] = FFMAX(pps_ref_count[0], pps->ref_count[0]);
  716. pps_ref_count[1] = FFMAX(pps_ref_count[1], pps->ref_count[1]);
  717. }
  718. }
  719. // Detect unmarked random access points
  720. if ( err >= 0
  721. && h->long_ref_count==0
  722. && ( h->short_ref_count<=2
  723. || pps_ref_count[0] <= 2 && pps_ref_count[1] <= 1 && h->avctx->has_b_frames
  724. || pps_ref_count[0] <= 1 + (h->picture_structure != PICT_FRAME) && pps_ref_count[1] <= 1)
  725. && pps_ref_count[0]<=2 + (h->picture_structure != PICT_FRAME) + (2*!h->has_recovery_point)
  726. && h->cur_pic_ptr->f->pict_type == AV_PICTURE_TYPE_I){
  727. h->cur_pic_ptr->recovered |= 1;
  728. if(!h->avctx->has_b_frames)
  729. h->frame_recovered |= FRAME_RECOVERED_SEI;
  730. }
  731. out:
  732. return (h->avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
  733. }
  734. int ff_h264_decode_ref_pic_marking(H264SliceContext *sl, GetBitContext *gb,
  735. const H2645NAL *nal, void *logctx)
  736. {
  737. int i;
  738. MMCO *mmco = sl->mmco;
  739. int nb_mmco = 0;
  740. if (nal->type == H264_NAL_IDR_SLICE) { // FIXME fields
  741. skip_bits1(gb); // broken_link
  742. if (get_bits1(gb)) {
  743. mmco[0].opcode = MMCO_LONG;
  744. mmco[0].long_arg = 0;
  745. nb_mmco = 1;
  746. }
  747. sl->explicit_ref_marking = 1;
  748. } else {
  749. sl->explicit_ref_marking = get_bits1(gb);
  750. if (sl->explicit_ref_marking) {
  751. for (i = 0; i < MAX_MMCO_COUNT; i++) {
  752. MMCOOpcode opcode = get_ue_golomb_31(gb);
  753. mmco[i].opcode = opcode;
  754. if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG) {
  755. mmco[i].short_pic_num =
  756. (sl->curr_pic_num - get_ue_golomb_long(gb) - 1) &
  757. (sl->max_pic_num - 1);
  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. sl->nb_mmco = i;
  770. return -1;
  771. }
  772. mmco[i].long_arg = long_arg;
  773. }
  774. if (opcode > (unsigned) MMCO_LONG) {
  775. av_log(logctx, AV_LOG_ERROR,
  776. "illegal memory management control operation %d\n",
  777. opcode);
  778. sl->nb_mmco = i;
  779. return -1;
  780. }
  781. if (opcode == MMCO_END)
  782. break;
  783. }
  784. nb_mmco = i;
  785. }
  786. }
  787. sl->nb_mmco = nb_mmco;
  788. return 0;
  789. }