1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15**********/
16// "liveMedia"
17// Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved.
18// A simplified version of "H264or5VideoStreamFramer" that takes only complete,
19// discrete frames (rather than an arbitrary byte stream) as input.
20// This avoids the parsing and data copying overhead of the full
21// "H264or5VideoStreamFramer".
22// Implementation
23
24#include "H264or5VideoStreamDiscreteFramer.hh"
25
26H264or5VideoStreamDiscreteFramer
27::H264or5VideoStreamDiscreteFramer(int hNumber, UsageEnvironment& env, FramedSource* inputSource,
28 Boolean includeStartCodeInOutput,
29 Boolean insertAccessUnitDelimiters)
30 : H264or5VideoStreamFramer(hNumber, env, inputSource, False/*don't create a parser*/,
31 includeStartCodeInOutput, insertAccessUnitDelimiters) {
32}
33
34H264or5VideoStreamDiscreteFramer::~H264or5VideoStreamDiscreteFramer() {
35}
36
37void H264or5VideoStreamDiscreteFramer::doGetNextFrame() {
38 if (fIncludeStartCodeInOutput) {
39 // Prepend a 4-byte 'start code' (0x00000001) to the output:
40 if (fMaxSize < 4) { // there's no space
41 fNumTruncatedBytes = 4 - fMaxSize;
42 handleClosure();
43 return;
44 }
45 *fTo++ = 0x00; *fTo++ = 0x00; *fTo++ = 0x00; *fTo++ = 0x01;
46 fMaxSize -= 4;
47 }
48
49 if (fInsertAccessUnitDelimiters && pictureEndMarker()) {
50 // Deliver an "access_unit_delimiter" NAL unit instead:
51 unsigned const audNALSize = fHNumber == 264 ? 2 : 3;
52
53 if (audNALSize > fMaxSize) { // there's no space
54 fNumTruncatedBytes = audNALSize - fMaxSize;
55 handleClosure();
56 return;
57 }
58
59 if (fHNumber == 264) {
60 *fTo++ = 9; // "Access unit delimiter" nal_unit_type
61 *fTo++ = 0xF0; // "primary_pic_type" (7); "rbsp_trailing_bits()"
62 } else { // H.265
63 *fTo++ = 35<<1; // "Access unit delimiter" nal_unit_type
64 *fTo++ = 0; // "nuh_layer_id" (0); "nuh_temporal_id_plus1" (0) (Is this correct??)
65 *fTo++ = 0x50; // "pic_type" (2); "rbsp_trailing_bits()" (Is this correct??)
66 }
67
68 fFrameSize = (fIncludeStartCodeInOutput ? 4: 0) + audNALSize;
69 pictureEndMarker() = False; // for next time
70 afterGetting(this); // complete delivery to the downstream object
71 } else {
72 // Normal case:
73 // Arrange to read data (which should be a complete H.264 or H.265 NAL unit)
74 // from our data source, directly into the client's input buffer.
75 // After reading this, we'll do some parsing on the frame.
76 fInputSource->getNextFrame(fTo, fMaxSize,
77 afterGettingFrame, this,
78 FramedSource::handleClosure, this);
79 }
80}
81
82void H264or5VideoStreamDiscreteFramer
83::afterGettingFrame(void* clientData, unsigned frameSize,
84 unsigned numTruncatedBytes,
85 struct timeval presentationTime,
86 unsigned durationInMicroseconds) {
87 H264or5VideoStreamDiscreteFramer* source = (H264or5VideoStreamDiscreteFramer*)clientData;
88 source->afterGettingFrame1(frameSize, numTruncatedBytes, presentationTime, durationInMicroseconds);
89}
90
91void H264or5VideoStreamDiscreteFramer
92::afterGettingFrame1(unsigned frameSize, unsigned numTruncatedBytes,
93 struct timeval presentationTime,
94 unsigned durationInMicroseconds) {
95 // Get the "nal_unit_type", to see if this NAL unit is one that we want to save a copy of:
96 u_int8_t nal_unit_type;
97 if (fHNumber == 264 && frameSize >= 1) {
98 nal_unit_type = fTo[0]&0x1F;
99 } else if (fHNumber == 265 && frameSize >= 2) {
100 nal_unit_type = (fTo[0]&0x7E)>>1;
101 } else {
102 // This is too short to be a valid NAL unit, so just assume a bogus nal_unit_type
103 nal_unit_type = 0xFF;
104 }
105
106 // Begin by checking for a (likely) common error: NAL units that (erroneously) begin with a
107 // 0x00000001 or 0x000001 'start code'. (Those start codes should only be in byte-stream data;
108 // *not* data that consists of discrete NAL units.)
109 // Once again, to be clear: The NAL units that you feed to a "H264or5VideoStreamDiscreteFramer"
110 // MUST NOT include start codes.
111 if (frameSize >= 4 && fTo[0] == 0 && fTo[1] == 0 && ((fTo[2] == 0 && fTo[3] == 1) || fTo[2] == 1)) {
112 envir() << "H264or5VideoStreamDiscreteFramer error: MPEG 'start code' seen in the input\n";
113 } else if (isVPS(nal_unit_type)) { // Video parameter set (VPS)
114 saveCopyOfVPS(fTo, frameSize);
115 } else if (isSPS(nal_unit_type)) { // Sequence parameter set (SPS)
116 saveCopyOfSPS(fTo, frameSize);
117 } else if (isPPS(nal_unit_type)) { // Picture parameter set (PPS)
118 saveCopyOfPPS(fTo, frameSize);
119 }
120
121 fPictureEndMarker = nalUnitEndsAccessUnit(nal_unit_type);
122
123 // Finally, complete delivery to the client:
124 fFrameSize = fIncludeStartCodeInOutput ? (4+frameSize) : frameSize;
125 fNumTruncatedBytes = numTruncatedBytes;
126 fPresentationTime = presentationTime;
127 fDurationInMicroseconds = durationInMicroseconds;
128 afterGetting(this);
129}
130
131Boolean H264or5VideoStreamDiscreteFramer::nalUnitEndsAccessUnit(u_int8_t nal_unit_type) {
132 // Check whether this NAL unit ends the current 'access unit' (basically, a video frame).
133 // Unfortunately, we can't do this reliably, because we don't yet know anything about the
134 // *next* NAL unit that we'll see. So, we guess this as best as we can, by assuming that
135 // if this NAL unit is a VCL NAL unit, then it ends the current 'access unit'.
136 //
137 // This will be wrong if you are streaming multiple 'slices' per picture. In that case,
138 // you can define a subclass that reimplements this virtual function to do the right thing.
139
140 return isVCL(nal_unit_type);
141}
142