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.

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