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