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