"Fossies" - the Fresh Open Source Software Archive

Member "quicktime4linux-2.3/stsdtable.c" (9 Jan 2007, 12988 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 void quicktime_mjqt_init(quicktime_mjqt_t *mjqt)
    7 {
    8 }
    9 
   10 void quicktime_mjqt_delete(quicktime_mjqt_t *mjqt)
   11 {
   12 }
   13 
   14 void quicktime_mjqt_dump(quicktime_mjqt_t *mjqt)
   15 {
   16 }
   17 
   18 
   19 void quicktime_mjht_init(quicktime_mjht_t *mjht)
   20 {
   21 }
   22 
   23 void quicktime_mjht_delete(quicktime_mjht_t *mjht)
   24 {
   25 }
   26 
   27 void quicktime_mjht_dump(quicktime_mjht_t *mjht)
   28 {
   29 }
   30 
   31 // Set esds header to a copy of the argument
   32 void quicktime_set_mpeg4_header(quicktime_stsd_table_t *table,
   33     unsigned char *data, 
   34     int size)
   35 {
   36     if(table->esds.mpeg4_header)
   37     {
   38         free(table->esds.mpeg4_header);
   39     }
   40 
   41     table->esds.mpeg4_header = calloc(1, size);
   42     memcpy(table->esds.mpeg4_header, data, size);
   43     table->esds.mpeg4_header_size = size;
   44 }
   45 
   46 static void read_wave(quicktime_t *file, 
   47     quicktime_stsd_table_t *table, 
   48     quicktime_atom_t *parent_atom)
   49 {
   50     quicktime_atom_t leaf_atom;
   51 //printf("read_wave 1 start=0x%llx\n", quicktime_position(file));
   52     while(quicktime_position(file) < parent_atom->end)
   53     {
   54         quicktime_atom_read_header(file, &leaf_atom);
   55         if(quicktime_atom_is(&leaf_atom, "esds"))
   56         {
   57             quicktime_read_esds(file, &leaf_atom, &table->esds);
   58         }
   59         else
   60         if(quicktime_atom_is(&leaf_atom, "frma"))
   61         {
   62 // Extra data for QDM2
   63             quicktime_read_frma(file, parent_atom, &leaf_atom, &table->frma);
   64         }
   65         else
   66             quicktime_atom_skip(file, &leaf_atom);
   67     }
   68 }
   69 
   70 void quicktime_read_stsd_audio(quicktime_t *file, 
   71     quicktime_stsd_table_t *table, 
   72     quicktime_atom_t *parent_atom)
   73 {
   74     quicktime_atom_t leaf_atom;
   75 
   76     table->version = quicktime_read_int16(file);
   77     table->revision = quicktime_read_int16(file);
   78     quicktime_read_data(file, table->vendor, 4);
   79     table->channels = quicktime_read_int16(file);
   80     table->sample_size = quicktime_read_int16(file);
   81     table->compression_id = quicktime_read_int16(file);
   82     table->packet_size = quicktime_read_int16(file);
   83     table->sample_rate = quicktime_read_fixed32(file);
   84 
   85 // Kluge for fixed32 limitation
   86 if(table->sample_rate + 65536 == 96000 ||
   87     table->sample_rate + 65536 == 88200) table->sample_rate += 65536;
   88 
   89 
   90 // Version 1 fields
   91     if(table->version > 0)
   92     {
   93         table->samples_per_packet = quicktime_read_int32(file);
   94         table->bytes_per_packet = quicktime_read_int32(file);
   95         table->bytes_per_frame = quicktime_read_int32(file);
   96         table->bytes_per_sample = quicktime_read_int32(file);
   97 
   98 // Skip another 20 bytes
   99         if(table->version == 2)
  100         {
  101             quicktime_set_position(file, quicktime_position(file) + 0x14);
  102         }
  103 
  104         while(quicktime_position(file) < parent_atom->end)
  105         {
  106             quicktime_atom_read_header(file, &leaf_atom);
  107 
  108             if(quicktime_atom_is(&leaf_atom, "wave"))
  109             {
  110                 read_wave(file, table, &leaf_atom);
  111             }
  112             else
  113             {
  114                 quicktime_atom_skip(file, &leaf_atom);
  115             }
  116         }
  117     }
  118 
  119 // FFMPEG says the esds sometimes contains a sample rate that overrides
  120 // the sample table.
  121     quicktime_esds_samplerate(table, &table->esds);
  122 }
  123 
  124 void quicktime_write_stsd_audio(quicktime_t *file, quicktime_stsd_table_t *table)
  125 {
  126     quicktime_write_int16(file, table->version);
  127     quicktime_write_int16(file, table->revision);
  128     quicktime_write_data(file, table->vendor, 4);
  129     quicktime_write_int16(file, table->channels);
  130     quicktime_write_int16(file, table->sample_size);
  131 
  132     quicktime_write_int16(file, table->compression_id);
  133     quicktime_write_int16(file, table->packet_size);
  134     quicktime_write_fixed32(file, table->sample_rate);
  135 
  136 // Write header for mp4v
  137     if(table->esds.mpeg4_header_size && table->esds.mpeg4_header)
  138     {
  139 // Version 1 info
  140         quicktime_write_int32(file, 0);
  141         quicktime_write_int32(file, 0);
  142         quicktime_write_int32(file, 0);
  143         quicktime_write_int32(file, 0);
  144 
  145         quicktime_atom_t wave_atom;
  146         quicktime_atom_t frma_atom;
  147         quicktime_atom_t mp4a_atom;
  148         quicktime_atom_write_header(file, &wave_atom, "wave");
  149 
  150         quicktime_atom_write_header(file, &frma_atom, "frma");
  151         quicktime_write_data(file, "mp4a", 4);
  152         quicktime_atom_write_footer(file, &frma_atom);
  153 
  154         quicktime_atom_write_header(file, &mp4a_atom, "mp4a");
  155         quicktime_write_int32(file, 0x0);
  156         quicktime_atom_write_footer(file, &mp4a_atom);
  157 
  158         quicktime_write_esds(file, &table->esds, 0, 1);
  159         quicktime_atom_write_footer(file, &wave_atom);
  160     }
  161 }
  162 
  163 void quicktime_read_stsd_video(quicktime_t *file, 
  164     quicktime_stsd_table_t *table, 
  165     quicktime_atom_t *parent_atom)
  166 {
  167     quicktime_atom_t leaf_atom;
  168     int len;
  169     
  170     table->version = quicktime_read_int16(file);
  171     table->revision = quicktime_read_int16(file);
  172     quicktime_read_data(file, table->vendor, 4);
  173     table->temporal_quality = quicktime_read_int32(file);
  174     table->spatial_quality = quicktime_read_int32(file);
  175     table->width = quicktime_read_int16(file);
  176     table->height = quicktime_read_int16(file);
  177     table->dpi_horizontal = quicktime_read_fixed32(file);
  178     table->dpi_vertical = quicktime_read_fixed32(file);
  179     table->data_size = quicktime_read_int32(file);
  180     table->frames_per_sample = quicktime_read_int16(file);
  181     len = quicktime_read_char(file);
  182     quicktime_read_data(file, table->compressor_name, 31);
  183     table->depth = quicktime_read_int16(file);
  184     table->ctab_id = quicktime_read_int16(file);
  185     
  186     while(quicktime_position(file) < parent_atom->end)
  187     {
  188         quicktime_atom_read_header(file, &leaf_atom);
  189 /*
  190  * printf("quicktime_read_stsd_video 1 %llx %llx %llx %s\n", 
  191  * leaf_atom.start, leaf_atom.end, quicktime_position(file),
  192  * leaf_atom.type);
  193  */
  194 
  195 
  196         if(quicktime_atom_is(&leaf_atom, "esds"))
  197         {
  198             quicktime_read_esds(file, &leaf_atom, &table->esds);
  199         }
  200         else
  201         if(quicktime_atom_is(&leaf_atom, "avcC"))
  202         {
  203             quicktime_read_avcc(file, &leaf_atom, &table->avcc);
  204         }
  205         else
  206         if(quicktime_atom_is(&leaf_atom, "ctab"))
  207         {
  208             quicktime_read_ctab(file, &(table->ctab));
  209         }
  210         else
  211         if(quicktime_atom_is(&leaf_atom, "gama"))
  212         {
  213             table->gamma = quicktime_read_fixed32(file);
  214         }
  215         else
  216         if(quicktime_atom_is(&leaf_atom, "fiel"))
  217         {
  218             table->fields = quicktime_read_char(file);
  219             table->field_dominance = quicktime_read_char(file);
  220         }
  221         else
  222             quicktime_atom_skip(file, &leaf_atom);
  223 
  224 
  225 /*      if(quicktime_atom_is(&leaf_atom, "mjqt")) */
  226 /*      { */
  227 /*          quicktime_read_mjqt(file, &(table->mjqt)); */
  228 /*      } */
  229 /*      else */
  230 /*      if(quicktime_atom_is(&leaf_atom, "mjht")) */
  231 /*      { */
  232 /*          quicktime_read_mjht(file, &(table->mjht)); */
  233 /*      } */
  234 /*      else */
  235     }
  236 //printf("quicktime_read_stsd_video 2\n");
  237 }
  238 
  239 void quicktime_write_stsd_video(quicktime_t *file, quicktime_stsd_table_t *table)
  240 {
  241     quicktime_write_int16(file, table->version);
  242     quicktime_write_int16(file, table->revision);
  243     quicktime_write_data(file, table->vendor, 4);
  244     quicktime_write_int32(file, table->temporal_quality);
  245     quicktime_write_int32(file, table->spatial_quality);
  246     quicktime_write_int16(file, table->width);
  247     quicktime_write_int16(file, table->height);
  248     quicktime_write_fixed32(file, table->dpi_horizontal);
  249     quicktime_write_fixed32(file, table->dpi_vertical);
  250     quicktime_write_int32(file, table->data_size);
  251     quicktime_write_int16(file, table->frames_per_sample);
  252     quicktime_write_char(file, strlen(table->compressor_name));
  253     quicktime_write_data(file, table->compressor_name, 31);
  254     quicktime_write_int16(file, table->depth);
  255     quicktime_write_int16(file, table->ctab_id);
  256 
  257 
  258 // Write field order for mjpa
  259     if(table->fields)
  260     {
  261         quicktime_atom_t atom;
  262 
  263         quicktime_atom_write_header(file, &atom, "fiel");
  264         quicktime_write_char(file, table->fields);
  265         quicktime_write_char(file, table->field_dominance);
  266         quicktime_atom_write_footer(file, &atom);
  267     }
  268 
  269 // Write header for mp4v
  270     if(table->esds.mpeg4_header_size && table->esds.mpeg4_header)
  271     {
  272         quicktime_write_esds(file, &table->esds, 1, 0);
  273     }
  274 
  275     if(table->avcc.data_size)
  276     {
  277         quicktime_write_avcc(file, &table->avcc);
  278     }
  279 
  280 // Write another 32 bits
  281     if(table->version == 1)
  282         quicktime_write_int32(file, 0x0);
  283 }
  284 
  285 void quicktime_read_stsd_table(quicktime_t *file, quicktime_minf_t *minf, quicktime_stsd_table_t *table)
  286 {
  287     quicktime_atom_t leaf_atom;
  288 
  289     quicktime_atom_read_header(file, &leaf_atom);
  290 
  291     table->format[0] = leaf_atom.type[0];
  292     table->format[1] = leaf_atom.type[1];
  293     table->format[2] = leaf_atom.type[2];
  294     table->format[3] = leaf_atom.type[3];
  295     quicktime_read_data(file, table->reserved, 6);
  296     table->data_reference = quicktime_read_int16(file);
  297 
  298     if(minf->is_audio) quicktime_read_stsd_audio(file, table, &leaf_atom);
  299     if(minf->is_video) quicktime_read_stsd_video(file, table, &leaf_atom);
  300 }
  301 
  302 void quicktime_stsd_table_init(quicktime_stsd_table_t *table)
  303 {
  304     int i;
  305     table->format[0] = 'y';
  306     table->format[1] = 'u';
  307     table->format[2] = 'v';
  308     table->format[3] = '2';
  309     for(i = 0; i < 6; i++) table->reserved[i] = 0;
  310     table->data_reference = 1;
  311 
  312     table->version = 0;
  313     table->revision = 0;
  314     table->vendor[0] = 'l';
  315     table->vendor[1] = 'n';
  316     table->vendor[2] = 'u';
  317     table->vendor[3] = 'x';
  318 
  319     table->temporal_quality = 100;
  320     table->spatial_quality = 258;
  321     table->width = 0;
  322     table->height = 0;
  323     table->dpi_horizontal = 72;
  324     table->dpi_vertical = 72;
  325     table->data_size = 0;
  326     table->frames_per_sample = 1;
  327     for(i = 0; i < 32; i++) table->compressor_name[i] = 0;
  328     sprintf(table->compressor_name, "Quicktime for Linux");
  329     table->depth = 24;
  330     table->ctab_id = 65535;
  331     quicktime_ctab_init(&(table->ctab));
  332     table->gamma = 0;
  333     table->fields = 0;
  334     table->field_dominance = 1;
  335     quicktime_mjqt_init(&(table->mjqt));
  336     quicktime_mjht_init(&(table->mjht));
  337     
  338     table->channels = 0;
  339     table->sample_size = 0;
  340     table->compression_id = 0;
  341     table->packet_size = 0;
  342     table->sample_rate = 0;
  343 }
  344 
  345 void quicktime_stsd_table_delete(quicktime_stsd_table_t *table)
  346 {
  347     quicktime_ctab_delete(&(table->ctab));
  348     quicktime_mjqt_delete(&(table->mjqt));
  349     quicktime_mjht_delete(&(table->mjht));
  350     quicktime_delete_avcc(&(table->avcc));
  351     quicktime_delete_esds(&(table->esds));
  352     quicktime_delete_frma(&(table->frma));
  353     
  354 }
  355 
  356 void quicktime_stsd_video_dump(quicktime_stsd_table_t *table)
  357 {
  358     printf("       version %d\n", table->version);
  359     printf("       revision %d\n", table->revision);
  360     printf("       vendor %c%c%c%c\n", table->vendor[0], table->vendor[1], table->vendor[2], table->vendor[3]);
  361     printf("       temporal_quality %ld\n", table->temporal_quality);
  362     printf("       spatial_quality %ld\n", table->spatial_quality);
  363     printf("       width %d\n", table->width);
  364     printf("       height %d\n", table->height);
  365     printf("       dpi_horizontal %f\n", table->dpi_horizontal);
  366     printf("       dpi_vertical %f\n", table->dpi_vertical);
  367     printf("       data_size %ld\n", table->data_size);
  368     printf("       frames_per_sample %d\n", table->frames_per_sample);
  369     printf("       compressor_name %s\n", table->compressor_name);
  370     printf("       depth %d\n", table->depth);
  371     printf("       ctab_id %d\n", table->ctab_id);
  372     printf("       gamma %f\n", table->gamma);
  373     if(table->fields)
  374     {
  375         printf("       fields %d\n", table->fields);
  376         printf("       field dominance %d\n", table->field_dominance);
  377     }
  378     if(!table->ctab_id) quicktime_ctab_dump(&(table->ctab));
  379     quicktime_mjqt_dump(&(table->mjqt));
  380     quicktime_mjht_dump(&(table->mjht));
  381     quicktime_esds_dump(&table->esds);
  382     quicktime_avcc_dump(&table->avcc);
  383     quicktime_frma_dump(&table->frma);
  384 }
  385 
  386 void quicktime_stsd_audio_dump(quicktime_stsd_table_t *table)
  387 {
  388     printf("       version %d\n", table->version);
  389     printf("       revision %d\n", table->revision);
  390     printf("       vendor %c%c%c%c\n", table->vendor[0], table->vendor[1], table->vendor[2], table->vendor[3]);
  391     printf("       channels %d\n", table->channels);
  392     printf("       sample_size %d\n", table->sample_size);
  393     printf("       compression_id 0x%x\n", table->compression_id);
  394     printf("       packet_size %d\n", table->packet_size);
  395     printf("       sample_rate %f\n", table->sample_rate);
  396     if(table->version > 0)
  397     {
  398         printf("       samples_per_packet %d\n", table->samples_per_packet);
  399         printf("       bytes_per_packet %d\n", table->bytes_per_packet);
  400         printf("       bytes_per_frame %d\n", table->bytes_per_frame);
  401         printf("       bytes_per_sample %d\n", table->bytes_per_sample);
  402     }
  403     quicktime_esds_dump(&table->esds);
  404     quicktime_avcc_dump(&table->avcc);
  405     quicktime_frma_dump(&table->frma);
  406 }
  407 
  408 void quicktime_stsd_table_dump(void *minf_ptr, quicktime_stsd_table_t *table)
  409 {
  410     quicktime_minf_t *minf = minf_ptr;
  411     printf("       format %c%c%c%c\n", table->format[0], table->format[1], table->format[2], table->format[3]);
  412     quicktime_print_chars("       reserved ", table->reserved, 6);
  413     printf("       data_reference %d\n", table->data_reference);
  414 
  415     if(minf->is_audio) quicktime_stsd_audio_dump(table);
  416     if(minf->is_video) quicktime_stsd_video_dump(table);
  417 }
  418 
  419 void quicktime_write_stsd_table(quicktime_t *file, quicktime_minf_t *minf, quicktime_stsd_table_t *table)
  420 {
  421     quicktime_atom_t atom;
  422     quicktime_atom_write_header(file, &atom, table->format);
  423 /*printf("quicktime_write_stsd_table %c%c%c%c\n", table->format[0], table->format[1], table->format[2], table->format[3]); */
  424     quicktime_write_data(file, table->reserved, 6);
  425     quicktime_write_int16(file, table->data_reference);
  426     
  427     if(minf->is_audio) quicktime_write_stsd_audio(file, table);
  428     if(minf->is_video) quicktime_write_stsd_video(file, table);
  429 
  430     quicktime_atom_write_footer(file, &atom);
  431 }