Merge commit '218d6844b37d339ffbf2044ad07d8be7767e2734'
[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 "libavutil/attributes.h"
31 #include "parser.h"
32 #include "h264data.h"
33 #include "golomb.h"
34 #include "internal.h"
35
36
37 static int h264_find_frame_end(H264Context *h, const uint8_t *buf,
38                                int buf_size)
39 {
40     int i, j;
41     uint32_t state;
42     ParseContext *pc = &h->parse_context;
43     int next_avc= h->is_avc ? 0 : buf_size;
44
45 //    mb_addr= pc->mb_addr - 1;
46     state = pc->state;
47     if (state > 13)
48         state = 7;
49
50     if (h->is_avc && !h->nal_length_size)
51         av_log(h->avctx, AV_LOG_ERROR, "AVC-parser: nal length size invalid\n");
52
53     for (i = 0; i < buf_size; i++) {
54         if (i >= next_avc) {
55             int nalsize = 0;
56             i = next_avc;
57             for (j = 0; j < h->nal_length_size; j++)
58                 nalsize = (nalsize << 8) | buf[i++];
59             if (nalsize <= 0 || nalsize > buf_size - i) {
60                 av_log(h->avctx, AV_LOG_ERROR, "AVC-parser: nal size %d remaining %d\n", nalsize, buf_size - i);
61                 return buf_size;
62             }
63             next_avc = i + nalsize;
64             state    = 5;
65         }
66
67         if (state == 7) {
68             i += h->h264dsp.h264_find_start_code_candidate(buf + i, next_avc - i);
69             if (i < next_avc)
70                 state = 2;
71         } else if (state <= 2) {
72             if (buf[i] == 1)
73                 state ^= 5;            // 2->7, 1->4, 0->5
74             else if (buf[i])
75                 state = 7;
76             else
77                 state >>= 1;           // 2->1, 1->0, 0->0
78         } else if (state <= 5) {
79             int v = buf[i] & 0x1F;
80             if (v == 6 || v == 7 || v == 8 || v == 9) {
81                 if (pc->frame_start_found) {
82                     i++;
83                     goto found;
84                 }
85             } else if (v == 1 || v == 2 || v == 5) {
86                 state += 8;
87                 continue;
88             }
89             state = 7;
90         } else {
91             h->parse_history[h->parse_history_count++]= buf[i];
92             if (h->parse_history_count>3) {
93                 unsigned int mb, last_mb= h->parse_last_mb;
94                 GetBitContext gb;
95
96                 init_get_bits(&gb, h->parse_history, 8*h->parse_history_count);
97                 h->parse_history_count=0;
98                 mb= get_ue_golomb_long(&gb);
99                 last_mb= h->parse_last_mb;
100                 h->parse_last_mb= mb;
101                 if (pc->frame_start_found) {
102                     if (mb <= last_mb)
103                         goto found;
104                 } else
105                     pc->frame_start_found = 1;
106                 state = 7;
107             }
108         }
109     }
110     pc->state = state;
111     if (h->is_avc)
112         return next_avc;
113     return END_NOT_FOUND;
114
115 found:
116     pc->state             = 7;
117     pc->frame_start_found = 0;
118     if (h->is_avc)
119         return next_avc;
120     return i - (state & 5) - 3 * (state > 7);
121 }
122
123 /**
124  * Parse NAL units of found picture and decode some basic information.
125  *
126  * @param s parser context.
127  * @param avctx codec context.
128  * @param buf buffer with field/frame data.
129  * @param buf_size size of the buffer.
130  */
131 static inline int parse_nal_units(AVCodecParserContext *s,
132                                   AVCodecContext *avctx,
133                                   const uint8_t *buf, int buf_size)
134 {
135     H264Context *h         = s->priv_data;
136     const uint8_t *buf_end = buf + buf_size;
137     unsigned int pps_id;
138     unsigned int slice_type;
139     int state = -1;
140     const uint8_t *ptr;
141     int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
142     int field_poc[2];
143
144     /* set some sane default values */
145     s->pict_type         = AV_PICTURE_TYPE_I;
146     s->key_frame         = 0;
147     s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
148
149     h->avctx                        = avctx;
150     h->sei_recovery_frame_cnt       = -1;
151     h->sei_dpb_output_delay         = 0;
152     h->sei_cpb_removal_delay        = -1;
153     h->sei_buffering_period_present = 0;
154     h->sei_fpa.frame_packing_arrangement_cancel_flag = -1;
155
156     if (!buf_size)
157         return 0;
158
159     for (;;) {
160         int src_length, dst_length, consumed, nalsize = 0;
161         if (h->is_avc) {
162             int i;
163             if (h->nal_length_size >= buf_end - buf) break;
164             nalsize = 0;
165             for (i = 0; i < h->nal_length_size; i++)
166                 nalsize = (nalsize << 8) | *buf++;
167             if (nalsize <= 0 || nalsize > buf_end - buf) {
168                 av_log(h->avctx, AV_LOG_ERROR, "AVC: nal size %d\n", nalsize);
169                 break;
170             }
171             src_length = nalsize;
172         } else {
173         buf = avpriv_find_start_code(buf, buf_end, &state);
174         if (buf >= buf_end)
175             break;
176         --buf;
177         src_length = buf_end - buf;
178         }
179         switch (state & 0x1f) {
180         case NAL_SLICE:
181         case NAL_IDR_SLICE:
182             // Do not walk the whole buffer just to decode slice header
183             if (src_length > 20)
184                 src_length = 20;
185             break;
186         }
187         ptr = ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
188         if (ptr == NULL || dst_length < 0)
189             break;
190
191         init_get_bits(&h->gb, ptr, 8 * dst_length);
192         switch (h->nal_unit_type) {
193         case NAL_SPS:
194             ff_h264_decode_seq_parameter_set(h);
195             break;
196         case NAL_PPS:
197             ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
198             break;
199         case NAL_SEI:
200             ff_h264_decode_sei(h);
201             break;
202         case NAL_IDR_SLICE:
203             s->key_frame = 1;
204
205             h->prev_frame_num        = 0;
206             h->prev_frame_num_offset = 0;
207             h->prev_poc_msb          =
208             h->prev_poc_lsb          = 0;
209         /* fall through */
210         case NAL_SLICE:
211             get_ue_golomb_long(&h->gb);  // skip first_mb_in_slice
212             slice_type   = get_ue_golomb_31(&h->gb);
213             s->pict_type = golomb_to_pict_type[slice_type % 5];
214             if (h->sei_recovery_frame_cnt >= 0) {
215                 /* key frame, since recovery_frame_cnt is set */
216                 s->key_frame = 1;
217             }
218             pps_id = get_ue_golomb(&h->gb);
219             if (pps_id >= MAX_PPS_COUNT) {
220                 av_log(h->avctx, AV_LOG_ERROR,
221                        "pps_id out of range\n");
222                 return -1;
223             }
224             if (!h->pps_buffers[pps_id]) {
225                 av_log(h->avctx, AV_LOG_ERROR,
226                        "non-existing PPS referenced\n");
227                 return -1;
228             }
229             h->pps = *h->pps_buffers[pps_id];
230             if (!h->sps_buffers[h->pps.sps_id]) {
231                 av_log(h->avctx, AV_LOG_ERROR,
232                        "non-existing SPS referenced\n");
233                 return -1;
234             }
235             h->sps       = *h->sps_buffers[h->pps.sps_id];
236             h->frame_num = get_bits(&h->gb, h->sps.log2_max_frame_num);
237
238             avctx->profile = ff_h264_get_profile(&h->sps);
239             avctx->level   = h->sps.level_idc;
240
241             if (h->sps.frame_mbs_only_flag) {
242                 h->picture_structure = PICT_FRAME;
243             } else {
244                 if (get_bits1(&h->gb)) { // field_pic_flag
245                     h->picture_structure = PICT_TOP_FIELD + get_bits1(&h->gb); // bottom_field_flag
246                 } else {
247                     h->picture_structure = PICT_FRAME;
248                 }
249             }
250
251             if (h->nal_unit_type == NAL_IDR_SLICE)
252                 get_ue_golomb(&h->gb); /* idr_pic_id */
253             if (h->sps.poc_type == 0) {
254                 h->poc_lsb = get_bits(&h->gb, h->sps.log2_max_poc_lsb);
255
256                 if (h->pps.pic_order_present == 1 &&
257                     h->picture_structure == PICT_FRAME)
258                     h->delta_poc_bottom = get_se_golomb(&h->gb);
259             }
260
261             if (h->sps.poc_type == 1 &&
262                 !h->sps.delta_pic_order_always_zero_flag) {
263                 h->delta_poc[0] = get_se_golomb(&h->gb);
264
265                 if (h->pps.pic_order_present == 1 &&
266                     h->picture_structure == PICT_FRAME)
267                     h->delta_poc[1] = get_se_golomb(&h->gb);
268             }
269
270             ff_init_poc(h, field_poc, &s->output_picture_number);
271
272             if (h->sps.pic_struct_present_flag) {
273                 switch (h->sei_pic_struct) {
274                 case SEI_PIC_STRUCT_TOP_FIELD:
275                 case SEI_PIC_STRUCT_BOTTOM_FIELD:
276                     s->repeat_pict = 0;
277                     break;
278                 case SEI_PIC_STRUCT_FRAME:
279                 case SEI_PIC_STRUCT_TOP_BOTTOM:
280                 case SEI_PIC_STRUCT_BOTTOM_TOP:
281                     s->repeat_pict = 1;
282                     break;
283                 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
284                 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
285                     s->repeat_pict = 2;
286                     break;
287                 case SEI_PIC_STRUCT_FRAME_DOUBLING:
288                     s->repeat_pict = 3;
289                     break;
290                 case SEI_PIC_STRUCT_FRAME_TRIPLING:
291                     s->repeat_pict = 5;
292                     break;
293                 default:
294                     s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
295                     break;
296                 }
297             } else {
298                 s->repeat_pict = h->picture_structure == PICT_FRAME ? 1 : 0;
299             }
300
301             if (h->picture_structure == PICT_FRAME) {
302                 s->picture_structure = AV_PICTURE_STRUCTURE_FRAME;
303                 if (h->sps.pic_struct_present_flag) {
304                     switch (h->sei_pic_struct) {
305                     case SEI_PIC_STRUCT_TOP_BOTTOM:
306                     case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
307                         s->field_order = AV_FIELD_TT;
308                         break;
309                     case SEI_PIC_STRUCT_BOTTOM_TOP:
310                     case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
311                         s->field_order = AV_FIELD_BB;
312                         break;
313                     default:
314                         s->field_order = AV_FIELD_PROGRESSIVE;
315                         break;
316                     }
317                 } else {
318                     if (field_poc[0] < field_poc[1])
319                         s->field_order = AV_FIELD_TT;
320                     else if (field_poc[0] > field_poc[1])
321                         s->field_order = AV_FIELD_BB;
322                     else
323                         s->field_order = AV_FIELD_PROGRESSIVE;
324                 }
325             } else {
326                 if (h->picture_structure == PICT_TOP_FIELD)
327                     s->picture_structure = AV_PICTURE_STRUCTURE_TOP_FIELD;
328                 else
329                     s->picture_structure = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
330                 s->field_order = AV_FIELD_UNKNOWN;
331             }
332
333             return 0; /* no need to evaluate the rest */
334         }
335         buf += h->is_avc ? nalsize : consumed;
336     }
337     if (q264)
338         return 0;
339     /* didn't find a picture! */
340     av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
341     return -1;
342 }
343
344 static int h264_parse(AVCodecParserContext *s,
345                       AVCodecContext *avctx,
346                       const uint8_t **poutbuf, int *poutbuf_size,
347                       const uint8_t *buf, int buf_size)
348 {
349     H264Context *h   = s->priv_data;
350     ParseContext *pc = &h->parse_context;
351     int next;
352
353     if (!h->got_first) {
354         h->got_first = 1;
355         if (avctx->extradata_size) {
356             h->avctx = avctx;
357             // must be done like in decoder, otherwise opening the parser,
358             // letting it create extradata and then closing and opening again
359             // will cause has_b_frames to be always set.
360             // Note that estimate_timings_from_pts does exactly this.
361             if (!avctx->has_b_frames)
362                 h->low_delay = 1;
363             ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
364         }
365     }
366
367     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
368         next = buf_size;
369     } else {
370         next = h264_find_frame_end(h, buf, buf_size);
371
372         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
373             *poutbuf      = NULL;
374             *poutbuf_size = 0;
375             return buf_size;
376         }
377
378         if (next < 0 && next != END_NOT_FOUND) {
379             av_assert1(pc->last_index + next >= 0);
380             h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); // update state
381         }
382     }
383
384     parse_nal_units(s, avctx, buf, buf_size);
385
386     if (h->sei_cpb_removal_delay >= 0) {
387         s->dts_sync_point    = h->sei_buffering_period_present;
388         s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
389         s->pts_dts_delta     = h->sei_dpb_output_delay;
390     } else {
391         s->dts_sync_point    = INT_MIN;
392         s->dts_ref_dts_delta = INT_MIN;
393         s->pts_dts_delta     = INT_MIN;
394     }
395
396     if (s->flags & PARSER_FLAG_ONCE) {
397         s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
398     }
399
400     *poutbuf      = buf;
401     *poutbuf_size = buf_size;
402     return next;
403 }
404
405 static int h264_split(AVCodecContext *avctx,
406                       const uint8_t *buf, int buf_size)
407 {
408     int i;
409     uint32_t state = -1;
410     int has_sps    = 0;
411
412     for (i = 0; i <= buf_size; i++) {
413         if ((state & 0xFFFFFF1F) == 0x107)
414             has_sps = 1;
415         /*  if ((state&0xFFFFFF1F) == 0x101 ||
416          *     (state&0xFFFFFF1F) == 0x102 ||
417          *     (state&0xFFFFFF1F) == 0x105) {
418          *  }
419          */
420         if ((state & 0xFFFFFF00) == 0x100 && (state & 0xFFFFFF1F) != 0x107 &&
421             (state & 0xFFFFFF1F) != 0x108 && (state & 0xFFFFFF1F) != 0x109) {
422             if (has_sps) {
423                 while (i > 4 && buf[i - 5] == 0)
424                     i--;
425                 return i - 4;
426             }
427         }
428         if (i < buf_size)
429             state = (state << 8) | buf[i];
430     }
431     return 0;
432 }
433
434 static void close(AVCodecParserContext *s)
435 {
436     H264Context *h   = s->priv_data;
437     ParseContext *pc = &h->parse_context;
438
439     av_free(pc->buffer);
440     ff_h264_free_context(h);
441 }
442
443 static av_cold int init(AVCodecParserContext *s)
444 {
445     H264Context *h = s->priv_data;
446     h->thread_context[0]   = h;
447     h->slice_context_count = 1;
448     ff_h264dsp_init(&h->h264dsp, 8, 1);
449     return 0;
450 }
451
452 AVCodecParser ff_h264_parser = {
453     .codec_ids      = { AV_CODEC_ID_H264 },
454     .priv_data_size = sizeof(H264Context),
455     .parser_init    = init,
456     .parser_parse   = h264_parse,
457     .parser_close   = close,
458     .split          = h264_split,
459 };