MJ2 codec restructuration. Created libopenjpeg_097 directory and moved libopenjpeg...
[openjpeg.git] / mj2 / mj2.c
index 074f408ff63def66b6b903e4860efe0e1d01b821..cdafe751f959d8c8ca41f5ca0b7304b9ac659bc7 100644 (file)
--- a/mj2/mj2.c
+++ b/mj2/mj2.c
@@ -91,18 +91,21 @@ void mj2_memory_free(mj2_movie_t * movie)
     tk = &movie->tk[i];
     if (tk->name_size != 0)
       free(tk->name);
-    if (tk->jp2_struct.comps != 0)
-      free(tk->jp2_struct.comps);
-    if (tk->jp2_struct.cl != 0)
-      free(tk->jp2_struct.cl);
+    if (tk->track_type == 0)  {// Video track
+      if (tk->jp2_struct.comps != 0)
+       free(tk->jp2_struct.comps);
+      if (tk->jp2_struct.cl != 0)
+       free(tk->jp2_struct.cl);
+      if (tk->num_jp2x != 0)
+      free(tk->jp2xdata);
+
+    }
     if (tk->num_url != 0)
       free(tk->url);
     if (tk->num_urn != 0)
       free(tk->urn);
     if (tk->num_br != 0)
       free(tk->br);
-    if (tk->num_jp2x != 0)
-      free(tk->jp2xdata);
     if (tk->num_tts != 0)
       free(tk->tts);
     if (tk->num_chunks != 0)
@@ -144,8 +147,19 @@ int mj2_read_boxhdr(mj2_box_t * box)
 
 /*
 * 
-* Initialisation of a Standard Video Track
-* with one sample per chunk
+* Initialisation of a Standard Movie, given a simple movie structure defined by the user 
+* The movie will have one sample per chunk
+* 
+* Arguments: mj2_movie_t * movie
+* Several variables of "movie" must be defined in order to enable a correct execution of 
+* this function:
+*   - The number of tracks of each type (movie->num_vtk, movie->num_stk, movie->num_htk)
+*   - The memory for each must be allocated (movie->tk)
+*   - For each track:
+*        The track type (tk->track_type)
+*        The number of sample (tk->num_samples)
+*        The sample rate (tk->sample_rate)
+*
 */
 
 int mj2_init_stdmovie(mj2_movie_t * movie)
@@ -153,13 +167,12 @@ int mj2_init_stdmovie(mj2_movie_t * movie)
   int i;
   unsigned int j;
   time_t ltime;
+
   movie->brand = MJ2_MJ2;
   movie->minversion = 0;
-  if (!((movie->num_cl<100) && (movie->num_cl>-1))) {
-    movie->num_cl = 2;
-    movie->cl =
-      (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
-  }
+  movie->num_cl = 2;
+  movie->cl =
+    (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
 
   movie->cl[0] = MJ2_MJ2;
   movie->cl[1] = MJ2_MJ2S;
@@ -183,6 +196,9 @@ int mj2_init_stdmovie(mj2_movie_t * movie)
   for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) {
     mj2_tk_t *tk = &movie->tk[i];
     movie->next_tk_id++;
+    tk->jp2_struct.comps = NULL;
+    tk->jp2_struct.cl = NULL;
+    
     if (tk->track_type == 0) {
       if (tk->num_samples == 0)
        return 1;
@@ -204,9 +220,15 @@ int mj2_init_stdmovie(mj2_movie_t * movie)
       tk->sampletochunk[0].first_chunk = 1;
       tk->sampletochunk[0].samples_per_chunk = 1;
       tk->sampletochunk[0].sample_descr_idx = 1;
+      
+      if (tk->sample_rate == 0) {
+       fprintf(stderr,"Error while initializing MJ2 movie: Sample rate of track %d must be different from zero\n",tk->track_ID);
+       return 1;
+      }
 
-      for (j = 0; j < tk->num_samples; j++)
+      for (j = 0; j < tk->num_samples; j++) {
        tk->sample[j].sample_delta = tk->timescale / tk->sample_rate;
+      }
 
       tk->num_tts = 1;
       tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
@@ -258,6 +280,10 @@ int mj2_init_stdmovie(mj2_movie_t * movie)
       tk->visual_w = tk->w << 16;
       tk->visual_h = tk->h << 16;
     }
+    else {
+      tk->num_br = 0;
+      tk->jp2xdata = NULL;
+    }
   }
   return 0;
 }
@@ -1820,6 +1846,16 @@ int mj2_read_smhd(mj2_tk_t * tk)
   tk->track_type = 1;
   tk->balance = cio_read(2);
 
+  /* Init variables to zero to avoid problems when freeeing memory
+  The values will possibly be overidded when decoding the track structure */
+  tk->num_br = 0;
+  tk->num_url = 0;
+  tk->num_urn = 0;
+  tk->num_chunks = 0;
+  tk->num_tts = 0;
+  tk->num_samplestochunk = 0;
+  tk->num_samples = 0;
+
   cio_skip(2);                 /* Reserved */
 
   if (cio_tell() - box.init_pos != box.length) {
@@ -1890,6 +1926,17 @@ int mj2_read_hmhd(mj2_tk_t * tk)
   tk->avgbitrate = cio_read(4);
   tk->slidingavgbitrate = cio_read(4);
 
+  /* Init variables to zero to avoid problems when freeeing memory
+  The values will possibly be overidded when decoding the track structure */
+  tk->num_br = 0;
+  tk->num_url = 0;
+  tk->num_urn = 0;
+  tk->num_chunks = 0;
+  tk->num_tts = 0;
+  tk->num_samplestochunk = 0;
+  tk->num_samples = 0;
+
+
   if (cio_tell() - box.init_pos != box.length) {
     fprintf(stderr, "Error with HMHD Box size\n");
     return 1;
@@ -2709,7 +2756,7 @@ int mj2_read_struct(FILE *file, mj2_movie_t * movie) {
     case MJ2_SKIP:
       fsresult = fseek(file,foffset+box.length,SEEK_SET);
       if( fsresult ) {
-       fprintf(stderr, "End of file reached while trying to read SKIP bo\nx" );
+       fprintf(stderr, "End of file reached while trying to read SKIP box\n" );
        return 1;
       }
       foffset += box.length;
@@ -2718,7 +2765,7 @@ int mj2_read_struct(FILE *file, mj2_movie_t * movie) {
       fprintf(stderr, "Unknown box in MJ2 stream\n");
       fsresult = fseek(file,foffset+box.length,SEEK_SET);
       if( fsresult ) {
-       fprintf(stderr, "End of file reached while trying to read %s box\n", box.type ); 
+       fprintf(stderr, "End of file reached while trying to read end of unknown box\n"); 
        return 1;
       }      
       foffset += box.length;