opj_compress: improve help message regarding new IMF switch
[openjpeg.git] / src / bin / jp2 / opj_compress.c
index 3131a433429faa246346ece7c5545a19e476d20a..cd3953e99d63df69ca0e1e50444c99483629fd57 100644 (file)
@@ -287,6 +287,13 @@ 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, "-jpip\n");
     fprintf(stdout, "    Write jpip codestream index box in JP2 output file.\n");
     fprintf(stdout, "    Currently supports only RPCL order.\n");
@@ -568,7 +575,8 @@ 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_UINT32 i, j;
     int totlen, c;
@@ -583,7 +591,8 @@ 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'}
     };
 
     /* parse the command line */
@@ -824,7 +833,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;
             }
@@ -1129,6 +1138,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);
@@ -1739,9 +1840,12 @@ 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();
 
     /* set encoding parameters to default values */
@@ -1763,8 +1867,9 @@ 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) == 1) {
+        ret = 1;
+        goto fin;
     }
 
     /* Read directory if necessary */
@@ -1776,18 +1881,21 @@ int main(int argc, char **argv)
                     char)); /* Stores at max 10 image file names*/
             dirptr->filename = (char**) malloc(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;
             }
         }
         if (load_images(dirptr, img_fol.imgdirpath) == 1) {
-            return 0;
+            ret = 0;
+            goto fin;
         }
         if (num_images == 0) {
             fprintf(stdout, "Folder is empty\n");
-            return 0;
+            ret = 0;
+            goto fin;
         }
     } else {
         num_images = 1;
@@ -1833,7 +1941,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;
 
@@ -1841,7 +1950,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;
 
@@ -1849,7 +1959,8 @@ 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;
 
@@ -1858,7 +1969,8 @@ int main(int argc, char **argv)
             image = tiftoimage(parameters.infile, &parameters);
             if (!image) {
                 fprintf(stderr, "Unable to load tiff file\n");
-                return 1;
+                ret = 1;
+                goto fin;
             }
             break;
 #endif /* OPJ_HAVE_LIBTIFF */
@@ -1867,7 +1979,8 @@ int main(int argc, char **argv)
             image = rawtoimage(parameters.infile, &parameters, &raw_cp);
             if (!image) {
                 fprintf(stderr, "Unable to load raw file\n");
-                return 1;
+                ret = 1;
+                goto fin;
             }
             break;
 
@@ -1875,7 +1988,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;
 
@@ -1883,7 +1997,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;
 
@@ -1892,7 +2007,8 @@ 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 */
@@ -1903,7 +2019,8 @@ int main(int argc, char **argv)
         */
         if (!image) {
             fprintf(stderr, "Unable to load file: got no image\n");
-            return 1;
+            ret = 1;
+            goto fin;
         }
 
         /* Decide if MCT should be used */
@@ -1914,12 +2031,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]);
+                }
             }
         }
 
@@ -1959,13 +2113,15 @@ 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;
         }
 
         /* 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 */
@@ -1978,7 +2134,8 @@ 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)) {
@@ -1987,7 +2144,8 @@ int main(int argc, char **argv)
                     opj_stream_destroy(l_stream);
                     opj_destroy_codec(l_codec);
                     opj_image_destroy(image);
-                    return 1;
+                    ret = 1;
+                    goto fin;
                 }
             }
             free(l_data);
@@ -2008,7 +2166,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++;
@@ -2024,26 +2183,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);
     }
@@ -2065,5 +2213,5 @@ fails:
         }
         free(dirptr);
     }
-    return 1;
+    return ret;
 }