Mercurial > libavcodec.hg
annotate h264_parser.c @ 8442:60be1c7d8345 libavcodec
Make h264 parser 50% faster.
| author | michael |
|---|---|
| date | Tue, 23 Dec 2008 02:34:58 +0000 |
| parents | a38b31a2ac52 |
| children | 7c82ed8d4824 |
| 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 /** | |
| 23 * @file h264_parser.c | |
| 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){ | |
| 8442 | 47 #ifdef HAVE_FAST_UNALIGNED |
| 48 # ifdef HAVE_FAST_64BIT | |
| 49 while(i<buf_size && !((~*(uint64_t*)(buf+i) & (*(uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL)) | |
| 50 i+=8; | |
| 51 # else | |
| 52 while(i<buf_size && !((~*(uint32_t*)(buf+i) & (*(uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U)) | |
| 53 i+=4; | |
| 54 # endif | |
| 55 #endif | |
| 4975 | 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==7 || v==8 || v==9){ | |
| 69 if(pc->frame_start_found){ | |
| 70 i++; | |
| 7986 | 71 goto found; |
| 4975 | 72 } |
| 73 }else if(v==1 || v==2 || v==5){ | |
| 74 if(pc->frame_start_found){ | |
| 75 state+=8; | |
| 76 continue; | |
| 77 }else | |
| 78 pc->frame_start_found = 1; | |
| 79 } | |
| 80 state= 7; | |
| 81 }else{ | |
| 82 if(buf[i] & 0x80) | |
| 83 goto found; | |
| 84 state= 7; | |
| 85 } | |
| 86 } | |
| 87 pc->state= state; | |
| 88 return END_NOT_FOUND; | |
| 7986 | 89 |
| 90 found: | |
| 91 pc->state=7; | |
| 92 pc->frame_start_found= 0; | |
| 93 return i-(state&5); | |
| 4975 | 94 } |
| 95 | |
| 96 static int h264_parse(AVCodecParserContext *s, | |
| 97 AVCodecContext *avctx, | |
| 98 const uint8_t **poutbuf, int *poutbuf_size, | |
| 99 const uint8_t *buf, int buf_size) | |
| 100 { | |
| 101 H264Context *h = s->priv_data; | |
| 102 ParseContext *pc = &h->s.parse_context; | |
| 103 int next; | |
| 104 | |
| 105 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ | |
| 106 next= buf_size; | |
| 107 }else{ | |
| 108 next= ff_h264_find_frame_end(h, buf, buf_size); | |
| 109 | |
| 110 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { | |
| 111 *poutbuf = NULL; | |
| 112 *poutbuf_size = 0; | |
| 113 return buf_size; | |
| 114 } | |
| 115 | |
| 116 if(next<0 && next != END_NOT_FOUND){ | |
| 117 assert(pc->last_index + next >= 0 ); | |
| 118 ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 *poutbuf = buf; | |
| 123 *poutbuf_size = buf_size; | |
| 124 return next; | |
| 125 } | |
| 126 | |
| 127 static int h264_split(AVCodecContext *avctx, | |
| 128 const uint8_t *buf, int buf_size) | |
| 129 { | |
| 130 int i; | |
| 131 uint32_t state = -1; | |
| 132 int has_sps= 0; | |
| 133 | |
| 134 for(i=0; i<=buf_size; i++){ | |
| 135 if((state&0xFFFFFF1F) == 0x107) | |
| 136 has_sps=1; | |
| 137 /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){ | |
| 138 }*/ | |
| 139 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){ | |
| 140 if(has_sps){ | |
| 141 while(i>4 && buf[i-5]==0) i--; | |
| 142 return i-4; | |
| 143 } | |
| 144 } | |
| 145 if (i<buf_size) | |
| 146 state= (state<<8) | buf[i]; | |
| 147 } | |
| 148 return 0; | |
| 149 } | |
| 150 | |
| 7991 | 151 static void close(AVCodecParserContext *s) |
|
7989
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
152 { |
|
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
153 H264Context *h = s->priv_data; |
|
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
154 ParseContext *pc = &h->s.parse_context; |
|
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 av_free(pc->buffer); |
|
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
157 } |
|
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
158 |
| 4975 | 159 |
| 160 AVCodecParser h264_parser = { | |
| 161 { CODEC_ID_H264 }, | |
| 162 sizeof(H264Context), | |
| 163 NULL, | |
| 164 h264_parse, | |
|
7989
0607ff0877ff
ff_parse_close() is not the correct function for H264Context.
michael
parents:
7986
diff
changeset
|
165 close, |
| 4975 | 166 h264_split, |
| 167 }; |
