/** * Find the end of the current frame in the bitstream. * @return the position of the first byte of the next frame, or -1 */ int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s) { int i; uint32_t state = pc->state; /* EOF considered as end of frame */ if (buf_size == 0) return 0; /* 0 frame start -> 1/4 1 first_SEQEXT -> 0/2 2 first field start -> 3/0 3 second_SEQEXT -> 2/0 4 searching end */ for (i = 0; i < buf_size; i++) { av_assert1(pc->frame_start_found >= 0 && pc->frame_start_found <= 4); if (pc->frame_start_found & 1) { if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80) pc->frame_start_found--; else if (state == EXT_START_CODE + 2) { if ((buf[i] & 3) == 3) pc->frame_start_found = 0; else pc->frame_start_found = (pc->frame_start_found + 1) & 3; } state++; } else { i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1; if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) { i++; pc->frame_start_found = 4; } if (state == SEQ_END_CODE) { pc->frame_start_found = 0; pc->state=-1; return i+1; } if (pc->frame_start_found == 2 && state == SEQ_START_CODE) pc->frame_start_found = 0; if (pc->frame_start_found < 4 && state == EXT_START_CODE) pc->frame_start_found++; if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) { if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) { pc->frame_start_found = 0; pc->state = -1; return i - 3; } } if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) { ff_fetch_timestamp(s, i - 3, 1); } } } pc->state = state; return END_NOT_FOUND; }
/** * * @param buf input * @param buf_size input length, to signal EOF, this should be 0 (so that the last frame can be output) * @param pts input presentation timestamp * @param dts input decoding timestamp * @param poutbuf will contain a pointer to the first byte of the output frame * @param poutbuf_size will contain the length of the output frame * @return the number of bytes of the input bitstream used * * Example: * @code * while(in_len){ * len = av_parser_parse(myparser, AVCodecContext, &data, &size, * in_data, in_len, * pts, dts); * in_data += len; * in_len -= len; * * if(size) * decode_frame(data, size); * } * @endcode */ int av_parser_parse( AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts ) { LogStr("Init"); int index, i; uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]; if (buf_size == 0) { /* padding is always necessary even if EOF, so we add it here */ memset(dummy_buf, 0, sizeof(dummy_buf)); buf = dummy_buf; } else { /* add a new packet descriptor */ if (pts != AV_NOPTS_VALUE || dts != AV_NOPTS_VALUE) { i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1); s->cur_frame_start_index = i; s->cur_frame_offset[i] = s->cur_offset; s->cur_frame_end[i] = s->cur_offset + buf_size; s->cur_frame_pts[i] = pts; s->cur_frame_dts[i] = dts; } } if (s->fetch_timestamp) { s->fetch_timestamp = 0; s->last_pts = s->pts; s->last_dts = s->dts; ff_fetch_timestamp(s, 0, 0); } /* WARNING: the returned index can be negative */ index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf, poutbuf_size, buf, buf_size); //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); /* update the file pointer */ if (*poutbuf_size) { /* fill the data for the current frame */ s->frame_offset = s->next_frame_offset; /* offset of the next frame */ s->next_frame_offset = s->cur_offset + index; s->fetch_timestamp = 1; } if (index < 0) { index = 0; } s->cur_offset += index; LogStr("Exit"); return index; }
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts, int64_t pos) { int index, i; uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE]; if(!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) { s->next_frame_offset = s->cur_offset = pos; s->flags |= PARSER_FLAG_FETCHED_OFFSET; } if (buf_size == 0) { /* padding is always necessary even if EOF, so we add it here */ memset(dummy_buf, 0, sizeof(dummy_buf)); buf = dummy_buf; } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */ /* add a new packet descriptor */ i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1); s->cur_frame_start_index = i; s->cur_frame_offset[i] = s->cur_offset; s->cur_frame_end[i] = s->cur_offset + buf_size; s->cur_frame_pts[i] = pts; s->cur_frame_dts[i] = dts; s->cur_frame_pos[i] = pos; } if (s->fetch_timestamp){ s->fetch_timestamp=0; s->last_pts = s->pts; s->last_dts = s->dts; s->last_pos = s->pos; ff_fetch_timestamp(s, 0, 0); } /* WARNING: the returned index can be negative */ index = s->parser->parser_parse(s, avctx, (const uint8_t **)poutbuf, poutbuf_size, buf, buf_size); /* update the file pointer */ if (*poutbuf_size) { /* fill the data for the current frame */ s->frame_offset = s->next_frame_offset; /* offset of the next frame */ s->next_frame_offset = s->cur_offset + index; s->fetch_timestamp=1; } if (index < 0) index = 0; s->cur_offset += index; return index; }