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 "opj_getopt.h"
42 Size of memory first allocated for MOOV box
44 #define TEMP_BUF 10000
46 #define ENUMCS_GRAY 16
47 #define ENUMCS_SRGB 17
48 #define ENUMCS_SYCC 18
50 /* -------------------------------------------------------------------------- */
53 sample error callback expecting a FILE* client object
55 void error_callback(const char *msg, void *client_data) {
56 FILE *stream = (FILE*)client_data;
57 fprintf(stream, "[ERROR] %s", msg);
60 sample warning callback expecting a FILE* client object
62 void warning_callback(const char *msg, void *client_data) {
63 FILE *stream = (FILE*)client_data;
64 fprintf(stream, "[WARNING] %s", msg);
67 sample debug callback expecting a FILE* client object
69 void info_callback(const char *msg, void *client_data) {
70 FILE *stream = (FILE*)client_data;
71 fprintf(stream, "[INFO] %s", msg);
74 /* -------------------------------------------------------------------------- */
79 fprintf(stdout,"HELP for frames_to_mj2\n----\n\n");
80 fprintf(stdout,"- the -h option displays this help information on screen\n\n");
83 fprintf(stdout,"List of parameters for the MJ2 encoder:\n");
85 fprintf(stdout,"REMARKS:\n");
86 fprintf(stdout,"---------\n");
89 (stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
91 (stdout,"COD and QCD never appear in the tile_header.\n");
93 fprintf(stdout,"By default:\n");
94 fprintf(stdout,"------------\n");
96 fprintf(stdout," * Lossless\n");
97 fprintf(stdout," * 1 tile\n");
98 fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
99 fprintf(stdout," * Size of code-block : 64 x 64\n");
100 fprintf(stdout," * Number of resolutions: 6\n");
101 fprintf(stdout," * No SOP marker in the codestream\n");
102 fprintf(stdout," * No EPH marker in the codestream\n");
103 fprintf(stdout," * No sub-sampling in x or y direction\n");
104 fprintf(stdout," * No mode switch activated\n");
105 fprintf(stdout," * Progression order: LRCP\n");
106 fprintf(stdout," * No index file\n");
107 fprintf(stdout," * No ROI upshifted\n");
108 fprintf(stdout," * No offset of the origin of the image\n");
109 fprintf(stdout," * No offset of the origin of the tiles\n");
110 fprintf(stdout," * Reversible DWT 5-3\n");
111 fprintf(stdout,"\n");
112 fprintf(stdout,"Parameters:\n");
113 fprintf(stdout,"------------\n");
114 fprintf(stdout,"\n");
116 (stdout,"Required Parameters (except with -h):\n");
118 (stdout,"-i : source file (-i source.yuv) \n");
120 (stdout,"-o : destination file (-o dest.mj2) \n");
122 (stdout,"Optional Parameters:\n");
123 fprintf(stdout,"-h : display the help information \n");
124 fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n");
125 fprintf(stdout," - The rate specified for each quality level is the desired \n");
126 fprintf(stdout," compression factor.\n");
127 fprintf(stdout," Example: -r 20,10,1 means quality 1: compress 20x, \n");
128 fprintf(stdout," quality 2: compress 10x and quality 3: compress lossless\n");
129 fprintf(stdout," (options -r and -q cannot be used together)\n");
131 fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n");
132 fprintf(stdout," (options -r and -q cannot be used together)\n");
134 fprintf(stdout,"-n : number of resolutions (-n 3) \n");
135 fprintf(stdout,"-b : size of code block (-b 32,32) \n");
136 fprintf(stdout,"-c : size of precinct (-c 128,128) \n");
137 fprintf(stdout,"-t : size of tile (-t 512,512) \n");
139 (stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
141 (stdout,"-s : subsampling factor (-s 2,2) [-s X,Y] \n");
142 fprintf(stdout," Remark: subsampling bigger than 2 can produce error\n");
144 (stdout,"-S : write SOP marker before each packet \n");
146 (stdout,"-E : write EPH marker after each header packet \n");
148 (stdout,"-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
150 (stdout," 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
152 (stdout," Indicate multiple modes by adding their values. \n");
154 (stdout," Example: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
156 (stdout,"-R : c=%%d,U=%%d : quantization indices upshifted \n");
158 (stdout," for component c=%%d [%%d = 0,1,2]\n");
160 (stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n");
162 (stdout,"-d : offset of the origin of the image (-d 150,300) \n");
164 (stdout,"-T : offset of the origin of the tiles (-T 100,75) \n");
165 fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n");
166 fprintf(stdout,"-W : image width, height and the dx and dy subsampling \n");
167 fprintf(stdout," of the Cb and Cr components for YUV files \n");
168 fprintf(stdout," (default is '352,288,2,2' for CIF format's 352x288 and 4:2:0)\n");
169 fprintf(stdout,"-F : video frame rate (set to 25 by default)\n");
170 fprintf(stdout,"-D : depth, precision in bits [8 .. 16]; default:8\n");
171 fprintf(stdout,"-C : comment\n");
172 fprintf(stdout,"\n");
173 fprintf(stdout,"IMPORTANT:\n");
174 fprintf(stdout,"-----------\n");
175 fprintf(stdout,"\n");
176 fprintf(stdout,"The index file has the structure below:\n");
177 fprintf(stdout,"---------------------------------------\n");
178 fprintf(stdout,"\n");
179 fprintf(stdout,"Image_height Image_width\n");
180 fprintf(stdout,"progression order\n");
181 fprintf(stdout,"Tiles_size_X Tiles_size_Y\n");
182 fprintf(stdout,"Components_nb\n");
183 fprintf(stdout,"Layers_nb\n");
184 fprintf(stdout,"decomposition_levels\n");
185 fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
186 fprintf(stdout," [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
187 fprintf(stdout,"Main_header_end_position\n");
188 fprintf(stdout,"Codestream_size\n");
189 fprintf(stdout,"Tile_0 start_pos end_Theader end_pos TotalDisto NumPix MaxMSE\n");
190 fprintf(stdout,"Tile_1 '' '' '' '' '' ''\n");
191 fprintf(stdout,"...\n");
192 fprintf(stdout,"Tile_Nt '' '' '' '' '' ''\n");
193 fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
194 fprintf(stdout,"...\n");
195 fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n");
197 fprintf(stdout,"MaxDisto\n");
199 fprintf(stdout,"TotalDisto\n\n");
202 OPJ_PROG_ORDER give_progression(char progression[5])
204 if (progression[0] == 'L' && progression[1] == 'R'
205 && progression[2] == 'C' && progression[3] == 'P') {
208 if (progression[0] == 'R' && progression[1] == 'L'
209 && progression[2] == 'C' && progression[3] == 'P') {
212 if (progression[0] == 'R' && progression[1] == 'P'
213 && progression[2] == 'C' && progression[3] == 'L') {
216 if (progression[0] == 'P' && progression[1] == 'C'
217 && progression[2] == 'R' && progression[3] == 'L') {
220 if (progression[0] == 'C' && progression[1] == 'P'
221 && progression[2] == 'R' && progression[3] == 'L') {
235 int main(int argc, char **argv)
237 mj2_cparameters_t mj2_parameters; /* MJ2 compression parameters */
238 opj_cparameters_t *j2k_parameters; /* J2K compression parameters */
239 opj_event_mgr_t event_mgr; /* event manager */
248 long mdat_initpos, offset;
254 int prec = 8;/* DEFAULT */
255 double total_time = 0;
257 memset(&mj2_parameters, 0, sizeof(mj2_cparameters_t));
260 mj2_parameters.w = 352; // CIF default value
261 mj2_parameters.h = 288; // CIF default value
262 mj2_parameters.CbCr_subsampling_dx = 2; // CIF default value
263 mj2_parameters.CbCr_subsampling_dy = 2; // CIF default value
264 mj2_parameters.frame_rate = 25;
265 mj2_parameters.prec = 8; /* DEFAULT */
266 mj2_parameters.enumcs = ENUMCS_SYCC; /* FIXME: ENUMCS_YUV420 */
267 mj2_parameters.meth = 1; /* enumerated color space */
270 configure the event callbacks (not required)
271 setting of each callback is optionnal
273 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
274 event_mgr.error_handler = error_callback;
275 event_mgr.warning_handler = warning_callback;
276 event_mgr.info_handler = NULL;
278 /* set J2K encoding parameters to default values */
279 opj_set_default_encoder_parameters(&mj2_parameters.j2k_parameters);
280 j2k_parameters = &mj2_parameters.j2k_parameters;
282 /* Create comment for codestream */
283 if(j2k_parameters->cp_comment == NULL) {
284 const char comment[] = "Created by OpenJPEG version ";
285 const size_t clen = strlen(comment);
286 const char *version = opj_version();
287 j2k_parameters->cp_comment = (char*)malloc(clen+strlen(version)+1);
288 sprintf(j2k_parameters->cp_comment,"%s%s", comment, version);
292 int c = opj_getopt(argc, argv,
293 "i:o:r:q:f:t:n:c:b:p:s:d:P:S:E:M:R:T:C:I:W:F:D:h");
297 case 'i': /* IN fill */
299 char *infile = opj_optarg;
311 if ((S1 == 'y' && S2 == 'u' && S3 == 'v')
312 || (S1 == 'Y' && S2 == 'U' && S3 == 'V')) {
313 mj2_parameters.decod_format = YUV_DFMT;
317 "!! Unrecognized format for infile : %c%c%c [accept only *.yuv] !!\n\n",
321 strncpy(mj2_parameters.infile, infile, sizeof(mj2_parameters.infile)-1);
324 /* ----------------------------------------------------- */
325 case 'o': /* OUT fill */
327 char *outfile = opj_optarg;
338 outfile = opj_optarg;
340 if ((S1 == 'm' && S2 == 'j' && S3 == '2')
341 || (S1 == 'M' && S2 == 'J' && S3 == '2'))
342 mj2_parameters.cod_format = MJ2_CFMT;
345 "Unknown output format image *.%c%c%c [only *.mj2]!! \n",
349 strncpy(mj2_parameters.outfile, outfile, sizeof(mj2_parameters.outfile)-1);
352 /* ----------------------------------------------------- */
353 case 'r': /* rates rates/distorsion */
357 while (sscanf(s, "%f", &rate) == 1) {
358 j2k_parameters->tcp_rates[j2k_parameters->tcp_numlayers] = rate * 2;
359 j2k_parameters->tcp_numlayers++;
360 while (*s && *s != ',') {
367 j2k_parameters->cp_disto_alloc = 1;
370 /* ----------------------------------------------------- */
371 case 'q': /* add fixed_quality */
373 while (sscanf(s, "%f", &j2k_parameters->tcp_distoratio[j2k_parameters->tcp_numlayers]) == 1) {
374 j2k_parameters->tcp_numlayers++;
375 while (*s && *s != ',') {
382 j2k_parameters->cp_fixed_quality = 1;
385 /* ----------------------------------------------------- */
386 case 'f': /* mod fixed_quality (before : -q) */
388 int *row = NULL, *col = NULL;
389 int numlayers = 0, numresolution = 0, matrix_width = 0;
392 sscanf(s, "%d", &numlayers);
397 j2k_parameters->tcp_numlayers = numlayers;
398 numresolution = j2k_parameters->numresolution;
399 matrix_width = numresolution * 3;
400 j2k_parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
403 for (i = 0; i < numlayers; i++) {
404 row = &j2k_parameters->cp_matrice[i * matrix_width];
406 j2k_parameters->tcp_rates[i] = 1;
407 sscanf(s, "%d,", &col[0]);
413 for (j = 1; j < numresolution; j++) {
415 sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
424 if (i < numlayers - 1)
427 j2k_parameters->cp_fixed_alloc = 1;
430 /* ----------------------------------------------------- */
431 case 't': /* tiles */
432 sscanf(opj_optarg, "%d,%d", &j2k_parameters->cp_tdx, &j2k_parameters->cp_tdy);
433 j2k_parameters->tile_size_on = OPJ_TRUE;
435 /* ----------------------------------------------------- */
436 case 'n': /* resolution */
437 sscanf(opj_optarg, "%d", &j2k_parameters->numresolution);
439 /* ----------------------------------------------------- */
440 case 'c': /* precinct dimension */
445 char *s = opj_optarg;
448 sscanf(s, "[%d,%d]%c", &j2k_parameters->prcw_init[res_spec],
449 &j2k_parameters->prch_init[res_spec], &sep);
450 j2k_parameters->csty |= 0x01;
452 s = strpbrk(s, "]") + 2;
455 j2k_parameters->res_spec = res_spec;
459 /* ----------------------------------------------------- */
460 case 'b': /* code-block dimension */
462 int cblockw_init = 0, cblockh_init = 0;
463 sscanf(opj_optarg, "%d,%d", &cblockw_init, &cblockh_init);
464 if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
465 || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
467 "!! Size of code_block error (option -b) !!\n\nRestriction :\n"
468 " * width*height<=4096\n * 4<=width,height<= 1024\n\n");
471 j2k_parameters->cblockw_init = cblockw_init;
472 j2k_parameters->cblockh_init = cblockh_init;
475 /* ----------------------------------------------------- */
476 case 'p': /* progression order */
480 strncpy(progression, opj_optarg, 5);
481 j2k_parameters->prog_order = give_progression(progression);
482 if (j2k_parameters->prog_order == -1) {
483 fprintf(stderr, "Unrecognized progression order "
484 "[LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
489 /* ----------------------------------------------------- */
490 case 's': /* subsampling factor */
492 if (sscanf(opj_optarg, "%d,%d", &j2k_parameters->subsampling_dx,
493 &j2k_parameters->subsampling_dy) != 2) {
494 fprintf(stderr, "'-s' sub-sampling argument error ! [-s dx,dy]\n");
499 /* ----------------------------------------------------- */
500 case 'd': /* coordonnate of the reference grid */
502 if (sscanf(opj_optarg, "%d,%d", &j2k_parameters->image_offset_x0,
503 &j2k_parameters->image_offset_y0) != 2) {
504 fprintf(stderr, "-d 'coordonnate of the reference grid' argument "
505 "error !! [-d x0,y0]\n");
510 /* ----------------------------------------------------- */
511 case 'h': /* Display an help description */
515 /* ----------------------------------------------------- */
518 int numpocs = 0; /* number of progression order change (POC) default 0 */
519 opj_poc_t *POC = NULL; /* POC : used in case of Progression order change */
521 char *s = opj_optarg;
522 POC = j2k_parameters->POC;
524 while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%4s", &POC[numpocs].tile,
525 &POC[numpocs].resno0, &POC[numpocs].compno0,
526 &POC[numpocs].layno1, &POC[numpocs].resno1,
527 &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
528 POC[numpocs].prg1 = give_progression(POC[numpocs].progorder);
530 while (*s && *s != '/') {
538 j2k_parameters->numpocs = numpocs;
541 /* ------------------------------------------------------ */
542 case 'S': /* SOP marker */
543 j2k_parameters->csty |= 0x02;
545 /* ------------------------------------------------------ */
546 case 'E': /* EPH marker */
547 j2k_parameters->csty |= 0x04;
549 /* ------------------------------------------------------ */
550 case 'M': /* Mode switch pas tous au point !! */
551 if (sscanf(opj_optarg, "%d", &value) == 1) {
552 for (i = 0; i <= 5; i++) {
553 int cache = value & (1 << i);
555 j2k_parameters->mode |= (1 << i);
559 /* ------------------------------------------------------ */
562 if (sscanf(opj_optarg, "OI:c=%d,U=%d", &j2k_parameters->roi_compno,
563 &j2k_parameters->roi_shift) != 2) {
564 fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
569 /* ------------------------------------------------------ */
570 case 'T': /* Tile offset */
572 if (sscanf(opj_optarg, "%d,%d", &j2k_parameters->cp_tx0, &j2k_parameters->cp_ty0) != 2) {
573 fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
578 /* ------------------------------------------------------ */
579 case 'C': /* Add a comment */
581 j2k_parameters->cp_comment = (char*)malloc(strlen(opj_optarg) + 1);
582 if(j2k_parameters->cp_comment) {
583 strcpy(j2k_parameters->cp_comment, opj_optarg);
587 /* ------------------------------------------------------ */
588 case 'I': /* reversible or not */
590 j2k_parameters->irreversible = 1;
593 /* ------------------------------------------------------ */
594 case 'W': /* Width and Height and Cb and Cr subsampling in case of YUV format files */
596 (opj_optarg, "%d,%d,%d,%d", &mj2_parameters.w, &mj2_parameters.h, &mj2_parameters.CbCr_subsampling_dx,
597 &mj2_parameters.CbCr_subsampling_dy) != 4) {
598 fprintf(stderr, "-W argument error");
602 /* ------------------------------------------------------ */
603 case 'F': /* Video frame rate */
604 if (sscanf(opj_optarg, "%d", &mj2_parameters.frame_rate) != 1) {
605 fprintf(stderr, "-F argument error");
609 /* ------------------------------------------------------ */
610 case 'D': /* Depth: the precision */
611 if(sscanf(opj_optarg, "%d", &prec) != 1) prec = 0;
621 if (!mj2_parameters.cod_format || !mj2_parameters.decod_format) {
623 "Usage: %s -i yuv-file -o mj2-file (+ options)\n",argv[0]);
626 if(prec < 1 || prec > 16)
628 fprintf(stderr, "Error: Depth %d must be in the range 8 .. 16\n",prec);
631 if ((j2k_parameters->cp_disto_alloc || j2k_parameters->cp_fixed_alloc || j2k_parameters->cp_fixed_quality)
632 && (!(j2k_parameters->cp_disto_alloc ^ j2k_parameters->cp_fixed_alloc ^ j2k_parameters->cp_fixed_quality))) {
633 fprintf(stderr, "Error: options -r -q and -f cannot be used together !!\n");
635 } /* mod fixed_quality */
637 /* if no rate entered, lossless by default */
638 if (j2k_parameters->tcp_numlayers == 0) {
639 j2k_parameters->tcp_rates[0] = 0; /* MOD antonin : losslessbug */
640 j2k_parameters->tcp_numlayers++;
641 j2k_parameters->cp_disto_alloc = 1;
644 if((j2k_parameters->cp_tx0 > j2k_parameters->image_offset_x0) || (j2k_parameters->cp_ty0 > j2k_parameters->image_offset_y0)) {
646 "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
647 j2k_parameters->cp_tx0, j2k_parameters->image_offset_x0, j2k_parameters->cp_ty0, j2k_parameters->image_offset_y0);
651 for (i = 0; i < j2k_parameters->numpocs; i++) {
652 if (j2k_parameters->POC[i].prg == -1) {
654 "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
659 if (j2k_parameters->cp_tdx > mj2_parameters.Dim[0] || j2k_parameters->cp_tdy > mj2_parameters.Dim[1]) {
661 "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
662 j2k_parameters->cp_tdx, mj2_parameters.Dim[0], j2k_parameters->cp_tdy, mj2_parameters.Dim[1]);
666 /* to respect profile - 0 */
667 /* ---------------------- */
669 x1 = !mj2_parameters.Dim[0] ? (mj2_parameters.w - 1) * j2k_parameters->subsampling_dx
670 + 1 : mj2_parameters.Dim[0] + (mj2_parameters.w - 1) * j2k_parameters->subsampling_dx + 1;
671 y1 = !mj2_parameters.Dim[1] ? (mj2_parameters.h - 1) * j2k_parameters->subsampling_dy
672 + 1 : mj2_parameters.Dim[1] + (mj2_parameters.h - 1) * j2k_parameters->subsampling_dy + 1;
673 mj2_parameters.numcomps = 3; /* YUV files only have 3 components */
675 mj2_parameters.prec = prec;
677 j2k_parameters->tcp_mct = 0;
679 mj2file = fopen(mj2_parameters.outfile, "wb");
682 fprintf(stderr, "failed to open %s for writing\n", argv[2]);
686 /* get a MJ2 decompressor handle */
687 cinfo = mj2_create_compress();
688 movie = (opj_mj2_t*)cinfo->mj2_handle;
690 /* catch events using our callbacks and give a local context */
691 opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);
693 /* setup encoder parameters */
694 mj2_setup_encoder(movie, &mj2_parameters);
696 movie->tk[0].num_samples =
697 yuv_num_frames(&movie->tk[0],mj2_parameters.infile);
699 if (movie->tk[0].num_samples == 0) {
703 // One sample per chunk
704 movie->tk[0].chunk = (mj2_chunk_t*)
705 malloc(movie->tk[0].num_samples * sizeof(mj2_chunk_t));
706 movie->tk[0].sample = (mj2_sample_t*)
707 malloc(movie->tk[0].num_samples * sizeof(mj2_sample_t));
709 if (mj2_init_stdmovie(movie)) {
710 fprintf(stderr, "Error with movie initialization");
714 // Writing JP, FTYP and MDAT boxes
715 // Assuming that the JP and FTYP boxes won't be longer than 300 bytes:
716 buf = (unsigned char*)
717 malloc (300 * sizeof(unsigned char));
719 cio = opj_cio_open((opj_common_ptr)movie->cinfo, buf, 300);
722 mj2_write_ftyp(movie, cio);
724 mdat_initpos = cio_tell(cio);
727 cio_write(cio, MJ2_MDAT, 4);
729 fwrite(buf,cio_tell(cio),1,mj2file);
731 offset = cio_tell(cio);
735 for(i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++)
737 if(movie->tk[i].track_type != 0)
739 fprintf(stderr, "Unable to write sound or hint tracks\n");
747 tk->num_chunks = tk->num_samples;
748 numframes = tk->num_samples;
751 fprintf(stderr, "Video Track number %d\n", i);
753 img = mj2_image_create(tk, j2k_parameters);
755 buflen = 2 * (tk->w * tk->h * 8);
756 buf = (unsigned char *) malloc(buflen*sizeof(unsigned char));
758 for(sampleno = 0; sampleno < numframes; sampleno++)
760 double init_time = opj_clock();
763 if(yuvtoimage(tk, img, sampleno, j2k_parameters,
764 mj2_parameters.infile))
766 fprintf(stderr, "Error with frame number %d in YUV file\n", sampleno);
770 /* setup the encoder parameters using the current image and user parameters */
771 opj_setup_encoder(cinfo, j2k_parameters, img);
773 cio = opj_cio_open((opj_common_ptr)movie->cinfo, buf, buflen);
776 cio_write(cio, JP2_JP2C, 4); // JP2C
778 /* encode the image */
779 bSuccess = opj_encode(cinfo, cio, img, NULL);
783 fprintf(stderr, "failed to encode image\n");
787 len = cio_tell(cio) - 8;
789 cio_write(cio, len+8,4);
792 tk->sample[sampleno].sample_size = len+8;
793 tk->sample[sampleno].offset = offset;
794 tk->chunk[sampleno].offset = offset; // There is one sample per chunk
795 fwrite(buf, 1, len+8, mj2file);
798 elapsed_time = opj_clock()-init_time;
799 fprintf(stderr, "Frame number %d/%d encoded in %.2f mseconds\n",
800 sampleno + 1, numframes, elapsed_time*1000);
801 total_time += elapsed_time;
805 opj_image_destroy(img);
809 fseek(mj2file, mdat_initpos, SEEK_SET);
811 buf = (unsigned char*) malloc(4*sizeof(unsigned char));
813 // Init a cio to write box length variable in a little endian way
814 cio = opj_cio_open(NULL, buf, 4);
815 cio_write(cio, offset - mdat_initpos, 4);
816 fwrite(buf, 4, 1, mj2file);
817 fseek(mj2file,0,SEEK_END);
821 buf = (unsigned char*)
822 malloc ((TEMP_BUF+numframes*20) * sizeof(unsigned char));
823 cio = opj_cio_open(movie->cinfo, buf, (TEMP_BUF+numframes*20));
824 mj2_write_moov(movie, cio);
825 fwrite(buf,cio_tell(cio),1,mj2file);
828 fprintf(stdout,"Total encoding time: %.2f s for %d frames (%.1f fps)\n",
829 total_time, numframes, (float)numframes/total_time);
834 /* free remaining compression structures */
835 mj2_destroy_compress(movie);
838 if(j2k_parameters->cp_comment) free(j2k_parameters->cp_comment);
839 if(j2k_parameters->cp_matrice) free(j2k_parameters->cp_matrice);