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 = NULL;
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 goto err_out;
00063 s->parser = parser;
00064 s->priv_data = av_mallocz(parser->priv_data_size);
00065 if (!s->priv_data)
00066 goto err_out;
00067 s->fetch_timestamp=1;
00068 s->pict_type = AV_PICTURE_TYPE_I;
00069 if (parser->parser_init) {
00070 ret = parser->parser_init(s);
00071 if (ret != 0)
00072 goto err_out;
00073 }
00074 s->key_frame = -1;
00075 s->convergence_duration = 0;
00076 s->dts_sync_point = INT_MIN;
00077 s->dts_ref_dts_delta = INT_MIN;
00078 s->pts_dts_delta = INT_MIN;
00079 return s;
00080
00081 err_out:
00082 if (s)
00083 av_freep(&s->priv_data);
00084 av_free(s);
00085 return NULL;
00086 }
00087
00088 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove){
00089 int i;
00090
00091 s->dts= s->pts= AV_NOPTS_VALUE;
00092 s->pos= -1;
00093 s->offset= 0;
00094 for(i = 0; i < AV_PARSER_PTS_NB; i++) {
00095 if ( s->cur_offset + off >= s->cur_frame_offset[i]
00096 && (s->frame_offset < s->cur_frame_offset[i] ||
00097 (!s->frame_offset && !s->next_frame_offset))
00098
00099 && s->cur_frame_end[i]){
00100 s->dts= s->cur_frame_dts[i];
00101 s->pts= s->cur_frame_pts[i];
00102 s->pos= s->cur_frame_pos[i];
00103 s->offset = s->next_frame_offset - s->cur_frame_offset[i];
00104 if(remove)
00105 s->cur_frame_offset[i]= INT64_MAX;
00106 if(s->cur_offset + off < s->cur_frame_end[i])
00107 break;
00108 }
00109 }
00110 }
00111
00112 int av_parser_parse2(AVCodecParserContext *s,
00113 AVCodecContext *avctx,
00114 uint8_t **poutbuf, int *poutbuf_size,
00115 const uint8_t *buf, int buf_size,
00116 int64_t pts, int64_t dts,
00117 int64_t pos)
00118 {
00119 int index, i;
00120 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
00121
00122 if(!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
00123 s->next_frame_offset =
00124 s->cur_offset = pos;
00125 s->flags |= PARSER_FLAG_FETCHED_OFFSET;
00126 }
00127
00128 if (buf_size == 0) {
00129
00130 memset(dummy_buf, 0, sizeof(dummy_buf));
00131 buf = dummy_buf;
00132 } else if (s->cur_offset + buf_size !=
00133 s->cur_frame_end[s->cur_frame_start_index]) {
00134
00135 i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
00136 s->cur_frame_start_index = i;
00137 s->cur_frame_offset[i] = s->cur_offset;
00138 s->cur_frame_end[i] = s->cur_offset + buf_size;
00139 s->cur_frame_pts[i] = pts;
00140 s->cur_frame_dts[i] = dts;
00141 s->cur_frame_pos[i] = pos;
00142 }
00143
00144 if (s->fetch_timestamp){
00145 s->fetch_timestamp=0;
00146 s->last_pts = s->pts;
00147 s->last_dts = s->dts;
00148 s->last_pos = s->pos;
00149 ff_fetch_timestamp(s, 0, 0);
00150 }
00151
00152
00153 index = s->parser->parser_parse(s, avctx, (const uint8_t **)poutbuf, poutbuf_size, buf, buf_size);
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
00169 int av_parser_change(AVCodecParserContext *s,
00170 AVCodecContext *avctx,
00171 uint8_t **poutbuf, int *poutbuf_size,
00172 const uint8_t *buf, int buf_size, int keyframe){
00173
00174 if(s && s->parser->split){
00175 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
00176 int i= s->parser->split(avctx, buf, buf_size);
00177 buf += i;
00178 buf_size -= i;
00179 }
00180 }
00181
00182
00183 *poutbuf= (uint8_t *) buf;
00184 *poutbuf_size= buf_size;
00185 if(avctx->extradata){
00186 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
00187
00188 ){
00189 int size= buf_size + avctx->extradata_size;
00190 *poutbuf_size= size;
00191 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00192
00193 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
00194 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00195 return 1;
00196 }
00197 }
00198
00199 return 0;
00200 }
00201
00202 void av_parser_close(AVCodecParserContext *s)
00203 {
00204 if(s){
00205 if (s->parser->parser_close)
00206 s->parser->parser_close(s);
00207 av_free(s->priv_data);
00208 av_free(s);
00209 }
00210 }
00211
00212
00213
00214 int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
00215 {
00216 if(pc->overread){
00217 av_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
00218 pc->overread, pc->state, next, pc->index, pc->overread_index);
00219 av_dlog(NULL, "%X %X %X %X\n", (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
00220 }
00221
00222
00223 for(; pc->overread>0; pc->overread--){
00224 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
00225 }
00226
00227
00228 if(!*buf_size && next == END_NOT_FOUND){
00229 next= 0;
00230 }
00231
00232 pc->last_index= pc->index;
00233
00234
00235 if(next == END_NOT_FOUND){
00236 void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00237
00238 if(!new_buffer)
00239 return AVERROR(ENOMEM);
00240 pc->buffer = new_buffer;
00241 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
00242 pc->index += *buf_size;
00243 return -1;
00244 }
00245
00246 *buf_size=
00247 pc->overread_index= pc->index + next;
00248
00249
00250 if(pc->index){
00251 void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00252
00253 if(!new_buffer)
00254 return AVERROR(ENOMEM);
00255 pc->buffer = new_buffer;
00256 if (next > -FF_INPUT_BUFFER_PADDING_SIZE)
00257 memcpy(&pc->buffer[pc->index], *buf,
00258 next + FF_INPUT_BUFFER_PADDING_SIZE);
00259 pc->index = 0;
00260 *buf= pc->buffer;
00261 }
00262
00263
00264 for(;next < 0; next++){
00265 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
00266 pc->state64 = (pc->state64<<8) | pc->buffer[pc->last_index + next];
00267 pc->overread++;
00268 }
00269
00270 if(pc->overread){
00271 av_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
00272 pc->overread, pc->state, next, pc->index, pc->overread_index);
00273 av_dlog(NULL, "%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00274 }
00275
00276 return 0;
00277 }
00278
00279 void ff_parse_close(AVCodecParserContext *s)
00280 {
00281 ParseContext *pc = s->priv_data;
00282
00283 av_freep(&pc->buffer);
00284 }
00285
00286
00287
00288 int ff_mpeg4video_split(AVCodecContext *avctx,
00289 const uint8_t *buf, int buf_size)
00290 {
00291 int i;
00292 uint32_t state= -1;
00293
00294 for(i=0; i<buf_size; i++){
00295 state= (state<<8) | buf[i];
00296 if(state == 0x1B3 || state == 0x1B6)
00297 return i-3;
00298 }
00299 return 0;
00300 }