00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "parser.h"
00024
00025 AVCodecParser *av_first_parser = NULL;
00026
00027 AVCodecParser* av_parser_next(AVCodecParser *p){
00028 if(p) return p->next;
00029 else return av_first_parser;
00030 }
00031
00032 void av_register_codec_parser(AVCodecParser *parser)
00033 {
00034 parser->next = av_first_parser;
00035 av_first_parser = parser;
00036 }
00037
00038 AVCodecParserContext *av_parser_init(int codec_id)
00039 {
00040 AVCodecParserContext *s;
00041 AVCodecParser *parser;
00042 int ret;
00043
00044 if(codec_id == CODEC_ID_NONE)
00045 return NULL;
00046
00047 for(parser = av_first_parser; parser != NULL; parser = parser->next) {
00048 if (parser->codec_ids[0] == codec_id ||
00049 parser->codec_ids[1] == codec_id ||
00050 parser->codec_ids[2] == codec_id ||
00051 parser->codec_ids[3] == codec_id ||
00052 parser->codec_ids[4] == codec_id)
00053 goto found;
00054 }
00055 return NULL;
00056 found:
00057 s = av_mallocz(sizeof(AVCodecParserContext));
00058 if (!s)
00059 return NULL;
00060 s->parser = parser;
00061 s->priv_data = av_mallocz(parser->priv_data_size);
00062 if (!s->priv_data) {
00063 av_free(s);
00064 return NULL;
00065 }
00066 if (parser->parser_init) {
00067 ret = parser->parser_init(s);
00068 if (ret != 0) {
00069 av_free(s->priv_data);
00070 av_free(s);
00071 return NULL;
00072 }
00073 }
00074 s->fetch_timestamp=1;
00075 s->pict_type = FF_I_TYPE;
00076 return s;
00077 }
00078
00079 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove){
00080 int i;
00081
00082 s->dts= s->pts= AV_NOPTS_VALUE;
00083 s->offset= 0;
00084 for(i = 0; i < AV_PARSER_PTS_NB; i++) {
00085 if ( s->next_frame_offset + off >= s->cur_frame_offset[i]
00086 &&(s-> frame_offset < s->cur_frame_offset[i] || !s->frame_offset)
00087
00088 && s->cur_frame_end[i]){
00089 s->dts= s->cur_frame_dts[i];
00090 s->pts= s->cur_frame_pts[i];
00091 s->offset = s->next_frame_offset - s->cur_frame_offset[i];
00092 if(remove)
00093 s->cur_frame_offset[i]= INT64_MAX;
00094 }
00095 }
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 int av_parser_parse(AVCodecParserContext *s,
00123 AVCodecContext *avctx,
00124 uint8_t **poutbuf, int *poutbuf_size,
00125 const uint8_t *buf, int buf_size,
00126 int64_t pts, int64_t dts)
00127 {
00128 int index, i;
00129 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
00130
00131 if (buf_size == 0) {
00132
00133 memset(dummy_buf, 0, sizeof(dummy_buf));
00134 buf = dummy_buf;
00135 } else {
00136
00137 if(pts != AV_NOPTS_VALUE || dts != AV_NOPTS_VALUE){
00138 i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
00139 s->cur_frame_start_index = i;
00140 s->cur_frame_offset[i] = s->cur_offset;
00141 s->cur_frame_end[i] = s->cur_offset + buf_size;
00142 s->cur_frame_pts[i] = pts;
00143 s->cur_frame_dts[i] = dts;
00144 }
00145 }
00146
00147 if (s->fetch_timestamp){
00148 s->fetch_timestamp=0;
00149 s->last_pts = s->pts;
00150 s->last_dts = s->dts;
00151 ff_fetch_timestamp(s, 0, 0);
00152 }
00153
00154
00155 index = s->parser->parser_parse(s, avctx, (const uint8_t **)poutbuf, poutbuf_size, buf, buf_size);
00156
00157
00158 if (*poutbuf_size) {
00159
00160 s->frame_offset = s->next_frame_offset;
00161
00162
00163 s->next_frame_offset = s->cur_offset + index;
00164 s->fetch_timestamp=1;
00165 }
00166 if (index < 0)
00167 index = 0;
00168 s->cur_offset += index;
00169 return index;
00170 }
00171
00172
00173
00174
00175
00176
00177 int av_parser_change(AVCodecParserContext *s,
00178 AVCodecContext *avctx,
00179 uint8_t **poutbuf, int *poutbuf_size,
00180 const uint8_t *buf, int buf_size, int keyframe){
00181
00182 if(s && s->parser->split){
00183 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
00184 int i= s->parser->split(avctx, buf, buf_size);
00185 buf += i;
00186 buf_size -= i;
00187 }
00188 }
00189
00190
00191 *poutbuf= (uint8_t *) buf;
00192 *poutbuf_size= buf_size;
00193 if(avctx->extradata){
00194 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
00195
00196 ){
00197 int size= buf_size + avctx->extradata_size;
00198 *poutbuf_size= size;
00199 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00200
00201 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
00202 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00203 return 1;
00204 }
00205 }
00206
00207 return 0;
00208 }
00209
00210 void av_parser_close(AVCodecParserContext *s)
00211 {
00212 if(s){
00213 if (s->parser->parser_close)
00214 s->parser->parser_close(s);
00215 av_free(s->priv_data);
00216 av_free(s);
00217 }
00218 }
00219
00220
00221
00222
00223
00224
00225
00226 int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
00227 {
00228 #if 0
00229 if(pc->overread){
00230 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
00231 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00232 }
00233 #endif
00234
00235
00236 for(; pc->overread>0; pc->overread--){
00237 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
00238 }
00239
00240
00241 if(!*buf_size && next == END_NOT_FOUND){
00242 next= 0;
00243 }
00244
00245 pc->last_index= pc->index;
00246
00247
00248 if(next == END_NOT_FOUND){
00249 void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00250
00251 if(!new_buffer)
00252 return AVERROR(ENOMEM);
00253 pc->buffer = new_buffer;
00254 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
00255 pc->index += *buf_size;
00256 return -1;
00257 }
00258
00259 *buf_size=
00260 pc->overread_index= pc->index + next;
00261
00262
00263 if(pc->index){
00264 void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00265
00266 if(!new_buffer)
00267 return AVERROR(ENOMEM);
00268 pc->buffer = new_buffer;
00269 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
00270 pc->index = 0;
00271 *buf= pc->buffer;
00272 }
00273
00274
00275 for(;next < 0; next++){
00276 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
00277 pc->overread++;
00278 }
00279
00280 #if 0
00281 if(pc->overread){
00282 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
00283 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00284 }
00285 #endif
00286
00287 return 0;
00288 }
00289
00290 void ff_parse_close(AVCodecParserContext *s)
00291 {
00292 ParseContext *pc = s->priv_data;
00293
00294 av_free(pc->buffer);
00295 }
00296
00297 void ff_parse1_close(AVCodecParserContext *s)
00298 {
00299 ParseContext1 *pc1 = s->priv_data;
00300
00301 av_free(pc1->pc.buffer);
00302 av_free(pc1->enc);
00303 }
00304
00305
00306
00307 int ff_mpeg4video_split(AVCodecContext *avctx,
00308 const uint8_t *buf, int buf_size)
00309 {
00310 int i;
00311 uint32_t state= -1;
00312
00313 for(i=0; i<buf_size; i++){
00314 state= (state<<8) | buf[i];
00315 if(state == 0x1B3 || state == 0x1B6)
00316 return i-3;
00317 }
00318 return 0;
00319 }