Robustified MJ2 codecs

This commit is contained in:
Francois-Olivier Devaux 2007-08-21 10:18:40 +00:00
parent d9924962ff
commit 1d0dbeb232
6 changed files with 29 additions and 22 deletions

View File

@ -252,7 +252,6 @@ int main(int argc, char **argv)
mj2_parameters.CbCr_subsampling_dx = 2; // CIF default value mj2_parameters.CbCr_subsampling_dx = 2; // CIF default value
mj2_parameters.CbCr_subsampling_dy = 2; // CIF default value mj2_parameters.CbCr_subsampling_dy = 2; // CIF default value
mj2_parameters.frame_rate = 25; mj2_parameters.frame_rate = 25;
/* /*
configure the event callbacks (not required) configure the event callbacks (not required)
setting of each callback is optionnal setting of each callback is optionnal
@ -275,6 +274,8 @@ int main(int argc, char **argv)
sprintf(j2k_parameters->cp_comment,"%s%s", comment, version); sprintf(j2k_parameters->cp_comment,"%s%s", comment, version);
} }
mj2_parameters.decod_format = 0;
mj2_parameters.cod_format = 0;
while (1) { while (1) {
int c = getopt(argc, argv, int c = getopt(argc, argv,
@ -610,7 +611,7 @@ int main(int argc, char **argv)
/* Error messages */ /* Error messages */
/* -------------- */ /* -------------- */
if (!mj2_parameters.infile || !mj2_parameters.outfile) { if (!mj2_parameters.cod_format || !mj2_parameters.decod_format) {
fprintf(stderr, fprintf(stderr,
"Correct usage: mj2_encoder -i yuv-file -o mj2-file (+ options)\n"); "Correct usage: mj2_encoder -i yuv-file -o mj2-file (+ options)\n");
return 1; return 1;
@ -681,8 +682,8 @@ int main(int argc, char **argv)
mj2_setup_encoder(movie, &mj2_parameters); mj2_setup_encoder(movie, &mj2_parameters);
movie->tk[0].num_samples = yuv_num_frames(&movie->tk[0],mj2_parameters.infile); movie->tk[0].num_samples = yuv_num_frames(&movie->tk[0],mj2_parameters.infile);
if (!movie->tk[0].num_samples) { if (movie->tk[0].num_samples == -1) {
fprintf(stderr,"Unable to count the number of frames in YUV input file\n"); return 1;
} }
// One sample per chunk // One sample per chunk
@ -705,6 +706,7 @@ int main(int argc, char **argv)
cio_write(cio, MJ2_MDAT, 4); cio_write(cio, MJ2_MDAT, 4);
fwrite(buf,cio_tell(cio),1,mj2file); fwrite(buf,cio_tell(cio),1,mj2file);
offset = cio_tell(cio); offset = cio_tell(cio);
opj_cio_close(cio);
opj_free(buf); opj_free(buf);
for (i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++) { for (i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++) {
@ -727,7 +729,7 @@ int main(int argc, char **argv)
for (sampleno = 0; sampleno < numframes; sampleno++) { for (sampleno = 0; sampleno < numframes; sampleno++) {
double init_time = opj_clock(); double init_time = opj_clock();
double elapsed_time; double elapsed_time;
if (!yuvtoimage(tk, img, sampleno, j2k_parameters, mj2_parameters.infile)) { if (yuvtoimage(tk, img, sampleno, j2k_parameters, mj2_parameters.infile)) {
fprintf(stderr, "Error with frame number %d in YUV file\n", sampleno); fprintf(stderr, "Error with frame number %d in YUV file\n", sampleno);
return 1; return 1;
} }
@ -751,6 +753,7 @@ int main(int argc, char **argv)
len = cio_tell(cio) - 8; len = cio_tell(cio) - 8;
cio_seek(cio, 0); cio_seek(cio, 0);
cio_write(cio, len+8,4); cio_write(cio, len+8,4);
opj_cio_close(cio);
tk->sample[sampleno].sample_size = len+8; tk->sample[sampleno].sample_size = len+8;
tk->sample[sampleno].offset = offset; tk->sample[sampleno].offset = offset;
tk->chunk[sampleno].offset = offset; // There is one sample per chunk tk->chunk[sampleno].offset = offset; // There is one sample per chunk
@ -784,6 +787,7 @@ int main(int argc, char **argv)
cio = opj_cio_open(movie->cinfo, buf, (TEMP_BUF+numframes*20)); cio = opj_cio_open(movie->cinfo, buf, (TEMP_BUF+numframes*20));
mj2_write_moov(movie, cio); mj2_write_moov(movie, cio);
fwrite(buf,cio_tell(cio),1,mj2file); fwrite(buf,cio_tell(cio),1,mj2file);
opj_free(buf);
fprintf(stdout,"Total encoding time: %.2f s for %d frames (%.1f fps)\n", total_time, numframes, (float)numframes/total_time); fprintf(stdout,"Total encoding time: %.2f s for %d frames (%.1f fps)\n", total_time, numframes, (float)numframes/total_time);

View File

@ -2081,7 +2081,7 @@ void mj2_write_mdhd(mj2_tk_t * tk, opj_cio_t *cio)
cio_write(cio, tk->creation_time, 4); /* Creation Time */ cio_write(cio, tk->creation_time, 4); /* Creation Time */
time(&ltime); /* Time since 1/1/70 */ time(&ltime); /* Time since 1/1/70 */
modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */ modification_time = (unsigned int)ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
cio_write(cio, modification_time, 4); /* Modification Time */ cio_write(cio, modification_time, 4); /* Modification Time */
@ -2224,7 +2224,7 @@ void mj2_write_tkhd(mj2_tk_t * tk, opj_cio_t *cio)
cio_write(cio, 3, 4); /* Version=0, flags=3 */ cio_write(cio, 3, 4); /* Version=0, flags=3 */
time(&ltime); /* Time since 1/1/70 */ time(&ltime); /* Time since 1/1/70 */
tk->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */ tk->modification_time = (unsigned int)ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
cio_write(cio, tk->creation_time, 4); /* Creation Time */ cio_write(cio, tk->creation_time, 4); /* Creation Time */
@ -2416,7 +2416,7 @@ void mj2_write_mvhd(opj_mj2_t * movie, opj_cio_t *cio)
cio_write(cio, 0, 4); /* Version = 0, flags = 0 */ cio_write(cio, 0, 4); /* Version = 0, flags = 0 */
time(&ltime); /* Time since 1/1/70 */ time(&ltime); /* Time since 1/1/70 */
movie->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */ movie->modification_time = (unsigned int)ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
cio_write(cio, movie->creation_time, 4); /* Creation Time */ cio_write(cio, movie->creation_time, 4); /* Creation Time */
@ -2748,8 +2748,10 @@ void mj2_setup_decoder(opj_mj2_t *movie, mj2_dparameters_t *mj2_parameters) {
movie->num_vtk=0; movie->num_vtk=0;
movie->num_stk=0; movie->num_stk=0;
movie->num_htk=0; movie->num_htk=0;
/* setup the J2K decoder parameters */ /* setup the J2K decoder parameters */
j2k_setup_decoder(movie->cinfo->j2k_handle, &mj2_parameters->j2k_parameters); j2k_setup_decoder(movie->cinfo->j2k_handle, &mj2_parameters->j2k_parameters);
} }
void mj2_destroy_decompress(opj_mj2_t *movie) { void mj2_destroy_decompress(opj_mj2_t *movie) {
@ -2757,9 +2759,8 @@ void mj2_destroy_decompress(opj_mj2_t *movie) {
int i; int i;
mj2_tk_t *tk=NULL; mj2_tk_t *tk=NULL;
if (movie->cinfo->j2k_handle) { if (movie->cinfo->j2k_handle)
j2k_destroy_compress(movie->j2k); j2k_destroy_compress(movie->j2k);
}
if (movie->num_cl != 0) if (movie->num_cl != 0)
opj_free(movie->cl); opj_free(movie->cl);

View File

@ -80,13 +80,13 @@
Decompressed format used in parameters Decompressed format used in parameters
YUV = 0 YUV = 0
*/ */
#define YUV_DFMT 0 #define YUV_DFMT 1
/** /**
Compressed format used in parameters Compressed format used in parameters
MJ2 = 0 MJ2 = 0
*/ */
#define MJ2_CFMT 0 #define MJ2_CFMT 2
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */

View File

@ -46,7 +46,7 @@ int yuv_num_frames(mj2_tk_t * tk, char *infile)
f = fopen(infile,"rb"); f = fopen(infile,"rb");
if (!f) { if (!f) {
fprintf(stderr, "failed to open %s for reading\n",infile); fprintf(stderr, "failed to open %s for reading\n",infile);
return 1; return -1;
} }
frame_size = (int) (tk->w * tk->h * (1.0 + (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy))); /* Calculate frame size */ frame_size = (int) (tk->w * tk->h * (1.0 + (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy))); /* Calculate frame size */
@ -58,7 +58,7 @@ int yuv_num_frames(mj2_tk_t * tk, char *infile)
fprintf(stderr, fprintf(stderr,
"YUV does not contains any frame of %d x %d size\n", tk->w, "YUV does not contains any frame of %d x %d size\n", tk->w,
tk->h); tk->h);
return 0; return -1;
} }
numimages = end_of_f / frame_size; /* Calculate number of images */ numimages = end_of_f / frame_size; /* Calculate number of images */
@ -99,7 +99,7 @@ opj_image_t *mj2_image_create(mj2_tk_t * tk, opj_cparameters_t *parameters)
return img; return img;
} }
bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile) char yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile)
{ {
int i, compno; int i, compno;
int offset; int offset;
@ -111,8 +111,8 @@ bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters
yuvfile = fopen(infile,"rb"); yuvfile = fopen(infile,"rb");
if (!yuvfile) { if (!yuvfile) {
fprintf(stderr, "failed to open %s for reading\n",parameters->infile); fprintf(stderr, "failed to open %s for readings\n",parameters->infile);
return false; return 1;
} }
offset = (int) ((double) (frame_num * tk->w * tk->h) * (1.0 + offset = (int) ((double) (frame_num * tk->w * tk->h) * (1.0 +
@ -125,7 +125,7 @@ bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters
fprintf(stderr, "Cannot reach frame number %d in yuv file !!\n", fprintf(stderr, "Cannot reach frame number %d in yuv file !!\n",
frame_num); frame_num);
fclose(yuvfile); fclose(yuvfile);
return false; return 1;
} }
img->x0 = tk->Dim[0]; img->x0 = tk->Dim[0];
@ -140,13 +140,13 @@ bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters
&& !feof(yuvfile); i++) { && !feof(yuvfile); i++) {
if (!fread(&img->comps[compno].data[i], 1, 1, yuvfile)) { if (!fread(&img->comps[compno].data[i], 1, 1, yuvfile)) {
fprintf(stderr, "Error reading %s file !!\n", infile); fprintf(stderr, "Error reading %s file !!\n", infile);
return false; return 1;
} }
} }
} }
fclose(yuvfile); fclose(yuvfile);
return true; return 0;
} }

