* Parse NAL units of found picture and decode some basic information. * * @param s parser context. * @param avctx codec context. * @param buf buffer with field/frame data. * @param buf_size size of the buffer. */
| 260 | * @param buf_size size of the buffer. |
| 261 | */ |
| 262 | static inline int parse_nal_units(AVCodecParserContext *s, |
| 263 | AVCodecContext *avctx, |
| 264 | const uint8_t * const buf, int buf_size) |
| 265 | { |
| 266 | H264ParseContext *p = s->priv_data; |
| 267 | H2645RBSP rbsp = { NULL }; |
| 268 | H2645NAL nal = { NULL }; |
| 269 | int buf_index, next_avc; |
| 270 | unsigned int pps_id; |
| 271 | unsigned int slice_type; |
| 272 | int state = -1, got_reset = 0; |
| 273 | int q264 = buf_size >=4 && !memcmp("Q264", buf, 4); |
| 274 | int field_poc[2]; |
| 275 | int ret; |
| 276 | |
| 277 | /* set some sane default values */ |
| 278 | s->pict_type = AV_PICTURE_TYPE_I; |
| 279 | s->key_frame = 0; |
| 280 | s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN; |
| 281 | |
| 282 | ff_h264_sei_uninit(&p->sei); |
| 283 | p->sei.common.frame_packing.arrangement_cancel_flag = -1; |
| 284 | p->sei.common.unregistered.x264_build = -1; |
| 285 | |
| 286 | if (!buf_size) |
| 287 | return 0; |
| 288 | |
| 289 | av_fast_padded_malloc(&rbsp.rbsp_buffer, &rbsp.rbsp_buffer_alloc_size, buf_size); |
| 290 | if (!rbsp.rbsp_buffer) |
| 291 | return AVERROR(ENOMEM); |
| 292 | |
| 293 | buf_index = 0; |
| 294 | next_avc = p->is_avc ? 0 : buf_size; |
| 295 | for (;;) { |
| 296 | const SPS *sps; |
| 297 | int src_length, consumed, nalsize = 0; |
| 298 | |
| 299 | if (buf_index >= next_avc) { |
| 300 | nalsize = get_nalsize(p->nal_length_size, buf, buf_size, &buf_index, avctx); |
| 301 | if (nalsize < 0) |
| 302 | break; |
| 303 | next_avc = buf_index + nalsize; |
| 304 | } else { |
| 305 | buf_index = find_start_code(buf, buf_size, buf_index, next_avc); |
| 306 | if (buf_index >= buf_size) |
| 307 | break; |
| 308 | if (buf_index >= next_avc) |
| 309 | continue; |
| 310 | } |
| 311 | src_length = next_avc - buf_index; |
| 312 | |
| 313 | state = buf[buf_index]; |
| 314 | switch (state & 0x1f) { |
| 315 | case H264_NAL_SLICE: |
| 316 | case H264_NAL_IDR_SLICE: |
| 317 | // Do not walk the whole buffer just to decode slice header |
| 318 | if ((state & 0x1f) == H264_NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) { |
| 319 | /* IDR or disposable slice |
no test coverage detected