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

Audio

开发平台:

Visual C++

  1.   update_ref_list();
  2. }
  3. /*!
  4.  ************************************************************************
  5.  * brief
  6.  *    Mark the current picture used for long term reference
  7.  ************************************************************************
  8.  */
  9. static void mm_mark_current_picture_long_term(StorablePicture *p, int long_term_frame_idx)
  10. {
  11.   // remove long term pictures with same long_term_frame_idx
  12.   if (p->structure == FRAME)
  13.   {
  14.     unmark_long_term_frame_for_reference_by_frame_idx(long_term_frame_idx);
  15.   }
  16.   else
  17.   {
  18.     unmark_long_term_field_for_reference_by_frame_idx(p->structure, long_term_frame_idx, 1, p->pic_num, 0);
  19.   }
  20.   p->is_long_term = 1;
  21.   p->long_term_frame_idx = long_term_frame_idx;
  22. }
  23. /*!
  24.  ************************************************************************
  25.  * brief
  26.  *    Perform Adaptive memory control decoded reference picture marking process
  27.  ************************************************************************
  28.  */
  29. static void adaptive_memory_management(StorablePicture* p)
  30. {
  31.   DecRefPicMarking_t *tmp_drpm;
  32.   img->last_has_mmco_5 = 0;
  33.   assert (!p->idr_flag);
  34.   assert (p->adaptive_ref_pic_buffering_flag);
  35.   while (p->dec_ref_pic_marking_buffer)
  36.   {
  37.     tmp_drpm = p->dec_ref_pic_marking_buffer;
  38.     switch (tmp_drpm->memory_management_control_operation)
  39.     {
  40.       case 0:
  41.         if (tmp_drpm->Next != NULL)
  42.         {
  43.           error ("memory_management_control_operation = 0 not last operation in buffer", 500);
  44.         }
  45.         break;
  46.       case 1:
  47.         mm_unmark_short_term_for_reference(p, tmp_drpm->difference_of_pic_nums_minus1);
  48.         update_ref_list();
  49.         break;
  50.       case 2:
  51.         mm_unmark_long_term_for_reference(p, tmp_drpm->long_term_pic_num);
  52.         update_ltref_list();
  53.         break;
  54.       case 3:
  55.         mm_assign_long_term_frame_idx(p, tmp_drpm->difference_of_pic_nums_minus1, tmp_drpm->long_term_frame_idx);
  56.         update_ref_list();
  57.         update_ltref_list();
  58.         break;
  59.       case 4:
  60.         mm_update_max_long_term_frame_idx (tmp_drpm->max_long_term_frame_idx_plus1);
  61.         update_ltref_list();
  62.         break;
  63.       case 5:
  64.         mm_unmark_all_short_term_for_reference();
  65.         mm_unmark_all_long_term_for_reference();
  66.        img->last_has_mmco_5 = 1;
  67.         break;
  68.       case 6:
  69.         mm_mark_current_picture_long_term(p, tmp_drpm->long_term_frame_idx);
  70.         check_num_ref();
  71.         break;
  72.       default:
  73.         error ("invalid memory_management_control_operation in buffer", 500);
  74.     }
  75.     p->dec_ref_pic_marking_buffer = tmp_drpm->Next;
  76.     free (tmp_drpm);
  77.   }
  78.   if ( img->last_has_mmco_5 )
  79.   {
  80.     p->pic_num = p->frame_num = 0;
  81.     switch (p->structure)
  82.     {
  83.     case TOP_FIELD:
  84.       {
  85.         p->poc = p->top_poc = img->toppoc =0;
  86.         break;
  87.       }
  88.     case BOTTOM_FIELD:
  89.       {
  90.         p->poc = p->bottom_poc = img->bottompoc = 0;
  91.         break;
  92.       }
  93.     case FRAME:
  94.       {
  95.         p->top_poc    -= p->poc;
  96.         p->bottom_poc -= p->poc;
  97.         img->toppoc = p->top_poc;
  98.         img->bottompoc = p->bottom_poc;
  99.         p->poc = imin (p->top_poc, p->bottom_poc);
  100.         img->framepoc = p->poc;
  101.         break;
  102.       }
  103.     }
  104.     img->ThisPOC = p->poc;
  105.     flush_dpb();
  106.   }
  107. }
  108. /*!
  109.  ************************************************************************
  110.  * brief
  111.  *    Store a picture in DPB. This includes cheking for space in DPB and
  112.  *    flushing frames.
  113.  *    If we received a frame, we need to check for a new store, if we
  114.  *    got a field, check if it's the second field of an already allocated
  115.  *    store.
  116.  *
  117.  * param p
  118.  *    Picture to be stored
  119.  *
  120.  ************************************************************************
  121.  */
  122. void store_picture_in_dpb(StorablePicture* p)
  123. {
  124.   unsigned i;
  125.   int poc, pos;
  126.   // picture error concealment
  127.   extern int pocs_in_dpb[100];
  128.   // diagnostics
  129.   //printf ("Storing (%s) non-ref pic with frame_num #%dn", (p->type == FRAME)?"FRAME":(p->type == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", p->pic_num);
  130.   // if frame, check for new store,
  131.   assert (p!=NULL);
  132.   img->last_has_mmco_5=0;
  133.   img->last_pic_bottom_field = (p->structure == BOTTOM_FIELD);
  134.   if (p->idr_flag)
  135.   {
  136.     idr_memory_management(p);
  137.   // picture error concealment
  138.     memset(pocs_in_dpb, 0, sizeof(int)*100);
  139.   }
  140.   else
  141.   {
  142.     // adaptive memory management
  143.     if (p->used_for_reference && (p->adaptive_ref_pic_buffering_flag))
  144.       adaptive_memory_management(p);
  145.   }
  146.   if ((p->structure==TOP_FIELD)||(p->structure==BOTTOM_FIELD))
  147.   {
  148.     // check for frame store with same pic_number
  149.     if (dpb.last_picture)
  150.     {
  151.       if ((int)dpb.last_picture->frame_num == p->pic_num)
  152.       {
  153.         if (((p->structure==TOP_FIELD)&&(dpb.last_picture->is_used==2))||((p->structure==BOTTOM_FIELD)&&(dpb.last_picture->is_used==1)))
  154.         {
  155.           if ((p->used_for_reference && (dpb.last_picture->is_orig_reference!=0))||
  156.               (!p->used_for_reference && (dpb.last_picture->is_orig_reference==0)))
  157.           {
  158.             insert_picture_in_dpb(dpb.last_picture, p);
  159.             update_ref_list();
  160.             update_ltref_list();
  161.             dump_dpb();
  162.             dpb.last_picture = NULL;
  163.             return;
  164.           }
  165.         }
  166.       }
  167.     }
  168.   }
  169.   // this is a frame or a field which has no stored complementary field
  170.   // sliding window, if necessary
  171.   if ((!p->idr_flag)&&(p->used_for_reference && (!p->adaptive_ref_pic_buffering_flag)))
  172.   {
  173.     sliding_window_memory_management(p);
  174.   }
  175.   // picture error concealment
  176.   if(img->conceal_mode != 0)
  177.     for(i=0;i<dpb.size;i++)
  178.       if(dpb.fs[i]->is_reference)
  179.         dpb.fs[i]->concealment_reference = 1;
  180.   // first try to remove unused frames
  181.   if (dpb.used_size==dpb.size)
  182.   {
  183.     // picture error concealment
  184.     if (img->conceal_mode != 0)
  185.       conceal_non_ref_pics(2);
  186.     remove_unused_frame_from_dpb();
  187.     if(img->conceal_mode != 0)
  188.       sliding_window_poc_management(p);
  189.   }
  190.   // then output frames until one can be removed
  191.   while (dpb.used_size==dpb.size)
  192.   {
  193.     // non-reference frames may be output directly
  194.     if (!p->used_for_reference)
  195.     {
  196.       get_smallest_poc(&poc, &pos);
  197.       if ((-1==pos) || (p->poc < poc))
  198.       {
  199.         direct_output(p, p_out);
  200.         return;
  201.       }
  202.     }
  203.     // flush a frame
  204.     output_one_frame_from_dpb();
  205.   }
  206.   // check for duplicate frame number in short term reference buffer
  207.   if ((p->used_for_reference)&&(!p->is_long_term))
  208.   {
  209.     for (i=0; i<dpb.ref_frames_in_buffer; i++)
  210.     {
  211.       if (dpb.fs_ref[i]->frame_num == p->frame_num)
  212.       {
  213.         error("duplicate frame_num in short-term reference picture buffer", 500);
  214.       }
  215.     }
  216.   }
  217.   // store at end of buffer
  218. //  printf ("store frame/field at pos %dn",dpb.used_size);
  219.   insert_picture_in_dpb(dpb.fs[dpb.used_size],p);
  220.   // picture error concealment
  221.   if (p->idr_flag)
  222.   {
  223.       img->earlier_missing_poc = 0;
  224.   }
  225.   if (p->structure != FRAME)
  226.   {
  227.     dpb.last_picture = dpb.fs[dpb.used_size];
  228.   }
  229.   else
  230.   {
  231.     dpb.last_picture = NULL;
  232.   }
  233.   dpb.used_size++;
  234.   if(img->conceal_mode != 0)
  235.       pocs_in_dpb[dpb.used_size-1] = p->poc;
  236.   update_ref_list();
  237.   update_ltref_list();
  238.   check_num_ref();
  239.   dump_dpb();
  240. }
  241. /*!
  242.  ************************************************************************
  243.  * brief
  244.  *    Insert the picture into the DPB. A free DPB position is necessary
  245.  *    for frames, .
  246.  *
  247.  * param fs
  248.  *    FrameStore into which the picture will be inserted
  249.  * param p
  250.  *    StorablePicture to be inserted
  251.  *
  252.  ************************************************************************
  253.  */
  254. static void insert_picture_in_dpb(FrameStore* fs, StorablePicture* p)
  255. {
  256. //  printf ("insert (%s) pic with frame_num #%d, poc %dn", (p->structure == FRAME)?"FRAME":(p->structure == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", p->pic_num, p->poc);
  257.   assert (p!=NULL);
  258.   assert (fs!=NULL);
  259.   switch (p->structure)
  260.   {
  261.   case FRAME:
  262.     fs->frame = p;
  263.     fs->is_used = 3;
  264.     if (p->used_for_reference)
  265.     {
  266.       fs->is_reference = 3;
  267.       fs->is_orig_reference = 3;
  268.       if (p->is_long_term)
  269.       {
  270.         fs->is_long_term = 3;
  271.         fs->long_term_frame_idx = p->long_term_frame_idx;
  272.       }
  273.     }
  274.     // generate field views
  275.     dpb_split_field(fs);
  276.     break;
  277.   case TOP_FIELD:
  278.     fs->top_field = p;
  279.     fs->is_used |= 1;
  280.     if (p->used_for_reference)
  281.     {
  282.       fs->is_reference |= 1;
  283.       fs->is_orig_reference |= 1;
  284.       if (p->is_long_term)
  285.       {
  286.         fs->is_long_term |= 1;
  287.         fs->long_term_frame_idx = p->long_term_frame_idx;
  288.       }
  289.     }
  290.     if (fs->is_used == 3)
  291.     {
  292.       // generate frame view
  293.       dpb_combine_field(fs);
  294.     } else
  295.     {
  296.       fs->poc = p->poc;
  297.       gen_field_ref_ids(p);
  298.     }
  299.     break;
  300.   case BOTTOM_FIELD:
  301.     fs->bottom_field = p;
  302.     fs->is_used |= 2;
  303.     if (p->used_for_reference)
  304.     {
  305.       fs->is_reference |= 2;
  306.       fs->is_orig_reference |= 2;
  307.       if (p->is_long_term)
  308.       {
  309.         fs->is_long_term |= 2;
  310.         fs->long_term_frame_idx = p->long_term_frame_idx;
  311.       }
  312.     }
  313.     if (fs->is_used == 3)
  314.     {
  315.       // generate frame view
  316.       dpb_combine_field(fs);
  317.     } 
  318.     else
  319.     {
  320.       fs->poc = p->poc;
  321.       gen_field_ref_ids(p);
  322.     }
  323.     break;
  324.   }
  325.   fs->frame_num = p->pic_num;
  326.   fs->recovery_frame = p->recovery_frame;
  327.   fs->is_output = p->is_output;
  328.   if (fs->is_used==3)
  329.   {
  330.     calculate_frame_no(p);
  331.     if (-1!=p_ref && !params->silent)
  332.       find_snr(snr, fs->frame, p_ref);
  333.   }
  334. }
  335. /*!
  336.  ************************************************************************
  337.  * brief
  338.  *    Check if one of the frames/fields in frame store is used for reference
  339.  ************************************************************************
  340.  */
  341. static int is_used_for_reference(FrameStore* fs)
  342. {
  343.   if (fs->is_reference)
  344.   {
  345.     return 1;
  346.   }
  347.   if (fs->is_used == 3) // frame
  348.   {
  349.     if (fs->frame->used_for_reference)
  350.     {
  351.       return 1;
  352.     }
  353.   }
  354.   if (fs->is_used & 1) // top field
  355.   {
  356.     if (fs->top_field)
  357.     {
  358.       if (fs->top_field->used_for_reference)
  359.       {
  360.         return 1;
  361.       }
  362.     }
  363.   }
  364.   if (fs->is_used & 2) // bottom field
  365.   {
  366.     if (fs->bottom_field)
  367.     {
  368.       if (fs->bottom_field->used_for_reference)
  369.       {
  370.         return 1;
  371.       }
  372.     }
  373.   }
  374.   return 0;
  375. }
  376. /*!
  377.  ************************************************************************
  378.  * brief
  379.  *    Check if one of the frames/fields in frame store is used for short-term reference
  380.  ************************************************************************
  381.  */
  382. static int is_short_term_reference(FrameStore* fs)
  383. {
  384.   if (fs->is_used==3) // frame
  385.   {
  386.     if ((fs->frame->used_for_reference)&&(!fs->frame->is_long_term))
  387.     {
  388.       return 1;
  389.     }
  390.   }
  391.   if (fs->is_used & 1) // top field
  392.   {
  393.     if (fs->top_field)
  394.     {
  395.       if ((fs->top_field->used_for_reference)&&(!fs->top_field->is_long_term))
  396.       {
  397.         return 1;
  398.       }
  399.     }
  400.   }
  401.   if (fs->is_used & 2) // bottom field
  402.   {
  403.     if (fs->bottom_field)
  404.     {
  405.       if ((fs->bottom_field->used_for_reference)&&(!fs->bottom_field->is_long_term))
  406.       {
  407.         return 1;
  408.       }
  409.     }
  410.   }
  411.   return 0;
  412. }
  413. /*!
  414.  ************************************************************************
  415.  * brief
  416.  *    Check if one of the frames/fields in frame store is used for short-term reference
  417.  ************************************************************************
  418.  */
  419. static int is_long_term_reference(FrameStore* fs)
  420. {
  421.   if (fs->is_used==3) // frame
  422.   {
  423.     if ((fs->frame->used_for_reference)&&(fs->frame->is_long_term))
  424.     {
  425.       return 1;
  426.     }
  427.   }
  428.   if (fs->is_used & 1) // top field
  429.   {
  430.     if (fs->top_field)
  431.     {
  432.       if ((fs->top_field->used_for_reference)&&(fs->top_field->is_long_term))
  433.       {
  434.         return 1;
  435.       }
  436.     }
  437.   }
  438.   if (fs->is_used & 2) // bottom field
  439.   {
  440.     if (fs->bottom_field)
  441.     {
  442.       if ((fs->bottom_field->used_for_reference)&&(fs->bottom_field->is_long_term))
  443.       {
  444.         return 1;
  445.       }
  446.     }
  447.   }
  448.   return 0;
  449. }
  450. /*!
  451.  ************************************************************************
  452.  * brief
  453.  *    remove one frame from DPB
  454.  ************************************************************************
  455.  */
  456. static void remove_frame_from_dpb(int pos)
  457. {
  458.   FrameStore* fs = dpb.fs[pos];
  459.   FrameStore* tmp;
  460.   unsigned i;
  461. //  printf ("remove frame with frame_num #%dn", fs->frame_num);
  462.   switch (fs->is_used)
  463.   {
  464.   case 3:
  465.     free_storable_picture(fs->frame);
  466.     free_storable_picture(fs->top_field);
  467.     free_storable_picture(fs->bottom_field);
  468.     fs->frame=NULL;
  469.     fs->top_field=NULL;
  470.     fs->bottom_field=NULL;
  471.     break;
  472.   case 2:
  473.     free_storable_picture(fs->bottom_field);
  474.     fs->bottom_field=NULL;
  475.     break;
  476.   case 1:
  477.     free_storable_picture(fs->top_field);
  478.     fs->top_field=NULL;
  479.     break;
  480.   case 0:
  481.     break;
  482.   default:
  483.     error("invalid frame store type",500);
  484.   }
  485.   fs->is_used = 0;
  486.   fs->is_long_term = 0;
  487.   fs->is_reference = 0;
  488.   fs->is_orig_reference = 0;
  489.   // move empty framestore to end of buffer
  490.   tmp = dpb.fs[pos];
  491.   for (i=pos; i<dpb.used_size-1;i++)
  492.   {
  493.     dpb.fs[i] = dpb.fs[i+1];
  494.   }
  495.   dpb.fs[dpb.used_size-1] = tmp;
  496.   dpb.used_size--;
  497. }
  498. /*!
  499.  ************************************************************************
  500.  * brief
  501.  *    find smallest POC in the DPB.
  502.  ************************************************************************
  503.  */
  504. static void get_smallest_poc(int *poc,int * pos)
  505. {
  506.   unsigned i;
  507.   if (dpb.used_size<1)
  508.   {
  509.     error("Cannot determine smallest POC, DPB empty.",150);
  510.   }
  511.   *pos=-1;
  512.   *poc = INT_MAX;
  513.   for (i=0; i<dpb.used_size; i++)
  514.   {
  515.     if ((*poc>dpb.fs[i]->poc)&&(!dpb.fs[i]->is_output))
  516.     {
  517.       *poc = dpb.fs[i]->poc;
  518.       *pos=i;
  519.     }
  520.   }
  521. }
  522. /*!
  523.  ************************************************************************
  524.  * brief
  525.  *    Remove a picture from DPB which is no longer needed.
  526.  ************************************************************************
  527.  */
  528. static int remove_unused_frame_from_dpb(void)
  529. {
  530.   unsigned i;
  531.   // check for frames that were already output and no longer used for reference
  532.   for (i=0; i<dpb.used_size; i++)
  533.   {
  534.     if (dpb.fs[i]->is_output && (!is_used_for_reference(dpb.fs[i])))
  535.     {
  536.       remove_frame_from_dpb(i);
  537.       return 1;
  538.     }
  539.   }
  540.   return 0;
  541. }
  542. /*!
  543.  ************************************************************************
  544.  * brief
  545.  *    Output one picture stored in the DPB.
  546.  ************************************************************************
  547.  */
  548. static void output_one_frame_from_dpb(void)
  549. {
  550.   int poc, pos;
  551.   //diagnostics
  552.   if (dpb.used_size<1)
  553.   {
  554.     error("Cannot output frame, DPB empty.",150);
  555.   }
  556.   // find smallest POC
  557.   get_smallest_poc(&poc, &pos);
  558.   if(pos==-1)
  559.   {
  560.     error("no frames for output available", 150);
  561.   }
  562.   // call the output function
  563. //  printf ("output frame with frame_num #%d, poc %d (dpb. dpb.size=%d, dpb.used_size=%d)n", dpb.fs[pos]->frame_num, dpb.fs[pos]->frame->poc, dpb.size, dpb.used_size);
  564.   // picture error concealment
  565.   if(img->conceal_mode != 0)
  566.   {
  567.     if(dpb.last_output_poc == 0)
  568.     {
  569.       write_lost_ref_after_idr(pos);
  570.     }
  571.     write_lost_non_ref_pic(poc, p_out);
  572.   }
  573. // JVT-P072 ends
  574.   write_stored_frame(dpb.fs[pos], p_out);
  575.   // picture error concealment
  576.   if(img->conceal_mode == 0)
  577.     if (dpb.last_output_poc >= poc)
  578.     {
  579.       error ("output POC must be in ascending order", 150);
  580.     }
  581.   dpb.last_output_poc = poc;
  582.   // free frame store and move empty store to end of buffer
  583.   if (!is_used_for_reference(dpb.fs[pos]))
  584.   {
  585.     remove_frame_from_dpb(pos);
  586.   }
  587. }
  588. /*!
  589.  ************************************************************************
  590.  * brief
  591.  *    All stored picture are output. Should be called to empty the buffer
  592.  ************************************************************************
  593.  */
  594. void flush_dpb(void)
  595. {
  596.   unsigned i;
  597.   //diagnostics
  598. //  printf("Flush remaining frames from dpb. dpb.size=%d, dpb.used_size=%dn",dpb.size,dpb.used_size);
  599. //  if(img->conceal_mode == 0)
  600.   if (img->conceal_mode != 0)
  601.     conceal_non_ref_pics(0);
  602.   // mark all frames unused
  603.   for (i=0; i<dpb.used_size; i++)
  604.   {
  605.     unmark_for_reference (dpb.fs[i]);
  606.   }
  607.   while (remove_unused_frame_from_dpb()) ;
  608.   // output frames in POC order
  609.   while (dpb.used_size)
  610.   {
  611.     output_one_frame_from_dpb();
  612.   }
  613.   dpb.last_output_poc = INT_MIN;
  614. }
  615. void gen_field_ref_ids(StorablePicture *p)
  616. {
  617.   int i,j, dummylist0, dummylist1;
  618.    //! Generate Frame parameters from field information.
  619.   for (i=0 ; i<p->size_x/4 ; i++)
  620.   {
  621.     for (j=0 ; j<p->size_y/4 ; j++)
  622.     {
  623.         dummylist0= p->motion.ref_idx[LIST_0][j][i];
  624.         dummylist1= p->motion.ref_idx[LIST_1][j][i];
  625.         //! association with id already known for fields.
  626.         p->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? p->ref_pic_num[p->slice_id[j>>2][i>>2]][LIST_0][dummylist0] : 0;
  627.         p->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? p->ref_pic_num[p->slice_id[j>>2][i>>2]][LIST_1][dummylist1] : 0;
  628.         p->motion.field_frame[j][i]=1;
  629.     }
  630.   }
  631. }
  632. /*!
  633.  ************************************************************************
  634.  * brief
  635.  *    Extract top field from a frame
  636.  ************************************************************************
  637.  */
  638. void dpb_split_field(FrameStore *fs)
  639. {
  640.   int i, j, ii, jj, jj4;
  641.   int idiv,jdiv;
  642.   int currentmb;
  643.   int dummylist0, dummylist1;
  644.   int twosz16 = 2 * (fs->frame->size_x >> 4);
  645.   fs->poc = fs->frame->poc;
  646.   if (!fs->frame->frame_mbs_only_flag)
  647.   {
  648.     fs->top_field    = alloc_storable_picture(TOP_FIELD,    fs->frame->size_x, fs->frame->size_y, fs->frame->size_x_cr, fs->frame->size_y_cr);
  649.     fs->bottom_field = alloc_storable_picture(BOTTOM_FIELD, fs->frame->size_x, fs->frame->size_y, fs->frame->size_x_cr, fs->frame->size_y_cr);
  650.     for (i = 0; i < (fs->frame->size_y>>1); i++)
  651.     {
  652.       memcpy(fs->top_field->imgY[i], fs->frame->imgY[i*2], fs->frame->size_x*sizeof(imgpel));
  653.     }
  654.     for (i = 0; i< (fs->frame->size_y_cr>>1); i++)
  655.     {
  656.       memcpy(fs->top_field->imgUV[0][i], fs->frame->imgUV[0][i*2], fs->frame->size_x_cr*sizeof(imgpel));
  657.       memcpy(fs->top_field->imgUV[1][i], fs->frame->imgUV[1][i*2], fs->frame->size_x_cr*sizeof(imgpel));
  658.     }
  659.     for (i = 0; i < (fs->frame->size_y>>1); i++)
  660.     {
  661.       memcpy(fs->bottom_field->imgY[i], fs->frame->imgY[i*2 + 1], fs->frame->size_x*sizeof(imgpel));
  662.     }
  663.     for (i = 0; i < (fs->frame->size_y_cr>>1); i++)
  664.     {
  665.       memcpy(fs->bottom_field->imgUV[0][i], fs->frame->imgUV[0][i*2 + 1], fs->frame->size_x_cr*sizeof(imgpel));
  666.       memcpy(fs->bottom_field->imgUV[1][i], fs->frame->imgUV[1][i*2 + 1], fs->frame->size_x_cr*sizeof(imgpel));
  667.     }
  668.     fs->top_field->poc = fs->frame->top_poc;
  669.     fs->bottom_field->poc =  fs->frame->bottom_poc;
  670.     fs->top_field->frame_poc =  fs->frame->frame_poc;
  671.     fs->top_field->bottom_poc =fs->bottom_field->bottom_poc =  fs->frame->bottom_poc;
  672.     fs->top_field->top_poc =fs->bottom_field->top_poc =  fs->frame->top_poc;
  673.     fs->bottom_field->frame_poc =  fs->frame->frame_poc;
  674.     fs->top_field->used_for_reference = fs->bottom_field->used_for_reference
  675.                                       = fs->frame->used_for_reference;
  676.     fs->top_field->is_long_term = fs->bottom_field->is_long_term
  677.                                 = fs->frame->is_long_term;
  678.     fs->long_term_frame_idx = fs->top_field->long_term_frame_idx
  679.                             = fs->bottom_field->long_term_frame_idx
  680.                             = fs->frame->long_term_frame_idx;
  681.     fs->top_field->coded_frame = fs->bottom_field->coded_frame = 1;
  682.     fs->top_field->MbaffFrameFlag = fs->bottom_field->MbaffFrameFlag
  683.                                   = fs->frame->MbaffFrameFlag;
  684.     fs->frame->top_field    = fs->top_field;
  685.     fs->frame->bottom_field = fs->bottom_field;
  686.     fs->top_field->bottom_field = fs->bottom_field;
  687.     fs->top_field->frame        = fs->frame;
  688.     fs->bottom_field->top_field = fs->top_field;
  689.     fs->bottom_field->frame     = fs->frame;
  690.     fs->top_field->chroma_format_idc = fs->bottom_field->chroma_format_idc = fs->frame->chroma_format_idc;
  691.     //store reference picture index
  692.     for (j=0; j<=fs->frame->max_slice_id; j++)
  693.     {
  694.       memcpy(&fs->top_field->ref_pic_num   [j][LIST_0][0], &fs->frame->ref_pic_num[j][2 + LIST_0][0], 66 * sizeof(int64));
  695.       memcpy(&fs->top_field->ref_pic_num   [j][LIST_1][0], &fs->frame->ref_pic_num[j][2 + LIST_1][0], 33 * sizeof(int64));            
  696.       memcpy(&fs->bottom_field->ref_pic_num[j][LIST_0][0], &fs->frame->ref_pic_num[j][4 + LIST_0][0], 33 * sizeof(int64));
  697.       memcpy(&fs->bottom_field->ref_pic_num[j][LIST_1][0], &fs->frame->ref_pic_num[j][4 + LIST_1][0], 33 * sizeof(int64));
  698.     }
  699.   }
  700.   else
  701.   {
  702.     fs->top_field=NULL;
  703.     fs->bottom_field=NULL;
  704.     fs->frame->top_field=NULL;
  705.     fs->frame->bottom_field=NULL;
  706.   }
  707.   for (j = 0; (j < fs->frame->size_y >> 2) ; j++)
  708.   {
  709.     jdiv = j >> 2;
  710.     for (i = 0 ; i < (fs->frame->size_x >> 2) ; i++)
  711.     {
  712.       idiv = (i >> 2);
  713.       currentmb = twosz16*(jdiv >> 1)+ (idiv)*2 + (jdiv & 0x01);
  714.       if (fs->frame->MbaffFrameFlag && fs->frame->motion.mb_field[currentmb])
  715.       {
  716.         int list_offset = currentmb%2? 4: 2;
  717.         dummylist0 = fs->frame->motion.ref_idx[LIST_0][j][i];
  718.         dummylist1 = fs->frame->motion.ref_idx[LIST_1][j][i];
  719.         //! association with id already known for fields.
  720.         fs->frame->motion.ref_id[LIST_0 + list_offset][j][i] = (dummylist0>=0)? fs->frame->ref_pic_num[fs->frame->slice_id[jdiv][idiv]][LIST_0 + list_offset][dummylist0] : 0;
  721.         fs->frame->motion.ref_id[LIST_1 + list_offset][j][i] = (dummylist1>=0)? fs->frame->ref_pic_num[fs->frame->slice_id[jdiv][idiv]][LIST_1 + list_offset][dummylist1] : 0;
  722.         //! need to make association with frames
  723.         fs->frame->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->frm_ref_pic_num[fs->frame->slice_id[jdiv][idiv]][LIST_0 + list_offset][dummylist0] : 0;
  724.         fs->frame->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->frm_ref_pic_num[fs->frame->slice_id[jdiv][idiv]][LIST_1 + list_offset][dummylist1] : 0;
  725.       }
  726.       else
  727.       {
  728.         dummylist0 = fs->frame->motion.ref_idx[LIST_0][j][i];
  729.         dummylist1 = fs->frame->motion.ref_idx[LIST_1][j][i];
  730.         fs->frame->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->ref_pic_num[fs->frame->slice_id[jdiv][idiv]][LIST_0][dummylist0] : -1;
  731.         fs->frame->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->ref_pic_num[fs->frame->slice_id[jdiv][idiv]][LIST_1][dummylist1] : -1;
  732.       }
  733.     }
  734.   }
  735.   if (!fs->frame->frame_mbs_only_flag && fs->frame->MbaffFrameFlag)
  736.   {
  737.     for (j=0 ; j< (fs->frame->size_y >> 3); j++)
  738.     {
  739.       jj = (j >> 2)*8 + (j & 0x03);
  740.       jj4 = jj + 4;
  741.       jdiv = (j >> 1);
  742.       for (i=0 ; i < (fs->frame->size_x>>2); i++)
  743.       {
  744.         idiv = (i >> 2);
  745.         currentmb = twosz16*(jdiv >> 1)+ (idiv)*2 + (jdiv & 0x01);
  746.         // Assign field mvs attached to MB-Frame buffer to the proper buffer
  747.         if (fs->frame->motion.mb_field[currentmb])
  748.         {
  749.           fs->bottom_field->motion.field_frame[j][i] = fs->top_field->motion.field_frame[j][i]=1;
  750.           fs->frame->motion.field_frame[2*j][i] = fs->frame->motion.field_frame[2*j+1][i]=1;
  751.           fs->bottom_field->motion.mv[LIST_0][j][i][0] = fs->frame->motion.mv[LIST_0][jj4][i][0];
  752.           fs->bottom_field->motion.mv[LIST_0][j][i][1] = fs->frame->motion.mv[LIST_0][jj4][i][1];
  753.           fs->bottom_field->motion.mv[LIST_1][j][i][0] = fs->frame->motion.mv[LIST_1][jj4][i][0];
  754.           fs->bottom_field->motion.mv[LIST_1][j][i][1] = fs->frame->motion.mv[LIST_1][jj4][i][1];
  755.           fs->bottom_field->motion.ref_idx[LIST_0][j][i] = fs->frame->motion.ref_idx[LIST_0][jj4][i];
  756.           fs->bottom_field->motion.ref_idx[LIST_1][j][i] = fs->frame->motion.ref_idx[LIST_1][jj4][i];
  757.           fs->bottom_field->motion.ref_id[LIST_0][j][i] = fs->frame->motion.ref_id[LIST_0+4][jj4][i];
  758.           fs->bottom_field->motion.ref_id[LIST_1][j][i] = fs->frame->motion.ref_id[LIST_1+4][jj4][i];
  759.           fs->top_field->motion.mv[LIST_0][j][i][0] = fs->frame->motion.mv[LIST_0][jj][i][0];
  760.           fs->top_field->motion.mv[LIST_0][j][i][1] = fs->frame->motion.mv[LIST_0][jj][i][1];
  761.           fs->top_field->motion.mv[LIST_1][j][i][0] = fs->frame->motion.mv[LIST_1][jj][i][0];
  762.           fs->top_field->motion.mv[LIST_1][j][i][1] = fs->frame->motion.mv[LIST_1][jj][i][1];
  763.           fs->top_field->motion.ref_idx[LIST_0][j][i] = fs->frame->motion.ref_idx[LIST_0][jj][i];
  764.           fs->top_field->motion.ref_idx[LIST_1][j][i] = fs->frame->motion.ref_idx[LIST_1][jj][i];
  765.           fs->top_field->motion.ref_id[LIST_0][j][i] = fs->frame->motion.ref_id[LIST_0+2][jj][i];
  766.           fs->top_field->motion.ref_id[LIST_1][j][i] = fs->frame->motion.ref_id[LIST_1+2][jj][i];
  767.         }
  768.       }
  769.     }
  770.   }
  771.   //! Generate field MVs from Frame MVs
  772.   if (!fs->frame->frame_mbs_only_flag)
  773.   {
  774.     for (j=0 ; j < (fs->frame->size_y >> 3) ; j++)
  775.     {
  776.       jj = 2* RSD(j);
  777.       jdiv = (j >> 1);
  778.       for (i=0 ; i < (fs->frame->size_x >> 2) ; i++)
  779.       {
  780.         ii = RSD(i);
  781.         idiv = (i >> 2);
  782.         currentmb = twosz16 * (jdiv >> 1)+ (idiv)*2 + (jdiv & 0x01);
  783.         if (!fs->frame->MbaffFrameFlag  || !fs->frame->motion.mb_field[currentmb])
  784.         {
  785.           fs->frame->motion.field_frame[2*j+1][i] = fs->frame->motion.field_frame[2*j][i]=0;
  786.           fs->top_field->motion.field_frame[j][i] = fs->bottom_field->motion.field_frame[j][i] = 0;
  787.           fs->top_field->motion.mv[LIST_0][j][i][0] = fs->bottom_field->motion.mv[LIST_0][j][i][0] = fs->frame->motion.mv[LIST_0][jj][ii][0];
  788.           fs->top_field->motion.mv[LIST_0][j][i][1] = fs->bottom_field->motion.mv[LIST_0][j][i][1] = fs->frame->motion.mv[LIST_0][jj][ii][1];
  789.           fs->top_field->motion.mv[LIST_1][j][i][0] = fs->bottom_field->motion.mv[LIST_1][j][i][0] = fs->frame->motion.mv[LIST_1][jj][ii][0];
  790.           fs->top_field->motion.mv[LIST_1][j][i][1] = fs->bottom_field->motion.mv[LIST_1][j][i][1] = fs->frame->motion.mv[LIST_1][jj][ii][1];
  791.           // Scaling of references is done here since it will not affect spatial direct (2*0 =0)
  792.           if (fs->frame->motion.ref_idx[LIST_0][jj][ii] == -1)
  793.             fs->top_field->motion.ref_idx[LIST_0][j][i] = fs->bottom_field->motion.ref_idx[LIST_0][j][i] = - 1;
  794.           else
  795.           {
  796.             dummylist0=fs->top_field->motion.ref_idx[LIST_0][j][i] = fs->bottom_field->motion.ref_idx[LIST_0][j][i] = fs->frame->motion.ref_idx[LIST_0][jj][ii] ;
  797.             fs->top_field   ->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->top_ref_pic_num[fs->frame->slice_id[jj>>2][ii>>2]][LIST_0][dummylist0] : 0;
  798.             fs->bottom_field->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->frame->bottom_ref_pic_num[fs->frame->slice_id[jj>>2][ii>>2]][LIST_0][dummylist0] : 0;
  799.           }
  800.           if (fs->frame->motion.ref_idx[LIST_1][jj][ii] == -1)
  801.             fs->top_field->motion.ref_idx[LIST_1][j][i] = fs->bottom_field->motion.ref_idx[LIST_1][j][i] = - 1;
  802.           else
  803.           {
  804.             dummylist1=fs->top_field->motion.ref_idx[LIST_1][j][i] = fs->bottom_field->motion.ref_idx[LIST_1][j][i] = fs->frame->motion.ref_idx[LIST_1][jj][ii];
  805.             fs->top_field   ->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->top_ref_pic_num[fs->frame->slice_id[jj>>2][ii>>2]][LIST_1][dummylist1] : 0;
  806.             fs->bottom_field->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->frame->bottom_ref_pic_num[fs->frame->slice_id[jj>>2][ii>>2]][LIST_1][dummylist1] : 0;
  807.           }
  808.         }
  809.         else
  810.         {
  811.           fs->frame->motion.field_frame[2*j+1][i] = fs->frame->motion.field_frame[2*j][i]= fs->frame->motion.mb_field[currentmb];
  812.         }
  813.       }
  814.     }
  815.   }
  816.   else
  817.   {
  818.     memset( &(fs->frame->motion.field_frame[0][0]), 0, (fs->frame->size_y * fs->frame->size_x >> 4) * sizeof(byte));
  819.   }
  820. }
  821. /*!
  822.  ************************************************************************
  823.  * brief
  824.  *    Generate a frame from top and bottom fields,
  825.  *    YUV components and display information only
  826.  ************************************************************************
  827.  */
  828. void dpb_combine_field_yuv(FrameStore *fs)
  829. {
  830.   int i;
  831.   fs->frame = alloc_storable_picture(FRAME, fs->top_field->size_x, fs->top_field->size_y*2, fs->top_field->size_x_cr, fs->top_field->size_y_cr*2);
  832.   for (i=0; i<fs->top_field->size_y; i++)
  833.   {
  834.     memcpy(fs->frame->imgY[i*2],     fs->top_field->imgY[i]   , fs->top_field->size_x*sizeof(imgpel));     // top field
  835.     memcpy(fs->frame->imgY[i*2 + 1], fs->bottom_field->imgY[i], fs->bottom_field->size_x*sizeof(imgpel)); // bottom field
  836.   }
  837.   for (i=0; i<fs->top_field->size_y_cr; i++)
  838.   {
  839.     memcpy(fs->frame->imgUV[0][i*2],     fs->top_field->imgUV[0][i],    fs->top_field->size_x_cr*sizeof(imgpel));
  840.     memcpy(fs->frame->imgUV[0][i*2 + 1], fs->bottom_field->imgUV[0][i], fs->bottom_field->size_x_cr*sizeof(imgpel));
  841.     memcpy(fs->frame->imgUV[1][i*2],     fs->top_field->imgUV[1][i],    fs->top_field->size_x_cr*sizeof(imgpel));
  842.     memcpy(fs->frame->imgUV[1][i*2 + 1], fs->bottom_field->imgUV[1][i], fs->bottom_field->size_x_cr*sizeof(imgpel));
  843.   }
  844.   fs->poc=fs->frame->poc =fs->frame->frame_poc = imin (fs->top_field->poc, fs->bottom_field->poc);
  845.   fs->bottom_field->frame_poc=fs->top_field->frame_poc=fs->frame->poc;
  846.   fs->bottom_field->top_poc=fs->frame->top_poc=fs->top_field->poc;
  847.   fs->top_field->bottom_poc=fs->frame->bottom_poc=fs->bottom_field->poc;
  848.   fs->frame->used_for_reference = (fs->top_field->used_for_reference && fs->bottom_field->used_for_reference );
  849.   fs->frame->is_long_term = (fs->top_field->is_long_term && fs->bottom_field->is_long_term );
  850.   if (fs->frame->is_long_term)
  851.     fs->frame->long_term_frame_idx = fs->long_term_frame_idx;
  852.   fs->frame->top_field    = fs->top_field;
  853.   fs->frame->bottom_field = fs->bottom_field;
  854.   fs->frame->coded_frame = 0;
  855.   fs->frame->chroma_format_idc = fs->top_field->chroma_format_idc;
  856.   fs->frame->frame_cropping_flag = fs->top_field->frame_cropping_flag;
  857.   if (fs->frame->frame_cropping_flag)
  858.   {
  859.     fs->frame->frame_cropping_rect_top_offset = fs->top_field->frame_cropping_rect_top_offset;
  860.     fs->frame->frame_cropping_rect_bottom_offset = fs->top_field->frame_cropping_rect_bottom_offset;
  861.     fs->frame->frame_cropping_rect_left_offset = fs->top_field->frame_cropping_rect_left_offset;
  862.     fs->frame->frame_cropping_rect_right_offset = fs->top_field->frame_cropping_rect_right_offset;
  863.   }
  864.   fs->top_field->frame = fs->bottom_field->frame = fs->frame;
  865. }
  866. /*!
  867.  ************************************************************************
  868.  * brief
  869.  *    Generate a frame from top and bottom fields
  870.  ************************************************************************
  871.  */
  872. void dpb_combine_field(FrameStore *fs)
  873. {
  874.   int i,j, jj, jj4;
  875.   int dummylist0, dummylist1;
  876.   dpb_combine_field_yuv(fs);
  877.   //combine field for frame
  878.   for (j=0; j<=(imax(fs->top_field->max_slice_id, fs->bottom_field->max_slice_id)); j++)
  879.   {
  880.     for (i=0;i<16;i++)
  881.     {
  882.       fs->frame->ref_pic_num[j][LIST_1][i]=  i64min ((fs->top_field->ref_pic_num[j][LIST_1][2*i]/2)*2, (fs->bottom_field->ref_pic_num[j][LIST_1][2*i]/2)*2);
  883.     }
  884.     for (i=0;i<16;i++)
  885.     {
  886.       fs->frame->ref_pic_num[j][LIST_0][i] = i64min((fs->top_field->ref_pic_num[j][LIST_0][2*i]/2)*2, (fs->bottom_field->ref_pic_num[j][LIST_0][2*i]/2)*2);
  887.     }
  888.   }
  889.    //! Use inference flag to remap mvs/references
  890.   //! Generate Frame parameters from field information.
  891.   for (j=0 ; j < (fs->top_field->size_y >> 2) ; j++)
  892.   {
  893.     jj = 8*(j >> 2) + (j & 0x03);
  894.     jj4 = jj + 4;
  895.     for (i=0 ; i< (fs->top_field->size_x >> 2) ; i++)
  896.     {
  897.       fs->frame->motion.field_frame[jj][i]= fs->frame->motion.field_frame[jj4][i]=1;
  898.       fs->frame->motion.mv[LIST_0][jj][i][0] = fs->top_field->motion.mv[LIST_0][j][i][0];
  899.       fs->frame->motion.mv[LIST_0][jj][i][1] = fs->top_field->motion.mv[LIST_0][j][i][1];
  900.       fs->frame->motion.mv[LIST_1][jj][i][0] = fs->top_field->motion.mv[LIST_1][j][i][0];
  901.       fs->frame->motion.mv[LIST_1][jj][i][1] = fs->top_field->motion.mv[LIST_1][j][i][1];
  902.       dummylist0=fs->frame->motion.ref_idx[LIST_0][jj][i]  = fs->top_field->motion.ref_idx[LIST_0][j][i];
  903.       dummylist1=fs->frame->motion.ref_idx[LIST_1][jj][i]  = fs->top_field->motion.ref_idx[LIST_1][j][i];
  904.       //! association with id already known for fields.
  905.       fs->top_field->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->top_field->ref_pic_num[fs->top_field->slice_id[j>>2][i>>2]][LIST_0][dummylist0] : 0;
  906.       fs->top_field->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->top_field->ref_pic_num[fs->top_field->slice_id[j>>2][i>>2]][LIST_1][dummylist1] : 0;
  907.       //! need to make association with frames
  908.       fs->frame->motion.ref_id[LIST_0][jj][i] = (dummylist0>=0)? fs->top_field->frm_ref_pic_num[fs->top_field->slice_id[j>>2][i>>2]][LIST_0][dummylist0] : 0;
  909.       fs->frame->motion.ref_id[LIST_1][jj][i] = (dummylist1>=0)? fs->top_field->frm_ref_pic_num[fs->top_field->slice_id[j>>2][i>>2]][LIST_1][dummylist1] : 0;
  910.       fs->frame->motion.mv[LIST_0][jj4][i][0] = fs->bottom_field->motion.mv[LIST_0][j][i][0];
  911.       fs->frame->motion.mv[LIST_0][jj4][i][1] = fs->bottom_field->motion.mv[LIST_0][j][i][1] ;
  912.       fs->frame->motion.mv[LIST_1][jj4][i][0] = fs->bottom_field->motion.mv[LIST_1][j][i][0];
  913.       fs->frame->motion.mv[LIST_1][jj4][i][1] = fs->bottom_field->motion.mv[LIST_1][j][i][1] ;
  914.       dummylist0=fs->frame->motion.ref_idx[LIST_0][jj4][i]  = fs->bottom_field->motion.ref_idx[LIST_0][j][i];
  915.       dummylist1=fs->frame->motion.ref_idx[LIST_1][jj4][i]  = fs->bottom_field->motion.ref_idx[LIST_1][j][i];
  916.       fs->bottom_field->motion.ref_id[LIST_0][j][i] = (dummylist0>=0)? fs->bottom_field->ref_pic_num[fs->bottom_field->slice_id[j>>2][i>>2]][LIST_0][dummylist0] : 0;
  917.       fs->bottom_field->motion.ref_id[LIST_1][j][i] = (dummylist1>=0)? fs->bottom_field->ref_pic_num[fs->bottom_field->slice_id[j>>2][i>>2]][LIST_1][dummylist1] : 0;
  918.       //! need to make association with frames
  919.       fs->frame->motion.ref_id[LIST_0][jj4][i] = (dummylist0>=0)? fs->bottom_field->frm_ref_pic_num[fs->bottom_field->slice_id[j>>2][i>>2]][LIST_0][dummylist0] : -1;
  920.       fs->frame->motion.ref_id[LIST_1][jj4][i] = (dummylist1>=0)? fs->bottom_field->frm_ref_pic_num[fs->bottom_field->slice_id[j>>2][i>>2]][LIST_1][dummylist1] : -1;
  921.       fs->top_field->motion.field_frame[j][i]=1;
  922.       fs->bottom_field->motion.field_frame[j][i]=1;
  923.     }
  924.   }
  925. }
  926. /*!
  927.  ************************************************************************
  928.  * brief
  929.  *    Allocate memory for buffering of reference picture reordering commands
  930.  ************************************************************************
  931.  */
  932. void alloc_ref_pic_list_reordering_buffer(Slice *currSlice)
  933. {
  934.   int size = img->num_ref_idx_l0_active+1;
  935.   if (img->type!=I_SLICE && img->type!=SI_SLICE)
  936.   {
  937.     if ((currSlice->reordering_of_pic_nums_idc_l0 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: reordering_of_pic_nums_idc_l0");
  938.     if ((currSlice->abs_diff_pic_num_minus1_l0 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: abs_diff_pic_num_minus1_l0");
  939.     if ((currSlice->long_term_pic_idx_l0 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: long_term_pic_idx_l0");
  940.   }
  941.   else
  942.   {
  943.     currSlice->reordering_of_pic_nums_idc_l0 = NULL;
  944.     currSlice->abs_diff_pic_num_minus1_l0 = NULL;
  945.     currSlice->long_term_pic_idx_l0 = NULL;
  946.   }
  947.   size = img->num_ref_idx_l1_active+1;
  948.   if (img->type==B_SLICE)
  949.   {
  950.     if ((currSlice->reordering_of_pic_nums_idc_l1 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: reordering_of_pic_nums_idc_l1");
  951.     if ((currSlice->abs_diff_pic_num_minus1_l1 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: abs_diff_pic_num_minus1_l1");
  952.     if ((currSlice->long_term_pic_idx_l1 = calloc(size,sizeof(int)))==NULL) no_mem_exit("alloc_ref_pic_list_reordering_buffer: long_term_pic_idx_l1");
  953.   }
  954.   else
  955.   {
  956.     currSlice->reordering_of_pic_nums_idc_l1 = NULL;
  957.     currSlice->abs_diff_pic_num_minus1_l1 = NULL;
  958.     currSlice->long_term_pic_idx_l1 = NULL;
  959.   }
  960. }
  961. /*!
  962.  ************************************************************************
  963.  * brief
  964.  *    Free memory for buffering of reference picture reordering commands
  965.  ************************************************************************
  966.  */
  967. void free_ref_pic_list_reordering_buffer(Slice *currSlice)
  968. {
  969.   if (currSlice->reordering_of_pic_nums_idc_l0)
  970.     free(currSlice->reordering_of_pic_nums_idc_l0);
  971.   if (currSlice->abs_diff_pic_num_minus1_l0)
  972.     free(currSlice->abs_diff_pic_num_minus1_l0);
  973.   if (currSlice->long_term_pic_idx_l0)
  974.     free(currSlice->long_term_pic_idx_l0);
  975.   currSlice->reordering_of_pic_nums_idc_l0 = NULL;
  976.   currSlice->abs_diff_pic_num_minus1_l0 = NULL;
  977.   currSlice->long_term_pic_idx_l0 = NULL;
  978.   if (currSlice->reordering_of_pic_nums_idc_l1)
  979.     free(currSlice->reordering_of_pic_nums_idc_l1);
  980.   if (currSlice->abs_diff_pic_num_minus1_l1)
  981.     free(currSlice->abs_diff_pic_num_minus1_l1);
  982.   if (currSlice->long_term_pic_idx_l1)
  983.     free(currSlice->long_term_pic_idx_l1);
  984.   currSlice->reordering_of_pic_nums_idc_l1 = NULL;
  985.   currSlice->abs_diff_pic_num_minus1_l1 = NULL;
  986.   currSlice->long_term_pic_idx_l1 = NULL;
  987. }
  988. /*!
  989.  ************************************************************************
  990.  * brief
  991.  *      Tian Dong
  992.  *          June 13, 2002, Modifed on July 30, 2003
  993.  *
  994.  *      If a gap in frame_num is found, try to fill the gap
  995.  * param img
  996.  *
  997.  ************************************************************************
  998.  */
  999. void fill_frame_num_gap(ImageParameters *img)
  1000. {
  1001.   int CurrFrameNum;
  1002.   int UnusedShortTermFrameNum;
  1003.   StorablePicture *picture = NULL;
  1004.   int tmp1 = img->delta_pic_order_cnt[0];
  1005.   int tmp2 = img->delta_pic_order_cnt[1];
  1006.   img->delta_pic_order_cnt[0] = img->delta_pic_order_cnt[1] = 0;
  1007. //  printf("A gap in frame number is found, try to fill it.n");
  1008.   UnusedShortTermFrameNum = (img->pre_frame_num + 1) % img->MaxFrameNum;
  1009.   CurrFrameNum = img->frame_num;
  1010.   while (CurrFrameNum != UnusedShortTermFrameNum)
  1011.   {
  1012.     picture = alloc_storable_picture (FRAME, img->width, img->height, img->width_cr, img->height_cr);
  1013.     picture->coded_frame = 1;
  1014.     picture->pic_num = UnusedShortTermFrameNum;
  1015.     picture->frame_num = UnusedShortTermFrameNum;
  1016.     picture->non_existing = 1;
  1017.     picture->is_output = 1;
  1018.     picture->used_for_reference = 1;
  1019.     picture->adaptive_ref_pic_buffering_flag = 0;
  1020.     img->frame_num = UnusedShortTermFrameNum;
  1021.     if (active_sps->pic_order_cnt_type!=0)
  1022.     {
  1023.       decode_poc(img);
  1024.     }
  1025.     picture->top_poc=img->toppoc;
  1026.     picture->bottom_poc=img->bottompoc;
  1027.     picture->frame_poc=img->framepoc;
  1028.     picture->poc=img->framepoc;
  1029.     store_picture_in_dpb(picture);
  1030.     picture=NULL;
  1031.     img->pre_frame_num = UnusedShortTermFrameNum;
  1032.     UnusedShortTermFrameNum = (UnusedShortTermFrameNum + 1) % img->MaxFrameNum;
  1033.   }
  1034.   img->delta_pic_order_cnt[0] = tmp1;
  1035.   img->delta_pic_order_cnt[1] = tmp2;
  1036.   img->frame_num = CurrFrameNum;
  1037. }
  1038. /*!
  1039.  ************************************************************************
  1040.  * brief
  1041.  *    Allocate motion parameter memory for colocated structure
  1042.  *
  1043.  ************************************************************************
  1044.  */
  1045. void alloc_motion_params(MotionParams *ftype, int size_y, int size_x)
  1046. {
  1047.   get_mem3Dint64 (&(ftype->ref_pic_id), 2, size_y, size_x);
  1048.   get_mem4Dshort (&(ftype->mv)        , 2, size_y, size_x, 2);
  1049.   get_mem3D      ((byte****)(&(ftype->ref_idx)) , 2, size_y, size_x);
  1050.   get_mem2D      (&(ftype->moving_block) , size_y, size_x);
  1051. }
  1052. /*!
  1053.  ************************************************************************
  1054.  * brief
  1055.  *    Allocate co-located memory
  1056.  *
  1057.  * param size_x
  1058.  *    horizontal luma size
  1059.  * param size_y
  1060.  *    vertical luma size
  1061.  * param mb_adaptive_frame_field_flag
  1062.  *    flag that indicates macroblock adaptive frame/field coding
  1063.  *
  1064.  * return
  1065.  *    the allocated StorablePicture structure
  1066.  ************************************************************************
  1067.  */
  1068. ColocatedParams* alloc_colocated(int size_x, int size_y, int mb_adaptive_frame_field_flag)
  1069. {
  1070.   ColocatedParams *s;
  1071.   s = calloc(1, sizeof(ColocatedParams));
  1072.   if (NULL == s)
  1073.     no_mem_exit("alloc_colocated: s");
  1074.   s->size_x = size_x;
  1075.   s->size_y = size_y;
  1076.   alloc_motion_params(&s->frame, size_y / BLOCK_SIZE, size_x / BLOCK_SIZE);
  1077.   if (mb_adaptive_frame_field_flag)
  1078.   {
  1079.     alloc_motion_params(&s->top   , size_y / (BLOCK_SIZE * 2), size_x / BLOCK_SIZE);
  1080.     alloc_motion_params(&s->bottom, size_y / (BLOCK_SIZE * 2), size_x / BLOCK_SIZE);
  1081.   }
  1082.   s->mb_adaptive_frame_field_flag  = mb_adaptive_frame_field_flag;
  1083.   return s;
  1084. }
  1085. /*!
  1086.  ************************************************************************
  1087.  * brief
  1088.  *    Free co-located memory.
  1089.  *
  1090.  * param p
  1091.  *    Picture to be freed
  1092.  *
  1093.  ************************************************************************
  1094.  */
  1095. void free_colocated(ColocatedParams* p)
  1096. {
  1097.   if (p)
  1098.   {
  1099.     free_mem3D      ((byte***)p->frame.ref_idx);
  1100.     free_mem3Dint64 (p->frame.ref_pic_id);
  1101.     free_mem4Dshort (p->frame.mv);
  1102.     if (p->frame.moving_block)
  1103.     {
  1104.       free_mem2D (p->frame.moving_block);
  1105.       p->frame.moving_block=NULL;
  1106.     }
  1107.     if (p->mb_adaptive_frame_field_flag)
  1108.     {
  1109.       free_mem3D      ((byte***)p->top.ref_idx);
  1110.       free_mem3Dint64 (p->top.ref_pic_id);
  1111.       free_mem4Dshort (p->top.mv);
  1112.       if (p->top.moving_block)
  1113.       {
  1114.         free_mem2D (p->top.moving_block);
  1115.         p->top.moving_block=NULL;
  1116.       }
  1117.       free_mem3D      ((byte***)p->bottom.ref_idx);
  1118.       free_mem3Dint64 (p->bottom.ref_pic_id);
  1119.       free_mem4Dshort (p->bottom.mv);
  1120.       if (p->bottom.moving_block)
  1121.       {
  1122.         free_mem2D (p->bottom.moving_block);
  1123.         p->bottom.moving_block=NULL;
  1124.       }
  1125.     }
  1126.     free(p);
  1127.     p=NULL;
  1128.   }
  1129. }
  1130. /*!
  1131.  ************************************************************************
  1132.  * brief
  1133.  *    Compute co-located motion info
  1134.  *
  1135.  ************************************************************************
  1136.  */
  1137. void compute_colocated(ColocatedParams* p, StorablePicture **listX[6])
  1138. {
  1139.   StorablePicture *fs = listX[LIST_1 ][0];
  1140.   StorablePicture *fs_top = fs, *fs_bottom = fs;
  1141.   int i,j, ii, jj, jdiv;
  1142.   int fs_size_x4 = (fs->size_x >> 2);
  1143.   int fs_size_y4 = (fs->size_y >> 2);
  1144.   if (img->MbaffFrameFlag)
  1145.   {
  1146.     fs_top= listX[LIST_1 + 2][0];
  1147.     fs_bottom= listX[LIST_1 + 4][0];
  1148.   }
  1149.   else
  1150.   {
  1151.     if (img->field_pic_flag)
  1152.     {
  1153.       if ((img->structure != fs->structure) && (fs->coded_frame))
  1154.       {
  1155.         if (img->structure==TOP_FIELD)
  1156.         {
  1157.           fs_top=fs_bottom=fs = listX[LIST_1 ][0]->top_field;
  1158.         }
  1159.         else
  1160.         {
  1161.           fs_top=fs_bottom=fs = listX[LIST_1 ][0]->bottom_field;
  1162.         }
  1163.       }
  1164.     }
  1165.   }
  1166.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1167.   {
  1168.     for (j=0 ; j < (fs->size_y>>2); j++)
  1169.     {
  1170.       jdiv = (j>>1);
  1171.       jj = jdiv + ((j>>3)<<1);
  1172.       for (i=0 ; i < fs_size_x4 ; i++)
  1173.       {
  1174.         if (img->MbaffFrameFlag && fs->motion.field_frame[j][i])
  1175.         {
  1176.           //! Assign frame buffers for field MBs
  1177.           //! Check whether we should use top or bottom field mvs.
  1178.           //! Depending on the assigned poc values.
  1179.           if (iabs(dec_picture->poc - fs_bottom->poc)> iabs(dec_picture->poc -fs_top->poc) )
  1180.           {
  1181.             p->frame.mv[LIST_0][j][i][0]      = fs_top->motion.mv[LIST_0][jdiv][i][0];
  1182.             p->frame.mv[LIST_0][j][i][1]      = fs_top->motion.mv[LIST_0][jdiv][i][1] ;
  1183.             p->frame.mv[LIST_1][j][i][0]      = fs_top->motion.mv[LIST_1][jdiv][i][0];
  1184.             p->frame.mv[LIST_1][j][i][1]      = fs_top->motion.mv[LIST_1][jdiv][i][1] ;
  1185.             p->frame.ref_idx[LIST_0][j][i]    = fs_top->motion.ref_idx[LIST_0][jdiv][i];
  1186.             p->frame.ref_idx[LIST_1][j][i]    = fs_top->motion.ref_idx[LIST_1][jdiv][i];
  1187.             p->frame.ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][jj][i];
  1188.             p->frame.ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][jj][i];
  1189.             p->is_long_term             = fs_top->is_long_term;
  1190.           }
  1191.           else
  1192.           {
  1193.             p->frame.mv[LIST_0][j][i][0]      = fs_bottom->motion.mv[LIST_0][jdiv][i][0];
  1194.             p->frame.mv[LIST_0][j][i][1]      = fs_bottom->motion.mv[LIST_0][jdiv][i][1] ;
  1195.             p->frame.mv[LIST_1][j][i][0]      = fs_bottom->motion.mv[LIST_1][jdiv][i][0];
  1196.             p->frame.mv[LIST_1][j][i][1]      = fs_bottom->motion.mv[LIST_1][jdiv][i][1] ;
  1197.             p->frame.ref_idx[LIST_0][j][i]    = fs_bottom->motion.ref_idx[LIST_0][jdiv][i];
  1198.             p->frame.ref_idx[LIST_1][j][i]    = fs_bottom->motion.ref_idx[LIST_1][jdiv][i];
  1199.             p->frame.ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][jj + 4][i];
  1200.             p->frame.ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][jj + 4][i];
  1201.             p->is_long_term             = fs_bottom->is_long_term;
  1202.           }
  1203.         }
  1204.         else
  1205.         {
  1206.           p->frame.mv[LIST_0][j][i][0]      = fs->motion.mv[LIST_0][j][i][0];
  1207.           p->frame.mv[LIST_0][j][i][1]      = fs->motion.mv[LIST_0][j][i][1] ;
  1208.           p->frame.mv[LIST_1][j][i][0]      = fs->motion.mv[LIST_1][j][i][0];
  1209.           p->frame.mv[LIST_1][j][i][1]      = fs->motion.mv[LIST_1][j][i][1] ;
  1210.           p->frame.ref_idx[LIST_0][j][i]    = fs->motion.ref_idx[LIST_0][j][i];
  1211.           p->frame.ref_idx[LIST_1][j][i]    = fs->motion.ref_idx[LIST_1][j][i];
  1212.           p->frame.ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][j][i];
  1213.           p->frame.ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][j][i];
  1214.           p->is_long_term             = fs->is_long_term;
  1215.         }
  1216.       }
  1217.     }
  1218.   }
  1219.   //! Generate field MVs from Frame MVs
  1220.   if (img->structure || img->MbaffFrameFlag)
  1221.   {
  1222.     for (j=0 ; j<fs->size_y/8 ; j++)
  1223.     {
  1224.       jj = RSD(j);
  1225.       for (i=0 ; i<fs->size_x/4 ; i++)
  1226.       {
  1227.         ii = RSD(i);
  1228.         //! Do nothing if macroblock as field coded in MB-AFF
  1229.         if (!img->MbaffFrameFlag )
  1230.         {
  1231.           p->frame.mv[LIST_0][j][i][0] = fs->motion.mv[LIST_0][jj][ii][0];
  1232.           p->frame.mv[LIST_0][j][i][1] = fs->motion.mv[LIST_0][jj][ii][1];
  1233.           p->frame.mv[LIST_1][j][i][0] = fs->motion.mv[LIST_1][jj][ii][0];
  1234.           p->frame.mv[LIST_1][j][i][1] = fs->motion.mv[LIST_1][jj][ii][1];
  1235.           // Scaling of references is done here since it will not affect spatial direct (2*0 =0)
  1236.           if (fs->motion.ref_idx[LIST_0][jj][ii] == -1)
  1237.           {
  1238.             p->frame.ref_idx   [LIST_0][j][i] = -1;
  1239.             p->frame.ref_pic_id[LIST_0][j][i] = -1;
  1240.           }
  1241.           else
  1242.           {
  1243.             p->frame.ref_idx   [LIST_0][j][i] = fs->motion.ref_idx[LIST_0][jj][ii] ;
  1244.             p->frame.ref_pic_id[LIST_0][j][i] = fs->motion.ref_id [LIST_0][jj][ii];
  1245.           }
  1246.           if (fs->motion.ref_idx[LIST_1][jj][ii] == -1)
  1247.           {
  1248.             p->frame.ref_idx   [LIST_1][j][i] = -1;
  1249.             p->frame.ref_pic_id[LIST_1][j][i] = -1;
  1250.           }
  1251.           else
  1252.           {
  1253.             p->frame.ref_idx   [LIST_1][j][i] = fs->motion.ref_idx[LIST_1][jj][ii];
  1254.             p->frame.ref_pic_id[LIST_1][j][i] = fs->motion.ref_id [LIST_1][jj][ii];
  1255.           }
  1256.           p->is_long_term = fs->is_long_term;
  1257.           if (img->direct_spatial_mv_pred_flag == 1)
  1258.           {
  1259.             p->frame.moving_block[j][i] =
  1260.               !((!p->is_long_term
  1261.               && ((p->frame.ref_idx[LIST_0][j][i] == 0)
  1262.               &&  (iabs(p->frame.mv[LIST_0][j][i][0])>>1 == 0)
  1263.               &&  (iabs(p->frame.mv[LIST_0][j][i][1])>>1 == 0)))
  1264.               || ((p->frame.ref_idx[LIST_0][j][i] == -1)
  1265.               &&  (p->frame.ref_idx[LIST_1][j][i] == 0)
  1266.               &&  (iabs(p->frame.mv[LIST_1][j][i][0])>>1 == 0)
  1267.               &&  (iabs(p->frame.mv[LIST_1][j][i][1])>>1 == 0)));
  1268.           }
  1269.         }
  1270.         else
  1271.         {
  1272.           p->bottom.mv[LIST_0][j][i][0] = fs_bottom->motion.mv[LIST_0][jj][ii][0];
  1273.           p->bottom.mv[LIST_0][j][i][1] = fs_bottom->motion.mv[LIST_0][jj][ii][1];
  1274.           p->bottom.mv[LIST_1][j][i][0] = fs_bottom->motion.mv[LIST_1][jj][ii][0];
  1275.           p->bottom.mv[LIST_1][j][i][1] = fs_bottom->motion.mv[LIST_1][jj][ii][1];
  1276.           p->bottom.ref_idx[LIST_0][j][i] = fs_bottom->motion.ref_idx[LIST_0][jj][ii];
  1277.           p->bottom.ref_idx[LIST_1][j][i] = fs_bottom->motion.ref_idx[LIST_1][jj][ii];
  1278.           p->bottom.ref_pic_id[LIST_0][j][i] = fs_bottom->motion.ref_id[LIST_0][jj][ii];
  1279.           p->bottom.ref_pic_id[LIST_1][j][i] = fs_bottom->motion.ref_id[LIST_1][jj][ii];
  1280.           if (img->direct_spatial_mv_pred_flag == 1)
  1281.           {
  1282.             p->bottom.moving_block[j][i] =
  1283.               !((!fs_bottom->is_long_term
  1284.               && ((p->bottom.ref_idx[LIST_0][j][i] == 0)
  1285.               &&  (iabs(p->bottom.mv[LIST_0][j][i][0])>>1 == 0)
  1286.               &&  (iabs(p->bottom.mv[LIST_0][j][i][1])>>1 == 0)))
  1287.               || ((p->bottom.ref_idx[LIST_0][j][i] == -1)
  1288.               &&  (p->bottom.ref_idx[LIST_1][j][i] == 0)
  1289.               &&  (iabs(p->bottom.mv[LIST_1][j][i][0])>>1 == 0)
  1290.               &&  (iabs(p->bottom.mv[LIST_1][j][i][1])>>1 == 0)));
  1291.           }
  1292.           p->top.mv[LIST_0][j][i][0] = fs_top->motion.mv[LIST_0][jj][ii][0];
  1293.           p->top.mv[LIST_0][j][i][1] = fs_top->motion.mv[LIST_0][jj][ii][1];
  1294.           p->top.mv[LIST_1][j][i][0] = fs_top->motion.mv[LIST_1][jj][ii][0];
  1295.           p->top.mv[LIST_1][j][i][1] = fs_top->motion.mv[LIST_1][jj][ii][1];
  1296.           p->top.ref_idx[LIST_0][j][i] = fs_top->motion.ref_idx[LIST_0][jj][ii];
  1297.           p->top.ref_idx[LIST_1][j][i] = fs_top->motion.ref_idx[LIST_1][jj][ii];
  1298.           p->top.ref_pic_id[LIST_0][j][i] = fs_top->motion.ref_id[LIST_0][jj][ii];
  1299.           p->top.ref_pic_id[LIST_1][j][i] = fs_top->motion.ref_id[LIST_1][jj][ii];
  1300.           if (img->direct_spatial_mv_pred_flag == 1)
  1301.           {
  1302.             p->top.moving_block[j][i] =
  1303.               !((!fs_top->is_long_term
  1304.               && ((p->top.ref_idx[LIST_0][j][i] == 0)
  1305.               &&  (iabs(p->top.mv[LIST_0][j][i][0])>>1 == 0)
  1306.               &&  (iabs(p->top.mv[LIST_0][j][i][1])>>1 == 0)))
  1307.               || ((p->top.ref_idx[LIST_0][j][i] == -1)
  1308.               &&  (p->top.ref_idx[LIST_1][j][i] == 0)
  1309.               &&  (iabs(p->top.mv[LIST_1][j][i][0])>>1 == 0)
  1310.               &&  (iabs(p->top.mv[LIST_1][j][i][1])>>1 == 0)));
  1311.           }
  1312.           if ((img->direct_spatial_mv_pred_flag == 0 ) && !fs->motion.field_frame[2*j][i])
  1313.           {
  1314.             p->top.mv[LIST_0][j][i][1] /= 2;
  1315.             p->top.mv[LIST_1][j][i][1] /= 2;
  1316.             p->bottom.mv[LIST_0][j][i][1] /= 2;
  1317.             p->bottom.mv[LIST_1][j][i][1] /= 2;
  1318.           }
  1319.         }
  1320.       }
  1321.     }
  1322.   }
  1323.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1324.   {
  1325.     //! Use inference flag to remap mvs/references
  1326.     //! Frame with field co-located
  1327.     if (!img->structure)
  1328.     {
  1329.       for (j=0 ; j < fs_size_y4; j++)
  1330.       {
  1331.         jdiv = (j >> 1);
  1332.         jj   = jdiv + ((j >> 3) << 2);
  1333.         for (i = 0 ; i < fs_size_x4; i++)
  1334.         {
  1335.           if (fs->motion.field_frame[j][i])
  1336.           {
  1337.             if (iabs(dec_picture->poc - fs->bottom_field->poc) > iabs(dec_picture->poc - fs->top_field->poc))
  1338.             {
  1339.               p->frame.mv[LIST_0][j][i][0] = fs->top_field->motion.mv[LIST_0][jdiv][i][0];
  1340.               p->frame.mv[LIST_0][j][i][1] = fs->top_field->motion.mv[LIST_0][jdiv][i][1] ;
  1341.               p->frame.mv[LIST_1][j][i][0] = fs->top_field->motion.mv[LIST_1][jdiv][i][0];
  1342.               p->frame.mv[LIST_1][j][i][1] = fs->top_field->motion.mv[LIST_1][jdiv][i][1] ;
  1343.               p->frame.ref_idx[LIST_0][j][i]    = fs->top_field->motion.ref_idx[LIST_0][jdiv][i];
  1344.               p->frame.ref_idx[LIST_1][j][i]    = fs->top_field->motion.ref_idx[LIST_1][jdiv][i];
  1345.               p->frame.ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][jj][i];
  1346.               p->frame.ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][jj][i];
  1347.               p->is_long_term             = fs->top_field->is_long_term;
  1348.             }
  1349.             else
  1350.             {
  1351.               p->frame.mv[LIST_0][j][i][0] = fs->bottom_field->motion.mv[LIST_0][jdiv][i][0];
  1352.               p->frame.mv[LIST_0][j][i][1] = fs->bottom_field->motion.mv[LIST_0][jdiv][i][1] ;
  1353.               p->frame.mv[LIST_1][j][i][0] = fs->bottom_field->motion.mv[LIST_1][jdiv][i][0];
  1354.               p->frame.mv[LIST_1][j][i][1] = fs->bottom_field->motion.mv[LIST_1][jdiv][i][1] ;
  1355.               p->frame.ref_idx[LIST_0][j][i]  = fs->bottom_field->motion.ref_idx[LIST_0][jdiv][i];
  1356.               p->frame.ref_idx[LIST_1][j][i]  = fs->bottom_field->motion.ref_idx[LIST_1][jdiv][i];
  1357.               p->frame.ref_pic_id[LIST_0][j][i] = fs->motion.ref_id[LIST_0][jj + 4][i];
  1358.               p->frame.ref_pic_id[LIST_1][j][i] = fs->motion.ref_id[LIST_1][jj + 4][i];
  1359.               p->is_long_term             = fs->bottom_field->is_long_term;
  1360.             }
  1361.           }
  1362.         }
  1363.       }
  1364.     }
  1365.   }
  1366.   p->is_long_term = fs->is_long_term;
  1367.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1368.   {
  1369.     if (img->direct_spatial_mv_pred_flag == 1)
  1370.     {
  1371.       for (j=0 ; j < (fs->size_y>>2); j++)
  1372.       {
  1373.         jj = RSD(j);
  1374.         for (i=0 ; i < (fs->size_x>>2); i++)
  1375.         {
  1376.           ii = RSD(i);
  1377.           p->frame.mv[LIST_0][j][i][0]=p->frame.mv[LIST_0][jj][ii][0];
  1378.           p->frame.mv[LIST_0][j][i][1]=p->frame.mv[LIST_0][jj][ii][1];
  1379.           p->frame.mv[LIST_1][j][i][0]=p->frame.mv[LIST_1][jj][ii][0];
  1380.           p->frame.mv[LIST_1][j][i][1]=p->frame.mv[LIST_1][jj][ii][1];
  1381.           p->frame.ref_idx[LIST_0][j][i]=p->frame.ref_idx[LIST_0][jj][ii];
  1382.           p->frame.ref_idx[LIST_1][j][i]=p->frame.ref_idx[LIST_1][jj][ii];
  1383.           p->frame.ref_pic_id[LIST_0][j][i] = p->frame.ref_pic_id[LIST_0][jj][ii];
  1384.           p->frame.ref_pic_id[LIST_1][j][i] = p->frame.ref_pic_id[LIST_1][jj][ii];
  1385.           p->frame.moving_block[j][i]=
  1386.             !((!p->is_long_term
  1387.             && ((p->frame.ref_idx[LIST_0][j][i] == 0)
  1388.             &&  (iabs(p->frame.mv[LIST_0][j][i][0])>>1 == 0)
  1389.             &&  (iabs(p->frame.mv[LIST_0][j][i][1])>>1 == 0)))
  1390.             || ((p->frame.ref_idx[LIST_0][j][i] == -1)
  1391.             &&  (p->frame.ref_idx[LIST_1][j][i] == 0)
  1392.             &&  (iabs(p->frame.mv[LIST_1][j][i][0])>>1 == 0)
  1393.             &&  (iabs(p->frame.mv[LIST_1][j][i][1])>>1 == 0)));
  1394.         }
  1395.       }
  1396.     }
  1397.     else
  1398.     {
  1399.       for (j=0 ; j < (fs->size_y>>2); j++)
  1400.       {
  1401.         jj = RSD(j);
  1402.         for (i=0 ; i < (fs->size_x>>2); i++)
  1403.         {
  1404.           ii = RSD(i);
  1405.           p->frame.mv[LIST_0][j][i][0]=p->frame.mv[LIST_0][jj][ii][0];
  1406.           p->frame.mv[LIST_0][j][i][1]=p->frame.mv[LIST_0][jj][ii][1];
  1407.           p->frame.mv[LIST_1][j][i][0]=p->frame.mv[LIST_1][jj][ii][0];
  1408.           p->frame.mv[LIST_1][j][i][1]=p->frame.mv[LIST_1][jj][ii][1];
  1409.           p->frame.ref_idx[LIST_0][j][i]=p->frame.ref_idx[LIST_0][jj][ii];
  1410.           p->frame.ref_idx[LIST_1][j][i]=p->frame.ref_idx[LIST_1][jj][ii];
  1411.           p->frame.ref_pic_id[LIST_0][j][i] = p->frame.ref_pic_id[LIST_0][jj][ii];
  1412.           p->frame.ref_pic_id[LIST_1][j][i] = p->frame.ref_pic_id[LIST_1][jj][ii];
  1413.         }
  1414.       }
  1415.     }
  1416.   }
  1417.   else
  1418.   {
  1419.     if (img->direct_spatial_mv_pred_flag == 1)
  1420.     {
  1421.       for (j=0 ; j < fs_size_y4; j++)
  1422.       {
  1423.         memcpy(&p->frame.mv[LIST_0][j][0][0], &fs->motion.mv[LIST_0][j][0][0], 2 * fs_size_x4 * sizeof(short));
  1424.         memcpy(&p->frame.mv[LIST_1][j][0][0], &fs->motion.mv[LIST_1][j][0][0], 2 * fs_size_x4 * sizeof(short));
  1425.         memcpy(p->frame.ref_idx[LIST_0][j], fs->motion.ref_idx[LIST_0][j], fs_size_x4 * sizeof(char));
  1426.         memcpy(p->frame.ref_idx[LIST_1][j], fs->motion.ref_idx[LIST_1][j], fs_size_x4 * sizeof(char));
  1427.         memcpy(p->frame.ref_pic_id[LIST_0][j], fs->motion.ref_id[LIST_0][j], fs_size_x4 * sizeof(int64));
  1428.         memcpy(p->frame.ref_pic_id[LIST_1][j], fs->motion.ref_id[LIST_1][j], fs_size_x4 * sizeof(int64));
  1429.         for (i=0 ; i < fs_size_x4; i++)
  1430.         {
  1431.           p->frame.moving_block[j][i]=
  1432.             !((!p->is_long_term
  1433.             && ((p->frame.ref_idx[LIST_0][j][i] == 0)
  1434.             &&  (iabs(p->frame.mv[LIST_0][j][i][0])>>1 == 0)
  1435.             &&  (iabs(p->frame.mv[LIST_0][j][i][1])>>1 == 0)))
  1436.             || ((p->frame.ref_idx[LIST_0][j][i] == -1)
  1437.             &&  (p->frame.ref_idx[LIST_1][j][i] == 0)
  1438.             &&  (iabs(p->frame.mv[LIST_1][j][i][0])>>1 == 0)
  1439.             &&  (iabs(p->frame.mv[LIST_1][j][i][1])>>1 == 0)));
  1440.         }
  1441.       }
  1442.     }
  1443.     else
  1444.     {
  1445.       for (j=0 ; j < fs_size_y4; j++)
  1446.       {
  1447.         memcpy(&p->frame.mv[LIST_0][j][0][0], &fs->motion.mv[LIST_0][j][0][0], 2 * fs_size_x4 * sizeof(short));
  1448.         memcpy(&p->frame.mv[LIST_1][j][0][0], &fs->motion.mv[LIST_1][j][0][0], 2 * fs_size_x4 * sizeof(short));
  1449.         memcpy(p->frame.ref_idx[LIST_0][j], fs->motion.ref_idx[LIST_0][j], fs_size_x4 * sizeof(char));
  1450.         memcpy(p->frame.ref_idx[LIST_1][j], fs->motion.ref_idx[LIST_1][j], fs_size_x4 * sizeof(char));
  1451.         memcpy(p->frame.ref_pic_id[LIST_0][j], fs->motion.ref_id[LIST_0][j], fs_size_x4 * sizeof(int64));
  1452.         memcpy(p->frame.ref_pic_id[LIST_1][j], fs->motion.ref_id[LIST_1][j], fs_size_x4 * sizeof(int64));   
  1453.       }
  1454.     }
  1455.   }
  1456.   if (img->direct_spatial_mv_pred_flag == 0)
  1457.   {    
  1458.     if (img->MbaffFrameFlag || !img->structure)
  1459.     {
  1460.       for (j=0 ; j < fs_size_y4; j++)
  1461.       {
  1462.         for (i=0 ; i < fs_size_x4 ; i++)
  1463.         {
  1464.           if (fs->motion.field_frame[j][i])
  1465.           {
  1466.             p->frame.mv[LIST_0][j][i][1] *= 2;
  1467.             p->frame.mv[LIST_1][j][i][1] *= 2;
  1468.           }
  1469.         }
  1470.       }
  1471.     }
  1472.     else  if (img->structure)
  1473.     {
  1474.       for (j=0 ; j < fs_size_y4; j++)
  1475.       {
  1476.         for (i=0 ; i < fs_size_x4 ; i++)
  1477.         {
  1478.           if (!fs->motion.field_frame[j][i])
  1479.           {
  1480.             p->frame.mv[LIST_0][j][i][1] /= 2;
  1481.             p->frame.mv[LIST_1][j][i][1] /= 2;
  1482.           }
  1483.         }
  1484.       }
  1485.     }
  1486.     for (j=0; j<2 + (img->MbaffFrameFlag * 4);j+=2)
  1487.     {
  1488.       for (i=0; i<listXsize[j];i++)
  1489.       {
  1490.         int prescale, iTRb, iTRp;
  1491.         if (j==0)
  1492.         {
  1493.           iTRb = iClip3( -128, 127, dec_picture->poc - listX[LIST_0 + j][i]->poc );
  1494.         }
  1495.         else if (j == 2)
  1496.         {
  1497.           iTRb = iClip3( -128, 127, dec_picture->top_poc - listX[LIST_0 + j][i]->poc );
  1498.         }
  1499.         else
  1500.         {
  1501.           iTRb = iClip3( -128, 127, dec_picture->bottom_poc - listX[LIST_0 + j][i]->poc );
  1502.         }
  1503.         iTRp = iClip3( -128, 127,  listX[LIST_1 + j][0]->poc - listX[LIST_0 + j][i]->poc);
  1504.         if (iTRp!=0)
  1505.         {
  1506.           prescale = ( 16384 + iabs( iTRp / 2 ) ) / iTRp;
  1507.           img->mvscale[j][i] = iClip3( -1024, 1023, ( iTRb * prescale + 32 ) >> 6 ) ;
  1508.         }
  1509.         else
  1510.         {
  1511.           img->mvscale[j][i] = 9999;
  1512.         }
  1513.       }
  1514.     }
  1515.   }
  1516. }
  1517. /*!
  1518.  ************************************************************************
  1519.  * brief
  1520.  *    Compute co-located motion info
  1521.  *    for 4:4:4 Independent mode
  1522.  *
  1523.  ************************************************************************
  1524.  */
  1525. void compute_colocated_JV(ColocatedParams* p, StorablePicture **listX[6])
  1526. {
  1527.   StorablePicture *fs, *fs_top, *fs_bottom;
  1528.   int i,j, ii, jj, jdiv;
  1529.   int np = img->colour_plane_id;
  1530.   fs_top = fs_bottom = fs = listX[LIST_1 ][0];
  1531.   if (img->MbaffFrameFlag)
  1532.   {
  1533.     fs_top= listX[LIST_1 + 2][0];
  1534.     fs_bottom= listX[LIST_1 + 4][0];
  1535.   }
  1536.   else
  1537.   {
  1538.     if (img->field_pic_flag)
  1539.     {
  1540.       if ((img->structure != fs->structure) && (fs->coded_frame))
  1541.       {
  1542.         if (img->structure==TOP_FIELD)
  1543.         {
  1544.           fs_top=fs_bottom=fs = listX[LIST_1 ][0]->top_field;
  1545.         }
  1546.         else
  1547.         {
  1548.           fs_top=fs_bottom=fs = listX[LIST_1 ][0]->bottom_field;
  1549.         }
  1550.       }
  1551.     }
  1552.   }
  1553.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1554.   {
  1555.     for (j=0 ; j<fs->size_y/4 ; j++)
  1556.     {
  1557.       jdiv = j/2;
  1558.       jj = j/2 + 4 * (j/8);
  1559.       for (i=0 ; i<fs->size_x/4 ; i++)
  1560.       {
  1561.         if (img->MbaffFrameFlag && fs->motion.field_frame[j][i])
  1562.         {
  1563.           //! Assign frame buffers for field MBs
  1564.           //! Check whether we should use top or bottom field mvs.
  1565.           //! Depending on the assigned poc values.
  1566.           if (iabs(dec_picture->poc - fs_bottom->poc)> iabs(dec_picture->poc -fs_top->poc) )
  1567.           {
  1568.             p->frame.mv[LIST_0][j][i][0]    = fs_top->JVmotion[np].mv[LIST_0][jdiv][i][0];
  1569.             p->frame.mv[LIST_0][j][i][1]    = fs_top->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
  1570.             p->frame.mv[LIST_1][j][i][0]    = fs_top->JVmotion[np].mv[LIST_1][jdiv][i][0];
  1571.             p->frame.mv[LIST_1][j][i][1]    = fs_top->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
  1572.             p->frame.ref_idx[LIST_0][j][i]  = fs_top->JVmotion[np].ref_idx[LIST_0][jdiv][i];
  1573.             p->frame.ref_idx[LIST_1][j][i]  = fs_top->JVmotion[np].ref_idx[LIST_1][jdiv][i];
  1574.             p->frame.ref_pic_id[LIST_0][j][i]   = fs->JVmotion[np].ref_id[LIST_0][jj][i];
  1575.             p->frame.ref_pic_id[LIST_1][j][i]   = fs->JVmotion[np].ref_id[LIST_1][jj][i];
  1576.             p->is_long_term             = fs_top->is_long_term;
  1577.           }
  1578.           else
  1579.           {
  1580.             p->frame.mv[LIST_0][j][i][0]      = fs_bottom->JVmotion[np].mv[LIST_0][jdiv][i][0];
  1581.             p->frame.mv[LIST_0][j][i][1]      = fs_bottom->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
  1582.             p->frame.mv[LIST_1][j][i][0]      = fs_bottom->JVmotion[np].mv[LIST_1][jdiv][i][0];
  1583.             p->frame.mv[LIST_1][j][i][1]      = fs_bottom->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
  1584.             p->frame.ref_idx[LIST_0][j][i]    = fs_bottom->JVmotion[np].ref_idx[LIST_0][jdiv][i];
  1585.             p->frame.ref_idx[LIST_1][j][i]    = fs_bottom->JVmotion[np].ref_idx[LIST_1][jdiv][i];
  1586.             p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj + 4][i];
  1587.             p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj + 4][i];
  1588.             p->is_long_term             = fs_bottom->is_long_term;
  1589.           }
  1590.         }
  1591.         else
  1592.         {
  1593.           p->frame.mv[LIST_0][j][i][0]      = fs->JVmotion[np].mv[LIST_0][j][i][0];
  1594.           p->frame.mv[LIST_0][j][i][1]      = fs->JVmotion[np].mv[LIST_0][j][i][1] ;
  1595.           p->frame.mv[LIST_1][j][i][0]      = fs->JVmotion[np].mv[LIST_1][j][i][0];
  1596.           p->frame.mv[LIST_1][j][i][1]      = fs->JVmotion[np].mv[LIST_1][j][i][1] ;
  1597.           p->frame.ref_idx[LIST_0][j][i]    = fs->JVmotion[np].ref_idx[LIST_0][j][i];
  1598.           p->frame.ref_idx[LIST_1][j][i]    = fs->JVmotion[np].ref_idx[LIST_1][j][i];
  1599.           p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][j][i];
  1600.           p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][j][i];
  1601.           p->is_long_term             = fs->is_long_term;
  1602.         }
  1603.       }
  1604.     }
  1605.   }
  1606.   //! Generate field MVs from Frame MVs
  1607.   if (img->structure || img->MbaffFrameFlag)
  1608.   {
  1609.     for (j=0 ; j<fs->size_y/8 ; j++)
  1610.     {
  1611.       jj = RSD(j);
  1612.       for (i=0 ; i<fs->size_x/4 ; i++)
  1613.       {
  1614.         ii = RSD(i);
  1615.         //! Do nothing if macroblock as field coded in MB-AFF
  1616.         if (!img->MbaffFrameFlag )
  1617.         {
  1618.           p->frame.mv[LIST_0][j][i][0] = fs->JVmotion[np].mv[LIST_0][jj][ii][0];
  1619.           p->frame.mv[LIST_0][j][i][1] = fs->JVmotion[np].mv[LIST_0][jj][ii][1];
  1620.           p->frame.mv[LIST_1][j][i][0] = fs->JVmotion[np].mv[LIST_1][jj][ii][0];
  1621.           p->frame.mv[LIST_1][j][i][1] = fs->JVmotion[np].mv[LIST_1][jj][ii][1];
  1622.           // Scaling of references is done here since it will not affect spatial direct (2*0 =0)
  1623.           if (fs->JVmotion[np].ref_idx[LIST_0][jj][ii] == -1)
  1624.           {
  1625.             p->frame.ref_idx   [LIST_0][j][i] = -1;
  1626.             p->frame.ref_pic_id[LIST_0][j][i] = -1;
  1627.           }
  1628.           else
  1629.           {
  1630.             p->frame.ref_idx   [LIST_0][j][i] = fs->JVmotion[np].ref_idx[LIST_0][jj][ii] ;
  1631.             p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id [LIST_0][jj][ii];
  1632.           }
  1633.           if (fs->JVmotion[np].ref_idx[LIST_1][jj][ii] == -1)
  1634.           {
  1635.             p->frame.ref_idx   [LIST_1][j][i] = -1;
  1636.             p->frame.ref_pic_id[LIST_1][j][i] = -1;
  1637.           }
  1638.           else
  1639.           {
  1640.             p->frame.ref_idx   [LIST_1][j][i] = fs->JVmotion[np].ref_idx[LIST_1][jj][ii];
  1641.             p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id [LIST_1][jj][ii];
  1642.           }
  1643.           p->is_long_term = fs->is_long_term;
  1644.           if (img->direct_spatial_mv_pred_flag == 1)
  1645.           {
  1646.             p->frame.moving_block[j][i] =
  1647.               !((!p->is_long_term
  1648.               && ((p->frame.ref_idx[LIST_0][j][i] == 0)
  1649.               &&  (iabs(p->frame.mv[LIST_0][j][i][0])>>1 == 0)
  1650.               &&  (iabs(p->frame.mv[LIST_0][j][i][1])>>1 == 0)))
  1651.               || ((p->frame.ref_idx[LIST_0][j][i] == -1)
  1652.               &&  (p->frame.ref_idx[LIST_1][j][i] == 0)
  1653.               &&  (iabs(p->frame.mv[LIST_1][j][i][0])>>1 == 0)
  1654.               &&  (iabs(p->frame.mv[LIST_1][j][i][1])>>1 == 0)));
  1655.           }
  1656.         }
  1657.         else
  1658.         {
  1659.           p->bottom.mv[LIST_0][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_0][jj][ii][0];
  1660.           p->bottom.mv[LIST_0][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_0][jj][ii][1];
  1661.           p->bottom.mv[LIST_1][j][i][0] = fs_bottom->JVmotion[np].mv[LIST_1][jj][ii][0];
  1662.           p->bottom.mv[LIST_1][j][i][1] = fs_bottom->JVmotion[np].mv[LIST_1][jj][ii][1];
  1663.           p->bottom.ref_idx[LIST_0][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_0][jj][ii];
  1664.           p->bottom.ref_idx[LIST_1][j][i] = fs_bottom->JVmotion[np].ref_idx[LIST_1][jj][ii];
  1665.           p->bottom.ref_pic_id[LIST_0][j][i] = fs_bottom->JVmotion[np].ref_id[LIST_0][jj][ii];
  1666.           p->bottom.ref_pic_id[LIST_1][j][i] = fs_bottom->JVmotion[np].ref_id[LIST_1][jj][ii];
  1667.           if (img->direct_spatial_mv_pred_flag == 1)
  1668.           {
  1669.             p->bottom.moving_block[j][i] =
  1670.               !((!fs_bottom->is_long_term
  1671.               && ((p->bottom.ref_idx[LIST_0][j][i] == 0)
  1672.               &&  (iabs(p->bottom.mv[LIST_0][j][i][0])>>1 == 0)
  1673.               &&  (iabs(p->bottom.mv[LIST_0][j][i][1])>>1 == 0)))
  1674.               || ((p->bottom.ref_idx[LIST_0][j][i] == -1)
  1675.               &&  (p->bottom.ref_idx[LIST_1][j][i] == 0)
  1676.               &&  (iabs(p->bottom.mv[LIST_1][j][i][0])>>1 == 0)
  1677.               &&  (iabs(p->bottom.mv[LIST_1][j][i][1])>>1 == 0)));
  1678.           }
  1679.           p->top.mv[LIST_0][j][i][0] = fs_top->JVmotion[np].mv[LIST_0][jj][ii][0];
  1680.           p->top.mv[LIST_0][j][i][1] = fs_top->JVmotion[np].mv[LIST_0][jj][ii][1];
  1681.           p->top.mv[LIST_1][j][i][0] = fs_top->JVmotion[np].mv[LIST_1][jj][ii][0];
  1682.           p->top.mv[LIST_1][j][i][1] = fs_top->JVmotion[np].mv[LIST_1][jj][ii][1];
  1683.           p->top.ref_idx[LIST_0][j][i] = fs_top->JVmotion[np].ref_idx[LIST_0][jj][ii];
  1684.           p->top.ref_idx[LIST_1][j][i] = fs_top->JVmotion[np].ref_idx[LIST_1][jj][ii];
  1685.           p->top.ref_pic_id[LIST_0][j][i] = fs_top->JVmotion[np].ref_id[LIST_0][jj][ii];
  1686.           p->top.ref_pic_id[LIST_1][j][i] = fs_top->JVmotion[np].ref_id[LIST_1][jj][ii];
  1687.           if (img->direct_spatial_mv_pred_flag == 1)
  1688.           {
  1689.             p->top.moving_block[j][i] =
  1690.               !((!fs_top->is_long_term
  1691.               && ((p->top.ref_idx[LIST_0][j][i] == 0)
  1692.               &&  (iabs(p->top.mv[LIST_0][j][i][0])>>1 == 0)
  1693.               &&  (iabs(p->top.mv[LIST_0][j][i][1])>>1 == 0)))
  1694.               || ((p->top.ref_idx[LIST_0][j][i] == -1)
  1695.               &&  (p->top.ref_idx[LIST_1][j][i] == 0)
  1696.               &&  (iabs(p->top.mv[LIST_1][j][i][0])>>1 == 0)
  1697.               &&  (iabs(p->top.mv[LIST_1][j][i][1])>>1 == 0)));
  1698.           }
  1699.           if ((img->direct_spatial_mv_pred_flag == 0 ) && !fs->motion.field_frame[2*j][i])
  1700.           {
  1701.             p->top.mv[LIST_0][j][i][1] /= 2;
  1702.             p->top.mv[LIST_1][j][i][1] /= 2;
  1703.             p->bottom.mv[LIST_0][j][i][1] /= 2;
  1704.             p->bottom.mv[LIST_1][j][i][1] /= 2;
  1705.           }
  1706.         }
  1707.       }
  1708.     }
  1709.   }
  1710.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1711.   {
  1712.     //! Use inference flag to remap mvs/references
  1713.     //! Frame with field co-located
  1714.     if (!img->structure)
  1715.     {
  1716.       for (j=0 ; j<fs->size_y/4 ; j++)
  1717.       {
  1718.         jdiv = j/2;
  1719.         jj = j/2 + 4*(j/8);
  1720.         for (i=0 ; i<fs->size_x/4 ; i++)
  1721.         {
  1722.           if (fs->motion.field_frame[j][i])
  1723.           {
  1724.             if (iabs(dec_picture->poc - fs->bottom_field->poc) > iabs(dec_picture->poc - fs->top_field->poc))
  1725.             {
  1726.               p->frame.mv[LIST_0][j][i][0] = fs->top_field->JVmotion[np].mv[LIST_0][jdiv][i][0];
  1727.               p->frame.mv[LIST_0][j][i][1] = fs->top_field->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
  1728.               p->frame.mv[LIST_1][j][i][0] = fs->top_field->JVmotion[np].mv[LIST_1][jdiv][i][0];
  1729.               p->frame.mv[LIST_1][j][i][1] = fs->top_field->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
  1730.               p->frame.ref_idx[LIST_0][j][i]  = fs->top_field->JVmotion[np].ref_idx[LIST_0][jdiv][i];
  1731.               p->frame.ref_idx[LIST_1][j][i]  = fs->top_field->JVmotion[np].ref_idx[LIST_1][jdiv][i];
  1732.               p->frame.ref_pic_id[LIST_0][j][i]   = fs->JVmotion[np].ref_id[LIST_0][jj][i];
  1733.               p->frame.ref_pic_id[LIST_1][j][i]   = fs->JVmotion[np].ref_id[LIST_1][jj][i];
  1734.               p->is_long_term               = fs->top_field->is_long_term;
  1735.             }
  1736.             else
  1737.             {
  1738.               p->frame.mv[LIST_0][j][i][0] = fs->bottom_field->JVmotion[np].mv[LIST_0][jdiv][i][0];
  1739.               p->frame.mv[LIST_0][j][i][1] = fs->bottom_field->JVmotion[np].mv[LIST_0][jdiv][i][1] ;
  1740.               p->frame.mv[LIST_1][j][i][0] = fs->bottom_field->JVmotion[np].mv[LIST_1][jdiv][i][0];
  1741.               p->frame.mv[LIST_1][j][i][1] = fs->bottom_field->JVmotion[np].mv[LIST_1][jdiv][i][1] ;
  1742.               p->frame.ref_idx[LIST_0][j][i]  = fs->bottom_field->JVmotion[np].ref_idx[LIST_0][jdiv][i];
  1743.               p->frame.ref_idx[LIST_1][j][i]  = fs->bottom_field->JVmotion[np].ref_idx[LIST_1][jdiv][i];
  1744.               p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][jj + 4][i];
  1745.               p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][jj + 4][i];
  1746.               p->is_long_term             = fs->bottom_field->is_long_term;
  1747.             }
  1748.           }
  1749.         }
  1750.       }
  1751.     }
  1752.   }
  1753.   p->is_long_term = fs->is_long_term;
  1754.   if (!active_sps->frame_mbs_only_flag || active_sps->direct_8x8_inference_flag)
  1755.   {
  1756.     for (j=0 ; j<fs->size_y/4 ; j++)
  1757.     {
  1758.       jj = RSD(j);
  1759.       for (i=0 ; i<fs->size_x/4 ; i++)
  1760.       {
  1761.         ii = RSD(i);
  1762.         p->frame.mv[LIST_0][j][i][0] = p->frame.mv[LIST_0][jj][ii][0];
  1763.         p->frame.mv[LIST_0][j][i][1] = p->frame.mv[LIST_0][jj][ii][1];
  1764.         p->frame.mv[LIST_1][j][i][0] = p->frame.mv[LIST_1][jj][ii][0];
  1765.         p->frame.mv[LIST_1][j][i][1] = p->frame.mv[LIST_1][jj][ii][1];
  1766.         p->frame.ref_idx[LIST_0][j][i]=p->frame.ref_idx[LIST_0][jj][ii];
  1767.         p->frame.ref_idx[LIST_1][j][i]=p->frame.ref_idx[LIST_1][jj][ii];
  1768.         p->frame.ref_pic_id[LIST_0][j][i] = p->frame.ref_pic_id[LIST_0][jj][ii];
  1769.         p->frame.ref_pic_id[LIST_1][j][i] = p->frame.ref_pic_id[LIST_1][jj][ii];
  1770.         if (img->direct_spatial_mv_pred_flag == 1)
  1771.         {
  1772.           p->frame.moving_block[j][i]= (byte) (
  1773.             !((!p->is_long_term
  1774.             && ((p->frame.ref_idx[LIST_0][j][i] == 0)
  1775.             &&  (iabs(p->frame.mv[LIST_0][j][i][0])>>1 == 0)
  1776.             &&  (iabs(p->frame.mv[LIST_0][j][i][1])>>1 == 0)))
  1777.             || ((p->frame.ref_idx[LIST_0][j][i] == -1)
  1778.             &&  (p->frame.ref_idx[LIST_1][j][i] == 0)
  1779.             &&  (iabs(p->frame.mv[LIST_1][j][i][0])>>1 == 0)
  1780.             &&  (iabs(p->frame.mv[LIST_1][j][i][1])>>1 == 0))));
  1781.         }
  1782.       }
  1783.     }
  1784.   }
  1785.   else
  1786.   {
  1787.     for (j=0 ; j<fs->size_y/4 ; j++)
  1788.     {
  1789.       jj = RSD(j);
  1790.       for (i=0 ; i<fs->size_x/4 ; i++)
  1791.       {
  1792.         ii = RSD(i);
  1793.         //! Use inference flag to remap mvs/references
  1794.         p->frame.mv[LIST_0][j][i][0] = fs->JVmotion[np].mv[LIST_0][j][i][0];
  1795.         p->frame.mv[LIST_0][j][i][1] = fs->JVmotion[np].mv[LIST_0][j][i][1];
  1796.         p->frame.mv[LIST_1][j][i][0] = fs->JVmotion[np].mv[LIST_1][j][i][0];
  1797.         p->frame.mv[LIST_1][j][i][1] = fs->JVmotion[np].mv[LIST_1][j][i][1];
  1798.         p->frame.ref_idx[LIST_0][j][i] = fs->JVmotion[np].ref_idx[LIST_0][j][i];
  1799.         p->frame.ref_idx[LIST_1][j][i] = fs->JVmotion[np].ref_idx[LIST_1][j][i];
  1800.         p->frame.ref_pic_id[LIST_0][j][i] = fs->JVmotion[np].ref_id[LIST_0][j][i];
  1801.         p->frame.ref_pic_id[LIST_1][j][i] = fs->JVmotion[np].ref_id[LIST_1][j][i];
  1802.         if (img->direct_spatial_mv_pred_flag == 1)
  1803.         {
  1804.           p->frame.moving_block[j][i]= (byte) (
  1805.             !((!p->is_long_term
  1806.             && ((p->frame.ref_idx[LIST_0][j][i] == 0)
  1807.             &&  (iabs(p->frame.mv[LIST_0][j][i][0])>>1 == 0)
  1808.             &&  (iabs(p->frame.mv[LIST_0][j][i][1])>>1 == 0)))
  1809.             || ((p->frame.ref_idx[LIST_0][j][i] == -1)
  1810.             &&  (p->frame.ref_idx[LIST_1][j][i] == 0)
  1811.             &&  (iabs(p->frame.mv[LIST_1][j][i][0])>>1 == 0)
  1812.             &&  (iabs(p->frame.mv[LIST_1][j][i][1])>>1 == 0))));
  1813.         }
  1814.       }
  1815.     }
  1816.   }
  1817.   if (img->direct_spatial_mv_pred_flag ==0)
  1818.   {
  1819.     for (j=0 ; j<fs->size_y/4 ; j++)
  1820.     {
  1821.       for (i=0 ; i<fs->size_x/4 ; i++)
  1822.       {
  1823.         if ((!img->MbaffFrameFlag &&!img->structure && fs->motion.field_frame[j][i]) || (img->MbaffFrameFlag && fs->motion.field_frame[j][i]))
  1824.         {
  1825.           p->frame.mv[LIST_0][j][i][1] *= 2;
  1826.           p->frame.mv[LIST_1][j][i][1] *= 2;
  1827.         }
  1828.         else  if (img->structure && !fs->motion.field_frame[j][i])
  1829.         {
  1830.           p->frame.mv[LIST_0][j][i][1] /= 2;
  1831.           p->frame.mv[LIST_1][j][i][1] /= 2;
  1832.         }
  1833.       }
  1834.     }
  1835.     for (j=0; j<2 + (img->MbaffFrameFlag * 4);j+=2)
  1836.     {
  1837.       for (i=0; i<listXsize[j];i++)
  1838.       {
  1839.         int prescale, iTRb, iTRp;
  1840.         if (j==0)
  1841.         {
  1842.           iTRb = iClip3( -128, 127, dec_picture->poc - listX[LIST_0 + j][i]->poc );
  1843.         }
  1844.         else if (j == 2)
  1845.         {
  1846.           iTRb = iClip3( -128, 127, dec_picture->top_poc - listX[LIST_0 + j][i]->poc );
  1847.         }
  1848.         else
  1849.         {
  1850.           iTRb = iClip3( -128, 127, dec_picture->bottom_poc - listX[LIST_0 + j][i]->poc );
  1851.         }
  1852.         iTRp = iClip3( -128, 127,  listX[LIST_1 + j][0]->poc - listX[LIST_0 + j][i]->poc);
  1853.         if (iTRp!=0)
  1854.         {
  1855.           prescale = ( 16384 + iabs( iTRp / 2 ) ) / iTRp;
  1856.           img->mvscale[j][i] = iClip3( -1024, 1023, ( iTRb * prescale + 32 ) >> 6 ) ;
  1857.         }
  1858.         else
  1859.         {
  1860.           img->mvscale[j][i] = 9999;
  1861.         }
  1862.       }
  1863.     }
  1864.   }
  1865. }
  1866. void copy_storable_param_JV( PicMotionParams *JVplane, PicMotionParams *motion )
  1867. {
  1868.   // copy ref_idx
  1869.   memcpy( JVplane->ref_idx[0][0], motion->ref_idx[0][0], 2 * (img->height / BLOCK_SIZE) * (img->width / BLOCK_SIZE) * sizeof(byte));
  1870.   // copy ref_pic_id
  1871.   memcpy( JVplane->ref_pic_id[0][0], motion->ref_pic_id[0][0], 6 * (img->height / BLOCK_SIZE) * (img->width / BLOCK_SIZE) * sizeof(int64));
  1872.   // copy motion.ref_id
  1873.   memcpy( JVplane->ref_id[0][0], motion->ref_id[0][0], 6 * (img->height / BLOCK_SIZE) * (img->width / BLOCK_SIZE) * sizeof(int64));
  1874.   // copy mv
  1875.   memcpy( JVplane->mv[0][0][0], motion->mv[0][0][0], 2 * (img->height / BLOCK_SIZE) * (img->width / BLOCK_SIZE) * 2 * sizeof(short) );
  1876. }