|
| 1 | +// RFC 9328 RTP Payload Format for Versatile Video Coding(VVC) |
| 2 | + |
| 3 | +#include "mpeg4-vvc.h" |
| 4 | +#include "sdp-payload.h" |
| 5 | +#include "base64.h" |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <stdlib.h> |
| 9 | +#include <string.h> |
| 10 | +#include <assert.h> |
| 11 | +#include <errno.h> |
| 12 | + |
| 13 | +int sdp_h266(uint8_t *data, int bytes, const char* proto, unsigned short port, int payload, int frequence, const void* extra, int extra_size) |
| 14 | +{ |
| 15 | + static const char* pattern = |
| 16 | + "m=video %hu %s %d\n" |
| 17 | + "a=rtpmap:%d H266/90000\n" |
| 18 | + "a=fmtp:%d"; |
| 19 | + |
| 20 | + const uint8_t nalu[] = { 13/*dci*/, 14/*vps*/, 15/*sps*/, 16/*pps*/ }; |
| 21 | + const char* sprop[] = { "sprop-dci", "sprop-vps", "sprop-sps", "sprop-pps" }; |
| 22 | + |
| 23 | + int r, n; |
| 24 | + int i, j, k; |
| 25 | + struct mpeg4_vvc_t vvc; |
| 26 | + |
| 27 | + assert(90000 == frequence); |
| 28 | + r = mpeg4_vvc_decoder_configuration_record_load((const uint8_t*)extra, extra_size, &vvc); |
| 29 | + if (r < 0) |
| 30 | + return r; |
| 31 | + |
| 32 | + n = snprintf((char*)data, bytes, pattern, port, proto && *proto ? proto : "RTP/AVP", payload, payload, payload); |
| 33 | + |
| 34 | + for (i = 0; i < sizeof(nalu) / sizeof(nalu[0]) && n < bytes; i++) |
| 35 | + { |
| 36 | + if (i > 0 && n < bytes) data[n++] = ';'; |
| 37 | + n += snprintf((char*)data + n, bytes - n, " %s=", sprop[i]); |
| 38 | + |
| 39 | + for (k = j = 0; j < vvc.numOfArrays; j++) |
| 40 | + { |
| 41 | + assert(vvc.nalu[j].bytes > 2 && vvc.nalu[j].type == ((vvc.nalu[j].data[1] >> 3) & 0x1F)); |
| 42 | + if (nalu[i] != vvc.nalu[j].type) |
| 43 | + continue; |
| 44 | + |
| 45 | + if (n + 1 + vvc.nalu[j].bytes * 2 > bytes) |
| 46 | + return -ENOMEM; // don't have enough memory |
| 47 | + |
| 48 | + if (k++ > 0 && n < bytes) data[n++] = ','; |
| 49 | + n += (int)base64_encode((char*)data + n, vvc.nalu[j].data, vvc.nalu[j].bytes); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if(n < bytes) |
| 54 | + data[n++] = '\n'; |
| 55 | + return n; |
| 56 | +} |
| 57 | + |
| 58 | +int sdp_h266_load(uint8_t* data, int bytes, const char* vps, const char* sps, const char* pps, const char* sei, const char* dci) |
| 59 | +{ |
| 60 | + int i, n, len, off; |
| 61 | + const char* p, * next; |
| 62 | + const char* sprops[5]; |
| 63 | + const uint8_t startcode[] = { 0x00, 0x00, 0x00, 0x01 }; |
| 64 | + |
| 65 | + off = 0; |
| 66 | + sprops[0] = vps; |
| 67 | + sprops[1] = sps; |
| 68 | + sprops[2] = pps; |
| 69 | + sprops[3] = sei; |
| 70 | + sprops[4] = dci; |
| 71 | + for (i = 0; i < sizeof(sprops) / sizeof(sprops[0]); i++) |
| 72 | + { |
| 73 | + p = sprops[i]; |
| 74 | + while (p) |
| 75 | + { |
| 76 | + next = strchr(p, ','); |
| 77 | + len = next ? (int)(next - p) : (int)strlen(p); |
| 78 | + if (off + (len + 3) / 4 * 3 + (int)sizeof(startcode) > bytes) |
| 79 | + return -ENOMEM; // don't have enough space |
| 80 | + |
| 81 | + memcpy(data + off, startcode, sizeof(startcode)); |
| 82 | + n = (int)base64_decode(data + off + sizeof(startcode), p, len); |
| 83 | + assert(n <= (len + 3) / 4 * 3); |
| 84 | + off += n + sizeof(startcode); |
| 85 | + off += n; |
| 86 | + |
| 87 | + p = next ? next + 1 : NULL; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
0 commit comments