hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright 2019 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#include "codec.h"
 
#include <sys/prctl.h>
 
#include "buffer.h"
#include "utils.h"
 
namespace easymedia
{
 
    Codec::Codec()
    {
        memset(&config, 0, sizeof(config));
    }
 
    Codec::~Codec()
    {
    }
 
    std::shared_ptr<MediaBuffer> Codec::GetExtraData(void** data, size_t* size)
    {
        if (data && size && extra_data) {
            *data = extra_data->GetPtr();
            *size = extra_data->GetValidSize();
        }
        return extra_data;
    }
 
    bool Codec::SetExtraData(void* data, size_t size, bool realloc)
    {
        if (!realloc) {
            if (!extra_data) {
                extra_data = std::make_shared<MediaBuffer>();
                if (!extra_data) {
                    return false;
                }
            }
            extra_data->SetPtr(data);
            extra_data->SetValidSize(size);
            extra_data->SetSize(size);
            extra_data->SetUserData(nullptr);
            return true;
        }
 
        if (!data || size == 0) {
            return false;
        }
 
        auto extradata = MediaBuffer::Alloc(size);
        if (!extradata || extradata->GetSize() < size) {
            LOG_NO_MEMORY();
            return false;
        }
        memcpy(extradata->GetPtr(), data, size);
        extradata->SetValidSize(size);
        extra_data = extradata;
        return true;
    }
 
    bool Codec::Init()
    {
        return false;
    }
 
    // Copy from ffmpeg.
    static const uint8_t* find_startcode_internal(const uint8_t* p, const uint8_t* end)
    {
        const uint8_t* a = p + 4 - ((intptr_t)p & 3);
 
        for (end -= 3; p < a && p < end; p++) {
            if (p[0] == 0 && p[1] == 0 && p[2] == 1) {
                return p;
            }
        }
 
        for (end -= 3; p < end; p += 4) {
            uint32_t x = *(const uint32_t*)p;
            //      if ((x - 0x01000100) & (~x) & 0x80008000) // little endian
            //      if ((x - 0x00010001) & (~x) & 0x00800080) // big endian
            if ((x - 0x01010101) & (~x) & 0x80808080) { // generic
                if (p[1] == 0) {
                    if (p[0] == 0 && p[2] == 1) {
                        return p;
                    }
                    if (p[2] == 0 && p[3] == 1) {
                        return p + 1;
                    }
                }
                if (p[3] == 0) {
                    if (p[2] == 0 && p[4] == 1) {
                        return p + 2;
                    }
                    if (p[4] == 0 && p[5] == 1) {
                        return p + 3;
                    }
                }
            }
        }
 
        for (end += 3; p < end; p++) {
            if (p[0] == 0 && p[1] == 0 && p[2] == 1) {
                return p;
            }
        }
 
        return end + 3;
    }
 
    const uint8_t* find_nalu_startcode(const uint8_t* p, const uint8_t* end)
    {
        const uint8_t* out = find_startcode_internal(p, end);
        if (p < out && out < end && !out[-1]) {
            out--;
        }
        return out;
    }
 
    std::list<std::shared_ptr<MediaBuffer>> split_h264_separate(const uint8_t* buffer, size_t length, int64_t timestamp)
    {
        std::list<std::shared_ptr<MediaBuffer>> l;
        const uint8_t* p = buffer;
        const uint8_t* end = p + length;
        const uint8_t *nal_start = nullptr, *nal_end = nullptr;
        nal_start = find_nalu_startcode(p, end);
        // 00 00 01 or 00 00 00 01
        size_t start_len = (nal_start[2] == 1 ? 3 : 4);
        for (;;) {
            if (nal_start == end) {
                break;
            }
            nal_start += start_len;
            nal_end = find_nalu_startcode(nal_start, end);
            size_t size = nal_end - nal_start + start_len;
            uint8_t nal_type = (*nal_start) & 0x1F;
            uint32_t flag;
            switch (nal_type) {
                case 7:
                case 8:
                    flag = MediaBuffer::kExtraIntra;
                    break;
                default:
                    flag = 0;
            }
 
            // not extraIntra?
            if (!flag) {
                break;
            }
 
            auto sub_buffer = MediaBuffer::Alloc(size);
            if (!sub_buffer) {
                LOG_NO_MEMORY(); // fatal error
                l.clear();
                return l;
            }
            memcpy(sub_buffer->GetPtr(), nal_start - start_len, size);
            sub_buffer->SetValidSize(size);
            sub_buffer->SetUserFlag(flag);
            sub_buffer->SetUSTimeStamp(timestamp);
            sub_buffer->SetType(Type::Video);
            l.push_back(sub_buffer);
 
            nal_start = nal_end;
        }
        return std::move(l);
    }
 
