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.

831 lines
29KB

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