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.

706 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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "internal.h"
  27. #include "dsputil.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. MpegEncContext * const s = &h->s;
  95. int i, len;
  96. if(h->slice_type_nos==AV_PICTURE_TYPE_B){
  97. Picture *sorted[32];
  98. int cur_poc, list;
  99. int lens[2];
  100. if(FIELD_PICTURE)
  101. cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
  102. else
  103. cur_poc= s->current_picture_ptr->poc;
  104. for(list= 0; list<2; list++){
  105. len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
  106. len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
  107. assert(len<=32);
  108. len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure);
  109. len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
  110. assert(len<=32);
  111. if(len < h->ref_count[list])
  112. memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
  113. lens[list]= len;
  114. }
  115. if(lens[0] == lens[1] && lens[1] > 1){
  116. 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++);
  117. if(i == lens[0])
  118. FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
  119. }
  120. }else{
  121. len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure);
  122. len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure);
  123. assert(len <= 32);
  124. if(len < h->ref_count[0])
  125. memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
  126. }
  127. #ifdef TRACE
  128. for (i=0; i<h->ref_count[0]; i++) {
  129. 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]);
  130. }
  131. if(h->slice_type_nos==AV_PICTURE_TYPE_B){
  132. for (i=0; i<h->ref_count[1]; i++) {
  133. 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]);
  134. }
  135. }
  136. #endif
  137. return 0;
  138. }
  139. static void print_short_term(H264Context *h);
  140. static void print_long_term(H264Context *h);
  141. /**
  142. * Extract structure information about the picture described by pic_num in
  143. * the current decoding context (frame or field). Note that pic_num is
  144. * picture number without wrapping (so, 0<=pic_num<max_pic_num).
  145. * @param pic_num picture number for which to extract structure information
  146. * @param structure one of PICT_XXX describing structure of picture
  147. * with pic_num
  148. * @return frame number (short term) or long term index of picture
  149. * described by pic_num
  150. */
  151. static int pic_num_extract(H264Context *h, int pic_num, int *structure){
  152. MpegEncContext * const s = &h->s;
  153. *structure = s->picture_structure;
  154. if(FIELD_PICTURE){
  155. if (!(pic_num & 1))
  156. /* opposite field */
  157. *structure ^= PICT_FRAME;
  158. pic_num >>= 1;
  159. }
  160. return pic_num;
  161. }
  162. int ff_h264_decode_ref_pic_list_reordering(H264Context *h){
  163. MpegEncContext * const s = &h->s;
  164. int list, index, pic_structure;
  165. print_short_term(h);
  166. print_long_term(h);
  167. for(list=0; list<h->list_count; list++){
  168. memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
  169. if(get_bits1(&s->gb)){
  170. int pred= h->curr_pic_num;
  171. for(index=0; ; index++){
  172. unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
  173. unsigned int pic_id;
  174. int i;
  175. Picture *ref = NULL;
  176. if(reordering_of_pic_nums_idc==3)
  177. break;
  178. if(index >= h->ref_count[list]){
  179. av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
  180. return -1;
  181. }
  182. if(reordering_of_pic_nums_idc<3){
  183. if(reordering_of_pic_nums_idc<2){
  184. const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
  185. int frame_num;
  186. if(abs_diff_pic_num > h->max_pic_num){
  187. av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  188. return -1;
  189. }
  190. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  191. else pred+= abs_diff_pic_num;
  192. pred &= h->max_pic_num - 1;
  193. frame_num = pic_num_extract(h, pred, &pic_structure);
  194. for(i= h->short_ref_count-1; i>=0; i--){
  195. ref = h->short_ref[i];
  196. assert(ref->f.reference);
  197. assert(!ref->long_ref);
  198. if(
  199. ref->frame_num == frame_num &&
  200. (ref->f.reference & pic_structure)
  201. )
  202. break;
  203. }
  204. if(i>=0)
  205. ref->pic_id= pred;
  206. }else{
  207. int long_idx;
  208. pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
  209. long_idx= pic_num_extract(h, pic_id, &pic_structure);
  210. if(long_idx>31){
  211. av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
  212. return -1;
  213. }
  214. ref = h->long_ref[long_idx];
  215. assert(!(ref && !ref->f.reference));
  216. if (ref && (ref->f.reference & pic_structure)) {
  217. ref->pic_id= pic_id;
  218. assert(ref->long_ref);
  219. i=0;
  220. }else{
  221. i=-1;
  222. }
  223. }
  224. if (i < 0) {
  225. av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  226. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  227. } else {
  228. for(i=index; i+1<h->ref_count[list]; i++){
  229. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  230. break;
  231. }
  232. for(; i > index; i--){
  233. h->ref_list[list][i]= h->ref_list[list][i-1];
  234. }
  235. h->ref_list[list][index]= *ref;
  236. if (FIELD_PICTURE){
  237. pic_as_field(&h->ref_list[list][index], pic_structure);
  238. }
  239. }
  240. }else{
  241. av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  242. return -1;
  243. }
  244. }
  245. }
  246. }
  247. for(list=0; list<h->list_count; list++){
  248. for(index= 0; index < h->ref_count[list]; index++){
  249. if (!h->ref_list[list][index].f.data[0]) {
  250. av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
  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<2; list++){ //FIXME try list_count
  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. MpegEncContext * const s = &h->s;
  319. int i;
  320. for(i=0; i<h->short_ref_count; i++){
  321. Picture *pic= h->short_ref[i];
  322. if(s->avctx->debug&FF_DEBUG_MMCO)
  323. av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  324. if(pic->frame_num == frame_num) {
  325. *idx = i;
  326. return pic;
  327. }
  328. }
  329. return NULL;
  330. }
  331. /**
  332. * Remove a picture from the short term reference list by its index in
  333. * that list. This does no checking on the provided index; it is assumed
  334. * to be valid. Other list entries are shifted down.
  335. * @param i index into h->short_ref of picture to remove.
  336. */
  337. static void remove_short_at_index(H264Context *h, int i){
  338. assert(i >= 0 && i < h->short_ref_count);
  339. h->short_ref[i]= NULL;
  340. if (--h->short_ref_count)
  341. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
  342. }
  343. /**
  344. *
  345. * @return the removed picture or NULL if an error occurs
  346. */
  347. static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
  348. MpegEncContext * const s = &h->s;
  349. Picture *pic;
  350. int i;
  351. if(s->avctx->debug&FF_DEBUG_MMCO)
  352. av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  353. pic = find_short(h, frame_num, &i);
  354. if (pic){
  355. if(unreference_pic(h, pic, ref_mask))
  356. remove_short_at_index(h, i);
  357. }
  358. return pic;
  359. }
  360. /**
  361. * Remove a picture from the long term reference list by its index in
  362. * that list.
  363. * @return the removed picture or NULL if an error occurs
  364. */
  365. static Picture * remove_long(H264Context *h, int i, int ref_mask){
  366. Picture *pic;
  367. pic= h->long_ref[i];
  368. if (pic){
  369. if(unreference_pic(h, pic, ref_mask)){
  370. assert(h->long_ref[i]->long_ref == 1);
  371. h->long_ref[i]->long_ref= 0;
  372. h->long_ref[i]= NULL;
  373. h->long_ref_count--;
  374. }
  375. }
  376. return pic;
  377. }
  378. void ff_h264_remove_all_refs(H264Context *h){
  379. int i;
  380. for(i=0; i<16; i++){
  381. remove_long(h, i, 0);
  382. }
  383. assert(h->long_ref_count==0);
  384. for(i=0; i<h->short_ref_count; i++){
  385. unreference_pic(h, h->short_ref[i], 0);
  386. h->short_ref[i]= NULL;
  387. }
  388. h->short_ref_count=0;
  389. }
  390. /**
  391. * print short term list
  392. */
  393. static void print_short_term(H264Context *h) {
  394. uint32_t i;
  395. if(h->s.avctx->debug&FF_DEBUG_MMCO) {
  396. av_log(h->s.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->s.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->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",
  415. i, pic->frame_num, pic->poc, pic->f.data[0]);
  416. }
  417. }
  418. }
  419. }
  420. void ff_generate_sliding_window_mmcos(H264Context *h) {
  421. MpegEncContext * const s = &h->s;
  422. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  423. h->mmco_index= 0;
  424. if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
  425. !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->f.reference)) {
  426. h->mmco[0].opcode= MMCO_SHORT2UNUSED;
  427. h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
  428. h->mmco_index= 1;
  429. if (FIELD_PICTURE) {
  430. h->mmco[0].short_pic_num *= 2;
  431. h->mmco[1].opcode= MMCO_SHORT2UNUSED;
  432. h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
  433. h->mmco_index= 2;
  434. }
  435. }
  436. }
  437. int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  438. MpegEncContext * const s = &h->s;
  439. int i, av_uninit(j);
  440. int current_ref_assigned=0, err=0;
  441. Picture *av_uninit(pic);
  442. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  443. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  444. for(i=0; i<mmco_count; i++){
  445. int av_uninit(structure), av_uninit(frame_num);
  446. if(s->avctx->debug&FF_DEBUG_MMCO)
  447. 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);
  448. if( mmco[i].opcode == MMCO_SHORT2UNUSED
  449. || mmco[i].opcode == MMCO_SHORT2LONG){
  450. frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
  451. pic = find_short(h, frame_num, &j);
  452. if(!pic){
  453. if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
  454. || h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
  455. av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
  456. err = AVERROR_INVALIDDATA;
  457. }
  458. continue;
  459. }
  460. }
  461. switch(mmco[i].opcode){
  462. case MMCO_SHORT2UNUSED:
  463. if(s->avctx->debug&FF_DEBUG_MMCO)
  464. 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);
  465. remove_short(h, frame_num, structure ^ PICT_FRAME);
  466. break;
  467. case MMCO_SHORT2LONG:
  468. if (h->long_ref[mmco[i].long_arg] != pic)
  469. remove_long(h, mmco[i].long_arg, 0);
  470. remove_short_at_index(h, j);
  471. h->long_ref[ mmco[i].long_arg ]= pic;
  472. if (h->long_ref[ mmco[i].long_arg ]){
  473. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  474. h->long_ref_count++;
  475. }
  476. break;
  477. case MMCO_LONG2UNUSED:
  478. j = pic_num_extract(h, mmco[i].long_arg, &structure);
  479. pic = h->long_ref[j];
  480. if (pic) {
  481. remove_long(h, j, structure ^ PICT_FRAME);
  482. } else if(s->avctx->debug&FF_DEBUG_MMCO)
  483. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
  484. break;
  485. case MMCO_LONG:
  486. // Comment below left from previous code as it is an interresting note.
  487. /* First field in pair is in short term list or
  488. * at a different long term index.
  489. * This is not allowed; see 7.4.3.3, notes 2 and 3.
  490. * Report the problem and keep the pair where it is,
  491. * and mark this field valid.
  492. */
  493. if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
  494. remove_long(h, mmco[i].long_arg, 0);
  495. h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
  496. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  497. h->long_ref_count++;
  498. }
  499. s->current_picture_ptr->f.reference |= s->picture_structure;
  500. current_ref_assigned=1;
  501. break;
  502. case MMCO_SET_MAX_LONG:
  503. assert(mmco[i].long_arg <= 16);
  504. // just remove the long term which index is greater than new max
  505. for(j = mmco[i].long_arg; j<16; j++){
  506. remove_long(h, j, 0);
  507. }
  508. break;
  509. case MMCO_RESET:
  510. while(h->short_ref_count){
  511. remove_short(h, h->short_ref[0]->frame_num, 0);
  512. }
  513. for(j = 0; j < 16; j++) {
  514. remove_long(h, j, 0);
  515. }
  516. h->frame_num=
  517. s->current_picture_ptr->frame_num= 0;
  518. h->mmco_reset = 1;
  519. s->current_picture_ptr->mmco_reset=1;
  520. break;
  521. default: assert(0);
  522. }
  523. }
  524. if (!current_ref_assigned) {
  525. /* Second field of complementary field pair; the first field of
  526. * which is already referenced. If short referenced, it
  527. * should be first entry in short_ref. If not, it must exist
  528. * in long_ref; trying to put it on the short list here is an
  529. * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
  530. */
  531. if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
  532. /* Just mark the second field valid */
  533. s->current_picture_ptr->f.reference = PICT_FRAME;
  534. } else if (s->current_picture_ptr->long_ref) {
  535. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
  536. "assignment for second field "
  537. "in complementary field pair "
  538. "(first field is long term)\n");
  539. err = AVERROR_INVALIDDATA;
  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. err = AVERROR_INVALIDDATA;
  545. }
  546. if(h->short_ref_count)
  547. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  548. h->short_ref[0]= s->current_picture_ptr;
  549. h->short_ref_count++;
  550. s->current_picture_ptr->f.reference |= s->picture_structure;
  551. }
  552. }
  553. if (h->long_ref_count + h->short_ref_count -
  554. (h->short_ref[0] == s->current_picture_ptr) > h->sps.ref_frame_count){
  555. /* We have too many reference frames, probably due to corrupted
  556. * stream. Need to discard one frame. Prevents overrun of the
  557. * short_ref and long_ref buffers.
  558. */
  559. av_log(h->s.avctx, AV_LOG_ERROR,
  560. "number of reference frames (%d+%d) exceeds max (%d; probably "
  561. "corrupt input), discarding one\n",
  562. h->long_ref_count, h->short_ref_count, h->sps.ref_frame_count);
  563. err = AVERROR_INVALIDDATA;
  564. if (h->long_ref_count && !h->short_ref_count) {
  565. for (i = 0; i < 16; ++i)
  566. if (h->long_ref[i])
  567. break;
  568. assert(i < 16);
  569. remove_long(h, i, 0);
  570. } else {
  571. pic = h->short_ref[h->short_ref_count - 1];
  572. remove_short(h, pic->frame_num, 0);
  573. }
  574. }
  575. print_short_term(h);
  576. print_long_term(h);
  577. return (h->s.avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
  578. }
  579. int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
  580. MpegEncContext * const s = &h->s;
  581. int i;
  582. h->mmco_index= 0;
  583. if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
  584. s->broken_link= get_bits1(gb) -1;
  585. if(get_bits1(gb)){
  586. h->mmco[0].opcode= MMCO_LONG;
  587. h->mmco[0].long_arg= 0;
  588. h->mmco_index= 1;
  589. }
  590. }else{
  591. if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
  592. for(i= 0; i<MAX_MMCO_COUNT; i++) {
  593. MMCOOpcode opcode= get_ue_golomb_31(gb);
  594. h->mmco[i].opcode= opcode;
  595. if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
  596. h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
  597. /* if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
  598. av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
  599. return -1;
  600. }*/
  601. }
  602. if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
  603. unsigned int long_arg= get_ue_golomb_31(gb);
  604. if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG && long_arg == 16) && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
  605. av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
  606. return -1;
  607. }
  608. h->mmco[i].long_arg= long_arg;
  609. }
  610. if(opcode > (unsigned)MMCO_LONG){
  611. av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
  612. return -1;
  613. }
  614. if(opcode == MMCO_END)
  615. break;
  616. }
  617. h->mmco_index= i;
  618. }else{
  619. ff_generate_sliding_window_mmcos(h);
  620. }
  621. }
  622. return 0;
  623. }