Hi all,

I've searched the list archives and googled a bit for my problem, but 
didn't find anything that makes sense to me.

I am trying to encode one video and one audio stream into an AVI. The video 
is H264 at 25 fps and the audio is raw 16bit PCM at 44100 Hz. The actual 
encoding goes fine and the file plays back, but there is a severe confusion 
about the time_base of the audio stream which messes up the final duration 
of the file (actually only the audio stream).

I am filling out all the time_base fields. My first question would be why 
there are two time_base fields, one in the AVStream structure and then 
another one in the AVCodecContext structure (AVStream->codec). I've looked 
a bit at the av_write_header() function, and I don't get it at all. For 
example, there is code like this:

    /* init PTS generation */
    for(i=0;i<s->nb_streams;i++) {
        int64_t den = AV_NOPTS_VALUE;
        st = s->streams[i];

        switch (st->codec->codec_type) {
        case CODEC_TYPE_AUDIO:
            den = (int64_t)st->time_base.num * st->codec->sample_rate;
            break;
        case CODEC_TYPE_VIDEO:
            den = (int64_t)st->time_base.num * st->codec->time_base.den;
            break;
        default:
            break;
        }
        if (den != AV_NOPTS_VALUE) {
            if (den <= 0)
                return AVERROR_INVALIDDATA;
            av_frac_init(&st->pts, 0, 0, den);
        }
    }

So it uses time_base.num from AVStream, and time_base.den from 
AVStream->codec for vieo streams?


In any case, what happens is that I set the audio stream timebase to 
"1/44100", both in AVStream and AVStream->codec. Additionally, I set 
sample_rate to 44100.

After calling av_write_header(), the AVStream time_base has magically 
changed to 1/2000. I have no idea where that comes from. It seems to be the 
AVI muxer doing it, since when I comment out the call to write_header() of 
the muxer in av_write_header(), it doesn't change. From looking at the AVI 
code, I don't see where it happens.

Anyways, when I prepare the audio packets, I calculate the PTS like this:

        fPacket.pts = (encodeInfo->start_time
                * fStream->time_base.den / fStream->time_base.num) / 1000000;

encodeInfo->start_time is the chunk start time relative to the stream start 
(0) in 1/1000000 seconds. fStream is the AVStream structure.

So what happens is that even though the time_base changed, I should still 
be calculating the PTS correctly. The problem is that 
AVStream->codec->time_base was not adjusted. It's still 1/44100. The 
resulting file reports a much too long duration as a result. I've 
calculated that the reported duration pts is correct for a 1/44100 
timebase, but of course the reported AVStream time_base when I open the 
file is 1/2000.

The interesting thing is that when I do not set any of the time_base values 
for audio streams, but only the sample rate, the same thing happens. 
Initially, the stream has 1/90000, the codec has 0/1 and after calling 
av_write_header, the stream is 1/2000, codec still 0/1, but the resulting 
file reports a wrong duration for the audio stream (would fit for 1/44100).

What am I doing wrong? :-) Am I even supposed to calculate the packet PTS 
which I pass to av_write_frame()? Thanks for any and all help!

Best regards,
-Stephan

_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to