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.

790 lines
27KB

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