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.

758 lines
27KB

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