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.

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