diff --git a/libavcodec/vc1.c b/libavcodec/vc1.c index 82bf8db0de..8d79c18172 100644 --- a/libavcodec/vc1.c +++ b/libavcodec/vc1.c @@ -805,10 +805,8 @@ static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb); * @param gb GetBit context initialized from Codec context extra_data * @return Status */ -static int decode_sequence_header(AVCodecContext *avctx, GetBitContext *gb) +int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb) { - VC1Context *v = avctx->priv_data; - av_log(avctx, AV_LOG_DEBUG, "Header: %0X\n", show_bits(gb, 32)); v->profile = get_bits(gb, 2); if (v->profile == PROFILE_COMPLEX) @@ -1025,9 +1023,8 @@ static int decode_sequence_header_adv(VC1Context *v, GetBitContext *gb) return 0; } -static int decode_entry_point(AVCodecContext *avctx, GetBitContext *gb) +int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb) { - VC1Context *v = avctx->priv_data; int i; av_log(avctx, AV_LOG_DEBUG, "Entry point: %08X\n", show_bits_long(gb, 32)); @@ -1074,7 +1071,7 @@ static int decode_entry_point(AVCodecContext *avctx, GetBitContext *gb) return 0; } -static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb) +int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb) { int pqindex, lowquant, status; @@ -1101,6 +1098,9 @@ static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb) if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE) skip_bits(gb, 7); // skip buffer fullness + if(v->parse_only) + return 0; + /* calculate RND */ if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_BI_TYPE) v->rnd = 1; @@ -1286,7 +1286,7 @@ static int vc1_parse_frame_header(VC1Context *v, GetBitContext* gb) return 0; } -static int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb) +int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb) { int pqindex, lowquant; int status; @@ -1362,6 +1362,9 @@ static int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext* gb) if(v->s.pict_type == FF_I_TYPE || v->s.pict_type == FF_P_TYPE) v->use_ic = 0; + if(v->parse_only) + return 0; + switch(v->s.pict_type) { case FF_I_TYPE: case FF_BI_TYPE: @@ -3915,41 +3918,6 @@ static void vc1_decode_blocks(VC1Context *v) } } -/** Find VC-1 marker in buffer - * @return position where next marker starts or end of buffer if no marker found - */ -static av_always_inline const uint8_t* find_next_marker(const uint8_t *src, const uint8_t *end) -{ - uint32_t mrk = 0xFFFFFFFF; - - if(end-src < 4) return end; - while(src < end){ - mrk = (mrk << 8) | *src++; - if(IS_MARKER(mrk)) - return src-4; - } - return end; -} - -static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst) -{ - int dsize = 0, i; - - if(size < 4){ - for(dsize = 0; dsize < size; dsize++) *dst++ = *src++; - return size; - } - for(i = 0; i < size; i++, src++) { - if(src[0] == 3 && i >= 2 && !src[-1] && !src[-2] && i < size-1 && src[1] < 4) { - dst[dsize++] = src[1]; - src++; - i++; - } else - dst[dsize++] = *src; - } - return dsize; -} - /** Initialize a VC1/WMV3 decoder * @todo TODO: Handle VC-1 IDUs (Transport level?) * @todo TODO: Decypher remaining bits in extra_data @@ -3991,7 +3959,7 @@ static av_cold int vc1_decode_init(AVCodecContext *avctx) init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8); - if (decode_sequence_header(avctx, &gb) < 0) + if (vc1_decode_sequence_header(avctx, v, &gb) < 0) return -1; count = avctx->extradata_size*8 - get_bits_count(&gb); @@ -4028,14 +3996,14 @@ static av_cold int vc1_decode_init(AVCodecContext *avctx) init_get_bits(&gb, buf2, buf2_size * 8); switch(AV_RB32(start)){ case VC1_CODE_SEQHDR: - if(decode_sequence_header(avctx, &gb) < 0){ + if(vc1_decode_sequence_header(avctx, v, &gb) < 0){ av_free(buf2); return -1; } seq_initialized = 1; break; case VC1_CODE_ENTRYPOINT: - if(decode_entry_point(avctx, &gb) < 0){ + if(vc1_decode_entry_point(avctx, v, &gb) < 0){ av_free(buf2); return -1; } @@ -4150,7 +4118,7 @@ static int vc1_decode_frame(AVCodecContext *avctx, case VC1_CODE_ENTRYPOINT: /* it should be before frame data */ buf_size2 = vc1_unescape_buffer(start + 4, size, buf2); init_get_bits(&s->gb, buf2, buf_size2*8); - decode_entry_point(avctx, &s->gb); + vc1_decode_entry_point(avctx, v, &s->gb); break; case VC1_CODE_SLICE: av_log(avctx, AV_LOG_ERROR, "Sliced decoding is not implemented (yet)\n"); diff --git a/libavcodec/vc1.h b/libavcodec/vc1.h index 67cdc7ae64..e90feb2984 100644 --- a/libavcodec/vc1.h +++ b/libavcodec/vc1.h @@ -310,6 +310,57 @@ typedef struct VC1Context{ uint8_t bfraction_lut_index;///< Index for BFRACTION value (see Table 40, reproduced into ff_vc1_bfraction_lut[]) uint8_t broken_link; ///< Broken link flag (BROKEN_LINK syntax element) uint8_t closed_entry; ///< Closed entry point flag (CLOSED_ENTRY syntax element) + + int parse_only; ///< Context is used within parser } VC1Context; +/** Find VC-1 marker in buffer + * @return position where next marker starts or end of buffer if no marker found + */ +static av_always_inline const uint8_t* find_next_marker(const uint8_t *src, const uint8_t *end) +{ + uint32_t mrk = 0xFFFFFFFF; + + if(end-src < 4) return end; + while(src < end){ + mrk = (mrk << 8) | *src++; + if(IS_MARKER(mrk)) + return src-4; + } + return end; +} + +static av_always_inline int vc1_unescape_buffer(const uint8_t *src, int size, uint8_t *dst) +{ + int dsize = 0, i; + + if(size < 4){ + for(dsize = 0; dsize < size; dsize++) *dst++ = *src++; + return size; + } + for(i = 0; i < size; i++, src++) { + if(src[0] == 3 && i >= 2 && !src[-1] && !src[-2] && i < size-1 && src[1] < 4) { + dst[dsize++] = src[1]; + src++; + i++; + } else + dst[dsize++] = *src; + } + return dsize; +} + +/** + * Decode Simple/Main Profiles sequence header + * @see Figure 7-8, p16-17 + * @param avctx Codec context + * @param gb GetBit context initialized from Codec context extra_data + * @return Status + */ +int vc1_decode_sequence_header(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb); + +int vc1_decode_entry_point(AVCodecContext *avctx, VC1Context *v, GetBitContext *gb); + +int vc1_parse_frame_header (VC1Context *v, GetBitContext *gb); +int vc1_parse_frame_header_adv(VC1Context *v, GetBitContext *gb); + #endif /* AVCODEC_VC1_H */ diff --git a/libavcodec/vc1_parser.c b/libavcodec/vc1_parser.c index 3f4618dbed..55580c95d1 100644 --- a/libavcodec/vc1_parser.c +++ b/libavcodec/vc1_parser.c @@ -27,6 +27,58 @@ #include "parser.h" #include "vc1.h" +#include "get_bits.h" + +typedef struct { + ParseContext pc; + VC1Context v; +} VC1ParseContext; + +static void vc1_extract_headers(AVCodecParserContext *s, AVCodecContext *avctx, + const uint8_t *buf, int buf_size) +{ + VC1ParseContext *vpc = s->priv_data; + GetBitContext gb; + const uint8_t *start, *end, *next; + uint8_t *buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE); + + vpc->v.s.avctx = avctx; + vpc->v.parse_only = 1; + next = buf; + + for(start = buf, end = buf + buf_size; next < end; start = next){ + int buf2_size, size; + + next = find_next_marker(start + 4, end); + size = next - start - 4; + buf2_size = vc1_unescape_buffer(start + 4, size, buf2); + init_get_bits(&gb, buf2, buf2_size * 8); + if(size <= 0) continue; + switch(AV_RB32(start)){ + case VC1_CODE_SEQHDR: + vc1_decode_sequence_header(avctx, &vpc->v, &gb); + break; + case VC1_CODE_ENTRYPOINT: + vc1_decode_entry_point(avctx, &vpc->v, &gb); + break; + case VC1_CODE_FRAME: + if(vpc->v.profile < PROFILE_ADVANCED) + vc1_parse_frame_header (&vpc->v, &gb); + else + vc1_parse_frame_header_adv(&vpc->v, &gb); + + /* keep FF_BI_TYPE internal to VC1 */ + if (vpc->v.s.pict_type == FF_BI_TYPE) + s->pict_type = FF_B_TYPE; + else + s->pict_type = vpc->v.s.pict_type; + + break; + } + } + + av_free(buf2); +} /** * finds the end of the current frame in the bitstream. @@ -75,20 +127,23 @@ static int vc1_parse(AVCodecParserContext *s, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size) { - ParseContext *pc = s->priv_data; + VC1ParseContext *vpc = s->priv_data; int next; if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ next= buf_size; }else{ - next= vc1_find_frame_end(pc, buf, buf_size); + next= vc1_find_frame_end(&vpc->pc, buf, buf_size); - if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { + if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) { *poutbuf = NULL; *poutbuf_size = 0; return buf_size; } } + + vc1_extract_headers(s, avctx, buf, buf_size); + *poutbuf = buf; *poutbuf_size = buf_size; return next; @@ -116,7 +171,7 @@ static int vc1_split(AVCodecContext *avctx, AVCodecParser vc1_parser = { { CODEC_ID_VC1 }, - sizeof(ParseContext1), + sizeof(VC1ParseContext), NULL, vc1_parse, ff_parse1_close,