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.

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