opj_encoder_set_extra_options(): add a GUARD_BITS=value option
[openjpeg.git] / src / bin / jp2 / opj_compress.c
index 58fdaaecf97a9118c0dbe0c5e8c33b8cc2de01bc..85e1e56669705d0490816348ea84a65f029bb839 100644 (file)
@@ -44,6 +44,7 @@
 #include <stdlib.h>
 #include <math.h>
 #include <assert.h>
+#include <limits.h>
 
 #ifdef _WIN32
 #include "windirent.h"
@@ -141,7 +142,7 @@ static void encode_help_display(void)
     fprintf(stdout, "-i <file>\n");
     fprintf(stdout, "    Input file\n");
     fprintf(stdout,
-            "    Known extensions are <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|RAW|RAWL|TGA>\n");
+            "    Known extensions are <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|TIFF|RAW|YUV|RAWL|TGA>\n");
     fprintf(stdout, "    If used, '-o <file>' must be provided\n");
     fprintf(stdout, "-o <compressed file>\n");
     fprintf(stdout, "    Output file (accepted extensions are j2k or jp2).\n");
@@ -153,12 +154,12 @@ static void encode_help_display(void)
     fprintf(stdout, "    Required only if -ImgDir is used\n");
     fprintf(stdout,
             "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
-    fprintf(stdout, "    Characteristics of the raw input image\n");
+    fprintf(stdout, "    Characteristics of the raw or yuv input image\n");
     fprintf(stdout,
             "    If subsampling is omitted, 1x1 is assumed for all components\n");
-    fprintf(stdout, "      Example: -F 512,512,3,8,u@1x1:2x2:2x2\n");
+    fprintf(stdout, "     Example: -F 512,512,3,8,u@1x1:2x2:2x2\n");
     fprintf(stdout,
-            "               for raw 512x512 image with 4:2:0 subsampling\n");
+            "              for raw or yuv 512x512 size with 4:2:0 subsampling\n");
     fprintf(stdout, "    Required only if RAW or RAWL input file is provided.\n");
     fprintf(stdout, "\n");
     fprintf(stdout, "Optional Parameters:\n");
@@ -186,6 +187,11 @@ static void encode_help_display(void)
     fprintf(stdout,
             "    It corresponds to the number of DWT decompositions +1. \n");
     fprintf(stdout, "    Default: 6.\n");
+    fprintf(stdout, "-TargetBitDepth <target bit depth>\n");
+    fprintf(stdout, "    Target bit depth.\n");
+    fprintf(stdout, "    Number of bits per component to use from input image\n");
+    fprintf(stdout, "    if all bits are unwanted.\n");
+    fprintf(stdout, "    (Currently only implemented for TIF.)\n");
     fprintf(stdout, "-b <cblk width>,<cblk height>\n");
     fprintf(stdout,
             "    Code-block size. The dimension must respect the constraint \n");
@@ -203,9 +209,9 @@ static void encode_help_display(void)
     fprintf(stdout,
             "    to the highest resolution level and subsequent records to lower \n");
     fprintf(stdout,
-            "    resolution levels. The last specified record is right-shifted for each \n");
+            "    resolution levels. The last specified record is halved successively for each \n");
     fprintf(stdout, "    remaining lower resolution levels.\n");
