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.

796 lines
28KB

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