mbuffer.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:135k
源码类别:

Audio

开发平台:

Visual C++

  1. /*!
  2.  ***********************************************************************
  3.  *  file
  4.  *      mbuffer.c
  5.  *
  6.  *  brief
  7.  *      Frame buffer functions
  8.  *
  9.  *  author
  10.  *      Main contributors (see contributors.h for copyright, address and affiliation details)
  11.  *      - Karsten S黨ring                 <suehring@hhi.de>
  12.  *      - Alexis Tourapis                 <alexismt@ieee.org>
  13.  *      - Jill Boyce                      <jill.boyce@thomson.net>
  14.  *      - Saurav K Bandyopadhyay          <saurav@ieee.org>
  15.  *      - Zhenyu Wu                       <Zhenyu.Wu@thomson.net
  16.  *      - Purvin Pandit                   <Purvin.Pandit@thomson.net>
  17.  *
  18.  ***********************************************************************
  19.  */
  20. #include <limits.h>
  21. #include "global.h"
  22. #include "mbuffer.h"
  23. #include "memalloc.h"
  24. #include "output.h"
  25. #include "image.h"
  26. #include "header.h"
  27. // picture error concealment
  28. #include "erc_api.h"
  29. static void insert_picture_in_dpb(FrameStore* fs, StorablePicture* p);
  30. static void output_one_frame_from_dpb(void);
  31. static int  is_used_for_reference(FrameStore* fs);
  32. static void get_smallest_poc(int *poc,int * pos);
  33. static int  remove_unused_frame_from_dpb(void);
  34. static int  is_short_term_reference(FrameStore* fs);
  35. static int  is_long_term_reference(FrameStore* fs);
  36. void gen_field_ref_ids(StorablePicture *p);
  37. DecodedPictureBuffer dpb;
  38. StorablePicture **listX[6];
  39. StorablePicture *no_reference_picture; //!< dummy storable picture for recovery point
  40. ColocatedParams *Co_located = NULL;
  41. ColocatedParams *Co_located_JV[MAX_PLANE] = { NULL, NULL, NULL };  //!< Co_located to be used during 4:4:4 independent mode decoding
  42. extern StorablePicture *dec_picture;
  43. int listXsize[6];
  44. #define MAX_LIST_SIZE 33
  45. /*!
  46.  ************************************************************************
  47.  * brief
  48.  *    Print out list of pictures in DPB. Used for debug purposes.
  49.  ************************************************************************
  50.  */
  51. void dump_dpb(void)
  52. {
  53. #if DUMP_DPB
  54.   unsigned i;
  55.   for (i=0; i<dpb.used_size;i++)
  56.   {
  57.     printf("(");
  58.     printf("fn=%d  ", dpb.fs[i]->frame_num);
  59.     if (dpb.fs[i]->is_used & 1)
  60.     {
  61.       if (dpb.fs[i]->top_field)
  62.         printf("T: poc=%d  ", dpb.fs[i]->top_field->poc);
  63.       else
  64.         printf("T: poc=%d  ", dpb.fs[i]->frame->top_poc);
  65.     }
  66.     if (dpb.fs[i]->is_used & 2)
  67.     {
  68.       if (dpb.fs[i]->bottom_field)
  69.         printf("B: poc=%d  ", dpb.fs[i]->bottom_field->poc);
  70.       else
  71.         printf("B: poc=%d  ", dpb.fs[i]->frame->bottom_poc);
  72.     }
  73.     if (dpb.fs[i]->is_used == 3)
  74.       printf("F: poc=%d  ", dpb.fs[i]->frame->poc);
  75.     printf("G: poc=%d)  ", dpb.fs[i]->poc);
  76.     if (dpb.fs[i]->is_reference) printf ("ref (%d) ", dpb.fs[i]->is_reference);
  77.     if (dpb.fs[i]->is_long_term) printf ("lt_ref (%d) ", dpb.fs[i]->is_reference);
  78.     if (dpb.fs[i]->is_output) printf ("out  ");
  79.     if (dpb.fs[i]->is_used == 3)
  80.     {
  81.       if (dpb.fs[i]->frame->non_existing) printf ("ne  ");
  82.     }
  83.     printf ("n");
  84.   }
  85. #endif
  86. }
  87. /*!
  88.  ************************************************************************
  89.  * brief
  90.  *    Returns the size of the dpb depending on level and picture size
  91.  *
  92.  *
  93.  ************************************************************************
  94.  */
  95. int getDpbSize(void)
  96. {
  97.   int pic_size = (active_sps->pic_width_in_mbs_minus1 + 1) * (active_sps->pic_height_in_map_units_minus1 + 1) * (active_sps->frame_mbs_only_flag?1:2) * 384;
  98.   int size = 0;
  99.   switch (active_sps->level_idc)
  100.   {
  101.   case 9:
  102.     size = 152064;
  103.     break;
  104.   case 10:
  105.     size = 152064;
  106.     break;
  107.   case 11:
  108.     if (!IS_FREXT_PROFILE(active_sps->profile_idc) && (active_sps->constrained_set3_flag == 1))
  109.       size = 152064;
  110.     else
  111.       size = 345600;
  112.     break;
  113.   case 12:
  114.     size = 912384;
  115.     break;
  116.   case 13:
  117.     size = 912384;
  118.     break;
  119.   case 20:
  120.     size = 912384;
  121.     break;
  122.   case 21:
  123.     size = 1824768;
  124.     break;
  125.   case 22:
  126.     size = 3110400;
  127.     break;
  128.   case 30:
  129.     size = 3110400;
  130.     break;
  131.   case 31:
  132.     size = 6912000;
  133.     break;
  134.   case 32:
  135.     size = 7864320;
  136.     break;
  137.   case 40:
  138.     size = 12582912;
  139.     break;
  140.   case 41:
  141.     size = 12582912;
  142.     break;
  143.   case 42:
  144.     size = 13369344;
  145.     break;
  146.   case 50:
  147.     size = 42393600;
  148.     break;
  149.   case 51:
  150.     size = 70778880;
  151.     break;
  152.   default:
  153.     error ("undefined level", 500);
  154.     break;
  155.   }
  156.   size /= pic_size;
  157.   size = imin( size, 16);
  158.   if (active_sps->vui_parameters_present_flag && active_sps->vui_seq_parameters.bitstream_restriction_flag)
  159.   {
  160.     if ((int)active_sps->vui_seq_parameters.max_dec_frame_buffering > size)
  161.     {
  162.       error ("max_dec_frame_buffering larger than MaxDpbSize", 500);
  163.     }
  164.     size = imax (1, active_sps->vui_seq_parameters.max_dec_frame_buffering);
  165.   }
  166.   return size;
  167. }
  168. /*!
  169.  ************************************************************************
  170.  * brief
  171.  *    Check then number of frames marked "used for reference" and break
  172.  *    if maximum is exceeded
  173.  *
  174.  ************************************************************************
  175.  */
  176. void check_num_ref(void)
  177. {
  178.   if ((int)(dpb.ltref_frames_in_buffer +  dpb.ref_frames_in_buffer ) > (imax(1,dpb.num_ref_frames)))
  179.   {
  180.     error ("Max. number of reference frames exceeded. Invalid stream.", 500);
  181.   }
  182. }
  183. /*!
  184.  ************************************************************************
  185.  * brief
  186.  *    Allocate memory for decoded picture buffer and initialize with sane values.
  187.  *
  188.  ************************************************************************
  189.  */
  190. void init_dpb(void)
  191. {
  192.   unsigned i,j;
  193.   if (dpb.init_done)
  194.   {
  195.     free_dpb();
  196.   }
  197.   dpb.size      = getDpbSize();
  198.   dpb.num_ref_frames = active_sps->num_ref_frames;
  199.   if (dpb.size < active_sps->num_ref_frames)
  200.   {
  201.     error ("DPB size at specified level is smaller than the specified number of reference frames. This is not allowed.n", 1000);
  202.   }
  203.   dpb.used_size = 0;
  204.   dpb.last_picture = NULL;
  205.   dpb.ref_frames_in_buffer = 0;
  206.   dpb.ltref_frames_in_buffer = 0;
  207.   dpb.fs = calloc(dpb.size, sizeof (FrameStore*));
  208.   if (NULL==dpb.fs)
  209.     no_mem_exit("init_dpb: dpb->fs");
  210.   dpb.fs_ref = calloc(dpb.size, sizeof (FrameStore*));
  211.   if (NULL==dpb.fs_ref)
  212.     no_mem_exit("init_dpb: dpb->fs_ref");
  213.   dpb.fs_ltref = calloc(dpb.size, sizeof (FrameStore*));
  214.   if (NULL==dpb.fs_ltref)
  215.     no_mem_exit("init_dpb: dpb->fs_ltref");
  216.   for (i=0; i<dpb.size; i++)
  217.   {
  218.     dpb.fs[i]       = alloc_frame_store();
  219.     dpb.fs_ref[i]   = NULL;
  220.     dpb.fs_ltref[i] = NULL;
  221.   }
  222.   for (i=0; i<6; i++)
  223.   {
  224.     listX[i] = calloc(MAX_LIST_SIZE, sizeof (StorablePicture*)); // +1 for reordering
  225.     if (NULL==listX[i])
  226.       no_mem_exit("init_dpb: listX[i]");
  227.   }
  228.   /* allocate a dummy storable picture */
  229.   no_reference_picture = alloc_storable_picture (FRAME, img->width, img->height, img->width_cr, img->height_cr);
  230.   no_reference_picture->top_field    = no_reference_picture;
  231.   no_reference_picture->bottom_field = no_reference_picture;
  232.   no_reference_picture->frame        = no_reference_picture;
  233.   for (j=0;j<6;j++)
  234.   {
  235.     for (i=0; i<MAX_LIST_SIZE; i++)
  236.     {
  237.       listX[j][i] = NULL;
  238.     }
  239.     listXsize[j]=0;
  240.   }
  241.   dpb.last_output_poc = INT_MIN;
  242.   img->last_has_mmco_5 = 0;
  243.   dpb.init_done = 1;
  244.   // picture error concealment
  245.   if(img->conceal_mode !=0)
  246.       last_out_fs = alloc_frame_store();
  247. }
  248. /*!
  249.  ************************************************************************
  250.  * brief
  251.  *    Free memory for decoded picture buffer.
  252.  ************************************************************************
  253.  */
  254. void free_dpb(void)
  255. {
  256.   unsigned i;
  257.   if (dpb.fs)
  258.   {
  259.     for (i=0; i<dpb.size; i++)
  260.     {
  261.       free_frame_store(dpb.fs[i]);
  262.     }
  263.     free (dpb.fs);
  264.     dpb.fs=NULL;
  265.   }
  266.   if (dpb.fs_ref)
  267.   {
  268.     free (dpb.fs_ref);
  269.   }
  270.   if (dpb.fs_ltref)
  271.   {
  272.     free (dpb.fs_ltref);
  273.   }
  274.   dpb.last_output_poc = INT_MIN;
  275.   for (i=0; i<6; i++)
  276.     if (listX[i])
  277.     {
  278.       free (listX[i]);
  279.       listX[i] = NULL;
  280.     }
  281.   dpb.init_done = 0;
  282.   // picture error concealment
  283.   if(img->conceal_mode != 0)
  284.       free_frame_store(last_out_fs);
  285.   free_storable_picture(no_reference_picture);
  286. }
  287. /*!
  288.  ************************************************************************
  289.  * brief
  290.  *    Allocate memory for decoded picture buffer frame stores an initialize with sane values.
  291.  *
  292.  * return
  293.  *    the allocated FrameStore structure
  294.  ************************************************************************
  295.  */
  296. FrameStore* alloc_frame_store(void)
  297. {
  298.   FrameStore *f;
  299.   f = calloc (1, sizeof(FrameStore));
  300.   if (NULL==f)
  301.     no_mem_exit("alloc_frame_store: f");
  302.   f->is_used      = 0;
  303.   f->is_reference = 0;
  304.   f->is_long_term = 0;
  305.   f->is_orig_reference = 0;
  306.   f->is_output = 0;
  307.   f->frame        = NULL;;
  308.   f->top_field    = NULL;
  309.   f->bottom_field = NULL;
  310.   return f;
  311. }
  312. void alloc_pic_motion(PicMotionParams *motion, int size_y, int size_x)
  313. {
  314.   get_mem3Dint64 (&(motion->ref_pic_id), 6, size_y, size_x);
  315.   get_mem3Dint64 (&(motion->ref_id),     6, size_y, size_x);
  316.   get_mem4Dshort (&(motion->mv),         2, size_y, size_x, 2);
  317.   get_mem3D      ((byte****)(&(motion->ref_idx)),    2, size_y , size_x);
  318.   motion->mb_field = calloc (size_y * size_x, sizeof(byte));
  319.   if (motion->mb_field == NULL)
  320.     no_mem_exit("alloc_storable_picture: motion->mb_field");
  321.   get_mem2D (&(motion->field_frame), size_y, size_x);
  322. }
  323. /*!
  324.  ************************************************************************
  325.  * brief
  326.  *    Allocate memory for a stored picture.
  327.  *
  328.  * param structure
  329.  *    picture structure
  330.  * param size_x
  331.  *    horizontal luma size
  332.  * param size_y
  333.  *    vertical luma size
  334.  * param size_x_cr
  335.  *    horizontal chroma size
  336.  * param size_y_cr
  337.  *    vertical chroma size
  338.  *
  339.  * return
  340.  *    the allocated StorablePicture structure
  341.  ************************************************************************
  342.  */
  343. StorablePicture* alloc_storable_picture(PictureStructure structure, int size_x, int size_y, int size_x_cr, int size_y_cr)
  344. {
  345.   StorablePicture *s;
  346.   int   nplane;
  347.   //printf ("Allocating (%s) picture (x=%d, y=%d, x_cr=%d, y_cr=%d)n", (type == FRAME)?"FRAME":(type == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", size_x, size_y, size_x_cr, size_y_cr);
  348.   s = calloc (1, sizeof(StorablePicture));
  349.   if (NULL==s)
  350.     no_mem_exit("alloc_storable_picture: s");
  351.   if (structure!=FRAME)
  352.   {
  353.     size_y    /= 2;
  354.     size_y_cr /= 2;
  355.   }
  356.   s->PicSizeInMbs = (size_x*size_y)/256;
  357.   s->imgUV = NULL;
  358.   get_mem2Dpel (&(s->imgY), size_y, size_x);
  359.   if (active_sps->chroma_format_idc != YUV400)
  360.     get_mem3Dpel (&(s->imgUV), 2, size_y_cr, size_x_cr);
  361.   
  362.   get_mem2Dshort (&(s->slice_id), size_y / MB_BLOCK_SIZE, size_x / MB_BLOCK_SIZE);
  363.   alloc_pic_motion(&s->motion, size_y / BLOCK_SIZE, size_x / BLOCK_SIZE);
  364.   if( IS_INDEPENDENT(img) )
  365.   {
  366.     for( nplane=0; nplane<MAX_PLANE; nplane++ )
  367.     {
  368.       alloc_pic_motion(&s->JVmotion[nplane], size_y / BLOCK_SIZE, size_x / BLOCK_SIZE);
  369.     }
  370.   }
  371.   s->pic_num=0;
  372.   s->frame_num=0;
  373.   s->long_term_frame_idx=0;
  374.   s->long_term_pic_num=0;
  375.   s->used_for_reference=0;
  376.   s->is_long_term=0;
  377.   s->non_existing=0;
  378.   s->is_output = 0;
  379.   s->max_slice_id = 0;
  380.   s->structure=structure;
  381.   s->size_x = size_x;
  382.   s->size_y = size_y;
  383.   s->size_x_cr = size_x_cr;
  384.   s->size_y_cr = size_y_cr;
  385.   s->size_x_m1 = size_x - 1;
  386.   s->size_y_m1 = size_y - 1;
  387.   s->size_x_cr_m1 = size_x_cr - 1;
  388.   s->size_y_cr_m1 = size_y_cr - 1;
  389.   s->top_field    = no_reference_picture;
  390.   s->bottom_field = no_reference_picture;
  391.   s->frame        = no_reference_picture;
  392.   s->dec_ref_pic_marking_buffer = NULL;
  393.   s->coded_frame                   = 0;
  394.   s->MbaffFrameFlag                = 0;
  395.   s->top_poc = s->bottom_poc = s->poc = 0;
  396.   s->seiHasTone_mapping = 0;
  397.   return s;
  398. }
  399. /*!
  400.  ************************************************************************
  401.  * brief
  402.  *    Free frame store memory.
  403.  *
  404.  * param f
  405.  *    FrameStore to be freed
  406.  *
  407.  ************************************************************************
  408.  */
  409. void free_frame_store(FrameStore* f)
  410. {
  411.   if (f)
  412.   {
  413.     if (f->frame)
  414.     {
  415.       free_storable_picture(f->frame);
  416.       f->frame=NULL;
  417.     }
  418.     if (f->top_field)
  419.     {
  420.       free_storable_picture(f->top_field);
  421.       f->top_field=NULL;
  422.     }
  423.     if (f->bottom_field)
  424.     {
  425.       free_storable_picture(f->bottom_field);
  426.       f->bottom_field=NULL;
  427.     }
  428.     free(f);
  429.   }
  430. }
  431. void free_pic_motion(PicMotionParams *motion)
  432. {
  433.   if (motion->ref_pic_id)
  434.   {
  435.     free_mem3Dint64 (motion->ref_pic_id);
  436.     motion->ref_pic_id = NULL;
  437.   }
  438.   if (motion->ref_id)
  439.   {
  440.     free_mem3Dint64 (motion->ref_id);
  441.     motion->ref_id = NULL;
  442.   }
  443.   if (motion->mv)
  444.   {
  445.     free_mem4Dshort (motion->mv);
  446.     motion->mv = NULL;
  447.   }
  448.   if (motion->ref_idx)
  449.   {
  450.     free_mem3D ((byte***)motion->ref_idx);
  451.     motion->ref_idx = NULL;
  452.   }
  453.   if (motion->mb_field)
  454.   {
  455.     free(motion->mb_field);
  456.     motion->mb_field = NULL;
  457.   }
  458.   if (motion->field_frame)
  459.   {
  460.     free_mem2D (motion->field_frame);
  461.     motion->field_frame=NULL;
  462.   }
  463. }
  464. /*!
  465.  ************************************************************************
  466.  * brief
  467.  *    Free picture memory.
  468.  *
  469.  * param p
  470.  *    Picture to be freed
  471.  *
  472.  ************************************************************************
  473.  */
  474. void free_storable_picture(StorablePicture* p)
  475. {
  476.   int nplane;
  477.   if (p)
  478.   {
  479.     free_pic_motion(&p->motion);
  480.     if( IS_INDEPENDENT(img) )
  481.     {
  482.       for( nplane=0; nplane<MAX_PLANE; nplane++ )
  483.       {
  484.         free_pic_motion(&p->JVmotion[nplane]);
  485.       }
  486.     }
  487.     if (p->imgY)
  488.     {
  489.       free_mem2Dpel (p->imgY);
  490.       p->imgY=NULL;
  491.     }
  492.     if (p->imgUV)
  493.     {
  494.       free_mem3Dpel (p->imgUV);
  495.       p->imgUV=NULL;
  496.     }
  497.     if (p->slice_id)
  498.     {
  499.       free_mem2Dshort(p->slice_id);
  500.       p->slice_id=NULL;
  501.     }
  502.     if (p->seiHasTone_mapping)
  503.       free(p->tone_mapping_lut);
  504.     free(p);
  505.     p = NULL;
  506.   }
  507. }
  508. /*!
  509.  ************************************************************************
  510.  * brief
  511.  *    mark FrameStore unused for reference
  512.  *
  513.  ************************************************************************
  514.  */
  515. static void unmark_for_reference(FrameStore* fs)
  516. {
  517.   if (fs->is_used & 1)
  518.   {
  519.     if (fs->top_field)
  520.     {
  521.       fs->top_field->used_for_reference = 0;
  522.     }
  523.   }
  524.   if (fs->is_used & 2)
  525.   {
  526.     if (fs->bottom_field)
  527.     {
  528.       fs->bottom_field->used_for_reference = 0;
  529.     }
  530.   }
  531.   if (fs->is_used == 3)
  532.   {
  533.     if (fs->top_field && fs->bottom_field)
  534.     {
  535.       fs->top_field->used_for_reference = 0;
  536.       fs->bottom_field->used_for_reference = 0;
  537.     }
  538.     fs->frame->used_for_reference = 0;
  539.   }
  540.   fs->is_reference = 0;
  541.   if(fs->frame)
  542.   {
  543.     free_pic_motion(&fs->frame->motion);
  544.   }
  545.   if (fs->top_field)
  546.   {
  547.     free_pic_motion(&fs->top_field->motion);
  548.   }
  549.   if (fs->bottom_field)
  550.   {
  551.     free_pic_motion(&fs->bottom_field->motion);
  552.   }
  553. }
  554. /*!
  555.  ************************************************************************
  556.  * brief
  557.  *    mark FrameStore unused for reference and reset long term flags
  558.  *
  559.  ************************************************************************
  560.  */
  561. static void unmark_for_long_term_reference(FrameStore* fs)
  562. {
  563.   if (fs->is_used & 1)
  564.   {
  565.     if (fs->top_field)
  566.     {
  567.       fs->top_field->used_for_reference = 0;
  568.       fs->top_field->is_long_term = 0;
  569.     }
  570.   }
  571.   if (fs->is_used & 2)
  572.   {
  573.     if (fs->bottom_field)
  574.     {
  575.       fs->bottom_field->used_for_reference = 0;
  576.       fs->bottom_field->is_long_term = 0;
  577.     }
  578.   }
  579.   if (fs->is_used == 3)
  580.   {
  581.     if (fs->top_field && fs->bottom_field)
  582.     {
  583.       fs->top_field->used_for_reference = 0;
  584.       fs->top_field->is_long_term = 0;
  585.       fs->bottom_field->used_for_reference = 0;
  586.       fs->bottom_field->is_long_term = 0;
  587.     }
  588.     fs->frame->used_for_reference = 0;
  589.     fs->frame->is_long_term = 0;
  590.   }
  591.   fs->is_reference = 0;
  592.   fs->is_long_term = 0;
  593. }
  594. /*!
  595.  ************************************************************************
  596.  * brief
  597.  *    compares two stored pictures by picture number for qsort in descending order
  598.  *
  599.  ************************************************************************
  600.  */
  601. static int compare_pic_by_pic_num_desc( const void *arg1, const void *arg2 )
  602. {
  603.   if ( (*(StorablePicture**)arg1)->pic_num < (*(StorablePicture**)arg2)->pic_num)
  604.     return 1;
  605.   if ( (*(StorablePicture**)arg1)->pic_num > (*(StorablePicture**)arg2)->pic_num)
  606.     return -1;
  607.   else
  608.     return 0;
  609. }
  610. /*!
  611.  ************************************************************************
  612.  * brief
  613.  *    compares two stored pictures by picture number for qsort in descending order
  614.  *
  615.  ************************************************************************
  616.  */
  617. static int compare_pic_by_lt_pic_num_asc( const void *arg1, const void *arg2 )
  618. {
  619.   if ( (*(StorablePicture**)arg1)->long_term_pic_num < (*(StorablePicture**)arg2)->long_term_pic_num)
  620.     return -1;
  621.   if ( (*(StorablePicture**)arg1)->long_term_pic_num > (*(StorablePicture**)arg2)->long_term_pic_num)
  622.     return 1;
  623.   else
  624.     return 0;
  625. }
  626. /*!
  627.  ************************************************************************
  628.  * brief
  629.  *    compares two frame stores by pic_num for qsort in descending order
  630.  *
  631.  ************************************************************************
  632.  */
  633. static int compare_fs_by_frame_num_desc( const void *arg1, const void *arg2 )
  634. {
  635.   if ( (*(FrameStore**)arg1)->frame_num_wrap < (*(FrameStore**)arg2)->frame_num_wrap)
  636.     return 1;
  637.   if ( (*(FrameStore**)arg1)->frame_num_wrap > (*(FrameStore**)arg2)->frame_num_wrap)
  638.     return -1;
  639.   else
  640.     return 0;
  641. }
  642. /*!
  643.  ************************************************************************
  644.  * brief
  645.  *    compares two frame stores by lt_pic_num for qsort in descending order
  646.  *
  647.  ************************************************************************
  648.  */
  649. static int compare_fs_by_lt_pic_idx_asc( const void *arg1, const void *arg2 )
  650. {
  651.   if ( (*(FrameStore**)arg1)->long_term_frame_idx < (*(FrameStore**)arg2)->long_term_frame_idx)
  652.     return -1;
  653.   if ( (*(FrameStore**)arg1)->long_term_frame_idx > (*(FrameStore**)arg2)->long_term_frame_idx)
  654.     return 1;
  655.   else
  656.     return 0;
  657. }
  658. /*!
  659.  ************************************************************************
  660.  * brief
  661.  *    compares two stored pictures by poc for qsort in ascending order
  662.  *
  663.  ************************************************************************
  664.  */
  665. static int compare_pic_by_poc_asc( const void *arg1, const void *arg2 )
  666. {
  667.   if ( (*(StorablePicture**)arg1)->poc < (*(StorablePicture**)arg2)->poc)
  668.     return -1;
  669.   
  670.   if ( (*(StorablePicture**)arg1)->poc > (*(StorablePicture**)arg2)->poc)
  671.     return 1;
  672.   else
  673.     return 0;
  674. }
  675. /*!
  676.  ************************************************************************
  677.  * brief
  678.  *    compares two stored pictures by poc for qsort in descending order
  679.  *
  680.  ************************************************************************
  681.  */
  682. static int compare_pic_by_poc_desc( const void *arg1, const void *arg2 )
  683. {
  684.   if ( (*(StorablePicture**)arg1)->poc < (*(StorablePicture**)arg2)->poc)
  685.     return 1;
  686.   if ( (*(StorablePicture**)arg1)->poc > (*(StorablePicture**)arg2)->poc)
  687.     return -1;
  688.   else
  689.     return 0;
  690. }
  691. /*!
  692.  ************************************************************************
  693.  * brief
  694.  *    compares two frame stores by poc for qsort in ascending order
  695.  *
  696.  ************************************************************************
  697.  */
  698. static int compare_fs_by_poc_asc( const void *arg1, const void *arg2 )
  699. {
  700.   if ( (*(FrameStore**)arg1)->poc < (*(FrameStore**)arg2)->poc)
  701.     return -1;
  702.   
  703.   if ( (*(FrameStore**)arg1)->poc > (*(FrameStore**)arg2)->poc)
  704.     return 1;
  705.   else
  706.     return 0;
  707. }
  708. /*!
  709.  ************************************************************************
  710.  * brief
  711.  *    compares two frame stores by poc for qsort in descending order
  712.  *
  713.  ************************************************************************
  714.  */
  715. static int compare_fs_by_poc_desc( const void *arg1, const void *arg2 )
  716. {
  717.   if ( (*(FrameStore**)arg1)->poc < (*(FrameStore**)arg2)->poc)
  718.     return 1;
  719.   if ( (*(FrameStore**)arg1)->poc > (*(FrameStore**)arg2)->poc)
  720.     return -1;
  721.   else
  722.     return 0;
  723. }
  724. /*!
  725.  ************************************************************************
  726.  * brief
  727.  *    returns true, if picture is short term reference picture
  728.  *
  729.  ************************************************************************
  730.  */
  731. int is_short_ref(StorablePicture *s)
  732. {
  733.   return ((s->used_for_reference) && (!(s->is_long_term)));
  734. }
  735. /*!
  736.  ************************************************************************
  737.  * brief
  738.  *    returns true, if picture is long term reference picture
  739.  *
  740.  ************************************************************************
  741.  */
  742. int is_long_ref(StorablePicture *s)
  743. {
  744.   return ((s->used_for_reference) && (s->is_long_term));
  745. }
  746. /*!
  747.  ************************************************************************
  748.  * brief
  749.  *    Generates a alternating field list from a given FrameStore list
  750.  *
  751.  ************************************************************************
  752.  */
  753. static void gen_pic_list_from_frame_list(PictureStructure currStrcture, FrameStore **fs_list, int list_idx, StorablePicture **list, int *list_size, int long_term)
  754. {
  755.   int top_idx = 0;
  756.   int bot_idx = 0;
  757.   int (*is_ref)(StorablePicture *s);
  758.   if (long_term)
  759.     is_ref=is_long_ref;
  760.   else
  761.     is_ref=is_short_ref;
  762.   if (currStrcture == TOP_FIELD)
  763.   {
  764.     while ((top_idx<list_idx)||(bot_idx<list_idx))
  765.     {
  766.       for ( ; top_idx<list_idx; top_idx++)
  767.       {
  768.         if(fs_list[top_idx]->is_used & 1)
  769.         {
  770.           if(is_ref(fs_list[top_idx]->top_field))
  771.           {
  772.             // short term ref pic
  773.             list[*list_size] = fs_list[top_idx]->top_field;
  774.             (*list_size)++;
  775.             top_idx++;
  776.             break;
  777.           }
  778.         }
  779.       }
  780.       for ( ; bot_idx<list_idx; bot_idx++)
  781.       {
  782.         if(fs_list[bot_idx]->is_used & 2)
  783.         {
  784.           if(is_ref(fs_list[bot_idx]->bottom_field))
  785.           {
  786.             // short term ref pic
  787.             list[*list_size] = fs_list[bot_idx]->bottom_field;
  788.             (*list_size)++;
  789.             bot_idx++;
  790.             break;
  791.           }
  792.         }
  793.       }
  794.     }
  795.   }
  796.   if (currStrcture == BOTTOM_FIELD)
  797.   {
  798.     while ((top_idx<list_idx)||(bot_idx<list_idx))
  799.     {
  800.       for ( ; bot_idx<list_idx; bot_idx++)
  801.       {
  802.         if(fs_list[bot_idx]->is_used & 2)
  803.         {
  804.           if(is_ref(fs_list[bot_idx]->bottom_field))
  805.           {
  806.             // short term ref pic
  807.             list[*list_size] = fs_list[bot_idx]->bottom_field;
  808.             (*list_size)++;
  809.             bot_idx++;
  810.             break;
  811.           }
  812.         }
  813.       }
  814.       for ( ; top_idx<list_idx; top_idx++)
  815.       {
  816.         if(fs_list[top_idx]->is_used & 1)
  817.         {
  818.           if(is_ref(fs_list[top_idx]->top_field))
  819.           {
  820.             // short term ref pic
  821.             list[*list_size] = fs_list[top_idx]->top_field;
  822.             (*list_size)++;
  823.             top_idx++;
  824.             break;
  825.           }
  826.         }
  827.       }
  828.     }
  829.   }
  830. }
  831. /*!
  832.  ************************************************************************
  833.  * brief
  834.  *    Initialize listX[0] and list 1 depending on current picture type
  835.  *
  836.  ************************************************************************
  837.  */
  838. void init_lists(int currSliceType, PictureStructure currPicStructure)
  839. {
  840.   int add_top = 0, add_bottom = 0;
  841.   unsigned i;
  842.   int j;
  843.   int MaxFrameNum = 1 << (active_sps->log2_max_frame_num_minus4 + 4);
  844.   int diff;
  845.   int list0idx = 0;
  846.   int list0idx_1 = 0;
  847.   int listltidx = 0;
  848.   FrameStore **fs_list0;
  849.   FrameStore **fs_list1;
  850.   FrameStore **fs_listlt;
  851.   StorablePicture *tmp_s;
  852.   if (currPicStructure == FRAME)
  853.   {
  854.     for (i=0; i<dpb.ref_frames_in_buffer; i++)
  855.     {
  856.       if (dpb.fs_ref[i]->is_used==3)
  857.       {
  858.         if ((dpb.fs_ref[i]->frame->used_for_reference)&&(!dpb.fs_ref[i]->frame->is_long_term))
  859.         {
  860.           if( dpb.fs_ref[i]->frame_num > img->frame_num )
  861.           {
  862.             dpb.fs_ref[i]->frame_num_wrap = dpb.fs_ref[i]->frame_num - MaxFrameNum;
  863.           }
  864.           else
  865.           {
  866.             dpb.fs_ref[i]->frame_num_wrap = dpb.fs_ref[i]->frame_num;
  867.           }
  868.           dpb.fs_ref[i]->frame->pic_num = dpb.fs_ref[i]->frame_num_wrap;
  869.         }
  870.       }
  871.     }
  872.     // update long_term_pic_num
  873.     for (i = 0; i < dpb.ltref_frames_in_buffer; i++)
  874.     {
  875.       if (dpb.fs_ltref[i]->is_used==3)
  876.       {
  877.         if (dpb.fs_ltref[i]->frame->is_long_term)
  878.         {
  879.           dpb.fs_ltref[i]->frame->long_term_pic_num = dpb.fs_ltref[i]->frame->long_term_frame_idx;
  880.         }
  881.       }
  882.     }
  883.   }
  884.   else
  885.   {
  886.     if (currPicStructure == TOP_FIELD)
  887.     {
  888.       add_top    = 1;
  889.       add_bottom = 0;
  890.     }
  891.     else
  892.     {
  893.       add_top    = 0;
  894.       add_bottom = 1;
  895.     }
  896.     for (i=0; i<dpb.ref_frames_in_buffer; i++)
  897.     {
  898.       if (dpb.fs_ref[i]->is_reference)
  899.       {
  900.         if( dpb.fs_ref[i]->frame_num > img->frame_num )
  901.         {
  902.           dpb.fs_ref[i]->frame_num_wrap = dpb.fs_ref[i]->frame_num - MaxFrameNum;
  903.         }
  904.         else
  905.         {
  906.           dpb.fs_ref[i]->frame_num_wrap = dpb.fs_ref[i]->frame_num;
  907.         }
  908.         if (dpb.fs_ref[i]->is_reference & 1)
  909.         {
  910.           dpb.fs_ref[i]->top_field->pic_num = (2 * dpb.fs_ref[i]->frame_num_wrap) + add_top;
  911.         }
  912.         if (dpb.fs_ref[i]->is_reference & 2)
  913.         {
  914.           dpb.fs_ref[i]->bottom_field->pic_num = (2 * dpb.fs_ref[i]->frame_num_wrap) + add_bottom;
  915.         }
  916.       }
  917.     }
  918.     // update long_term_pic_num
  919.     for (i=0; i<dpb.ltref_frames_in_buffer; i++)
  920.     {
  921.       if (dpb.fs_ltref[i]->is_long_term & 1)
  922.       {
  923.         dpb.fs_ltref[i]->top_field->long_term_pic_num = 2 * dpb.fs_ltref[i]->top_field->long_term_frame_idx + add_top;
  924.       }
  925.       if (dpb.fs_ltref[i]->is_long_term & 2)
  926.       {
  927.         dpb.fs_ltref[i]->bottom_field->long_term_pic_num = 2 * dpb.fs_ltref[i]->bottom_field->long_term_frame_idx + add_bottom;
  928.       }
  929.     }
  930.   }
  931.   if ((currSliceType == I_SLICE)||(currSliceType == SI_SLICE))
  932.   {
  933.     listXsize[0] = 0;
  934.     listXsize[1] = 0;
  935.     return;
  936.   }
  937.   if ((currSliceType == P_SLICE)||(currSliceType == SP_SLICE))
  938.   {
  939.     // Calculate FrameNumWrap and PicNum
  940.     if (currPicStructure == FRAME)
  941.     {
  942.       for (i=0; i<dpb.ref_frames_in_buffer; i++)
  943.       {
  944.         if (dpb.fs_ref[i]->is_used==3)
  945.         {
  946.           if ((dpb.fs_ref[i]->frame->used_for_reference)&&(!dpb.fs_ref[i]->frame->is_long_term))
  947.           {
  948.             listX[0][list0idx++] = dpb.fs_ref[i]->frame;
  949.           }
  950.         }
  951.       }
  952.       // order list 0 by PicNum
  953.       qsort((void *)listX[0], list0idx, sizeof(StorablePicture*), compare_pic_by_pic_num_desc);
  954.       listXsize[0] = list0idx;
  955. //      printf("listX[0] (PicNum): "); for (i=0; i<list0idx; i++){printf ("%d  ", listX[0][i]->pic_num);} printf("n");
  956.       // long term handling
  957.       for (i=0; i<dpb.ltref_frames_in_buffer; i++)
  958.       {
  959.         if (dpb.fs_ltref[i]->is_used==3)
  960.         {
  961.           if (dpb.fs_ltref[i]->frame->is_long_term)
  962.           {
  963.             listX[0][list0idx++]=dpb.fs_ltref[i]->frame;
  964.           }
  965.         }
  966.       }
  967.       qsort((void *)&listX[0][listXsize[0]], list0idx-listXsize[0], sizeof(StorablePicture*), compare_pic_by_lt_pic_num_asc);
  968.       listXsize[0] = list0idx;
  969.     }
  970.     else
  971.     {
  972.       fs_list0 = calloc(dpb.size, sizeof (FrameStore*));
  973.       if (NULL==fs_list0)
  974.          no_mem_exit("init_lists: fs_list0");
  975.       fs_listlt = calloc(dpb.size, sizeof (FrameStore*));
  976.       if (NULL==fs_listlt)
  977.          no_mem_exit("init_lists: fs_listlt");
  978.       for (i=0; i<dpb.ref_frames_in_buffer; i++)
  979.       {
  980.         if (dpb.fs_ref[i]->is_reference)
  981.         {
  982.           fs_list0[list0idx++] = dpb.fs_ref[i];
  983.         }
  984.       }
  985.       qsort((void *)fs_list0, list0idx, sizeof(FrameStore*), compare_fs_by_frame_num_desc);
  986. //      printf("fs_list0 (FrameNum): "); for (i=0; i<list0idx; i++){printf ("%d  ", fs_list0[i]->frame_num_wrap);} printf("n");
  987.       listXsize[0] = 0;
  988.       gen_pic_list_from_frame_list(currPicStructure, fs_list0, list0idx, listX[0], &listXsize[0], 0);
  989. //      printf("listX[0] (PicNum): "); for (i=0; i<listXsize[0]; i++){printf ("%d  ", listX[0][i]->pic_num);} printf("n");
  990.       // long term handling
  991.       for (i=0; i<dpb.ltref_frames_in_buffer; i++)
  992.       {
  993.         fs_listlt[listltidx++]=dpb.fs_ltref[i];
  994.       }
  995.       qsort((void *)fs_listlt, listltidx, sizeof(FrameStore*), compare_fs_by_lt_pic_idx_asc);
  996.       gen_pic_list_from_frame_list(currPicStructure, fs_listlt, listltidx, listX[0], &listXsize[0], 1);
  997.       free(fs_list0);
  998.       free(fs_listlt);
  999.     }
  1000.     listXsize[1] = 0;
  1001.   }
  1002.   else
  1003.   {
  1004.     // B-Slice
  1005.     if (currPicStructure == FRAME)
  1006.     {
  1007.       for (i=0; i<dpb.ref_frames_in_buffer; i++)
  1008.       {
  1009.         if (dpb.fs_ref[i]->is_used==3)
  1010.         {
  1011.           if ((dpb.fs_ref[i]->frame->used_for_reference)&&(!dpb.fs_ref[i]->frame->is_long_term))
  1012.           {
  1013.             if (img->framepoc >= dpb.fs_ref[i]->frame->poc) //!KS use >= for error concealment
  1014. //            if (img->framepoc > dpb.fs_ref[i]->frame->poc)
  1015.             {
  1016.               listX[0][list0idx++] = dpb.fs_ref[i]->frame;
  1017.             }
  1018.           }
  1019.         }
  1020.       }
  1021.       qsort((void *)listX[0], list0idx, sizeof(StorablePicture*), compare_pic_by_poc_desc);
  1022.       list0idx_1 = list0idx;
  1023.       for (i=0; i<dpb.ref_frames_in_buffer; i++)
  1024.       {
  1025.         if (dpb.fs_ref[i]->is_used==3)
  1026.         {
  1027.           if ((dpb.fs_ref[i]->frame->used_for_reference)&&(!dpb.fs_ref[i]->frame->is_long_term))
  1028.           {
  1029.             if (img->framepoc < dpb.fs_ref[i]->frame->poc)
  1030.             {
  1031.               listX[0][list0idx++] = dpb.fs_ref[i]->frame;
  1032.             }
  1033.           }
  1034.         }
  1035.       }
  1036.       qsort((void *)&listX[0][list0idx_1], list0idx-list0idx_1, sizeof(StorablePicture*), compare_pic_by_poc_asc);
  1037.       for (j=0; j<list0idx_1; j++)
  1038.       {
  1039.         listX[1][list0idx-list0idx_1+j]=listX[0][j];
  1040.       }
  1041.       for (j=list0idx_1; j<list0idx; j++)
  1042.       {
  1043.         listX[1][j-list0idx_1]=listX[0][j];
  1044.       }
  1045.       listXsize[0] = listXsize[1] = list0idx;
  1046. //      printf("listX[0] currPoc=%d (Poc): ", img->framepoc); for (i=0; i<listXsize[0]; i++){printf ("%d  ", listX[0][i]->poc);} printf("n");
  1047. //      printf("listX[1] currPoc=%d (Poc): ", img->framepoc); for (i=0; i<listXsize[1]; i++){printf ("%d  ", listX[1][i]->poc);} printf("n");
  1048.       // long term handling
  1049.       for (i=0; i<dpb.ltref_frames_in_buffer; i++)
  1050.       {
  1051.         if (dpb.fs_ltref[i]->is_used==3)
  1052.         {
  1053.           if (dpb.fs_ltref[i]->frame->is_long_term)
  1054.           {
  1055.             listX[0][list0idx]  =dpb.fs_ltref[i]->frame;
  1056.             listX[1][list0idx++]=dpb.fs_ltref[i]->frame;
  1057.           }
  1058.         }
  1059.       }
  1060.       qsort((void *)&listX[0][listXsize[0]], list0idx-listXsize[0], sizeof(StorablePicture*), compare_pic_by_lt_pic_num_asc);
  1061.       qsort((void *)&listX[1][listXsize[0]], list0idx-listXsize[0], sizeof(StorablePicture*), compare_pic_by_lt_pic_num_asc);
  1062.       listXsize[0] = listXsize[1] = list0idx;
  1063.     }
  1064.     else
  1065.     {
  1066.       fs_list0 = calloc(dpb.size, sizeof (FrameStore*));
  1067.       if (NULL==fs_list0)
  1068.          no_mem_exit("init_lists: fs_list0");
  1069.       fs_list1 = calloc(dpb.size, sizeof (FrameStore*));
  1070.       if (NULL==fs_list1)
  1071.          no_mem_exit("init_lists: fs_list1");
  1072.       fs_listlt = calloc(dpb.size, sizeof (FrameStore*));
  1073.       if (NULL==fs_listlt)
  1074.          no_mem_exit("init_lists: fs_listlt");
  1075.       listXsize[0] = 0;
  1076.       listXsize[1] = 1;
  1077.       for (i=0; i<dpb.ref_frames_in_buffer; i++)
  1078.       {
  1079.         if (dpb.fs_ref[i]->is_used)
  1080.         {
  1081.           if (img->ThisPOC >= dpb.fs_ref[i]->poc)
  1082.           {
  1083.             fs_list0[list0idx++] = dpb.fs_ref[i];
  1084.           }
  1085.         }
  1086.       }
  1087.       qsort((void *)fs_list0, list0idx, sizeof(FrameStore*), compare_fs_by_poc_desc);
  1088.       list0idx_1 = list0idx;
  1089.       for (i=0; i<dpb.ref_frames_in_buffer; i++)
  1090.       {
  1091.         if (dpb.fs_ref[i]->is_used)
  1092.         {
  1093.           if (img->ThisPOC < dpb.fs_ref[i]->poc)
  1094.           {
  1095.             fs_list0[list0idx++] = dpb.fs_ref[i];
  1096.           }
  1097.         }
  1098.       }
  1099.       qsort((void *)&fs_list0[list0idx_1], list0idx-list0idx_1, sizeof(FrameStore*), compare_fs_by_poc_asc);
  1100.       for (j=0; j<list0idx_1; j++)
  1101.       {
  1102.         fs_list1[list0idx-list0idx_1+j]=fs_list0[j];
  1103.       }
  1104.       for (j=list0idx_1; j<list0idx; j++)
  1105.       {
  1106.         fs_list1[j-list0idx_1]=fs_list0[j];
  1107.       }
  1108. //      printf("fs_list0 currPoc=%d (Poc): ", img->ThisPOC); for (i=0; i<list0idx; i++){printf ("%d  ", fs_list0[i]->poc);} printf("n");
  1109. //      printf("fs_list1 currPoc=%d (Poc): ", img->ThisPOC); for (i=0; i<list0idx; i++){printf ("%d  ", fs_list1[i]->poc);} printf("n");
  1110.       listXsize[0] = 0;
  1111.       listXsize[1] = 0;
  1112.       gen_pic_list_from_frame_list(currPicStructure, fs_list0, list0idx, listX[0], &listXsize[0], 0);
  1113.       gen_pic_list_from_frame_list(currPicStructure, fs_list1, list0idx, listX[1], &listXsize[1], 0);
  1114. //      printf("listX[0] currPoc=%d (Poc): ", img->framepoc); for (i=0; i<listXsize[0]; i++){printf ("%d  ", listX[0][i]->poc);} printf("n");
  1115. //      printf("listX[1] currPoc=%d (Poc): ", img->framepoc); for (i=0; i<listXsize[1]; i++){printf ("%d  ", listX[1][i]->poc);} printf("n");
  1116.       // long term handling
  1117.       for (i=0; i<dpb.ltref_frames_in_buffer; i++)
  1118.       {
  1119.         fs_listlt[listltidx++]=dpb.fs_ltref[i];
  1120.       }
  1121.       qsort((void *)fs_listlt, listltidx, sizeof(FrameStore*), compare_fs_by_lt_pic_idx_asc);
  1122.       gen_pic_list_from_frame_list(currPicStructure, fs_listlt, listltidx, listX[0], &listXsize[0], 1);
  1123.       gen_pic_list_from_frame_list(currPicStructure, fs_listlt, listltidx, listX[1], &listXsize[1], 1);
  1124.       free(fs_list0);
  1125.       free(fs_list1);
  1126.       free(fs_listlt);
  1127.     }
  1128.   }
  1129.   if ((listXsize[0] == listXsize[1]) && (listXsize[0] > 1))
  1130.   {
  1131.     // check if lists are identical, if yes swap first two elements of listX[1]
  1132.     diff=0;
  1133.     for (j = 0; j< listXsize[0]; j++)
  1134.     {
  1135.       if (listX[0][j]!=listX[1][j])
  1136.         diff=1;
  1137.     }
  1138.     if (!diff)
  1139.     {
  1140.       tmp_s = listX[1][0];
  1141.       listX[1][0]=listX[1][1];
  1142.       listX[1][1]=tmp_s;
  1143.     }
  1144.   }
  1145.   // set max size
  1146.   listXsize[0] = imin (listXsize[0], img->num_ref_idx_l0_active);
  1147.   listXsize[1] = imin (listXsize[1], img->num_ref_idx_l1_active);
  1148.   // set the unused list entries to NULL
  1149.   for (i=listXsize[0]; i< (MAX_LIST_SIZE) ; i++)
  1150.   {
  1151.       listX[0][i] = no_reference_picture;
  1152.   }
  1153.   for (i=listXsize[1]; i< (MAX_LIST_SIZE) ; i++)
  1154.   {
  1155.       listX[1][i] = no_reference_picture;
  1156.   }
  1157. }
  1158. /*!
  1159.  ************************************************************************
  1160.  * brief
  1161.  *    Initialize listX[2..5] from lists 0 and 1
  1162.  *    listX[2]: list0 for current_field==top
  1163.  *    listX[3]: list1 for current_field==top
  1164.  *    listX[4]: list0 for current_field==bottom
  1165.  *    listX[5]: list1 for current_field==bottom
  1166.  *
  1167.  ************************************************************************
  1168.  */
  1169. void init_mbaff_lists(void)
  1170. {
  1171.   unsigned j;
  1172.   int i;
  1173.   for (i=2;i<6;i++)
  1174.   {
  1175.     for (j=0; j<MAX_LIST_SIZE; j++)
  1176.     {
  1177.       listX[i][j] = no_reference_picture;
  1178.     }
  1179.     listXsize[i]=0;
  1180.   }
  1181.   for (i=0; i<listXsize[0]; i++)
  1182.   {
  1183.     listX[2][2*i  ] = listX[0][i]->top_field;
  1184.     listX[2][2*i+1] = listX[0][i]->bottom_field;
  1185.     listX[4][2*i  ] = listX[0][i]->bottom_field;
  1186.     listX[4][2*i+1] = listX[0][i]->top_field;
  1187.   }
  1188.   listXsize[2]=listXsize[4]=listXsize[0] * 2;
  1189.   for (i=0; i<listXsize[1]; i++)
  1190.   {
  1191.     listX[3][2*i  ] = listX[1][i]->top_field;
  1192.     listX[3][2*i+1] = listX[1][i]->bottom_field;
  1193.     listX[5][2*i  ] = listX[1][i]->bottom_field;
  1194.     listX[5][2*i+1] = listX[1][i]->top_field;
  1195.   }
  1196.   listXsize[3]=listXsize[5]=listXsize[1] * 2;
  1197. }
  1198.  /*!
  1199.  ************************************************************************
  1200.  * brief
  1201.  *    Returns short term pic with given picNum
  1202.  *
  1203.  ************************************************************************
  1204.  */
  1205. static StorablePicture*  get_short_term_pic(int picNum)
  1206. {
  1207.   unsigned i;
  1208.   for (i=0; i<dpb.ref_frames_in_buffer; i++)
  1209.   {
  1210.     if (img->structure==FRAME)
  1211.     {
  1212.       if (dpb.fs_ref[i]->is_reference == 3)
  1213.         if ((!dpb.fs_ref[i]->frame->is_long_term)&&(dpb.fs_ref[i]->frame->pic_num == picNum))
  1214.           return dpb.fs_ref[i]->frame;
  1215.     }
  1216.     else
  1217.     {
  1218.       if (dpb.fs_ref[i]->is_reference & 1)
  1219.         if ((!dpb.fs_ref[i]->top_field->is_long_term)&&(dpb.fs_ref[i]->top_field->pic_num == picNum))
  1220.           return dpb.fs_ref[i]->top_field;
  1221.       if (dpb.fs_ref[i]->is_reference & 2)
  1222.         if ((!dpb.fs_ref[i]->bottom_field->is_long_term)&&(dpb.fs_ref[i]->bottom_field->pic_num == picNum))
  1223.           return dpb.fs_ref[i]->bottom_field;
  1224.     }
  1225.   }
  1226.   return no_reference_picture;
  1227. }
  1228. /*!
  1229.  ************************************************************************
  1230.  * brief
  1231.  *    Returns short term pic with given LongtermPicNum
  1232.  *
  1233.  ************************************************************************
  1234.  */
  1235. static StorablePicture*  get_long_term_pic(int LongtermPicNum)
  1236. {
  1237.   unsigned i;
  1238.   for (i=0; i<dpb.ltref_frames_in_buffer; i++)
  1239.   {
  1240.     if (img->structure==FRAME)
  1241.     {
  1242.       if (dpb.fs_ltref[i]->is_reference == 3)
  1243.         if ((dpb.fs_ltref[i]->frame->is_long_term)&&(dpb.fs_ltref[i]->frame->long_term_pic_num == LongtermPicNum))
  1244.           return dpb.fs_ltref[i]->frame;
  1245.     }
  1246.     else
  1247.     {
  1248.       if (dpb.fs_ltref[i]->is_reference & 1)
  1249.         if ((dpb.fs_ltref[i]->top_field->is_long_term)&&(dpb.fs_ltref[i]->top_field->long_term_pic_num == LongtermPicNum))
  1250.           return dpb.fs_ltref[i]->top_field;
  1251.       if (dpb.fs_ltref[i]->is_reference & 2)
  1252.         if ((dpb.fs_ltref[i]->bottom_field->is_long_term)&&(dpb.fs_ltref[i]->bottom_field->long_term_pic_num == LongtermPicNum))
  1253.           return dpb.fs_ltref[i]->bottom_field;
  1254.     }
  1255.   }
  1256.   return NULL;
  1257. }
  1258. /*!
  1259.  ************************************************************************
  1260.  * brief
  1261.  *    Reordering process for short-term reference pictures
  1262.  *
  1263.  ************************************************************************
  1264.  */
  1265. static void reorder_short_term(StorablePicture **RefPicListX, int num_ref_idx_lX_active_minus1, int picNumLX, int *refIdxLX)
  1266. {
  1267.   int cIdx, nIdx;
  1268.   StorablePicture *picLX;
  1269.   picLX = get_short_term_pic(picNumLX);
  1270.   for( cIdx = num_ref_idx_lX_active_minus1+1; cIdx > *refIdxLX; cIdx-- )
  1271.     RefPicListX[ cIdx ] = RefPicListX[ cIdx - 1];
  1272.   RefPicListX[ (*refIdxLX)++ ] = picLX;
  1273.   nIdx = *refIdxLX;
  1274.   for( cIdx = *refIdxLX; cIdx <= num_ref_idx_lX_active_minus1+1; cIdx++ )
  1275.     if (RefPicListX[ cIdx ])
  1276.       if( (RefPicListX[ cIdx ]->is_long_term ) ||  (RefPicListX[ cIdx ]->pic_num != picNumLX ))
  1277.         RefPicListX[ nIdx++ ] = RefPicListX[ cIdx ];
  1278. }
  1279. /*!
  1280.  ************************************************************************
  1281.  * brief
  1282.  *    Reordering process for short-term reference pictures
  1283.  *
  1284.  ************************************************************************
  1285.  */
  1286. static void reorder_long_term(StorablePicture **RefPicListX, int num_ref_idx_lX_active_minus1, int LongTermPicNum, int *refIdxLX)
  1287. {
  1288.   int cIdx, nIdx;
  1289.   StorablePicture *picLX;
  1290.   picLX = get_long_term_pic(LongTermPicNum);
  1291.   for( cIdx = num_ref_idx_lX_active_minus1+1; cIdx > *refIdxLX; cIdx-- )
  1292.     RefPicListX[ cIdx ] = RefPicListX[ cIdx - 1];
  1293.   RefPicListX[ (*refIdxLX)++ ] = picLX;
  1294.   nIdx = *refIdxLX;
  1295.   for( cIdx = *refIdxLX; cIdx <= num_ref_idx_lX_active_minus1+1; cIdx++ )
  1296.     if (RefPicListX[ cIdx ])
  1297.       if( (!RefPicListX[ cIdx ]->is_long_term ) ||  (RefPicListX[ cIdx ]->long_term_pic_num != LongTermPicNum ))
  1298.         RefPicListX[ nIdx++ ] = RefPicListX[ cIdx ];
  1299. }
  1300. /*!
  1301.  ************************************************************************
  1302.  * brief
  1303.  *    Reordering process for reference picture lists
  1304.  *
  1305.  ************************************************************************
  1306.  */
  1307. void reorder_ref_pic_list(StorablePicture **list, int *list_size, int num_ref_idx_lX_active_minus1, int *reordering_of_pic_nums_idc, int *abs_diff_pic_num_minus1, int *long_term_pic_idx)
  1308. {
  1309.   int i;
  1310.   int maxPicNum, currPicNum, picNumLXNoWrap, picNumLXPred, picNumLX;
  1311.   int refIdxLX = 0;
  1312.   if (img->structure==FRAME)
  1313.   {
  1314.     maxPicNum  = img->MaxFrameNum;
  1315.     currPicNum = img->frame_num;
  1316.   }
  1317.   else
  1318.   {
  1319.     maxPicNum  = 2 * img->MaxFrameNum;
  1320.     currPicNum = 2 * img->frame_num + 1;
  1321.   }
  1322.   picNumLXPred = currPicNum;
  1323.   for (i=0; reordering_of_pic_nums_idc[i]!=3; i++)
  1324.   {
  1325.     if (reordering_of_pic_nums_idc[i]>3)
  1326.       error ("Invalid remapping_of_pic_nums_idc command", 500);
  1327.     if (reordering_of_pic_nums_idc[i] < 2)
  1328.     {
  1329.       if (reordering_of_pic_nums_idc[i] == 0)
  1330.       {
  1331.         if( picNumLXPred - ( abs_diff_pic_num_minus1[i] + 1 ) < 0 )
  1332.           picNumLXNoWrap = picNumLXPred - ( abs_diff_pic_num_minus1[i] + 1 ) + maxPicNum;
  1333.         else
  1334.           picNumLXNoWrap = picNumLXPred - ( abs_diff_pic_num_minus1[i] + 1 );
  1335.       }
  1336.       else // (remapping_of_pic_nums_idc[i] == 1)
  1337.       {
  1338.         if( picNumLXPred + ( abs_diff_pic_num_minus1[i] + 1 )  >=  maxPicNum )
  1339.           picNumLXNoWrap = picNumLXPred + ( abs_diff_pic_num_minus1[i] + 1 ) - maxPicNum;
  1340.         else
  1341.           picNumLXNoWrap = picNumLXPred + ( abs_diff_pic_num_minus1[i] + 1 );
  1342.       }
  1343.       picNumLXPred = picNumLXNoWrap;
  1344.       if( picNumLXNoWrap > currPicNum )
  1345.         picNumLX = picNumLXNoWrap - maxPicNum;
  1346.       else
  1347.         picNumLX = picNumLXNoWrap;
  1348.       reorder_short_term(list, num_ref_idx_lX_active_minus1, picNumLX, &refIdxLX);
  1349.     }
  1350.     else //(remapping_of_pic_nums_idc[i] == 2)
  1351.     {
  1352.       reorder_long_term(list, num_ref_idx_lX_active_minus1, long_term_pic_idx[i], &refIdxLX);
  1353.     }
  1354.   }
  1355.   // that's a definition
  1356.   *list_size = num_ref_idx_lX_active_minus1 + 1;
  1357. }
  1358. /*!
  1359.  ************************************************************************
  1360.  * brief
  1361.  *    Update the list of frame stores that contain reference frames/fields
  1362.  *
  1363.  ************************************************************************
  1364.  */
  1365. void update_ref_list(void)
  1366. {
  1367.   unsigned i, j;
  1368.   for (i=0, j=0; i<dpb.used_size; i++)
  1369.   {
  1370.     if (is_short_term_reference(dpb.fs[i]))
  1371.     {
  1372.       dpb.fs_ref[j++]=dpb.fs[i];
  1373.     }
  1374.   }
  1375.   dpb.ref_frames_in_buffer = j;
  1376.   while (j<dpb.size)
  1377.   {
  1378.     dpb.fs_ref[j++]=NULL;
  1379.   }
  1380. }
  1381. /*!
  1382.  ************************************************************************
  1383.  * brief
  1384.  *    Update the list of frame stores that contain long-term reference
  1385.  *    frames/fields
  1386.  *
  1387.  ************************************************************************
  1388.  */
  1389. void update_ltref_list(void)
  1390. {
  1391.   unsigned i, j;
  1392.   for (i=0, j=0; i<dpb.used_size; i++)
  1393.   {
  1394.     if (is_long_term_reference(dpb.fs[i]))
  1395.     {
  1396.       dpb.fs_ltref[j++]=dpb.fs[i];
  1397.     }
  1398.   }
  1399.   dpb.ltref_frames_in_buffer=j;
  1400.   while (j<dpb.size)
  1401.   {
  1402.     dpb.fs_ltref[j++]=NULL;
  1403.   }
  1404. }
  1405. /*!
  1406.  ************************************************************************
  1407.  * brief
  1408.  *    Perform Memory management for idr pictures
  1409.  *
  1410.  ************************************************************************
  1411.  */
  1412. static void idr_memory_management(StorablePicture* p)
  1413. {
  1414.   unsigned i;
  1415.   assert (p->idr_flag);
  1416.   if (p->no_output_of_prior_pics_flag)
  1417.   {
  1418.     // free all stored pictures
  1419.     for (i=0; i<dpb.used_size; i++)
  1420.     {
  1421.       // reset all reference settings
  1422.       free_frame_store(dpb.fs[i]);
  1423.       dpb.fs[i] = alloc_frame_store();
  1424.     }
  1425.     for (i=0; i<dpb.ref_frames_in_buffer; i++)
  1426.     {
  1427.       dpb.fs_ref[i]=NULL;
  1428.     }
  1429.     for (i=0; i<dpb.ltref_frames_in_buffer; i++)
  1430.     {
  1431.       dpb.fs_ltref[i]=NULL;
  1432.     }
  1433.     dpb.used_size=0;
  1434.   }
  1435.   else
  1436.   {
  1437.     flush_dpb();
  1438.   }
  1439.   dpb.last_picture = NULL;
  1440.   update_ref_list();
  1441.   update_ltref_list();
  1442.   dpb.last_output_poc = INT_MIN;
  1443.   if (p->long_term_reference_flag)
  1444.   {
  1445.     dpb.max_long_term_pic_idx = 0;
  1446.     p->is_long_term           = 1;
  1447.     p->long_term_frame_idx    = 0;
  1448.   }
  1449.   else
  1450.   {
  1451.     dpb.max_long_term_pic_idx = -1;
  1452.     p->is_long_term           = 0;
  1453.   }
  1454. }
  1455. /*!
  1456.  ************************************************************************
  1457.  * brief
  1458.  *    Perform Sliding window decoded reference picture marking process
  1459.  *
  1460.  ************************************************************************
  1461.  */
  1462. static void sliding_window_memory_management(StorablePicture* p)
  1463. {
  1464.   unsigned i;
  1465.   assert (!p->idr_flag);
  1466.   // if this is a reference pic with sliding sliding window, unmark first ref frame
  1467.   if (dpb.ref_frames_in_buffer==dpb.num_ref_frames - dpb.ltref_frames_in_buffer)
  1468.   {
  1469.     for (i=0; i<dpb.used_size;i++)
  1470.     {
  1471.       if (dpb.fs[i]->is_reference  && (!(dpb.fs[i]->is_long_term)))
  1472.       {
  1473.         unmark_for_reference(dpb.fs[i]);
  1474.         update_ref_list();
  1475.         break;
  1476.       }
  1477.     }
  1478.   }
  1479.   p->is_long_term = 0;
  1480. }
  1481. /*!
  1482.  ************************************************************************
  1483.  * brief
  1484.  *    Calculate picNumX
  1485.  ************************************************************************
  1486.  */
  1487. static int get_pic_num_x (StorablePicture *p, int difference_of_pic_nums_minus1)
  1488. {
  1489.   int currPicNum;
  1490.   if (p->structure == FRAME)
  1491.     currPicNum = p->frame_num;
  1492.   else
  1493.     currPicNum = 2 * p->frame_num + 1;
  1494.   return currPicNum - (difference_of_pic_nums_minus1 + 1);
  1495. }
  1496. /*!
  1497.  ************************************************************************
  1498.  * brief
  1499.  *    Adaptive Memory Management: Mark short term picture unused
  1500.  ************************************************************************
  1501.  */
  1502. static void mm_unmark_short_term_for_reference(StorablePicture *p, int difference_of_pic_nums_minus1)
  1503. {
  1504.   int picNumX;
  1505.   unsigned i;
  1506.   picNumX = get_pic_num_x(p, difference_of_pic_nums_minus1);
  1507.   for (i=0; i<dpb.ref_frames_in_buffer; i++)
  1508.   {
  1509.     if (p->structure == FRAME)
  1510.     {
  1511.       if ((dpb.fs_ref[i]->is_reference==3) && (dpb.fs_ref[i]->is_long_term==0))
  1512.       {
  1513.         if (dpb.fs_ref[i]->frame->pic_num == picNumX)
  1514.         {
  1515.           unmark_for_reference(dpb.fs_ref[i]);
  1516.           return;
  1517.         }
  1518.       }
  1519.     }
  1520.     else
  1521.     {
  1522.       if ((dpb.fs_ref[i]->is_reference & 1) && (!(dpb.fs_ref[i]->is_long_term & 1)))
  1523.       {
  1524.         if (dpb.fs_ref[i]->top_field->pic_num == picNumX)
  1525.         {
  1526.           dpb.fs_ref[i]->top_field->used_for_reference = 0;
  1527.           dpb.fs_ref[i]->is_reference &= 2;
  1528.           if (dpb.fs_ref[i]->is_used == 3)
  1529.           {
  1530.             dpb.fs_ref[i]->frame->used_for_reference = 0;
  1531.           }
  1532.           return;
  1533.         }
  1534.       }
  1535.       if ((dpb.fs_ref[i]->is_reference & 2) && (!(dpb.fs_ref[i]->is_long_term & 2)))
  1536.       {
  1537.         if (dpb.fs_ref[i]->bottom_field->pic_num == picNumX)
  1538.         {
  1539.           dpb.fs_ref[i]->bottom_field->used_for_reference = 0;
  1540.           dpb.fs_ref[i]->is_reference &= 1;
  1541.           if (dpb.fs_ref[i]->is_used == 3)
  1542.           {
  1543.             dpb.fs_ref[i]->frame->used_for_reference = 0;
  1544.           }
  1545.           return;
  1546.         }
  1547.       }
  1548.     }
  1549.   }
  1550. }
  1551. /*!
  1552.  ************************************************************************
  1553.  * brief
  1554.  *    Adaptive Memory Management: Mark long term picture unused
  1555.  ************************************************************************
  1556.  */
  1557. static void mm_unmark_long_term_for_reference(StorablePicture *p, int long_term_pic_num)
  1558. {
  1559.   unsigned i;
  1560.   for (i=0; i<dpb.ltref_frames_in_buffer; i++)
  1561.   {
  1562.     if (p->structure == FRAME)
  1563.     {
  1564.       if ((dpb.fs_ltref[i]->is_reference==3) && (dpb.fs_ltref[i]->is_long_term==3))
  1565.       {
  1566.         if (dpb.fs_ltref[i]->frame->long_term_pic_num == long_term_pic_num)
  1567.         {
  1568.           unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1569.         }
  1570.       }
  1571.     }
  1572.     else
  1573.     {
  1574.       if ((dpb.fs_ltref[i]->is_reference & 1) && ((dpb.fs_ltref[i]->is_long_term & 1)))
  1575.       {
  1576.         if (dpb.fs_ltref[i]->top_field->long_term_pic_num == long_term_pic_num)
  1577.         {
  1578.           dpb.fs_ltref[i]->top_field->used_for_reference = 0;
  1579.           dpb.fs_ltref[i]->top_field->is_long_term = 0;
  1580.           dpb.fs_ltref[i]->is_reference &= 2;
  1581.           dpb.fs_ltref[i]->is_long_term &= 2;
  1582.           if (dpb.fs_ltref[i]->is_used == 3)
  1583.           {
  1584.             dpb.fs_ltref[i]->frame->used_for_reference = 0;
  1585.             dpb.fs_ltref[i]->frame->is_long_term = 0;
  1586.           }
  1587.           return;
  1588.         }
  1589.       }
  1590.       if ((dpb.fs_ltref[i]->is_reference & 2) && ((dpb.fs_ltref[i]->is_long_term & 2)))
  1591.       {
  1592.         if (dpb.fs_ltref[i]->bottom_field->long_term_pic_num == long_term_pic_num)
  1593.         {
  1594.           dpb.fs_ltref[i]->bottom_field->used_for_reference = 0;
  1595.           dpb.fs_ltref[i]->bottom_field->is_long_term = 0;
  1596.           dpb.fs_ltref[i]->is_reference &= 1;
  1597.           dpb.fs_ltref[i]->is_long_term &= 1;
  1598.           if (dpb.fs_ltref[i]->is_used == 3)
  1599.           {
  1600.             dpb.fs_ltref[i]->frame->used_for_reference = 0;
  1601.             dpb.fs_ltref[i]->frame->is_long_term = 0;
  1602.           }
  1603.           return;
  1604.         }
  1605.       }
  1606.     }
  1607.   }
  1608. }
  1609. /*!
  1610.  ************************************************************************
  1611.  * brief
  1612.  *    Mark a long-term reference frame or complementary field pair unused for referemce
  1613.  ************************************************************************
  1614.  */
  1615. static void unmark_long_term_frame_for_reference_by_frame_idx(int long_term_frame_idx)
  1616. {
  1617.   unsigned i;
  1618.   for(i=0; i<dpb.ltref_frames_in_buffer; i++)
  1619.   {
  1620.     if (dpb.fs_ltref[i]->long_term_frame_idx == long_term_frame_idx)
  1621.       unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1622.   }
  1623. }
  1624. /*!
  1625.  ************************************************************************
  1626.  * brief
  1627.  *    Mark a long-term reference field unused for reference only if it's not
  1628.  *    the complementary field of the picture indicated by picNumX
  1629.  ************************************************************************
  1630.  */
  1631. static void unmark_long_term_field_for_reference_by_frame_idx(PictureStructure structure, int long_term_frame_idx, int mark_current, unsigned curr_frame_num, int curr_pic_num)
  1632. {
  1633.   unsigned i;
  1634.   assert(structure!=FRAME);
  1635.   if (curr_pic_num<0)
  1636.     curr_pic_num+=(2*img->MaxFrameNum);
  1637.   for(i=0; i<dpb.ltref_frames_in_buffer; i++)
  1638.   {
  1639.     if (dpb.fs_ltref[i]->long_term_frame_idx == long_term_frame_idx)
  1640.     {
  1641.       if (structure == TOP_FIELD)
  1642.       {
  1643.         if ((dpb.fs_ltref[i]->is_long_term == 3))
  1644.         {
  1645.           unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1646.         }
  1647.         else
  1648.         {
  1649.           if ((dpb.fs_ltref[i]->is_long_term == 1))
  1650.           {
  1651.             unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1652.           }
  1653.           else
  1654.           {
  1655.             if (mark_current)
  1656.             {
  1657.               if (dpb.last_picture)
  1658.               {
  1659.                 if ( ( dpb.last_picture != dpb.fs_ltref[i] )|| dpb.last_picture->frame_num != curr_frame_num)
  1660.                   unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1661.               }
  1662.               else
  1663.               {
  1664.                 unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1665.               }
  1666.             }
  1667.             else
  1668.             {
  1669.               if ((dpb.fs_ltref[i]->frame_num) != (unsigned)(curr_pic_num/2))
  1670.               {
  1671.                 unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1672.               }
  1673.             }
  1674.           }
  1675.         }
  1676.       }
  1677.       if (structure == BOTTOM_FIELD)
  1678.       {
  1679.         if ((dpb.fs_ltref[i]->is_long_term == 3))
  1680.         {
  1681.           unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1682.         }
  1683.         else
  1684.         {
  1685.           if ((dpb.fs_ltref[i]->is_long_term == 2))
  1686.           {
  1687.             unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1688.           }
  1689.           else
  1690.           {
  1691.             if (mark_current)
  1692.             {
  1693.               if (dpb.last_picture)
  1694.               {
  1695.                 if ( ( dpb.last_picture != dpb.fs_ltref[i] )|| dpb.last_picture->frame_num != curr_frame_num)
  1696.                   unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1697.               }
  1698.               else
  1699.               {
  1700.                 unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1701.               }
  1702.             }
  1703.             else
  1704.             {
  1705.               if ((dpb.fs_ltref[i]->frame_num) != (unsigned)(curr_pic_num/2))
  1706.               {
  1707.                 unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1708.               }
  1709.             }
  1710.           }
  1711.         }
  1712.       }
  1713.     }
  1714.   }
  1715. }
  1716. /*!
  1717.  ************************************************************************
  1718.  * brief
  1719.  *    mark a picture as long-term reference
  1720.  ************************************************************************
  1721.  */
  1722. static void mark_pic_long_term(StorablePicture* p, int long_term_frame_idx, int picNumX)
  1723. {
  1724.   unsigned i;
  1725.   int add_top, add_bottom;
  1726.   if (p->structure == FRAME)
  1727.   {
  1728.     for (i=0; i<dpb.ref_frames_in_buffer; i++)
  1729.     {
  1730.       if (dpb.fs_ref[i]->is_reference == 3)
  1731.       {
  1732.         if ((!dpb.fs_ref[i]->frame->is_long_term)&&(dpb.fs_ref[i]->frame->pic_num == picNumX))
  1733.         {
  1734.           dpb.fs_ref[i]->long_term_frame_idx = dpb.fs_ref[i]->frame->long_term_frame_idx
  1735.                                              = long_term_frame_idx;
  1736.           dpb.fs_ref[i]->frame->long_term_pic_num = long_term_frame_idx;
  1737.           dpb.fs_ref[i]->frame->is_long_term = 1;
  1738.           if (dpb.fs_ref[i]->top_field && dpb.fs_ref[i]->bottom_field)
  1739.           {
  1740.             dpb.fs_ref[i]->top_field->long_term_frame_idx = dpb.fs_ref[i]->bottom_field->long_term_frame_idx
  1741.                                                           = long_term_frame_idx;
  1742.             dpb.fs_ref[i]->top_field->long_term_pic_num = long_term_frame_idx;
  1743.             dpb.fs_ref[i]->bottom_field->long_term_pic_num = long_term_frame_idx;
  1744.             dpb.fs_ref[i]->top_field->is_long_term = dpb.fs_ref[i]->bottom_field->is_long_term
  1745.                                                    = 1;
  1746.           }
  1747.           dpb.fs_ref[i]->is_long_term = 3;
  1748.           return;
  1749.         }
  1750.       }
  1751.     }
  1752.     printf ("Warning: reference frame for long term marking not foundn");
  1753.   }
  1754.   else
  1755.   {
  1756.     if (p->structure == TOP_FIELD)
  1757.     {
  1758.       add_top    = 1;
  1759.       add_bottom = 0;
  1760.     }
  1761.     else
  1762.     {
  1763.       add_top    = 0;
  1764.       add_bottom = 1;
  1765.     }
  1766.     for (i=0; i<dpb.ref_frames_in_buffer; i++)
  1767.     {
  1768.       if (dpb.fs_ref[i]->is_reference & 1)
  1769.       {
  1770.         if ((!dpb.fs_ref[i]->top_field->is_long_term)&&(dpb.fs_ref[i]->top_field->pic_num == picNumX))
  1771.         {
  1772.           if ((dpb.fs_ref[i]->is_long_term) && (dpb.fs_ref[i]->long_term_frame_idx != long_term_frame_idx))
  1773.           {
  1774.               printf ("Warning: assigning long_term_frame_idx different from other fieldn");
  1775.           }
  1776.           dpb.fs_ref[i]->long_term_frame_idx = dpb.fs_ref[i]->top_field->long_term_frame_idx
  1777.                                              = long_term_frame_idx;
  1778.           dpb.fs_ref[i]->top_field->long_term_pic_num = 2 * long_term_frame_idx + add_top;
  1779.           dpb.fs_ref[i]->top_field->is_long_term = 1;
  1780.           dpb.fs_ref[i]->is_long_term |= 1;
  1781.           if (dpb.fs_ref[i]->is_long_term == 3)
  1782.           {
  1783.             dpb.fs_ref[i]->frame->is_long_term = 1;
  1784.             dpb.fs_ref[i]->frame->long_term_frame_idx = dpb.fs_ref[i]->frame->long_term_pic_num = long_term_frame_idx;
  1785.           }
  1786.           return;
  1787.         }
  1788.       }
  1789.       if (dpb.fs_ref[i]->is_reference & 2)
  1790.       {
  1791.         if ((!dpb.fs_ref[i]->bottom_field->is_long_term)&&(dpb.fs_ref[i]->bottom_field->pic_num == picNumX))
  1792.         {
  1793.           if ((dpb.fs_ref[i]->is_long_term) && (dpb.fs_ref[i]->long_term_frame_idx != long_term_frame_idx))
  1794.           {
  1795.               printf ("Warning: assigning long_term_frame_idx different from other fieldn");
  1796.           }
  1797.           dpb.fs_ref[i]->long_term_frame_idx = dpb.fs_ref[i]->bottom_field->long_term_frame_idx
  1798.                                              = long_term_frame_idx;
  1799.           dpb.fs_ref[i]->bottom_field->long_term_pic_num = 2 * long_term_frame_idx + add_bottom;
  1800.           dpb.fs_ref[i]->bottom_field->is_long_term = 1;
  1801.           dpb.fs_ref[i]->is_long_term |= 2;
  1802.           if (dpb.fs_ref[i]->is_long_term == 3)
  1803.           {
  1804.             dpb.fs_ref[i]->frame->is_long_term = 1;
  1805.             dpb.fs_ref[i]->frame->long_term_frame_idx = dpb.fs_ref[i]->frame->long_term_pic_num = long_term_frame_idx;
  1806.           }
  1807.           return;
  1808.         }
  1809.       }
  1810.     }
  1811.     printf ("Warning: reference field for long term marking not foundn");
  1812.   }
  1813. }
  1814. /*!
  1815.  ************************************************************************
  1816.  * brief
  1817.  *    Assign a long term frame index to a short term picture
  1818.  ************************************************************************
  1819.  */
  1820. static void mm_assign_long_term_frame_idx(StorablePicture* p, int difference_of_pic_nums_minus1, int long_term_frame_idx)
  1821. {
  1822.   int picNumX;
  1823.   picNumX = get_pic_num_x(p, difference_of_pic_nums_minus1);
  1824.   // remove frames/fields with same long_term_frame_idx
  1825.   if (p->structure == FRAME)
  1826.   {
  1827.     unmark_long_term_frame_for_reference_by_frame_idx(long_term_frame_idx);
  1828.   }
  1829.   else
  1830.   {
  1831.     unsigned i;
  1832.     PictureStructure structure = FRAME;
  1833.     for (i=0; i<dpb.ref_frames_in_buffer; i++)
  1834.     {
  1835.       if (dpb.fs_ref[i]->is_reference & 1)
  1836.       {
  1837.         if (dpb.fs_ref[i]->top_field->pic_num == picNumX)
  1838.         {
  1839.           structure = TOP_FIELD;
  1840.           break;
  1841.         }
  1842.       }
  1843.       if (dpb.fs_ref[i]->is_reference & 2)
  1844.       {
  1845.         if (dpb.fs_ref[i]->bottom_field->pic_num == picNumX)
  1846.         {
  1847.           structure = BOTTOM_FIELD;
  1848.           break;
  1849.         }
  1850.       }
  1851.     }
  1852.     if (structure==FRAME)
  1853.     {
  1854.       error ("field for long term marking not found",200);
  1855.     }
  1856.     unmark_long_term_field_for_reference_by_frame_idx(structure, long_term_frame_idx, 0, 0, picNumX);
  1857.   }
  1858.   mark_pic_long_term(p, long_term_frame_idx, picNumX);
  1859. }
  1860. /*!
  1861.  ************************************************************************
  1862.  * brief
  1863.  *    Set new max long_term_frame_idx
  1864.  ************************************************************************
  1865.  */
  1866. void mm_update_max_long_term_frame_idx(int max_long_term_frame_idx_plus1)
  1867. {
  1868.   unsigned i;
  1869.   dpb.max_long_term_pic_idx = max_long_term_frame_idx_plus1 - 1;
  1870.   // check for invalid frames
  1871.   for (i=0; i<dpb.ltref_frames_in_buffer; i++)
  1872.   {
  1873.     if (dpb.fs_ltref[i]->long_term_frame_idx > dpb.max_long_term_pic_idx)
  1874.     {
  1875.       unmark_for_long_term_reference(dpb.fs_ltref[i]);
  1876.     }
  1877.   }
  1878. }
  1879. /*!
  1880.  ************************************************************************
  1881.  * brief
  1882.  *    Mark all long term reference pictures unused for reference
  1883.  ************************************************************************
  1884.  */
  1885. static void mm_unmark_all_long_term_for_reference (void)
  1886. {
  1887.   mm_update_max_long_term_frame_idx(0);
  1888. }
  1889. /*!
  1890.  ************************************************************************
  1891.  * brief
  1892.  *    Mark all short term reference pictures unused for reference
  1893.  ************************************************************************
  1894.  */
  1895. static void mm_unmark_all_short_term_for_reference (void)
  1896. {
  1897.   unsigned int i;
  1898.   for (i=0; i<dpb.ref_frames_in_buffer; i++)
  1899.   {
  1900.     unmark_for_reference(dpb.fs_ref[i]);
  1901.   }