1e0e9324cSopenharmony_ci/* 2e0e9324cSopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3e0e9324cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e0e9324cSopenharmony_ci * you may not use this file except in compliance with the License. 5e0e9324cSopenharmony_ci * You may obtain a copy of the License at 6e0e9324cSopenharmony_ci * 7e0e9324cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e0e9324cSopenharmony_ci * 9e0e9324cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e0e9324cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e0e9324cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e0e9324cSopenharmony_ci * See the License for the specific language governing permissions and 13e0e9324cSopenharmony_ci * limitations under the License. 14e0e9324cSopenharmony_ci */ 15e0e9324cSopenharmony_ci 16e0e9324cSopenharmony_ci#include "h264_frame.h" 17e0e9324cSopenharmony_ci#include "common/common_macro.h" 18e0e9324cSopenharmony_ci#include "common/sharing_log.h" 19e0e9324cSopenharmony_ci 20e0e9324cSopenharmony_cinamespace OHOS { 21e0e9324cSopenharmony_cinamespace Sharing { 22e0e9324cSopenharmony_cisize_t PrefixSize(const char *ptr, size_t len) 23e0e9324cSopenharmony_ci{ 24e0e9324cSopenharmony_ci if (ptr == nullptr) { 25e0e9324cSopenharmony_ci return 0; 26e0e9324cSopenharmony_ci } 27e0e9324cSopenharmony_ci 28e0e9324cSopenharmony_ci if (len < 4) { // 4:byte offset 29e0e9324cSopenharmony_ci return 0; 30e0e9324cSopenharmony_ci } 31e0e9324cSopenharmony_ci if (ptr[0] != 0x00 || ptr[1] != 0x00) { 32e0e9324cSopenharmony_ci return 0; 33e0e9324cSopenharmony_ci } 34e0e9324cSopenharmony_ci if (ptr[2] == 0x00 && ptr[3] == 0x01) { // 2:byte offset, 3:byte offset 35e0e9324cSopenharmony_ci return 4; // 4:prefix size 36e0e9324cSopenharmony_ci } 37e0e9324cSopenharmony_ci if (ptr[2] == 0x01) { // 2:byte offset 38e0e9324cSopenharmony_ci return 3; // 3:prefix size 39e0e9324cSopenharmony_ci } 40e0e9324cSopenharmony_ci 41e0e9324cSopenharmony_ci return 0; 42e0e9324cSopenharmony_ci} 43e0e9324cSopenharmony_ci 44e0e9324cSopenharmony_cistatic const char *MemFind(const char *buf, ssize_t len, const char *subBuf, ssize_t subLen) 45e0e9324cSopenharmony_ci{ 46e0e9324cSopenharmony_ci if (buf == nullptr || subBuf == nullptr) { 47e0e9324cSopenharmony_ci return NULL; 48e0e9324cSopenharmony_ci } 49e0e9324cSopenharmony_ci 50e0e9324cSopenharmony_ci for (auto i = 0; i < len - subLen; ++i) { 51e0e9324cSopenharmony_ci if (memcmp(buf + i, subBuf, subLen) == 0) { 52e0e9324cSopenharmony_ci return buf + i; 53e0e9324cSopenharmony_ci } 54e0e9324cSopenharmony_ci } 55e0e9324cSopenharmony_ci 56e0e9324cSopenharmony_ci return NULL; 57e0e9324cSopenharmony_ci} 58e0e9324cSopenharmony_ci 59e0e9324cSopenharmony_civoid SplitH264(const char *ptr, size_t len, size_t prefix, const std::function<void(const char *, size_t, size_t)> &cb) 60e0e9324cSopenharmony_ci{ 61e0e9324cSopenharmony_ci RETURN_IF_NULL(ptr); 62e0e9324cSopenharmony_ci if (prefix <= 0) { 63e0e9324cSopenharmony_ci prefix = PrefixSize(ptr, len); 64e0e9324cSopenharmony_ci } 65e0e9324cSopenharmony_ci auto start = ptr + prefix; 66e0e9324cSopenharmony_ci auto end = ptr + len; 67e0e9324cSopenharmony_ci size_t nextPrefix; 68e0e9324cSopenharmony_ci while (true) { 69e0e9324cSopenharmony_ci auto nextStart = MemFind(start, end - start, "\x00\x00\x01", 3); 70e0e9324cSopenharmony_ci if (nextStart) { 71e0e9324cSopenharmony_ci if (*(nextStart - 1) == 0x00) { 72e0e9324cSopenharmony_ci nextStart -= 1; 73e0e9324cSopenharmony_ci nextPrefix = 4; // 4:prefix size 74e0e9324cSopenharmony_ci } else { 75e0e9324cSopenharmony_ci nextPrefix = 3; // 3:prefix size 76e0e9324cSopenharmony_ci } 77e0e9324cSopenharmony_ci 78e0e9324cSopenharmony_ci cb(start - prefix, nextStart - start + prefix, prefix); 79e0e9324cSopenharmony_ci 80e0e9324cSopenharmony_ci start = nextStart + nextPrefix; 81e0e9324cSopenharmony_ci 82e0e9324cSopenharmony_ci prefix = nextPrefix; 83e0e9324cSopenharmony_ci continue; 84e0e9324cSopenharmony_ci } 85e0e9324cSopenharmony_ci 86e0e9324cSopenharmony_ci cb(start - prefix, end - start + prefix, prefix); 87e0e9324cSopenharmony_ci break; 88e0e9324cSopenharmony_ci } 89e0e9324cSopenharmony_ci} 90e0e9324cSopenharmony_ci} // namespace Sharing 91e0e9324cSopenharmony_ci} // namespace OHOS