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 void av_register_codec_parser(AVCodecParser *parser)
00028 {
00029 parser->next = av_first_parser;
00030 av_first_parser = parser;
00031 }
00032
00033 AVCodecParserContext *av_parser_init(int codec_id)
00034 {
00035 AVCodecParserContext *s;
00036 AVCodecParser *parser;
00037 int ret;
00038
00039 if(codec_id == CODEC_ID_NONE)
00040 return NULL;
00041
00042 for(parser = av_first_parser; parser != NULL; parser = parser->next) {
00043 if (parser->codec_ids[0] == codec_id ||
00044 parser->codec_ids[1] == codec_id ||
00045 parser->codec_ids[2] == codec_id ||
00046 parser->codec_ids[3] == codec_id ||
00047 parser->codec_ids[4] == codec_id)
00048 goto found;
00049 }
00050 return NULL;
00051 found:
00052 s = av_mallocz(sizeof(AVCodecParserContext));
00053 if (!s)
00054 return NULL;
00055 s->parser = parser;
00056 s->priv_data = av_mallocz(parser->priv_data_size);
00057 if (!s->priv_data) {
00058 av_free(s);
00059 return NULL;
00060 }
00061 if (parser->parser_init) {
00062 ret = parser->parser_init(s);
00063 if (ret != 0) {
00064 av_free(s->priv_data);
00065 av_free(s);
00066 return NULL;
00067 }
00068 }
00069 s->fetch_timestamp=1;
00070 s->pict_type = FF_I_TYPE;
00071 return s;
00072 }
00073
00098 int av_parser_parse(AVCodecParserContext *s,
00099 AVCodecContext *avctx,
00100 uint8_t **poutbuf, int *poutbuf_size,
00101 const uint8_t *buf, int buf_size,
00102 int64_t pts, int64_t dts)
00103 {
00104 int index, i, k;
00105 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
00106
00107 if (buf_size == 0) {
00108
00109 memset(dummy_buf, 0, sizeof(dummy_buf));
00110 buf = dummy_buf;
00111 } else {
00112
00113 k = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
00114 s->cur_frame_start_index = k;
00115 s->cur_frame_offset[k] = s->cur_offset;
00116 s->cur_frame_pts[k] = pts;
00117 s->cur_frame_dts[k] = dts;
00118
00119
00120 if (s->fetch_timestamp){
00121 s->fetch_timestamp=0;
00122 s->last_pts = pts;
00123 s->last_dts = dts;
00124 s->last_offset = 0;
00125 s->cur_frame_pts[k] =
00126 s->cur_frame_dts[k] = AV_NOPTS_VALUE;
00127 }
00128 }
00129
00130
00131 index = s->parser->parser_parse(s, avctx, (const uint8_t **)poutbuf, poutbuf_size, buf, buf_size);
00132
00133
00134 if (*poutbuf_size) {
00135
00136 s->frame_offset = s->last_frame_offset;
00137 s->pts = s->last_pts;
00138 s->dts = s->last_dts;
00139 s->offset = s->last_offset;
00140
00141
00142 s->last_frame_offset = s->cur_offset + index;
00143
00144
00145
00146
00147
00148
00149 k = s->cur_frame_start_index;
00150 for(i = 0; i < AV_PARSER_PTS_NB; i++) {
00151 if (s->last_frame_offset >= s->cur_frame_offset[k])
00152 break;
00153 k = (k - 1) & (AV_PARSER_PTS_NB - 1);
00154 }
00155
00156 s->last_pts = s->cur_frame_pts[k];
00157 s->last_dts = s->cur_frame_dts[k];
00158 s->last_offset = s->last_frame_offset - s->cur_frame_offset[k];
00159
00160
00161
00162 if(index == buf_size){
00163 s->fetch_timestamp=1;
00164 }
00165 }
00166 if (index < 0)
00167 index = 0;
00168 s->cur_offset += index;
00169 return index;
00170 }
00171
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->parser->parser_close)
00213 s->parser->parser_close(s);
00214 av_free(s->priv_data);
00215 av_free(s);
00216 }
00217
00218
00219
00224 int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
00225 {
00226 #if 0
00227 if(pc->overread){
00228 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
00229 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00230 }
00231 #endif
00232
00233
00234 for(; pc->overread>0; pc->overread--){
00235 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
00236 }
00237
00238
00239 if(!*buf_size && next == END_NOT_FOUND){
00240 next= 0;
00241 }
00242
00243 pc->last_index= pc->index;
00244
00245
00246 if(next == END_NOT_FOUND){
00247 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00248
00249 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
00250 pc->index += *buf_size;
00251 return -1;
00252 }
00253
00254 *buf_size=
00255 pc->overread_index= pc->index + next;
00256
00257
00258 if(pc->index){
00259 pc->buffer= av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00260
00261 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
00262 pc->index = 0;
00263 *buf= pc->buffer;
00264 }
00265
00266
00267 for(;next < 0; next++){
00268 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
00269 pc->overread++;
00270 }
00271
00272 #if 0
00273 if(pc->overread){
00274 printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
00275 printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00276 }
00277 #endif
00278
00279 return 0;
00280 }
00281
00282 void ff_parse_close(AVCodecParserContext *s)
00283 {
00284 ParseContext *pc = s->priv_data;
00285
00286 av_free(pc->buffer);
00287 }
00288
00289 void ff_parse1_close(AVCodecParserContext *s)
00290 {
00291 ParseContext1 *pc1 = s->priv_data;
00292
00293 av_free(pc1->pc.buffer);
00294 av_free(pc1->enc);
00295 }
00296
00297
00298
00299 int ff_mpeg4video_split(AVCodecContext *avctx,
00300 const uint8_t *buf, int buf_size)
00301 {
00302 int i;
00303 uint32_t state= -1;
00304
00305 for(i=0; i<buf_size; i++){
00306 state= (state<<8) | buf[i];
00307 if(state == 0x1B3 || state == 0x1B6)
00308 return i-3;
00309 }
00310 return 0;
00311 }