annotate DOCS/tech/general.txt @ 132:642d64c1cc33

translation by Gabucino
author gabucino
date Sun, 18 Mar 2001 13:57:06 +0000
parents
children 1cb4ee3b4890
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
132
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
1
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
2 So, I'll describe how this stuff works.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
3
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
4 The basis of the program's structure is basically logical, however it's
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
5 a big hack :)
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
6
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
7 The main modules:
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
8
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
9 1. streamer.c: this is the input, this reads the file or the VCD.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
10 what it has to know: appropriate buffering, seek, skip functions,
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
11 reading by bytes, or blocks with any size.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
12 The stream_t structure describes the input stream, file/device.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
13
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
14 2. demuxer.c: this makes the demultiplexing of the input to audio and video
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
15 channels, and their reading by buffered packages.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
16 The demuxer.c is basically a framework, which is the same for all the
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
17 input formats, and there are parsers for each of them (mpeg-es,
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
18 mpeg-ps, avi, avi-ni, asf), these are in the demux_*.c files.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
19 The structure is the demuxer_t. There is only one demuxer.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
20
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
21 2.a. demuxer stream, that is DS. Its struct is demux_stream_t
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
22 Every channel (a/v) has one.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
23 For now, there can be 2 for each demuxer, one for the audio and one
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
24 for the video.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
25
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
26 2.b. demux_packet_t, that is DP.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
27 This contains one chunk (avi) or packet (asf,mpg).
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
28 In the memory they are stored as chained lists, since they are of
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
29 different sizes.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
30
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
31 Now, how this reading works?
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
32 - demuxer.c/demux_read_data() is called, it gets how many bytes,
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
33 and where (memory address), would we like to read, and from which
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
34 DS. The codecs call this.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
35 - this checks if the given DS's buffer contains something, if so, it
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
36 reads from there as much as needed. If there isn't enough, it calls
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
37 ds_fill_buffer(), which:
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
38 - checks if the given DS has buffered packages (DP's), if so, it moves
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
39 the oldest to the buffer, and reads on. If the list is empty, it
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
40 calls demux_fill_buffer() :
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
41 - this calls the parser for the input format, which reads the file
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
42 onward, and moves the found packages to their buffers.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
43 Well it we'd like an audio package, but only a bunch of video
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
44 packages are available, then sooner or later the:
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
45 DEMUXER: Too many (%d in %d bytes) audio packets in the buffer
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
46 error shows up.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
47
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
48 So everything is ok 'till now, I want to move them to a separate lib.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
49
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
50 Now, go on:
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
51
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
52 3. mplayer.c - ooh, he's the boss :)
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
53 The timing is solved odd, since it has/recommended to be done differently
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
54 for each of the formats, and sometimes can be done by many ways.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
55 There are the a_frame and v_frame float variables, they store the
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
56 just played a/v position is seconds.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
57 A new frame is displayed if v_frame<a_frame, and sound is decoded if
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
58 a_frame<v_frame.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
59 When playing (a/v), it increases the variables by the duration of the
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
60 played a/v. In video, it's usually 1.0/fps, but I have to mention that
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
61 doesn't really matters at video, for example asf doesn't have that,
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
62 instead there is "duration" and it can change per frames.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
63 MPEG2 has "repeat_count" which delays the frame by 1-2.5 ...
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
64 Maybe only AVI and MPEG1 has fixed fps.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
65
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
66 So everything works perfect until the audio and video are in perfect
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
67 synchronity, since the audio goes, it gives the timing, and if the
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
68 time of a frame passed, the next frame is displayed.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
69 But what if these two aren't synchronized in the input file?
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
70 PTS correction kicks in. The input demuxers read the PTS (presentation
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
71 timestamp) of the packages, and with it we can see if the streams
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
72 are synchronized. Then MPlayer can correct the a_frame, within
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
73 a given maximal bounder (see -mc option). The summary of the
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
74 corrections can be found in c_total .
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
75
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
76 Of course this is not everything, several things suck.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
77 For example the soundcards delay, which has to be corrected by
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
78 MPlayer: that's why it needs the size of the audio buffer. It can
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
79 be measured with select(), which is unfortunately not supported by
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
80 every card... That's when it has to be given with the -abs option.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
81
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
82 Then there's another problem: in MPEG, the PTS is not given by
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
83 frames, rather by sectors, which can contain 10 frames, or only 0.1 .
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
84 In order this won't fuck up timing, we average the PTS by 5 frames,
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
85 and use this when correcting.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
86
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
87 Life didn't get simpler with AVI. There's the "official" timing
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
88 method, the BPS-based, so the header contains how many compressed
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
89 audio bytes belong to one second of frames.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
90 Of course this doesn't always work... why it should :)
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
91 So I emulate the MPEG's PTS/sector method on AVI, that is the
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
92 AVI parser calculates a fake PTS for every read chunk, decided by
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
93 the type of the frames. This is how my timing is done. And sometimes
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
94 this works better.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
95
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
96 In AVI, usually there is a bigger piece of audio stored first, then
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
97 comes the video. This needs to be calculated into the delay, this is
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
98 called "Initial PTS delay".
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
99 Of course there are 2 of them, one is stored in the header and not
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
100 really used :) the other isn't stored anywhere, this can only be
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
101 measured...
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
102
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
103 4. Codecs. They are separate libs.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
104 For example libac3, libmpeg2, xa/*, alaw.c, opendivx/*, loader, mp3lib.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
105 mplayer.c calls them if a piece of audio or video needs to be played.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
106 (see the beginning of 3.)
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
107 And they call the appropriate demuxer, to get the compressed data.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
108 (see 2.)
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
109
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
110 5.a Codec controller: this is the greates hack in the whole :)
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
111 The libmpeg2 is so unstable, that I can't believe it.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
112 Of course I don't mean it bullshit :) rather it only accepts
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
113 totally perfect, errorfree streams. If it founds error, it's
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
114 just a segfault ;) And don't start laughing, this is great this way,
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
115 from the view of speed it would be 50-100% slower if stuffed full with
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
116 verifications. That's why I solved it by running it in a separate
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
117 process, and if it dies, who cares, just start another.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
118 However, a few things are needed for this:
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
119 - codec controller process: a separate process, which sleeps, but if
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
120 its child (the libmpeg2 process) dies, it quickly starts another.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
121 So the MPlayer doesn't have to care about this, it just pumps the
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
122 compressed stuff into the child, which displays it.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
123 - shmem: the compressed data, and the uncompressed frames are both
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
124 in shared memory, so all 3 processes (mplayer, codeccontrol,
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
125 libmpeg2 codec) sees 'em, so they can trade data fast.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
126 - FIFO is used for the communication between them.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
127 - If the child dies while decoding, the succesfully decoded data
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
128 isn't lost, they are inherited by the new child throught the
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
129 shared mem! So only a little error can be seen in the video,
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
130 it won't disappear or turn green, as in the older versions.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
131
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
132 The disadvantage of this all is that since the libvo and libmpeg2
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
133 are closely related, the libvo needs to run in the same process as
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
134 the libmpeg2, in the one that keeps dying/reborning, and not in the
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
135 one that has the controlling process, the MPlayer. This causes a
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
136 lot of problems, mostly at the handling of events in the libvo window
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
137 (keypresses, etc). So there are miscellaneous workarounds, a lot of
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
138 FIFO, and trick which exploits that X doesn't care which process
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
139 queries its events.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
140
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
141 I'd like to solve this in the near future, and use the signal/longjmp
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
142 (this is a hack, too:)) method, developed on the mpeg2dec-devel list.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
143
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
144 5. libvo: this displays the frame. There are 2 different output routines in it:
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
145
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
146 5.a draw_slice(): this displays YV12 pictures (3 frames, a full sized which
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
147 contains brightness, and 2 with 1/4 sizes, which contain the colour
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
148 info). MPEG codecs (libmpeg2, opendivx) use this. This doesn't have
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
149 to display the whole frame, only update small parts of it.
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
150 5.b draw_frame(): this is the older interface, this displays only complete
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
151 frames, and can do only packed format (YUV2, RGB/BGR).
642d64c1cc33 translation by Gabucino
gabucino
parents:
diff changeset
152 Win32 codecs use this (DivX, Indeo, etc).