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.

710 lines
26KB

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