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