| 1 | /********** |
| 2 | This library is free software; you can redistribute it and/or modify it under |
| 3 | the terms of the GNU Lesser General Public License as published by the |
| 4 | Free Software Foundation; either version 3 of the License, or (at your |
| 5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
| 6 | |
| 7 | This library is distributed in the hope that it will be useful, but WITHOUT |
| 8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 10 | more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Lesser General Public License |
| 13 | along with this library; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | **********/ |
| 16 | // "liveMedia" |
| 17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
| 18 | // RTP sink for MPEG video (RFC 2250) |
| 19 | // Implementation |
| 20 | |
| 21 | #include "MPEG1or2VideoRTPSink.hh" |
| 22 | #include "MPEG1or2VideoStreamFramer.hh" |
| 23 | |
| 24 | MPEG1or2VideoRTPSink::MPEG1or2VideoRTPSink(UsageEnvironment& env, Groupsock* RTPgs) |
| 25 | : VideoRTPSink(env, RTPgs, 32, 90000, "MPV" ) { |
| 26 | fPictureState.temporal_reference = 0; |
| 27 | fPictureState.picture_coding_type = fPictureState.vector_code_bits = 0; |
| 28 | } |
| 29 | |
| 30 | MPEG1or2VideoRTPSink::~MPEG1or2VideoRTPSink() { |
| 31 | } |
| 32 | |
| 33 | MPEG1or2VideoRTPSink* |
| 34 | MPEG1or2VideoRTPSink::createNew(UsageEnvironment& env, Groupsock* RTPgs) { |
| 35 | return new MPEG1or2VideoRTPSink(env, RTPgs); |
| 36 | } |
| 37 | |
| 38 | Boolean MPEG1or2VideoRTPSink::sourceIsCompatibleWithUs(MediaSource& source) { |
| 39 | // Our source must be an appropriate framer: |
| 40 | return source.isMPEG1or2VideoStreamFramer(); |
| 41 | } |
| 42 | |
| 43 | Boolean MPEG1or2VideoRTPSink::allowFragmentationAfterStart() const { |
| 44 | return True; |
| 45 | } |
| 46 | |
| 47 | Boolean MPEG1or2VideoRTPSink |
| 48 | ::frameCanAppearAfterPacketStart(unsigned char const* frameStart, |
| 49 | unsigned numBytesInFrame) const { |
| 50 | // A 'frame' (which in this context can mean a header or a slice as well as a |
| 51 | // complete picture) can appear at other than the first position in a packet |
| 52 | // in all situations, EXCEPT when it follows the end of (i.e., the last slice |
| 53 | // of) a picture. I.e., the headers at the beginning of a picture must |
| 54 | // appear at the start of a RTP packet. |
| 55 | if (!fPreviousFrameWasSlice) return True; |
| 56 | |
| 57 | // A slice is already packed into this packet. We allow this new 'frame' |
| 58 | // to be packed after it, provided that it is also a slice: |
| 59 | return numBytesInFrame >= 4 |
| 60 | && frameStart[0] == 0 && frameStart[1] == 0 && frameStart[2] == 1 |
| 61 | && frameStart[3] >= 1 && frameStart[3] <= 0xAF; |
| 62 | } |
| 63 | |
| 64 | #define 0x000001B3 |
| 65 | #define PICTURE_START_CODE 0x00000100 |
| 66 | |
| 67 | void MPEG1or2VideoRTPSink |
| 68 | ::doSpecialFrameHandling(unsigned fragmentationOffset, |
| 69 | unsigned char* frameStart, |
| 70 | unsigned numBytesInFrame, |
| 71 | struct timeval framePresentationTime, |
| 72 | unsigned numRemainingBytes) { |
| 73 | Boolean thisFrameIsASlice = False; // until we learn otherwise |
| 74 | if (isFirstFrameInPacket()) { |
| 75 | fSequenceHeaderPresent = fPacketBeginsSlice = fPacketEndsSlice = False; |
| 76 | } |
| 77 | |
| 78 | if (fragmentationOffset == 0) { |
| 79 | // Begin by inspecting the 4-byte code at the start of the frame: |
| 80 | if (numBytesInFrame < 4) return; // shouldn't happen |
| 81 | unsigned startCode = (frameStart[0]<<24) | (frameStart[1]<<16) |
| 82 | | (frameStart[2]<<8) | frameStart[3]; |
| 83 | |
| 84 | if (startCode == VIDEO_SEQUENCE_HEADER_START_CODE) { |
| 85 | // This is a video sequence header |
| 86 | fSequenceHeaderPresent = True; |
| 87 | } else if (startCode == PICTURE_START_CODE) { |
| 88 | // This is a picture header |
| 89 | |
| 90 | // Record the parameters of this picture: |
| 91 | if (numBytesInFrame < 8) return; // shouldn't happen |
| 92 | unsigned next4Bytes = (frameStart[4]<<24) | (frameStart[5]<<16) |
| 93 | | (frameStart[6]<<8) | frameStart[7]; |
| 94 | unsigned char byte8 = numBytesInFrame == 8 ? 0 : frameStart[8]; |
| 95 | |
| 96 | fPictureState.temporal_reference = (next4Bytes&0xFFC00000)>>(32-10); |
| 97 | fPictureState.picture_coding_type = (next4Bytes&0x00380000)>>(32-(10+3)); |
| 98 | |
| 99 | unsigned char FBV, BFC, FFV, FFC; |
| 100 | FBV = BFC = FFV = FFC = 0; |
| 101 | switch (fPictureState.picture_coding_type) { |
| 102 | case 3: |
| 103 | FBV = (byte8&0x40)>>6; |
| 104 | BFC = (byte8&0x38)>>3; |
| 105 | // fall through to: |
| 106 | case 2: |
| 107 | FFV = (next4Bytes&0x00000004)>>2; |
| 108 | FFC = ((next4Bytes&0x00000003)<<1) | ((byte8&0x80)>>7); |
| 109 | } |
| 110 | |
| 111 | fPictureState.vector_code_bits = (FBV<<7) | (BFC<<4) | (FFV<<3) | FFC; |
| 112 | } else if ((startCode&0xFFFFFF00) == 0x00000100) { |
| 113 | unsigned char lastCodeByte = startCode&0xFF; |
| 114 | |
| 115 | if (lastCodeByte <= 0xAF) { |
| 116 | // This is (the start of) a slice |
| 117 | thisFrameIsASlice = True; |
| 118 | } else { |
| 119 | // This is probably a GOP header; we don't do anything with this |
| 120 | } |
| 121 | } else { |
| 122 | // The first 4 bytes aren't a code that we recognize. |
| 123 | envir() << "Warning: MPEG1or2VideoRTPSink::doSpecialFrameHandling saw strange first 4 bytes " |
| 124 | << (void*)startCode << ", but we're not a fragment\n" ; |
| 125 | } |
| 126 | } else { |
| 127 | // We're a fragment (other than the first) of a slice. |
| 128 | thisFrameIsASlice = True; |
| 129 | } |
| 130 | |
| 131 | if (thisFrameIsASlice) { |
| 132 | // This packet begins a slice iff there's no fragmentation offset: |
| 133 | fPacketBeginsSlice = (fragmentationOffset == 0); |
| 134 | |
| 135 | // This packet also ends a slice iff there are no fragments remaining: |
| 136 | fPacketEndsSlice = (numRemainingBytes == 0); |
| 137 | } |
| 138 | |
| 139 | // Set the video-specific header based on the parameters that we've seen. |
| 140 | // Note that this may get done more than once, if several frames appear |
| 141 | // in the packet. That's OK, because this situation happens infrequently, |
| 142 | // and we want the video-specific header to reflect the most up-to-date |
| 143 | // information (in particular, from a Picture Header) anyway. |
| 144 | unsigned = |
| 145 | // T == 0 |
| 146 | (fPictureState.temporal_reference<<16) | |
| 147 | // AN == N == 0 |
| 148 | (fSequenceHeaderPresent<<13) | |
| 149 | (fPacketBeginsSlice<<12) | |
| 150 | (fPacketEndsSlice<<11) | |
| 151 | (fPictureState.picture_coding_type<<8) | |
| 152 | fPictureState.vector_code_bits; |
| 153 | setSpecialHeaderWord(videoSpecificHeader); |
| 154 | |
| 155 | // Also set the RTP timestamp. (As above, we do this for each frame |
| 156 | // in the packet.) |
| 157 | setTimestamp(framePresentationTime); |
| 158 | |
| 159 | // Set the RTP 'M' (marker) bit iff this frame ends (i.e., is the last |
| 160 | // slice of) a picture (and there are no fragments remaining). |
| 161 | // This relies on the source being a "MPEG1or2VideoStreamFramer". |
| 162 | MPEG1or2VideoStreamFramer* framerSource = (MPEG1or2VideoStreamFramer*)fSource; |
| 163 | if (framerSource != NULL && framerSource->pictureEndMarker() |
| 164 | && numRemainingBytes == 0) { |
| 165 | setMarkerBit(); |
| 166 | framerSource->pictureEndMarker() = False; |
| 167 | } |
| 168 | |
| 169 | fPreviousFrameWasSlice = thisFrameIsASlice; |
| 170 | } |
| 171 | |
| 172 | unsigned MPEG1or2VideoRTPSink::() const { |
| 173 | // There's a 4 byte special video header: |
| 174 | return 4; |
| 175 | } |
| 176 | |