#include "libavutil/common.h"
-#include "parser.h"
+#include "golomb_legacy.h"
#include "hevc.h"
+#include "hevcdec.h"
+#include "h2645_parse.h"
+#include "parser.h"
#define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
+#define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
+
+typedef struct HEVCParserContext {
+ ParseContext pc;
+
+ H2645Packet pkt;
+ HEVCParamSets ps;
+
+ int parsed_extradata;
+} HEVCParserContext;
+
+static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
+ AVCodecContext *avctx)
+{
+ HEVCParserContext *ctx = s->priv_data;
+ GetBitContext *gb = &nal->gb;
+
+ HEVCPPS *pps;
+ HEVCSPS *sps;
+ HEVCWindow *ow;
+ unsigned int pps_id;
+
+ get_bits1(gb); // first slice in pic
+ if (IS_IRAP_NAL(nal))
+ get_bits1(gb); // no output of prior pics
+
+ pps_id = get_ue_golomb_long(gb);
+ if (pps_id >= HEVC_MAX_PPS_COUNT || !ctx->ps.pps_list[pps_id]) {
+ av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
+ return AVERROR_INVALIDDATA;
+ }
+ pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
+ sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
+ ow = &sps->output_window;
+
+ /* export the stream parameters */
+ s->coded_width = sps->width;
+ s->coded_height = sps->height;
+ s->width = sps->width - ow->left_offset - ow->right_offset;
+ s->height = sps->height - ow->top_offset - ow->bottom_offset;
+ s->format = sps->pix_fmt;
+ avctx->profile = sps->ptl.general_ptl.profile_idc;
+ avctx->level = sps->ptl.general_ptl.level_idc;
+
+ /* ignore the rest for now*/
+
+ return 0;
+}
+
+static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
+ int buf_size, AVCodecContext *avctx)
+{
+ HEVCParserContext *ctx = s->priv_data;
+ int ret, i;
+
+ ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
+ AV_CODEC_ID_HEVC);
+ if (ret < 0)
+ return ret;
+
+ for (i = 0; i < ctx->pkt.nb_nals; i++) {
+ H2645NAL *nal = &ctx->pkt.nals[i];
+
+ /* ignore everything except parameter sets and VCL NALUs */
+ switch (nal->type) {
+ case HEVC_NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
+ case HEVC_NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
+ case HEVC_NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
+ case HEVC_NAL_TRAIL_R:
+ case HEVC_NAL_TRAIL_N:
+ case HEVC_NAL_TSA_N:
+ case HEVC_NAL_TSA_R:
+ case HEVC_NAL_STSA_N:
+ case HEVC_NAL_STSA_R:
+ case HEVC_NAL_BLA_W_LP:
+ case HEVC_NAL_BLA_W_RADL:
+ case HEVC_NAL_BLA_N_LP:
+ case HEVC_NAL_IDR_W_RADL:
+ case HEVC_NAL_IDR_N_LP:
+ case HEVC_NAL_CRA_NUT:
+ case HEVC_NAL_RADL_N:
+ case HEVC_NAL_RADL_R:
+ case HEVC_NAL_RASL_N:
+ case HEVC_NAL_RASL_R: hevc_parse_slice_header(s, nal, avctx); break;
+ }
+ }
+
+ return 0;
+}
+
/**
* Find the end of the current frame in the bitstream.
* @return the position of the first byte of the next frame, or END_NOT_FOUND
static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
int buf_size)
{
+ HEVCParserContext *ctx = s->priv_data;
+ ParseContext *pc = &ctx->pc;
int i;
- ParseContext *pc = s->priv_data;
for (i = 0; i < buf_size; i++) {
int nut;
nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
// Beginning of access unit
- if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
+ if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_AUD) || nut == HEVC_NAL_SEI_PREFIX ||
(nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
if (pc->frame_start_found) {
pc->frame_start_found = 0;
return i - 5;
}
- } else if (nut <= NAL_RASL_R ||
- (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
+ } else if (nut <= HEVC_NAL_RASL_R ||
+ (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
int first_slice_segment_in_pic_flag = buf[i] >> 7;
if (first_slice_segment_in_pic_flag) {
if (!pc->frame_start_found) {
pc->frame_start_found = 1;
- s->key_frame = nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT;
+ s->key_frame = nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT;
} else { // First slice of next frame found
pc->frame_start_found = 0;
return i - 5;
const uint8_t *buf, int buf_size)
{
int next;
- ParseContext *pc = s->priv_data;
+
+ HEVCParserContext *ctx = s->priv_data;
+ ParseContext *pc = &ctx->pc;
+
+ if (avctx->extradata && !ctx->parsed_extradata) {
+ parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
+ ctx->parsed_extradata = 1;
+ }
if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
next = buf_size;
}
}
+ parse_nal_units(s, buf, buf_size, avctx);
+
*poutbuf = buf;
*poutbuf_size = buf_size;
return next;
state = (state << 8) | buf[i];
if (((state >> 8) & 0xFFFFFF) == START_CODE) {
int nut = (state >> 1) & 0x3F;
- if (nut >= NAL_VPS && nut <= NAL_PPS)
+ if (nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_PPS)
has_ps = 1;
else if (has_ps)
return i - 3;
return 0;
}
+static void hevc_parser_close(AVCodecParserContext *s)
+{
+ HEVCParserContext *ctx = s->priv_data;
+ int i;
+
+ for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
+ av_buffer_unref(&ctx->ps.vps_list[i]);
+ for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
+ av_buffer_unref(&ctx->ps.sps_list[i]);
+ for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
+ av_buffer_unref(&ctx->ps.pps_list[i]);
+
+ ff_h2645_packet_uninit(&ctx->pkt);
+
+ av_freep(&ctx->pc.buffer);
+}
+
AVCodecParser ff_hevc_parser = {
.codec_ids = { AV_CODEC_ID_HEVC },
- .priv_data_size = sizeof(ParseContext),
+ .priv_data_size = sizeof(HEVCParserContext),
.parser_parse = hevc_parse,
- .parser_close = ff_parse_close,
+ .parser_close = hevc_parser_close,
.split = hevc_split,
};