h264_refs.c 26 KB

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