00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #include "global.h"
00054
00055 #include "fmo.h"
00056
00057
00058 static void FmoGenerateType0MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps);
00059 static void FmoGenerateType1MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps);
00060 static void FmoGenerateType2MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps);
00061 static void FmoGenerateType3MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps);
00062 static void FmoGenerateType4MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps);
00063 static void FmoGenerateType5MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps);
00064 static void FmoGenerateType6MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps);
00065
00066
00067 static int FmoGenerateMapUnitToSliceGroupMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps);
00068 static int FmoGenerateMBAmap (ImageParameters * p_Img, seq_parameter_set_rbsp_t* sps);
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 static int FmoGenerateMapUnitToSliceGroupMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps)
00084 {
00085 p_Img->PicSizeInMapUnits = p_Img->PicHeightInMapUnits * p_Img->PicWidthInMbs;
00086
00087
00088 if (pps->slice_group_map_type == 6)
00089 {
00090 if ((pps->pic_size_in_map_units_minus1+1) != p_Img->PicSizeInMapUnits)
00091 {
00092 error ("wrong pps->pic_size_in_map_units_minus1 for used SPS and FMO type 6", 500);
00093 }
00094 }
00095
00096
00097 if (p_Img->MapUnitToSliceGroupMap)
00098 free (p_Img->MapUnitToSliceGroupMap);
00099
00100 if ((p_Img->MapUnitToSliceGroupMap = malloc ((p_Img->PicSizeInMapUnits) * sizeof (byte))) == NULL)
00101 {
00102 printf ("cannot allocated %d bytes for p_Img->MapUnitToSliceGroupMap , exit\n", (int) ( p_Img->PicSizeInMapUnits * sizeof (byte)));
00103 exit (-1);
00104 }
00105
00106 if (pps->num_slice_groups_minus1 == 0)
00107 {
00108 memset (p_Img->MapUnitToSliceGroupMap , 0, p_Img->PicSizeInMapUnits * sizeof (byte));
00109 return 0;
00110 }
00111
00112 switch (pps->slice_group_map_type)
00113 {
00114 case 0:
00115 FmoGenerateType0MapUnitMap (p_Img, pps);
00116 break;
00117 case 1:
00118 FmoGenerateType1MapUnitMap (p_Img, pps);
00119 break;
00120 case 2:
00121 FmoGenerateType2MapUnitMap (p_Img, pps);
00122 break;
00123 case 3:
00124 FmoGenerateType3MapUnitMap (p_Img, pps);
00125 break;
00126 case 4:
00127 FmoGenerateType4MapUnitMap (p_Img, pps);
00128 break;
00129 case 5:
00130 FmoGenerateType5MapUnitMap (p_Img, pps);
00131 break;
00132 case 6:
00133 FmoGenerateType6MapUnitMap (p_Img, pps);
00134 break;
00135 default:
00136 printf ("Illegal slice_group_map_type %d , exit \n", pps->slice_group_map_type);
00137 exit (-1);
00138 }
00139 return 0;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 static int FmoGenerateMBAmap (ImageParameters * p_Img, seq_parameter_set_rbsp_t* sps)
00156 {
00157 unsigned i;
00158
00159
00160 if (p_Img->MBAmap)
00161 free (p_Img->MBAmap);
00162
00163
00164 if ((p_Img->MBAmap = malloc ((p_Img->PicSizeInMbs) * sizeof (byte))) == NULL)
00165 {
00166 printf ("cannot allocated %d bytes for p_Img->MBAmap, exit\n", (int) ((p_Img->PicSizeInMbs) * sizeof (byte)));
00167 exit (-1);
00168 }
00169
00170 if ((sps->frame_mbs_only_flag) || p_Img->field_picture)
00171 {
00172 for (i=0; i<p_Img->PicSizeInMbs; i++)
00173 {
00174 p_Img->MBAmap[i] = p_Img->MapUnitToSliceGroupMap [i];
00175 }
00176 }
00177 else
00178 if (sps->mb_adaptive_frame_field_flag && (! p_Img->field_picture))
00179 {
00180 for (i=0; i<p_Img->PicSizeInMbs; i++)
00181 {
00182 p_Img->MBAmap[i] = p_Img->MapUnitToSliceGroupMap [i >> 1];
00183 }
00184 }
00185 else
00186 {
00187 for (i=0; i<p_Img->PicSizeInMbs; i++)
00188 {
00189 p_Img->MBAmap[i] = p_Img->MapUnitToSliceGroupMap [(i/(2*p_Img->PicWidthInMbs))*p_Img->PicWidthInMbs+(i%p_Img->PicWidthInMbs)];
00190 }
00191 }
00192 return 0;
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 int FmoInit(ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps, seq_parameter_set_rbsp_t * sps)
00210 {
00211
00212 #ifdef PRINT_FMO_MAPS
00213 unsigned i,j;
00214 int bottom;
00215 #endif
00216
00217 int k;
00218 for (k = 0; k < MAXSLICEGROUPIDS; k++)
00219 p_Img->FirstMBInSlice[k] = -1;
00220
00221 FmoGenerateMapUnitToSliceGroupMap(p_Img, pps);
00222 FmoGenerateMBAmap(p_Img, sps);
00223
00224 #ifdef PRINT_FMO_MAPS
00225 printf("\n");
00226 printf("FMO Map (Units):\n");
00227
00228 for (j=0; j<p_Img->PicHeightInMapUnits; j++)
00229 {
00230 for (i=0; i<p_Img->PicWidthInMbs; i++)
00231 {
00232 printf("%d ",p_Img->MapUnitToSliceGroupMap [i+j*p_Img->PicWidthInMbs]);
00233 }
00234 printf("\n");
00235 }
00236 printf("\n");
00237
00238 if(sps->mb_adaptive_frame_field_flag==0)
00239 {
00240 printf("FMO Map (Mb):\n");
00241 for (j=0; j<(p_Img->PicSizeInMbs/p_Img->PicWidthInMbs); j++)
00242 {
00243 for (i=0; i<p_Img->PicWidthInMbs; i++)
00244 {
00245 printf("%d ",p_Img->MBAmap[i+j*p_Img->PicWidthInMbs]);
00246 }
00247 printf("\n");
00248 }
00249 printf("\n");
00250 }
00251 else
00252 {
00253 printf("FMO Map (Mb in scan order for MBAFF):\n");
00254 for (j=0; j<(p_Img->PicSizeInMbs/p_Img->PicWidthInMbs); j++)
00255 {
00256 for (i=0; i<p_Img->PicWidthInMbs; i++)
00257 {
00258 bottom=(j & 0x01);
00259 printf("%d ",p_Img->MBAmap[(j-bottom)*p_Img->PicWidthInMbs+i*2+bottom]);
00260 }
00261 printf("\n");
00262
00263 }
00264 printf("\n");
00265
00266 }
00267
00268 #endif
00269
00270 return 0;
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280 void FmoUninit(ImageParameters *p_Img)
00281 {
00282 if (p_Img->MBAmap)
00283 {
00284 free (p_Img->MBAmap);
00285 p_Img->MBAmap = NULL;
00286 }
00287 if (p_Img->MapUnitToSliceGroupMap )
00288 {
00289 free (p_Img->MapUnitToSliceGroupMap );
00290 p_Img->MapUnitToSliceGroupMap = NULL;
00291 }
00292
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 static void FmoGenerateType0MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps )
00308 {
00309 unsigned iGroup, j;
00310 unsigned i = 0;
00311 do
00312 {
00313 for( iGroup = 0;
00314 (iGroup <= pps->num_slice_groups_minus1) && (i < p_Img->PicSizeInMapUnits);
00315 i += pps->run_length_minus1[iGroup++] + 1)
00316 {
00317 for( j = 0; j <= pps->run_length_minus1[ iGroup ] && i + j < p_Img->PicSizeInMapUnits; j++ )
00318 p_Img->MapUnitToSliceGroupMap [i+j] = (byte) iGroup;
00319 }
00320 }
00321 while( i < p_Img->PicSizeInMapUnits );
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 static void FmoGenerateType1MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps )
00337 {
00338 unsigned i;
00339 for( i = 0; i < p_Img->PicSizeInMapUnits; i++ )
00340 {
00341 p_Img->MapUnitToSliceGroupMap [i] = (byte) (((i%p_Img->PicWidthInMbs)+(((i/p_Img->PicWidthInMbs)*(pps->num_slice_groups_minus1+1))>>1))
00342 %(pps->num_slice_groups_minus1+1));
00343 }
00344 }
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 static void FmoGenerateType2MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps )
00358 {
00359 int iGroup;
00360 unsigned i, x, y;
00361 unsigned yTopLeft, xTopLeft, yBottomRight, xBottomRight;
00362
00363 for( i = 0; i < p_Img->PicSizeInMapUnits; i++ )
00364 p_Img->MapUnitToSliceGroupMap [ i ] = (byte) pps->num_slice_groups_minus1;
00365
00366 for( iGroup = pps->num_slice_groups_minus1 - 1 ; iGroup >= 0; iGroup-- )
00367 {
00368 yTopLeft = pps->top_left[ iGroup ] / p_Img->PicWidthInMbs;
00369 xTopLeft = pps->top_left[ iGroup ] % p_Img->PicWidthInMbs;
00370 yBottomRight = pps->bottom_right[ iGroup ] / p_Img->PicWidthInMbs;
00371 xBottomRight = pps->bottom_right[ iGroup ] % p_Img->PicWidthInMbs;
00372 for( y = yTopLeft; y <= yBottomRight; y++ )
00373 for( x = xTopLeft; x <= xBottomRight; x++ )
00374 p_Img->MapUnitToSliceGroupMap [ y * p_Img->PicWidthInMbs + x ] = (byte) iGroup;
00375 }
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390 static void FmoGenerateType3MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps )
00391 {
00392 unsigned i, k;
00393 int leftBound, topBound, rightBound, bottomBound;
00394 int x, y, xDir, yDir;
00395 int mapUnitVacant;
00396
00397 unsigned mapUnitsInSliceGroup0 = imin((pps->slice_group_change_rate_minus1 + 1) * p_Img->slice_group_change_cycle, p_Img->PicSizeInMapUnits);
00398
00399 for( i = 0; i < p_Img->PicSizeInMapUnits; i++ )
00400 p_Img->MapUnitToSliceGroupMap [ i ] = 2;
00401
00402 x = ( p_Img->PicWidthInMbs - pps->slice_group_change_direction_flag ) / 2;
00403 y = ( p_Img->PicHeightInMapUnits - pps->slice_group_change_direction_flag ) / 2;
00404
00405 leftBound = x;
00406 topBound = y;
00407 rightBound = x;
00408 bottomBound = y;
00409
00410 xDir = pps->slice_group_change_direction_flag - 1;
00411 yDir = pps->slice_group_change_direction_flag;
00412
00413 for( k = 0; k < p_Img->PicSizeInMapUnits; k += mapUnitVacant )
00414 {
00415 mapUnitVacant = ( p_Img->MapUnitToSliceGroupMap [ y * p_Img->PicWidthInMbs + x ] == 2 );
00416 if( mapUnitVacant )
00417 p_Img->MapUnitToSliceGroupMap [ y * p_Img->PicWidthInMbs + x ] = (byte) ( k >= mapUnitsInSliceGroup0 );
00418
00419 if( xDir == -1 && x == leftBound )
00420 {
00421 leftBound = imax( leftBound - 1, 0 );
00422 x = leftBound;
00423 xDir = 0;
00424 yDir = 2 * pps->slice_group_change_direction_flag - 1;
00425 }
00426 else
00427 if( xDir == 1 && x == rightBound )
00428 {
00429 rightBound = imin( rightBound + 1, (int)p_Img->PicWidthInMbs - 1 );
00430 x = rightBound;
00431 xDir = 0;
00432 yDir = 1 - 2 * pps->slice_group_change_direction_flag;
00433 }
00434 else
00435 if( yDir == -1 && y == topBound )
00436 {
00437 topBound = imax( topBound - 1, 0 );
00438 y = topBound;
00439 xDir = 1 - 2 * pps->slice_group_change_direction_flag;
00440 yDir = 0;
00441 }
00442 else
00443 if( yDir == 1 && y == bottomBound )
00444 {
00445 bottomBound = imin( bottomBound + 1, (int)p_Img->PicHeightInMapUnits - 1 );
00446 y = bottomBound;
00447 xDir = 2 * pps->slice_group_change_direction_flag - 1;
00448 yDir = 0;
00449 }
00450 else
00451 {
00452 x = x + xDir;
00453 y = y + yDir;
00454 }
00455 }
00456
00457 }
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470 static void FmoGenerateType4MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps )
00471 {
00472
00473 unsigned mapUnitsInSliceGroup0 = imin((pps->slice_group_change_rate_minus1 + 1) * p_Img->slice_group_change_cycle, p_Img->PicSizeInMapUnits);
00474 unsigned sizeOfUpperLeftGroup = pps->slice_group_change_direction_flag ? ( p_Img->PicSizeInMapUnits - mapUnitsInSliceGroup0 ) : mapUnitsInSliceGroup0;
00475
00476 unsigned i;
00477
00478 for( i = 0; i < p_Img->PicSizeInMapUnits; i++ )
00479 if( i < sizeOfUpperLeftGroup )
00480 p_Img->MapUnitToSliceGroupMap [ i ] = (byte) pps->slice_group_change_direction_flag;
00481 else
00482 p_Img->MapUnitToSliceGroupMap [ i ] = (byte) (1 - pps->slice_group_change_direction_flag);
00483
00484 }
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 static void FmoGenerateType5MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps )
00498 {
00499
00500 unsigned mapUnitsInSliceGroup0 = imin((pps->slice_group_change_rate_minus1 + 1) * p_Img->slice_group_change_cycle, p_Img->PicSizeInMapUnits);
00501 unsigned sizeOfUpperLeftGroup = pps->slice_group_change_direction_flag ? ( p_Img->PicSizeInMapUnits - mapUnitsInSliceGroup0 ) : mapUnitsInSliceGroup0;
00502
00503 unsigned i,j, k = 0;
00504
00505 for( j = 0; j < p_Img->PicWidthInMbs; j++ )
00506 for( i = 0; i < p_Img->PicHeightInMapUnits; i++ )
00507 if( k++ < sizeOfUpperLeftGroup )
00508 p_Img->MapUnitToSliceGroupMap [ i * p_Img->PicWidthInMbs + j ] = (byte) pps->slice_group_change_direction_flag;
00509 else
00510 p_Img->MapUnitToSliceGroupMap [ i * p_Img->PicWidthInMbs + j ] = (byte) (1 - pps->slice_group_change_direction_flag);
00511
00512 }
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 static void FmoGenerateType6MapUnitMap (ImageParameters * p_Img, pic_parameter_set_rbsp_t * pps )
00526 {
00527 unsigned i;
00528 for (i=0; i<p_Img->PicSizeInMapUnits; i++)
00529 {
00530 p_Img->MapUnitToSliceGroupMap [i] = (byte) pps->slice_group_id[i];
00531 }
00532 }
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 int FmoStartPicture (ImageParameters *p_Img)
00544 {
00545 int i;
00546
00547 assert (p_Img->MBAmap != NULL);
00548
00549 for (i=0; i<MAXSLICEGROUPIDS; i++)
00550 p_Img->FirstMBInSlice[i] = FmoGetFirstMBOfSliceGroup (p_Img, i);
00551 return 0;
00552 }
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566 int FmoEndPicture ()
00567 {
00568
00569 return 0;
00570 }
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582 int FmoMB2SliceGroup (ImageParameters *p_Img, int mb)
00583 {
00584 assert (mb < (int)p_Img->PicSizeInMbs);
00585 assert (p_Img->MBAmap != NULL);
00586 return p_Img->MBAmap[mb];
00587 }
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599 int FmoGetNextMBNr (ImageParameters *p_Img, int CurrentMbNr)
00600 {
00601
00602 int SliceGroupID = FmoMB2SliceGroup (p_Img, CurrentMbNr);
00603
00604 while (++CurrentMbNr<(int)p_Img->PicSizeInMbs && p_Img->MBAmap[CurrentMbNr] != SliceGroupID)
00605 ;
00606
00607 if (CurrentMbNr >= (int)p_Img->PicSizeInMbs)
00608 return -1;
00609 else
00610 return CurrentMbNr;
00611 }
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624 int FmoGetPreviousMBNr (ImageParameters *p_Img, int CurrentMbNr)
00625 {
00626
00627 int SliceGroupID = FmoMB2SliceGroup (p_Img, CurrentMbNr);
00628 CurrentMbNr--;
00629 while (CurrentMbNr>=0 && p_Img->MBAmap[CurrentMbNr] != SliceGroupID)
00630 CurrentMbNr--;
00631
00632 if (CurrentMbNr < 0)
00633 return -1;
00634 else
00635 return CurrentMbNr;
00636 }
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649 int FmoGetFirstMBOfSliceGroup (ImageParameters *p_Img, int SliceGroupID)
00650 {
00651 int i = 0;
00652 while ((i<(int)p_Img->PicSizeInMbs) && (FmoMB2SliceGroup (p_Img, i) != SliceGroupID))
00653 i++;
00654
00655 if (i < (int)p_Img->PicSizeInMbs)
00656 return i;
00657 else
00658 return -1;
00659 }
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675 int FmoGetLastCodedMBOfSliceGroup (ImageParameters *p_Img, int SliceGroupID)
00676 {
00677 int i;
00678 int LastMB = -1;
00679
00680 for (i=0; i<(int)p_Img->PicSizeInMbs; i++)
00681 if (FmoMB2SliceGroup (p_Img, i) == SliceGroupID)
00682 LastMB = i;
00683 return LastMB;
00684 }
00685
00686
00687 void FmoSetLastMacroblockInSlice ( ImageParameters *p_Img, int mb)
00688 {
00689
00690
00691
00692
00693 int currSliceGroup = FmoMB2SliceGroup (p_Img, mb);
00694 assert (mb >= 0);
00695 mb = FmoGetNextMBNr (p_Img, mb);
00696 p_Img->FirstMBInSlice[currSliceGroup] = mb;
00697 }
00698
00699 int FmoGetFirstMacroblockInSlice ( ImageParameters *p_Img, int SliceGroup)
00700 {
00701 return p_Img->FirstMBInSlice[SliceGroup];
00702
00703
00704 }
00705
00706
00707 int FmoSliceGroupCompletelyCoded( ImageParameters *p_Img, int SliceGroupID)
00708 {
00709 if (FmoGetFirstMacroblockInSlice (p_Img, SliceGroupID) < 0)
00710 return TRUE;
00711 else
00712 return FALSE;
00713 }
00714
00715
00716