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.

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