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.

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