r1005: Increase the number of displayed digits for resample audio dialog box
[cinelerra_cv/mob.git] / cinelerra / asset.C
blob43de0a4a85b3df96dfbc232c4b4caf2aa6e87a5b
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         reset_timecode();
165         return 0;
168 int Asset::reset_index()
170         index_status = INDEX_NOTTESTED;
171         index_start = old_index_end = index_end = 0;
172         index_offsets = 0;
173         index_sizes = 0;
174         index_zoom = 0;
175         index_bytes = 0;
176         index_buffer = 0;
177         return 0;
180 int Asset::reset_timecode()
182         strcpy(reel_name, "cin0000");
183         reel_number = 0;
184         tcstart = 0;
185         tcend = 0;
186         tcformat = 0;
187         
188         return 0;
191 void Asset::copy_from(Asset *asset, int do_index)
193         copy_location(asset);
194         copy_format(asset, do_index);
197 void Asset::copy_location(Asset *asset)
199         strcpy(this->path, asset->path);
200         strcpy(this->folder, asset->folder);
203 void Asset::copy_format(Asset *asset, int do_index)
205         if(do_index) update_index(asset);
207         audio_data = asset->audio_data;
208         format = asset->format;
209         channels = asset->channels;
210         sample_rate = asset->sample_rate;
211         bits = asset->bits;
212         byte_order = asset->byte_order;
213         signed_ = asset->signed_;
214         header = asset->header;
215         dither = asset->dither;
216         mp3_bitrate = asset->mp3_bitrate;
217         mp4a_bitrate = asset->mp4a_bitrate;
218         mp4a_quantqual = asset->mp4a_quantqual;
219         use_header = asset->use_header;
220         aspect_ratio = asset->aspect_ratio;
221         interlace_autofixoption = asset->interlace_autofixoption;
222         interlace_mode = asset->interlace_mode;
223         interlace_fixmethod = asset->interlace_fixmethod;
225         video_data = asset->video_data;
226         layers = asset->layers;
227         frame_rate = asset->frame_rate;
228         width = asset->width;
229         height = asset->height;
230         strcpy(vcodec, asset->vcodec);
231         strcpy(acodec, asset->acodec);
233         this->audio_length = asset->audio_length;
234         this->video_length = asset->video_length;
237         ampeg_bitrate = asset->ampeg_bitrate;
238         ampeg_derivative = asset->ampeg_derivative;
241         vorbis_vbr = asset->vorbis_vbr;
242         vorbis_min_bitrate = asset->vorbis_min_bitrate;
243         vorbis_bitrate = asset->vorbis_bitrate;
244         vorbis_max_bitrate = asset->vorbis_max_bitrate;
246         
247         theora_fix_bitrate = asset->theora_fix_bitrate;
248         theora_bitrate = asset->theora_bitrate;
249         theora_quality = asset->theora_quality;
250         theora_sharpness = asset->theora_sharpness;
251         theora_keyframe_frequency = asset->theora_keyframe_frequency;
252         theora_keyframe_force_frequency = asset->theora_keyframe_frequency;
255         jpeg_quality = asset->jpeg_quality;
257 // mpeg parameters
258         vmpeg_iframe_distance = asset->vmpeg_iframe_distance;
259         vmpeg_pframe_distance = asset->vmpeg_pframe_distance;
260         vmpeg_progressive = asset->vmpeg_progressive;
261         vmpeg_denoise = asset->vmpeg_denoise;
262         vmpeg_bitrate = asset->vmpeg_bitrate;
263         vmpeg_derivative = asset->vmpeg_derivative;
264         vmpeg_quantization = asset->vmpeg_quantization;
265         vmpeg_cmodel = asset->vmpeg_cmodel;
266         vmpeg_fix_bitrate = asset->vmpeg_fix_bitrate;
267         vmpeg_seq_codes = asset->vmpeg_seq_codes;
268         vmpeg_preset = asset->vmpeg_preset;
269         vmpeg_field_order = asset->vmpeg_field_order;
272         divx_bitrate = asset->divx_bitrate;
273         divx_rc_period = asset->divx_rc_period;
274         divx_rc_reaction_ratio = asset->divx_rc_reaction_ratio;
275         divx_rc_reaction_period = asset->divx_rc_reaction_period;
276         divx_max_key_interval = asset->divx_max_key_interval;
277         divx_max_quantizer = asset->divx_max_quantizer;
278         divx_min_quantizer = asset->divx_min_quantizer;
279         divx_quantizer = asset->divx_quantizer;
280         divx_quality = asset->divx_quality;
281         divx_fix_bitrate = asset->divx_fix_bitrate;
282         divx_use_deblocking = asset->divx_use_deblocking;
284         h264_bitrate = asset->h264_bitrate;
285         h264_quantizer = asset->h264_quantizer;
286         h264_fix_bitrate = asset->h264_fix_bitrate;
289         ms_bitrate = asset->ms_bitrate;
290         ms_bitrate_tolerance = asset->ms_bitrate_tolerance;
291         ms_interlaced = asset->ms_interlaced;
292         ms_quantization = asset->ms_quantization;
293         ms_gop_size = asset->ms_gop_size;
294         ms_fix_bitrate = asset->ms_fix_bitrate;
296         
297         ac3_bitrate = asset->ac3_bitrate;
298         
299         png_use_alpha = asset->png_use_alpha;
300         exr_use_alpha = asset->exr_use_alpha;
301         exr_compression = asset->exr_compression;
303         tiff_cmodel = asset->tiff_cmodel;
304         tiff_compression = asset->tiff_compression;
306         strcpy(pipe, asset->pipe);
307         use_pipe = asset->use_pipe;
309         strcpy(reel_name, asset->reel_name);
310         reel_number = asset->reel_number;
311         tcstart = asset->tcstart;
312         tcend = asset->tcend;
313         tcformat = asset->tcformat;
316 int64_t Asset::get_index_offset(int channel)
318         if(channel < channels && index_offsets)
319                 return index_offsets[channel];
320         else
321                 return 0;
324 int64_t Asset::get_index_size(int channel)
326         if(channel < channels && index_sizes)
327                 return index_sizes[channel];
328         else
329                 return 0;
333 char* Asset::get_compression_text(int audio, int video)
335         if(audio)
336         {
337                 switch(format)
338                 {
339                         case FILE_MOV:
340                         case FILE_AVI:
341                                 if(acodec[0])
342                                         return quicktime_acodec_title(acodec);
343                                 else
344                                         return 0;
345                                 break;
346                 }
347         }
348         else
349         if(video)
350         {
351                 switch(format)
352                 {
353                         case FILE_MOV:
354                         case FILE_AVI:
355                                 if(vcodec[0])
356                                         return quicktime_vcodec_title(vcodec);
357                                 else
358                                         return 0;
359                                 break;
360                 }
361         }
362         return 0;
365 Asset& Asset::operator=(Asset &asset)
367         copy_location(&asset);
368         copy_format(&asset, 1);
369         return *this;
373 int Asset::equivalent(Asset &asset, 
374         int test_audio, 
375         int test_video)
377         int result = (!strcmp(asset.path, path) &&
378                 format == asset.format);
380         if(test_audio && result)
381         {
382                 result = (channels == asset.channels && 
383                         sample_rate == asset.sample_rate && 
384                         bits == asset.bits && 
385                         byte_order == asset.byte_order && 
386                         signed_ == asset.signed_ && 
387                         header == asset.header && 
388                         dither == asset.dither &&
389                         !strcmp(acodec, asset.acodec));
390         }
393         if(test_video && result)
394         {
395                 result = (layers == asset.layers && 
396                         frame_rate == asset.frame_rate &&
397                         asset.interlace_autofixoption == interlace_autofixoption &&
398                         asset.interlace_mode    == interlace_mode &&
399                         interlace_fixmethod     == asset.interlace_fixmethod &&
400                         width == asset.width &&
401                         height == asset.height &&
402                         !strcmp(vcodec, asset.vcodec) &&
403                         strcmp(reel_name, asset.reel_name) == 0 &&
404                         reel_number == asset.reel_number &&
405                         tcstart == asset.tcstart &&
406                         tcend == asset.tcend &&
407                         tcformat == asset.tcformat);
408         }
410         return result;
413 int Asset::operator==(Asset &asset)
416         return equivalent(asset, 
417                 1, 
418                 1);
421 int Asset::operator!=(Asset &asset)
423         return !(*this == asset);
426 int Asset::test_path(const char *path)
428         if(!strcasecmp(this->path, path)) 
429                 return 1; 
430         else 
431                 return 0;
434 int Asset::test_plugin_title(const char *path)
438 int Asset::read(FileXML *file, 
439         int expand_relative)
441         int result = 0;
443 // Check for relative path.
444         if(expand_relative)
445         {
446                 char new_path[BCTEXTLEN];
447                 char asset_directory[BCTEXTLEN];
448                 char input_directory[BCTEXTLEN];
449                 FileSystem fs;
451                 strcpy(new_path, path);
452                 fs.set_current_dir("");
454                 fs.extract_dir(asset_directory, path);
456 // No path in asset.
457 // Take path of XML file.
458                 if(!asset_directory[0])
459                 {
460                         fs.extract_dir(input_directory, file->filename);
462 // Input file has a path
463                         if(input_directory[0])
464                         {
465                                 fs.join_names(path, input_directory, new_path);
466                         }
467                 }
468         }
471         while(!result)
472         {
473                 result = file->read_tag();
474                 if(!result)
475                 {
476                         if(file->tag.title_is("/ASSET"))
477                         {
478                                 result = 1;
479                         }
480                         else
481                         if(file->tag.title_is("AUDIO"))
482                         {
483                                 read_audio(file);
484                         }
485                         else
486                         if(file->tag.title_is("AUDIO_OMIT"))
487                         {
488                                 read_audio(file);
489                         }
490                         else
491                         if(file->tag.title_is("FORMAT"))
492                         {
493                                 char *string = file->tag.get_property("TYPE");
494                                 format = File::strtoformat(string);
495                                 use_header = 
496                                         file->tag.get_property("USE_HEADER", use_header);
497                         }
498                         else
499                         if(file->tag.title_is("FOLDER"))
500                         {
501                                 strcpy(folder, file->read_text());
502                         }
503                         else
504                         if(file->tag.title_is("VIDEO"))
505                         {
506                                 read_video(file);
507                         }
508                         else
509                         if(file->tag.title_is("VIDEO_OMIT"))
510                         {
511                                 read_video(file);
512                         }
513                         else
514                         if(file->tag.title_is("INDEX"))
515                         {
516                                 read_index(file);
517                         }
518                 }
519         }
521 //printf("Asset::read 2\n");
522         return 0;
525 int Asset::read_audio(FileXML *file)
527         if(file->tag.title_is("AUDIO")) audio_data = 1;
528         channels = file->tag.get_property("CHANNELS", 2);
529 // This is loaded from the index file after the EDL but this 
530 // should be overridable in the EDL.
531         if(!sample_rate) sample_rate = file->tag.get_property("RATE", 44100);
532         bits = file->tag.get_property("BITS", 16);
533         byte_order = file->tag.get_property("BYTE_ORDER", 1);
534         signed_ = file->tag.get_property("SIGNED", 1);
535         header = file->tag.get_property("HEADER", 0);
536         dither = file->tag.get_property("DITHER", 0);
538         audio_length = file->tag.get_property("AUDIO_LENGTH", 0);
539         acodec[0] = 0;
540         file->tag.get_property("ACODEC", acodec);
541         
544 //      ampeg_bitrate = file->tag.get_property("AMPEG_BITRATE", ampeg_bitrate);
545 //      ampeg_derivative = file->tag.get_property("AMPEG_DERIVATIVE", ampeg_derivative);
546 // 
547 //      vorbis_vbr = file->tag.get_property("VORBIS_VBR", vorbis_vbr);
548 //      vorbis_min_bitrate = file->tag.get_property("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
549 //      vorbis_bitrate = file->tag.get_property("VORBIS_BITRATE", vorbis_bitrate);
550 //      vorbis_max_bitrate = file->tag.get_property("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
551 // 
552 //      mp3_bitrate = file->tag.get_property("MP3_BITRATE", mp3_bitrate);
554         if(!video_data)
555         {
556                 tcstart = 0;
557                 tcend = audio_length;
558                 tcformat = 0;
559         }
561         return 0;
564 int Asset::read_video(FileXML *file)
566         char string[BCTEXTLEN];
568         if(file->tag.title_is("VIDEO")) video_data = 1;
569         height = file->tag.get_property("HEIGHT", height);
570         width = file->tag.get_property("WIDTH", width);
571         layers = file->tag.get_property("LAYERS", layers);
572 // This is loaded from the index file after the EDL but this 
573 // should be overridable in the EDL.
574         if(!frame_rate) frame_rate = file->tag.get_property("FRAMERATE", frame_rate);
575         vcodec[0] = 0;
576         file->tag.get_property("VCODEC", vcodec);
578         video_length = file->tag.get_property("VIDEO_LENGTH", 0);
580         interlace_autofixoption = file->tag.get_property("INTERLACE_AUTOFIX",0);
582         ilacemode_to_xmltext(string, BC_ILACE_MODE_NOTINTERLACED);
583         interlace_mode = ilacemode_from_xmltext(file->tag.get_property("INTERLACE_MODE",string), BC_ILACE_MODE_NOTINTERLACED);
585         ilacefixmethod_to_xmltext(string, BC_ILACE_FIXMETHOD_NONE);
586         interlace_fixmethod = ilacefixmethod_from_xmltext(file->tag.get_property("INTERLACE_FIXMETHOD",string), BC_ILACE_FIXMETHOD_NONE);
588         file->tag.get_property("REEL_NAME", reel_name);
589         reel_number = file->tag.get_property("REEL_NUMBER", reel_number);
590         tcstart = file->tag.get_property("TCSTART", tcstart);
591         tcend = file->tag.get_property("TCEND", tcend);
592         tcformat = file->tag.get_property("TCFORMAT", tcformat);
594         return 0;
597 int Asset::read_index(FileXML *file)
599         delete [] index_offsets;
600         index_offsets = new int64_t[channels];
601         delete [] index_sizes;
602         index_sizes = new int64_t[channels];
603         for(int i = 0; i < channels; i++) 
604         {
605                 index_offsets[i] = 0;
606                 index_sizes[i] = 0;
607         }
609         int current_offset = 0;
610         int current_size = 0;
611         int result = 0;
613         index_zoom = file->tag.get_property("ZOOM", 1);
614         index_bytes = file->tag.get_property("BYTES", (int64_t)0);
616         while(!result)
617         {
618                 result = file->read_tag();
619                 if(!result)
620                 {
621                         if(file->tag.title_is("/INDEX"))
622                         {
623                                 result = 1;
624                         }
625                         else
626                         if(file->tag.title_is("OFFSET"))
627                         {
628                                 if(current_offset < channels)
629                                 {
630                                         index_offsets[current_offset++] = file->tag.get_property("FLOAT", 0);
631 //printf("Asset::read_index %d %d\n", current_offset - 1, index_offsets[current_offset - 1]);
632                                 }
633                         }
634                         else
635                         if(file->tag.title_is("SIZE"))
636                         {
637                                 if(current_size < channels)
638                                 {
639                                         index_sizes[current_size++] = file->tag.get_property("FLOAT", 0);
640                                 }
641                         }
642                 }
643         }
644         return 0;
647 int Asset::write_index(char *path, int data_bytes)
649         FILE *file;
650         if(!(file = fopen(path, "wb")))
651         {
652 // failed to create it
653                 printf(_("Asset::write_index Couldn't write index file %s to disk.\n"), path);
654         }
655         else
656         {
657                 FileXML xml;
658 // Pad index start position
659                 fwrite((char*)&(index_start), sizeof(int64_t), 1, file);
661                 index_status = INDEX_READY;
662 // Write encoding information
663                 write(&xml, 
664                         1, 
665                         "");
666                 xml.write_to_file(file);
667                 index_start = ftell(file);
668                 fseek(file, 0, SEEK_SET);
669 // Write index start
670                 fwrite((char*)&(index_start), sizeof(int64_t), 1, file);
671                 fseek(file, index_start, SEEK_SET);
673 // Write index data
674                 fwrite(index_buffer, 
675                         data_bytes, 
676                         1, 
677                         file);
678                 fclose(file);
679         }
681 // Force reread of header
682         index_status = INDEX_NOTTESTED;
683 //      index_status = INDEX_READY;
684         index_end = audio_length;
685         old_index_end = 0;
686         index_start = 0;
689 // Output path is the path of the output file if name truncation is desired.
690 // It is a "" if complete names should be used.
692 int Asset::write(FileXML *file, 
693         int include_index, 
694         char *output_path)
696         char new_path[BCTEXTLEN];
697         char asset_directory[BCTEXTLEN];
698         char output_directory[BCTEXTLEN];
699         FileSystem fs;
701 // Make path relative
702         fs.extract_dir(asset_directory, path);
703         if(output_path && output_path[0]) 
704                 fs.extract_dir(output_directory, output_path);
705         else
706                 output_directory[0] = 0;
708 // Asset and EDL are in same directory.  Extract just the name.
709         if(!strcmp(asset_directory, output_directory))
710         {
711                 fs.extract_name(new_path, path);
712         }
713         else
714         {
715                 strcpy(new_path, path);
716         }
718         file->tag.set_title("ASSET");
719         file->tag.set_property("SRC", new_path);
720         file->append_tag();
721         file->append_newline();
723         file->tag.set_title("FOLDER");
724         file->append_tag();
725         file->append_text(folder);
726         file->tag.set_title("/FOLDER");
727         file->append_tag();
728         file->append_newline();
730 // Write the format information
731         file->tag.set_title("FORMAT");
733         file->tag.set_property("TYPE", 
734                 File::formattostr(format));
735         file->tag.set_property("USE_HEADER", use_header);
737         file->append_tag();
738         file->tag.set_title("/FORMAT");
739         file->append_tag();
740         file->append_newline();
742 // Requiring data to exist caused batch render to lose settings.
743 // But the only way to know if an asset doesn't have audio or video data 
744 // is to not write the block.
745 // So change the block name if the asset doesn't have the data.
746         /* if(audio_data) */ write_audio(file);
747         /* if(video_data) */ write_video(file);
748         if(index_status == 0 && include_index) write_index(file);  // index goes after source
750         file->tag.set_title("/ASSET");
751         file->append_tag();
752         file->append_newline();
753         return 0;
756 int Asset::write_audio(FileXML *file)
758 // Let the reader know if the asset has the data by naming the block.
759         if(audio_data)
760                 file->tag.set_title("AUDIO");
761         else
762                 file->tag.set_title("AUDIO_OMIT");
763 // Necessary for PCM audio
764         file->tag.set_property("CHANNELS", channels);
765         file->tag.set_property("RATE", sample_rate);
766         file->tag.set_property("BITS", bits);
767         file->tag.set_property("BYTE_ORDER", byte_order);
768         file->tag.set_property("SIGNED", signed_);
769         file->tag.set_property("HEADER", header);
770         file->tag.set_property("DITHER", dither);
771         if(acodec[0])
772                 file->tag.set_property("ACODEC", acodec);
773         
774         file->tag.set_property("AUDIO_LENGTH", audio_length);
778 // Rely on defaults operations for these.
780 //      file->tag.set_property("AMPEG_BITRATE", ampeg_bitrate);
781 //      file->tag.set_property("AMPEG_DERIVATIVE", ampeg_derivative);
782 // 
783 //      file->tag.set_property("VORBIS_VBR", vorbis_vbr);
784 //      file->tag.set_property("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
785 //      file->tag.set_property("VORBIS_BITRATE", vorbis_bitrate);
786 //      file->tag.set_property("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
787 // 
788 //      file->tag.set_property("MP3_BITRATE", mp3_bitrate);
789 // 
793         file->append_tag();
794         if(audio_data)
795           file->tag.set_title("/AUDIO");
796         else
797           file->tag.set_title("/AUDIO_OMIT");
798         file->append_tag();
799         file->append_newline();
800         return 0;
803 int Asset::write_video(FileXML *file)
805         char string[BCTEXTLEN];
807         if(video_data)
808                 file->tag.set_title("VIDEO");
809         else
810                 file->tag.set_title("VIDEO_OMIT");
811         file->tag.set_property("HEIGHT", height);
812         file->tag.set_property("WIDTH", width);
813         file->tag.set_property("LAYERS", layers);
814         file->tag.set_property("FRAMERATE", frame_rate);
815         if(vcodec[0])
816                 file->tag.set_property("VCODEC", vcodec);
818         file->tag.set_property("VIDEO_LENGTH", video_length);
820         file->tag.set_property("INTERLACE_AUTOFIX", interlace_autofixoption);
822         ilacemode_to_xmltext(string, interlace_mode);
823         file->tag.set_property("INTERLACE_MODE", string);
825         ilacefixmethod_to_xmltext(string, interlace_fixmethod);
826         file->tag.set_property("INTERLACE_FIXMETHOD", string);
829         file->tag.set_property("REEL_NAME", reel_name);
830         file->tag.set_property("REEL_NUMBER", reel_number);
831         file->tag.set_property("TCSTART", tcstart);
832         file->tag.set_property("TCEND", tcend);
833         file->tag.set_property("TCFORMAT", tcformat);
835         file->append_tag();
836         if(video_data)
837                 file->tag.set_title("/VIDEO");
838         else
839                 file->tag.set_title("/VIDEO_OMIT");
841         file->append_tag();
842         file->append_newline();
843         return 0;
846 int Asset::write_index(FileXML *file)
848         file->tag.set_title("INDEX");
849         file->tag.set_property("ZOOM", index_zoom);
850         file->tag.set_property("BYTES", index_bytes);
851         file->append_tag();
852         file->append_newline();
854         if(index_offsets)
855         {
856                 for(int i = 0; i < channels; i++)
857                 {
858                         file->tag.set_title("OFFSET");
859                         file->tag.set_property("FLOAT", index_offsets[i]);
860                         file->append_tag();
861                         file->tag.set_title("/OFFSET");
862                         file->append_tag();
863                         file->tag.set_title("SIZE");
864                         file->tag.set_property("FLOAT", index_sizes[i]);
865                         file->append_tag();
866                         file->tag.set_title("/SIZE");
867                         file->append_tag();
868                 }
869         }
871         file->append_newline();
872         file->tag.set_title("/INDEX");
873         file->append_tag();
874         file->append_newline();
875         return 0;
881 char* Asset::construct_param(char *param, char *prefix, char *return_value)
883         if(prefix)
884                 sprintf(return_value, "%s%s", prefix, param);
885         else
886                 strcpy(return_value, param);
887         return return_value;
890 #define UPDATE_DEFAULT(x, y) defaults->update(construct_param(x, prefix, string), y);
891 #define GET_DEFAULT(x, y) defaults->get(construct_param(x, prefix, string), y);
893 void Asset::load_defaults(BC_Hash *defaults, 
894         char *prefix, 
895         int do_format,
896         int do_compression,
897         int do_path,
898         int do_data_types,
899         int do_bits)
901         char string[BCTEXTLEN];
903 // Can't save codec here because it's specific to render, record, and effect.
904 // The codec has to be UNKNOWN for file probing to work.
906         if(do_path)
907         {
908                 GET_DEFAULT("PATH", path);
909         }
911         if(do_compression)
912         {
913                 GET_DEFAULT("AUDIO_CODEC", acodec);
914                 GET_DEFAULT("VIDEO_CODEC", vcodec);
915         }
917         if(do_format)
918         {
919                 format = GET_DEFAULT("FORMAT", format);
920         }
922         if(do_data_types)
923         {
924                 audio_data = GET_DEFAULT("AUDIO", 1);
925                 video_data = GET_DEFAULT("VIDEO", 1);
926         }
928         if(do_bits)
929         {
930                 bits = GET_DEFAULT("BITS", 16);
931                 dither = GET_DEFAULT("DITHER", 0);
932                 signed_ = GET_DEFAULT("SIGNED", 1);
933                 byte_order = GET_DEFAULT("BYTE_ORDER", 1);
934         }
936         ampeg_bitrate = GET_DEFAULT("AMPEG_BITRATE", ampeg_bitrate);
937         ampeg_derivative = GET_DEFAULT("AMPEG_DERIVATIVE", ampeg_derivative);
939         vorbis_vbr = GET_DEFAULT("VORBIS_VBR", vorbis_vbr);
940         vorbis_min_bitrate = GET_DEFAULT("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
941         vorbis_bitrate = GET_DEFAULT("VORBIS_BITRATE", vorbis_bitrate);
942         vorbis_max_bitrate = GET_DEFAULT("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
944         theora_fix_bitrate = GET_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
945         theora_bitrate = GET_DEFAULT("THEORA_BITRATE", theora_bitrate);
946         theora_quality = GET_DEFAULT("THEORA_QUALITY", theora_quality);
947         theora_sharpness = GET_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
948         theora_keyframe_frequency = GET_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
949         theora_keyframe_force_frequency = GET_DEFAULT("THEORA_FORCE_KEYFRAME_FEQUENCY", theora_keyframe_force_frequency);
953         mp3_bitrate = GET_DEFAULT("MP3_BITRATE", mp3_bitrate);
954         mp4a_bitrate = GET_DEFAULT("MP4A_BITRATE", mp4a_bitrate);
955         mp4a_quantqual = GET_DEFAULT("MP4A_QUANTQUAL", mp4a_quantqual);
957         jpeg_quality = GET_DEFAULT("JPEG_QUALITY", jpeg_quality);
958         aspect_ratio = GET_DEFAULT("ASPECT_RATIO", aspect_ratio);
960         interlace_autofixoption = BC_ILACE_AUTOFIXOPTION_AUTO;
961         interlace_mode          = BC_ILACE_MODE_UNDETECTED;
962         interlace_fixmethod     = BC_ILACE_FIXMETHOD_UPONE;
964 // MPEG format information
965         vmpeg_iframe_distance = GET_DEFAULT("VMPEG_IFRAME_DISTANCE", vmpeg_iframe_distance);
966         vmpeg_pframe_distance = GET_DEFAULT("VMPEG_PFRAME_DISTANCE", vmpeg_pframe_distance);
967         vmpeg_progressive = GET_DEFAULT("VMPEG_PROGRESSIVE", vmpeg_progressive);
968         vmpeg_denoise = GET_DEFAULT("VMPEG_DENOISE", vmpeg_denoise);
969         vmpeg_bitrate = GET_DEFAULT("VMPEG_BITRATE", vmpeg_bitrate);
970         vmpeg_derivative = GET_DEFAULT("VMPEG_DERIVATIVE", vmpeg_derivative);
971         vmpeg_quantization = GET_DEFAULT("VMPEG_QUANTIZATION", vmpeg_quantization);
972         vmpeg_cmodel = GET_DEFAULT("VMPEG_CMODEL", vmpeg_cmodel);
973         vmpeg_fix_bitrate = GET_DEFAULT("VMPEG_FIX_BITRATE", vmpeg_fix_bitrate);
974         vmpeg_seq_codes = GET_DEFAULT("VMPEG_SEQ_CODES", vmpeg_seq_codes);
975         vmpeg_preset = GET_DEFAULT("VMPEG_PRESET", vmpeg_preset);
976         vmpeg_field_order = GET_DEFAULT("VMPEG_FIELD_ORDER", vmpeg_field_order);
978         h264_bitrate = GET_DEFAULT("H264_BITRATE", h264_bitrate);
979         h264_quantizer = GET_DEFAULT("H264_QUANTIZER", h264_quantizer);
980         h264_fix_bitrate = GET_DEFAULT("H264_FIX_BITRATE", h264_fix_bitrate);
983         divx_bitrate = GET_DEFAULT("DIVX_BITRATE", divx_bitrate);
984         divx_rc_period = GET_DEFAULT("DIVX_RC_PERIOD", divx_rc_period);
985         divx_rc_reaction_ratio = GET_DEFAULT("DIVX_RC_REACTION_RATIO", divx_rc_reaction_ratio);
986         divx_rc_reaction_period = GET_DEFAULT("DIVX_RC_REACTION_PERIOD", divx_rc_reaction_period);
987         divx_max_key_interval = GET_DEFAULT("DIVX_MAX_KEY_INTERVAL", divx_max_key_interval);
988         divx_max_quantizer = GET_DEFAULT("DIVX_MAX_QUANTIZER", divx_max_quantizer);
989         divx_min_quantizer = GET_DEFAULT("DIVX_MIN_QUANTIZER", divx_min_quantizer);
990         divx_quantizer = GET_DEFAULT("DIVX_QUANTIZER", divx_quantizer);
991         divx_quality = GET_DEFAULT("DIVX_QUALITY", divx_quality);
992         divx_fix_bitrate = GET_DEFAULT("DIVX_FIX_BITRATE", divx_fix_bitrate);
993         divx_use_deblocking = GET_DEFAULT("DIVX_USE_DEBLOCKING", divx_use_deblocking);
995         theora_fix_bitrate = GET_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
996         theora_bitrate = GET_DEFAULT("THEORA_BITRATE", theora_bitrate);
997         theora_quality = GET_DEFAULT("THEORA_QUALITY", theora_quality);
998         theora_sharpness = GET_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
999         theora_keyframe_frequency = GET_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
1000         theora_keyframe_force_frequency = GET_DEFAULT("THEORA_FORCE_KEYFRAME_FEQUENCY", theora_keyframe_force_frequency);
1003         ms_bitrate = GET_DEFAULT("MS_BITRATE", ms_bitrate);
1004         ms_bitrate_tolerance = GET_DEFAULT("MS_BITRATE_TOLERANCE", ms_bitrate_tolerance);
1005         ms_interlaced = GET_DEFAULT("MS_INTERLACED", ms_interlaced);
1006         ms_quantization = GET_DEFAULT("MS_QUANTIZATION", ms_quantization);
1007         ms_gop_size = GET_DEFAULT("MS_GOP_SIZE", ms_gop_size);
1008         ms_fix_bitrate = GET_DEFAULT("MS_FIX_BITRATE", ms_fix_bitrate);
1010         ac3_bitrate = GET_DEFAULT("AC3_BITRATE", ac3_bitrate);
1012         png_use_alpha = GET_DEFAULT("PNG_USE_ALPHA", png_use_alpha);
1013         exr_use_alpha = GET_DEFAULT("EXR_USE_ALPHA", exr_use_alpha);
1014         exr_compression = GET_DEFAULT("EXR_COMPRESSION", exr_compression);
1015         tiff_cmodel = GET_DEFAULT("TIFF_CMODEL", tiff_cmodel);
1016         tiff_compression = GET_DEFAULT("TIFF_COMPRESSION", tiff_compression);
1018         // this extra 'FORMAT_' prefix is just here for legacy reasons
1019         use_pipe = GET_DEFAULT("FORMAT_YUV_USE_PIPE", use_pipe);
1020         GET_DEFAULT("FORMAT_YUV_PIPE", pipe);
1022         GET_DEFAULT("REEL_NAME", reel_name);
1023         reel_number = GET_DEFAULT("REEL_NUMBER", reel_number);
1024         tcstart = GET_DEFAULT("TCSTART", tcstart);
1025         tcend = GET_DEFAULT("TCEND", tcend);
1026         tcformat = GET_DEFAULT("TCFORMAT", tcformat);
1029 void Asset::save_defaults(BC_Hash *defaults, 
1030         char *prefix,
1031         int do_format,
1032         int do_compression,
1033         int do_path,
1034         int do_data_types,
1035         int do_bits)
1037         char string[BCTEXTLEN];
1039         UPDATE_DEFAULT("PATH", path);
1041         if(do_format)
1042         {
1043                 UPDATE_DEFAULT("FORMAT", format);
1044         }
1046         if(do_data_types)
1047         {
1048                 UPDATE_DEFAULT("AUDIO", audio_data);
1049                 UPDATE_DEFAULT("VIDEO", video_data);
1050         }
1052         if(do_compression)
1053         {
1054                 UPDATE_DEFAULT("AUDIO_CODEC", acodec);
1055                 UPDATE_DEFAULT("VIDEO_CODEC", vcodec);
1057                 UPDATE_DEFAULT("AMPEG_BITRATE", ampeg_bitrate);
1058                 UPDATE_DEFAULT("AMPEG_DERIVATIVE", ampeg_derivative);
1060                 UPDATE_DEFAULT("VORBIS_VBR", vorbis_vbr);
1061                 UPDATE_DEFAULT("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
1062                 UPDATE_DEFAULT("VORBIS_BITRATE", vorbis_bitrate);
1063                 UPDATE_DEFAULT("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
1066                 UPDATE_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
1067                 UPDATE_DEFAULT("THEORA_BITRATE", theora_bitrate);
1068                 UPDATE_DEFAULT("THEORA_QUALITY", theora_quality);
1069                 UPDATE_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
1070                 UPDATE_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
1071                 UPDATE_DEFAULT("THEORA_FORCE_KEYFRAME_FEQUENCY", theora_keyframe_force_frequency);
1075                 UPDATE_DEFAULT("MP3_BITRATE", mp3_bitrate);
1076                 UPDATE_DEFAULT("MP4A_BITRATE", mp4a_bitrate);
1077                 UPDATE_DEFAULT("MP4A_QUANTQUAL", mp4a_quantqual);
1083                 UPDATE_DEFAULT("JPEG_QUALITY", jpeg_quality);
1084                 UPDATE_DEFAULT("ASPECT_RATIO", aspect_ratio);
1086 // MPEG format information
1087                 UPDATE_DEFAULT("VMPEG_IFRAME_DISTANCE", vmpeg_iframe_distance);
1088                 UPDATE_DEFAULT("VMPEG_PFRAME_DISTANCE", vmpeg_pframe_distance);
1089                 UPDATE_DEFAULT("VMPEG_PROGRESSIVE", vmpeg_progressive);
1090                 UPDATE_DEFAULT("VMPEG_DENOISE", vmpeg_denoise);
1091                 UPDATE_DEFAULT("VMPEG_BITRATE", vmpeg_bitrate);
1092                 UPDATE_DEFAULT("VMPEG_DERIVATIVE", vmpeg_derivative);
1093                 UPDATE_DEFAULT("VMPEG_QUANTIZATION", vmpeg_quantization);
1094                 UPDATE_DEFAULT("VMPEG_CMODEL", vmpeg_cmodel);
1095                 UPDATE_DEFAULT("VMPEG_FIX_BITRATE", vmpeg_fix_bitrate);
1096                 UPDATE_DEFAULT("VMPEG_SEQ_CODES", vmpeg_seq_codes);
1097                 UPDATE_DEFAULT("VMPEG_PRESET", vmpeg_preset);
1098                 UPDATE_DEFAULT("VMPEG_FIELD_ORDER", vmpeg_field_order);
1100                 UPDATE_DEFAULT("H264_BITRATE", h264_bitrate);
1101                 UPDATE_DEFAULT("H264_QUANTIZER", h264_quantizer);
1102                 UPDATE_DEFAULT("H264_FIX_BITRATE", h264_fix_bitrate);
1104                 UPDATE_DEFAULT("DIVX_BITRATE", divx_bitrate);
1105                 UPDATE_DEFAULT("DIVX_RC_PERIOD", divx_rc_period);
1106                 UPDATE_DEFAULT("DIVX_RC_REACTION_RATIO", divx_rc_reaction_ratio);
1107                 UPDATE_DEFAULT("DIVX_RC_REACTION_PERIOD", divx_rc_reaction_period);
1108                 UPDATE_DEFAULT("DIVX_MAX_KEY_INTERVAL", divx_max_key_interval);
1109                 UPDATE_DEFAULT("DIVX_MAX_QUANTIZER", divx_max_quantizer);
1110                 UPDATE_DEFAULT("DIVX_MIN_QUANTIZER", divx_min_quantizer);
1111                 UPDATE_DEFAULT("DIVX_QUANTIZER", divx_quantizer);
1112                 UPDATE_DEFAULT("DIVX_QUALITY", divx_quality);
1113                 UPDATE_DEFAULT("DIVX_FIX_BITRATE", divx_fix_bitrate);
1114                 UPDATE_DEFAULT("DIVX_USE_DEBLOCKING", divx_use_deblocking);
1117                 UPDATE_DEFAULT("MS_BITRATE", ms_bitrate);
1118                 UPDATE_DEFAULT("MS_BITRATE_TOLERANCE", ms_bitrate_tolerance);
1119                 UPDATE_DEFAULT("MS_INTERLACED", ms_interlaced);
1120                 UPDATE_DEFAULT("MS_QUANTIZATION", ms_quantization);
1121                 UPDATE_DEFAULT("MS_GOP_SIZE", ms_gop_size);
1122                 UPDATE_DEFAULT("MS_FIX_BITRATE", ms_fix_bitrate);
1124                 UPDATE_DEFAULT("AC3_BITRATE", ac3_bitrate);
1127                 UPDATE_DEFAULT("PNG_USE_ALPHA", png_use_alpha);
1128                 UPDATE_DEFAULT("EXR_USE_ALPHA", exr_use_alpha);
1129                 UPDATE_DEFAULT("EXR_COMPRESSION", exr_compression);
1130                 UPDATE_DEFAULT("TIFF_CMODEL", tiff_cmodel);
1131                 UPDATE_DEFAULT("TIFF_COMPRESSION", tiff_compression);
1133                 UPDATE_DEFAULT("FORMAT_YUV_USE_PIPE", use_pipe);
1134                 UPDATE_DEFAULT("FORMAT_YUV_PIPE", pipe);
1135         }
1137         if(do_bits)
1138         {
1139                 UPDATE_DEFAULT("BITS", bits);
1140                 UPDATE_DEFAULT("DITHER", dither);
1141                 UPDATE_DEFAULT("SIGNED", signed_);
1142                 UPDATE_DEFAULT("BYTE_ORDER", byte_order);
1143         }
1145         UPDATE_DEFAULT("REEL_NAME", reel_name);
1146         UPDATE_DEFAULT("REEL_NUMBER", reel_number);
1147         UPDATE_DEFAULT("TCSTART", tcstart);
1148         UPDATE_DEFAULT("TCEND", tcend);
1149         UPDATE_DEFAULT("TCFORMAT", tcformat);
1160 int Asset::update_path(char *new_path)
1162         strcpy(path, new_path);
1163         return 0;
1166 double Asset::total_length_framealigned(double fps) 
1168         if (video_data && audio_data) {
1169                 double aud = floor(( (double)audio_length / sample_rate) * fps) / fps;
1170                 double vid = floor(( (double)video_length / frame_rate) * fps) / fps;
1171                 return MIN(aud,vid);
1172         }
1173         
1174         if (audio_data)
1175                 return (double)audio_length / sample_rate;
1176         
1177         if (video_data)
1178                 return (double)video_length / frame_rate;
1181 void Asset::update_index(Asset *asset)
1183 //printf("Asset::update_index 1 %d\n", index_status);
1184         index_status = asset->index_status;
1185         index_zoom = asset->index_zoom;          // zoom factor of index data
1186         index_start = asset->index_start;        // byte start of index data in the index file
1187         index_bytes = asset->index_bytes;        // Total bytes in source file for comparison before rebuilding the index
1188         index_end = asset->index_end;
1189         old_index_end = asset->old_index_end;    // values for index build
1191         delete [] index_offsets;
1192         delete [] index_sizes;
1193         index_offsets = 0;
1194         index_sizes = 0;
1195         
1196         if(asset->index_offsets)
1197         {
1198                 index_offsets = new int64_t[asset->channels];
1199                 index_sizes = new int64_t[asset->channels];
1201                 int i;
1202                 for(i = 0; i < asset->channels; i++)
1203                 {
1204 // offsets of channels in index file in floats
1205                         index_offsets[i] = asset->index_offsets[i];  
1206                         index_sizes[i] = asset->index_sizes[i];
1207                 }
1208         }
1209         index_buffer = asset->index_buffer;    // pointer
1212 int Asset::set_timecode(char *tc, int format, int end)
1214         int hr, min, sec;
1216         hr = ((int) tc[0] - 48) * 10 + (int) tc[1] - 48;
1217         min = ((int) tc[3] - 48) * 10 + (int) tc[4] - 48;
1218         sec = ((int) tc[6] - 48) * 10 + (int) tc[7] - 48;
1219         
1220         // This needs to be modified to handle drop-frame
1221         
1222         if(end)
1223                 tcend = (int64_t) (((hr * 3600) + (min * 60) + sec) * frame_rate);
1224         else
1225                 tcstart = (int64_t) (((hr * 3600) + (min * 60) + sec) * frame_rate);
1227         tcformat = format;
1228         return 0;
1231 int Asset::dump()
1233         printf("  asset::dump\n");
1234         printf("   %p %s\n", this, path);
1235         printf("   index_status %d\n", index_status);
1236         printf("   format %d\n", format);
1237         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",
1238                 audio_data, channels, sample_rate, bits, byte_order, signed_, header, dither, acodec[0], acodec[1], acodec[2], acodec[3]);
1239         printf("   audio_length %lld\n", audio_length);
1240         char string[BCTEXTLEN];
1241         ilacemode_to_xmltext(string, interlace_mode);
1242         printf("   video_data %d layers %d framerate %f width %d height %d vcodec %c%c%c%c aspect_ratio %f interlace_mode %s\n",
1243                video_data, layers, frame_rate, width, height, vcodec[0], vcodec[1], vcodec[2], vcodec[3], aspect_ratio, string);
1244         printf("   video_length %lld \n", video_length);
1245         printf("   reel_name %s reel_number %i tcstart %d tcend %d tcf %d\n",
1246                 reel_name, reel_number, tcstart, tcend, tcformat);
1247         
1248         return 0;