2 * Copyright (c) 2003-2004, Fran�ois-Olivier Devaux
3 * Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
36 #include "mj2_convert.h"
38 #define MJ2_JP 0x6a502020
39 #define MJ2_FTYP 0x66747970
40 #define MJ2_MJ2 0x6d6a7032
41 #define MJ2_MJ2S 0x6d6a3273
42 #define MJ2_MDAT 0x6d646174
43 #define MJ2_MOOV 0x6d6f6f76
44 #define MJ2_MVHD 0x6d766864
45 #define MJ2_TRAK 0x7472616b
46 #define MJ2_TKHD 0x746b6864
47 #define MJ2_MDIA 0x6d646961
48 #define MJ2_MDHD 0x6d646864
49 #define MJ2_MHDR 0x6d686472
50 #define MJ2_HDLR 0x68646C72
51 #define MJ2_MINF 0x6d696e66
52 #define MJ2_VMHD 0x766d6864
53 #define MJ2_SMHD 0x736d6864
54 #define MJ2_HMHD 0x686d6864
55 #define MJ2_DINF 0x64696e66
56 #define MJ2_DREF 0x64726566
57 #define MJ2_URL 0x75726c20
58 #define MJ2_URN 0x75726e20
59 #define MJ2_STBL 0x7374626c
60 #define MJ2_STSD 0x73747364
61 #define MJ2_STTS 0x73747473
62 #define MJ2_STSC 0x73747363
63 #define MJ2_STSZ 0x7374737A
64 #define MJ2_STCO 0x7374636f
65 #define MJ2_MOOF 0x6d6f6f66
66 #define MJ2_FREE 0x66726565
67 #define MJ2_SKIP 0x736b6970
68 #define MJ2_JP2C 0x6a703263
69 #define MJ2_FIEL 0x6669656c
70 #define MJ2_JP2P 0x6a703270
71 #define MJ2_JP2X 0x6a703278
72 #define MJ2_JSUB 0x6a737562
73 #define MJ2_ORFO 0x6f72666f
74 #define MJ2_MVEX 0x6d766578
75 #define MJ2_JP2 0x6a703220
76 #define MJ2_J2P0 0x4a325030
80 * Free movie structure memory
83 void mj2_memory_free(mj2_movie_t * movie)
88 if (movie->num_cl != 0)
91 for (i = 0; i < movie->num_vtk + movie->num_stk + movie->num_htk; i++) {
93 if (tk->name_size != 0)
101 if (tk->num_jp2x != 0)
103 if (tk->num_tts != 0)
105 if (tk->num_chunks != 0)
107 if (tk->num_samplestochunk != 0)
108 free(tk->sampletochunk);
109 if (tk->num_samples != 0)
122 int mj2_read_boxhdr(mj2_box_t * box)
124 box->init_pos = cio_tell();
125 box->length = cio_read(4);
126 box->type = cio_read(4);
127 if (box->length == 1) {
128 if (cio_read(4) != 0) {
129 fprintf(stderr, "Error: Cannot handle box sizes higher than 2^32\n");
132 box->length = cio_read(4);
139 * Initialisation of a Standard Video Track
140 * with one sample per chunk
143 int mj2_init_stdmovie(mj2_movie_t * movie)
148 movie->brand = MJ2_MJ2;
149 movie->minversion = 0;
152 (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
154 movie->cl[0] = MJ2_MJ2;
155 movie->cl[1] = MJ2_MJ2S;
156 time(<ime); /* Time since 1/1/70 */
157 movie->creation_time = (unsigned int) ltime + 2082844800; /* Seconds between 1/1/04 and 1/1/70 */
158 movie->timescale = 1000;
160 movie->rate = 1; /* Rate to play presentation (default = 0x00010000) */
161 movie->volume = 1; /* Movie volume (default = 0x0100) */
162 movie->trans_matrix[0] = 0x00010000; /* Transformation matrix for video */
163 movie->trans_matrix[1] = 0; /* Unity is { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 } */
164 movie->trans_matrix[2] = 0;
165 movie->trans_matrix[3] = 0;
166 movie->trans_matrix[4] = 0x00010000;
167 movie->trans_matrix[5] = 0;
168 movie->trans_matrix[6] = 0;
169 movie->trans_matrix[7] = 0;
170 movie->trans_matrix[8] = 0x40000000;
174 for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) {
175 mj2_tk_t *tk = &movie->tk[i];
176 if (tk->track_type == 0) {
177 tk->num_samples = yuv_num_frames(tk);
179 if (tk->num_samples == 0)
185 tk->timescale = 1000; /* Timescale = 1 ms */
188 (mj2_sample_t *) malloc(tk->num_samples * sizeof(mj2_sample_t));
189 tk->num_chunks = tk->num_samples;
191 (mj2_chunk_t *) malloc(tk->num_chunks * sizeof(mj2_chunk_t));
192 tk->chunk[0].num_samples = 1;
193 tk->chunk[0].sample_descr_idx = 1;
195 tk->same_sample_size = 0;
197 tk->num_samplestochunk = 1; /* One sample per chunk */
199 (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
200 sizeof(mj2_sampletochunk_t));
201 tk->sampletochunk[0].first_chunk = 1;
202 tk->sampletochunk[0].samples_per_chunk = 1;
203 tk->sampletochunk[0].sample_descr_idx = 1;
205 for (j = 0; j < tk->num_samples; j++)
206 tk->sample[j].sample_delta = tk->timescale / tk->sample_rate;
209 tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
210 tk->tts[0].sample_count = tk->num_samples;
211 tk->tts[0].sample_delta = tk->timescale / tk->sample_rate;
213 tk->horizresolution = 0x00480000; /* Horizontal resolution (typically 72) */
214 tk->vertresolution = 0x00480000; /* Vertical resolution (typically 72) */
215 tk->compressorname[0] = 0x0f4d6f74; /* Compressor Name[]: Motion JPEG2000 */
216 tk->compressorname[1] = 0x696f6e20;
217 tk->compressorname[2] = 0x4a504547;
218 tk->compressorname[3] = 0x32303030;
219 tk->compressorname[4] = 0x00120000;
220 tk->compressorname[5] = 0;
221 tk->compressorname[6] = 0x00000042;
222 tk->compressorname[7] = 0x000000DC;
223 tk->num_url = 0; /* Number of URL */
224 tk->num_urn = 0; /* Number of URN */
225 tk->graphicsmode = 0; /* Graphicsmode */
226 tk->opcolor[0] = 0; /* OpColor */
227 tk->opcolor[1] = 0; /* OpColor */
228 tk->opcolor[2] = 0; /* OpColor */
229 tk->creation_time = movie->creation_time; /* Seconds between 1/1/04 and 1/1/70 */
230 tk->language = 0; /* Language (undefined) */
233 tk->trans_matrix[0] = 0x00010000; /* Transformation matrix for track */
234 tk->trans_matrix[1] = 0; /* Unity is { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 } */
235 tk->trans_matrix[2] = 0;
236 tk->trans_matrix[3] = 0;
237 tk->trans_matrix[4] = 0x00010000;
238 tk->trans_matrix[5] = 0;
239 tk->trans_matrix[6] = 0;
240 tk->trans_matrix[7] = 0;
241 tk->trans_matrix[8] = 0x40000000;
244 tk->or_fieldcount = 1;
245 tk->or_fieldorder = 0;
247 tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
249 tk->br[1] = MJ2_J2P0;
251 tk->hsub = 2; /* 4:2:0 */
252 tk->vsub = 2; /* 4:2:0 */
261 * Time To Sample box Decompact
264 void mj2_tts_decompact(mj2_tk_t * tk)
268 for (i = 0; i < tk->num_tts; i++) {
269 tk->num_samples += tk->tts[i].sample_count;
273 (mj2_sample_t *) malloc(tk->num_samples * sizeof(mj2_sample_t));
275 for (i = 0; i < tk->num_tts; i++) {
276 for (j = 0; j < tk->tts[i].sample_count; j++) {
277 tk->sample[j].sample_delta = tk->tts[i].sample_delta;
283 * Sample To Chunk box Decompact
286 void mj2_stsc_decompact(mj2_tk_t * tk)
292 if (tk->num_samplestochunk == 1) {
294 (unsigned int) ceil((double) tk->num_samples /
295 (double) tk->sampletochunk[0].samples_per_chunk);
297 (mj2_chunk_t *) malloc(tk->num_chunks * sizeof(mj2_chunk_t));
298 for (k = 0; k < tk->num_chunks; k++) {
299 tk->chunk[k].num_samples = tk->sampletochunk[0].samples_per_chunk;
304 (mj2_chunk_t *) malloc(tk->num_samples * sizeof(mj2_chunk_t));
306 for (i = 0; i < tk->num_samplestochunk -1 ; i++) {
307 for (j = tk->sampletochunk[i].first_chunk - 1;
308 j < tk->sampletochunk[i + 1].first_chunk - 1; j++) {
309 tk->chunk[j].num_samples = tk->sampletochunk[i].samples_per_chunk;
311 sampleno += tk->chunk[j].num_samples;
314 tk->num_chunks += (int)(tk->num_samples - sampleno) / tk->sampletochunk[tk->num_samplestochunk - 1].samples_per_chunk;
315 for (k = tk->sampletochunk[tk->num_samplestochunk - 1].first_chunk - 1;
316 k < tk->num_chunks; k++) {
317 tk->chunk[k].num_samples =
318 tk->sampletochunk[tk->num_samplestochunk - 1].samples_per_chunk;
320 tk->chunk = realloc(tk->chunk, tk->num_chunks * sizeof(mj2_chunk_t));
327 * Chunk offset box Decompact
330 void mj2_stco_decompact(mj2_tk_t * tk)
335 int intra_chunk_offset;
337 for (i = 0; i < tk->num_chunks; i++) {
338 intra_chunk_offset = 0;
339 for (j = 0; j < tk->chunk[i].num_samples; j++) {
340 tk->sample[k].offset = intra_chunk_offset + tk->chunk[i].offset;
341 intra_chunk_offset += tk->sample[k].sample_size;
356 box.init_pos = cio_tell();
359 cio_write(MJ2_JP, 4); /* JP */
360 cio_write(0x0d0a870a, 4); /* 0x0d0a870a required in a JP box */
362 box.length = cio_tell() - box.init_pos;
363 cio_seek(box.init_pos);
364 cio_write(box.length, 4);
365 cio_seek(box.init_pos + box.length);
371 * JPEG 2000 signature
378 mj2_read_boxhdr(&box);
379 if (MJ2_JP != box.type) { /* Check Marker */
380 fprintf(stderr, "Error: Expected JP Marker\n");
383 if (0x0d0a870a != cio_read(4)) { /* read the 0x0d0a870a required in a JP box */
384 fprintf(stderr, "Error with JP Marker\n");
387 if (cio_tell() - box.init_pos != box.length) { /* Check box length */
388 fprintf(stderr, "Error with JP Box size \n");
401 void mj2_write_ftyp(mj2_movie_t * movie)
405 box.init_pos = cio_tell();
408 cio_write(MJ2_FTYP, 4); /* FTYP */
409 cio_write(movie->brand, 4); /* BR */
410 cio_write(movie->minversion, 4); /* MinV */
412 for (i = 0; i < movie->num_cl; i++)
413 cio_write(movie->cl[i], 4); /* CL */
415 box.length = cio_tell() - box.init_pos;
416 cio_seek(box.init_pos);
417 cio_write(box.length, 4); /* Length */
418 cio_seek(box.init_pos + box.length);
427 int mj2_read_ftyp(mj2_movie_t * movie)
432 mj2_read_boxhdr(&box); /* Box Size */
433 if (MJ2_FTYP != box.type) {
434 fprintf(stderr, "Error: Expected FTYP Marker\n");
438 movie->brand = cio_read(4); /* BR */
439 movie->minversion = cio_read(4); /* MinV */
440 movie->num_cl = (box.length - 16) / 4;
442 (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
444 for (i = movie->num_cl - 1; i > -1; i--)
445 movie->cl[i] = cio_read(4); /* CLi */
447 if (cio_tell() - box.init_pos != box.length) {
448 fprintf(stderr, "Error with FTYP Box\n");
460 int mj2_write_mdat(FILE * outfile, mj2_movie_t * movie, j2k_image_t * img,
461 j2k_cp_t * cp, char *outbuf, char *index)
463 unsigned char box_len_ptr;
468 int pos_correction = 0;
471 box.init_pos = cio_tell();
473 cio_write(MJ2_MDAT, 4); /* MDAT */
475 for (i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++) {
476 if (movie->tk[i].track_type != 0) {
477 fprintf(stderr, "Unable to write sound or hint tracks\n");
484 fprintf(stderr, "Video Track number %d\n", i + 1);
487 fwrite(outbuf, 1, len, outfile);
488 pos_correction = cio_tell() + pos_correction;
491 /* Copy the first tile coding parameters (tcp) to cp_init */
494 (j2k_tcp_t *) malloc(cp->tw * cp->th * sizeof(j2k_tcp_t));
495 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
496 for (l = 0; l < cp->tcps[tileno].numlayers; l++) {
497 cp_init.tcps[tileno].rates[l] = cp->tcps[tileno].rates[l];
498 //tileno = cp->tcps[tileno].rates[l];
503 for (j = 0; j < tk->num_samples; j++) {
504 outbuf = (char *) malloc(cp->tdx * cp->tdy * cp->th * cp->tw * 2);
505 cio_init(outbuf, cp->tdx * cp->tdy * cp->th * cp->tw * 2);
507 fprintf(stderr, "Frame number %d/%d: \n", j + 1, tk->num_samples);
510 if (!yuvtoimage(tk, img, j)) {
511 fprintf(stderr, "Error with frame number %d in YUV file\n", j);
515 len = jp2_write_jp2c(img, cp, outbuf, index);
517 for (m = 0; m < img->numcomps; m++) {
518 free(img->comps[m].data);
521 tk->sample[j].sample_size = len;
523 tk->sample[j].offset = pos_correction;
524 tk->chunk[j].offset = pos_correction; /* There is one sample per chunk */
526 fwrite(outbuf, 1, len, outfile);
528 pos_correction = cio_tell() + pos_correction;
532 /* Copy the cp_init parameters to cp->tcps */
534 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
535 for (k = 0; k < cp->tcps[tileno].numlayers; k++) {
536 cp->tcps[tileno].rates[k] = cp_init.tcps[tileno].rates[k];
542 box.length = pos_correction - box.init_pos;
544 fseek(outfile, box.init_pos, SEEK_SET);
546 cio_init(&box_len_ptr, 4); /* Init a cio to write box length variable in a little endian way */
547 cio_write(box.length, 4);
549 fwrite(&box_len_ptr, 4, 1, outfile);
551 fseek(outfile, box.init_pos + box.length, SEEK_SET);
563 int mj2_read_mdat(mj2_movie_t * movie, unsigned char *src, char *outfile)
567 int jp2c_cio_len, jp2c_len, pos_correction = 0;
572 mj2_read_boxhdr(&box);
573 if (MJ2_MDAT != box.type) {
574 fprintf(stderr, "Error: Expected MDAT Marker\n");
578 pos_correction = cio_tell() - box.init_pos;
580 f = fopen(outfile, "w"); /* Erase content of file if it already exists */
583 for (track_nb = 0; track_nb < movie->next_tk_id - 1; track_nb++) {
584 if (movie->tk[track_nb].imagefile != NULL) {
585 fprintf(stderr, "%s", movie->tk[track_nb].imagefile);
587 f = fopen(movie->tk[track_nb].imagefile, "w"); // Erase content of file if it already exists
593 track_nb < movie->num_htk + movie->num_stk + movie->num_vtk;
595 mj2_tk_t *tk = &movie->tk[track_nb];
596 if (tk->track_type != 0) {
597 cio_seek(box.init_pos);
598 cio_skip(box.length);
600 fprintf(stderr, "Track %d: Width=%d Height=%d\n", track_nb,
602 fprintf(stderr, "%d Samples\n", tk->num_samples);
604 if (tk->imagefile == NULL) {
605 tk->imagefile = outfile;
608 for (i = 0; i < tk->num_samples; i++) {
610 mj2_sample_t *sample = &tk->sample[i];
615 fprintf(stderr, "Frame %d / %d: \n", i+1, tk->num_samples);
617 cio_init(src + sample->offset, 8);
619 jp2c_cio_len = cio_tell();
620 jp2c_len = cio_read(4);
623 if (MJ2_JP2C != cio_read(4)) {
624 fprintf(stderr, "Error: Expected JP2C Marker\n");
628 pos = src + sample->offset + 8;
630 cio_seek(sample->offset + 8);
632 if (!j2k_decode(pos, sample->sample_size, &img, &cp))
635 if (imagetoyuv(&img, &cp, tk->imagefile))
640 if (cio_tell() + 8 != jp2c_len) {
641 fprintf(stderr, "Error with JP2C Box Size\n");
649 cio_seek(box.init_pos);
650 cio_skip(box.length); /* Go back to box end */
661 void mj2_write_stco(mj2_tk_t * tk)
666 box.init_pos = cio_tell();
668 cio_write(MJ2_STCO, 4); /* STCO */
670 cio_write(0, 4); /* Version = 0, flags = 0 */
672 cio_write(tk->num_chunks, 4); /* Entry Count */
674 for (i = 0; i < tk->num_chunks; i++) {
675 cio_write(tk->chunk[i].offset, 4); /* Entry offset */
678 box.length = cio_tell() - box.init_pos;
679 cio_seek(box.init_pos);
680 cio_write(box.length, 4); /* L */
681 cio_seek(box.init_pos + box.length);
690 int mj2_read_stco(mj2_tk_t * tk)
695 mj2_read_boxhdr(&box); /* Box Size */
696 if (MJ2_STCO != box.type) {
697 fprintf(stderr, "Error: Expected STCO Marker\n");
701 if (0 != cio_read(1)) { /* Version = 0 */
702 fprintf(stderr, "Error: Only Version 0 handled in STCO box\n");
706 if (0 != cio_read(3)) { /* Flags = 0 */
707 fprintf(stderr, "Error with flag in STCO box. Expected flag 0\n");
712 if (cio_read(4) != tk->num_chunks) {
714 "Error in STCO box: expecting same amount of entry-count as chunks \n");
716 for (i = 0; i < tk->num_chunks; i++) {
717 tk->chunk[i].offset = cio_read(4); /* Entry offset */
721 mj2_stco_decompact(tk);
724 if (cio_tell() - box.init_pos != box.length) {
725 fprintf(stderr, "Error with STCO Box size\n");
737 void mj2_write_stsz(mj2_tk_t * tk)
742 box.init_pos = cio_tell();
744 cio_write(MJ2_STSZ, 4); /* STSZ */
746 cio_write(0, 4); /* Version = 0, flags = 0 */
748 if (tk->same_sample_size == 1) { /* If they all have the same size */
749 cio_write(tk->sample[0].sample_size, 4); /* Size */
751 cio_write(1, 4); /* Entry count = 1 */
755 cio_write(0, 4); /* Sample Size = 0 becase they all have different sizes */
757 cio_write(tk->num_samples, 4); /* Sample Count */
759 for (i = 0; i < tk->num_samples; i++) {
760 cio_write(tk->sample[i].sample_size, 4);
764 box.length = cio_tell() - box.init_pos;
765 cio_seek(box.init_pos);
766 cio_write(box.length, 4); /* L */
767 cio_seek(box.init_pos + box.length);
776 int mj2_read_stsz(mj2_tk_t * tk)
782 mj2_read_boxhdr(&box); /* Box Size */
783 if (MJ2_STSZ != box.type) {
784 fprintf(stderr, "Error: Expected STSZ Marker\n");
789 if (0 != cio_read(1)) { /* Version = 0 */
790 fprintf(stderr, "Error: Only Version 0 handled in STSZ box\n");
794 if (0 != cio_read(3)) { /* Flags = 0 */
795 fprintf(stderr, "Error with flag in STSZ box. Expected flag 0\n");
799 sample_size = cio_read(4);
801 if (sample_size != 0) { /* Samples do have the same size */
802 tk->same_sample_size = 1;
803 for (i = 0; i < tk->num_samples; i++) {
804 tk->sample[i].sample_size = sample_size;
806 cio_skip(4); /* Sample count = 1 */
808 tk->same_sample_size = 0;
809 if (tk->num_samples != cio_read(4)) { /* Sample count */
811 "Error in STSZ box. Expected that sample-count is number of samples in track\n");
814 for (i = 0; i < tk->num_samples; i++) {
815 tk->sample[i].sample_size = cio_read(4); /* Sample Size */
818 if (cio_tell() - box.init_pos != box.length) {
819 fprintf(stderr, "Error with STSZ Box size\n");
833 void mj2_write_stsc(mj2_tk_t * tk)
838 box.init_pos = cio_tell();
840 cio_write(MJ2_STSC, 4); /* STSC */
842 cio_write(0, 4); /* Version = 0, flags = 0 */
844 cio_write(tk->num_samplestochunk, 4); /* Entry Count */
846 for (i = 0; i < tk->num_samplestochunk; i++) {
847 cio_write(tk->sampletochunk[i].first_chunk, 4); /* First Chunk */
848 cio_write(tk->sampletochunk[i].samples_per_chunk, 4); /* Samples per chunk */
849 cio_write(tk->sampletochunk[i].sample_descr_idx, 4); /* Samples description index */
853 box.length = cio_tell() - box.init_pos;
854 cio_seek(box.init_pos);
855 cio_write(box.length, 4); /* L */
856 cio_seek(box.init_pos + box.length);
865 int mj2_read_stsc(mj2_tk_t * tk)
870 mj2_read_boxhdr(&box); /* Box Size */
871 if (MJ2_STSC != box.type) {
872 fprintf(stderr, "Error: Expected STSC Marker\n");
877 if (0 != cio_read(1)) { /* Version = 0 */
878 fprintf(stderr, "Error: Only Version 0 handled in STSC box\n");
882 if (0 != cio_read(3)) { /* Flags = 0 */
883 fprintf(stderr, "Error with flag in STSC box. Expected flag 0\n");
887 tk->num_samplestochunk = cio_read(4);
890 (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
891 sizeof(mj2_sampletochunk_t));
894 for (i = 0; i < tk->num_samplestochunk; i++) {
895 tk->sampletochunk[i].first_chunk = cio_read(4);
896 tk->sampletochunk[i].samples_per_chunk = cio_read(4);
897 tk->sampletochunk[i].sample_descr_idx = cio_read(4);
900 mj2_stsc_decompact(tk); /* decompact sample to chunk box */
903 if (cio_tell() - box.init_pos != box.length) {
904 fprintf(stderr, "Error with STSC Box size\n");
916 void mj2_write_stts(mj2_tk_t * tk)
922 box.init_pos = cio_tell();
924 cio_write(MJ2_STTS, 4); /* STTS */
926 cio_write(0, 4); /* Version = 0, flags = 0 */
928 cio_write(tk->num_tts, 4); /* entry_count */
929 for (i = 0; i < tk->num_tts; i++) {
930 cio_write(tk->tts[i].sample_count, 4); /* Sample-count */
931 cio_write(tk->tts[i].sample_delta, 4); /* Sample-Delta */
934 box.length = cio_tell() - box.init_pos;
935 cio_seek(box.init_pos);
936 cio_write(box.length, 4); /* L */
937 cio_seek(box.init_pos + box.length);
946 int mj2_read_stts(mj2_tk_t * tk)
952 mj2_read_boxhdr(&box);
953 if (MJ2_STTS != box.type) {
954 fprintf(stderr, "Error: Expected STTS Marker\n");
959 if (0 != cio_read(1)) { /* Version = 0 */
960 fprintf(stderr, "Error: Only Version 0 handled in STTS box\n");
964 if (0 != cio_read(3)) { /* Flags = 0 */
965 fprintf(stderr, "Error with flag in STTS box. Expected flag 0\n");
969 tk->num_tts = cio_read(4);
971 tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
973 for (i = 0; i < tk->num_tts; i++) {
974 tk->tts[i].sample_count = cio_read(4);
975 tk->tts[i].sample_delta = cio_read(4);
978 mj2_tts_decompact(tk);
980 if (cio_tell() - box.init_pos != box.length) {
981 fprintf(stderr, "Error with STTS Box size\n");
993 void mj2_write_fiel(mj2_tk_t * tk)
998 box.init_pos = cio_tell();
1000 cio_write(MJ2_FIEL, 4); /* STTS */
1002 cio_write(tk->fieldcount, 1); /* Field count */
1003 cio_write(tk->fieldorder, 1); /* Field order */
1006 box.length = cio_tell() - box.init_pos;
1007 cio_seek(box.init_pos);
1008 cio_write(box.length, 4); /* L */
1009 cio_seek(box.init_pos + box.length);
1018 int mj2_read_fiel(mj2_tk_t * tk)
1023 mj2_read_boxhdr(&box);
1024 if (MJ2_FIEL != box.type) {
1025 fprintf(stderr, "Error: Expected FIEL Marker\n");
1030 tk->fieldcount = cio_read(1);
1031 tk->fieldorder = cio_read(1);
1033 if (cio_tell() - box.init_pos != box.length) {
1034 fprintf(stderr, "Error with FIEL Box size\n");
1041 * Write the ORFO box
1043 * Original Format Box
1046 void mj2_write_orfo(mj2_tk_t * tk)
1050 box.init_pos = cio_tell();
1052 cio_write(MJ2_ORFO, 4);
1054 cio_write(tk->or_fieldcount, 1); /* Original Field count */
1055 cio_write(tk->or_fieldorder, 1); /* Original Field order */
1058 box.length = cio_tell() - box.init_pos;
1059 cio_seek(box.init_pos);
1060 cio_write(box.length, 4); /* L */
1061 cio_seek(box.init_pos + box.length);
1067 * Original Format Box
1070 int mj2_read_orfo(mj2_tk_t * tk)
1075 mj2_read_boxhdr(&box);
1076 if (MJ2_ORFO != box.type) {
1077 fprintf(stderr, "Error: Expected ORFO Marker\n");
1082 tk->or_fieldcount = cio_read(1);
1083 tk->or_fieldorder = cio_read(1);
1085 if (cio_tell() - box.init_pos != box.length) {
1086 fprintf(stderr, "Error with ORFO Box size\n");
1093 * Write the JP2P box
1098 void mj2_write_jp2p(mj2_tk_t * tk)
1104 box.init_pos = cio_tell();
1106 cio_write(MJ2_JP2P, 4);
1108 cio_write(0, 4); /* Version 0, flags =0 */
1110 for (i = 0; i < tk->num_br; i++) {
1111 cio_write(tk->br[i], 4);
1114 box.length = cio_tell() - box.init_pos;
1115 cio_seek(box.init_pos);
1116 cio_write(box.length, 4); /* L */
1117 cio_seek(box.init_pos + box.length);
1126 int mj2_read_jp2p(mj2_tk_t * tk)
1132 mj2_read_boxhdr(&box);
1133 if (MJ2_JP2P != box.type) {
1134 fprintf(stderr, "Error: Expected JP2P Marker\n");
1138 if (0 != cio_read(1)) { /* Version = 0 */
1139 fprintf(stderr, "Error: Only Version 0 handled in JP2P box\n");
1143 if (0 != cio_read(3)) { /* Flags = 0 */
1144 fprintf(stderr, "Error with flag in JP2P box. Expected flag 0\n");
1149 tk->num_br = (box.length - 12) / 4;
1150 tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
1152 for (i = 0; i < tk->num_br; i++) {
1153 tk->br[i] = cio_read(4);
1156 if (cio_tell() - box.init_pos != box.length) {
1157 fprintf(stderr, "Error with JP2P Box size\n");
1164 * Write the JP2X box
1169 void mj2_write_jp2x(mj2_tk_t * tk)
1175 box.init_pos = cio_tell();
1177 cio_write(MJ2_JP2X, 4);
1179 for (i = 0; i < tk->num_jp2x; i++) {
1180 cio_write(tk->jp2xdata[i], 1);
1183 box.length = cio_tell() - box.init_pos;
1184 cio_seek(box.init_pos);
1185 cio_write(box.length, 4); /* L */
1186 cio_seek(box.init_pos + box.length);
1195 int mj2_read_jp2x(mj2_tk_t * tk)
1201 mj2_read_boxhdr(&box);
1202 if (MJ2_JP2X != box.type) {
1203 fprintf(stderr, "Error: Expected JP2X Marker\n");
1208 tk->num_jp2x = (box.length - 8);
1210 (unsigned char *) malloc(tk->num_jp2x * sizeof(unsigned char));
1212 for (i = 0; i < tk->num_jp2x; i++) {
1213 tk->jp2xdata[i] = cio_read(1);
1216 if (cio_tell() - box.init_pos != box.length) {
1217 fprintf(stderr, "Error with JP2X Box size\n");
1224 * Write the JSUB box
1226 * MJP2 Subsampling Box
1229 void mj2_write_jsub(mj2_tk_t * tk)
1234 box.init_pos = cio_tell();
1236 cio_write(MJ2_JSUB, 4);
1238 cio_write(tk->hsub, 1);
1239 cio_write(tk->vsub, 1);
1240 cio_write(tk->hoff, 1);
1241 cio_write(tk->voff, 1);
1243 box.length = cio_tell() - box.init_pos;
1244 cio_seek(box.init_pos);
1245 cio_write(box.length, 4); /* L */
1246 cio_seek(box.init_pos + box.length);
1252 * MJP2 Subsampling Box
1255 int mj2_read_jsub(mj2_tk_t * tk)
1259 mj2_read_boxhdr(&box);
1260 if (MJ2_JSUB != box.type) {
1261 fprintf(stderr, "Error: Expected JSUB Marker\n");
1265 tk->hsub = cio_read(1);
1266 tk->vsub = cio_read(1);
1267 tk->hoff = cio_read(1);;
1268 tk->voff = cio_read(1);
1270 if (cio_tell() - box.init_pos != box.length) {
1271 fprintf(stderr, "Error with JSUB Box size\n");
1278 * Write the SMJ2 box
1280 * Visual Sample Entry Description
1283 void mj2_write_smj2(j2k_image_t * img, mj2_tk_t * tk)
1288 box.init_pos = cio_tell();
1290 cio_write(MJ2_MJ2, 4); /* MJ2 */
1292 cio_write(0, 4); /* Version = 0, flags = 0 */
1296 cio_write(0, 2); /* Pre-defined */
1298 cio_write(0, 2); /* Reserved */
1300 cio_write(0, 4); /* Pre-defined */
1301 cio_write(0, 4); /* Pre-defined */
1302 cio_write(0, 4); /* Pre-defined */
1304 cio_write(tk->w, 2); /* Width */
1305 cio_write(tk->h, 2); /* Height */
1307 cio_write(tk->horizresolution, 4); /* Horizontal resolution */
1308 cio_write(tk->vertresolution, 4); /* Vertical resolution */
1310 cio_write(0, 4); /* Reserved */
1312 cio_write(1, 2); /* Pre-defined = 1 */
1314 cio_write(tk->compressorname[0], 4); /* Compressor Name */
1315 cio_write(tk->compressorname[1], 4);
1316 cio_write(tk->compressorname[2], 4);
1317 cio_write(tk->compressorname[3], 4);
1318 cio_write(tk->compressorname[4], 4);
1319 cio_write(tk->compressorname[5], 4);
1320 cio_write(tk->compressorname[6], 4);
1321 cio_write(tk->compressorname[7], 4);
1325 for (i = 0; i < img->numcomps; i++)
1326 tk->depth += img->comps[i].bpp;
1328 cio_write(tk->depth, 2); /* Depth */
1330 cio_write(0xffff, 2); /* Pre-defined = -1 */
1332 jp2_init_stdjp2(&tk->jp2_struct, img);
1334 jp2_write_jp2h(&tk->jp2_struct);
1338 if (tk->num_br != 0)
1340 if (tk->num_jp2x != 0)
1346 box.length = cio_tell() - box.init_pos;
1347 cio_seek(box.init_pos);
1348 cio_write(box.length, 4); /* L */
1349 cio_seek(box.init_pos + box.length);
1355 * Visual Sample Entry Description
1358 int mj2_read_smj2(j2k_image_t * img, mj2_tk_t * tk)
1364 mj2_read_boxhdr(&box);
1366 if (MJ2_MJ2 != box.type) {
1367 fprintf(stderr, "Error in SMJ2 box: Expected MJ2 Marker\n");
1371 if (0 != cio_read(1)) { /* Version = 0 */
1372 fprintf(stderr, "Error: Only Version 0 handled in MJP2 box\n");
1376 if (0 != cio_read(3)) { /* Flags = 0 */
1377 fprintf(stderr, "Error with flag in MJP2 box. Expected flag 0\n");
1383 cio_skip(2); /* Pre-defined */
1385 cio_skip(2); /* Reserved */
1387 cio_skip(4); /* Pre-defined */
1388 cio_skip(4); /* Pre-defined */
1389 cio_skip(4); /* Pre-defined */
1391 tk->w = cio_read(2); /* Width */
1392 tk->h = cio_read(2); /* Height */
1394 tk->horizresolution = cio_read(4); /* Horizontal resolution */
1395 tk->vertresolution = cio_read(4); /* Vertical resolution */
1397 cio_skip(4); /* Reserved */
1399 cio_skip(2); /* Pre-defined = 1 */
1401 tk->compressorname[0] = cio_read(4); /* Compressor Name */
1402 tk->compressorname[1] = cio_read(4);
1403 tk->compressorname[2] = cio_read(4);
1404 tk->compressorname[3] = cio_read(4);
1405 tk->compressorname[4] = cio_read(4);
1406 tk->compressorname[5] = cio_read(4);
1407 tk->compressorname[6] = cio_read(4);
1408 tk->compressorname[7] = cio_read(4);
1410 tk->depth = cio_read(2); /* Depth */
1412 /* Init std value */
1416 tk->or_fieldcount = 1;
1417 tk->or_fieldorder = 0;
1419 cio_skip(2); /* Pre-defined = -1 */
1421 if (jp2_read_jp2h(&tk->jp2_struct)) {
1422 fprintf(stderr, "Error with JP2H Box\n");
1426 for (i = 0; cio_tell() - box.init_pos < box.length; i++) {
1427 mj2_read_boxhdr(&box2);
1428 cio_seek(box2.init_pos);
1429 switch (box2.type) {
1431 if (mj2_read_fiel(tk))
1436 if (mj2_read_jp2p(tk))
1441 if (mj2_read_jp2x(tk))
1446 if (mj2_read_jsub(tk))
1451 if (mj2_read_orfo(tk))
1456 fprintf(stderr, "Error with MJP2 Box size\n");
1467 * Write the STSD box
1469 * Sample Description
1472 void mj2_write_stsd(mj2_tk_t * tk, j2k_image_t * img)
1476 box.init_pos = cio_tell();
1478 cio_write(MJ2_STSD, 4); /* STSD */
1480 cio_write(0, 4); /* Version = 0, flags = 0 */
1482 cio_write(1, 4); /* entry_count = 1 (considering same JP2 headerboxes) */
1484 if (tk->track_type == 0) {
1485 mj2_write_smj2(img, tk);
1486 } else if (tk->track_type == 1) {
1489 if (tk->track_type == 2) {
1494 box.length = cio_tell() - box.init_pos;
1495 cio_seek(box.init_pos);
1496 cio_write(box.length, 4); /* L */
1497 cio_seek(box.init_pos + box.length);
1503 * Sample Description
1506 int mj2_read_stsd(mj2_tk_t * tk, j2k_image_t * img)
1509 int entry_count, len_2skip;
1513 mj2_read_boxhdr(&box);
1515 if (MJ2_STSD != box.type) {
1516 fprintf(stderr, "Error: Expected STSD Marker\n");
1520 if (0 != cio_read(1)) { /* Version = 0 */
1521 fprintf(stderr, "Error: Only Version 0 handled in STSD box\n");
1525 if (0 != cio_read(3)) { /* Flags = 0 */
1526 fprintf(stderr, "Error with flag in STSD box. Expected flag 0\n");
1530 entry_count = cio_read(4);
1532 if (tk->track_type == 0) {
1533 for (i = 0; i < entry_count; i++) {
1534 if (mj2_read_smj2(img, tk))
1537 } else if (tk->track_type == 1) {
1538 len_2skip = cio_read(4); // Not implemented -> skipping box
1539 cio_skip(len_2skip - 4);
1540 } else if (tk->track_type == 2) {
1541 len_2skip = cio_read(4); // Not implemented -> skipping box
1542 cio_skip(len_2skip - 4);
1546 if (cio_tell() - box.init_pos != box.length) {
1547 fprintf(stderr, "Error with STSD Box size\n");
1554 * Write the STBL box
1556 * Sample table box box
1559 void mj2_write_stbl(mj2_tk_t * tk, j2k_image_t * img)
1563 box.init_pos = cio_tell();
1565 cio_write(MJ2_STBL, 4); /* STBL */
1567 mj2_write_stsd(tk, img);
1573 box.length = cio_tell() - box.init_pos;
1574 cio_seek(box.init_pos);
1575 cio_write(box.length, 4); /* L */
1576 cio_seek(box.init_pos + box.length);
1582 * Sample table box box
1585 int mj2_read_stbl(mj2_tk_t * tk, j2k_image_t * img)
1589 mj2_read_boxhdr(&box);
1590 if (MJ2_STBL != box.type) {
1591 fprintf(stderr, "Error: Expected STBL Marker\n");
1595 if (mj2_read_stsd(tk, img))
1597 if (mj2_read_stts(tk))
1599 if (mj2_read_stsc(tk))
1601 if (mj2_read_stsz(tk))
1603 if (mj2_read_stco(tk))
1606 if (cio_tell() - box.init_pos != box.length) {
1607 fprintf(stderr, "Error with STBL Box size\n");
1619 void mj2_write_url(mj2_tk_t * tk, int url_num)
1623 box.init_pos = cio_tell();
1625 cio_write(MJ2_URL, 4); /* URL */
1628 cio_write(1, 4); /* Version = 0, flags = 1 because stored in same file */
1630 cio_write(0, 4); /* Version = 0, flags = 0 */
1631 cio_write(tk->url[url_num - 1].location[0], 4);
1632 cio_write(tk->url[url_num - 1].location[1], 4);
1633 cio_write(tk->url[url_num - 1].location[2], 4);
1634 cio_write(tk->url[url_num - 1].location[3], 4);
1637 box.length = cio_tell() - box.init_pos;
1638 cio_seek(box.init_pos);
1639 cio_write(box.length, 4); /* L */
1640 cio_seek(box.init_pos + box.length);
1649 int mj2_read_url(mj2_tk_t * tk, int urn_num)
1653 mj2_read_boxhdr(&box);
1654 if (MJ2_URL != box.type) {
1655 fprintf(stderr, "Error: Expected URL Marker\n");
1659 if (0 != cio_read(1)) { /* Version = 0 */
1660 fprintf(stderr, "Error: Only Version 0 handled in URL box\n");
1664 if (1 != cio_read(3)) { /* If flags = 1 --> media data in file */
1665 tk->url[urn_num].location[0] = cio_read(4);
1666 tk->url[urn_num].location[1] = cio_read(4);
1667 tk->url[urn_num].location[2] = cio_read(4);
1668 tk->url[urn_num].location[3] = cio_read(4);
1674 if (cio_tell() - box.init_pos != box.length) {
1675 fprintf(stderr, "Error with URL Box size\n");
1687 void mj2_write_urn(mj2_tk_t * tk, int urn_num)
1691 box.init_pos = cio_tell();
1693 cio_write(MJ2_URN, 4); /* URN */
1695 cio_write(0, 4); /* Version = 0, flags = 0 */
1697 cio_write(tk->urn[urn_num].name[0], 4);
1698 cio_write(tk->urn[urn_num].name[1], 4);
1699 cio_write(tk->urn[urn_num].name[2], 4);
1700 cio_write(tk->urn[urn_num].name[3], 4);
1701 cio_write(tk->urn[urn_num].location[0], 4);
1702 cio_write(tk->urn[urn_num].location[1], 4);
1703 cio_write(tk->urn[urn_num].location[2], 4);
1704 cio_write(tk->urn[urn_num].location[3], 4);
1706 box.length = cio_tell() - box.init_pos;
1707 cio_seek(box.init_pos);
1708 cio_write(box.length, 4); /* L */
1709 cio_seek(box.init_pos + box.length);
1718 int mj2_read_urn(mj2_tk_t * tk, int urn_num)
1723 mj2_read_boxhdr(&box);
1724 if (MJ2_URN != box.type) {
1725 fprintf(stderr, "Error: Expected URN Marker\n");
1729 if (0 != cio_read(1)) { /* Version = 0 */
1730 fprintf(stderr, "Error: Only Version 0 handled in URN box\n");
1734 if (1 != cio_read(3)) { /* If flags = 1 --> media data in file */
1735 tk->urn[urn_num].name[0] = cio_read(4);
1736 tk->urn[urn_num].name[1] = cio_read(4);
1737 tk->urn[urn_num].name[2] = cio_read(4);
1738 tk->urn[urn_num].name[3] = cio_read(4);
1739 tk->urn[urn_num].location[0] = cio_read(4);
1740 tk->urn[urn_num].location[1] = cio_read(4);
1741 tk->urn[urn_num].location[2] = cio_read(4);
1742 tk->urn[urn_num].location[3] = cio_read(4);
1746 if (cio_tell() - box.init_pos != box.length) {
1747 fprintf(stderr, "Error with URN Box size\n");
1755 * Write the DREF box
1757 * Data reference box
1760 void mj2_write_dref(mj2_tk_t * tk)
1765 box.init_pos = cio_tell();
1767 cio_write(MJ2_DREF, 4); /* DREF */
1769 cio_write(0, 4); /* Version = 0, flags = 0 */
1771 if (tk->num_url + tk->num_urn == 0) { /* Media data in same file */
1772 cio_write(1, 4); /* entry_count = 1 */
1773 mj2_write_url(tk, 0);
1775 cio_write(tk->num_url + tk->num_urn, 4); /* entry_count */
1777 for (i = 0; i < tk->num_url; i++)
1778 mj2_write_url(tk, i + 1);
1780 for (i = 0; i < tk->num_urn; i++)
1781 mj2_write_urn(tk, i);
1784 box.length = cio_tell() - box.init_pos;
1785 cio_seek(box.init_pos);
1786 cio_write(box.length, 4); /* L */
1787 cio_seek(box.init_pos + box.length);
1793 * Data reference box
1796 int mj2_read_dref(mj2_tk_t * tk)
1800 int entry_count, marker;
1803 mj2_read_boxhdr(&box);
1804 if (MJ2_DREF != box.type) {
1805 fprintf(stderr, "Error: Expected DREF Marker\n");
1809 if (0 != cio_read(1)) { /* Version = 0 */
1810 fprintf(stderr, "Error: Only Version 0 handled in DREF box\n");
1814 if (0 != cio_read(3)) { /* Flags = 0 */
1815 fprintf(stderr, "Error with flag in DREF box. Expected flag 0\n");
1819 entry_count = cio_read(4);
1823 for (i = 0; i < entry_count; i++) {
1825 marker = cio_read(4);
1826 if (marker == MJ2_URL) {
1829 if (mj2_read_url(tk, tk->num_url))
1831 } else if (marker == MJ2_URN) {
1834 if (mj2_read_urn(tk, tk->num_urn))
1837 fprintf(stderr, "Error with in DREF box. Expected URN or URL box\n");
1844 if (cio_tell() - box.init_pos != box.length) {
1845 fprintf(stderr, "Error with DREF Box size\n");
1852 * Write the DINF box
1854 * Data information box
1857 void mj2_write_dinf(mj2_tk_t * tk)
1861 box.init_pos = cio_tell();
1863 cio_write(MJ2_DINF, 4); /* DINF */
1867 box.length = cio_tell() - box.init_pos;
1868 cio_seek(box.init_pos);
1869 cio_write(box.length, 4); /* L */
1870 cio_seek(box.init_pos + box.length);
1876 * Data information box
1879 int mj2_read_dinf(mj2_tk_t * tk)
1883 mj2_read_boxhdr(&box);
1884 if (MJ2_DINF != box.type) {
1885 fprintf(stderr, "Error: Expected DINF Marker\n");
1889 if (mj2_read_dref(tk))
1892 if (cio_tell() - box.init_pos != box.length) {
1893 fprintf(stderr, "Error with DINF Box size\n");
1900 * Write the VMHD box
1902 * Video Media information box
1905 void mj2_write_vmhd(mj2_tk_t * tk)
1909 box.init_pos = cio_tell();
1911 cio_write(MJ2_VMHD, 4); /* VMHD */
1913 cio_write(1, 4); /* Version = 0, flags = 1 */
1915 cio_write(tk->graphicsmode, 2);
1916 cio_write(tk->opcolor[0], 2);
1917 cio_write(tk->opcolor[1], 2);
1918 cio_write(tk->opcolor[2], 2);
1920 box.length = cio_tell() - box.init_pos;
1921 cio_seek(box.init_pos);
1922 cio_write(box.length, 4); /* L */
1923 cio_seek(box.init_pos + box.length);
1929 * Video Media information box
1932 int mj2_read_vmhd(mj2_tk_t * tk)
1936 mj2_read_boxhdr(&box);
1937 if (MJ2_VMHD != box.type) {
1938 fprintf(stderr, "Error: Expected VMHD Marker\n");
1942 if (0 != cio_read(1)) { /* Version = 0 */
1943 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
1947 if (1 != cio_read(3)) { /* Flags = 1 */
1948 fprintf(stderr, "Error with flag in VMHD box. Expected flag 1\n");
1953 tk->graphicsmode = cio_read(2);
1954 tk->opcolor[0] = cio_read(2);
1955 tk->opcolor[1] = cio_read(2);
1956 tk->opcolor[2] = cio_read(2);
1958 if (cio_tell() - box.init_pos != box.length) {
1959 fprintf(stderr, "Error with VMHD Box size\n");
1966 * Write the SMHD box
1968 * Sound Media information box
1971 void mj2_write_smhd(mj2_tk_t * tk)
1975 box.init_pos = cio_tell();
1977 cio_write(MJ2_SMHD, 4); /* SMHD */
1979 cio_write(0, 4); /* Version = 0, flags = 0 */
1981 cio_write(tk->balance << 8, 2);
1983 cio_write(0, 2); /* Reserved */
1985 box.length = cio_tell() - box.init_pos;
1986 cio_seek(box.init_pos);
1987 cio_write(box.length, 4); /* L */
1988 cio_seek(box.init_pos + box.length);
1994 * Sound Media information box
1997 int mj2_read_smhd(mj2_tk_t * tk)
2001 mj2_read_boxhdr(&box);
2002 if (MJ2_SMHD != box.type) {
2003 fprintf(stderr, "Error: Expected SMHD Marker\n");
2007 if (0 != cio_read(1)) { /* Version = 0 */
2008 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
2012 if (0 != cio_read(3)) { /* Flags = 0 */
2013 fprintf(stderr, "Error with flag in VMHD box. Expected flag 0\n");
2018 tk->balance = cio_read(2) >> 8;
2020 cio_skip(2); /* Reserved */
2022 if (cio_tell() - box.init_pos != box.length) {
2023 fprintf(stderr, "Error with SMHD Box size\n");
2030 * Write the HMHD box
2032 * Hint Media information box
2035 void mj2_write_hmhd(mj2_tk_t * tk)
2039 box.init_pos = cio_tell();
2041 cio_write(MJ2_HMHD, 4); /* HMHD */
2043 cio_write(0, 4); /* Version = 0, flags = 0 */
2045 cio_write(tk->maxPDUsize, 2);
2046 cio_write(tk->avgPDUsize, 2);
2047 cio_write(tk->maxbitrate, 4);
2048 cio_write(tk->avgbitrate, 4);
2049 cio_write(tk->slidingavgbitrate, 4);
2051 box.length = cio_tell() - box.init_pos;
2052 cio_seek(box.init_pos);
2053 cio_write(box.length, 4); /* L */
2054 cio_seek(box.init_pos + box.length);
2060 * Hint Media information box
2063 int mj2_read_hmhd(mj2_tk_t * tk)
2067 mj2_read_boxhdr(&box);
2068 if (MJ2_HMHD != box.type) {
2069 fprintf(stderr, "Error: Expected HMHD Marker\n");
2073 if (0 != cio_read(1)) { /* Version = 0 */
2074 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
2078 if (0 != cio_read(3)) { /* Flags = 0 */
2079 fprintf(stderr, "Error with flag in VMHD box. Expected flag 0\n");
2084 tk->maxPDUsize = cio_read(2);
2085 tk->avgPDUsize = cio_read(2);
2086 tk->maxbitrate = cio_read(4);
2087 tk->avgbitrate = cio_read(4);
2088 tk->slidingavgbitrate = cio_read(4);
2090 if (cio_tell() - box.init_pos != box.length) {
2091 fprintf(stderr, "Error with HMHD Box size\n");
2098 * Write the MINF box
2100 * Media information box
2103 void mj2_write_minf(mj2_tk_t * tk, j2k_image_t * img)
2107 box.init_pos = cio_tell();
2109 cio_write(MJ2_MINF, 4); /* MINF */
2111 if (tk->track_type == 0) {
2113 } else if (tk->track_type == 1) {
2115 } else if (tk->track_type == 2) {
2120 mj2_write_stbl(tk, img);
2122 box.length = cio_tell() - box.init_pos;
2123 cio_seek(box.init_pos);
2124 cio_write(box.length, 4); /* L */
2125 cio_seek(box.init_pos + box.length);
2131 * Media information box
2134 int mj2_read_minf(mj2_tk_t * tk, j2k_image_t * img)
2137 unsigned int box_type;
2140 mj2_read_boxhdr(&box);
2141 if (MJ2_MINF != box.type) {
2142 fprintf(stderr, "Error: Expected MINF Marker\n");
2147 box_type = cio_read(4);
2150 if (box_type == MJ2_VMHD) {
2151 if (mj2_read_vmhd(tk))
2153 } else if (box_type == MJ2_SMHD) {
2154 if (mj2_read_smhd(tk))
2156 } else if (box_type == MJ2_HMHD) {
2157 if (mj2_read_hmhd(tk))
2160 fprintf(stderr, "Error in MINF box expected vmhd, smhd or hmhd\n");
2164 if (mj2_read_dinf(tk))
2167 if (mj2_read_stbl(tk, img))
2170 if (cio_tell() - box.init_pos != box.length) {
2171 fprintf(stderr, "Error with MINF Box size\n");
2178 * Write the HDLR box
2180 * Handler reference box
2183 void mj2_write_hdlr(mj2_tk_t * tk)
2187 box.init_pos = cio_tell();
2189 cio_write(MJ2_HDLR, 4); /* HDLR */
2191 cio_write(0, 4); /* Version = 0, flags = 0 */
2193 cio_write(0, 4); /* Predefine */
2195 tk->name = 0; /* The track name is immediately determined by the track type */
2197 if (tk->track_type == 0) {
2198 tk->handler_type = 0x76696465; /* Handler type: vide */
2199 cio_write(tk->handler_type, 4);
2203 cio_write(0, 4); /* Reserved */
2205 cio_write(0x76696465, 4);
2206 cio_write(0x6F206d65, 4);
2207 cio_write(0x64696120, 4);
2208 cio_write(0x74726163, 4);
2209 cio_write(0x6b00, 2); /* String: video media track */
2210 } else if (tk->track_type == 1) {
2211 tk->handler_type = 0x736F756E; /* Handler type: soun */
2212 cio_write(tk->handler_type, 4);
2216 cio_write(0, 4); /* Reserved */
2218 cio_write(0x536F756E, 4);
2219 cio_write(0x6400, 2); /* String: Sound */
2220 } else if (tk->track_type == 2) {
2221 tk->handler_type = 0x68696E74; /* Handler type: hint */
2222 cio_write(tk->handler_type, 4);
2226 cio_write(0, 4); /* Reserved */
2228 cio_write(0x48696E74, 4);
2229 cio_write(0, 2); /* String: Hint */
2232 box.length = cio_tell() - box.init_pos;
2233 cio_seek(box.init_pos);
2234 cio_write(box.length, 4); /* L */
2235 cio_seek(box.init_pos + box.length);
2241 * Handler reference box
2244 int mj2_read_hdlr(mj2_tk_t * tk)
2249 mj2_read_boxhdr(&box);
2250 if (MJ2_HDLR != box.type) {
2251 fprintf(stderr, "Error: Expected HDLR Marker\n");
2256 if (0 != cio_read(1)) { /* Version = 0 */
2257 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
2261 if (0 != cio_read(3)) { /* Flags = 0 */
2262 fprintf(stderr, "Error with flag in VMHD box. Expected flag 0\n");
2266 cio_skip(4); /* Reserved */
2268 tk->handler_type = cio_read(4);
2269 cio_skip(12); /* Reserved */
2271 tk->name_size = box.length - 32;
2273 tk->name = (char *) malloc(tk->name_size * sizeof(char));
2274 for (i = 0; i < tk->name_size; i++) {
2275 tk->name[i] = cio_read(1); /* Name */
2278 if (cio_tell() - box.init_pos != box.length) {
2279 fprintf(stderr, "Error with HDLR Box size\n");
2286 * Write the MDHD box
2291 void mj2_write_mdhd(mj2_tk_t * tk)
2296 unsigned int modification_time;
2298 box.init_pos = cio_tell();
2300 cio_write(MJ2_MDHD, 4); /* MDHD */
2302 cio_write(0, 4); /* Version = 0, flags = 0 */
2304 cio_write(tk->creation_time, 4); /* Creation Time */
2306 time(<ime); /* Time since 1/1/70 */
2307 modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2309 cio_write(modification_time, 4); /* Modification Time */
2311 cio_write(tk->timescale, 4); /* Timescale */
2315 for (i = 0; i < tk->num_samples; i++)
2316 tk->duration += tk->sample[i].sample_delta;
2318 cio_write(tk->duration, 4); /* Duration */
2320 cio_write(tk->language, 2); /* Language */
2322 cio_write(0, 2); /* Predefined */
2324 box.length = cio_tell() - box.init_pos;
2325 cio_seek(box.init_pos);
2326 cio_write(box.length, 4); /* L */
2327 cio_seek(box.init_pos + box.length);
2336 int mj2_read_mdhd(mj2_tk_t * tk)
2340 mj2_read_boxhdr(&box);
2341 if (!(MJ2_MHDR == box.type || MJ2_MDHD == box.type)) { // Kakadu writes MHDR instead of MDHD
2342 fprintf(stderr, "Error: Expected MDHD Marker\n");
2346 if (0 != cio_read(1)) { /* Version = 0 */
2347 fprintf(stderr, "Error: Only Version 0 handled in MDHD box\n");
2351 if (0 != cio_read(3)) { /* Flags = 0 */
2352 fprintf(stderr, "Error with flag in MDHD box. Expected flag 0\n");
2357 tk->creation_time = cio_read(4); /* Creation Time */
2359 tk->modification_time = cio_read(4); /* Modification Time */
2361 tk->timescale = cio_read(4); /* Timescale */
2363 tk->duration = cio_read(4); /* Duration */
2365 tk->language = cio_read(2); /* Language */
2367 cio_skip(2); /* Predefined */
2369 if (cio_tell() - box.init_pos != box.length) {
2370 fprintf(stderr, "Error with MDHD Box size\n");
2377 * Write the MDIA box
2382 void mj2_write_mdia(mj2_tk_t * tk, j2k_image_t * img)
2386 box.init_pos = cio_tell();
2388 cio_write(MJ2_MDIA, 4); /* MDIA */
2392 mj2_write_minf(tk, img);
2394 box.length = cio_tell() - box.init_pos;
2395 cio_seek(box.init_pos);
2396 cio_write(box.length, 4); /* L */
2397 cio_seek(box.init_pos + box.length);
2406 int mj2_read_mdia(mj2_tk_t * tk, j2k_image_t * img)
2410 mj2_read_boxhdr(&box);
2411 if (MJ2_MDIA != box.type) {
2412 fprintf(stderr, "Error: Expected MDIA Marker\n");
2416 if (mj2_read_mdhd(tk))
2418 if (mj2_read_hdlr(tk))
2420 if (mj2_read_minf(tk, img))
2423 if (cio_tell() - box.init_pos != box.length) {
2424 fprintf(stderr, "Error with MDIA Box size\n");
2431 * Write the TKHD box
2436 void mj2_write_tkhd(mj2_tk_t * tk)
2442 box.init_pos = cio_tell();
2445 cio_write(MJ2_TKHD, 4); /* TKHD */
2447 cio_write(3, 4); /* Version=0, flags=3 */
2449 time(<ime); /* Time since 1/1/70 */
2450 tk->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2452 cio_write(tk->creation_time, 4); /* Creation Time */
2454 cio_write(tk->modification_time, 4); /* Modification Time */
2456 cio_write(tk->track_ID, 4); /* Track ID */
2458 cio_write(0, 4); /* Reserved */
2462 for (i = 0; i < tk->num_samples; i++)
2463 tk->duration += tk->sample[i].sample_delta;
2465 cio_write(tk->duration, 4); /* Duration */
2467 cio_write(0, 4); /* Reserved */
2468 cio_write(0, 4); /* Reserved */
2470 cio_write(tk->layer, 2); /* Layer */
2472 cio_write(0, 2); /* Predefined */
2474 cio_write(tk->volume << 8, 2); /* Volume */
2476 cio_write(0, 2); /* Reserved */
2478 cio_write(tk->trans_matrix[0], 4); /* Transformation matrix for track */
2479 cio_write(tk->trans_matrix[1], 4);
2480 cio_write(tk->trans_matrix[2], 4);
2481 cio_write(tk->trans_matrix[3], 4);
2482 cio_write(tk->trans_matrix[4], 4);
2483 cio_write(tk->trans_matrix[5], 4);
2484 cio_write(tk->trans_matrix[6], 4);
2485 cio_write(tk->trans_matrix[7], 4);
2486 cio_write(tk->trans_matrix[8], 4);
2488 cio_write(tk->w << 16, 4); /* Video Width */
2490 cio_write(tk->h << 16, 4); /* Video Height */
2492 box.length = cio_tell() - box.init_pos;
2493 cio_seek(box.init_pos);
2494 cio_write(box.length, 4); /* L */
2495 cio_seek(box.init_pos + box.length);
2504 int mj2_read_tkhd(mj2_tk_t * tk)
2510 mj2_read_boxhdr(&box);
2512 if (MJ2_TKHD != box.type) {
2513 fprintf(stderr, "Error: Expected TKHD Marker\n");
2517 if (0 != cio_read(1)) { /* Version = 0 */
2518 fprintf(stderr, "Error: Only Version 0 handled in TKHD box\n");
2524 if (!(flag == 1 || flag == 2 || flag == 3 || flag == 4)) { /* Flags = 1,2,3 or 4 */
2526 "Error with flag in TKHD box: Expected flag 1,2,3 or 4\n");
2530 tk->creation_time = cio_read(4); /* Creation Time */
2532 tk->modification_time = cio_read(4); /* Modification Time */
2534 tk->track_ID = cio_read(4); /* Track ID */
2536 cio_skip(4); /* Reserved */
2538 tk->duration = cio_read(4); /* Duration */
2540 cio_skip(8); /* Reserved */
2542 tk->layer = cio_read(2); /* Layer */
2544 cio_read(2); /* Predefined */
2546 tk->volume = cio_read(2) >> 8; /* Volume */
2548 cio_skip(2); /* Reserved */
2550 tk->trans_matrix[0] = cio_read(4); /* Transformation matrix for track */
2551 tk->trans_matrix[1] = cio_read(4);
2552 tk->trans_matrix[2] = cio_read(4);
2553 tk->trans_matrix[3] = cio_read(4);
2554 tk->trans_matrix[4] = cio_read(4);
2555 tk->trans_matrix[5] = cio_read(4);
2556 tk->trans_matrix[6] = cio_read(4);
2557 tk->trans_matrix[7] = cio_read(4);
2558 tk->trans_matrix[8] = cio_read(4);
2560 tk->w = cio_read(4) >> 16; /* Video Width */
2562 tk->h = cio_read(4) >> 16; /* Video Height */
2564 if (cio_tell() - box.init_pos != box.length) {
2565 fprintf(stderr, "Error with TKHD Box size\n");
2572 * Write the TRAK box
2577 void mj2_write_trak(mj2_tk_t * tk, j2k_image_t * img)
2581 box.init_pos = cio_tell();
2584 cio_write(MJ2_TRAK, 4); /* TRAK */
2587 mj2_write_mdia(tk, img);
2589 box.length = cio_tell() - box.init_pos;
2590 cio_seek(box.init_pos);
2591 cio_write(box.length, 4); /* L */
2592 cio_seek(box.init_pos + box.length);
2601 int mj2_read_trak(mj2_tk_t * tk, j2k_image_t * img)
2605 mj2_read_boxhdr(&box);
2606 if (MJ2_TRAK != box.type) {
2607 fprintf(stderr, "Error: Expected TRAK Marker\n");
2610 if (mj2_read_tkhd(tk))
2612 if (mj2_read_mdia(tk, img))
2614 if (cio_tell() - box.init_pos != box.length) {
2615 fprintf(stderr, "Error with TRAK Box\n");
2622 * Write the MVHD box
2627 void mj2_write_mvhd(mj2_movie_t * movie)
2635 box.init_pos = cio_tell();
2637 cio_write(MJ2_MVHD, 4); /* MVHD */
2639 cio_write(0, 4); /* Version = 0, flags = 0 */
2641 time(<ime); /* Time since 1/1/70 */
2642 movie->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2644 cio_write(movie->creation_time, 4); /* Creation Time */
2646 cio_write(movie->modification_time, 4); /* Modification Time */
2648 cio_write(movie->timescale, 4); /* Timescale */
2650 movie->duration = 0;
2652 for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
2653 mj2_tk_t *tk = &movie->tk[i];
2655 for (j = 0; j < tk->num_samples; j++) {
2656 movie->duration += tk->sample[j].sample_delta;
2660 cio_write(movie->duration, 4);
2662 cio_write(movie->rate << 16, 4); /* Rate to play presentation */
2664 cio_write(movie->volume << 8, 2); /* Volume */
2666 cio_write(0, 2); /* Reserved */
2667 cio_write(0, 4); /* Reserved */
2668 cio_write(0, 4); /* Reserved */
2670 cio_write(movie->trans_matrix[0], 4); /* Transformation matrix for video */
2671 cio_write(movie->trans_matrix[1], 4);
2672 cio_write(movie->trans_matrix[2], 4);
2673 cio_write(movie->trans_matrix[3], 4);
2674 cio_write(movie->trans_matrix[4], 4);
2675 cio_write(movie->trans_matrix[5], 4);
2676 cio_write(movie->trans_matrix[6], 4);
2677 cio_write(movie->trans_matrix[7], 4);
2678 cio_write(movie->trans_matrix[8], 4);
2680 cio_write(0, 4); /* Pre-defined */
2681 cio_write(0, 4); /* Pre-defined */
2682 cio_write(0, 4); /* Pre-defined */
2683 cio_write(0, 4); /* Pre-defined */
2684 cio_write(0, 4); /* Pre-defined */
2685 cio_write(0, 4); /* Pre-defined */
2688 for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) {
2689 if (max_tk_num < movie->tk[i].track_ID)
2690 max_tk_num = movie->tk[i].track_ID;
2693 movie->next_tk_id = max_tk_num + 1;
2695 cio_write(movie->next_tk_id, 4); /* ID of Next track to be added */
2697 box.length = cio_tell() - box.init_pos;
2698 cio_seek(box.init_pos);
2699 cio_write(box.length, 4); /* L */
2700 cio_seek(box.init_pos + box.length);
2709 int mj2_read_mvhd(mj2_movie_t * movie)
2713 mj2_read_boxhdr(&box);
2714 if (MJ2_MVHD != box.type) {
2715 fprintf(stderr, "Error: Expected MVHD Marker\n");
2720 if (0 != cio_read(4)) { /* Version = 0, flags = 0 */
2721 fprintf(stderr, "Error: Only Version 0 handled\n");
2724 movie->creation_time = cio_read(4); /* Creation Time */
2726 movie->modification_time = cio_read(4); /* Modification Time */
2728 movie->timescale = cio_read(4); /* Timescale */
2730 movie->duration = cio_read(4); /* Duration */
2732 movie->rate = cio_read(4) >> 16; /* Rate to play presentation */
2734 movie->volume = cio_read(2) >> 8; /* Volume */
2736 cio_skip(10); /* Reserved */
2738 movie->trans_matrix[0] = cio_read(4); /* Transformation matrix for video */
2739 movie->trans_matrix[1] = cio_read(4);
2740 movie->trans_matrix[2] = cio_read(4);
2741 movie->trans_matrix[3] = cio_read(4);
2742 movie->trans_matrix[4] = cio_read(4);
2743 movie->trans_matrix[5] = cio_read(4);
2744 movie->trans_matrix[6] = cio_read(4);
2745 movie->trans_matrix[7] = cio_read(4);
2746 movie->trans_matrix[8] = cio_read(4);
2748 cio_skip(24); /* Pre-defined */
2750 movie->next_tk_id = cio_read(4); /* ID of Next track to be added */
2752 if (cio_tell() - box.init_pos != box.length) {
2753 fprintf(stderr, "Error with MVHD Box Size\n");
2761 * Write the MOOV box
2766 void mj2_write_moov(mj2_movie_t * movie, j2k_image_t * img)
2771 box.init_pos = cio_tell();
2773 cio_write(MJ2_MOOV, 4); /* MOOV */
2775 mj2_write_mvhd(movie);
2777 for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
2778 mj2_write_trak(&movie->tk[i], img);
2781 box.length = cio_tell() - box.init_pos;
2782 cio_seek(box.init_pos);
2783 cio_write(box.length, 4); /* L */
2784 cio_seek(box.init_pos + box.length);
2794 int mj2_read_moov(mj2_movie_t * movie, j2k_image_t * img)
2800 mj2_read_boxhdr(&box);
2801 if (MJ2_MOOV != box.type) {
2802 fprintf(stderr, "Error: Expected MOOV Marker\n");
2808 if (mj2_read_mvhd(movie))
2813 (mj2_tk_t *) malloc((movie->next_tk_id - 1) * sizeof(mj2_tk_t));
2815 for (i=0; i < (unsigned int)movie->next_tk_id - 1; i++)
2816 movie->tk[i].imagefile = NULL;
2818 for (i = 0; cio_tell() - box.init_pos < box.length; i++) {
2819 mj2_read_boxhdr(&box2);
2820 if (box2.type == MJ2_TRAK) {
2821 cio_seek(box2.init_pos);
2822 if (mj2_read_trak(&movie->tk[i], img))
2825 if (movie->tk[i].track_type == 0) {
2827 } else if (movie->tk[i].track_type == 1) {
2829 } else if (movie->tk[i].track_type == 2) {
2832 } else if (box2.type == MJ2_MVEX) {
2833 cio_seek(box2.init_pos);
2834 cio_skip(box2.length);
2837 fprintf(stderr, "Error with MOOV Box: Expected TRAK or MVEX box\n");
2845 int mj2_encode(mj2_movie_t * movie, j2k_cp_t * cp, char *index)
2855 outbuf = (char *) malloc(cp->tdx * cp->tdy * 2);
2856 cio_init(outbuf, cp->tdx * cp->tdy * 2);
2858 outfile = fopen(movie->mj2file, "wb");
2860 fprintf(stderr, "failed to open %s for writing\n", movie->mj2file);
2865 mj2_write_ftyp(movie);
2867 if (mj2_write_mdat(outfile, movie, &img, cp, outbuf, index)) {
2868 fprintf(stderr, "Error writing tracks\n\n");
2872 outbuf = (char *) malloc(cp->tdx * cp->tdy * 2);
2873 cio_init(outbuf, cp->tdx * cp->tdy * 2);
2875 mj2_write_moov(movie, &img);
2878 fwrite(outbuf, 1, len, outfile);
2882 for (i = 0; i < movie->tk[0].jp2_struct.numcomps; i++) {
2884 sprintf(&tmp, "Compo%d", i);
2885 if (remove(&tmp) == -1) {
2886 fprintf(stderr, "failed to kill %s file !\n", &tmp);
2897 int mj2_decode(unsigned char *src, int len, mj2_movie_t * movie,
2898 j2k_cp_t * cp, char *outfile)
2900 unsigned int MDATbox_pos = 0;
2901 unsigned int MOOVboxend_pos = 0;
2909 if (mj2_read_ftyp(movie))
2913 for (; cio_numbytesleft() > 0;) {
2914 mj2_read_boxhdr(&box);
2917 cio_seek(box.init_pos);
2918 if (MOOVboxend_pos == 0) {
2919 MDATbox_pos = box.init_pos;
2920 cio_skip(box.length); /* The MDAT box is only read while the MOOV box is decoded */
2922 if (mj2_read_mdat(movie, src, outfile))
2928 cio_seek(box.init_pos);
2929 if (mj2_read_moov(movie, &img))
2931 MOOVboxend_pos = cio_tell();
2932 if (MDATbox_pos != 0) {
2933 cio_seek(MDATbox_pos); /* After MOOV box, read the MDAT box */
2935 if (mj2_read_mdat(movie, src, outfile))
2938 cio_seek(MOOVboxend_pos);
2943 cio_seek(box.init_pos);
2944 cio_skip(box.length);
2947 cio_seek(box.init_pos);
2948 cio_skip(box.length);
2951 cio_seek(box.init_pos);
2952 cio_skip(box.length);
2955 fprintf(stderr, "Unknown box\n");