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