    std::list<std::shared_ptr<MediaBuffer>> split_h265_separate(const uint8_t* buffer, size_t length, int64_t timestamp)
    {
        std::list<std::shared_ptr<MediaBuffer>> l;
        const uint8_t* p = buffer;
        const uint8_t* end = p + length;
        const uint8_t *nal_start = nullptr, *nal_end = nullptr;
        nal_start = find_nalu_startcode(p, end);
        // 00 00 01 or 00 00 00 01
        size_t start_len = (nal_start[2] == 1 ? 3 : 4);
        for (;;) {
            if (nal_start == end) {
                break;
            }
            nal_start += start_len;
            nal_end = find_nalu_startcode(nal_start, end);
            size_t size = nal_end - nal_start + start_len;
            uint8_t nal_type = ((*nal_start) & 0x7E) >> 1;
            uint32_t flag;
            switch (nal_type) {
                case 32:
                case 33:
                case 34:
                    flag = MediaBuffer::kExtraIntra;
                    break;
                default:
                    flag = 0;
            }
 
            // not extraIntra?
            if (!flag) {
                break;
            }
 
            auto sub_buffer = MediaBuffer::Alloc(size);
            if (!sub_buffer) {
                LOG_NO_MEMORY(); // fatal error
                l.clear();
                return l;
            }
            memcpy(sub_buffer->GetPtr(), nal_start - start_len, size);
            sub_buffer->SetValidSize(size);
            sub_buffer->SetUserFlag(flag);
            sub_buffer->SetUSTimeStamp(timestamp);
            l.push_back(sub_buffer);
 
            nal_start = nal_end;
        }
        return std::move(l);
    }
 
    static void* FindNaluByType(std::shared_ptr<MediaBuffer>& mb, int nal_type, int& size, CodecType c_type)
    {
        if ((c_type != CODEC_TYPE_H264) && (c_type != CODEC_TYPE_H265)) {
            LOG("ERROR: %s failed! Invalid codec type\n", __func__);
            return NULL;
        }
 
        void* target_nalu = NULL;
        const uint8_t* start = (uint8_t*)mb->GetPtr();
        const uint8_t* end = start + mb->GetValidSize();
        const uint8_t *nal_start = nullptr, *nal_end = nullptr;
        nal_start = nal_end = find_nalu_startcode(start, end);
        // 00 00 01 or 00 00 00 01
        int start_len = (nal_start[2] == 1 ? 3 : 4);
        int nal_size = 0;
        uint8_t type = 0;
 
        for (;;) {
            if (nal_start == end) {
                break;
            }
            nal_start = nal_end;
            nal_end = find_nalu_startcode(nal_start + start_len, end);
            nal_size = nal_end - nal_start;
 
            if (c_type == CODEC_TYPE_H264) {
                type = *(nal_start + start_len) & 0x1F;
            } else {
                type = (*(nal_start + start_len) & 0x7E) >> 1;
            }
 
            if (type == nal_type) {
                size = nal_size;
                target_nalu = (void*)nal_start;
                break;
            }
        }
 
        return target_nalu;
    }
 
