00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "global.h"
00016 #include "memalloc.h"
00017 #include "rtp.h"
00018 #include "mbuffer.h"
00019 #include "sei.h"
00020 #include "vlc.h"
00021 #include "header.h"
00022
00023 Boolean seiHasTemporal_reference=FALSE;
00024 Boolean seiHasClock_timestamp=FALSE;
00025 Boolean seiHasPanscan_rect=FALSE;
00026 Boolean seiHasHrd_picture=FALSE;
00027 Boolean seiHasFiller_payload=FALSE;
00028 Boolean seiHasUser_data_registered_itu_t_t35=FALSE;
00029 Boolean seiHasUser_data_unregistered=FALSE;
00030 Boolean seiHasRecoveryPoint_info=FALSE;
00031 Boolean seiHasRef_pic_buffer_management_repetition=FALSE;
00032 Boolean seiHasSpare_picture=FALSE;
00033 Boolean seiHasBuffering_period=FALSE;
00034 Boolean seiHasPicTiming_info=FALSE;
00035 Boolean seiHasSceneInformation=FALSE;
00036 Boolean seiHasSubseq_information=FALSE;
00037 Boolean seiHasSubseq_layer_characteristics=FALSE;
00038 Boolean seiHasSubseq_characteristics=FALSE;
00039 Boolean seiHasTone_mapping=FALSE;
00040 Boolean seiHasPostFilterHints_info=FALSE;
00041 Boolean seiHasDRPMRepetition_info=FALSE;
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 sei_struct sei_message[2];
00055
00056 void InitSEIMessages()
00057 {
00058 int i;
00059 for (i=0; i<2; i++)
00060 {
00061 sei_message[i].data = malloc(MAXRTPPAYLOADLEN);
00062 if( sei_message[i].data == NULL ) no_mem_exit("InitSEIMessages: sei_message[i].data");
00063 sei_message[i].subPacketType = SEI_PACKET_TYPE;
00064 clear_sei_message(i);
00065 }
00066
00067
00068 seiSparePicturePayload.data = NULL;
00069 InitSparePicture();
00070 if (params->NumFramesInELSubSeq != 0)
00071 {
00072 InitSubseqLayerInfo();
00073 InitSubseqChar();
00074 }
00075 InitSceneInformation();
00076
00077 InitPanScanRectInfo();
00078
00079 InitUser_data_unregistered();
00080
00081 InitUser_data_registered_itu_t_t35();
00082
00083 InitRandomAccess();
00084
00085 InitToneMapping();
00086
00087 InitPostFilterHints();
00088
00089 InitBufferingPeriod();
00090
00091 InitPicTiming();
00092
00093 InitDRPMRepetition();
00094 }
00095
00096 void CloseSEIMessages()
00097 {
00098 int i;
00099
00100 if (params->NumFramesInELSubSeq != 0)
00101 CloseSubseqLayerInfo();
00102
00103 CloseSubseqChar();
00104 CloseSparePicture();
00105 CloseSceneInformation();
00106 ClosePanScanRectInfo();
00107 CloseUser_data_unregistered();
00108 CloseUser_data_registered_itu_t_t35();
00109 CloseRandomAccess();
00110 CloseToneMapping();
00111 ClosePostFilterHints();
00112 CloseBufferingPeriod();
00113 ClosePicTiming();
00114 CloseDRPMRepetition();
00115
00116 for (i=0; i<MAX_LAYER_NUMBER; i++)
00117 {
00118 if ( sei_message[i].data ) free( sei_message[i].data );
00119 sei_message[i].data = NULL;
00120 }
00121 }
00122
00123 Boolean HaveAggregationSEI()
00124 {
00125 if (sei_message[AGGREGATION_SEI].available && img->type != B_SLICE)
00126 return TRUE;
00127 if (seiHasSubseqInfo)
00128 return TRUE;
00129 if (seiHasSubseqLayerInfo && img->number == 0)
00130 return TRUE;
00131 if (seiHasSubseqChar)
00132 return TRUE;
00133 if (seiHasSceneInformation)
00134 return TRUE;
00135 if (seiHasPanScanRectInfo)
00136 return TRUE;
00137 if (seiHasUser_data_unregistered_info)
00138 return TRUE;
00139 if (seiHasUser_data_registered_itu_t_t35_info)
00140 return TRUE;
00141 if (seiHasRecoveryPoint_info)
00142 return TRUE;
00143 if (seiHasTone_mapping)
00144 return TRUE;
00145 if (seiHasPostFilterHints_info)
00146 return TRUE;
00147 if (seiHasBuffering_period)
00148 return TRUE;
00149 if (seiHasPicTiming_info)
00150 return TRUE;
00151 if (seiHasDRPMRepetition_info)
00152 return TRUE;
00153
00154 return FALSE;
00155
00156
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 void write_sei_message(int id, byte* payload, int payload_size, int payload_type)
00178 {
00179 int offset, type, size;
00180 assert(payload_type >= 0 && payload_type < SEI_MAX_ELEMENTS);
00181
00182 type = payload_type;
00183 size = payload_size;
00184 offset = sei_message[id].payloadSize;
00185
00186 while ( type > 254 )
00187 {
00188 sei_message[id].data[offset++] = 0xFF;
00189 type = type - 255;
00190 }
00191 sei_message[id].data[offset++] = (byte) type;
00192
00193 while ( size > 254 )
00194 {
00195 sei_message[id].data[offset++] = 0xFF;
00196 size = size - 255;
00197 }
00198 sei_message[id].data[offset++] = (byte) size;
00199
00200 memcpy(sei_message[id].data + offset, payload, payload_size);
00201 offset += payload_size;
00202
00203 sei_message[id].payloadSize = offset;
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217 void finalize_sei_message(int id)
00218 {
00219 int offset = sei_message[id].payloadSize;
00220
00221 sei_message[id].data[offset] = 0x80;
00222 sei_message[id].payloadSize++;
00223
00224 sei_message[id].available = TRUE;
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 void clear_sei_message(int id)
00240 {
00241 memset( sei_message[id].data, 0, MAXRTPPAYLOADLEN);
00242 sei_message[id].payloadSize = 0;
00243 sei_message[id].available = FALSE;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 void AppendTmpbits2Buf( Bitstream* dest, Bitstream* source )
00259 {
00260 int i, j;
00261 byte mask;
00262 int bits_in_last_byte;
00263
00264
00265 for (i=0; i<source->byte_pos; i++)
00266 {
00267 mask = 0x80;
00268 for (j=0; j<8; j++)
00269 {
00270 dest->byte_buf <<= 1;
00271 if (source->streamBuffer[i] & mask)
00272 dest->byte_buf |= 1;
00273 dest->bits_to_go--;
00274 mask >>= 1;
00275 if (dest->bits_to_go==0)
00276 {
00277 dest->bits_to_go = 8;
00278 dest->streamBuffer[dest->byte_pos++]=dest->byte_buf;
00279 dest->byte_buf = 0;
00280 }
00281 }
00282 }
00283
00284 bits_in_last_byte = 8-source->bits_to_go;
00285 if ( bits_in_last_byte > 0 )
00286 {
00287 mask = (byte) (1 << (bits_in_last_byte-1));
00288 for (j=0; j<bits_in_last_byte; j++)
00289 {
00290 dest->byte_buf <<= 1;
00291 if (source->byte_buf & mask)
00292 dest->byte_buf |= 1;
00293 dest->bits_to_go--;
00294 mask >>= 1;
00295 if (dest->bits_to_go==0)
00296 {
00297 dest->bits_to_go = 8;
00298 dest->streamBuffer[dest->byte_pos++]=dest->byte_buf;
00299 dest->byte_buf = 0;
00300 }
00301 }
00302 }
00303 }
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 Boolean seiHasSparePicture = FALSE;
00322 spare_picture_struct seiSparePicturePayload;
00323
00324
00325
00326
00327
00328
00329
00330 void InitSparePicture()
00331 {
00332 if ( seiSparePicturePayload.data != NULL ) CloseSparePicture();
00333
00334 seiSparePicturePayload.data = malloc( sizeof(Bitstream) );
00335 if ( seiSparePicturePayload.data == NULL ) no_mem_exit("InitSparePicture: seiSparePicturePayload.data");
00336 seiSparePicturePayload.data->streamBuffer = malloc(MAXRTPPAYLOADLEN);
00337 if ( seiSparePicturePayload.data->streamBuffer == NULL ) no_mem_exit("InitSparePicture: seiSparePicturePayload.data->streamBuffer");
00338 memset( seiSparePicturePayload.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
00339 seiSparePicturePayload.num_spare_pics = 0;
00340 seiSparePicturePayload.target_frame_num = 0;
00341
00342 seiSparePicturePayload.data->bits_to_go = 8;
00343 seiSparePicturePayload.data->byte_pos = 0;
00344 seiSparePicturePayload.data->byte_buf = 0;
00345 }
00346
00347
00348
00349
00350
00351
00352
00353 void CloseSparePicture()
00354 {
00355 if (seiSparePicturePayload.data->streamBuffer)
00356 free(seiSparePicturePayload.data->streamBuffer);
00357 seiSparePicturePayload.data->streamBuffer = NULL;
00358 if (seiSparePicturePayload.data)
00359 free(seiSparePicturePayload.data);
00360 seiSparePicturePayload.data = NULL;
00361 seiSparePicturePayload.num_spare_pics = 0;
00362 seiSparePicturePayload.target_frame_num = 0;
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 void CalculateSparePicture()
00377 {
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540 }
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557 void ComposeSparePictureMessage(int delta_spare_frame_num, int ref_area_indicator, Bitstream *tmpBitstream)
00558 {
00559 Bitstream *bitstream = seiSparePicturePayload.data;
00560 SyntaxElement sym;
00561
00562 sym.type = SE_HEADER;
00563 sym.mapping = ue_linfo;
00564
00565 sym.value1 = delta_spare_frame_num;
00566 writeSyntaxElement2Buf_UVLC(&sym, bitstream);
00567 sym.value1 = ref_area_indicator;
00568 writeSyntaxElement2Buf_UVLC(&sym, bitstream);
00569
00570 AppendTmpbits2Buf( bitstream, tmpBitstream );
00571 }
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587 Boolean CompressSpareMBMap(unsigned char **map_sp, Bitstream *bitstream)
00588 {
00589 int j, k;
00590 int noc, bit0, bit1, bitc;
00591 SyntaxElement sym;
00592 int x, y, left, right, bottom, top, directx, directy;
00593
00594
00595 int size_uncompressed = (img->height/16) * (img->width/16);
00596 int size_compressed = 0;
00597 Boolean ret;
00598
00599
00600 sym.type = SE_HEADER;
00601 sym.mapping = ue_linfo;
00602 noc = 0;
00603 bit0 = 0;
00604 bit1 = 1;
00605 bitc = bit0;
00606
00607
00608 x = ( img->width/16 - 1 ) / 2;
00609 y = ( img->height/16 - 1 ) / 2;
00610 left = right = x;
00611 top = bottom = y;
00612 directx = 0;
00613 directy = 1;
00614 for (j=0; j<img->height/16; j++)
00615 for (k=0; k<img->width/16; k++)
00616 {
00617
00618 if ( map_sp[y][x] == bitc ) noc++;
00619 else
00620 {
00621 sym.value1 = noc;
00622 size_compressed += writeSyntaxElement2Buf_UVLC(&sym, bitstream);
00623 noc=0;
00624 }
00625
00626 if ( directx == -1 && directy == 0 )
00627 {
00628 if (x > left) x--;
00629 else if (x == 0)
00630 {
00631 y = bottom + 1;
00632 bottom++;
00633 directx = 1;
00634 directy = 0;
00635 }
00636 else if (x == left)
00637 {
00638 x--;
00639 left--;
00640 directx = 0;
00641 directy = 1;
00642 }
00643 }
00644 else if ( directx == 1 && directy == 0 )
00645 {
00646 if (x < right) x++;
00647 else if (x == img->width/16 - 1)
00648 {
00649 y = top - 1;
00650 top--;
00651 directx = -1;
00652 directy = 0;
00653 }
00654 else if (x == right)
00655 {
00656 x++;
00657 right++;
00658 directx = 0;
00659 directy = -1;
00660 }
00661 }
00662 else if ( directx == 0 && directy == -1 )
00663 {
00664 if ( y > top) y--;
00665 else if (y == 0)
00666 {
00667 x = left - 1;
00668 left--;
00669 directx = 0;
00670 directy = 1;
00671 }
00672 else if (y == top)
00673 {
00674 y--;
00675 top--;
00676 directx = -1;
00677 directy = 0;
00678 }
00679 }
00680 else if ( directx == 0 && directy == 1 )
00681 {
00682 if (y < bottom) y++;
00683 else if (y == img->height/16 - 1)
00684 {
00685 x = right+1;
00686 right++;
00687 directx = 0;
00688 directy = -1;
00689 }
00690 else if (y == bottom)
00691 {
00692 y++;
00693 bottom++;
00694 directx = 1;
00695 directy = 0;
00696 }
00697 }
00698 }
00699 if (noc!=0)
00700 {
00701 sym.value1 = noc;
00702 size_compressed += writeSyntaxElement2Buf_UVLC(&sym, bitstream);
00703 }
00704
00705 ret = (size_compressed<size_uncompressed? TRUE : FALSE);
00706 if ( !ret )
00707 {
00708
00709 bitstream->byte_buf = 0;
00710 bitstream->bits_to_go = 8;
00711 bitstream->byte_pos = 0;
00712 for (j=0; j<img->height/16; j++)
00713 {
00714 for (k=0; k<img->width/16; k++)
00715 {
00716 bitstream->byte_buf <<= 1;
00717 if (map_sp[j][k]) bitstream->byte_buf |= 1;
00718 bitstream->bits_to_go--;
00719 if (bitstream->bits_to_go==0)
00720 {
00721 bitstream->bits_to_go = 8;
00722 bitstream->streamBuffer[bitstream->byte_pos++]=bitstream->byte_buf;
00723 bitstream->byte_buf = 0;
00724 }
00725 }
00726 }
00727 }
00728
00729 return ret;
00730 }
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747 void FinalizeSpareMBMap()
00748 {
00749 int CurrFrameNum = img->number % MAX_FN;
00750 int delta_frame_num;
00751 SyntaxElement sym;
00752 Bitstream *dest, *source;
00753
00754 sym.type = SE_HEADER;
00755 sym.mapping = ue_linfo;
00756
00757 source = seiSparePicturePayload.data;
00758 dest = malloc(sizeof(Bitstream));
00759 if ( dest == NULL ) no_mem_exit("FinalizeSpareMBMap: dest");
00760 dest->streamBuffer = malloc(MAXRTPPAYLOADLEN);
00761 if ( dest->streamBuffer == NULL ) no_mem_exit("FinalizeSpareMBMap: dest->streamBuffer");
00762 dest->bits_to_go = 8;
00763 dest->byte_pos = 0;
00764 dest->byte_buf = 0;
00765 memset( dest->streamBuffer, 0, MAXRTPPAYLOADLEN);
00766
00767
00768 delta_frame_num = CurrFrameNum - seiSparePicturePayload.target_frame_num;
00769 if ( delta_frame_num < 0 ) delta_frame_num += MAX_FN;
00770 sym.value1 = delta_frame_num;
00771 writeSyntaxElement2Buf_UVLC(&sym, dest);
00772
00773
00774 sym.value1 = seiSparePicturePayload.num_spare_pics - 1;
00775 writeSyntaxElement2Buf_UVLC(&sym, dest);
00776
00777
00778 AppendTmpbits2Buf( dest, source);
00779
00780
00781 if ( dest->bits_to_go != 8 )
00782 {
00783 (dest->byte_buf) <<= 1;
00784 dest->byte_buf |= 1;
00785 dest->bits_to_go--;
00786 if ( dest->bits_to_go != 0 ) (dest->byte_buf) <<= (dest->bits_to_go);
00787 dest->bits_to_go = 8;
00788 dest->streamBuffer[dest->byte_pos++]=dest->byte_buf;
00789 dest->byte_buf = 0;
00790 }
00791 seiSparePicturePayload.payloadSize = dest->byte_pos;
00792
00793
00794 seiSparePicturePayload.data = dest;
00795 free( source->streamBuffer );
00796 free( source );
00797 }
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809 Boolean seiHasSubseqInfo = FALSE;
00810 subseq_information_struct seiSubseqInfo[MAX_LAYER_NUMBER];
00811
00812
00813
00814
00815
00816
00817
00818 void InitSubseqInfo(int currLayer)
00819 {
00820 static unsigned short id = 0;
00821
00822 seiHasSubseqInfo = TRUE;
00823 seiSubseqInfo[currLayer].subseq_layer_num = currLayer;
00824 seiSubseqInfo[currLayer].subseq_id = id++;
00825 seiSubseqInfo[currLayer].last_picture_flag = 0;
00826 seiSubseqInfo[currLayer].stored_frame_cnt = -1;
00827 seiSubseqInfo[currLayer].payloadSize = 0;
00828
00829 seiSubseqInfo[currLayer].data = malloc( sizeof(Bitstream) );
00830 if ( seiSubseqInfo[currLayer].data == NULL ) no_mem_exit("InitSubseqInfo: seiSubseqInfo[currLayer].data");
00831 seiSubseqInfo[currLayer].data->streamBuffer = malloc( MAXRTPPAYLOADLEN );
00832 if ( seiSubseqInfo[currLayer].data->streamBuffer == NULL ) no_mem_exit("InitSubseqInfo: seiSubseqInfo[currLayer].data->streamBuffer");
00833 seiSubseqInfo[currLayer].data->bits_to_go = 8;
00834 seiSubseqInfo[currLayer].data->byte_pos = 0;
00835 seiSubseqInfo[currLayer].data->byte_buf = 0;
00836 memset( seiSubseqInfo[currLayer].data->streamBuffer, 0, MAXRTPPAYLOADLEN );
00837 }
00838
00839
00840
00841
00842
00843
00844
00845 void UpdateSubseqInfo(int currLayer)
00846 {
00847 if (img->type != B_SLICE)
00848 {
00849 seiSubseqInfo[currLayer].stored_frame_cnt ++;
00850 seiSubseqInfo[currLayer].stored_frame_cnt = seiSubseqInfo[currLayer].stored_frame_cnt % MAX_FN;
00851 }
00852
00853 if ( currLayer == 0 )
00854 {
00855 if ( img->number == params->no_frm_base - 1 )
00856 seiSubseqInfo[currLayer].last_picture_flag = 1;
00857 else
00858 seiSubseqInfo[currLayer].last_picture_flag = 0;
00859 }
00860 if ( currLayer == 1 )
00861 {
00862 if ( ((img->gop_number % (params->NumFramesInELSubSeq + 1) == 0) && (params->NumberBFrames != 0) && (img->gop_number > 0)) ||
00863 ((img->gop_number % (params->NumFramesInELSubSeq + 1) == params->NumFramesInELSubSeq) && (params->NumberBFrames==0))
00864 )
00865 seiSubseqInfo[currLayer].last_picture_flag = 1;
00866 else
00867 seiSubseqInfo[currLayer].last_picture_flag = 0;
00868 }
00869 }
00870
00871
00872
00873
00874
00875
00876
00877 void FinalizeSubseqInfo(int currLayer)
00878 {
00879 SyntaxElement sym;
00880 Bitstream *dest = seiSubseqInfo[currLayer].data;
00881
00882 sym.type = SE_HEADER;
00883 sym.mapping = ue_linfo;
00884
00885 sym.value1 = seiSubseqInfo[currLayer].subseq_layer_num;
00886 writeSyntaxElement2Buf_UVLC(&sym, dest);
00887 sym.value1 = seiSubseqInfo[currLayer].subseq_id;
00888 writeSyntaxElement2Buf_UVLC(&sym, dest);
00889 sym.bitpattern = seiSubseqInfo[currLayer].last_picture_flag;
00890 sym.len = 1;
00891 writeSyntaxElement2Buf_Fixed(&sym, dest);
00892 sym.value1 = seiSubseqInfo[currLayer].stored_frame_cnt;
00893 writeSyntaxElement2Buf_UVLC(&sym, dest);
00894
00895
00896 if ( dest->bits_to_go != 8 )
00897 {
00898 (dest->byte_buf) <<= 1;
00899 dest->byte_buf |= 1;
00900 dest->bits_to_go--;
00901 if ( dest->bits_to_go != 0 ) (dest->byte_buf) <<= (dest->bits_to_go);
00902 dest->bits_to_go = 8;
00903 dest->streamBuffer[dest->byte_pos++]=dest->byte_buf;
00904 dest->byte_buf = 0;
00905 }
00906 seiSubseqInfo[currLayer].payloadSize = dest->byte_pos;
00907
00908
00909 }
00910
00911
00912
00913
00914
00915
00916
00917 void ClearSubseqInfoPayload(int currLayer)
00918 {
00919 seiSubseqInfo[currLayer].data->bits_to_go = 8;
00920 seiSubseqInfo[currLayer].data->byte_pos = 0;
00921 seiSubseqInfo[currLayer].data->byte_buf = 0;
00922 memset( seiSubseqInfo[currLayer].data->streamBuffer, 0, MAXRTPPAYLOADLEN );
00923 seiSubseqInfo[currLayer].payloadSize = 0;
00924 }
00925
00926
00927
00928
00929
00930
00931
00932 void CloseSubseqInfo(int currLayer)
00933 {
00934 seiSubseqInfo[currLayer].stored_frame_cnt = -1;
00935 seiSubseqInfo[currLayer].payloadSize = 0;
00936
00937 free( seiSubseqInfo[currLayer].data->streamBuffer );
00938 free( seiSubseqInfo[currLayer].data );
00939 }
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951 Boolean seiHasSubseqLayerInfo = FALSE;
00952 subseq_layer_information_struct seiSubseqLayerInfo;
00953
00954
00955
00956
00957
00958
00959
00960 void InitSubseqLayerInfo()
00961 {
00962 int i;
00963 seiHasSubseqLayerInfo = TRUE;
00964 seiSubseqLayerInfo.layer_number = 0;
00965 for (i=0; i<MAX_LAYER_NUMBER; i++)
00966 {
00967 seiSubseqLayerInfo.bit_rate[i] = 0;
00968 seiSubseqLayerInfo.frame_rate[i] = 0;
00969 seiSubseqLayerInfo.layer_number++;
00970 }
00971 }
00972
00973
00974
00975
00976
00977
00978
00979 void CloseSubseqLayerInfo()
00980 {
00981 }
00982
00983
00984
00985
00986
00987
00988
00989 void FinalizeSubseqLayerInfo()
00990 {
00991 int i, pos;
00992 pos = 0;
00993 seiSubseqLayerInfo.payloadSize = 0;
00994 for (i=0; i<seiSubseqLayerInfo.layer_number; i++)
00995 {
00996 *((unsigned short*)&(seiSubseqLayerInfo.data[pos])) = seiSubseqLayerInfo.bit_rate[i];
00997 pos += 2;
00998 *((unsigned short*)&(seiSubseqLayerInfo.data[pos])) = seiSubseqLayerInfo.frame_rate[i];
00999 pos += 2;
01000 seiSubseqLayerInfo.payloadSize += 4;
01001 }
01002 }
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014 Boolean seiHasSubseqChar = FALSE;
01015 subseq_char_information_struct seiSubseqChar;
01016
01017 void InitSubseqChar()
01018 {
01019 seiSubseqChar.data = malloc( sizeof(Bitstream) );
01020 if( seiSubseqChar.data == NULL ) no_mem_exit("InitSubseqChar: seiSubseqChar.data");
01021 seiSubseqChar.data->streamBuffer = malloc(MAXRTPPAYLOADLEN);
01022 if( seiSubseqChar.data->streamBuffer == NULL ) no_mem_exit("InitSubseqChar: seiSubseqChar.data->streamBuffer");
01023 ClearSubseqCharPayload();
01024
01025 seiSubseqChar.subseq_layer_num = img->layer;
01026 seiSubseqChar.subseq_id = seiSubseqInfo[img->layer].subseq_id;
01027 seiSubseqChar.duration_flag = 0;
01028 seiSubseqChar.average_rate_flag = 0;
01029 seiSubseqChar.num_referenced_subseqs = 0;
01030 }
01031
01032 void ClearSubseqCharPayload()
01033 {
01034 memset( seiSubseqChar.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
01035 seiSubseqChar.data->bits_to_go = 8;
01036 seiSubseqChar.data->byte_pos = 0;
01037 seiSubseqChar.data->byte_buf = 0;
01038 seiSubseqChar.payloadSize = 0;
01039
01040 seiHasSubseqChar = FALSE;
01041 }
01042
01043 void UpdateSubseqChar()
01044 {
01045 seiSubseqChar.subseq_layer_num = img->layer;
01046 seiSubseqChar.subseq_id = seiSubseqInfo[img->layer].subseq_id;
01047 seiSubseqChar.duration_flag = 0;
01048 seiSubseqChar.average_rate_flag = 0;
01049 seiSubseqChar.average_bit_rate = 100;
01050 seiSubseqChar.average_frame_rate = 30;
01051 seiSubseqChar.num_referenced_subseqs = 0;
01052 seiSubseqChar.ref_subseq_layer_num[0] = 1;
01053 seiSubseqChar.ref_subseq_id[0] = 2;
01054 seiSubseqChar.ref_subseq_layer_num[1] = 3;
01055 seiSubseqChar.ref_subseq_id[1] = 4;
01056
01057 seiHasSubseqChar = TRUE;
01058 }
01059
01060 void FinalizeSubseqChar()
01061 {
01062 int i;
01063 SyntaxElement sym;
01064 Bitstream *dest = seiSubseqChar.data;
01065
01066 sym.type = SE_HEADER;
01067 sym.mapping = ue_linfo;
01068
01069 sym.value1 = seiSubseqChar.subseq_layer_num;
01070 writeSyntaxElement2Buf_UVLC(&sym, dest);
01071 sym.value1 = seiSubseqChar.subseq_id;
01072 writeSyntaxElement2Buf_UVLC(&sym, dest);
01073 sym.bitpattern = seiSubseqChar.duration_flag;
01074 sym.len = 1;
01075 writeSyntaxElement2Buf_Fixed(&sym, dest);
01076 if ( seiSubseqChar.duration_flag )
01077 {
01078 sym.bitpattern = seiSubseqChar.subseq_duration;
01079 sym.len = 32;
01080 writeSyntaxElement2Buf_Fixed(&sym, dest);
01081 }
01082 sym.bitpattern = seiSubseqChar.average_rate_flag;
01083 sym.len = 1;
01084 writeSyntaxElement2Buf_Fixed(&sym, dest);
01085 if ( seiSubseqChar.average_rate_flag )
01086 {
01087 sym.bitpattern = seiSubseqChar.average_bit_rate;
01088 sym.len = 16;
01089 writeSyntaxElement2Buf_Fixed(&sym, dest);
01090 sym.bitpattern = seiSubseqChar.average_frame_rate;
01091 sym.len = 16;
01092 writeSyntaxElement2Buf_Fixed(&sym, dest);
01093 }
01094 sym.value1 = seiSubseqChar.num_referenced_subseqs;
01095 writeSyntaxElement2Buf_UVLC(&sym, dest);
01096 for (i=0; i<seiSubseqChar.num_referenced_subseqs; i++)
01097 {
01098 sym.value1 = seiSubseqChar.ref_subseq_layer_num[i];
01099 writeSyntaxElement2Buf_UVLC(&sym, dest);
01100 sym.value1 = seiSubseqChar.ref_subseq_id[i];
01101 writeSyntaxElement2Buf_UVLC(&sym, dest);
01102 }
01103
01104
01105 if ( dest->bits_to_go != 8 )
01106 {
01107 (dest->byte_buf) <<= 1;
01108 dest->byte_buf |= 1;
01109 dest->bits_to_go--;
01110 if ( dest->bits_to_go != 0 ) (dest->byte_buf) <<= (dest->bits_to_go);
01111 dest->bits_to_go = 8;
01112 dest->streamBuffer[dest->byte_pos++]=dest->byte_buf;
01113 dest->byte_buf = 0;
01114 }
01115 seiSubseqChar.payloadSize = dest->byte_pos;
01116 }
01117
01118 void CloseSubseqChar()
01119 {
01120 if (seiSubseqChar.data)
01121 {
01122 free(seiSubseqChar.data->streamBuffer);
01123 free(seiSubseqChar.data);
01124 }
01125 seiSubseqChar.data = NULL;
01126 }
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140 scene_information_struct seiSceneInformation;
01141
01142 void InitSceneInformation()
01143 {
01144 seiHasSceneInformation = TRUE;
01145
01146 seiSceneInformation.scene_id = 0;
01147 seiSceneInformation.scene_transition_type = 0;
01148 seiSceneInformation.second_scene_id = -1;
01149
01150 seiSceneInformation.data = malloc( sizeof(Bitstream) );
01151 if( seiSceneInformation.data == NULL ) no_mem_exit("InitSceneInformation: seiSceneInformation.data");
01152 seiSceneInformation.data->streamBuffer = malloc( MAXRTPPAYLOADLEN );
01153 if( seiSceneInformation.data->streamBuffer == NULL ) no_mem_exit("InitSceneInformation: seiSceneInformation.data->streamBuffer");
01154 seiSceneInformation.data->bits_to_go = 8;
01155 seiSceneInformation.data->byte_pos = 0;
01156 seiSceneInformation.data->byte_buf = 0;
01157 memset( seiSceneInformation.data->streamBuffer, 0, MAXRTPPAYLOADLEN );
01158 }
01159
01160 void CloseSceneInformation()
01161 {
01162 if (seiSceneInformation.data)
01163 {
01164 free(seiSceneInformation.data->streamBuffer);
01165 free(seiSceneInformation.data);
01166 }
01167 seiSceneInformation.data = NULL;
01168 }
01169
01170 void FinalizeSceneInformation()
01171 {
01172 SyntaxElement sym;
01173 Bitstream *dest = seiSceneInformation.data;
01174
01175 sym.type = SE_HEADER;
01176 sym.mapping = ue_linfo;
01177
01178 sym.bitpattern = seiSceneInformation.scene_id;
01179 sym.len = 8;
01180 writeSyntaxElement2Buf_Fixed(&sym, dest);
01181
01182 sym.value1 = seiSceneInformation.scene_transition_type;
01183 writeSyntaxElement2Buf_UVLC(&sym, dest);
01184
01185 if(seiSceneInformation.scene_transition_type > 3)
01186 {
01187 sym.bitpattern = seiSceneInformation.second_scene_id;
01188 sym.len = 8;
01189 writeSyntaxElement2Buf_Fixed(&sym, dest);
01190 }
01191
01192
01193 if ( dest->bits_to_go != 8 )
01194 {
01195 (dest->byte_buf) <<= 1;
01196 dest->byte_buf |= 1;
01197 dest->bits_to_go--;
01198 if ( dest->bits_to_go != 0 ) (dest->byte_buf) <<= (dest->bits_to_go);
01199 dest->bits_to_go = 8;
01200 dest->streamBuffer[dest->byte_pos++]=dest->byte_buf;
01201 dest->byte_buf = 0;
01202 }
01203 seiSceneInformation.payloadSize = dest->byte_pos;
01204 }
01205
01206
01207
01208
01209 void UpdateSceneInformation(Boolean HasSceneInformation, int sceneID, int sceneTransType, int secondSceneID)
01210 {
01211 seiHasSceneInformation = HasSceneInformation;
01212
01213 assert (sceneID < 256);
01214 seiSceneInformation.scene_id = sceneID;
01215
01216 assert (sceneTransType <= 6 );
01217 seiSceneInformation.scene_transition_type = sceneTransType;
01218
01219 if(sceneTransType > 3)
01220 {
01221 assert (secondSceneID < 256);
01222 seiSceneInformation.second_scene_id = secondSceneID;
01223 }
01224 }
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238 Boolean seiHasPanScanRectInfo = FALSE;
01239 panscanrect_information_struct seiPanScanRectInfo;
01240
01241 void InitPanScanRectInfo()
01242 {
01243
01244 seiPanScanRectInfo.data = malloc( sizeof(Bitstream) );
01245 if( seiPanScanRectInfo.data == NULL ) no_mem_exit("InitPanScanRectInfo: seiPanScanRectInfo.data");
01246 seiPanScanRectInfo.data->streamBuffer = malloc(MAXRTPPAYLOADLEN);
01247 if( seiPanScanRectInfo.data->streamBuffer == NULL ) no_mem_exit("InitPanScanRectInfo: seiPanScanRectInfo.data->streamBuffer");
01248 ClearPanScanRectInfoPayload();
01249
01250 seiPanScanRectInfo.pan_scan_rect_left_offset = 0;
01251 seiPanScanRectInfo.pan_scan_rect_right_offset = 0;
01252 seiPanScanRectInfo.pan_scan_rect_top_offset = 0;
01253 seiPanScanRectInfo.pan_scan_rect_bottom_offset = 0;
01254
01255 }
01256
01257
01258 void ClearPanScanRectInfoPayload()
01259 {
01260 memset( seiPanScanRectInfo.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
01261 seiPanScanRectInfo.data->bits_to_go = 8;
01262 seiPanScanRectInfo.data->byte_pos = 0;
01263 seiPanScanRectInfo.data->byte_buf = 0;
01264 seiPanScanRectInfo.payloadSize = 0;
01265
01266 seiHasPanScanRectInfo = FALSE;
01267 }
01268
01269 void UpdatePanScanRectInfo()
01270 {
01271 seiPanScanRectInfo.pan_scan_rect_id = 3;
01272 seiPanScanRectInfo.pan_scan_rect_left_offset = 10;
01273 seiPanScanRectInfo.pan_scan_rect_right_offset = 40;
01274 seiPanScanRectInfo.pan_scan_rect_top_offset = 20;
01275 seiPanScanRectInfo.pan_scan_rect_bottom_offset =32;
01276 seiHasPanScanRectInfo = TRUE;
01277 }
01278
01279 void FinalizePanScanRectInfo()
01280 {
01281 SyntaxElement sym;
01282 Bitstream *dest = seiPanScanRectInfo.data;
01283
01284
01285 sym.type = SE_HEADER;
01286 sym.mapping = ue_linfo;
01287
01288 sym.value1 = seiPanScanRectInfo.pan_scan_rect_id;
01289 writeSyntaxElement2Buf_UVLC(&sym, dest);
01290 sym.value1 = seiPanScanRectInfo.pan_scan_rect_left_offset;
01291 writeSyntaxElement2Buf_UVLC(&sym, dest);
01292 sym.value1 = seiPanScanRectInfo.pan_scan_rect_right_offset;
01293 writeSyntaxElement2Buf_UVLC(&sym, dest);
01294 sym.value1 = seiPanScanRectInfo.pan_scan_rect_top_offset;
01295 writeSyntaxElement2Buf_UVLC(&sym, dest);
01296 sym.value1 = seiPanScanRectInfo.pan_scan_rect_bottom_offset;
01297 writeSyntaxElement2Buf_UVLC(&sym, dest);
01298
01299
01300 #ifdef PRINT_PAN_SCAN_RECT
01301 printf("Pan Scan Id %d Left %d Right %d Top %d Bottom %d \n", seiPanScanRectInfo.pan_scan_rect_id, seiPanScanRectInfo.pan_scan_rect_left_offset, seiPanScanRectInfo.pan_scan_rect_right_offset, seiPanScanRectInfo.pan_scan_rect_top_offset, seiPanScanRectInfo.pan_scan_rect_bottom_offset);
01302 #endif
01303 #ifdef PRINT_PAN_SCAN_RECT
01304 #undef PRINT_PAN_SCAN_RECT
01305 #endif
01306
01307 if ( dest->bits_to_go != 8 )
01308 {
01309 (dest->byte_buf) <<= 1;
01310 dest->byte_buf |= 1;
01311 dest->bits_to_go--;
01312 if ( dest->bits_to_go != 0 ) (dest->byte_buf) <<= (dest->bits_to_go);
01313 dest->bits_to_go = 8;
01314 dest->streamBuffer[dest->byte_pos++]=dest->byte_buf;
01315 dest->byte_buf = 0;
01316 }
01317 seiPanScanRectInfo.payloadSize = dest->byte_pos;
01318 }
01319
01320
01321
01322 void ClosePanScanRectInfo()
01323 {
01324 if (seiPanScanRectInfo.data)
01325 {
01326 free(seiPanScanRectInfo.data->streamBuffer);
01327 free(seiPanScanRectInfo.data);
01328 }
01329 seiPanScanRectInfo.data = NULL;
01330 }
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341 Boolean seiHasUser_data_unregistered_info;
01342 user_data_unregistered_information_struct seiUser_data_unregistered;
01343 void InitUser_data_unregistered()
01344 {
01345
01346 seiUser_data_unregistered.data = malloc( sizeof(Bitstream) );
01347 if( seiUser_data_unregistered.data == NULL ) no_mem_exit("InitUser_data_unregistered: seiUser_data_unregistered.data");
01348 seiUser_data_unregistered.data->streamBuffer = malloc(MAXRTPPAYLOADLEN);
01349 if( seiUser_data_unregistered.data->streamBuffer == NULL ) no_mem_exit("InitUser_data_unregistered: seiUser_data_unregistered.data->streamBuffer");
01350 seiUser_data_unregistered.byte = malloc(MAXRTPPAYLOADLEN);
01351 if( seiUser_data_unregistered.byte == NULL ) no_mem_exit("InitUser_data_unregistered: seiUser_data_unregistered.byte");
01352 ClearUser_data_unregistered();
01353
01354 }
01355
01356
01357 void ClearUser_data_unregistered()
01358 {
01359 memset( seiUser_data_unregistered.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
01360 seiUser_data_unregistered.data->bits_to_go = 8;
01361 seiUser_data_unregistered.data->byte_pos = 0;
01362 seiUser_data_unregistered.data->byte_buf = 0;
01363 seiUser_data_unregistered.payloadSize = 0;
01364
01365 memset( seiUser_data_unregistered.byte, 0, MAXRTPPAYLOADLEN);
01366 seiUser_data_unregistered.total_byte = 0;
01367
01368 seiHasUser_data_unregistered_info = FALSE;
01369 }
01370
01371 void UpdateUser_data_unregistered()
01372 {
01373 int i, temp_data;
01374 int total_byte;
01375
01376
01377 total_byte = 7;
01378 for(i = 0; i < total_byte; i++)
01379 {
01380 temp_data = i * 4;
01381 seiUser_data_unregistered.byte[i] = (char) iClip3(0, 255, temp_data);
01382 }
01383 seiUser_data_unregistered.total_byte = total_byte;
01384 }
01385
01386 void FinalizeUser_data_unregistered()
01387 {
01388 int i;
01389 SyntaxElement sym;
01390 Bitstream *dest = seiUser_data_unregistered.data;
01391
01392 sym.type = SE_HEADER;
01393 sym.mapping = ue_linfo;
01394
01395
01396 for( i = 0; i < seiUser_data_unregistered.total_byte; i++)
01397 {
01398 sym.bitpattern = seiUser_data_unregistered.byte[i];
01399 sym.len = 8;
01400 writeSyntaxElement2Buf_Fixed(&sym, dest);
01401 #ifdef PRINT_USER_DATA_UNREGISTERED_INFO
01402 printf("Unreg data payload_byte = %d\n", seiUser_data_unregistered.byte[i]);
01403 #endif
01404 }
01405 #ifdef PRINT_USER_DATA_UNREGISTERED_INFO
01406 #undef PRINT_USER_DATA_UNREGISTERED_INFO
01407 #endif
01408
01409 if ( dest->bits_to_go != 8 )
01410 {
01411 (dest->byte_buf) <<= 1;
01412 dest->byte_buf |= 1;
01413 dest->bits_to_go--;
01414 if ( dest->bits_to_go != 0 ) (dest->byte_buf) <<= (dest->bits_to_go);
01415 dest->bits_to_go = 8;
01416 dest->streamBuffer[dest->byte_pos++]=dest->byte_buf;
01417 dest->byte_buf = 0;
01418 }
01419 seiUser_data_unregistered.payloadSize = dest->byte_pos;
01420 }
01421
01422 void CloseUser_data_unregistered()
01423 {
01424 if (seiUser_data_unregistered.data)
01425 {
01426 free(seiUser_data_unregistered.data->streamBuffer);
01427 free(seiUser_data_unregistered.data);
01428 }
01429 seiUser_data_unregistered.data = NULL;
01430 if(seiUser_data_unregistered.byte)
01431 {
01432 free(seiUser_data_unregistered.byte);
01433 }
01434 }
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446 Boolean seiHasUser_data_registered_itu_t_t35_info;
01447 user_data_registered_itu_t_t35_information_struct seiUser_data_registered_itu_t_t35;
01448 void InitUser_data_registered_itu_t_t35()
01449 {
01450
01451 seiUser_data_registered_itu_t_t35.data = malloc( sizeof(Bitstream) );
01452 if( seiUser_data_registered_itu_t_t35.data == NULL ) no_mem_exit("InitUser_data_unregistered: seiUser_data_registered_itu_t_t35.data");
01453 seiUser_data_registered_itu_t_t35.data->streamBuffer = malloc(MAXRTPPAYLOADLEN);
01454 if( seiUser_data_registered_itu_t_t35.data->streamBuffer == NULL ) no_mem_exit("InitUser_data_unregistered: seiUser_data_registered_itu_t_t35.data->streamBuffer");
01455 seiUser_data_registered_itu_t_t35.byte = malloc(MAXRTPPAYLOADLEN);
01456 if( seiUser_data_registered_itu_t_t35.data == NULL ) no_mem_exit("InitUser_data_unregistered: seiUser_data_registered_itu_t_t35.byte");
01457 ClearUser_data_registered_itu_t_t35();
01458
01459 }
01460
01461
01462 void ClearUser_data_registered_itu_t_t35()
01463 {
01464 memset( seiUser_data_registered_itu_t_t35.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
01465 seiUser_data_registered_itu_t_t35.data->bits_to_go = 8;
01466 seiUser_data_registered_itu_t_t35.data->byte_pos = 0;
01467 seiUser_data_registered_itu_t_t35.data->byte_buf = 0;
01468 seiUser_data_registered_itu_t_t35.payloadSize = 0;
01469
01470 memset( seiUser_data_registered_itu_t_t35.byte, 0, MAXRTPPAYLOADLEN);
01471 seiUser_data_registered_itu_t_t35.total_byte = 0;
01472 seiUser_data_registered_itu_t_t35.itu_t_t35_country_code = 0;
01473 seiUser_data_registered_itu_t_t35.itu_t_t35_country_code_extension_byte = 0;
01474
01475 seiHasUser_data_registered_itu_t_t35_info = FALSE;
01476 }
01477
01478 void UpdateUser_data_registered_itu_t_t35()
01479 {
01480 int i, temp_data;
01481 int total_byte;
01482 int country_code;
01483
01484 country_code = 82;
01485
01486 if(country_code < 0xFF)
01487 {
01488 seiUser_data_registered_itu_t_t35.itu_t_t35_country_code = country_code;
01489 }
01490 else
01491 {
01492 seiUser_data_registered_itu_t_t35.itu_t_t35_country_code = 0xFF;
01493 seiUser_data_registered_itu_t_t35.itu_t_t35_country_code_extension_byte = country_code - 0xFF;
01494 }
01495
01496 total_byte = 7;
01497 for(i = 0; i < total_byte; i++)
01498 {
01499 temp_data = i * 3;
01500 seiUser_data_registered_itu_t_t35.byte[i] = (char) iClip3(0, 255, temp_data);
01501 }
01502 seiUser_data_registered_itu_t_t35.total_byte = total_byte;
01503 }
01504
01505 void FinalizeUser_data_registered_itu_t_t35()
01506 {
01507 int i;
01508 SyntaxElement sym;
01509 Bitstream *dest = seiUser_data_registered_itu_t_t35.data;
01510
01511 sym.type = SE_HEADER;
01512 sym.mapping = ue_linfo;
01513
01514 sym.bitpattern = seiUser_data_registered_itu_t_t35.itu_t_t35_country_code;
01515 sym.len = 8;
01516 writeSyntaxElement2Buf_Fixed(&sym, dest);
01517
01518
01519 #ifdef PRINT_USER_DATA_REGISTERED_ITU_T_T35_INFO
01520 printf(" ITU_T_T35_COUNTRTY_CODE %d \n", seiUser_data_registered_itu_t_t35.itu_t_t35_country_code);
01521 #endif
01522
01523 if(seiUser_data_registered_itu_t_t35.itu_t_t35_country_code == 0xFF)
01524 {
01525 sym.bitpattern = seiUser_data_registered_itu_t_t35.itu_t_t35_country_code_extension_byte;
01526 sym.len = 8;
01527 writeSyntaxElement2Buf_Fixed(&sym, dest);
01528 #ifdef PRINT_USER_DATA_REGISTERED_ITU_T_T35_INFO
01529 printf(" ITU_T_T35_COUNTRTY_CODE_EXTENSION_BYTE %d \n", seiUser_data_registered_itu_t_t35.itu_t_t35_country_code_extension_byte);
01530 #endif
01531 }
01532
01533 for( i = 0; i < seiUser_data_registered_itu_t_t35.total_byte; i++)
01534 {
01535 sym.bitpattern = seiUser_data_registered_itu_t_t35.byte[i];
01536 sym.len = 8;
01537 writeSyntaxElement2Buf_Fixed(&sym, dest);
01538 #ifdef PRINT_USER_DATA_REGISTERED_ITU_T_T35_INFO
01539 printf("itu_t_t35 payload_byte = %d\n", seiUser_data_registered_itu_t_t35.byte[i]);
01540 #endif
01541 }
01542 #ifdef PRINT_USER_DATA_REGISTERED_ITU_T_T35_INFO
01543 #undef PRINT_USER_DATA_REGISTERED_ITU_T_T35_INFO
01544 #endif
01545
01546 if ( dest->bits_to_go != 8 )
01547 {
01548 (dest->byte_buf) <<= 1;
01549 dest->byte_buf |= 1;
01550 dest->bits_to_go--;
01551 if ( dest->bits_to_go != 0 ) (dest->byte_buf) <<= (dest->bits_to_go);
01552 dest->bits_to_go = 8;
01553 dest->streamBuffer[dest->byte_pos++]=dest->byte_buf;
01554 dest->byte_buf = 0;
01555 }
01556 seiUser_data_registered_itu_t_t35.payloadSize = dest->byte_pos;
01557 }
01558
01559 void CloseUser_data_registered_itu_t_t35()
01560 {
01561 if (seiUser_data_registered_itu_t_t35.data)
01562 {
01563 free(seiUser_data_registered_itu_t_t35.data->streamBuffer);
01564 free(seiUser_data_registered_itu_t_t35.data);
01565 }
01566 seiUser_data_registered_itu_t_t35.data = NULL;
01567 if(seiUser_data_registered_itu_t_t35.byte)
01568 {
01569 free(seiUser_data_registered_itu_t_t35.byte);
01570 }
01571 }
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582 recovery_point_information_struct seiRecoveryPoint;
01583 void InitRandomAccess()
01584 {
01585
01586 seiRecoveryPoint.data = malloc( sizeof(Bitstream) );
01587 if( seiRecoveryPoint.data == NULL ) no_mem_exit("InitRandomAccess: seiRandomAccess.data");
01588 seiRecoveryPoint.data->streamBuffer = malloc(MAXRTPPAYLOADLEN);
01589 if( seiRecoveryPoint.data->streamBuffer == NULL ) no_mem_exit("InitRandomAccess: seiRandomAccess.data->streamBuffer");
01590 ClearRandomAccess();
01591
01592 }
01593
01594
01595 void ClearRandomAccess()
01596 {
01597 memset( seiRecoveryPoint.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
01598 seiRecoveryPoint.data->bits_to_go = 8;
01599 seiRecoveryPoint.data->byte_pos = 0;
01600 seiRecoveryPoint.data->byte_buf = 0;
01601 seiRecoveryPoint.payloadSize = 0;
01602
01603 seiRecoveryPoint.recovery_frame_cnt = 0;
01604 seiRecoveryPoint.broken_link_flag = 0;
01605 seiRecoveryPoint.exact_match_flag = 0;
01606 seiRecoveryPoint.changing_slice_group_idc = 0;
01607
01608 seiHasRecoveryPoint_info = FALSE;
01609 }
01610
01611 void UpdateRandomAccess()
01612 {
01613 if(img->type == I_SLICE)
01614 {
01615 seiRecoveryPoint.recovery_frame_cnt = 0;
01616 seiRecoveryPoint.exact_match_flag = 1;
01617 seiRecoveryPoint.broken_link_flag = 0;
01618 seiRecoveryPoint.changing_slice_group_idc = 0;
01619 seiHasRecoveryPoint_info = TRUE;
01620 }
01621 else
01622 {
01623 seiHasRecoveryPoint_info = FALSE;
01624 }
01625 }
01626
01627 void FinalizeRandomAccess()
01628 {
01629 Bitstream *bitstream = seiRecoveryPoint.data;
01630
01631 ue_v( "SEI: recovery_frame_cnt", seiRecoveryPoint.recovery_frame_cnt, bitstream);
01632 u_1 ( "SEI: exact_match_flag", seiRecoveryPoint.exact_match_flag, bitstream);
01633 u_1 ( "SEI: broken_link_flag", seiRecoveryPoint.broken_link_flag, bitstream);
01634 u_v (2, "SEI: changing_slice_group_idc", seiRecoveryPoint.changing_slice_group_idc, bitstream);
01635
01636
01637
01638 #ifdef PRINT_RECOVERY_POINT
01639 printf(" recovery_frame_cnt %d \n", seiRecoveryPoint.recovery_frame_cnt);
01640 printf(" exact_match_flag %d \n", seiRecoveryPoint.exact_match_flag);
01641 printf(" broken_link_flag %d \n", seiRecoveryPoint.broken_link_flag);
01642 printf(" changing_slice_group_idc %d \n", seiRecoveryPoint.changing_slice_group_idc);
01643 printf(" %d %d \n", bitstream->byte_pos, bitstream->bits_to_go);
01644
01645 #undef PRINT_RECOVERY_POINT
01646 #endif
01647
01648 if ( bitstream->bits_to_go != 8 )
01649 {
01650 (bitstream->byte_buf) <<= 1;
01651 bitstream->byte_buf |= 1;
01652 bitstream->bits_to_go--;
01653 if ( bitstream->bits_to_go != 0 )
01654 (bitstream->byte_buf) <<= (bitstream->bits_to_go);
01655 bitstream->bits_to_go = 8;
01656 bitstream->streamBuffer[bitstream->byte_pos++]=bitstream->byte_buf;
01657 bitstream->byte_buf = 0;
01658 }
01659 seiRecoveryPoint.payloadSize = bitstream->byte_pos;
01660 }
01661
01662 void CloseRandomAccess()
01663 {
01664 if (seiRecoveryPoint.data)
01665 {
01666 free(seiRecoveryPoint.data->streamBuffer);
01667 free(seiRecoveryPoint.data);
01668 }
01669 seiRecoveryPoint.data = NULL;
01670 }
01671
01672
01673
01674
01675
01676
01677
01678
01679
01680
01681 tone_mapping_struct seiToneMapping;
01682
01683 int ParseToneMappingConfigFile(tone_mapping_struct* pSeiToneMapping)
01684 {
01685 int i;
01686 int ret;
01687 FILE* fp;
01688 char buf[1024];
01689 unsigned int tmp;
01690
01691 printf ("Parsing Tone mapping cfg file %s ..........\n\n", params->ToneMappingFile);
01692 if ((fp = fopen(params->ToneMappingFile, "r")) == NULL)
01693 {
01694 fprintf(stderr, "Tone mapping config file %s is not found, disable tone mapping SEI\n", params->ToneMappingFile);
01695 seiHasTone_mapping=FALSE;
01696
01697 return 1;
01698 }
01699
01700
01701 while (fscanf(fp, "%s", buf)!=EOF)
01702 {
01703 ret = 1;
01704 if (strcmp(buf, "tone_map_id")==0)
01705 {
01706 ret = fscanf(fp, " = %ud\n", &(pSeiToneMapping->tone_map_id));
01707 }
01708 else if (strcmp(buf, "tone_map_cancel_flag")==0)
01709 {
01710 ret = fscanf(fp, " = %ud\n", &tmp);
01711 pSeiToneMapping->tone_map_cancel_flag = tmp ? 1 : 0;
01712 }
01713 else if (strcmp(buf, "tone_map_repetition_period")==0)
01714 {
01715 ret = fscanf(fp, " = %ud\n", &(pSeiToneMapping->tone_map_repetition_period));
01716 }
01717 else if (strcmp(buf, "coded_data_bit_depth")==0)
01718 {
01719 ret = fscanf(fp, " = %ud\n", &tmp);
01720 pSeiToneMapping->coded_data_bit_depth = (unsigned char) tmp;
01721 }
01722 else if (strcmp(buf, "sei_bit_depth")==0)
01723 {
01724 ret = fscanf(fp, " = %ud\n", &tmp);
01725 pSeiToneMapping->sei_bit_depth = (unsigned char) tmp;
01726 }
01727 else if (strcmp(buf, "model_id")==0)
01728 {
01729 ret = fscanf(fp, " = %ud\n", &(pSeiToneMapping->model_id));
01730 }
01731
01732 else if (strcmp(buf, "min_value")==0)
01733 {
01734 ret = fscanf(fp, " = %d\n", &(pSeiToneMapping->min_value));
01735 }
01736 else if (strcmp(buf, "max_value")==0)
01737 {
01738 ret = fscanf(fp, " = %d\n", &(pSeiToneMapping->max_value));
01739 }
01740
01741 else if (strcmp(buf, "sigmoid_midpoint")==0)
01742 {
01743 ret = fscanf(fp, " = %d\n", &(pSeiToneMapping->sigmoid_midpoint));
01744 }
01745 else if (strcmp(buf, "sigmoid_width")==0)
01746 {
01747 ret = fscanf(fp, " = %d\n", &(pSeiToneMapping->sigmoid_width));
01748 }
01749
01750 else if (strcmp(buf, "start_of_coded_interval")==0)
01751 {
01752 int max_output_num = 1<<(pSeiToneMapping->sei_bit_depth);
01753 ret = fscanf(fp, " = ");
01754 if (ret!=0)
01755 {
01756 error ("ParseToneMappingConfigFile: error parsing tone mapping config file",500);
01757 }
01758 for (i=0; i < max_output_num; i++)
01759 {
01760 ret = fscanf(fp, "%d\n", &(pSeiToneMapping->start_of_coded_interval[i]));
01761 if (ret!=1)
01762 {
01763 error ("ParseToneMappingConfigFile: error parsing tone mapping config file",500);
01764 }
01765 }
01766 }
01767
01768 else if (strcmp(buf, "num_pivots")==0)
01769 {
01770 ret = fscanf(fp, " = %d\n", &(pSeiToneMapping->num_pivots));
01771 }
01772
01773 else if (strcmp(buf, "coded_pivot_value")==0)
01774 {
01775 ret = fscanf(fp, " = ");
01776 if (ret!=0)
01777 {
01778 error ("ParseToneMappingConfigFile: error parsing tone mapping config file",500);
01779 }
01780 for (i=0; i < pSeiToneMapping->num_pivots; i++)
01781 {
01782 ret = fscanf(fp, "%d\n", &(pSeiToneMapping->coded_pivot_value[i]));
01783 if (ret!=1)
01784 {
01785 error ("ParseToneMappingConfigFile: error parsing tone mapping config file",500);
01786 }
01787 }
01788 }
01789 else if (strcmp(buf, "sei_pivot_value")==0)
01790 {
01791 ret = fscanf(fp, " = ");
01792 if (ret!=0)
01793 {
01794 error ("ParseToneMappingConfigFile: error parsing tone mapping config file",500);
01795 }
01796 for (i=0; i < pSeiToneMapping->num_pivots; i++)
01797 {
01798 ret = fscanf(fp, "%d\n", &(pSeiToneMapping->sei_pivot_value[i]));
01799 if (ret!=1)
01800 {
01801 error ("ParseToneMappingConfigFile: error parsing tone mapping config file",500);
01802 }
01803 }
01804 }
01805 else
01806 {
01807
01808 if (NULL == fgets(buf, sizeof(buf), fp))
01809 {
01810 error ("ParseToneMappingConfigFile: error parsing tone mapping config file",500);
01811 }
01812 }
01813 if (ret!=1)
01814 {
01815 error ("ParseToneMappingConfigFile: error parsing tone mapping config file",500);
01816 }
01817 }
01818
01819 fclose(fp);
01820
01821 return 0;
01822 }
01823
01824 void InitToneMapping()
01825 {
01826 if (params->ToneMappingSEIPresentFlag == 0)
01827 {
01828 seiHasTone_mapping = FALSE;
01829 return;
01830 }
01831 else
01832 seiHasTone_mapping = TRUE;
01833
01834 seiToneMapping.data = malloc( sizeof(Bitstream) );
01835 if( seiToneMapping.data == NULL ) no_mem_exit("InitToneMapping: seiToneMapping.data");
01836 seiToneMapping.data->streamBuffer = malloc(MAXRTPPAYLOADLEN);
01837 if( seiToneMapping.data->streamBuffer == NULL ) no_mem_exit("InitToneMapping: seiToneMapping.data->streamBuffer");
01838 memset( seiToneMapping.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
01839 seiToneMapping.data->bits_to_go = 8;
01840 seiToneMapping.data->byte_pos = 0;
01841 seiToneMapping.data->byte_buf = 0;
01842 seiToneMapping.payloadSize = 0;
01843
01844
01845 ParseToneMappingConfigFile(&seiToneMapping);
01846 }
01847
01848 void FinalizeToneMapping()
01849 {
01850 Bitstream *bitstream = seiToneMapping.data;
01851 int i;
01852
01853 ue_v("SEI: tone_map_id" , seiToneMapping.tone_map_id, bitstream);
01854 u_1("SEI: tone_map_cancel_flag" , seiToneMapping.tone_map_cancel_flag, bitstream);
01855
01856 #ifdef PRINT_TONE_MAPPING
01857 printf("frame %d: Tone-mapping SEI message\n", img->frame_num);
01858 printf("tone_map_id = %d\n", seiToneMapping.tone_map_id);
01859 printf("tone_map_cancel_flag = %d\n", seiToneMapping.tone_map_cancel_flag);
01860 #endif
01861 if (!seiToneMapping.tone_map_cancel_flag)
01862 {
01863 ue_v( "SEI: tone_map_repetition_period", seiToneMapping.tone_map_repetition_period, bitstream);
01864 u_v (8,"SEI: coded_data_bit_depth" , seiToneMapping.coded_data_bit_depth, bitstream);
01865 u_v (8,"SEI: sei_bit_depth" , seiToneMapping.sei_bit_depth, bitstream);
01866 ue_v( "SEI: model_id" , seiToneMapping.model_id, bitstream);
01867
01868 #ifdef PRINT_TONE_MAPPING
01869 printf("tone_map_repetition_period = %d\n", seiToneMapping.tone_map_repetition_period);
01870 printf("coded_data_bit_depth = %d\n", seiToneMapping.coded_data_bit_depth);
01871 printf("sei_bit_depth = %d\n", seiToneMapping.sei_bit_depth);
01872 printf("model_id = %d\n", seiToneMapping.model_id);
01873 #endif
01874 if (seiToneMapping.model_id == 0)
01875 {
01876 u_v (32,"SEI: min_value", seiToneMapping.min_value, bitstream);
01877 u_v (32,"SEI: min_value", seiToneMapping.max_value, bitstream);
01878 #ifdef PRINT_TONE_MAPPING
01879 printf("min_value = %d, max_value = %d\n", seiToneMapping.min_value, seiToneMapping.max_value);
01880 #endif
01881 }
01882 else if (seiToneMapping.model_id == 1)
01883 {
01884 u_v (32,"SEI: sigmoid_midpoint", seiToneMapping.sigmoid_midpoint, bitstream);
01885 u_v (32,"SEI: sigmoid_width", seiToneMapping.sigmoid_width, bitstream);
01886 #ifdef PRINT_TONE_MAPPING
01887 printf("sigmoid_midpoint = %d, sigmoid_width = %d\n", seiToneMapping.sigmoid_midpoint, seiToneMapping.sigmoid_width);
01888 #endif
01889 }
01890 else if (seiToneMapping.model_id == 2)
01891 {
01892 int bit_depth_val = 1<<seiToneMapping.sei_bit_depth;
01893 for (i=0; i<bit_depth_val; i++)
01894 {
01895 u_v((((seiToneMapping.coded_data_bit_depth+7)>>3)<<3), "SEI: start_of_coded_interval", seiToneMapping.start_of_coded_interval[i], bitstream);
01896 #ifdef PRINT_TONE_MAPPING
01897
01898 #endif
01899 }
01900 }
01901 else if (seiToneMapping.model_id == 3)
01902 {
01903 u_v (16,"SEI: num_pivots", seiToneMapping.num_pivots, bitstream);
01904 #ifdef PRINT_TONE_MAPPING
01905 printf("num_pivots = %d\n", seiToneMapping.num_pivots);
01906 #endif
01907 for (i=0; i < seiToneMapping.num_pivots; i++)
01908 {
01909 u_v( (((seiToneMapping.coded_data_bit_depth+7)>>3)<<3), "SEI: coded_pivot_value", seiToneMapping.coded_pivot_value[i], bitstream);
01910 u_v( (((seiToneMapping.sei_bit_depth+7)>>3)<<3), "SEI: sei_pivot_value", seiToneMapping.sei_pivot_value[i], bitstream);
01911 #ifdef PRINT_TONE_MAPPING
01912 printf("coded_pivot_value[%d] = %d, sei_pivot_value[%d] = %d\n", i, seiToneMapping.coded_pivot_value[i], i, seiToneMapping.sei_pivot_value[i]);
01913 #endif
01914 }
01915 }
01916 }
01917
01918
01919 if ( bitstream->bits_to_go != 8 )
01920 {
01921 (bitstream->byte_buf) <<= 1;
01922 bitstream->byte_buf |= 1;
01923 bitstream->bits_to_go--;
01924 if ( bitstream->bits_to_go != 0 )
01925 (bitstream->byte_buf) <<= (bitstream->bits_to_go);
01926 bitstream->bits_to_go = 8;
01927 bitstream->streamBuffer[bitstream->byte_pos++]=bitstream->byte_buf;
01928 bitstream->byte_buf = 0;
01929 }
01930 seiToneMapping.payloadSize = bitstream->byte_pos;
01931 }
01932
01933
01934 void UpdateToneMapping()
01935 {
01936
01937
01938
01939 }
01940
01941 void ClearToneMapping()
01942 {
01943 memset( seiToneMapping.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
01944 seiToneMapping.data->bits_to_go = 8;
01945 seiToneMapping.data->byte_pos = 0;
01946 seiToneMapping.data->byte_buf = 0;
01947 seiToneMapping.payloadSize = 0;
01948
01949 seiHasTone_mapping=FALSE;
01950 }
01951
01952 void CloseToneMapping()
01953 {
01954
01955 if (seiToneMapping.data)
01956 {
01957 free(seiToneMapping.data->streamBuffer);
01958 free(seiToneMapping.data);
01959 }
01960 seiToneMapping.data = NULL;
01961 seiHasTone_mapping = FALSE;
01962 }
01963
01964
01965
01966
01967
01968
01969
01970
01971
01972
01973 post_filter_information_struct seiPostFilterHints;
01974
01975 void InitPostFilterHints()
01976 {
01977 seiPostFilterHints.data = malloc( sizeof(Bitstream) );
01978 if( seiPostFilterHints.data == NULL ) no_mem_exit("InitPostFilterHints: seiPostFilterHints.data");
01979 seiPostFilterHints.data->streamBuffer = malloc(MAXRTPPAYLOADLEN);
01980 if( seiPostFilterHints.data->streamBuffer == NULL ) no_mem_exit("InitPostFilterHints: seiPostFilterHints.data->streamBuffer");
01981 ClearPostFilterHints();
01982 }
01983
01984 void ClearPostFilterHints()
01985 {
01986 memset( seiPostFilterHints.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
01987 seiPostFilterHints.data->bits_to_go = 8;
01988 seiPostFilterHints.data->byte_pos = 0;
01989 seiPostFilterHints.data->byte_buf = 0;
01990 seiPostFilterHints.payloadSize = 0;
01991
01992 seiPostFilterHints.filter_hint_size_y = 0;
01993 seiPostFilterHints.filter_hint_size_x = 0;
01994 seiPostFilterHints.filter_hint_type = 0;
01995 seiPostFilterHints.additional_extension_flag = 0;
01996 }
01997
01998 void UpdatePostFilterHints()
01999 {
02000 unsigned int color_component, cx, cy;
02001 seiPostFilterHints.filter_hint_type = 0;
02002 seiPostFilterHints.filter_hint_size_y = seiPostFilterHints.filter_hint_size_x = 5;
02003 get_mem3Dint(&seiPostFilterHints.filter_hint, 3, seiPostFilterHints.filter_hint_size_y, seiPostFilterHints.filter_hint_size_x);
02004
02005 for (color_component = 0; color_component < 3; color_component ++)
02006 for (cy = 0; cy < seiPostFilterHints.filter_hint_size_y; cy ++)
02007 for (cx = 0; cx < seiPostFilterHints.filter_hint_size_x; cx ++)
02008 seiPostFilterHints.filter_hint[color_component][cy][cx] = 1;
02009
02010 seiPostFilterHints.additional_extension_flag = 0;
02011 }
02012
02013 void FinalizePostFilterHints()
02014 {
02015 Bitstream *bitstream = seiPostFilterHints.data;
02016 unsigned int color_component, cx, cy;
02017
02018 ue_v( "SEI: post_filter_hint_size_y", seiPostFilterHints.filter_hint_size_y, bitstream);
02019 ue_v( "SEI: post_filter_hint_size_x", seiPostFilterHints.filter_hint_size_x, bitstream);
02020 u_v (2,"SEI: post_filter_hint_type", seiPostFilterHints.filter_hint_type, bitstream);
02021
02022 for (color_component = 0; color_component < 3; color_component ++)
02023 for (cy = 0; cy < seiPostFilterHints.filter_hint_size_y; cy ++)
02024 for (cx = 0; cx < seiPostFilterHints.filter_hint_size_x; cx ++)
02025 se_v("SEI: post_filter_hints", seiPostFilterHints.filter_hint[color_component][cy][cx], bitstream);
02026
02027 u_1 ("SEI: post_filter_additional_extension_flag", seiPostFilterHints.additional_extension_flag, bitstream);
02028
02029
02030 #ifdef PRINT_POST_FILTER_HINTS
02031 printf(" post_filter_hint_size_y %d \n", seiPostFilterHints.filter_hint_size_y);
02032 printf(" post_filter_hint_size_x %d \n", seiPostFilterHints.filter_hint_size_x);
02033 printf(" post_filter_hint_type %d \n", seiPostFilterHints.filter_hint_type);
02034 for (color_component = 0; color_component < 3; color_component ++)
02035 for (cy = 0; cy < seiPostFilterHints.filter_hint_size_y; cy ++)
02036 for (cx = 0; cx < seiPostFilterHints.filter_hint_size_x; cx ++)
02037 printf(" post_filter_hint[%d][%d][%d] %d \n", color_component, cy, cx, filter_hint[color_component][cy][cx]);
02038
02039 printf(" additional_extension_flag %d \n", seiPostFilterHints.additional_extension_flag);
02040
02041 #undef PRINT_POST_FILTER_HINTS
02042 #endif
02043
02044 if ( bitstream->bits_to_go != 8 )
02045 {
02046 (bitstream->byte_buf) <<= 1;
02047 bitstream->byte_buf |= 1;
02048 bitstream->bits_to_go--;
02049 if ( bitstream->bits_to_go != 0 )
02050 (bitstream->byte_buf) <<= (bitstream->bits_to_go);
02051 bitstream->bits_to_go = 8;
02052 bitstream->streamBuffer[bitstream->byte_pos++]=bitstream->byte_buf;
02053 bitstream->byte_buf = 0;
02054 }
02055 seiPostFilterHints.payloadSize = bitstream->byte_pos;
02056 }
02057
02058 void ClosePostFilterHints()
02059 {
02060 if (seiPostFilterHints.data)
02061 {
02062 free(seiPostFilterHints.data->streamBuffer);
02063 free(seiPostFilterHints.data);
02064 if (seiPostFilterHints.filter_hint)
02065 free_mem3Dint(seiPostFilterHints.filter_hint);
02066 }
02067 seiPostFilterHints.data = NULL;
02068 }
02069
02070
02071
02072
02073
02074
02075
02076 int Write_SEI_NALU(int len)
02077 {
02078 NALU_t *nalu = NULL;
02079 int RBSPlen = 0;
02080 int NALUlen;
02081 byte *rbsp;
02082
02083 if (HaveAggregationSEI())
02084 {
02085 nalu = AllocNALU(64000);
02086 rbsp = sei_message[AGGREGATION_SEI].data;
02087 RBSPlen = sei_message[AGGREGATION_SEI].payloadSize;
02088 NALUlen = RBSPtoNALU (rbsp, nalu, RBSPlen, NALU_TYPE_SEI, NALU_PRIORITY_LOW, 0, 1);
02089 nalu->startcodeprefix_len = 4;
02090
02091 len += WriteNALU (nalu);
02092 FreeNALU (nalu);
02093 }
02094
02095 return len;
02096 }
02097
02098
02099
02100
02101
02102
02103
02104
02105 void InitBufferingPeriod()
02106 {
02107 seiBufferingPeriod.data = malloc( sizeof(Bitstream) );
02108 if( seiBufferingPeriod.data == NULL )
02109 no_mem_exit("InitBufferingPeriod: seiBufferingPeriod.data");
02110
02111 seiBufferingPeriod.data->streamBuffer = malloc(MAXRTPPAYLOADLEN);
02112 if( seiBufferingPeriod.data->streamBuffer == NULL )
02113 no_mem_exit("InitBufferingPeriod: seiBufferingPeriod.data->streamBuffer");
02114
02115 ClearBufferingPeriod();
02116 }
02117
02118 void ClearBufferingPeriod()
02119 {
02120 unsigned int SchedSelIdx;
02121 memset( seiBufferingPeriod.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
02122
02123 seiBufferingPeriod.data->bits_to_go = 8;
02124 seiBufferingPeriod.data->byte_pos = 0;
02125 seiBufferingPeriod.data->byte_buf = 0;
02126 seiBufferingPeriod.payloadSize = 0;
02127
02128 seiBufferingPeriod.seq_parameter_set_id = active_sps->seq_parameter_set_id;
02129 if ( active_sps->vui_seq_parameters.nal_hrd_parameters_present_flag )
02130 {
02131 for ( SchedSelIdx = 0; SchedSelIdx <= active_sps->vui_seq_parameters.nal_hrd_parameters.cpb_cnt_minus1; SchedSelIdx++ )
02132 {
02133 seiBufferingPeriod.nal_initial_cpb_removal_delay[SchedSelIdx] = 0;
02134 seiBufferingPeriod.nal_initial_cpb_removal_delay_offset[SchedSelIdx] = 0;
02135 }
02136 }
02137 if ( active_sps->vui_seq_parameters.vcl_hrd_parameters_present_flag )
02138 {
02139 for ( SchedSelIdx = 0; SchedSelIdx <= active_sps->vui_seq_parameters.vcl_hrd_parameters.cpb_cnt_minus1; SchedSelIdx++ )
02140 {
02141 seiBufferingPeriod.vcl_initial_cpb_removal_delay[SchedSelIdx] = 0;
02142 seiBufferingPeriod.vcl_initial_cpb_removal_delay_offset[SchedSelIdx] = 0;
02143 }
02144 }
02145
02146 seiHasBuffering_period = FALSE;
02147 }
02148
02149 void UpdateBufferingPeriod()
02150 {
02151 seiHasBuffering_period = FALSE;
02152 }
02153
02154 void FinalizeBufferingPeriod()
02155 {
02156 unsigned int SchedSelIdx;
02157 Bitstream *bitstream = seiBufferingPeriod.data;
02158
02159 ue_v( "SEI: seq_parameter_set_id", seiBufferingPeriod.seq_parameter_set_id, bitstream);
02160 if ( active_sps->vui_seq_parameters.nal_hrd_parameters_present_flag )
02161 {
02162 for ( SchedSelIdx = 0; SchedSelIdx <= active_sps->vui_seq_parameters.nal_hrd_parameters.cpb_cnt_minus1; SchedSelIdx++ )
02163 {
02164 u_v( active_sps->vui_seq_parameters.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1,
02165 "SEI: initial_cpb_removal_delay", seiBufferingPeriod.nal_initial_cpb_removal_delay[SchedSelIdx], bitstream);
02166 u_v( active_sps->vui_seq_parameters.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1,
02167 "SEI: initial_cpb_removal_delay_offset", seiBufferingPeriod.nal_initial_cpb_removal_delay_offset[SchedSelIdx], bitstream);
02168 }
02169 }
02170 if ( active_sps->vui_seq_parameters.vcl_hrd_parameters_present_flag )
02171 {
02172 for ( SchedSelIdx = 0; SchedSelIdx <= active_sps->vui_seq_parameters.vcl_hrd_parameters.cpb_cnt_minus1; SchedSelIdx++ )
02173 {
02174 u_v( active_sps->vui_seq_parameters.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1,
02175 "SEI: initial_cpb_removal_delay", seiBufferingPeriod.vcl_initial_cpb_removal_delay[SchedSelIdx], bitstream);
02176 u_v( active_sps->vui_seq_parameters.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1,
02177 "SEI: initial_cpb_removal_delay_offset", seiBufferingPeriod.vcl_initial_cpb_removal_delay_offset[SchedSelIdx], bitstream);
02178 }
02179 }
02180
02181
02182 if ( bitstream->bits_to_go != 8 )
02183 {
02184 (bitstream->byte_buf) <<= 1;
02185 bitstream->byte_buf |= 1;
02186 bitstream->bits_to_go--;
02187 if ( bitstream->bits_to_go != 0 )
02188 (bitstream->byte_buf) <<= (bitstream->bits_to_go);
02189 bitstream->bits_to_go = 8;
02190 bitstream->streamBuffer[bitstream->byte_pos++]=bitstream->byte_buf;
02191 bitstream->byte_buf = 0;
02192 }
02193 seiBufferingPeriod.payloadSize = bitstream->byte_pos;
02194 }
02195
02196 void CloseBufferingPeriod()
02197 {
02198 if (seiBufferingPeriod.data)
02199 {
02200 free(seiBufferingPeriod.data->streamBuffer);
02201 free(seiBufferingPeriod.data);
02202 }
02203 seiBufferingPeriod.data = NULL;
02204 }
02205
02206
02207
02208
02209
02210
02211
02212 void InitPicTiming()
02213 {
02214 seiPicTiming.data = malloc( sizeof(Bitstream) );
02215 if( seiPicTiming.data == NULL )
02216 no_mem_exit("InitPicTiming: seiPicTiming.data");
02217
02218 seiPicTiming.data->streamBuffer = malloc(MAXRTPPAYLOADLEN);
02219 if( seiPicTiming.data->streamBuffer == NULL )
02220 no_mem_exit("InitPicTiming: seiPicTiming.data->streamBuffer");
02221
02222 ClearPicTiming();
02223 }
02224
02225
02226
02227
02228
02229
02230
02231 void ClearPicTiming()
02232 {
02233 memset( seiPicTiming.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
02234
02235 seiPicTiming.data->bits_to_go = 8;
02236 seiPicTiming.data->byte_pos = 0;
02237 seiPicTiming.data->byte_buf = 0;
02238 seiPicTiming.payloadSize = 0;
02239
02240
02241 seiPicTiming.cpb_removal_delay = 0;
02242 seiPicTiming.dpb_output_delay = 0;
02243 seiPicTiming.pic_struct = 0;
02244 memset(seiPicTiming.clock_timestamp_flag, 0, MAX_PIC_STRUCT_VALUE * sizeof(Boolean) );
02245 seiPicTiming.ct_type = 0;
02246 seiPicTiming.nuit_field_based_flag = FALSE;
02247 seiPicTiming.counting_type = 0;
02248 seiPicTiming.full_timestamp_flag = FALSE;
02249 seiPicTiming.discontinuity_flag = FALSE;
02250 seiPicTiming.cnt_dropped_flag = FALSE;
02251 seiPicTiming.n_frames = 0;
02252 seiPicTiming.seconds_value = 0;
02253 seiPicTiming.minutes_value = 0;
02254 seiPicTiming.hours_value = 0;
02255 seiPicTiming.seconds_flag = FALSE;
02256 seiPicTiming.minutes_flag = FALSE;
02257 seiPicTiming.hours_flag = FALSE;
02258 seiPicTiming.time_offset = 0;
02259
02260 seiHasPicTiming_info = FALSE;
02261 }
02262
02263
02264
02265
02266
02267
02268
02269 void UpdatePicTiming()
02270 {
02271 seiHasPicTiming_info = FALSE;
02272 }
02273
02274
02275
02276
02277
02278
02279
02280 void FinalizePicTiming()
02281 {
02282 Bitstream *bitstream = seiPicTiming.data;
02283
02284 Boolean CpbDpbDelaysPresentFlag = (Boolean) (active_sps->vui_parameters_present_flag
02285 && ( (active_sps->vui_seq_parameters.nal_hrd_parameters_present_flag != 0)
02286 ||(active_sps->vui_seq_parameters.vcl_hrd_parameters_present_flag != 0)));
02287 hrd_parameters_t *hrd = NULL;
02288
02289 assert( active_sps->vui_seq_parameters.vcl_hrd_parameters_present_flag || active_sps->vui_seq_parameters.nal_hrd_parameters_present_flag );
02290 if (active_sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
02291 hrd = &(active_sps->vui_seq_parameters.vcl_hrd_parameters);
02292 else if (active_sps->vui_seq_parameters.nal_hrd_parameters_present_flag)
02293 hrd = &(active_sps->vui_seq_parameters.nal_hrd_parameters);
02294 else
02295 error ("HRD structures not properly created.",-1);
02296
02297 if ( CpbDpbDelaysPresentFlag )
02298 {
02299 u_v( hrd->cpb_removal_delay_length_minus1 + 1, "SEI: cpb_removal_delay", seiPicTiming.cpb_removal_delay, bitstream);
02300 u_v( hrd->dpb_output_delay_length_minus1 + 1, "SEI: dpb_output_delay", seiPicTiming.dpb_output_delay, bitstream);
02301 }
02302 if ( active_sps->vui_seq_parameters.pic_struct_present_flag )
02303 {
02304 int NumClockTS = 0, i;
02305 int bottom_field_flag = (img->structure == BOTTOM_FIELD);
02306
02307 u_v( 4, "SEI: pic_struct", seiPicTiming.pic_struct, bitstream);
02308
02309 switch( seiPicTiming.pic_struct )
02310 {
02311 case 0:
02312 default:
02313
02314 assert( img->fld_flag == FALSE );
02315 NumClockTS = 1;
02316 break;
02317 case 1:
02318
02319 assert( (img->fld_flag == TRUE) && (bottom_field_flag == FALSE) );
02320 NumClockTS = 1;
02321 break;
02322 case 2:
02323
02324 assert( (img->fld_flag == TRUE) && (bottom_field_flag == TRUE) );
02325 NumClockTS = 1;
02326 break;
02327 case 3:
02328
02329 case 4:
02330
02331 assert( img->fld_flag == FALSE );
02332 NumClockTS = 2;
02333 break;
02334 case 5:
02335
02336 case 6:
02337
02338 assert( img->fld_flag == FALSE );
02339 NumClockTS = 3;
02340 case 7:
02341
02342 assert( (img->fld_flag == FALSE) && active_sps->vui_seq_parameters.fixed_frame_rate_flag == 1 );
02343 NumClockTS = 2;
02344 break;
02345 case 8:
02346
02347 assert( (img->fld_flag == FALSE) && active_sps->vui_seq_parameters.fixed_frame_rate_flag == 1 );
02348 NumClockTS = 3;
02349 break;
02350 }
02351 for ( i = 0; i < NumClockTS; i++ )
02352 {
02353 u_1( "SEI: clock_timestamp_flag", seiPicTiming.clock_timestamp_flag[i], bitstream);
02354 if ( seiPicTiming.clock_timestamp_flag[i] )
02355 {
02356 u_v( 2, "SEI: ct_type", seiPicTiming.ct_type, bitstream);
02357 u_1( "SEI: nuit_field_based_flag", seiPicTiming.nuit_field_based_flag, bitstream);
02358 u_v( 5, "SEI: counting_type", seiPicTiming.counting_type, bitstream);
02359 u_1( "SEI: full_timestamp_flag", seiPicTiming.full_timestamp_flag, bitstream);
02360 u_1( "SEI: discontinuity_flag", seiPicTiming.discontinuity_flag, bitstream);
02361 u_1( "SEI: cnt_dropped_flag", seiPicTiming.cnt_dropped_flag, bitstream);
02362 u_v( 8, "SEI: n_frames", seiPicTiming.n_frames, bitstream);
02363
02364 if ( seiPicTiming.full_timestamp_flag )
02365 {
02366 u_v( 6, "SEI: seconds_value", seiPicTiming.seconds_value, bitstream);
02367 u_v( 6, "SEI: minutes_value", seiPicTiming.minutes_value, bitstream);
02368 u_v( 5, "SEI: hours_value", seiPicTiming.hours_value, bitstream);
02369 }
02370 else
02371 {
02372 u_1( "SEI: seconds_flag", seiPicTiming.seconds_flag, bitstream);
02373 if (seiPicTiming.seconds_flag)
02374 {
02375 u_v( 6, "SEI: seconds_value", seiPicTiming.seconds_value, bitstream);
02376 u_1( "SEI: minutes_flag", seiPicTiming.minutes_flag, bitstream);
02377 if (seiPicTiming.minutes_flag)
02378 {
02379 u_v( 6, "SEI: minutes_value", seiPicTiming.minutes_value, bitstream);
02380 u_1( "SEI: hours_flag", seiPicTiming.hours_flag, bitstream);
02381 if (seiPicTiming.hours_flag)
02382 {
02383 u_v( 5, "SEI: hours_value", seiPicTiming.hours_value, bitstream);
02384 }
02385 }
02386 }
02387 }
02388 if ( hrd->time_offset_length )
02389 {
02390 u_v( hrd->time_offset_length, "SEI: time_offset", seiPicTiming.time_offset, bitstream);
02391 }
02392 }
02393 }
02394 }
02395
02396
02397 if ( bitstream->bits_to_go != 8 )
02398 {
02399 (bitstream->byte_buf) <<= 1;
02400 bitstream->byte_buf |= 1;
02401 bitstream->bits_to_go--;
02402 if ( bitstream->bits_to_go != 0 )
02403 (bitstream->byte_buf) <<= (bitstream->bits_to_go);
02404 bitstream->bits_to_go = 8;
02405 bitstream->streamBuffer[bitstream->byte_pos++]=bitstream->byte_buf;
02406 bitstream->byte_buf = 0;
02407 }
02408 seiPicTiming.payloadSize = bitstream->byte_pos;
02409 }
02410
02411
02412
02413
02414
02415
02416
02417 void ClosePicTiming()
02418 {
02419 if (seiPicTiming.data)
02420 {
02421 free(seiPicTiming.data->streamBuffer);
02422 free(seiPicTiming.data);
02423 }
02424 seiPicTiming.data = NULL;
02425 }
02426
02427
02428
02429
02430
02431
02432
02433 void InitDRPMRepetition()
02434 {
02435 seiDRPMRepetition.data = malloc( sizeof(Bitstream) );
02436 if( seiDRPMRepetition.data == NULL )
02437 no_mem_exit("InitDRPMRepetition: seiDRPMRepetition.data");
02438
02439 seiDRPMRepetition.data->streamBuffer = malloc(MAXRTPPAYLOADLEN);
02440 if( seiDRPMRepetition.data->streamBuffer == NULL )
02441 no_mem_exit("InitDRPMRepetition: seiDRPMRepetition.data->streamBuffer");
02442
02443 ClearDRPMRepetition();
02444 }
02445
02446
02447
02448
02449
02450
02451
02452 void ClearDRPMRepetition()
02453 {
02454 memset( seiDRPMRepetition.data->streamBuffer, 0, MAXRTPPAYLOADLEN);
02455
02456 seiDRPMRepetition.data->bits_to_go = 8;
02457 seiDRPMRepetition.data->byte_pos = 0;
02458 seiDRPMRepetition.data->byte_buf = 0;
02459 seiDRPMRepetition.payloadSize = 0;
02460
02461 seiDRPMRepetition.original_bottom_field_flag = FALSE;
02462 seiDRPMRepetition.original_field_pic_flag = FALSE;
02463 seiDRPMRepetition.original_frame_num = 0;
02464 seiDRPMRepetition.original_idr_flag = FALSE;
02465 if ( seiDRPMRepetition.dec_ref_pic_marking_buffer_saved != NULL )
02466 {
02467 free_drpm_buffer( seiDRPMRepetition.dec_ref_pic_marking_buffer_saved );
02468 seiDRPMRepetition.dec_ref_pic_marking_buffer_saved = NULL;
02469 }
02470
02471 seiHasDRPMRepetition_info = FALSE;
02472 }
02473
02474
02475
02476
02477
02478
02479
02480 void UpdateDRPMRepetition()
02481 {
02482 if ( seiDRPMRepetition.dec_ref_pic_marking_buffer_saved != NULL )
02483 {
02484 seiHasDRPMRepetition_info = TRUE;
02485 }
02486 else
02487 {
02488 seiHasDRPMRepetition_info = FALSE;
02489 }
02490 }
02491
02492
02493
02494
02495
02496
02497
02498 void FinalizeDRPMRepetition()
02499 {
02500 Bitstream *bitstream = seiDRPMRepetition.data;
02501
02502
02503 u_1( "SEI: original_idr_flag", seiDRPMRepetition.original_idr_flag, bitstream);
02504 ue_v( "SEI: original_frame_num", seiDRPMRepetition.original_frame_num, bitstream);
02505 if ( !active_sps->frame_mbs_only_flag )
02506 {
02507 u_1( "SEI: original_field_pic_flag", seiDRPMRepetition.original_field_pic_flag, bitstream);
02508 if ( seiDRPMRepetition.original_field_pic_flag )
02509 {
02510 u_1( "SEI: original_bottom_field_flag", seiDRPMRepetition.original_bottom_field_flag, bitstream);
02511 }
02512 }
02513
02514 dec_ref_pic_marking( bitstream,
02515 seiDRPMRepetition.dec_ref_pic_marking_buffer_saved,
02516 seiDRPMRepetition.original_idr_flag, 0, 0);
02517
02518
02519 if ( bitstream->bits_to_go != 8 )
02520 {
02521 (bitstream->byte_buf) <<= 1;
02522 bitstream->byte_buf |= 1;
02523 bitstream->bits_to_go--;
02524 if ( bitstream->bits_to_go != 0 )
02525 (bitstream->byte_buf) <<= (bitstream->bits_to_go);
02526 bitstream->bits_to_go = 8;
02527 bitstream->streamBuffer[bitstream->byte_pos++]=bitstream->byte_buf;
02528 bitstream->byte_buf = 0;
02529 }
02530 seiDRPMRepetition.payloadSize = bitstream->byte_pos;
02531 }
02532
02533
02534
02535
02536
02537
02538
02539 void CloseDRPMRepetition()
02540 {
02541 if (seiDRPMRepetition.data)
02542 {
02543 free(seiDRPMRepetition.data->streamBuffer);
02544 free(seiDRPMRepetition.data);
02545 }
02546 seiDRPMRepetition.data = NULL;
02547 if ( seiDRPMRepetition.dec_ref_pic_marking_buffer_saved != NULL )
02548 {
02549 free_drpm_buffer( seiDRPMRepetition.dec_ref_pic_marking_buffer_saved );
02550 seiDRPMRepetition.dec_ref_pic_marking_buffer_saved = NULL;
02551 }
02552 }
02553
02554
02555
02556
02557
02558
02559
02560
02561
02562
02563
02564
02565 void PrepareAggregationSEIMessage(void)
02566 {
02567 Boolean has_aggregation_sei_message = FALSE;
02568
02569 clear_sei_message(AGGREGATION_SEI);
02570
02571
02572
02573 if (seiHasSparePicture && img->type != B_SLICE)
02574 {
02575 FinalizeSpareMBMap();
02576 assert(seiSparePicturePayload.data->byte_pos == seiSparePicturePayload.payloadSize);
02577 write_sei_message(AGGREGATION_SEI, seiSparePicturePayload.data->streamBuffer, seiSparePicturePayload.payloadSize, SEI_SPARE_PIC);
02578 has_aggregation_sei_message = TRUE;
02579 }
02580
02581 if (seiHasSubseqInfo)
02582 {
02583 FinalizeSubseqInfo(img->layer);
02584 write_sei_message(AGGREGATION_SEI, seiSubseqInfo[img->layer].data->streamBuffer, seiSubseqInfo[img->layer].payloadSize, SEI_SUB_SEQ_INFO);
02585 ClearSubseqInfoPayload(img->layer);
02586 has_aggregation_sei_message = TRUE;
02587 }
02588
02589 if (seiHasSubseqLayerInfo && img->number == 0)
02590 {
02591 FinalizeSubseqLayerInfo();
02592 write_sei_message(AGGREGATION_SEI, seiSubseqLayerInfo.data, seiSubseqLayerInfo.payloadSize, SEI_SUB_SEQ_LAYER_CHARACTERISTICS);
02593 seiHasSubseqLayerInfo = FALSE;
02594 has_aggregation_sei_message = TRUE;
02595 }
02596
02597 if (seiHasSubseqChar)
02598 {
02599 FinalizeSubseqChar();
02600 write_sei_message(AGGREGATION_SEI, seiSubseqChar.data->streamBuffer, seiSubseqChar.payloadSize, SEI_SUB_SEQ_CHARACTERISTICS);
02601 ClearSubseqCharPayload();
02602 has_aggregation_sei_message = TRUE;
02603 }
02604
02605 if (seiHasPanScanRectInfo)
02606 {
02607 FinalizePanScanRectInfo();
02608 write_sei_message(AGGREGATION_SEI, seiPanScanRectInfo.data->streamBuffer, seiPanScanRectInfo.payloadSize, SEI_PAN_SCAN_RECT);
02609 ClearPanScanRectInfoPayload();
02610 has_aggregation_sei_message = TRUE;
02611 }
02612
02613 if (seiHasUser_data_unregistered_info)
02614 {
02615 FinalizeUser_data_unregistered();
02616 write_sei_message(AGGREGATION_SEI, seiUser_data_unregistered.data->streamBuffer, seiUser_data_unregistered.payloadSize, SEI_USER_DATA_UNREGISTERED);
02617 ClearUser_data_unregistered();
02618 has_aggregation_sei_message = TRUE;
02619 }
02620
02621 if (seiHasUser_data_registered_itu_t_t35_info)
02622 {
02623 FinalizeUser_data_registered_itu_t_t35();
02624 write_sei_message(AGGREGATION_SEI, seiUser_data_registered_itu_t_t35.data->streamBuffer, seiUser_data_registered_itu_t_t35.payloadSize, SEI_USER_DATA_REGISTERED_ITU_T_T35);
02625 ClearUser_data_registered_itu_t_t35();
02626 has_aggregation_sei_message = TRUE;
02627 }
02628
02629
02630
02631 if (seiHasSceneInformation)
02632 {
02633 FinalizeSceneInformation();
02634 write_sei_message(AGGREGATION_SEI, seiSceneInformation.data->streamBuffer, seiSceneInformation.payloadSize, SEI_SCENE_INFO);
02635 has_aggregation_sei_message = TRUE;
02636 }
02637
02638 if (seiHasTone_mapping)
02639 {
02640 FinalizeToneMapping();
02641 write_sei_message(AGGREGATION_SEI, seiToneMapping.data->streamBuffer, seiToneMapping.payloadSize, SEI_TONE_MAPPING);
02642 ClearToneMapping();
02643 has_aggregation_sei_message = TRUE;
02644 }
02645
02646 if (seiHasPostFilterHints_info)
02647 {
02648 FinalizePostFilterHints();
02649 write_sei_message(AGGREGATION_SEI, seiPostFilterHints.data->streamBuffer, seiPostFilterHints.payloadSize, SEI_POST_FILTER_HINTS);
02650 has_aggregation_sei_message = TRUE;
02651 }
02652
02653 if (seiHasBuffering_period)
02654 {
02655 FinalizeBufferingPeriod();
02656 write_sei_message(AGGREGATION_SEI, seiBufferingPeriod.data->streamBuffer, seiBufferingPeriod.payloadSize, SEI_BUFFERING_PERIOD);
02657 has_aggregation_sei_message = TRUE;
02658 }
02659
02660 if (seiHasPicTiming_info)
02661 {
02662 FinalizePicTiming();
02663 write_sei_message(AGGREGATION_SEI, seiPicTiming.data->streamBuffer, seiPicTiming.payloadSize, SEI_PIC_TIMING);
02664 has_aggregation_sei_message = TRUE;
02665 }
02666
02667 if (seiHasRecoveryPoint_info)
02668 {
02669 FinalizeRandomAccess();
02670 write_sei_message(AGGREGATION_SEI, seiRecoveryPoint.data->streamBuffer, seiRecoveryPoint.payloadSize, SEI_RECOVERY_POINT);
02671 ClearRandomAccess();
02672 has_aggregation_sei_message = TRUE;
02673 }
02674
02675 if (seiHasDRPMRepetition_info)
02676 {
02677 FinalizeDRPMRepetition();
02678 write_sei_message(AGGREGATION_SEI, seiDRPMRepetition.data->streamBuffer, seiDRPMRepetition.payloadSize, SEI_DEC_REF_PIC_MARKING_REPETITION);
02679 has_aggregation_sei_message = TRUE;
02680 ClearDRPMRepetition();
02681 }
02682
02683
02684 if (has_aggregation_sei_message)
02685 {
02686 finalize_sei_message(AGGREGATION_SEI);
02687 }
02688 }
02689
02690
02691
02692
02693
02694
02695
02696 void free_drpm_buffer( DecRefPicMarking_t *pDRPM )
02697 {
02698 DecRefPicMarking_t *pTmp = pDRPM, *pPrev;
02699
02700 if (pTmp == NULL)
02701 return;
02702
02703 while( pTmp->Next != NULL )
02704 {
02705 pPrev = pTmp;
02706 pTmp = pTmp->Next;
02707 free( pPrev );
02708 }
02709 free( pTmp );
02710 }
02711
02712