2 * Copyright (c) 2003-2004, Fran�ois-Olivier Devaux
3 * Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
38 #include "mj2_convert.h"
39 #include "compat/getopt.h"
42 Size of memory first allocated for MOOV box
44 #define TEMP_BUF 10000
46 /* -------------------------------------------------------------------------- */
49 sample error callback expecting a FILE* client object
51 void error_callback(const char *msg, void *client_data) {
52 FILE *stream = (FILE*)client_data;
53 fprintf(stream, "[ERROR] %s", msg);
56 sample warning callback expecting a FILE* client object
58 void warning_callback(const char *msg, void *client_data) {
59 FILE *stream = (FILE*)client_data;
60 fprintf(stream, "[WARNING] %s", msg);
63 sample debug callback expecting a FILE* client object
65 void info_callback(const char *msg, void *client_data) {
66 FILE *stream = (FILE*)client_data;
67 fprintf(stream, "[INFO] %s", msg);
70 /* -------------------------------------------------------------------------- */
75 fprintf(stdout,"HELP\n----\n\n");
76 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
79 fprintf(stdout,"List of parameters for the MJ2 encoder:\n");
81 fprintf(stdout,"REMARKS:\n");
82 fprintf(stdout,"---------\n");
85 (stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
87 (stdout,"COD and QCD never appear in the tile_header.\n");
89 fprintf(stdout,"By default:\n");
90 fprintf(stdout,"------------\n");
92 fprintf(stdout," * Lossless\n");
93 fprintf(stdout," * 1 tile\n");
94 fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
95 fprintf(stdout," * Size of code-block : 64 x 64\n");
96 fprintf(stdout," * Number of resolutions: 6\n");
97 fprintf(stdout," * No SOP marker in the codestream\n");
98 fprintf(stdout," * No EPH marker in the codestream\n");
99 fprintf(stdout," * No sub-sampling in x or y direction\n");
100 fprintf(stdout," * No mode switch activated\n");
101 fprintf(stdout," * Progression order: LRCP\n");
102 fprintf(stdout," * No index file\n");
103 fprintf(stdout," * No ROI upshifted\n");
104 fprintf(stdout," * No offset of the origin of the image\n");
105 fprintf(stdout," * No offset of the origin of the tiles\n");
106 fprintf(stdout," * Reversible DWT 5-3\n");
107 fprintf(stdout,"\n");
108 fprintf(stdout,"Parameters:\n");
109 fprintf(stdout,"------------\n");
110 fprintf(stdout,"\n");
112 (stdout,"Required Parameters (except with -h):\n");
114 (stdout,"-i : source file (-i source.yuv) \n");
116 (stdout,"-o : destination file (-o dest.mj2) \n");
118 (stdout,"Optional Parameters:\n");
119 fprintf(stdout,"-h : display the help information \n");
120 fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n ");
121 fprintf(stdout," - The rate specified for each quality level is the desired \n");
122 fprintf(stdout," compression factor.\n");
123 fprintf(stdout," Example: -r 20,10,1 means quality 1: compress 20x, \n");
124 fprintf(stdout," quality 2: compress 10x and quality 3: compress lossless\n");
125 fprintf(stdout," (options -r and -q cannot be used together)\n ");
127 fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n ");
129 fprintf(stdout," (options -r and -q cannot be used together)\n ");
131 fprintf(stdout,"-n : number of resolutions (-n 3) \n");
132 fprintf(stdout,"-b : size of code block (-b 32,32) \n");
133 fprintf(stdout,"-c : size of precinct (-c 128,128) \n");
134 fprintf(stdout,"-t : size of tile (-t 512,512) \n");
136 (stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
138 (stdout,"-s : subsampling factor (-s 2,2) [-s X,Y] \n");
139 fprintf(stdout," Remark: subsampling bigger than 2 can produce error\n");
141 (stdout,"-SOP : write SOP marker before each packet \n");
143 (stdout,"-EPH : write EPH marker after each header packet \n");
145 (stdout,"-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
147 (stdout," 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
149 (stdout," Indicate multiple modes by adding their values. \n");
151 (stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
153 (stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n");
155 (stdout," for component c=%%d [%%d = 0,1,2]\n");
157 (stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n");
159 (stdout,"-d : offset of the origin of the image (-d 150,300) \n");
161 (stdout,"-T : offset of the origin of the tiles (-T 100,75) \n");
162 fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n");
163 fprintf(stdout,"-W : image width, height and the dx and dy subsampling \n");
164 fprintf(stdout," of the Cb and Cr components for YUV files \n");
165 fprintf(stdout," (default is '352,288,2,2' for CIF format's 352x288 and 4:2:0)\n");
166 fprintf(stdout,"-F : video frame rate (set to 25 by default)\n");
168 fprintf(stdout,"\n");
169 fprintf(stdout,"IMPORTANT:\n");
170 fprintf(stdout,"-----------\n");
171 fprintf(stdout,"\n");
172 fprintf(stdout,"The index file has the structure below:\n");
173 fprintf(stdout,"---------------------------------------\n");
174 fprintf(stdout,"\n");
175 fprintf(stdout,"Image_height Image_width\n");
176 fprintf(stdout,"progression order\n");
177 fprintf(stdout,"Tiles_size_X Tiles_size_Y\n");
178 fprintf(stdout,"Components_nb\n");
179 fprintf(stdout,"Layers_nb\n");
180 fprintf(stdout,"decomposition_levels\n");
181 fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
182 fprintf(stdout," [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
183 fprintf(stdout,"Main_header_end_position\n");
184 fprintf(stdout,"Codestream_size\n");
185 fprintf(stdout,"Tile_0 start_pos end_Theader end_pos TotalDisto NumPix MaxMSE\n");
186 fprintf(stdout,"Tile_1 '' '' '' '' '' ''\n");
187 fprintf(stdout,"...\n");
188 fprintf(stdout,"Tile_Nt '' '' '' '' '' ''\n");
189 fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
190 fprintf(stdout,"...\n");
191 fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n");
193 fprintf(stdout,"MaxDisto\n");
195 fprintf(stdout,"TotalDisto\n\n");
198 int give_progression(char progression[4])
200 if (progression[0] == 'L' && progression[1] == 'R'
201 && progression[2] == 'C' && progression[3] == 'P') {
204 if (progression[0] == 'R' && progression[1] == 'L'
205 && progression[2] == 'C' && progression[3] == 'P') {
208 if (progression[0] == 'R' && progression[1] == 'P'
209 && progression[2] == 'C' && progression[3] == 'L') {
212 if (progression[0] == 'P' && progression[1] == 'C'
213 && progression[2] == 'R' && progression[3] == 'L') {
216 if (progression[0] == 'C' && progression[1] == 'P'
217 && progression[2] == 'R' && progression[3] == 'L') {
231 int main(int argc, char **argv)
233 mj2_cparameters_t mj2_parameters; /* MJ2 compression parameters */
234 opj_cparameters_t *j2k_parameters; /* J2K compression parameters */
235 opj_event_mgr_t event_mgr; /* event manager */
244 long mdat_initpos, offset;
250 double total_time = 0;
254 mj2_parameters.Dim[0] = 0;
255 mj2_parameters.Dim[1] = 0;
256 mj2_parameters.w = 352; // CIF default value
257 mj2_parameters.h = 288; // CIF default value
258 mj2_parameters.CbCr_subsampling_dx = 2; // CIF default value
259 mj2_parameters.CbCr_subsampling_dy = 2; // CIF default value
260 mj2_parameters.frame_rate = 25;
262 configure the event callbacks (not required)
263 setting of each callback is optionnal
265 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
266 event_mgr.error_handler = error_callback;
267 event_mgr.warning_handler = warning_callback;
268 event_mgr.info_handler = NULL;
270 /* set J2K encoding parameters to default values */
271 opj_set_default_encoder_parameters(&mj2_parameters.j2k_parameters);
272 j2k_parameters = &mj2_parameters.j2k_parameters;
274 /* Create comment for codestream */
275 if(j2k_parameters->cp_comment == NULL) {
276 const char comment[] = "Created by OpenJPEG version ";
277 const size_t clen = strlen(comment);
278 const char *version = opj_version();
279 j2k_parameters->cp_comment = (char*)malloc(clen+strlen(version)+1);
280 sprintf(j2k_parameters->cp_comment,"%s%s", comment, version);
283 mj2_parameters.decod_format = 0;
284 mj2_parameters.cod_format = 0;
287 int c = getopt(argc, argv,
288 "i:o:r:q:f:t:n:c:b:p:s:d:h P:S:E:M:R:T:C:I:W:F:");
292 case 'i': /* IN fill */
294 char *infile = optarg;
306 if ((S1 == 'y' && S2 == 'u' && S3 == 'v')
307 || (S1 == 'Y' && S2 == 'U' && S3 == 'V')) {
308 mj2_parameters.decod_format = YUV_DFMT;
312 "!! Unrecognized format for infile : %c%c%c [accept only *.yuv] !!\n\n",
316 strncpy(mj2_parameters.infile, infile, sizeof(mj2_parameters.infile)-1);
319 /* ----------------------------------------------------- */
320 case 'o': /* OUT fill */
322 char *outfile = optarg;
335 if ((S1 == 'm' && S2 == 'j' && S3 == '2')
336 || (S1 == 'M' && S2 == 'J' && S3 == '2'))
337 mj2_parameters.cod_format = MJ2_CFMT;
340 "Unknown output format image *.%c%c%c [only *.mj2]!! \n",
344 strncpy(mj2_parameters.outfile, outfile, sizeof(mj2_parameters.outfile)-1);
347 /* ----------------------------------------------------- */
348 case 'r': /* rates rates/distorsion */
352 while (sscanf(s, "%f", &rate) == 1) {
353 j2k_parameters->tcp_rates[j2k_parameters->tcp_numlayers] = rate * 2;
354 j2k_parameters->tcp_numlayers++;
355 while (*s && *s != ',') {
362 j2k_parameters->cp_disto_alloc = 1;
365 /* ----------------------------------------------------- */
366 case 'q': /* add fixed_quality */
368 while (sscanf(s, "%f", &j2k_parameters->tcp_distoratio[j2k_parameters->tcp_numlayers]) == 1) {
369 j2k_parameters->tcp_numlayers++;
370 while (*s && *s != ',') {
377 j2k_parameters->cp_fixed_quality = 1;
380 /* ----------------------------------------------------- */
381 case 'f': /* mod fixed_quality (before : -q) */
383 int *row = NULL, *col = NULL;
384 int numlayers = 0, numresolution = 0, matrix_width = 0;
387 sscanf(s, "%d", &numlayers);
392 j2k_parameters->tcp_numlayers = numlayers;
393 numresolution = j2k_parameters->numresolution;
394 matrix_width = numresolution * 3;
395 j2k_parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
398 for (i = 0; i < numlayers; i++) {
399 row = &j2k_parameters->cp_matrice[i * matrix_width];
401 j2k_parameters->tcp_rates[i] = 1;
402 sscanf(s, "%d,", &col[0]);
408 for (j = 1; j < numresolution; j++) {
410 sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
419 if (i < numlayers - 1)
422 j2k_parameters->cp_fixed_alloc = 1;
425 /* ----------------------------------------------------- */
426 case 't': /* tiles */
427 sscanf(optarg, "%d,%d", &j2k_parameters->cp_tdx, &j2k_parameters->cp_tdy);
428 j2k_parameters->tile_size_on = true;
430 /* ----------------------------------------------------- */
431 case 'n': /* resolution */
432 sscanf(optarg, "%d", &j2k_parameters->numresolution);
434 /* ----------------------------------------------------- */
435 case 'c': /* precinct dimension */
443 sscanf(s, "[%d,%d]%c", &j2k_parameters->prcw_init[res_spec],
444 &j2k_parameters->prch_init[res_spec], &sep);
445 j2k_parameters->csty |= 0x01;
447 s = strpbrk(s, "]") + 2;
450 j2k_parameters->res_spec = res_spec;
454 /* ----------------------------------------------------- */
455 case 'b': /* code-block dimension */
457 int cblockw_init = 0, cblockh_init = 0;
458 sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
459 if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
460 || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
462 "!! Size of code_block error (option -b) !!\n\nRestriction :\n"
463 " * width*height<=4096\n * 4<=width,height<= 1024\n\n");
466 j2k_parameters->cblockw_init = cblockw_init;
467 j2k_parameters->cblockh_init = cblockh_init;
470 /* ----------------------------------------------------- */
471 case 'p': /* progression order */
475 strncpy(progression, optarg, 4);
476 j2k_parameters->prog_order = give_progression(progression);
477 if (j2k_parameters->prog_order == -1) {
478 fprintf(stderr, "Unrecognized progression order "
479 "[LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
484 /* ----------------------------------------------------- */
485 case 's': /* subsampling factor */
487 if (sscanf(optarg, "%d,%d", &j2k_parameters->subsampling_dx,
488 &j2k_parameters->subsampling_dy) != 2) {
489 fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n");
494 /* ----------------------------------------------------- */
495 case 'd': /* coordonnate of the reference grid */
497 if (sscanf(optarg, "%d,%d", &j2k_parameters->image_offset_x0,
498 &j2k_parameters->image_offset_y0) != 2) {
499 fprintf(stderr, "-d 'coordonnate of the reference grid' argument "
500 "error !! [-d x0,y0]\n");
505 /* ----------------------------------------------------- */
506 case 'h': /* Display an help description */
510 /* ----------------------------------------------------- */
513 int numpocs = 0; /* number of progression order change (POC) default 0 */
514 opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */
517 POC = j2k_parameters->POC;
519 while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%4s", &POC[numpocs].tile,
520 &POC[numpocs].resno0, &POC[numpocs].compno0,
521 &POC[numpocs].layno1, &POC[numpocs].resno1,
522 &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
523 POC[numpocs].prg1 = give_progression(POC[numpocs].progorder);
525 while (*s && *s != '/') {
533 j2k_parameters->numpocs = numpocs;
536 /* ------------------------------------------------------ */
537 case 'S': /* SOP marker */
538 j2k_parameters->csty |= 0x02;
540 /* ------------------------------------------------------ */
541 case 'E': /* EPH marker */
542 j2k_parameters->csty |= 0x04;
544 /* ------------------------------------------------------ */
545 case 'M': /* Mode switch pas tous au point !! */
546 if (sscanf(optarg, "%d", &value) == 1) {
547 for (i = 0; i <= 5; i++) {
548 int cache = value & (1 << i);
550 j2k_parameters->mode |= (1 << i);
554 /* ------------------------------------------------------ */
557 if (sscanf(optarg, "OI:c=%d,U=%d", &j2k_parameters->roi_compno,
558 &j2k_parameters->roi_shift) != 2) {
559 fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
564 /* ------------------------------------------------------ */
565 case 'T': /* Tile offset */
567 if (sscanf(optarg, "%d,%d", &j2k_parameters->cp_tx0, &j2k_parameters->cp_ty0) != 2) {
568 fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
573 /* ------------------------------------------------------ */
574 case 'C': /* Add a comment */
576 j2k_parameters->cp_comment = (char*)malloc(strlen(optarg) + 1);
577 if(j2k_parameters->cp_comment) {
578 strcpy(j2k_parameters->cp_comment, optarg);
582 /* ------------------------------------------------------ */
583 case 'I': /* reversible or not */
585 j2k_parameters->irreversible = 1;
588 /* ------------------------------------------------------ */
589 case 'W': /* Width and Height and Cb and Cr subsampling in case of YUV format files */
591 (optarg, "%d,%d,%d,%d", &mj2_parameters.w, &mj2_parameters.h, &mj2_parameters.CbCr_subsampling_dx,
592 &mj2_parameters.CbCr_subsampling_dy) != 4) {
593 fprintf(stderr, "-W argument error");
597 /* ------------------------------------------------------ */
598 case 'F': /* Video frame rate */
599 if (sscanf(optarg, "%d", &mj2_parameters.frame_rate) != 1) {
600 fprintf(stderr, "-F argument error");
604 /* ------------------------------------------------------ */
612 if (!mj2_parameters.cod_format || !mj2_parameters.decod_format) {
614 "Correct usage: mj2_encoder -i yuv-file -o mj2-file (+ options)\n");
618 if ((j2k_parameters->cp_disto_alloc || j2k_parameters->cp_fixed_alloc || j2k_parameters->cp_fixed_quality)
619 && (!(j2k_parameters->cp_disto_alloc ^ j2k_parameters->cp_fixed_alloc ^ j2k_parameters->cp_fixed_quality))) {
620 fprintf(stderr, "Error: options -r -q and -f cannot be used together !!\n");
622 } /* mod fixed_quality */
624 /* if no rate entered, lossless by default */
625 if (j2k_parameters->tcp_numlayers == 0) {
626 j2k_parameters->tcp_rates[0] = 0; /* MOD antonin : losslessbug */
627 j2k_parameters->tcp_numlayers++;
628 j2k_parameters->cp_disto_alloc = 1;
631 if((j2k_parameters->cp_tx0 > j2k_parameters->image_offset_x0) || (j2k_parameters->cp_ty0 > j2k_parameters->image_offset_y0)) {
633 "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
634 j2k_parameters->cp_tx0, j2k_parameters->image_offset_x0, j2k_parameters->cp_ty0, j2k_parameters->image_offset_y0);
638 for (i = 0; i < j2k_parameters->numpocs; i++) {
639 if (j2k_parameters->POC[i].prg == -1) {
641 "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
646 if (j2k_parameters->cp_tdx > mj2_parameters.Dim[0] || j2k_parameters->cp_tdy > mj2_parameters.Dim[1]) {
648 "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
649 j2k_parameters->cp_tdx, mj2_parameters.Dim[0], j2k_parameters->cp_tdy, mj2_parameters.Dim[1]);
653 /* to respect profile - 0 */
654 /* ---------------------- */
656 x1 = !mj2_parameters.Dim[0] ? (mj2_parameters.w - 1) * j2k_parameters->subsampling_dx
657 + 1 : mj2_parameters.Dim[0] + (mj2_parameters.w - 1) * j2k_parameters->subsampling_dx + 1;
658 y1 = !mj2_parameters.Dim[1] ? (mj2_parameters.h - 1) * j2k_parameters->subsampling_dy
659 + 1 : mj2_parameters.Dim[1] + (mj2_parameters.h - 1) * j2k_parameters->subsampling_dy + 1;
660 mj2_parameters.numcomps = 3; /* Because YUV files only have 3 components */
661 mj2_parameters.prec = 8; /* Because in YUV files, components have 8-bit depth */
663 j2k_parameters->tcp_mct = 0;
665 mj2file = fopen(mj2_parameters.outfile, "wb");
668 fprintf(stderr, "failed to open %s for writing\n", argv[2]);
672 /* get a MJ2 decompressor handle */
673 cinfo = mj2_create_compress();
674 movie = (opj_mj2_t*)cinfo->mj2_handle;
676 /* catch events using our callbacks and give a local context */
677 opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);
679 /* setup encoder parameters */
680 mj2_setup_encoder(movie, &mj2_parameters);
682 movie->tk[0].num_samples = yuv_num_frames(&movie->tk[0],mj2_parameters.infile);
683 if (movie->tk[0].num_samples == -1) {
687 // One sample per chunk
688 movie->tk[0].chunk = (mj2_chunk_t*) malloc(movie->tk[0].num_samples * sizeof(mj2_chunk_t));
689 movie->tk[0].sample = (mj2_sample_t*) malloc(movie->tk[0].num_samples * sizeof(mj2_sample_t));
691 if (mj2_init_stdmovie(movie)) {
692 fprintf(stderr, "Error with movie initialization");
696 // Writing JP, FTYP and MDAT boxes
697 buf = (unsigned char*) malloc (300 * sizeof(unsigned char)); // Assuming that the JP and FTYP
698 // boxes won't be longer than 300 bytes
699 cio = opj_cio_open((opj_common_ptr)movie->cinfo, buf, 300);
701 mj2_write_ftyp(movie, cio);
702 mdat_initpos = cio_tell(cio);
704 cio_write(cio, MJ2_MDAT, 4);
705 fwrite(buf,cio_tell(cio),1,mj2file);
706 offset = cio_tell(cio);
710 for (i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++) {
711 if (movie->tk[i].track_type != 0) {
712 fprintf(stderr, "Unable to write sound or hint tracks\n");
718 tk->num_chunks = tk->num_samples;
719 numframes = tk->num_samples;
721 fprintf(stderr, "Video Track number %d\n", i + 1);
723 img = mj2_image_create(tk, j2k_parameters);
724 buflen = 2 * (tk->w * tk->h * 8);
725 buf = (unsigned char *) malloc(buflen*sizeof(unsigned char));
727 for (sampleno = 0; sampleno < numframes; sampleno++) {
728 double init_time = opj_clock();
730 if (yuvtoimage(tk, img, sampleno, j2k_parameters, mj2_parameters.infile)) {
731 fprintf(stderr, "Error with frame number %d in YUV file\n", sampleno);
735 /* setup the encoder parameters using the current image and user parameters */
736 opj_setup_encoder(cinfo, j2k_parameters, img);
738 cio = opj_cio_open((opj_common_ptr)movie->cinfo, buf, buflen);
741 cio_write(cio, JP2_JP2C, 4); // JP2C
743 /* encode the image */
744 bSuccess = opj_encode(cinfo, cio, img, NULL);
747 fprintf(stderr, "failed to encode image\n");
751 len = cio_tell(cio) - 8;
753 cio_write(cio, len+8,4);
755 tk->sample[sampleno].sample_size = len+8;
756 tk->sample[sampleno].offset = offset;
757 tk->chunk[sampleno].offset = offset; // There is one sample per chunk
758 fwrite(buf, 1, len+8, mj2file);
760 elapsed_time = opj_clock()-init_time;
761 fprintf(stderr, "Frame number %d/%d encoded in %.2f mseconds\n", sampleno + 1, numframes, elapsed_time*1000);
762 total_time += elapsed_time;
765 /* free buffer data */
767 /* free image data */
768 opj_image_destroy(img);
772 fseek(mj2file, mdat_initpos, SEEK_SET);
774 buf = (unsigned char*) malloc(4*sizeof(unsigned char));
776 // Init a cio to write box length variable in a little endian way
777 cio = opj_cio_open(NULL, buf, 4);
778 cio_write(cio, offset - mdat_initpos, 4);
779 fwrite(buf, 4, 1, mj2file);
780 fseek(mj2file,0,SEEK_END);
784 buf = (unsigned char*) malloc ((TEMP_BUF+numframes*20) * sizeof(unsigned char));
785 cio = opj_cio_open(movie->cinfo, buf, (TEMP_BUF+numframes*20));
786 mj2_write_moov(movie, cio);
787 fwrite(buf,cio_tell(cio),1,mj2file);
790 fprintf(stdout,"Total encoding time: %.2f s for %d frames (%.1f fps)\n", total_time, numframes, (float)numframes/total_time);
795 /* free remaining compression structures */
796 mj2_destroy_compress(movie);
798 /* free user parameters structure */
799 if(j2k_parameters->cp_comment) free(j2k_parameters->cp_comment);
800 if(j2k_parameters->cp_matrice) free(j2k_parameters->cp_matrice);