View File

@ -37,7 +37,7 @@ int imagetobmp(opj_image_t * img, char *outfile);
opj_image_t *mj2_image_create(mj2_tk_t * tk, opj_cparameters_t *parameters); opj_image_t *mj2_image_create(mj2_tk_t * tk, opj_cparameters_t *parameters);
bool yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile); char yuvtoimage(mj2_tk_t * tk, opj_image_t * img, int frame_num, opj_cparameters_t *parameters, char* infile);
int yuv_num_frames(mj2_tk_t * tk, char *infile); int yuv_num_frames(mj2_tk_t * tk, char *infile);

View File

@ -145,6 +145,7 @@ int main(int argc, char *argv[]) {
{ {
double init_time = opj_clock(); double init_time = opj_clock();
double elapsed_time; double elapsed_time;
sample = &track->sample[snum]; sample = &track->sample[snum];
if (sample->sample_size-8 > max_codstrm_size) { if (sample->sample_size-8 > max_codstrm_size) {
max_codstrm_size = sample->sample_size-8; max_codstrm_size = sample->sample_size-8;
@ -203,6 +204,7 @@ int main(int argc, char *argv[]) {
free(frame_codestream); free(frame_codestream);
fclose(file); fclose(file);
/* free remaining structures */ /* free remaining structures */
if(dinfo) { if(dinfo) {
mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle); mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);