r956: README.BUILD - add more library recommendations
[cinelerra_cv/ct.git] / cinelerra / asset.C
blob2fc4772a751ab2dacb740cb1a6a9669cec0414c8
2 #include "asset.h"
3 #include "assets.h"
4 #include "bchash.h"
5 #include "bcsignals.h"
6 #include "clip.h"
7 #include "edl.h"
8 #include "file.h"
9 #include "filesystem.h"
10 #include "filexml.h"
11 #include "quicktime.h"
12 #include "interlacemodes.h"
15 #include <stdio.h>
16 #include <string.h>
19 Asset::Asset()
20  : ListItem<Asset>(), GarbageObject("Asset")
22         init_values();
25 Asset::Asset(Asset &asset)
26  : ListItem<Asset>(), GarbageObject("Asset")
28         init_values();
29         *this = asset;
32 Asset::Asset(const char *path)
33  : ListItem<Asset>(), GarbageObject("Asset")
35         init_values();
36         strcpy(this->path, path);
39 Asset::Asset(const int plugin_type, const char *plugin_title)
40  : ListItem<Asset>(), GarbageObject("Asset")
42         init_values();
45 Asset::~Asset()
47         delete [] index_offsets;
48         delete [] index_sizes;
49 // Don't delete index buffer since it is shared with the index thread.
53 int Asset::init_values()
55         path[0] = 0;
56         strcpy(folder, MEDIA_FOLDER);
57 //      format = FILE_MOV;
58 // Has to be unknown for file probing to succeed
59         format = FILE_UNKNOWN;
60         channels = 0;
61         sample_rate = 0;
62         bits = 0;
63         byte_order = 0;
64         signed_ = 0;
65         header = 0;
66         dither = 0;
67         audio_data = 0;
68         video_data = 0;
69         audio_length = 0;
70         video_length = 0;
72         layers = 0;
73         frame_rate = 0;
74         width = 0;
75         height = 0;
76         strcpy(vcodec, QUICKTIME_YUV2);
77         strcpy(acodec, QUICKTIME_TWOS);
78         jpeg_quality = 100;
79         aspect_ratio = -1;
80         interlace_autofixoption = BC_ILACE_AUTOFIXOPTION_AUTO;
81         interlace_mode = BC_ILACE_MODE_UNDETECTED;
82         interlace_fixmethod = BC_ILACE_FIXMETHOD_NONE;
84         ampeg_bitrate = 256;
85         ampeg_derivative = 3;
87         vorbis_vbr = 0;
88         vorbis_min_bitrate = -1;
89         vorbis_bitrate = 128000;
90         vorbis_max_bitrate = -1;
92         theora_fix_bitrate = 1;
93         theora_bitrate = 860000;
94         theora_quality = 16;
95         theora_sharpness = 2;
96         theora_keyframe_frequency = 64;
97         theora_keyframe_force_frequency = 64;
99         mp3_bitrate = 256000;
102         mp4a_bitrate = 256000;
103         mp4a_quantqual = 100;
107 // mpeg parameters
108         vmpeg_iframe_distance = 45;
109         vmpeg_pframe_distance = 0;
110         vmpeg_progressive = 0;
111         vmpeg_denoise = 1;
112         vmpeg_bitrate = 1000000;
113         vmpeg_derivative = 1;
114         vmpeg_quantization = 15;
115         vmpeg_cmodel = 0;
116         vmpeg_fix_bitrate = 0;
117         vmpeg_seq_codes = 0;
118         vmpeg_preset = 0;
119         vmpeg_field_order = 0;
121 // Divx parameters.  BC_Hash from encore2
122         divx_bitrate = 2000000;
123         divx_rc_period = 50;
124         divx_rc_reaction_ratio = 45;
125         divx_rc_reaction_period = 10;
126         divx_max_key_interval = 250;
127         divx_max_quantizer = 31;
128         divx_min_quantizer = 1;
129         divx_quantizer = 5;
130         divx_quality = 5;
131         divx_fix_bitrate = 1;
132         divx_use_deblocking = 1;
134         h264_bitrate = 2000000;
135         h264_quantizer = 5;
136         h264_fix_bitrate = 0;
138         ms_bitrate = 1000000;
139         ms_bitrate_tolerance = 500000;
140         ms_quantization = 10;
141         ms_interlaced = 0;
142         ms_gop_size = 45;
143         ms_fix_bitrate = 1;
145         ac3_bitrate = 128;
147         png_use_alpha = 0;
148         exr_use_alpha = 0;
149         exr_compression = 0;
151         tiff_cmodel = 0;
152         tiff_compression = 0;
154         use_header = 1;
157         reset_index();
158         id = EDL::next_id();
160         pipe[0] = 0;
161         use_pipe = 0;
163         strcpy(prefix, "");
165         reset_timecode();
167         return 0;
170 int Asset::reset_index()
172         index_status = INDEX_NOTTESTED;
173         index_start = old_index_end = index_end = 0;
174         index_offsets = 0;
175         index_sizes = 0;
176         index_zoom = 0;
177         index_bytes = 0;
178         index_buffer = 0;
179         return 0;
182 int Asset::reset_timecode()
184         strcpy(reel_name, "cin0000");
185         reel_number = 0;
186         tcstart = 0;
187         tcend = 0;
188         tcformat = 0;
189         
190         return 0;
193 void Asset::copy_from(Asset *asset, int do_index)
195         copy_location(asset);
196         copy_format(asset, do_index);
199 void Asset::copy_location(Asset *asset)
201         strcpy(this->path, asset->path);
202         strcpy(this->folder, asset->folder);
205 void Asset::copy_format(Asset *asset, int do_index)
207         if(do_index) update_index(asset);
209         audio_data = asset->audio_data;
210         format = asset->format;
211         channels = asset->channels;
212         sample_rate = asset->sample_rate;
213         bits = asset->bits;
214         byte_order = asset->byte_order;
215         signed_ = asset->signed_;
216         header = asset->header;
217         dither = asset->dither;
218         mp3_bitrate = asset->mp3_bitrate;
219         mp4a_bitrate = asset->mp4a_bitrate;
220         mp4a_quantqual = asset->mp4a_quantqual;
221         use_header = asset->use_header;
222         aspect_ratio = asset->aspect_ratio;
223         interlace_autofixoption = asset->interlace_autofixoption;
224         interlace_mode = asset->interlace_mode;
225         interlace_fixmethod = asset->interlace_fixmethod;
227         video_data = asset->video_data;
228         layers = asset->layers;
229         frame_rate = asset->frame_rate;
230         width = asset->width;
231         height = asset->height;
232         strcpy(vcodec, asset->vcodec);
233         strcpy(acodec, asset->acodec);
235         this->audio_length = asset->audio_length;
236         this->video_length = asset->video_length;
239         ampeg_bitrate = asset->ampeg_bitrate;
240         ampeg_derivative = asset->ampeg_derivative;
243         vorbis_vbr = asset->vorbis_vbr;
244         vorbis_min_bitrate = asset->vorbis_min_bitrate;
245         vorbis_bitrate = asset->vorbis_bitrate;
246         vorbis_max_bitrate = asset->vorbis_max_bitrate;
248         
249         theora_fix_bitrate = asset->theora_fix_bitrate;
250         theora_bitrate = asset->theora_bitrate;
251         theora_quality = asset->theora_quality;
252         theora_sharpness = asset->theora_sharpness;
253         theora_keyframe_frequency = asset->theora_keyframe_frequency;
254         theora_keyframe_force_frequency = asset->theora_keyframe_frequency;
257         jpeg_quality = asset->jpeg_quality;
259 // mpeg parameters
260         vmpeg_iframe_distance = asset->vmpeg_iframe_distance;
261         vmpeg_pframe_distance = asset->vmpeg_pframe_distance;
262         vmpeg_progressive = asset->vmpeg_progressive;
263         vmpeg_denoise = asset->vmpeg_denoise;
264         vmpeg_bitrate = asset->vmpeg_bitrate;
265         vmpeg_derivative = asset->vmpeg_derivative;
266         vmpeg_quantization = asset->vmpeg_quantization;
267         vmpeg_cmodel = asset->vmpeg_cmodel;
268         vmpeg_fix_bitrate = asset->vmpeg_fix_bitrate;
269         vmpeg_seq_codes = asset->vmpeg_seq_codes;
270         vmpeg_preset = asset->vmpeg_preset;
271         vmpeg_field_order = asset->vmpeg_field_order;
274         divx_bitrate = asset->divx_bitrate;
275         divx_rc_period = asset->divx_rc_period;
276         divx_rc_reaction_ratio = asset->divx_rc_reaction_ratio;
277         divx_rc_reaction_period = asset->divx_rc_reaction_period;
278         divx_max_key_interval = asset->divx_max_key_interval;
279         divx_max_quantizer = asset->divx_max_quantizer;
280         divx_min_quantizer = asset->divx_min_quantizer;
281         divx_quantizer = asset->divx_quantizer;
282         divx_quality = asset->divx_quality;
283         divx_fix_bitrate = asset->divx_fix_bitrate;
284         divx_use_deblocking = asset->divx_use_deblocking;
286         h264_bitrate = asset->h264_bitrate;
287         h264_quantizer = asset->h264_quantizer;
288         h264_fix_bitrate = asset->h264_fix_bitrate;
291         ms_bitrate = asset->ms_bitrate;
292         ms_bitrate_tolerance = asset->ms_bitrate_tolerance;
293         ms_interlaced = asset->ms_interlaced;
294         ms_quantization = asset->ms_quantization;
295         ms_gop_size = asset->ms_gop_size;
296         ms_fix_bitrate = asset->ms_fix_bitrate;
298         
299         ac3_bitrate = asset->ac3_bitrate;
300         
301         png_use_alpha = asset->png_use_alpha;
302         exr_use_alpha = asset->exr_use_alpha;
303         exr_compression = asset->exr_compression;
305         tiff_cmodel = asset->tiff_cmodel;
306         tiff_compression = asset->tiff_compression;
308         strcpy(pipe, asset->pipe);
309         use_pipe = asset->use_pipe;
311         // FUTURE: should this be here or in copy_from()?
312         strcpy(prefix, asset->prefix);
314         strcpy(reel_name, asset->reel_name);
315         reel_number = asset->reel_number;
316         tcstart = asset->tcstart;
317         tcend = asset->tcend;
318         tcformat = asset->tcformat;
321 int64_t Asset::get_index_offset(int channel)
323         if(channel < channels && index_offsets)
324                 return index_offsets[channel];
325         else
326                 return 0;
329 int64_t Asset::get_index_size(int channel)
331         if(channel < channels && index_sizes)
332                 return index_sizes[channel];
333         else
334                 return 0;
338 char* Asset::get_compression_text(int audio, int video)
340         if(audio)
341         {
342                 switch(format)
343                 {
344                         case FILE_MOV:
345                         case FILE_AVI:
346                                 if(acodec[0])
347                                         return quicktime_acodec_title(acodec);
348                                 else
349                                         return 0;
350                                 break;
351                 }
352         }
353         else
354         if(video)
355         {
356                 switch(format)
357                 {
358                         case FILE_MOV:
359                         case FILE_AVI:
360                                 if(vcodec[0])
361                                         return quicktime_vcodec_title(vcodec);
362                                 else
363                                         return 0;
364                                 break;
365                 }
366         }
367         return 0;
370 Asset& Asset::operator=(Asset &asset)
372         copy_location(&asset);
373         copy_format(&asset, 1);
374         return *this;
378 int Asset::equivalent(Asset &asset, 
379         int test_audio, 
380         int test_video)
382         int result = (!strcmp(asset.path, path) &&
383                 format == asset.format);
385         if(test_audio && result)
386         {
387                 result = (channels == asset.channels && 
388                         sample_rate == asset.sample_rate && 
389                         bits == asset.bits && 
390                         byte_order == asset.byte_order && 
391                         signed_ == asset.signed_ && 
392                         header == asset.header && 
393                         dither == asset.dither &&
394                         !strcmp(acodec, asset.acodec));
395         }
398         if(test_video && result)
399         {
400                 result = (layers == asset.layers && 
401                         frame_rate == asset.frame_rate &&
402                         asset.interlace_autofixoption == interlace_autofixoption &&
403                         asset.interlace_mode    == interlace_mode &&
404                         interlace_fixmethod     == asset.interlace_fixmethod &&
405                         width == asset.width &&
406                         height == asset.height &&
407                         !strcmp(vcodec, asset.vcodec) &&
408                         strcmp(reel_name, asset.reel_name) == 0 &&
409                         reel_number == asset.reel_number &&
410                         tcstart == asset.tcstart &&
411                         tcend == asset.tcend &&
412                         tcformat == asset.tcformat);
413         }
415         return result;
418 int Asset::operator==(Asset &asset)
421         return equivalent(asset, 
422                 1, 
423                 1);
426 int Asset::operator!=(Asset &asset)
428         return !(*this == asset);
431 int Asset::test_path(const char *path)
433         if(!strcasecmp(this->path, path)) 
434                 return 1; 
435         else 
436                 return 0;
439 int Asset::test_plugin_title(const char *path)
443 int Asset::read(FileXML *file, 
444         int expand_relative)
446         int result = 0;
448 // Check for relative path.
449         if(expand_relative)
450         {
451                 char new_path[BCTEXTLEN];
452                 char asset_directory[BCTEXTLEN];
453                 char input_directory[BCTEXTLEN];
454                 FileSystem fs;
456                 strcpy(new_path, path);
457                 fs.set_current_dir("");
459                 fs.extract_dir(asset_directory, path);
461 // No path in asset.
462 // Take path of XML file.
463                 if(!asset_directory[0])
464                 {
465                         fs.extract_dir(input_directory, file->filename);
467 // Input file has a path
468                         if(input_directory[0])
469                         {
470                                 fs.join_names(path, input_directory, new_path);
471                         }
472                 }
473         }
476         while(!result)
477         {
478                 result = file->read_tag();
479                 if(!result)
480                 {
481                         if(file->tag.title_is("/ASSET"))
482                         {
483                                 result = 1;
484                         }
485                         else
486                         if(file->tag.title_is("AUDIO"))
487                         {
488                                 read_audio(file);
489                         }
490                         else
491                         if(file->tag.title_is("AUDIO_OMIT"))
492                         {
493                                 read_audio(file);
494                         }
495                         else
496                         if(file->tag.title_is("FORMAT"))
497                         {
498                                 char *string = file->tag.get_property("TYPE");
499                                 format = File::strtoformat(string);
500                                 use_header = 
501                                         file->tag.get_property("USE_HEADER", use_header);
502                         }
503                         else
504                         if(file->tag.title_is("FOLDER"))
505                         {
506                                 strcpy(folder, file->read_text());
507                         }
508                         else
509                         if(file->tag.title_is("VIDEO"))
510                         {
511                                 read_video(file);
512                         }
513                         else
514                         if(file->tag.title_is("VIDEO_OMIT"))
515                         {
516                                 read_video(file);
517                         }
518                         else
519                         if(file->tag.title_is("INDEX"))
520                         {
521                                 read_index(file);
522                         }
523                 }
524         }
526 //printf("Asset::read 2\n");
527         return 0;
530 int Asset::read_audio(FileXML *file)
532         if(file->tag.title_is("AUDIO")) audio_data = 1;
533         channels = file->tag.get_property("CHANNELS", 2);
534 // This is loaded from the index file after the EDL but this 
535 // should be overridable in the EDL.
536         if(!sample_rate) sample_rate = file->tag.get_property("RATE", 44100);
537         bits = file->tag.get_property("BITS", 16);
538         byte_order = file->tag.get_property("BYTE_ORDER", 1);
539         signed_ = file->tag.get_property("SIGNED", 1);
540         header = file->tag.get_property("HEADER", 0);
541         dither = file->tag.get_property("DITHER", 0);
543         audio_length = file->tag.get_property("AUDIO_LENGTH", 0);
544         acodec[0] = 0;
545         file->tag.get_property("ACODEC", acodec);
546         
549 //      ampeg_bitrate = file->tag.get_property("AMPEG_BITRATE", ampeg_bitrate);
550 //      ampeg_derivative = file->tag.get_property("AMPEG_DERIVATIVE", ampeg_derivative);
551 // 
552 //      vorbis_vbr = file->tag.get_property("VORBIS_VBR", vorbis_vbr);
553 //      vorbis_min_bitrate = file->tag.get_property("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
554 //      vorbis_bitrate = file->tag.get_property("VORBIS_BITRATE", vorbis_bitrate);
555 //      vorbis_max_bitrate = file->tag.get_property("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
556 // 
557 //      mp3_bitrate = file->tag.get_property("MP3_BITRATE", mp3_bitrate);
559         if(!video_data)
560         {
561                 tcstart = 0;
562                 tcend = audio_length;
563                 tcformat = 0;
564         }
566         return 0;
569 int Asset::read_video(FileXML *file)
571         char string[BCTEXTLEN];
573         if(file->tag.title_is("VIDEO")) video_data = 1;
574         height = file->tag.get_property("HEIGHT", height);
575         width = file->tag.get_property("WIDTH", width);
576         layers = file->tag.get_property("LAYERS", layers);
577 // This is loaded from the index file after the EDL but this 
578 // should be overridable in the EDL.
579         if(!frame_rate) frame_rate = file->tag.get_property("FRAMERATE", frame_rate);
580         vcodec[0] = 0;
581         file->tag.get_property("VCODEC", vcodec);
583         video_length = file->tag.get_property("VIDEO_LENGTH", 0);
585         interlace_autofixoption = file->tag.get_property("INTERLACE_AUTOFIX",0);
587         ilacemode_to_xmltext(string, BC_ILACE_MODE_NOTINTERLACED);
588         interlace_mode = ilacemode_from_xmltext(file->tag.get_property("INTERLACE_MODE",string), BC_ILACE_MODE_NOTINTERLACED);
590         ilacefixmethod_to_xmltext(string, BC_ILACE_FIXMETHOD_NONE);
591         interlace_fixmethod = ilacefixmethod_from_xmltext(file->tag.get_property("INTERLACE_FIXMETHOD",string), BC_ILACE_FIXMETHOD_NONE);
593         file->tag.get_property("REEL_NAME", reel_name);
594         reel_number = file->tag.get_property("REEL_NUMBER", reel_number);
595         tcstart = file->tag.get_property("TCSTART", tcstart);
596         tcend = file->tag.get_property("TCEND", tcend);
597         tcformat = file->tag.get_property("TCFORMAT", tcformat);
599         return 0;
602 int Asset::read_index(FileXML *file)
604         delete [] index_offsets;
605         index_offsets = new int64_t[channels];
606         delete [] index_sizes;
607         index_sizes = new int64_t[channels];
608         for(int i = 0; i < channels; i++) 
609         {
610                 index_offsets[i] = 0;
611                 index_sizes[i] = 0;
612         }
614         int current_offset = 0;
615         int current_size = 0;
616         int result = 0;
618         index_zoom = file->tag.get_property("ZOOM", 1);
619         index_bytes = file->tag.get_property("BYTES", (int64_t)0);
621         while(!result)
622         {
623                 result = file->read_tag();
624                 if(!result)
625                 {
626                         if(file->tag.title_is("/INDEX"))
627                         {
628                                 result = 1;
629                         }
630                         else
631                         if(file->tag.title_is("OFFSET"))
632                         {
633                                 if(current_offset < channels)
634                                 {
635                                         index_offsets[current_offset++] = file->tag.get_property("FLOAT", 0);
636 //printf("Asset::read_index %d %d\n", current_offset - 1, index_offsets[current_offset - 1]);
637                                 }
638                         }
639                         else
640                         if(file->tag.title_is("SIZE"))
641                         {
642                                 if(current_size < channels)
643                                 {
644                                         index_sizes[current_size++] = file->tag.get_property("FLOAT", 0);
645                                 }
646                         }
647                 }
648         }
649         return 0;
652 int Asset::write_index(char *path, int data_bytes)
654         FILE *file;
655         if(!(file = fopen(path, "wb")))
656         {
657 // failed to create it
658                 printf(_("Asset::write_index Couldn't write index file %s to disk.\n"), path);
659         }
660         else
661         {
662                 FileXML xml;
663 // Pad index start position
664                 fwrite((char*)&(index_start), sizeof(int64_t), 1, file);
666                 index_status = INDEX_READY;
667 // Write encoding information
668                 write(&xml, 
669                         1, 
670                         "");
671                 xml.write_to_file(file);
672                 index_start = ftell(file);
673                 fseek(file, 0, SEEK_SET);
674 // Write index start
675                 fwrite((char*)&(index_start), sizeof(int64_t), 1, file);
676                 fseek(file, index_start, SEEK_SET);
678 // Write index data
679                 fwrite(index_buffer, 
680                         data_bytes, 
681                         1, 
682                         file);
683                 fclose(file);
684         }
686 // Force reread of header
687         index_status = INDEX_NOTTESTED;
688 //      index_status = INDEX_READY;
689         index_end = audio_length;
690         old_index_end = 0;
691         index_start = 0;
694 // Output path is the path of the output file if name truncation is desired.
695 // It is a "" if complete names should be used.
697 int Asset::write(FileXML *file, 
698         int include_index, 
699         char *output_path)
701         char new_path[BCTEXTLEN];
702         char asset_directory[BCTEXTLEN];
703         char output_directory[BCTEXTLEN];
704         FileSystem fs;
706 // Make path relative
707         fs.extract_dir(asset_directory, path);
708         if(output_path && output_path[0]) 
709                 fs.extract_dir(output_directory, output_path);
710         else
711                 output_directory[0] = 0;
713 // Asset and EDL are in same directory.  Extract just the name.
714         if(!strcmp(asset_directory, output_directory))
715         {
716                 fs.extract_name(new_path, path);
717         }
718         else
719         {
720                 strcpy(new_path, path);
721         }
723         file->tag.set_title("ASSET");
724         file->tag.set_property("SRC", new_path);
725         file->append_tag();
726         file->append_newline();
728         file->tag.set_title("FOLDER");
729         file->append_tag();
730         file->append_text(folder);
731         file->tag.set_title("/FOLDER");
732         file->append_tag();
733         file->append_newline();
735 // Write the format information
736         file->tag.set_title("FORMAT");
738         file->tag.set_property("TYPE", 
739                 File::formattostr(format));
740         file->tag.set_property("USE_HEADER", use_header);
742         file->append_tag();
743         file->tag.set_title("/FORMAT");
744         file->append_tag();
745         file->append_newline();
747 // Requiring data to exist caused batch render to lose settings.
748 // But the only way to know if an asset doesn't have audio or video data 
749 // is to not write the block.
750 // So change the block name if the asset doesn't have the data.
751         /* if(audio_data) */ write_audio(file);
752         /* if(video_data) */ write_video(file);
753         if(index_status == 0 && include_index) write_index(file);  // index goes after source
755         file->tag.set_title("/ASSET");
756         file->append_tag();
757         file->append_newline();
758         return 0;
761 int Asset::write_audio(FileXML *file)
763 // Let the reader know if the asset has the data by naming the block.
764         if(audio_data)
765                 file->tag.set_title("AUDIO");
766         else
767                 file->tag.set_title("AUDIO_OMIT");
768 // Necessary for PCM audio
769         file->tag.set_property("CHANNELS", channels);
770         file->tag.set_property("RATE", sample_rate);
771         file->tag.set_property("BITS", bits);
772         file->tag.set_property("BYTE_ORDER", byte_order);
773         file->tag.set_property("SIGNED", signed_);
774         file->tag.set_property("HEADER", header);
775         file->tag.set_property("DITHER", dither);
776         if(acodec[0])
777                 file->tag.set_property("ACODEC", acodec);
778         
779         file->tag.set_property("AUDIO_LENGTH", audio_length);
783 // Rely on defaults operations for these.
785 //      file->tag.set_property("AMPEG_BITRATE", ampeg_bitrate);
786 //      file->tag.set_property("AMPEG_DERIVATIVE", ampeg_derivative);
787 // 
788 //      file->tag.set_property("VORBIS_VBR", vorbis_vbr);
789 //      file->tag.set_property("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
790 //      file->tag.set_property("VORBIS_BITRATE", vorbis_bitrate);
791 //      file->tag.set_property("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
792 // 
793 //      file->tag.set_property("MP3_BITRATE", mp3_bitrate);
794 // 
798         file->append_tag();
799         if(audio_data)
800           file->tag.set_title("/AUDIO");
801         else
802           file->tag.set_title("/AUDIO_OMIT");
803         file->append_tag();
804         file->append_newline();
805         return 0;
808 int Asset::write_video(FileXML *file)
810         char string[BCTEXTLEN];
812         if(video_data)
813                 file->tag.set_title("VIDEO");
814         else
815                 file->tag.set_title("VIDEO_OMIT");
816         file->tag.set_property("HEIGHT", height);
817         file->tag.set_property("WIDTH", width);
818         file->tag.set_property("LAYERS", layers);
819         file->tag.set_property("FRAMERATE", frame_rate);
820         if(vcodec[0])
821                 file->tag.set_property("VCODEC", vcodec);
823         file->tag.set_property("VIDEO_LENGTH", video_length);
825         file->tag.set_property("INTERLACE_AUTOFIX", interlace_autofixoption);
827         ilacemode_to_xmltext(string, interlace_mode);
828         file->tag.set_property("INTERLACE_MODE", string);
830         ilacefixmethod_to_xmltext(string, interlace_fixmethod);
831         file->tag.set_property("INTERLACE_FIXMETHOD", string);
834         file->tag.set_property("REEL_NAME", reel_name);
835         file->tag.set_property("REEL_NUMBER", reel_number);
836         file->tag.set_property("TCSTART", tcstart);
837         file->tag.set_property("TCEND", tcend);
838         file->tag.set_property("TCFORMAT", tcformat);
840         file->append_tag();
841         if(video_data)
842                 file->tag.set_title("/VIDEO");
843         else
844                 file->tag.set_title("/VIDEO_OMIT");
846         file->append_tag();
847         file->append_newline();
848         return 0;
851 int Asset::write_index(FileXML *file)
853         file->tag.set_title("INDEX");
854         file->tag.set_property("ZOOM", index_zoom);
855         file->tag.set_property("BYTES", index_bytes);
856         file->append_tag();
857         file->append_newline();
859         if(index_offsets)
860         {
861                 for(int i = 0; i < channels; i++)
862                 {
863                         file->tag.set_title("OFFSET");
864                         file->tag.set_property("FLOAT", index_offsets[i]);
865                         file->append_tag();
866                         file->tag.set_title("/OFFSET");
867                         file->append_tag();
868                         file->tag.set_title("SIZE");
869                         file->tag.set_property("FLOAT", index_sizes[i]);
870                         file->append_tag();
871                         file->tag.set_title("/SIZE");
872                         file->append_tag();
873                 }
874         }
876         file->append_newline();
877         file->tag.set_title("/INDEX");
878         file->append_tag();
879         file->append_newline();
880         return 0;
886 char* Asset::construct_param(char *param, char *prefix, char *return_value)
888         if(prefix)
889                 sprintf(return_value, "%s%s", prefix, param);
890         else
891                 strcpy(return_value, param);
892         return return_value;
895 #define UPDATE_DEFAULT(x, y) defaults->update(construct_param(x, prefix, string), y);
896 #define GET_DEFAULT(x, y) defaults->get(construct_param(x, prefix, string), y);
898 void Asset::load_defaults(BC_Hash *defaults, 
899         char *prefix, 
900         int do_format,
901         int do_compression,
902         int do_path,
903         int do_data_types,
904         int do_bits)
906         char string[BCTEXTLEN];
908 // Can't save codec here because it's specific to render, record, and effect.
909 // The codec has to be UNKNOWN for file probing to work.
911         if(do_path)
912         {
913                 GET_DEFAULT("PATH", path);
914         }
916         if(do_compression)
917         {
918                 GET_DEFAULT("AUDIO_CODEC", acodec);
919                 GET_DEFAULT("VIDEO_CODEC", vcodec);
920         }
922         if(do_format)
923         {
924                 format = GET_DEFAULT("FORMAT", format);
925         }
927         if(do_data_types)
928         {
929                 audio_data = GET_DEFAULT("AUDIO", 1);
930                 video_data = GET_DEFAULT("VIDEO", 1);
931         }
933         if(do_bits)
934         {
935                 bits = GET_DEFAULT("BITS", 16);
936                 dither = GET_DEFAULT("DITHER", 0);
937                 signed_ = GET_DEFAULT("SIGNED", 1);
938                 byte_order = GET_DEFAULT("BYTE_ORDER", 1);
939         }
941         // NOTE: this should never be saved
942         strcpy(this->prefix, prefix ? prefix : "");
944         ampeg_bitrate = GET_DEFAULT("AMPEG_BITRATE", ampeg_bitrate);
945         ampeg_derivative = GET_DEFAULT("AMPEG_DERIVATIVE", ampeg_derivative);
947         vorbis_vbr = GET_DEFAULT("VORBIS_VBR", vorbis_vbr);
948         vorbis_min_bitrate = GET_DEFAULT("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
949         vorbis_bitrate = GET_DEFAULT("VORBIS_BITRATE", vorbis_bitrate);
950         vorbis_max_bitrate = GET_DEFAULT("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
952         theora_fix_bitrate = GET_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
953         theora_bitrate = GET_DEFAULT("THEORA_BITRATE", theora_bitrate);
954         theora_quality = GET_DEFAULT("THEORA_QUALITY", theora_quality);
955         theora_sharpness = GET_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
956         theora_keyframe_frequency = GET_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
957         theora_keyframe_force_frequency = GET_DEFAULT("THEORA_FORCE_KEYFRAME_FEQUENCY", theora_keyframe_force_frequency);
961         mp3_bitrate = GET_DEFAULT("MP3_BITRATE", mp3_bitrate);
962         mp4a_bitrate = GET_DEFAULT("MP4A_BITRATE", mp4a_bitrate);
963         mp4a_quantqual = GET_DEFAULT("MP4A_QUANTQUAL", mp4a_quantqual);
965         jpeg_quality = GET_DEFAULT("JPEG_QUALITY", jpeg_quality);
966         aspect_ratio = GET_DEFAULT("ASPECT_RATIO", aspect_ratio);
968         interlace_autofixoption = BC_ILACE_AUTOFIXOPTION_AUTO;
969         interlace_mode          = BC_ILACE_MODE_UNDETECTED;
970         interlace_fixmethod     = BC_ILACE_FIXMETHOD_UPONE;
972 // MPEG format information
973         vmpeg_iframe_distance = GET_DEFAULT("VMPEG_IFRAME_DISTANCE", vmpeg_iframe_distance);
974         vmpeg_pframe_distance = GET_DEFAULT("VMPEG_PFRAME_DISTANCE", vmpeg_pframe_distance);
975         vmpeg_progressive = GET_DEFAULT("VMPEG_PROGRESSIVE", vmpeg_progressive);
976         vmpeg_denoise = GET_DEFAULT("VMPEG_DENOISE", vmpeg_denoise);
977         vmpeg_bitrate = GET_DEFAULT("VMPEG_BITRATE", vmpeg_bitrate);
978         vmpeg_derivative = GET_DEFAULT("VMPEG_DERIVATIVE", vmpeg_derivative);
979         vmpeg_quantization = GET_DEFAULT("VMPEG_QUANTIZATION", vmpeg_quantization);
980         vmpeg_cmodel = GET_DEFAULT("VMPEG_CMODEL", vmpeg_cmodel);
981         vmpeg_fix_bitrate = GET_DEFAULT("VMPEG_FIX_BITRATE", vmpeg_fix_bitrate);
982         vmpeg_seq_codes = GET_DEFAULT("VMPEG_SEQ_CODES", vmpeg_seq_codes);
983         vmpeg_preset = GET_DEFAULT("VMPEG_PRESET", vmpeg_preset);
984         vmpeg_field_order = GET_DEFAULT("VMPEG_FIELD_ORDER", vmpeg_field_order);
986         h264_bitrate = GET_DEFAULT("H264_BITRATE", h264_bitrate);
987         h264_quantizer = GET_DEFAULT("H264_QUANTIZER", h264_quantizer);
988         h264_fix_bitrate = GET_DEFAULT("H264_FIX_BITRATE", h264_fix_bitrate);
991         divx_bitrate = GET_DEFAULT("DIVX_BITRATE", divx_bitrate);
992         divx_rc_period = GET_DEFAULT("DIVX_RC_PERIOD", divx_rc_period);
993         divx_rc_reaction_ratio = GET_DEFAULT("DIVX_RC_REACTION_RATIO", divx_rc_reaction_ratio);
994         divx_rc_reaction_period = GET_DEFAULT("DIVX_RC_REACTION_PERIOD", divx_rc_reaction_period);
995         divx_max_key_interval = GET_DEFAULT("DIVX_MAX_KEY_INTERVAL", divx_max_key_interval);
996         divx_max_quantizer = GET_DEFAULT("DIVX_MAX_QUANTIZER", divx_max_quantizer);
997         divx_min_quantizer = GET_DEFAULT("DIVX_MIN_QUANTIZER", divx_min_quantizer);
998         divx_quantizer = GET_DEFAULT("DIVX_QUANTIZER", divx_quantizer);
999         divx_quality = GET_DEFAULT("DIVX_QUALITY", divx_quality);
1000         divx_fix_bitrate = GET_DEFAULT("DIVX_FIX_BITRATE", divx_fix_bitrate);
1001         divx_use_deblocking = GET_DEFAULT("DIVX_USE_DEBLOCKING", divx_use_deblocking);
1003         theora_fix_bitrate = GET_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
1004         theora_bitrate = GET_DEFAULT("THEORA_BITRATE", theora_bitrate);
1005         theora_quality = GET_DEFAULT("THEORA_QUALITY", theora_quality);
1006         theora_sharpness = GET_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
1007         theora_keyframe_frequency = GET_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
1008         theora_keyframe_force_frequency = GET_DEFAULT("THEORA_FORCE_KEYFRAME_FEQUENCY", theora_keyframe_force_frequency);
1011         ms_bitrate = GET_DEFAULT("MS_BITRATE", ms_bitrate);
1012         ms_bitrate_tolerance = GET_DEFAULT("MS_BITRATE_TOLERANCE", ms_bitrate_tolerance);
1013         ms_interlaced = GET_DEFAULT("MS_INTERLACED", ms_interlaced);
1014         ms_quantization = GET_DEFAULT("MS_QUANTIZATION", ms_quantization);
1015         ms_gop_size = GET_DEFAULT("MS_GOP_SIZE", ms_gop_size);
1016         ms_fix_bitrate = GET_DEFAULT("MS_FIX_BITRATE", ms_fix_bitrate);
1018         ac3_bitrate = GET_DEFAULT("AC3_BITRATE", ac3_bitrate);
1020         png_use_alpha = GET_DEFAULT("PNG_USE_ALPHA", png_use_alpha);
1021         exr_use_alpha = GET_DEFAULT("EXR_USE_ALPHA", exr_use_alpha);
1022         exr_compression = GET_DEFAULT("EXR_COMPRESSION", exr_compression);
1023         tiff_cmodel = GET_DEFAULT("TIFF_CMODEL", tiff_cmodel);
1024         tiff_compression = GET_DEFAULT("TIFF_COMPRESSION", tiff_compression);
1026         GET_DEFAULT("REEL_NAME", reel_name);
1027         reel_number = GET_DEFAULT("REEL_NUMBER", reel_number);
1028         tcstart = GET_DEFAULT("TCSTART", tcstart);
1029         tcend = GET_DEFAULT("TCEND", tcend);
1030         tcformat = GET_DEFAULT("TCFORMAT", tcformat);
1032         load_format_defaults(defaults);
1035 // FUTURE: put more of the format specific variables in here
1036 void Asset::load_format_defaults(BC_Hash *defaults) {
1037         char temp[BCTEXTLEN];
1038         char string[BCTEXTLEN];
1039         if (! format) return;
1041         // NOTE: old value is used if no init value set before GET_DEFAULT 
1043         // override the defaults with those for this format
1044         sprintf(temp, "FORMAT_%s_USE_PIPE", FILE_FORMAT_PREFIX(format));
1045         use_pipe = 0;  
1046         use_pipe = GET_DEFAULT(temp, use_pipe);
1048         sprintf(temp, "FORMAT_%s_PIPE", FILE_FORMAT_PREFIX(format));
1049         sprintf(pipe, "");
1050         GET_DEFAULT(temp, pipe);
1052         sprintf(temp, "FORMAT_%s_PATH", FILE_FORMAT_PREFIX(format));
1053         sprintf(path, "");
1054         GET_DEFAULT(temp, path);
1056         
1058 void Asset::save_defaults(BC_Hash *defaults, 
1059         char *prefix,
1060         int do_format,
1061         int do_compression,
1062         int do_path,
1063         int do_data_types,
1064         int do_bits)
1066         char string[BCTEXTLEN];
1068         UPDATE_DEFAULT("PATH", path);
1070         if(do_format)
1071         {
1072                 UPDATE_DEFAULT("FORMAT", format);
1073         }
1075         if(do_data_types)
1076         {
1077                 UPDATE_DEFAULT("AUDIO", audio_data);
1078                 UPDATE_DEFAULT("VIDEO", video_data);
1079         }
1081         if(do_compression)
1082         {
1083                 UPDATE_DEFAULT("AUDIO_CODEC", acodec);
1084                 UPDATE_DEFAULT("VIDEO_CODEC", vcodec);
1086                 UPDATE_DEFAULT("AMPEG_BITRATE", ampeg_bitrate);
1087                 UPDATE_DEFAULT("AMPEG_DERIVATIVE", ampeg_derivative);
1089                 UPDATE_DEFAULT("VORBIS_VBR", vorbis_vbr);
1090                 UPDATE_DEFAULT("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
1091                 UPDATE_DEFAULT("VORBIS_BITRATE", vorbis_bitrate);
1092                 UPDATE_DEFAULT("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
1095                 UPDATE_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
1096                 UPDATE_DEFAULT("THEORA_BITRATE", theora_bitrate);
1097                 UPDATE_DEFAULT("THEORA_QUALITY", theora_quality);
1098                 UPDATE_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
1099                 UPDATE_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
1100                 UPDATE_DEFAULT("THEORA_FORCE_KEYFRAME_FEQUENCY", theora_keyframe_force_frequency);
1104                 UPDATE_DEFAULT("MP3_BITRATE", mp3_bitrate);
1105                 UPDATE_DEFAULT("MP4A_BITRATE", mp4a_bitrate);
1106                 UPDATE_DEFAULT("MP4A_QUANTQUAL", mp4a_quantqual);
1112                 UPDATE_DEFAULT("JPEG_QUALITY", jpeg_quality);
1113                 UPDATE_DEFAULT("ASPECT_RATIO", aspect_ratio);
1115 // MPEG format information
1116                 UPDATE_DEFAULT("VMPEG_IFRAME_DISTANCE", vmpeg_iframe_distance);
1117                 UPDATE_DEFAULT("VMPEG_PFRAME_DISTANCE", vmpeg_pframe_distance);
1118                 UPDATE_DEFAULT("VMPEG_PROGRESSIVE", vmpeg_progressive);
1119                 UPDATE_DEFAULT("VMPEG_DENOISE", vmpeg_denoise);
1120                 UPDATE_DEFAULT("VMPEG_BITRATE", vmpeg_bitrate);
1121                 UPDATE_DEFAULT("VMPEG_DERIVATIVE", vmpeg_derivative);
1122                 UPDATE_DEFAULT("VMPEG_QUANTIZATION", vmpeg_quantization);
1123                 UPDATE_DEFAULT("VMPEG_CMODEL", vmpeg_cmodel);
1124                 UPDATE_DEFAULT("VMPEG_FIX_BITRATE", vmpeg_fix_bitrate);
1125                 UPDATE_DEFAULT("VMPEG_SEQ_CODES", vmpeg_seq_codes);
1126                 UPDATE_DEFAULT("VMPEG_PRESET", vmpeg_preset);
1127                 UPDATE_DEFAULT("VMPEG_FIELD_ORDER", vmpeg_field_order);
1129                 UPDATE_DEFAULT("H264_BITRATE", h264_bitrate);
1130                 UPDATE_DEFAULT("H264_QUANTIZER", h264_quantizer);
1131                 UPDATE_DEFAULT("H264_FIX_BITRATE", h264_fix_bitrate);
1133                 UPDATE_DEFAULT("DIVX_BITRATE", divx_bitrate);
1134                 UPDATE_DEFAULT("DIVX_RC_PERIOD", divx_rc_period);
1135                 UPDATE_DEFAULT("DIVX_RC_REACTION_RATIO", divx_rc_reaction_ratio);
1136                 UPDATE_DEFAULT("DIVX_RC_REACTION_PERIOD", divx_rc_reaction_period);
1137                 UPDATE_DEFAULT("DIVX_MAX_KEY_INTERVAL", divx_max_key_interval);
1138                 UPDATE_DEFAULT("DIVX_MAX_QUANTIZER", divx_max_quantizer);
1139                 UPDATE_DEFAULT("DIVX_MIN_QUANTIZER", divx_min_quantizer);
1140                 UPDATE_DEFAULT("DIVX_QUANTIZER", divx_quantizer);
1141                 UPDATE_DEFAULT("DIVX_QUALITY", divx_quality);
1142                 UPDATE_DEFAULT("DIVX_FIX_BITRATE", divx_fix_bitrate);
1143                 UPDATE_DEFAULT("DIVX_USE_DEBLOCKING", divx_use_deblocking);
1146                 UPDATE_DEFAULT("MS_BITRATE", ms_bitrate);
1147                 UPDATE_DEFAULT("MS_BITRATE_TOLERANCE", ms_bitrate_tolerance);
1148                 UPDATE_DEFAULT("MS_INTERLACED", ms_interlaced);
1149                 UPDATE_DEFAULT("MS_QUANTIZATION", ms_quantization);
1150                 UPDATE_DEFAULT("MS_GOP_SIZE", ms_gop_size);
1151                 UPDATE_DEFAULT("MS_FIX_BITRATE", ms_fix_bitrate);
1153                 UPDATE_DEFAULT("AC3_BITRATE", ac3_bitrate);
1156                 UPDATE_DEFAULT("PNG_USE_ALPHA", png_use_alpha);
1157                 UPDATE_DEFAULT("EXR_USE_ALPHA", exr_use_alpha);
1158                 UPDATE_DEFAULT("EXR_COMPRESSION", exr_compression);
1159                 UPDATE_DEFAULT("TIFF_CMODEL", tiff_cmodel);
1160                 UPDATE_DEFAULT("TIFF_COMPRESSION", tiff_compression);
1161         }
1163         if(do_bits)
1164         {
1165                 UPDATE_DEFAULT("BITS", bits);
1166                 UPDATE_DEFAULT("DITHER", dither);
1167                 UPDATE_DEFAULT("SIGNED", signed_);
1168                 UPDATE_DEFAULT("BYTE_ORDER", byte_order);
1169         }
1171         UPDATE_DEFAULT("REEL_NAME", reel_name);
1172         UPDATE_DEFAULT("REEL_NUMBER", reel_number);
1173         UPDATE_DEFAULT("TCSTART", tcstart);
1174         UPDATE_DEFAULT("TCEND", tcend);
1175         UPDATE_DEFAULT("TCFORMAT", tcformat);
1177         save_format_defaults(defaults);
1181 // FUTURE: put more of the format specific variables in here
1182 void Asset::save_format_defaults(BC_Hash *defaults) {
1183         char temp[BCTEXTLEN];
1184         char string[BCTEXTLEN];
1185         if (! format) return;
1187         sprintf(temp, "FORMAT_%s_USE_PIPE", FILE_FORMAT_PREFIX(format));
1188         UPDATE_DEFAULT(temp, use_pipe);
1190         sprintf(temp, "FORMAT_%s_PIPE", FILE_FORMAT_PREFIX(format));
1191         UPDATE_DEFAULT(temp, pipe);
1193         sprintf(temp, "FORMAT_%s_PATH", FILE_FORMAT_PREFIX(format));
1194         UPDATE_DEFAULT(temp, path);
1198 int Asset::update_path(char *new_path)
1200         strcpy(path, new_path);
1201         return 0;
1204 double Asset::total_length_framealigned(double fps) 
1206         if (video_data && audio_data) {
1207                 double aud = floor(( (double)audio_length / sample_rate) * fps) / fps;
1208                 double vid = floor(( (double)video_length / frame_rate) * fps) / fps;
1209                 return MIN(aud,vid);
1210         }
1211         
1212         if (audio_data)
1213                 return (double)audio_length / sample_rate;
1214         
1215         if (video_data)
1216                 return (double)video_length / frame_rate;
1219 void Asset::update_index(Asset *asset)
1221 //printf("Asset::update_index 1 %d\n", index_status);
1222         index_status = asset->index_status;
1223         index_zoom = asset->index_zoom;          // zoom factor of index data
1224         index_start = asset->index_start;        // byte start of index data in the index file
1225         index_bytes = asset->index_bytes;        // Total bytes in source file for comparison before rebuilding the index
1226         index_end = asset->index_end;
1227         old_index_end = asset->old_index_end;    // values for index build
1229         delete [] index_offsets;
1230         delete [] index_sizes;
1231         index_offsets = 0;
1232         index_sizes = 0;
1233         
1234         if(asset->index_offsets)
1235         {
1236                 index_offsets = new int64_t[asset->channels];
1237                 index_sizes = new int64_t[asset->channels];
1239                 int i;
1240                 for(i = 0; i < asset->channels; i++)
1241                 {
1242 // offsets of channels in index file in floats
1243                         index_offsets[i] = asset->index_offsets[i];  
1244                         index_sizes[i] = asset->index_sizes[i];
1245                 }
1246         }
1247         index_buffer = asset->index_buffer;    // pointer
1250 int Asset::set_timecode(char *tc, int format, int end)
1252         int hr, min, sec;
1254         hr = ((int) tc[0] - 48) * 10 + (int) tc[1] - 48;
1255         min = ((int) tc[3] - 48) * 10 + (int) tc[4] - 48;
1256         sec = ((int) tc[6] - 48) * 10 + (int) tc[7] - 48;
1257         
1258         // This needs to be modified to handle drop-frame
1259         
1260         if(end)
1261                 tcend = (int64_t) (((hr * 3600) + (min * 60) + sec) * frame_rate);
1262         else
1263                 tcstart = (int64_t) (((hr * 3600) + (min * 60) + sec) * frame_rate);
1265         tcformat = format;
1266         return 0;
1269 int Asset::dump()
1271         printf("  asset::dump\n");
1272         printf("   %p %s\n", this, path);
1273         printf("   index_status %d\n", index_status);
1274         printf("   format %d\n", format);
1275         printf("   audio_data %d channels %d samplerate %d bits %d byte_order %d signed %d header %d dither %d acodec %c%c%c%c\n",
1276                 audio_data, channels, sample_rate, bits, byte_order, signed_, header, dither, acodec[0], acodec[1], acodec[2], acodec[3]);
1277         printf("   audio_length %lld\n", audio_length);
1278         char string[BCTEXTLEN];
1279         ilacemode_to_xmltext(string, interlace_mode);
1280         printf("   video_data %d layers %d framerate %f width %d height %d vcodec %c%c%c%c aspect_ratio %f interlace_mode %s\n",
1281                video_data, layers, frame_rate, width, height, vcodec[0], vcodec[1], vcodec[2], vcodec[3], aspect_ratio, string);
1282         printf("   video_length %lld \n", video_length);
1283         printf("   reel_name %s reel_number %i tcstart %d tcend %d tcf %d\n",
1284                 reel_name, reel_number, tcstart, tcend, tcformat);
1285         
1286         return 0;