A Better mtv frontend

mtv communicates with mtvp via a pair of pipes, and a proprietary protocol. This protocol can be used to create an interface without the problems that mtvcontrol suffers from.

I don't have an interface yet, but I've got almost enough of the protocol sufficiently reverse-engineered that I can soon implement an interface. I still need to figure out some of the window-based commands. Not sure how soon I'll have time though.

How mtv works with mtvp

when the MpegTV's interface program (mtv) starts, it first makes two pipes, which end up with the file descriptors 3,4 and 5,6. mtv then starts mtvp with the option -r3,4,5,6 which tells it about these file descriptors (mtvp will inherit them from mtv). mtv then closes descriptors 3 and 6. It writes to fd 4 in order to send messages from mtvp, and reads from fd 5 to receive messages. Likewise, mtvp writes to fd 6 and reads from fd 3.

The protocol used to communicate is based on packets with the following format:

    +------------+--------------+---------+--------------------+------------+
    | sequence # | command type | command | size of parameters | parameters |
    | 2 bytes    | 2 bytes      | 2 bytes | 2 bytes            | (variable) |
    +------------+--------------+---------+--------------------+------------+
Each direction maintains its own sequence numbers, starting at zero.

The following command types have been identified:

The following is a list of commands. Each direction has its own command set; while they hardly overlap, they do slightly. In some cases the overlapping command code means the same thing in both directions, in other cases it doesn't. The only place where the combined type and command overlap is type 0x02 command 0x0a.
Commands from mtv to mtvp
TypeCommandpurposeparameters
0x020x02exit-
0x040x08set pause/play state:2 == play, 1 == pause, 0==stopped or done?, 0x8000000x used when scrolling
0x020x09step to next frame-
0x020x0ajump to end of program ???-
0x040x0bjump to random location in filebyte offset (from beginning of file)
0x040x0cset looping0==off, 1==on
0x040x10set auto play0==off, 1==on
0x040x11set display mode0==no display, 1==gray, 2==8bit, 3==truecolor
0x360x12gamma correction1.4 is default for suns
0x040x13set frame mode0==maintain nominal frame rate, 1==all frames, 2==I&P frames, 3==Iframes
0x060x14set frame rate0==use encoded framerate from file; 29.97 is most common
0x040x15mute1==mute, 0==unmute
0x040x16set audio channels0==both, 1==left, 2==right
0x040x17set audio quality0==low, 1==medium, 2==high
0x060x18set volume0.0 to 1.0 (loudest)
0x060x19set balance0.0 (left) to 1.0 (right)
0x040x1ause pcm vol/bal???:?
0x020x25where are you in the file?-
0x020x26what is your frame rate?-
0x020x27what is your current frame?-
0x360x29contrast0.0==normal
0x360x2abrightness0.0==normal
0x040x2cset audio outputbitmask: for suns, has these values: 0x04==line-out, 0x02==headphone, 0x01=speaker
0x000x35security key4 byte key
0x040x41set aspect ratio0==square, 1==from file, 2==unconstrained
0x360x43saturation1.0==normal
0x380x47set filenull-terminated filename
0x000x4eX11-related requestsee "Extended Commands" below
Commands from mtvp to mtv
0x040x01unidentified, at startuponly observed value has been 0x0a
0x000x02ACK???followed by the actuall packet being ACKed, including sequence number, and then 4 more unidentified bytes
0x020x04unidentified, at startup-
0x020x05unidentified, at exit-
0x040x06my pause/play state issame as 0x02 0x08, above
0x040x07i've read this many bytesbytecount
0x060x08my frame rate isframerate
0x040x09i'm on frame numberframe number
0x020x0aunidentified, sent after filename received. note that this is "jump to end of program" command when sent in the other direction-
0x020x0bunidentified, at exit-
0x060x0dmy volume is0.0 to 1.0
0x060x0emy current balance is0.0 to 1.0
0x040x12the file size issize of file in bytes
0x020x15unidentified, response-
0x000x17video informationsee "Extended Commands" below
0x000x18audio informationsee "Extended Commands" below
0x000x19freeze frame info (sent whenever frame is frozen for any reason, e.g. pause, end, slider in use)see "Extended Commands" below
0x000x1acombined data (audio, video, overhead) bits/second. i.e. seconds of playback can be calculated by (8*filesize/this number)integer - number of bits
0x040x1bunidentified, response to something - maybe means "end transaction" or something like that?4 bytes of data, observed value always 0x0100
0x040x1caudio out can be ???see 0x04 0x2c above
0x040x1dmy audio output is set tosee 0x04 0x2c above
0x020x1eunidentified, after EOF-
0x020x1funidentified, after EOF-
0x040x20unidentified, at startupvalue changes with each run, samples: 0b45, 055e, 09ad, 09fa
0x020x22unidentified, at startup-
0x000x23unidentified, at startup4 bytes of data, observed value of zero

