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.

816 lines
29KB

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