2007-05-10 16:21:09 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2003-2004, Fran<EFBFBD>ois-Olivier Devaux
|
|
|
|
|
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2007-10-18 14:26:11 +02:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2010-12-08 12:06:41 +01:00
|
|
|
|
#include "opj_config.h"
|
2007-10-18 14:26:11 +02:00
|
|
|
|
#include "openjpeg.h"
|
2011-04-14 00:38:50 +02:00
|
|
|
|
#include "j2k_lib.h"
|
|
|
|
|
#include "j2k.h"
|
|
|
|
|
#include "jp2.h"
|
2007-05-10 16:21:09 +02:00
|
|
|
|
#include "mj2.h"
|
|
|
|
|
#include "mj2_convert.h"
|
|
|
|
|
|
2010-12-08 12:06:41 +01:00
|
|
|
|
#ifdef HAVE_LIBLCMS2
|
|
|
|
|
#include <lcms2.h>
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_LIBLCMS1
|
|
|
|
|
#include <lcms.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include "color.h"
|
2007-05-10 16:21:09 +02:00
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
sample error callback expecting a FILE* client object
|
|
|
|
|
*/
|
|
|
|
|
void error_callback(const char *msg, void *client_data) {
|
|
|
|
|
FILE *stream = (FILE*)client_data;
|
|
|
|
|
fprintf(stream, "[ERROR] %s", msg);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
sample warning callback expecting a FILE* client object
|
|
|
|
|
*/
|
|
|
|
|
void warning_callback(const char *msg, void *client_data) {
|
|
|
|
|
FILE *stream = (FILE*)client_data;
|
|
|
|
|
fprintf(stream, "[WARNING] %s", msg);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
sample debug callback expecting a FILE* client object
|
|
|
|
|
*/
|
|
|
|
|
void info_callback(const char *msg, void *client_data) {
|
|
|
|
|
FILE *stream = (FILE*)client_data;
|
|
|
|
|
fprintf(stream, "[INFO] %s", msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
2012-04-23 15:23:38 +02:00
|
|
|
|
#define JP2_RFC3745_MAGIC "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a"
|
2007-05-10 16:21:09 +02:00
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
mj2_dparameters_t mj2_parameters; /* decompression parameters */
|
|
|
|
|
opj_dinfo_t* dinfo;
|
|
|
|
|
opj_event_mgr_t event_mgr; /* event manager */
|
|
|
|
|
opj_cio_t *cio = NULL;
|
2012-04-23 15:23:38 +02:00
|
|
|
|
unsigned int tnum, snum, failed;
|
2007-05-10 16:21:09 +02:00
|
|
|
|
opj_mj2_t *movie;
|
|
|
|
|
mj2_tk_t *track;
|
|
|
|
|
mj2_sample_t *sample;
|
|
|
|
|
unsigned char* frame_codestream;
|
2012-04-23 15:23:38 +02:00
|
|
|
|
FILE *infile, *outfile;
|
2007-05-10 16:21:09 +02:00
|
|
|
|
opj_image_t *img = NULL;
|
|
|
|
|
unsigned int max_codstrm_size = 0;
|
|
|
|
|
double total_time = 0;
|
|
|
|
|
unsigned int numframes = 0;
|
|
|
|
|
|
|
|
|
|
if (argc != 3) {
|
2012-04-23 15:23:38 +02:00
|
|
|
|
printf("\nUsage: %s inputfile.mj2 outputfile.yuv\n\n",argv[0]);
|
2007-05-10 16:21:09 +02:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-23 15:23:38 +02:00
|
|
|
|
infile = fopen(argv[1], "rb");
|
2007-05-10 16:21:09 +02:00
|
|
|
|
|
2012-04-23 15:23:38 +02:00
|
|
|
|
if (!infile) {
|
2007-05-10 16:21:09 +02:00
|
|
|
|
fprintf(stderr, "failed to open %s for reading\n", argv[1]);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2012-04-23 15:23:38 +02:00
|
|
|
|
{
|
|
|
|
|
unsigned char buf[28];
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
memset(buf, 0, 28);
|
|
|
|
|
n = fread(buf, 1, 24, infile);
|
|
|
|
|
|
|
|
|
|
if(memcmp(buf, JP2_RFC3745_MAGIC, 12) == 0
|
|
|
|
|
&& memcmp(buf+20, "\x6d\x6a\x70\x32", 4) == 0)
|
|
|
|
|
{
|
|
|
|
|
rewind(infile);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fclose(infile);
|
|
|
|
|
fprintf(stderr,"%s:%d: %s\n\tThis file is not an MJ2 file."
|
|
|
|
|
"Quitting\n",__FILE__,__LINE__,argv[0]);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Checking output file: */
|
2007-05-10 16:21:09 +02:00
|
|
|
|
outfile = fopen(argv[2], "w");
|
2012-04-23 15:23:38 +02:00
|
|
|
|
|
|
|
|
|
if (!outfile) {
|
2007-05-10 16:21:09 +02:00
|
|
|
|
fprintf(stderr, "failed to open %s for writing\n", argv[2]);
|
2012-04-23 15:23:38 +02:00
|
|
|
|
fclose(infile);
|
|
|
|
|
return 1;
|
2007-05-10 16:21:09 +02:00
|
|
|
|
}
|
2012-04-23 15:23:38 +02:00
|
|
|
|
fclose(outfile); remove(argv[2]);
|
|
|
|
|
|
|
|
|
|
frame_codestream = NULL;
|
|
|
|
|
failed = 1;
|
2007-05-10 16:21:09 +02:00
|
|
|
|
/*
|
|
|
|
|
configure the event callbacks (not required)
|
|
|
|
|
setting of each callback is optionnal
|
|
|
|
|
*/
|
|
|
|
|
memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
|
|
|
|
|
event_mgr.error_handler = error_callback;
|
|
|
|
|
event_mgr.warning_handler = warning_callback;
|
|
|
|
|
event_mgr.info_handler = NULL;
|
|
|
|
|
|
|
|
|
|
/* get a MJ2 decompressor handle */
|
|
|
|
|
dinfo = mj2_create_decompress();
|
2012-04-23 15:23:38 +02:00
|
|
|
|
if(dinfo == NULL) goto fin;
|
|
|
|
|
|
2010-03-24 12:20:45 +01:00
|
|
|
|
movie = (opj_mj2_t*)dinfo->mj2_handle;
|
2007-05-10 16:21:09 +02:00
|
|
|
|
|
|
|
|
|
/* catch events using our callbacks and give a local context */
|
|
|
|
|
opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
|
|
|
|
|
|
2011-01-23 19:33:06 +01:00
|
|
|
|
memset(&mj2_parameters, 0, sizeof(mj2_dparameters_t));
|
2007-05-10 16:21:09 +02:00
|
|
|
|
/* set J2K decoding parameters to default values */
|
|
|
|
|
opj_set_default_decoder_parameters(&mj2_parameters.j2k_parameters);
|
|
|
|
|
|
|
|
|
|
/* setup the decoder decoding parameters using user parameters */
|
2011-01-23 19:33:06 +01:00
|
|
|
|
mj2_setup_decoder(movie, &mj2_parameters);
|
2012-04-23 15:23:38 +02:00
|
|
|
|
|
|
|
|
|
/* Create the movie structure: */
|
|
|
|
|
if (mj2_read_struct(infile, movie))
|
|
|
|
|
goto fin;
|
2007-05-10 16:21:09 +02:00
|
|
|
|
|
2012-04-23 15:23:38 +02:00
|
|
|
|
/* Decode first video track */
|
2007-05-10 16:21:09 +02:00
|
|
|
|
for (tnum=0; tnum < (unsigned int)(movie->num_htk + movie->num_stk + movie->num_vtk); tnum++) {
|
|
|
|
|
if (movie->tk[tnum].track_type == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (movie->tk[tnum].track_type != 0) {
|
|
|
|
|
printf("Error. Movie does not contain any video track\n");
|
2012-04-23 15:23:38 +02:00
|
|
|
|
goto fin;
|
2007-05-10 16:21:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
track = &movie->tk[tnum];
|
2012-04-23 15:23:38 +02:00
|
|
|
|
|
|
|
|
|
if(track->jp2_struct.enumcs != ENUMCS_SYCC)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr,"%s:%d: %s\n"
|
|
|
|
|
"\tERROR: this MJ2 file does not contain YUV frames.\n"
|
|
|
|
|
"\tPlease try extract_j2k_from_mj2 for this file.\n",
|
|
|
|
|
__FILE__,__LINE__,argv[0]);
|
|
|
|
|
goto fin;
|
|
|
|
|
}
|
|
|
|
|
/* Output info on first video track: */
|
2007-05-10 16:21:09 +02:00
|
|
|
|
fprintf(stdout,"The first video track contains %d frames.\nWidth: %d, Height: %d \n\n",
|
|
|
|
|
track->num_samples, track->w, track->h);
|
|
|
|
|
|
|
|
|
|
max_codstrm_size = track->sample[0].sample_size-8;
|
2012-04-23 15:23:38 +02:00
|
|
|
|
frame_codestream = (unsigned char*)
|
|
|
|
|
malloc(max_codstrm_size * sizeof(unsigned char));
|
|
|
|
|
if(frame_codestream == NULL) goto fin;
|
2007-05-10 16:21:09 +02:00
|
|
|
|
|
|
|
|
|
numframes = track->num_samples;
|
|
|
|
|
|
|
|
|
|
for (snum=0; snum < numframes; snum++)
|
|
|
|
|
{
|
2012-04-23 15:23:38 +02:00
|
|
|
|
double init_time = opj_clock();
|
|
|
|
|
double elapsed_time;
|
2007-08-21 12:18:40 +02:00
|
|
|
|
|
2007-05-10 16:21:09 +02:00
|
|
|
|
sample = &track->sample[snum];
|
|
|
|
|
if (sample->sample_size-8 > max_codstrm_size) {
|
|
|
|
|
max_codstrm_size = sample->sample_size-8;
|
2010-03-24 12:20:45 +01:00
|
|
|
|
if ((frame_codestream = (unsigned char*)
|
|
|
|
|
realloc(frame_codestream, max_codstrm_size)) == NULL) {
|
2007-05-10 16:21:09 +02:00
|
|
|
|
printf("Error reallocation memory\n");
|
2012-04-23 15:23:38 +02:00
|
|
|
|
goto fin;
|
2007-05-10 16:21:09 +02:00
|
|
|
|
};
|
|
|
|
|
}
|
2012-04-23 15:23:38 +02:00
|
|
|
|
fseek(infile,sample->offset+8,SEEK_SET);
|
|
|
|
|
/* Assuming that jp and ftyp markers size do: */
|
|
|
|
|
fread(frame_codestream, sample->sample_size-8, 1, infile);
|
2007-05-10 16:21:09 +02:00
|
|
|
|
|
|
|
|
|
/* open a byte stream */
|
2012-04-23 15:23:38 +02:00
|
|
|
|
cio = opj_cio_open((opj_common_ptr)dinfo, frame_codestream, sample->sample_size-8);
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
2012-04-23 15:23:38 +02:00
|
|
|
|
if(cio == NULL) goto fin;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
2012-04-23 15:23:38 +02:00
|
|
|
|
img = opj_decode(dinfo, cio);
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
2012-04-23 15:23:38 +02:00
|
|
|
|
if(img == NULL) goto fin;
|
2010-12-08 12:06:41 +01:00
|
|
|
|
|
2012-04-23 15:23:38 +02:00
|
|
|
|
/* Convert frame to YUV: */
|
|
|
|
|
if (!imagetoyuv(img, argv[2]))
|
|
|
|
|
goto fin;
|
2007-05-10 16:21:09 +02:00
|
|
|
|
|
2012-04-23 15:23:38 +02:00
|
|
|
|
opj_cio_close(cio);
|
|
|
|
|
|
|
|
|
|
opj_image_destroy(img);
|
|
|
|
|
|
|
|
|
|
elapsed_time = opj_clock()-init_time;
|
|
|
|
|
fprintf(stderr, "Frame number %d/%d decoded in %.2f mseconds\n",
|
|
|
|
|
snum + 1, numframes, elapsed_time*1000);
|
|
|
|
|
total_time += elapsed_time;
|
|
|
|
|
|
|
|
|
|
}/* for (snum */
|
|
|
|
|
|
|
|
|
|
fprintf(stdout, "%d frame(s) correctly decompressed\n", snum);
|
|
|
|
|
fprintf(stdout,"Total decoding time: %.2f seconds (%.1f fps)\n",
|
|
|
|
|
total_time, (float)numframes/total_time);
|
|
|
|
|
|
|
|
|
|
failed = 0;
|
|
|
|
|
|
|
|
|
|
fin:
|
|
|
|
|
fclose(infile);
|
|
|
|
|
|
|
|
|
|
if(frame_codestream) free(frame_codestream);
|
2007-08-21 12:18:40 +02:00
|
|
|
|
|
2007-05-10 16:21:09 +02:00
|
|
|
|
/* free remaining structures */
|
2012-04-23 15:23:38 +02:00
|
|
|
|
if(dinfo)
|
|
|
|
|
{
|
|
|
|
|
mj2_destroy_decompress(movie);
|
2007-05-10 16:21:09 +02:00
|
|
|
|
free(dinfo);
|
2012-04-23 15:23:38 +02:00
|
|
|
|
}
|
2007-05-10 16:21:09 +02:00
|
|
|
|
|
2012-04-23 15:23:38 +02:00
|
|
|
|
return failed;
|
|
|
|
|
}/* main() */
|