Browse code

hevc_parser: Set pict_type, key_frame and output_picture_number.

Conflicts:
libavcodec/hevc.c

Yusuke Nakamura authored on 2013/10/27 19:07:43
Showing 3 changed files
... ...
@@ -2192,8 +2192,8 @@ static int decode_nal_unit(HEVCContext *s, const uint8_t *nal, int length)
2192 2192
 
2193 2193
 /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
2194 2194
    between these functions would be nice. */
2195
-static int extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
2196
-                        HEVCNAL *nal)
2195
+int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
2196
+                         HEVCNAL *nal)
2197 2197
 {
2198 2198
     int i, si, di;
2199 2199
     uint8_t *dst;
... ...
@@ -2279,7 +2279,8 @@ static int extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
2279 2279
                     if (!s->skipped_bytes_pos)
2280 2280
                         return AVERROR(ENOMEM);
2281 2281
                 }
2282
-                s->skipped_bytes_pos[s->skipped_bytes-1] = di - 1;
2282
+                if (s->skipped_bytes_pos)
2283
+                    s->skipped_bytes_pos[s->skipped_bytes-1] = di - 1;
2283 2284
                 continue;
2284 2285
             } else // next start code
2285 2286
                 goto nsc;
... ...
@@ -2363,7 +2364,7 @@ static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
2363 2363
         s->skipped_bytes_pos = s->skipped_bytes_pos_nal[s->nb_nals];
2364 2364
         nal = &s->nals[s->nb_nals];
2365 2365
 
2366
-        consumed = extract_rbsp(s, buf, extract_length, nal);
2366
+        consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
2367 2367
 
2368 2368
         s->skipped_bytes_nal[s->nb_nals] = s->skipped_bytes;
2369 2369
         s->skipped_bytes_pos_size_nal[s->nb_nals] = s->skipped_bytes_pos_size;
... ...
@@ -872,6 +872,9 @@ int ff_hevc_decode_nal_sps(HEVCContext *s);
872 872
 int ff_hevc_decode_nal_pps(HEVCContext *s);
873 873
 int ff_hevc_decode_nal_sei(HEVCContext *s);
874 874
 
875
+int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
876
+                         HEVCNAL *nal);
877
+
875 878
 /**
876 879
  * Mark all frames in DPB as unused for reference.
877 880
  */
... ...
@@ -23,9 +23,15 @@
23 23
 #include "libavutil/common.h"
24 24
 #include "parser.h"
25 25
 #include "hevc.h"
26
+#include "golomb.h"
26 27
 
27 28
 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
28 29
 
