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
00103 int av_parser_parse(AVCodecParserContext *s,
00104 AVCodecContext *avctx,
00105 uint8_t **poutbuf, int *poutbuf_size,
00106 const uint8_t *buf, int buf_size,
00107 int64_t pts, int64_t dts)
00108 {
00109 int index, i, k;
00110 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
00111
00112 if (buf_size == 0) {
00113
00114 memset(dummy_buf, 0, sizeof(dummy_buf));
00115 buf = dummy_buf;
00116 } else {
00117
00118 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
00119 s->cur_frame_start_index = k;
00120 s->cur_frame_offset[k] = s->cur_offset;
00121 s->cur_frame_pts[k] = pts;
00122 s->cur_frame_dts[k] = dts;
00123
00124
00125 if (s->fetch_timestamp){
00126 s->fetch_timestamp=0;
00127 s->last_pts = pts;
00128 s->last_dts = dts;
00129 s->last_offset = 0;
00130 s->cur_frame_pts[k] =
00131 s->cur_frame_dts[k] = AV_NOPTS_VALUE;
00132 }
00133 }
00134
00135
00136 index = s->parser->parser_parse(s, avctx, (const uint8_t **)poutbuf, poutbuf_size, buf, buf_size);
00137
00138
00139 if (*poutbuf_size) {
00140
00141 s->frame_offset = s->last_frame_offset;
00142 s->pts = s->last_pts;
00143 s->dts = s->last_dts;
00144 s->offset = s->last_offset;
00145
00146
00147 s->last_frame_offset = s->cur_offset + index;
00148
00149
00150
00151
00152
00153
00154 k = s->cur_frame_start_index;
00155 for(i = 0; i < AV_PARSER_PTS_NB; i++) {
00156 if (s->last_frame_offset >= s->cur_frame_offset[k])
00157 break;
00158 k = (k - 1) & (AV_PARSER_PTS_NB - 1);
00159 }
00160
00161 s->last_pts = s->cur_frame_pts[k];
00162 s->last_dts = s->cur_frame_dts[k];
00163 s->last_offset = s->last_frame_offset - s->cur_frame_offset[k];
00164
00165
00166
00167 if(index == buf_size){
00168 s->fetch_timestamp=1;
00169 }
00170 }
00171 if (index < 0)
00172 index = 0;
00173 s->cur_offset += index;
00174 return index;
00175 }
00176
00182 int av_parser_change(AVCodecParserContext *s,
00183 AVCodecContext *avctx,
00184 uint8_t **poutbuf, int *poutbuf_size,
00185 const uint8_t *buf, int buf_size, int keyframe){
00186
00187 if(s && s->parser->split){
00188 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
00189 int i= s->parser->split(avctx, buf, buf_size);
00190 buf += i;
00191 buf_size -= i;
00192 }
00193 }
00194
00195
00196 *poutbuf= (uint8_t *) buf;
00197 *poutbuf_size= buf_size;
00198 if(avctx->extradata){
00199 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
00200
00201 ){
00202 int size= buf_size + avctx->extradata_size;
00203 *poutbuf_size= size;
00204 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00205
00206 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
00207 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00208 return 1;
00209 }
00210 }
00211
00212 return 0;
00213 }
00214
00215 void av_parser_close(AVCodecParserContext *s)
00216 {
00217 if (s->parser->parser_close)
00218 s->parser->parser_close(s);
00219 av_free(s->priv_data);
00220 av_free(s);
00221 }
00222
00223
00224
00229 int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
00230 {
00231 #if 0
00232 if(pc->overread){
00233 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
00234 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00235 }
00236 #endif
00237
00238
00239 for(; pc->overread>0; pc->overread--){
00240 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
00241 }
00242
00243
00244 if(!*buf_size && next == END_NOT_FOUND){
00245 next= 0;
00246 }
00247
00248 pc->last_index= pc->index;
00249
00250
00251 if(next == END_NOT_FOUND){
00252 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00253
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 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00265
00266 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
00267 pc->index = 0;
00268 *buf= pc->buffer;
00269 }
00270
00271
00272 for(;next < 0; next++){
00273 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
00274 pc->overread++;
00275 }
00276
00277 #if 0
00278 if(pc->overread){
00279 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
00280 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00281 }
00282 #endif
00283
00284 return 0;
00285 }
00286
00287 void ff_parse_close(AVCodecParserContext *s)
00288 {
00289 ParseContext *pc = s->priv_data;
00290
00291 av_free(pc->buffer);
00292 }
00293
00294 void ff_parse1_close(AVCodecParserContext *s)
00295 {
00296 ParseContext1 *pc1 = s->priv_data;
00297
00298 av_free(pc1->pc.buffer);
00299 av_free(pc1->enc);
00300 }
00301
00302
00303
00304 int ff_mpeg4video_split(AVCodecContext *avctx,
00305 const uint8_t *buf, int buf_size)
00306 {
00307 int i;
00308 uint32_t state= -1;
00309
00310 for(i=0; i<buf_size; i++){
00311 state= (state<<8) | buf[i];
00312 if(state == 0x1B3 || state == 0x1B6)
00313 return i-3;
00314 }
00315 return 0;
00316 }