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