Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__H264__App
comparison libavcodec/h264_parser.c @ 9:ea1ba68cf0ed
update to match api changes + add sscc produced source
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Wed, 05 Jun 2013 14:43:26 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:c61522d93710 |
|---|---|
| 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 #include <unistd.h> | |
| 29 | |
| 30 #include "golomb.h" | |
| 31 #include "libavutil/error.h" | |
| 32 #include "h264_types.h" | |
| 33 | |
| 34 #undef NDEBUG | |
| 35 #include <assert.h> | |
| 36 | |
| 37 #define END_NOT_FOUND (-100) | |
| 38 | |
| 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; | |
| 43 | |
| 44 state= s->state; | |
| 45 if(state>13) | |
| 46 state= 7; | |
| 47 | |
| 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; | |
| 55 | |
| 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; | |
| 89 | |
| 90 found: | |
| 91 s->state=7; | |
| 92 s->frame_start_found= 0; | |
| 93 return i-(state&5); | |
| 94 } | |
| 95 | |
| 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 } | |
| 103 | |
| 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; | |
| 109 | |
| 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 } | |
| 117 | |
| 118 ///end found | |
| 119 *buf_size= s->index + next; | |
| 120 /* append to buffer */ | |
| 121 | |
| 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; | |
| 125 | |
| 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 } | |
| 132 | |
| 133 return 0; | |
| 134 } | |
| 135 | |
| 136 static int h264_parse(ParserContext *s, GetBitContext *gb, | |
| 137 uint8_t *buf, int buf_size) | |
| 138 { | |
| 139 int next; | |
| 140 | |
| 141 next= ff_h264_find_frame_end(s, buf, buf_size); | |
| 142 | |
| 143 if (ff_combine_frame(s, gb, next, &buf, &buf_size) < 0) { | |
| 144 gb->buf_size = 0; | |
| 145 return buf_size; | |
| 146 } | |
| 147 | |
| 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 } | |
| 152 | |
| 153 gb->buf_size = buf_size; | |
| 154 return next; | |
| 155 } | |
| 156 | |
| 157 static int ff_raw_read_partial_packet(ParserContext *pc) | |
| 158 { | |
| 159 int len= -1; | |
| 160 | |
| 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 } | |
| 168 | |
| 169 return len; | |
| 170 } | |
| 171 | |
| 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); | |
| 176 | |
| 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; | |
| 186 | |
| 187 if (gb->buf_size) { | |
| 188 break; | |
| 189 } | |
| 190 } | |
| 191 | |
| 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 } | |
| 203 | |
| 204 assert(gb->raw!=NULL); | |
| 205 | |
| 206 } | |
| 207 | |
| 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; | |
| 217 | |
| 218 return pc; | |
| 219 } | |
| 220 | |
| 221 void free_parse_context(ParserContext *pc){ | |
| 222 av_free(pc->data); | |
| 223 av_free(pc); | |
| 224 } |