Extended Commands

Three commands have been observed that set multiple parameters at once, One from mtv to mtvp, and two from mtvp to mtv. The one from mtv seems to be related to X11. The command is type 0x00, command 0x4e. The size of the paramaters is 264. The first 8 bytes seem to be a subcommand: size (possibly), as the first four bytes, and then the command, which so far has been from 0 to 7. The remaining block is the data, which always seems to be 256 bytes. The subcommands seem to each have multiple functions, but this is part of what they seem to be doing: Unfortunately, I don't have much sketched out for the content of these data blocks.

From mtvp to mtv, there are three extended commands, one for video information (0x00 0x17) and one for audio information (0x00 0x18). They are shorter, 48 and 40 bytes of data respectively. I've mostly mapped out their meaning:

    0x00 0x17 video information, 48 bytes
    +------+------+------+------+------+------+------+------+
 0  |   00 |   00 |   00 |   01 |    width of MPEG video    |
    +------+------+------+------+------+------+------+------+
 8  |   height of MPEG video    | number of bits per second |
    +------+------+------+------+------+------+------+------+
16  |   VSV? buffer size ???    |      |      |      |      |
    +------+------+------+------+------+------+------+------+
24  |    double == frame rate                               |
    +------+------+------+------+------+------+------+------+
32  |      |      |      |      |      |      |      |      |
    +------+------+------+------+------+------+------+------+
40  |    double == pixel aspect ration                      |
    +------+------+------+------+------+------+------+------+


    0x00 0x18 audio information, 40 bytes
    +------+------+------+------+------+------+------+------+
 0  |   00 |   00 |   00 |   0? |   00 |   00 |   00 |   02 |
    +------+------+------+------+------+------+------+------+
 8  |  audio recorded bits/sec  |   00 |   00 |   00 |   00 |
    +------+------+------+------+------+------+------+------+
16  |    double == sample rate or playback rate             |
    +------+------+------+------+------+------+------+------+
24  |   00 |   00 |   00 |   00 |   00 |   00 |   00 |   0? |
    +------+------+------+------+------+------+------+------+
32  |   00 |   00 |   00 |   00 |   00 |   00 |   00 |   00 |
    +------+------+------+------+------+------+------+------+


    0x00 0x19 single frame information, 64
    +------+------+------+------+------+------+------+------+
 0  | frame type, 0=I, 1=P, 2=B |    temporal reference     |
    +------+------+------+------+------+------+------+------+
 8  |    double == VBV delay, in seconds                    |
    +------+------+------+------+------+------+------+------+
16  |   00 |   00 |   00 |   0x |   00 |   00 |   00 |   0x | forward f-code?
    +------+------+------+------+------+------+------+------+
24  |   00 |   00 |   00 |   01 |   00 |   00 |   00 |   01 | backward f-code?
    +------+------+------+------+------+------+------+------+
32  |      |      |      |      |      |      |      |      |
    +------+------+------+------+------+------+------+------+
40  | frames played to this pnt |      |      |      |      |
    +------+------+------+------+------+------+------+------+
48  |   00 |   01 |   00 |   00 |   00 |   00 |   00 |   00 |
    +------+------+------+------+------+------+------+------+
56  |    double == seconds into content where this frame is |
    +------+------+------+------+------+------+------+------+

Reverse Engineering and the Law

I AM NOT A LAWYER However, I have studied this issue and come to the conclusion that the software and information presented here was legally develloped and derived, and can be legally distributed in the United States.

Reverse engineering is the process of figuring out how something works only by examining the final product. This information can then be used to develop competing products or add-on products. At the time of this writing (June, 2000), reverse engineering is still legal (in the United States).

On the other hand, the license that you agree to does explicitly forbid this activity. However it is unclear that such an agreement is legal. Software licenses are widely believed to be so legally flawed as to be worthless. In other words, if reverse engineering is legal, MpegTV can't make it illegal.

Of course, under strict terms of their agreement, even the original klunky interface (mtvcontrol) might be considered a violation of their license, since it is a derivative work (sort of). There was even some reverse-engineering involved in its development, since the mtvp documentation turned out to be incorrect in a couple of cases.


Tom Fine's Home Send Me Email