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