[Libav-user] Inaccurate duration and play speed from calculating PTS while AAC encoding

funta lkm9125 at gmail.com
Thu Jan 16 11:58:48 CET 2014


I'm trying to make AAC encoder for various audio format (FLAC, MP3, AAC, AC3
on my purpose)
It could be worked with video or audio source file

I used this formula in PTS calculation
============================
(int)((double)audio_samples * ((out_acodec->time_base.den /
out_acodec->time_base.num) / samplerate));
============================

But, some file makes inaccurate duration and play speed
Duration, could be longer than original duration
Play speed, It doesn't mean physical music speed(result file has always
maintain 1X. well encoded), does mean unactual sec. time speed, also
progressive(elapsed time) value speed on video player

I've tested with several files, results are below

(INPUT FILE => OUTPUT FILE)
FLAC(S16) 04:00 => AAC(S16) 04:00 [correct]
FLAC(S16) 04:51 => AAC(S16) 04:51 [correct]
FLAC(S16) 24:35 => AAC(S16) 24:35 [correct]
*FLAC(S16) 01:38 => AAC(S16) 01:47 [incorrect]*
MP3(S16P) 03:03 => AAC(S16) 03:03 [correct]
MP3(S16P) 03:35 => AAC(S16) 03:35 [correct]
MP3(S16P) 03:36 => AAC(S16) 03:36 [correct]
H264/AAC(FLTP) 00:41 => AAC(S16) 00:41 [correct]
H264/AAC(FLTP) 02:03 => AAC(S16) 02:03 [correct]
H264/AAC(FLTP) 04:40 => AAC(S16) 04:40 [correct]
H264/AAC(FLTP) 06:46 => AAC(S16) 06:46 [correct]
*H264/AC3(FLTP) 05:00 => AAC(S16) 05:26 [incorrect]*
*H264/AC3(FLTP) 10:58 => AAC(S16) 11:56 [incorrect]*
XVID/MP3(S16P) 03:21 => AAC(S16) 03:21 [correct]
*XVID/MP3(S16P) 04:05 => AAC(S16) 04:27 [incorrect]*


I didn't receive any error message while encoding. My encoder has worked
properly
I think the PTS formula has this issue
Am I missing something?



Here is main loop
==============================
// for PTS
int audio_samples = 0;
double samplerate = out_acodec->sample_rate;

while(1) {
	...
	if(pkt.stream_index == in_astream->index) {
		...
		ret = avcodec_decode_audio4(in_acodec, in_frame, &got_frame, &pkt);
		if(got_frame) {
			...
			// I'm using FIFO, Swresample in this loop
			while(av_fifo_size(fifo) >= frame_bytes) {
				...
				av_init_packet(&newpkt);

				// calculating PTS(what is wrong?)
				out_frame->pts = (int)((double)audio_samples *
((out_acodec->time_base.den / out_acodec->time_base.num) / samplerate));

				avcodec_encode_audio2(out_acodec, &newpkt, out_frame, &got_packet);
				if(got_packet) {
					newpkt.stream_index = out_astream->index;
					newpkt.flags |= AV_PKT_FLAG_KEY;
					av_interleaved_write_frame(out, &newpkt);
					av_free_packet(&newpkt);
				}
				...
				audio_samples += out_frame->nb_samples;
			}
			...
		}
	}
	...
}
==============================



--
View this message in context: http://libav-users.943685.n4.nabble.com/Inaccurate-duration-and-play-speed-from-calculating-PTS-while-AAC-encoding-tp4659136.html
Sent from the libav-users mailing list archive at Nabble.com.


More information about the Libav-user mailing list