version update, bld tweaks, resize track booby, 10fps for images
[goodguy/cinelerra.git] / cinelerra-5.1 / libzmpeg3 / atrack.C
1 #include "libzmpeg3.h"
2
3 zatrack_t::
4 atrack_t(zmpeg3_t *zsrc, int custom_id, int format,
5          demuxer_t *demux, int no)
6 {
7   channels = zsrc->channel_counts ? zsrc->channel_counts[no] : 0;
8   sample_rate = 0;
9   total_samples = 0;
10   demuxer = new demuxer_t(zsrc, this, 0, custom_id);
11   if( zsrc->seekable ) {
12     demux->copy_titles(demuxer);
13   }
14
15   current_position = 0;
16   nudge = 0;
17   number = no;
18   pid = custom_id;
19   pts_origin = -1.;
20   reset_pts();
21
22   if( zsrc->sample_offsets ) { /* Copy pointers */
23     sample_offsets = zsrc->sample_offsets[number];
24     total_sample_offsets = zsrc->total_sample_offsets[number];
25     total_samples = zsrc->total_samples[number];
26     demuxer->stream_end = zsrc->audio_eof[number];
27     nudge = zsrc->nudging[number];
28   }
29 }
30
31 zatrack_t *zmpeg3_t::
32 new_atrack_t(int custom_id, int format, demuxer_t *demux, int number)
33 {
34   atrack_t *new_atrack = new atrack_t(this,custom_id,format,demux,number);
35   new_atrack->audio = new_audio_t(new_atrack, format);
36   if( !new_atrack->audio ) { /* Failed */
37     delete new_atrack;
38     new_atrack = 0;
39   }
40
41   return new_atrack;
42 }
43
44 zatrack_t::
45 ~atrack_t()
46 {
47   if( audio ) delete audio;
48   if( demuxer ) delete demuxer;
49   if( sample_offsets && private_offsets ) {
50     delete [] sample_offsets;
51   }
52 }
53
54 void zatrack_t::
55 extend_sample_offsets()
56 {
57   if( sample_offsets_allocated <= total_sample_offsets ) {
58     long new_allocation = ZMAX(2*total_sample_offsets, 1024);
59     int64_t *new_offsets = new int64_t[new_allocation];
60     for( int i=0; i<total_sample_offsets; ++i )
61       new_offsets[i] = sample_offsets[i];
62     delete [] sample_offsets;
63     sample_offsets = new_offsets;
64     sample_offsets_allocated = new_allocation;
65   }
66 }
67
68 void zatrack_t::
69 append_samples(int64_t offset)
70 {
71   extend_sample_offsets();
72   sample_offsets[total_sample_offsets++] = offset;
73   private_offsets = 1;
74 }
75
76 void zatrack_t::update_audio_time()
77 {
78   double pts = frame_pts;
79   frame_pts = -1;
80   if( pts < 0 ) return;
81   int64_t audio_pos = audio->audio_position();
82   if( pts_starttime < 0. && sample_rate > 0 ) {
83     pts_starttime = pts;
84     pts_offset = audio_pos / (double)sample_rate;
85     if( pts_origin < 0. )
86       pts_origin = pts - pts_offset;
87   }
88   else if( pts < pts_starttime ) { // check for pts rollover
89     if( pts_starttime-pts > 0x100000000ll / 90000 )
90       pts += 0x200000000ll / 90000;
91   }
92   double atime = pts_audio_time(pts);
93   if( atime > audio_time ) {
94     pts_position = audio_pos;
95     audio_time = atime;
96 //zmsgs("track %02x audio_time=%f\n", pid, audio_time);
97   }
98 }
99
100 double zatrack_t::get_audio_time()
101 {
102   double atime = audio_time;
103   if( atime >= 0 && sample_rate > 0 ) {
104     int64_t audio_pos = audio->audio_position();
105     atime += (audio_pos-pts_position) / (double)sample_rate;
106   }
107   return atime;
108 }
109
110 void zatrack_t::
111 reset_pts()
112 {
113   askip = 0;
114   demuxer->pes_audio_time = -1.;
115   pts_starttime = demuxer->src->pts_padding >= 0 ? -1. : 0.;
116   audio_time = -1.;
117   frame_pts = -1.;
118   pts_position = 0;
119   pts_offset = 0;
120 }
121
122 int64_t zatrack_t::apparent_position()
123 {
124   int64_t pos = demuxer->absolute_position();
125   int l = -1;
126   int r = total_sample_offsets;
127   while( (r-l) > 1 ) {
128     int m = (r+l) >> 1;
129     int64_t mpos = sample_offsets[m];
130     if( pos == mpos ) return m;
131     if( pos > mpos ) l = m;
132     else r = m;
133   }
134   int64_t result = (int64_t)l * AUDIO_CHUNKSIZE;
135   if( r < total_sample_offsets )
136     result += (AUDIO_CHUNKSIZE * (pos - sample_offsets[l])) /
137                  (sample_offsets[r] - sample_offsets[l]);
138   return result;
139 }
140