minor changes in header inclusions
[openjpeg.git] / mj2 / frames_to_mj2.c
index d95fbeb7b20289ad466d87679630d7bd291cf92b..6b1fd536a4589da120169f9f49c79424d1b5a12a 100644 (file)
 * POSSIBILITY OF SUCH DAMAGE.
 */
 
-#include "opj_includes.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "openjpeg.h"
+#include "../libopenjpeg/j2k_lib.h"
+#include "../libopenjpeg/j2k.h"
+#include "../libopenjpeg/jp2.h"
+#include "../libopenjpeg/cio.h"
 #include "mj2.h"
 #include "mj2_convert.h"
-#include "compat/getopt.h"
+#include "getopt.h"
 
 /**
 Size of memory first allocated for MOOV box
@@ -64,7 +72,7 @@ void info_callback(const char *msg, void *client_data) {
 
 void help_display()
 {
-  fprintf(stdout,"HELP\n----\n\n");
+  fprintf(stdout,"HELP for frames_to_mj2\n----\n\n");
   fprintf(stdout,"- the -h option displays this help information on screen\n\n");
   
   
@@ -141,8 +149,6 @@ void help_display()
     (stdout,"                 Indicate multiple modes by adding their values. \n");
   fprintf
     (stdout,"                 ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
-  fprintf
-    (stdout,"-x           : create an index file *.Idx (-x index_name.Idx) \n");
   fprintf
     (stdout,"-ROI         : c=%%d,U=%%d : quantization indices upshifted \n");
   fprintf
@@ -228,12 +234,12 @@ int main(int argc, char **argv)
        opj_cparameters_t *j2k_parameters;      /* J2K compression parameters */
        opj_event_mgr_t event_mgr;              /* event manager */
        opj_cio_t *cio;
-       char value;
+       int value;
   opj_mj2_t *movie;
        opj_image_t *img;
   int i, j;
   char *s, S1, S2, S3;
-  char *buf;
+  unsigned char *buf;
   int x1, y1,  len;
   long mdat_initpos, offset;
   FILE *mj2file;
@@ -241,7 +247,7 @@ int main(int argc, char **argv)
        opj_cinfo_t* cinfo;
   bool bSuccess;
        int numframes;
-       double total_time = 0;
+       double total_time = 0;  
 
   /* default value */
   /* ------------- */
@@ -251,8 +257,7 @@ int main(int argc, char **argv)
   mj2_parameters.h = 288;                      // CIF default value
   mj2_parameters.CbCr_subsampling_dx = 2;      // CIF default value
   mj2_parameters.CbCr_subsampling_dy = 2;      // CIF default value
-  mj2_parameters.frame_rate = 25;
-  
+  mj2_parameters.frame_rate = 25;        
        /*
        configure the event callbacks (not required)
        setting of each callback is optionnal
@@ -275,10 +280,12 @@ int main(int argc, char **argv)
                sprintf(j2k_parameters->cp_comment,"%s%s", comment, version);
        }
 
+       mj2_parameters.decod_format = 0;
+       mj2_parameters.cod_format = 0;
 
   while (1) {
     int c = getopt(argc, argv,
-      "i:o:r:q:f:t:n:c:b:x:p:s:d:h P:S:E:M:R:T:C:I:W:F:");
+      "i:o:r:q:f:t:n:c:b:p:s:d:P:S:E:M:R:T:C:I:W:F:h");
     if (c == -1)
       break;
     switch (c) {
@@ -461,14 +468,6 @@ int main(int argc, char **argv)
                        }
                        break;
       /* ----------------------------------------------------- */
