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.

907 lines
32KB

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