Log in

View Full Version : Effect of pulldown on timestamps?


unixfs
30th November 2004, 09:24
Hi,
I wrote a dvd muxer for mencoder, that is working well with
PAL movies, but I'm having horrible sync problems with variable fps clips.

E.g. the clip always reports 29.97 fps in its sequence header, and
I can see that every couple of seconds the
- repeat_first_field, progressive_frame and top_field_first in the picture coding extension
- and progressive_sequence in the sequence etension
change.

According to the specs draft I have and to the sources in
mplayer I assume that the duration of every frame will change
based on the values of these fields, so
I wonder if I can assume that for every frame its final duration
can be calculated as
(1/fps) * (n/100) where n is given by the value of picture.display_time set by this function:

static int header_process_picture_coding_extension (mp_mpeg_header_t * picture, unsigned char * buffer)
{
picture->picture_structure = buffer[2] & 3;
picture->top_field_first = buffer[3] >> 7;
picture->repeat_first_field = (buffer[3] >> 1) & 1;
picture->progressive_frame = buffer[4] >> 7;

picture->display_time=100;
if(picture->repeat_first_field){
if(picture->progressive_sequence){
if(picture->top_field_first)
picture->display_time+=200;
else
picture->display_time+=100;
} else
if(picture->progressive_frame){
picture->display_time+=50;
}
}
//temopral hack. We calc time on every field, so if we have 2 fields
// interlaced we'll end with double time for 1 frame
if( picture->picture_structure!=3 ) picture->display_time/=2;
return 0;
}

Thanks.

Sulik
7th December 2004, 08:46
It's actually simpler than this.
First of all, repeat_first_field is only allowed for progressive frames, and it does not affect the presentation time of the frame.

If repeat_first_field is 1, then the frame duration is 1.5*(1/fps)

This is really only used to carry 24Hz FILM content to 29.97Hz NTSC, so you will see a cadence of 1.0, 1.5, 1.0, 1.5 etc.
In a more advanced decoder on a progressive display, you could replace each pair of {1.0, 1.5} by {1.25,1.25), in order to restore the original movie frame rate.

unixfs
7th December 2004, 16:25
I can see the 1.5 1.0 ... sequence in my test clip.
Does it mean that when I meet a 24 fps frame with a 1.5
I should set its duration as 1.5 * (1/24) * 90000 ?

Thanks for your answer!

mpucoder
7th December 2004, 17:30
NO! The duration of a frame is always according to the display framerate, not the source.
Since rff is primarily used with NTSC (it can be used for others) the duration of a normal frame is 3003, one with rff is 1.5 x 3003 = 4504.5
Although pts/dts has no fractional part, you must keep track of it, ie a sequence of 1.0 1.5 1.0 1.5 lasts 5 frames. 5 x 3003 = 15015

I found the easiest way to handle this is to count fields not frames or clocks. Then you can convert at any time to clocks with:
edit : (total_fields >> 1) * 3003
(see below for why the edit)

Nic
8th December 2004, 13:16
@mpucoder
I understood you till this point:
(total_fields << 1) * 3003

Why the "<< 1" (or *2) ? I would have assumed it would be a ">> 1" (or /2)

Cheers and thanks for the explanation,
-Nic

unixfs
8th December 2004, 13:29
I agree, >> 1 seems to make more sense.

MPUCoder,
I applied you explanation, but even so my sample is totally
out of sync. All the other muxers I tried give the same result
(yours refuses it because it has 1920x1080 resolution),
so there must be something particular or wrong with it.

Thank you for you help!

mpucoder
8th December 2004, 14:40
Nic, you're right. It should be (total_fields >> 1) * 3003 and only for NTSC.
For PAL use 3600.

And even though pulldown is an "NTSC thing" it can be used for PAL. I've seen rff being used as another form of temporal compression. And why not? If three frames are identical, just encode two with rff set on both.

mpucoder
8th December 2004, 14:53
I just re-read the first post. Progressive sequence is another animal altogether. So far everything has been about rff (and tff) for a non-progressive (ie interlaced) sequence.
This is not to be confused with progressive frames. Progressive frame has to do with how the picture is encoded, progressive sequence deals with the display.

In a progressive sequence rff and tff take on a different meaning. Instead of repeating fields, whole frames are repeated.

tff rff frames
0 0 1
0 1 2
1 1 3

Note that tff should not be set if rff is not.
So if rff is set one or two frames will be repeated, depending on tff.

Sulik
9th December 2004, 08:12
top_field_first and repeat_first_field have no meaning if progressive_sequence=1. This violates the MPEG-2 specification.

FILM sequences have progressive_sequence=0, but progressive_frame=1 for every frame (repeat_first_field can only be 1 if progressive_frame=1). top_field_first will change every time repeat_first_field is used (in order to maintain the cadence of top-bottom-top-bottom.. fields for interlaced displays)

mpucoder
9th December 2004, 14:22
Originally posted by Sulik
top_field_first and repeat_first_field have no meaning if progressive_sequence=1. This violates the MPEG-2 specification.
You should read the spec before you make a statement like that.
This is straight from iso 13818-2


top_field_first -- The meaning of this element depends upon picture_structure, progressive_sequence and
repeat_first_field.
If progressive_sequence is equal to ‘0’, this flag indicates what field of a reconstructed frame is output
first by the decoding process:
In a field picture top_field_first shall have the value ‘0’, and the only field output by the decoding process
is the decoded field picture.
In a frame picture top_field_first being set to ‘1’ indicates that the top field of the reconstructed frame is
the first field output by the decoding process. top_field_first being set to ‘0’ indicates that the bottom field
of the reconstructed frame is the first field output by decoding process

If progressive_sequence is equal to ‘1’, this flag, combined with repeat_first_field, indicates how many
times (one, two or three) the reconstructed frame is output by the decoding process.
If repeat_first_field is set to 0, top_field_first shall be set to ‘0’. In this case the output of the decoding
process corresponding to this reconstructed frame consists of one progressive frame.
If top_field_first is set to 0 and repeat_first_field is set to ‘1’, the output of the decoding process
corresponding to this reconstructed frame consists of two identical progressive frames.
If top_field_first is set to 1 and repeat_first_field is set to ‘1’, the output of the decoding process
corresponding to this reconstructed frame consists of three identical progressive frames.