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