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.

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