1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
10 static pthread_mutex_t
*decode_lock
= 0;
13 static void toc_error()
16 "mpeg3audio: sample accurate seeking without a table of contents \n"
17 "is no longer supported. Use mpeg3toc <mpeg file> <table of contents>\n"
18 "to generate a table of contents and load the table of contents instead.\n");
22 static int rewind_audio(mpeg3audio_t
*audio
)
24 mpeg3_atrack_t
*track
= audio
->track
;
25 if(track
->sample_offsets
)
26 mpeg3demux_seek_byte(track
->demuxer
, track
->sample_offsets
[0]);
28 mpeg3demux_seek_byte(track
->demuxer
, 0);
32 /* Advance to next header and read it. */
33 static int read_header(mpeg3audio_t
*audio
)
39 mpeg3_atrack_t
*track
= audio
->track
;
45 audio
->packet_position
= 8;
46 result
= mpeg3demux_read_data(track
->demuxer
,
47 audio
->packet_buffer
+ 1,
54 for(i
= 0; i
< 7; i
++)
55 audio
->packet_buffer
[i
] = audio
->packet_buffer
[i
+ 1];
59 audio
->packet_buffer
[7] =
60 mpeg3demux_read_char(track
->demuxer
);
61 result
= mpeg3demux_eof(track
->demuxer
);
63 //printf("read_header 10 %d\n", mpeg3demux_eof(track->demuxer));
67 got_it
= (mpeg3_ac3_header(audio
->ac3_decoder
,
68 audio
->packet_buffer
) != 0);
74 * printf("read_header 100 offset=%llx got_it=%d\n",
75 * mpeg3demux_tell_byte(track->demuxer),
78 }while(!result
&& !got_it
&& try < 0x10000);
83 if(audio
->ac3_decoder
->channels
> track
->channels
)
84 track
->channels
= audio
->ac3_decoder
->channels
;
85 track
->sample_rate
= audio
->ac3_decoder
->samplerate
;
86 audio
->framesize
= audio
->ac3_decoder
->framesize
;
87 if(track
->sample_rate
<= 0) track
->sample_rate
= 48000;
88 //printf("read_header %d %d %d\n", track->channels, track->sample_rate, audio->framesize);
90 if(track
->sample_rate
<= 0) track
->sample_rate
= 48000;
96 audio
->packet_position
= 4;
97 /* Layer 1 not supported */
98 if(audio
->layer_decoder
->layer
== 1)
103 result
= mpeg3demux_read_data(track
->demuxer
,
104 audio
->packet_buffer
+ 1,
111 for(i
= 0; i
< 3; i
++)
112 audio
->packet_buffer
[i
] = audio
->packet_buffer
[i
+ 1];
116 audio
->packet_buffer
[3] =
117 mpeg3demux_read_char(track
->demuxer
);
118 result
= mpeg3demux_eof(track
->demuxer
);
123 got_it
= (mpeg3_layer_header(audio
->layer_decoder
,
124 audio
->packet_buffer
) != 0);
126 * printf(__FUNCTION__ " got_it=%d packet=%02x%02x%02x%02x\n",
128 * (unsigned char)audio->packet_buffer[0],
129 * (unsigned char)audio->packet_buffer[1],
130 * (unsigned char)audio->packet_buffer[2],
131 * (unsigned char)audio->packet_buffer[3]);
134 }while(!result
&& !got_it
&& try < 0x10000);
138 if(audio
->layer_decoder
->channels
> track
->channels
)
139 track
->channels
= audio
->layer_decoder
->channels
;
140 track
->sample_rate
= audio
->layer_decoder
->samplerate
;
141 audio
->framesize
= audio
->layer_decoder
->framesize
;
147 audio
->packet_position
= PCM_HEADERSIZE
;
148 result
= mpeg3demux_read_data(track
->demuxer
,
149 audio
->packet_buffer
+ 1,
156 for(i
= 0; i
< PCM_HEADERSIZE
- 1; i
++)
157 audio
->packet_buffer
[i
] = audio
->packet_buffer
[i
+ 1];
161 audio
->packet_buffer
[PCM_HEADERSIZE
- 1] =
162 mpeg3demux_read_char(track
->demuxer
);
163 result
= mpeg3demux_eof(track
->demuxer
);
168 got_it
= (mpeg3_pcm_header(audio
->pcm_decoder
,
169 audio
->packet_buffer
) != 0);
171 }while(!result
&& !got_it
&& try < 0x10000);
175 if(audio
->pcm_decoder
->channels
> track
->channels
)
176 track
->channels
= audio
->pcm_decoder
->channels
;
177 track
->sample_rate
= audio
->pcm_decoder
->samplerate
;
178 audio
->framesize
= audio
->pcm_decoder
->framesize
;
186 static int delete_struct(mpeg3audio_t
*audio
)
189 mpeg3_atrack_t
*track
= audio
->track
;
193 for(i
= 0; i
< track
->channels
; i
++)
194 free(audio
->output
[i
]);
197 if(audio
->ac3_decoder
) mpeg3_delete_ac3(audio
->ac3_decoder
);
198 if(audio
->layer_decoder
) mpeg3_delete_layer(audio
->layer_decoder
);
199 if(audio
->pcm_decoder
) mpeg3_delete_pcm(audio
->pcm_decoder
);
208 static int read_frame(mpeg3audio_t
*audio
, int render
)
211 mpeg3_atrack_t
*track
= audio
->track
;
212 mpeg3_t
*file
= audio
->file
;
213 float **temp_output
= 0;
216 int old_channels
= track
->channels
;
218 // Liba52 is not reentrant
219 if(track
->format
== AUDIO_AC3
)
221 pthread_mutex_lock(decode_lock
);
224 /* Find and read next header */
225 result
= read_header(audio
);
228 /* Read rest of frame */
231 result
= mpeg3demux_read_data(track
->demuxer
,
232 audio
->packet_buffer
+ audio
->packet_position
,
233 audio
->framesize
- audio
->packet_position
);
236 /* Handle increase in channel count, for ATSC */
237 if(old_channels
< track
->channels
)
239 float **new_output
= calloc(sizeof(float*), track
->channels
);
240 for(i
= 0; i
< track
->channels
; i
++)
242 new_output
[i
] = calloc(sizeof(float), audio
->output_allocated
);
244 memcpy(new_output
[i
],
246 sizeof(float) * audio
->output_size
);
248 for(i
= 0; i
< old_channels
; i
++)
249 free(audio
->output
[i
]);
251 audio
->output
= new_output
;
258 temp_output
= malloc(sizeof(float*) * track
->channels
);
259 for(i
= 0; i
< track
->channels
; i
++)
261 temp_output
[i
] = audio
->output
[i
] + audio
->output_size
;
268 switch(track
->format
)
271 samples
= mpeg3audio_doac3(audio
->ac3_decoder
,
272 audio
->packet_buffer
,
276 //printf("read_frame %d\n", samples);
280 switch(audio
->layer_decoder
->layer
)
283 samples
= mpeg3audio_dolayer2(audio
->layer_decoder
,
284 audio
->packet_buffer
,
292 samples
= mpeg3audio_dolayer3(audio
->layer_decoder
,
293 audio
->packet_buffer
,
306 samples
= mpeg3audio_dopcm(audio
->pcm_decoder
,
307 audio
->packet_buffer
,
316 audio
->output_size
+= samples
;
322 // Liba52 is not reentrant
323 if(track
->format
== AUDIO_AC3
)
325 pthread_mutex_unlock(decode_lock
);
329 // Shift demuxer data
331 mpeg3demux_shift_data(track
->demuxer
, track
->demuxer
->data_position
);
341 /* Get the length. */
342 /* Use chunksize if demuxer has a table of contents */
343 /* For elementary streams use sample count over a certain number of
344 bytes to guess total samples */
345 /* For program streams use timecode */
346 static int get_length(mpeg3audio_t
*audio
)
349 mpeg3_t
*file
= audio
->file
;
350 mpeg3_atrack_t
*track
= audio
->track
;
354 if(track
->sample_offsets
)
358 /* Get stream parameters for header validation */
361 samples
= read_frame(audio
, 0);
364 result
= track
->total_samples
;
367 // Estimate using multiplexed stream size in seconds
368 if(!file
->is_audio_stream
)
370 /* Get stream parameters for header validation */
371 /* Need a table of contents */
374 samples
= read_frame(audio
, 0);
377 // result = (long)(mpeg3demux_length(track->demuxer) *
378 // track->sample_rate);
382 // Estimate using average bitrate
385 long max_bytes
= 0x40000;
386 long test_samples
= 0;
388 int64_t total_bytes
= mpeg3demux_movie_size(track
->demuxer
);
390 while(!error
&& test_bytes
< max_bytes
)
392 int samples
= read_frame(audio
, 0);
393 if(!samples
) error
= 1;
394 test_samples
+= samples
;
395 test_bytes
+= audio
->framesize
;
397 result
= (long)(((double)total_bytes
/ test_bytes
) * test_samples
+ 0.5);
400 audio
->output_size
= 0;
410 int calculate_format(mpeg3_t
*file
, mpeg3_atrack_t
*track
)
414 /* Determine the format of the stream. */
415 /* If it isn't in the first 8 bytes give up and go to a movie. */
416 if(track
->format
== AUDIO_UNKNOWN
)
418 unsigned char header
[8];
419 if(!mpeg3demux_read_data(track
->demuxer
,
423 //printf("calculate_format %lld\n", mpeg3demux_tell_byte(track->demuxer));
424 if(!mpeg3_ac3_check(header
))
425 track
->format
= AUDIO_AC3
;
427 track
->format
= AUDIO_MPEG
;
440 mpeg3audio_t
* mpeg3audio_new(mpeg3_t
*file
,
441 mpeg3_atrack_t
*track
,
444 mpeg3audio_t
*audio
= calloc(1, sizeof(mpeg3audio_t
));
450 pthread_mutexattr_t attr
;
451 pthread_mutexattr_init(&attr
);
452 decode_lock
= calloc(1, sizeof(pthread_mutex_t
));
453 pthread_mutex_init(decode_lock
, &attr
);
457 audio
->track
= track
;
459 audio
->byte_seek
= -1;
460 audio
->sample_seek
= -1;
461 track
->format
= format
;
464 if(calculate_format(file
, track
)) result
= 1;
466 //printf("mpeg3audio_new %lld\n", mpeg3demux_tell_byte(track->demuxer));
467 /* get stream parameters */
468 if(!result
&& file
->seekable
)
470 switch(track
->format
)
473 audio
->ac3_decoder
= mpeg3_new_ac3();
476 audio
->layer_decoder
= mpeg3_new_layer();
479 audio
->pcm_decoder
= mpeg3_new_pcm();
490 result
= read_header(audio
);
491 //printf("mpeg3audio_new 1 %d\n", result);
495 /* Set up the output buffer */
496 if(!result
&& file
->seekable
)
498 audio
->output
= calloc(sizeof(float*), track
->channels
);
499 audio
->output_allocated
= 4;
500 for(i
= 0; i
< track
->channels
; i
++)
502 audio
->output
[i
] = calloc(sizeof(float), audio
->output_allocated
);
507 /* Calculate Length */
508 if(!result
&& file
->seekable
)
511 track
->total_samples
= get_length(audio
);
516 delete_struct(audio
);
537 static int seek(mpeg3audio_t
*audio
)
540 mpeg3_t
*file
= audio
->file
;
541 mpeg3_atrack_t
*track
= audio
->track
;
542 mpeg3_demuxer_t
*demuxer
= track
->demuxer
;
545 /* Stream is unseekable */
546 if(!file
->seekable
) return 0;
548 /* Sample seek was requested */
549 if(audio
->sample_seek
>= 0)
553 * printf(__FUNCTION__ " 1 %d %d %d %d\n",
554 * audio->sample_seek,
555 * track->current_position,
556 * audio->output_position,
557 * audio->output_position + audio->output_size);
560 /* Don't do anything if the destination is inside the sample buffer */
561 if(audio
->sample_seek
>= audio
->output_position
&&
562 audio
->sample_seek
<= audio
->output_position
+ audio
->output_size
)
567 /* Use table of contents */
568 if(track
->sample_offsets
)
573 index
= audio
->sample_seek
/ MPEG3_AUDIO_CHUNKSIZE
;
574 if(index
>= track
->total_sample_offsets
) index
= track
->total_sample_offsets
- 1;
575 byte
= track
->sample_offsets
[index
];
577 mpeg3demux_seek_byte(demuxer
, byte
);
579 audio
->output_position
= index
* MPEG3_AUDIO_CHUNKSIZE
;
580 audio
->output_size
= 0;
584 if(!file
->is_audio_stream
)
589 * double time_position = (double)audio->sample_seek / track->sample_rate;
590 * result |= mpeg3demux_seek_time(demuxer, time_position);
592 audio
->output_position
= audio
->sample_seek
;
593 audio
->output_size
= 0;
599 int64_t byte
= (int64_t)((double)audio
->sample_seek
/
600 track
->total_samples
*
601 mpeg3demux_movie_size(demuxer
));
602 //printf(__FUNCTION__ " 5\n");
604 mpeg3demux_seek_byte(demuxer
, byte
);
605 audio
->output_position
= audio
->sample_seek
;
606 audio
->output_size
= 0;
611 /* Percentage seek was requested */
612 if(audio
->byte_seek
>= 0)
614 mpeg3demux_seek_byte(demuxer
, audio
->byte_seek
);
616 // Scan for pts if we're the first to seek.
618 * if(file->percentage_pts < 0)
620 * file->percentage_pts = mpeg3demux_scan_pts(demuxer);
624 * mpeg3demux_goto_pts(demuxer, file->percentage_pts);
628 audio
->output_position
= 0;
629 audio
->output_size
= 0;
635 mpeg3demux_reset_pts(demuxer
);
636 switch(track
->format
)
639 mpeg3_layer_reset(audio
->layer_decoder
);
643 audio
->sample_seek
= -1;
644 audio
->byte_seek
= -1;
658 /* ================================================================ */
660 /* ================================================================ */
664 int mpeg3audio_delete(mpeg3audio_t
*audio
)
666 delete_struct(audio
);
670 int mpeg3audio_seek_byte(mpeg3audio_t
*audio
, int64_t byte
)
672 audio
->byte_seek
= byte
;
676 int mpeg3audio_seek_sample(mpeg3audio_t
*audio
, long sample
)
678 mpeg3_atrack_t
*track
= audio
->track
;
679 // Doesn't work for rereading audio during percentage seeking
680 // if(sample > track->total_samples) sample = track->total_samples;
681 if(sample
< 0) sample
= 0;
682 audio
->sample_seek
= sample
;
686 /* Read raw frames for the mpeg3cat utility */
687 int mpeg3audio_read_raw(mpeg3audio_t
*audio
,
688 unsigned char *output
,
694 mpeg3_atrack_t
*track
= audio
->track
;
698 switch(track
->format
)
701 /* Just write the AC3 stream */
702 result
= mpeg3demux_read_data(track
->demuxer
,
709 /* Fix the mpeg stream */
712 if(mpeg3demux_read_data(track
->demuxer
,
722 // This is required to strip the headers
723 if(mpeg3demux_read_data(track
->demuxer
,
727 *size
= audio
->framesize
;
736 // This probably doesn't work.
737 result
= read_header(audio
);
738 switch(track
->format
)
741 /* Just write the AC3 stream */
742 result
= mpeg3demux_read_data(track
->demuxer
,
745 *size
= audio
->framesize
;
746 //printf("mpeg3audio_read_raw 1 %d\n", audio->framesize);
750 /* Fix the mpeg stream */
753 if(mpeg3demux_read_data(track
->demuxer
,
758 *size
+= audio
->framesize
;
763 if(mpeg3demux_read_data(track
->demuxer
,
767 *size
= audio
->framesize
;
775 void mpeg3_shift_audio(mpeg3audio_t
*audio
, int diff
)
778 mpeg3_atrack_t
*track
= audio
->track
;
780 for(k
= 0; k
< track
->channels
; k
++)
782 for(i
= 0, j
= diff
; j
< audio
->output_size
; i
++, j
++)
784 audio
->output
[k
][i
] = audio
->output
[k
][j
];
787 audio
->output_size
-= diff
;
788 audio
->output_position
+= diff
;
793 /* Channel is 0 to channels - 1 */
794 int mpeg3audio_decode_audio(mpeg3audio_t
*audio
,
800 mpeg3_t
*file
= audio
->file
;
801 mpeg3_atrack_t
*track
= audio
->track
;
804 /* Always render since now the TOC contains index files. */
809 /* Minimum amount of data must be present for streaming mode */
810 if(!file
->seekable
&&
811 track
->demuxer
->data_size
< MPEG3_AUDIO_STREAM_SIZE
) return 1;
814 /* Get header for streaming mode */
815 if(track
->format
== AUDIO_UNKNOWN
)
816 if(calculate_format(file
, track
)) return 1;
818 if(track
->format
== AUDIO_AC3
&& !audio
->ac3_decoder
)
819 audio
->ac3_decoder
= mpeg3_new_ac3();
821 if(track
->format
== AUDIO_MPEG
&& !audio
->layer_decoder
)
822 audio
->layer_decoder
= mpeg3_new_layer();
824 if(track
->format
== AUDIO_PCM
&& !audio
->pcm_decoder
)
825 audio
->pcm_decoder
= mpeg3_new_pcm();
829 /* Handle seeking requests */
832 new_size
= track
->current_position
+
835 audio
->output_position
;
837 /* Expand output until enough room exists for new data */
838 if(new_size
> audio
->output_allocated
)
841 for(i
= 0; i
< track
->channels
; i
++)
844 new_output
= calloc(sizeof(float), new_size
);
845 memcpy(new_output
, audio
->output
[i
], sizeof(float) * audio
->output_size
);
846 free(audio
->output
[i
]);
847 audio
->output
[i
] = new_output
;
849 audio
->output_allocated
= new_size
;
853 /* Decode frames until the output is ready */
856 if(audio
->output_position
+ audio
->output_size
>=
857 track
->current_position
+ len
||
859 mpeg3demux_eof(track
->demuxer
)) break;
863 if(!file
->seekable
&&
864 track
->demuxer
->data_size
<
865 MPEG3_AUDIO_STREAM_SIZE
) break;
867 int samples
= read_frame(audio
, render
);
878 /* Copy the buffer to the output */
879 if(channel
>= track
->channels
) channel
= track
->channels
- 1;
883 for(i
= 0, j
= track
->current_position
- audio
->output_position
;
884 i
< len
&& j
< audio
->output_size
;
887 output_f
[i
] = audio
->output
[channel
][j
];
898 for(i
= 0, j
= track
->current_position
- audio
->output_position
;
899 i
< len
&& j
< audio
->output_size
;
902 sample
= (int)(audio
->output
[channel
][j
] * 32767);
903 if(sample
> 32767) sample
= 32767;
905 if(sample
< -32768) sample
= -32768;
907 output_i
[i
] = sample
;
917 /* Shift audio back */
918 if(audio
->output_size
> MPEG3_AUDIO_HISTORY
)
920 int diff
= audio
->output_size
- MPEG3_AUDIO_HISTORY
;
921 mpeg3_shift_audio(audio
, diff
);
926 if(audio
->output_size
> 0)