home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / xmovie / filemov.C < prev    next >
C/C++ Source or Header  |  2000-11-29  |  6KB  |  280 lines

  1. #include "asset.h"
  2. #include "colormodels.h"
  3. #include "file.h"
  4. #include "filemov.h"
  5. #include "vframe.h"
  6. #include <string.h>
  7.  
  8. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  9.  
  10. FileMOV::FileMOV(Asset *asset, File *file)
  11.  : FileBase(asset, file)
  12. {
  13.     reset_parameters();
  14.     asset->format = MOV;
  15.     asset->byte_order = 0;
  16. }
  17.  
  18. FileMOV::~FileMOV()
  19. {
  20.     close_file();
  21. }
  22.  
  23. int FileMOV::reset_parameters_derived()
  24. {
  25.     file = 0;
  26.     return 0;
  27. }
  28.  
  29. int FileMOV::open_file()
  30. {
  31.     if(!(file = quicktime_open(asset->path, 1, 0)))
  32.     {
  33.         perror("FileMOV::open_file");
  34.         return 1;
  35.     }
  36.  
  37.     quicktime_set_preload(file, FileBase::file->prebuffer_size);
  38.     quicktime_set_cpus(file, FileBase::file->cpus);
  39.     read_header();
  40.     
  41.     if(!asset->video_data && !asset->audio_data)
  42.     {
  43.         quicktime_close(file);
  44.         return 3;
  45.     }
  46.     return 0;
  47. }
  48.  
  49. int FileMOV::close_file_derived()
  50. {
  51.     if(file) quicktime_close(file);
  52.     file = 0;
  53.     return 0;
  54. }
  55.  
  56. int FileMOV::read_header()
  57. {
  58. // determine if the audio can be read before declaring audio data
  59.     if(quicktime_has_audio(file))
  60.         if(quicktime_supported_audio(file, 0))
  61.                 asset->audio_data = 1;
  62.             else
  63.                 printf("FileMOV::read_header: unsupported audio codec\n");
  64.  
  65.  
  66.     if(asset->audio_data)
  67.     {
  68.         asset->audio_streams = 1;
  69.         asset->channels = 0;
  70.         asset->channels = quicktime_track_channels(file, 0);
  71.         asset->rate = quicktime_sample_rate(file, 0);
  72.         asset->bits = quicktime_audio_bits(file, 0);
  73.         char *compressor = quicktime_audio_compressor(file, 0);
  74.  
  75.         if(quicktime_supported_audio(file, 0))
  76.         {
  77.             asset->bits = 16;
  78.             asset->signed_ = 1;
  79.             asset->byte_order = internal_byte_order;
  80.         }
  81.         else
  82.         if(match4(compressor, "raw ")) asset->signed_ = 0;
  83.         else
  84.         if(match4(compressor, "twos")) asset->signed_ = 1;
  85.         else
  86.         printf("FileMOV::read_header: unsupported audio codec\n");
  87.     }
  88.  
  89. // determine if the video can be read before declaring video data
  90.     if(quicktime_has_video(file))
  91.         if(quicktime_supported_video(file, 0))
  92.                 asset->video_data = 1;
  93.             else
  94.                 printf("FileMOV::read_header: unsupported video codec\n");
  95.  
  96.     if(asset->video_data)
  97.     {
  98.         asset->video_streams = 1;
  99.         asset->width = quicktime_video_width(file, 0);
  100.         asset->height = quicktime_video_height(file, 0);
  101.         asset->frame_rate = quicktime_frame_rate(file, 0);
  102.         
  103.         char *compressor = quicktime_video_compressor(file, 0);
  104.         asset->compression[0] = compressor[0];
  105.         asset->compression[1] = compressor[1];
  106.         asset->compression[2] = compressor[2];
  107.         asset->compression[3] = compressor[3];
  108.     }
  109.     return 0;
  110. }
  111.  
  112. long FileMOV::get_audio_length()
  113. {
  114.     long result = quicktime_audio_length(file, 0);
  115.     return result;
  116. }
  117.  
  118. long FileMOV::get_video_length()
  119. {
  120.     long result = quicktime_video_length(file, 0);
  121.     return result;
  122. }
  123.  
  124. int FileMOV::get_position(double &percentage, double &seconds)
  125. {
  126.     float apercentage = 0, aseconds = 0;
  127.     float vpercentage = 0, vseconds = 0;
  128.  
  129.     if(file)
  130.     {
  131.         if(asset->audio_data)
  132.         {
  133.             apercentage = (double)get_audio_position() / get_audio_length();
  134.             aseconds = (double)get_audio_position() / asset->rate;
  135.         }
  136.  
  137.         if(asset->video_data)
  138.         {
  139.             vpercentage = (double)get_video_position() / get_video_length();
  140.             vseconds = (double)get_video_position() / asset->frame_rate;
  141.         }
  142.  
  143.         percentage = MAX(apercentage, vpercentage);
  144.         seconds = MAX(aseconds, vseconds);
  145.     }
  146.  
  147.     return 0;
  148. }
  149.  
  150. long FileMOV::get_video_position()
  151. {
  152.     return quicktime_video_position(file, 0);
  153. }
  154.  
  155. long FileMOV::get_audio_position()
  156. {
  157.     return quicktime_audio_position(file, 0);
  158. }
  159.  
  160. int FileMOV::end_of_audio()
  161. {
  162.     return quicktime_audio_position(file, 0) >= quicktime_audio_length(file, 0);
  163. }
  164.  
  165.  
  166. int FileMOV::end_of_video()
  167. {
  168.     return quicktime_video_position(file, 0) >= quicktime_video_length(file, 0);
  169. }
  170.  
  171. int FileMOV::set_position(double percentage)
  172. {
  173.     int result = 0;
  174.  
  175.     result |= set_audio_position((int)(percentage * get_audio_length()));
  176.     result |= set_video_position((int)(percentage * get_video_length()));
  177.     return result;
  178. }
  179.  
  180. int FileMOV::set_audio_position(long x)
  181. {
  182.     audio_position = x;
  183.     return quicktime_set_audio_position(file, x, 0);
  184. }
  185.  
  186. int FileMOV::set_video_position(long x)
  187. {
  188.     video_position = x;
  189.     return quicktime_set_video_position(file, x, 0);
  190. }
  191.  
  192. int FileMOV::drop_frames(int frames)
  193. {
  194.     video_position += frames;
  195.     return quicktime_set_video_position(file, video_position, 0);
  196. }
  197.  
  198. int FileMOV::frame_back()
  199. {
  200.     if(asset->video_data)
  201.     {
  202.         video_position--;
  203.         audio_position = 0;
  204.     }
  205.     else
  206.     {
  207.         video_position = 0;
  208.         audio_position -= 44100;
  209.     }
  210.     quicktime_set_video_position(file, video_position, 0);
  211.     quicktime_set_audio_position(file, audio_position, 0);
  212.     return  0;
  213. }
  214.  
  215.  
  216. #define IMPORT1 \
  217.     for(int i = 0; i < asset->height; i++) \
  218.     {
  219.  
  220. #define IMPORT2 \
  221.     }
  222.  
  223. int FileMOV::read_frame(unsigned char *frame)
  224. {
  225.     int i, result;
  226.     unsigned char **row_pointers;
  227.     row_pointers = new unsigned char*[asset->height];
  228.  
  229.     for(i = 0; i < asset->height; i++)
  230.         row_pointers[i] = &frame[i * asset->width * 3];
  231.  
  232.     result = quicktime_decode_video(file, row_pointers, 0);
  233.  
  234.     delete row_pointers;
  235.     video_position++;
  236.     return result;
  237. }
  238.  
  239. int FileMOV::read_audio(char *buffer, long len)
  240. {
  241.     if(quicktime_supported_audio(file, 0))
  242.     {
  243.         int channel;
  244.         int i, j;
  245.         int result = 0;
  246.  
  247.         get_read_buffer(len);
  248.  
  249.         for(channel = 0; channel < asset->channels && !result; channel++)
  250.         {
  251. //printf("FileMOV::read_raw %d\n", channel);
  252.             quicktime_set_audio_position(file, audio_position, 0);
  253.             result = quicktime_decode_audio(file, read_buffer, 0, len, channel);
  254.  
  255.             BCBASE_INT16 *output_ptr = (BCBASE_INT16*)buffer + channel;
  256.             BCBASE_INT16 *input_ptr = (BCBASE_INT16*)read_buffer;
  257.             BCBASE_INT16 *input_end = input_ptr + len;
  258.  
  259. // Interlace the audio for the sound driver.
  260.             while(input_ptr < input_end)
  261.             {
  262.                 *output_ptr = *input_ptr++;
  263.                 output_ptr += asset->channels;
  264.             }
  265.         }
  266.     }
  267.  
  268.     audio_position += len;
  269.     return 0;
  270. }
  271.  
  272. int FileMOV::load_into_ram()
  273. {
  274.     if(asset->video_data)
  275.     {
  276. //        quicktime_cache_frames(file, 0);
  277.     }
  278.     return 0;
  279. }
  280.