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.

807 lines
28KB

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