1 #include "../libzmpeg3.h"
3 static class audio_decode_t {
7 pthread_mutexattr_t attr;
8 pthread_mutexattr_init(&attr);
9 pthread_mutex_init(&zlock, &attr);
12 pthread_mutex_destroy(&zlock);
14 void lock() { pthread_mutex_lock(&zlock); }
15 void unlock() { pthread_mutex_unlock(&zlock); }
18 static void toc_error()
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");
29 if( track->sample_offsets )
30 track->demuxer->seek_byte(track->sample_offsets[0]);
32 track->demuxer->seek_byte(0);
38 /* Advance to next header and read it. */
47 switch(track->format) {
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);
54 if( packet_position-pos < 8 ) {
55 packet_buffer[packet_position++] = track->demuxer->read_char();
58 got_it = ac3_decoder->ac3_header(&packet_buffer[pos]);
64 channels = ac3_decoder->channels;
65 samplerate = ac3_decoder->samplerate;
66 framesize = ac3_decoder->framesize;
72 /* Layer 1 not supported */
73 if( layer_decoder->layer == 1 ) break;
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;
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;
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);
94 if( packet_position-pos < 4 ) {
95 packet_buffer[packet_position++] = track->demuxer->read_char();
98 got_it = layer_decoder->layer3_header(&packet_buffer[pos]);
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]);
104 /* ID3 tags need to reset the count to skip the tags */
105 if( layer_decoder->id3_state != id3_IDLE ) count = 0x10000;
109 channels = layer_decoder->channels;
110 samplerate = layer_decoder->samplerate;
111 framesize = layer_decoder->framesize;
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);
122 if( packet_position-pos < PCM_HEADERSIZE ) {
123 packet_buffer[packet_position++] = track->demuxer->read_char();
126 got_it = pcm_decoder->pcm_header(&packet_buffer[pos]);
132 channels = pcm_decoder->channels;
133 samplerate = pcm_decoder->samplerate;
134 framesize = pcm_decoder->framesize;
145 memmove(packet_buffer, &packet_buffer[pos], packet_position-=pos);
147 if( track->demuxer->error() )
150 if( channels > track->channels )
151 track->channels = channels;
152 track->sample_rate = samplerate;
154 //zmsgs("%d %d %d\n", track->channels, track->sample_rate, framesize);
163 for( int i=0; i<output_channels; ++i )
167 if( ac3_decoder ) delete ac3_decoder;
168 if( layer_decoder ) delete layer_decoder;
169 if( pcm_decoder ) delete pcm_decoder;
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];
184 for( i=0; i<channels; ++i )
185 new_output[i] = output[i];
186 while( i < output_channels ) // fewer channels
187 delete [] output[i++];
191 output_channels = channels;
195 read_frame(int render)
197 zdemuxer_t *demux = track->demuxer;
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);
211 /* Handle changes in channel count, for ATSC */
212 if( output_channels != 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;
221 if( !(result = demux->read_data(bfr, len)) )
222 packet_position += len;
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;
230 switch(track->format) {
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);
238 switch( layer_decoder->layer ) {
240 samples = layer_decoder->do_layer2(packet_buffer,framesize,out,render);
243 samples = layer_decoder->do_layer3(packet_buffer,framesize,out,render);
248 samples = pcm_decoder->do_pcm(packet_buffer,framesize,out,render);
252 output_size += samples;
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 */
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;
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);
283 result = track->sample_offsets ? track->total_samples : 0;
284 /* : (long)(track->demuxer->length() * track->sample_rate); */
286 else { /* Estimate using average bitrate */
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;
296 result = (long)(((double)total_bytes / test_bytes) * test_samples + 0.5);
299 track->demuxer->stream_end = stream_end;
306 calculate_format(zmpeg3_t *src)
310 /* Determine the format of the stream. */
311 /* If it isn't in the first 8 bytes give up and go to a movie. */
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());
318 format = !zaudio_decoder_ac3_t::ac3_check(header) ? afmt_AC3 : afmt_MPEG;
320 memcpy(audio->packet_buffer+1, header, sizeof(header));
321 audio->packet_position = 9;
335 init_audio(zmpeg3_t *zsrc, zatrack_t *ztrack, int zformat)
340 demuxer_t *demux = track->demuxer;
343 track->format = zformat;
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 ) {
352 ac3_decoder = new audio_decoder_ac3_t();
355 layer_decoder = new audio_decoder_layer_t();
358 pcm_decoder = new audio_decoder_pcm_t();
364 int64_t stream_end = demux->stream_end;
365 //if( !track->sample_offsets )
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;
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;
378 //zmsgs("1 %d %d %d start_byte=0x%jx\n", track->format,
379 // layer_decoder->layer, result, start_byte);
385 new_audio_t(zatrack_t *ztrack, int zformat)
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();
394 else if( seekable ) {
406 demuxer_t *demux = track->demuxer;
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;
431 demux->seek_byte(byte);
433 else if( sample_seek > 0 && !src->is_audio_stream() ) { /* Use demuxer */
435 /* double time_position = (double)sample_seek / track->sample_rate; */
436 /* result |= mpeg3demux_seek_time(demuxer, time_position); */
437 output_position = sample_seek;
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;
446 demux->seek_byte(byte);
453 else if( byte_seek >= 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(); */
462 /* demux->goto_pts(src->percentage_pts); */
470 switch( track->format ) {
472 layer_decoder->layer_reset();
480 /* ================================*/
482 /* ================================*/
485 seek_byte(int64_t byte)
492 seek_sample(long sample)
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;
500 /* Read raw frames for the mpeg3cat utility */
502 read_raw(uint8_t *output, long *size, long max_size)
505 switch( track->format ) {
506 case afmt_AC3: /* Just write the AC3 stream */
507 if( track->demuxer->read_data(output, 0x800) ) return 1;
511 case afmt_MPEG: /* Fix the mpeg stream */
512 if( track->demuxer->read_data(output, 0x800) ) return 1;
516 case afmt_PCM: /* This is required to strip the headers */
517 if( track->demuxer->read_data(output, framesize) ) return 1;
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);
530 //zmsgs("1 %d\n", framesize);
532 case afmt_MPEG: /* Fix the mpeg stream */
534 if(track->demuxer->read_data(output, framesize)) return 1;
539 if(track->demuxer->read_data(output, framesize)) return 1;
548 shift_audio(int64_t diff)
550 int len = diff > 0 ? output_size - diff : 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;
558 output_position += output_size;
563 /* zero pad data to match pts */
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;
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);
582 else if( padding > track->sample_rate )
583 result = silence = 1;
584 else if( !(++track->askip & 1) )
587 if( padding > AUDIO_MAX_DECODE ) padding = AUDIO_MAX_DECODE;
588 int avail = output_allocated - output_size;
589 if( padding > avail ) padding = avail;
591 int n = track->sample_rate/20;
592 if( output_size < n ) silence = 1;
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;
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++;
607 output_size += padding;
608 track->pts_position += padding;
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);
623 audio_pts_skipping(int samples)
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;
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);
640 else if( !(--track->askip & 1) || skipping > track->sample_rate )
643 if( samples < skipping ) skipping = samples;
644 if( output_size < skipping ) skipping = output_size;
645 output_size -= skipping;
646 track->pts_position -= skipping;
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);
660 update_audio_history()
662 int64_t diff = track->track_position() - output_position;
663 if( diff ) shift_audio(diff);
666 /* Channel is 0 to channels - 1 */
667 /* Always render since now the TOC contains index files. */
669 decode_audio(void *output_v, int atyp, int channel, int len)
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);
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;
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 */
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];
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);
704 output[i] = new_output;
706 output_allocated = new_size;
710 /* Decode frames until the output is ready */
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;
729 int samples = read_frame(1);
730 if( samples < 0 ) break;
731 if( !samples ) { ++retry; continue; }
733 if( audio_pts_skipping(samples) ) continue;
734 track->update_frame_pts();
735 track->update_audio_time();
738 /* Copy the buffer to the output */
739 if( !track->channels ) return 1;
740 if( channel >= track->channels )
741 channel = track->channels - 1;
743 j = track->track_position() - output_position;
745 if( k > len ) k = len;
746 float *out = j >= 0 && channel < output_channels ?
747 output[channel] + j : 0;
749 /* transmit data in specified format */
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.;
760 float *output_f = (float *)output_v;
762 memmove(output_f,out,(i=k)*sizeof(output_f[0]));
763 while( i < len ) output_f[i++] = 0.;
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;
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;
781 //zmsgs("output_size=%d chan=%d len=%d\n",output_size,channel,len);
782 return output_size > 0 ? 0 : 1;