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