Skip to content

Commit

Permalink
dd: Dependency Descriptor RTP header extension (#1170)
Browse files Browse the repository at this point in the history
* dd: Dependency Descriptor RTP header extension

Defined in:
https://aomediacodec.github.io/av1-rtp-spec/#dependency-descriptor-rtp-header-extension

This work was sponsored by Nvidia Corporation

* dd: fix windows warning

* test: disable dd_print()

* dd: use re_printf()

* dd: remove debugging

* dd: fix for clang analyze

* dd: add limit check for count
  • Loading branch information
alfredh authored Jul 31, 2024
1 parent 133266f commit 7d8ac60
Show file tree
Hide file tree
Showing 13 changed files with 1,408 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ set(HEADERS
include/re_convert.h
include/re_crc32.h
include/re_dbg.h
include/re_dd.h
include/re_dns.h
include/re_fmt.h
include/re_h264.h
Expand Down Expand Up @@ -226,6 +227,10 @@ set(SRCS

src/dbg/dbg.c

src/dd/dd.c
src/dd/dd_enc.c
src/dd/putbit.c

src/dns/client.c
src/dns/cstr.c
src/dns/dname.c
Expand Down
109 changes: 109 additions & 0 deletions include/re_dd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @file re_dd.h Dependency Descriptor (DD)
*
* Copyright (C) 2010 - 2023 Alfred E. Heggestad
*/


/*
* Put bits wrapper (XXX: move to common place)
*/

struct putbit {
struct mbuf *mb;
size_t bit_pos;
};

void putbit_init(struct putbit *pb, struct mbuf *mb);
int putbit_one(struct putbit *pb, unsigned bit);
int putbit_write(struct putbit *pb, unsigned count, unsigned val);
int putbit_write_ns(struct putbit *pb, unsigned n, unsigned v);


/*
* Dependency Descriptor (DD)
*
* DT: Decode Target
* DTI: Decode Target Indication
* SID: Spatial ID
* TID: Temporal ID
*/

/* Constants. */
enum {
DD_MAX_SPATIAL_IDS = 4u,
DD_MAX_TEMPORAL_IDS = 4u,
DD_MAX_TEMPLATES = 8u,
DD_MAX_FDIFFS = 16u,
DD_MAX_DECODE_TARGETS= 16u,
DD_MAX_CHAINS = 32u,
};


/* Decode Target Indication (DTI) */
enum dd_dti {
DD_DTI_NOT_PRESENT = 0,
DD_DTI_DISCARDABLE = 1,
DD_DTI_SWITCH = 2,
DD_DTI_REQUIRED = 3,
};

enum dd_next_layer_idc {
DD_SAME_LAYER = 0,
DD_NEXT_TEMPORAL_LAYER = 1,
DD_NEXT_SPATIAL_LAYER = 2,
DD_NO_MORE_TEMPLATES = 3,
};

/*
* https://aomediacodec.github.io/av1-rtp-spec/
* #dependency-descriptor-rtp-header-extension
*/
struct dd {

/* Mandatory Descriptor Fields */
unsigned start_of_frame:1;
unsigned end_of_frame:1;
unsigned frame_dependency_template_id:6;
uint16_t frame_number;

// TODO: needed?
bool ext;

unsigned template_dependency_structure_present_flag:1;
unsigned active_decode_targets_present_flag:1;
unsigned custom_dtis_flag:1;
unsigned custom_fdiffs_flag:1;
unsigned custom_chains_flag:1;

unsigned active_decode_targets_bitmask;
unsigned template_id_offset:6;
uint8_t dt_cnt;
uint8_t template_cnt;
uint8_t max_spatial_id;

uint8_t template_spatial_id[DD_MAX_TEMPLATES];
uint8_t template_temporal_id[DD_MAX_TEMPLATES];

/* render_resolutions */
bool resolutions_present_flag;
uint16_t max_render_width_minus_1[DD_MAX_SPATIAL_IDS];
uint16_t max_render_height_minus_1[DD_MAX_SPATIAL_IDS];
uint8_t render_count;

/* type: enum dd_dti */
uint8_t template_dti[DD_MAX_TEMPLATES][DD_MAX_DECODE_TARGETS];

/* template fdiffs */
uint8_t template_fdiff[DD_MAX_TEMPLATES][DD_MAX_FDIFFS];
uint8_t template_fdiff_cnt[DD_MAX_TEMPLATES];

/* template chains */
uint8_t decode_target_protected_by[DD_MAX_DECODE_TARGETS];
uint8_t template_chain_fdiff[DD_MAX_TEMPLATES][DD_MAX_CHAINS];
uint8_t chain_cnt;
};

int dd_encode(struct mbuf *mb, const struct dd *dd);
int dd_decode(struct dd *dd, const uint8_t *buf, size_t sz);
void dd_print(const struct dd *dd);
18 changes: 18 additions & 0 deletions include/re_h264.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,21 @@ int h264_stap_encode(struct mbuf *mb, const uint8_t *frame,
size_t frame_sz);
int h264_stap_decode_annexb(struct mbuf *mb_frame, struct mbuf *mb_pkt);
int h264_stap_decode_annexb_long(struct mbuf *mb_frame, struct mbuf *mb_pkt);


/*
* Get bits wrapper
*/

struct getbit {
const uint8_t *buf;
size_t pos;
size_t end;
};

void getbit_init(struct getbit *gb, const uint8_t *buf, size_t size);
size_t getbit_get_left(const struct getbit *gb);
unsigned get_bit(struct getbit *gb);
int get_ue_golomb(struct getbit *gb, unsigned *valp);
unsigned get_bits(struct getbit *gb, unsigned n);
unsigned getbit_read_ns(struct getbit *gb, unsigned n);
61 changes: 61 additions & 0 deletions src/av1/getbit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @file getbit.c Get bits helper
*
* Copyright (C) 2023 Alfred E. Heggestad
*/

#include <re.h>
#include <re_av1.h>


#define DEBUG_MODULE "av1"
#define DEBUG_LEVEL 5
#include <re_dbg.h>


void getbit_init(struct getbit *gb, const uint8_t *buf, size_t size_bits)
{
if (!gb)
return;

gb->buf = buf;
gb->pos = 0;
gb->end = size_bits;
}


size_t getbit_get_left(const struct getbit *gb)
{
if (!gb)
return 0;

if (gb->end > gb->pos)
return gb->end - gb->pos;
else
return 0;
}


unsigned get_bit(struct getbit *gb)
{
register unsigned tmp;

if (!gb)
return 0;

if (gb->pos >= gb->end) {
DEBUG_WARNING("get_bit: read past end"
" (%zu >= %zu)\n", gb->pos, gb->end);
assert(0);
return 0;
}

const uint8_t *p = gb->buf;
tmp = ((*(p + (gb->pos >> 0x3))) >> (0x7 - (gb->pos & 0x7))) & 0x1;

++gb->pos;

return tmp;
}


Loading

0 comments on commit 7d8ac60

Please sign in to comment.