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.

769 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 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].f.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].f.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. 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. return -1;
  426. }
  427. return 0;
  428. }
  429. int ff_generate_sliding_window_mmcos(H264Context *h, int first_slice)
  430. {
  431. MpegEncContext * const s = &h->s;
  432. MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
  433. int mmco_index = 0, i;
  434. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  435. if (h->short_ref_count &&
  436. h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
  437. !(FIELD_PICTURE && !s->first_field &&
  438. s->current_picture_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->s.avctx, AV_LOG_ERROR,
  455. "Inconsistent MMCO state between slices [%d, %d, %d]\n",
  456. mmco_index, h->mmco_index, i);
  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. MpegEncContext * const s = &h->s;
  463. int i, av_uninit(j);
  464. int current_ref_assigned=0, err=0;
  465. Picture *av_uninit(pic);
  466. if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  467. av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
  468. for(i=0; i<mmco_count; i++){
  469. int av_uninit(structure), av_uninit(frame_num);
  470. if(s->avctx->debug&FF_DEBUG_MMCO)
  471. 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);
  472. if( mmco[i].opcode == MMCO_SHORT2UNUSED
  473. || mmco[i].opcode == MMCO_SHORT2LONG){
  474. frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
  475. pic = find_short(h, frame_num, &j);
  476. if(!pic){
  477. if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
  478. || h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
  479. av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
  480. err = AVERROR_INVALIDDATA;
  481. }
  482. continue;
  483. }
  484. }
  485. switch(mmco[i].opcode){
  486. case MMCO_SHORT2UNUSED:
  487. if(s->avctx->debug&FF_DEBUG_MMCO)
  488. 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);
  489. remove_short(h, frame_num, structure ^ PICT_FRAME);
  490. break;
  491. case MMCO_SHORT2LONG:
  492. if (h->long_ref[mmco[i].long_arg] != pic)
  493. remove_long(h, mmco[i].long_arg, 0);
  494. remove_short_at_index(h, j);
  495. h->long_ref[ mmco[i].long_arg ]= pic;
  496. if (h->long_ref[ mmco[i].long_arg ]){
  497. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  498. h->long_ref_count++;
  499. }
  500. break;
  501. case MMCO_LONG2UNUSED:
  502. j = pic_num_extract(h, mmco[i].long_arg, &structure);
  503. pic = h->long_ref[j];
  504. if (pic) {
  505. remove_long(h, j, structure ^ PICT_FRAME);
  506. } else if(s->avctx->debug&FF_DEBUG_MMCO)
  507. av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
  508. break;
  509. case MMCO_LONG:
  510. // Comment below left from previous code as it is an interresting note.
  511. /* First field in pair is in short term list or
  512. * at a different long term index.
  513. * This is not allowed; see 7.4.3.3, notes 2 and 3.
  514. * Report the problem and keep the pair where it is,
  515. * and mark this field valid.
  516. */
  517. if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
  518. remove_long(h, mmco[i].long_arg, 0);
  519. h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
  520. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  521. h->long_ref_count++;
  522. }
  523. s->current_picture_ptr->f.reference |= s->picture_structure;
  524. current_ref_assigned=1;
  525. break;
  526. case MMCO_SET_MAX_LONG:
  527. assert(mmco[i].long_arg <= 16);
  528. // just remove the long term which index is greater than new max
  529. for(j = mmco[i].long_arg; j<16; j++){
  530. remove_long(h, j, 0);
  531. }
  532. break;
  533. case MMCO_RESET:
  534. while(h->short_ref_count){
  535. remove_short(h, h->short_ref[0]->frame_num, 0);
  536. }
  537. for(j = 0; j < 16; j++) {
  538. remove_long(h, j, 0);
  539. }
  540. h->frame_num=
  541. s->current_picture_ptr->frame_num= 0;
  542. h->mmco_reset = 1;
  543. s->current_picture_ptr->mmco_reset=1;
  544. break;
  545. default: assert(0);
  546. }
  547. }
  548. if (!current_ref_assigned) {
  549. /* Second field of complementary field pair; the first field of
  550. * which is already referenced. If short referenced, it
  551. * should be first entry in short_ref. If not, it must exist
  552. * in long_ref; trying to put it on the short list here is an
  553. * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
  554. */
  555. if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
  556. /* Just mark the second field valid */
  557. s->current_picture_ptr->f.reference = PICT_FRAME;
  558. } else if (s->current_picture_ptr->long_ref) {
  559. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
  560. "assignment for second field "
  561. "in complementary field pair "
  562. "(first field is long term)\n");
  563. err = AVERROR_INVALIDDATA;
  564. } else {
  565. pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
  566. if(pic){
  567. av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  568. err = AVERROR_INVALIDDATA;
  569. }
  570. if(h->short_ref_count)
  571. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  572. h->short_ref[0]= s->current_picture_ptr;
  573. h->short_ref_count++;
  574. s->current_picture_ptr->f.reference |= s->picture_structure;
  575. }
  576. }
  577. if (h->long_ref_count + h->short_ref_count -
  578. (h->short_ref[0] == s->current_picture_ptr) > h->sps.ref_frame_count){
  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->s.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. return (h->s.avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
  602. }
  603. int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb,
  604. int first_slice)
  605. {
  606. MpegEncContext * const s = &h->s;
  607. int i, ret;
  608. MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
  609. int mmco_index = 0;
  610. if (h->nal_unit_type == NAL_IDR_SLICE){ // FIXME fields
  611. s->broken_link = get_bits1(gb) - 1;
  612. if (get_bits1(gb)){
  613. mmco[0].opcode = MMCO_LONG;
  614. mmco[0].long_arg = 0;
  615. mmco_index = 1;
  616. }
  617. } else {
  618. if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
  619. for (i = 0; i < MAX_MMCO_COUNT; i++) {
  620. MMCOOpcode opcode = get_ue_golomb_31(gb);
  621. mmco[i].opcode = opcode;
  622. if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG){
  623. mmco[i].short_pic_num =
  624. (h->curr_pic_num - get_ue_golomb(gb) - 1) &
  625. (h->max_pic_num - 1);
  626. #if 0
  627. if (mmco[i].short_pic_num >= h->short_ref_count ||
  628. h->short_ref[ mmco[i].short_pic_num ] == NULL){
  629. av_log(s->avctx, AV_LOG_ERROR,
  630. "illegal short ref in memory management control "
  631. "operation %d\n", mmco);
  632. return -1;
  633. }
  634. #endif
  635. }
  636. if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
  637. opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG) {
  638. unsigned int long_arg = get_ue_golomb_31(gb);
  639. if (long_arg >= 32 ||
  640. (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG &&
  641. long_arg == 16) &&
  642. !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
  643. av_log(h->s.avctx, AV_LOG_ERROR,
  644. "illegal long ref in memory management control "
  645. "operation %d\n", opcode);
  646. return -1;
  647. }
  648. mmco[i].long_arg = long_arg;
  649. }
  650. if (opcode > (unsigned) MMCO_LONG){
  651. av_log(h->s.avctx, AV_LOG_ERROR,
  652. "illegal memory management control operation %d\n",
  653. opcode);
  654. return -1;
  655. }
  656. if (opcode == MMCO_END)
  657. break;
  658. }
  659. mmco_index = i;
  660. } else {
  661. if (first_slice) {
  662. ret = ff_generate_sliding_window_mmcos(h, first_slice);
  663. if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
  664. return ret;
  665. }
  666. mmco_index = -1;
  667. }
  668. }
  669. if (first_slice && mmco_index != -1) {
  670. h->mmco_index = mmco_index;
  671. } else if (!first_slice && mmco_index >= 0 &&
  672. (mmco_index != h->mmco_index ||
  673. (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
  674. av_log(h->s.avctx, AV_LOG_ERROR,
  675. "Inconsistent MMCO state between slices [%d, %d, %d]\n",
  676. mmco_index, h->mmco_index, i);
  677. return AVERROR_INVALIDDATA;
  678. }
  679. return 0;
  680. }