Browse code

avcodec/hevc_parser: Reenable the old parser under ADVANCED_PARSER define

Feel free to use either, they both work but the old one provides
more information but needs the hevc decoder

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>

Michael Niedermayer authored on 2015/07/13 09:07:15
Showing 1 changed files
... ...
@@ -30,6 +30,8 @@
30 30
 
31 31
 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
32 32
 
33
+#define ADVANCED_PARSER
34
+
33 35
 typedef struct HEVCParserContext {
34 36
     ParseContext pc;
35 37
 
... ...
@@ -37,6 +39,10 @@ typedef struct HEVCParserContext {
37 37
     HEVCParamSets ps;
38 38
 
39 39
     int parsed_extradata;
40
+
41
+#ifdef ADVANCED_PARSER
42
+    HEVCContext h;
43
+#endif
40 44
 } HEVCParserContext;
41 45
 
42 46
 static int hevc_parse_slice_header(AVCodecParserContext *s, HEVCNAL *nal,
... ...
@@ -75,6 +81,7 @@ static int hevc_parse_slice_header(AVCodecParserContext *s, HEVCNAL *nal,
75 75
     return 0;
76 76
 }
77 77
 
78
+#ifndef ADVANCED_PARSER
78 79
 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
79 80
                            int buf_size, AVCodecContext *avctx)
80 81
 {
... ...
@@ -114,6 +121,7 @@ static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
114 114
 
115 115
     return 0;
116 116
 }
117
+#endif
117 118
 
118 119
 /**
119 120
  * Find the end of the current frame in the bitstream.
... ...
@@ -166,180 +174,190 @@ static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
166 166
  * @param buf buffer with field/frame data.
167 167
  * @param buf_size size of the buffer.
168 168
  */
169
-// static inline int parse_nal_unitsX(AVCodecParserContext *s, AVCodecContext *avctx,
170
-//                       const uint8_t *buf, int buf_size)
171
-// {
172
-//     HEVCContext   *h  = &((HEVCParseContext *)s->priv_data)->h;
173
-//     GetBitContext *gb = &h->HEVClc->gb;
174
-//     SliceHeader   *sh = &h->sh;
175
-//     HEVCParamSets *ps = &h->ps;
176
-//     HEVCPacket   *pkt = &h->pkt;
177
-//     const uint8_t *buf_end = buf + buf_size;
178
-//     int state = -1, i;
179
-//     HEVCNAL *nal;
180
-//
181
-//     /* set some sane default values */
182
-//     s->pict_type         = AV_PICTURE_TYPE_I;
183
-//     s->key_frame         = 0;
184
-//     s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
185
-//
186
-//     h->avctx = avctx;
187
-//
188
-//     if (!buf_size)
189
-//         return 0;
190
-//
191
-//     if (pkt->nals_allocated < 1) {
192
-//         HEVCNAL *tmp = av_realloc_array(pkt->nals, 1, sizeof(*tmp));
193
-//         if (!tmp)
194
-//             return AVERROR(ENOMEM);
195
-//         pkt->nals = tmp;
196
-//         memset(pkt->nals, 0, sizeof(*tmp));
197
-//         pkt->nals_allocated = 1;
198
-//     }
199
-//
200
-//     nal = &pkt->nals[0];
201
-//
202
-//     for (;;) {
203
-//         int src_length, consumed;
204
-//         buf = avpriv_find_start_code(buf, buf_end, &state);
205
-//         if (--buf + 2 >= buf_end)
206
-//             break;
207
-//         src_length = buf_end - buf;
208
-//
209
-//         h->nal_unit_type = (*buf >> 1) & 0x3f;
210
-//         h->temporal_id   = (*(buf + 1) & 0x07) - 1;
211
-//         if (h->nal_unit_type <= NAL_CRA_NUT) {
212
-//             // Do not walk the whole buffer just to decode slice segment header
213
-//             if (src_length > 20)
214
-//                 src_length = 20;
215
-//         }
216
-//
217
-//         consumed = ff_hevc_extract_rbsp(h, buf, src_length, nal);
218
-//         if (consumed < 0)
219
-//             return consumed;
220
-//
221
-//         init_get_bits8(gb, nal->data + 2, nal->size);
222
-//         switch (h->nal_unit_type) {
223
-//         case NAL_VPS:
224
-//             ff_hevc_decode_nal_vps(gb, avctx, ps);
225
-//             break;
226
-//         case NAL_SPS:
227
-//             ff_hevc_decode_nal_sps(gb, avctx, ps, h->apply_defdispwin);
228
-//             break;
229
-//         case NAL_PPS:
230
-//             ff_hevc_decode_nal_pps(gb, avctx, ps);
231
-//             break;
232
-//         case NAL_SEI_PREFIX:
233
-//         case NAL_SEI_SUFFIX:
234
-//             ff_hevc_decode_nal_sei(h);
235
-//             break;
236
-//         case NAL_TRAIL_N:
237
-//         case NAL_TRAIL_R:
238
-//         case NAL_TSA_N:
239
-//         case NAL_TSA_R:
240
-//         case NAL_STSA_N:
241
-//         case NAL_STSA_R:
242
-//         case NAL_RADL_N:
243
-//         case NAL_RADL_R:
244
-//         case NAL_RASL_N:
245
-//         case NAL_RASL_R:
246
-//         case NAL_BLA_W_LP:
247
-//         case NAL_BLA_W_RADL:
248
-//         case NAL_BLA_N_LP:
249
-//         case NAL_IDR_W_RADL:
250
-//         case NAL_IDR_N_LP:
251
-//         case NAL_CRA_NUT:
252
-//             sh->first_slice_in_pic_flag = get_bits1(gb);
253
-//             s->picture_structure = h->picture_struct;
254
-//             s->field_order = h->picture_struct;
255
-//
256
-//             if (IS_IRAP(h)) {
257
-//                 s->key_frame = 1;
258
-//                 sh->no_output_of_prior_pics_flag = get_bits1(gb);
259
-//             }
260
-//
261
-//             sh->pps_id = get_ue_golomb(gb);
262
-//             if (sh->pps_id >= MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
263
-//                 av_log(h->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
264
-//                 return AVERROR_INVALIDDATA;
265
-//             }
266
-//             ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
267
-//
268
-//             if (ps->pps->sps_id >= MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
269
-//                 av_log(h->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
270
-//                 return AVERROR_INVALIDDATA;
271
-//             }
272
-//             if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
273
-//                 ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
274
-//                 ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
275
-//             }
276
-//
277
-//             if (!sh->first_slice_in_pic_flag) {
278
-//                 int slice_address_length;
279
-//
280
-//                 if (ps->pps->dependent_slice_segments_enabled_flag)
281
-//                     sh->dependent_slice_segment_flag = get_bits1(gb);
282
-//                 else
283
-//                     sh->dependent_slice_segment_flag = 0;
284
-//
285
-//                 slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
286
-//                                                       ps->sps->ctb_height);
287
-//                 sh->slice_segment_addr = slice_address_length ? get_bits(gb, slice_address_length) : 0;
288
-//                 if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
289
-//                     av_log(h->avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
290
-//                            sh->slice_segment_addr);
291
-//                     return AVERROR_INVALIDDATA;
292
-//                 }
293
-//             } else
294
-//                 sh->dependent_slice_segment_flag = 0;
295
-//
296
-//             if (sh->dependent_slice_segment_flag)
297
-//                 break;
298
-//
299
-//             for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
300
-//                 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
301
-//
302
-//             sh->slice_type = get_ue_golomb(gb);
303
-//             if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
304
-//                   sh->slice_type == B_SLICE)) {
305
-//                 av_log(h->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
306
-//                        sh->slice_type);
307
-//                 return AVERROR_INVALIDDATA;
308
-//             }
309
-//             s->pict_type = sh->slice_type == B_SLICE ? AV_PICTURE_TYPE_B :
310
-//                            sh->slice_type == P_SLICE ? AV_PICTURE_TYPE_P :
311
-//                                                        AV_PICTURE_TYPE_I;
312
-//
313
-//             if (ps->pps->output_flag_present_flag)
314
-//                 sh->pic_output_flag = get_bits1(gb);
315
-//
316
-//             if (ps->sps->separate_colour_plane_flag)
317
-//                 sh->colour_plane_id = get_bits(gb, 2);
318
-//
319
-//             if (!IS_IDR(h)) {
320
-//                 sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
321
-//                 s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
322
-//             } else
323
-//                 s->output_picture_number = h->poc = 0;
324
-//
325
-//             if (h->temporal_id == 0 &&
326
-//                 h->nal_unit_type != NAL_TRAIL_N &&
327
-//                 h->nal_unit_type != NAL_TSA_N &&
328
-//                 h->nal_unit_type != NAL_STSA_N &&
329
-//                 h->nal_unit_type != NAL_RADL_N &&
330
-//                 h->nal_unit_type != NAL_RASL_N &&
331
-//                 h->nal_unit_type != NAL_RADL_R &&
332
-//                 h->nal_unit_type != NAL_RASL_R)
333
-//                 h->pocTid0 = h->poc;
334
-//
335
-//             return 0; /* no need to evaluate the rest */
336
-//         }
337
-//         buf += consumed;
338
-//     }
339
-//     /* didn't find a picture! */
340
-//     av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
341
-//     return -1;
342
-// }
169
+#ifdef ADVANCED_PARSER
170
+static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
171
+                           int buf_size, AVCodecContext *avctx)
172
+{
173
+    HEVCParserContext *ctx = s->priv_data;
174
+    HEVCContext       *h   = &ctx->h;
175
+    GetBitContext      *gb;
176
+    SliceHeader        *sh = &h->sh;
177
+    HEVCParamSets *ps = &h->ps;
178
+    HEVCPacket   *pkt = &h->pkt;
179
+    const uint8_t *buf_end = buf + buf_size;
180
+    int state = -1, i;
181
+    HEVCNAL *nal;
182
+
183
+    if (!h->HEVClc)
184
+        h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
185
+    if (!h->HEVClc)
186
+        return AVERROR(ENOMEM);
187
+
188
+    gb = &h->HEVClc->gb;
189
+
190
+    /* set some sane default values */
191
+    s->pict_type         = AV_PICTURE_TYPE_I;
192
+    s->key_frame         = 0;
193
+    s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
194
+
195
+    h->avctx = avctx;
196
+
197
+    if (!buf_size)
198
+        return 0;
199
+
200
+    if (pkt->nals_allocated < 1) {
201
+        HEVCNAL *tmp = av_realloc_array(pkt->nals, 1, sizeof(*tmp));
202
+        if (!tmp)
203
+            return AVERROR(ENOMEM);
204
+        pkt->nals = tmp;
205
+        memset(pkt->nals, 0, sizeof(*tmp));
206
+        pkt->nals_allocated = 1;
207
+    }
208
+
209
+    nal = &pkt->nals[0];
210
+
211
+    for (;;) {
212
+        int src_length, consumed;
213
+        buf = avpriv_find_start_code(buf, buf_end, &state);
214
+        if (--buf + 2 >= buf_end)
215
+            break;
216
+        src_length = buf_end - buf;
217
+
218
+        h->nal_unit_type = (*buf >> 1) & 0x3f;
219
+        h->temporal_id   = (*(buf + 1) & 0x07) - 1;
220
+        if (h->nal_unit_type <= NAL_CRA_NUT) {
221
+            // Do not walk the whole buffer just to decode slice segment header
222
+            if (src_length > 20)
223
+                src_length = 20;
224
+        }
225
+
226
+        consumed = ff_hevc_extract_rbsp(NULL, buf, src_length, nal);
227
+        if (consumed < 0)
228
+            return consumed;
229
+
230
+        init_get_bits8(gb, nal->data + 2, nal->size);
231
+        switch (h->nal_unit_type) {
232
+        case NAL_VPS:
233
+            ff_hevc_decode_nal_vps(gb, avctx, ps);
234
+            break;
235
+        case NAL_SPS:
236
+            ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
237
+            break;
238
+        case NAL_PPS:
239
+            ff_hevc_decode_nal_pps(gb, avctx, ps);
240
+            break;
241
+        case NAL_SEI_PREFIX:
242
+        case NAL_SEI_SUFFIX:
243
+            ff_hevc_decode_nal_sei(h);
244
+            break;
245
+        case NAL_TRAIL_N:
246
+        case NAL_TRAIL_R:
247
+        case NAL_TSA_N:
248
+        case NAL_TSA_R:
249
+        case NAL_STSA_N:
250
+        case NAL_STSA_R:
251
+        case NAL_RADL_N:
252
+        case NAL_RADL_R:
253
+        case NAL_RASL_N:
254
+        case NAL_RASL_R:
255
+        case NAL_BLA_W_LP:
256
+        case NAL_BLA_W_RADL:
257
+        case NAL_BLA_N_LP:
258
+        case NAL_IDR_W_RADL:
259
+        case NAL_IDR_N_LP:
260
+        case NAL_CRA_NUT:
261
+            sh->first_slice_in_pic_flag = get_bits1(gb);
262
+            s->picture_structure = h->picture_struct;
263
+            s->field_order = h->picture_struct;
264
+
265
+            if (IS_IRAP(h)) {
266
+                s->key_frame = 1;
267
+                sh->no_output_of_prior_pics_flag = get_bits1(gb);
268
+            }
269
+
270
+            sh->pps_id = get_ue_golomb(gb);
271
+            if (sh->pps_id >= MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
272
+                av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
273
+                return AVERROR_INVALIDDATA;
274
+            }
275
+            ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
276
+
277
+            if (ps->pps->sps_id >= MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
278
+                av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
279
+                return AVERROR_INVALIDDATA;
280
+            }
281
+            if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
282
+                ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
283
+                ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
284
+            }
285
+
286
+            if (!sh->first_slice_in_pic_flag) {
287
+                int slice_address_length;
288
+
289
+                if (ps->pps->dependent_slice_segments_enabled_flag)
290
+                    sh->dependent_slice_segment_flag = get_bits1(gb);
291
+                else
292
+                    sh->dependent_slice_segment_flag = 0;
293
+
294
+                slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
295
+                                                      ps->sps->ctb_height);
296
+                sh->slice_segment_addr = slice_address_length ? get_bits(gb, slice_address_length) : 0;
297
+                if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
298
+                    av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
299
+                           sh->slice_segment_addr);
300
+                    return AVERROR_INVALIDDATA;
301
+                }
302
+            } else
303
+                sh->dependent_slice_segment_flag = 0;
304
+
305
+            if (sh->dependent_slice_segment_flag)
306
+                break;
307
+
308
+            for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
309
+                skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
310
+
311
+            sh->slice_type = get_ue_golomb(gb);
312
+            if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
313
+                  sh->slice_type == B_SLICE)) {
314
+                av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
315
+                       sh->slice_type);
316
+                return AVERROR_INVALIDDATA;
317
+            }
318
+            s->pict_type = sh->slice_type == B_SLICE ? AV_PICTURE_TYPE_B :
319
+                           sh->slice_type == P_SLICE ? AV_PICTURE_TYPE_P :
320
+                                                       AV_PICTURE_TYPE_I;
321
+
322
+            if (ps->pps->output_flag_present_flag)
323
+                sh->pic_output_flag = get_bits1(gb);
324
+
325
+            if (ps->sps->separate_colour_plane_flag)
326
+                sh->colour_plane_id = get_bits(gb, 2);
327
+
328
+            if (!IS_IDR(h)) {
329
+                sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
330
+                s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
331
+            } else
332
+                s->output_picture_number = h->poc = 0;
333
+
334
+            if (h->temporal_id == 0 &&
335
+                h->nal_unit_type != NAL_TRAIL_N &&
336
+                h->nal_unit_type != NAL_TSA_N &&
337
+                h->nal_unit_type != NAL_STSA_N &&
338
+                h->nal_unit_type != NAL_RADL_N &&
339
+                h->nal_unit_type != NAL_RASL_N &&
340
+                h->nal_unit_type != NAL_RADL_R &&
341
+                h->nal_unit_type != NAL_RASL_R)
342
+                h->pocTid0 = h->poc;
343
+
344
+            return 0; /* no need to evaluate the rest */
345
+        }
346
+        buf += consumed;
347
+    }
348
+    /* didn't find a picture! */
349
+    av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
350
+    return -1;
351
+}
352
+#endif
343 353
 
344 354
 static int hevc_parse(AVCodecParserContext *s,
345 355
                       AVCodecContext *avctx,
... ...
@@ -400,6 +418,11 @@ static void hevc_parser_close(AVCodecParserContext *s)
400 400
     HEVCParserContext *ctx = s->priv_data;
401 401
     int i;
402 402
 
403
+#ifdef ADVANCED_PARSER
404
+    HEVCContext  *h  = &ctx->h;
405
+    av_freep(&h->HEVClc);
406
+#endif
407
+
403 408
     for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
404 409
         av_buffer_unref(&ctx->ps.vps_list[i]);
405 410
     for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)