allow ffmpeg video to resample curr_pos, add bluray format
[goodguy/history.git] / cinelerra-5.0 / quicktime / qtprivate.h
1 #ifndef PRIVATE_H
2 #define PRIVATE_H
3
4 /* ================================= structures */
5
6 /* Version used internally.  You need to query it with the C functions */
7 #define QUICKTIME_MAJOR 2
8 #define QUICKTIME_MINOR 3
9 #define QUICKTIME_RELEASE 0
10
11
12 #define HEADER_LENGTH 8
13 #define MAXTRACKS 1024
14
15 /* Crazy Mich R. Soft constants */
16 #define AVI_HASINDEX       0x00000010  // Index at end of file?
17 #define AVI_MUSTUSEINDEX   0x00000020
18 #define AVI_ISINTERLEAVED  0x00000100
19 #define AVI_TRUSTCKTYPE    0x00000800  // Use CKType to find key frames?
20 #define AVI_WASCAPTUREFILE 0x00010000
21 #define AVI_COPYRIGHTED    0x00020000
22 #define AVIF_WASCAPTUREFILE     0x00010000
23 #define AVI_KEYFRAME       0x10
24 #define AVI_INDEX_OF_CHUNKS 0x01
25 #define AVI_INDEX_OF_INDEXES 0x00
26
27 #define AVI_FRAME_RATE_BASE 10000
28 #define MAX_RIFFS  0x100
29
30 #define QT_TRACE printf("%s: %d\n", __FILE__, __LINE__);
31
32 #include <stdio.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35
36
37
38
39 #define FTELL ftello64
40 #define FSEEK fseeko64
41
42
43 // ffmpeg requires global variable initialization
44 // These are defined in mpeg4.c
45 extern int ffmpeg_initialized;
46 extern pthread_mutex_t ffmpeg_lock;
47
48
49
50 // Utility functions for vbr audio.  This is accessed through quicktime_audio_map_t.
51 typedef struct
52 {
53 // End of the input/output buffer in the file in samples
54         int64_t input_end, output_end;
55 // Number of samples in input/output buffer before end
56         int input_size, output_size;
57 // Position in input/output buffer of buffer_end
58         int inp_ptr, out_ptr;
59 // Output buffer, linear for each channel.
60         double **output_buffer;
61 // Copy of channel count for deletion
62         int channels;
63 // Input buffer for temporary compressed data
64         int input_allocation;
65         unsigned char *input_buffer;
66 // Next sample/frame to read
67         int64_t sample;
68 } quicktime_vbr_t;
69
70
71
72
73
74
75
76
77
78 typedef struct
79 {
80 /* for AVI it's the end of the 8 byte header in the file */
81 /* for Quicktime it's the start of the 8 byte header in the file */
82         int64_t start;
83         int64_t end;        /* byte endpoint in file */
84         int64_t size;       /* byte size for writing */
85         int use_64;         /* Use 64 bit header */
86         unsigned char type[5];
87 } quicktime_atom_t;
88
89 typedef struct
90 {
91         float values[9];
92 } quicktime_matrix_t;
93
94
95 typedef struct
96 {
97         int version;
98         long flags;
99         unsigned long creation_time;
100         unsigned long modification_time;
101         int track_id;
102         long reserved1;
103         long duration;
104         char reserved2[8];
105         int layer;
106         int alternate_group;
107         float volume;
108         long reserved3;
109         quicktime_matrix_t matrix;
110         float track_width;
111         float track_height;
112 } quicktime_tkhd_t;
113
114
115 typedef struct
116 {
117         long seed;
118         long flags;
119         long size;
120         short int *alpha;
121         short int *red;
122         short int *green;
123         short int *blue;
124 } quicktime_ctab_t;
125
126
127
128 /* ===================== sample table ======================== // */
129
130
131
132 /* sample description */
133
134 typedef struct
135 {
136         int motion_jpeg_quantization_table;
137 } quicktime_mjqt_t;
138
139
140 typedef struct
141 {
142         int motion_jpeg_huffman_table;
143 } quicktime_mjht_t;
144
145
146 typedef struct
147 {
148         char *mpeg4_header;
149         int mpeg4_header_size;
150 } quicktime_esds_t;
151
152 typedef struct
153 {
154         char *data;
155         int data_size;
156 } quicktime_avcc_t;
157
158 typedef struct
159 {
160         char *data;
161         int data_size;
162 } quicktime_frma_t;
163
164 typedef struct
165 {
166         char format[4];
167         char reserved[6];
168         int data_reference;
169
170 /* common to audio and video */
171         int version;
172         int revision;
173         char vendor[4];
174
175 /* video description */
176         long temporal_quality;
177         long spatial_quality;
178         int width;
179         int height;
180         float dpi_horizontal;
181         float dpi_vertical;
182         int64_t data_size;
183         int frames_per_sample;
184         char compressor_name[32];
185         int depth;
186         int ctab_id;
187         quicktime_ctab_t ctab;
188         float gamma;
189 /* 0, 1, or 2 */
190         int fields;
191 /* 0 - unknown     1 - top first     2 - bottom first */
192         int field_dominance;
193         quicktime_mjqt_t mjqt;
194         quicktime_mjht_t mjht;
195
196 /* audio description */
197         int channels;
198         int sample_size;
199         int compression_id;
200         int packet_size;
201         float sample_rate;
202
203 /* Version 1 of audio description */
204         int samples_per_packet;
205         int bytes_per_packet;
206         int bytes_per_frame;
207         int bytes_per_sample;
208
209         quicktime_esds_t esds;
210         quicktime_avcc_t avcc;
211
212         int extradata_size;
213         char *extradata;
214
215         quicktime_frma_t frma;
216 } quicktime_stsd_table_t;
217
218
219 typedef struct
220 {
221         int version;
222         long flags;
223         long total_entries;
224         quicktime_stsd_table_t *table;
225 } quicktime_stsd_t;
226
227
228 /* time to sample */
229 typedef struct
230 {
231         long sample_count;
232         long sample_duration;
233 } quicktime_stts_table_t;
234
235 typedef struct
236 {
237         int version;
238         long flags;
239         long total_entries;
240         int is_vbr;
241         quicktime_stts_table_t *table;
242 } quicktime_stts_t;
243
244
245 /* sync sample */
246 typedef struct
247 {
248         int64_t sample;
249 } quicktime_stss_table_t;
250
251 typedef struct
252 {
253         int version;
254         long flags;
255         long total_entries;
256         long entries_allocated;
257         quicktime_stss_table_t *table;
258 } quicktime_stss_t;
259
260
261 /* sample to chunk */
262 typedef struct
263 {
264         long chunk;
265         long samples;
266         long id;
267 } quicktime_stsc_table_t;
268
269 typedef struct
270 {
271         int version;
272         long flags;
273         long total_entries;
274
275         long entries_allocated;
276         quicktime_stsc_table_t *table;
277 } quicktime_stsc_t;
278
279
280 /* sample size */
281 typedef struct
282 {
283         int64_t size;
284 } quicktime_stsz_table_t;
285
286 typedef struct
287 {
288         int version;
289         long flags;
290         int64_t sample_size;
291         long total_entries;
292
293         long entries_allocated;    /* used by the library for allocating a table */
294         quicktime_stsz_table_t *table;
295 } quicktime_stsz_t;
296
297
298 /* chunk offset */
299 typedef struct
300 {
301         int64_t offset;
302 } quicktime_stco_table_t;
303
304 typedef struct
305 {
306         int version;
307         long flags;
308         long total_entries;
309
310         long entries_allocated;    /* used by the library for allocating a table */
311         quicktime_stco_table_t *table;
312 } quicktime_stco_t;
313
314
315 /* sample table */
316 typedef struct
317 {
318         int version;
319         long flags;
320         quicktime_stsd_t stsd;
321         quicktime_stts_t stts;
322         quicktime_stss_t stss;
323         quicktime_stsc_t stsc;
324         quicktime_stsz_t stsz;
325         quicktime_stco_t stco;
326 } quicktime_stbl_t;
327
328 /* data reference */
329
330 typedef struct
331 {
332         int64_t size;
333         char type[4];
334         int version;
335         long flags;
336         char *data_reference;
337 } quicktime_dref_table_t;
338
339 typedef struct
340 {
341         int version;
342         long flags;
343         long total_entries;
344         quicktime_dref_table_t *table;
345 } quicktime_dref_t;
346
347 /* data information */
348
349 typedef struct
350 {
351         quicktime_dref_t dref;
352 } quicktime_dinf_t;
353
354 /* video media header */
355
356 typedef struct
357 {
358         int version;
359         long flags;
360         int graphics_mode;
361         int opcolor[3];
362 } quicktime_vmhd_t;
363
364
365 /* sound media header */
366
367 typedef struct
368 {
369         int version;
370         long flags;
371         int balance;
372         int reserved;
373 } quicktime_smhd_t;
374
375 /* handler reference */
376
377 typedef struct
378 {
379         int version;
380         long flags;
381         char component_type[4];
382         char component_subtype[4];
383         long component_manufacturer;
384         long component_flags;
385         long component_flag_mask;
386         char component_name[256];
387 } quicktime_hdlr_t;
388
389 /* media information */
390
391 typedef struct
392 {
393         int is_video;
394         int is_audio;
395         quicktime_vmhd_t vmhd;
396         quicktime_smhd_t smhd;
397         quicktime_stbl_t stbl;
398         quicktime_hdlr_t hdlr;
399         quicktime_dinf_t dinf;
400 } quicktime_minf_t;
401
402
403 /* media header */
404
405 typedef struct
406 {
407         int version;
408         long flags;
409         unsigned long creation_time;
410         unsigned long modification_time;
411         long time_scale;
412         long duration;
413         int language;
414         int quality;
415 } quicktime_mdhd_t;
416
417
418 /* media */
419
420 typedef struct
421 {
422         quicktime_mdhd_t mdhd;
423         quicktime_minf_t minf;
424         quicktime_hdlr_t hdlr;
425 } quicktime_mdia_t;
426
427 /* edit list */
428 typedef struct
429 {
430         long duration;
431         long time;
432         float rate;
433 } quicktime_elst_table_t;
434
435 typedef struct
436 {
437         int version;
438         long flags;
439         long total_entries;
440
441         quicktime_elst_table_t *table;
442 } quicktime_elst_t;
443
444 typedef struct
445 {
446         quicktime_elst_t elst;
447 } quicktime_edts_t;
448
449
450
451
452 typedef struct
453 {
454         quicktime_tkhd_t tkhd;
455         quicktime_mdia_t mdia;
456         quicktime_edts_t edts;
457 } quicktime_trak_t;
458
459
460 typedef struct
461 {
462         int version;
463         long flags;
464         unsigned long creation_time;
465         unsigned long modification_time;
466         long time_scale;
467         long duration;
468         float preferred_rate;
469         float preferred_volume;
470         char reserved[10];
471         quicktime_matrix_t matrix;
472         long preview_time;
473         long preview_duration;
474         long poster_time;
475         long selection_time;
476         long selection_duration;
477         long current_time;
478         long next_track_id;
479 } quicktime_mvhd_t;
480
481 typedef struct
482 {
483         char *copyright;
484         int copyright_len;
485         char *name;
486         int name_len;
487         char *info;
488         int info_len;
489         char *require;
490         int require_len;
491         char *encoder;
492         int encoder_len;
493 } quicktime_udta_t;
494
495
496 typedef struct
497 {
498         int total_tracks;
499
500         quicktime_mvhd_t mvhd;
501         quicktime_trak_t *trak[MAXTRACKS];
502         quicktime_udta_t udta;
503         quicktime_ctab_t ctab;
504 } quicktime_moov_t;
505
506 typedef struct
507 {
508         quicktime_atom_t atom;
509 } quicktime_mdat_t;
510
511 typedef struct
512 {
513 /* Offset of end of 8 byte chunk header relative to ix->base_offset */
514         int relative_offset;
515 /* size of data without 8 byte header */
516         int size;
517 } quicktime_ixtable_t;
518
519 typedef struct
520 {
521         quicktime_atom_t atom;
522         quicktime_ixtable_t *table;
523         int table_size;
524         int table_allocation;
525         int longs_per_entry;
526         int index_type;
527 /* ixtable relative_offset is relative to this */
528         int64_t base_offset;
529 /* ix atom title */
530         char tag[5];
531 /* corresponding chunk id */
532         char chunk_id[5];
533 } quicktime_ix_t;
534
535
536 typedef struct
537 {
538         quicktime_atom_t atom;
539
540 /* Partial index */
541 /* For writing only, there are multiple movi objects with multiple ix tables. */
542 /* This is not used for reading.  Instead an ix_t object in indx_t is used. */
543         quicktime_ix_t *ix[MAXTRACKS];
544 } quicktime_movi_t;
545
546 typedef struct
547 {
548 /* Start of start of corresponding ix## header */
549         int64_t index_offset;
550 /* Size not including 8 byte header */
551         int index_size;
552 /* duration in "ticks" */
553         int duration;
554
555 /* Partial index for reading only. */
556         quicktime_ix_t *ix;
557 } quicktime_indxtable_t;
558
559 typedef struct
560 {
561         quicktime_atom_t atom;
562         int longs_per_entry;
563         int index_subtype;
564         int index_type;
565 /* corresponding chunk id: 00wb, 00dc */
566         char chunk_id[5];
567
568 /* Number of partial indexes here */
569         int table_size;
570         int table_allocation;
571         quicktime_indxtable_t *table;
572 } quicktime_indx_t;
573
574 /* AVI equivalent for each trak.  Use file->moov.total_tracks */
575 /* Need it for super indexes during reading. */
576 typedef struct
577 {
578         quicktime_atom_t atom;
579 /* Super index for reading */
580         quicktime_indx_t indx;
581 /* AVI needs header placeholders before anything else is written */
582         int64_t length_offset;
583 /* Sample count read directly from auds */
584         int64_t samples;
585 /* Bytes per second read directly from auds */
586         int bytes_per_second;
587 /* Bytes totalled up from idx1 */
588         int64_t total_bytes;
589         int64_t samples_per_chunk_offset;
590         int64_t sample_size_offset;
591 /* Start of indx header for later writing */
592         int64_t indx_offset;
593 /* Size of JUNK without 8 byte header which is to be replaced by indx */
594         int64_t padding_size;
595 /* Tag for writer with NULL termination: 00wb, 00dc   Not available in reader.*/
596         char tag[5];
597 /* Flags for reader.  Not available in writer. */
598         int is_audio;
599         int is_video;
600 /* Notify reader the super indexes are valid */
601         int have_indx;
602 } quicktime_strl_t;
603
604 typedef struct
605 {
606         quicktime_atom_t atom;
607         int64_t frames_offset;
608         int64_t bitrate_offset;
609 /* Offsets to be written during file closure */
610         int64_t total_frames_offset;
611
612 /* AVI equivalent for each trak.  Use file->moov.total_tracks */
613 /* Need it for super indexes during reading. */
614         quicktime_strl_t *strl[MAXTRACKS];
615 } quicktime_hdrl_t;
616
617 typedef struct
618 {
619         char tag[5];
620         uint32_t flags;
621 /* Start of 8 byte chunk header relative to start of the 'movi' string */
622         int32_t offset;
623 /* Size of chunk less the 8 byte header */
624         int32_t size;
625 } quicktime_idx1table_t;
626
627 typedef struct
628 {
629         quicktime_atom_t atom;
630         quicktime_idx1table_t *table;
631         int table_size;
632         int table_allocation;
633 } quicktime_idx1_t;
634
635 typedef struct
636 {
637         quicktime_atom_t atom;
638         quicktime_movi_t movi;
639         quicktime_hdrl_t hdrl;
640
641 /* Full index */
642         quicktime_idx1_t idx1;
643 /* Notify reader the idx1 table is valid */
644         int have_idx1;
645         int have_hdrl;
646 } quicktime_riff_t;
647
648 typedef struct
649 {
650         unsigned char *y, *u, *v;
651         int y_size;
652         int u_size;
653         int v_size;
654         int64_t frame_number;
655 } quicktime_cacheframe_t;
656
657 typedef struct
658 {
659         quicktime_cacheframe_t *frames;
660         int total;
661         int allocation;
662         int max;
663 } quicktime_cache_t;
664
665 /* table of pointers to every track */
666 typedef struct
667 {
668         quicktime_trak_t *track; /* real quicktime track corresponding to this table */
669         int channels;            /* number of audio channels in the track */
670         int64_t current_position;   /* current sample in output file */
671         int64_t current_chunk;      /* current chunk in output file */
672         quicktime_vbr_t vbr;     /* Stores for vbr codecs */
673
674         void *codec;
675 } quicktime_audio_map_t;
676
677 typedef struct
678 {
679         quicktime_trak_t *track;
680         int64_t current_position;   /* current frame in output file */
681         int64_t current_chunk;      /* current chunk in output file */
682 // Cache for the current GOP after a seek.
683         quicktime_cache_t *frame_cache;
684
685         void *codec;
686 } quicktime_video_map_t;
687
688 /* file descriptor passed to all routines */
689 typedef struct
690 {
691         FILE *stream;
692         int64_t total_length;
693         quicktime_mdat_t mdat;
694         quicktime_moov_t moov;
695         int rd;
696         int wr;
697
698 /* If the moov atom is compressed */
699         int compressed_moov;
700         unsigned char *moov_data;
701 /*
702  * Temporary storage of compressed sizes.  If the file length is shorter than the
703  * uncompressed sizes, it won't work.
704  */
705         int64_t moov_end;
706         int64_t moov_size;
707         int64_t old_preload_size;
708         char *old_preload_buffer;
709         int64_t old_preload_start;
710         int64_t old_preload_end;
711         int64_t old_preload_ptr;
712
713
714
715
716
717 /* ASF/WMV section */
718         int use_asf;
719         void *asf;
720
721
722
723
724 /* AVI section */
725         int use_avi;
726 /* AVI tree */
727         quicktime_riff_t *riff[MAX_RIFFS];
728         int total_riffs;
729 /* is odml version of AVI */
730         int is_odml;
731
732
733
734
735
736
737
738
739 /* for begining and ending frame writes where the user wants to write the  */
740 /* file descriptor directly */
741         int64_t offset;
742
743 /* I/O */
744 /* Current position of virtual file descriptor */
745         int64_t file_position;
746
747 /* Work around a bug in glibc where ftello returns only 32 bits by maintaining */
748 /* our own position */
749         int64_t ftell_position;
750
751 /* Read ahead buffer */
752         int64_t preload_size;      /* Enables preload when nonzero. */
753         char *preload_buffer;
754         int64_t preload_start;     /* Start of preload_buffer in file */
755         int64_t preload_end;       /* End of preload buffer in file */
756         int64_t preload_ptr;       /* Offset of preload_start in preload_buffer */
757
758 /* Temporary storage until tracks are created */
759         int cache_size;
760
761 /* Write ahead buffer */
762 /* Amount of data in presave buffer */
763         int64_t presave_size;
764 /* Next presave byte's position in file */
765         int64_t presave_position;
766         char *presave_buffer;
767 /* Presave doesn't matter a whole lot, so its size is fixed */
768 #define QUICKTIME_PRESAVE 0x100000
769         int use_presave;
770
771 /* mapping of audio channels to movie tracks */
772 /* one audio map entry exists for each channel */
773         int total_atracks;
774         quicktime_audio_map_t *atracks;
775
776 /* mapping of video tracks to movie tracks */
777         int total_vtracks;
778         quicktime_video_map_t *vtracks;
779
780 /* Number of processors at our disposal */
781         int cpus;
782
783 /* Parameters for frame currently being decoded */
784         int do_scaling;
785         int in_x, in_y, in_w, in_h, out_w, out_h;
786         int color_model, row_span;
787
788 /* Adjustable timecode frame number */
789         int64_t current_frame;
790
791 } quicktime_t;
792
793
794 /* Structure created in track_map to describe the codec */
795 typedef struct
796 {
797         void (*delete_vcodec)(quicktime_video_map_t *vtrack);
798         void (*delete_acodec)(quicktime_audio_map_t *atrack);
799 /* Decode a single frame */
800         int (*decode_video)(quicktime_t *file,
801                                 unsigned char **row_pointers,
802                                 int track);
803 /* Encode a single frame */
804         int (*encode_video)(quicktime_t *file,
805                                 unsigned char **row_pointers,
806                                 int track);
807 /* Decode a certain number of samples */
808         int (*decode_audio)(quicktime_t *file,
809                                 int16_t *output_i,
810                                 float *output_f,
811                                 long samples,
812                                 int track,
813                                 int channel);
814 /* Encode a chunk of audio */
815         int (*encode_audio)(quicktime_t *file,
816                                 int16_t **input_i,
817                                 float **input_f,
818                                 int track,
819                                 long samples);
820         int (*reads_colormodel)(quicktime_t *file,
821                 int colormodel,
822                 int track);
823         int (*writes_colormodel)(quicktime_t *file,
824                 int colormodel,
825                 int track);
826
827         int (*set_parameter)(quicktime_t *file,
828                 int track,
829                 char *key,
830                 void *value);
831         void (*flush)(quicktime_t *file,
832                 int track);
833
834 /* AVI codec ID for audio.  AVI codec ID's are based on WAV files, by the way. */
835         int wav_id;
836
837 /* Pointer to static character code for identifying the codec. */
838         char *fourcc;
839
840 /* English title of codec.  Optional. */
841         char *title;
842
843 /* English description of codec.  Optional. */
844         char *desc;
845
846 /* Frame cache for seeking only. */
847
848
849 /* Proprietary data for the codec to allocate and delete. */
850         void *priv;
851 } quicktime_codec_t;
852
853
854
855 /* Structure tabulated in plugin.c to search the codecs */
856 typedef struct
857 {
858         int32_t fourcc, id;
859         void (*init_vcodec)(quicktime_video_map_t *);
860         void (*init_acodec)(quicktime_audio_map_t *);
861 } quicktime_codectable_t;
862
863
864
865
866 #endif