Mercurial > libavcodec.hg
annotate h264_parser.c @ 8989:cdd8fa4a362a libavcodec
Add convergence_duration to AVCodecParserContext.
Patch by Ivan Schreter, schreter gmx net
author | cehoyos |
---|---|
date | Thu, 19 Feb 2009 23:35:59 +0000 |
parents | 8551f8149146 |
children | 1c69c50d78f1 |
rev | line source |
---|---|
4975 | 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 /** | |
8718
e9d9d946f213
Use full internal pathname in doxygen @file directives.
diego
parents:
8610
diff
changeset
|
23 * @file libavcodec/h264_parser.c |
4975 | 24 * H.264 / AVC / MPEG4 part10 parser. |
25 * @author Michael Niedermayer <michaelni@gmx.at> | |
26 */ | |
27 | |
28 #include "parser.h" | |
29 #include "h264_parser.h" | |
30 | |
31 #include <assert.h> | |
32 | |
33 | |
34 int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size) | |
35 { | |
36 int i; | |
37 uint32_t state; | |
38 ParseContext *pc = &(h->s.parse_context); | |
39 //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]); | |
40 // mb_addr= pc->mb_addr - 1; | |
41 state= pc->state; | |
42 if(state>13) | |
43 state= 7; | |
44 | |
45 for(i=0; i<buf_size; i++){ | |
46 if(state==7){ | |
8590 | 47 #if HAVE_FAST_UNALIGNED |
8503 | 48 /* we check i<buf_size instead of i+3/7 because its simpler |
49 * and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end | |
50 */ | |
8590 | 51 # if HAVE_FAST_64BIT |
8763 | 52 while(i<buf_size && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL)) |
8442 | 53 i+=8; |
54 # else | |
8763 | 55 while(i<buf_size && !((~*(const uint32_t*)(buf+i) & (*(const uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U)) |
8442 | 56 i+=4; |
57 # endif | |
58 #endif | |
4975 | 59 for(; i<buf_size; i++){ |
60 if(!buf[i]){ | |
61 state=2; | |
62 break; | |
63 } | |
64 } | |
65 }else if(state<=2){ | |
66 if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5 | |
67 else if(buf[i]) state = 7; | |
68 else state>>=1; //2->1, 1->0, 0->0 | |
69 }else if(state<=5){ | |
70 int v= buf[i] & 0x1F; | |
71 if(v==7 || v==8 || v==9){ | |
72 if(pc->frame_start_found){ | |
73 i++; | |
7986 | 74 goto found; |
4975 | 75 } |
76 }else if(v==1 || v==2 || v==5){ | |
77 if(pc->frame_start_found){ | |
78 state+=8; | |
79 continue; | |
80 }else | |
81 pc->frame_start_found = 1; | |
82 } | |
83 state= 7; | |
84 }else{ | |
85 if(buf[i] & 0x80) | |
86 goto found; | |
87 state= 7; | |
88 } | |
89 } | |
90 pc->state= state; | |
91 return END_NOT_FOUND; | |
7986 | 92 |
93 found: | |
94 pc->state=7; | |
95 pc->frame_start_found= 0; | |
96 return i-(state&5); | |
4975 | 97 } |
98 | |
99 static int h264_parse(AVCodecParserContext *s, | |
100 AVCodecContext *avctx, | |
101 const uint8_t **poutbuf, int *poutbuf_size, | |
102 const uint8_t *buf, int buf_size) | |
103 { | |
104 H264Context *h = s->priv_data; | |
105 ParseContext *pc = &h->s.parse_context; | |
106 int next; | |
107 | |
108 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ | |
109 next= buf_size; | |
110 }else{ | |
111 next= ff_h264_find_frame_end(h, buf, buf_size); | |
112 | |
113 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { | |
114 *poutbuf = NULL; | |
115 *poutbuf_size = 0; | |
116 return buf_size; | |
117 } | |
118 | |
119 if(next<0 && next != END_NOT_FOUND){ | |
120 assert(pc->last_index + next >= 0 ); | |
121 ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state | |
122 } | |
123 } | |
124 | |
125 *poutbuf = buf; | |
126 *poutbuf_size = buf_size; | |
127 return next; | |
128 } | |
129 | |
130 static int h264_split(AVCodecContext *avctx, | |
131 const uint8_t *buf, int buf_size) | |
132 { | |
133 int i; | |
134 uint32_t state = -1; | |
135 int has_sps= 0; | |
136 | |
137 for(i=0; i<=buf_size; i++){ | |
138 if((state&0xFFFFFF1F) == 0x107) | |
139 has_sps=1; | |
140 /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){ | |
141 }*/ | |
142 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){ | |
143 if(has_sps){ | |
144 while(i>4 && buf[i-5]==0) i--; | |
145 return i-4; | |
146 } | |
147 } | |
148 if (i<buf_size) | |
149 state= (state<<8) | buf[i]; | |
150 } | |
151 return 0; | |
152 } | |
153 | |
7991 | 154 static void close(AVCodecParserContext *s) |
7989
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
155 { |
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
156 H264Context *h = s->priv_data; |
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
157 ParseContext *pc = &h->s.parse_context; |
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
158 |
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
159 av_free(pc->buffer); |
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
160 } |
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
161 |
4975 | 162 |
163 AVCodecParser h264_parser = { | |
8610 | 164 { CODEC_ID_H264 }, |
4975 | 165 sizeof(H264Context), |
166 NULL, | |
167 h264_parse, | |
7989
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
168 close, |
4975 | 169 h264_split, |
170 }; |