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