• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_SHARING_H264_FRAME_H
17 #define OHOS_SHARING_H264_FRAME_H
18 
19 #include <cstdlib>
20 #include <memory>
21 #include <string>
22 #include "frame.h"
23 
24 namespace OHOS {
25 namespace Sharing {
26 
27 #define H264_TYPE(v) ((uint8_t)(v)&0x1F)
28 
29 size_t PrefixSize(const char *ptr, size_t len);
30 void SplitH264(const char *ptr, size_t len, size_t prefix, const std::function<void(const char *, size_t, size_t)> &cb);
31 // template <typename Parent>
32 class H264Frame : public FrameImpl {
33 public:
34     using Ptr = std::shared_ptr<H264Frame>;
35 
36     enum {
37         NAL_IDR = 5,
38         NAL_SEI = 6,
39         NAL_SPS = 7,
40         NAL_PPS = 8,
41         NAL_AUD = 9,
42         NAL_B_P = 1,
43     };
44 
H264Frame()45     H264Frame()
46     {
47         this->codecId_ = CODEC_H264;
48     }
49 
H264Frame(DataBuffer && dataBuffer)50     explicit H264Frame(DataBuffer &&dataBuffer) : FrameImpl(std::move(dataBuffer))
51     {
52         this->codecId_ = CODEC_H264;
53     }
54 
55     H264Frame(uint8_t *ptr, size_t size, uint32_t dts, uint32_t pts = 0, size_t prefix_size = 0)
56     {
57         this->Assign((char *)ptr, (int32_t)size);
58         dts_ = dts;
59         pts_ = pts;
60         prefixSize_ = prefix_size;
61         this->codecId_ = CODEC_H264;
62     }
63 
~H264Frame()64     ~H264Frame() override {};
65 
KeyFrame()66     bool KeyFrame() override
67     {
68         auto nalPtr = (uint8_t *)this->Data() + this->PrefixSize();
69         return H264_TYPE(*nalPtr) == NAL_IDR && DecodeAble();
70     }
71 
ConfigFrame()72     bool ConfigFrame() override
73     {
74         auto nalPtr = (uint8_t *)this->Data() + this->PrefixSize();
75         switch (H264_TYPE(*nalPtr)) {
76             case NAL_SPS: // fall-through
77             case NAL_PPS:
78                 return true;
79             default:
80                 return false;
81         }
82     }
83 
DropAble()84     bool DropAble() override
85     {
86         auto nalPtr = (uint8_t *)this->Data() + this->PrefixSize();
87         switch (H264_TYPE(*nalPtr)) {
88             case NAL_SEI: // fall-through
89             case NAL_AUD:
90                 return true;
91             default:
92                 return false;
93         }
94     }
95 
DecodeAble()96     bool DecodeAble() override
97     {
98         auto nalPtr = (uint8_t *)this->Data() + this->PrefixSize();
99         auto type = H264_TYPE(*nalPtr);
100 
101         return type >= NAL_B_P && type <= NAL_IDR && (nalPtr[1] & 0x80);
102     }
103 };
104 } // namespace Sharing
105 } // namespace OHOS
106 #endif