libavcodec/vc1_parser.c
Go to the documentation of this file.
00001 /*
00002  * VC-1 and WMV3 parser
00003  * Copyright (c) 2006-2007 Konstantin Shishkov
00004  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, 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 
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             vc1_decode_sequence_header(avctx, &vpc->v, &gb);
00061             break;
00062         case VC1_CODE_ENTRYPOINT:
00063             vc1_decode_entry_point(avctx, &vpc->v, &gb);
00064             break;
00065         case VC1_CODE_FRAME:
00066             if(vpc->v.profile < PROFILE_ADVANCED)
00067                 vc1_parse_frame_header    (&vpc->v, &gb);
00068             else
00069                 vc1_parse_frame_header_adv(&vpc->v, &gb);
00070 
00071             /* keep AV_PICTURE_TYPE_BI internal to VC1 */
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                 // process pulldown flags
00079                 s->repeat_pict = 1;
00080                 // Pulldown flags are only valid when 'broadcast' has been set.
00081                 // So ticks_per_frame will be 2
00082                 if (vpc->v.rff){
00083                     // repeat field
00084                     s->repeat_pict = 2;
00085                 }else if (vpc->v.rptfrm){
00086                     // repeat frames
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         /* EOF considered as end of frame */
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      = { CODEC_ID_VC1 },
00196     .priv_data_size = sizeof(VC1ParseContext),
00197     .parser_init    = vc1_parse_init,
00198     .parser_parse   = vc1_parse,
00199     .parser_close   = ff_parse1_close,
00200     .split          = vc1_split,
00201 };