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 static 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 = AV_PICTURE_TYPE_I;
00076 s->key_frame = -1;
00077 s->convergence_duration = 0;
00078 s->dts_sync_point = INT_MIN;
00079 s->dts_ref_dts_delta = INT_MIN;
00080 s->pts_dts_delta = INT_MIN;
00081 return s;
00082 }
00083
00084 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove){
00085 int i;
00086
00087 s->dts= s->pts= AV_NOPTS_VALUE;
00088 s->pos= -1;
00089 s->offset= 0;
00090 for(i = 0; i < AV_PARSER_PTS_NB; i++) {
00091 if ( s->cur_offset + off >= s->cur_frame_offset[i]
00092 && (s->frame_offset < s->cur_frame_offset[i] ||
00093 (!s->frame_offset && !s->next_frame_offset))
00094
00095 && s->cur_frame_end[i]){
00096 s->dts= s->cur_frame_dts[i];
00097 s->pts= s->cur_frame_pts[i];
00098 s->pos= s->cur_frame_pos[i];
00099 s->offset = s->next_frame_offset - s->cur_frame_offset[i];
00100 if(remove)
00101 s->cur_frame_offset[i]= INT64_MAX;
00102 if(s->cur_offset + off < s->cur_frame_end[i])
00103 break;
00104 }
00105 }
00106 }
00107
00108 #if LIBAVCODEC_VERSION_MAJOR < 53
00109
00135 int av_parser_parse(AVCodecParserContext *s,
00136 AVCodecContext *avctx,
00137 uint8_t **poutbuf, int *poutbuf_size,
00138 const uint8_t *buf, int buf_size,
00139 int64_t pts, int64_t dts)
00140 {
00141 return av_parser_parse2(s, avctx, poutbuf, poutbuf_size, buf, buf_size, pts, dts, AV_NOPTS_VALUE);
00142 }
00143 #endif
00144
00145 int av_parser_parse2(AVCodecParserContext *s,
00146 AVCodecContext *avctx,
00147 uint8_t **poutbuf, int *poutbuf_size,
00148 const uint8_t *buf, int buf_size,
00149 int64_t pts, int64_t dts,
00150 int64_t pos)
00151 {
00152 int index, i;
00153 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
00154
00155 if(!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
00156 s->next_frame_offset =
00157 s->cur_offset = pos;
00158 s->flags |= PARSER_FLAG_FETCHED_OFFSET;
00159 }
00160
00161 if (buf_size == 0) {
00162
00163 memset(dummy_buf, 0, sizeof(dummy_buf));
00164 buf = dummy_buf;
00165 } else if (s->cur_offset + buf_size !=
00166 s->cur_frame_end[s->cur_frame_start_index]) {
00167
00168 i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
00169 s->cur_frame_start_index = i;
00170 s->cur_frame_offset[i] = s->cur_offset;
00171 s->cur_frame_end[i] = s->cur_offset + buf_size;
00172 s->cur_frame_pts[i] = pts;
00173 s->cur_frame_dts[i] = dts;
00174 s->cur_frame_pos[i] = pos;
00175 }
00176
00177 if (s->fetch_timestamp){
00178 s->fetch_timestamp=0;
00179 s->last_pts = s->pts;
00180 s->last_dts = s->dts;
00181 s->last_pos = s->pos;
00182 ff_fetch_timestamp(s, 0, 0);
00183 }
00184
00185
00186 index = s->parser->parser_parse(s, avctx, (const uint8_t **)poutbuf, poutbuf_size, buf, buf_size);
00187
00188
00189 if (*poutbuf_size) {
00190
00191 s->frame_offset = s->next_frame_offset;
00192
00193
00194 s->next_frame_offset = s->cur_offset + index;
00195 s->fetch_timestamp=1;
00196 }
00197 if (index < 0)
00198 index = 0;
00199 s->cur_offset += index;
00200 return index;
00201 }
00202
00208 int av_parser_change(AVCodecParserContext *s,
00209 AVCodecContext *avctx,
00210 uint8_t **poutbuf, int *poutbuf_size,
00211 const uint8_t *buf, int buf_size, int keyframe){
00212
00213 if(s && s->parser->split){
00214 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
00215 int i= s->parser->split(avctx, buf, buf_size);
00216 buf += i;
00217 buf_size -= i;
00218 }
00219 }
00220
00221
00222 *poutbuf= (uint8_t *) buf;
00223 *poutbuf_size= buf_size;
00224 if(avctx->extradata){
00225 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
00226
00227 ){
00228 int size= buf_size + avctx->extradata_size;
00229 *poutbuf_size= size;
00230 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00231
00232 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
00233 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00234 return 1;
00235 }
00236 }
00237
00238 return 0;
00239 }
00240
00241 void av_parser_close(AVCodecParserContext *s)
00242 {
00243 if(s){
00244 if (s->parser->parser_close)
00245 s->parser->parser_close(s);
00246 av_free(s->priv_data);
00247 av_free(s);
00248 }
00249 }
00250
00251
00252
00257 int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
00258 {
00259 #if 0
00260 if(pc->overread){
00261 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
00262 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00263 }
00264 #endif
00265
00266
00267 for(; pc->overread>0; pc->overread--){
00268 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
00269 }
00270
00271
00272 if(!*buf_size && next == END_NOT_FOUND){
00273 next= 0;
00274 }
00275
00276 pc->last_index= pc->index;
00277
00278
00279 if(next == END_NOT_FOUND){
00280 void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00281
00282 if(!new_buffer)
00283 return AVERROR(ENOMEM);
00284 pc->buffer = new_buffer;
00285 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
00286 pc->index += *buf_size;
00287 return -1;
00288 }
00289
00290 *buf_size=
00291 pc->overread_index= pc->index + next;
00292
00293
00294 if(pc->index){
00295 void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00296
00297 if(!new_buffer)
00298 return AVERROR(ENOMEM);
00299 pc->buffer = new_buffer;
00300 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
00301 pc->index = 0;
00302 *buf= pc->buffer;
00303 }
00304
00305
00306 for(;next < 0; next++){
00307 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
00308 pc->state64 = (pc->state64<<8) | pc->buffer[pc->last_index + next];
00309 pc->overread++;
00310 }
00311
00312 if(pc->overread){
00313 av_dlog(pc, "overread %d, state:%X next:%d index:%d o_index:%d\n",
00314 pc->overread, pc->state, next, pc->index, pc->overread_index);
00315 av_dlog(pc, "%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00316 }
00317
00318 return 0;
00319 }
00320
00321 void ff_parse_close(AVCodecParserContext *s)
00322 {
00323 ParseContext *pc = s->priv_data;
00324
00325 av_freep(&pc->buffer);
00326 }
00327
00328 void ff_parse1_close(AVCodecParserContext *s)
00329 {
00330 ParseContext1 *pc1 = s->priv_data;
00331
00332 av_free(pc1->pc.buffer);
00333 av_free(pc1->enc);
00334 }
00335
00336
00337
00338 int ff_mpeg4video_split(AVCodecContext *avctx,
00339 const uint8_t *buf, int buf_size)
00340 {
00341 int i;
00342 uint32_t state= -1;
00343
00344 for(i=0; i<buf_size; i++){
00345 state= (state<<8) | buf[i];
00346 if(state == 0x1B3 || state == 0x1B6)
00347 return i-3;
00348 }
00349 return 0;
00350 }