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.

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