00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "contributors.h"
00015
00016 #include <ctype.h>
00017 #include <limits.h>
00018 #include "global.h"
00019
00020 #include "explicit_gop.h"
00021 #include "image.h"
00022 #include "nalucommon.h"
00023 #include "report.h"
00024
00025
00026
00027
00028
00029
00030
00031 void create_hierarchy(ImageParameters *p_Img, InputParameters *p_Inp)
00032 {
00033 int i, j;
00034 int centerB = (p_Inp->NumberBFrames >> 1);
00035 GOP_DATA tmp;
00036
00037 if (p_Inp->HierarchicalCoding == 1)
00038 {
00039 for (i=0;i<p_Inp->NumberBFrames;i++)
00040 {
00041 if (i < centerB)
00042 {
00043 p_Img->gop_structure[i].slice_type = ( p_Inp->PReplaceBSlice ) ? P_SLICE : B_SLICE;
00044 p_Img->gop_structure[i].display_no = i * 2 + 1;
00045 p_Img->gop_structure[i].hierarchy_layer = 1;
00046 p_Img->gop_structure[i].reference_idc = NALU_PRIORITY_LOW;
00047 p_Img->gop_structure[i].slice_qp = imax(0, (p_Inp->qp[0][B_SLICE] + (p_Inp->HierarchyLevelQPEnable ? -1: p_Inp->qpBRSOffset[0])));
00048
00049 }
00050 else
00051 {
00052 p_Img->gop_structure[i].slice_type = ( p_Inp->PReplaceBSlice ) ? P_SLICE : B_SLICE;
00053 p_Img->gop_structure[i].display_no = (i - centerB) * 2;
00054 p_Img->gop_structure[i].hierarchy_layer = 0;
00055 p_Img->gop_structure[i].reference_idc = NALU_PRIORITY_DISPOSABLE;
00056 p_Img->gop_structure[i].slice_qp = p_Inp->qp[0][B_SLICE];
00057 }
00058 }
00059 p_Img->GopLevels = 2;
00060 }
00061 else
00062 {
00063 int GOPlevels = 1;
00064 int Bframes = p_Inp->NumberBFrames;
00065 int *curGOPLevelfrm,*curGOPLeveldist ;
00066 int curlevel = GOPlevels ;
00067 int i;
00068
00069 while (((Bframes + 1 ) >> GOPlevels) > 1)
00070 {
00071 GOPlevels ++;
00072 }
00073
00074 curlevel = GOPlevels;
00075 p_Img->GopLevels = GOPlevels;
00076 if (NULL == (curGOPLevelfrm = (int*)malloc(GOPlevels * sizeof(int)))) no_mem_exit("create_hierarchy:curGOPLevelfrm");
00077 if (NULL == (curGOPLeveldist= (int*)malloc(GOPlevels * sizeof(int)))) no_mem_exit("create_hierarchy:curGOPLeveldist");
00078
00079 for (i=0; i <p_Inp->NumberBFrames; i++)
00080 {
00081 p_Img->gop_structure[i].display_no = i;
00082 p_Img->gop_structure[i].slice_type = ( p_Inp->PReplaceBSlice ) ? P_SLICE : B_SLICE;
00083 p_Img->gop_structure[i].hierarchy_layer = 0;
00084 p_Img->gop_structure[i].reference_idc = NALU_PRIORITY_DISPOSABLE;
00085 p_Img->gop_structure[i].slice_qp = p_Inp->qp[0][B_SLICE];
00086 }
00087
00088 for (j = 1; j < GOPlevels; j++)
00089 {
00090 for (i = (1 << j) - 1; i < Bframes + 1 - (1 << j); i += (1 << j))
00091 {
00092 p_Img->gop_structure[i].hierarchy_layer = j;
00093 p_Img->gop_structure[i].reference_idc = NALU_PRIORITY_LOW;
00094 p_Img->gop_structure[i].slice_qp = imax(0, p_Inp->qp[0][B_SLICE] + (p_Inp->HierarchyLevelQPEnable ? -j: p_Inp->qpBRSOffset[0]));
00095
00096
00097
00098
00099
00100 }
00101 }
00102
00103 for (i = 1; i < Bframes; i++)
00104 {
00105 j = i;
00106
00107 while (j > 0 && p_Img->gop_structure[j].hierarchy_layer > p_Img->gop_structure[j-1].hierarchy_layer)
00108 {
00109 tmp = p_Img->gop_structure[j-1];
00110 p_Img->gop_structure[j-1] = p_Img->gop_structure[j];
00111 p_Img->gop_structure[j] = tmp;
00112 j--;
00113 }
00114 }
00115 }
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 void init_gop_structure(ImageParameters *p_Img, InputParameters *p_Inp)
00127 {
00128 int max_gopsize = p_Inp->NumberBFrames;
00129
00130 p_Img->gop_structure = calloc(imax(10,max_gopsize), sizeof (GOP_DATA));
00131 if (NULL==p_Img->gop_structure)
00132 no_mem_exit("init_gop_structure: gop_structure");
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142 void clear_gop_structure(ImageParameters *p_Img)
00143 {
00144 if (p_Img->gop_structure)
00145 free(p_Img->gop_structure);
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155 void interpret_gop_structure(ImageParameters *p_Img, InputParameters *p_Inp)
00156 {
00157 int nLength = strlen(p_Inp->ExplicitHierarchyFormat);
00158 int i =0, k, dqp, display_no;
00159 int slice_read =0, order_read = 0, stored_read = 0, qp_read =0;
00160 int coded_frame = 0;
00161
00162 if (nLength > 0)
00163 {
00164
00165 for (i = 0; i < nLength ; i++)
00166 {
00167
00168 if (slice_read == 0)
00169 {
00170 switch (p_Inp->ExplicitHierarchyFormat[i])
00171 {
00172 case 'P':
00173 case 'p':
00174 p_Img->gop_structure[coded_frame].slice_type = P_SLICE;
00175 break;
00176 case 'B':
00177 case 'b':
00178 p_Img->gop_structure[coded_frame].slice_type = B_SLICE;
00179 break;
00180 case 'I':
00181 case 'i':
00182 p_Img->gop_structure[coded_frame].slice_type = I_SLICE;
00183 break;
00184 default:
00185 snprintf(errortext, ET_SIZE, "Slice Type invalid in ExplicitHierarchyFormat param. Please check configuration file.");
00186 error (errortext, 400);
00187 break;
00188 }
00189 slice_read = 1;
00190 }
00191 else
00192 {
00193
00194 if (order_read == 0)
00195 {
00196 if (isdigit((int)(*(p_Inp->ExplicitHierarchyFormat+i))))
00197 {
00198 sscanf(p_Inp->ExplicitHierarchyFormat+i,"%d",&display_no);
00199 p_Img->gop_structure[coded_frame].display_no = display_no;
00200 order_read = 1;
00201 if (display_no < 0 || display_no >= p_Inp->NumberBFrames)
00202 {
00203 snprintf(errortext, ET_SIZE, "Invalid Frame Order value. Frame position needs to be in [0,%d] range.",p_Inp->NumberBFrames - 1);
00204 error (errortext, 400);
00205 }
00206 for (k=0;k<coded_frame;k++)
00207 {
00208 if (p_Img->gop_structure[k].display_no == display_no)
00209 {
00210 snprintf(errortext, ET_SIZE, "Frame Order value %d in frame %d already used for enhancement frame %d.",display_no,coded_frame,k);
00211 error (errortext, 400);
00212 }
00213 }
00214 }
00215 else
00216 {
00217 snprintf(errortext, ET_SIZE, "Slice Type needs to be followed by Display Order. Please check configuration file.");
00218 error (errortext, 400);
00219 }
00220 }
00221 else if (order_read == 1)
00222 {
00223 if (stored_read == 0 && !(isdigit((int)(*(p_Inp->ExplicitHierarchyFormat+i)))))
00224 {
00225 switch (p_Inp->ExplicitHierarchyFormat[i])
00226 {
00227 case 'E':
00228 case 'e':
00229 p_Img->gop_structure[coded_frame].reference_idc = NALU_PRIORITY_DISPOSABLE;
00230 p_Img->gop_structure[coded_frame].hierarchy_layer = 0;
00231 break;
00232 case 'R':
00233 case 'r':
00234 p_Img->gop_structure[coded_frame].reference_idc= NALU_PRIORITY_LOW;
00235 p_Img->gop_structure[coded_frame].hierarchy_layer = 1;
00236 p_Img->GopLevels = 2;
00237 break;
00238 default:
00239 snprintf(errortext, ET_SIZE, "Reference_IDC invalid in ExplicitHierarchyFormat param. Please check configuration file.");
00240 error (errortext, 400);
00241 break;
00242 }
00243 stored_read = 1;
00244 }
00245 else if (stored_read == 1 && qp_read == 0)
00246 {
00247 if (isdigit((int)(*(p_Inp->ExplicitHierarchyFormat+i))))
00248 {
00249 sscanf(p_Inp->ExplicitHierarchyFormat+i,"%d",&dqp);
00250
00251 p_Img->gop_structure[coded_frame].slice_qp = p_Inp->qp[0][ p_Img->gop_structure[coded_frame].slice_type ];
00252 p_Img->gop_structure[coded_frame].slice_qp = iClip3(-p_Img->bitdepth_luma_qp_scale, 51,p_Img->gop_structure[coded_frame].slice_qp + dqp);
00253 qp_read = 1;
00254 }
00255 else
00256 {
00257 snprintf(errortext, ET_SIZE, "Reference_IDC needs to be followed by QP. Please check configuration file.");
00258 error (errortext, 400);
00259 }
00260 }
00261 else if (stored_read == 1 && qp_read == 1 && !(isdigit((int)(*(p_Inp->ExplicitHierarchyFormat+i)))) && (i < nLength - 3))
00262 {
00263 stored_read =0;
00264 qp_read=0;
00265 order_read=0;
00266 slice_read=0;
00267 i--;
00268 coded_frame ++;
00269 if (coded_frame >= p_Inp->NumberBFrames )
00270 {
00271 snprintf(errortext, ET_SIZE, "Total number of frames in Enhancement GOP need to be fewer or equal to NumberBFrames parameter.");
00272 error (errortext, 400);
00273 }
00274 }
00275 }
00276 }
00277 }
00278 }
00279 else
00280 {
00281 snprintf(errortext, ET_SIZE, "ExplicitHierarchyFormat is empty. Please check configuration file.");
00282 error (errortext, 400);
00283 }
00284
00285 p_Inp->NumberBFrames = coded_frame + 1;
00286 }
00287