Merge CV, ver=5.1; ops/methods from HV, and interface from CV where possible
[goodguy/history.git] / cinelerra-5.1 / cinelerra / asset.C
1 /*
2  * CINELERRA
3  * Copyright (C) 2010 Adam Williams <broadcast at earthling dot net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20
21
22 #include "asset.h"
23 #include "assets.h"
24 #include "bchash.h"
25 #include "bcsignals.h"
26 #include "clip.h"
27 #include "edl.h"
28 #include "file.h"
29 #include "filesystem.h"
30 #include "filexml.h"
31 #include "indexstate.h"
32 #include "interlacemodes.h"
33
34 #include <stdio.h>
35 #include <string.h>
36
37
38 Asset::Asset()
39  : Indexable(1), ListItem<Asset>()
40 {
41         init_values();
42 }
43
44 Asset::Asset(Asset &asset)
45  : Indexable(1), ListItem<Asset>()
46 {
47         init_values();
48         this->copy_from(&asset, 1);
49 }
50
51 Asset::Asset(const char *path)
52  : Indexable(1), ListItem<Asset>()
53 {
54         init_values();
55         strcpy(this->path, path);
56 }
57
58 Asset::Asset(const int plugin_type, const char *plugin_title)
59  : Indexable(1), ListItem<Asset>()
60 {
61         init_values();
62 }
63
64 Asset::~Asset()
65 {
66 }
67
68 int Asset::init_values()
69 {
70         path[0] = 0;
71 // Has to be unknown for file probing to succeed
72         format = FILE_UNKNOWN;
73         fformat[0] = 0;
74         bits = 0;
75         byte_order = 0;
76         signed_ = 0;
77         header = 0;
78         dither = 0;
79         reset_audio();
80         reset_video();
81
82         strcpy(vcodec, "");
83         strcpy(acodec, "");
84
85         ff_audio_options[0] = 0;
86         ff_video_options[0] = 0;
87         ff_audio_bitrate = 0;
88         ff_video_bitrate = 0;
89         ff_video_quality = 0;
90
91         jpeg_quality = 80;
92         aspect_ratio = -1;
93         interlace_autofixoption = BC_ILACE_AUTOFIXOPTION_AUTO;
94         interlace_mode = BC_ILACE_MODE_UNDETECTED;
95         interlace_fixmethod = BC_ILACE_FIXMETHOD_NONE;
96
97         ampeg_bitrate = 256;
98         ampeg_derivative = 3;
99
100         vorbis_vbr = 0;
101         vorbis_min_bitrate = -1;
102         vorbis_bitrate = 128000;
103         vorbis_max_bitrate = -1;
104
105         theora_fix_bitrate = 1;
106         theora_bitrate = 860000;
107         theora_quality = 16;
108         theora_sharpness = 2;
109         theora_keyframe_frequency = 64;
110         theora_keyframe_force_frequency = 64;
111
112 // mpeg parameters
113         vmpeg_iframe_distance = 45;
114         vmpeg_pframe_distance = 0;
115         vmpeg_progressive = 0;
116         vmpeg_denoise = 1;
117         vmpeg_bitrate = 1000000;
118         vmpeg_derivative = 1;
119         vmpeg_quantization = 15;
120         vmpeg_cmodel = BC_YUV420P;
121         vmpeg_fix_bitrate = 0;
122         vmpeg_seq_codes = 0;
123         vmpeg_preset = 0;
124         vmpeg_field_order = 0;
125
126         ac3_bitrate = 128;
127
128         png_use_alpha = 0;
129         exr_use_alpha = 0;
130         exr_compression = 0;
131
132         tiff_cmodel = 0;
133         tiff_compression = 0;
134
135         use_header = 1;
136         id = EDL::next_id();
137         reset_timecode();
138
139         return 0;
140 }
141
142 void Asset::reset_audio()
143 {
144         audio_data = 0;
145         channels = 0;
146         sample_rate = 0;
147         audio_length = 0;
148 }
149
150 void Asset::reset_video()
151 {
152         video_data = 0;
153         layers = 0;
154         actual_width = width = 0;
155         actual_height = height = 0;
156         video_length = 0;
157         vmpeg_cmodel = BC_YUV420P;
158         frame_rate = 0;
159         program = -1;
160 }
161
162
163 void Asset::boundaries()
164 {
165 //printf("Asset::boundaries %d %d %f\n", __LINE__, sample_rate, frame_rate);
166 // sample_rate & frame_rate are user defined
167 //      CLAMP(sample_rate, 1, 1000000);
168 //      CLAMP(frame_rate, 0.001, 1000000);
169         CLAMP(channels, 0, 16);
170         CLAMP(width, 0, 10000);
171         CLAMP(height, 0, 10000);
172 //printf("Asset::boundaries %d %d %f\n", __LINE__, sample_rate, frame_rate);
173 }
174
175 int Asset::reset_timecode()
176 {
177         strcpy(reel_name, "cin0000");
178         reel_number = 0;
179         tcstart = 0;
180         tcend = 0;
181         tcformat = 0;
182
183         return 0;
184 }
185
186 void Asset::copy_from(Asset *asset, int do_index)
187 {
188         copy_location(asset);
189         copy_format(asset, do_index);
190 }
191
192 void Asset::copy_location(Asset *asset)
193 {
194         strcpy(this->path, asset->path);
195         strcpy(this->folder, asset->folder);
196 }
197
198 void Asset::copy_format(Asset *asset, int do_index)
199 {
200         if(do_index) update_index(asset);
201
202         audio_data = asset->audio_data;
203         format = asset->format;
204         strcpy(fformat, asset->fformat);
205         channels = asset->channels;
206         sample_rate = asset->sample_rate;
207         bits = asset->bits;
208         byte_order = asset->byte_order;
209         signed_ = asset->signed_;
210         header = asset->header;
211         dither = asset->dither;
212         mp3_bitrate = asset->mp3_bitrate;
213         use_header = asset->use_header;
214         aspect_ratio = asset->aspect_ratio;
215         interlace_autofixoption = asset->interlace_autofixoption;
216         interlace_mode = asset->interlace_mode;
217         interlace_fixmethod = asset->interlace_fixmethod;
218
219         video_data = asset->video_data;
220         layers = asset->layers;
221         program = asset->program;
222         frame_rate = asset->frame_rate;
223         width = asset->width;
224         height = asset->height;
225         actual_width = asset->actual_width;
226         actual_height = asset->actual_height;
227         strcpy(vcodec, asset->vcodec);
228         strcpy(acodec, asset->acodec);
229
230         strcpy(ff_audio_options, asset->ff_audio_options);
231         strcpy(ff_video_options, asset->ff_video_options);
232         ff_audio_bitrate = asset->ff_audio_bitrate;
233         ff_video_bitrate = asset->ff_video_bitrate;
234         ff_video_quality = asset->ff_video_quality;
235
236         this->audio_length = asset->audio_length;
237         this->video_length = asset->video_length;
238
239
240         ampeg_bitrate = asset->ampeg_bitrate;
241         ampeg_derivative = asset->ampeg_derivative;
242
243
244         vorbis_vbr = asset->vorbis_vbr;
245         vorbis_min_bitrate = asset->vorbis_min_bitrate;
246         vorbis_bitrate = asset->vorbis_bitrate;
247         vorbis_max_bitrate = asset->vorbis_max_bitrate;
248
249
250         theora_fix_bitrate = asset->theora_fix_bitrate;
251         theora_bitrate = asset->theora_bitrate;
252         theora_quality = asset->theora_quality;
253         theora_sharpness = asset->theora_sharpness;
254         theora_keyframe_frequency = asset->theora_keyframe_frequency;
255         theora_keyframe_force_frequency = asset->theora_keyframe_frequency;
256
257
258         jpeg_quality = asset->jpeg_quality;
259
260 // mpeg parameters
261         vmpeg_iframe_distance = asset->vmpeg_iframe_distance;
262         vmpeg_pframe_distance = asset->vmpeg_pframe_distance;
263         vmpeg_progressive = asset->vmpeg_progressive;
264         vmpeg_denoise = asset->vmpeg_denoise;
265         vmpeg_bitrate = asset->vmpeg_bitrate;
266         vmpeg_derivative = asset->vmpeg_derivative;
267         vmpeg_quantization = asset->vmpeg_quantization;
268         vmpeg_cmodel = asset->vmpeg_cmodel;
269         vmpeg_fix_bitrate = asset->vmpeg_fix_bitrate;
270         vmpeg_seq_codes = asset->vmpeg_seq_codes;
271         vmpeg_preset = asset->vmpeg_preset;
272         vmpeg_field_order = asset->vmpeg_field_order;
273
274         ac3_bitrate = asset->ac3_bitrate;
275
276         png_use_alpha = asset->png_use_alpha;
277         exr_use_alpha = asset->exr_use_alpha;
278         exr_compression = asset->exr_compression;
279
280         tiff_cmodel = asset->tiff_cmodel;
281         tiff_compression = asset->tiff_compression;
282
283         strcpy(reel_name, asset->reel_name);
284         reel_number = asset->reel_number;
285         tcstart = asset->tcstart;
286         tcend = asset->tcend;
287         tcformat = asset->tcformat;
288 }
289
290 int64_t Asset::get_index_offset(int channel)
291 {
292         return index_state->get_index_offset(channel);
293 }
294
295 int64_t Asset::get_index_size(int channel)
296 {
297         return index_state->get_index_size(channel);
298 }
299
300
301 char* Asset::get_compression_text(int audio, int video)
302 {
303         if(audio) {
304                 switch(format) {
305                 case FILE_FFMPEG:
306                         if( acodec[0] ) return acodec;
307                         break;
308                 }
309         }
310         if(video) {
311                 switch(format) {
312                 case FILE_FFMPEG:
313                         if( vcodec[0] ) return vcodec;
314                         break;
315                 }
316         }
317         return 0;
318 }
319
320 Asset& Asset::operator=(Asset &asset)
321 {
322 printf("Asset::operator=\n");
323         copy_location(&asset);
324         copy_format(&asset, 1);
325         return *this;
326 }
327
328
329 int Asset::equivalent(Asset &asset,
330         int test_audio,
331         int test_video)
332 {
333         int result = (!strcmp(asset.path, path) &&
334                 format == asset.format);
335
336         if(result && format == FILE_FFMPEG)
337                 result = !strcmp(fformat, asset.fformat);
338
339         if(test_audio && result)
340         {
341                 result = (channels == asset.channels &&
342                         sample_rate == asset.sample_rate &&
343                         bits == asset.bits &&
344                         byte_order == asset.byte_order &&
345                         signed_ == asset.signed_ &&
346                         header == asset.header &&
347                         dither == asset.dither &&
348                         !strcmp(acodec, asset.acodec));
349                 if(result && format == FILE_FFMPEG)
350                         result = !strcmp(ff_audio_options, asset.ff_audio_options) &&
351                                 ff_audio_bitrate == asset.ff_audio_bitrate;
352
353         }
354
355
356         if(test_video && result)
357         {
358                 result = (layers == asset.layers &&
359                         program == asset.program &&
360                         frame_rate == asset.frame_rate &&
361                         asset.interlace_autofixoption == interlace_autofixoption &&
362                         asset.interlace_mode    == interlace_mode &&
363                         interlace_fixmethod     == asset.interlace_fixmethod &&
364                         width == asset.width &&
365                         height == asset.height &&
366                         !strcmp(vcodec, asset.vcodec) &&
367                         strcmp(reel_name, asset.reel_name) == 0 &&
368                         reel_number == asset.reel_number &&
369                         tcstart == asset.tcstart &&
370                         tcend == asset.tcend &&
371                         tcformat == asset.tcformat);
372                 if(result && format == FILE_FFMPEG)
373                         result = !strcmp(ff_video_options, asset.ff_video_options) &&
374                                 ff_video_bitrate == asset.ff_video_bitrate &&
375                                 ff_video_quality == asset.ff_video_quality;
376         }
377
378         return result;
379 }
380
381 int Asset::operator==(Asset &asset)
382 {
383
384         return equivalent(asset,
385                 1,
386                 1);
387 }
388
389 int Asset::operator!=(Asset &asset)
390 {
391         return !(*this == asset);
392 }
393
394 int Asset::test_path(const char *path)
395 {
396         if(!strcasecmp(this->path, path))
397                 return 1;
398         else
399                 return 0;
400 }
401
402 int Asset::read(FileXML *file,
403         int expand_relative)
404 {
405         int result = 0;
406
407 // Check for relative path.
408         if(expand_relative)
409         {
410                 char new_path[BCTEXTLEN];
411                 char asset_directory[BCTEXTLEN];
412                 char input_directory[BCTEXTLEN];
413                 FileSystem fs;
414
415                 strcpy(new_path, path);
416                 fs.set_current_dir("");
417
418                 fs.extract_dir(asset_directory, path);
419
420 // No path in asset.
421 // Take path of XML file.
422                 if(!asset_directory[0])
423                 {
424                         fs.extract_dir(input_directory, file->filename);
425
426 // Input file has a path
427                         if(input_directory[0])
428                         {
429                                 fs.join_names(path, input_directory, new_path);
430                         }
431                 }
432         }
433
434
435         while(!result)
436         {
437                 result = file->read_tag();
438                 if(!result)
439                 {
440                         if(file->tag.title_is("/ASSET"))
441                         {
442                                 result = 1;
443                         }
444                         else
445                         if(file->tag.title_is("AUDIO"))
446                         {
447                                 read_audio(file);
448                         }
449                         else
450                         if(file->tag.title_is("AUDIO_OMIT"))
451                         {
452                                 read_audio(file);
453                         }
454                         else
455                         if(file->tag.title_is("FORMAT"))
456                         {
457                                 const char *string = file->tag.get_property("TYPE");
458                                 format = File::strtoformat(string);
459                                 use_header =
460                                         file->tag.get_property("USE_HEADER", use_header);
461                                 file->tag.get_property("FFORMAT", fformat);
462                         }
463                         else
464                         if(file->tag.title_is("FOLDER"))
465                         {
466                                 strcpy(folder, file->read_text());
467                         }
468                         else
469                         if(file->tag.title_is("VIDEO"))
470                         {
471                                 read_video(file);
472                         }
473                         else
474                         if(file->tag.title_is("VIDEO_OMIT"))
475                         {
476                                 read_video(file);
477                         }
478                         else
479                         if(file->tag.title_is("INDEX"))
480                         {
481                                 read_index(file);
482                         }
483                 }
484         }
485
486         boundaries();
487 //printf("Asset::read 2\n");
488         return 0;
489 }
490
491 int Asset::read_audio(FileXML *file)
492 {
493         if(file->tag.title_is("AUDIO")) audio_data = 1;
494         channels = file->tag.get_property("CHANNELS", 2);
495 // This is loaded from the index file after the EDL but this
496 // should be overridable in the EDL.
497         if(!sample_rate) sample_rate = file->tag.get_property("RATE", 48000);
498         bits = file->tag.get_property("BITS", 16);
499         byte_order = file->tag.get_property("BYTE_ORDER", 1);
500         signed_ = file->tag.get_property("SIGNED", 1);
501         header = file->tag.get_property("HEADER", 0);
502         dither = file->tag.get_property("DITHER", 0);
503
504         audio_length = file->tag.get_property("AUDIO_LENGTH", (int64_t)0);
505         acodec[0] = 0;
506         file->tag.get_property("ACODEC", acodec);
507
508         if(!video_data)
509         {
510                 tcstart = 0;
511                 tcend = audio_length;
512                 tcformat = 0;
513         }
514         return 0;
515 }
516
517 int Asset::read_video(FileXML *file)
518 {
519         char string[BCTEXTLEN];
520
521         if(file->tag.title_is("VIDEO")) video_data = 1;
522         actual_height = file->tag.get_property("ACTUAL_HEIGHT", actual_height);
523         actual_width = file->tag.get_property("ACTUAL_WIDTH", actual_width);
524         height = file->tag.get_property("HEIGHT", height);
525         width = file->tag.get_property("WIDTH", width);
526         layers = file->tag.get_property("LAYERS", layers);
527         program = file->tag.get_property("PROGRAM", program);
528 // This is loaded from the index file after the EDL but this
529 // should be overridable in the EDL.
530         if(EQUIV(frame_rate, 0)) frame_rate = file->tag.get_property("FRAMERATE", frame_rate);
531         vcodec[0] = 0;
532         file->tag.get_property("VCODEC", vcodec);
533
534         video_length = file->tag.get_property("VIDEO_LENGTH", (int64_t)0);
535
536         interlace_autofixoption = file->tag.get_property("INTERLACE_AUTOFIX",0);
537
538         ilacemode_to_xmltext(string, BC_ILACE_MODE_NOTINTERLACED);
539         interlace_mode = ilacemode_from_xmltext(file->tag.get_property("INTERLACE_MODE",string), BC_ILACE_MODE_NOTINTERLACED);
540
541         ilacefixmethod_to_xmltext(string, BC_ILACE_FIXMETHOD_NONE);
542         interlace_fixmethod = ilacefixmethod_from_xmltext(file->tag.get_property("INTERLACE_FIXMETHOD",string), BC_ILACE_FIXMETHOD_NONE);
543
544         file->tag.get_property("REEL_NAME", reel_name);
545         reel_number = file->tag.get_property("REEL_NUMBER", reel_number);
546         tcstart = file->tag.get_property("TCSTART", tcstart);
547         tcend = file->tag.get_property("TCEND", tcend);
548         tcformat = file->tag.get_property("TCFORMAT", tcformat);
549
550         return 0;
551 }
552
553 int Asset::read_index(FileXML *file)
554 {
555         index_state->read_xml(file, channels);
556         return 0;
557 }
558
559 // Output path is the path of the output file if name truncation is desired.
560 // It is a "" if complete names should be used.
561
562 int Asset::write(FileXML *file,
563         int include_index,
564         const char *output_path)
565 {
566         char new_path[BCTEXTLEN];
567         char asset_directory[BCTEXTLEN];
568         char output_directory[BCTEXTLEN];
569         FileSystem fs;
570
571 // Make path relative
572         fs.extract_dir(asset_directory, path);
573         if(output_path && output_path[0])
574                 fs.extract_dir(output_directory, output_path);
575         else
576                 output_directory[0] = 0;
577
578 // Asset and EDL are in same directory.  Extract just the name.
579         if(!strcmp(asset_directory, output_directory))
580         {
581                 fs.extract_name(new_path, path);
582         }
583         else
584         {
585                 strcpy(new_path, path);
586         }
587
588         file->tag.set_title("ASSET");
589         file->tag.set_property("SRC", new_path);
590         file->append_tag();
591         file->append_newline();
592
593         file->tag.set_title("FOLDER");
594         file->append_tag();
595         file->append_text(folder);
596         file->tag.set_title("/FOLDER");
597         file->append_tag();
598         file->append_newline();
599
600 // Write the format information
601         file->tag.set_title("FORMAT");
602
603         file->tag.set_property("TYPE",
604                 File::formattostr(format));
605         file->tag.set_property("USE_HEADER", use_header);
606         file->tag.set_property("FFORMAT", fformat);
607
608         file->append_tag();
609         file->tag.set_title("/FORMAT");
610         file->append_tag();
611         file->append_newline();
612
613 // Requiring data to exist caused batch render to lose settings.
614 // But the only way to know if an asset doesn't have audio or video data
615 // is to not write the block.
616 // So change the block name if the asset doesn't have the data.
617         write_audio(file);
618         write_video(file);
619 // index goes after source
620         if(index_state->index_status == INDEX_READY && include_index)
621                 write_index(file);
622
623         file->tag.set_title("/ASSET");
624         file->append_tag();
625         file->append_newline();
626         return 0;
627 }
628
629 int Asset::write_audio(FileXML *file)
630 {
631 // Let the reader know if the asset has the data by naming the block.
632         if(audio_data)
633                 file->tag.set_title("AUDIO");
634         else
635                 file->tag.set_title("AUDIO_OMIT");
636 // Necessary for PCM audio
637         file->tag.set_property("CHANNELS", channels);
638         file->tag.set_property("RATE", sample_rate);
639         file->tag.set_property("BITS", bits);
640         file->tag.set_property("BYTE_ORDER", byte_order);
641         file->tag.set_property("SIGNED", signed_);
642         file->tag.set_property("HEADER", header);
643         file->tag.set_property("DITHER", dither);
644         if(acodec[0])
645                 file->tag.set_property("ACODEC", acodec);
646
647         file->tag.set_property("AUDIO_LENGTH", audio_length);
648
649
650
651 // Rely on defaults operations for these.
652
653 //      file->tag.set_property("AMPEG_BITRATE", ampeg_bitrate);
654 //      file->tag.set_property("AMPEG_DERIVATIVE", ampeg_derivative);
655 //
656 //      file->tag.set_property("VORBIS_VBR", vorbis_vbr);
657 //      file->tag.set_property("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
658 //      file->tag.set_property("VORBIS_BITRATE", vorbis_bitrate);
659 //      file->tag.set_property("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
660 //
661 //      file->tag.set_property("MP3_BITRATE", mp3_bitrate);
662 //
663
664
665
666         file->append_tag();
667         if(audio_data)
668           file->tag.set_title("/AUDIO");
669         else
670           file->tag.set_title("/AUDIO_OMIT");
671         file->append_tag();
672         file->append_newline();
673         return 0;
674 }
675
676 int Asset::write_video(FileXML *file)
677 {
678         char string[BCTEXTLEN];
679
680         if(video_data)
681                 file->tag.set_title("VIDEO");
682         else
683                 file->tag.set_title("VIDEO_OMIT");
684         file->tag.set_property("ACTUAL_HEIGHT", actual_height);
685         file->tag.set_property("ACTUAL_WIDTH", actual_width);
686         file->tag.set_property("HEIGHT", height);
687         file->tag.set_property("WIDTH", width);
688         file->tag.set_property("LAYERS", layers);
689         file->tag.set_property("PROGRAM", program);
690         file->tag.set_property("FRAMERATE", frame_rate);
691         if(vcodec[0])
692                 file->tag.set_property("VCODEC", vcodec);
693
694         file->tag.set_property("VIDEO_LENGTH", video_length);
695
696         file->tag.set_property("INTERLACE_AUTOFIX", interlace_autofixoption);
697
698         ilacemode_to_xmltext(string, interlace_mode);
699         file->tag.set_property("INTERLACE_MODE", string);
700
701         ilacefixmethod_to_xmltext(string, interlace_fixmethod);
702         file->tag.set_property("INTERLACE_FIXMETHOD", string);
703
704
705         file->tag.set_property("REEL_NAME", reel_name);
706         file->tag.set_property("REEL_NUMBER", reel_number);
707         file->tag.set_property("TCSTART", tcstart);
708         file->tag.set_property("TCEND", tcend);
709         file->tag.set_property("TCFORMAT", tcformat);
710
711         file->append_tag();
712         if(video_data)
713                 file->tag.set_title("/VIDEO");
714         else
715                 file->tag.set_title("/VIDEO_OMIT");
716
717         file->append_tag();
718         file->append_newline();
719         return 0;
720 }
721
722 int Asset::write_index(FileXML *file)
723 {
724         index_state->write_xml(file);
725         return 0;
726 }
727
728
729 char* Asset::construct_param(const char *param, const char *prefix, char *return_value)
730 {
731         if(prefix)
732                 sprintf(return_value, "%s%s", prefix, param);
733         else
734                 strcpy(return_value, param);
735         return return_value;
736 }
737
738 #define UPDATE_DEFAULT(x, y) defaults->update(construct_param(x, prefix, string), y);
739 #define GET_DEFAULT(x, y) defaults->get(construct_param(x, prefix, string), y);
740
741 void Asset::load_defaults(BC_Hash *defaults,
742         const char *prefix,
743         int do_format,
744         int do_compression,
745         int do_path,
746         int do_data_types,
747         int do_bits)
748 {
749         char string[BCTEXTLEN];
750
751 // Can't save codec here because it's specific to render, record, and effect.
752 // The codec has to be UNKNOWN for file probing to work.
753
754         if(do_path)
755         {
756                 GET_DEFAULT("PATH", path);
757         }
758
759         if(do_compression)
760         {
761                 GET_DEFAULT("AUDIO_CODEC", acodec);
762                 GET_DEFAULT("VIDEO_CODEC", vcodec);
763         }
764
765         if(do_format)
766         {
767                 format = GET_DEFAULT("FORMAT", format);
768                 use_header = GET_DEFAULT("USE_HEADER", use_header);
769                 GET_DEFAULT("FFORMAT", fformat);
770         }
771
772         if(do_data_types)
773         {
774                 audio_data = GET_DEFAULT("AUDIO", 1);
775                 video_data = GET_DEFAULT("VIDEO", 1);
776         }
777
778         if(do_bits)
779         {
780                 bits = GET_DEFAULT("BITS", 16);
781                 dither = GET_DEFAULT("DITHER", 0);
782                 signed_ = GET_DEFAULT("SIGNED", 1);
783                 byte_order = GET_DEFAULT("BYTE_ORDER", 1);
784
785
786
787                 channels = GET_DEFAULT("CHANNELS", 2);
788                 if(!sample_rate) sample_rate = GET_DEFAULT("RATE", 48000);
789                 header = GET_DEFAULT("HEADER", 0);
790                 audio_length = GET_DEFAULT("AUDIO_LENGTH", (int64_t)0);
791
792
793
794                 height = GET_DEFAULT("HEIGHT", height);
795                 width = GET_DEFAULT("WIDTH", width);
796                 actual_height = GET_DEFAULT("ACTUAL_HEIGHT", actual_height);
797                 actual_width = GET_DEFAULT("ACTUAL_WIDTH", actual_width);
798                 program = GET_DEFAULT("PROGRAM", program);
799                 layers = GET_DEFAULT("LAYERS", layers);
800                 if(EQUIV(frame_rate, 0)) frame_rate = GET_DEFAULT("FRAMERATE", frame_rate);
801                 video_length = GET_DEFAULT("VIDEO_LENGTH", (int64_t)0);
802         }
803
804         ampeg_bitrate = GET_DEFAULT("AMPEG_BITRATE", ampeg_bitrate);
805         ampeg_derivative = GET_DEFAULT("AMPEG_DERIVATIVE", ampeg_derivative);
806
807         vorbis_vbr = GET_DEFAULT("VORBIS_VBR", vorbis_vbr);
808         vorbis_min_bitrate = GET_DEFAULT("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
809         vorbis_bitrate = GET_DEFAULT("VORBIS_BITRATE", vorbis_bitrate);
810         vorbis_max_bitrate = GET_DEFAULT("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
811
812         theora_fix_bitrate = GET_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
813         theora_bitrate = GET_DEFAULT("THEORA_BITRATE", theora_bitrate);
814         theora_quality = GET_DEFAULT("THEORA_QUALITY", theora_quality);
815         theora_sharpness = GET_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
816         theora_keyframe_frequency = GET_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
817         theora_keyframe_force_frequency = GET_DEFAULT("THEORA_FORCE_KEYFRAME_FREQUENCY", theora_keyframe_force_frequency);
818
819         GET_DEFAULT("FF_AUDIO_OPTIONS", ff_audio_options);
820         ff_audio_bitrate = GET_DEFAULT("FF_AUDIO_BITRATE", ff_audio_bitrate);
821         GET_DEFAULT("FF_VIDEO_OPTIONS", ff_video_options);
822         ff_video_bitrate = GET_DEFAULT("FF_VIDEO_BITRATE", ff_video_bitrate);
823         ff_video_quality = GET_DEFAULT("FF_VIDEO_QUALITY", ff_video_quality);
824
825         mp3_bitrate = GET_DEFAULT("MP3_BITRATE", mp3_bitrate);
826
827         jpeg_quality = GET_DEFAULT("JPEG_QUALITY", jpeg_quality);
828         aspect_ratio = GET_DEFAULT("ASPECT_RATIO", aspect_ratio);
829
830         interlace_autofixoption = BC_ILACE_AUTOFIXOPTION_AUTO;
831         interlace_mode          = BC_ILACE_MODE_UNDETECTED;
832         interlace_fixmethod     = BC_ILACE_FIXMETHOD_UPONE;
833
834 // MPEG format information
835         vmpeg_iframe_distance = GET_DEFAULT("VMPEG_IFRAME_DISTANCE", vmpeg_iframe_distance);
836         vmpeg_pframe_distance = GET_DEFAULT("VMPEG_PFRAME_DISTANCE", vmpeg_pframe_distance);
837         vmpeg_progressive = GET_DEFAULT("VMPEG_PROGRESSIVE", vmpeg_progressive);
838         vmpeg_denoise = GET_DEFAULT("VMPEG_DENOISE", vmpeg_denoise);
839         vmpeg_bitrate = GET_DEFAULT("VMPEG_BITRATE", vmpeg_bitrate);
840         vmpeg_derivative = GET_DEFAULT("VMPEG_DERIVATIVE", vmpeg_derivative);
841         vmpeg_quantization = GET_DEFAULT("VMPEG_QUANTIZATION", vmpeg_quantization);
842         vmpeg_cmodel = GET_DEFAULT("VMPEG_CMODEL", vmpeg_cmodel);
843         vmpeg_fix_bitrate = GET_DEFAULT("VMPEG_FIX_BITRATE", vmpeg_fix_bitrate);
844         vmpeg_seq_codes = GET_DEFAULT("VMPEG_SEQ_CODES", vmpeg_seq_codes);
845         vmpeg_preset = GET_DEFAULT("VMPEG_PRESET", vmpeg_preset);
846         vmpeg_field_order = GET_DEFAULT("VMPEG_FIELD_ORDER", vmpeg_field_order);
847
848         theora_fix_bitrate = GET_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
849         theora_bitrate = GET_DEFAULT("THEORA_BITRATE", theora_bitrate);
850         theora_quality = GET_DEFAULT("THEORA_QUALITY", theora_quality);
851         theora_sharpness = GET_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
852         theora_keyframe_frequency = GET_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
853         theora_keyframe_force_frequency = GET_DEFAULT("THEORA_FORCE_KEYFRAME_FEQUENCY", theora_keyframe_force_frequency);
854
855
856         ac3_bitrate = GET_DEFAULT("AC3_BITRATE", ac3_bitrate);
857
858         png_use_alpha = GET_DEFAULT("PNG_USE_ALPHA", png_use_alpha);
859         exr_use_alpha = GET_DEFAULT("EXR_USE_ALPHA", exr_use_alpha);
860         exr_compression = GET_DEFAULT("EXR_COMPRESSION", exr_compression);
861         tiff_cmodel = GET_DEFAULT("TIFF_CMODEL", tiff_cmodel);
862         tiff_compression = GET_DEFAULT("TIFF_COMPRESSION", tiff_compression);
863
864         GET_DEFAULT("REEL_NAME", reel_name);
865         reel_number = GET_DEFAULT("REEL_NUMBER", reel_number);
866         tcstart = GET_DEFAULT("TCSTART", tcstart);
867         tcend = GET_DEFAULT("TCEND", tcend);
868         tcformat = GET_DEFAULT("TCFORMAT", tcformat);
869
870         boundaries();
871 }
872
873 void Asset::save_defaults(BC_Hash *defaults,
874         const char *prefix,
875         int do_format,
876         int do_compression,
877         int do_path,
878         int do_data_types,
879         int do_bits)
880 {
881         char string[BCTEXTLEN];
882
883         UPDATE_DEFAULT("PATH", path);
884
885
886
887
888         if(do_format)
889         {
890                 UPDATE_DEFAULT("FORMAT", format);
891                 UPDATE_DEFAULT("USE_HEADER", use_header);
892                 UPDATE_DEFAULT("FFORMAT", fformat);
893         }
894
895         if(do_data_types)
896         {
897                 UPDATE_DEFAULT("AUDIO", audio_data);
898                 UPDATE_DEFAULT("VIDEO", video_data);
899         }
900
901         if(do_compression)
902         {
903                 UPDATE_DEFAULT("AUDIO_CODEC", acodec);
904                 UPDATE_DEFAULT("VIDEO_CODEC", vcodec);
905
906                 UPDATE_DEFAULT("AMPEG_BITRATE", ampeg_bitrate);
907                 UPDATE_DEFAULT("AMPEG_DERIVATIVE", ampeg_derivative);
908
909                 UPDATE_DEFAULT("VORBIS_VBR", vorbis_vbr);
910                 UPDATE_DEFAULT("VORBIS_MIN_BITRATE", vorbis_min_bitrate);
911                 UPDATE_DEFAULT("VORBIS_BITRATE", vorbis_bitrate);
912                 UPDATE_DEFAULT("VORBIS_MAX_BITRATE", vorbis_max_bitrate);
913
914                 UPDATE_DEFAULT("FF_AUDIO_OPTIONS", ff_audio_options);
915                 UPDATE_DEFAULT("FF_AUDIO_BITRATE", ff_audio_bitrate);
916                 UPDATE_DEFAULT("FF_VIDEO_OPTIONS", ff_video_options);
917                 UPDATE_DEFAULT("FF_VIDEO_BITRATE", ff_video_bitrate);
918                 UPDATE_DEFAULT("FF_VIDEO_QUALITY", ff_video_quality);
919
920                 UPDATE_DEFAULT("THEORA_FIX_BITRATE", theora_fix_bitrate);
921                 UPDATE_DEFAULT("THEORA_BITRATE", theora_bitrate);
922                 UPDATE_DEFAULT("THEORA_QUALITY", theora_quality);
923                 UPDATE_DEFAULT("THEORA_SHARPNESS", theora_sharpness);
924                 UPDATE_DEFAULT("THEORA_KEYFRAME_FREQUENCY", theora_keyframe_frequency);
925                 UPDATE_DEFAULT("THEORA_FORCE_KEYFRAME_FREQUENCY", theora_keyframe_force_frequency);
926
927
928
929                 UPDATE_DEFAULT("MP3_BITRATE", mp3_bitrate);
930
931                 UPDATE_DEFAULT("JPEG_QUALITY", jpeg_quality);
932                 UPDATE_DEFAULT("ASPECT_RATIO", aspect_ratio);
933
934 // MPEG format information
935                 UPDATE_DEFAULT("VMPEG_IFRAME_DISTANCE", vmpeg_iframe_distance);
936                 UPDATE_DEFAULT("VMPEG_PFRAME_DISTANCE", vmpeg_pframe_distance);
937                 UPDATE_DEFAULT("VMPEG_PROGRESSIVE", vmpeg_progressive);
938                 UPDATE_DEFAULT("VMPEG_DENOISE", vmpeg_denoise);
939                 UPDATE_DEFAULT("VMPEG_BITRATE", vmpeg_bitrate);
940                 UPDATE_DEFAULT("VMPEG_DERIVATIVE", vmpeg_derivative);
941                 UPDATE_DEFAULT("VMPEG_QUANTIZATION", vmpeg_quantization);
942                 UPDATE_DEFAULT("VMPEG_CMODEL", vmpeg_cmodel);
943                 UPDATE_DEFAULT("VMPEG_FIX_BITRATE", vmpeg_fix_bitrate);
944                 UPDATE_DEFAULT("VMPEG_SEQ_CODES", vmpeg_seq_codes);
945                 UPDATE_DEFAULT("VMPEG_PRESET", vmpeg_preset);
946                 UPDATE_DEFAULT("VMPEG_FIELD_ORDER", vmpeg_field_order);
947
948
949                 UPDATE_DEFAULT("AC3_BITRATE", ac3_bitrate);
950
951
952                 UPDATE_DEFAULT("PNG_USE_ALPHA", png_use_alpha);
953                 UPDATE_DEFAULT("EXR_USE_ALPHA", exr_use_alpha);
954                 UPDATE_DEFAULT("EXR_COMPRESSION", exr_compression);
955                 UPDATE_DEFAULT("TIFF_CMODEL", tiff_cmodel);
956                 UPDATE_DEFAULT("TIFF_COMPRESSION", tiff_compression);
957         }
958
959         if(do_bits)
960         {
961                 UPDATE_DEFAULT("BITS", bits);
962                 UPDATE_DEFAULT("DITHER", dither);
963                 UPDATE_DEFAULT("SIGNED", signed_);
964                 UPDATE_DEFAULT("BYTE_ORDER", byte_order);
965
966
967
968
969
970
971                 UPDATE_DEFAULT("CHANNELS", channels);
972                 UPDATE_DEFAULT("RATE", sample_rate);
973                 UPDATE_DEFAULT("HEADER", header);
974                 UPDATE_DEFAULT("AUDIO_LENGTH", audio_length);
975
976
977
978                 UPDATE_DEFAULT("HEIGHT", height);
979                 UPDATE_DEFAULT("WIDTH", width);
980                 UPDATE_DEFAULT("ACTUAL_HEIGHT", actual_height);
981                 UPDATE_DEFAULT("ACTUAL_WIDTH", actual_width);
982                 UPDATE_DEFAULT("PROGRAM", program);
983                 UPDATE_DEFAULT("LAYERS", layers);
984                 UPDATE_DEFAULT("FRAMERATE", frame_rate);
985                 UPDATE_DEFAULT("VIDEO_LENGTH", video_length);
986
987         }
988
989         UPDATE_DEFAULT("REEL_NAME", reel_name);
990         UPDATE_DEFAULT("REEL_NUMBER", reel_number);
991         UPDATE_DEFAULT("TCSTART", tcstart);
992         UPDATE_DEFAULT("TCEND", tcend);
993         UPDATE_DEFAULT("TCFORMAT", tcformat);
994 }
995
996
997
998
999
1000
1001
1002
1003
1004 int Asset::dump(FILE *fp)
1005 {
1006         fprintf(fp,"  asset::dump\n");
1007         fprintf(fp,"   this=%p path=%s\n", this, path);
1008         fprintf(fp,"   index_status %d\n", index_state->index_status);
1009         fprintf(fp,"   format %d\n", format);
1010         fprintf(fp,"   fformat=\"%s\"\n", fformat);
1011         fprintf(fp,"   ff_audio_options=\"%s\"\n", ff_audio_options);
1012         fprintf(fp,"   ff_audio_bitrate=%d\n", ff_audio_bitrate);
1013         fprintf(fp,"   ff_video_options=\"%s\"\n", ff_video_options);
1014         fprintf(fp,"   ff_video_bitrate=%d\n", ff_video_bitrate);
1015         fprintf(fp,"   ff_video_quality=%d\n", ff_video_quality);
1016         fprintf(fp,"   audio_data %d channels %d samplerate %d bits %d"
1017                 " byte_order %d signed %d header %d dither %d acodec %c%c%c%c\n",
1018                 audio_data, channels, sample_rate, bits, byte_order, signed_,
1019                 header, dither, acodec[0], acodec[1], acodec[2], acodec[3]);
1020         fprintf(fp,"   audio_length %jd\n", audio_length);
1021         char string[BCTEXTLEN];
1022         ilacemode_to_xmltext(string, interlace_mode);
1023         fprintf(fp,"   video_data %d program %d layers %d framerate %f width %d"
1024                 " height %d vcodec %c%c%c%c aspect_ratio %f ilace_mode %s\n",
1025                 video_data, layers, program, frame_rate, width, height,
1026                 vcodec[0], vcodec[1], vcodec[2], vcodec[3], aspect_ratio,string);
1027         printf("   reel_name %s reel_number %i tcstart %jd tcend %jd tcf %d\n",
1028                 reel_name, reel_number, tcstart, tcend, tcformat);
1029         fprintf(fp,"   video_length %jd \n", video_length);
1030
1031
1032         return 0;
1033 }
1034
1035
1036 // For Indexable
1037 int Asset::get_audio_channels()
1038 {
1039         return channels;
1040 }
1041
1042 int Asset::get_sample_rate()
1043 {
1044         return sample_rate;
1045 }
1046
1047 int64_t Asset::get_audio_samples()
1048 {
1049         return audio_length;
1050 }
1051
1052 int Asset::have_audio()
1053 {
1054         return audio_data;
1055 }
1056
1057 int Asset::have_video()
1058 {
1059         return video_data;
1060 }
1061
1062 int Asset::get_w()
1063 {
1064         return width;
1065 }
1066
1067 int Asset::get_h()
1068 {
1069         return height;
1070 }
1071
1072 double Asset::get_frame_rate()
1073 {
1074         return frame_rate;
1075 }
1076
1077
1078 int Asset::get_video_layers()
1079 {
1080         return layers;
1081 }
1082
1083 int Asset::get_program()
1084 {
1085         return program;
1086 }
1087
1088 int64_t Asset::get_video_frames()
1089 {
1090         return video_length;
1091 }
1092
1093 double Asset::total_length_framealigned(double fps)
1094 {
1095         if (video_data && audio_data) {
1096                 double aud = floor(( (double)audio_length / sample_rate) * fps) / fps;
1097                 double vid = floor(( (double)video_length / frame_rate) * fps) / fps;
1098                 return MIN(aud,vid);
1099         }
1100
1101         if (audio_data)
1102                 return (double)audio_length / sample_rate;
1103
1104         if (video_data)
1105                 return (double)video_length / frame_rate;
1106
1107         return 0;
1108 }
1109
1110 int Asset::set_timecode(char *tc, int format, int end)
1111 {
1112         int hr, min, sec;
1113
1114         hr = ((int) tc[0] - 48) * 10 + (int) tc[1] - 48;
1115         min = ((int) tc[3] - 48) * 10 + (int) tc[4] - 48;
1116         sec = ((int) tc[6] - 48) * 10 + (int) tc[7] - 48;
1117
1118         // This needs to be modified to handle drop-frame
1119
1120         if(end)
1121                 tcend = (int64_t) (((hr * 3600) + (min * 60) + sec) * frame_rate);
1122         else
1123                 tcstart = (int64_t) (((hr * 3600) + (min * 60) + sec) * frame_rate);
1124
1125         tcformat = format;
1126         return 0;
1127 }
1128
1129