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.

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