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.

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