From 43a6b9234514ca7b3d8715ffd9aca39bc762bd54 Mon Sep 17 00:00:00 2001 From: Francois-Olivier Devaux Date: Wed, 8 Dec 2004 11:22:18 +0000 Subject: [PATCH] New modular version of the MJ2 library. The goal of this new version is to make the library more independent from OpenJPEG --- mj2/frames_to_mj2.c | 403 ++++++++++++++++++++++++------------ mj2/frames_to_mj2.dsp | 147 +++++++------- mj2/mj2.c | 460 +++++++++++------------------------------- mj2/mj2.h | 18 +- mj2/mj2_convert.c | 251 ++++++++++++++--------- mj2/mj2_convert.h | 39 +--- mj2/mj2_to_frames.c | 223 ++++++++++---------- mj2/mj2_to_frames.dsp | 149 +++++++------- 8 files changed, 831 insertions(+), 859 deletions(-) diff --git a/mj2/frames_to_mj2.c b/mj2/frames_to_mj2.c index 74d2a33e..fbc42fa3 100644 --- a/mj2/frames_to_mj2.c +++ b/mj2/frames_to_mj2.c @@ -30,20 +30,35 @@ #include #include #include +#include #ifndef DONT_HAVE_GETOPT #include #else #include "compat/getopt.h" #endif + #include "mj2_convert.h" +#define MJ2_MDAT 0x6d646174 +#define JP2_JP2C 0x6a703263 + +//MEMORY LEAK +#ifdef _DEBUG +#define _CRTDBG_MAP_ALLOC +#include // Must be included first +#include +#endif +//MEM + +jmp_buf j2k_error; + void help_display() { fprintf(stdout,"HELP\n----\n\n"); fprintf(stdout,"- the -h option displays this help information on screen\n\n"); - - - fprintf(stdout,"List of parameters for the coder JPEG 2000:\n"); + + + fprintf(stdout,"List of parameters for the MJ2 encoder:\n"); fprintf(stdout,"\n"); fprintf(stdout,"REMARKS:\n"); fprintf(stdout,"---------\n"); @@ -84,53 +99,37 @@ void help_display() fprintf(stdout,"\n"); fprintf (stdout,"Required Parameters (except with -h):\n"); - fprintf(stdout,"\n"); fprintf (stdout,"-i : source file (-i source.yuv) \n"); - fprintf(stdout,"\n"); fprintf (stdout,"-o : destination file (-o dest.mj2) \n"); - fprintf(stdout,"\n"); fprintf (stdout,"Optional Parameters:\n"); - fprintf(stdout,"\n"); - fprintf(stdout,"-h : display the help information \n "); - fprintf(stdout,"\n"); + fprintf(stdout,"-h : display the help information \n"); fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n "); fprintf(stdout," - The rate specified for each quality level is the desired \n"); fprintf(stdout," compression factor.\n"); fprintf(stdout," Example: -r 20,10,1 means quality 1: compress 20x, \n"); fprintf(stdout," quality 2: compress 10x and quality 3: compress lossless\n"); - fprintf(stdout,"\n"); fprintf(stdout," (options -r and -q cannot be used together)\n "); - fprintf(stdout,"\n"); - + fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n "); - + fprintf(stdout," (options -r and -q cannot be used together)\n "); - - fprintf(stdout,"\n"); + fprintf(stdout,"-n : number of resolutions (-n 3) \n"); - fprintf(stdout,"\n"); fprintf(stdout,"-b : size of code block (-b 32,32) \n"); - fprintf(stdout,"\n"); fprintf(stdout,"-c : size of precinct (-c 128,128) \n"); - fprintf(stdout,"\n"); fprintf(stdout,"-t : size of tile (-t 512,512) \n"); - fprintf(stdout,"\n"); fprintf (stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n"); - fprintf(stdout,"\n"); fprintf (stdout,"-s : subsampling factor (-s 2,2) [-s X,Y] \n"); fprintf(stdout," Remark: subsampling bigger than 2 can produce error\n"); - fprintf(stdout,"\n"); fprintf (stdout,"-SOP : write SOP marker before each packet \n"); - fprintf(stdout,"\n"); fprintf (stdout,"-EPH : write EPH marker after each header packet \n"); - fprintf(stdout,"\n"); fprintf (stdout,"-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n"); fprintf @@ -139,31 +138,24 @@ void help_display() (stdout," Indicate multiple modes by adding their values. \n"); fprintf (stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n"); - fprintf(stdout,"\n"); fprintf (stdout,"-x : create an index file *.Idx (-x index_name.Idx) \n"); - fprintf(stdout,"\n"); fprintf (stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n"); fprintf (stdout," for component c=%%d [%%d = 0,1,2]\n"); fprintf (stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n"); - fprintf(stdout,"\n"); fprintf (stdout,"-d : offset of the origin of the image (-d 150,300) \n"); - fprintf(stdout,"\n"); fprintf (stdout,"-T : offset of the origin of the tiles (-T 100,75) \n"); - fprintf(stdout,"\n"); fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n"); - fprintf(stdout,"\n"); fprintf(stdout,"-W : image width, height and the dx and dy subsampling \n"); fprintf(stdout," of the Cb and Cr components for YUV files \n"); fprintf(stdout," (default is '352,288,2,2' for CIF format's 352x288 and 4:2:0)\n"); - fprintf(stdout,"\n"); fprintf(stdout,"-F : video frame rate (set to 25 by default)\n"); - + fprintf(stdout,"\n"); fprintf(stdout,"IMPORTANT:\n"); fprintf(stdout,"-----------\n"); @@ -188,32 +180,32 @@ void help_display() fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n"); fprintf(stdout,"...\n"); fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n"); - + fprintf(stdout,"MaxDisto\n"); - + fprintf(stdout,"TotalDisto\n\n"); } int give_progression(char progression[4]) { if (progression[0] == 'L' && progression[1] == 'R' - && progression[2] == 'C' && progression[3] == 'P') { + && progression[2] == 'C' && progression[3] == 'P') { return 0; } else { if (progression[0] == 'R' && progression[1] == 'L' - && progression[2] == 'C' && progression[3] == 'P') { + && progression[2] == 'C' && progression[3] == 'P') { return 1; } else { if (progression[0] == 'R' && progression[1] == 'P' - && progression[2] == 'C' && progression[3] == 'L') { + && progression[2] == 'C' && progression[3] == 'L') { return 2; } else { if (progression[0] == 'P' && progression[1] == 'C' - && progression[2] == 'R' && progression[3] == 'L') { + && progression[2] == 'R' && progression[3] == 'L') { return 3; } else { if (progression[0] == 'C' && progression[1] == 'P' - && progression[2] == 'R' && progression[3] == 'L') { + && progression[2] == 'R' && progression[3] == 'L') { return 4; } else { return -1; @@ -255,15 +247,15 @@ void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec) numbands = 3 * tccp->numresolutions - 2; for (bandno = 0; bandno < numbands; bandno++) { double stepsize; - + int resno, level, orient, gain; resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1; orient = bandno == 0 ? 0 : (bandno - 1) % 3 + 1; level = tccp->numresolutions - 1 - resno; gain = tccp->qmfbid == 0 ? 0 : (orient == - 0 ? 0 : (orient == 1 - || orient == 2 ? 1 : 2)); + 0 ? 0 : (orient == 1 + || orient == 2 ? 1 : 2)); if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) { stepsize = 1.0; } else { @@ -299,11 +291,12 @@ int main(int argc, char **argv) j2k_poc_t POC[32]; /* POC : used in case of Progression order change */ j2k_poc_t *tcp_poc; j2k_tccp_t *tccp; - int i, tileno, j; + int i, tileno, l, j; char *infile = 0; char *outfile = 0; char *index = 0; char *s, S1, S2, S3; + char *buf; int ir = 0; int res_spec = 0; /* For various precinct sizes specification */ char sep; @@ -315,9 +308,12 @@ int main(int argc, char **argv) int frame_rate; /* Video Frame Rate */ int numcomps; /* In YUV files, numcomps always considered as 3 */ int prec; /* In YUV files, precision always considered as 8 */ - int x1, y1; - - + int x1, y1, len, jp2c_initpos, m, k, pos; + long mdat_initpos, offset; + FILE *mj2file, *yuvfile; + unsigned int sampleno; + j2k_image_t img; + /* default value */ /* ------------- */ NumResolution = 6; @@ -347,18 +343,18 @@ int main(int argc, char **argv) CbCr_subsampling_dx = 2; // CIF default value CbCr_subsampling_dy = 2; // CIF default value frame_rate = 25; - - + + Tile_arg = 0; cp_init.tcps = (j2k_tcp_t *) malloc(sizeof(j2k_tcp_t)); /* initialisation if only one tile */ tcp_init = &cp_init.tcps[0]; tcp_init->numlayers = 0; - + cp.intermed_file = 0; // Don't store each tile in a file during processing - + while (1) { int c = getopt(argc, argv, - "i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I:W:F:"); + "i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I:W:F:"); if (c == -1) break; switch (c) { @@ -374,15 +370,15 @@ int main(int argc, char **argv) S2 = *s; s--; S1 = *s; - + if ((S1 == 'y' && S2 == 'u' && S3 == 'v') - || (S1 == 'Y' && S2 == 'U' && S3 == 'V')) { + || (S1 == 'Y' && S2 == 'U' && S3 == 'V')) { cp.image_type = 3; break; } fprintf(stderr, - "!! Unrecognized format for infile : %c%c%c [accept only *.yuv] !!\n\n", - S1, S2, S3); + "!! Unrecognized format for infile : %c%c%c [accept only *.yuv] !!\n\n", + S1, S2, S3); return 1; break; /* ----------------------------------------------------- */ @@ -397,27 +393,27 @@ int main(int argc, char **argv) S2 = *outfile; outfile--; S1 = *outfile; - + outfile = optarg; - + if ((S1 == 'm' && S2 == 'j' && S3 == '2') - || (S1 == 'M' && S2 == 'J' && S3 == '2')) + || (S1 == 'M' && S2 == 'J' && S3 == '2')) cp.JPEG2000_format = 2; else { fprintf(stderr, - "Unknown output format image *.%c%c%c [only *.mj2]!! \n", - S1, S2, S3); + "Unknown output format image *.%c%c%c [only *.mj2]!! \n", + S1, S2, S3); return 1; } - - - + + + break; /* ----------------------------------------------------- */ case 'r': /* rates rates/distorsion */ s = optarg; while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers]) - == 1) { + == 1) { tcp_init->numlayers++; while (*s && *s != ',') { s++; @@ -433,7 +429,7 @@ int main(int argc, char **argv) case 'q': /* add fixed_quality */ s = optarg; while (sscanf - (s, "%f", &tcp_init->distoratio[tcp_init->numlayers]) == 1) { + (s, "%f", &tcp_init->distoratio[tcp_init->numlayers]) == 1) { tcp_init->numlayers++; while (*s && *s != ',') { s++; @@ -455,7 +451,7 @@ int main(int argc, char **argv) s++; cp.matrice = (int *) malloc(tcp_init->numlayers * NumResolution * 3 * - sizeof(int)); + sizeof(int)); s = s + 2; for (i = 0; i < tcp_init->numlayers; i++) { tcp_init->rates[i] = 1; @@ -467,9 +463,9 @@ int main(int argc, char **argv) cp.matrice[i * NumResolution * 3 + 2] = 0; for (j = 1; j < NumResolution; j++) { sscanf(s, "%d,%d,%d", - &cp.matrice[i * NumResolution * 3 + j * 3 + 0], - &cp.matrice[i * NumResolution * 3 + j * 3 + 1], - &cp.matrice[i * NumResolution * 3 + j * 3 + 2]); + &cp.matrice[i * NumResolution * 3 + j * 3 + 0], + &cp.matrice[i * NumResolution * 3 + j * 3 + 1], + &cp.matrice[i * NumResolution * 3 + j * 3 + 2]); s += 6; if (cp.matrice[i * NumResolution * 3 + j * 3] > 9) s++; @@ -498,7 +494,7 @@ int main(int argc, char **argv) do { sep = 0; sscanf(s, "[%d,%d]%c", &prcw_init[res_spec], - &prch_init[res_spec], &sep); + &prch_init[res_spec], &sep); CSty |= 0x01; res_spec++; s = strpbrk(s, "]") + 2; @@ -508,9 +504,9 @@ int main(int argc, char **argv) case 'b': /* code-block dimension */ sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init); if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024 - || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) { + || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) { fprintf(stderr, - "!! Size of code_block error (option -b) !!\n\nRestriction :\n * width*height<=4096\n * 4<=width,height<= 1024\n\n"); + "!! Size of code_block error (option -b) !!\n\nRestriction :\n * width*height<=4096\n * 4<=width,height<= 1024\n\n"); return 1; } break; @@ -527,19 +523,19 @@ int main(int argc, char **argv) s++; } Prog_order = give_progression(progression); - + if (Prog_order == -1) { fprintf(stderr, - "Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n"); + "Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n"); return 1; } break; /* ----------------------------------------------------- */ case 's': /* subsampling factor */ if (sscanf(optarg, "%d,%d", &subsampling_dx, &subsampling_dy) - != 2) { + != 2) { fprintf(stderr, - "'-s' sub-sampling argument error ! [-s dx,dy]\n"); + "'-s' sub-sampling argument error ! [-s dx,dy]\n"); return 1; } break; @@ -547,7 +543,7 @@ int main(int argc, char **argv) case 'd': /* coordonnate of the reference grid */ if (sscanf(optarg, "%d,%d", &Dim[0], &Dim[1]) != 2) { fprintf(stderr, - "-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n"); + "-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n"); return 1; } break; @@ -561,12 +557,12 @@ int main(int argc, char **argv) fprintf(stderr, "/----------------------------------\\\n"); fprintf(stderr, "| POC option not fully tested !! |\n"); fprintf(stderr, "\\----------------------------------/\n"); - + s = optarg; while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile, - &POC[numpocs].resno0, &POC[numpocs].compno0, - &POC[numpocs].layno1, &POC[numpocs].resno1, - &POC[numpocs].compno1, POC[numpocs].progorder) == 7) { + &POC[numpocs].resno0, &POC[numpocs].compno0, + &POC[numpocs].layno1, &POC[numpocs].resno1, + &POC[numpocs].compno1, POC[numpocs].progorder) == 7) { POC[numpocs].prg = give_progression(POC[numpocs].progorder); /* POC[numpocs].tile; */ numpocs++; @@ -621,8 +617,8 @@ int main(int argc, char **argv) /* ------------------------------------------------------ */ case 'W': /* Width and Height and Cb and Cr subsampling in case of YUV format files */ if (sscanf - (optarg, "%d,%d,%d,%d", &w, &h, &CbCr_subsampling_dx, - &CbCr_subsampling_dy) != 4) { + (optarg, "%d,%d,%d,%d", &w, &h, &CbCr_subsampling_dx, + &CbCr_subsampling_dy) != 4) { fprintf(stderr, "-W argument error"); return 1; } @@ -639,58 +635,58 @@ int main(int argc, char **argv) return 1; } } - + cp.tx0 = TX0; cp.ty0 = TY0; - + /* Error messages */ /* -------------- */ if (!infile || !outfile) { fprintf(stderr, - "usage: frames_to_mj2 -i yuv-file -o mj2-file (+ options)\n"); + "Correct usage: mj2_encoder -i yuv-file -o mj2-file (+ options)\n"); return 1; } - + if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality) - && (!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) { + && (!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) { fprintf(stderr, - "Error: options -r -q and -f can not be used together !!\n"); + "Error: options -r -q and -f can not be used together !!\n"); return 1; } // mod fixed_quality - + /* if no rate entered, lossless by default */ if (tcp_init->numlayers == 0) { tcp_init->rates[tcp_init->numlayers] = 0; //MOD antonin : losslessbug tcp_init->numlayers++; cp.disto_alloc = 1; } - + if (TX0 > Dim[0] || TY0 > Dim[1]) { fprintf(stderr, - "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n", - TX0, Dim[0], TY0, Dim[1]); + "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n", + TX0, Dim[0], TY0, Dim[1]); return 1; } - + for (i = 0; i < numpocs; i++) { if (POC[i].prg == -1) { fprintf(stderr, - "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n", - i + 1); + "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n", + i + 1); } } - + /* to respect profile - 0 */ /* ---------------------- */ numD_min = 0; - + x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w - - 1) * + 1) * subsampling_dx + 1; y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h - - 1) * + 1) * subsampling_dy + 1; - + if (Tile_arg == 1) { cp.tw = int_ceildiv(x1 - cp.tx0, cp.tdx); cp.th = int_ceildiv(y1 - cp.ty0, cp.tdy); @@ -698,24 +694,24 @@ int main(int argc, char **argv) cp.tdx = x1 - cp.tx0; cp.tdy = y1 - cp.ty0; } - - + + /* Initialization for PPM marker */ cp.ppm = 0; cp.ppm_data = NULL; cp.ppm_previous = 0; cp.ppm_store = 0; - + numcomps = 3; /* Because YUV files only have 3 components */ - + tcp_init->mct = 0; /* No component transform needed */ - + prec = 8; /* Because in YUV files, components have 8-bit depth */ - + /* Init the mutiple tiles */ /* ---------------------- */ cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t)); - + for (tileno = 0; tileno < cp.tw * cp.th; tileno++) { tcp = &cp.tcps[tileno]; tcp->numlayers = tcp_init->numlayers; @@ -731,7 +727,7 @@ int main(int argc, char **argv) tcp->ppt = 0; tcp->ppt_data = NULL; tcp->ppt_store = 0; - + numpocs_tile = 0; tcp->POC = 0; if (numpocs) { @@ -753,7 +749,7 @@ int main(int argc, char **argv) } tcp->numpocs = numpocs_tile; tcp->tccps = (j2k_tccp_t *) malloc(numcomps * sizeof(j2k_tccp_t)); - + for (i = 0; i < numcomps; i++) { tccp = &tcp->tccps[i]; tccp->csty = CSty & 0x01; /* 0 => one precinct || 1 => custom precinct */ @@ -776,7 +772,7 @@ int main(int argc, char **argv) tccp->prcw[j] = 1; else tccp->prcw[j] = int_floorlog2(prcw_init[p]); - + if (prch_init[p] < 1) tccp->prch[j] = 1; else @@ -796,7 +792,7 @@ int main(int argc, char **argv) } p++; /*printf("\nsize precinct pour level %d : %d,%d\n", j, - tccp->prcw[j], tccp->prch[j]); */ + tccp->prcw[j], tccp->prch[j]); */ } } else { for (j = 0; j < tccp->numresolutions; j++) { @@ -807,18 +803,21 @@ int main(int argc, char **argv) calc_explicit_stepsizes(tccp, prec); } } - + + mj2file = fopen(outfile, "wb"); + + if (!mj2file) { + fprintf(stderr, "failed to open %s for writing\n", argv[2]); + return 1; + } + movie.tk = (mj2_tk_t *) malloc(sizeof(mj2_tk_t)); movie.num_vtk = 1; movie.num_stk = 0; movie.num_htk = 0; - - movie.tk[0].imagefile = infile; + movie.tk[0].track_type = 0; // Video Track movie.tk[0].track_ID = 1; - movie.mj2file = outfile; - movie.tk[0].subsampling_dx = subsampling_dx; - movie.tk[0].subsampling_dy = subsampling_dy; movie.tk[0].Dim[0] = Dim[0]; movie.tk[0].Dim[1] = Dim[1]; movie.tk[0].w = w; @@ -826,24 +825,174 @@ int main(int argc, char **argv) movie.tk[0].CbCr_subsampling_dx = CbCr_subsampling_dx; movie.tk[0].CbCr_subsampling_dy = CbCr_subsampling_dy; movie.tk[0].sample_rate = frame_rate; - + movie.tk[0].jp2_struct.numcomps = 3; // NC + jp2_init_stdjp2(&movie.tk[0].jp2_struct); + + movie.tk[0].jp2_struct.w = w; + movie.tk[0].jp2_struct.h = h; + movie.tk[0].jp2_struct.bpc = 7; + movie.tk[0].jp2_struct.meth = 1; + movie.tk[0].jp2_struct.enumcs = 18; // YUV + + + yuvfile = fopen(infile,"rb"); + if (!yuvfile) { + fprintf(stderr, "failed to open %s for reading\n",infile); + return 1; + } + + movie.tk[0].num_samples = yuv_num_frames(&movie.tk[0],yuvfile); + if (!movie.tk[0].num_samples) { + fprintf(stderr,"Unable to count the number of frames in YUV input file\n"); + } + + // One sample per chunk + movie.tk[0].chunk = (mj2_chunk_t*) malloc(movie.tk[0].num_samples * sizeof(mj2_chunk_t)); + movie.tk[0].sample = (mj2_sample_t*) malloc(movie.tk[0].num_samples * sizeof(mj2_sample_t)); + if (mj2_init_stdmovie(&movie)) { fprintf(stderr, "Error with movie initialization"); return 1; - }; - if (mj2_encode(&movie, &cp, index)) { - fprintf(stderr, "Error encoding movie"); - return 1; - }; + }; + + // Writing JP, FTYP and MDAT boxes + buf = (char*) malloc (300 * sizeof(char)); // Assuming that the JP and FTYP + // boxes won't be longer than 300 bytes + cio_init(buf , 300); + mj2_write_jp(); + mj2_write_ftyp(&movie); + mdat_initpos = cio_tell(); + cio_skip(4); + cio_write(MJ2_MDAT, 4); + fwrite(buf,cio_tell(),1,mj2file); + offset = cio_tell(); + free(buf); + + for (i = 0; i < movie.num_stk + movie.num_htk + movie.num_vtk; i++) { + if (movie.tk[i].track_type != 0) { + fprintf(stderr, "Unable to write sound or hint tracks\n"); + } else { + //j2k_cp_t cp_init; + mj2_tk_t *tk; + + tk = &movie.tk[i]; + + //tk->sample = + //(mj2_sample_t *) malloc(tk->num_samples * sizeof(mj2_sample_t)); + tk->num_chunks = tk->num_samples; + //tk->chunk = + //(mj2_chunk_t *) malloc(tk->num_chunks * sizeof(mj2_chunk_t)); + + fprintf(stderr, "Video Track number %d\n", i + 1); + + // Copy the first tile coding parameters (tcp) to cp_init + + //cp_init.tcps = + //(j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t)); + for (tileno = 0; tileno < cp.tw * cp.th; tileno++) { + for (l = 0; l < cp.tcps[tileno].numlayers; l++) { + cp_init.tcps[tileno].rates[l] = cp.tcps[tileno].rates[l]; + //tileno = cp.tcps[tileno].rates[l]; + } + } + + for (sampleno = 0; sampleno < tk->num_samples; sampleno++) { + buf = (char *) malloc(cp.tdx * cp.tdy * cp.th * cp.tw * 2); + cio_init(buf, cp.tdx * cp.tdy * cp.th * cp.tw * 2); + + fprintf(stderr, "Frame number %d/%d: ", sampleno + 1, tk->num_samples); + + + if (!yuvtoimage(yuvfile, tk, &img, sampleno, subsampling_dx, subsampling_dy)) { + fprintf(stderr, "Error with frame number %d in YUV file\n", sampleno); + return 1; + } + + jp2c_initpos = cio_tell(); + cio_skip(4); + cio_write(JP2_JP2C, 4); // JP2C + + len = j2k_encode(&img, &cp, buf, cp.tdx * cp.tdy * 2, index); + if (len ==0) { + fprintf(stderr,"Unable to encode image"); + return 1; + } + + cio_seek(jp2c_initpos); + cio_write(len+8,4); + + for (m = 0; m < img.numcomps; m++) { + free(img.comps[m].data); + } + free(img.comps); + + tk->sample[sampleno].sample_size = len+8; + + tk->sample[sampleno].offset = offset; + tk->chunk[sampleno].offset = offset; // There is one sample per chunk + + fwrite(buf, 1, len+8, mj2file); + + offset += len+8; + + free(buf); + + // Copy the cp_init parameters to cp.tcps + + for (tileno = 0; tileno < cp.tw * cp.th; tileno++) { + for (k = 0; k < cp.tcps[tileno].numlayers; k++) { + cp.tcps[tileno].rates[k] = cp_init.tcps[tileno].rates[k]; + } + } + } + } + } + + fseek(mj2file, mdat_initpos, SEEK_SET); + + buf = (char*) malloc(4*sizeof(char)); + + cio_init(buf, 4); // Init a cio to write box length variable in a little endian way + cio_write(offset - mdat_initpos, 4); + fwrite(buf, 4, 1, mj2file); + fseek(mj2file,0,SEEK_END); + free(buf); + + // Writing MOOV box + i=1; + buf = (char*) malloc (10000 * sizeof(char)); + cio_init(buf , i*10000); + if (setjmp(j2k_error)) { + i++; + realloc(buf,i*10000* sizeof(char)); + pos = cio_tell(); + cio_init(buf , i*10000); + cio_seek(pos); + } + mj2_write_moov(&movie); + fwrite(buf,cio_tell(),1,mj2file); + + // Ending program mj2_memory_free(&movie); - - /* Free cp memory */ free(cp_init.tcps); if (tcp_init->numlayers > 9) free(cp.matrice); for (tileno = 0; tileno < cp.tw * cp.th; tileno++) free(cp.tcps[tileno].tccps); - free(cp.tcps); - + free(cp.tcps); + fclose(mj2file); + free(buf); + unlink("Compo0"); + unlink("Compo1"); + unlink("Compo2"); + + //MEMORY LEAK + #ifdef _DEBUG + _CrtDumpMemoryLeaks(); + #endif + //MEM + return 0; } + + diff --git a/mj2/frames_to_mj2.dsp b/mj2/frames_to_mj2.dsp index 56c8dfd7..d1f03c22 100644 --- a/mj2/frames_to_mj2.dsp +++ b/mj2/frames_to_mj2.dsp @@ -32,48 +32,47 @@ RSC=rc.exe # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 0 -# PROP BASE Output_Dir "Release" -# PROP BASE Intermediate_Dir "Release" +# PROP BASE Output_Dir "frames_to_mj2___Win32_Release" +# PROP BASE Intermediate_Dir "frames_to_mj2___Win32_Release" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 0 -# PROP Output_Dir "Release" -# PROP Intermediate_Dir "Release" +# PROP Output_Dir "frames_to_mj2___Win32_Release" +# PROP Intermediate_Dir "frames_to_mj2___Win32_Release" # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c -# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg" /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /c -# ADD BASE RSC /l 0x80c /d "NDEBUG" -# ADD RSC /l 0x80c /i "../libopenjpeg" /d "NDEBUG" +# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c +# ADD BASE RSC /l 0x809 /d "NDEBUG" +# ADD RSC /l 0x809 /d "NDEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe -# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 -# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 !ELSEIF "$(CFG)" == "frames_to_mj2 - Win32 Debug" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 1 -# PROP BASE Output_Dir "Debug" -# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Output_Dir "frames_to_mj2___Win32_Debug" +# PROP BASE Intermediate_Dir "frames_to_mj2___Win32_Debug" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 1 -# PROP Output_Dir "Debug" -# PROP Intermediate_Dir "Debug" -# PROP Ignore_Export_Lib 0 +# PROP Output_Dir "frames_to_mj2___Win32_Debug" +# PROP Intermediate_Dir "frames_to_mj2___Win32_Debug" # PROP Target_Dir "" -# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c -# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "_DEBUG" /D "DONT_HAVE_GETOPT" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /FR /YX /FD /GZ /c -# ADD BASE RSC /l 0x80c /d "_DEBUG" -# ADD RSC /l 0x80c /d "_DEBUG" +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c +# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /GZ /c +# ADD BASE RSC /l 0x809 /d "_DEBUG" +# ADD RSC /l 0x809 /d "_DEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe -# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept -# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept !ENDIF @@ -81,25 +80,28 @@ LINK32=link.exe # Name "frames_to_mj2 - Win32 Release" # Name "frames_to_mj2 - Win32 Debug" -# Begin Group "Source Files" +# Begin Group "MJ2" -# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" -# Begin Source File +# PROP Default_Filter "" +# Begin Group "MJ2 Header Files" -SOURCE=..\libopenjpeg\bio.c -# End Source File +# PROP Default_Filter "" # Begin Source File -SOURCE=..\libopenjpeg\cio.c +SOURCE=.\compat\getopt.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\dwt.c +SOURCE=.\mj2.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\fix.c +SOURCE=.\mj2_convert.h # End Source File +# End Group +# Begin Group "MJ2 Source Files" + +# PROP Default_Filter "" # Begin Source File SOURCE=.\frames_to_mj2.c @@ -110,148 +112,157 @@ SOURCE=.\compat\getopt.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\int.c +SOURCE=.\mj2.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\j2k.c +SOURCE=.\mj2_convert.c # End Source File +# End Group +# End Group +# Begin Group "OpenJPEG" + +# PROP Default_Filter "" +# Begin Group "OpenJPEG Header Files" + +# PROP Default_Filter "" # Begin Source File -SOURCE=..\libopenjpeg\jp2.c +SOURCE=..\libopenjpeg\bio.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\jpt.c +SOURCE=..\libopenjpeg\cio.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\mct.c +SOURCE=..\libopenjpeg\dwt.c # End Source File # Begin Source File -SOURCE=.\mj2.c +SOURCE=..\libopenjpeg\dwt.h # End Source File # Begin Source File -SOURCE=.\mj2_convert.c +SOURCE=..\libopenjpeg\fix.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\mqc.c +SOURCE=..\libopenjpeg\int.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\pi.c +SOURCE=..\libopenjpeg\j2k.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\raw.c +SOURCE=..\libopenjpeg\jp2.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\t1.c +SOURCE=..\libopenjpeg\jpt.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\t2.c +SOURCE=..\libopenjpeg\mct.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\tcd.c +SOURCE=..\libopenjpeg\mqc.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\tgt.c +SOURCE=..\libopenjpeg\openjpeg.h # End Source File -# End Group -# Begin Group "Header Files" +# Begin Source File -# PROP Default_Filter "h;hpp;hxx;hm;inl" +SOURCE=..\libopenjpeg\pi.h +# End Source File # Begin Source File -SOURCE=..\libopenjpeg\bio.h +SOURCE=..\libopenjpeg\raw.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\cio.h +SOURCE=..\libopenjpeg\t1.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\dwt.h +SOURCE=..\libopenjpeg\t2.h # End Source File # Begin Source File -SOURCE=..\libopenjpeg\fix.h +SOURCE=..\libopenjpeg\tcd.h # End Source File # Begin Source File -SOURCE=.\compat\getopt.h +SOURCE=..\libopenjpeg\tgt.h # End Source File +# End Group +# Begin Group "OpenJPEG Source Files" + +# PROP Default_Filter "" # Begin Source File -SOURCE=..\libopenjpeg\int.h +SOURCE=..\libopenjpeg\bio.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\j2k.h +SOURCE=..\libopenjpeg\cio.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\jp2.h +SOURCE=..\libopenjpeg\fix.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\jpt.h +SOURCE=..\libopenjpeg\int.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\mct.h +SOURCE=..\libopenjpeg\j2k.c # End Source File # Begin Source File -SOURCE=.\mj2.h +SOURCE=..\libopenjpeg\jp2.c # End Source File # Begin Source File -SOURCE=.\mj2_convert.h +SOURCE=..\libopenjpeg\jpt.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\mqc.h +SOURCE=..\libopenjpeg\mct.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\openjpeg.h +SOURCE=..\libopenjpeg\mqc.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\pi.h +SOURCE=..\libopenjpeg\pi.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\raw.h +SOURCE=..\libopenjpeg\raw.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\t1.h +SOURCE=..\libopenjpeg\t1.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\t2.h +SOURCE=..\libopenjpeg\t2.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\tcd.h +SOURCE=..\libopenjpeg\tcd.c # End Source File # Begin Source File -SOURCE=..\libopenjpeg\tgt.h +SOURCE=..\libopenjpeg\tgt.c # End Source File # End Group -# Begin Group "Resource Files" - -# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" # End Group # End Target # End Project diff --git a/mj2/mj2.c b/mj2/mj2.c index 4b2757f8..9d10129f 100644 --- a/mj2/mj2.c +++ b/mj2/mj2.c @@ -25,7 +25,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include #include @@ -33,7 +32,7 @@ #include #include "mj2.h" -#include "mj2_convert.h" +#include #define MJ2_JP 0x6a502020 #define MJ2_FTYP 0x66747970 @@ -92,6 +91,10 @@ void mj2_memory_free(mj2_movie_t * movie) tk = &movie->tk[i]; if (tk->name_size != 0) free(tk->name); + if (tk->jp2_struct.comps != 0) + free(tk->jp2_struct.comps); + if (tk->jp2_struct.cl != 0) + free(tk->jp2_struct.cl); if (tk->num_url != 0) free(tk->url); if (tk->num_urn != 0) @@ -130,6 +133,11 @@ int mj2_read_boxhdr(mj2_box_t * box) return 1; }; box->length = cio_read(4); + if (box->length == 0) + box->length = cio_numbytesleft() + 12; + } + else if (box->length == 0) { + box->length = cio_numbytesleft() + 8; } return 0; } @@ -147,9 +155,11 @@ int mj2_init_stdmovie(mj2_movie_t * movie) time_t ltime; movie->brand = MJ2_MJ2; movie->minversion = 0; - movie->num_cl = 2; - movie->cl = - (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int)); + if (!((movie->num_cl<100) && (movie->num_cl>-1))) { + movie->num_cl = 2; + movie->cl = + (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int)); + } movie->cl[0] = MJ2_MJ2; movie->cl[1] = MJ2_MJ2S; @@ -168,14 +178,12 @@ int mj2_init_stdmovie(mj2_movie_t * movie) movie->trans_matrix[6] = 0; movie->trans_matrix[7] = 0; movie->trans_matrix[8] = 0x40000000; - - + movie->next_tk_id = 1; for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) { mj2_tk_t *tk = &movie->tk[i]; + movie->next_tk_id++; if (tk->track_type == 0) { - tk->num_samples = yuv_num_frames(tk); - if (tk->num_samples == 0) return 1; @@ -184,11 +192,6 @@ int mj2_init_stdmovie(mj2_movie_t * movie) tk->timescale = 1000; /* Timescale = 1 ms */ - tk->sample = - (mj2_sample_t *) malloc(tk->num_samples * sizeof(mj2_sample_t)); - tk->num_chunks = tk->num_samples; - tk->chunk = - (mj2_chunk_t *) malloc(tk->num_chunks * sizeof(mj2_chunk_t)); tk->chunk[0].num_samples = 1; tk->chunk[0].sample_descr_idx = 1; @@ -451,211 +454,6 @@ int mj2_read_ftyp(mj2_movie_t * movie) return 0; } -/* -* Write the MDAT box -* -* Media Data box -* -*/ -int mj2_write_mdat(FILE * outfile, mj2_movie_t * movie, j2k_image_t * img, - j2k_cp_t * cp, char *outbuf, char *index) -{ - unsigned char box_len_ptr; - mj2_box_t box; - int len, l, k; - int i, m; - unsigned int j; - int pos_correction = 0; - int tileno; - - box.init_pos = cio_tell(); - cio_skip(4); - cio_write(MJ2_MDAT, 4); /* MDAT */ - - for (i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++) { - if (movie->tk[i].track_type != 0) { - fprintf(stderr, "Unable to write sound or hint tracks\n"); - } else { - j2k_cp_t cp_init; - mj2_tk_t *tk; - - tk = &movie->tk[i]; - - fprintf(stderr, "Video Track number %d\n", i + 1); - - len = cio_tell(); - fwrite(outbuf, 1, len, outfile); - pos_correction = cio_tell() + pos_correction; - free(outbuf); - - /* Copy the first tile coding parameters (tcp) to cp_init */ - - cp_init.tcps = - (j2k_tcp_t *) malloc(cp->tw * cp->th * sizeof(j2k_tcp_t)); - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { - for (l = 0; l < cp->tcps[tileno].numlayers; l++) { - cp_init.tcps[tileno].rates[l] = cp->tcps[tileno].rates[l]; - //tileno = cp->tcps[tileno].rates[l]; - } - } - - - for (j = 0; j < tk->num_samples; j++) { - outbuf = (char *) malloc(cp->tdx * cp->tdy * cp->th * cp->tw * 2); - cio_init(outbuf, cp->tdx * cp->tdy * cp->th * cp->tw * 2); - - fprintf(stderr, "Frame number %d/%d: \n", j + 1, tk->num_samples); - - - if (!yuvtoimage(tk, img, j)) { - fprintf(stderr, "Error with frame number %d in YUV file\n", j); - return 1; - } - - len = jp2_write_jp2c(img, cp, outbuf, index); - - for (m = 0; m < img->numcomps; m++) { - free(img->comps[m].data); - } - - tk->sample[j].sample_size = len; - - tk->sample[j].offset = pos_correction; - tk->chunk[j].offset = pos_correction; /* There is one sample per chunk */ - - fwrite(outbuf, 1, len, outfile); - - pos_correction = cio_tell() + pos_correction; - - free(outbuf); - - /* Copy the cp_init parameters to cp->tcps */ - - for (tileno = 0; tileno < cp->tw * cp->th; tileno++) { - for (k = 0; k < cp->tcps[tileno].numlayers; k++) { - cp->tcps[tileno].rates[k] = cp_init.tcps[tileno].rates[k]; - } - } - } - } - - box.length = pos_correction - box.init_pos; - - fseek(outfile, box.init_pos, SEEK_SET); - - cio_init(&box_len_ptr, 4); /* Init a cio to write box length variable in a little endian way */ - cio_write(box.length, 4); - - fwrite(&box_len_ptr, 4, 1, outfile); - - fseek(outfile, box.init_pos + box.length, SEEK_SET); - } - return 0; -} - - -/* -* Read the MDAT box -* -* Media Data box -* -*/ -int mj2_read_mdat(mj2_movie_t * movie, unsigned char *src, char *outfile) -{ - int track_nb; - unsigned int i; - int jp2c_cio_len, jp2c_len, pos_correction = 0; - FILE *f=NULL; - int compno; - - mj2_box_t box; - - mj2_read_boxhdr(&box); - if (MJ2_MDAT != box.type) { - fprintf(stderr, "Error: Expected MDAT Marker\n"); - return 1; - } - - pos_correction = cio_tell() - box.init_pos; - - f = fopen(outfile, "w"); /* Erase content of file if it already exists */ - fclose(f); - - for (track_nb = 0; track_nb < movie->next_tk_id - 1; track_nb++) { - if (movie->tk[track_nb].imagefile != NULL) { - fprintf(stderr, "%s", movie->tk[track_nb].imagefile); - - f = fopen(movie->tk[track_nb].imagefile, "w"); // Erase content of file if it already exists - fclose(f); - } - } - - for (track_nb = 0; - track_nb < movie->num_htk + movie->num_stk + movie->num_vtk; - track_nb++) { - mj2_tk_t *tk = &movie->tk[track_nb]; - if (tk->track_type != 0) { - cio_seek(box.init_pos); - cio_skip(box.length); - } else { - fprintf(stderr, "Track %d: Width=%d Height=%d\n", track_nb, - tk->w, tk->h); - fprintf(stderr, "%d Samples\n", tk->num_samples); - - if (tk->imagefile == NULL) { - tk->imagefile = outfile; - } - - for (i = 0; i < tk->num_samples; i++) { - - mj2_sample_t *sample = &tk->sample[i]; - j2k_image_t img; - j2k_cp_t cp; - unsigned char *pos; - - fprintf(stderr, "Frame %d / %d: \n", i+1, tk->num_samples); - - cio_init(src + sample->offset, 8); - - jp2c_cio_len = cio_tell(); - jp2c_len = cio_read(4); - - - if (MJ2_JP2C != cio_read(4)) { - fprintf(stderr, "Error: Expected JP2C Marker\n"); - return 1; - } - - pos = src + sample->offset + 8; - - cio_seek(sample->offset + 8); - - if (!j2k_decode(pos, sample->sample_size, &img, &cp)) - return 1; - - if (imagetoyuv(&img, &cp, tk->imagefile)) - return 1; - - j2k_dec_release(); - - for (compno=0; compno < img.numcomps; compno++) - free(img.comps[compno].data); - - - if (cio_tell() + 8 != jp2c_len) { - fprintf(stderr, "Error with JP2C Box Size\n"); - return 1; - } - - } - } - } - - cio_seek(box.init_pos); - cio_skip(box.length); /* Go back to box end */ - - return 0; -} /* * Write the STCO box @@ -1285,9 +1083,8 @@ int mj2_read_jsub(mj2_tk_t * tk) * Visual Sample Entry Description * */ -void mj2_write_smj2(j2k_image_t * img, mj2_tk_t * tk) +void mj2_write_smj2(mj2_tk_t * tk) { - int i; mj2_box_t box; box.init_pos = cio_tell(); @@ -1325,17 +1122,10 @@ void mj2_write_smj2(j2k_image_t * img, mj2_tk_t * tk) cio_write(tk->compressorname[6], 4); cio_write(tk->compressorname[7], 4); - tk->depth = 0; - - for (i = 0; i < img->numcomps; i++) - tk->depth += img->comps[i].bpp; - cio_write(tk->depth, 2); /* Depth */ cio_write(0xffff, 2); /* Pre-defined = -1 */ - jp2_init_stdjp2(&tk->jp2_struct, img); - jp2_write_jp2h(&tk->jp2_struct); mj2_write_fiel(tk); @@ -1427,6 +1217,12 @@ int mj2_read_smj2(j2k_image_t * img, mj2_tk_t * tk) fprintf(stderr, "Error with JP2H Box\n"); return 1; } + + tk->jp2_struct.comps = (jp2_comps_t *) malloc(tk->jp2_struct.numcomps * sizeof(jp2_comps_t)); + tk->jp2_struct.cl = (int *) malloc(sizeof(int)); + + tk->num_br = 0; + tk->num_jp2x = 0; for (i = 0; cio_tell() - box.init_pos < box.length; i++) { mj2_read_boxhdr(&box2); @@ -1474,7 +1270,7 @@ int mj2_read_smj2(j2k_image_t * img, mj2_tk_t * tk) * Sample Description * */ -void mj2_write_stsd(mj2_tk_t * tk, j2k_image_t * img) +void mj2_write_stsd(mj2_tk_t * tk) { mj2_box_t box; @@ -1487,7 +1283,7 @@ void mj2_write_stsd(mj2_tk_t * tk, j2k_image_t * img) cio_write(1, 4); /* entry_count = 1 (considering same JP2 headerboxes) */ if (tk->track_type == 0) { - mj2_write_smj2(img, tk); + mj2_write_smj2(tk); } else if (tk->track_type == 1) { // Not implemented } @@ -1561,7 +1357,7 @@ int mj2_read_stsd(mj2_tk_t * tk, j2k_image_t * img) * Sample table box box * */ -void mj2_write_stbl(mj2_tk_t * tk, j2k_image_t * img) +void mj2_write_stbl(mj2_tk_t * tk) { mj2_box_t box; @@ -1569,7 +1365,7 @@ void mj2_write_stbl(mj2_tk_t * tk, j2k_image_t * img) cio_skip(4); cio_write(MJ2_STBL, 4); /* STBL */ - mj2_write_stsd(tk, img); + mj2_write_stsd(tk); mj2_write_stts(tk); mj2_write_stsc(tk); mj2_write_stsz(tk); @@ -2105,7 +1901,7 @@ int mj2_read_hmhd(mj2_tk_t * tk) * Media information box * */ -void mj2_write_minf(mj2_tk_t * tk, j2k_image_t * img) +void mj2_write_minf(mj2_tk_t * tk) { mj2_box_t box; @@ -2122,7 +1918,7 @@ void mj2_write_minf(mj2_tk_t * tk, j2k_image_t * img) } mj2_write_dinf(tk); - mj2_write_stbl(tk, img); + mj2_write_stbl(tk); box.length = cio_tell() - box.init_pos; cio_seek(box.init_pos); @@ -2384,7 +2180,7 @@ int mj2_read_mdhd(mj2_tk_t * tk) * Media box * */ -void mj2_write_mdia(mj2_tk_t * tk, j2k_image_t * img) +void mj2_write_mdia(mj2_tk_t * tk) { mj2_box_t box; @@ -2394,7 +2190,7 @@ void mj2_write_mdia(mj2_tk_t * tk, j2k_image_t * img) mj2_write_mdhd(tk); mj2_write_hdlr(tk); - mj2_write_minf(tk, img); + mj2_write_minf(tk); box.length = cio_tell() - box.init_pos; cio_seek(box.init_pos); @@ -2579,7 +2375,7 @@ int mj2_read_tkhd(mj2_tk_t * tk) * Track box * */ -void mj2_write_trak(mj2_tk_t * tk, j2k_image_t * img) +void mj2_write_trak(mj2_tk_t * tk) { mj2_box_t box; @@ -2589,7 +2385,7 @@ void mj2_write_trak(mj2_tk_t * tk, j2k_image_t * img) cio_write(MJ2_TRAK, 4); /* TRAK */ mj2_write_tkhd(tk); - mj2_write_mdia(tk, img); + mj2_write_mdia(tk); box.length = cio_tell() - box.init_pos; cio_seek(box.init_pos); @@ -2768,7 +2564,7 @@ int mj2_read_mvhd(mj2_movie_t * movie) * Movie Box * */ -void mj2_write_moov(mj2_movie_t * movie, j2k_image_t * img) +void mj2_write_moov(mj2_movie_t * movie) { int i; mj2_box_t box; @@ -2780,7 +2576,7 @@ void mj2_write_moov(mj2_movie_t * movie, j2k_image_t * img) mj2_write_mvhd(movie); for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) { - mj2_write_trak(&movie->tk[i], img); + mj2_write_trak(&movie->tk[i]); } box.length = cio_tell() - box.init_pos; @@ -2817,9 +2613,6 @@ int mj2_read_moov(mj2_movie_t * movie, j2k_image_t * img) movie->tk = (mj2_tk_t *) malloc((movie->next_tk_id - 1) * sizeof(mj2_tk_t)); - for (i=0; i < (unsigned int)movie->next_tk_id - 1; i++) - movie->tk[i].imagefile = NULL; - for (i = 0; cio_tell() - box.init_pos < box.length; i++) { mj2_read_boxhdr(&box2); if (box2.type == MJ2_TRAK) { @@ -2846,121 +2639,112 @@ int mj2_read_moov(mj2_movie_t * movie, j2k_image_t * img) return 0; } - -int mj2_encode(mj2_movie_t * movie, j2k_cp_t * cp, char *index) -{ - - char *outbuf=NULL; - FILE *outfile=NULL; - int len; - unsigned int i; - - j2k_image_t img; - - outbuf = (char *) malloc(cp->tdx * cp->tdy * 2); - cio_init(outbuf, cp->tdx * cp->tdy * 2); - - outfile = fopen(movie->mj2file, "wb"); - if (!outfile) { - fprintf(stderr, "failed to open %s for writing\n", movie->mj2file); - return 1; - } - - mj2_write_jp(); - mj2_write_ftyp(movie); - - if (mj2_write_mdat(outfile, movie, &img, cp, outbuf, index)) { - fprintf(stderr, "Error writing tracks\n\n"); - return 1; - } - - outbuf = (char *) malloc(cp->tdx * cp->tdy * 2); - cio_init(outbuf, cp->tdx * cp->tdy * 2); - - mj2_write_moov(movie, &img); - - len = cio_tell(); - fwrite(outbuf, 1, len, outfile); - - fclose(outfile); - - for (i = 0; i < movie->tk[0].jp2_struct.numcomps; i++) { - char tmp[20]; - sprintf(tmp, "Compo%d", i); - if (remove(tmp) == -1) { - fprintf(stderr, "failed to kill %s file !\n", tmp); - } - } - - free(img.comps); - free(outbuf); - - return 0; - -} - -int mj2_decode(unsigned char *src, int len, mj2_movie_t * movie, - j2k_cp_t * cp, char *outfile) -{ - unsigned int MDATbox_pos = 0; - unsigned int MOOVboxend_pos = 0; +int mj2_read_struct(FILE *file, mj2_movie_t * movie) { mj2_box_t box; j2k_image_t img; + char * src; + int fsresult; + int foffset; - cio_init(src, len); - + movie->num_vtk=0; + movie->num_stk=0; + movie->num_htk=0; + + src = (char*) malloc (300 * sizeof(char)); + fread(src,300,1, file); // Assuming that jp and ftyp markers size do + // not exceed 300 bytes + + cio_init(src, 300); + if (mj2_read_jp()) return 1; if (mj2_read_ftyp(movie)) return 1; + fsresult = fseek(file,cio_tell(),SEEK_SET); + if( fsresult ) { + fprintf(stderr, "End of file reached while trying to read data after FTYP box\n" ); + return 1; + } - for (; cio_numbytesleft() > 0;) { - mj2_read_boxhdr(&box); - switch (box.type) { + foffset = cio_tell(); + + box.type = 0; + + fread(src,30,1,file); + cio_init(src, 30); + mj2_read_boxhdr(&box); + + while(box.type != MJ2_MOOV) { + + switch(box.type) + { case MJ2_MDAT: - cio_seek(box.init_pos); - if (MOOVboxend_pos == 0) { - MDATbox_pos = box.init_pos; - cio_skip(box.length); /* The MDAT box is only read while the MOOV box is decoded */ - } else { - if (mj2_read_mdat(movie, src, outfile)) - return 1; - } - break; - - case MJ2_MOOV: - cio_seek(box.init_pos); - if (mj2_read_moov(movie, &img)) + fsresult = fseek(file,foffset+box.length,SEEK_SET); + if( fsresult ) { + fprintf(stderr, "End of file reached while trying to read MDAT box\n" ); return 1; - MOOVboxend_pos = cio_tell(); - if (MDATbox_pos != 0) { - cio_seek(MDATbox_pos); /* After MOOV box, read the MDAT box */ - - if (mj2_read_mdat(movie, src, outfile)) - return 1; - - cio_seek(MOOVboxend_pos); } + foffset += box.length; break; - + case MJ2_MOOF: - cio_seek(box.init_pos); - cio_skip(box.length); - break; + fsresult = fseek(file,foffset+box.length,SEEK_SET); + if( fsresult ) { + fprintf(stderr, "End of file reached while trying to read MOOF box\n" ); + return 1; + } + foffset += box.length; + break; case MJ2_FREE: - cio_seek(box.init_pos); - cio_skip(box.length); - break; + fsresult = fseek(file,foffset+box.length,SEEK_SET); + if( fsresult ) { + fprintf(stderr, "End of file reached while trying to read FREE box\n" ); + return 1; + } + foffset += box.length; + break; case MJ2_SKIP: - cio_seek(box.init_pos); - cio_skip(box.length); - break; + fsresult = fseek(file,foffset+box.length,SEEK_SET); + if( fsresult ) { + fprintf(stderr, "End of file reached while trying to read SKIP bo\nx" ); + return 1; + } + foffset += box.length; + break; default: - fprintf(stderr, "Unknown box\n"); + fprintf(stderr, "Unknown box in MJ2 stream\n"); + fsresult = fseek(file,foffset+box.length,SEEK_SET); + if( fsresult ) { + fprintf(stderr, "End of file reached while trying to read %s box\n", box.type ); + return 1; + } + foffset += box.length; + break; + } + fsresult = fread(src,8,1,file); + if (fsresult != 1) { + fprintf(stderr, "MOOV box not found in file\n"); return 1; } + cio_init(src, 8); + mj2_read_boxhdr(&box); + } + + + fseek(file,-8,SEEK_CUR); + src = realloc(src,box.length); + fsresult = fread(src,box.length,1,file); + if (fsresult != 1) { + fprintf(stderr, "End of file reached while trying to read MOOV box\n"); + return 1; } + cio_init(src, box.length); + + if (mj2_read_moov(movie, &img)) + return 1; + + free(src); return 0; } diff --git a/mj2/mj2.h b/mj2/mj2.h index 622d63c6..d200d098 100644 --- a/mj2/mj2.h +++ b/mj2/mj2.h @@ -25,8 +25,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ - -#include "j2k.h" +#include +#include #ifndef __MJ2_H #define __MJ2_H @@ -66,7 +66,6 @@ typedef struct { /* URN */ typedef struct { /* Video Track Parameters */ int track_ID; int track_type; - char *imagefile; unsigned int creation_time; unsigned int modification_time; int duration; @@ -91,8 +90,8 @@ typedef struct { /* Video Track Parameters */ int h; int CbCr_subsampling_dx; int CbCr_subsampling_dy; - int subsampling_dx; - int subsampling_dy; + // int subsampling_dx; + // int subsampling_dy; int sample_rate; int sample_description; int horizresolution; @@ -133,7 +132,6 @@ typedef struct { /* Movie */ unsigned int minversion; int num_cl; unsigned int *cl; - char *mj2file; unsigned int creation_time; unsigned int modification_time; int timescale; @@ -154,6 +152,10 @@ typedef struct { int init_pos; } mj2_box_t; +void mj2_write_jp(); + +void mj2_write_ftyp(mj2_movie_t * movie); + /* * Use this function to initialize a standard movie with standard values * It has one sample per chunk @@ -190,4 +192,8 @@ int mj2_decode(unsigned char *src, int len, mj2_movie_t * movie, */ void mj2_memory_free(mj2_movie_t * movie); +int mj2_read_struct(FILE *file, mj2_movie_t * movie); + +void mj2_write_moov(mj2_movie_t * movie); + #endif diff --git a/mj2/mj2_convert.c b/mj2/mj2_convert.c index 7d6d2620..d7a2d118 100644 --- a/mj2/mj2_convert.c +++ b/mj2/mj2_convert.c @@ -1,46 +1,9 @@ -/* -* Copyright (c) 2003-2004, François-Olivier Devaux -* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium -* All rights reserved. -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* 1. Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* 2. Redistributions in binary form must reproduce the above copyright -* notice, this list of conditions and the following disclaimer in the -* documentation and/or other materials provided with the distribution. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE -* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -* POSSIBILITY OF SUCH DAMAGE. -*/ - -#include #include -#include -#include -#ifndef WIN32 -#include -#endif -#include -#include "mj2.h" - -// -->> -->> -->> -->> - -// YUV IMAGE FORMAT - -//-- <<-- <<-- <<-- */ +#include +#include "mj2.h" +#include +#include /* ----------------------- */ /* */ @@ -50,18 +13,11 @@ /* */ /* ----------------------- */ -int yuv_num_frames(mj2_tk_t * tk) +int yuv_num_frames(mj2_tk_t * tk, FILE *f) { - FILE *f; int numimages, frame_size; long end_of_f; - f = fopen(tk->imagefile, "rb"); - if (!f) { - fprintf(stderr, "Failed to open %s for reading !!\n", tk->imagefile); - return 0; - } - frame_size = (int) (tk->w * tk->h * (1.0 + (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy))); /* Calculate frame size */ fseek(f, 0, SEEK_END); @@ -87,45 +43,35 @@ int yuv_num_frames(mj2_tk_t * tk) // // ----------------------- -int yuvtoimage(mj2_tk_t * tk, j2k_image_t * img, int frame_num) +int yuvtoimage(FILE *yuvfile, mj2_tk_t * tk, j2k_image_t * img, + int frame_num, int subsampling_dx, int subsampling_dy) { - FILE *f; int i, j; int offset; long end_of_f, position; FILE *Compo; - - - f = fopen(tk->imagefile, "rb"); - if (!f) { - fprintf(stderr, "Failed to open %s for reading !!\n", tk->imagefile); - return 0; - } - offset = (int) ((double) (frame_num * tk->w * tk->h) * (1.0 + 1.0 * (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy))); - fseek(f, 0, SEEK_END); - end_of_f = ftell(f); - fseek(f, sizeof(unsigned char) * offset, SEEK_SET); - position = ftell(f); + fseek(yuvfile, 0, SEEK_END); + end_of_f = ftell(yuvfile); + fseek(yuvfile, sizeof(unsigned char) * offset, SEEK_SET); + position = ftell(yuvfile); if (position >= end_of_f) { - fprintf(stderr, "Cannot reach frame number %d in %s file !!\n", - frame_num, tk->imagefile); + fprintf(stderr, "Cannot reach frame number %d in yuv file !!\n", + frame_num); return 0; } img->x0 = tk->Dim[0]; img->y0 = tk->Dim[1]; - img->x1 = - !tk->Dim[0] ? (tk->w - 1) * tk->subsampling_dx + 1 : tk->Dim[0] + - (tk->w - 1) * tk->subsampling_dx + 1; - img->y1 = - !tk->Dim[1] ? (tk->h - 1) * tk->subsampling_dy + 1 : tk->Dim[1] + - (tk->h - 1) * tk->subsampling_dy + 1; + img->x1 = !tk->Dim[0] ? (tk->w - 1) * subsampling_dx + 1 : tk->Dim[0] + + (tk->w - 1) * subsampling_dx + 1; + img->y1 = !tk->Dim[1] ? (tk->h - 1) * subsampling_dy + 1 : tk->Dim[1] + + (tk->h - 1) * subsampling_dy + 1; img->numcomps = 3; img->color_space = 3; img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t)); @@ -136,11 +82,11 @@ int yuvtoimage(mj2_tk_t * tk, j2k_image_t * img, int frame_num) img->comps[i].bpp = 8; img->comps[i].sgnd = 0; if (i == 0) { - img->comps[i].dx = tk->subsampling_dx; - img->comps[i].dy = tk->subsampling_dy; + img->comps[i].dx = subsampling_dx; + img->comps[i].dy = subsampling_dy; } else { - img->comps[i].dx = tk->subsampling_dx * tk->CbCr_subsampling_dx; - img->comps[i].dy = tk->subsampling_dy * tk->CbCr_subsampling_dy; + img->comps[i].dx = subsampling_dx * tk->CbCr_subsampling_dx; + img->comps[i].dy = subsampling_dy * tk->CbCr_subsampling_dy; } } @@ -150,9 +96,9 @@ int yuvtoimage(mj2_tk_t * tk, j2k_image_t * img, int frame_num) } for (i = 0; i < (tk->w * tk->h / (img->comps[0].dx * img->comps[0].dy)) - && !feof(f); i++) { + && !feof(yuvfile); i++) { unsigned char y; - j = fread(&y, 1, 1, f); + j = fread(&y, 1, 1, yuvfile); fwrite(&y, 1, 1, Compo); } @@ -165,9 +111,9 @@ int yuvtoimage(mj2_tk_t * tk, j2k_image_t * img, int frame_num) for (i = 0; i < (tk->w * tk->h / (img->comps[1].dx * img->comps[1].dy)) - && !feof(f); i++) { + && !feof(yuvfile); i++) { unsigned char cb; - j = fread(&cb, sizeof(unsigned char), 1, f); + j = fread(&cb, sizeof(unsigned char), 1, yuvfile); fwrite(&cb, 1, 1, Compo); } @@ -180,18 +126,18 @@ int yuvtoimage(mj2_tk_t * tk, j2k_image_t * img, int frame_num) for (i = 0; i < (tk->w * tk->h / (img->comps[2].dx * img->comps[2].dy)) - && !feof(f); i++) { + && !feof(yuvfile); i++) { unsigned char cr; - j = fread(&cr, sizeof(unsigned char), 1, f); + j = fread(&cr, sizeof(unsigned char), 1, yuvfile); fwrite(&cr, 1, 1, Compo); } fclose(Compo); - return 1; } + // ----------------------- // // @@ -204,42 +150,42 @@ int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile) { FILE *f; int i; - + if (img->numcomps == 3) { if (img->comps[0].dx != img->comps[1].dx / 2 - || img->comps[1].dx != img->comps[2].dx) { + || img->comps[1].dx != img->comps[2].dx) { fprintf(stderr, - "Error with the input image components size: cannot create yuv file)\n"); + "Error with the input image components size: cannot create yuv file)\n"); return 1; } } else if (!(img->numcomps == 1)) { fprintf(stderr, - "Error with the number of image components(must be one or three)\n"); + "Error with the number of image components(must be one or three)\n"); return 1; } - + f = fopen(outfile, "a+b"); if (!f) { fprintf(stderr, "failed to open %s for writing\n", outfile); return 1; } - - + + for (i = 0; i < (img->comps[0].w * img->comps[0].h); i++) { unsigned char y; y = img->comps[0].data[i]; fwrite(&y, 1, 1, f); } - - + + if (img->numcomps == 3) { for (i = 0; i < (img->comps[1].w * img->comps[1].h); i++) { unsigned char cb; cb = img->comps[1].data[i]; fwrite(&cb, 1, 1, f); } - - + + for (i = 0; i < (img->comps[2].w * img->comps[2].h); i++) { unsigned char cr; cr = img->comps[2].data[i]; @@ -250,18 +196,123 @@ int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile) unsigned char cb = 125; fwrite(&cb, 1, 1, f); } - - + + for (i = 0; i < (img->comps[0].w * img->comps[0].h * 0.25); i++) { unsigned char cr = 125; fwrite(&cr, 1, 1, f); } - } - - - + } fclose(f); return 0; +} +// ----------------------- +// +// +// IMAGE to BMP +// +// ----------------------- -} +int imagetobmp(j2k_image_t * img, j2k_cp_t * cp, char *outfile) { + int w,wr,h,hr,i,pad; + FILE *f; + + if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx + && img->comps[1].dx == img->comps[2].dx + && img->comps[0].dy == img->comps[1].dy + && img->comps[1].dy == img->comps[2].dy + && img->comps[0].prec == img->comps[1].prec + && img->comps[1].prec == img->comps[2].prec) { + /* -->> -->> -->> -->> + + 24 bits color + + <<-- <<-- <<-- <<-- */ + + f = fopen(outfile, "wb"); + if (!f) { + fprintf(stderr, "failed to open %s for writing\n", outfile); + return 1; + } + + w = img->comps[0].w; + wr = int_ceildivpow2(img->comps[0].w, img->comps[0].factor); + + h = img->comps[0].h; + hr = int_ceildivpow2(img->comps[0].h, img->comps[0].factor); + + fprintf(f, "BM"); + + /* FILE HEADER */ + /* ------------- */ + fprintf(f, "%c%c%c%c", + (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + + 54) & 0xff, + (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) + >> 8) & 0xff, + (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) + >> 16) & 0xff, + (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) + >> 24) & 0xff); + fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, + ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); + fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff, + ((54) >> 16) & 0xff, ((54) >> 24) & 0xff); + + /* INFO HEADER */ + /* ------------- */ + fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, + ((40) >> 16) & 0xff, ((40) >> 24) & 0xff); + fprintf(f, "%c%c%c%c", (unsigned char) ((wr) & 0xff), + (unsigned char) ((wr) >> 8) & 0xff, + (unsigned char) ((wr) >> 16) & 0xff, + (unsigned char) ((wr) >> 24) & 0xff); + fprintf(f, "%c%c%c%c", (unsigned char) ((hr) & 0xff), + (unsigned char) ((hr) >> 8) & 0xff, + (unsigned char) ((hr) >> 16) & 0xff, + (unsigned char) ((hr) >> 24) & 0xff); + fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff); + fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff); + fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, + ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); + fprintf(f, "%c%c%c%c", + (unsigned char) (3 * hr * wr + + 3 * hr * (wr % 2)) & 0xff, + (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> + 8) & 0xff, + (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> + 16) & 0xff, + (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> + 24) & 0xff); + fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, + ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); + fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, + ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff); + fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, + ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); + fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, + ((0) >> 16) & 0xff, ((0) >> 24) & 0xff); + + for (i = 0; i < wr * hr; i++) { + unsigned char R, G, B; + /* a modifier */ + // R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; + R = img->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; + // G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; + G = img->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; + // B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]; + B = img->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)]; + fprintf(f, "%c%c%c", B, G, R); + + if ((i + 1) % wr == 0) { + for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */ + fprintf(f, "%c", 0); + } + } + fclose(f); + free(img->comps[1].data); + free(img->comps[2].data); + } + return 0; +} \ No newline at end of file diff --git a/mj2/mj2_convert.h b/mj2/mj2_convert.h index f9d64869..e1a7dc72 100644 --- a/mj2/mj2_convert.h +++ b/mj2/mj2_convert.h @@ -1,39 +1,16 @@ -/* -* Copyright (c) 2003-2004, François-Olivier Devaux -* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium -* All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - #include "mj2.h" #ifndef __MJ2_CONVERT_H #define __MJ2_CONVERT_H -int yuv_num_frames(mj2_tk_t * tk); +int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile); -int yuvtoimage(mj2_tk_t * tk, j2k_image_t * img, int frame_num); +int imagetobmp(j2k_image_t * img, j2k_cp_t * cp, char *outfile); + +int yuvtoimage(FILE *yuvfile, mj2_tk_t * tk, j2k_image_t * img, + int frame_num, int subsampling_dx, int subsampling_dy); + +int yuv_num_frames(mj2_tk_t * tk, FILE *f); -int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile); -#endif +#endif \ No newline at end of file diff --git a/mj2/mj2_to_frames.c b/mj2/mj2_to_frames.c index d775b5aa..7509600d 100644 --- a/mj2/mj2_to_frames.c +++ b/mj2/mj2_to_frames.c @@ -1,144 +1,127 @@ -/* Copyright (c) 2003-2004, François-Olivier Devaux -* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium -* All rights reserved. -* -* All rights reserved. -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* 1. Redistributions of source code must retain the above copyright -* notice, this list of conditions and the following disclaimer. -* 2. Redistributions in binary form must reproduce the above copyright -* notice, this list of conditions and the following disclaimer in the -* documentation and/or other materials provided with the distribution. -* -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS' -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE -* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -* POSSIBILITY OF SUCH DAMAGE. -*/ - -#include #include -#include -#include +#include +#include + #include "mj2.h" #include "mj2_convert.h" +#include -int ceildiv(int a, int b) -{ - return (a + b - 1) / b; -} - -int main(int argc, char **argv) -{ - FILE *f=NULL; - char *src=NULL, *src_name=NULL; - char *dest=NULL, S1, S2, S3; - int len; +//MEMORY LEAK +#ifdef _DEBUG +#define _CRTDBG_MAP_ALLOC +#include // Must be included first +#include +#endif +//MEM + +jmp_buf j2k_error; + +int main(int argc, char *argv[]) { + + unsigned int tnum, snum; + mj2_movie_t movie; + mj2_tk_t *track; + mj2_sample_t *sample; + unsigned char* frame_codestream; + FILE *file, *outfile; + char outfilename[50]; + j2k_image_t img; j2k_cp_t cp; - mj2_movie_t mj2_movie; - + int i; - if (argc < 3) { - fprintf(stderr, - "usage: %s mj2-file raw_yuv-file -reduce n (<- optional)\n", - argv[0]); + if (argc != 3) { + printf("Bad syntax: Usage: MJ2_decoder inputfile.mj2 outputfile.yuv\n"); + printf("Example: MJ2_decoder foreman.mj2 foreman.yuv\n"); return 1; } - - f = fopen(argv[1], "rb"); - if (!f) { + + file = fopen(argv[1], "rb"); + + if (!file) { fprintf(stderr, "failed to open %s for reading\n", argv[1]); return 1; } - dest = argv[2]; + // Checking output file + outfile = fopen(argv[2], "w"); + if (!file) { + fprintf(stderr, "failed to open %s for writing\n", argv[2]); + return 1; + } + fclose(outfile); - cp.reduce_on = 0; - cp.reduce_value = 0; + if (mj2_read_struct(file, &movie)) // Creating the movie structure + return 1; - /* OPTION REDUCE IS ACTIVE */ - if (argc == 5) { - if (strcmp(argv[3], "-reduce")) { - fprintf(stderr, - "usage: options " "-reduce n" - " where n is the factor of reduction [%s]\n", argv[3]); - return 1; - } - cp.reduce_on = 1; - sscanf(argv[4], "%d", &cp.reduce_value); - } - while (*dest) { - dest++; - } - dest--; - S3 = *dest; - dest--; - S2 = *dest; - dest--; - S1 = *dest; - - if (!((S1 == 'y' && S2 == 'u' && S3 == 'v') - || (S1 == 'Y' && S2 == 'U' && S3 == 'V'))) { - fprintf(stderr, - "!! Unrecognized format for outfile : %c%c%c [accept only *.yuv] !!\n", - S1, S2, S3); - fprintf(stderr, - "usage: mj2-file raw_yuv-file -reduce n (<- optional)\n\n"); + // Decode first video track + tnum = 0; + while (movie.tk[tnum].track_type != 0) + tnum ++; - return 1; - } + track = &movie.tk[tnum]; - fseek(f, 0, SEEK_END); - len = ftell(f); - fseek(f, 0, SEEK_SET); - src = (char *) malloc(len); - fread(src, 1, len, f); - fclose(f); + // Output info on first video tracl + fprintf(stdout,"The first video track contains %d frames.\nWidth: %d, Height: %d \n\n", + track->num_samples, track->w, track->h); - src_name = argv[1]; - while (*src_name) { - src_name++; - } - src_name--; - S3 = *src_name; - src_name--; - S2 = *src_name; - src_name--; - S1 = *src_name; - - /* MJ2 format */ - if ((S1 == 'm' && S2 == 'j' && S3 == '2') - || (S1 == 'M' && S2 == 'J' && S3 == '2')) { - mj2_movie.num_stk = 0; - mj2_movie.num_htk = 0; - mj2_movie.num_vtk = 0; - mj2_movie.mj2file = argv[1]; - if (mj2_decode(src, len, &mj2_movie, &cp, argv[2])) { - fprintf(stderr, "mj2_to_frames: failed to decode image!\n"); + for (snum=0; snum < track->num_samples; snum++) + { + fprintf(stdout,"Frame %d: ",snum+1); + sample = &track->sample[snum]; + frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker + fseek(file,sample->offset+8,SEEK_SET); + fread(frame_codestream,sample->sample_size-8,1, file); // Assuming that jp and ftyp markers size do + + if (!j2k_decode(frame_codestream, sample->sample_size-8, &img, &cp)) // Decode J2K to image return 1; - } - mj2_memory_free(&mj2_movie); - } else { - fprintf(stderr, - "mj2_to_frames : Unknown format image *.%c%c%c [only *.mj2]!! \n", - S1, S2, S3); - fprintf(stderr, - "usage: mj2-file raw_yuv-file -reduce n (<- optional)\n\n"); - return 1; + if (((img.numcomps == 3) && (img.comps[0].dx == img.comps[1].dx / 2) + && (img.comps[0].dx == img.comps[2].dx / 2 ) && (img.comps[0].dx == 1)) + || (img.numcomps == 1)) { + + if (imagetoyuv(&img, &cp, argv[2])) // Convert image to YUV + return 1; + } + else if ((img.numcomps == 3) && + (img.comps[0].dx == 1) && (img.comps[1].dx == 1)&& + (img.comps[2].dx == 1))// If YUV 4:4:4 input --> to bmp + { + fprintf(stdout,"The frames will be output in a bmp format (output_1.bmp, ...)\n"); + sprintf(outfilename,"output_%d.bmp",snum); + if (imagetobmp(&img, &cp, outfilename)) // Convert image to YUV + return 1; + + } + else { + fprintf(stdout,"Image component dimensions are unknown. Unable to output image\n"); + fprintf(stdout,"The frames will be output in a j2k file (output_1.j2k, ...)\n"); + + sprintf(outfilename,"output_%d.j2k",snum); + outfile = fopen(outfilename, "wb"); + if (!outfile) { + fprintf(stderr, "failed to open %s for writing\n",outfilename); + return 1; + } + fwrite(frame_codestream,sample->sample_size-8,1,outfile); + fclose(outfile); + } + for (i=0; i