"Fossies" - the Fresh Open Source Software Archive

Member "quicktime4linux-2.3/avi_strl.c" (20 Aug 2007, 11467 Bytes) of package /linux/privat/old/quicktime4linux-2.3-src.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "funcprotos.h"
    2 #include "quicktime.h"
    3 #include <string.h>
    4 
    5 
    6 // Update during close:
    7 // length
    8 // samples per chunk
    9 #define JUNK_SIZE 0x1018
   10 
   11 
   12 
   13 quicktime_strl_t* quicktime_new_strl()
   14 {
   15     quicktime_strl_t *strl = calloc(1, sizeof(quicktime_strl_t));
   16     return strl;
   17 }
   18 
   19 
   20 void quicktime_init_strl(quicktime_t *file, 
   21     quicktime_audio_map_t *atrack,
   22     quicktime_video_map_t *vtrack,
   23     quicktime_trak_t *trak,
   24     quicktime_strl_t *strl)
   25 {
   26     quicktime_atom_t list_atom, strh_atom, strf_atom;
   27     quicktime_atom_t junk_atom;
   28     int i;
   29 
   30 /* Construct tag */
   31     if(vtrack)
   32     {
   33         strl->tag[0] = '0' + (trak->tkhd.track_id - 1) / 10;
   34         strl->tag[1] = '0' + (trak->tkhd.track_id - 1) % 10;
   35         strl->tag[2] = 'd';
   36         strl->tag[3] = 'c';
   37     }
   38     else
   39     if(atrack)
   40     {
   41         strl->tag[0] = '0' + (trak->tkhd.track_id - 1) / 10;
   42         strl->tag[1] = '0' + (trak->tkhd.track_id - 1) % 10;
   43         strl->tag[2] = 'w';
   44         strl->tag[3] = 'b';
   45     }
   46 
   47 
   48 /* LIST 'strl' */
   49     quicktime_atom_write_header(file, &list_atom, "LIST");
   50     quicktime_write_char32(file, "strl");
   51 
   52 /* 'strh' */
   53     quicktime_atom_write_header(file, &strh_atom, "strh");
   54 
   55 
   56 
   57 /* vids */
   58     if(vtrack)
   59     {
   60         quicktime_write_char32(file, "vids");
   61         quicktime_write_char32(file, 
   62             trak->mdia.minf.stbl.stsd.table[0].format);
   63 /* flags */
   64         quicktime_write_int32_le(file, 0);
   65 /* priority */
   66         quicktime_write_int16_le(file, 0);
   67 /* language */
   68         quicktime_write_int16_le(file, 0);
   69 /* initial frame */
   70         quicktime_write_int32_le(file, 0);
   71 
   72 /* framerate denominator */
   73         quicktime_write_int32_le(file, 
   74             trak->mdia.minf.stbl.stts.table[0].sample_duration);
   75 /* framerate numerator */
   76         quicktime_write_int32_le(file, 
   77             trak->mdia.mdhd.time_scale);
   78 
   79 /* start */
   80         quicktime_write_int32_le(file, 0);
   81         strl->length_offset = quicktime_position(file);
   82 /* length: fill later */
   83         quicktime_write_int32_le(file, 0); 
   84 /* suggested buffer size */
   85         quicktime_write_int32_le(file, 0); 
   86 /* quality */
   87         quicktime_write_int32_le(file, -1); 
   88 /* sample size */
   89         quicktime_write_int32_le(file, 0); 
   90         quicktime_write_int16_le(file, 0);
   91         quicktime_write_int16_le(file, 0);
   92         quicktime_write_int16_le(file, trak->tkhd.track_width);
   93         quicktime_write_int16_le(file, trak->tkhd.track_height);
   94     }
   95     else
   96 /* auds */
   97     if(atrack)
   98     {
   99         quicktime_write_char32(file, "auds");
  100         quicktime_write_int32_le(file, 0);
  101 /* flags */
  102         quicktime_write_int32_le(file, 0);
  103 /* priority */
  104         quicktime_write_int16_le(file, 0);
  105 /* language */
  106         quicktime_write_int16_le(file, 0);
  107 /* initial frame */
  108         quicktime_write_int32_le(file, 0);
  109         strl->samples_per_chunk_offset = quicktime_position(file);
  110 /* samples per chunk */
  111         quicktime_write_int32_le(file, 0);
  112 /* sample rate * samples per chunk  if uncompressed */
  113 /* sample rate if compressed */
  114         quicktime_write_int32_le(file, 0);
  115 /* start */
  116         quicktime_write_int32_le(file, 0);
  117         strl->length_offset = quicktime_position(file);
  118 /* length, XXX: filled later */
  119         quicktime_write_int32_le(file, 0);
  120 /* suggested buffer size */
  121         quicktime_write_int32_le(file, 0);
  122 /* quality */
  123         quicktime_write_int32_le(file, -1);
  124 /* sample size: 0 for compressed and number of bytes for uncompressed */
  125         strl->sample_size_offset = quicktime_position(file);
  126         quicktime_write_int32_le(file, 0);
  127         quicktime_write_int32_le(file, 0);
  128         quicktime_write_int32_le(file, 0);
  129     }
  130     quicktime_atom_write_footer(file, &strh_atom);
  131 
  132 
  133 
  134 
  135 
  136 
  137 
  138 /* strf */
  139     quicktime_atom_write_header(file, &strf_atom, "strf");
  140 
  141     if(vtrack)
  142     {
  143 /* atom size repeated */
  144         quicktime_write_int32_le(file, 40);  
  145         quicktime_write_int32_le(file, trak->tkhd.track_width);
  146         quicktime_write_int32_le(file, trak->tkhd.track_height);
  147 /* planes */
  148         quicktime_write_int16_le(file, 1);  
  149 /* depth */
  150         quicktime_write_int16_le(file, 24); 
  151         quicktime_write_char32(file, 
  152             trak->mdia.minf.stbl.stsd.table[0].format);
  153         quicktime_write_int32_le(file, 
  154             trak->tkhd.track_width * trak->tkhd.track_height * 3);
  155         quicktime_write_int32_le(file, 0);
  156         quicktime_write_int32_le(file, 0);
  157         quicktime_write_int32_le(file, 0);
  158         quicktime_write_int32_le(file, 0);
  159     }
  160     else
  161     if(atrack)
  162     {
  163 /* By now the codec is instantiated so the WAV ID is available. */
  164         int wav_id = 0x0;
  165         quicktime_codec_t *codec_base = atrack->codec;
  166 
  167         if(codec_base->wav_id)
  168             wav_id = codec_base->wav_id;
  169         quicktime_write_int16_le(file, 
  170             wav_id);
  171         quicktime_write_int16_le(file, 
  172             trak->mdia.minf.stbl.stsd.table[0].channels);
  173         quicktime_write_int32_le(file, 
  174             trak->mdia.minf.stbl.stsd.table[0].sample_rate);
  175 /* bitrate in bytes */
  176         quicktime_write_int32_le(file, 256000 / 8); 
  177 /* block align */
  178         quicktime_write_int16_le(file, 1); 
  179 /* bits per sample */
  180         quicktime_write_int16_le(file, 
  181             trak->mdia.minf.stbl.stsd.table[0].sample_size); 
  182         quicktime_write_int16_le(file, 0);
  183     }
  184 
  185     quicktime_atom_write_footer(file, &strf_atom);
  186 
  187 
  188 
  189 
  190 /* Junk is required in Windows. */
  191 /* In Heroine Kernel it's padding for the super index */
  192     strl->indx_offset = quicktime_position(file);
  193     strl->padding_size = JUNK_SIZE;
  194 
  195 
  196 
  197     quicktime_atom_write_header(file, &junk_atom, "JUNK");
  198     for(i = 0; i < JUNK_SIZE; i += 4)
  199         quicktime_write_int32_le(file, 0);
  200     quicktime_atom_write_footer(file, &junk_atom);
  201 
  202 
  203 /* Initialize super index */
  204     quicktime_init_indx(file, &strl->indx, strl);
  205 
  206 
  207     quicktime_atom_write_footer(file, &list_atom);
  208 }
  209 
  210 
  211 
  212 void quicktime_delete_strl(quicktime_strl_t *strl)
  213 {
  214     quicktime_delete_indx(&strl->indx);
  215     free(strl);
  216 }
  217 
  218 void quicktime_read_strl(quicktime_t *file, 
  219     quicktime_strl_t *strl, 
  220     quicktime_atom_t *parent_atom)
  221 {
  222 // These are 0 if no track is currently being processed.
  223 // Set to 1 if audio or video track is being processed.
  224     char data[4], codec[4];
  225     int denominator;
  226     int numerator;
  227     double frame_rate;
  228     int width;
  229     int height;
  230     int depth;
  231     int frames;
  232     int bytes_per_sample = 0;
  233     int sample_size;
  234     int samples_per_chunk = 0;
  235     int channels;
  236     int sample_rate;
  237     int compression_id;
  238     quicktime_trak_t *trak = 0;
  239     quicktime_riff_t *first_riff = file->riff[0];
  240 
  241 
  242     codec[0] = codec[1] = codec[2] = codec[3] = 0;
  243 
  244 /* AVI translation: */
  245 /* vids -> trak */
  246 /* auds -> trak */
  247 /* Only one track is in each strl object */
  248     do
  249     {
  250         quicktime_atom_t leaf_atom;
  251         quicktime_atom_read_header(file, &leaf_atom);
  252 
  253 // strh
  254         if(quicktime_atom_is(&leaf_atom, "strh"))
  255         {
  256 // stream type
  257             quicktime_read_data(file, data, 4);
  258 
  259             if(quicktime_match_32(data, "vids"))
  260             {
  261                 trak = quicktime_add_trak(file);
  262                 width = 0;
  263                 height = 0;
  264                 depth = 24;
  265                 frames = 0;
  266                 strl->is_video = 1;
  267 
  268 
  269                 trak->tkhd.track_id = file->moov.mvhd.next_track_id;
  270                 file->moov.mvhd.next_track_id++;
  271 
  272 
  273 /* Codec */
  274                 quicktime_read_data(file, 
  275                     codec, 
  276                     4);
  277 /* Blank */
  278                 quicktime_set_position(file, quicktime_position(file) + 12);
  279                 denominator = quicktime_read_int32_le(file);
  280                 numerator = quicktime_read_int32_le(file);
  281 /*
  282  * printf("quicktime_read_strl 1 %c%c%c%c %d %d\n", 
  283  * codec[0], 
  284  * codec[1], 
  285  * codec[2], 
  286  * codec[3],
  287  * numerator,
  288  * denominator);
  289  */
  290                 if(denominator != 0)
  291                     frame_rate = (double)numerator / denominator;
  292                 else
  293                     frame_rate = numerator;
  294 
  295 // Canon TX1
  296                 if(numerator == 1000000 &&
  297                     denominator == 33333)
  298                     frame_rate = 30.00;
  299 
  300 /* Blank */
  301                 quicktime_set_position(file, quicktime_position(file) + 4);
  302                 frames = quicktime_read_int32_le(file);
  303             }
  304             else
  305 /* AVI auds */
  306             if(quicktime_match_32(data, "auds"))
  307             {
  308                 trak = quicktime_add_trak(file);
  309                 sample_size = 16;
  310                 channels = 2;
  311                 sample_rate = 0;
  312                 compression_id = 0;
  313                 strl->is_audio = 1;
  314 
  315                 trak->tkhd.track_id = file->moov.mvhd.next_track_id;
  316                 file->moov.mvhd.next_track_id++;
  317 // codec tag
  318                 quicktime_read_data(file, 
  319                     codec, 
  320                     4);
  321 //printf("quicktime_read_strl 2 %c%c%c%c\n", codec[0], codec[1], codec[2], codec[3]);
  322 // flags 32, priority 16, language 16, initial frame 32
  323                 quicktime_set_position(file, quicktime_position(file) + 12);
  324 
  325                 samples_per_chunk = quicktime_read_int32_le(file);
  326                 strl->bytes_per_second = quicktime_read_int32_le(file);
  327                 quicktime_set_position(file, quicktime_position(file) + 4);
  328 /* length of track */
  329                 strl->samples = quicktime_read_int32_le(file);
  330 /* suggested buffer size, quality */
  331                 quicktime_set_position(file, quicktime_position(file) + 8);
  332 
  333 // If this is 0 use constant samples_per_chunk to guess locations.
  334 // If it isn't 0 synthesize samples per chunk table to get locations.
  335 // McRowesoft doesn't really obey this rule so we may need to base it on codec ID.
  336                 bytes_per_sample = quicktime_read_int32_le(file);
  337 //printf("quicktime_read_strl 20 %d\n", samples_per_chunk);
  338             }
  339         }
  340 // strf
  341         else
  342         if(quicktime_atom_is(&leaf_atom, "strf"))
  343         {
  344             if(strl->is_video)
  345             {
  346 /* atom size repeated */
  347                 quicktime_read_int32_le(file);
  348                 width = quicktime_read_int32_le(file);
  349                 height = quicktime_read_int32_le(file);
  350 /* Panes */
  351                 quicktime_read_int16_le(file);
  352 /* Depth in bits */
  353                 depth = quicktime_read_int16_le(file);
  354                 quicktime_read_data(file, 
  355                     codec, 
  356                     4);
  357             }
  358             else
  359             if(strl->is_audio)
  360             {
  361                 compression_id = quicktime_read_int16_le(file);
  362                 channels = quicktime_read_int16_le(file);
  363                 sample_rate = quicktime_read_int32_le(file);
  364                 quicktime_set_position(file, quicktime_position(file) + 6);
  365                 sample_size = quicktime_read_int16_le(file);
  366 //printf("quicktime_read_strl 40 %d %d %d\n", channels, sample_rate, sample_size);
  367             }
  368         }
  369         else
  370 // Super index.
  371 // Read the super index + all the partial indexes now
  372         if(quicktime_atom_is(&leaf_atom, "indx"))
  373         {
  374 //printf("quicktime_read_strl 50\n");
  375             quicktime_read_indx(file, strl, &leaf_atom);
  376             strl->have_indx = 1;
  377         }
  378 
  379 //printf("quicktime_read_strl 60\n");
  380 
  381 
  382 // Next object
  383         quicktime_atom_skip(file, &leaf_atom);
  384     }while(quicktime_position(file) < parent_atom->end);
  385 //printf("quicktime_read_strl 70 %d %d\n", strl->is_audio, strl->is_video);
  386 
  387 
  388     if(strl->is_video)
  389     {
  390 /* Generate quicktime structures */
  391         quicktime_trak_init_video(file, 
  392             trak, 
  393             width, 
  394             height, 
  395             frame_rate,
  396             codec);
  397         quicktime_mhvd_init_video(file, 
  398             &file->moov.mvhd, 
  399             frame_rate);
  400         trak->mdia.mdhd.duration = frames;
  401 //          trak->mdia.mdhd.time_scale = 1;
  402         memcpy(trak->mdia.minf.stbl.stsd.table[0].format, codec, 4);
  403         trak->mdia.minf.stbl.stsd.table[0].depth = depth;
  404     }
  405     else
  406     if(strl->is_audio)
  407     {
  408 /* Generate quicktime structures */
  409 //printf("quicktime_read_strl 70 %d\n", sample_size);
  410         quicktime_trak_init_audio(file, 
  411                     trak, 
  412                     channels, 
  413                     sample_rate, 
  414                     sample_size, 
  415                     codec);
  416 
  417 
  418 // We store a constant samples per chunk based on the 
  419 // packet size if sample_size zero
  420 // and calculate the samples per chunk based on the chunk size if sample_size 
  421 // is nonzero.
  422 //      trak->mdia.minf.stbl.stsd.table[0].sample_size = bytes_per_sample;
  423         trak->mdia.minf.stbl.stsd.table[0].compression_id = compression_id;
  424 
  425 /* Synthesize stsc table for constant samples per chunk */
  426         if(!bytes_per_sample)
  427         {
  428 /* Should be enough entries allocated in quicktime_stsc_init_table */
  429             trak->mdia.minf.stbl.stsc.table[0].samples = samples_per_chunk;
  430             trak->mdia.minf.stbl.stsc.total_entries = 1;
  431         }
  432 //printf("quicktime_read_strl samples=%d samples_per_chunk=%d bytes_per_sample=%d bytes_per_second=%d\n", 
  433 //strl->samples, samples_per_chunk, bytes_per_sample, strl->bytes_per_second);
  434     }
  435 
  436 
  437 //printf("quicktime_read_strl 100\n");
  438 }
  439 
  440 
  441 
  442