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.

881 lines
32KB

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