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.

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