00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028 #include "parser.h"
00029 #include "vc1.h"
00030 #include "get_bits.h"
00031
00032 typedef struct {
00033 ParseContext pc;
00034 VC1Context v;
00035 } VC1ParseContext;
00036
00037 static void vc1_extract_headers(AVCodecParserContext *s, AVCodecContext *avctx,
00038 const uint8_t *buf, int buf_size)
00039 {
00040 VC1ParseContext *vpc = s->priv_data;
00041 GetBitContext gb;
00042 const uint8_t *start, *end, *next;
00043 uint8_t *buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00044
00045 vpc->v.s.avctx = avctx;
00046 vpc->v.parse_only = 1;
00047 next = buf;
00048 s->repeat_pict = 0;
00049
00050 for(start = buf, end = buf + buf_size; next < end; start = next){
00051 int buf2_size, size;
00052
00053 next = find_next_marker(start + 4, end);
00054 size = next - start - 4;
00055 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
00056 init_get_bits(&gb, buf2, buf2_size * 8);
00057 if(size <= 0) continue;
00058 switch(AV_RB32(start)){
00059 case VC1_CODE_SEQHDR:
00060 ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
00061 break;
00062 case VC1_CODE_ENTRYPOINT:
00063 ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
00064 break;
00065 case VC1_CODE_FRAME:
00066 if(vpc->v.profile < PROFILE_ADVANCED)
00067 ff_vc1_parse_frame_header (&vpc->v, &gb);
00068 else
00069 ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
00070
00071
00072 if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
00073 s->pict_type = AV_PICTURE_TYPE_B;
00074 else
00075 s->pict_type = vpc->v.s.pict_type;
00076
00077 if (avctx->ticks_per_frame > 1){
00078
00079 s->repeat_pict = 1;
00080
00081
00082 if (vpc->v.rff){
00083
00084 s->repeat_pict = 2;
00085 }else if (vpc->v.rptfrm){
00086
00087 s->repeat_pict = vpc->v.rptfrm * 2 + 1;
00088 }
00089 }
00090
00091 break;
00092 }
00093 }
00094
00095 av_free(buf2);
00096 }
00097
00102 static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf,
00103 int buf_size) {
00104 int pic_found, i;
00105 uint32_t state;
00106
00107 pic_found= pc->frame_start_found;
00108 state= pc->state;
00109
00110 i=0;
00111 if(!pic_found){
00112 for(i=0; i<buf_size; i++){
00113 state= (state<<8) | buf[i];
00114 if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
00115 i++;
00116 pic_found=1;
00117 break;
00118 }
00119 }
00120 }
00121
00122 if(pic_found){
00123
00124 if (buf_size == 0)
00125 return 0;
00126 for(; i<buf_size; i++){
00127 state= (state<<8) | buf[i];
00128 if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
00129 pc->frame_start_found=0;
00130 pc->state=-1;
00131 return i-3;
00132 }
00133 }
00134 }
00135 pc->frame_start_found= pic_found;
00136 pc->state= state;
00137 return END_NOT_FOUND;
00138 }
00139
00140 static int vc1_parse(AVCodecParserContext *s,
00141 AVCodecContext *avctx,
00142 const uint8_t **poutbuf, int *poutbuf_size,
00143 const uint8_t *buf, int buf_size)
00144 {
00145 VC1ParseContext *vpc = s->priv_data;
00146 int next;
00147
00148 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
00149 next= buf_size;
00150 }else{
00151 next= vc1_find_frame_end(&vpc->pc, buf, buf_size);
00152
00153 if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
00154 *poutbuf = NULL;
00155 *poutbuf_size = 0;
00156 return buf_size;
00157 }
00158 }
00159
00160 vc1_extract_headers(s, avctx, buf, buf_size);
00161
00162 *poutbuf = buf;
00163 *poutbuf_size = buf_size;
00164 return next;
00165 }
00166
00167 static int vc1_split(AVCodecContext *avctx,
00168 const uint8_t *buf, int buf_size)
00169 {
00170 int i;
00171 uint32_t state= -1;
00172 int charged=0;
00173
00174 for(i=0; i<buf_size; i++){
00175 state= (state<<8) | buf[i];
00176 if(IS_MARKER(state)){
00177 if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
00178 charged=1;
00179 }else if(charged){
00180 return i-3;
00181 }
00182 }
00183 }
00184 return 0;
00185 }
00186
00187 static int vc1_parse_init(AVCodecParserContext *s)
00188 {
00189 VC1ParseContext *vpc = s->priv_data;
00190 vpc->v.s.slice_context_count = 1;
00191 return ff_vc1_init_common(&vpc->v);
00192 }
00193
00194 AVCodecParser ff_vc1_parser = {
00195 .codec_ids = { AV_CODEC_ID_VC1 },
00196 .priv_data_size = sizeof(VC1ParseContext),
00197 .parser_init = vc1_parse_init,
00198 .parser_parse = vc1_parse,
00199 .parser_close = ff_parse_close,
00200 .split = vc1_split,
00201 };