h264: move parser-only variables to their own context
[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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 #include "libavutil/attributes.h"
29 #include "parser.h"
30 #include "h264data.h"
31 #include "golomb.h"
32 #include "internal.h"
33 #include "mpegutils.h"
34
35 #include <assert.h>
36
37 typedef struct H264ParseContext {
38     H264Context h;
39     ParseContext pc;
40     int got_first;
41 } H264ParseContext;
42
43
44 static int h264_find_frame_end(H264ParseContext *p, const uint8_t *buf,
45                                int buf_size)
46 {
47     H264Context *h = &p->h;
48     int i;
49     uint32_t state;
50     ParseContext *pc = &p->pc;
51 //    mb_addr= pc->mb_addr - 1;
52     state = pc->state;
53     if (state > 13)
54         state = 7;
55
56     for (i = 0; i < buf_size; i++) {
57         if (state == 7) {
58             i += h->h264dsp.startcode_find_candidate(buf + i, buf_size - i);
59             if (i < buf_size)
60                 state = 2;
61         } else if (state <= 2) {
62             if (buf[i] == 1)
63                 state ^= 5;            // 2->7, 1->4, 0->5
64             else if (buf[i])
65                 state = 7;
66             else
67                 state >>= 1;           // 2->1, 1->0, 0->0
68         } else if (state <= 5) {
69             int nalu_type = buf[i] & 0x1F;
70             if (nalu_type == NAL_SEI || nalu_type == NAL_SPS ||
71                 nalu_type == NAL_PPS || nalu_type == NAL_AUD) {
72                 if (pc->frame_start_found) {
73                     i++;
74                     goto found;
75                 }
76             } else if (nalu_type == NAL_SLICE || nalu_type == NAL_DPA ||
77                        nalu_type == NAL_IDR_SLICE) {
78                 if (pc->frame_start_found) {
79                     state += 8;
80                     continue;
81                 } else
82                     pc->frame_start_found = 1;
83             }
84             state = 7;
85         } else {
86             if (buf[i] & 0x80)
87                 goto found;
88             state = 7;
89         }
90     }
91     pc->state = state;
92     return END_NOT_FOUND;
93
94 found:
95     pc->state             = 7;
96     pc->frame_start_found = 0;
97     return i - (state & 5);
98 }
99
100 static int scan_mmco_reset(AVCodecParserContext *s)
101 {
102     H264ParseContext *p = s->priv_data;
103     H264Context      *h = &p->h;
104
105     h->slice_type_nos = s->pict_type & 3;
106
107     if (h->pps.redundant_pic_cnt_present)
108         get_ue_golomb(&h->gb); // redundant_pic_count
109
110     if (ff_set_ref_count(h) < 0)
111         return AVERROR_INVALIDDATA;
112
113     if (h->slice_type_nos != AV_PICTURE_TYPE_I) {
114         int list;
115         for (list = 0; list < h->list_count; list++) {
116             if (get_bits1(&h->gb)) {
117                 int index;
118                 for (index = 0; ; index++) {
119                     unsigned int reordering_of_pic_nums_idc = get_ue_golomb_31(&h->gb);
120
121                     if (reordering_of_pic_nums_idc < 3)
122                         get_ue_golomb(&h->gb);
123                     else if (reordering_of_pic_nums_idc > 3) {
124                         av_log(h->avctx, AV_LOG_ERROR,
125                                "illegal reordering_of_pic_nums_idc %d\n",
126                                reordering_of_pic_nums_idc);
127                         return AVERROR_INVALIDDATA;
128                     } else
129                         break;
130
131                     if (index >= h->ref_count[list]) {
132                         av_log(h->avctx, AV_LOG_ERROR,
133                                "reference count %d overflow\n", index);
134                         return AVERROR_INVALIDDATA;
135                     }
136                 }
137             }
138         }
139     }
140
141     if ((h->pps.weighted_pred && h->slice_type_nos == AV_PICTURE_TYPE_P) ||
142         (h->pps.weighted_bipred_idc == 1 && h->slice_type_nos == AV_PICTURE_TYPE_B))
143         ff_pred_weight_table(h);
144
145     if (get_bits1(&h->gb)) { // adaptive_ref_pic_marking_mode_flag
146         int i;
147         for (i = 0; i < MAX_MMCO_COUNT; i++) {
148             MMCOOpcode opcode = get_ue_golomb_31(&h->gb);
149             if (opcode > (unsigned) MMCO_LONG) {
150                 av_log(h->avctx, AV_LOG_ERROR,
151                        "illegal memory management control operation %d\n",
152                        opcode);
153                 return AVERROR_INVALIDDATA;
154             }
155             if (opcode == MMCO_END)
156                return 0;
157             else if (opcode == MMCO_RESET)
158                 return 1;
159
160             if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG)
161                 get_ue_golomb(&h->gb);
162             if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
163                 opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG)
164                 get_ue_golomb_31(&h->gb);
165         }
166     }
167
168     return 0;
169 }
170
171 /**
172  * Parse NAL units of found picture and decode some basic information.
173  *
174  * @param s parser context.
175  * @param avctx codec context.
176  * @param buf buffer with field/frame data.
177  * @param buf_size size of the buffer.
178  */
179 static inline int parse_nal_units(AVCodecParserContext *s,
180                                   AVCodecContext *avctx,
181                                   const uint8_t *buf, int buf_size)
182 {
183     H264ParseContext *p = s->priv_data;
184     H264Context      *h = &p->h;
185     const uint8_t *buf_end = buf + buf_size;
186     unsigned int pps_id;
187     unsigned int slice_type;
188     int state = -1, got_reset = 0;
189     const uint8_t *ptr;
190     int field_poc[2];
191
192     /* set some sane default values */
193     s->pict_type         = AV_PICTURE_TYPE_I;
194     s->key_frame         = 0;
195     s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
196
197     h->avctx = avctx;
198     ff_h264_reset_sei(h);
199
200     if (!buf_size)
201         return 0;
202
203     for (;;) {
204         int src_length, dst_length, consumed;
205         buf = avpriv_find_start_code(buf, buf_end, &state);
206         if (buf >= buf_end)
207             break;
208         --buf;
209         src_length = buf_end - buf;
210         switch (state & 0x1f) {
211         case NAL_SLICE:
212         case NAL_IDR_SLICE:
213             // Do not walk the whole buffer just to decode slice header
214             if ((state & 0x1f) == NAL_IDR_SLICE || ((state >> 5) & 0x3) == 0) {
215                 /* IDR or disposable slice
216                  * No need to decode many bytes because MMCOs shall not be present. */
217                 if (src_length > 60)
218                     src_length = 60;
219             } else {
220                 /* To decode up to MMCOs */
221                 if (src_length > 1000)
222                     src_length = 1000;
223             }
224             break;
225         }
226         ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
227         if (!ptr || dst_length < 0)
228             break;
229
230         init_get_bits(&h->gb, ptr, 8 * dst_length);
231         switch (h->nal_unit_type) {
232         case NAL_SPS:
233             ff_h264_decode_seq_parameter_set(h);
234             break;
235         case NAL_PPS:
236             ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
237             break;
238         case NAL_SEI:
239             ff_h264_decode_sei(h);
240             break;
241         case NAL_IDR_SLICE:
242             s->key_frame = 1;
243
244             h->prev_frame_num        = 0;
245             h->prev_frame_num_offset = 0;
246             h->prev_poc_msb          =
247             h->prev_poc_lsb          = 0;
248         /* fall through */
249         case NAL_SLICE:
250             get_ue_golomb(&h->gb);  // skip first_mb_in_slice
251             slice_type   = get_ue_golomb_31(&h->gb);
252             s->pict_type = golomb_to_pict_type[slice_type % 5];
253             if (h->sei_recovery_frame_cnt >= 0) {
254                 /* key frame, since recovery_frame_cnt is set */
255                 s->key_frame = 1;
256             }
257             pps_id = get_ue_golomb(&h->gb);
258             if (pps_id >= MAX_PPS_COUNT) {
259                 av_log(h->avctx, AV_LOG_ERROR,
260                        "pps_id %u out of range\n", pps_id);
261                 return -1;
262             }
263             if (!h->pps_buffers[pps_id]) {
264                 av_log(h->avctx, AV_LOG_ERROR,
265                        "non-existing PPS %u referenced\n", pps_id);
266                 return -1;
267             }
268             h->pps = *h->pps_buffers[pps_id];
269             if (!h->sps_buffers[h->pps.sps_id]) {
270                 av_log(h->avctx, AV_LOG_ERROR,
271                        "non-existing SPS %u referenced\n", h->pps.sps_id);
272                 return -1;
273             }
274             h->sps       = *h->sps_buffers[h->pps.sps_id];
275             h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
276
277             avctx->profile = ff_h264_get_profile(&h->sps);
278             avctx->level   = h->sps.level_idc;
279
280             if (h->sps.frame_mbs_only_flag) {
281                 h->picture_structure = PICT_FRAME;
282             } else {
283                 if (get_bits1(&h->gb)) { // field_pic_flag
284                     h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
285                 } else {
286                     h->picture_structure = PICT_FRAME;
287                 }
288             }
289
290             if (h->nal_unit_type == NAL_IDR_SLICE)
291                 get_ue_golomb(&h->gb); /* idr_pic_id */
292             if (h->sps.poc_type == 0) {
293                 h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
294
295                 if (h->pps.pic_order_present == 1 &&
296                     h->picture_structure == PICT_FRAME)
297                     h->delta_poc_bottom = get_se_golomb(&h->gb);
298             }
299
300             if (h->sps.poc_type == 1 &&
301                 !h->sps.delta_pic_order_always_zero_flag) {
302                 h->delta_poc[0] = get_se_golomb(&h->gb);
303
304                 if (h->pps.pic_order_present == 1 &&
305                     h->picture_structure == PICT_FRAME)
306                     h->delta_poc[1] = get_se_golomb(&h->gb);
307             }
308
309             /* Decode POC of this picture.
310              * The prev_ values needed for decoding POC of the next picture are not set here. */
311             field_poc[0] = field_poc[1] = INT_MAX;
312             ff_init_poc(h, field_poc, &s->output_picture_number);
313
314             /* Continue parsing to check if MMCO_RESET is present.
315              * FIXME: MMCO_RESET could appear in non-first slice.
316              *        Maybe, we should parse all undisposable non-IDR slice of this
317              *        picture until encountering MMCO_RESET in a slice of it. */
318             if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
319                 got_reset = scan_mmco_reset(s);
320                 if (got_reset < 0)
321                     return got_reset;
322             }
323
324             /* Set up the prev_ values for decoding POC of the next picture. */
325             h->prev_frame_num        = got_reset ? 0 : h->frame_num;
326             h->prev_frame_num_offset = got_reset ? 0 : h->frame_num_offset;
327             if (h->nal_ref_idc != 0) {
328                 if (!got_reset) {
329                     h->prev_poc_msb = h->poc_msb;
330                     h->prev_poc_lsb = h->poc_lsb;
331                 } else {
332                     h->prev_poc_msb = 0;
333                     h->prev_poc_lsb =
334                         h->picture_structure == PICT_BOTTOM_FIELD ? 0 : field_poc[0];
335                 }
336             }
337
338             if (h->sps.pic_struct_present_flag) {
339                 switch (h->sei_pic_struct) {
340                 case SEI_PIC_STRUCT_TOP_FIELD:
341                 case SEI_PIC_STRUCT_BOTTOM_FIELD:
342                     s->repeat_pict = 0;
343                     break;
344                 case SEI_PIC_STRUCT_FRAME:
345                 case SEI_PIC_STRUCT_TOP_BOTTOM:
346                 case SEI_PIC_STRUCT_BOTTOM_TOP:
347                     s->repeat_pict = 1;
348                     break;
349                 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
350                 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
351                     s->repeat_pict = 2;
352                     break;
353                 case SEI_PIC_STRUCT_FRAME_DOUBLING:
354                     s->repeat_pict = 3;
355                     break;
356                 case SEI_PIC_STRUCT_FRAME_TRIPLING:
357                     s->repeat_pict = 5;
358                     break;
359                 default:
360                     s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
361                     break;
362                 }
363             } else {
364                 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
365             }
366
367             if (h->picture_structure == PICT_FRAME) {
368                 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
369                 if (h->sps.pic_struct_present_flag) {
370                     switch (h->sei_pic_struct) {
371                     case SEI_PIC_STRUCT_TOP_BOTTOM:
372                     case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
373                         s->field_order = AV_FIELD_TT;
374                         break;
375                     case SEI_PIC_STRUCT_BOTTOM_TOP:
376                     case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
377                         s->field_order = AV_FIELD_BB;
378                         break;
379                     default:
380                         s->field_order = AV_FIELD_PROGRESSIVE;
381                         break;
382                     }
383                 } else {
384                     if (field_poc[0] < field_poc[1])
385                         s->field_order = AV_FIELD_TT;
386                     else if (field_poc[0] > field_poc[1])
387                         s->field_order = AV_FIELD_BB;
388                     else
389                         s->field_order = AV_FIELD_PROGRESSIVE;
390                 }
391             } else {
392                 if (h->picture_structure == PICT_TOP_FIELD)
393                     s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
394                 else
395                     s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
396                 s->field_order = AV_FIELD_UNKNOWN;
397             }
398
399             return 0; /* no need to evaluate the rest */
400         }
401         buf += consumed;
402     }
403     /* didn't find a picture! */
404     av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
405     return -1;
406 }
407
408 static int h264_parse(AVCodecParserContext *s,
409                       AVCodecContext *avctx,
410                       const uint8_t **poutbuf, int *poutbuf_size,
411                       const uint8_t *buf, int buf_size)
412 {
413     H264ParseContext *p = s->priv_data;
414     H264Context      *h = &p->h;
415     ParseContext *pc = &p->pc;
416     int next;
417
418     if (!p->got_first) {
419         p->got_first = 1;
420         if (avctx->extradata_size) {
421             h->avctx = avctx;
422             // must be done like in the decoder.
423             // otherwise opening the parser, creating extradata,
424             // and then closing and opening again
425             // will cause has_b_frames to be always set.
426             // NB: estimate_timings_from_pts behaves exactly like this.
427             if (!avctx->has_b_frames)
428                 h->low_delay = 1;
429             ff_h264_decode_extradata(h);
430         }
431     }
432
433     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
434         next = buf_size;
435     } else {
436         next = h264_find_frame_end(p, buf, buf_size);
437
438         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
439             *poutbuf      = NULL;
440             *poutbuf_size = 0;
441             return buf_size;
442         }
443
444         if (next < 0 && next != END_NOT_FOUND) {
445             assert(pc->last_index + next >= 0);
446             h264_find_frame_end(p, &pc->buffer[pc->last_index + next], -next); // update state
447         }
448     }
449
450     parse_nal_units(s, avctx, buf, buf_size);
451
452     if (h->sei_cpb_removal_delay >= 0) {
453         s->dts_sync_point    = h->sei_buffering_period_present;
454         s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
455         s->pts_dts_delta     = h->sei_dpb_output_delay;
456     } else {
457         s->dts_sync_point    = INT_MIN;
458         s->dts_ref_dts_delta = INT_MIN;
459         s->pts_dts_delta     = INT_MIN;
460     }
461
462     if (s->flags & PARSER_FLAG_ONCE) {
463         s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
464     }
465
466     *poutbuf      = buf;
467     *poutbuf_size = buf_size;
468     return next;
469 }
470
471 static int h264_split(AVCodecContext *avctx,
472                       const uint8_t *buf, int buf_size)
473 {
474     int i;
475     uint32_t state = -1;
476     int has_sps    = 0;
477
478     for (i = 0; i <= buf_size; i++) {
479         if ((state & 0xFFFFFF1F) == 0x107)
480             has_sps = 1;
481         /*  if((state&0xFFFFFF1F) == 0x101 ||
482          *     (state&0xFFFFFF1F) == 0x102 ||
483          *     (state&0xFFFFFF1F) == 0x105) {
484          *  }
485          */
486         if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x106 &&
487             (state & 0xFFFFFF1F) != 0x107 && (state & 0xFFFFFF1F) != 0x108 &&
488             (state & 0xFFFFFF1F) != 0x109 && (state & 0xFFFFFF1F) != 0x10d &&
489             (state & 0xFFFFFF1F) != 0x10f) {
490             if (has_sps) {
491                 while (i > 4 && buf[i - 5] == 0)
492                     i--;
493                 return i - 4;
494             }
495         }
496         if (i < buf_size)
497             state = (state << 8) | buf[i];
498     }
499     return 0;
500 }
501
502 static void close(AVCodecParserContext *s)
503 {
504     H264ParseContext *p = s->priv_data;
505     H264Context      *h = &p->h;
506     ParseContext *pc = &p->pc;
507
508     av_free(pc->buffer);
509     ff_h264_free_context(h);
510 }
511
512 static av_cold int init(AVCodecParserContext *s)
513 {
514     H264ParseContext *p = s->priv_data;
515     H264Context      *h = &p->h;
516     h->thread_context[0]   = h;
517     h->slice_context_count = 1;
518     ff_h264dsp_init(&h->h264dsp, 8, 1);
519     return 0;
520 }
521
522 AVCodecParser ff_h264_parser = {
523     .codec_ids      = { AV_CODEC_ID_H264 },
524     .priv_data_size = sizeof(H264ParseContext),
525     .parser_init    = init,
526     .parser_parse   = h264_parse,
527     .parser_close   = close,
528     .split          = h264_split,
529 };