30
+typedef struct HEVCParseContext {
31
+    HEVCContext  h;
32
+    ParseContext pc;
33
+} HEVCParseContext;
34
+
29 35
 /**
30 36
  * Find the end of the current frame in the bitstream.
31 37
  * @return the position of the first byte of the next frame, or END_NOT_FOUND
... ...
@@ -33,7 +39,7 @@
33 33
 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf, int buf_size)
34 34
 {
35 35
     int i;
36
-    ParseContext *pc = s->priv_data;
36
+    ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
37 37
 
38 38
     for (i = 0; i < buf_size; i++) {
39 39
         int nut;
... ...
@@ -57,7 +63,6 @@ static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf, int
57 57
             if (first_slice_segment_in_pic_flag) {
58 58
                 if (!pc->frame_start_found) {
59 59
                     pc->frame_start_found = 1;
60
-                    s->key_frame = nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT;
61 60
                 } else { // First slice of next frame found
62 61
                     pc->frame_start_found = 0;
63 62
                     return i - 5;
... ...
@@ -69,13 +74,193 @@ static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf, int
69 69
     return END_NOT_FOUND;
70 70
 }
71 71
 
72
+/**
73
+ * Parse NAL units of found picture and decode some basic information.
74
+ *
75
+ * @param s parser context.
76
+ * @param avctx codec context.
77
+ * @param buf buffer with field/frame data.
78
+ * @param buf_size size of the buffer.
79
+ */
80
+static inline int parse_nal_units(AVCodecParserContext *s,
81
+                                  AVCodecContext *avctx,
82
+                                  const uint8_t *buf, int buf_size)
83
+{
84
+    HEVCContext   *h  = &((HEVCParseContext *)s->priv_data)->h;
85
+    GetBitContext *gb = &h->HEVClc->gb;
86
+    SliceHeader   *sh = &h->sh;
87
+    const uint8_t *buf_end = buf + buf_size;
88
+    int state = -1, i;
89
+    HEVCNAL *nal;
90
+
91
+    /* set some sane default values */
92
+    s->pict_type         = AV_PICTURE_TYPE_I;
93
+    s->key_frame         = 0;
94
+    s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
95
+
96
+    h->avctx = avctx;
97
+
98
+    if (!buf_size)
99
+        return 0;
100
+
101
+    if (h->nals_allocated < 1) {
102
+        HEVCNAL *tmp = av_realloc_array(h->nals, 1, sizeof(*tmp));
103
+        if (!tmp)
104
+            return AVERROR(ENOMEM);
105
+        h->nals = tmp;
106
+        memset(h->nals, 0, sizeof(*tmp));
107
+        h->nals_allocated = 1;
108
+    }
109
+
110
+    nal = &h->nals[0];
111
+
112
+    for (;;) {
113
+        int src_length, consumed;
114
+        buf = avpriv_find_start_code(buf, buf_end, &state);
115
+        if (--buf + 2 >= buf_end)
116
+            break;
117
+        src_length = buf_end - buf;
118
+
119
+        h->nal_unit_type = (*buf >> 1) & 0x3f;
120
+        h->temporal_id   = (*(buf + 1) & 0x07) - 1;
121
+        if (h->nal_unit_type <= NAL_CRA_NUT) {
122
+            // Do not walk the whole buffer just to decode slice segment header
123
+            if (src_length > 20)
124
+                src_length = 20;
125
+        }
126
+
127
+        consumed = ff_hevc_extract_rbsp(h, buf, src_length, nal);
128
+        if (consumed < 0)
129
+            return consumed;
130
+
131
+        init_get_bits8(gb, nal->data + 2, nal->size);
132
+        switch (h->nal_unit_type) {
133
+        case NAL_VPS:
134
+            ff_hevc_decode_nal_vps(h);
135
+            break;
136
+        case NAL_SPS:
137
+            ff_hevc_decode_nal_sps(h);
138
+            break;
139
+        case NAL_PPS:
140
+            ff_hevc_decode_nal_pps(h);
141
+            break;
142
+        case NAL_SEI_PREFIX:
143
+        case NAL_SEI_SUFFIX:
144
+            ff_hevc_decode_nal_sei(h);
145
+            break;
146
+        case NAL_TRAIL_N:
147
+        case NAL_TRAIL_R:
148
+        case NAL_TSA_N:
149
+        case NAL_TSA_R:
150
+        case NAL_STSA_N:
151
+        case NAL_STSA_R:
152
+        case NAL_RADL_N:
153
+        case NAL_RADL_R:
154
+        case NAL_RASL_N:
155
+        case NAL_RASL_R:
156
+        case NAL_BLA_W_LP:
157
+        case NAL_BLA_W_RADL:
158
+        case NAL_BLA_N_LP:
159
+        case NAL_IDR_W_RADL:
160
+        case NAL_IDR_N_LP:
161
+        case NAL_CRA_NUT:
162
+            sh->first_slice_in_pic_flag = get_bits1(gb);
163
+
164
+            if (h->nal_unit_type >= 16 && h->nal_unit_type <= 23) {
165
+                s->key_frame = 1;
166
+                sh->no_output_of_prior_pics_flag = get_bits1(gb);
167
+            }
168
+
169
+            sh->pps_id = get_ue_golomb(gb);
170
+            if (sh->pps_id >= MAX_PPS_COUNT || !h->pps_list[sh->pps_id]) {
171
+                av_log(h->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
172
+                return AVERROR_INVALIDDATA;
173
+            }
174
+            h->pps = (HEVCPPS*)h->pps_list[sh->pps_id]->data;
175
+
176
+            if (h->pps->sps_id >= MAX_SPS_COUNT || !h->sps_list[h->pps->sps_id]) {
177
+                av_log(h->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", h->pps->sps_id);
178
+                return AVERROR_INVALIDDATA;
179
+            }
180
+            if (h->sps != (HEVCSPS*)h->sps_list[h->pps->sps_id]->data) {
181
+                h->sps = (HEVCSPS*)h->sps_list[h->pps->sps_id]->data;
182
+                h->vps = h->vps_list[h->sps->vps_id];
183
+            }
184
+
185
+            if (!sh->first_slice_in_pic_flag) {
186
+                int slice_address_length;
187
+
188
+                if (h->pps->dependent_slice_segments_enabled_flag)
189
+                    sh->dependent_slice_segment_flag = get_bits1(gb);
190
+                else
191
+                    sh->dependent_slice_segment_flag = 0;
192
+
193
+                slice_address_length = av_ceil_log2_c(h->sps->ctb_width *
194
+                                                      h->sps->ctb_height);
195
+                sh->slice_segment_addr = get_bits(gb, slice_address_length);
196
+                if (sh->slice_segment_addr >= h->sps->ctb_width * h->sps->ctb_height) {
197
+                    av_log(h->avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
198
+                           sh->slice_segment_addr);
199
+                    return AVERROR_INVALIDDATA;
200
+                }
201
+            } else
202
+                sh->dependent_slice_segment_flag = 0;
203
+
204
+            if (sh->dependent_slice_segment_flag)
205
+                break;
206
+
207
+            for (i = 0; i < h->pps->num_extra_slice_header_bits; i++)
208
+                skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
209
+
210
+            sh->slice_type = get_ue_golomb(gb);
211
+            if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
212
+                  sh->slice_type == B_SLICE)) {
213
+                av_log(h->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
214
+                       sh->slice_type);
215
+                return AVERROR_INVALIDDATA;
216
+            }
217
+            s->pict_type = sh->slice_type == B_SLICE ? AV_PICTURE_TYPE_B :
218
+                           sh->slice_type == P_SLICE ? AV_PICTURE_TYPE_P :
219
+                                                       AV_PICTURE_TYPE_I;
220
+
221
+            if (h->pps->output_flag_present_flag)
222
+                sh->pic_output_flag = get_bits1(gb);
223
+
224
+            if (h->sps->separate_colour_plane_flag)
225
+                sh->colour_plane_id = get_bits(gb, 2);
226
+
227
+            if (!IS_IDR(h)) {
228
+                sh->pic_order_cnt_lsb = get_bits(gb, h->sps->log2_max_poc_lsb);
229
+                s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
230
+            } else
231
+                s->output_picture_number = h->poc = 0;
232
+
233
+            if (h->temporal_id == 0 &&
234
+                h->nal_unit_type != NAL_TRAIL_N &&
235
+                h->nal_unit_type != NAL_TSA_N &&
236
+                h->nal_unit_type != NAL_STSA_N &&
237
+                h->nal_unit_type != NAL_RADL_N &&
238
+                h->nal_unit_type != NAL_RASL_N &&
239
+                h->nal_unit_type != NAL_RADL_R &&
240
+                h->nal_unit_type != NAL_RASL_R)
241
+                h->pocTid0 = h->poc;
242
+
243
+            return 0; /* no need to evaluate the rest */
244
+        }
245
+        buf += consumed;
246
+    }
247
+    /* didn't find a picture! */
248
+    av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
249
+    return -1;
250
+}
251
+
72 252
 static int hevc_parse(AVCodecParserContext *s,
73 253
                       AVCodecContext *avctx,
74 254
                       const uint8_t **poutbuf, int *poutbuf_size,
75 255
                       const uint8_t *buf, int buf_size)
