Browse code

avcodec/hevc_parser: move slice header parsing to its own function

Reviewed-by: Hendrik Leppkes <h.leppkes@gmail.com>
Reviewed-by: Aaron Levinson <alevinsn@aracnet.com>
Signed-off-by: James Almer <jamrial@gmail.com>

James Almer authored on 2017/05/02 02:32:07
Showing 1 changed files
... ...
@@ -92,6 +92,122 @@ static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
92 92
     return END_NOT_FOUND;
93 93
 }
94 94
 
95
+static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
96
+                                   AVCodecContext *avctx)
97
+{
98
+    HEVCParserContext *ctx = s->priv_data;
99
+    HEVCParamSets *ps = &ctx->ps;
100
+    HEVCSEIContext *sei = &ctx->sei;
101
+    SliceHeader *sh = &ctx->sh;
102
+    GetBitContext *gb = &nal->gb;
103
+    int i, num = 0, den = 0;
104
+
105
+    sh->first_slice_in_pic_flag = get_bits1(gb);
106
+    s->picture_structure = sei->picture_timing.picture_struct;
107
+    s->field_order = sei->picture_timing.picture_struct;
108
+
109
+    if (IS_IRAP_NAL(nal)) {
110
+        s->key_frame = 1;
111
+        sh->no_output_of_prior_pics_flag = get_bits1(gb);
112
+    }
113
+
114
+    sh->pps_id = get_ue_golomb(gb);
115
+    if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
116
+        av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
117
+        return AVERROR_INVALIDDATA;
118
+    }
119
+    ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
120
+
121
+    if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
122
+        av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
123
+        return AVERROR_INVALIDDATA;
124
+    }
125
+    if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
126
+        ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
127
+        ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
128
+    }
129
+
130
+    s->coded_width  = ps->sps->width;
131
+    s->coded_height = ps->sps->height;
132
+    s->width        = ps->sps->output_width;
133
+    s->height       = ps->sps->output_height;
134
+    s->format       = ps->sps->pix_fmt;
135
+    avctx->profile  = ps->sps->ptl.general_ptl.profile_idc;
136
+    avctx->level    = ps->sps->ptl.general_ptl.level_idc;
137
+
138
+    if (ps->vps->vps_timing_info_present_flag) {
139
+        num = ps->vps->vps_num_units_in_tick;
140
+        den = ps->vps->vps_time_scale;
141
+    } else if (ps->sps->vui.vui_timing_info_present_flag) {
142
+        num = ps->sps->vui.vui_num_units_in_tick;
143
+        den = ps->sps->vui.vui_time_scale;
144
+    }
145
+
146
+    if (num != 0 && den != 0)
147
+        av_reduce(&avctx->framerate.den, &avctx->framerate.num,
148
+                  num, den, 1 << 30);
149
+
150
+    if (!sh->first_slice_in_pic_flag) {
151
+        int slice_address_length;
152
+
153
+        if (ps->pps->dependent_slice_segments_enabled_flag)
154
+            sh->dependent_slice_segment_flag = get_bits1(gb);
155
+        else
156
+            sh->dependent_slice_segment_flag = 0;
157
+
158
+        slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
159
+                                              ps->sps->ctb_height);
160
+        sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
161
+        if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
162
+            av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
163
+                   sh->slice_segment_addr);
164
+            return AVERROR_INVALIDDATA;
165
+        }
166
+    } else
167
+        sh->dependent_slice_segment_flag = 0;
168
+
169
+    if (sh->dependent_slice_segment_flag)
170
+        return 0; /* break; */
171
+
172
+    for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
173
+        skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
174
+
175
+    sh->slice_type = get_ue_golomb(gb);
176
+    if (!(sh->slice_type == HEVC_SLICE_I || sh->slice_type == HEVC_SLICE_P ||
177
+          sh->slice_type == HEVC_SLICE_B)) {
178
+        av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
179
+               sh->slice_type);
180
+        return AVERROR_INVALIDDATA;
181
+    }
182
+    s->pict_type = sh->slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
183
+                   sh->slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
184
+                                               AV_PICTURE_TYPE_I;
185
+
186
+    if (ps->pps->output_flag_present_flag)
187
+        sh->pic_output_flag = get_bits1(gb);
188
+
189
+    if (ps->sps->separate_colour_plane_flag)
190
+        sh->colour_plane_id = get_bits(gb, 2);
191
+
192
+    if (!IS_IDR_NAL(nal)) {
193
+        sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
194
+        s->output_picture_number = ctx->poc = ff_hevc_compute_poc(ps->sps, ctx->pocTid0, sh->pic_order_cnt_lsb, nal->type);
195
+    } else
196
+        s->output_picture_number = ctx->poc = 0;
197
+
198
+    if (nal->temporal_id == 0 &&
199
+        nal->type != HEVC_NAL_TRAIL_N &&
200
+        nal->type != HEVC_NAL_TSA_N &&
201
+        nal->type != HEVC_NAL_STSA_N &&
202
+        nal->type != HEVC_NAL_RADL_N &&
203
+        nal->type != HEVC_NAL_RASL_N &&
204
+        nal->type != HEVC_NAL_RADL_R &&
205
+        nal->type != HEVC_NAL_RASL_R)
206
+        ctx->pocTid0 = ctx->poc;
207
+
208
+    return 1; /* no need to evaluate the rest */
209
+}
210
+
95 211
 /**
96 212
  * Parse NAL units of found picture and decode some basic information.
97 213
  *
... ...
@@ -104,7 +220,6 @@ static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
104 104
                            int buf_size, AVCodecContext *avctx)
105 105
 {
106 106
     HEVCParserContext *ctx = s->priv_data;
107
-    SliceHeader        *sh = &ctx->sh;
108 107
     HEVCParamSets *ps = &ctx->ps;
109 108
     HEVCSEIContext *sei = &ctx->sei;
110 109
     int is_global = buf == avctx->extradata;
... ...
@@ -125,8 +240,6 @@ static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
125 125
     for (i = 0; i < ctx->pkt.nb_nals; i++) {
126 126
         H2645NAL *nal = &ctx->pkt.nals[i];
127 127
         GetBitContext *gb = &nal->gb;
128
-        int num = 0, den = 0;
129
-
130 128
 
131 129
         switch (nal->type) {
132 130
         case HEVC_NAL_VPS:
... ...
@@ -164,110 +277,10 @@ static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
164 164
                 return AVERROR_INVALIDDATA;
165 165
             }
166 166
 
167
-            sh->first_slice_in_pic_flag = get_bits1(gb);
168
-            s->picture_structure = sei->picture_timing.picture_struct;
169
-            s->field_order = sei->picture_timing.picture_struct;
170
-
171
-            if (IS_IRAP_NAL(nal)) {
172
-                s->key_frame = 1;
173
-                sh->no_output_of_prior_pics_flag = get_bits1(gb);
174
-            }
175
-
176
-            sh->pps_id = get_ue_golomb(gb);
177
-            if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
178
-                av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
179
-                return AVERROR_INVALIDDATA;
180
-            }
181
-            ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
182
-
183
-            if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
184
-                av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
185
-                return AVERROR_INVALIDDATA;
186
-            }
187
-            if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
188
-                ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
189
-                ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
190
-            }
191
-
192
-            s->coded_width  = ps->sps->width;
193
-            s->coded_height = ps->sps->height;
194
-            s->width        = ps->sps->output_width;
195
-            s->height       = ps->sps->output_height;
196
-            s->format       = ps->sps->pix_fmt;
197
-            avctx->profile  = ps->sps->ptl.general_ptl.profile_idc;
198
-            avctx->level    = ps->sps->ptl.general_ptl.level_idc;
199
-
200
-            if (ps->vps->vps_timing_info_present_flag) {
201
-                num = ps->vps->vps_num_units_in_tick;
202
-                den = ps->vps->vps_time_scale;
203
-            } else if (ps->sps->vui.vui_timing_info_present_flag) {
204
-                num = ps->sps->vui.vui_num_units_in_tick;
205
-                den = ps->sps->vui.vui_time_scale;
206
-            }
207
-
208
-            if (num != 0 && den != 0)
209
-                av_reduce(&avctx->framerate.den, &avctx->framerate.num,
210
-                          num, den, 1 << 30);
211
-
212
-            if (!sh->first_slice_in_pic_flag) {
213
-                int slice_address_length;
214
-
215
-                if (ps->pps->dependent_slice_segments_enabled_flag)
216
-                    sh->dependent_slice_segment_flag = get_bits1(gb);
217
-                else
218
-                    sh->dependent_slice_segment_flag = 0;
219
-
220
-                slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
221
-                                                      ps->sps->ctb_height);
222
-                sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
223
-                if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
224
-                    av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
225
-                           sh->slice_segment_addr);
226
-                    return AVERROR_INVALIDDATA;
227
-                }
228
-            } else
229
-                sh->dependent_slice_segment_flag = 0;
230
-
231
-            if (sh->dependent_slice_segment_flag)
232
-                break;
233
-
234
-            for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
235
-                skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
236
-
237
-            sh->slice_type = get_ue_golomb(gb);
238
-            if (!(sh->slice_type == HEVC_SLICE_I || sh->slice_type == HEVC_SLICE_P ||
239
-                  sh->slice_type == HEVC_SLICE_B)) {
240
-                av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
241
-                       sh->slice_type);
242
-                return AVERROR_INVALIDDATA;
243
-            }
244
-            s->pict_type = sh->slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
245
-                           sh->slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
246
-                                                       AV_PICTURE_TYPE_I;
247
-
248
-            if (ps->pps->output_flag_present_flag)
249
-                sh->pic_output_flag = get_bits1(gb);
250
-
251
-            if (ps->sps->separate_colour_plane_flag)
252
-                sh->colour_plane_id = get_bits(gb, 2);
253
-
254
-            if (!IS_IDR_NAL(nal)) {
255
-                sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
256
-                s->output_picture_number = ctx->poc = ff_hevc_compute_poc(ps->sps, ctx->pocTid0, sh->pic_order_cnt_lsb, nal->type);
257
-            } else
258
-                s->output_picture_number = ctx->poc = 0;
259
-
260
-            if (nal->temporal_id == 0 &&
261
-                nal->type != HEVC_NAL_TRAIL_N &&
262
-                nal->type != HEVC_NAL_TSA_N &&
263
-                nal->type != HEVC_NAL_STSA_N &&
264
-                nal->type != HEVC_NAL_RADL_N &&
265
-                nal->type != HEVC_NAL_RASL_N &&
266
-                nal->type != HEVC_NAL_RADL_R &&
267
-                nal->type != HEVC_NAL_RASL_R)
268
-                ctx->pocTid0 = ctx->poc;
269
-
270
-            return 0; /* no need to evaluate the rest */
167
+            ret = hevc_parse_slice_header(s, nal, avctx);
168
+            if (ret)
169
+                return ret;
170
+            break;
271 171
         }
272 172
     }
273 173
     /* didn't find a picture! */