From faf5c3464ae75b6a0c14e1a1a5cae3faf2f199c4 Mon Sep 17 00:00:00 2001 From: "Alfred E. Heggestad" Date: Sun, 25 Oct 2015 09:59:58 +0100 Subject: move H.264 packetization code from avcodec to core --- modules/avcodec/avcodec.c | 3 + modules/avcodec/avcodec.h | 6 -- modules/avcodec/decode.c | 4 +- modules/avcodec/encode.c | 3 +- modules/avcodec/h264.c | 188 ---------------------------------------------- modules/avcodec/h26x.h | 61 --------------- modules/avcodec/module.mk | 2 +- 7 files changed, 8 insertions(+), 259 deletions(-) delete mode 100644 modules/avcodec/h264.c (limited to 'modules') diff --git a/modules/avcodec/avcodec.c b/modules/avcodec/avcodec.c index dfcd38d..3df9ba7 100644 --- a/modules/avcodec/avcodec.c +++ b/modules/avcodec/avcodec.c @@ -14,6 +14,9 @@ #include "avcodec.h" +const uint8_t h264_level_idc = 0x0c; + + int avcodec_resolve_codecid(const char *s) { if (0 == str_casecmp(s, "H263")) diff --git a/modules/avcodec/avcodec.h b/modules/avcodec/avcodec.h index 890df5f..f3f0398 100644 --- a/modules/avcodec/avcodec.h +++ b/modules/avcodec/avcodec.h @@ -55,13 +55,7 @@ int decode_h263_test(struct viddec_state *st, struct vidframe *frame, int decode_sdpparam_h264(struct videnc_state *st, const struct pl *name, const struct pl *val); -int h264_packetize(struct mbuf *mb, size_t pktsize, - videnc_packet_h *pkth, void *arg); int h264_decode(struct viddec_state *st, struct mbuf *src); -int h264_nal_send(bool first, bool last, - bool marker, uint32_t ihdr, const uint8_t *buf, - size_t size, size_t maxsz, - videnc_packet_h *pkth, void *arg); int avcodec_resolve_codecid(const char *s); diff --git a/modules/avcodec/decode.c b/modules/avcodec/decode.c index 06d43b0..5fb6b6f 100644 --- a/modules/avcodec/decode.c +++ b/modules/avcodec/decode.c @@ -219,9 +219,9 @@ int h264_decode(struct viddec_state *st, struct mbuf *src) err = h264_hdr_encode(&h264_hdr, st->mb); } else if (H264_NAL_FU_A == h264_hdr.type) { - struct fu fu; + struct h264_fu fu; - err = fu_hdr_decode(&fu, src); + err = h264_fu_hdr_decode(&fu, src); if (err) return err; h264_hdr.type = fu.type; diff --git a/modules/avcodec/encode.c b/modules/avcodec/encode.c index 6e51d63..69e2bbf 100644 --- a/modules/avcodec/encode.c +++ b/modules/avcodec/encode.c @@ -632,7 +632,8 @@ int encode(struct videnc_state *st, bool update, const struct vidframe *frame) break; case AV_CODEC_ID_H264: - err = h264_packetize(st->mb, st->encprm.pktsize, + err = h264_packetize(st->mb->buf, st->mb->end, + st->encprm.pktsize, st->pkth, st->arg); break; diff --git a/modules/avcodec/h264.c b/modules/avcodec/h264.c deleted file mode 100644 index 4c2aa59..0000000 --- a/modules/avcodec/h264.c +++ /dev/null @@ -1,188 +0,0 @@ -/** - * @file avcodec/h264.c H.264 video codec (RFC 3984) - * - * Copyright (C) 2010 Creytiv.com - */ -#include -#include -#include -#include -#include -#ifdef USE_X264 -#include -#endif -#include "h26x.h" -#include "avcodec.h" - - -const uint8_t h264_level_idc = 0x0c; - - -int h264_hdr_encode(const struct h264_hdr *hdr, struct mbuf *mb) -{ - uint8_t v; - - v = hdr->f<<7 | hdr->nri<<5 | hdr->type<<0; - - return mbuf_write_u8(mb, v); -} - - -int h264_hdr_decode(struct h264_hdr *hdr, struct mbuf *mb) -{ - uint8_t v; - - if (mbuf_get_left(mb) < 1) - return ENOENT; - - v = mbuf_read_u8(mb); - - hdr->f = v>>7 & 0x1; - hdr->nri = v>>5 & 0x3; - hdr->type = v>>0 & 0x1f; - - return 0; -} - - -int fu_hdr_encode(const struct fu *fu, struct mbuf *mb) -{ - uint8_t v = fu->s<<7 | fu->s<<6 | fu->r<<5 | fu->type; - return mbuf_write_u8(mb, v); -} - - -int fu_hdr_decode(struct fu *fu, struct mbuf *mb) -{ - uint8_t v; - - if (mbuf_get_left(mb) < 1) - return ENOENT; - - v = mbuf_read_u8(mb); - - fu->s = v>>7 & 0x1; - fu->e = v>>6 & 0x1; - fu->r = v>>5 & 0x1; - fu->type = v>>0 & 0x1f; - - return 0; -} - - -/* - * Find the NAL start sequence in a H.264 byte stream - * - * @note: copied from ffmpeg source - */ -const uint8_t *h264_find_startcode(const uint8_t *p, const uint8_t *end) -{ - const uint8_t *a = p + 4 - ((long)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*)(void *)p; - if ( (x - 0x01010101) & (~x) & 0x80808080 ) { - 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; -} - - -static int rtp_send_data(const uint8_t *hdr, size_t hdr_sz, - const uint8_t *buf, size_t sz, bool eof, - videnc_packet_h *pkth, void *arg) -{ - return pkth(eof, hdr, hdr_sz, buf, sz, arg); -} - - -int h264_nal_send(bool first, bool last, - bool marker, uint32_t ihdr, const uint8_t *buf, - size_t size, size_t maxsz, - videnc_packet_h *pkth, void *arg) -{ - uint8_t hdr = (uint8_t)ihdr; - int err = 0; - - if (first && last && size <= maxsz) { - err = rtp_send_data(&hdr, 1, buf, size, marker, - pkth, arg); - } - else { - uint8_t fu_hdr[2]; - const uint8_t type = hdr & 0x1f; - const uint8_t nri = hdr & 0x60; - const size_t sz = maxsz - 2; - - fu_hdr[0] = nri | H264_NAL_FU_A; - fu_hdr[1] = first ? (1<<7 | type) : type; - - while (size > sz) { - err |= rtp_send_data(fu_hdr, 2, buf, sz, false, - pkth, arg); - buf += sz; - size -= sz; - fu_hdr[1] &= ~(1 << 7); - } - - if (last) - fu_hdr[1] |= 1<<6; /* end bit */ - - err |= rtp_send_data(fu_hdr, 2, buf, size, marker && last, - pkth, arg); - } - - return err; -} - - -int h264_packetize(struct mbuf *mb, size_t pktsize, - videnc_packet_h *pkth, void *arg) -{ - const uint8_t *start = mb->buf; - const uint8_t *end = start + mb->end; - const uint8_t *r; - int err = 0; - - r = h264_find_startcode(mb->buf, end); - - while (r < end) { - const uint8_t *r1; - - /* skip zeros */ - while (!*(r++)) - ; - - r1 = h264_find_startcode(r, end); - - err |= h264_nal_send(true, true, (r1 >= end), r[0], - r+1, r1-r-1, pktsize, - pkth, arg); - r = r1; - } - - return err; -} diff --git a/modules/avcodec/h26x.h b/modules/avcodec/h26x.h index 7a21696..faff489 100644 --- a/modules/avcodec/h26x.h +++ b/modules/avcodec/h26x.h @@ -101,65 +101,4 @@ void h263_hdr_copy_strm(struct h263_hdr *hdr, const struct h263_strm *s); * H.264 */ - -/** NAL unit types (RFC 3984, Table 1) */ -enum { - H264_NAL_UNKNOWN = 0, - /* 1-23 NAL unit Single NAL unit packet per H.264 */ - H264_NAL_SLICE = 1, - H264_NAL_DPA = 2, - H264_NAL_DPB = 3, - H264_NAL_DPC = 4, - H264_NAL_IDR_SLICE = 5, - H264_NAL_SEI = 6, - H264_NAL_SPS = 7, - H264_NAL_PPS = 8, - H264_NAL_AUD = 9, - H264_NAL_END_SEQUENCE = 10, - H264_NAL_END_STREAM = 11, - H264_NAL_FILLER_DATA = 12, - H264_NAL_SPS_EXT = 13, - H264_NAL_AUX_SLICE = 19, - - H264_NAL_STAP_A = 24, /**< Single-time aggregation packet */ - H264_NAL_STAP_B = 25, /**< Single-time aggregation packet */ - H264_NAL_MTAP16 = 26, /**< Multi-time aggregation packet */ - H264_NAL_MTAP24 = 27, /**< Multi-time aggregation packet */ - H264_NAL_FU_A = 28, /**< Fragmentation unit */ - H264_NAL_FU_B = 29, /**< Fragmentation unit */ -}; - -/** - * H.264 Header defined in RFC 3984 - * - *
-      +---------------+
-      |0|1|2|3|4|5|6|7|
-      +-+-+-+-+-+-+-+-+
-      |F|NRI|  Type   |
-      +---------------+
- * 
- */ -struct h264_hdr { - unsigned f:1; /**< 1 bit - Forbidden zero bit (must be 0) */ - unsigned nri:2; /**< 2 bits - nal_ref_idc */ - unsigned type:5; /**< 5 bits - nal_unit_type */ -}; - -int h264_hdr_encode(const struct h264_hdr *hdr, struct mbuf *mb); -int h264_hdr_decode(struct h264_hdr *hdr, struct mbuf *mb); - -/** Fragmentation Unit header */ -struct 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 fu_hdr_encode(const struct fu *fu, struct mbuf *mb); -int fu_hdr_decode(struct fu *fu, struct mbuf *mb); - -const uint8_t *h264_find_startcode(const uint8_t *p, const uint8_t *end); - int h264_decode_sprop_params(AVCodecContext *codec, struct pl *pl); diff --git a/modules/avcodec/module.mk b/modules/avcodec/module.mk index acff21b..1927a94 100644 --- a/modules/avcodec/module.mk +++ b/modules/avcodec/module.mk @@ -9,7 +9,7 @@ USE_X264 := $(shell [ -f $(SYSROOT)/include/x264.h ] || \ [ -f $(SYSROOT_ALT)/include/x264.h ] && echo "yes") MOD := avcodec -$(MOD)_SRCS += avcodec.c h263.c h264.c encode.c decode.c +$(MOD)_SRCS += avcodec.c h263.c encode.c decode.c $(MOD)_LFLAGS += -lavcodec -lavutil CFLAGS += -DUSE_AVCODEC ifneq ($(USE_X264),) -- cgit v1.2.3