1 /*
2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
6 *
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8 * Copyright (c) 2002-2014, Professor Benoit Macq
9 * Copyright (c) 2003-2007, Francois-Olivier Devaux
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "openjpeg.h"
39 #include "cio.h"
40 #include "j2k.h"
41 #include "jp2.h"
42 #include "mj2.h"
43
44 /* -------------------------------------------------------------------------- */
45
46 /**
47 sample error callback expecting a FILE* client object
48 */
error_callback(const char * msg,void * client_data)49 void error_callback(const char *msg, void *client_data)
50 {
51 FILE *stream = (FILE*)client_data;
52 fprintf(stream, "[ERROR] %s", msg);
53 }
54 /**
55 sample warning callback expecting a FILE* client object
56 */
warning_callback(const char * msg,void * client_data)57 void warning_callback(const char *msg, void *client_data)
58 {
59 FILE *stream = (FILE*)client_data;
60 fprintf(stream, "[WARNING] %s", msg);
61 }
62 /**
63 sample debug callback expecting a FILE* client object
64 */
info_callback(const char * msg,void * client_data)65 void info_callback(const char *msg, void *client_data)
66 {
67 FILE *stream = (FILE*)client_data;
68 fprintf(stream, "[INFO] %s", msg);
69 }
70
71 /* -------------------------------------------------------------------------- */
72
73
main(int argc,char * argv[])74 int main(int argc, char *argv[])
75 {
76 opj_dinfo_t* dinfo;
77 opj_event_mgr_t event_mgr; /* event manager */
78 int tnum;
79 unsigned int 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[FILENAME_MAX];
86 mj2_dparameters_t parameters;
87
88 if (argc != 3) {
89 printf("Usage: %s mj2filename output_prefix\n", argv[0]);
90 printf("Example: %s foreman.mj2 output/foreman\n", argv[0]);
91 return 1;
92 }
93
94 if (strlen(argv[2]) + 11 > sizeof(outfilename)) {
95 fprintf(stderr, "filename %d too long\n", strlen(argv[2]) + 11);
96 return 1;
97 }
98
99 file = fopen(argv[1], "rb");
100
101 if (!file) {
102 fprintf(stderr, "failed to open %s for reading\n", argv[1]);
103 return 1;
104 }
105
106 /*
107 configure the event callbacks (not required)
108 setting of each callback is optional
109 */
110 memset(&event_mgr, 0, sizeof(opj_event_mgr_t));
111 event_mgr.error_handler = error_callback;
112 event_mgr.warning_handler = warning_callback;
113 event_mgr.info_handler = info_callback;
114
115 /* get a MJ2 decompressor handle */
116 dinfo = mj2_create_decompress();
117
118 /* catch events using our callbacks and give a local context */
119 opj_set_event_mgr((opj_common_ptr)dinfo, &event_mgr, stderr);
120
121 /* setup the decoder decoding parameters using user parameters */
122 memset(¶meters, 0, sizeof(mj2_dparameters_t));
123 movie = (opj_mj2_t*) dinfo->mj2_handle;
124 mj2_setup_decoder(movie, ¶meters);
125
126 if (mj2_read_struct(file, movie)) { /* Creating the movie structure*/
127 return 1;
128 }
129
130 /* Decode first video track */
131 tnum = 0;
132 while (movie->tk[tnum].track_type != 0) {
133 tnum ++;
134 }
135
136 track = &movie->tk[tnum];
137
138 fprintf(stdout, "Extracting %d frames from file...\n", track->num_samples);
139
140 for (snum = 0; snum < track->num_samples; snum++) {
141 sample = &track->sample[snum];
142 frame_codestream = (unsigned char*) malloc(sample->sample_size -
143 8); /* Skipping JP2C marker*/
144 fseek(file, sample->offset + 8, SEEK_SET);
145 fread(frame_codestream, sample->sample_size - 8, 1,
146 file); /* Assuming that jp and ftyp markers size do*/
147
148 {
149 int num = snprintf(outfilename, sizeof(outfilename),
150 "%s_%05d.j2k", argv[2],
151 snum);
152 if (num >= sizeof(outfilename)) {
153 fprintf(stderr, "maximum length of output prefix exceeded\n");
154 free(frame_codestream);
155 return 1;
156 }
157 }
158
159 outfile = fopen(outfilename, "wb");
160 if (!outfile) {
161 fprintf(stderr, "failed to open %s for writing\n", outfilename);
162 free(frame_codestream);
163 return 1;
164 }
165 fwrite(frame_codestream, sample->sample_size - 8, 1, outfile);
166 fclose(outfile);
167 free(frame_codestream);
168 }
169 fclose(file);
170 fprintf(stdout, "%d frames correctly extracted\n", snum);
171
172 /* free remaining structures */
173 if (dinfo) {
174 mj2_destroy_decompress((opj_mj2_t*)dinfo->mj2_handle);
175 }
176
177 return 0;
178 }
179