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)
291 if (tk->num_samplestochunk == 1) {
293 (unsigned int) ceil((double) tk->num_samples /
294 (double) tk->sampletochunk[0].samples_per_chunk);
296 (mj2_chunk_t *) malloc(tk->num_chunks * sizeof(mj2_chunk_t));
297 for (k = 0; k < tk->num_chunks; k++) {
298 tk->chunk[k].num_samples = tk->sampletochunk[0].samples_per_chunk;
303 (mj2_chunk_t *) malloc(tk->num_samples * sizeof(mj2_chunk_t));
305 for (i = 0; i < tk->num_samplestochunk - 1; i++) {
306 for (j = tk->sampletochunk[i].first_chunk - 1;
307 j < tk->sampletochunk[i + 1].first_chunk - 1; j++) {
308 tk->chunk[j].num_samples = tk->sampletochunk[i].samples_per_chunk;
313 for (k = tk->sampletochunk[tk->num_samplestochunk].first_chunk - 1;
314 k < tk->num_chunks; k++) {
315 tk->chunk[k].num_samples =
316 tk->sampletochunk[tk->num_samplestochunk].samples_per_chunk;
318 tk->chunk = realloc(tk->chunk, tk->num_chunks * sizeof(mj2_chunk_t));
324 * Chunk offset box Decompact
327 void mj2_stco_decompact(mj2_tk_t * tk)
332 int intra_chunk_offset;
334 for (i = 0; i < tk->num_chunks; i++) {
335 intra_chunk_offset = 0;
336 for (j = 0; j < tk->chunk[i].num_samples; j++) {
337 tk->sample[k].offset = intra_chunk_offset + tk->chunk[i].offset;
338 intra_chunk_offset += tk->sample[k].sample_size;
353 box.init_pos = cio_tell();
356 cio_write(MJ2_JP, 4); /* JP */
357 cio_write(0x0d0a870a, 4); /* 0x0d0a870a required in a JP box */
359 box.length = cio_tell() - box.init_pos;
360 cio_seek(box.init_pos);
361 cio_write(box.length, 4);
362 cio_seek(box.init_pos + box.length);
368 * JPEG 2000 signature
375 mj2_read_boxhdr(&box);
376 if (MJ2_JP != box.type) { /* Check Marker */
377 fprintf(stderr, "Error: Expected JP Marker\n");
380 if (0x0d0a870a != cio_read(4)) { /* read the 0x0d0a870a required in a JP box */
381 fprintf(stderr, "Error with JP Marker\n");
384 if (cio_tell() - box.init_pos != box.length) { /* Check box length */
385 fprintf(stderr, "Error with JP Box size \n");
398 void mj2_write_ftyp(mj2_movie_t * movie)
402 box.init_pos = cio_tell();
405 cio_write(MJ2_FTYP, 4); /* FTYP */
406 cio_write(movie->brand, 4); /* BR */
407 cio_write(movie->minversion, 4); /* MinV */
409 for (i = 0; i < movie->num_cl; i++)
410 cio_write(movie->cl[i], 4); /* CL */
412 box.length = cio_tell() - box.init_pos;
413 cio_seek(box.init_pos);
414 cio_write(box.length, 4); /* Length */
415 cio_seek(box.init_pos + box.length);
424 int mj2_read_ftyp(mj2_movie_t * movie)
429 mj2_read_boxhdr(&box); /* Box Size */
430 if (MJ2_FTYP != box.type) {
431 fprintf(stderr, "Error: Expected FTYP Marker\n");
435 movie->brand = cio_read(4); /* BR */
436 movie->minversion = cio_read(4); /* MinV */
437 movie->num_cl = (box.length - 16) / 4;
439 (unsigned int *) malloc(movie->num_cl * sizeof(unsigned int));
441 for (i = movie->num_cl - 1; i > -1; i--)
442 movie->cl[i] = cio_read(4); /* CLi */
444 if (cio_tell() - box.init_pos != box.length) {
445 fprintf(stderr, "Error with FTYP Box\n");
457 int mj2_write_mdat(FILE * outfile, mj2_movie_t * movie, j2k_image_t * img,
458 j2k_cp_t * cp, char *outbuf, char *index)
460 unsigned char box_len_ptr;
465 int pos_correction = 0;
468 box.init_pos = cio_tell();
470 cio_write(MJ2_MDAT, 4); /* MDAT */
473 for (i = 0; i < movie->num_stk; i++) {
474 fprintf(stderr, "Unable to write sound tracks\n");
477 for (i = 0; i < movie->num_htk; i++) {
478 fprintf(stderr, "Unable to write hint tracks\n");
481 for (i = 0; i < movie->num_vtk; i++) {
487 fprintf(stderr, "Video Track number %d\n", i + 1);
490 fwrite(outbuf, 1, len, outfile);
491 pos_correction = cio_tell() + pos_correction;
494 /* Copy the first tile coding parameters (tcp) to cp_init */
497 (j2k_tcp_t *) malloc(cp->tw * cp->th * sizeof(j2k_tcp_t));
498 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
499 for (l = 0; l < cp->tcps[tileno].numlayers; l++) {
500 cp_init.tcps[tileno].rates[l] = cp->tcps[tileno].rates[l];
501 //tileno = cp->tcps[tileno].rates[l];
506 for (j = 0; j < tk->num_samples; j++) {
507 outbuf = (char *) malloc(cp->tdx * cp->tdy * cp->th * cp->tw * 2);
508 cio_init(outbuf, cp->tdx * cp->tdy * cp->th * cp->tw * 2);
510 fprintf(stderr, "Frame number %d/%d: \n", j + 1, tk->num_samples);
513 if (!yuvtoimage(tk, img, j)) {
514 fprintf(stderr, "Error with frame number %d in YUV file\n", j);
518 len = jp2_write_jp2c(img, cp, outbuf, index);
520 for (m = 0; m < img->numcomps; m++) {
521 free(img->comps[m].data);
524 tk->sample[j].sample_size = len;
526 tk->sample[j].offset = pos_correction;
527 tk->chunk[j].offset = pos_correction; /* There is one sample per chunk */
529 fwrite(outbuf, 1, len, outfile);
531 pos_correction = cio_tell() + pos_correction;
535 /* Copy the cp_init parameters to cp->tcps */
537 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
538 for (k = 0; k < cp->tcps[tileno].numlayers; k++) {
539 cp->tcps[tileno].rates[k] = cp_init.tcps[tileno].rates[k];
545 box.length = pos_correction - box.init_pos;
547 fseek(outfile, box.init_pos, SEEK_SET);
549 cio_init(&box_len_ptr, 4); /* Init a cio to write box length variable in a little endian way */
550 cio_write(box.length, 4);
552 fwrite(&box_len_ptr, 4, 1, outfile);
554 fseek(outfile, box.init_pos + box.length, SEEK_SET);
565 int mj2_read_mdat(mj2_movie_t * movie, unsigned char *src, char *outfile)
569 int jp2c_cio_len, jp2c_len, pos_correction = 0;
574 mj2_read_boxhdr(&box);
575 if (MJ2_MDAT != box.type) {
576 fprintf(stderr, "Error: Expected MDAT Marker\n");
580 pos_correction = cio_tell() - box.init_pos;
582 f = fopen(outfile, "w"); /* Erase content of file if it already exists */
585 for (track_nb = 0; track_nb < movie->next_tk_id - 1; track_nb++) {
586 if ((movie->tk->imagefile != (char *) 0xcdcdcdcd)
587 && (movie->tk->imagefile != NULL)) {
588 fprintf(stderr, "%s", movie->tk[track_nb].imagefile);
590 f = fopen(movie->tk->imagefile, "w"); /* Erase content of file if it already exists */
597 track_nb < movie->num_htk + movie->num_stk + movie->num_vtk;
599 mj2_tk_t *tk = &movie->tk[track_nb];
600 if (tk->track_type != 0) {
601 cio_seek(box.init_pos);
602 cio_skip(box.length);
604 fprintf(stderr, "Track %d: Width=%d Height=%d\n", track_nb,
606 fprintf(stderr, "%d Samples\n", tk->num_samples);
607 if ((tk->imagefile == (char *) 0xcdcdcdcd)
608 || (movie->tk->imagefile == NULL)) {
609 tk->imagefile = outfile;
611 for (i = 0; i < tk->num_samples; i++) {
613 mj2_sample_t *sample = &tk->sample[i];
618 fprintf(stderr, "Frame %d: \n", i);
620 cio_init(src + sample->offset, 8);
622 jp2c_cio_len = cio_tell();
623 jp2c_len = cio_read(4);
626 if (MJ2_JP2C != cio_read(4)) {
627 fprintf(stderr, "Error: Expected JP2C Marker\n");
631 pos = src + sample->offset + 8;
633 cio_seek(sample->offset + 8);
635 if (!j2k_decode(pos, sample->sample_size, &img, &cp))
638 if (imagetoyuv(&img, &cp, tk->imagefile))
643 if (cio_tell() + 8 != jp2c_len) {
644 fprintf(stderr, "Error with JP2C Box Size\n");
652 cio_seek(box.init_pos);
653 cio_skip(box.length); /* Go back to box end */
664 void mj2_write_stco(mj2_tk_t * tk)
669 box.init_pos = cio_tell();
671 cio_write(MJ2_STCO, 4); /* STCO */
673 cio_write(0, 4); /* Version = 0, flags = 0 */
675 cio_write(tk->num_chunks, 4); /* Entry Count */
677 for (i = 0; i < tk->num_chunks; i++) {
678 cio_write(tk->chunk[i].offset, 4); /* Entry offset */
681 box.length = cio_tell() - box.init_pos;
682 cio_seek(box.init_pos);
683 cio_write(box.length, 4); /* L */
684 cio_seek(box.init_pos + box.length);
693 int mj2_read_stco(mj2_tk_t * tk)
698 mj2_read_boxhdr(&box); /* Box Size */
699 if (MJ2_STCO != box.type) {
700 fprintf(stderr, "Error: Expected STCO Marker\n");
704 if (0 != cio_read(1)) { /* Version = 0 */
705 fprintf(stderr, "Error: Only Version 0 handled in STCO box\n");
709 if (0 != cio_read(3)) { /* Flags = 0 */
710 fprintf(stderr, "Error with flag in STCO box. Expected flag 0\n");
715 if (cio_read(4) != tk->num_chunks) {
717 "Error in STCO box: expecting same amount of entry-count as chunks \n");
719 for (i = 0; i < tk->num_chunks; i++) {
720 tk->chunk[i].offset = cio_read(4); /* Entry offset */
724 mj2_stco_decompact(tk);
727 if (cio_tell() - box.init_pos != box.length) {
728 fprintf(stderr, "Error with STCO Box size\n");
740 void mj2_write_stsz(mj2_tk_t * tk)
745 box.init_pos = cio_tell();
747 cio_write(MJ2_STSZ, 4); /* STSZ */
749 cio_write(0, 4); /* Version = 0, flags = 0 */
751 if (tk->same_sample_size == 1) { /* If they all have the same size */
752 cio_write(tk->sample[0].sample_size, 4); /* Size */
754 cio_write(1, 4); /* Entry count = 1 */
758 cio_write(0, 4); /* Sample Size = 0 becase they all have different sizes */
760 cio_write(tk->num_samples, 4); /* Sample Count */
762 for (i = 0; i < tk->num_samples; i++) {
763 cio_write(tk->sample[i].sample_size, 4);
767 box.length = cio_tell() - box.init_pos;
768 cio_seek(box.init_pos);
769 cio_write(box.length, 4); /* L */
770 cio_seek(box.init_pos + box.length);
779 int mj2_read_stsz(mj2_tk_t * tk)
785 mj2_read_boxhdr(&box); /* Box Size */
786 if (MJ2_STSZ != box.type) {
787 fprintf(stderr, "Error: Expected STSZ Marker\n");
792 if (0 != cio_read(1)) { /* Version = 0 */
793 fprintf(stderr, "Error: Only Version 0 handled in STSZ box\n");
797 if (0 != cio_read(3)) { /* Flags = 0 */
798 fprintf(stderr, "Error with flag in STSZ box. Expected flag 0\n");
802 sample_size = cio_read(4);
804 if (sample_size != 0) { /* Samples do have the same size */
805 tk->same_sample_size = 1;
806 for (i = 0; i < tk->num_samples; i++) {
807 tk->sample[i].sample_size = sample_size;
809 cio_skip(4); /* Sample count = 1 */
811 tk->same_sample_size = 0;
812 if (tk->num_samples != cio_read(4)) { /* Sample count */
814 "Error in STSZ box. Expected that sample-count is number of samples in track\n");
817 for (i = 0; i < tk->num_samples; i++) {
818 tk->sample[i].sample_size = cio_read(4); /* Sample Size */
821 if (cio_tell() - box.init_pos != box.length) {
822 fprintf(stderr, "Error with STSZ Box size\n");
836 void mj2_write_stsc(mj2_tk_t * tk)
841 box.init_pos = cio_tell();
843 cio_write(MJ2_STSC, 4); /* STSC */
845 cio_write(0, 4); /* Version = 0, flags = 0 */
847 cio_write(tk->num_samplestochunk, 4); /* Entry Count */
849 for (i = 0; i < tk->num_samplestochunk; i++) {
850 cio_write(tk->sampletochunk[i].first_chunk, 4); /* First Chunk */
851 cio_write(tk->sampletochunk[i].samples_per_chunk, 4); /* Samples per chunk */
852 cio_write(tk->sampletochunk[i].sample_descr_idx, 4); /* Samples description index */
856 box.length = cio_tell() - box.init_pos;
857 cio_seek(box.init_pos);
858 cio_write(box.length, 4); /* L */
859 cio_seek(box.init_pos + box.length);
868 int mj2_read_stsc(mj2_tk_t * tk)
873 mj2_read_boxhdr(&box); /* Box Size */
874 if (MJ2_STSC != box.type) {
875 fprintf(stderr, "Error: Expected STSC Marker\n");
880 if (0 != cio_read(1)) { /* Version = 0 */
881 fprintf(stderr, "Error: Only Version 0 handled in STSC box\n");
885 if (0 != cio_read(3)) { /* Flags = 0 */
886 fprintf(stderr, "Error with flag in STSC box. Expected flag 0\n");
890 tk->num_samplestochunk = cio_read(4);
893 (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
894 sizeof(mj2_sampletochunk_t));
897 for (i = 0; i < tk->num_samplestochunk; i++) {
898 tk->sampletochunk[i].first_chunk = cio_read(4);
899 tk->sampletochunk[i].samples_per_chunk = cio_read(4);
900 tk->sampletochunk[i].sample_descr_idx = cio_read(4);
903 mj2_stsc_decompact(tk); /* decompact sample to chunk box */
906 if (cio_tell() - box.init_pos != box.length) {
907 fprintf(stderr, "Error with STSC Box size\n");
919 void mj2_write_stts(mj2_tk_t * tk)
925 box.init_pos = cio_tell();
927 cio_write(MJ2_STTS, 4); /* STTS */
929 cio_write(0, 4); /* Version = 0, flags = 0 */
931 cio_write(tk->num_tts, 4); /* entry_count */
932 for (i = 0; i < tk->num_tts; i++) {
933 cio_write(tk->tts[i].sample_count, 4); /* Sample-count */
934 cio_write(tk->tts[i].sample_delta, 4); /* Sample-Delta */
937 box.length = cio_tell() - box.init_pos;
938 cio_seek(box.init_pos);
939 cio_write(box.length, 4); /* L */
940 cio_seek(box.init_pos + box.length);
949 int mj2_read_stts(mj2_tk_t * tk)
955 mj2_read_boxhdr(&box);
956 if (MJ2_STTS != box.type) {
957 fprintf(stderr, "Error: Expected STTS Marker\n");
962 if (0 != cio_read(1)) { /* Version = 0 */
963 fprintf(stderr, "Error: Only Version 0 handled in STTS box\n");
967 if (0 != cio_read(3)) { /* Flags = 0 */
968 fprintf(stderr, "Error with flag in STTS box. Expected flag 0\n");
972 tk->num_tts = cio_read(4);
974 tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
976 for (i = 0; i < tk->num_tts; i++) {
977 tk->tts[i].sample_count = cio_read(4);
978 tk->tts[i].sample_delta = cio_read(4);
981 mj2_tts_decompact(tk);
983 if (cio_tell() - box.init_pos != box.length) {
984 fprintf(stderr, "Error with STTS Box size\n");
996 void mj2_write_fiel(mj2_tk_t * tk)
1001 box.init_pos = cio_tell();
1003 cio_write(MJ2_FIEL, 4); /* STTS */
1005 cio_write(tk->fieldcount, 1); /* Field count */
1006 cio_write(tk->fieldorder, 1); /* Field order */
1009 box.length = cio_tell() - box.init_pos;
1010 cio_seek(box.init_pos);
1011 cio_write(box.length, 4); /* L */
1012 cio_seek(box.init_pos + box.length);
1021 int mj2_read_fiel(mj2_tk_t * tk)
1026 mj2_read_boxhdr(&box);
1027 if (MJ2_FIEL != box.type) {
1028 fprintf(stderr, "Error: Expected FIEL Marker\n");
1033 tk->fieldcount = cio_read(1);
1034 tk->fieldorder = cio_read(1);
1036 if (cio_tell() - box.init_pos != box.length) {
1037 fprintf(stderr, "Error with FIEL Box size\n");
1044 * Write the ORFO box
1046 * Original Format Box
1049 void mj2_write_orfo(mj2_tk_t * tk)
1053 box.init_pos = cio_tell();
1055 cio_write(MJ2_ORFO, 4);
1057 cio_write(tk->or_fieldcount, 1); /* Original Field count */
1058 cio_write(tk->or_fieldorder, 1); /* Original Field order */
1061 box.length = cio_tell() - box.init_pos;
1062 cio_seek(box.init_pos);
1063 cio_write(box.length, 4); /* L */
1064 cio_seek(box.init_pos + box.length);
1070 * Original Format Box
1073 int mj2_read_orfo(mj2_tk_t * tk)
1078 mj2_read_boxhdr(&box);
1079 if (MJ2_ORFO != box.type) {
1080 fprintf(stderr, "Error: Expected ORFO Marker\n");
1085 tk->or_fieldcount = cio_read(1);
1086 tk->or_fieldorder = cio_read(1);
1088 if (cio_tell() - box.init_pos != box.length) {
1089 fprintf(stderr, "Error with ORFO Box size\n");
1096 * Write the JP2P box
1101 void mj2_write_jp2p(mj2_tk_t * tk)
1107 box.init_pos = cio_tell();
1109 cio_write(MJ2_JP2P, 4);
1111 cio_write(0, 4); /* Version 0, flags =0 */
1113 for (i = 0; i < tk->num_br; i++) {
1114 cio_write(tk->br[i], 4);
1117 box.length = cio_tell() - box.init_pos;
1118 cio_seek(box.init_pos);
1119 cio_write(box.length, 4); /* L */
1120 cio_seek(box.init_pos + box.length);
1129 int mj2_read_jp2p(mj2_tk_t * tk)
1135 mj2_read_boxhdr(&box);
1136 if (MJ2_JP2P != box.type) {
1137 fprintf(stderr, "Error: Expected JP2P Marker\n");
1141 if (0 != cio_read(1)) { /* Version = 0 */
1142 fprintf(stderr, "Error: Only Version 0 handled in JP2P box\n");
1146 if (0 != cio_read(3)) { /* Flags = 0 */
1147 fprintf(stderr, "Error with flag in JP2P box. Expected flag 0\n");
1152 tk->num_br = (box.length - 12) / 4;
1153 tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
1155 for (i = 0; i < tk->num_br; i++) {
1156 tk->br[i] = cio_read(4);
1159 if (cio_tell() - box.init_pos != box.length) {
1160 fprintf(stderr, "Error with JP2P Box size\n");
1167 * Write the JP2X box
1172 void mj2_write_jp2x(mj2_tk_t * tk)
1178 box.init_pos = cio_tell();
1180 cio_write(MJ2_JP2X, 4);
1182 for (i = 0; i < tk->num_jp2x; i++) {
1183 cio_write(tk->jp2xdata[i], 1);
1186 box.length = cio_tell() - box.init_pos;
1187 cio_seek(box.init_pos);
1188 cio_write(box.length, 4); /* L */
1189 cio_seek(box.init_pos + box.length);
1198 int mj2_read_jp2x(mj2_tk_t * tk)
1204 mj2_read_boxhdr(&box);
1205 if (MJ2_JP2X != box.type) {
1206 fprintf(stderr, "Error: Expected JP2X Marker\n");
1211 tk->num_jp2x = (box.length - 8);
1213 (unsigned char *) malloc(tk->num_jp2x * sizeof(unsigned char));
1215 for (i = 0; i < tk->num_jp2x; i++) {
1216 tk->jp2xdata[i] = cio_read(1);
1219 if (cio_tell() - box.init_pos != box.length) {
1220 fprintf(stderr, "Error with JP2X Box size\n");
1227 * Write the JSUB box
1229 * MJP2 Subsampling Box
1232 void mj2_write_jsub(mj2_tk_t * tk)
1237 box.init_pos = cio_tell();
1239 cio_write(MJ2_JSUB, 4);
1241 cio_write(tk->hsub, 1);
1242 cio_write(tk->vsub, 1);
1243 cio_write(tk->hoff, 1);
1244 cio_write(tk->voff, 1);
1246 box.length = cio_tell() - box.init_pos;
1247 cio_seek(box.init_pos);
1248 cio_write(box.length, 4); /* L */
1249 cio_seek(box.init_pos + box.length);
1255 * MJP2 Subsampling Box
1258 int mj2_read_jsub(mj2_tk_t * tk)
1262 mj2_read_boxhdr(&box);
1263 if (MJ2_JSUB != box.type) {
1264 fprintf(stderr, "Error: Expected JSUB Marker\n");
1268 tk->hsub = cio_read(1);
1269 tk->vsub = cio_read(1);
1270 tk->hoff = cio_read(1);;
1271 tk->voff = cio_read(1);
1273 if (cio_tell() - box.init_pos != box.length) {
1274 fprintf(stderr, "Error with JSUB Box size\n");
1281 * Write the SMJ2 box
1283 * Visual Sample Entry Description
1286 void mj2_write_smj2(j2k_image_t * img, mj2_tk_t * tk)
1291 box.init_pos = cio_tell();
1293 cio_write(MJ2_MJ2, 4); /* MJ2 */
1295 cio_write(0, 4); /* Version = 0, flags = 0 */
1299 cio_write(0, 2); /* Pre-defined */
1301 cio_write(0, 2); /* Reserved */
1303 cio_write(0, 4); /* Pre-defined */
1304 cio_write(0, 4); /* Pre-defined */
1305 cio_write(0, 4); /* Pre-defined */
1307 cio_write(tk->w, 2); /* Width */
1308 cio_write(tk->h, 2); /* Height */
1310 cio_write(tk->horizresolution, 4); /* Horizontal resolution */
1311 cio_write(tk->vertresolution, 4); /* Vertical resolution */
1313 cio_write(0, 4); /* Reserved */
1315 cio_write(1, 2); /* Pre-defined = 1 */
1317 cio_write(tk->compressorname[0], 4); /* Compressor Name */
1318 cio_write(tk->compressorname[1], 4);
1319 cio_write(tk->compressorname[2], 4);
1320 cio_write(tk->compressorname[3], 4);
1321 cio_write(tk->compressorname[4], 4);
1322 cio_write(tk->compressorname[5], 4);
1323 cio_write(tk->compressorname[6], 4);
1324 cio_write(tk->compressorname[7], 4);
1328 for (i = 0; i < img->numcomps; i++)
1329 tk->depth += img->comps[i].bpp;
1331 cio_write(tk->depth, 2); /* Depth */
1333 cio_write(0xffff, 2); /* Pre-defined = -1 */
1335 jp2_init_stdjp2(&tk->jp2_struct, img);
1337 jp2_write_jp2h(&tk->jp2_struct);
1341 if (tk->num_br != 0)
1343 if (tk->num_jp2x != 0)
1349 box.length = cio_tell() - box.init_pos;
1350 cio_seek(box.init_pos);
1351 cio_write(box.length, 4); /* L */
1352 cio_seek(box.init_pos + box.length);
1358 * Visual Sample Entry Description
1361 int mj2_read_smj2(j2k_image_t * img, mj2_tk_t * tk)
1367 mj2_read_boxhdr(&box);
1369 if (MJ2_MJ2 != box.type) {
1370 fprintf(stderr, "Error in SMJ2 box: Expected MJ2 Marker\n");
1374 if (0 != cio_read(1)) { /* Version = 0 */
1375 fprintf(stderr, "Error: Only Version 0 handled in MJP2 box\n");
1379 if (0 != cio_read(3)) { /* Flags = 0 */
1380 fprintf(stderr, "Error with flag in MJP2 box. Expected flag 0\n");
1386 cio_skip(2); /* Pre-defined */
1388 cio_skip(2); /* Reserved */
1390 cio_skip(4); /* Pre-defined */
1391 cio_skip(4); /* Pre-defined */
1392 cio_skip(4); /* Pre-defined */
1394 tk->w = cio_read(2); /* Width */
1395 tk->h = cio_read(2); /* Height */
1397 tk->horizresolution = cio_read(4); /* Horizontal resolution */
1398 tk->vertresolution = cio_read(4); /* Vertical resolution */
1400 cio_skip(4); /* Reserved */
1402 cio_skip(2); /* Pre-defined = 1 */
1404 tk->compressorname[0] = cio_read(4); /* Compressor Name */
1405 tk->compressorname[1] = cio_read(4);
1406 tk->compressorname[2] = cio_read(4);
1407 tk->compressorname[3] = cio_read(4);
1408 tk->compressorname[4] = cio_read(4);
1409 tk->compressorname[5] = cio_read(4);
1410 tk->compressorname[6] = cio_read(4);
1411 tk->compressorname[7] = cio_read(4);
1413 tk->depth = cio_read(2); /* Depth */
1415 /* Init std value */
1419 tk->or_fieldcount = 1;
1420 tk->or_fieldorder = 0;
1422 cio_skip(2); /* Pre-defined = -1 */
1424 if (jp2_read_jp2h(&tk->jp2_struct)) {
1425 fprintf(stderr, "Error with JP2H Box\n");
1429 for (i = 0; cio_tell() - box.init_pos < box.length; i++) {
1430 mj2_read_boxhdr(&box2);
1431 cio_seek(box2.init_pos);
1432 switch (box2.type) {
1434 if (mj2_read_fiel(tk))
1439 if (mj2_read_jp2p(tk))
1444 if (mj2_read_jp2x(tk))
1449 if (mj2_read_jsub(tk))
1454 if (mj2_read_orfo(tk))
1459 fprintf(stderr, "Error with MJP2 Box size\n");
1470 * Write the STSD box
1472 * Sample Description
1475 void mj2_write_stsd(mj2_tk_t * tk, j2k_image_t * img)
1479 box.init_pos = cio_tell();
1481 cio_write(MJ2_STSD, 4); /* STSD */
1483 cio_write(0, 4); /* Version = 0, flags = 0 */
1485 cio_write(1, 4); /* entry_count = 1 (considering same JP2 headerboxes) */
1487 if (tk->track_type == 0) {
1488 mj2_write_smj2(img, tk);
1489 } else if (tk->track_type == 1) {
1492 if (tk->track_type == 2) {
1497 box.length = cio_tell() - box.init_pos;
1498 cio_seek(box.init_pos);
1499 cio_write(box.length, 4); /* L */
1500 cio_seek(box.init_pos + box.length);
1506 * Sample Description
1509 int mj2_read_stsd(mj2_tk_t * tk, j2k_image_t * img)
1512 int entry_count, len_2skip;
1516 mj2_read_boxhdr(&box);
1518 if (MJ2_STSD != box.type) {
1519 fprintf(stderr, "Error: Expected STSD Marker\n");
1523 if (0 != cio_read(1)) { /* Version = 0 */
1524 fprintf(stderr, "Error: Only Version 0 handled in STSD box\n");
1528 if (0 != cio_read(3)) { /* Flags = 0 */
1529 fprintf(stderr, "Error with flag in STSD box. Expected flag 0\n");
1533 entry_count = cio_read(4);
1535 if (tk->track_type == 0) {
1536 for (i = 0; i < entry_count; i++) {
1537 if (mj2_read_smj2(img, tk))
1540 } else if (tk->track_type == 1) {
1541 len_2skip = cio_read(4); // Not implemented -> skipping box
1542 cio_skip(len_2skip - 4);
1543 } else if (tk->track_type == 2) {
1544 len_2skip = cio_read(4); // Not implemented -> skipping box
1545 cio_skip(len_2skip - 4);
1549 if (cio_tell() - box.init_pos != box.length) {
1550 fprintf(stderr, "Error with STSD Box size\n");
1557 * Write the STBL box
1559 * Sample table box box
1562 void mj2_write_stbl(mj2_tk_t * tk, j2k_image_t * img)
1566 box.init_pos = cio_tell();
1568 cio_write(MJ2_STBL, 4); /* STBL */
1570 mj2_write_stsd(tk, img);
1576 box.length = cio_tell() - box.init_pos;
1577 cio_seek(box.init_pos);
1578 cio_write(box.length, 4); /* L */
1579 cio_seek(box.init_pos + box.length);
1585 * Sample table box box
1588 int mj2_read_stbl(mj2_tk_t * tk, j2k_image_t * img)
1592 mj2_read_boxhdr(&box);
1593 if (MJ2_STBL != box.type) {
1594 fprintf(stderr, "Error: Expected STBL Marker\n");
1598 if (mj2_read_stsd(tk, img))
1600 if (mj2_read_stts(tk))
1602 if (mj2_read_stsc(tk))
1604 if (mj2_read_stsz(tk))
1606 if (mj2_read_stco(tk))
1609 if (cio_tell() - box.init_pos != box.length) {
1610 fprintf(stderr, "Error with STBL Box size\n");
1622 void mj2_write_url(mj2_tk_t * tk, int url_num)
1626 box.init_pos = cio_tell();
1628 cio_write(MJ2_URL, 4); /* URL */
1631 cio_write(1, 4); /* Version = 0, flags = 1 because stored in same file */
1633 cio_write(0, 4); /* Version = 0, flags = 0 */
1634 cio_write(tk->url[url_num - 1].location[0], 4);
1635 cio_write(tk->url[url_num - 1].location[1], 4);
1636 cio_write(tk->url[url_num - 1].location[2], 4);
1637 cio_write(tk->url[url_num - 1].location[3], 4);
1640 box.length = cio_tell() - box.init_pos;
1641 cio_seek(box.init_pos);
1642 cio_write(box.length, 4); /* L */
1643 cio_seek(box.init_pos + box.length);
1652 int mj2_read_url(mj2_tk_t * tk, int urn_num)
1656 mj2_read_boxhdr(&box);
1657 if (MJ2_URL != box.type) {
1658 fprintf(stderr, "Error: Expected URL Marker\n");
1662 if (0 != cio_read(1)) { /* Version = 0 */
1663 fprintf(stderr, "Error: Only Version 0 handled in URL box\n");
1667 if (1 != cio_read(3)) { /* If flags = 1 --> media data in file */
1668 tk->url[urn_num].location[0] = cio_read(4);
1669 tk->url[urn_num].location[1] = cio_read(4);
1670 tk->url[urn_num].location[2] = cio_read(4);
1671 tk->url[urn_num].location[3] = cio_read(4);
1677 if (cio_tell() - box.init_pos != box.length) {
1678 fprintf(stderr, "Error with URL Box size\n");
1690 void mj2_write_urn(mj2_tk_t * tk, int urn_num)
1694 box.init_pos = cio_tell();
1696 cio_write(MJ2_URN, 4); /* URN */
1698 cio_write(0, 4); /* Version = 0, flags = 0 */
1700 cio_write(tk->urn[urn_num].name[0], 4);
1701 cio_write(tk->urn[urn_num].name[1], 4);
1702 cio_write(tk->urn[urn_num].name[2], 4);
1703 cio_write(tk->urn[urn_num].name[3], 4);
1704 cio_write(tk->urn[urn_num].location[0], 4);
1705 cio_write(tk->urn[urn_num].location[1], 4);
1706 cio_write(tk->urn[urn_num].location[2], 4);
1707 cio_write(tk->urn[urn_num].location[3], 4);
1709 box.length = cio_tell() - box.init_pos;
1710 cio_seek(box.init_pos);
1711 cio_write(box.length, 4); /* L */
1712 cio_seek(box.init_pos + box.length);
1721 int mj2_read_urn(mj2_tk_t * tk, int urn_num)
1726 mj2_read_boxhdr(&box);
1727 if (MJ2_URN != box.type) {
1728 fprintf(stderr, "Error: Expected URN Marker\n");
1732 if (0 != cio_read(1)) { /* Version = 0 */
1733 fprintf(stderr, "Error: Only Version 0 handled in URN box\n");
1737 if (1 != cio_read(3)) { /* If flags = 1 --> media data in file */
1738 tk->urn[urn_num].name[0] = cio_read(4);
1739 tk->urn[urn_num].name[1] = cio_read(4);
1740 tk->urn[urn_num].name[2] = cio_read(4);
1741 tk->urn[urn_num].name[3] = cio_read(4);
1742 tk->urn[urn_num].location[0] = cio_read(4);
1743 tk->urn[urn_num].location[1] = cio_read(4);
1744 tk->urn[urn_num].location[2] = cio_read(4);
1745 tk->urn[urn_num].location[3] = cio_read(4);
1749 if (cio_tell() - box.init_pos != box.length) {
1750 fprintf(stderr, "Error with URN Box size\n");
1758 * Write the DREF box
1760 * Data reference box
1763 void mj2_write_dref(mj2_tk_t * tk)
1768 box.init_pos = cio_tell();
1770 cio_write(MJ2_DREF, 4); /* DREF */
1772 cio_write(0, 4); /* Version = 0, flags = 0 */
1774 if (tk->num_url + tk->num_urn == 0) { /* Media data in same file */
1775 cio_write(1, 4); /* entry_count = 1 */
1776 mj2_write_url(tk, 0);
1778 cio_write(tk->num_url + tk->num_urn, 4); /* entry_count */
1780 for (i = 0; i < tk->num_url; i++)
1781 mj2_write_url(tk, i + 1);
1783 for (i = 0; i < tk->num_urn; i++)
1784 mj2_write_urn(tk, i);
1787 box.length = cio_tell() - box.init_pos;
1788 cio_seek(box.init_pos);
1789 cio_write(box.length, 4); /* L */
1790 cio_seek(box.init_pos + box.length);
1796 * Data reference box
1799 int mj2_read_dref(mj2_tk_t * tk)
1803 int entry_count, marker;
1806 mj2_read_boxhdr(&box);
1807 if (MJ2_DREF != box.type) {
1808 fprintf(stderr, "Error: Expected DREF Marker\n");
1812 if (0 != cio_read(1)) { /* Version = 0 */
1813 fprintf(stderr, "Error: Only Version 0 handled in DREF box\n");
1817 if (0 != cio_read(3)) { /* Flags = 0 */
1818 fprintf(stderr, "Error with flag in DREF box. Expected flag 0\n");
1822 entry_count = cio_read(4);
1826 for (i = 0; i < entry_count; i++) {
1828 marker = cio_read(4);
1829 if (marker == MJ2_URL) {
1832 if (mj2_read_url(tk, tk->num_url))
1834 } else if (marker == MJ2_URN) {
1837 if (mj2_read_urn(tk, tk->num_urn))
1840 fprintf(stderr, "Error with in DREF box. Expected URN or URL box\n");
1847 if (cio_tell() - box.init_pos != box.length) {
1848 fprintf(stderr, "Error with DREF Box size\n");
1855 * Write the DINF box
1857 * Data information box
1860 void mj2_write_dinf(mj2_tk_t * tk)
1864 box.init_pos = cio_tell();
1866 cio_write(MJ2_DINF, 4); /* DINF */
1870 box.length = cio_tell() - box.init_pos;
1871 cio_seek(box.init_pos);
1872 cio_write(box.length, 4); /* L */
1873 cio_seek(box.init_pos + box.length);
1879 * Data information box
1882 int mj2_read_dinf(mj2_tk_t * tk)
1886 mj2_read_boxhdr(&box);
1887 if (MJ2_DINF != box.type) {
1888 fprintf(stderr, "Error: Expected DINF Marker\n");
1892 if (mj2_read_dref(tk))
1895 if (cio_tell() - box.init_pos != box.length) {
1896 fprintf(stderr, "Error with DINF Box size\n");
1903 * Write the VMHD box
1905 * Video Media information box
1908 void mj2_write_vmhd(mj2_tk_t * tk)
1912 box.init_pos = cio_tell();
1914 cio_write(MJ2_VMHD, 4); /* VMHD */
1916 cio_write(1, 4); /* Version = 0, flags = 1 */
1918 cio_write(tk->graphicsmode, 2);
1919 cio_write(tk->opcolor[0], 2);
1920 cio_write(tk->opcolor[1], 2);
1921 cio_write(tk->opcolor[2], 2);
1923 box.length = cio_tell() - box.init_pos;
1924 cio_seek(box.init_pos);
1925 cio_write(box.length, 4); /* L */
1926 cio_seek(box.init_pos + box.length);
1932 * Video Media information box
1935 int mj2_read_vmhd(mj2_tk_t * tk)
1939 mj2_read_boxhdr(&box);
1940 if (MJ2_VMHD != box.type) {
1941 fprintf(stderr, "Error: Expected VMHD Marker\n");
1945 if (0 != cio_read(1)) { /* Version = 0 */
1946 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
1950 if (1 != cio_read(3)) { /* Flags = 1 */
1951 fprintf(stderr, "Error with flag in VMHD box. Expected flag 1\n");
1956 tk->graphicsmode = cio_read(2);
1957 tk->opcolor[0] = cio_read(2);
1958 tk->opcolor[1] = cio_read(2);
1959 tk->opcolor[2] = cio_read(2);
1961 if (cio_tell() - box.init_pos != box.length) {
1962 fprintf(stderr, "Error with VMHD Box size\n");
1969 * Write the SMHD box
1971 * Sound Media information box
1974 void mj2_write_smhd(mj2_tk_t * tk)
1978 box.init_pos = cio_tell();
1980 cio_write(MJ2_SMHD, 4); /* SMHD */
1982 cio_write(0, 4); /* Version = 0, flags = 0 */
1984 cio_write(tk->balance << 8, 2);
1986 cio_write(0, 2); /* Reserved */
1988 box.length = cio_tell() - box.init_pos;
1989 cio_seek(box.init_pos);
1990 cio_write(box.length, 4); /* L */
1991 cio_seek(box.init_pos + box.length);
1997 * Sound Media information box
2000 int mj2_read_smhd(mj2_tk_t * tk)
2004 mj2_read_boxhdr(&box);
2005 if (MJ2_SMHD != box.type) {
2006 fprintf(stderr, "Error: Expected SMHD Marker\n");
2010 if (0 != cio_read(1)) { /* Version = 0 */
2011 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
2015 if (0 != cio_read(3)) { /* Flags = 0 */
2016 fprintf(stderr, "Error with flag in VMHD box. Expected flag 0\n");
2021 tk->balance = cio_read(2) >> 8;
2023 cio_skip(2); /* Reserved */
2025 if (cio_tell() - box.init_pos != box.length) {
2026 fprintf(stderr, "Error with SMHD Box size\n");
2033 * Write the HMHD box
2035 * Hint Media information box
2038 void mj2_write_hmhd(mj2_tk_t * tk)
2042 box.init_pos = cio_tell();
2044 cio_write(MJ2_HMHD, 4); /* HMHD */
2046 cio_write(0, 4); /* Version = 0, flags = 0 */
2048 cio_write(tk->maxPDUsize, 2);
2049 cio_write(tk->avgPDUsize, 2);
2050 cio_write(tk->maxbitrate, 4);
2051 cio_write(tk->avgbitrate, 4);
2052 cio_write(tk->slidingavgbitrate, 4);
2054 box.length = cio_tell() - box.init_pos;
2055 cio_seek(box.init_pos);
2056 cio_write(box.length, 4); /* L */
2057 cio_seek(box.init_pos + box.length);
2063 * Hint Media information box
2066 int mj2_read_hmhd(mj2_tk_t * tk)
2070 mj2_read_boxhdr(&box);
2071 if (MJ2_HMHD != box.type) {
2072 fprintf(stderr, "Error: Expected HMHD Marker\n");
2076 if (0 != cio_read(1)) { /* Version = 0 */
2077 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
2081 if (0 != cio_read(3)) { /* Flags = 0 */
2082 fprintf(stderr, "Error with flag in VMHD box. Expected flag 0\n");
2087 tk->maxPDUsize = cio_read(2);
2088 tk->avgPDUsize = cio_read(2);
2089 tk->maxbitrate = cio_read(4);
2090 tk->avgbitrate = cio_read(4);
2091 tk->slidingavgbitrate = cio_read(4);
2093 if (cio_tell() - box.init_pos != box.length) {
2094 fprintf(stderr, "Error with HMHD Box size\n");
2101 * Write the MINF box
2103 * Media information box
2106 void mj2_write_minf(mj2_tk_t * tk, j2k_image_t * img)
2110 box.init_pos = cio_tell();
2112 cio_write(MJ2_MINF, 4); /* MINF */
2114 if (tk->track_type == 0) {
2116 } else if (tk->track_type == 1) {
2118 } else if (tk->track_type == 2) {
2123 mj2_write_stbl(tk, img);
2125 box.length = cio_tell() - box.init_pos;
2126 cio_seek(box.init_pos);
2127 cio_write(box.length, 4); /* L */
2128 cio_seek(box.init_pos + box.length);
2134 * Media information box
2137 int mj2_read_minf(mj2_tk_t * tk, j2k_image_t * img)
2140 unsigned int box_type;
2143 mj2_read_boxhdr(&box);
2144 if (MJ2_MINF != box.type) {
2145 fprintf(stderr, "Error: Expected MINF Marker\n");
2150 box_type = cio_read(4);
2153 if (box_type == MJ2_VMHD) {
2154 if (mj2_read_vmhd(tk))
2156 } else if (box_type == MJ2_SMHD) {
2157 if (mj2_read_smhd(tk))
2159 } else if (box_type == MJ2_HMHD) {
2160 if (mj2_read_hmhd(tk))
2163 fprintf(stderr, "Error in MINF box expected vmhd, smhd or hmhd\n");
2167 if (mj2_read_dinf(tk))
2170 if (mj2_read_stbl(tk, img))
2173 if (cio_tell() - box.init_pos != box.length) {
2174 fprintf(stderr, "Error with MINF Box size\n");
2181 * Write the HDLR box
2183 * Handler reference box
2186 void mj2_write_hdlr(mj2_tk_t * tk)
2190 box.init_pos = cio_tell();
2192 cio_write(MJ2_HDLR, 4); /* HDLR */
2194 cio_write(0, 4); /* Version = 0, flags = 0 */
2196 cio_write(0, 4); /* Predefine */
2198 tk->name = 0; /* The track name is immediately determined by the track type */
2200 if (tk->track_type == 0) {
2201 tk->handler_type = 0x76696465; /* Handler type: vide */
2202 cio_write(tk->handler_type, 4);
2206 cio_write(0, 4); /* Reserved */
2208 cio_write(0x76696465, 4);
2209 cio_write(0x6F206d65, 4);
2210 cio_write(0x64696120, 4);
2211 cio_write(0x74726163, 4);
2212 cio_write(0x6b00, 2); /* String: video media track */
2213 } else if (tk->track_type == 1) {
2214 tk->handler_type = 0x736F756E; /* Handler type: soun */
2215 cio_write(tk->handler_type, 4);
2219 cio_write(0, 4); /* Reserved */
2221 cio_write(0x536F756E, 4);
2222 cio_write(0x6400, 2); /* String: Sound */
2223 } else if (tk->track_type == 2) {
2224 tk->handler_type = 0x68696E74; /* Handler type: hint */
2225 cio_write(tk->handler_type, 4);
2229 cio_write(0, 4); /* Reserved */
2231 cio_write(0x48696E74, 4);
2232 cio_write(0, 2); /* String: Hint */
2235 box.length = cio_tell() - box.init_pos;
2236 cio_seek(box.init_pos);
2237 cio_write(box.length, 4); /* L */
2238 cio_seek(box.init_pos + box.length);
2244 * Handler reference box
2247 int mj2_read_hdlr(mj2_tk_t * tk)
2252 mj2_read_boxhdr(&box);
2253 if (MJ2_HDLR != box.type) {
2254 fprintf(stderr, "Error: Expected HDLR Marker\n");
2259 if (0 != cio_read(1)) { /* Version = 0 */
2260 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
2264 if (0 != cio_read(3)) { /* Flags = 0 */
2265 fprintf(stderr, "Error with flag in VMHD box. Expected flag 0\n");
2269 cio_skip(4); /* Reserved */
2271 tk->handler_type = cio_read(4);
2272 cio_skip(12); /* Reserved */
2274 tk->name_size = box.length - 32;
2276 tk->name = (char *) malloc(tk->name_size * sizeof(char));
2277 for (i = 0; i < tk->name_size; i++) {
2278 tk->name[i] = cio_read(1); /* Name */
2281 if (cio_tell() - box.init_pos != box.length) {
2282 fprintf(stderr, "Error with HDLR Box size\n");
2289 * Write the MDHD box
2294 void mj2_write_mdhd(mj2_tk_t * tk)
2299 unsigned int modification_time;
2301 box.init_pos = cio_tell();
2303 cio_write(MJ2_MDHD, 4); /* MDHD */
2305 cio_write(0, 4); /* Version = 0, flags = 0 */
2307 cio_write(tk->creation_time, 4); /* Creation Time */
2309 time(<ime); /* Time since 1/1/70 */
2310 modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2312 cio_write(modification_time, 4); /* Modification Time */
2314 cio_write(tk->timescale, 4); /* Timescale */
2318 for (i = 0; i < tk->num_samples; i++)
2319 tk->duration += tk->sample[i].sample_delta;
2321 cio_write(tk->duration, 4); /* Duration */
2323 cio_write(tk->language, 2); /* Language */
2325 cio_write(0, 2); /* Predefined */
2327 box.length = cio_tell() - box.init_pos;
2328 cio_seek(box.init_pos);
2329 cio_write(box.length, 4); /* L */
2330 cio_seek(box.init_pos + box.length);
2339 int mj2_read_mdhd(mj2_tk_t * tk)
2343 mj2_read_boxhdr(&box);
2344 if (!(MJ2_MHDR == box.type || MJ2_MDHD == box.type)) { // Kakadu writes MHDR instead of MDHD
2345 fprintf(stderr, "Error: Expected MDHD Marker\n");
2349 if (0 != cio_read(1)) { /* Version = 0 */
2350 fprintf(stderr, "Error: Only Version 0 handled in MDHD box\n");
2354 if (0 != cio_read(3)) { /* Flags = 0 */
2355 fprintf(stderr, "Error with flag in MDHD box. Expected flag 0\n");
2360 tk->creation_time = cio_read(4); /* Creation Time */
2362 tk->modification_time = cio_read(4); /* Modification Time */
2364 tk->timescale = cio_read(4); /* Timescale */
2366 tk->duration = cio_read(4); /* Duration */
2368 tk->language = cio_read(2); /* Language */
2370 cio_skip(2); /* Predefined */
2372 if (cio_tell() - box.init_pos != box.length) {
2373 fprintf(stderr, "Error with MDHD Box size\n");
2380 * Write the MDIA box
2385 void mj2_write_mdia(mj2_tk_t * tk, j2k_image_t * img)
2389 box.init_pos = cio_tell();
2391 cio_write(MJ2_MDIA, 4); /* MDIA */
2395 mj2_write_minf(tk, img);
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_mdia(mj2_tk_t * tk, j2k_image_t * img)
2413 mj2_read_boxhdr(&box);
2414 if (MJ2_MDIA != box.type) {
2415 fprintf(stderr, "Error: Expected MDIA Marker\n");
2419 if (mj2_read_mdhd(tk))
2421 if (mj2_read_hdlr(tk))
2423 if (mj2_read_minf(tk, img))
2426 if (cio_tell() - box.init_pos != box.length) {
2427 fprintf(stderr, "Error with MDIA Box size\n");
2434 * Write the TKHD box
2439 void mj2_write_tkhd(mj2_tk_t * tk)
2445 box.init_pos = cio_tell();
2448 cio_write(MJ2_TKHD, 4); /* TKHD */
2450 cio_write(3, 4); /* Version=0, flags=3 */
2452 time(<ime); /* Time since 1/1/70 */
2453 tk->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2455 cio_write(tk->creation_time, 4); /* Creation Time */
2457 cio_write(tk->modification_time, 4); /* Modification Time */
2459 cio_write(tk->track_ID, 4); /* Track ID */
2461 cio_write(0, 4); /* Reserved */
2465 for (i = 0; i < tk->num_samples; i++)
2466 tk->duration += tk->sample[i].sample_delta;
2468 cio_write(tk->duration, 4); /* Duration */
2470 cio_write(0, 4); /* Reserved */
2471 cio_write(0, 4); /* Reserved */
2473 cio_write(tk->layer, 2); /* Layer */
2475 cio_write(0, 2); /* Predefined */
2477 cio_write(tk->volume << 8, 2); /* Volume */
2479 cio_write(0, 2); /* Reserved */
2481 cio_write(tk->trans_matrix[0], 4); /* Transformation matrix for track */
2482 cio_write(tk->trans_matrix[1], 4);
2483 cio_write(tk->trans_matrix[2], 4);
2484 cio_write(tk->trans_matrix[3], 4);
2485 cio_write(tk->trans_matrix[4], 4);
2486 cio_write(tk->trans_matrix[5], 4);
2487 cio_write(tk->trans_matrix[6], 4);
2488 cio_write(tk->trans_matrix[7], 4);
2489 cio_write(tk->trans_matrix[8], 4);
2491 cio_write(tk->w << 16, 4); /* Video Width */
2493 cio_write(tk->h << 16, 4); /* Video Height */
2495 box.length = cio_tell() - box.init_pos;
2496 cio_seek(box.init_pos);
2497 cio_write(box.length, 4); /* L */
2498 cio_seek(box.init_pos + box.length);
2507 int mj2_read_tkhd(mj2_tk_t * tk)
2513 mj2_read_boxhdr(&box);
2515 if (MJ2_TKHD != box.type) {
2516 fprintf(stderr, "Error: Expected TKHD Marker\n");
2520 if (0 != cio_read(1)) { /* Version = 0 */
2521 fprintf(stderr, "Error: Only Version 0 handled in TKHD box\n");
2527 if (!(flag == 1 || flag == 2 || flag == 3 || flag == 4)) { /* Flags = 1,2,3 or 4 */
2529 "Error with flag in TKHD box: Expected flag 1,2,3 or 4\n");
2533 tk->creation_time = cio_read(4); /* Creation Time */
2535 tk->modification_time = cio_read(4); /* Modification Time */
2537 tk->track_ID = cio_read(4); /* Track ID */
2539 cio_skip(4); /* Reserved */
2541 tk->duration = cio_read(4); /* Duration */
2543 cio_skip(8); /* Reserved */
2545 tk->layer = cio_read(2); /* Layer */
2547 cio_read(2); /* Predefined */
2549 tk->volume = cio_read(2) >> 8; /* Volume */
2551 cio_skip(2); /* Reserved */
2553 tk->trans_matrix[0] = cio_read(4); /* Transformation matrix for track */
2554 tk->trans_matrix[1] = cio_read(4);
2555 tk->trans_matrix[2] = cio_read(4);
2556 tk->trans_matrix[3] = cio_read(4);
2557 tk->trans_matrix[4] = cio_read(4);
2558 tk->trans_matrix[5] = cio_read(4);
2559 tk->trans_matrix[6] = cio_read(4);
2560 tk->trans_matrix[7] = cio_read(4);
2561 tk->trans_matrix[8] = cio_read(4);
2563 tk->w = cio_read(4) >> 16; /* Video Width */
2565 tk->h = cio_read(4) >> 16; /* Video Height */
2567 if (cio_tell() - box.init_pos != box.length) {
2568 fprintf(stderr, "Error with TKHD Box size\n");
2575 * Write the TRAK box
2580 void mj2_write_trak(mj2_tk_t * tk, j2k_image_t * img)
2584 box.init_pos = cio_tell();
2587 cio_write(MJ2_TRAK, 4); /* TRAK */
2590 mj2_write_mdia(tk, img);
2592 box.length = cio_tell() - box.init_pos;
2593 cio_seek(box.init_pos);
2594 cio_write(box.length, 4); /* L */
2595 cio_seek(box.init_pos + box.length);
2604 int mj2_read_trak(mj2_tk_t * tk, j2k_image_t * img)
2608 mj2_read_boxhdr(&box);
2609 if (MJ2_TRAK != box.type) {
2610 fprintf(stderr, "Error: Expected TRAK Marker\n");
2613 if (mj2_read_tkhd(tk))
2615 if (mj2_read_mdia(tk, img))
2617 if (cio_tell() - box.init_pos != box.length) {
2618 fprintf(stderr, "Error with TRAK Box\n");
2625 * Write the MVHD box
2630 void mj2_write_mvhd(mj2_movie_t * movie)
2638 box.init_pos = cio_tell();
2640 cio_write(MJ2_MVHD, 4); /* MVHD */
2642 cio_write(0, 4); /* Version = 0, flags = 0 */
2644 time(<ime); /* Time since 1/1/70 */
2645 movie->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2647 cio_write(movie->creation_time, 4); /* Creation Time */
2649 cio_write(movie->modification_time, 4); /* Modification Time */
2651 cio_write(movie->timescale, 4); /* Timescale */
2653 movie->duration = 0;
2655 for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
2656 mj2_tk_t *tk = &movie->tk[i];
2658 for (j = 0; j < tk->num_samples; j++) {
2659 movie->duration += tk->sample[j].sample_delta;
2663 cio_write(movie->duration, 4);
2665 cio_write(movie->rate << 16, 4); /* Rate to play presentation */
2667 cio_write(movie->volume << 8, 2); /* Volume */
2669 cio_write(0, 2); /* Reserved */
2670 cio_write(0, 4); /* Reserved */
2671 cio_write(0, 4); /* Reserved */
2673 cio_write(movie->trans_matrix[0], 4); /* Transformation matrix for video */
2674 cio_write(movie->trans_matrix[1], 4);
2675 cio_write(movie->trans_matrix[2], 4);
2676 cio_write(movie->trans_matrix[3], 4);
2677 cio_write(movie->trans_matrix[4], 4);
2678 cio_write(movie->trans_matrix[5], 4);
2679 cio_write(movie->trans_matrix[6], 4);
2680 cio_write(movie->trans_matrix[7], 4);
2681 cio_write(movie->trans_matrix[8], 4);
2683 cio_write(0, 4); /* Pre-defined */
2684 cio_write(0, 4); /* Pre-defined */
2685 cio_write(0, 4); /* Pre-defined */
2686 cio_write(0, 4); /* Pre-defined */
2687 cio_write(0, 4); /* Pre-defined */
2688 cio_write(0, 4); /* Pre-defined */
2691 for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) {
2692 if (max_tk_num < movie->tk[i].track_ID)
2693 max_tk_num = movie->tk[i].track_ID;
2696 movie->next_tk_id = max_tk_num + 1;
2698 cio_write(movie->next_tk_id, 4); /* ID of Next track to be added */
2700 box.length = cio_tell() - box.init_pos;
2701 cio_seek(box.init_pos);
2702 cio_write(box.length, 4); /* L */
2703 cio_seek(box.init_pos + box.length);
2712 int mj2_read_mvhd(mj2_movie_t * movie)
2716 mj2_read_boxhdr(&box);
2717 if (MJ2_MVHD != box.type) {
2718 fprintf(stderr, "Error: Expected MVHD Marker\n");
2723 if (0 != cio_read(4)) { /* Version = 0, flags = 0 */
2724 fprintf(stderr, "Error: Only Version 0 handled\n");
2727 movie->creation_time = cio_read(4); /* Creation Time */
2729 movie->modification_time = cio_read(4); /* Modification Time */
2731 movie->timescale = cio_read(4); /* Timescale */
2733 movie->duration = cio_read(4); /* Duration */
2735 movie->rate = cio_read(4) >> 16; /* Rate to play presentation */
2737 movie->volume = cio_read(2) >> 8; /* Volume */
2739 cio_skip(10); /* Reserved */
2741 movie->trans_matrix[0] = cio_read(4); /* Transformation matrix for video */
2742 movie->trans_matrix[1] = cio_read(4);
2743 movie->trans_matrix[2] = cio_read(4);
2744 movie->trans_matrix[3] = cio_read(4);
2745 movie->trans_matrix[4] = cio_read(4);
2746 movie->trans_matrix[5] = cio_read(4);
2747 movie->trans_matrix[6] = cio_read(4);
2748 movie->trans_matrix[7] = cio_read(4);
2749 movie->trans_matrix[8] = cio_read(4);
2751 cio_skip(24); /* Pre-defined */
2753 movie->next_tk_id = cio_read(4); /* ID of Next track to be added */
2755 if (cio_tell() - box.init_pos != box.length) {
2756 fprintf(stderr, "Error with MVHD Box Size\n");
2764 * Write the MOOV box
2769 void mj2_write_moov(mj2_movie_t * movie, j2k_image_t * img)
2774 box.init_pos = cio_tell();
2776 cio_write(MJ2_MOOV, 4); /* MOOV */
2778 mj2_write_mvhd(movie);
2780 for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
2781 mj2_write_trak(&movie->tk[i], img);
2784 box.length = cio_tell() - box.init_pos;
2785 cio_seek(box.init_pos);
2786 cio_write(box.length, 4); /* L */
2787 cio_seek(box.init_pos + box.length);
2797 int mj2_read_moov(mj2_movie_t * movie, j2k_image_t * img)
2803 mj2_read_boxhdr(&box);
2804 if (MJ2_MOOV != box.type) {
2805 fprintf(stderr, "Error: Expected MOOV Marker\n");
2811 if (mj2_read_mvhd(movie))
2816 (mj2_tk_t *) malloc((movie->next_tk_id - 1) * sizeof(mj2_tk_t));
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");