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.

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