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.

876 lines
31KB

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