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.
37 #define MJ2_JP 0x6a502020
38 #define MJ2_FTYP 0x66747970
39 #define MJ2_MJ2 0x6d6a7032
40 #define MJ2_MJ2S 0x6d6a3273
41 #define MJ2_MDAT 0x6d646174
42 #define MJ2_MOOV 0x6d6f6f76
43 #define MJ2_MVHD 0x6d766864
44 #define MJ2_TRAK 0x7472616b
45 #define MJ2_TKHD 0x746b6864
46 #define MJ2_MDIA 0x6d646961
47 #define MJ2_MDHD 0x6d646864
48 #define MJ2_MHDR 0x6d686472
49 #define MJ2_HDLR 0x68646C72
50 #define MJ2_MINF 0x6d696e66
51 #define MJ2_VMHD 0x766d6864
52 #define MJ2_SMHD 0x736d6864
53 #define MJ2_HMHD 0x686d6864
54 #define MJ2_DINF 0x64696e66
55 #define MJ2_DREF 0x64726566
56 #define MJ2_URL 0x75726c20
57 #define MJ2_URN 0x75726e20
58 #define MJ2_STBL 0x7374626c
59 #define MJ2_STSD 0x73747364
60 #define MJ2_STTS 0x73747473
61 #define MJ2_STSC 0x73747363
62 #define MJ2_STSZ 0x7374737A
63 #define MJ2_STCO 0x7374636f
64 #define MJ2_MOOF 0x6d6f6f66
65 #define MJ2_FREE 0x66726565
66 #define MJ2_SKIP 0x736b6970
67 #define MJ2_JP2C 0x6a703263
68 #define MJ2_FIEL 0x6669656c
69 #define MJ2_JP2P 0x6a703270
70 #define MJ2_JP2X 0x6a703278
71 #define MJ2_JSUB 0x6a737562
72 #define MJ2_ORFO 0x6f72666f
73 #define MJ2_MVEX 0x6d766578
74 #define MJ2_JP2 0x6a703220
75 #define MJ2_J2P0 0x4a325030
79 * Free movie structure memory
82 void mj2_memory_free(mj2_movie_t * movie)
87 if (movie->num_cl != 0)
90 for (i = 0; i < movie->num_vtk + movie->num_stk + movie->num_htk; i++) {
92 if (tk->name_size != 0)
94 if (tk->jp2_struct.comps != 0)
95 free(tk->jp2_struct.comps);
96 if (tk->jp2_struct.cl != 0)
97 free(tk->jp2_struct.cl);
100 if (tk->num_urn != 0)
104 if (tk->num_jp2x != 0)
106 if (tk->num_tts != 0)
108 if (tk->num_chunks != 0)
110 if (tk->num_samplestochunk != 0)
111 free(tk->sampletochunk);
112 if (tk->num_samples != 0)
125 int mj2_read_boxhdr(mj2_box_t * box)
127 box->init_pos = cio_tell();
128 box->length = cio_read(4);
129 box->type = cio_read(4);
130 if (box->length == 1) {
131 if (cio_read(4) != 0) {
132 fprintf(stderr, "Error: Cannot handle box sizes higher than 2^32\n");
135 box->length = cio_read(4);
136 if (box->length == 0)
137 box->length = cio_numbytesleft() + 12;
139 else if (box->length == 0) {
140 box->length = cio_numbytesleft() + 8;
147 * Initialisation of a Standard Video Track
148 * with one sample per chunk
151 int mj2_init_stdmovie(mj2_movie_t * movie)
156 movie->brand = MJ2_MJ2;
157 movie->minversion = 0;
160 (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
162 movie->cl[0] = MJ2_MJ2;
163 movie->cl[1] = MJ2_MJ2S;
164 time(<ime); /* Time since 1/1/70 */
165 movie->creation_time = (unsigned int) ltime + 2082844800; /* Seconds between 1/1/04 and 1/1/70 */
166 movie->timescale = 1000;
168 movie->rate = 1 << 16; /* Rate to play presentation (default = 0x00010000) */
169 movie->volume = 1 << 8; /* Movie volume (default = 0x0100) */
170 movie->trans_matrix[0] = 0x00010000; /* Transformation matrix for video */
171 movie->trans_matrix[1] = 0; /* Unity is { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 } */
172 movie->trans_matrix[2] = 0;
173 movie->trans_matrix[3] = 0;
174 movie->trans_matrix[4] = 0x00010000;
175 movie->trans_matrix[5] = 0;
176 movie->trans_matrix[6] = 0;
177 movie->trans_matrix[7] = 0;
178 movie->trans_matrix[8] = 0x40000000;
179 movie->next_tk_id = 1;
181 for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) {
182 mj2_tk_t *tk = &movie->tk[i];
184 tk->jp2_struct.comps = NULL;
185 tk->jp2_struct.cl = NULL;
187 if (tk->track_type == 0) {
188 if (tk->num_samples == 0)
194 tk->timescale = 1000; /* Timescale = 1 ms */
196 tk->chunk[0].num_samples = 1;
197 tk->chunk[0].sample_descr_idx = 1;
199 tk->same_sample_size = 0;
201 tk->num_samplestochunk = 1; /* One sample per chunk */
203 (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
204 sizeof(mj2_sampletochunk_t));
205 tk->sampletochunk[0].first_chunk = 1;
206 tk->sampletochunk[0].samples_per_chunk = 1;
207 tk->sampletochunk[0].sample_descr_idx = 1;
209 for (j = 0; j < tk->num_samples; j++)
210 tk->sample[j].sample_delta = tk->timescale / tk->sample_rate;
213 tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
214 tk->tts[0].sample_count = tk->num_samples;
215 tk->tts[0].sample_delta = tk->timescale / tk->sample_rate;
217 tk->horizresolution = 0x00480000; /* Horizontal resolution (typically 72) */
218 tk->vertresolution = 0x00480000; /* Vertical resolution (typically 72) */
219 tk->compressorname[0] = 0x0f4d6f74; /* Compressor Name[]: Motion JPEG2000 */
220 tk->compressorname[1] = 0x696f6e20;
221 tk->compressorname[2] = 0x4a504547;
222 tk->compressorname[3] = 0x32303030;
223 tk->compressorname[4] = 0x00120000;
224 tk->compressorname[5] = 0;
225 tk->compressorname[6] = 0x00000042;
226 tk->compressorname[7] = 0x000000DC;
227 tk->num_url = 0; /* Number of URL */
228 tk->num_urn = 0; /* Number of URN */
229 tk->graphicsmode = 0; /* Graphicsmode */
230 tk->opcolor[0] = 0; /* OpColor */
231 tk->opcolor[1] = 0; /* OpColor */
232 tk->opcolor[2] = 0; /* OpColor */
233 tk->creation_time = movie->creation_time; /* Seconds between 1/1/04 and 1/1/70 */
234 tk->language = 0; /* Language (undefined) */
236 tk->volume = 1 << 8; /* Movie volume (default = 0x0100) */
237 tk->trans_matrix[0] = 0x00010000; /* Transformation matrix for track */
238 tk->trans_matrix[1] = 0; /* Unity is { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 } */
239 tk->trans_matrix[2] = 0;
240 tk->trans_matrix[3] = 0;
241 tk->trans_matrix[4] = 0x00010000;
242 tk->trans_matrix[5] = 0;
243 tk->trans_matrix[6] = 0;
244 tk->trans_matrix[7] = 0;
245 tk->trans_matrix[8] = 0x40000000;
248 tk->or_fieldcount = 1;
249 tk->or_fieldorder = 0;
251 tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
253 tk->br[1] = MJ2_J2P0;
255 tk->hsub = 2; /* 4:2:0 */
256 tk->vsub = 2; /* 4:2:0 */
259 tk->visual_w = tk->w << 16;
260 tk->visual_h = tk->h << 16;
271 * Time To Sample box Decompact
274 void mj2_tts_decompact(mj2_tk_t * tk)
278 for (i = 0; i < tk->num_tts; i++) {
279 tk->num_samples += tk->tts[i].sample_count;
283 (mj2_sample_t *) malloc(tk->num_samples * sizeof(mj2_sample_t));
285 for (i = 0; i < tk->num_tts; i++) {
286 for (j = 0; j < tk->tts[i].sample_count; j++) {
287 tk->sample[j].sample_delta = tk->tts[i].sample_delta;
293 * Sample To Chunk box Decompact
296 void mj2_stsc_decompact(mj2_tk_t * tk)
302 if (tk->num_samplestochunk == 1) {
304 (unsigned int) ceil((double) tk->num_samples /
305 (double) tk->sampletochunk[0].samples_per_chunk);
307 (mj2_chunk_t *) malloc(tk->num_chunks * sizeof(mj2_chunk_t));
308 for (k = 0; k < tk->num_chunks; k++) {
309 tk->chunk[k].num_samples = tk->sampletochunk[0].samples_per_chunk;
314 (mj2_chunk_t *) malloc(tk->num_samples * sizeof(mj2_chunk_t));
316 for (i = 0; i < tk->num_samplestochunk -1 ; i++) {
317 for (j = tk->sampletochunk[i].first_chunk - 1;
318 j < tk->sampletochunk[i + 1].first_chunk - 1; j++) {
319 tk->chunk[j].num_samples = tk->sampletochunk[i].samples_per_chunk;
321 sampleno += tk->chunk[j].num_samples;
324 tk->num_chunks += (int)(tk->num_samples - sampleno) / tk->sampletochunk[tk->num_samplestochunk - 1].samples_per_chunk;
325 for (k = tk->sampletochunk[tk->num_samplestochunk - 1].first_chunk - 1;
326 k < tk->num_chunks; k++) {
327 tk->chunk[k].num_samples =
328 tk->sampletochunk[tk->num_samplestochunk - 1].samples_per_chunk;
330 tk->chunk = realloc(tk->chunk, tk->num_chunks * sizeof(mj2_chunk_t));
337 * Chunk offset box Decompact
340 void mj2_stco_decompact(mj2_tk_t * tk)
345 int intra_chunk_offset;
347 for (i = 0; i < tk->num_chunks; i++) {
348 intra_chunk_offset = 0;
349 for (j = 0; j < tk->chunk[i].num_samples; j++) {
350 tk->sample[k].offset = intra_chunk_offset + tk->chunk[i].offset;
351 intra_chunk_offset += tk->sample[k].sample_size;
366 box.init_pos = cio_tell();
369 cio_write(MJ2_JP, 4); /* JP */
370 cio_write(0x0d0a870a, 4); /* 0x0d0a870a required in a JP box */
372 box.length = cio_tell() - box.init_pos;
373 cio_seek(box.init_pos);
374 cio_write(box.length, 4);
375 cio_seek(box.init_pos + box.length);
381 * JPEG 2000 signature
388 mj2_read_boxhdr(&box);
389 if (MJ2_JP != box.type) { /* Check Marker */
390 fprintf(stderr, "Error: Expected JP Marker\n");
393 if (0x0d0a870a != cio_read(4)) { /* read the 0x0d0a870a required in a JP box */
394 fprintf(stderr, "Error with JP Marker\n");
397 if (cio_tell() - box.init_pos != box.length) { /* Check box length */
398 fprintf(stderr, "Error with JP Box size \n");
411 void mj2_write_ftyp(mj2_movie_t * movie)
415 box.init_pos = cio_tell();
418 cio_write(MJ2_FTYP, 4); /* FTYP */
419 cio_write(movie->brand, 4); /* BR */
420 cio_write(movie->minversion, 4); /* MinV */
422 for (i = 0; i < movie->num_cl; i++)
423 cio_write(movie->cl[i], 4); /* CL */
425 box.length = cio_tell() - box.init_pos;
426 cio_seek(box.init_pos);
427 cio_write(box.length, 4); /* Length */
428 cio_seek(box.init_pos + box.length);
437 int mj2_read_ftyp(mj2_movie_t * movie)
442 mj2_read_boxhdr(&box); /* Box Size */
443 if (MJ2_FTYP != box.type) {
444 fprintf(stderr, "Error: Expected FTYP Marker\n");
448 movie->brand = cio_read(4); /* BR */
449 movie->minversion = cio_read(4); /* MinV */
450 movie->num_cl = (box.length - 16) / 4;
452 (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
454 for (i = movie->num_cl - 1; i > -1; i--)
455 movie->cl[i] = cio_read(4); /* CLi */
457 if (cio_tell() - box.init_pos != box.length) {
458 fprintf(stderr, "Error with FTYP Box\n");
471 void mj2_write_stco(mj2_tk_t * tk)
476 box.init_pos = cio_tell();
478 cio_write(MJ2_STCO, 4); /* STCO */
480 cio_write(0, 4); /* Version = 0, flags = 0 */
482 cio_write(tk->num_chunks, 4); /* Entry Count */
484 for (i = 0; i < tk->num_chunks; i++) {
485 cio_write(tk->chunk[i].offset, 4); /* Entry offset */
488 box.length = cio_tell() - box.init_pos;
489 cio_seek(box.init_pos);
490 cio_write(box.length, 4); /* L */
491 cio_seek(box.init_pos + box.length);
500 int mj2_read_stco(mj2_tk_t * tk)
505 mj2_read_boxhdr(&box); /* Box Size */
506 if (MJ2_STCO != box.type) {
507 fprintf(stderr, "Error: Expected STCO Marker\n");
511 if (0 != cio_read(1)) { /* Version = 0 */
512 fprintf(stderr, "Error: Only Version 0 handled in STCO box\n");
516 if (0 != cio_read(3)) { /* Flags = 0 */
517 fprintf(stderr, "Error with flag in STCO box. Expected flag 0\n");
522 if (cio_read(4) != tk->num_chunks) {
524 "Error in STCO box: expecting same amount of entry-count as chunks \n");
526 for (i = 0; i < tk->num_chunks; i++) {
527 tk->chunk[i].offset = cio_read(4); /* Entry offset */
531 mj2_stco_decompact(tk);
534 if (cio_tell() - box.init_pos != box.length) {
535 fprintf(stderr, "Error with STCO Box size\n");
547 void mj2_write_stsz(mj2_tk_t * tk)
552 box.init_pos = cio_tell();
554 cio_write(MJ2_STSZ, 4); /* STSZ */
556 cio_write(0, 4); /* Version = 0, flags = 0 */
558 if (tk->same_sample_size == 1) { /* If they all have the same size */
559 cio_write(tk->sample[0].sample_size, 4); /* Size */
561 cio_write(1, 4); /* Entry count = 1 */
565 cio_write(0, 4); /* Sample Size = 0 becase they all have different sizes */
567 cio_write(tk->num_samples, 4); /* Sample Count */
569 for (i = 0; i < tk->num_samples; i++) {
570 cio_write(tk->sample[i].sample_size, 4);
574 box.length = cio_tell() - box.init_pos;
575 cio_seek(box.init_pos);
576 cio_write(box.length, 4); /* L */
577 cio_seek(box.init_pos + box.length);
586 int mj2_read_stsz(mj2_tk_t * tk)
592 mj2_read_boxhdr(&box); /* Box Size */
593 if (MJ2_STSZ != box.type) {
594 fprintf(stderr, "Error: Expected STSZ Marker\n");
599 if (0 != cio_read(1)) { /* Version = 0 */
600 fprintf(stderr, "Error: Only Version 0 handled in STSZ box\n");
604 if (0 != cio_read(3)) { /* Flags = 0 */
605 fprintf(stderr, "Error with flag in STSZ box. Expected flag 0\n");
609 sample_size = cio_read(4);
611 if (sample_size != 0) { /* Samples do have the same size */
612 tk->same_sample_size = 1;
613 for (i = 0; i < tk->num_samples; i++) {
614 tk->sample[i].sample_size = sample_size;
616 cio_skip(4); /* Sample count = 1 */
618 tk->same_sample_size = 0;
619 if (tk->num_samples != cio_read(4)) { /* Sample count */
621 "Error in STSZ box. Expected that sample-count is number of samples in track\n");
624 for (i = 0; i < tk->num_samples; i++) {
625 tk->sample[i].sample_size = cio_read(4); /* Sample Size */
628 if (cio_tell() - box.init_pos != box.length) {
629 fprintf(stderr, "Error with STSZ Box size\n");
643 void mj2_write_stsc(mj2_tk_t * tk)
648 box.init_pos = cio_tell();
650 cio_write(MJ2_STSC, 4); /* STSC */
652 cio_write(0, 4); /* Version = 0, flags = 0 */
654 cio_write(tk->num_samplestochunk, 4); /* Entry Count */
656 for (i = 0; i < tk->num_samplestochunk; i++) {
657 cio_write(tk->sampletochunk[i].first_chunk, 4); /* First Chunk */
658 cio_write(tk->sampletochunk[i].samples_per_chunk, 4); /* Samples per chunk */
659 cio_write(tk->sampletochunk[i].sample_descr_idx, 4); /* Samples description index */
663 box.length = cio_tell() - box.init_pos;
664 cio_seek(box.init_pos);
665 cio_write(box.length, 4); /* L */
666 cio_seek(box.init_pos + box.length);
675 int mj2_read_stsc(mj2_tk_t * tk)
680 mj2_read_boxhdr(&box); /* Box Size */
681 if (MJ2_STSC != box.type) {
682 fprintf(stderr, "Error: Expected STSC Marker\n");
687 if (0 != cio_read(1)) { /* Version = 0 */
688 fprintf(stderr, "Error: Only Version 0 handled in STSC box\n");
692 if (0 != cio_read(3)) { /* Flags = 0 */
693 fprintf(stderr, "Error with flag in STSC box. Expected flag 0\n");
697 tk->num_samplestochunk = cio_read(4);
700 (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
701 sizeof(mj2_sampletochunk_t));
704 for (i = 0; i < tk->num_samplestochunk; i++) {
705 tk->sampletochunk[i].first_chunk = cio_read(4);
706 tk->sampletochunk[i].samples_per_chunk = cio_read(4);
707 tk->sampletochunk[i].sample_descr_idx = cio_read(4);
710 mj2_stsc_decompact(tk); /* decompact sample to chunk box */
713 if (cio_tell() - box.init_pos != box.length) {
714 fprintf(stderr, "Error with STSC Box size\n");
726 void mj2_write_stts(mj2_tk_t * tk)
732 box.init_pos = cio_tell();
734 cio_write(MJ2_STTS, 4); /* STTS */
736 cio_write(0, 4); /* Version = 0, flags = 0 */
738 cio_write(tk->num_tts, 4); /* entry_count */
739 for (i = 0; i < tk->num_tts; i++) {
740 cio_write(tk->tts[i].sample_count, 4); /* Sample-count */
741 cio_write(tk->tts[i].sample_delta, 4); /* Sample-Delta */
744 box.length = cio_tell() - box.init_pos;
745 cio_seek(box.init_pos);
746 cio_write(box.length, 4); /* L */
747 cio_seek(box.init_pos + box.length);
756 int mj2_read_stts(mj2_tk_t * tk)
762 mj2_read_boxhdr(&box);
763 if (MJ2_STTS != box.type) {
764 fprintf(stderr, "Error: Expected STTS Marker\n");
769 if (0 != cio_read(1)) { /* Version = 0 */
770 fprintf(stderr, "Error: Only Version 0 handled in STTS box\n");
774 if (0 != cio_read(3)) { /* Flags = 0 */
775 fprintf(stderr, "Error with flag in STTS box. Expected flag 0\n");
779 tk->num_tts = cio_read(4);
781 tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
783 for (i = 0; i < tk->num_tts; i++) {
784 tk->tts[i].sample_count = cio_read(4);
785 tk->tts[i].sample_delta = cio_read(4);
788 mj2_tts_decompact(tk);
790 if (cio_tell() - box.init_pos != box.length) {
791 fprintf(stderr, "Error with STTS Box size\n");
803 void mj2_write_fiel(mj2_tk_t * tk)
808 box.init_pos = cio_tell();
810 cio_write(MJ2_FIEL, 4); /* STTS */
812 cio_write(tk->fieldcount, 1); /* Field count */
813 cio_write(tk->fieldorder, 1); /* Field order */
816 box.length = cio_tell() - box.init_pos;
817 cio_seek(box.init_pos);
818 cio_write(box.length, 4); /* L */
819 cio_seek(box.init_pos + box.length);
828 int mj2_read_fiel(mj2_tk_t * tk)
833 mj2_read_boxhdr(&box);
834 if (MJ2_FIEL != box.type) {
835 fprintf(stderr, "Error: Expected FIEL Marker\n");
840 tk->fieldcount = cio_read(1);
841 tk->fieldorder = cio_read(1);
843 if (cio_tell() - box.init_pos != box.length) {
844 fprintf(stderr, "Error with FIEL Box size\n");
853 * Original Format Box
856 void mj2_write_orfo(mj2_tk_t * tk)
860 box.init_pos = cio_tell();
862 cio_write(MJ2_ORFO, 4);
864 cio_write(tk->or_fieldcount, 1); /* Original Field count */
865 cio_write(tk->or_fieldorder, 1); /* Original Field order */
868 box.length = cio_tell() - box.init_pos;
869 cio_seek(box.init_pos);
870 cio_write(box.length, 4); /* L */
871 cio_seek(box.init_pos + box.length);
877 * Original Format Box
880 int mj2_read_orfo(mj2_tk_t * tk)
885 mj2_read_boxhdr(&box);
886 if (MJ2_ORFO != box.type) {
887 fprintf(stderr, "Error: Expected ORFO Marker\n");
892 tk->or_fieldcount = cio_read(1);
893 tk->or_fieldorder = cio_read(1);
895 if (cio_tell() - box.init_pos != box.length) {
896 fprintf(stderr, "Error with ORFO Box size\n");
908 void mj2_write_jp2p(mj2_tk_t * tk)
914 box.init_pos = cio_tell();
916 cio_write(MJ2_JP2P, 4);
918 cio_write(0, 4); /* Version 0, flags =0 */
920 for (i = 0; i < tk->num_br; i++) {
921 cio_write(tk->br[i], 4);
924 box.length = cio_tell() - box.init_pos;
925 cio_seek(box.init_pos);
926 cio_write(box.length, 4); /* L */
927 cio_seek(box.init_pos + box.length);
936 int mj2_read_jp2p(mj2_tk_t * tk)
942 mj2_read_boxhdr(&box);
943 if (MJ2_JP2P != box.type) {
944 fprintf(stderr, "Error: Expected JP2P Marker\n");
948 if (0 != cio_read(1)) { /* Version = 0 */
949 fprintf(stderr, "Error: Only Version 0 handled in JP2P box\n");
953 if (0 != cio_read(3)) { /* Flags = 0 */
954 fprintf(stderr, "Error with flag in JP2P box. Expected flag 0\n");
959 tk->num_br = (box.length - 12) / 4;
960 tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
962 for (i = 0; i < tk->num_br; i++) {
963 tk->br[i] = cio_read(4);
966 if (cio_tell() - box.init_pos != box.length) {
967 fprintf(stderr, "Error with JP2P Box size\n");
979 void mj2_write_jp2x(mj2_tk_t * tk)
985 box.init_pos = cio_tell();
987 cio_write(MJ2_JP2X, 4);
989 for (i = 0; i < tk->num_jp2x; i++) {
990 cio_write(tk->jp2xdata[i], 1);
993 box.length = cio_tell() - box.init_pos;
994 cio_seek(box.init_pos);
995 cio_write(box.length, 4); /* L */
996 cio_seek(box.init_pos + box.length);
1005 int mj2_read_jp2x(mj2_tk_t * tk)
1011 mj2_read_boxhdr(&box);
1012 if (MJ2_JP2X != box.type) {
1013 fprintf(stderr, "Error: Expected JP2X Marker\n");
1018 tk->num_jp2x = (box.length - 8);
1020 (unsigned char *) malloc(tk->num_jp2x * sizeof(unsigned char));
1022 for (i = 0; i < tk->num_jp2x; i++) {
1023 tk->jp2xdata[i] = cio_read(1);
1026 if (cio_tell() - box.init_pos != box.length) {
1027 fprintf(stderr, "Error with JP2X Box size\n");
1034 * Write the JSUB box
1036 * MJP2 Subsampling Box
1039 void mj2_write_jsub(mj2_tk_t * tk)
1044 box.init_pos = cio_tell();
1046 cio_write(MJ2_JSUB, 4);
1048 cio_write(tk->hsub, 1);
1049 cio_write(tk->vsub, 1);
1050 cio_write(tk->hoff, 1);
1051 cio_write(tk->voff, 1);
1053 box.length = cio_tell() - box.init_pos;
1054 cio_seek(box.init_pos);
1055 cio_write(box.length, 4); /* L */
1056 cio_seek(box.init_pos + box.length);
1062 * MJP2 Subsampling Box
1065 int mj2_read_jsub(mj2_tk_t * tk)
1069 mj2_read_boxhdr(&box);
1070 if (MJ2_JSUB != box.type) {
1071 fprintf(stderr, "Error: Expected JSUB Marker\n");
1075 tk->hsub = cio_read(1);
1076 tk->vsub = cio_read(1);
1077 tk->hoff = cio_read(1);;
1078 tk->voff = cio_read(1);
1080 if (cio_tell() - box.init_pos != box.length) {
1081 fprintf(stderr, "Error with JSUB Box size\n");
1088 * Write the SMJ2 box
1090 * Visual Sample Entry Description
1093 void mj2_write_smj2(mj2_tk_t * tk)
1097 box.init_pos = cio_tell();
1099 cio_write(MJ2_MJ2, 4); /* MJ2 */
1101 cio_write(0, 4); /* Version = 0, flags = 0 */
1105 cio_write(0, 2); /* Pre-defined */
1107 cio_write(0, 2); /* Reserved */
1109 cio_write(0, 4); /* Pre-defined */
1110 cio_write(0, 4); /* Pre-defined */
1111 cio_write(0, 4); /* Pre-defined */
1113 cio_write(tk->w, 2); /* Width */
1114 cio_write(tk->h, 2); /* Height */
1116 cio_write(tk->horizresolution, 4); /* Horizontal resolution */
1117 cio_write(tk->vertresolution, 4); /* Vertical resolution */
1119 cio_write(0, 4); /* Reserved */
1121 cio_write(1, 2); /* Pre-defined = 1 */
1123 cio_write(tk->compressorname[0], 4); /* Compressor Name */
1124 cio_write(tk->compressorname[1], 4);
1125 cio_write(tk->compressorname[2], 4);
1126 cio_write(tk->compressorname[3], 4);
1127 cio_write(tk->compressorname[4], 4);
1128 cio_write(tk->compressorname[5], 4);
1129 cio_write(tk->compressorname[6], 4);
1130 cio_write(tk->compressorname[7], 4);
1132 cio_write(tk->depth, 2); /* Depth */
1134 cio_write(0xffff, 2); /* Pre-defined = -1 */
1136 jp2_write_jp2h(&tk->jp2_struct);
1140 if (tk->num_br != 0)
1142 if (tk->num_jp2x != 0)
1148 box.length = cio_tell() - box.init_pos;
1149 cio_seek(box.init_pos);
1150 cio_write(box.length, 4); /* L */
1151 cio_seek(box.init_pos + box.length);
1157 * Visual Sample Entry Description
1160 int mj2_read_smj2(j2k_image_t * img, mj2_tk_t * tk)
1166 mj2_read_boxhdr(&box);
1168 if (MJ2_MJ2 != box.type) {
1169 fprintf(stderr, "Error in SMJ2 box: Expected MJ2 Marker\n");
1173 if (0 != cio_read(1)) { /* Version = 0 */
1174 fprintf(stderr, "Error: Only Version 0 handled in MJP2 box\n");
1178 if (0 != cio_read(3)) { /* Flags = 0 */
1179 fprintf(stderr, "Error with flag in MJP2 box. Expected flag 0\n");
1185 cio_skip(2); /* Pre-defined */
1187 cio_skip(2); /* Reserved */
1189 cio_skip(4); /* Pre-defined */
1190 cio_skip(4); /* Pre-defined */
1191 cio_skip(4); /* Pre-defined */
1193 tk->w = cio_read(2); /* Width */
1194 tk->h = cio_read(2); /* Height */
1196 tk->horizresolution = cio_read(4); /* Horizontal resolution */
1197 tk->vertresolution = cio_read(4); /* Vertical resolution */
1199 cio_skip(4); /* Reserved */
1201 cio_skip(2); /* Pre-defined = 1 */
1203 tk->compressorname[0] = cio_read(4); /* Compressor Name */
1204 tk->compressorname[1] = cio_read(4);
1205 tk->compressorname[2] = cio_read(4);
1206 tk->compressorname[3] = cio_read(4);
1207 tk->compressorname[4] = cio_read(4);
1208 tk->compressorname[5] = cio_read(4);
1209 tk->compressorname[6] = cio_read(4);
1210 tk->compressorname[7] = cio_read(4);
1212 tk->depth = cio_read(2); /* Depth */
1214 /* Init std value */
1218 tk->or_fieldcount = 1;
1219 tk->or_fieldorder = 0;
1221 cio_skip(2); /* Pre-defined = -1 */
1223 if (jp2_read_jp2h(&tk->jp2_struct)) {
1224 fprintf(stderr, "Error with JP2H Box\n");
1228 tk->jp2_struct.comps = (jp2_comps_t *) malloc(tk->jp2_struct.numcomps * sizeof(jp2_comps_t));
1229 tk->jp2_struct.cl = (int *) malloc(sizeof(int));
1234 for (i = 0; cio_tell() - box.init_pos < box.length; i++) {
1235 mj2_read_boxhdr(&box2);
1236 cio_seek(box2.init_pos);
1237 switch (box2.type) {
1239 if (mj2_read_fiel(tk))
1244 if (mj2_read_jp2p(tk))
1249 if (mj2_read_jp2x(tk))
1254 if (mj2_read_jsub(tk))
1259 if (mj2_read_orfo(tk))
1264 fprintf(stderr, "Error with MJP2 Box size\n");
1275 * Write the STSD box
1277 * Sample Description
1280 void mj2_write_stsd(mj2_tk_t * tk)
1284 box.init_pos = cio_tell();
1286 cio_write(MJ2_STSD, 4); /* STSD */
1288 cio_write(0, 4); /* Version = 0, flags = 0 */
1290 cio_write(1, 4); /* entry_count = 1 (considering same JP2 headerboxes) */
1292 if (tk->track_type == 0) {
1294 } else if (tk->track_type == 1) {
1297 if (tk->track_type == 2) {
1302 box.length = cio_tell() - box.init_pos;
1303 cio_seek(box.init_pos);
1304 cio_write(box.length, 4); /* L */
1305 cio_seek(box.init_pos + box.length);
1311 * Sample Description
1314 int mj2_read_stsd(mj2_tk_t * tk, j2k_image_t * img)
1317 int entry_count, len_2skip;
1321 mj2_read_boxhdr(&box);
1323 if (MJ2_STSD != box.type) {
1324 fprintf(stderr, "Error: Expected STSD Marker\n");
1328 if (0 != cio_read(1)) { /* Version = 0 */
1329 fprintf(stderr, "Error: Only Version 0 handled in STSD box\n");
1333 if (0 != cio_read(3)) { /* Flags = 0 */
1334 fprintf(stderr, "Error with flag in STSD box. Expected flag 0\n");
1338 entry_count = cio_read(4);
1340 if (tk->track_type == 0) {
1341 for (i = 0; i < entry_count; i++) {
1342 if (mj2_read_smj2(img, tk))
1345 } else if (tk->track_type == 1) {
1346 len_2skip = cio_read(4); // Not implemented -> skipping box
1347 cio_skip(len_2skip - 4);
1348 } else if (tk->track_type == 2) {
1349 len_2skip = cio_read(4); // Not implemented -> skipping box
1350 cio_skip(len_2skip - 4);
1354 if (cio_tell() - box.init_pos != box.length) {
1355 fprintf(stderr, "Error with STSD Box size\n");
1362 * Write the STBL box
1364 * Sample table box box
1367 void mj2_write_stbl(mj2_tk_t * tk)
1371 box.init_pos = cio_tell();
1373 cio_write(MJ2_STBL, 4); /* STBL */
1381 box.length = cio_tell() - box.init_pos;
1382 cio_seek(box.init_pos);
1383 cio_write(box.length, 4); /* L */
1384 cio_seek(box.init_pos + box.length);
1390 * Sample table box box
1393 int mj2_read_stbl(mj2_tk_t * tk, j2k_image_t * img)
1397 mj2_read_boxhdr(&box);
1398 if (MJ2_STBL != box.type) {
1399 fprintf(stderr, "Error: Expected STBL Marker\n");
1403 if (mj2_read_stsd(tk, img))
1405 if (mj2_read_stts(tk))
1407 if (mj2_read_stsc(tk))
1409 if (mj2_read_stsz(tk))
1411 if (mj2_read_stco(tk))
1414 if (cio_tell() - box.init_pos != box.length) {
1415 fprintf(stderr, "Error with STBL Box size\n");
1427 void mj2_write_url(mj2_tk_t * tk, int url_num)
1431 box.init_pos = cio_tell();
1433 cio_write(MJ2_URL, 4); /* URL */
1436 cio_write(1, 4); /* Version = 0, flags = 1 because stored in same file */
1438 cio_write(0, 4); /* Version = 0, flags = 0 */
1439 cio_write(tk->url[url_num - 1].location[0], 4);
1440 cio_write(tk->url[url_num - 1].location[1], 4);
1441 cio_write(tk->url[url_num - 1].location[2], 4);
1442 cio_write(tk->url[url_num - 1].location[3], 4);
1445 box.length = cio_tell() - box.init_pos;
1446 cio_seek(box.init_pos);
1447 cio_write(box.length, 4); /* L */
1448 cio_seek(box.init_pos + box.length);
1457 int mj2_read_url(mj2_tk_t * tk, int urn_num)
1461 mj2_read_boxhdr(&box);
1462 if (MJ2_URL != box.type) {
1463 fprintf(stderr, "Error: Expected URL Marker\n");
1467 if (0 != cio_read(1)) { /* Version = 0 */
1468 fprintf(stderr, "Error: Only Version 0 handled in URL box\n");
1472 if (1 != cio_read(3)) { /* If flags = 1 --> media data in file */
1473 tk->url[urn_num].location[0] = cio_read(4);
1474 tk->url[urn_num].location[1] = cio_read(4);
1475 tk->url[urn_num].location[2] = cio_read(4);
1476 tk->url[urn_num].location[3] = cio_read(4);
1482 if (cio_tell() - box.init_pos != box.length) {
1483 fprintf(stderr, "Error with URL Box size\n");
1495 void mj2_write_urn(mj2_tk_t * tk, int urn_num)
1499 box.init_pos = cio_tell();
1501 cio_write(MJ2_URN, 4); /* URN */
1503 cio_write(0, 4); /* Version = 0, flags = 0 */
1505 cio_write(tk->urn[urn_num].name[0], 4);
1506 cio_write(tk->urn[urn_num].name[1], 4);
1507 cio_write(tk->urn[urn_num].name[2], 4);
1508 cio_write(tk->urn[urn_num].name[3], 4);
1509 cio_write(tk->urn[urn_num].location[0], 4);
1510 cio_write(tk->urn[urn_num].location[1], 4);
1511 cio_write(tk->urn[urn_num].location[2], 4);
1512 cio_write(tk->urn[urn_num].location[3], 4);
1514 box.length = cio_tell() - box.init_pos;
1515 cio_seek(box.init_pos);
1516 cio_write(box.length, 4); /* L */
1517 cio_seek(box.init_pos + box.length);
1526 int mj2_read_urn(mj2_tk_t * tk, int urn_num)
1531 mj2_read_boxhdr(&box);
1532 if (MJ2_URN != box.type) {
1533 fprintf(stderr, "Error: Expected URN Marker\n");
1537 if (0 != cio_read(1)) { /* Version = 0 */
1538 fprintf(stderr, "Error: Only Version 0 handled in URN box\n");
1542 if (1 != cio_read(3)) { /* If flags = 1 --> media data in file */
1543 tk->urn[urn_num].name[0] = cio_read(4);
1544 tk->urn[urn_num].name[1] = cio_read(4);
1545 tk->urn[urn_num].name[2] = cio_read(4);
1546 tk->urn[urn_num].name[3] = cio_read(4);
1547 tk->urn[urn_num].location[0] = cio_read(4);
1548 tk->urn[urn_num].location[1] = cio_read(4);
1549 tk->urn[urn_num].location[2] = cio_read(4);
1550 tk->urn[urn_num].location[3] = cio_read(4);
1554 if (cio_tell() - box.init_pos != box.length) {
1555 fprintf(stderr, "Error with URN Box size\n");
1563 * Write the DREF box
1565 * Data reference box
1568 void mj2_write_dref(mj2_tk_t * tk)
1573 box.init_pos = cio_tell();
1575 cio_write(MJ2_DREF, 4); /* DREF */
1577 cio_write(0, 4); /* Version = 0, flags = 0 */
1579 if (tk->num_url + tk->num_urn == 0) { /* Media data in same file */
1580 cio_write(1, 4); /* entry_count = 1 */
1581 mj2_write_url(tk, 0);
1583 cio_write(tk->num_url + tk->num_urn, 4); /* entry_count */
1585 for (i = 0; i < tk->num_url; i++)
1586 mj2_write_url(tk, i + 1);
1588 for (i = 0; i < tk->num_urn; i++)
1589 mj2_write_urn(tk, i);
1592 box.length = cio_tell() - box.init_pos;
1593 cio_seek(box.init_pos);
1594 cio_write(box.length, 4); /* L */
1595 cio_seek(box.init_pos + box.length);
1601 * Data reference box
1604 int mj2_read_dref(mj2_tk_t * tk)
1608 int entry_count, marker;
1611 mj2_read_boxhdr(&box);
1612 if (MJ2_DREF != box.type) {
1613 fprintf(stderr, "Error: Expected DREF Marker\n");
1617 if (0 != cio_read(1)) { /* Version = 0 */
1618 fprintf(stderr, "Error: Only Version 0 handled in DREF box\n");
1622 if (0 != cio_read(3)) { /* Flags = 0 */
1623 fprintf(stderr, "Error with flag in DREF box. Expected flag 0\n");
1627 entry_count = cio_read(4);
1631 for (i = 0; i < entry_count; i++) {
1633 marker = cio_read(4);
1634 if (marker == MJ2_URL) {
1637 if (mj2_read_url(tk, tk->num_url))
1639 } else if (marker == MJ2_URN) {
1642 if (mj2_read_urn(tk, tk->num_urn))
1645 fprintf(stderr, "Error with in DREF box. Expected URN or URL box\n");
1652 if (cio_tell() - box.init_pos != box.length) {
1653 fprintf(stderr, "Error with DREF Box size\n");
1660 * Write the DINF box
1662 * Data information box
1665 void mj2_write_dinf(mj2_tk_t * tk)
1669 box.init_pos = cio_tell();
1671 cio_write(MJ2_DINF, 4); /* DINF */
1675 box.length = cio_tell() - box.init_pos;
1676 cio_seek(box.init_pos);
1677 cio_write(box.length, 4); /* L */
1678 cio_seek(box.init_pos + box.length);
1684 * Data information box
1687 int mj2_read_dinf(mj2_tk_t * tk)
1691 mj2_read_boxhdr(&box);
1692 if (MJ2_DINF != box.type) {
1693 fprintf(stderr, "Error: Expected DINF Marker\n");
1697 if (mj2_read_dref(tk))
1700 if (cio_tell() - box.init_pos != box.length) {
1701 fprintf(stderr, "Error with DINF Box size\n");
1708 * Write the VMHD box
1710 * Video Media information box
1713 void mj2_write_vmhd(mj2_tk_t * tk)
1717 box.init_pos = cio_tell();
1719 cio_write(MJ2_VMHD, 4); /* VMHD */
1721 cio_write(1, 4); /* Version = 0, flags = 1 */
1723 cio_write(tk->graphicsmode, 2);
1724 cio_write(tk->opcolor[0], 2);
1725 cio_write(tk->opcolor[1], 2);
1726 cio_write(tk->opcolor[2], 2);
1728 box.length = cio_tell() - box.init_pos;
1729 cio_seek(box.init_pos);
1730 cio_write(box.length, 4); /* L */
1731 cio_seek(box.init_pos + box.length);
1737 * Video Media information box
1740 int mj2_read_vmhd(mj2_tk_t * tk)
1744 mj2_read_boxhdr(&box);
1745 if (MJ2_VMHD != box.type) {
1746 fprintf(stderr, "Error: Expected VMHD Marker\n");
1750 if (0 != cio_read(1)) { /* Version = 0 */
1751 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
1755 if (1 != cio_read(3)) { /* Flags = 1 */
1756 fprintf(stderr, "Error with flag in VMHD box. Expected flag 1\n");
1761 tk->graphicsmode = cio_read(2);
1762 tk->opcolor[0] = cio_read(2);
1763 tk->opcolor[1] = cio_read(2);
1764 tk->opcolor[2] = cio_read(2);
1766 if (cio_tell() - box.init_pos != box.length) {
1767 fprintf(stderr, "Error with VMHD Box size\n");
1774 * Write the SMHD box
1776 * Sound Media information box
1779 void mj2_write_smhd(mj2_tk_t * tk)
1783 box.init_pos = cio_tell();
1785 cio_write(MJ2_SMHD, 4); /* SMHD */
1787 cio_write(0, 4); /* Version = 0, flags = 0 */
1789 cio_write(tk->balance, 2);
1791 cio_write(0, 2); /* Reserved */
1793 box.length = cio_tell() - box.init_pos;
1794 cio_seek(box.init_pos);
1795 cio_write(box.length, 4); /* L */
1796 cio_seek(box.init_pos + box.length);
1802 * Sound Media information box
1805 int mj2_read_smhd(mj2_tk_t * tk)
1809 mj2_read_boxhdr(&box);
1810 if (MJ2_SMHD != box.type) {
1811 fprintf(stderr, "Error: Expected SMHD Marker\n");
1815 if (0 != cio_read(1)) { /* Version = 0 */
1816 fprintf(stderr, "Error: Only Version 0 handled in SMHD box\n");
1820 if (0 != cio_read(3)) { /* Flags = 0 */
1821 fprintf(stderr, "Error with flag in SMHD box. Expected flag 0\n");
1826 tk->balance = cio_read(2);
1828 cio_skip(2); /* Reserved */
1830 if (cio_tell() - box.init_pos != box.length) {
1831 fprintf(stderr, "Error with SMHD Box size\n");
1838 * Write the HMHD box
1840 * Hint Media information box
1843 void mj2_write_hmhd(mj2_tk_t * tk)
1847 box.init_pos = cio_tell();
1849 cio_write(MJ2_HMHD, 4); /* HMHD */
1851 cio_write(0, 4); /* Version = 0, flags = 0 */
1853 cio_write(tk->maxPDUsize, 2);
1854 cio_write(tk->avgPDUsize, 2);
1855 cio_write(tk->maxbitrate, 4);
1856 cio_write(tk->avgbitrate, 4);
1857 cio_write(tk->slidingavgbitrate, 4);
1859 box.length = cio_tell() - box.init_pos;
1860 cio_seek(box.init_pos);
1861 cio_write(box.length, 4); /* L */
1862 cio_seek(box.init_pos + box.length);
1868 * Hint Media information box
1871 int mj2_read_hmhd(mj2_tk_t * tk)
1875 mj2_read_boxhdr(&box);
1876 if (MJ2_HMHD != box.type) {
1877 fprintf(stderr, "Error: Expected HMHD Marker\n");
1881 if (0 != cio_read(1)) { /* Version = 0 */
1882 fprintf(stderr, "Error: Only Version 0 handled in HMHD box\n");
1886 if (0 != cio_read(3)) { /* Flags = 0 */
1887 fprintf(stderr, "Error with flag in HMHD box. Expected flag 0\n");
1892 tk->maxPDUsize = cio_read(2);
1893 tk->avgPDUsize = cio_read(2);
1894 tk->maxbitrate = cio_read(4);
1895 tk->avgbitrate = cio_read(4);
1896 tk->slidingavgbitrate = cio_read(4);
1898 if (cio_tell() - box.init_pos != box.length) {
1899 fprintf(stderr, "Error with HMHD Box size\n");
1906 * Write the MINF box
1908 * Media information box
1911 void mj2_write_minf(mj2_tk_t * tk)
1915 box.init_pos = cio_tell();
1917 cio_write(MJ2_MINF, 4); /* MINF */
1919 if (tk->track_type == 0) {
1921 } else if (tk->track_type == 1) {
1923 } else if (tk->track_type == 2) {
1930 box.length = cio_tell() - box.init_pos;
1931 cio_seek(box.init_pos);
1932 cio_write(box.length, 4); /* L */
1933 cio_seek(box.init_pos + box.length);
1939 * Media information box
1942 int mj2_read_minf(mj2_tk_t * tk, j2k_image_t * img)
1945 unsigned int box_type;
1948 mj2_read_boxhdr(&box);
1949 if (MJ2_MINF != box.type) {
1950 fprintf(stderr, "Error: Expected MINF Marker\n");
1955 box_type = cio_read(4);
1958 if (box_type == MJ2_VMHD) {
1959 if (mj2_read_vmhd(tk))
1961 } else if (box_type == MJ2_SMHD) {
1962 if (mj2_read_smhd(tk))
1964 } else if (box_type == MJ2_HMHD) {
1965 if (mj2_read_hmhd(tk))
1968 fprintf(stderr, "Error in MINF box expected vmhd, smhd or hmhd\n");
1972 if (mj2_read_dinf(tk))
1975 if (mj2_read_stbl(tk, img))
1978 if (cio_tell() - box.init_pos != box.length) {
1979 fprintf(stderr, "Error with MINF Box size\n");
1986 * Write the HDLR box
1988 * Handler reference box
1991 void mj2_write_hdlr(mj2_tk_t * tk)
1995 box.init_pos = cio_tell();
1997 cio_write(MJ2_HDLR, 4); /* HDLR */
1999 cio_write(0, 4); /* Version = 0, flags = 0 */
2001 cio_write(0, 4); /* Predefine */
2003 tk->name = 0; /* The track name is immediately determined by the track type */
2005 if (tk->track_type == 0) {
2006 tk->handler_type = 0x76696465; /* Handler type: vide */
2007 cio_write(tk->handler_type, 4);
2011 cio_write(0, 4); /* Reserved */
2013 cio_write(0x76696465, 4);
2014 cio_write(0x6F206d65, 4);
2015 cio_write(0x64696120, 4);
2016 cio_write(0x74726163, 4);
2017 cio_write(0x6b00, 2); /* String: video media track */
2018 } else if (tk->track_type == 1) {
2019 tk->handler_type = 0x736F756E; /* Handler type: soun */
2020 cio_write(tk->handler_type, 4);
2024 cio_write(0, 4); /* Reserved */
2026 cio_write(0x536F756E, 4);
2027 cio_write(0x6400, 2); /* String: Sound */
2028 } else if (tk->track_type == 2) {
2029 tk->handler_type = 0x68696E74; /* Handler type: hint */
2030 cio_write(tk->handler_type, 4);
2034 cio_write(0, 4); /* Reserved */
2036 cio_write(0x48696E74, 4);
2037 cio_write(0, 2); /* String: Hint */
2040 box.length = cio_tell() - box.init_pos;
2041 cio_seek(box.init_pos);
2042 cio_write(box.length, 4); /* L */
2043 cio_seek(box.init_pos + box.length);
2049 * Handler reference box
2052 int mj2_read_hdlr(mj2_tk_t * tk)
2057 mj2_read_boxhdr(&box);
2058 if (MJ2_HDLR != box.type) {
2059 fprintf(stderr, "Error: Expected HDLR Marker\n");
2064 if (0 != cio_read(1)) { /* Version = 0 */
2065 fprintf(stderr, "Error: Only Version 0 handled in HDLR box\n");
2069 if (0 != cio_read(3)) { /* Flags = 0 */
2070 fprintf(stderr, "Error with flag in HDLR box. Expected flag 0\n");
2074 cio_skip(4); /* Reserved */
2076 tk->handler_type = cio_read(4);
2077 cio_skip(12); /* Reserved */
2079 tk->name_size = box.length - 32;
2081 tk->name = (char *) malloc(tk->name_size * sizeof(char));
2082 for (i = 0; i < tk->name_size; i++) {
2083 tk->name[i] = cio_read(1); /* Name */
2086 if (cio_tell() - box.init_pos != box.length) {
2087 fprintf(stderr, "Error with HDLR Box size\n");
2094 * Write the MDHD box
2099 void mj2_write_mdhd(mj2_tk_t * tk)
2104 unsigned int modification_time;
2106 box.init_pos = cio_tell();
2108 cio_write(MJ2_MDHD, 4); /* MDHD */
2110 cio_write(0, 4); /* Version = 0, flags = 0 */
2112 cio_write(tk->creation_time, 4); /* Creation Time */
2114 time(<ime); /* Time since 1/1/70 */
2115 modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2117 cio_write(modification_time, 4); /* Modification Time */
2119 cio_write(tk->timescale, 4); /* Timescale */
2123 for (i = 0; i < tk->num_samples; i++)
2124 tk->duration += tk->sample[i].sample_delta;
2126 cio_write(tk->duration, 4); /* Duration */
2128 cio_write(tk->language, 2); /* Language */
2130 cio_write(0, 2); /* Predefined */
2132 box.length = cio_tell() - box.init_pos;
2133 cio_seek(box.init_pos);
2134 cio_write(box.length, 4); /* L */
2135 cio_seek(box.init_pos + box.length);
2144 int mj2_read_mdhd(mj2_tk_t * tk)
2148 mj2_read_boxhdr(&box);
2149 if (!(MJ2_MHDR == box.type || MJ2_MDHD == box.type)) { // Kakadu writes MHDR instead of MDHD
2150 fprintf(stderr, "Error: Expected MDHD Marker\n");
2154 if (0 != cio_read(1)) { /* Version = 0 */
2155 fprintf(stderr, "Error: Only Version 0 handled in MDHD box\n");
2159 if (0 != cio_read(3)) { /* Flags = 0 */
2160 fprintf(stderr, "Error with flag in MDHD box. Expected flag 0\n");
2165 tk->creation_time = cio_read(4); /* Creation Time */
2167 tk->modification_time = cio_read(4); /* Modification Time */
2169 tk->timescale = cio_read(4); /* Timescale */
2171 tk->duration = cio_read(4); /* Duration */
2173 tk->language = cio_read(2); /* Language */
2175 cio_skip(2); /* Predefined */
2177 if (cio_tell() - box.init_pos != box.length) {
2178 fprintf(stderr, "Error with MDHD Box size\n");
2185 * Write the MDIA box
2190 void mj2_write_mdia(mj2_tk_t * tk)
2194 box.init_pos = cio_tell();
2196 cio_write(MJ2_MDIA, 4); /* MDIA */
2202 box.length = cio_tell() - box.init_pos;
2203 cio_seek(box.init_pos);
2204 cio_write(box.length, 4); /* L */
2205 cio_seek(box.init_pos + box.length);
2214 int mj2_read_mdia(mj2_tk_t * tk, j2k_image_t * img)
2218 mj2_read_boxhdr(&box);
2219 if (MJ2_MDIA != box.type) {
2220 fprintf(stderr, "Error: Expected MDIA Marker\n");
2224 if (mj2_read_mdhd(tk))
2226 if (mj2_read_hdlr(tk))
2228 if (mj2_read_minf(tk, img))
2231 if (cio_tell() - box.init_pos != box.length) {
2232 fprintf(stderr, "Error with MDIA Box size\n");
2239 * Write the TKHD box
2244 void mj2_write_tkhd(mj2_tk_t * tk)
2250 box.init_pos = cio_tell();
2253 cio_write(MJ2_TKHD, 4); /* TKHD */
2255 cio_write(3, 4); /* Version=0, flags=3 */
2257 time(<ime); /* Time since 1/1/70 */
2258 tk->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2260 cio_write(tk->creation_time, 4); /* Creation Time */
2262 cio_write(tk->modification_time, 4); /* Modification Time */
2264 cio_write(tk->track_ID, 4); /* Track ID */
2266 cio_write(0, 4); /* Reserved */
2270 for (i = 0; i < tk->num_samples; i++)
2271 tk->duration += tk->sample[i].sample_delta;
2273 cio_write(tk->duration, 4); /* Duration */
2275 cio_write(0, 4); /* Reserved */
2276 cio_write(0, 4); /* Reserved */
2278 cio_write(tk->layer, 2); /* Layer */
2280 cio_write(0, 2); /* Predefined */
2282 cio_write(tk->volume, 2); /* Volume */
2284 cio_write(0, 2); /* Reserved */
2286 cio_write(tk->trans_matrix[0], 4); /* Transformation matrix for track */
2287 cio_write(tk->trans_matrix[1], 4);
2288 cio_write(tk->trans_matrix[2], 4);
2289 cio_write(tk->trans_matrix[3], 4);
2290 cio_write(tk->trans_matrix[4], 4);
2291 cio_write(tk->trans_matrix[5], 4);
2292 cio_write(tk->trans_matrix[6], 4);
2293 cio_write(tk->trans_matrix[7], 4);
2294 cio_write(tk->trans_matrix[8], 4);
2296 cio_write(tk->visual_w, 4); /* Video Visual Width */
2298 cio_write(tk->visual_h, 4); /* Video Visual Height */
2300 box.length = cio_tell() - box.init_pos;
2301 cio_seek(box.init_pos);
2302 cio_write(box.length, 4); /* L */
2303 cio_seek(box.init_pos + box.length);
2312 int mj2_read_tkhd(mj2_tk_t * tk)
2318 mj2_read_boxhdr(&box);
2320 if (MJ2_TKHD != box.type) {
2321 fprintf(stderr, "Error: Expected TKHD Marker\n");
2325 if (0 != cio_read(1)) { /* Version = 0 */
2326 fprintf(stderr, "Error: Only Version 0 handled in TKHD box\n");
2332 if (!(flag == 1 || flag == 2 || flag == 3 || flag == 4)) { /* Flags = 1,2,3 or 4 */
2334 "Error with flag in TKHD box: Expected flag 1,2,3 or 4\n");
2338 tk->creation_time = cio_read(4); /* Creation Time */
2340 tk->modification_time = cio_read(4); /* Modification Time */
2342 tk->track_ID = cio_read(4); /* Track ID */
2344 cio_skip(4); /* Reserved */
2346 tk->duration = cio_read(4); /* Duration */
2348 cio_skip(8); /* Reserved */
2350 tk->layer = cio_read(2); /* Layer */
2352 cio_read(2); /* Predefined */
2354 tk->volume = cio_read(2); /* Volume */
2356 cio_skip(2); /* Reserved */
2358 tk->trans_matrix[0] = cio_read(4); /* Transformation matrix for track */
2359 tk->trans_matrix[1] = cio_read(4);
2360 tk->trans_matrix[2] = cio_read(4);
2361 tk->trans_matrix[3] = cio_read(4);
2362 tk->trans_matrix[4] = cio_read(4);
2363 tk->trans_matrix[5] = cio_read(4);
2364 tk->trans_matrix[6] = cio_read(4);
2365 tk->trans_matrix[7] = cio_read(4);
2366 tk->trans_matrix[8] = cio_read(4);
2368 tk->visual_w = cio_read(4); /* Video Visual Width */
2370 tk->visual_h = cio_read(4); /* Video Visual Height */
2372 if (cio_tell() - box.init_pos != box.length) {
2373 fprintf(stderr, "Error with TKHD Box size\n");
2380 * Write the TRAK box
2385 void mj2_write_trak(mj2_tk_t * tk)
2389 box.init_pos = cio_tell();
2392 cio_write(MJ2_TRAK, 4); /* TRAK */
2397 box.length = cio_tell() - box.init_pos;
2398 cio_seek(box.init_pos);
2399 cio_write(box.length, 4); /* L */
2400 cio_seek(box.init_pos + box.length);
2409 int mj2_read_trak(mj2_tk_t * tk, j2k_image_t * img)
2413 mj2_read_boxhdr(&box);
2414 if (MJ2_TRAK != box.type) {
2415 fprintf(stderr, "Error: Expected TRAK Marker\n");
2418 if (mj2_read_tkhd(tk))
2420 if (mj2_read_mdia(tk, img))
2422 if (cio_tell() - box.init_pos != box.length) {
2423 fprintf(stderr, "Error with TRAK Box\n");
2430 * Write the MVHD box
2435 void mj2_write_mvhd(mj2_movie_t * movie)
2443 box.init_pos = cio_tell();
2445 cio_write(MJ2_MVHD, 4); /* MVHD */
2447 cio_write(0, 4); /* Version = 0, flags = 0 */
2449 time(<ime); /* Time since 1/1/70 */
2450 movie->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2452 cio_write(movie->creation_time, 4); /* Creation Time */
2454 cio_write(movie->modification_time, 4); /* Modification Time */
2456 cio_write(movie->timescale, 4); /* Timescale */
2458 movie->duration = 0;
2460 for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
2461 mj2_tk_t *tk = &movie->tk[i];
2463 for (j = 0; j < tk->num_samples; j++) {
2464 movie->duration += tk->sample[j].sample_delta;
2468 cio_write(movie->duration, 4);
2470 cio_write(movie->rate, 4); /* Rate to play presentation */
2472 cio_write(movie->volume, 2); /* Volume */
2474 cio_write(0, 2); /* Reserved */
2475 cio_write(0, 4); /* Reserved */
2476 cio_write(0, 4); /* Reserved */
2478 cio_write(movie->trans_matrix[0], 4); /* Transformation matrix for video */
2479 cio_write(movie->trans_matrix[1], 4);
2480 cio_write(movie->trans_matrix[2], 4);
2481 cio_write(movie->trans_matrix[3], 4);
2482 cio_write(movie->trans_matrix[4], 4);
2483 cio_write(movie->trans_matrix[5], 4);
2484 cio_write(movie->trans_matrix[6], 4);
2485 cio_write(movie->trans_matrix[7], 4);
2486 cio_write(movie->trans_matrix[8], 4);
2488 cio_write(0, 4); /* Pre-defined */
2489 cio_write(0, 4); /* Pre-defined */
2490 cio_write(0, 4); /* Pre-defined */
2491 cio_write(0, 4); /* Pre-defined */
2492 cio_write(0, 4); /* Pre-defined */
2493 cio_write(0, 4); /* Pre-defined */
2496 for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) {
2497 if (max_tk_num < movie->tk[i].track_ID)
2498 max_tk_num = movie->tk[i].track_ID;
2501 movie->next_tk_id = max_tk_num + 1;
2503 cio_write(movie->next_tk_id, 4); /* ID of Next track to be added */
2505 box.length = cio_tell() - box.init_pos;
2506 cio_seek(box.init_pos);
2507 cio_write(box.length, 4); /* L */
2508 cio_seek(box.init_pos + box.length);
2517 int mj2_read_mvhd(mj2_movie_t * movie)
2521 mj2_read_boxhdr(&box);
2522 if (MJ2_MVHD != box.type) {
2523 fprintf(stderr, "Error: Expected MVHD Marker\n");
2528 if (0 != cio_read(4)) { /* Version = 0, flags = 0 */
2529 fprintf(stderr, "Error: Only Version 0 handled in MVHD box\n");
2532 movie->creation_time = cio_read(4); /* Creation Time */
2534 movie->modification_time = cio_read(4); /* Modification Time */
2536 movie->timescale = cio_read(4); /* Timescale */
2538 movie->duration = cio_read(4); /* Duration */
2540 movie->rate = cio_read(4); /* Rate to play presentation */
2542 movie->volume = cio_read(2); /* Volume */
2544 cio_skip(10); /* Reserved */
2546 movie->trans_matrix[0] = cio_read(4); /* Transformation matrix for video */
2547 movie->trans_matrix[1] = cio_read(4);
2548 movie->trans_matrix[2] = cio_read(4);
2549 movie->trans_matrix[3] = cio_read(4);
2550 movie->trans_matrix[4] = cio_read(4);
2551 movie->trans_matrix[5] = cio_read(4);
2552 movie->trans_matrix[6] = cio_read(4);
2553 movie->trans_matrix[7] = cio_read(4);
2554 movie->trans_matrix[8] = cio_read(4);
2556 cio_skip(24); /* Pre-defined */
2558 movie->next_tk_id = cio_read(4); /* ID of Next track to be added */
2560 if (cio_tell() - box.init_pos != box.length) {
2561 fprintf(stderr, "Error with MVHD Box Size\n");
2569 * Write the MOOV box
2574 void mj2_write_moov(mj2_movie_t * movie)
2579 box.init_pos = cio_tell();
2581 cio_write(MJ2_MOOV, 4); /* MOOV */
2583 mj2_write_mvhd(movie);
2585 for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
2586 mj2_write_trak(&movie->tk[i]);
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);
2602 int mj2_read_moov(mj2_movie_t * movie, j2k_image_t * img)
2608 mj2_read_boxhdr(&box);
2609 if (MJ2_MOOV != box.type) {
2610 fprintf(stderr, "Error: Expected MOOV Marker\n");
2616 if (mj2_read_mvhd(movie))
2621 (mj2_tk_t *) malloc((movie->next_tk_id - 1) * sizeof(mj2_tk_t));
2623 for (i = 0; cio_tell() - box.init_pos < box.length; i++) {
2624 mj2_read_boxhdr(&box2);
2625 if (box2.type == MJ2_TRAK) {
2626 cio_seek(box2.init_pos);
2627 if (mj2_read_trak(&movie->tk[i], img))
2630 if (movie->tk[i].track_type == 0) {
2632 } else if (movie->tk[i].track_type == 1) {
2634 } else if (movie->tk[i].track_type == 2) {
2637 } else if (box2.type == MJ2_MVEX) {
2638 cio_seek(box2.init_pos);
2639 cio_skip(box2.length);
2642 fprintf(stderr, "Error with MOOV Box: Expected TRAK or MVEX box\n");
2649 int mj2_read_struct(FILE *file, mj2_movie_t * movie) {
2660 src = (char*) malloc (300 * sizeof(char));
2661 fread(src,300,1, file); // Assuming that jp and ftyp markers size do
2662 // not exceed 300 bytes
2668 if (mj2_read_ftyp(movie))
2671 fsresult = fseek(file,cio_tell(),SEEK_SET);
2673 fprintf(stderr, "End of file reached while trying to read data after FTYP box\n" );
2677 foffset = cio_tell();
2681 fread(src,30,1,file);
2683 mj2_read_boxhdr(&box);
2685 while(box.type != MJ2_MOOV) {
2690 fsresult = fseek(file,foffset+box.length,SEEK_SET);
2692 fprintf(stderr, "End of file reached while trying to read MDAT box\n" );
2695 foffset += box.length;
2699 fsresult = fseek(file,foffset+box.length,SEEK_SET);
2701 fprintf(stderr, "End of file reached while trying to read MOOF box\n" );
2704 foffset += box.length;
2707 fsresult = fseek(file,foffset+box.length,SEEK_SET);
2709 fprintf(stderr, "End of file reached while trying to read FREE box\n" );
2712 foffset += box.length;
2715 fsresult = fseek(file,foffset+box.length,SEEK_SET);
2717 fprintf(stderr, "End of file reached while trying to read SKIP box\n" );
2720 foffset += box.length;
2723 fprintf(stderr, "Unknown box in MJ2 stream\n");
2724 fsresult = fseek(file,foffset+box.length,SEEK_SET);
2726 fprintf(stderr, "End of file reached while trying to read end of unknown box\n");
2729 foffset += box.length;
2732 fsresult = fread(src,8,1,file);
2733 if (fsresult != 1) {
2734 fprintf(stderr, "MOOV box not found in file\n");
2738 mj2_read_boxhdr(&box);
2741 fseek(file,foffset,SEEK_SET);
2742 src = realloc(src,box.length);
2743 fsresult = fread(src,box.length,1,file);
2744 if (fsresult != 1) {
2745 fprintf(stderr, "End of file reached while trying to read MOOV box\n");
2749 cio_init(src, box.length);
2751 if (mj2_read_moov(movie, &img))