Libav 0.7.1
|
00001 /* 00002 * Audio and Video frame extraction 00003 * Copyright (c) 2003 Fabrice Bellard 00004 * Copyright (c) 2003 Michael Niedermayer 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 if (parser->priv_data_size) { 00062 s->priv_data = av_mallocz(parser->priv_data_size); 00063 if (!s->priv_data) { 00064 av_free(s); 00065 return NULL; 00066 } 00067 } 00068 if (parser->parser_init) { 00069 ret = parser->parser_init(s); 00070 if (ret != 0) { 00071 av_free(s->priv_data); 00072 av_free(s); 00073 return NULL; 00074 } 00075 } 00076 s->fetch_timestamp=1; 00077 s->pict_type = AV_PICTURE_TYPE_I; 00078 s->key_frame = -1; 00079 s->convergence_duration = 0; 00080 s->dts_sync_point = INT_MIN; 00081 s->dts_ref_dts_delta = INT_MIN; 00082 s->pts_dts_delta = INT_MIN; 00083 return s; 00084 } 00085 00086 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove){ 00087 int i; 00088 00089 s->dts= s->pts= AV_NOPTS_VALUE; 00090 s->pos= -1; 00091 s->offset= 0; 00092 for(i = 0; i < AV_PARSER_PTS_NB; i++) { 00093 if ( s->cur_offset + off >= s->cur_frame_offset[i] 00094 && (s->frame_offset < s->cur_frame_offset[i] || 00095 (!s->frame_offset && !s->next_frame_offset)) // first field/frame 00096 //check is disabled because mpeg-ts doesnt send complete PES packets 00097 && /*s->next_frame_offset + off <*/ s->cur_frame_end[i]){ 00098 s->dts= s->cur_frame_dts[i]; 00099 s->pts= s->cur_frame_pts[i]; 00100 s->pos= s->cur_frame_pos[i]; 00101 s->offset = s->next_frame_offset - s->cur_frame_offset[i]; 00102 if(remove) 00103 s->cur_frame_offset[i]= INT64_MAX; 00104 if(s->cur_offset + off < s->cur_frame_end[i]) 00105 break; 00106 } 00107 } 00108 } 00109 00110 int av_parser_parse2(AVCodecParserContext *s, 00111 AVCodecContext *avctx, 00112 uint8_t **poutbuf, int *poutbuf_size, 00113 const uint8_t *buf, int buf_size, 00114 int64_t pts, int64_t dts, 00115 int64_t pos) 00116 { 00117 int index, i; 00118 uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]; 00119 00120 if(!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) { 00121 s->next_frame_offset = 00122 s->cur_offset = pos; 00123 s->flags |= PARSER_FLAG_FETCHED_OFFSET; 00124 } 00125 00126 if (buf_size == 0) { 00127 /* padding is always necessary even if EOF, so we add it here */ 00128 memset(dummy_buf, 0, sizeof(dummy_buf)); 00129 buf = dummy_buf; 00130 } else if (s->cur_offset + buf_size != 00131 s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */ 00132 /* add a new packet descriptor */ 00133 i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1); 00134 s->cur_frame_start_index = i; 00135 s->cur_frame_offset[i] = s->cur_offset; 00136 s->cur_frame_end[i] = s->cur_offset + buf_size; 00137 s->cur_frame_pts[i] = pts; 00138 s->cur_frame_dts[i] = dts; 00139 s->cur_frame_pos[i] = pos; 00140 } 00141 00142 if (s->fetch_timestamp){ 00143 s->fetch_timestamp=0; 00144 s->last_pts = s->pts; 00145 s->last_dts = s->dts; 00146 s->last_pos = s->pos; 00147 ff_fetch_timestamp(s, 0, 0); 00148 } 00149 00150 /* WARNING: the returned index can be negative */ 00151 index = s->parser->parser_parse(s, avctx, (const uint8_t **)poutbuf, poutbuf_size, buf, buf_size); 00152 //av_log(NULL, AV_LOG_DEBUG, "parser: in:%"PRId64", %"PRId64", out:%"PRId64", %"PRId64", in:%d out:%d id:%d\n", pts, dts, s->last_pts, s->last_dts, buf_size, *poutbuf_size, avctx->codec_id); 00153 /* update the file pointer */ 00154 if (*poutbuf_size) { 00155 /* fill the data for the current frame */ 00156 s->frame_offset = s->next_frame_offset; 00157 00158 /* offset of the next frame */ 00159 s->next_frame_offset = s->cur_offset + index; 00160 s->fetch_timestamp=1; 00161 } 00162 if (index < 0) 00163 index = 0; 00164 s->cur_offset += index; 00165 return index; 00166 } 00167 00173 int av_parser_change(AVCodecParserContext *s, 00174 AVCodecContext *avctx, 00175 uint8_t **poutbuf, int *poutbuf_size, 00176 const uint8_t *buf, int buf_size, int keyframe){ 00177 00178 if(s && s->parser->split){ 00179 if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){ 00180 int i= s->parser->split(avctx, buf, buf_size); 00181 buf += i; 00182 buf_size -= i; 00183 } 00184 } 00185 00186 /* cast to avoid warning about discarding qualifiers */ 00187 *poutbuf= (uint8_t *) buf; 00188 *poutbuf_size= buf_size; 00189 if(avctx->extradata){ 00190 if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) 00191 /*||(s->pict_type != AV_PICTURE_TYPE_I && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/ 00192 /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){ 00193 int size= buf_size + avctx->extradata_size; 00194 *poutbuf_size= size; 00195 *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); 00196 00197 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size); 00198 memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE); 00199 return 1; 00200 } 00201 } 00202 00203 return 0; 00204 } 00205 00206 void av_parser_close(AVCodecParserContext *s) 00207 { 00208 if(s){ 00209 if (s->parser->parser_close) 00210 s->parser->parser_close(s); 00211 av_free(s->priv_data); 00212 av_free(s); 00213 } 00214 } 00215 00216 /*****************************************************/ 00217 00222 int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size) 00223 { 00224 if(pc->overread){ 00225 av_dlog(pc, "overread %d, state:%X next:%d index:%d o_index:%d\n", 00226 pc->overread, pc->state, next, pc->index, pc->overread_index); 00227 av_dlog(pc, "%X %X %X %X\n", (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]); 00228 } 00229 00230 /* Copy overread bytes from last frame into buffer. */ 00231 for(; pc->overread>0; pc->overread--){ 00232 pc->buffer[pc->index++]= pc->buffer[pc->overread_index++]; 00233 } 00234 00235 /* flush remaining if EOF */ 00236 if(!*buf_size && next == END_NOT_FOUND){ 00237 next= 0; 00238 } 00239 00240 pc->last_index= pc->index; 00241 00242 /* copy into buffer end return */ 00243 if(next == END_NOT_FOUND){ 00244 void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); 00245 00246 if(!new_buffer) 00247 return AVERROR(ENOMEM); 00248 pc->buffer = new_buffer; 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 /* append to buffer */ 00258 if(pc->index){ 00259 void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE); 00260 00261 if(!new_buffer) 00262 return AVERROR(ENOMEM); 00263 pc->buffer = new_buffer; 00264 memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE ); 00265 pc->index = 0; 00266 *buf= pc->buffer; 00267 } 00268 00269 /* store overread bytes */ 00270 for(;next < 0; next++){ 00271 pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next]; 00272 pc->state64 = (pc->state64<<8) | pc->buffer[pc->last_index + next]; 00273 pc->overread++; 00274 } 00275 00276 if(pc->overread){ 00277 av_dlog(pc, "overread %d, state:%X next:%d index:%d o_index:%d\n", 00278 pc->overread, pc->state, next, pc->index, pc->overread_index); 00279 av_dlog(pc, "%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]); 00280 } 00281 00282 return 0; 00283 } 00284 00285 void ff_parse_close(AVCodecParserContext *s) 00286 { 00287 ParseContext *pc = s->priv_data; 00288 00289 av_freep(&pc->buffer); 00290 } 00291 00292 void ff_parse1_close(AVCodecParserContext *s) 00293 { 00294 ParseContext1 *pc1 = s->priv_data; 00295 00296 av_free(pc1->pc.buffer); 00297 av_free(pc1->enc); 00298 } 00299 00300 /*************************/ 00301 00302 int ff_mpeg4video_split(AVCodecContext *avctx, 00303 const uint8_t *buf, int buf_size) 00304 { 00305 int i; 00306 uint32_t state= -1; 00307 00308 for(i=0; i<buf_size; i++){ 00309 state= (state<<8) | buf[i]; 00310 if(state == 0x1B3 || state == 0x1B6) 00311 return i-3; 00312 } 00313 return 0; 00314 }