-    fprintf(stdout, "    Default: 215x215 at each resolution.\n");
+    fprintf(stdout, "    Default: 2^15x2^15 at each resolution.\n");
     fprintf(stdout, "-t <tile width>,<tile height>\n");
     fprintf(stdout, "    Tile size.\n");
     fprintf(stdout,
@@ -229,6 +235,10 @@ static void encode_help_display(void)
     fprintf(stdout, "    Write SOP marker before each packet.\n");
     fprintf(stdout, "-EPH\n");
     fprintf(stdout, "    Write EPH marker after each header packet.\n");
+    fprintf(stdout, "-PLT\n");
+    fprintf(stdout, "    Write PLT marker in tile-part header.\n");
+    fprintf(stdout, "-TLM\n");
+    fprintf(stdout, "    Write TLM marker in main header.\n");
     fprintf(stdout, "-M <key value>\n");
     fprintf(stdout, "    Mode switch.\n");
     fprintf(stdout, "    [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
@@ -287,11 +297,25 @@ static void encode_help_display(void)
     fprintf(stdout, "-cinema4K\n");
     fprintf(stdout, "    Digital Cinema 4K profile compliant codestream.\n");
     fprintf(stdout, "  Frames per second not required. Default value is 24fps.\n");
+    fprintf(stdout, "-IMF <PROFILE>[,mainlevel=X][,sublevel=Y][,framerate=FPS]\n");
+    fprintf(stdout, "    Interoperable Master Format compliant codestream.\n");
+    fprintf(stdout, "    <PROFILE>=2K, 4K, 8K, 2K_R, 4K_R or 8K_R.\n");
+    fprintf(stdout, "    X >= 0 and X <= 11.\n");
+    fprintf(stdout, "    Y >= 0 and Y <= 9.\n");
+    fprintf(stdout,
+            "    framerate > 0 may be specified to enhance checks and set maximum bit rate when Y > 0.\n");
+    fprintf(stdout, "-GuardBits value\n");
+    fprintf(stdout,
+            "    Number of guard bits in [0,7] range. Usually 1 or 2 (default value).\n");
     fprintf(stdout, "-jpip\n");
     fprintf(stdout, "    Write jpip codestream index box in JP2 output file.\n");
     fprintf(stdout, "    Currently supports only RPCL order.\n");
     fprintf(stdout, "-C <comment>\n");
     fprintf(stdout, "    Add <comment> in the comment marker segment.\n");
+    if (opj_has_thread_support()) {
+        fprintf(stdout, "  -threads <num_threads|ALL_CPUS>\n"
+                "    Number of threads to use for encoding or ALL_CPUS for all available cores.\n");
+    }
     /* UniPG>> */
 #ifdef USE_JPWL
     fprintf(stdout, "-W <params>\n");
@@ -313,7 +337,7 @@ static void encode_help_display(void)
             JPWL_MAX_NO_TILESPECS);
     fprintf(stdout,
             "     p selects the packet error protection (EEP/UEP with EPBs)\n");
-    fprintf(stdout, "      to be applied to raw data: 'type' can be\n");
+    fprintf(stdout, "      to be applied to raw or yuv data: 'type' can be\n");
     fprintf(stdout,
             "       [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
     fprintf(stdout,
@@ -465,6 +489,11 @@ static unsigned int get_num_images(char *imgdirpath)
         if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
             continue;
         }
+        if (num_images == UINT_MAX) {
+            fprintf(stderr, "Too many files in folder %s\n", imgdirpath);
+            num_images = 0;
+            break;
+        }
         num_images++;
     }
     closedir(dir);
@@ -503,10 +532,10 @@ static int get_file_format(char *filename)
 {
     unsigned int i;
     static const char *extension[] = {
-        "pgx", "pnm", "pgm", "ppm", "pbm", "pam", "bmp", "tif", "raw", "rawl", "tga", "png", "j2k", "jp2", "j2c", "jpc"
+        "pgx", "pnm", "pgm", "ppm", "pbm", "pam", "bmp", "tif", "tiff", "raw", "yuv", "rawl", "tga", "png", "j2k", "jp2", "j2c", "jpc"
     };
     static const int format[] = {
-        PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT, J2K_CFMT
+        PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, TIF_DFMT, RAW_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT, J2K_CFMT
     };
     char * ext = strrchr(filename, '.');
     if (ext == NULL) {
@@ -527,7 +556,8 @@ static char * get_file_name(char *name)
     return fname;
 }
 
-static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
+static char get_next_file(unsigned int imageno, dircnt_t *dirptr,
+                          img_fol_t *img_fol,
                           opj_cparameters_t *parameters)
 {
     char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
@@ -535,12 +565,18 @@ static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
     char *temp_p, temp1[OPJ_PATH_LEN] = "";
 
     strcpy(image_filename, dirptr->filename[imageno]);
-    fprintf(stderr, "File Number %d \"%s\"\n", imageno, image_filename);
+    fprintf(stderr, "File Number %u \"%s\"\n", imageno, image_filename);
     parameters->decod_format = get_file_format(image_filename);
     if (parameters->decod_format == -1) {
         return 1;
     }
-    sprintf(infilename, "%s/%s", img_fol->imgdirpath, image_filename);
+    if (strlen(img_fol->imgdirpath) + 1 + strlen(image_filename) + 1 > sizeof(
+                infilename)) {
+        return 1;
+    }
+    strcpy(infilename, img_fol->imgdirpath);
+    strcat(infilename, "/");
+    strcat(infilename, image_filename);
     if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile),
                      infilename) != 0) {
         return 1;
@@ -553,8 +589,15 @@ static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
         sprintf(temp1, ".%s", temp_p);
     }
     if (img_fol->set_out_format == 1) {
-        sprintf(outfilename, "%s/%s.%s", img_fol->imgdirpath, temp_ofname,
-                img_fol->out_format);
+        if (strlen(img_fol->imgdirpath) + 1 + strlen(temp_ofname) + 1 + strlen(
+                    img_fol->out_format) + 1 > sizeof(outfilename)) {
+            return 1;
+        }
+        strcpy(outfilename, img_fol->imgdirpath);
+        strcat(outfilename, "/");
+        strcat(outfilename, temp_ofname);
+        strcat(outfilename, ".");
+        strcat(outfilename, img_fol->out_format);
         if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
                          outfilename) != 0) {
             return 1;
@@ -568,7 +611,13 @@ static char get_next_file(int imageno, dircnt_t *dirptr, img_fol_t *img_fol,
 static int parse_cmdline_encoder(int argc, char **argv,
                                  opj_cparameters_t *parameters,
                                  img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename,
-                                 size_t indexfilename_size)
+                                 size_t indexfilename_size,
+                                 int* pOutFramerate,
+                                 OPJ_BOOL* pOutPLT,
+                                 OPJ_BOOL* pOutTLM,
+                                 int* pOutGuardBits,
+                                 int* pOutNumThreads,
+                                 unsigned int* pTarget_bitdepth)
 {
     OPJ_UINT32 i, j;
     int totlen, c;
@@ -583,11 +632,17 @@ static int parse_cmdline_encoder(int argc, char **argv,
         {"POC", REQ_ARG, NULL, 'P'},
         {"ROI", REQ_ARG, NULL, 'R'},
         {"jpip", NO_ARG, NULL, 'J'},
-        {"mct", REQ_ARG, NULL, 'Y'}
+        {"mct", REQ_ARG, NULL, 'Y'},
+        {"IMF", REQ_ARG, NULL, 'Z'},
+        {"PLT", NO_ARG, NULL, 'A'},
+        {"threads",   REQ_ARG, NULL, 'B'},
+        {"TLM", NO_ARG, NULL, 'D'},
+        {"TargetBitDepth", REQ_ARG, NULL, 'X'},
+        {"GuardBits", REQ_ARG, NULL, 'G'}
     };
 
     /* parse the command line */
-    const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:"
+    const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:X:G:"
 #ifdef USE_JPWL
                            "W:"
 #endif /* USE_JPWL */
@@ -619,7 +674,7 @@ static int parse_cmdline_encoder(int argc, char **argv,
             default:
                 fprintf(stderr,
                         "[ERROR] Unknown input file format: %s \n"
-                        "        Known file formats are *.pnm, *.pgm, *.ppm, *.pgx, *png, *.bmp, *.tif, *.raw or *.tga\n",
+                        "        Known file formats are *.pnm, *.pgm, *.ppm, *.pgx, *png, *.bmp, *.tif(f), *.raw, *.yuv or *.tga\n",
                         infile);
                 return 1;
             }
@@ -773,7 +828,7 @@ static int parse_cmdline_encoder(int argc, char **argv,
             }
             free(substr1);
             if (wrong) {
-                fprintf(stderr, "\nError: invalid raw image parameters\n");
+                fprintf(stderr, "\nError: invalid raw or yuv image parameters\n");
                 fprintf(stderr, "Please use the Format option -F:\n");
                 fprintf(stderr,
                         "-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
@@ -781,7 +836,8 @@ static int parse_cmdline_encoder(int argc, char **argv,
                         "If subsampling is omitted, 1x1 is assumed for all components\n");
                 fprintf(stderr,
                         "Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
-                fprintf(stderr, "         for raw 512x512 image with 4:2:0 subsampling\n");
+                fprintf(stderr,
+                        "         for raw or yuv 512x512 size with 4:2:0 subsampling\n");
                 fprintf(stderr, "Aborting.\n");
                 return 1;
             }
@@ -824,7 +880,7 @@ static int parse_cmdline_encoder(int argc, char **argv,
             parameters->tcp_numlayers = (int)numlayers;
             numresolution = (OPJ_UINT32)parameters->numresolution;
             matrix_width = numresolution * 3;
-            parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
+            parameters->cp_matrice = (int *) malloc(sizeof(int) * numlayers * matrix_width);
             if (parameters->cp_matrice == NULL) {
                 return 1;
             }
@@ -871,6 +927,24 @@ static int parse_cmdline_encoder(int argc, char **argv,
         }
         break;
 
+        /* ----------------------------------------------------- */
+        case 'X': {         /* target bitdepth */
+            char *s = opj_optarg;
+            sscanf(s, "%u", pTarget_bitdepth);
+            if (*pTarget_bitdepth == 0) {
+                fprintf(stderr, "Target bitdepth must be at least 1 bit.\n");
+                return 1;
+            }
+        }
+        break;
+
+        /* ----------------------------------------------------- */
+        case 'G': {         /* guard bits */
+            char *s = opj_optarg;
+            sscanf(s, "%d", pOutGuardBits);
+        }
+        break;
+
         /* ----------------------------------------------------- */
 
         case 'n': {         /* resolution */
@@ -908,8 +982,9 @@ static int parse_cmdline_encoder(int argc, char **argv,
         case 'b': {         /* code-block dimension */
             int cblockw_init = 0, cblockh_init = 0;
             sscanf(opj_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) {
+            if (cblockw_init > 1024 || cblockw_init < 4 ||
+                    cblockh_init > 1024 || cblockh_init < 4 ||
+                    cblockw_init * cblockh_init > 4096) {
                 fprintf(stderr,
                         "!! Size of code_block error (option -b) !!\n\nRestriction :\n"
                         "    * width*height<=4096\n    * 4<=width,height<= 1024\n\n");
@@ -937,9 +1012,10 @@ static int parse_cmdline_encoder(int argc, char **argv,
         /* ----------------------------------------------------- */
 
         case 'p': {         /* progression order */
-            char progression[4];
+            char progression[5];
 
             strncpy(progression, opj_optarg, 4);
+            progression[4] = 0;
             parameters->prog_order = give_progression(progression);
             if (parameters->prog_order == -1) {
                 fprintf(stderr, "Unrecognized progression order "
@@ -1128,6 +1204,98 @@ static int parse_cmdline_encoder(int argc, char **argv,
 
         /* ------------------------------------------------------ */
 
+        case 'Z': {         /* IMF profile*/
+            int mainlevel = 0;
+            int sublevel = 0;
+            int profile = 0;
+            int framerate = 0;
+            const char* msg =
+                "Wrong value for -IMF. Should be "
+                "<PROFILE>[,mainlevel=X][,sublevel=Y][,framerate=FPS] where <PROFILE> is one "
+                "of 2K/4K/8K/2K_R/4K_R/8K_R.\n";
+            char* comma;
+
+            comma = strstr(opj_optarg, ",mainlevel=");
+            if (comma && sscanf(comma + 1, "mainlevel=%d", &mainlevel) != 1) {
+                fprintf(stderr, "%s", msg);
+                return 1;
+            }
+
+            comma = strstr(opj_optarg, ",sublevel=");
+            if (comma && sscanf(comma + 1, "sublevel=%d", &sublevel) != 1) {
+                fprintf(stderr, "%s", msg);
+                return 1;
+            }
+
+            comma = strstr(opj_optarg, ",framerate=");
+            if (comma && sscanf(comma + 1, "framerate=%d", &framerate) != 1) {
+                fprintf(stderr, "%s", msg);
+                return 1;
+            }
+
+            comma = strchr(opj_optarg, ',');
+            if (comma != NULL) {
+                *comma = 0;
+            }
+
+            if (strcmp(opj_optarg, "2K") == 0) {
+                profile = OPJ_PROFILE_IMF_2K;
+            } else if (strcmp(opj_optarg, "4K") == 0) {
+                profile = OPJ_PROFILE_IMF_4K;
+            } else if (strcmp(opj_optarg, "8K") == 0) {
+                profile = OPJ_PROFILE_IMF_8K;
+            } else if (strcmp(opj_optarg, "2K_R") == 0) {
+                profile = OPJ_PROFILE_IMF_2K_R;
+            } else if (strcmp(opj_optarg, "4K_R") == 0) {
+                profile = OPJ_PROFILE_IMF_4K_R;
+            } else if (strcmp(opj_optarg, "8K_R") == 0) {
+                profile = OPJ_PROFILE_IMF_8K_R;
+            } else {
+                fprintf(stderr, "%s", msg);
+                return 1;
+            }
+
+            if (!(mainlevel >= 0 && mainlevel <= 15)) {
+                /* Voluntarily rough validation. More fine grained done in library */
+                fprintf(stderr, "Invalid mainlevel value.\n");
+                return 1;
+            }
+            if (!(sublevel >= 0 && sublevel <= 15)) {
+                /* Voluntarily rough validation. More fine grained done in library */
+                fprintf(stderr, "Invalid sublevel value.\n");
+                return 1;
+            }
+            parameters->rsiz = (OPJ_UINT16)(profile | (sublevel << 4) | mainlevel);
+
+            fprintf(stdout, "IMF profile activated\n"
+                    "Other options specified could be overridden\n");
+
+            if (pOutFramerate) {
+                *pOutFramerate = framerate;
+            }
+            if (framerate > 0 && sublevel > 0 && sublevel <= 9) {
+                const int limitMBitsSec[] = {
+                    0,
+                    OPJ_IMF_SUBLEVEL_1_MBITSSEC,
+                    OPJ_IMF_SUBLEVEL_2_MBITSSEC,
+                    OPJ_IMF_SUBLEVEL_3_MBITSSEC,
+                    OPJ_IMF_SUBLEVEL_4_MBITSSEC,
+                    OPJ_IMF_SUBLEVEL_5_MBITSSEC,
+                    OPJ_IMF_SUBLEVEL_6_MBITSSEC,
+                    OPJ_IMF_SUBLEVEL_7_MBITSSEC,
+                    OPJ_IMF_SUBLEVEL_8_MBITSSEC,
+                    OPJ_IMF_SUBLEVEL_9_MBITSSEC
+                };
+                parameters->max_cs_size = limitMBitsSec[sublevel] * (1000 * 1000 / 8) /
+                                          framerate;
+                fprintf(stdout, "Setting max codestream size to %d bytes.\n",
+                        parameters->max_cs_size);
+            }
+        }
+        break;
+
+        /* ------------------------------------------------------ */
+
         case 'Y': {         /* Shall we do an MCT ? 0:no_mct;1:rgb->ycc;2:custom mct (-m option required)*/
             int mct_mode = 0;
             sscanf(opj_optarg, "%d", &mct_mode);
@@ -1568,6 +1736,32 @@ static int parse_cmdline_encoder(int argc, char **argv,
         break;
         /* ------------------------------------------------------ */
 
+        case 'A': {         /* PLT markers */
+            *pOutPLT = OPJ_TRUE;
+        }
+        break;
+
+        /* ----------------------------------------------------- */
+        case 'B': { /* Number of threads */
+            if (strcmp(opj_optarg, "ALL_CPUS") == 0) {
+                *pOutNumThreads = opj_get_num_cpus();
+                if (*pOutNumThreads == 1) {
+                    *pOutNumThreads = 0;
+                }
+            } else {
+                sscanf(opj_optarg, "%d", pOutNumThreads);
+            }
+        }
+        break;
+        /* ------------------------------------------------------ */
+
+        case 'D': {         /* TLM markers */
+            *pOutTLM = OPJ_TRUE;
+        }
+        break;
+
+        /* ------------------------------------------------------ */
+
 
         default:
             fprintf(stderr, "[WARNING] An invalid option has been ignored\n");
@@ -1600,9 +1794,10 @@ static int parse_cmdline_encoder(int argc, char **argv,
         }
     }
 
-    if ((parameters->decod_format == RAW_DFMT && raw_cp->rawWidth == 0)
-            || (parameters->decod_format == RAWL_DFMT && raw_cp->rawWidth == 0)) {
-        fprintf(stderr, "[ERROR] invalid raw image parameters\n");
+    if ((parameters->decod_format == RAW_DFMT ||
+            parameters->decod_format == RAWL_DFMT)
+            && (raw_cp->rawWidth == 0)) {
+        fprintf(stderr, "[ERROR] invalid raw or yuv image parameters\n");
         fprintf(stderr, "Please use the Format option -F:\n");
         fprintf(stderr,
                 "-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
@@ -1738,11 +1933,22 @@ int main(int argc, char **argv)
     img_fol_t img_fol;
     dircnt_t *dirptr = NULL;
 
+    int ret = 0;
+
     OPJ_BOOL bSuccess;
     OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */
     OPJ_UINT32 l_nb_tiles = 4;
+    int framerate = 0;
     OPJ_FLOAT64 t = opj_clock();
 
+    OPJ_BOOL PLT = OPJ_FALSE;
+    OPJ_BOOL TLM = OPJ_FALSE;
+    int num_threads = 0;
+    int guard_bits = -1;
+
+    /** desired bitdepth from input file */
+    unsigned int target_bitdepth = 0;
+
     /* set encoding parameters to default values */
     opj_set_default_encoder_parameters(&parameters);
 
@@ -1762,32 +1968,38 @@ int main(int argc, char **argv)
     parameters.tcp_mct = (char)
                          255; /* This will be set later according to the input image or the provided option */
     if (parse_cmdline_encoder(argc, argv, &parameters, &img_fol, &raw_cp,
-                              indexfilename, sizeof(indexfilename)) == 1) {
-        goto fails;
+                              indexfilename, sizeof(indexfilename), &framerate, &PLT, &TLM,
+                              &guard_bits, &num_threads, &target_bitdepth) == 1) {
+        ret = 1;
+        goto fin;
     }
 
     /* Read directory if necessary */
     if (img_fol.set_imgdir == 1) {
         num_images = get_num_images(img_fol.imgdirpath);
+        if (num_images == 0) {
+            fprintf(stdout, "Folder is empty\n");
+            ret = 0;
+            goto fin;
+        }
         dirptr = (dircnt_t*)malloc(sizeof(dircnt_t));
         if (dirptr) {
-            dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof(
+            dirptr->filename_buf = (char*)calloc(num_images, OPJ_PATH_LEN * sizeof(
                     char)); /* Stores at max 10 image file names*/
-            dirptr->filename = (char**) malloc(num_images * sizeof(char*));
+            dirptr->filename = (char**) calloc(num_images, sizeof(char*));
             if (!dirptr->filename_buf) {
-                return 0;
+                ret = 0;
+                goto fin;
             }
             for (i = 0; i < num_images; i++) {
-                dirptr->filename[i] = dirptr->filename_buf + i * OPJ_PATH_LEN;
+                dirptr->filename[i] = dirptr->filename_buf + (size_t)i * OPJ_PATH_LEN;
             }
         }
         if (load_images(dirptr, img_fol.imgdirpath) == 1) {
-            return 0;
-        }
-        if (num_images == 0) {
-            fprintf(stdout, "Folder is empty\n");
-            return 0;
+            ret = 0;
+            goto fin;
         }
+
     } else {
         num_images = 1;
     }
@@ -1797,7 +2009,7 @@ int main(int argc, char **argv)
         fprintf(stderr, "\n");
 
         if (img_fol.set_imgdir == 1) {
-            if (get_next_file((int)imageno, dirptr, &img_fol, &parameters)) {
+            if (get_next_file(imageno, dirptr, &img_fol, &parameters)) {
                 fprintf(stderr, "skipping file...\n");
                 continue;
             }
@@ -1805,18 +2017,12 @@ int main(int argc, char **argv)
 
         switch (parameters.decod_format) {
         case PGX_DFMT:
-            break;
         case PXM_DFMT:
-            break;
         case BMP_DFMT:
-            break;
         case TIF_DFMT:
-            break;
         case RAW_DFMT:
         case RAWL_DFMT:
-            break;
         case TGA_DFMT:
-            break;
         case PNG_DFMT:
             break;
         default:
@@ -1832,7 +2038,8 @@ int main(int argc, char **argv)
             image = pgxtoimage(parameters.infile, &parameters);
             if (!image) {
                 fprintf(stderr, "Unable to load pgx file\n");
-                return 1;
+                ret = 1;
+                goto fin;
             }
             break;
 
@@ -1840,7 +2047,8 @@ int main(int argc, char **argv)
             image = pnmtoimage(parameters.infile, &parameters);
             if (!image) {
                 fprintf(stderr, "Unable to load pnm file\n");
-                return 1;
+                ret = 1;
+                goto fin;
             }
             break;
 
@@ -1848,16 +2056,18 @@ int main(int argc, char **argv)
             image = bmptoimage(parameters.infile, &parameters);
             if (!image) {
                 fprintf(stderr, "Unable to load bmp file\n");
-                return 1;
+                ret = 1;
+                goto fin;
             }
             break;
 
 #ifdef OPJ_HAVE_LIBTIFF
         case TIF_DFMT:
-            image = tiftoimage(parameters.infile, &parameters);
+            image = tiftoimage(parameters.infile, &parameters, target_bitdepth);
             if (!image) {
-                fprintf(stderr, "Unable to load tiff file\n");
-                return 1;
+                fprintf(stderr, "Unable to load tif(f) file\n");
+                ret = 1;
+                goto fin;
             }
             break;
 #endif /* OPJ_HAVE_LIBTIFF */
@@ -1865,8 +2075,9 @@ int main(int argc, char **argv)
         case RAW_DFMT:
             image = rawtoimage(parameters.infile, &parameters, &raw_cp);
             if (!image) {
-                fprintf(stderr, "Unable to load raw file\n");
-                return 1;
+                fprintf(stderr, "Unable to load raw or yuv file\n");
+                ret = 1;
+                goto fin;
             }
             break;
 
@@ -1874,7 +2085,8 @@ int main(int argc, char **argv)
             image = rawltoimage(parameters.infile, &parameters, &raw_cp);
             if (!image) {
                 fprintf(stderr, "Unable to load raw file\n");
-                return 1;
+                ret = 1;
+                goto fin;
             }
             break;
 
@@ -1882,7 +2094,8 @@ int main(int argc, char **argv)
             image = tgatoimage(parameters.infile, &parameters);
             if (!image) {
                 fprintf(stderr, "Unable to load tga file\n");
-                return 1;
+                ret = 1;
+                goto fin;
             }
             break;
 
@@ -1891,18 +2104,20 @@ int main(int argc, char **argv)
             image = pngtoimage(parameters.infile, &parameters);
             if (!image) {
                 fprintf(stderr, "Unable to load png file\n");
-                return 1;
+                ret = 1;
+                goto fin;
             }
             break;
 #endif /* OPJ_HAVE_LIBPNG */
         }
 
-        /* Can happen if input file is TIFF or PNG
+        /* Can happen if input file is TIF(F) or PNG
         * and OPJ_HAVE_LIBTIF or OPJ_HAVE_LIBPNG is undefined
         */
         if (!image) {
             fprintf(stderr, "Unable to load file: got no image\n");
-            return 1;
+            ret = 1;
+            goto fin;
         }
 
         /* Decide if MCT should be used */
@@ -1913,12 +2128,49 @@ int main(int argc, char **argv)
             if ((parameters.tcp_mct == 1) && (image->numcomps < 3)) {
                 fprintf(stderr, "RGB->YCC conversion cannot be used:\n");
                 fprintf(stderr, "Input image has less than 3 components\n");
-                return 1;
+                ret = 1;
+                goto fin;
             }
             if ((parameters.tcp_mct == 2) && (!parameters.mct_data)) {
                 fprintf(stderr, "Custom MCT has been set but no array-based MCT\n");
                 fprintf(stderr, "has been provided. Aborting.\n");
-                return 1;
+                ret = 1;
+                goto fin;
+            }
+        }
+
+        if (OPJ_IS_IMF(parameters.rsiz) && framerate > 0) {
+            const int mainlevel = OPJ_GET_IMF_MAINLEVEL(parameters.rsiz);
+            if (mainlevel > 0 && mainlevel <= OPJ_IMF_MAINLEVEL_MAX) {
+                const int limitMSamplesSec[] = {
+                    0,
+                    OPJ_IMF_MAINLEVEL_1_MSAMPLESEC,
+                    OPJ_IMF_MAINLEVEL_2_MSAMPLESEC,
+                    OPJ_IMF_MAINLEVEL_3_MSAMPLESEC,
+                    OPJ_IMF_MAINLEVEL_4_MSAMPLESEC,
+                    OPJ_IMF_MAINLEVEL_5_MSAMPLESEC,
+                    OPJ_IMF_MAINLEVEL_6_MSAMPLESEC,
+                    OPJ_IMF_MAINLEVEL_7_MSAMPLESEC,
+                    OPJ_IMF_MAINLEVEL_8_MSAMPLESEC,
+                    OPJ_IMF_MAINLEVEL_9_MSAMPLESEC,
+                    OPJ_IMF_MAINLEVEL_10_MSAMPLESEC,
+                    OPJ_IMF_MAINLEVEL_11_MSAMPLESEC
+                };
+                OPJ_UINT32 avgcomponents = image->numcomps;
+                double msamplespersec;
+                if (image->numcomps == 3 &&
+                        image->comps[1].dx == 2 &&
+                        image->comps[1].dy == 2) {
+                    avgcomponents = 2;
+                }
+                msamplespersec = (double)image->x1 * image->y1 * avgcomponents * framerate /
+                                 1e6;
+                if (msamplespersec > limitMSamplesSec[mainlevel]) {
+                    fprintf(stderr,
+                            "Warning: MSamples/sec is %f, whereas limit is %d.\n",
+                            msamplespersec,
+                            limitMSamplesSec[mainlevel]);
+                }
             }
         }
 
@@ -1958,13 +2210,47 @@ int main(int argc, char **argv)
             fprintf(stderr, "failed to encode image: opj_setup_encoder\n");
             opj_destroy_codec(l_codec);
             opj_image_destroy(image);
-            return 1;
+            ret = 1;
+            goto fin;
+        }
+
+        {
+            const char* options[4] = { NULL, NULL, NULL, NULL };
+            int iOpt = 0;
+            char szGuardBits[32];
+            if (PLT) {
+                options[iOpt++] = "PLT=YES";
+            }
+            if (TLM) {
+                options[iOpt++] = "TLM=YES";
+            }
+            if (guard_bits >= 0) {
+                sprintf(szGuardBits, "GUARD_BITS=%d", guard_bits);
+                options[iOpt++] = szGuardBits;
+            }
+            if (iOpt > 0 && !opj_encoder_set_extra_options(l_codec, options)) {
+                fprintf(stderr, "failed to encode image: opj_encoder_set_extra_options\n");
+                opj_destroy_codec(l_codec);
+                opj_image_destroy(image);
+                ret = 1;
+                goto fin;
+            }
+        }
+
+        if (num_threads >= 1 &&
+                !opj_codec_set_threads(l_codec, num_threads)) {
+            fprintf(stderr, "failed to set number of threads\n");
+            opj_destroy_codec(l_codec);
+            opj_image_destroy(image);
+            ret = 1;
+            goto fin;
         }
 
         /* open a byte stream for writing and allocate memory for all tiles */
         l_stream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_FALSE);
         if (! l_stream) {
-            return 1;
+            ret = 1;
+            goto fin;
         }
 
         /* encode the image */
@@ -1977,16 +2263,18 @@ int main(int argc, char **argv)
             OPJ_UINT32 l_data_size = 512 * 512 * 3;
             l_data = (OPJ_BYTE*) calloc(1, l_data_size);
             if (l_data == NULL) {
-                goto fails;
+                ret = 1;
+                goto fin;
             }
             for (i = 0; i < l_nb_tiles; ++i) {
                 if (! opj_write_tile(l_codec, i, l_data, l_data_size, l_stream)) {
-                    fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",
+                    fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %u!\n",
                             i);
                     opj_stream_destroy(l_stream);
                     opj_destroy_codec(l_codec);
                     opj_image_destroy(image);
-                    return 1;
+                    ret = 1;
+                    goto fin;
                 }
             }
             free(l_data);
@@ -2007,7 +2295,8 @@ int main(int argc, char **argv)
             opj_image_destroy(image);
             fprintf(stderr, "failed to encode image\n");
             remove(parameters.outfile);
-            return 1;
+            ret = 1;
+            goto fin;
         }
 
         num_compressed_files++;
@@ -2023,26 +2312,15 @@ int main(int argc, char **argv)
 
     }
 
-    /* free user parameters structure */
-    if (parameters.cp_comment) {
-        free(parameters.cp_comment);
-    }
-    if (parameters.cp_matrice) {
-        free(parameters.cp_matrice);
-    }
-    if (raw_cp.rawComps) {
-        free(raw_cp.rawComps);
-    }
-
     t = opj_clock() - t;
     if (num_compressed_files) {
         fprintf(stdout, "encode time: %d ms \n",
                 (int)((t * 1000.0) / (OPJ_FLOAT64)num_compressed_files));
     }
 
-    return 0;
+    ret = 0;
 
-fails:
+fin:
     if (parameters.cp_comment) {
         free(parameters.cp_comment);
     }
@@ -2064,5 +2342,5 @@ fails:
         }
         free(dirptr);
     }
-    return 1;
+    return ret;
 }