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 */
472 for (i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++) {
473 if (movie->tk[i].track_type != 0) {
474 fprintf(stderr, "Unable to write sound or hint tracks\n");
481 fprintf(stderr, "Video Track number %d\n", i + 1);
484 fwrite(outbuf, 1, len, outfile);
485 pos_correction = cio_tell() + pos_correction;
488 /* Copy the first tile coding parameters (tcp) to cp_init */
491 (j2k_tcp_t *) malloc(cp->tw * cp->th * sizeof(j2k_tcp_t));
492 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
493 for (l = 0; l < cp->tcps[tileno].numlayers; l++) {
494 cp_init.tcps[tileno].rates[l] = cp->tcps[tileno].rates[l];
495 //tileno = cp->tcps[tileno].rates[l];
500 for (j = 0; j < tk->num_samples; j++) {
501 outbuf = (char *) malloc(cp->tdx * cp->tdy * cp->th * cp->tw * 2);
502 cio_init(outbuf, cp->tdx * cp->tdy * cp->th * cp->tw * 2);
504 fprintf(stderr, "Frame number %d/%d: \n", j + 1, tk->num_samples);
507 if (!yuvtoimage(tk, img, j)) {
508 fprintf(stderr, "Error with frame number %d in YUV file\n", j);
512 len = jp2_write_jp2c(img, cp, outbuf, index);
514 for (m = 0; m < img->numcomps; m++) {
515 free(img->comps[m].data);
518 tk->sample[j].sample_size = len;
520 tk->sample[j].offset = pos_correction;
521 tk->chunk[j].offset = pos_correction; /* There is one sample per chunk */
523 fwrite(outbuf, 1, len, outfile);
525 pos_correction = cio_tell() + pos_correction;
529 /* Copy the cp_init parameters to cp->tcps */
531 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
532 for (k = 0; k < cp->tcps[tileno].numlayers; k++) {
533 cp->tcps[tileno].rates[k] = cp_init.tcps[tileno].rates[k];
539 box.length = pos_correction - box.init_pos;
541 fseek(outfile, box.init_pos, SEEK_SET);
543 cio_init(&box_len_ptr, 4); /* Init a cio to write box length variable in a little endian way */
544 cio_write(box.length, 4);
546 fwrite(&box_len_ptr, 4, 1, outfile);
548 fseek(outfile, box.init_pos + box.length, SEEK_SET);
560 int mj2_read_mdat(mj2_movie_t * movie, unsigned char *src, char *outfile)
564 int jp2c_cio_len, jp2c_len, pos_correction = 0;
569 mj2_read_boxhdr(&box);
570 if (MJ2_MDAT != box.type) {
571 fprintf(stderr, "Error: Expected MDAT Marker\n");
575 pos_correction = cio_tell() - box.init_pos;
577 f = fopen(outfile, "w"); /* Erase content of file if it already exists */
580 for (track_nb = 0; track_nb < movie->next_tk_id - 1; track_nb++) {
581 if ((movie->tk->imagefile != (char *) 0xcdcdcdcd)
582 && (movie->tk->imagefile != NULL)) {
583 fprintf(stderr, "%s", movie->tk[track_nb].imagefile);
585 f = fopen(movie->tk->imagefile, "w"); /* Erase content of file if it already exists */
592 track_nb < movie->num_htk + movie->num_stk + movie->num_vtk;
594 mj2_tk_t *tk = &movie->tk[track_nb];
595 if (tk->track_type != 0) {
596 cio_seek(box.init_pos);
597 cio_skip(box.length);
599 fprintf(stderr, "Track %d: Width=%d Height=%d\n", track_nb,
601 fprintf(stderr, "%d Samples\n", tk->num_samples);
602 if ((tk->imagefile == (char *) 0xcdcdcdcd)
603 || (movie->tk->imagefile == NULL)) {
604 tk->imagefile = outfile;
606 for (i = 0; i < tk->num_samples; i++) {
608 mj2_sample_t *sample = &tk->sample[i];
613 fprintf(stderr, "Frame %d: \n", i);
615 cio_init(src + sample->offset, 8);
617 jp2c_cio_len = cio_tell();
618 jp2c_len = cio_read(4);
621 if (MJ2_JP2C != cio_read(4)) {
622 fprintf(stderr, "Error: Expected JP2C Marker\n");
626 pos = src + sample->offset + 8;
628 cio_seek(sample->offset + 8);
630 if (!j2k_decode(pos, sample->sample_size, &img, &cp))
633 if (imagetoyuv(&img, &cp, tk->imagefile))
638 if (cio_tell() + 8 != jp2c_len) {
639 fprintf(stderr, "Error with JP2C Box Size\n");
647 cio_seek(box.init_pos);
648 cio_skip(box.length); /* Go back to box end */
659 void mj2_write_stco(mj2_tk_t * tk)
664 box.init_pos = cio_tell();
666 cio_write(MJ2_STCO, 4); /* STCO */
668 cio_write(0, 4); /* Version = 0, flags = 0 */
670 cio_write(tk->num_chunks, 4); /* Entry Count */
672 for (i = 0; i < tk->num_chunks; i++) {
673 cio_write(tk->chunk[i].offset, 4); /* Entry offset */
676 box.length = cio_tell() - box.init_pos;
677 cio_seek(box.init_pos);
678 cio_write(box.length, 4); /* L */
679 cio_seek(box.init_pos + box.length);
688 int mj2_read_stco(mj2_tk_t * tk)
693 mj2_read_boxhdr(&box); /* Box Size */
694 if (MJ2_STCO != box.type) {
695 fprintf(stderr, "Error: Expected STCO Marker\n");
699 if (0 != cio_read(1)) { /* Version = 0 */
700 fprintf(stderr, "Error: Only Version 0 handled in STCO box\n");
704 if (0 != cio_read(3)) { /* Flags = 0 */
705 fprintf(stderr, "Error with flag in STCO box. Expected flag 0\n");
710 if (cio_read(4) != tk->num_chunks) {
712 "Error in STCO box: expecting same amount of entry-count as chunks \n");
714 for (i = 0; i < tk->num_chunks; i++) {
715 tk->chunk[i].offset = cio_read(4); /* Entry offset */
719 mj2_stco_decompact(tk);
722 if (cio_tell() - box.init_pos != box.length) {
723 fprintf(stderr, "Error with STCO Box size\n");
735 void mj2_write_stsz(mj2_tk_t * tk)
740 box.init_pos = cio_tell();
742 cio_write(MJ2_STSZ, 4); /* STSZ */
744 cio_write(0, 4); /* Version = 0, flags = 0 */
746 if (tk->same_sample_size == 1) { /* If they all have the same size */
747 cio_write(tk->sample[0].sample_size, 4); /* Size */
749 cio_write(1, 4); /* Entry count = 1 */
753 cio_write(0, 4); /* Sample Size = 0 becase they all have different sizes */
755 cio_write(tk->num_samples, 4); /* Sample Count */
757 for (i = 0; i < tk->num_samples; i++) {
758 cio_write(tk->sample[i].sample_size, 4);
762 box.length = cio_tell() - box.init_pos;
763 cio_seek(box.init_pos);
764 cio_write(box.length, 4); /* L */
765 cio_seek(box.init_pos + box.length);
774 int mj2_read_stsz(mj2_tk_t * tk)
780 mj2_read_boxhdr(&box); /* Box Size */
781 if (MJ2_STSZ != box.type) {
782 fprintf(stderr, "Error: Expected STSZ Marker\n");
787 if (0 != cio_read(1)) { /* Version = 0 */
788 fprintf(stderr, "Error: Only Version 0 handled in STSZ box\n");
792 if (0 != cio_read(3)) { /* Flags = 0 */
793 fprintf(stderr, "Error with flag in STSZ box. Expected flag 0\n");
797 sample_size = cio_read(4);
799 if (sample_size != 0) { /* Samples do have the same size */
800 tk->same_sample_size = 1;
801 for (i = 0; i < tk->num_samples; i++) {
802 tk->sample[i].sample_size = sample_size;
804 cio_skip(4); /* Sample count = 1 */
806 tk->same_sample_size = 0;
807 if (tk->num_samples != cio_read(4)) { /* Sample count */
809 "Error in STSZ box. Expected that sample-count is number of samples in track\n");
812 for (i = 0; i < tk->num_samples; i++) {
813 tk->sample[i].sample_size = cio_read(4); /* Sample Size */
816 if (cio_tell() - box.init_pos != box.length) {
817 fprintf(stderr, "Error with STSZ Box size\n");
831 void mj2_write_stsc(mj2_tk_t * tk)
836 box.init_pos = cio_tell();
838 cio_write(MJ2_STSC, 4); /* STSC */
840 cio_write(0, 4); /* Version = 0, flags = 0 */
842 cio_write(tk->num_samplestochunk, 4); /* Entry Count */
844 for (i = 0; i < tk->num_samplestochunk; i++) {
845 cio_write(tk->sampletochunk[i].first_chunk, 4); /* First Chunk */
846 cio_write(tk->sampletochunk[i].samples_per_chunk, 4); /* Samples per chunk */
847 cio_write(tk->sampletochunk[i].sample_descr_idx, 4); /* Samples description index */
851 box.length = cio_tell() - box.init_pos;
852 cio_seek(box.init_pos);
853 cio_write(box.length, 4); /* L */
854 cio_seek(box.init_pos + box.length);
863 int mj2_read_stsc(mj2_tk_t * tk)
868 mj2_read_boxhdr(&box); /* Box Size */
869 if (MJ2_STSC != box.type) {
870 fprintf(stderr, "Error: Expected STSC Marker\n");
875 if (0 != cio_read(1)) { /* Version = 0 */
876 fprintf(stderr, "Error: Only Version 0 handled in STSC box\n");
880 if (0 != cio_read(3)) { /* Flags = 0 */
881 fprintf(stderr, "Error with flag in STSC box. Expected flag 0\n");
885 tk->num_samplestochunk = cio_read(4);
888 (mj2_sampletochunk_t *) malloc(tk->num_samplestochunk *
889 sizeof(mj2_sampletochunk_t));
892 for (i = 0; i < tk->num_samplestochunk; i++) {
893 tk->sampletochunk[i].first_chunk = cio_read(4);
894 tk->sampletochunk[i].samples_per_chunk = cio_read(4);
895 tk->sampletochunk[i].sample_descr_idx = cio_read(4);
898 mj2_stsc_decompact(tk); /* decompact sample to chunk box */
901 if (cio_tell() - box.init_pos != box.length) {
902 fprintf(stderr, "Error with STSC Box size\n");
914 void mj2_write_stts(mj2_tk_t * tk)
920 box.init_pos = cio_tell();
922 cio_write(MJ2_STTS, 4); /* STTS */
924 cio_write(0, 4); /* Version = 0, flags = 0 */
926 cio_write(tk->num_tts, 4); /* entry_count */
927 for (i = 0; i < tk->num_tts; i++) {
928 cio_write(tk->tts[i].sample_count, 4); /* Sample-count */
929 cio_write(tk->tts[i].sample_delta, 4); /* Sample-Delta */
932 box.length = cio_tell() - box.init_pos;
933 cio_seek(box.init_pos);
934 cio_write(box.length, 4); /* L */
935 cio_seek(box.init_pos + box.length);
944 int mj2_read_stts(mj2_tk_t * tk)
950 mj2_read_boxhdr(&box);
951 if (MJ2_STTS != box.type) {
952 fprintf(stderr, "Error: Expected STTS Marker\n");
957 if (0 != cio_read(1)) { /* Version = 0 */
958 fprintf(stderr, "Error: Only Version 0 handled in STTS box\n");
962 if (0 != cio_read(3)) { /* Flags = 0 */
963 fprintf(stderr, "Error with flag in STTS box. Expected flag 0\n");
967 tk->num_tts = cio_read(4);
969 tk->tts = (mj2_tts_t *) malloc(tk->num_tts * sizeof(mj2_tts_t));
971 for (i = 0; i < tk->num_tts; i++) {
972 tk->tts[i].sample_count = cio_read(4);
973 tk->tts[i].sample_delta = cio_read(4);
976 mj2_tts_decompact(tk);
978 if (cio_tell() - box.init_pos != box.length) {
979 fprintf(stderr, "Error with STTS Box size\n");
991 void mj2_write_fiel(mj2_tk_t * tk)
996 box.init_pos = cio_tell();
998 cio_write(MJ2_FIEL, 4); /* STTS */
1000 cio_write(tk->fieldcount, 1); /* Field count */
1001 cio_write(tk->fieldorder, 1); /* Field order */
1004 box.length = cio_tell() - box.init_pos;
1005 cio_seek(box.init_pos);
1006 cio_write(box.length, 4); /* L */
1007 cio_seek(box.init_pos + box.length);
1016 int mj2_read_fiel(mj2_tk_t * tk)
1021 mj2_read_boxhdr(&box);
1022 if (MJ2_FIEL != box.type) {
1023 fprintf(stderr, "Error: Expected FIEL Marker\n");
1028 tk->fieldcount = cio_read(1);
1029 tk->fieldorder = cio_read(1);
1031 if (cio_tell() - box.init_pos != box.length) {
1032 fprintf(stderr, "Error with FIEL Box size\n");
1039 * Write the ORFO box
1041 * Original Format Box
1044 void mj2_write_orfo(mj2_tk_t * tk)
1048 box.init_pos = cio_tell();
1050 cio_write(MJ2_ORFO, 4);
1052 cio_write(tk->or_fieldcount, 1); /* Original Field count */
1053 cio_write(tk->or_fieldorder, 1); /* Original Field order */
1056 box.length = cio_tell() - box.init_pos;
1057 cio_seek(box.init_pos);
1058 cio_write(box.length, 4); /* L */
1059 cio_seek(box.init_pos + box.length);
1065 * Original Format Box
1068 int mj2_read_orfo(mj2_tk_t * tk)
1073 mj2_read_boxhdr(&box);
1074 if (MJ2_ORFO != box.type) {
1075 fprintf(stderr, "Error: Expected ORFO Marker\n");
1080 tk->or_fieldcount = cio_read(1);
1081 tk->or_fieldorder = cio_read(1);
1083 if (cio_tell() - box.init_pos != box.length) {
1084 fprintf(stderr, "Error with ORFO Box size\n");
1091 * Write the JP2P box
1096 void mj2_write_jp2p(mj2_tk_t * tk)
1102 box.init_pos = cio_tell();
1104 cio_write(MJ2_JP2P, 4);
1106 cio_write(0, 4); /* Version 0, flags =0 */
1108 for (i = 0; i < tk->num_br; i++) {
1109 cio_write(tk->br[i], 4);
1112 box.length = cio_tell() - box.init_pos;
1113 cio_seek(box.init_pos);
1114 cio_write(box.length, 4); /* L */
1115 cio_seek(box.init_pos + box.length);
1124 int mj2_read_jp2p(mj2_tk_t * tk)
1130 mj2_read_boxhdr(&box);
1131 if (MJ2_JP2P != box.type) {
1132 fprintf(stderr, "Error: Expected JP2P Marker\n");
1136 if (0 != cio_read(1)) { /* Version = 0 */
1137 fprintf(stderr, "Error: Only Version 0 handled in JP2P box\n");
1141 if (0 != cio_read(3)) { /* Flags = 0 */
1142 fprintf(stderr, "Error with flag in JP2P box. Expected flag 0\n");
1147 tk->num_br = (box.length - 12) / 4;
1148 tk->br = (unsigned int *) malloc(tk->num_br * sizeof(unsigned int));
1150 for (i = 0; i < tk->num_br; i++) {
1151 tk->br[i] = cio_read(4);
1154 if (cio_tell() - box.init_pos != box.length) {
1155 fprintf(stderr, "Error with JP2P Box size\n");
1162 * Write the JP2X box
1167 void mj2_write_jp2x(mj2_tk_t * tk)
1173 box.init_pos = cio_tell();
1175 cio_write(MJ2_JP2X, 4);
1177 for (i = 0; i < tk->num_jp2x; i++) {
1178 cio_write(tk->jp2xdata[i], 1);
1181 box.length = cio_tell() - box.init_pos;
1182 cio_seek(box.init_pos);
1183 cio_write(box.length, 4); /* L */
1184 cio_seek(box.init_pos + box.length);
1193 int mj2_read_jp2x(mj2_tk_t * tk)
1199 mj2_read_boxhdr(&box);
1200 if (MJ2_JP2X != box.type) {
1201 fprintf(stderr, "Error: Expected JP2X Marker\n");
1206 tk->num_jp2x = (box.length - 8);
1208 (unsigned char *) malloc(tk->num_jp2x * sizeof(unsigned char));
1210 for (i = 0; i < tk->num_jp2x; i++) {
1211 tk->jp2xdata[i] = cio_read(1);
1214 if (cio_tell() - box.init_pos != box.length) {
1215 fprintf(stderr, "Error with JP2X Box size\n");
1222 * Write the JSUB box
1224 * MJP2 Subsampling Box
1227 void mj2_write_jsub(mj2_tk_t * tk)
1232 box.init_pos = cio_tell();
1234 cio_write(MJ2_JSUB, 4);
1236 cio_write(tk->hsub, 1);
1237 cio_write(tk->vsub, 1);
1238 cio_write(tk->hoff, 1);
1239 cio_write(tk->voff, 1);
1241 box.length = cio_tell() - box.init_pos;
1242 cio_seek(box.init_pos);
1243 cio_write(box.length, 4); /* L */
1244 cio_seek(box.init_pos + box.length);
1250 * MJP2 Subsampling Box
1253 int mj2_read_jsub(mj2_tk_t * tk)
1257 mj2_read_boxhdr(&box);
1258 if (MJ2_JSUB != box.type) {
1259 fprintf(stderr, "Error: Expected JSUB Marker\n");
1263 tk->hsub = cio_read(1);
1264 tk->vsub = cio_read(1);
1265 tk->hoff = cio_read(1);;
1266 tk->voff = cio_read(1);
1268 if (cio_tell() - box.init_pos != box.length) {
1269 fprintf(stderr, "Error with JSUB Box size\n");
1276 * Write the SMJ2 box
1278 * Visual Sample Entry Description
1281 void mj2_write_smj2(j2k_image_t * img, mj2_tk_t * tk)
1286 box.init_pos = cio_tell();
1288 cio_write(MJ2_MJ2, 4); /* MJ2 */
1290 cio_write(0, 4); /* Version = 0, flags = 0 */
1294 cio_write(0, 2); /* Pre-defined */
1296 cio_write(0, 2); /* Reserved */
1298 cio_write(0, 4); /* Pre-defined */
1299 cio_write(0, 4); /* Pre-defined */
1300 cio_write(0, 4); /* Pre-defined */
1302 cio_write(tk->w, 2); /* Width */
1303 cio_write(tk->h, 2); /* Height */
1305 cio_write(tk->horizresolution, 4); /* Horizontal resolution */
1306 cio_write(tk->vertresolution, 4); /* Vertical resolution */
1308 cio_write(0, 4); /* Reserved */
1310 cio_write(1, 2); /* Pre-defined = 1 */
1312 cio_write(tk->compressorname[0], 4); /* Compressor Name */
1313 cio_write(tk->compressorname[1], 4);
1314 cio_write(tk->compressorname[2], 4);
1315 cio_write(tk->compressorname[3], 4);
1316 cio_write(tk->compressorname[4], 4);
1317 cio_write(tk->compressorname[5], 4);
1318 cio_write(tk->compressorname[6], 4);
1319 cio_write(tk->compressorname[7], 4);
1323 for (i = 0; i < img->numcomps; i++)
1324 tk->depth += img->comps[i].bpp;
1326 cio_write(tk->depth, 2); /* Depth */
1328 cio_write(0xffff, 2); /* Pre-defined = -1 */
1330 jp2_init_stdjp2(&tk->jp2_struct, img);
1332 jp2_write_jp2h(&tk->jp2_struct);
1336 if (tk->num_br != 0)
1338 if (tk->num_jp2x != 0)
1344 box.length = cio_tell() - box.init_pos;
1345 cio_seek(box.init_pos);
1346 cio_write(box.length, 4); /* L */
1347 cio_seek(box.init_pos + box.length);
1353 * Visual Sample Entry Description
1356 int mj2_read_smj2(j2k_image_t * img, mj2_tk_t * tk)
1362 mj2_read_boxhdr(&box);
1364 if (MJ2_MJ2 != box.type) {
1365 fprintf(stderr, "Error in SMJ2 box: Expected MJ2 Marker\n");
1369 if (0 != cio_read(1)) { /* Version = 0 */
1370 fprintf(stderr, "Error: Only Version 0 handled in MJP2 box\n");
1374 if (0 != cio_read(3)) { /* Flags = 0 */
1375 fprintf(stderr, "Error with flag in MJP2 box. Expected flag 0\n");
1381 cio_skip(2); /* Pre-defined */
1383 cio_skip(2); /* Reserved */
1385 cio_skip(4); /* Pre-defined */
1386 cio_skip(4); /* Pre-defined */
1387 cio_skip(4); /* Pre-defined */
1389 tk->w = cio_read(2); /* Width */
1390 tk->h = cio_read(2); /* Height */
1392 tk->horizresolution = cio_read(4); /* Horizontal resolution */
1393 tk->vertresolution = cio_read(4); /* Vertical resolution */
1395 cio_skip(4); /* Reserved */
1397 cio_skip(2); /* Pre-defined = 1 */
1399 tk->compressorname[0] = cio_read(4); /* Compressor Name */
1400 tk->compressorname[1] = cio_read(4);
1401 tk->compressorname[2] = cio_read(4);
1402 tk->compressorname[3] = cio_read(4);
1403 tk->compressorname[4] = cio_read(4);
1404 tk->compressorname[5] = cio_read(4);
1405 tk->compressorname[6] = cio_read(4);
1406 tk->compressorname[7] = cio_read(4);
1408 tk->depth = cio_read(2); /* Depth */
1410 /* Init std value */
1414 tk->or_fieldcount = 1;
1415 tk->or_fieldorder = 0;
1417 cio_skip(2); /* Pre-defined = -1 */
1419 if (jp2_read_jp2h(&tk->jp2_struct)) {
1420 fprintf(stderr, "Error with JP2H Box\n");
1424 for (i = 0; cio_tell() - box.init_pos < box.length; i++) {
1425 mj2_read_boxhdr(&box2);
1426 cio_seek(box2.init_pos);
1427 switch (box2.type) {
1429 if (mj2_read_fiel(tk))
1434 if (mj2_read_jp2p(tk))
1439 if (mj2_read_jp2x(tk))
1444 if (mj2_read_jsub(tk))
1449 if (mj2_read_orfo(tk))
1454 fprintf(stderr, "Error with MJP2 Box size\n");
1465 * Write the STSD box
1467 * Sample Description
1470 void mj2_write_stsd(mj2_tk_t * tk, j2k_image_t * img)
1474 box.init_pos = cio_tell();
1476 cio_write(MJ2_STSD, 4); /* STSD */
1478 cio_write(0, 4); /* Version = 0, flags = 0 */
1480 cio_write(1, 4); /* entry_count = 1 (considering same JP2 headerboxes) */
1482 if (tk->track_type == 0) {
1483 mj2_write_smj2(img, tk);
1484 } else if (tk->track_type == 1) {
1487 if (tk->track_type == 2) {
1492 box.length = cio_tell() - box.init_pos;
1493 cio_seek(box.init_pos);
1494 cio_write(box.length, 4); /* L */
1495 cio_seek(box.init_pos + box.length);
1501 * Sample Description
1504 int mj2_read_stsd(mj2_tk_t * tk, j2k_image_t * img)
1507 int entry_count, len_2skip;
1511 mj2_read_boxhdr(&box);
1513 if (MJ2_STSD != box.type) {
1514 fprintf(stderr, "Error: Expected STSD Marker\n");
1518 if (0 != cio_read(1)) { /* Version = 0 */
1519 fprintf(stderr, "Error: Only Version 0 handled in STSD box\n");
1523 if (0 != cio_read(3)) { /* Flags = 0 */
1524 fprintf(stderr, "Error with flag in STSD box. Expected flag 0\n");
1528 entry_count = cio_read(4);
1530 if (tk->track_type == 0) {
1531 for (i = 0; i < entry_count; i++) {
1532 if (mj2_read_smj2(img, tk))
1535 } else if (tk->track_type == 1) {
1536 len_2skip = cio_read(4); // Not implemented -> skipping box
1537 cio_skip(len_2skip - 4);
1538 } else if (tk->track_type == 2) {
1539 len_2skip = cio_read(4); // Not implemented -> skipping box
1540 cio_skip(len_2skip - 4);
1544 if (cio_tell() - box.init_pos != box.length) {
1545 fprintf(stderr, "Error with STSD Box size\n");
1552 * Write the STBL box
1554 * Sample table box box
1557 void mj2_write_stbl(mj2_tk_t * tk, j2k_image_t * img)
1561 box.init_pos = cio_tell();
1563 cio_write(MJ2_STBL, 4); /* STBL */
1565 mj2_write_stsd(tk, img);
1571 box.length = cio_tell() - box.init_pos;
1572 cio_seek(box.init_pos);
1573 cio_write(box.length, 4); /* L */
1574 cio_seek(box.init_pos + box.length);
1580 * Sample table box box
1583 int mj2_read_stbl(mj2_tk_t * tk, j2k_image_t * img)
1587 mj2_read_boxhdr(&box);
1588 if (MJ2_STBL != box.type) {
1589 fprintf(stderr, "Error: Expected STBL Marker\n");
1593 if (mj2_read_stsd(tk, img))
1595 if (mj2_read_stts(tk))
1597 if (mj2_read_stsc(tk))
1599 if (mj2_read_stsz(tk))
1601 if (mj2_read_stco(tk))
1604 if (cio_tell() - box.init_pos != box.length) {
1605 fprintf(stderr, "Error with STBL Box size\n");
1617 void mj2_write_url(mj2_tk_t * tk, int url_num)
1621 box.init_pos = cio_tell();
1623 cio_write(MJ2_URL, 4); /* URL */
1626 cio_write(1, 4); /* Version = 0, flags = 1 because stored in same file */
1628 cio_write(0, 4); /* Version = 0, flags = 0 */
1629 cio_write(tk->url[url_num - 1].location[0], 4);
1630 cio_write(tk->url[url_num - 1].location[1], 4);
1631 cio_write(tk->url[url_num - 1].location[2], 4);
1632 cio_write(tk->url[url_num - 1].location[3], 4);
1635 box.length = cio_tell() - box.init_pos;
1636 cio_seek(box.init_pos);
1637 cio_write(box.length, 4); /* L */
1638 cio_seek(box.init_pos + box.length);
1647 int mj2_read_url(mj2_tk_t * tk, int urn_num)
1651 mj2_read_boxhdr(&box);
1652 if (MJ2_URL != box.type) {
1653 fprintf(stderr, "Error: Expected URL Marker\n");
1657 if (0 != cio_read(1)) { /* Version = 0 */
1658 fprintf(stderr, "Error: Only Version 0 handled in URL box\n");
1662 if (1 != cio_read(3)) { /* If flags = 1 --> media data in file */
1663 tk->url[urn_num].location[0] = cio_read(4);
1664 tk->url[urn_num].location[1] = cio_read(4);
1665 tk->url[urn_num].location[2] = cio_read(4);
1666 tk->url[urn_num].location[3] = cio_read(4);
1672 if (cio_tell() - box.init_pos != box.length) {
1673 fprintf(stderr, "Error with URL Box size\n");
1685 void mj2_write_urn(mj2_tk_t * tk, int urn_num)
1689 box.init_pos = cio_tell();
1691 cio_write(MJ2_URN, 4); /* URN */
1693 cio_write(0, 4); /* Version = 0, flags = 0 */
1695 cio_write(tk->urn[urn_num].name[0], 4);
1696 cio_write(tk->urn[urn_num].name[1], 4);
1697 cio_write(tk->urn[urn_num].name[2], 4);
1698 cio_write(tk->urn[urn_num].name[3], 4);
1699 cio_write(tk->urn[urn_num].location[0], 4);
1700 cio_write(tk->urn[urn_num].location[1], 4);
1701 cio_write(tk->urn[urn_num].location[2], 4);
1702 cio_write(tk->urn[urn_num].location[3], 4);
1704 box.length = cio_tell() - box.init_pos;
1705 cio_seek(box.init_pos);
1706 cio_write(box.length, 4); /* L */
1707 cio_seek(box.init_pos + box.length);
1716 int mj2_read_urn(mj2_tk_t * tk, int urn_num)
1721 mj2_read_boxhdr(&box);
1722 if (MJ2_URN != box.type) {
1723 fprintf(stderr, "Error: Expected URN Marker\n");
1727 if (0 != cio_read(1)) { /* Version = 0 */
1728 fprintf(stderr, "Error: Only Version 0 handled in URN box\n");
1732 if (1 != cio_read(3)) { /* If flags = 1 --> media data in file */
1733 tk->urn[urn_num].name[0] = cio_read(4);
1734 tk->urn[urn_num].name[1] = cio_read(4);
1735 tk->urn[urn_num].name[2] = cio_read(4);
1736 tk->urn[urn_num].name[3] = cio_read(4);
1737 tk->urn[urn_num].location[0] = cio_read(4);
1738 tk->urn[urn_num].location[1] = cio_read(4);
1739 tk->urn[urn_num].location[2] = cio_read(4);
1740 tk->urn[urn_num].location[3] = cio_read(4);
1744 if (cio_tell() - box.init_pos != box.length) {
1745 fprintf(stderr, "Error with URN Box size\n");
1753 * Write the DREF box
1755 * Data reference box
1758 void mj2_write_dref(mj2_tk_t * tk)
1763 box.init_pos = cio_tell();
1765 cio_write(MJ2_DREF, 4); /* DREF */
1767 cio_write(0, 4); /* Version = 0, flags = 0 */
1769 if (tk->num_url + tk->num_urn == 0) { /* Media data in same file */
1770 cio_write(1, 4); /* entry_count = 1 */
1771 mj2_write_url(tk, 0);
1773 cio_write(tk->num_url + tk->num_urn, 4); /* entry_count */
1775 for (i = 0; i < tk->num_url; i++)
1776 mj2_write_url(tk, i + 1);
1778 for (i = 0; i < tk->num_urn; i++)
1779 mj2_write_urn(tk, i);
1782 box.length = cio_tell() - box.init_pos;
1783 cio_seek(box.init_pos);
1784 cio_write(box.length, 4); /* L */
1785 cio_seek(box.init_pos + box.length);
1791 * Data reference box
1794 int mj2_read_dref(mj2_tk_t * tk)
1798 int entry_count, marker;
1801 mj2_read_boxhdr(&box);
1802 if (MJ2_DREF != box.type) {
1803 fprintf(stderr, "Error: Expected DREF Marker\n");
1807 if (0 != cio_read(1)) { /* Version = 0 */
1808 fprintf(stderr, "Error: Only Version 0 handled in DREF box\n");
1812 if (0 != cio_read(3)) { /* Flags = 0 */
1813 fprintf(stderr, "Error with flag in DREF box. Expected flag 0\n");
1817 entry_count = cio_read(4);
1821 for (i = 0; i < entry_count; i++) {
1823 marker = cio_read(4);
1824 if (marker == MJ2_URL) {
1827 if (mj2_read_url(tk, tk->num_url))
1829 } else if (marker == MJ2_URN) {
1832 if (mj2_read_urn(tk, tk->num_urn))
1835 fprintf(stderr, "Error with in DREF box. Expected URN or URL box\n");
1842 if (cio_tell() - box.init_pos != box.length) {
1843 fprintf(stderr, "Error with DREF Box size\n");
1850 * Write the DINF box
1852 * Data information box
1855 void mj2_write_dinf(mj2_tk_t * tk)
1859 box.init_pos = cio_tell();
1861 cio_write(MJ2_DINF, 4); /* DINF */
1865 box.length = cio_tell() - box.init_pos;
1866 cio_seek(box.init_pos);
1867 cio_write(box.length, 4); /* L */
1868 cio_seek(box.init_pos + box.length);
1874 * Data information box
1877 int mj2_read_dinf(mj2_tk_t * tk)
1881 mj2_read_boxhdr(&box);
1882 if (MJ2_DINF != box.type) {
1883 fprintf(stderr, "Error: Expected DINF Marker\n");
1887 if (mj2_read_dref(tk))
1890 if (cio_tell() - box.init_pos != box.length) {
1891 fprintf(stderr, "Error with DINF Box size\n");
1898 * Write the VMHD box
1900 * Video Media information box
1903 void mj2_write_vmhd(mj2_tk_t * tk)
1907 box.init_pos = cio_tell();
1909 cio_write(MJ2_VMHD, 4); /* VMHD */
1911 cio_write(1, 4); /* Version = 0, flags = 1 */
1913 cio_write(tk->graphicsmode, 2);
1914 cio_write(tk->opcolor[0], 2);
1915 cio_write(tk->opcolor[1], 2);
1916 cio_write(tk->opcolor[2], 2);
1918 box.length = cio_tell() - box.init_pos;
1919 cio_seek(box.init_pos);
1920 cio_write(box.length, 4); /* L */
1921 cio_seek(box.init_pos + box.length);
1927 * Video Media information box
1930 int mj2_read_vmhd(mj2_tk_t * tk)
1934 mj2_read_boxhdr(&box);
1935 if (MJ2_VMHD != box.type) {
1936 fprintf(stderr, "Error: Expected VMHD Marker\n");
1940 if (0 != cio_read(1)) { /* Version = 0 */
1941 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
1945 if (1 != cio_read(3)) { /* Flags = 1 */
1946 fprintf(stderr, "Error with flag in VMHD box. Expected flag 1\n");
1951 tk->graphicsmode = cio_read(2);
1952 tk->opcolor[0] = cio_read(2);
1953 tk->opcolor[1] = cio_read(2);
1954 tk->opcolor[2] = cio_read(2);
1956 if (cio_tell() - box.init_pos != box.length) {
1957 fprintf(stderr, "Error with VMHD Box size\n");
1964 * Write the SMHD box
1966 * Sound Media information box
1969 void mj2_write_smhd(mj2_tk_t * tk)
1973 box.init_pos = cio_tell();
1975 cio_write(MJ2_SMHD, 4); /* SMHD */
1977 cio_write(0, 4); /* Version = 0, flags = 0 */
1979 cio_write(tk->balance << 8, 2);
1981 cio_write(0, 2); /* Reserved */
1983 box.length = cio_tell() - box.init_pos;
1984 cio_seek(box.init_pos);
1985 cio_write(box.length, 4); /* L */
1986 cio_seek(box.init_pos + box.length);
1992 * Sound Media information box
1995 int mj2_read_smhd(mj2_tk_t * tk)
1999 mj2_read_boxhdr(&box);
2000 if (MJ2_SMHD != box.type) {
2001 fprintf(stderr, "Error: Expected SMHD Marker\n");
2005 if (0 != cio_read(1)) { /* Version = 0 */
2006 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
2010 if (0 != cio_read(3)) { /* Flags = 0 */
2011 fprintf(stderr, "Error with flag in VMHD box. Expected flag 0\n");
2016 tk->balance = cio_read(2) >> 8;
2018 cio_skip(2); /* Reserved */
2020 if (cio_tell() - box.init_pos != box.length) {
2021 fprintf(stderr, "Error with SMHD Box size\n");
2028 * Write the HMHD box
2030 * Hint Media information box
2033 void mj2_write_hmhd(mj2_tk_t * tk)
2037 box.init_pos = cio_tell();
2039 cio_write(MJ2_HMHD, 4); /* HMHD */
2041 cio_write(0, 4); /* Version = 0, flags = 0 */
2043 cio_write(tk->maxPDUsize, 2);
2044 cio_write(tk->avgPDUsize, 2);
2045 cio_write(tk->maxbitrate, 4);
2046 cio_write(tk->avgbitrate, 4);
2047 cio_write(tk->slidingavgbitrate, 4);
2049 box.length = cio_tell() - box.init_pos;
2050 cio_seek(box.init_pos);
2051 cio_write(box.length, 4); /* L */
2052 cio_seek(box.init_pos + box.length);
2058 * Hint Media information box
2061 int mj2_read_hmhd(mj2_tk_t * tk)
2065 mj2_read_boxhdr(&box);
2066 if (MJ2_HMHD != box.type) {
2067 fprintf(stderr, "Error: Expected HMHD Marker\n");
2071 if (0 != cio_read(1)) { /* Version = 0 */
2072 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
2076 if (0 != cio_read(3)) { /* Flags = 0 */
2077 fprintf(stderr, "Error with flag in VMHD box. Expected flag 0\n");
2082 tk->maxPDUsize = cio_read(2);
2083 tk->avgPDUsize = cio_read(2);
2084 tk->maxbitrate = cio_read(4);
2085 tk->avgbitrate = cio_read(4);
2086 tk->slidingavgbitrate = cio_read(4);
2088 if (cio_tell() - box.init_pos != box.length) {
2089 fprintf(stderr, "Error with HMHD Box size\n");
2096 * Write the MINF box
2098 * Media information box
2101 void mj2_write_minf(mj2_tk_t * tk, j2k_image_t * img)
2105 box.init_pos = cio_tell();
2107 cio_write(MJ2_MINF, 4); /* MINF */
2109 if (tk->track_type == 0) {
2111 } else if (tk->track_type == 1) {
2113 } else if (tk->track_type == 2) {
2118 mj2_write_stbl(tk, img);
2120 box.length = cio_tell() - box.init_pos;
2121 cio_seek(box.init_pos);
2122 cio_write(box.length, 4); /* L */
2123 cio_seek(box.init_pos + box.length);
2129 * Media information box
2132 int mj2_read_minf(mj2_tk_t * tk, j2k_image_t * img)
2135 unsigned int box_type;
2138 mj2_read_boxhdr(&box);
2139 if (MJ2_MINF != box.type) {
2140 fprintf(stderr, "Error: Expected MINF Marker\n");
2145 box_type = cio_read(4);
2148 if (box_type == MJ2_VMHD) {
2149 if (mj2_read_vmhd(tk))
2151 } else if (box_type == MJ2_SMHD) {
2152 if (mj2_read_smhd(tk))
2154 } else if (box_type == MJ2_HMHD) {
2155 if (mj2_read_hmhd(tk))
2158 fprintf(stderr, "Error in MINF box expected vmhd, smhd or hmhd\n");
2162 if (mj2_read_dinf(tk))
2165 if (mj2_read_stbl(tk, img))
2168 if (cio_tell() - box.init_pos != box.length) {
2169 fprintf(stderr, "Error with MINF Box size\n");
2176 * Write the HDLR box
2178 * Handler reference box
2181 void mj2_write_hdlr(mj2_tk_t * tk)
2185 box.init_pos = cio_tell();
2187 cio_write(MJ2_HDLR, 4); /* HDLR */
2189 cio_write(0, 4); /* Version = 0, flags = 0 */
2191 cio_write(0, 4); /* Predefine */
2193 tk->name = 0; /* The track name is immediately determined by the track type */
2195 if (tk->track_type == 0) {
2196 tk->handler_type = 0x76696465; /* Handler type: vide */
2197 cio_write(tk->handler_type, 4);
2201 cio_write(0, 4); /* Reserved */
2203 cio_write(0x76696465, 4);
2204 cio_write(0x6F206d65, 4);
2205 cio_write(0x64696120, 4);
2206 cio_write(0x74726163, 4);
2207 cio_write(0x6b00, 2); /* String: video media track */
2208 } else if (tk->track_type == 1) {
2209 tk->handler_type = 0x736F756E; /* Handler type: soun */
2210 cio_write(tk->handler_type, 4);
2214 cio_write(0, 4); /* Reserved */
2216 cio_write(0x536F756E, 4);
2217 cio_write(0x6400, 2); /* String: Sound */
2218 } else if (tk->track_type == 2) {
2219 tk->handler_type = 0x68696E74; /* Handler type: hint */
2220 cio_write(tk->handler_type, 4);
2224 cio_write(0, 4); /* Reserved */
2226 cio_write(0x48696E74, 4);
2227 cio_write(0, 2); /* String: Hint */
2230 box.length = cio_tell() - box.init_pos;
2231 cio_seek(box.init_pos);
2232 cio_write(box.length, 4); /* L */
2233 cio_seek(box.init_pos + box.length);
2239 * Handler reference box
2242 int mj2_read_hdlr(mj2_tk_t * tk)
2247 mj2_read_boxhdr(&box);
2248 if (MJ2_HDLR != box.type) {
2249 fprintf(stderr, "Error: Expected HDLR Marker\n");
2254 if (0 != cio_read(1)) { /* Version = 0 */
2255 fprintf(stderr, "Error: Only Version 0 handled in VMHD box\n");
2259 if (0 != cio_read(3)) { /* Flags = 0 */
2260 fprintf(stderr, "Error with flag in VMHD box. Expected flag 0\n");
2264 cio_skip(4); /* Reserved */
2266 tk->handler_type = cio_read(4);
2267 cio_skip(12); /* Reserved */
2269 tk->name_size = box.length - 32;
2271 tk->name = (char *) malloc(tk->name_size * sizeof(char));
2272 for (i = 0; i < tk->name_size; i++) {
2273 tk->name[i] = cio_read(1); /* Name */
2276 if (cio_tell() - box.init_pos != box.length) {
2277 fprintf(stderr, "Error with HDLR Box size\n");
2284 * Write the MDHD box
2289 void mj2_write_mdhd(mj2_tk_t * tk)
2294 unsigned int modification_time;
2296 box.init_pos = cio_tell();
2298 cio_write(MJ2_MDHD, 4); /* MDHD */
2300 cio_write(0, 4); /* Version = 0, flags = 0 */
2302 cio_write(tk->creation_time, 4); /* Creation Time */
2304 time(<ime); /* Time since 1/1/70 */
2305 modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2307 cio_write(modification_time, 4); /* Modification Time */
2309 cio_write(tk->timescale, 4); /* Timescale */
2313 for (i = 0; i < tk->num_samples; i++)
2314 tk->duration += tk->sample[i].sample_delta;
2316 cio_write(tk->duration, 4); /* Duration */
2318 cio_write(tk->language, 2); /* Language */
2320 cio_write(0, 2); /* Predefined */
2322 box.length = cio_tell() - box.init_pos;
2323 cio_seek(box.init_pos);
2324 cio_write(box.length, 4); /* L */
2325 cio_seek(box.init_pos + box.length);
2334 int mj2_read_mdhd(mj2_tk_t * tk)
2338 mj2_read_boxhdr(&box);
2339 if (!(MJ2_MHDR == box.type || MJ2_MDHD == box.type)) { // Kakadu writes MHDR instead of MDHD
2340 fprintf(stderr, "Error: Expected MDHD Marker\n");
2344 if (0 != cio_read(1)) { /* Version = 0 */
2345 fprintf(stderr, "Error: Only Version 0 handled in MDHD box\n");
2349 if (0 != cio_read(3)) { /* Flags = 0 */
2350 fprintf(stderr, "Error with flag in MDHD box. Expected flag 0\n");
2355 tk->creation_time = cio_read(4); /* Creation Time */
2357 tk->modification_time = cio_read(4); /* Modification Time */
2359 tk->timescale = cio_read(4); /* Timescale */
2361 tk->duration = cio_read(4); /* Duration */
2363 tk->language = cio_read(2); /* Language */
2365 cio_skip(2); /* Predefined */
2367 if (cio_tell() - box.init_pos != box.length) {
2368 fprintf(stderr, "Error with MDHD Box size\n");
2375 * Write the MDIA box
2380 void mj2_write_mdia(mj2_tk_t * tk, j2k_image_t * img)
2384 box.init_pos = cio_tell();
2386 cio_write(MJ2_MDIA, 4); /* MDIA */
2390 mj2_write_minf(tk, img);
2392 box.length = cio_tell() - box.init_pos;
2393 cio_seek(box.init_pos);
2394 cio_write(box.length, 4); /* L */
2395 cio_seek(box.init_pos + box.length);
2404 int mj2_read_mdia(mj2_tk_t * tk, j2k_image_t * img)
2408 mj2_read_boxhdr(&box);
2409 if (MJ2_MDIA != box.type) {
2410 fprintf(stderr, "Error: Expected MDIA Marker\n");
2414 if (mj2_read_mdhd(tk))
2416 if (mj2_read_hdlr(tk))
2418 if (mj2_read_minf(tk, img))
2421 if (cio_tell() - box.init_pos != box.length) {
2422 fprintf(stderr, "Error with MDIA Box size\n");
2429 * Write the TKHD box
2434 void mj2_write_tkhd(mj2_tk_t * tk)
2440 box.init_pos = cio_tell();
2443 cio_write(MJ2_TKHD, 4); /* TKHD */
2445 cio_write(3, 4); /* Version=0, flags=3 */
2447 time(<ime); /* Time since 1/1/70 */
2448 tk->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2450 cio_write(tk->creation_time, 4); /* Creation Time */
2452 cio_write(tk->modification_time, 4); /* Modification Time */
2454 cio_write(tk->track_ID, 4); /* Track ID */
2456 cio_write(0, 4); /* Reserved */
2460 for (i = 0; i < tk->num_samples; i++)
2461 tk->duration += tk->sample[i].sample_delta;
2463 cio_write(tk->duration, 4); /* Duration */
2465 cio_write(0, 4); /* Reserved */
2466 cio_write(0, 4); /* Reserved */
2468 cio_write(tk->layer, 2); /* Layer */
2470 cio_write(0, 2); /* Predefined */
2472 cio_write(tk->volume << 8, 2); /* Volume */
2474 cio_write(0, 2); /* Reserved */
2476 cio_write(tk->trans_matrix[0], 4); /* Transformation matrix for track */
2477 cio_write(tk->trans_matrix[1], 4);
2478 cio_write(tk->trans_matrix[2], 4);
2479 cio_write(tk->trans_matrix[3], 4);
2480 cio_write(tk->trans_matrix[4], 4);
2481 cio_write(tk->trans_matrix[5], 4);
2482 cio_write(tk->trans_matrix[6], 4);
2483 cio_write(tk->trans_matrix[7], 4);
2484 cio_write(tk->trans_matrix[8], 4);
2486 cio_write(tk->w << 16, 4); /* Video Width */
2488 cio_write(tk->h << 16, 4); /* Video Height */
2490 box.length = cio_tell() - box.init_pos;
2491 cio_seek(box.init_pos);
2492 cio_write(box.length, 4); /* L */
2493 cio_seek(box.init_pos + box.length);
2502 int mj2_read_tkhd(mj2_tk_t * tk)
2508 mj2_read_boxhdr(&box);
2510 if (MJ2_TKHD != box.type) {
2511 fprintf(stderr, "Error: Expected TKHD Marker\n");
2515 if (0 != cio_read(1)) { /* Version = 0 */
2516 fprintf(stderr, "Error: Only Version 0 handled in TKHD box\n");
2522 if (!(flag == 1 || flag == 2 || flag == 3 || flag == 4)) { /* Flags = 1,2,3 or 4 */
2524 "Error with flag in TKHD box: Expected flag 1,2,3 or 4\n");
2528 tk->creation_time = cio_read(4); /* Creation Time */
2530 tk->modification_time = cio_read(4); /* Modification Time */
2532 tk->track_ID = cio_read(4); /* Track ID */
2534 cio_skip(4); /* Reserved */
2536 tk->duration = cio_read(4); /* Duration */
2538 cio_skip(8); /* Reserved */
2540 tk->layer = cio_read(2); /* Layer */
2542 cio_read(2); /* Predefined */
2544 tk->volume = cio_read(2) >> 8; /* Volume */
2546 cio_skip(2); /* Reserved */
2548 tk->trans_matrix[0] = cio_read(4); /* Transformation matrix for track */
2549 tk->trans_matrix[1] = cio_read(4);
2550 tk->trans_matrix[2] = cio_read(4);
2551 tk->trans_matrix[3] = cio_read(4);
2552 tk->trans_matrix[4] = cio_read(4);
2553 tk->trans_matrix[5] = cio_read(4);
2554 tk->trans_matrix[6] = cio_read(4);
2555 tk->trans_matrix[7] = cio_read(4);
2556 tk->trans_matrix[8] = cio_read(4);
2558 tk->w = cio_read(4) >> 16; /* Video Width */
2560 tk->h = cio_read(4) >> 16; /* Video Height */
2562 if (cio_tell() - box.init_pos != box.length) {
2563 fprintf(stderr, "Error with TKHD Box size\n");
2570 * Write the TRAK box
2575 void mj2_write_trak(mj2_tk_t * tk, j2k_image_t * img)
2579 box.init_pos = cio_tell();
2582 cio_write(MJ2_TRAK, 4); /* TRAK */
2585 mj2_write_mdia(tk, img);
2587 box.length = cio_tell() - box.init_pos;
2588 cio_seek(box.init_pos);
2589 cio_write(box.length, 4); /* L */
2590 cio_seek(box.init_pos + box.length);
2599 int mj2_read_trak(mj2_tk_t * tk, j2k_image_t * img)
2603 mj2_read_boxhdr(&box);
2604 if (MJ2_TRAK != box.type) {
2605 fprintf(stderr, "Error: Expected TRAK Marker\n");
2608 if (mj2_read_tkhd(tk))
2610 if (mj2_read_mdia(tk, img))
2612 if (cio_tell() - box.init_pos != box.length) {
2613 fprintf(stderr, "Error with TRAK Box\n");
2620 * Write the MVHD box
2625 void mj2_write_mvhd(mj2_movie_t * movie)
2633 box.init_pos = cio_tell();
2635 cio_write(MJ2_MVHD, 4); /* MVHD */
2637 cio_write(0, 4); /* Version = 0, flags = 0 */
2639 time(<ime); /* Time since 1/1/70 */
2640 movie->modification_time = ltime + 2082844800; /* Seoonds between 1/1/04 and 1/1/70 */
2642 cio_write(movie->creation_time, 4); /* Creation Time */
2644 cio_write(movie->modification_time, 4); /* Modification Time */
2646 cio_write(movie->timescale, 4); /* Timescale */
2648 movie->duration = 0;
2650 for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
2651 mj2_tk_t *tk = &movie->tk[i];
2653 for (j = 0; j < tk->num_samples; j++) {
2654 movie->duration += tk->sample[j].sample_delta;
2658 cio_write(movie->duration, 4);
2660 cio_write(movie->rate << 16, 4); /* Rate to play presentation */
2662 cio_write(movie->volume << 8, 2); /* Volume */
2664 cio_write(0, 2); /* Reserved */
2665 cio_write(0, 4); /* Reserved */
2666 cio_write(0, 4); /* Reserved */
2668 cio_write(movie->trans_matrix[0], 4); /* Transformation matrix for video */
2669 cio_write(movie->trans_matrix[1], 4);
2670 cio_write(movie->trans_matrix[2], 4);
2671 cio_write(movie->trans_matrix[3], 4);
2672 cio_write(movie->trans_matrix[4], 4);
2673 cio_write(movie->trans_matrix[5], 4);
2674 cio_write(movie->trans_matrix[6], 4);
2675 cio_write(movie->trans_matrix[7], 4);
2676 cio_write(movie->trans_matrix[8], 4);
2678 cio_write(0, 4); /* Pre-defined */
2679 cio_write(0, 4); /* Pre-defined */
2680 cio_write(0, 4); /* Pre-defined */
2681 cio_write(0, 4); /* Pre-defined */
2682 cio_write(0, 4); /* Pre-defined */
2683 cio_write(0, 4); /* Pre-defined */
2686 for (i = 0; i < movie->num_htk + movie->num_stk + movie->num_vtk; i++) {
2687 if (max_tk_num < movie->tk[i].track_ID)
2688 max_tk_num = movie->tk[i].track_ID;
2691 movie->next_tk_id = max_tk_num + 1;
2693 cio_write(movie->next_tk_id, 4); /* ID of Next track to be added */
2695 box.length = cio_tell() - box.init_pos;
2696 cio_seek(box.init_pos);
2697 cio_write(box.length, 4); /* L */
2698 cio_seek(box.init_pos + box.length);
2707 int mj2_read_mvhd(mj2_movie_t * movie)
2711 mj2_read_boxhdr(&box);
2712 if (MJ2_MVHD != box.type) {
2713 fprintf(stderr, "Error: Expected MVHD Marker\n");
2718 if (0 != cio_read(4)) { /* Version = 0, flags = 0 */
2719 fprintf(stderr, "Error: Only Version 0 handled\n");
2722 movie->creation_time = cio_read(4); /* Creation Time */
2724 movie->modification_time = cio_read(4); /* Modification Time */
2726 movie->timescale = cio_read(4); /* Timescale */
2728 movie->duration = cio_read(4); /* Duration */
2730 movie->rate = cio_read(4) >> 16; /* Rate to play presentation */
2732 movie->volume = cio_read(2) >> 8; /* Volume */
2734 cio_skip(10); /* Reserved */
2736 movie->trans_matrix[0] = cio_read(4); /* Transformation matrix for video */
2737 movie->trans_matrix[1] = cio_read(4);
2738 movie->trans_matrix[2] = cio_read(4);
2739 movie->trans_matrix[3] = cio_read(4);
2740 movie->trans_matrix[4] = cio_read(4);
2741 movie->trans_matrix[5] = cio_read(4);
2742 movie->trans_matrix[6] = cio_read(4);
2743 movie->trans_matrix[7] = cio_read(4);
2744 movie->trans_matrix[8] = cio_read(4);
2746 cio_skip(24); /* Pre-defined */
2748 movie->next_tk_id = cio_read(4); /* ID of Next track to be added */
2750 if (cio_tell() - box.init_pos != box.length) {
2751 fprintf(stderr, "Error with MVHD Box Size\n");
2759 * Write the MOOV box
2764 void mj2_write_moov(mj2_movie_t * movie, j2k_image_t * img)
2769 box.init_pos = cio_tell();
2771 cio_write(MJ2_MOOV, 4); /* MOOV */
2773 mj2_write_mvhd(movie);
2775 for (i = 0; i < (movie->num_stk + movie->num_htk + movie->num_vtk); i++) {
2776 mj2_write_trak(&movie->tk[i], img);
2779 box.length = cio_tell() - box.init_pos;
2780 cio_seek(box.init_pos);
2781 cio_write(box.length, 4); /* L */
2782 cio_seek(box.init_pos + box.length);
2792 int mj2_read_moov(mj2_movie_t * movie, j2k_image_t * img)
2798 mj2_read_boxhdr(&box);
2799 if (MJ2_MOOV != box.type) {
2800 fprintf(stderr, "Error: Expected MOOV Marker\n");
2806 if (mj2_read_mvhd(movie))
2811 (mj2_tk_t *) malloc((movie->next_tk_id - 1) * sizeof(mj2_tk_t));
2813 for (i = 0; cio_tell() - box.init_pos < box.length; i++) {
2814 mj2_read_boxhdr(&box2);
2815 if (box2.type == MJ2_TRAK) {
2816 cio_seek(box2.init_pos);
2817 if (mj2_read_trak(&movie->tk[i], img))
2820 if (movie->tk[i].track_type == 0) {
2822 } else if (movie->tk[i].track_type == 1) {
2824 } else if (movie->tk[i].track_type == 2) {
2827 } else if (box2.type == MJ2_MVEX) {
2828 cio_seek(box2.init_pos);
2829 cio_skip(box2.length);
2832 fprintf(stderr, "Error with MOOV Box: Expected TRAK or MVEX box\n");
2840 int mj2_encode(mj2_movie_t * movie, j2k_cp_t * cp, char *index)
2850 outbuf = (char *) malloc(cp->tdx * cp->tdy * 2);
2851 cio_init(outbuf, cp->tdx * cp->tdy * 2);
2853 outfile = fopen(movie->mj2file, "wb");
2855 fprintf(stderr, "failed to open %s for writing\n", movie->mj2file);
2860 mj2_write_ftyp(movie);
2862 if (mj2_write_mdat(outfile, movie, &img, cp, outbuf, index)) {
2863 fprintf(stderr, "Error writing tracks\n\n");
2867 outbuf = (char *) malloc(cp->tdx * cp->tdy * 2);
2868 cio_init(outbuf, cp->tdx * cp->tdy * 2);
2870 mj2_write_moov(movie, &img);
2873 fwrite(outbuf, 1, len, outfile);
2877 for (i = 0; i < movie->tk[0].jp2_struct.numcomps; i++) {
2879 sprintf(&tmp, "Compo%d", i);
2880 if (remove(&tmp) == -1) {
2881 fprintf(stderr, "failed to kill %s file !\n", &tmp);
2892 int mj2_decode(unsigned char *src, int len, mj2_movie_t * movie,
2893 j2k_cp_t * cp, char *outfile)
2895 unsigned int MDATbox_pos = 0;
2896 unsigned int MOOVboxend_pos = 0;
2904 if (mj2_read_ftyp(movie))
2908 for (; cio_numbytesleft() > 0;) {
2909 mj2_read_boxhdr(&box);
2912 cio_seek(box.init_pos);
2913 if (MOOVboxend_pos == 0) {
2914 MDATbox_pos = box.init_pos;
2915 cio_skip(box.length); /* The MDAT box is only read while the MOOV box is decoded */
2917 if (mj2_read_mdat(movie, src, outfile))
2923 cio_seek(box.init_pos);
2924 if (mj2_read_moov(movie, &img))
2926 MOOVboxend_pos = cio_tell();
2927 if (MDATbox_pos != 0) {
2928 cio_seek(MDATbox_pos); /* After MOOV box, read the MDAT box */
2930 if (mj2_read_mdat(movie, src, outfile))
2933 cio_seek(MOOVboxend_pos);
2938 cio_seek(box.init_pos);
2939 cio_skip(box.length);
2942 cio_seek(box.init_pos);
2943 cio_skip(box.length);
2946 cio_seek(box.init_pos);
2947 cio_skip(box.length);
2950 fprintf(stderr, "Unknown box\n");