Help message updated
[openjpeg.git] / mj2 / frames_to_mj2.c
1 /*
2 * Copyright (c) 2003-2004, Fran�ois-Olivier Devaux
3 * Copyright (c) 2002-2004,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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.
26 */
27
28 #include <openjpeg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #include <string.h>
33 #ifndef DONT_HAVE_GETOPT
34 #include <getopt.h>
35 #else
36 #include "compat/getopt.h"
37 #endif
38 #include "mj2_convert.h"
39
40 void help_display()
41 {
42   printf("HELP\n----\n\n");
43   printf("- the -h option displays this help information on screen\n\n");
44
45
46   printf("List of parameters for the coder JPEG 2000:\n");
47   printf("\n");
48   printf("REMARKS:\n");
49   printf("---------\n");
50   printf("\n");
51   printf
52     ("The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
53   printf
54     ("COD and QCD never appear in the tile_header.\n");
55   printf("\n");
56   printf
57     ("- This coder can encode a mega image, a test was made on a 24000x24000 pixels \n");
58   printf
59     ("color image.  You need enough disk space memory (twice the original) to encode \n");
60   printf
61     ("the image,i.e. for a 1.5 GB image you need a minimum of 3GB of disk memory)\n");
62   printf("\n");
63   printf("By default:\n");
64   printf("------------\n");
65   printf("\n");
66   printf(" * Lossless\n");
67   printf(" * 1 tile\n");
68   printf(" * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
69   printf(" * Size of code-block : 64 x 64\n");
70   printf(" * Number of resolutions: 6\n");
71   printf(" * No SOP marker in the codestream\n");
72   printf(" * No EPH marker in the codestream\n");
73   printf(" * No sub-sampling in x or y direction\n");
74   printf(" * No mode switch activated\n");
75   printf(" * Progression order: LRCP\n");
76   printf(" * No index file\n");
77   printf(" * No ROI upshifted\n");
78   printf(" * No offset of the origin of the image\n");
79   printf(" * No offset of the origin of the tiles\n");
80   printf(" * Reversible DWT 5-3\n");
81   printf("\n");
82   printf("Parameters:\n");
83   printf("------------\n");
84   printf("\n");
85   printf
86     ("Required Parameters (except with -h):\n");
87   printf("\n");
88   printf
89     ("-i           : source file  (-i source.pnm also *.pgm, *.ppm) \n");
90   printf("\n");
91   printf
92     ("-o           : destination file (-o dest.j2k or .jp2) \n");
93   printf("\n");
94   printf
95     ("Optional Parameters:\n");
96   printf("\n");
97   printf("-h           : display the help information \n ");
98   printf("\n");
99   printf("-r           : different compression ratios for successive layers (-r 20,10,5)\n ");
100   printf("               - The rate specified for each quality level is the desired \n");
101   printf("                 compression factor.\n");
102   printf("                 Example: -r 20,10,1 means quality 1: compress 20x, \n");
103   printf("                   quality 2: compress 10x and quality 3: compress lossless\n");
104   printf("\n");
105   printf("               (options -r and -q cannot be used together)\n ");
106   printf("\n");
107
108   printf("-q           : different psnr for successive layers (-q 30,40,50) \n ");
109
110   printf("               (options -r and -q cannot be used together)\n ");
111
112   printf("\n");
113   printf("-n           : number of resolutions (-n 3) \n");
114   printf("\n");
115   printf("-b           : size of code block (-b 32,32) \n");
116   printf("\n");
117   printf("-c           : size of precinct (-c 128,128) \n");
118   printf("\n");
119   printf("-t           : size of tile (-t 512,512) \n");
120   printf("\n");
121   printf
122     ("-p           : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
123   printf("\n");
124   printf
125     ("-s           : subsampling factor (-s 2,2) [-s X,Y] \n");
126   printf("           Remark: subsampling bigger than 2 can produce error\n");
127   printf("\n");
128   printf
129     ("-SOP         : write SOP marker before each packet \n");
130   printf("\n");
131   printf
132     ("-EPH         : write EPH marker after each header packet \n");
133   printf("\n");
134   printf
135     ("-M           : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
136   printf
137     ("                 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
138   printf
139     ("                 Indicate multiple modes by adding their values. \n");
140   printf
141     ("                 ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
142   printf("\n");
143   printf
144     ("-x           : create an index file *.Idx (-x index_name.Idx) \n");
145   printf("\n");
146   printf
147     ("-ROI         : c=%%d,U=%%d : quantization indices upshifted \n");
148   printf
149     ("               for component c=%%d [%%d = 0,1,2]\n");
150   printf
151     ("               with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n");
152   printf("\n");
153   printf
154     ("-d           : offset of the origin of the image (-d 150,300) \n");
155   printf("\n");
156   printf
157     ("-T           : offset of the origin of the tiles (-T 100,75) \n");
158   printf("\n");
159   printf("-I           : use the irreversible DWT 9-7 (-I) \n");
160   printf("\n");
161   printf("-W           : image width, height and the dx and dy subsampling \n");
162   printf("               of the Cb and Cr components for YUV files \n");
163   printf("               (default is '352,288,2,2' for CIF format's 352x288 and 4:2:0)\n");
164   printf("\n");
165   printf("-F           : video frame rate (set to 25 by default)\n");
166
167   printf("\n");
168   printf("IMPORTANT:\n");
169   printf("-----------\n");
170   printf("\n");
171   printf("The index file has the structure below:\n");
172   printf("---------------------------------------\n");
173   printf("\n");
174   printf("Image_height Image_width\n");
175   printf("progression order\n");
176   printf("Tiles_size_X Tiles_size_Y\n");
177   printf("Components_nb\n");
178   printf("Layers_nb\n");
179   printf("decomposition_levels\n");
180   printf("[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
181   printf("   [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
182   printf("Main_header_end_position\n");
183   printf("Codestream_size\n");
184   printf("Tile_0 start_pos end_Theader end_pos TotalDisto NumPix MaxMSE\n");
185   printf("Tile_1   ''           ''        ''        ''       ''    ''\n");
186   printf("...\n");
187   printf("Tile_Nt   ''           ''        ''        ''       ''    ''\n");
188   printf("Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
189   printf("...\n");
190   printf("Tpacket_Np ''   ''    ''   ''    ''       ''       ''     ''\n");
191
192   printf("MaxDisto\n");
193
194   printf("TotalDisto\n\n");
195 }
196
197 int give_progression(char progression[4])
198 {
199   if (progression[0] == 'L' && progression[1] == 'R'
200       && progression[2] == 'C' && progression[3] == 'P') {
201     return 0;
202   } else {
203     if (progression[0] == 'R' && progression[1] == 'L'
204         && progression[2] == 'C' && progression[3] == 'P') {
205       return 1;
206     } else {
207       if (progression[0] == 'R' && progression[1] == 'P'
208           && progression[2] == 'C' && progression[3] == 'L') {
209         return 2;
210       } else {
211         if (progression[0] == 'P' && progression[1] == 'C'
212             && progression[2] == 'R' && progression[3] == 'L') {
213           return 3;
214         } else {
215           if (progression[0] == 'C' && progression[1] == 'P'
216               && progression[2] == 'R' && progression[3] == 'L') {
217             return 4;
218           } else {
219             return -1;
220           }
221         }
222       }
223     }
224   }
225 }
226
227 double dwt_norms_97[4][10] = {
228   {1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
229   {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
230   {2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
231   {2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
232 };
233
234 int floorlog2(int a)
235 {
236   int l;
237   for (l = 0; a > 1; l++) {
238     a >>= 1;
239   }
240   return l;
241 }
242
243 void encode_stepsize(int stepsize, int numbps, int *expn, int *mant)
244 {
245   int p, n;
246   p = floorlog2(stepsize) - 13;
247   n = 11 - floorlog2(stepsize);
248   *mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
249   *expn = numbps - p;
250 }
251
252 void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec)
253 {
254   int numbands, bandno;
255   numbands = 3 * tccp->numresolutions - 2;
256   for (bandno = 0; bandno < numbands; bandno++) {
257     double stepsize;
258
259     int resno, level, orient, gain;
260     resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1;
261     orient = bandno == 0 ? 0 : (bandno - 1) % 3 + 1;
262     level = tccp->numresolutions - 1 - resno;
263     gain =
264       tccp->qmfbid == 0 ? 0 : (orient ==
265                                0 ? 0 : (orient == 1
266                                         || orient == 2 ? 1 : 2));
267     if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
268       stepsize = 1.0;
269     } else {
270       double norm = dwt_norms_97[orient][level];
271       stepsize = (1 << (gain + 1)) / norm;
272     }
273     encode_stepsize((int) floor(stepsize * 8192.0), prec + gain,
274                     &tccp->stepsizes[bandno].expn,
275                     &tccp->stepsizes[bandno].mant);
276   }
277 }
278
279 int main(int argc, char **argv)
280 {
281   int NumResolution, numD_min;  /*   NumResolution : number of resolution                     */
282   int Tile_arg;                 /*   Tile_arg = 0 (not in argument) ou = 1 (in argument)      */
283   int CSty;                     /*   CSty : coding style                                      */
284   int Prog_order;               /*   progression order (default LRCP)                         */
285   char progression[4];
286   int numpocs, numpocs_tile;    /*   Number of progression order change (POC) default 0       */
287   int prcw_init[J2K_MAXRLVLS];  /*   Initialisation Precinct width                            */
288   int prch_init[J2K_MAXRLVLS];  /*   Initialisation Precinct height                           */
289   //int prcw_init, prch_init;                     /*   Initialisation precincts' size                           */
290   int cblockw_init, cblockh_init;       /*   Initialisation codeblocks' size                          */
291   int mode, value;              /*   Mode switch (cblk_style)                                 */
292   int subsampling_dx, subsampling_dy;   /* subsampling value for dx and dy                    */
293   int ROI_compno, ROI_shift;    /*   region of interrest                                      */
294   int Dim[2];                   /*   portion of the image coded                               */
295   int TX0, TY0;                 /*   tile off-set                                             */
296   mj2_movie_t movie;
297   j2k_cp_t cp, cp_init;         /*   cp_init is used to initialise in multiple tiles          */
298   j2k_tcp_t *tcp, *tcp_init;    /*   tcp_init is used to initialise in multiple tile          */
299   j2k_poc_t POC[32];            /*   POC : used in case of Progression order change           */
300   j2k_poc_t *tcp_poc;
301   j2k_tccp_t *tccp;
302   int i, tileno, j;
303   char *infile = 0;
304   char *outfile = 0;
305   char *index = 0;
306   char *s, S1, S2, S3;
307   int ir = 0;
308   int res_spec = 0;             /*   For various precinct sizes specification                 */
309   char sep;
310   int w;                        /*   Width of YUV file                                        */
311   int h;                        /*   Height of YUV file                                       */
312   int CbCr_subsampling_dx;      /*   Sample rate of YUV 4:4:4 4:2:2 or 4:2:0                  */
313   int CbCr_subsampling_dy;      /*   Sample rate of YUV 4:4:4 4:2:2 or 4:2:0                  */
314   int output_image_type = -1;
315   int frame_rate;               /*   Video Frame Rate                                         */
316   int numcomps;                 /*   In YUV files, numcomps always considered as 3            */
317   int prec;                     /*   In YUV files, precision always considered as 8           */
318   int x1, y1;
319
320
321   /* default value */
322   /* ------------- */
323   NumResolution = 6;
324   CSty = 0;
325   cblockw_init = 64;
326   cblockh_init = 64;
327   cp.tw = 1;
328   cp.th = 1;
329   cp.index_on = 0;
330   Prog_order = 0;
331   numpocs = 0;
332   mode = 0;
333   subsampling_dx = 1;
334   subsampling_dy = 1;
335   ROI_compno = -1;              /* no ROI */
336   ROI_shift = 0;
337   Dim[0] = 0;
338   Dim[1] = 0;
339   TX0 = 0;
340   TY0 = 0;
341   cp.comment = NULL;
342   cp.disto_alloc = 0;
343   cp.fixed_alloc = 0;
344   cp.fixed_quality = 0;         //add fixed_quality
345   w = 352;                      // CIF default value
346   h = 288;                      // CIF default value
347   CbCr_subsampling_dx = 2;      // CIF default value
348   CbCr_subsampling_dy = 2;      // CIF default value
349   frame_rate = 25;
350
351
352   Tile_arg = 0;
353   cp_init.tcps = (j2k_tcp_t *) malloc(sizeof(j2k_tcp_t));       /* initialisation if only one tile */
354   tcp_init = &cp_init.tcps[0];
355   tcp_init->numlayers = 0;
356
357   cp.intermed_file = 0;         // Don't store each tile in a file during processing 
358
359   while (1) {
360     int c = getopt(argc, argv,
361                    "i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I:W:F:");
362     if (c == -1)
363       break;
364     switch (c) {
365     case 'i':                   /* IN fill */
366       infile = optarg;
367       s = optarg;
368       while (*s) {
369         s++;
370       }
371       s--;
372       S3 = *s;
373       s--;
374       S2 = *s;
375       s--;
376       S1 = *s;
377
378       if ((S1 == 'y' && S2 == 'u' && S3 == 'v')
379           || (S1 == 'Y' && S2 == 'U' && S3 == 'V')) {
380         cp.image_type = 3;
381         break;
382       }
383       fprintf(stderr,
384               "!! Unrecognized format for infile : %c%c%c [accept only *.yuv] !!\n\n",
385               S1, S2, S3);
386       return 1;
387       break;
388       /* ----------------------------------------------------- */
389     case 'o':                   /* OUT fill */
390       outfile = optarg;
391       while (*outfile) {
392         outfile++;
393       }
394       outfile--;
395       S3 = *outfile;
396       outfile--;
397       S2 = *outfile;
398       outfile--;
399       S1 = *outfile;
400
401       outfile = optarg;
402
403       if ((S1 == 'm' && S2 == 'j' && S3 == '2')
404           || (S1 == 'M' && S2 == 'J' && S3 == '2'))
405         cp.JPEG2000_format = 2;
406       else {
407         fprintf(stderr,
408                 "Unknown output format image *.%c%c%c [only *.mj2]!! \n",
409                 S1, S2, S3);
410         return 1;
411       }
412
413
414
415       break;
416       /* ----------------------------------------------------- */
417     case 'r':                   /* rates rates/distorsion */
418       s = optarg;
419       while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers])
420              == 1) {
421         tcp_init->numlayers++;
422         while (*s && *s != ',') {
423           s++;
424         }
425         if (!*s)
426           break;
427         s++;
428       }
429       cp.disto_alloc = 1;
430       cp.matrice = NULL;
431       break;
432       /* ----------------------------------------------------- */
433     case 'q':                   /* add fixed_quality */
434       s = optarg;
435       while (sscanf
436              (s, "%f", &tcp_init->distoratio[tcp_init->numlayers]) == 1) {
437         tcp_init->numlayers++;
438         while (*s && *s != ',') {
439           s++;
440         }
441         if (!*s)
442           break;
443         s++;
444       }
445       cp.fixed_quality = 1;
446       cp.matrice = NULL;
447       break;
448       /* dda */
449       /* ----------------------------------------------------- */
450     case 'f':                   /* mod fixed_quality (before : -q) */
451       s = optarg;
452       sscanf(s, "%d", &tcp_init->numlayers);
453       s++;
454       if (tcp_init->numlayers > 9)
455         s++;
456       cp.matrice =
457         (int *) malloc(tcp_init->numlayers * NumResolution * 3 *
458                        sizeof(int));
459       s = s + 2;
460       for (i = 0; i < tcp_init->numlayers; i++) {
461         tcp_init->rates[i] = 1;
462         sscanf(s, "%d,", &cp.matrice[i * NumResolution * 3]);
463         s += 2;
464         if (cp.matrice[i * NumResolution * 3] > 9)
465           s++;
466         cp.matrice[i * NumResolution * 3 + 1] = 0;
467         cp.matrice[i * NumResolution * 3 + 2] = 0;
468         for (j = 1; j < NumResolution; j++) {
469           sscanf(s, "%d,%d,%d",
470                  &cp.matrice[i * NumResolution * 3 + j * 3 + 0],
471                  &cp.matrice[i * NumResolution * 3 + j * 3 + 1],
472                  &cp.matrice[i * NumResolution * 3 + j * 3 + 2]);
473           s += 6;
474           if (cp.matrice[i * NumResolution * 3 + j * 3] > 9)
475             s++;
476           if (cp.matrice[i * NumResolution * 3 + j * 3 + 1] > 9)
477             s++;
478           if (cp.matrice[i * NumResolution * 3 + j * 3 + 2] > 9)
479             s++;
480         }
481         if (i < tcp_init->numlayers - 1)
482           s++;
483       }
484       cp.fixed_alloc = 1;
485       break;
486       /* ----------------------------------------------------- */
487     case 't':                   /* tiles */
488       sscanf(optarg, "%d,%d", &cp.tdx, &cp.tdy);
489       Tile_arg = 1;
490       break;
491       /* ----------------------------------------------------- */
492     case 'n':                   /* resolution */
493       sscanf(optarg, "%d", &NumResolution);
494       break;
495       /* ----------------------------------------------------- */
496     case 'c':                   /* precinct dimension */
497       s = optarg;
498       do {
499         sep = 0;
500         sscanf(s, "[%d,%d]%c", &prcw_init[res_spec],
501                &prch_init[res_spec], &sep);
502         CSty |= 0x01;
503         res_spec++;
504         s = strpbrk(s, "]") + 2;
505       } while (sep == ',');
506       break;
507       /* ----------------------------------------------------- */
508     case 'b':                   /* code-block dimension */
509       sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
510       if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
511           || cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
512         fprintf(stderr,
513                 "!! Size of code_block error (option -b) !!\n\nRestriction :\n    * width*height<=4096\n    * 4<=width,height<= 1024\n\n");
514         return 1;
515       }
516       break;
517       /* ----------------------------------------------------- */
518     case 'x':                   /* creation of index file */
519       index = optarg;
520       cp.index_on = 1;
521       break;
522       /* ----------------------------------------------------- */
523     case 'p':                   /* progression order */
524       s = optarg;
525       for (i = 0; i < 4; i++) {
526         progression[i] = *s;
527         s++;
528       }
529       Prog_order = give_progression(progression);
530
531       if (Prog_order == -1) {
532         fprintf(stderr,
533                 "Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
534         return 1;
535       }
536       break;
537       /* ----------------------------------------------------- */
538     case 's':                   /* subsampling factor */
539       if (sscanf(optarg, "%d,%d", &subsampling_dx, &subsampling_dy)
540           != 2) {
541         fprintf(stderr,
542                 "'-s' sub-sampling argument error !  [-s dx,dy]\n");
543         return 1;
544       }
545       break;
546       /* ----------------------------------------------------- */
547     case 'd':                   /* coordonnate of the reference grid */
548       if (sscanf(optarg, "%d,%d", &Dim[0], &Dim[1]) != 2) {
549         fprintf(stderr,
550                 "-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
551         return 1;
552       }
553       break;
554       /* ----------------------------------------------------- */
555     case 'h':                   /* Display an help description */
556       help_display();
557       return 0;
558       break;
559       /* ----------------------------------------------------- */
560     case 'P':                   /* POC */
561       fprintf(stderr, "/----------------------------------\\\n");
562       fprintf(stderr, "|  POC option not fully tested !!  |\n");
563       fprintf(stderr, "\\----------------------------------/\n");
564
565       s = optarg;
566       while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
567                     &POC[numpocs].resno0, &POC[numpocs].compno0,
568                     &POC[numpocs].layno1, &POC[numpocs].resno1,
569                     &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
570         POC[numpocs].prg = give_progression(POC[numpocs].progorder);
571         /* POC[numpocs].tile; */
572         numpocs++;
573         while (*s && *s != '/') {
574           s++;
575         }
576         if (!*s)
577           break;
578         s++;
579       }
580       break;
581       /* ------------------------------------------------------ */
582     case 'S':                   /* SOP marker */
583       CSty |= 0x02;
584       break;
585       /* ------------------------------------------------------ */
586     case 'E':                   /* EPH marker */
587       CSty |= 0x04;
588       break;
589       /* ------------------------------------------------------ */
590     case 'M':                   /* Mode switch pas tous au point !! */
591       if (sscanf(optarg, "%d", &value) == 1) {
592         for (i = 0; i <= 5; i++) {
593           int cache = value & (1 << i);
594           if (cache)
595             mode |= (1 << i);
596         }
597       }
598       break;
599       /* ------------------------------------------------------ */
600     case 'R':                   /* ROI */
601       if (sscanf(optarg, "OI:c=%d,U=%d", &ROI_compno, &ROI_shift) != 2) {
602         fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
603         return 1;
604       }
605       break;
606       /* ------------------------------------------------------ */
607     case 'T':                   /* Tile offset */
608       if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) {
609         fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
610         return 1;
611       }
612       break;
613       /* ------------------------------------------------------ */
614     case 'C':                   /* Add a comment */
615       cp.comment = optarg;
616       break;
617       /* ------------------------------------------------------ */
618     case 'I':                   /* reversible or not */
619       ir = 1;
620       break;
621       /* ------------------------------------------------------ */
622     case 'W':                   /* Width and Height and Cb and Cr subsampling in case of YUV format files */
623       if (sscanf
624           (optarg, "%d,%d,%d,%d", &w, &h, &CbCr_subsampling_dx,
625            &CbCr_subsampling_dy) != 4) {
626         fprintf(stderr, "-W argument error");
627         return 1;
628       }
629       break;
630       /* ------------------------------------------------------ */
631     case 'F':                   /* Video frame rate */
632       if (sscanf(optarg, "%d", &frame_rate) != 1) {
633         fprintf(stderr, "-F argument error");
634         return 1;
635       }
636       break;
637       /* ------------------------------------------------------ */
638     default:
639       return 1;
640     }
641   }
642
643   cp.tx0 = TX0;
644   cp.ty0 = TY0;
645
646   /* Error messages */
647   /* -------------- */
648   if (!infile || !outfile) {
649     fprintf(stderr,
650             "usage: frames_to_mj2 -i yuv-file -o mj2-file (+ options)\n");
651     return 1;
652   }
653
654   if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality)
655       && (!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) {
656     fprintf(stderr,
657             "Error: options -r -q and -f can not be used together !!\n");
658     return 1;
659   }                             // mod fixed_quality
660
661   /* if no rate entered, lossless by default */
662   if (tcp_init->numlayers == 0) {
663     tcp_init->rates[tcp_init->numlayers] = 0;   //MOD antonin : losslessbug
664     tcp_init->numlayers++;
665     cp.disto_alloc = 1;
666   }
667
668   if (TX0 > Dim[0] || TY0 > Dim[1]) {
669     fprintf(stderr,
670             "Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
671             TX0, Dim[0], TY0, Dim[1]);
672     return 1;
673   }
674
675   for (i = 0; i < numpocs; i++) {
676     if (POC[i].prg == -1) {
677       fprintf(stderr,
678               "Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
679               i + 1);
680     }
681   }
682
683   /* to respect profile - 0 */
684   /* ---------------------- */
685   numD_min = 0;
686
687   x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
688                                                           1) *
689     subsampling_dx + 1;
690   y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
691                                                           1) *
692     subsampling_dy + 1;
693
694   if (Tile_arg == 1) {
695     cp.tw = int_ceildiv(x1 - cp.tx0, cp.tdx);
696     cp.th = int_ceildiv(y1 - cp.ty0, cp.tdy);
697   } else {
698     cp.tdx = x1 - cp.tx0;
699     cp.tdy = y1 - cp.ty0;
700   }
701
702
703   /* Initialization for PPM marker */
704   cp.ppm = 0;
705   cp.ppm_data = NULL;
706   cp.ppm_previous = 0;
707   cp.ppm_store = 0;
708
709   numcomps = 3;                 /* Because YUV files only have 3 components */
710
711   tcp_init->mct = 0;            /* No component transform needed */
712
713   prec = 8;                     /* Because in YUV files, components have 8-bit depth */
714
715   /* Init the mutiple tiles */
716   /* ---------------------- */
717   cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
718
719   for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
720     tcp = &cp.tcps[tileno];
721     tcp->numlayers = tcp_init->numlayers;
722     for (j = 0; j < tcp->numlayers; j++) {
723       if (cp.fixed_quality)     // add fixed_quality
724         tcp->distoratio[j] = tcp_init->distoratio[j];
725       else
726         tcp->rates[j] = tcp_init->rates[j];
727     }
728     tcp->csty = CSty;
729     tcp->prg = Prog_order;
730     tcp->mct = tcp_init->mct;
731     tcp->ppt = 0;
732     tcp->ppt_data = NULL;
733     tcp->ppt_store = 0;
734
735     numpocs_tile = 0;
736     tcp->POC = 0;
737     if (numpocs) {
738       /* intialisation of POC */
739       tcp->POC = 1;
740       for (i = 0; i < numpocs; i++) {
741         if (tileno == POC[i].tile - 1 || POC[i].tile == -1) {
742           tcp_poc = &tcp->pocs[numpocs_tile];
743           tcp_poc->resno0 = POC[numpocs_tile].resno0;
744           tcp_poc->compno0 = POC[numpocs_tile].compno0;
745           tcp_poc->layno1 = POC[numpocs_tile].layno1;
746           tcp_poc->resno1 = POC[numpocs_tile].resno1;
747           tcp_poc->compno1 = POC[numpocs_tile].compno1;
748           tcp_poc->prg = POC[numpocs_tile].prg;
749           tcp_poc->tile = POC[numpocs_tile].tile;
750           numpocs_tile++;
751         }
752       }
753     }
754     tcp->numpocs = numpocs_tile;
755     tcp->tccps = (j2k_tccp_t *) malloc(numcomps * sizeof(j2k_tccp_t));
756
757     for (i = 0; i < numcomps; i++) {
758       tccp = &tcp->tccps[i];
759       tccp->csty = CSty & 0x01; /* 0 => one precinct || 1 => custom precinct  */
760       tccp->numresolutions = NumResolution;
761       tccp->cblkw = int_floorlog2(cblockw_init);
762       tccp->cblkh = int_floorlog2(cblockh_init);
763       tccp->cblksty = mode;
764       tccp->qmfbid = ir ? 0 : 1;
765       tccp->qntsty = ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
766       tccp->numgbits = 2;
767       if (i == ROI_compno)
768         tccp->roishift = ROI_shift;
769       else
770         tccp->roishift = 0;
771       if (CSty & J2K_CCP_CSTY_PRT) {
772         int p = 0;
773         for (j = tccp->numresolutions - 1; j >= 0; j--) {
774           if (p < res_spec) {
775             if (prcw_init[p] < 1)
776               tccp->prcw[j] = 1;
777             else
778               tccp->prcw[j] = int_floorlog2(prcw_init[p]);
779
780             if (prch_init[p] < 1)
781               tccp->prch[j] = 1;
782             else
783               tccp->prch[j] = int_floorlog2(prch_init[p]);
784           } else {
785             int size_prcw, size_prch;
786             size_prcw = prcw_init[res_spec - 1] >> (p - (res_spec - 1));
787             size_prch = prch_init[res_spec - 1] >> (p - (res_spec - 1));
788             if (size_prcw < 1)
789               tccp->prcw[j] = 1;
790             else
791               tccp->prcw[j] = int_floorlog2(size_prcw);
792             if (size_prch < 1)
793               tccp->prch[j] = 1;
794             else
795               tccp->prch[j] = int_floorlog2(size_prch);
796           }
797           p++;
798           /*printf("\nsize precinct pour level %d : %d,%d\n", j,
799              tccp->prcw[j], tccp->prch[j]); */
800         }
801       } else {
802         for (j = 0; j < tccp->numresolutions; j++) {
803           tccp->prcw[j] = 15;
804           tccp->prch[j] = 15;
805         }
806       }
807       calc_explicit_stepsizes(tccp, prec);
808     }
809   }
810
811   movie.tk = (mj2_tk_t *) malloc(sizeof(mj2_tk_t));
812   movie.num_vtk = 1;
813   movie.num_stk = 0;
814   movie.num_htk = 0;
815
816   movie.tk[0].imagefile = infile;
817   movie.tk[0].track_type = 0;   // Video Track
818   movie.tk[0].track_ID = 1;
819   movie.mj2file = outfile;
820   movie.tk[0].subsampling_dx = subsampling_dx;
821   movie.tk[0].subsampling_dy = subsampling_dy;
822   movie.tk[0].Dim[0] = Dim[0];
823   movie.tk[0].Dim[1] = Dim[1];
824   movie.tk[0].w = w;
825   movie.tk[0].h = h;
826   movie.tk[0].CbCr_subsampling_dx = CbCr_subsampling_dx;
827   movie.tk[0].CbCr_subsampling_dy = CbCr_subsampling_dy;
828   movie.tk[0].sample_rate = frame_rate;
829
830   if (mj2_init_stdmovie(&movie)) {
831     fprintf(stderr, "Error with movie initialization");
832     return 1;
833   };
834   if (mj2_encode(&movie, &cp, index)) {
835     fprintf(stderr, "Error encoding movie");
836     return 1;
837   };
838   mj2_memory_free(&movie);
839
840   /* Free cp memory */
841   free(cp_init.tcps);
842   if (tcp_init->numlayers > 9)
843     free(cp.matrice);
844   for (tileno = 0; tileno < cp.tw * cp.th; tileno++)
845     free(cp.tcps[tileno].tccps);
846   free(cp.tcps);
847
848   return 0;
849 }