Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__H264__App
view libavcodec/h264_parser.c @ 2:897f711a7157
rearrange to work with autoconf
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Tue, 25 Sep 2012 15:55:33 +0200 |
| parents | |
| children |
line source
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 */
22 /**
23 * @file
24 * H.264 / AVC / MPEG4 part10 parser.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
28 #include <unistd.h>
30 #include "golomb.h"
31 #include "libavutil/error.h"
32 #include "h264_types.h"
34 #undef NDEBUG
35 #include <assert.h>
37 #define END_NOT_FOUND (-100)
39 static int ff_h264_find_frame_end(ParserContext *s, const uint8_t *buf, int buf_size)
40 {
41 int i;
42 uint32_t state;
44 state= s->state;
45 if(state>13)
46 state= 7;
48 for(i=0; i<buf_size; i++){
49 if(state==7){
50 /* we check i<buf_size instead of i+3/7 because its simpler
51 * and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end
52 */
53 while(i<buf_size && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
54 i+=8;
56 for(; i<buf_size; i++){
57 if(!buf[i]){
58 state=2;
59 break;
60 }
61 }
62 }else if(state<=2){
63 if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
64 else if(buf[i]) state = 7;
65 else state>>=1; //2->1, 1->0, 0->0
66 }else if(state<=5){
67 int v= buf[i] & 0x1F;
68 if(v==6 || v==7 || v==8 || v==9){
69 if(s->frame_start_found){
70 i++;
71 goto found;
72 }
73 }else if(v==1 || v==2 || v==5){
74 if(s->frame_start_found){
75 state+=8;
76 continue;
77 }else
78 s->frame_start_found = 1;
79 }
80 state= 7;
81 }else{
82 if(buf[i] & 0x80)
83 goto found;
84 state= 7;
85 }
86 }
87 s->state= state;
88 return END_NOT_FOUND;
90 found:
91 s->state=7;
92 s->frame_start_found= 0;
93 return i-(state&5);
94 }
96 static int ff_combine_frame(ParserContext *s, GetBitContext *gb, int next, uint8_t **buf, int *buf_size)
97 {
98 int i;
99 /* Copy overread bytes from last frame into buffer. */
100 for(i =0; s->overread_cnt>0; s->overread_cnt--, i++){
101 gb->raw[s->index++]= s->overread[i];
102 }
104 /* EOF - END_NOT_FOUND means no next frame start is found in current partial read. If buf_size of the partial read is 0 we are at EOF */
105 if(!*buf_size && next == END_NOT_FOUND){
106 next= 0;
107 }
108 s->last_index= s->index;
110 /* copy into buffer end return */
111 if(next == END_NOT_FOUND){
112 gb->raw = av_fast_realloc(gb->raw, &gb->alloc_size, (*buf_size) + s->index + FF_INPUT_BUFFER_PADDING_SIZE);
113 memcpy(&gb->raw[s->index], *buf, *buf_size);
114 s->index += *buf_size;
115 return -1;
116 }
118 ///end found
119 *buf_size= s->index + next;
120 /* append to buffer */
122 gb->raw = av_fast_realloc(gb->raw, &gb->alloc_size, next + s->index + FF_INPUT_BUFFER_PADDING_SIZE);
123 memcpy(&gb->raw[s->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
124 s->index = 0;
126 /* store overread bytes */
127 for(i=0; next < 0; next++, i++){
128 s->state = (s->state<<8) | gb->raw[s->last_index + next];
129 s->overread[i] = gb->raw[s->last_index + next];
130 s->overread_cnt++;
131 }
133 return 0;
134 }
136 static int h264_parse(ParserContext *s, GetBitContext *gb,
137 uint8_t *buf, int buf_size)
138 {
139 int next;
141 next= ff_h264_find_frame_end(s, buf, buf_size);
143 if (ff_combine_frame(s, gb, next, &buf, &buf_size) < 0) {
144 gb->buf_size = 0;
145 return buf_size;
146 }
148 if(next<0 && next != END_NOT_FOUND){
149 assert(s->last_index + next >= 0 );
150 ff_h264_find_frame_end(s, &gb->raw[s->last_index + next], -next); //update state
151 }
153 gb->buf_size = buf_size;
154 return next;
155 }
157 static int ff_raw_read_partial_packet(ParserContext *pc)
158 {
159 int len= -1;
161 if (!pc->eof_reached){
162 len = read( pc->ifile, pc->data, pc->buffer_size);
163 // printf("read task %d\t%d\n", pc->ifile, len); fflush(NULL);
164 if (len < pc->buffer_size) {
165 pc->eof_reached = 1;
166 }
167 }
169 return len;
170 }
172 void av_read_frame_internal(ParserContext *pc, GetBitContext *gb){
173 int len;
174 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]={0};
175 av_fast_malloc(&gb->raw, &gb->alloc_size, 2048+FF_INPUT_BUFFER_PADDING_SIZE);
177 //Parsing is performed before read, since there are ussually leftovers from parsing the previous frame.
178 for(;;) {
179 if (pc->cur_len>0){
180 len = h264_parse(pc, gb, pc->cur_ptr, pc->cur_len);
181 if (len<0)
182 len =0;
183 //* increment read pointer */
184 pc->cur_ptr += len;
185 pc->cur_len -= len;
187 if (gb->buf_size) {
188 break;
189 }
190 }
192 //check for ret and not parser->eof_reached as one "read" can contain more than 1 frame
193 pc->size= ff_raw_read_partial_packet(pc);
194 if (pc->size < 0) {
195 pc->final_frame =1;
196 /* return the last frames, if any */
197 h264_parse(pc, gb, dummy_buf, 0);
198 break;
199 }
200 pc->cur_ptr = pc->data;
201 pc->cur_len = pc->size;
202 }
204 assert(gb->raw!=NULL);
206 }
208 ParserContext *get_parse_context(int ifile){
209 ParserContext *pc = av_mallocz(sizeof(ParserContext));
210 pc->buffer_size = 2048;
211 pc->final_frame = 0;
212 pc->cur_len= 0;
213 pc->data = av_mallocz(2048 + FF_INPUT_BUFFER_PADDING_SIZE);
214 pc->size = 2048;
215 pc->eof_reached =0;
216 pc->ifile = ifile;
218 return pc;
219 }
221 void free_parse_context(ParserContext *pc){
222 av_free(pc->data);
223 av_free(pc);
224 }
