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.

912 lines
33KB

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