Merge commit 'a6e27f7add2698fdd89911632b570c3d0c3f2aaa'
[ffmpeg.git] / libavcodec / h264_parser.c
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... parser
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 parser.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27
28 #define UNCHECKED_BITSTREAM_READER 1
29
30 #include <assert.h>
31 #include <stdint.h>
32
33 #include "libavutil/avutil.h"
34 #include "libavutil/error.h"
35 #include "libavutil/log.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/pixfmt.h"
38
39 #include "get_bits.h"
40 #include "golomb.h"
41 #include "h264.h"
42 #include "h264data.h"
43 #include "internal.h"
44 #include "mpegutils.h"
45 #include "parser.h"
46
47 typedef struct H264ParseContext {
48     H264Context h;
49     ParseContext pc;
50     int got_first;
51 } H264ParseContext;
52
53
54 static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
55                                int buf_size)
56 {
57     H264Context *h = &p->h;
58     int i, j;
59     uint32_t state;
60     ParseContext *pc = &p->pc;
61
62     int next_avc= h->is_avc ? 0 : buf_size;
63 //    mb_addr= pc->mb_addr - 1;
64     state = pc->state;
65     if (state > 13)
66         state = 7;
67
68     if (h->is_avc && !h->nal_length_size)
69         av_log(h->avctx, AV_LOG_ERROR, "AVC-parser: nal length size invalid\n");
70
71     for (i = 0; i < buf_size; i++) {
72         if (i >= next_avc) {
73             int nalsize = 0;
74             i = next_avc;
75             for (j = 0; j < h->nal_length_size; j++)
76                 nalsize = (nalsize << 8) | buf[i++];
77             if (nalsize <= 0 || nalsize > buf_size - i) {
78                 av_log(h->avctx, AV_LOG_ERROR, "AVC-parser: nal size %d remaining %d\n", nalsize, buf_size - i);
79                 return buf_size;
80             }
81             next_avc = i + nalsize;
82             state    = 5;
83         }
84
85         if (state == 7) {
86             i += h->h264dsp.startcode_find_candidate(buf + i, next_avc - i);
87             if (i < next_avc)
88                 state = 2;
89         } else if (state <= 2) {
90             if (buf[i] == 1)
91                 state ^= 5;            // 2->7, 1->4, 0->5
92             else if (buf[i])
93                 state = 7;
94             else
95                 state >>= 1;           // 2->1, 1->0, 0->0
96         } else if (state <= 5) {
97             int nalu_type = buf[i] & 0x1F;
98             if (nalu_type == NAL_SEI || nalu_type == NAL_SPS ||
99                 nalu_type == NAL_PPS || nalu_type == NAL_AUD) {
100                 if (pc->frame_start_found) {
101                     i++;
102                     goto found;
103                 }
104             } else if (nalu_type == NAL_SLICE || nalu_type == NAL_DPA ||
105                        nalu_type == NAL_IDR_SLICE) {
106                 state += 8;
107                 continue;
108             }
109             state = 7;
110         } else {
111             h->parse_history[h->parse_history_count++]= buf[i];
112             if (h->parse_history_count>5) {
113                 unsigned int mb, last_mb= h->parse_last_mb;
114                 GetBitContext gb;
115
116                 init_get_bits(&gb, h->parse_history, 8*h->parse_history_count);
117                 h->parse_history_count=0;
118                 mb= get_ue_golomb_long(&gb);
119                 h->parse_last_mb= mb;
120                 if (pc->frame_start_found) {
121                     if (mb <= last_mb)
122                         goto found;
123                 } else
124                     pc->frame_start_found = 1;
125                 state = 7;
126             }
127         }
128     }
129     pc->state = state;
130     if (h->is_avc)
131         return next_avc;
132     return END_NOT_FOUND;
133
134 found:
135     pc->state             = 7;
136     pc->frame_start_found = 0;
137     if (h->is_avc)
138         return next_avc;
139     return i - (state & 5) - 5 * (state > 7);
140 }
141
142 static int scan_mmco_reset(AVCodecParserContext *s)
143 {
144     H264ParseContext *p = s->priv_data;
145     H264Context      *h = &p->h;
146     H264SliceContext *sl = &h->slice_ctx[0];
147     int list_count, ref_count[2];
148
149     sl->slice_type_nos = s->pict_type & 3;
150
151     if (h->pps.redundant_pic_cnt_present)
152         get_ue_golomb(&sl->gb); // redundant_pic_count
153
154     if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
155         get_bits1(&sl->gb); // direct_spatial_mv_pred
156
157     if (ff_h264_parse_ref_count(&list_count, ref_count, &sl->gb, &h->pps,
158                                 sl->slice_type_nos, h->picture_structure, h->avctx) < 0)
159         return AVERROR_INVALIDDATA;
160
161     if (sl->slice_type_nos != AV_PICTURE_TYPE_I) {
162         int list;
163         for (list = 0; list < list_count; list++) {
164             if (get_bits1(&sl->gb)) {
165                 int index;
166                 for (index = 0; ; index++) {
167                     unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&sl->gb);
168
169                     if (reordering_of_pic_nums_idc < 3)
170                         get_ue_golomb_long(&sl->gb);
171                     else if (reordering_of_pic_nums_idc > 3) {
172                         av_log(h->avctx, AV_LOG_ERROR,
173                                "illegal reordering_of_pic_nums_idc %d\n",
174                                reordering_of_pic_nums_idc);
175                         return AVERROR_INVALIDDATA;
176                     } else
177                         break;
178
179                     if (index >= ref_count[list]) {
180                         av_log(h->avctx, AV_LOG_ERROR,
181                                "reference count %d overflow\n", index);
182                         return AVERROR_INVALIDDATA;
183                     }
184                 }
185             }
186         }
187     }
188
189     if ((h->pps.weighted_pred && sl->slice_type_nos == AV_PICTURE_TYPE_P) ||
190         (h->pps.weighted_bipred_idc == 1 && sl->slice_type_nos == AV_PICTURE_TYPE_B))
191         ff_h264_pred_weight_table(&sl->gb, &h->sps, ref_count, sl->slice_type_nos,
192                                   &sl->pwt);
193
194     if (get_bits1(&sl->gb)) { // adaptive_ref_pic_marking_mode_flag
195         int i;
196         for (i = 0; i < MAX_MMCO_COUNT; i++) {
197             MMCOOpcode opcode = get_ue_golomb_31(&sl->gb);
198             if (opcode > (unsigned) MMCO_LONG) {
199                 av_log(h->avctx, AV_LOG_ERROR,
200                        "illegal memory management control operation %d\n",
201                        opcode);
202                 return AVERROR_INVALIDDATA;
203             }
204             if (opcode == MMCO_END)
205                return 0;
206             else if (opcode == MMCO_RESET)
207                 return 1;
208
209             if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
210                 get_ue_golomb_long(&sl->gb); // difference_of_pic_nums_minus1
211             if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
212                 opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
213                 get_ue_golomb_31(&sl->gb);
214         }
215     }
216
217     return 0;
218 }
219
220 /**
221  * Parse NAL units of found picture and decode some basic information.
222  *
223  * @param s parser context.
224  * @param avctx codec context.
225  * @param buf buffer with field/frame data.
226  * @param buf_size size of the buffer.
227  */
228 static inline int parse_nal_units(AVCodecParserContext *s,
229                                   AVCodecContext *avctx,
230                                   const uint8_t * const buf, int buf_size)
231 {
232     H264ParseContext *p = s->priv_data;
233     H264Context      *h = &p->h;
234     H264SliceContext *sl = &h->slice_ctx[0];
235     H2645NAL nal = { NULL };
236     int buf_index, next_avc;
237     unsigned int pps_id;
238     unsigned int slice_type;
239     int state = -1, got_reset = 0;
240     int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
241     int field_poc[2];
242     int ret;
243
244     /* set some sane default values */
245     s->pict_type         = AV_PICTURE_TYPE_I;
246     s->key_frame         = 0;
247     s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
248
249     h->avctx = avctx;
250     ff_h264_reset_sei(h);
251     h->sei_fpa.frame_packing_arrangement_cancel_flag = -1;
252
253     if (!buf_size)
254         return 0;
255
256     buf_index     = 0;
257     next_avc      = h->is_avc ? 0 : buf_size;
258     for (;;) {
259         int src_length, consumed, nalsize = 0;
260
261         if (buf_index >= next_avc) {
262             nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
263             if (nalsize < 0)
264                 break;
265             next_avc = buf_index + nalsize;
266         } else {
267             buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
268             if (buf_index >= buf_size)
269                 break;
270             if (buf_index >= next_avc)
271                 continue;
272         }
273         src_length = next_avc - buf_index;
274
275         state = buf[buf_index];
276         switch (state & 0x1f) {
277         case NAL_SLICE:
278         case NAL_IDR_SLICE:
279             // Do not walk the whole buffer just to decode slice header
280             if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
281                 /* IDR or disposable slice
282                  * No need to decode many bytes because MMCOs shall not be present. */
283                 if (src_length > 60)
284                     src_length = 60;
285             } else {
286                 /* To decode up to MMCOs */
287                 if (src_length > 1000)
288                     src_length = 1000;
289             }
290             break;
291         }
292         consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, &nal);
293         if (consumed < 0)
294             break;
295
296         buf_index += consumed;
297
298         ret = init_get_bits8(&nal.gb, nal.data, nal.size);
299         if (ret < 0)
300             goto fail;
301         get_bits1(&nal.gb);
302         nal.ref_idc = get_bits(&nal.gb, 2);
303         nal.type    = get_bits(&nal.gb, 5);
304
305         h->gb            = nal.gb;
306         h->nal_ref_idc   = nal.ref_idc;
307         h->nal_unit_type = nal.type;
308
309         switch (h->nal_unit_type) {
310         case NAL_SPS:
311             ff_h264_decode_seq_parameter_set(h, 0);
312             break;
313         case NAL_PPS:
314             ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
315             break;
316         case NAL_SEI:
317             ff_h264_decode_sei(h);
318             break;
319         case NAL_IDR_SLICE:
320             s->key_frame = 1;
321
322             h->prev_frame_num        = 0;
323             h->prev_frame_num_offset = 0;
324             h->prev_poc_msb          =
325             h->prev_poc_lsb          = 0;
326         /* fall through */
327         case NAL_SLICE:
328             sl->gb = nal.gb;
329             get_ue_golomb_long(&sl->gb);  // skip first_mb_in_slice
330             slice_type   = get_ue_golomb_31(&sl->gb);
331             s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5];
332             if (h->sei_recovery_frame_cnt >= 0) {
333                 /* key frame, since recovery_frame_cnt is set */
334                 s->key_frame = 1;
335             }
336             pps_id = get_ue_golomb(&sl->gb);
337             if (pps_id >= MAX_PPS_COUNT) {
338                 av_log(h->avctx, AV_LOG_ERROR,
339                        "pps_id %u out of range\n", pps_id);
340                 goto fail;
341             }
342             if (!h->pps_buffers[pps_id]) {
343                 av_log(h->avctx, AV_LOG_ERROR,
344                        "non-existing PPS %u referenced\n", pps_id);
345                 goto fail;
346             }
347             h->pps = *h->pps_buffers[pps_id];
348             if (!h->sps_buffers[h->pps.sps_id]) {
349                 av_log(h->avctx, AV_LOG_ERROR,
350                        "non-existing SPS %u referenced\n", h->pps.sps_id);
351                 goto fail;
352             }
353             h->sps       = *h->sps_buffers[h->pps.sps_id];
354             h->frame_num = get_bits(&sl->gb, h->sps.log2_max_frame_num);
355
356             if(h->sps.ref_frame_count <= 1 && h->pps.ref_count[0] <= 1 && s->pict_type == AV_PICTURE_TYPE_I)
357                 s->key_frame = 1;
358
359             s->coded_width  = 16 * h->sps.mb_width;
360             s->coded_height = 16 * h->sps.mb_height;
361             s->width        = s->coded_width  - (h->sps.crop_right + h->sps.crop_left);
362             s->height       = s->coded_height - (h->sps.crop_top   + h->sps.crop_bottom);
363             if (s->width <= 0 || s->height <= 0) {
364                 s->width  = s->coded_width;
365                 s->height = s->coded_height;
366             }
367
368             switch (h->sps.bit_depth_luma) {
369             case 9:
370                 if (CHROMA444(h))      s->format = AV_PIX_FMT_YUV444P9;
371                 else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P9;
372                 else                   s->format = AV_PIX_FMT_YUV420P9;
373                 break;
374             case 10:
375                 if (CHROMA444(h))      s->format = AV_PIX_FMT_YUV444P10;
376                 else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P10;
377                 else                   s->format = AV_PIX_FMT_YUV420P10;
378                 break;
379             case 8:
380                 if (CHROMA444(h))      s->format = AV_PIX_FMT_YUV444P;
381                 else if (CHROMA422(h)) s->format = AV_PIX_FMT_YUV422P;
382                 else                   s->format = AV_PIX_FMT_YUV420P;
383                 break;
384             default:
385                 s->format = AV_PIX_FMT_NONE;
386             }
387
388             avctx->profile = ff_h264_get_profile(&h->sps);
389             avctx->level   = h->sps.level_idc;
390
391             if (h->sps.frame_mbs_only_flag) {
392                 h->picture_structure = PICT_FRAME;
393             } else {
394                 if (get_bits1(&sl->gb)) { // field_pic_flag
395                     h->picture_structure = PICT_TOP_FIELD + get_bits1(&sl->gb); // bottom_field_flag
396                 } else {
397                     h->picture_structure = PICT_FRAME;
398                 }
399             }
400
401             if (h->nal_unit_type == NAL_IDR_SLICE)
402                 get_ue_golomb_long(&sl->gb); /* idr_pic_id */
403             if (h->sps.poc_type == 0) {
404                 h->poc_lsb = get_bits(&sl->gb, h->sps.log2_max_poc_lsb);
405
406                 if (h->pps.pic_order_present == 1 &&
407                     h->picture_structure == PICT_FRAME)
408                     h->delta_poc_bottom = get_se_golomb(&sl->gb);
409             }
410
411             if (h->sps.poc_type == 1 &&
412                 !h->sps.delta_pic_order_always_zero_flag) {
413                 h->delta_poc[0] = get_se_golomb(&sl->gb);
414
415                 if (h->pps.pic_order_present == 1 &&
416                     h->picture_structure == PICT_FRAME)
417                     h->delta_poc[1] = get_se_golomb(&sl->gb);
418             }
419
420             /* Decode POC of this picture.
421              * The prev_ values needed for decoding POC of the next picture are not set here. */
422             field_poc[0] = field_poc[1] = INT_MAX;
423             ff_init_poc(h, field_poc, &s->output_picture_number);
424
425             /* Continue parsing to check if MMCO_RESET is present.
426              * FIXME: MMCO_RESET could appear in non-first slice.
427              *        Maybe, we should parse all undisposable non-IDR slice of this
428              *        picture until encountering MMCO_RESET in a slice of it. */
429             if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
430                 got_reset = scan_mmco_reset(s);
431                 if (got_reset < 0)
432                     goto fail;
433             }
434
435             /* Set up the prev_ values for decoding POC of the next picture. */
436             h->prev_frame_num        = got_reset ? 0 : h->frame_num;
437             h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset;
438             if (h->nal_ref_idc != 0) {
439                 if (!got_reset) {
440                     h->prev_poc_msb = h->poc_msb;
441                     h->prev_poc_lsb = h->poc_lsb;
442                 } else {
443                     h->prev_poc_msb = 0;
444                     h->prev_poc_lsb =
445                         h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
446                 }
447             }
448
449             if (h->sps.pic_struct_present_flag) {
450                 switch (h->sei_pic_struct) {
451                 case SEI_PIC_STRUCT_TOP_FIELD:
452                 case SEI_PIC_STRUCT_BOTTOM_FIELD:
453                     s->repeat_pict = 0;
454                     break;
455                 case SEI_PIC_STRUCT_FRAME:
456                 case SEI_PIC_STRUCT_TOP_BOTTOM:
457                 case SEI_PIC_STRUCT_BOTTOM_TOP:
458                     s->repeat_pict = 1;
459                     break;
460                 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
461                 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
462                     s->repeat_pict = 2;
463                     break;
464                 case SEI_PIC_STRUCT_FRAME_DOUBLING:
465                     s->repeat_pict = 3;
466                     break;
467                 case SEI_PIC_STRUCT_FRAME_TRIPLING:
468                     s->repeat_pict = 5;
469                     break;
470                 default:
471                     s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
472                     break;
473                 }
474             } else {
475                 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
476             }
477
478             if (h->picture_structure == PICT_FRAME) {
479                 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
480                 if (h->sps.pic_struct_present_flag) {
481                     switch (h->sei_pic_struct) {
482                     case SEI_PIC_STRUCT_TOP_BOTTOM:
483                     case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
484                         s->field_order = AV_FIELD_TT;
485                         break;
486                     case SEI_PIC_STRUCT_BOTTOM_TOP:
487                     case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
488                         s->field_order = AV_FIELD_BB;
489                         break;
490                     default:
491                         s->field_order = AV_FIELD_PROGRESSIVE;
492                         break;
493                     }
494                 } else {
495                     if (field_poc[0] < field_poc[1])
496                         s->field_order = AV_FIELD_TT;
497                     else if (field_poc[0] > field_poc[1])
498                         s->field_order = AV_FIELD_BB;
499                     else
500                         s->field_order = AV_FIELD_PROGRESSIVE;
501                 }
502             } else {
503                 if (h->picture_structure == PICT_TOP_FIELD)
504                     s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
505                 else
506                     s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
507                 s->field_order = AV_FIELD_UNKNOWN;
508             }
509
510             av_freep(&nal.rbsp_buffer);
511             return 0; /* no need to evaluate the rest */
512         }
513     }
514     if (q264) {
515         av_freep(&nal.rbsp_buffer);
516         return 0;
517     }
518     /* didn't find a picture! */
519     av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
520 fail:
521     av_freep(&nal.rbsp_buffer);
522     return -1;
523 }
524
525 static int h264_parse(AVCodecParserContext *s,
526                       AVCodecContext *avctx,
527                       const uint8_t **poutbuf, int *poutbuf_size,
528                       const uint8_t *buf, int buf_size)
529 {
530     H264ParseContext *p = s->priv_data;
531     H264Context      *h = &p->h;
532     ParseContext *pc = &p->pc;
533     int next;
534
535     if (!p->got_first) {
536         p->got_first = 1;
537         if (avctx->extradata_size) {
538             h->avctx = avctx;
539             // must be done like in decoder, otherwise opening the parser,
540             // letting it create extradata and then closing and opening again
541             // will cause has_b_frames to be always set.
542             // Note that estimate_timings_from_pts does exactly this.
543             if (!avctx->has_b_frames)
544                 h->low_delay = 1;
545             ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
546         }
547     }
548
549     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
550         next = buf_size;
551     } else {
552         next = h264_find_frame_end(p, buf, buf_size);
553
554         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
555             *poutbuf      = NULL;
556             *poutbuf_size = 0;
557             return buf_size;
558         }
559
560         if (next < 0 && next != END_NOT_FOUND) {
561             av_assert1(pc->last_index + next >= 0);
562             h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next); // update state
563         }
564     }
565
566     parse_nal_units(s, avctx, buf, buf_size);
567
568     if (avctx->framerate.num)
569         avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
570     if (h->sei_cpb_removal_delay >= 0) {
571         s->dts_sync_point    = h->sei_buffering_period_present;
572         s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
573         s->pts_dts_delta     = h->sei_dpb_output_delay;
574     } else {
575         s->dts_sync_point    = INT_MIN;
576         s->dts_ref_dts_delta = INT_MIN;
577         s->pts_dts_delta     = INT_MIN;
578     }
579
580     if (s->flags & PARSER_FLAG_ONCE) {
581         s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
582     }
583
584     *poutbuf      = buf;
585     *poutbuf_size = buf_size;
586     return next;
587 }
588
589 static int h264_split(AVCodecContext *avctx,
590                       const uint8_t *buf, int buf_size)
591 {
592     uint32_t state = -1;
593     int has_sps    = 0;
594     int has_pps    = 0;
595     const uint8_t *ptr = buf, *end = buf + buf_size;
596     int nalu_type;
597
598     while (ptr < end) {
599         ptr = avpriv_find_start_code(ptr, end, &state);
600         if ((state & 0xFFFFFF00) != 0x100)
601             break;
602         nalu_type = state & 0x1F;
603         if (nalu_type == NAL_SPS) {
604             has_sps = 1;
605         } else if (nalu_type == NAL_PPS)
606             has_pps = 1;
607         /* else if (nalu_type == 0x01 ||
608          *     nalu_type == 0x02 ||
609          *     nalu_type == 0x05) {
610          *  }
611          */
612         else if ((nalu_type != NAL_SEI || has_pps) &&
613                   nalu_type != NAL_AUD && nalu_type != NAL_SPS_EXT &&
614                   nalu_type != 0x0f) {
615             if (has_sps) {
616                 while (ptr - 4 > buf && ptr[-5] == 0)
617                     ptr--;
618                 return ptr - 4 - buf;
619             }
620         }
621     }
622
623     return 0;
624 }
625
626 static void h264_close(AVCodecParserContext *s)
627 {
628     H264ParseContext *p = s->priv_data;
629     H264Context      *h = &p->h;
630     ParseContext *pc = &p->pc;
631
632     av_freep(&pc->buffer);
633     ff_h264_free_context(h);
634 }
635
636 static av_cold int init(AVCodecParserContext *s)
637 {
638     H264ParseContext *p = s->priv_data;
639     H264Context      *h = &p->h;
640
641     h->slice_ctx = av_mallocz(sizeof(*h->slice_ctx));
642     if (!h->slice_ctx)
643         return 0;
644     h->nb_slice_ctx = 1;
645
646     h->slice_context_count = 1;
647     ff_h264dsp_init(&h->h264dsp, 8, 1);
648     return 0;
649 }
650
651 AVCodecParser ff_h264_parser = {
652     .codec_ids      = { AV_CODEC_ID_H264 },
653     .priv_data_size = sizeof(H264ParseContext),
654     .parser_init    = init,
655     .parser_parse   = h264_parse,
656     .parser_close   = h264_close,
657     .split          = h264_split,
658 };