76 256
 {
77 257
     int next;
78
-    ParseContext *pc = s->priv_data;
258
+    ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
79 259
 
80 260
     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
81 261
         next = buf_size;
... ...
@@ -88,6 +273,8 @@ static int hevc_parse(AVCodecParserContext *s,
88 88
         }
89 89
     }
90 90
 
91
+    parse_nal_units(s, avctx, buf, buf_size);
92
+
91 93
     *poutbuf = buf;
92 94
     *poutbuf_size = buf_size;
93 95
     return next;
... ...
@@ -116,10 +303,43 @@ static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
116 116
     return 0;
117 117
 }
118 118
 
119
+static int hevc_init(AVCodecParserContext *s)
120
+{
121
+    HEVCContext  *h  = &((HEVCParseContext *)s->priv_data)->h;
122
+    h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
123
+    h->skipped_bytes_pos_size = INT_MAX;
124
+
125
+    return 0;
126
+}
127
+
128
+static void hevc_close(AVCodecParserContext *s)
129
+{
130
+    int i;
131
+    HEVCContext  *h  = &((HEVCParseContext *)s->priv_data)->h;
132
+    ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
133
+
134
+    av_freep(&h->skipped_bytes_pos);
135
+    av_freep(&h->HEVClc);
136
+    av_freep(&pc->buffer);
137
+
138
+    for (i = 0; i < FF_ARRAY_ELEMS(h->vps_list); i++)
139
+        av_freep(&h->vps_list[i]);
140
+    for (i = 0; i < FF_ARRAY_ELEMS(h->sps_list); i++)
141
+        av_buffer_unref(&h->sps_list[i]);
142
+    for (i = 0; i < FF_ARRAY_ELEMS(h->pps_list); i++)
143
+        av_buffer_unref(&h->pps_list[i]);
144
+
145
+    for (i = 0; i < h->nals_allocated; i++)
146
+        av_freep(&h->nals[i].rbsp_buffer);
147
+    av_freep(&h->nals);
148
+    h->nals_allocated = 0;
149
+}
150
+
119 151
 AVCodecParser ff_hevc_parser = {
120 152
     .codec_ids      = { AV_CODEC_ID_HEVC },
121
-    .priv_data_size = sizeof(ParseContext),
153
+    .priv_data_size = sizeof(HEVCParseContext),
154
+    .parser_init    = hevc_init,
122 155
     .parser_parse   = hevc_parse,
123
-    .parser_close   = ff_parse_close,
156
+    .parser_close   = hevc_close,
124 157
     .split          = hevc_split,
125 158
 };