rework audio import_samples + resample + playback speed sampling, fix clear_boarder...
[goodguy/cinelerra.git] / cinelerra-5.1 / libzmpeg3 / audio / audio.C
1 #include "../libzmpeg3.h"
2
3 static class audio_decode_t {
4   pthread_mutex_t zlock;
5 public:
6   audio_decode_t() {
7     pthread_mutexattr_t attr;
8     pthread_mutexattr_init(&attr);
9     pthread_mutex_init(&zlock, &attr);
10   }
11   ~audio_decode_t() {
12     pthread_mutex_destroy(&zlock);
13   }
14   void lock() { pthread_mutex_lock(&zlock); }
15   void unlock() { pthread_mutex_unlock(&zlock); }
16 } audio_decode;
17
18 static void toc_error()
19 {
20   zerr( "mpeg3audio: sample accurate seeking without a table of contents \n"
21     "is no longer supported.  Use mpeg3toc <mpeg file> <table of contents>\n"
22     "to generate a table of contents and load the table of contents instead.\n");
23 }
24
25
26 int zaudio_t::
27 rewind_audio()
28 {
29   if( track->sample_offsets )
30     track->demuxer->seek_byte(track->sample_offsets[0]);
31   else
32     track->demuxer->seek_byte(0);
33   track->reset_pts();
34   packet_position = 0;
35   return 0;
36 }
37
38 /* Advance to next header and read it. */
39 int zaudio_t::
40 read_header()
41 {
42   int count = 0x10000;
43   int got_it = 0;
44   int result = 1;
45   int pos = 0;
46
47   switch(track->format) {
48   case afmt_AC3:
49     while( count > 0 && !track->demuxer->eof() ) {
50       if( pos >= (int)sizeof(packet_buffer)-8 ) {
51         memmove(packet_buffer, &packet_buffer[pos], packet_position-=pos);
52         pos = 0;
53       }
54       if( packet_position-pos < 8 ) {
55         packet_buffer[packet_position++] = track->demuxer->read_char();
56         continue;
57       }
58       got_it = ac3_decoder->ac3_header(&packet_buffer[pos]);
59       if( got_it ) break;
60       ++pos;  --count;
61     }
62
63     if( got_it ) {
64       channels = ac3_decoder->channels;
65       samplerate = ac3_decoder->samplerate;
66       framesize = ac3_decoder->framesize;
67       result = 0;
68     }
69     break;
70
71   case afmt_MPEG:
72     /* Layer 1 not supported */
73     if( layer_decoder->layer == 1 ) break;
74
75     /* Load starting bytes and check format when not seekable */
76     if( !src->seekable && !packet_position && !track->total_sample_offsets ) {
77       if( src->demuxer->payload_unit_start_indicator ) {
78         if( track->demuxer->read_data(packet_buffer,packet_position=4) ) break;
79         /* reject mp4 data */
80         if( !zaudio_decoder_layer_t::id3_check(packet_buffer) &&
81               zaudio_decoder_layer_t::layer_check(packet_buffer) ) {
82           zerrs("bad mp3 header, pid=%04x, track ignored\n", track->pid);
83           track->format = afmt_IGNORE;
84           result = -1;
85         }
86       }
87     }
88
89     while( count > 0 && !track->demuxer->eof() ) {
90       if( pos >= (int)sizeof(packet_buffer)-4 ) {
91         memmove(packet_buffer, &packet_buffer[pos], packet_position-=pos);
92         pos = 0;
93       }
94       if( packet_position-pos < 4 ) {
95         packet_buffer[packet_position++] = track->demuxer->read_char();
96         continue;
97       }
98       got_it = layer_decoder->layer3_header(&packet_buffer[pos]);
99       if( got_it ) break;
100 //zmsgs("%d got_it=%d packet=%02x%02x%02x%02x\n", __LINE__,
101 //  got_it, (uint8_t)packet_buffer[pos+0], (uint8_t)packet_buffer[pos+1],
102 //  (uint8_t)packet_buffer[pos+2], (uint8_t)packet_buffer[pos+3]);
103       ++pos;  --count;
104       /* ID3 tags need to reset the count to skip the tags */
105       if( layer_decoder->id3_state != id3_IDLE ) count = 0x10000;
106     }
107
108     if( got_it ) {
109       channels = layer_decoder->channels;
110       samplerate = layer_decoder->samplerate;
111       framesize = layer_decoder->framesize;
112       result = 0;
113     }
114     break;
115
116   case afmt_PCM:
117     while( count > 0 && !track->demuxer->eof() ) {
118       if( pos >= (int)sizeof(packet_buffer)-PCM_HEADERSIZE ) {
119         memmove(packet_buffer, &packet_buffer[pos], packet_position-=pos);
120         pos = 0;
121       }
122       if( packet_position-pos < PCM_HEADERSIZE ) {
123         packet_buffer[packet_position++] = track->demuxer->read_char();
124         continue;
125       }
126       got_it = pcm_decoder->pcm_header(&packet_buffer[pos]);
127       if( got_it ) break;
128       ++pos;  --count;
129     }
130
131     if( got_it ) {
132       channels = pcm_decoder->channels;
133       samplerate = pcm_decoder->samplerate;
134       framesize = pcm_decoder->framesize;
135       result = 0;
136     }
137     break;
138
139   default:
140     result = -1;
141     break;
142   }
143
144   if( pos > 0 )
145     memmove(packet_buffer, &packet_buffer[pos], packet_position-=pos);
146
147   if( track->demuxer->error() )
148     result = 1;
149   if( !result ) {
150     if( channels > track->channels )
151       track->channels = channels;
152     track->sample_rate = samplerate;
153   }
154 //zmsgs("%d %d %d\n", track->channels, track->sample_rate, framesize);
155
156   return result;
157 }
158
159 zaudio_t::
160 ~audio_t()
161 {
162   if( output ) {
163     for( int i=0; i<output_channels; ++i )
164       delete [] output[i];
165     delete [] output;
166   }
167   if( ac3_decoder ) delete ac3_decoder;
168   if( layer_decoder ) delete layer_decoder;
169   if( pcm_decoder ) delete pcm_decoder;
170 }
171
172 void zaudio_t::
173 update_channels()
174 {
175   int i;
176   float **new_output = new float *[channels];
177   if( output_channels < channels ) {
178     for( i=0; i<output_channels; ++i )
179       new_output[i] = output[i];
180     while( i < channels ) // more channels
181       new_output[i++] = new float[output_allocated];
182   }
183   else {
184     for( i=0; i<channels; ++i )
185       new_output[i] = output[i];
186     while( i < output_channels ) // fewer channels
187       delete [] output[i++];
188   }
189   delete [] output;
190   output = new_output;
191   output_channels = channels;
192 }
193
194 int zaudio_t::
195 read_frame(int render)
196 {
197   zdemuxer_t *demux = track->demuxer;
198   int samples = -1;
199
200   /* Find and read next header */
201   int result = read_header();
202 //if( src->seekable ) {
203 //  int64_t app_pos = track->apparent_position();
204 //  int64_t aud_pos = audio_position();
205 //  double atime = track->get_audio_time();
206 //  int64_t pts_pos = atime * track->sample_rate + 0.5;
207 //  zmsgs(" apr_pos %jd/%jd + %jd  %jd + %jd\n",
208 //    app_pos, aud_pos, app_pos-aud_pos, pts_pos, pts_pos-aud_pos);
209 //}
210   if( !result ) {
211     /* Handle changes in channel count, for ATSC */
212     if( output_channels != channels )
213       update_channels();
214     /* try to read rest of frame */
215     int len = framesize - packet_position;
216     uint8_t *bfr = packet_buffer + packet_position;
217     if( !src->seekable ) {
218       int data_length = demux->zdata.length();
219       if( data_length < len ) len = data_length;
220     }
221     if( !(result = demux->read_data(bfr, len)) )
222       packet_position += len;
223   }
224
225   if( !result && packet_position >= framesize ) {
226     float *out[output_channels];
227     for( int i=0; i<output_channels; ++i )
228       out[i] = output[i] + output_size;
229
230     switch(track->format) {
231     case afmt_AC3:
232       audio_decode.lock(); /* Liba52 is not reentrant */
233       samples = ac3_decoder->do_ac3(packet_buffer,framesize,out,render);
234       audio_decode.unlock();
235 //zmsgs("ac3 %d samples\n", samples);
236       break;
237     case afmt_MPEG:
238       switch( layer_decoder->layer ) {
239       case 2:
240         samples = layer_decoder->do_layer2(packet_buffer,framesize,out,render);
241         break;
242       case 3:
243         samples = layer_decoder->do_layer3(packet_buffer,framesize,out,render);
244         break;
245       }
246       break;
247     case afmt_PCM:
248       samples = pcm_decoder->do_pcm(packet_buffer,framesize,out,render);
249       break;
250     }
251     if( samples > 0 )
252       output_size += samples;
253     packet_position = 0;
254   }
255
256   return samples;
257 }
258
259 /* Get the length. */
260 /* Use chunksize if demuxer has a table of contents */
261 /* For elementary streams use sample count over a certain number of */
262 /*  bytes to guess total samples */
263 /* For program streams use timecode */
264 int zaudio_t::
265 get_length()
266 {
267   int result = 0;
268   int samples = 0;
269   int64_t stream_end = track->demuxer->stream_end;
270   int64_t total_bytes = track->demuxer->movie_size();
271   int64_t stream_max = total_bytes;
272   if( stream_max > 0x1000000 ) stream_max = 0x1000000;
273   track->demuxer->stream_end = stream_max;
274   rewind_audio();
275   /* Table of contents */
276   if( track->sample_offsets || !src->is_audio_stream() ) {
277     /* Estimate using multiplexed stream size in seconds */
278     /* Get stream parameters for header validation */
279     /* Need a table of contents */
280     for( int retry=0; retry<100 && samples<=0; ++retry ) {
281       samples = read_frame(0);
282     }
283     result = track->sample_offsets ? track->total_samples : 0;
284     /* : (long)(track->demuxer->length() * track->sample_rate); */
285   }
286   else { /* Estimate using average bitrate */
287     long test_bytes = 0;
288     long max_bytes = 0x40000;
289     long test_samples = 0;
290     while( test_bytes < max_bytes ) {
291       int samples = read_frame(0);
292       if( samples <= 0 ) break;
293       test_samples += samples;
294       test_bytes += framesize;
295     }
296     result = (long)(((double)total_bytes / test_bytes) * test_samples + 0.5);
297   }
298
299   track->demuxer->stream_end = stream_end;
300   output_size = 0;
301   rewind_audio();
302   return result;
303 }
304
305 int zatrack_t::
306 calculate_format(zmpeg3_t *src)
307 {
308   uint8_t header[8];
309   int result = 0;
310   /* Determine the format of the stream.  */
311   /* If it isn't in the first 8 bytes give up and go to a movie. */
312   switch( format ) {
313   case afmt_UNKNOWN:
314     /* Need these 8 bytes later on for header parsing */
315     result = demuxer->read_data(header, sizeof(header));
316 //zmsgs("%jd\n", demuxer->tell_byte());
317     if( !result )
318       format = !zaudio_decoder_ac3_t::ac3_check(header) ? afmt_AC3 : afmt_MPEG;
319     if( audio ) {
320       memcpy(audio->packet_buffer+1, header, sizeof(header));
321       audio->packet_position = 9;
322     }
323     break;
324   case afmt_IGNORE:
325     result = 1;
326     break;
327   default:
328     break;
329   }
330
331   return result;
332 }
333
334 int zaudio_t::
335 init_audio(zmpeg3_t *zsrc, zatrack_t *ztrack, int zformat)
336 {
337   int result = 0;
338   src = zsrc;
339   track = ztrack;
340   demuxer_t *demux = track->demuxer;
341   byte_seek = -1;
342   sample_seek = -1;
343   track->format = zformat;
344
345   if( zsrc->seekable )
346     result = track->calculate_format(src);
347 //zmsgs("%jd\n", demux->tell_byte());
348   /* get stream parameters */
349   if( !result && zsrc->seekable ) {
350     switch( track->format ) {
351     case afmt_AC3:
352       ac3_decoder = new audio_decoder_ac3_t();
353       break;
354     case afmt_MPEG:
355       layer_decoder = new audio_decoder_layer_t();
356       break;
357     case afmt_PCM:
358       pcm_decoder = new audio_decoder_pcm_t();
359       break;
360     default:
361       result = 1;
362     }
363     if( !result ) {
364       int64_t stream_end = demux->stream_end;
365       //if( !track->sample_offsets )
366       {
367         if( zsrc->is_transport_stream() )
368           demux->stream_end = START_BYTE + MAX_TS_PROBE;
369         else if( src->is_program_stream() )
370           demux->stream_end = START_BYTE + MAX_PGM_PROBE;
371       }
372       rewind_audio();
373       result = read_header();
374       if( !result && src->is_audio_stream() )
375         start_byte = demux->tell_byte() - zsrc->packet_size;
376       demux->stream_end = stream_end;
377     }
378 //zmsgs("1 %d %d %d start_byte=0x%jx\n", track->format, 
379 // layer_decoder->layer, result, start_byte);
380   }
381   return result;
382 }
383
384 zaudio_t *zmpeg3_t::
385 new_audio_t(zatrack_t *ztrack, int zformat)
386 {
387   audio_t *new_audio = new audio_t();
388   int result = new_audio->init_audio(this,ztrack,zformat);
389   /* Calculate Length */
390   if( !result && seekable ) {
391     new_audio->rewind_audio();
392     ztrack->total_samples = new_audio->get_length();
393   }
394   else if( seekable ) {
395     delete new_audio;
396     new_audio = 0;
397   }
398   return new_audio;
399 }
400
401
402
403 int zaudio_t::
404 seek()
405 {
406   demuxer_t *demux = track->demuxer;
407   int seeked = 0;
408 /* Stream is unseekable */
409   if( !src->seekable ) return 0;
410 /* Sample seek was requested */
411   if( sample_seek >= 0 ) {
412     sample_seek += track->nudge;
413     if( sample_seek < 0 ) sample_seek = 0;
414 /* Doesn't work with VBR streams + ID3 tags */
415 //zmsgs("%d %jd %jd %jd %jd\n", __LINE__, sample_seek,
416 //  track->current_position, output_position, audio_position());
417 /* Don't do anything if the destination is inside the sample buffer */
418     if( sample_seek < output_position ||
419         sample_seek >= audio_position() ) {
420       if( sample_seek != output_position ) {
421         /* Use table of contents */
422         if( track->sample_offsets ) {
423           int index = sample_seek / AUDIO_CHUNKSIZE;
424           if( index >= track->total_sample_offsets )
425             index = track->total_sample_offsets - 1;
426           /* incase of padding */
427           int64_t byte = track->sample_offsets[index];
428           while( index > 0 && byte == track->sample_offsets[index-1] ) --index;
429           output_position = (int64_t)index * AUDIO_CHUNKSIZE;
430           output_size = 0;
431           demux->seek_byte(byte);
432         }
433         else if( sample_seek > 0 && !src->is_audio_stream() ) { /* Use demuxer */
434           toc_error();
435 /*        double time_position = (double)sample_seek / track->sample_rate; */
436 /*        result |= mpeg3demux_seek_time(demuxer, time_position); */
437           output_position = sample_seek;
438         }
439         else { /* Use bitrate for elementary stream */
440           int64_t total_bytes = demux->movie_size() - start_byte;
441           int64_t byte = (int64_t)(total_bytes * 
442             ((double)sample_seek)/track->total_samples) + start_byte;
443 //zmsgs("%d byte=%jd\n", __LINE__, byte);
444           output_position = sample_seek;
445           output_size = 0;
446           demux->seek_byte(byte);
447         }
448         seeked = 1;
449       }
450     }
451     sample_seek = -1;
452   }
453   else if( byte_seek >= 0 ) {
454     output_position = 0;
455     output_size = 0;
456     /* Percentage seek was requested */
457     demux->seek_byte(byte_seek);
458     /* Scan for pts if we're the first to seek. */
459     /* if( src->percentage_pts < 0 ) */
460     /*   file->percentage_pts = demux->scan_pts(); */
461     /* else */
462     /*   demux->goto_pts(src->percentage_pts); */
463     seeked = 1;
464     byte_seek = -1;
465   }
466
467   if( seeked ) {
468     packet_position = 0;
469     track->reset_pts();
470     switch( track->format ) {
471     case afmt_MPEG:
472       layer_decoder->layer_reset();
473       break;
474     }
475   }
476
477   return 0;
478 }
479
480 /* ================================*/
481 /*          ENTRY POINTS           */
482 /* ================================*/
483
484 int zaudio_t::
485 seek_byte(int64_t byte)
486 {
487   byte_seek = byte;
488   return 0;
489 }
490
491 int zaudio_t::
492 seek_sample(long sample)
493 {
494 /* Doesn't work for rereading audio during percentage seeking */
495 /*  if(sample > track->total_samples) sample = track->total_samples; */
496   sample_seek = (int64_t)sample;
497   return 0;
498 }
499
500 /* Read raw frames for the mpeg3cat utility */
501 int zaudio_t::
502 read_raw(uint8_t *output, long *size, long max_size)
503 {
504   *size = 0;
505   switch( track->format ) {
506   case afmt_AC3: /* Just write the AC3 stream */
507     if( track->demuxer->read_data(output, 0x800) ) return 1;
508     *size = 0x800;
509     break;
510
511   case afmt_MPEG: /* Fix the mpeg stream */
512     if( track->demuxer->read_data(output, 0x800) ) return 1;
513     *size = 0x800;
514     break;
515     
516   case afmt_PCM: /* This is required to strip the headers */
517     if( track->demuxer->read_data(output, framesize) ) return 1;
518     *size = framesize;
519     break;
520   }
521
522   return 0;
523 #if 0
524 /* This probably doesn't work. */
525   result = read_header(audio);
526   switch( track->format ) {
527   case afmt_AC3: /* Just write the AC3 stream */
528     result = track->demuxer->read_data(output, framesize);
529     *size = framesize;
530 //zmsgs("1 %d\n", framesize);
531     break;
532   case afmt_MPEG: /* Fix the mpeg stream */
533     if( !result ) {
534       if(track->demuxer->read_data(output, framesize)) return 1;
535       *size += framesize;
536     }
537     break;
538   case afmt_PCM:
539     if(track->demuxer->read_data(output, framesize)) return 1;
540     *size = framesize;
541     break;
542   }
543   return result;
544 #endif
545 }
546
547 void zaudio_t::
548 shift_audio(int64_t diff)
549 {
550   int len = diff > 0 ? output_size - diff : 0;
551   if( len > 0 ) {
552     for( int i=0; i<output_channels; ++i )
553       memmove(output[i], output[i]+diff, len*sizeof(*output[i]));
554     output_position += diff;
555     output_size -= diff;
556   }
557   else {
558     output_position += output_size;
559     output_size = 0;
560   }
561 }
562
563 /* zero pad data to match pts */
564 int zaudio_t::
565 audio_pts_padding()
566 {
567   int result = 0, silence = 0;
568   if( track->audio_time >= 0 && src->pts_padding > 0 ) {
569     int64_t pts_pos = track->audio_time * track->sample_rate + 0.5;
570     int padding = pts_pos - track->pts_position;
571     if( padding > 0 ) {
572       if( track->askip <= 0 ) {
573         int limit = track->sample_rate/8;
574         if( padding > limit ) {
575           int64_t aud_pos = audio_position();
576           zmsgs("audio start padding  pid %02x @ %12jd (%12.6f) %d samples\n",
577                 track->pid, aud_pos, track->get_audio_time(), padding);
578           track->askip = 1;
579           result = 1;
580         }
581       }
582       else if( padding > track->sample_rate )
583         result = silence = 1;
584       else if( !(++track->askip & 1) )
585         result = 1;
586       if( result ) {
587         if( padding > AUDIO_MAX_DECODE ) padding = AUDIO_MAX_DECODE;
588         int avail = output_allocated - output_size;
589         if( padding > avail ) padding = avail;
590         if( padding > 0 ) {
591           int n = track->sample_rate/20;
592           if( output_size < n ) silence = 1;
593           if( silence ) {
594             for( int chan=0; chan<output_channels; ++chan ) {
595               float *out = output[chan] + output_size;
596               for( int i=0; i<padding; ++i ) *out++ = 0;
597             }
598           }
599           else {
600             int k = output_size - n;
601             for( int chan=0; chan<output_channels; ++chan ) {
602               float *out = output[chan] + output_size;
603               float *pad = output[chan] + k;
604               for( int i=0; i<padding; ++i ) *out++ = *pad++;
605             }
606           }
607           output_size += padding;
608           track->pts_position += padding;
609         }
610       }
611     }
612     else if( track->askip > 0 ) {
613       int64_t aud_pos = audio_position();
614       zmsgs("audio end   padding  pid %02x @ %12jd (%12.6f) %d\n",
615             track->pid, aud_pos, track->get_audio_time(), (1+track->askip)/2);
616       track->askip = 0;
617     }
618   }
619   return result;
620 }
621
622 int zaudio_t::
623 audio_pts_skipping(int samples)
624 {
625   int result = 0;
626   if( track->audio_time >= 0 && src->pts_padding > 0 ) {
627     int64_t pts_pos = track->audio_time * track->sample_rate + 0.5;
628     int skipping = track->pts_position - pts_pos;
629     if( skipping > 0 ) {
630       if( track->askip >= 0 ) {
631         int limit = track->sample_rate/8;
632         if( skipping > 3*limit ) {
633           int64_t aud_pos = audio_position();
634           zmsgs("audio start skipping pid %02x @ %12jd (%12.6f) %d samples\n",
635                 track->pid, aud_pos, track->get_audio_time(), skipping);
636           track->askip = -1;
637           result = 1;
638         }
639       }
640       else if( !(--track->askip & 1) || skipping > track->sample_rate )
641         result = 1;
642       if( result ) {
643         if( samples < skipping ) skipping = samples;
644         if( output_size < skipping ) skipping = output_size;
645         output_size -= skipping;
646         track->pts_position -= skipping;
647       }
648     }
649     else if( track->askip < 0 ) {
650       int64_t aud_pos = audio_position();
651       zmsgs("audio end   skipping pid %02x @ %12jd (%12.6f) %d\n",
652             track->pid, aud_pos, track->get_audio_time(), (1-track->askip)/2);
653       track->askip = 0;
654     }
655   }
656   return result;
657 }
658
659 void zaudio_t::
660 update_audio_history()
661 {
662   int64_t diff = track->track_position() - output_position;
663   if( diff ) shift_audio(diff);
664 }
665
666 /* Channel is 0 to channels - 1 */
667 /* Always render since now the TOC contains index files. */
668 int zaudio_t::
669 decode_audio(void *output_v, int atyp, int channel, int len)
670 {
671   int i, j, k;
672   zdemuxer_t *demux = track->demuxer;
673 //zmsgs("decode_audio(out_pos=%jd-%jd, aud_pos %jd, seek %jd  ch=%d, len=%d)\n",
674 //  output_position, audio_position(), track->track_position(),
675 //  (sample_seek<0 ? sample_seek : sample_seek+track->nudge), channel, len);
676
677   /* Get header for streaming mode */
678   if( track->format == afmt_IGNORE ) return 1;
679   if( track->format == afmt_UNKNOWN ) {
680     if( track->calculate_format(src) ) return 1;
681   }
682   if( track->format == afmt_AC3 && !ac3_decoder )
683     ac3_decoder = new audio_decoder_ac3_t();
684   else if( track->format == afmt_MPEG && !layer_decoder )
685     layer_decoder = new audio_decoder_layer_t();
686   else if( track->format == afmt_PCM && !pcm_decoder )
687     pcm_decoder = new audio_decoder_pcm_t();
688   /* Handle seeking requests */
689   seek();
690   int64_t end_pos = track->track_position() + len;
691   long new_size = end_pos - output_position + MAXFRAMESAMPLEZ;
692   /* Expand output until enough room exists for new data */
693   if( new_size > output_allocated ) {
694     for( i=0; i<output_channels; ++i ) {
695       float *old_output = output[i];
696       float *new_output = new float[new_size];
697       if( output_size ) {
698         int len = output_size * sizeof(new_output[0]);
699         memmove(new_output, old_output, len);
700         len = (new_size-output_size) * sizeof(new_output[0]);
701         memset(new_output+output_size, 0, len);
702       }
703       delete [] output[i];
704       output[i] = new_output;
705     }
706     output_allocated = new_size;
707   }
708
709
710   /* Decode frames until the output is ready */
711   int retry = 0;
712
713   while( retry < 256 ) {
714     int64_t aud_pos = audio_position();
715     int64_t demand = end_pos - aud_pos;
716     if( demand <= 0 ) break;
717     if( audio_pts_padding() ) continue;
718     if( output_allocated-output_size < AUDIO_MAX_DECODE ) break;
719 //zmsgs(" out_pos/sz %jd/%d=aud_pos %jd, end_pos %jd, demand %jd,
720 //  tell/eof %jd/%d\n", output_position, output_size, aud_pos, end_pos,
721 //  demand, demux->tell_byte(), demux->eof());
722     if( demux->eof() ) break;
723     int needed_history = len > AUDIO_HISTORY ? len : AUDIO_HISTORY;
724     /* if overflowing, shift audio back */
725     if( src->seekable && output_size > needed_history ) {
726       int diff = demand + output_size - needed_history;
727       shift_audio(diff);
728     }
729     int samples = read_frame(1);
730     if( samples < 0 ) break;
731     if( !samples ) { ++retry; continue; }
732     retry = 0;
733     if( audio_pts_skipping(samples) ) continue;
734     track->update_frame_pts();
735     track->update_audio_time();
736   }
737
738   /* Copy the buffer to the output */
739   if( !track->channels ) return 1;
740   if( channel >= track->channels )
741     channel = track->channels - 1;
742   i = 0;
743   j = track->track_position() - output_position;
744   k = output_size - j;
745   if( k > len ) k = len;
746   float *out = j >= 0 && channel < output_channels ?
747     output[channel] + j : 0;
748
749   /* transmit data in specified format */
750   switch( atyp ) {
751   case atyp_NONE:
752     break;
753   case atyp_DOUBLE: {
754     double *output_d = (double *)output_v;
755     if( out ) while( i < k )
756       output_d[i++] = *out++;
757     while( i < len ) output_d[i++] = 0.;
758     break; }
759   case atyp_FLOAT: {
760     float *output_f = (float *)output_v;
761     if( out )
762       memmove(output_f,out,(i=k)*sizeof(output_f[0]));
763     while( i < len ) output_f[i++] = 0.;
764     break; }
765   case atyp_INT: {
766     int *output_i = (int *)output_v;
767     if( out ) while( i < k )
768       output_i[i++] = clip((int)(*out++ * 0x7fffff),-0x800000,0x7fffff);
769     while( i < len ) output_i[i++] = 0;
770     break; }
771   case atyp_SHORT: {
772     short *output_s = (short *)output_v;
773     if( out ) while( i < k )
774       output_s[i++] = clip((int)(*out++ * 0x7fff),-0x8000,0x7fff);
775     while( i < len ) output_s[i++] = 0;
776     break; }
777   default:
778     return 1;
779   }
780
781 //zmsgs("output_size=%d chan=%d len=%d\n",output_size,channel,len);
782   return output_size > 0 ? 0 : 1;
783 }
784