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