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