"Fossies" - the Fresh Open Source Software Archive

Member "quicktime4linux-2.3/codecs.c" (11 Jul 2008, 9093 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 "colormodels.h"
    2 #include "funcprotos.h"
    3 #include "quicktime.h"
    4 #include <string.h>
    5 
    6 static int delete_vcodec_stub(quicktime_video_map_t *vtrack)
    7 {
    8     printf("delete_vcodec_stub called\n");
    9     return 0;
   10 }
   11 
   12 static int delete_acodec_stub(quicktime_audio_map_t *atrack)
   13 {
   14     printf("delete_acodec_stub called\n");
   15     return 0;
   16 }
   17 
   18 static int decode_video_stub(quicktime_t *file, 
   19                 unsigned char **row_pointers, 
   20                 int track)
   21 {
   22     printf("decode_video_stub called\n");
   23     return 1;
   24 }
   25 
   26 static int encode_video_stub(quicktime_t *file, 
   27                 unsigned char **row_pointers, 
   28                 int track)
   29 {
   30     printf("encode_video_stub called\n");
   31     return 1;
   32 }
   33 
   34 static int decode_audio_stub(quicktime_t *file, 
   35                     int16_t *output_i, 
   36                     float *output_f, 
   37                     long samples, 
   38                     int track, 
   39                     int channel)
   40 {
   41     printf("decode_audio_stub called\n");
   42     return 1;
   43 }
   44 
   45 static int encode_audio_stub(quicktime_t *file, 
   46                 int16_t **input_i, 
   47                 float **input_f, 
   48                 int track, 
   49                 long samples)
   50 {
   51     printf("encode_audio_stub called\n");
   52     return 1;
   53 }
   54 
   55 
   56 static int reads_colormodel_stub(quicktime_t *file, 
   57         int colormodel, 
   58         int track)
   59 {
   60     return (colormodel == BC_RGB888);
   61 }
   62 
   63 static int writes_colormodel_stub(quicktime_t *file, 
   64         int colormodel, 
   65         int track)
   66 {
   67     return (colormodel == BC_RGB888);
   68 }
   69 
   70 static void flush_codec_stub(quicktime_t *file, int track)
   71 {
   72 }
   73 
   74 /* Convert Microsoft audio id to codec */
   75 void quicktime_id_to_codec(char *result, int id)
   76 {
   77     switch(id)
   78     {
   79         case 0x55:
   80             memcpy(result, QUICKTIME_MP3, 4);
   81             break;
   82         case 0x161:
   83             memcpy(result, QUICKTIME_WMA, 4);
   84             break;
   85         default:
   86             printf("quicktime_id_to_codec: unknown audio id: %p\n", id);
   87             break;
   88     }
   89 }
   90 
   91 int quicktime_codec_to_id(char *codec)
   92 {
   93     if(quicktime_match_32(codec, QUICKTIME_MP3))
   94         return 0x55;
   95     else
   96     if(quicktime_match_32(codec, QUICKTIME_WMA))
   97         return 0x161;
   98     else
   99         printf("quicktime_codec_to_id: unknown codec %c%c%c%c\n", codec[0], codec[1], codec[2], codec[3]);
  100 }
  101 
  102 
  103 static quicktime_codec_t* new_codec()
  104 {
  105     quicktime_codec_t *codec = calloc(1, sizeof(quicktime_codec_t));
  106     codec->delete_vcodec = delete_vcodec_stub;
  107     codec->delete_acodec = delete_acodec_stub;
  108     codec->decode_video = decode_video_stub;
  109     codec->encode_video = encode_video_stub;
  110     codec->decode_audio = decode_audio_stub;
  111     codec->encode_audio = encode_audio_stub;
  112     codec->reads_colormodel = reads_colormodel_stub;
  113     codec->writes_colormodel = writes_colormodel_stub;
  114     codec->flush = flush_codec_stub;
  115     return codec;
  116 }
  117 
  118 int new_vcodec(quicktime_video_map_t *vtrack)
  119 {
  120     quicktime_codec_t *codec_base = vtrack->codec = new_codec();
  121     char *compressor = vtrack->track->mdia.minf.stbl.stsd.table[0].format;
  122     int result = quicktime_find_vcodec(vtrack);
  123 
  124     if(result)
  125     {
  126         fprintf(stderr, 
  127             "new_vcodec: couldn't find codec for \"%c%c%c%c\"\n",
  128             compressor[0],
  129             compressor[1],
  130             compressor[2],
  131             compressor[3]);
  132         free(codec_base);
  133         vtrack->codec = 0;
  134         return 1;
  135     }
  136 
  137     return 0;
  138 }
  139 
  140 int new_acodec(quicktime_audio_map_t *atrack)
  141 {
  142     quicktime_codec_t *codec_base = atrack->codec = new_codec();
  143     char *compressor = atrack->track->mdia.minf.stbl.stsd.table[0].format;
  144     int result = quicktime_find_acodec(atrack);
  145 
  146     if(result)
  147     {
  148         fprintf(stderr, 
  149             "new_acodec: couldn't find codec for \"%c%c%c%c\"\n",
  150             compressor[0],
  151             compressor[1],
  152             compressor[2],
  153             compressor[3]);
  154         free(codec_base);
  155         atrack->codec = 0;
  156         return 1;
  157     }
  158 
  159     return 0;
  160 }
  161 
  162 int quicktime_init_vcodec(quicktime_video_map_t *vtrack)
  163 {
  164     int result = new_vcodec(vtrack);
  165     return result;
  166 }
  167 
  168 int quicktime_init_acodec(quicktime_audio_map_t *atrack)
  169 {
  170     int result = new_acodec(atrack);
  171     return result;
  172 }
  173 
  174 
  175 int quicktime_delete_vcodec(quicktime_video_map_t *vtrack)
  176 {
  177     if(vtrack->codec)
  178     {
  179         quicktime_codec_t *codec_base = vtrack->codec;
  180         if(codec_base->priv)
  181             codec_base->delete_vcodec(vtrack);
  182         free(vtrack->codec);
  183     }
  184     vtrack->codec = 0;
  185     return 0;
  186 }
  187 
  188 int quicktime_delete_acodec(quicktime_audio_map_t *atrack)
  189 {
  190     if(atrack->codec)
  191     {
  192         quicktime_codec_t *codec_base = atrack->codec;
  193         if(codec_base->priv)
  194             codec_base->delete_acodec(atrack);
  195         free(atrack->codec);
  196     }
  197     atrack->codec = 0;
  198     return 0;
  199 }
  200 
  201 int quicktime_supported_video(quicktime_t *file, int track)
  202 {
  203     if(track < file->total_vtracks)
  204     {
  205         quicktime_video_map_t *video_map = &file->vtracks[track];
  206 
  207         if(video_map->codec)
  208             return 1;
  209         else
  210             return 0;
  211     }
  212     return 0;
  213 }
  214 
  215 int quicktime_supported_audio(quicktime_t *file, int track)
  216 {
  217     if(track < file->total_atracks)
  218     {
  219         quicktime_audio_map_t *audio_map = &file->atracks[track];
  220         if(audio_map->codec)
  221             return 1;
  222         else
  223             return 0;
  224     }
  225     return 0;
  226     
  227 }
  228 
  229 
  230 long quicktime_decode_video(quicktime_t *file, 
  231     unsigned char **row_pointers, 
  232     int track)
  233 {
  234     int result;
  235 
  236     if(track < 0 || track >= file->total_vtracks)
  237     {
  238         fprintf(stderr, "quicktime_decode_video: track %d out of range %d - %d\n",
  239             track,
  240             0,
  241             file->total_vtracks);
  242         return 1;
  243     }
  244 
  245 /* Get dimensions from first video track */
  246     if(!file->do_scaling)
  247     {
  248         quicktime_video_map_t *video_map = &file->vtracks[track];
  249         quicktime_trak_t *trak = video_map->track;
  250         int track_width = trak->tkhd.track_width;
  251         int track_height = trak->tkhd.track_height;
  252 
  253         file->in_x = 0;
  254         file->in_y = 0;
  255         file->in_w = track_width;
  256         file->in_h = track_height;
  257         file->out_w = track_width;
  258         file->out_h = track_height;
  259     }
  260 
  261     result = ((quicktime_codec_t*)file->vtracks[track].codec)->decode_video(file, 
  262         row_pointers, 
  263         track);
  264     file->vtracks[track].current_position++;
  265     return result;
  266 }
  267 
  268 void quicktime_set_parameter(quicktime_t *file, const char *key, void *value)
  269 {
  270     int i;
  271     for(i = 0; i < file->total_vtracks; i++)
  272     {
  273         quicktime_codec_t *codec = (quicktime_codec_t*)file->vtracks[i].codec;
  274         if(codec)
  275             if(codec->set_parameter) codec->set_parameter(file, i, key, value);
  276     }
  277 
  278     for(i = 0; i < file->total_atracks; i++)
  279     {
  280         quicktime_codec_t *codec = (quicktime_codec_t*)file->atracks[i].codec;
  281         if(codec)
  282             if(codec->set_parameter) codec->set_parameter(file, i, key, value);
  283     }
  284 }
  285 
  286 int quicktime_encode_video(quicktime_t *file, 
  287     unsigned char **row_pointers, 
  288     int track)
  289 {
  290     int result;
  291     result = ((quicktime_codec_t*)file->vtracks[track].codec)->encode_video(file, row_pointers, track);
  292     file->vtracks[track].current_position++;
  293     return result;
  294 }
  295 
  296 
  297 int quicktime_decode_audio(quicktime_t *file, 
  298                 int16_t *output_i, 
  299                 float *output_f, 
  300                 long samples, 
  301                 int channel)
  302 {
  303     int quicktime_track, quicktime_channel;
  304     int result = 1;
  305 
  306     quicktime_channel_location(file, &quicktime_track, &quicktime_channel, channel);
  307     result = ((quicktime_codec_t*)file->atracks[quicktime_track].codec)->decode_audio(file, 
  308                 output_i, 
  309                 output_f, 
  310                 samples, 
  311                 quicktime_track, 
  312                 quicktime_channel);
  313     file->atracks[quicktime_track].current_position += samples;
  314 
  315     return result;
  316 }
  317 
  318 /* Since all channels are written at the same time: */
  319 /* Encode using the compressor for the first audio track. */
  320 /* Which means all the audio channels must be on the same track. */
  321 
  322 int quicktime_encode_audio(quicktime_t *file, int16_t **input_i, float **input_f, long samples)
  323 {
  324     int result = 1;
  325     char *compressor = quicktime_audio_compressor(file, 0);
  326 
  327     result = ((quicktime_codec_t*)file->atracks[0].codec)->encode_audio(file, 
  328         input_i, 
  329         input_f,
  330         0, 
  331         samples);
  332     file->atracks[0].current_position += samples;
  333 
  334     return result;
  335 }
  336 
  337 int quicktime_reads_cmodel(quicktime_t *file, 
  338     int colormodel, 
  339     int track)
  340 {
  341     int result = ((quicktime_codec_t*)file->vtracks[track].codec)->reads_colormodel(file, colormodel, track);
  342     return result;
  343 }
  344 
  345 int quicktime_writes_cmodel(quicktime_t *file, 
  346     int colormodel, 
  347     int track)
  348 {
  349     return ((quicktime_codec_t*)file->vtracks[track].codec)->writes_colormodel(file, colormodel, track);
  350 }
  351 
  352 /* Compressors that can only encode a window at a time */
  353 /* need to flush extra data here. */
  354 
  355 int quicktime_flush_acodec(quicktime_t *file, int track)
  356 {
  357     ((quicktime_codec_t*)file->atracks[track].codec)->flush(file, track);
  358     return 0;
  359 };
  360 
  361 void quicktime_flush_vcodec(quicktime_t *file, int track)
  362 {
  363     ((quicktime_codec_t*)file->vtracks[track].codec)->flush(file, track);
  364 }
  365 
  366 int64_t quicktime_samples_to_bytes(quicktime_trak_t *track, long samples)
  367 {
  368     char *compressor = track->mdia.minf.stbl.stsd.table[0].format;
  369     int channels = track->mdia.minf.stbl.stsd.table[0].channels;
  370 
  371     if(quicktime_match_32(compressor, QUICKTIME_DV))
  372         return (int64_t)samples * track->mdia.minf.stbl.stsz.sample_size;
  373 
  374     if(quicktime_match_32(compressor, QUICKTIME_IMA4)) 
  375         return samples * channels;
  376 
  377     if(quicktime_match_32(compressor, QUICKTIME_ULAW)) 
  378         return samples * channels;
  379 
  380 /* Default use the sample size specification for TWOS and RAW */
  381     return samples * channels * track->mdia.minf.stbl.stsd.table[0].sample_size / 8;
  382 }
  383 
  384 int quicktime_codecs_flush(quicktime_t *file)
  385 {
  386     int result = 0;
  387     int i;
  388     if(!file->wr) return result;
  389 
  390     if(file->total_atracks)
  391     {
  392         for(i = 0; i < file->total_atracks && !result; i++)
  393         {
  394             quicktime_flush_acodec(file, i);
  395         }
  396     }
  397 
  398     if(file->total_vtracks)
  399     {
  400         for(i = 0; i < file->total_vtracks && !result; i++)
  401         {
  402             quicktime_flush_vcodec(file, i);
  403         }
  404     }
  405     return result;
  406 }
  407