Code improvements using 'g++-4.4.0' to trace the code (v1.4). Thanks to Winfried...
[openjpeg.git] / mj2 / mj2_to_frames.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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "openjpeg.h"
33 #include "j2k_lib.h"
34 #include "j2k.h"
35 #include "jp2.h"
36 #include "mj2.h"
37 #include "mj2_convert.h"
38
39 /* -------------------------------------------------------------------------- */
40
41 /**
42 sample error callback expecting a FILE* client object
43 */
44 void error_callback(const char *msg, void *client_data) {
45         FILE *stream = (FILE*)client_data;
46         fprintf(stream, "[ERROR] %s", msg);
47 }
48 /**
49 sample warning callback expecting a FILE* client object
50 */
51 void warning_callback(const char *msg, void *client_data) {
52         FILE *stream = (FILE*)client_data;
53         fprintf(stream, "[WARNING] %s", msg);
54 }
55 /**
56 sample debug callback expecting a FILE* client object
57 */
58 void info_callback(const char *msg, void *client_data) {
59         FILE *stream = (FILE*)client_data;
60         fprintf(stream, "[INFO] %s", msg);
61 }
62
63 /* -------------------------------------------------------------------------- */
64
65
66 int main(int argc, char *argv[]) {
67         mj2_dparameters_t mj2_parameters;                       /* decompression parameters */
68         opj_dinfo_t* dinfo; 
69         opj_event_mgr_t event_mgr;              /* event manager */     
70         opj_cio_t *cio = NULL;
71   unsigned int tnum, snum;
72   opj_mj2_t *movie;
73   mj2_tk_t *track;
74   mj2_sample_t *sample;
75   unsigned char* frame_codestream;
76   FILE *file, *outfile;
77   char outfilename[50];
78   opj_image_t *img = NULL;
79         unsigned int max_codstrm_size = 0;
80         double total_time = 0;
81         unsigned int numframes = 0;
82                         
83   if (argc != 3) {
84     printf("Bad syntax: Usage: mj2_to_frames inputfile.mj2 outputfile.yuv\n"); 
85     printf("Example: MJ2_decoder foreman.mj2 foreman.yuv\n");
86     return 1;
87   }
88   
89   file = fopen(argv[1], "rb");
90   
91   if (!file) {
92     fprintf(stderr, "failed to open %s for reading\n", argv[1]);
93     return 1;
94   }
95         
96   // Checking output file
97   outfile = fopen(argv[2], "w");
98   if (!file) {
99     fprintf(stderr, "failed to open %s for writing\n", argv[2]);
100     return 1;
101   }
102   fclose(outfile);
103         
104         /*
105         configure the event callbacks (not required)
106         setting of each callback is optionnal
107         */
108         memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
109         event_mgr.error_handler = error_callback;
110         event_mgr.warning_handler = warning_callback;
111         event_mgr.info_handler = NULL;
112         
113         /* get a MJ2 decompressor handle */
114         dinfo = mj2_create_decompress();
115         movie = (opj_mj2_t*)dinfo->mj2_handle;
116         
117         /* catch events using our callbacks and give a local context */
118         opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);           
119
120         /* set J2K decoding parameters to default values */
121         opj_set_default_decoder_parameters(&mj2_parameters.j2k_parameters);
122         
123         /* setup the decoder decoding parameters using user parameters */
124         mj2_setup_decoder((opj_mj2_t*)dinfo->mj2_handle, &mj2_parameters);
125                         
126   if (mj2_read_struct(file, movie)) // Creating the movie structure
127     return 1;   
128         
129   // Decode first video track 
130         for (tnum=0; tnum < (unsigned int)(movie->num_htk + movie->num_stk + movie->num_vtk); tnum++) {
131                 if (movie->tk[tnum].track_type == 0) 
132                         break;
133         }
134         
135         if (movie->tk[tnum].track_type != 0) {
136                 printf("Error. Movie does not contain any video track\n");
137                 return 1;
138         }
139         
140   track = &movie->tk[tnum];
141         
142   // Output info on first video tracl
143   fprintf(stdout,"The first video track contains %d frames.\nWidth: %d, Height: %d \n\n",
144     track->num_samples, track->w, track->h);
145         
146         max_codstrm_size = track->sample[0].sample_size-8;
147         frame_codestream = (unsigned char*) malloc(max_codstrm_size * sizeof(unsigned char)); 
148
149         numframes = track->num_samples;
150         
151   for (snum=0; snum < numframes; snum++)
152   {
153                 double init_time = opj_clock();
154                 double elapsed_time;
155
156     sample = &track->sample[snum];
157                 if (sample->sample_size-8 > max_codstrm_size) {
158                         max_codstrm_size =  sample->sample_size-8;
159                         if ((frame_codestream = (unsigned char*)
160                                 realloc(frame_codestream, max_codstrm_size)) == NULL) {
161                                 printf("Error reallocation memory\n");
162                                 return 1;
163                         };              
164                 }
165     fseek(file,sample->offset+8,SEEK_SET);
166     fread(frame_codestream, sample->sample_size-8, 1, file);  // Assuming that jp and ftyp markers size do
167                 
168                 /* open a byte stream */
169                 cio = opj_cio_open((opj_common_ptr)dinfo, frame_codestream, sample->sample_size-8);
170                 
171                 img = opj_decode(dinfo, cio); // Decode J2K to image
172                                 
173     if (((img->numcomps == 3) && (img->comps[0].dx == img->comps[1].dx / 2) 
174       && (img->comps[0].dx == img->comps[2].dx / 2 ) && (img->comps[0].dx == 1)) 
175       || (img->numcomps == 1)) {
176       
177       if (!imagetoyuv(img, argv[2]))    // Convert image to YUV
178                                 return 1;
179     }
180     else if ((img->numcomps == 3) && 
181       (img->comps[0].dx == 1) && (img->comps[1].dx == 1)&&
182       (img->comps[2].dx == 1))// If YUV 4:4:4 input --> to bmp
183     {
184       fprintf(stdout,"The frames will be output in a bmp format (output_1.bmp, ...)\n");
185       sprintf(outfilename,"output_%d.bmp",snum);
186       if (imagetobmp(img, outfilename)) // Convert image to YUV
187                                 return 1;
188       
189     }
190     else {
191       fprintf(stdout,"Image component dimensions are unknown. Unable to output image\n");
192       fprintf(stdout,"The frames will be output in a j2k file (output_1.j2k, ...)\n");
193                         
194       sprintf(outfilename,"output_%d.j2k",snum);
195       outfile = fopen(outfilename, "wb");
196       if (!outfile) {
197                                 fprintf(stderr, "failed to open %s for writing\n",outfilename);
198                                 return 1;
199       }
200       fwrite(frame_codestream,sample->sample_size-8,1,outfile);
201       fclose(outfile);
202     }
203                 /* close the byte stream */
204                 opj_cio_close(cio);     
205                 /* free image data structure */
206                 opj_image_destroy(img);
207                 elapsed_time = opj_clock()-init_time;
208                 fprintf(stderr, "Frame number %d/%d decoded in %.2f mseconds\n", snum + 1, numframes, elapsed_time*1000);
209                 total_time += elapsed_time;
210
211   }
212         
213         free(frame_codestream); 
214   fclose(file); 
215
216         /* free remaining structures */
217         if(dinfo) {
218                 mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);
219         }
220         free(dinfo);
221         
222         fprintf(stdout, "%d frame(s) correctly decompressed\n", snum);
223         fprintf(stdout,"Total decoding time: %.2f seconds (%.1f fps)\n", total_time, (float)numframes/total_time);
224                 
225   return 0;
226 }