    void* GetVpsFromBuffer(std::shared_ptr<MediaBuffer>& mb, int& size, CodecType c_type)
    {
 
        if (c_type != CODEC_TYPE_H265) {
            return NULL;
        }
 
        return FindNaluByType(mb, 32, size, c_type);
    }
 
    void* GetSpsFromBuffer(std::shared_ptr<MediaBuffer>& mb, int& size, CodecType c_type)
    {
 
        int nalu_type = 0;
        if (c_type == CODEC_TYPE_H265) {
            nalu_type = 33;
        } else if (c_type == CODEC_TYPE_H264) {
            nalu_type = 7;
        } else {
            return NULL;
        }
 
        return FindNaluByType(mb, nalu_type, size, c_type);
    }
 
    void* GetPpsFromBuffer(std::shared_ptr<MediaBuffer>& mb, int& size, CodecType c_type)
    {
 
        int nalu_type = 0;
        if (c_type == CODEC_TYPE_H265) {
            nalu_type = 34;
        } else if (c_type == CODEC_TYPE_H264) {
            nalu_type = 8;
        } else {
            return NULL;
        }
 
        return FindNaluByType(mb, nalu_type, size, c_type);
    }
 
    void* GetSpsPpsFromBuffer(std::shared_ptr<MediaBuffer>& mb, int& size, CodecType c_type)
    {
 
        void* sps_ptr = NULL;
        int sps_size = 0;
        void* pps_ptrt = NULL;
        int pps_size = 0;
 
        // get sps from buffer
        sps_ptr = GetSpsFromBuffer(mb, sps_size, c_type);
        if (!sps_ptr) {
            return NULL;
        }
        // get pps from buffer
        pps_ptrt = GetPpsFromBuffer(mb, pps_size, c_type);
        if (!pps_ptrt) {
            return NULL;
        }
 
        size = sps_size + pps_size;
        return sps_ptr;
    }
 
    void* GetVpsSpsPpsFromBuffer(std::shared_ptr<MediaBuffer>& mb, int& size, CodecType c_type)
    {
 
        void* vps_ptr = NULL;
        int vps_size = 0;
        void* sps_ptr = NULL;
        int sps_size = 0;
        void* pps_ptrt = NULL;
        int pps_size = 0;
 
        // get vps from buffer
        vps_ptr = GetVpsFromBuffer(mb, vps_size, c_type);
        if (!vps_ptr) {
            return NULL;
        }
        // get sps from buffer
        sps_ptr = GetSpsFromBuffer(mb, sps_size, c_type);
        if (!sps_ptr) {
            return NULL;
        }
        // get pps from buffer
        pps_ptrt = GetPpsFromBuffer(mb, pps_size, c_type);
        if (!pps_ptrt) {
            return NULL;
        }
 
        size = vps_size + sps_size + pps_size;
        return vps_ptr;
    }
 
    void* GetSeiFromBuffer(std::shared_ptr<MediaBuffer>& mb, int& size, CodecType c_type)
    {
 
        int nalu_type = 0;
        if (c_type == CODEC_TYPE_H265) {
            nalu_type = 39;
        } else if (c_type == CODEC_TYPE_H264) {
            nalu_type = 6;
        } else {
            return NULL;
        }
 
        return FindNaluByType(mb, nalu_type, size, c_type);
    }
 
    // Last nalu must be IDR otherwise will occure error.
    void* GetIntraFromBuffer(std::shared_ptr<MediaBuffer>& mb, int& size, CodecType c_type)
    {
        void* idr_ptr = NULL;
        int nalu_type = 0;
        if (c_type == CODEC_TYPE_H265) {
            nalu_type = 19;
        } else if (c_type == CODEC_TYPE_H264) {
            nalu_type = 5;
        } else {
            return NULL;
        }
 
        idr_ptr = FindNaluByType(mb, nalu_type, size, c_type);
        size = mb->GetValidSize() - (int)((uint8_t*)idr_ptr - (uint8_t*)mb->GetPtr());
 
        return idr_ptr;
    }
 
} // namespace easymedia