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