Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

h264: move from rem to re #350

Merged
merged 2 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ set(HEADERS
include/re_dns.h
include/re_fmt.h
include/re.h
include/re_h264.h
include/re_h265.h
include/re_hash.h
include/re_hmac.h
Expand Down Expand Up @@ -267,6 +268,10 @@ set(SRCS
src/fmt/time.c
src/fmt/unicode.c

src/h264/getbit.c
src/h264/nal.c
src/h264/sps.c

src/h265/nal.c

src/hash/func.c
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ MODULES += shim
MODULES += trice
MODULES += pcp
MODULES += av1
MODULES += h264
MODULES += h265

INSTALL := install
Expand Down
101 changes: 101 additions & 0 deletions include/re_h264.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @file rem_h264.h Interface to H.264 header parser
*
* Copyright (C) 2010 Creytiv.com
*/


/** NAL unit types */
enum h264_nalu {
H264_NALU_SLICE = 1,
H264_NALU_DPA = 2,
H264_NALU_DPB = 3,
H264_NALU_DPC = 4,
H264_NALU_IDR_SLICE = 5,
H264_NALU_SEI = 6,
H264_NALU_SPS = 7,
H264_NALU_PPS = 8,
H264_NALU_AUD = 9,
H264_NALU_END_SEQUENCE = 10,
H264_NALU_END_STREAM = 11,
H264_NALU_FILLER_DATA = 12,
H264_NALU_SPS_EXT = 13,
H264_NALU_AUX_SLICE = 19,

H264_NALU_STAP_A = 24,
H264_NALU_STAP_B = 25,
H264_NALU_MTAP16 = 26,
H264_NALU_MTAP24 = 27,
H264_NALU_FU_A = 28,
H264_NALU_FU_B = 29,
};


/**
* H.264 NAL Header
*/
struct h264_nal_header {
unsigned f:1; /**< Forbidden zero bit (must be 0) */
unsigned nri:2; /**< nal_ref_idc */
unsigned type:5; /**< NAL unit type */
};


int h264_nal_header_encode(struct mbuf *mb, const struct h264_nal_header *hdr);
int h264_nal_header_decode(struct h264_nal_header *hdr, struct mbuf *mb);
const char *h264_nal_unit_name(enum h264_nalu nal_type);


/**
* H.264 Sequence Parameter Set (SPS)
*/
struct h264_sps {
uint8_t profile_idc;
uint8_t level_idc;
uint8_t seq_parameter_set_id; /* 0-31 */
uint8_t chroma_format_idc; /* 0-3 */

unsigned log2_max_frame_num;
unsigned pic_order_cnt_type;

unsigned max_num_ref_frames;
unsigned pic_width_in_mbs;
unsigned pic_height_in_map_units;

unsigned frame_crop_left_offset; /* pixels */
unsigned frame_crop_right_offset; /* pixels */
unsigned frame_crop_top_offset; /* pixels */
unsigned frame_crop_bottom_offset; /* pixels */
};

int h264_sps_decode(struct h264_sps *sps, const uint8_t *p, size_t len);
void h264_sps_resolution(const struct h264_sps *sps,
unsigned *width, unsigned *height);
const char *h264_sps_chroma_format_name(uint8_t chroma_format_idc);


typedef int (h264_packet_h)(bool marker, uint64_t rtp_ts,
const uint8_t *hdr, size_t hdr_len,
const uint8_t *pld, size_t pld_len,
void *arg);

/** Fragmentation Unit header */
struct h264_fu {
unsigned s:1; /**< Start bit */
unsigned e:1; /**< End bit */
unsigned r:1; /**< The Reserved bit MUST be equal to 0 */
unsigned type:5; /**< The NAL unit payload type */
};

int h264_fu_hdr_encode(const struct h264_fu *fu, struct mbuf *mb);
int h264_fu_hdr_decode(struct h264_fu *fu, struct mbuf *mb);

const uint8_t *h264_find_startcode(const uint8_t *p, const uint8_t *end);

int h264_packetize(uint64_t rtp_ts, const uint8_t *buf, size_t len,
size_t pktsize, h264_packet_h *pkth, void *arg);
int h264_nal_send(bool first, bool last,
bool marker, uint32_t ihdr, uint64_t rtp_ts,
const uint8_t *buf, size_t size, size_t maxsz,
h264_packet_h *pkth, void *arg);
bool h264_is_keyframe(int type);
92 changes: 92 additions & 0 deletions src/h264/getbit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* @file h264/getbit.c Generic bit parser
*
* Copyright (C) 2010 Creytiv.com
*/

#include <re_types.h>
#include <re_fmt.h>
#include "h264.h"


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

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


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)
{
const uint8_t *p;
register unsigned tmp;

if (!gb)
return 0;

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

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

++gb->pos;

return tmp;
}


int get_ue_golomb(struct getbit *gb, unsigned *valp)
{
unsigned zeros = 0;
unsigned info;
int i;

if (!gb)
return EINVAL;

while (1) {

if (getbit_get_left(gb) < 1)
return EBADMSG;

if (0 == get_bit(gb))
++zeros;
else
break;
}

info = 1 << zeros;

for (i = zeros - 1; i >= 0; i--) {

if (getbit_get_left(gb) < 1)
return EBADMSG;

info |= get_bit(gb) << i;
}

if (valp)
*valp = info - 1;

return 0;
}
18 changes: 18 additions & 0 deletions src/h264/h264.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file h264/h264.h Internal interface
*
* Copyright (C) 2010 Creytiv.com
*/


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);
9 changes: 9 additions & 0 deletions src/h264/mod.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# mod.mk
#
# Copyright (C) 2010 Creytiv.com
#

SRCS += h264/getbit.c
SRCS += h264/nal.c
SRCS += h264/sps.c
Loading