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.

703 lines
25KB

  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->data[i] += pic->linesize[i];
  39. pic->reference = parity;
  40. pic->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->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] ]->reference & sel)))
  62. i[0]++;
  63. while(i[1]<len && !(in[ i[1] ] && (in[ i[1] ]->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. assert(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. assert(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].data[0] == h->default_ref_list[1][i].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. assert(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].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].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->reference);
  198. assert(!ref->long_ref);
  199. if(
  200. ref->frame_num == frame_num &&
  201. (ref->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->reference));
  217. if(ref && (ref->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].data[0]){
  251. av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
  252. if(h->default_ref_list[list][0].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<2; list++){ //FIXME try list_count
  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].linesize[j] <<= 1;
  270. field[0].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].data[j] += frame->linesize[j];
  275. field[1].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->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->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. }
  391. /**
  392. * print short term list
  393. */
  394. static void print_short_term(H264Context *h) {
  395. uint32_t i;
  396. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  397. av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
  398. for(i=0; i<h->short_ref_count; i++){
  399. Picture *pic= h->short_ref[i];
  400. av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->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->s.avctx->debug&FF_DEBUG_MMCO) {
  410. av_log(h->s.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->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
  415. }
  416. }
  417. }
  418. }
  419. void ff_generate_sliding_window_mmcos(H264Context *h) {
  420. MpegEncContext * const s = &h->s;
  421. av_assert0(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  422. h->mmco_index= 0;
  423. if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
  424. !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->reference)) {
  425. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  426. h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  427. h->mmco_index= 1;
  428. if (FIELD_PICTURE) {
  429. h->mmco[0].short_pic_num *= 2;
  430. h->mmco[1].opcode= MMCO_SHORT2UNUSED;
  431. h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
  432. h->mmco_index= 2;
  433. }
  434. }
  435. }
  436. int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  437. MpegEncContext * const s = &h->s;
  438. int i, av_uninit(j);
  439. int current_ref_assigned=0;
  440. Picture *av_uninit(pic);
  441. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  442. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  443. for(i=0; i<mmco_count; i++){
  444. int av_uninit(structure), av_uninit(frame_num);
  445. if(s->avctx->debug&FF_DEBUG_MMCO)
  446. 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);
  447. if( mmco[i].opcode == MMCO_SHORT2UNUSED
  448. || mmco[i].opcode == MMCO_SHORT2LONG){
  449. frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
  450. pic = find_short(h, frame_num, &j);
  451. if(!pic){
  452. if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
  453. || h->long_ref[mmco[i].long_arg]->frame_num != frame_num)
  454. av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
  455. continue;
  456. }
  457. }
  458. switch(mmco[i].opcode){
  459. case MMCO_SHORT2UNUSED:
  460. if(s->avctx->debug&FF_DEBUG_MMCO)
  461. 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);
  462. remove_short(h, frame_num, structure ^ PICT_FRAME);
  463. break;
  464. case MMCO_SHORT2LONG:
  465. if (h->long_ref[mmco[i].long_arg] != pic)
  466. remove_long(h, mmco[i].long_arg, 0);
  467. remove_short_at_index(h, j);
  468. h->long_ref[ mmco[i].long_arg ]= pic;
  469. if (h->long_ref[ mmco[i].long_arg ]){
  470. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  471. h->long_ref_count++;
  472. }
  473. break;
  474. case MMCO_LONG2UNUSED:
  475. j = pic_num_extract(h, mmco[i].long_arg, &structure);
  476. pic = h->long_ref[j];
  477. if (pic) {
  478. remove_long(h, j, structure ^ PICT_FRAME);
  479. } else if(s->avctx->debug&FF_DEBUG_MMCO)
  480. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
  481. break;
  482. case MMCO_LONG:
  483. // Comment below left from previous code as it is an interresting note.
  484. /* First field in pair is in short term list or
  485. * at a different long term index.
  486. * This is not allowed; see 7.4.3.3, notes 2 and 3.
  487. * Report the problem and keep the pair where it is,
  488. * and mark this field valid.
  489. */
  490. if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
  491. remove_long(h, mmco[i].long_arg, 0);
  492. h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
  493. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  494. h->long_ref_count++;
  495. }
  496. s->current_picture_ptr->reference |= s->picture_structure;
  497. current_ref_assigned=1;
  498. break;
  499. case MMCO_SET_MAX_LONG:
  500. assert(mmco[i].long_arg <= 16);
  501. // just remove the long term which index is greater than new max
  502. for(j = mmco[i].long_arg; j<16; j++){
  503. remove_long(h, j, 0);
  504. }
  505. break;
  506. case MMCO_RESET:
  507. while(h->short_ref_count){
  508. remove_short(h, h->short_ref[0]->frame_num, 0);
  509. }
  510. for(j = 0; j < 16; j++) {
  511. remove_long(h, j, 0);
  512. }
  513. s->current_picture_ptr->poc=
  514. s->current_picture_ptr->field_poc[0]=
  515. s->current_picture_ptr->field_poc[1]=
  516. h->poc_lsb=
  517. h->poc_msb=
  518. h->frame_num=
  519. s->current_picture_ptr->frame_num= 0;
  520. s->current_picture_ptr->mmco_reset=1;
  521. break;
  522. default: assert(0);
  523. }
  524. }
  525. if (!current_ref_assigned) {
  526. /* Second field of complementary field pair; the first field of
  527. * which is already referenced. If short referenced, it
  528. * should be first entry in short_ref. If not, it must exist
  529. * in long_ref; trying to put it on the short list here is an
  530. * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
  531. */
  532. if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
  533. /* Just mark the second field valid */
  534. s->current_picture_ptr->reference = PICT_FRAME;
  535. } else if (s->current_picture_ptr->long_ref) {
  536. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
  537. "assignment for second field "
  538. "in complementary field pair "
  539. "(first field is long term)\n");
  540. } else {
  541. pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
  542. if(pic){
  543. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  544. }
  545. if(h->short_ref_count)
  546. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  547. h->short_ref[0]= s->current_picture_ptr;
  548. h->short_ref_count++;
  549. s->current_picture_ptr->reference |= s->picture_structure;
  550. }
  551. }
  552. if (h->long_ref_count + h->short_ref_count > FFMAX(h->sps.ref_frame_count, 1)){
  553. /* We have too many reference frames, probably due to corrupted
  554. * stream. Need to discard one frame. Prevents overrun of the
  555. * short_ref and long_ref buffers.
  556. */
  557. av_log(h->s.avctx, AV_LOG_ERROR,
  558. "number of reference frames (%d+%d) exceeds max (%d; probably "
  559. "corrupt input), discarding one\n",
  560. h->long_ref_count, h->short_ref_count, h->sps.ref_frame_count);
  561. if (h->long_ref_count && !h->short_ref_count) {
  562. for (i = 0; i < 16; ++i)
  563. if (h->long_ref[i])
  564. break;
  565. assert(i < 16);
  566. remove_long(h, i, 0);
  567. } else {
  568. pic = h->short_ref[h->short_ref_count - 1];
  569. remove_short(h, pic->frame_num, 0);
  570. }
  571. }
  572. print_short_term(h);
  573. print_long_term(h);
  574. return 0;
  575. }
  576. int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
  577. MpegEncContext * const s = &h->s;
  578. int i;
  579. h->mmco_index= 0;
  580. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  581. s->broken_link= get_bits1(gb) -1;
  582. if(get_bits1(gb)){
  583. h->mmco[0].opcode= MMCO_LONG;
  584. h->mmco[0].long_arg= 0;
  585. h->mmco_index= 1;
  586. }
  587. }else{
  588. if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
  589. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  590. MMCOOpcode opcode= get_ue_golomb_31(gb);
  591. h->mmco[i].opcode= opcode;
  592. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  593. h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
  594. /* if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
  595. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  596. return -1;
  597. }*/
  598. }
  599. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  600. unsigned int long_arg= get_ue_golomb_31(gb);
  601. if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
  602. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  603. return -1;
  604. }
  605. h->mmco[i].long_arg= long_arg;
  606. }
  607. if(opcode > (unsigned)MMCO_LONG){
  608. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  609. return -1;
  610. }
  611. if(opcode == MMCO_END)
  612. break;
  613. }
  614. h->mmco_index= i;
  615. }else{
  616. ff_generate_sliding_window_mmcos(h);
  617. }
  618. }
  619. return 0;
  620. }