History

avcodec/evc_parser: make ff_evc_parse_nal_unit() local to the parser

This is in preparation for the following commits.

James Almer committed 3 years ago

Commit a5663f21 parent ff7a4cd3 files · +202 −40Browse files

Showing the changes to libavcodec (3 of 3 files). Show all

Unified · Split

3 files
  1. libavcodec/evc_parse.c +9 −8
  2. libavcodec/evc_parse.h +3 −0
  3. libavcodec/evc_parser.c +190 −32
libavcodec/evc_parse.c+9−8
@@ -70,11 +70,12 @@ int ff_evc_get_temporal_id(const uint8_t *bits, int bits_size, void *logctx) · 69 unmodified lines
7070 }
7171
7272 // @see ISO_IEC_23094-1 (7.3.2.6 Slice layer RBSP syntax)
73−static int evc_parse_slice_header(EVCParserContext *ctx, EVCParserSliceHeader *sh, const uint8_t *bs, int bs_size)
73+int ff_evc_parse_slice_header(EVCParserSliceHeader *sh, const EVCParamSets *ps,
74+ enum EVCNALUnitType nalu_type, const uint8_t *bs, int bs_size)
7475 {
7576 GetBitContext gb;
76− EVCParserPPS *pps;
77− EVCParserSPS *sps;
77+ const EVCParserPPS *pps;
78+ const EVCParserSPS *sps;
7879
7980 int num_tiles_in_slice = 0;
8081 int slice_pic_parameter_set_id;
@@ -88,11 +89,11 @@ static int evc_parse_slice_header(EVCParserContext *ctx, EVCParserSliceHeader *s · 7 unmodified lines
8889 if (slice_pic_parameter_set_id < 0 || slice_pic_parameter_set_id >= EVC_MAX_PPS_COUNT)
8990 return AVERROR_INVALIDDATA;
9091
91− pps = ctx->ps.pps[slice_pic_parameter_set_id];
92+ pps = ps->pps[slice_pic_parameter_set_id];
9293 if(!pps)
9394 return AVERROR_INVALIDDATA;
9495
95− sps = ctx->ps.sps[pps->pps_seq_parameter_set_id];
96+ sps = ps->sps[pps->pps_seq_parameter_set_id];
9697 if(!sps)
9798 return AVERROR_INVALIDDATA;
9899
@@ -121,7 +122,7 @@ static int evc_parse_slice_header(EVCParserContext *ctx, EVCParserSliceHeader *s · 22 unmodified lines
121122
122123 sh->slice_type = get_ue_golomb(&gb);
123124
124− if (ctx->nalu_type == EVC_IDR_NUT)
125+ if (nalu_type == EVC_IDR_NUT)
125126 sh->no_output_of_prior_pics_flag = get_bits(&gb, 1);
126127
127128 if (sps->sps_mmvd_flag && ((sh->slice_type == EVC_SLICE_TYPE_B) || (sh->slice_type == EVC_SLICE_TYPE_P)))
@@ -175,7 +176,7 @@ static int evc_parse_slice_header(EVCParserContext *ctx, EVCParserSliceHeader *s · 47 unmodified lines
175176 }
176177 }
177178
178− if (ctx->nalu_type != EVC_IDR_NUT) {
179+ if (nalu_type != EVC_IDR_NUT) {
179180 if (sps->sps_pocs_flag)
180181 sh->slice_pic_order_cnt_lsb = get_bits(&gb, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
181182 }
@@ -372,7 +373,7 @@ int ff_evc_parse_nal_unit(EVCParserContext *ctx, const uint8_t *buf, int buf_siz · 190 unmodified lines
372373 EVCParserSliceHeader sh;
373374 int ret;
374375
375− ret = evc_parse_slice_header(ctx, &sh, data, nalu_size);
376+ ret = ff_evc_parse_slice_header(&sh, &ctx->ps, nalu_type, data, nalu_size);
376377 if (ret < 0) {
377378 av_log(logctx, AV_LOG_ERROR, "Slice header parsing error\n");
378379 return ret;
libavcodec/evc_parse.h+3−0
@@ -159,6 +159,9 @@ int ff_evc_get_temporal_id(const uint8_t *bits, int bits_size, void *logctx); · 158 unmodified lines
159159
160160 int ff_evc_parse_nal_unit(EVCParserContext *ctx, const uint8_t *buf, int buf_size, void *logctx);
161161
162+int ff_evc_parse_slice_header(EVCParserSliceHeader *sh, const EVCParamSets *ps,
163+ enum EVCNALUnitType nalu_type, const uint8_t *buf, int buf_size);
164+
162165 // POC (picture order count of the current picture) derivation
163166 // @see ISO/IEC 23094-1:2020(E) 8.3.1 Decoding process for picture order count
164167 int ff_evc_derive_poc(const EVCParamSets *ps, const EVCParserSliceHeader *sh,
libavcodec/evc_parser.c+190−32
@@ -25,6 +25,180 @@ · 24 unmodified lines
2525 #include "evc.h"
2626 #include "evc_parse.h"
2727
28+#define NUM_CHROMA_FORMATS 4 // @see ISO_IEC_23094-1 section 6.2 table 2
29+
30+static const enum AVPixelFormat pix_fmts_8bit[NUM_CHROMA_FORMATS] = {
31+ AV_PIX_FMT_GRAY8, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P
32+};
33+
34+static const enum AVPixelFormat pix_fmts_9bit[NUM_CHROMA_FORMATS] = {
35+ AV_PIX_FMT_GRAY9, AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9
36+};
37+
38+static const enum AVPixelFormat pix_fmts_10bit[NUM_CHROMA_FORMATS] = {
39+ AV_PIX_FMT_GRAY10, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10
40+};
41+
42+static const enum AVPixelFormat pix_fmts_12bit[NUM_CHROMA_FORMATS] = {
43+ AV_PIX_FMT_GRAY12, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12
44+};
45+
46+static const enum AVPixelFormat pix_fmts_14bit[NUM_CHROMA_FORMATS] = {
47+ AV_PIX_FMT_GRAY14, AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14
48+};
49+
50+static const enum AVPixelFormat pix_fmts_16bit[NUM_CHROMA_FORMATS] = {
51+ AV_PIX_FMT_GRAY16, AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16
52+};
53+
54+static int parse_nal_unit(AVCodecParserContext *s, AVCodecContext *avctx,
55+ const uint8_t *buf, int buf_size)
56+{
57+ EVCParserContext *ctx = s->priv_data;
58+ int nalu_type, tid;
59+
60+ if (buf_size <= 0) {
61+ av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size: (%d)\n", buf_size);
62+ return AVERROR_INVALIDDATA;
63+ }
64+
65+ // @see ISO_IEC_23094-1_2020, 7.4.2.2 NAL unit header semantic (Table 4 - NAL unit type codes and NAL unit type classes)
66+ // @see enum EVCNALUnitType in evc.h
67+ nalu_type = evc_get_nalu_type(buf, buf_size, avctx);
68+ if (nalu_type < EVC_NOIDR_NUT || nalu_type > EVC_UNSPEC_NUT62) {
69+ av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit type: (%d)\n", nalu_type);
70+ return AVERROR_INVALIDDATA;
71+ }
72+
73+ tid = ff_evc_get_temporal_id(buf, buf_size, avctx);
74+ if (tid < 0) {
75+ av_log(avctx, AV_LOG_ERROR, "Invalid temporial id: (%d)\n", tid);
76+ return AVERROR_INVALIDDATA;
77+ }
78+
79+ buf += EVC_NALU_HEADER_SIZE;
80+ buf_size -= EVC_NALU_HEADER_SIZE;
81+
82+ switch (nalu_type) {
83+ case EVC_SPS_NUT: {
84+ EVCParserSPS *sps = ff_evc_parse_sps(&ctx->ps, buf, buf_size);
85+ if (!sps) {
86+ av_log(avctx, AV_LOG_ERROR, "SPS parsing error\n");
87+ return AVERROR_INVALIDDATA;
88+ }
89+ break;
90+ }
91+ case EVC_PPS_NUT: {
92+ EVCParserPPS *pps = ff_evc_parse_pps(&ctx->ps, buf, buf_size);
93+ if (!pps) {
94+ av_log(avctx, AV_LOG_ERROR, "PPS parsing error\n");
95+ return AVERROR_INVALIDDATA;
96+ }
97+ break;
98+ }
99+ case EVC_IDR_NUT: // Coded slice of a IDR or non-IDR picture
100+ case EVC_NOIDR_NUT: {
101+ const EVCParserPPS *pps;
102+ const EVCParserSPS *sps;
103+ EVCParserSliceHeader sh;
104+ int bit_depth;
105+ int ret;
106+
107+ ret = ff_evc_parse_slice_header(&sh, &ctx->ps, nalu_type, buf, buf_size);
108+ if (ret < 0) {
109+ av_log(avctx, AV_LOG_ERROR, "Slice header parsing error\n");
110+ return ret;
111+ }
112+
113+ pps = ctx->ps.pps[sh.slice_pic_parameter_set_id];
114+ sps = ctx->ps.sps[pps->pps_seq_parameter_set_id];
115+ av_assert0(sps && pps);
116+
117+ s->coded_width = sps->pic_width_in_luma_samples;
118+ s->coded_height = sps->pic_height_in_luma_samples;
119+
120+ if (sps->picture_cropping_flag) {
121+ s->width = sps->pic_width_in_luma_samples - sps->picture_crop_left_offset - sps->picture_crop_right_offset;
122+ s->height = sps->pic_height_in_luma_samples - sps->picture_crop_top_offset - sps->picture_crop_bottom_offset;
123+ } else {
124+ s->width = sps->pic_width_in_luma_samples;
125+ s->height = sps->pic_height_in_luma_samples;
126+ }
127+
128+ switch (sh.slice_type) {
129+ case EVC_SLICE_TYPE_B: {
130+ s->pict_type = AV_PICTURE_TYPE_B;
131+ break;
132+ }
133+ case EVC_SLICE_TYPE_P: {
134+ s->pict_type = AV_PICTURE_TYPE_P;
135+ break;
136+ }
137+ case EVC_SLICE_TYPE_I: {
138+ s->pict_type = AV_PICTURE_TYPE_I;
139+ break;
140+ }
141+ default: {
142+ s->pict_type = AV_PICTURE_TYPE_NONE;
143+ }
144+ }
145+
146+ avctx->profile = sps->profile_idc;
147+
148+ if (sps->vui_parameters_present_flag && sps->vui_parameters.timing_info_present_flag) {
149+ int64_t num = sps->vui_parameters.num_units_in_tick;
150+ int64_t den = sps->vui_parameters.time_scale;
151+ if (num != 0 && den != 0)
152+ av_reduce(&avctx->framerate.den, &avctx->framerate.num, num, den, 1 << 30);
153+ } else
154+ avctx->framerate = (AVRational) { 0, 1 };
155+
156+ bit_depth = sps->bit_depth_chroma_minus8 + 8;
157+ s->format = AV_PIX_FMT_NONE;
158+
159+ switch (bit_depth) {
160+ case 8:
161+ s->format = pix_fmts_8bit[sps->chroma_format_idc];
162+ break;
163+ case 9:
164+ s->format = pix_fmts_9bit[sps->chroma_format_idc];
165+ break;
166+ case 10:
167+ s->format = pix_fmts_10bit[sps->chroma_format_idc];
168+ break;
169+ case 12:
170+ s->format = pix_fmts_12bit[sps->chroma_format_idc];
171+ break;
172+ case 14:
173+ s->format = pix_fmts_14bit[sps->chroma_format_idc];
174+ break;
175+ case 16:
176+ s->format = pix_fmts_16bit[sps->chroma_format_idc];
177+ break;
178+ }
179+
180+ s->key_frame = (nalu_type == EVC_IDR_NUT) ? 1 : 0;
181+
182+ // POC (picture order count of the current picture) derivation
183+ // @see ISO/IEC 23094-1:2020(E) 8.3.1 Decoding process for picture order count
184+ ret = ff_evc_derive_poc(&ctx->ps, &sh, &ctx->poc, nalu_type, tid);
185+ if (ret < 0)
186+ return ret;
187+
188+ s->output_picture_number = ctx->poc.PicOrderCntVal;
189+
190+ break;
191+ }
192+ case EVC_SEI_NUT: // Supplemental Enhancement Information
193+ case EVC_APS_NUT: // Adaptation parameter set
194+ case EVC_FD_NUT: // Filler data
195+ default:
196+ break;
197+ }
198+
199+ return 0;
200+}
201+
28202 /**
29203 * Parse NAL units of found picture and decode some basic information.
30204 *
@@ -35,13 +209,13 @@ · 4 unmodified lines
35209 */
36210 static int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
37211 {
38− EVCParserContext *ctx = s->priv_data;
39212 const uint8_t *data = buf;
40213 int data_size = buf_size;
41214 int bytes_read = 0;
42− int nalu_size = 0;
43215
44216 while (data_size > 0) {
217+ int nalu_size = 0;
218+ int ret;
45219
46220 // Buffer size is not enough for buffer to store NAL unit 4-bytes prefix (length)
47221 if (data_size < EVC_NALU_LENGTH_PREFIX_SIZE)
@@ -57,31 +231,12 @@ static int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx, const · 9 unmodified lines
57231 if (data_size < nalu_size)
58232 return AVERROR_INVALIDDATA;
59233
60− if (ff_evc_parse_nal_unit(ctx, data, nalu_size, avctx) != 0) {
234+ ret = parse_nal_unit(s, avctx, data, nalu_size);
235+ if (ret < 0) {
61236 av_log(avctx, AV_LOG_ERROR, "Parsing of NAL unit failed\n");
62237 return AVERROR_INVALIDDATA;
63238 }
64239
65− if(ctx->nalu_type == EVC_SPS_NUT) {
66−
67− s->coded_width = ctx->coded_width;
68− s->coded_height = ctx->coded_height;
69− s->width = ctx->width;
70− s->height = ctx->height;
71−
72− s->format = ctx->format;
73−
74− avctx->framerate = ctx->framerate;
75− avctx->profile = ctx->profile;
76−
77− } else if(ctx->nalu_type == EVC_NOIDR_NUT || ctx->nalu_type == EVC_IDR_NUT) {
78−
79− s->pict_type = ctx->pict_type;
80− s->key_frame = ctx->key_frame;
81− s->output_picture_number = ctx->output_picture_number;
82−
83− }
84−
85240 data += nalu_size;
86241 data_size -= nalu_size;
87242 }
@@ -90,8 +245,10 @@ static int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx, const · 2 unmodified lines
90245
91246 // Decoding nal units from evcC (EVCDecoderConfigurationRecord)
92247 // @see @see ISO/IEC 14496-15:2021 Coding of audio-visual objects - Part 15: section 12.3.3.2
93−static int decode_extradata(EVCParserContext *ctx, const uint8_t *data, int size, void *logctx)
248+static int decode_extradata(AVCodecParserContext *s, AVCodecContext *avctx)
94249 {
250+ const uint8_t *data = avctx->extradata;
251+ int size = avctx->extradata_size;
95252 int ret = 0;
96253 GetByteContext gb;
97254
@@ -108,7 +265,7 @@ static int decode_extradata(EVCParserContext *ctx, const uint8_t *data, int size · 10 unmodified lines
108265 // The value of this field shall be one of 0, 1, or 3 corresponding to a length encoded with 1, 2, or 4 bytes, respectively.
109266
110267 if (bytestream2_get_bytes_left(&gb) < 18) {
111− av_log(logctx, AV_LOG_ERROR, "evcC %d too short\n", size);
268+ av_log(avctx, AV_LOG_ERROR, "evcC %d too short\n", size);
112269 return AVERROR_INVALIDDATA;
113270 }
114271
@@ -121,7 +278,7 @@ static int decode_extradata(EVCParserContext *ctx, const uint8_t *data, int size · 6 unmodified lines
121278 if( nalu_length_field_size != 1 &&
122279 nalu_length_field_size != 2 &&
123280 nalu_length_field_size != 4 ) {
124− av_log(logctx, AV_LOG_ERROR, "The length in bytes of the NALUnitLenght field in a EVC video stream has unsupported value of %d\n", nalu_length_field_size);
281+ av_log(avctx, AV_LOG_ERROR, "The length in bytes of the NALUnitLenght field in a EVC video stream has unsupported value of %d\n", nalu_length_field_size);
125282 return AVERROR_INVALIDDATA;
126283 }
127284
@@ -142,7 +299,7 @@ static int decode_extradata(EVCParserContext *ctx, const uint8_t *data, int size · 14 unmodified lines
142299 int nal_unit_length = bytestream2_get_be16(&gb);
143300
144301 if (bytestream2_get_bytes_left(&gb) < nal_unit_length) {
145− av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit size in extradata.\n");
302+ av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size in extradata.\n");
146303 return AVERROR_INVALIDDATA;
147304 }
148305
@@ -150,8 +307,8 @@ static int decode_extradata(EVCParserContext *ctx, const uint8_t *data, int size · 1 unmodified line
150307 nal_unit_type == EVC_PPS_NUT ||
151308 nal_unit_type == EVC_APS_NUT ||
152309 nal_unit_type == EVC_SEI_NUT ) {
153− if (ff_evc_parse_nal_unit(ctx, gb.buffer, nal_unit_length, logctx) != 0) {
154− av_log(logctx, AV_LOG_ERROR, "Parsing of NAL unit failed\n");
310+ if (parse_nal_unit(s, avctx, gb.buffer, nal_unit_length) != 0) {
311+ av_log(avctx, AV_LOG_ERROR, "Parsing of NAL unit failed\n");
155312 return AVERROR_INVALIDDATA;
156313 }
157314 }
@@ -173,8 +330,11 @@ static int evc_parse(AVCodecParserContext *s, AVCodecContext *avctx, · 15 unmodified lines
173330 int ret;
174331 EVCParserContext *ctx = s->priv_data;
175332
333+ s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
334+ s->key_frame = 0;
335+
176336 if (avctx->extradata && !ctx->parsed_extradata) {
177− decode_extradata(ctx, avctx->extradata, avctx->extradata_size, avctx);
337+ decode_extradata(s, avctx);
178338 ctx->parsed_extradata = 1;
179339 }
180340
@@ -187,8 +347,6 @@ static int evc_parse(AVCodecParserContext *s, AVCodecContext *avctx, · 6 unmodified lines
187347 return buf_size;
188348 }
189349
190− s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
191−
192350 // poutbuf contains just one Access Unit
193351 *poutbuf = buf;
194352 *poutbuf_size = buf_size;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t