00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "contributors.h"
00015 #include "global.h"
00016 #include "report.h"
00017 #include "explicit_seq.h"
00018
00019
00020
00021
00022
00023
00024
00025 static int ReadTextField(FILE *expSeqFile, char* keyword)
00026 {
00027 char readline [100];
00028 char word[64];
00029 int err = -1;
00030 int read_line = 0;
00031 do
00032 {
00033 read_line++;
00034
00035 if (NULL == fgets(readline, 100, expSeqFile))
00036 error ("error parsing explicit sequence file", 500),
00037 err = sscanf(readline,"%s",word);
00038 if (err == -1)
00039 break;
00040 if (strcasecmp(word, keyword) != 0)
00041 err = - 1;
00042
00043 if (feof(expSeqFile))
00044 break;
00045 }
00046 while (err == - 1 && read_line < 6);
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 return err;
00057 }
00058
00059
00060
00061
00062
00063
00064
00065 void ReadIntField(FILE *expSeqFile, char* format, char* keyword, int* value)
00066 {
00067 char readline [100];
00068 char word[64];
00069 int err = -1;
00070 int read_line = 0;
00071
00072 do
00073 {
00074 read_line++;
00075 if (NULL == fgets(readline, 100, expSeqFile))
00076 error ("error parsing explicit sequence file", 500),
00077 err = sscanf(readline, format, word, value);
00078 if (feof(expSeqFile))
00079 break;
00080 }
00081 while (strcasecmp(word, keyword) != 0 && read_line < 6);
00082
00083 if (err != 2 || strcasecmp(word, keyword) != 0)
00084 {
00085 printf("Error while reading %s.\n", keyword);
00086 report_stats_on_error();
00087 }
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 void ReadCharField(FILE *expSeqFile, char* format, char* keyword, char* value)
00097 {
00098 char readline [100];
00099 char word[64];
00100 int err = -1;
00101 int read_line = 0;
00102
00103 do
00104 {
00105 read_line++;
00106 if (NULL == fgets(readline, 100, expSeqFile))
00107 error ("error parsing explicit sequence file", 500),
00108 err = sscanf(readline, format, word, value);
00109
00110 while (!feof(expSeqFile))
00111 break;
00112 }
00113 while (strcasecmp(word, keyword) != 0 && read_line < 6);
00114
00115 if (err != 2 || strcasecmp(word, keyword) != 0)
00116 {
00117 printf("Error while reading %s.\n", keyword);
00118 report_stats_on_error();
00119 }
00120 }
00121
00122
00123 static void ParseSliceType(char *slice_type, ExpFrameInfo *info, int coding_index)
00124 {
00125 if ( strcasecmp(slice_type, "P") == 0 )
00126 {
00127 info->slice_type = P_SLICE;
00128 }
00129 else if ( strcasecmp(slice_type, "B") == 0 )
00130 {
00131 info->slice_type = B_SLICE;
00132 }
00133 else if ( strcasecmp(slice_type, "I") == 0 )
00134 {
00135 info->slice_type = I_SLICE;
00136 }
00137 else if ( strcasecmp(slice_type, "SP") == 0 )
00138 {
00139 info->slice_type = SP_SLICE;
00140 }
00141 else if ( strcasecmp(slice_type, "SI") == 0 )
00142 {
00143 info->slice_type = SI_SLICE;
00144 }
00145 else
00146 {
00147 printf("ReadExplicitSeqFile : invalid slice type\n");
00148 report_stats_on_error();
00149 }
00150
00151 if (coding_index == 0 && info->slice_type != I_SLICE)
00152 {
00153 printf("ReadExplicitSeqFile : First coded picture needs to be Intra.\n");
00154 report_stats_on_error();
00155 }
00156 }
00157
00158 static void ParseReferenceIDC(int reference_idc, int coding_index)
00159 {
00160 if ( reference_idc < NALU_PRIORITY_DISPOSABLE || reference_idc > NALU_PRIORITY_HIGHEST)
00161 {
00162 printf("ReadExplicitSeqFile : Invalid reference indicator \n");
00163 report_stats_on_error();
00164 }
00165
00166 if (coding_index == 0 && reference_idc == NALU_PRIORITY_DISPOSABLE)
00167 {
00168 printf("ReadExplicitSeqFile : First coded picture needs to be a reference picture.\n");
00169 report_stats_on_error();
00170 }
00171 }
00172
00173
00174 static void ParseSeqNumber(int seq_number, ExpSeqInfo *seq_info, int coding_index)
00175 {
00176 int i;
00177 for (i = 0; i < imin(coding_index, seq_info->no_frames); i++)
00178 {
00179 if (seq_info->info[i].seq_number == seq_number)
00180 {
00181 printf("ReadExplicitSeqFile : SeqNumber used for current frame already used. Terminating\n");
00182 report_stats_on_error();
00183 }
00184 }
00185 }
00186
00187
00188
00189
00190
00191
00192
00193 void ReadFrameData(FILE *expSeqFile, ExpSeqInfo *seq_info, int coding_index)
00194 {
00195 Boolean slice_type_present = FALSE;
00196 Boolean seq_number_present = FALSE;
00197 char readline [100];
00198 char word[64], value[64];
00199 int err = -1;
00200 ExpFrameInfo *info = &seq_info->info[coding_index % seq_info->no_frames];
00201
00202 info->reference_idc = NALU_PRIORITY_HIGHEST;
00203
00204 ReadTextField (expSeqFile, "{");
00205 do
00206 {
00207
00208 if (NULL == fgets(readline, 100, expSeqFile))
00209 error ("error parsing explicit sequence file", 500),
00210
00211 err = sscanf(readline, "%s : %s", word, value);
00212 if (err == 1)
00213 {
00214 if (strcasecmp(word, "}") == 0)
00215 break;
00216 else if (strcasecmp(word, "{") == 0)
00217 {
00218 printf("Invalid \"{\" character found. Terminating\n");
00219 report_stats_on_error();
00220 }
00221 }
00222 else if (err == 2)
00223 {
00224 if (strcasecmp(word, "SeqNumber") == 0)
00225 {
00226 info->seq_number = atoi(value);
00227 ParseSeqNumber(info->seq_number, seq_info, coding_index);
00228 seq_number_present = TRUE;
00229 }
00230 else if (strcasecmp(word, "SliceType") == 0)
00231 {
00232 ParseSliceType(value, info, coding_index);
00233 slice_type_present = TRUE;
00234 }
00235 else if (strcasecmp(word, "IDRPicture") == 0)
00236 info->is_idr = atoi(value);
00237 else if (strcasecmp(word, "Reference") == 0)
00238 {
00239 info->reference_idc = atoi(value);
00240 ParseReferenceIDC(info->reference_idc, coding_index);
00241 }
00242 }
00243 }
00244 while (!feof(expSeqFile));
00245
00246 if (slice_type_present == FALSE || seq_number_present == FALSE)
00247 {
00248 printf("Sequence info file does not contain all mandatory info (SeqNumber or SliceType). Terminating.\n");
00249 report_stats_on_error();
00250 }
00251 }
00252
00253
00254
00255
00256
00257
00258
00259 void ReadExplicitSeqFile(ExpSeqInfo *seq_info, FILE *expSFile, int coding_index)
00260 {
00261 int frm_header = ReadTextField(expSFile, "Frame");
00262
00263 if (frm_header != -1)
00264 {
00265 ReadFrameData (expSFile, seq_info, coding_index);
00266 }
00267 else
00268 {
00269 printf("ReadExplicitSeqFile : No more data. \n");
00270 report_stats_on_error();
00271 }
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 void ExplicitUpdateImgParams(ExpFrameInfo *info, ImageParameters *p_Img, InputParameters *p_Inp)
00281 {
00282 set_slice_type( p_Img, p_Inp, info->slice_type );
00283 p_Img->frame_no = info->seq_number;
00284 p_Img->nal_reference_idc = info->reference_idc;
00285
00286 p_Img->toppoc = 2 * p_Img->frame_no;
00287 p_Img->bottompoc = p_Img->toppoc + 1;
00288 p_Img->framepoc = imin (p_Img->toppoc, p_Img->bottompoc);
00289
00290
00291 p_Img->delta_pic_order_cnt[0] = 0;
00292 p_Img->delta_pic_order_cnt[1] = 0;
00293
00294 p_Img->number ++;
00295 p_Img->gop_number = (p_Img->number - p_Img->start_frame_no);
00296 p_Img->frm_number = p_Img->number;
00297
00298 p_Img->frm_no_in_file = p_Img->frame_no;
00299 }
00300
00301
00302
00303
00304
00305
00306
00307 void OpenExplicitSeqFile(ImageParameters *p_Img, InputParameters *p_Inp)
00308 {
00309 int frm_count = 0;
00310 p_Img->expSFile = fopen(p_Inp->ExplicitSeqFile, "r");
00311 if (p_Img->expSFile == NULL)
00312 {
00313 printf("ERROR while opening the explicit sequence information file.\n");
00314 report_stats_on_error();
00315 }
00316
00317 if (ReadTextField(p_Img->expSFile, "Sequence") == -1)
00318 {
00319 printf("Sequence info file is of invalid format. Terminating\n");
00320 report_stats_on_error();
00321 }
00322 else
00323 {
00324 ReadIntField (p_Img->expSFile, "%s : %d", "FrameCount", &frm_count);
00325 if (frm_count > 0)
00326 {
00327 p_Img->expSeq = (ExpSeqInfo *) malloc(sizeof(ExpSeqInfo));
00328 p_Img->expSeq->no_frames = frm_count;
00329 p_Img->expSeq->info = (ExpFrameInfo *) calloc(frm_count, sizeof(ExpFrameInfo));
00330 }
00331 else
00332 {
00333 printf("Invalid FrameCount in Sequence info file. Terminating\n");
00334 report_stats_on_error();
00335 }
00336 }
00337 }
00338
00339
00340
00341
00342
00343
00344
00345 void CloseExplicitSeqFile(ImageParameters *p_Img)
00346 {
00347 if (p_Img->expSFile != NULL)
00348 fclose(p_Img->expSFile);
00349 if (p_Img->expSeq != NULL)
00350 {
00351 free(p_Img->expSeq->info);
00352 free(p_Img->expSeq);
00353 }
00354 }
00355
00356