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