-    case 'x':                  /* creation of index file */
-      {
-                               char *index = optarg;
-                               strncpy(j2k_parameters->index, index, sizeof(j2k_parameters->index)-1);
-                               j2k_parameters->index_on = 1;
-                       }
-                       break;
-      /* ----------------------------------------------------- */
     case 'p':                  /* progression order */
                        {
                                char progression[4];
@@ -520,7 +519,7 @@ int main(int argc, char **argv)
                                while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%4s", &POC[numpocs].tile,
                                        &POC[numpocs].resno0, &POC[numpocs].compno0,
                                        &POC[numpocs].layno1, &POC[numpocs].resno1,
-                                       &POC[numpocs].compno1, &POC[numpocs].progorder) == 7) {
+                                       &POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
                                        POC[numpocs].prg1 = give_progression(POC[numpocs].progorder);
                                        numpocs++;
                                        while (*s && *s != '/') {
@@ -610,9 +609,9 @@ int main(int argc, char **argv)
     
   /* Error messages */
   /* -------------- */
-  if (!mj2_parameters.infile || !mj2_parameters.outfile) {
+       if (!mj2_parameters.cod_format || !mj2_parameters.decod_format) {
     fprintf(stderr,
-      "Correct usage: mj2_encoder -i yuv-file -o mj2-file (+ options)\n");
+      "Usage: %s -i yuv-file -o mj2-file (+ options)\n",argv[0]);
     return 1;
   }
   
@@ -672,7 +671,7 @@ int main(int argc, char **argv)
     
        /* get a MJ2 decompressor handle */
        cinfo = mj2_create_compress();
-       movie = cinfo->mj2_handle;
+       movie = (opj_mj2_t*)cinfo->mj2_handle;
        
        /* catch events using our callbacks and give a local context */
        opj_set_event_mgr((opj_common_ptr)cinfo, &event_mgr, stderr);
@@ -681,8 +680,8 @@ int main(int argc, char **argv)
        mj2_setup_encoder(movie, &mj2_parameters);   
   
   movie->tk[0].num_samples = yuv_num_frames(&movie->tk[0],mj2_parameters.infile); 
-  if (!movie->tk[0].num_samples) {
-    fprintf(stderr,"Unable to count the number of frames in YUV input file\n");
+  if (movie->tk[0].num_samples == -1) {
+               return 1;
   }
   
   // One sample per chunk
@@ -695,7 +694,7 @@ int main(int argc, char **argv)
   };    
   
   // Writing JP, FTYP and MDAT boxes 
-  buf = (char*) malloc (300 * sizeof(char)); // Assuming that the JP and FTYP
+  buf = (unsigned char*) malloc (300 * sizeof(unsigned char)); // Assuming that the JP and FTYP
   // boxes won't be longer than 300 bytes
        cio = opj_cio_open((opj_common_ptr)movie->cinfo, buf, 300);
   mj2_write_jp(cio);
@@ -705,8 +704,9 @@ int main(int argc, char **argv)
   cio_write(cio, MJ2_MDAT, 4); 
   fwrite(buf,cio_tell(cio),1,mj2file);
   offset = cio_tell(cio);
-  opj_free(buf);
-  
+  opj_cio_close(cio);
+  free(buf);
+
   for (i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++) {
     if (movie->tk[i].track_type != 0) {
       fprintf(stderr, "Unable to write sound or hint tracks\n");
@@ -722,12 +722,12 @@ int main(int argc, char **argv)
                        
                        img = mj2_image_create(tk, j2k_parameters);          
                        buflen = 2 * (tk->w * tk->h * 8);
-                       buf = (char *) malloc(buflen*sizeof(char));     
+                       buf = (unsigned char *) malloc(buflen*sizeof(unsigned char));   
 
       for (sampleno = 0; sampleno < numframes; sampleno++) {           
                                double init_time = opj_clock();
                                double elapsed_time;
-                               if (!yuvtoimage(tk, img, sampleno, j2k_parameters, mj2_parameters.infile)) {
+                               if (yuvtoimage(tk, img, sampleno, j2k_parameters, mj2_parameters.infile)) {
                                        fprintf(stderr, "Error with frame number %d in YUV file\n", sampleno);
                                        return 1;
                                }
@@ -741,7 +741,7 @@ int main(int argc, char **argv)
                                cio_write(cio, JP2_JP2C, 4);    // JP2C
 
                                /* encode the image */
-                               bSuccess = opj_encode(cinfo, cio, img, j2k_parameters->index);
+                               bSuccess = opj_encode(cinfo, cio, img, NULL);
                                if (!bSuccess) {
                                        opj_cio_close(cio);
                                        fprintf(stderr, "failed to encode image\n");
@@ -751,6 +751,7 @@ int main(int argc, char **argv)
                                len = cio_tell(cio) - 8;
                                cio_seek(cio, 0);
                                cio_write(cio, len+8,4);
+                               opj_cio_close(cio);
                                tk->sample[sampleno].sample_size = len+8;                               
                                tk->sample[sampleno].offset = offset;
                                tk->chunk[sampleno].offset = offset;    // There is one sample per chunk 
@@ -762,7 +763,7 @@ int main(int argc, char **argv)
 
       }
                        /* free buffer data */
-                       opj_free(buf);
+                       free(buf);
                        /* free image data */
                        opj_image_destroy(img);
     }
@@ -770,20 +771,21 @@ int main(int argc, char **argv)
   
   fseek(mj2file, mdat_initpos, SEEK_SET);
        
-  buf = (char*) malloc(4*sizeof(char));
+  buf = (unsigned char*) malloc(4*sizeof(unsigned char));
 
        // Init a cio to write box length variable in a little endian way 
        cio = opj_cio_open(NULL, buf, 4);
   cio_write(cio, offset - mdat_initpos, 4);
   fwrite(buf, 4, 1, mj2file);
   fseek(mj2file,0,SEEK_END);
-  opj_free(buf);
-  
+  free(buf);
+
   // Writing MOOV box 
-       buf = (char*) malloc ((TEMP_BUF+numframes*20) * sizeof(char));
+       buf = (unsigned char*) malloc ((TEMP_BUF+numframes*20) * sizeof(unsigned char));
        cio = opj_cio_open(movie->cinfo, buf, (TEMP_BUF+numframes*20));
        mj2_write_moov(movie, cio);
   fwrite(buf,cio_tell(cio),1,mj2file);
+  free(buf);
 
        fprintf(stdout,"Total encoding time: %.2f s for %d frames (%.1f fps)\n", total_time, numframes, (float)numframes/total_time);