-/*
-* Write the MDAT box
-*
-* Media Data box
-*
-*/
-int mj2_write_mdat(FILE * outfile, mj2_movie_t * movie, j2k_image_t * img,
- j2k_cp_t * cp, char *outbuf, char *index)
-{
- unsigned char box_len_ptr;
- mj2_box_t box;
- int len, l, k;
- int i, m;
- unsigned int j;
- int pos_correction = 0;
- int tileno;
-
- box.init_pos = cio_tell();
- cio_skip(4);
- cio_write(MJ2_MDAT, 4); /* MDAT */
-
- for (i = 0; i < movie->num_stk + movie->num_htk + movie->num_vtk; i++) {
- if (movie->tk[i].track_type != 0) {
- fprintf(stderr, "Unable to write sound or hint tracks\n");
- } else {
- j2k_cp_t cp_init;
- mj2_tk_t *tk;
-
- tk = &movie->tk[i];
-
- fprintf(stderr, "Video Track number %d\n", i + 1);
-
- len = cio_tell();
- fwrite(outbuf, 1, len, outfile);
- pos_correction = cio_tell() + pos_correction;
- free(outbuf);
-
- /* Copy the first tile coding parameters (tcp) to cp_init */
-
- cp_init.tcps =
- (j2k_tcp_t *) malloc(cp->tw * cp->th * sizeof(j2k_tcp_t));
- for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
- for (l = 0; l < cp->tcps[tileno].numlayers; l++) {
- cp_init.tcps[tileno].rates[l] = cp->tcps[tileno].rates[l];
- //tileno = cp->tcps[tileno].rates[l];
- }
- }
-
-
- for (j = 0; j < tk->num_samples; j++) {
- outbuf = (char *) malloc(cp->tdx * cp->tdy * cp->th * cp->tw * 2);
- cio_init(outbuf, cp->tdx * cp->tdy * cp->th * cp->tw * 2);
-
- fprintf(stderr, "Frame number %d/%d: \n", j + 1, tk->num_samples);
-
-
- if (!yuvtoimage(tk, img, j)) {
- fprintf(stderr, "Error with frame number %d in YUV file\n", j);
- return 1;
- }
-
- len = jp2_write_jp2c(img, cp, outbuf, index);
-
- for (m = 0; m < img->numcomps; m++) {
- free(img->comps[m].data);
- }
-
- tk->sample[j].sample_size = len;
-
- tk->sample[j].offset = pos_correction;
- tk->chunk[j].offset = pos_correction; /* There is one sample per chunk */
-
- fwrite(outbuf, 1, len, outfile);
-
- pos_correction = cio_tell() + pos_correction;
-
- free(outbuf);
-
- /* Copy the cp_init parameters to cp->tcps */
-
- for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
- for (k = 0; k < cp->tcps[tileno].numlayers; k++) {
- cp->tcps[tileno].rates[k] = cp_init.tcps[tileno].rates[k];
- }
- }
- }
- }
-
- box.length = pos_correction - box.init_pos;
-
- fseek(outfile, box.init_pos, SEEK_SET);
-
- cio_init(&box_len_ptr, 4); /* Init a cio to write box length variable in a little endian way */
- cio_write(box.length, 4);
-
- fwrite(&box_len_ptr, 4, 1, outfile);
-
- fseek(outfile, box.init_pos + box.length, SEEK_SET);
- }
- return 0;
-}
-
-
-/*
-* Read the MDAT box
-*
-* Media Data box
-*
-*/
-int mj2_read_mdat(mj2_movie_t * movie, unsigned char *src, char *outfile)
-{
- int track_nb;
- unsigned int i;
- int jp2c_cio_len, jp2c_len, pos_correction = 0;
- FILE *f=NULL;
- int compno;
-
- mj2_box_t box;
-
- mj2_read_boxhdr(&box);
- if (MJ2_MDAT != box.type) {
- fprintf(stderr, "Error: Expected MDAT Marker\n");
- return 1;
- }
-
- pos_correction = cio_tell() - box.init_pos;
-
- f = fopen(outfile, "w"); /* Erase content of file if it already exists */
- fclose(f);
-
- for (track_nb = 0; track_nb < movie->next_tk_id - 1; track_nb++) {
- if (movie->tk[track_nb].imagefile != NULL) {
- fprintf(stderr, "%s", movie->tk[track_nb].imagefile);
-
- f = fopen(movie->tk[track_nb].imagefile, "w"); // Erase content of file if it already exists
- fclose(f);
- }
- }
-
- for (track_nb = 0;
- track_nb < movie->num_htk + movie->num_stk + movie->num_vtk;
- track_nb++) {
- mj2_tk_t *tk = &movie->tk[track_nb];
- if (tk->track_type != 0) {
- cio_seek(box.init_pos);
- cio_skip(box.length);
- } else {
- fprintf(stderr, "Track %d: Width=%d Height=%d\n", track_nb,
- tk->w, tk->h);
- fprintf(stderr, "%d Samples\n", tk->num_samples);
-
- if (tk->imagefile == NULL) {
- tk->imagefile = outfile;
- }
-
- for (i = 0; i < tk->num_samples; i++) {
-
- mj2_sample_t *sample = &tk->sample[i];
- j2k_image_t img;
- j2k_cp_t cp;
- unsigned char *pos;
-
- fprintf(stderr, "Frame %d / %d: \n", i+1, tk->num_samples);
-
- cio_init(src + sample->offset, 8);
-
- jp2c_cio_len = cio_tell();
- jp2c_len = cio_read(4);
-
-
- if (MJ2_JP2C != cio_read(4)) {
- fprintf(stderr, "Error: Expected JP2C Marker\n");
- return 1;
- }
-
- pos = src + sample->offset + 8;
-
- cio_seek(sample->offset + 8);
-
- if (!j2k_decode(pos, sample->sample_size, &img, &cp))
- return 1;
-
- if (imagetoyuv(&img, &cp, tk->imagefile))
- return 1;
-
- j2k_dec_release();
-
- for (compno=0; compno < img.numcomps; compno++)
- free(img.comps[compno].data);
-
-
- if (cio_tell() + 8 != jp2c_len) {
- fprintf(stderr, "Error with JP2C Box Size\n");
- return 1;
- }
-
- }
- }
- }
-
- cio_seek(box.init_pos);
- cio_skip(box.length); /* Go back to box end */
-
- return 0;
-}