annotate ffmpeg/libavcodec/vc1_parser.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * VC-1 and WMV3 parser
yading@10 3 * Copyright (c) 2006-2007 Konstantin Shishkov
yading@10 4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
yading@10 5 *
yading@10 6 * This file is part of FFmpeg.
yading@10 7 *
yading@10 8 * FFmpeg is free software; you can redistribute it and/or
yading@10 9 * modify it under the terms of the GNU Lesser General Public
yading@10 10 * License as published by the Free Software Foundation; either
yading@10 11 * version 2.1 of the License, or (at your option) any later version.
yading@10 12 *
yading@10 13 * FFmpeg is distributed in the hope that it will be useful,
yading@10 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 16 * Lesser General Public License for more details.
yading@10 17 *
yading@10 18 * You should have received a copy of the GNU Lesser General Public
yading@10 19 * License along with FFmpeg; if not, write to the Free Software
yading@10 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 21 */
yading@10 22
yading@10 23 /**
yading@10 24 * @file
yading@10 25 * VC-1 and WMV3 parser
yading@10 26 */
yading@10 27
yading@10 28 #include "parser.h"
yading@10 29 #include "vc1.h"
yading@10 30 #include "get_bits.h"
yading@10 31
yading@10 32 typedef struct {
yading@10 33 ParseContext pc;
yading@10 34 VC1Context v;
yading@10 35 } VC1ParseContext;
yading@10 36
yading@10 37 static void vc1_extract_headers(AVCodecParserContext *s, AVCodecContext *avctx,
yading@10 38 const uint8_t *buf, int buf_size)
yading@10 39 {
yading@10 40 VC1ParseContext *vpc = s->priv_data;
yading@10 41 GetBitContext gb;
yading@10 42 const uint8_t *start, *end, *next;
yading@10 43 uint8_t *buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
yading@10 44
yading@10 45 vpc->v.s.avctx = avctx;
yading@10 46 vpc->v.parse_only = 1;
yading@10 47 next = buf;
yading@10 48 s->repeat_pict = 0;
yading@10 49
yading@10 50 for(start = buf, end = buf + buf_size; next < end; start = next){
yading@10 51 int buf2_size, size;
yading@10 52
yading@10 53 next = find_next_marker(start + 4, end);
yading@10 54 size = next - start - 4;
yading@10 55 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
yading@10 56 init_get_bits(&gb, buf2, buf2_size * 8);
yading@10 57 if(size <= 0) continue;
yading@10 58 switch(AV_RB32(start)){
yading@10 59 case VC1_CODE_SEQHDR:
yading@10 60 ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
yading@10 61 break;
yading@10 62 case VC1_CODE_ENTRYPOINT:
yading@10 63 ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
yading@10 64 break;
yading@10 65 case VC1_CODE_FRAME:
yading@10 66 if(vpc->v.profile < PROFILE_ADVANCED)
yading@10 67 ff_vc1_parse_frame_header (&vpc->v, &gb);
yading@10 68 else
yading@10 69 ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
yading@10 70
yading@10 71 /* keep AV_PICTURE_TYPE_BI internal to VC1 */
yading@10 72 if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
yading@10 73 s->pict_type = AV_PICTURE_TYPE_B;
yading@10 74 else
yading@10 75 s->pict_type = vpc->v.s.pict_type;
yading@10 76
yading@10 77 if (avctx->ticks_per_frame > 1){
yading@10 78 // process pulldown flags
yading@10 79 s->repeat_pict = 1;
yading@10 80 // Pulldown flags are only valid when 'broadcast' has been set.
yading@10 81 // So ticks_per_frame will be 2
yading@10 82 if (vpc->v.rff){
yading@10 83 // repeat field
yading@10 84 s->repeat_pict = 2;
yading@10 85 }else if (vpc->v.rptfrm){
yading@10 86 // repeat frames
yading@10 87 s->repeat_pict = vpc->v.rptfrm * 2 + 1;
yading@10 88 }
yading@10 89 }
yading@10 90
yading@10 91 break;
yading@10 92 }
yading@10 93 }
yading@10 94
yading@10 95 av_free(buf2);
yading@10 96 }
yading@10 97
yading@10 98 /**
yading@10 99 * Find the end of the current frame in the bitstream.
yading@10 100 * @return the position of the first byte of the next frame, or -1
yading@10 101 */
yading@10 102 static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf,
yading@10 103 int buf_size) {
yading@10 104 int pic_found, i;
yading@10 105 uint32_t state;
yading@10 106
yading@10 107 pic_found= pc->frame_start_found;
yading@10 108 state= pc->state;
yading@10 109
yading@10 110 i=0;
yading@10 111 if(!pic_found){
yading@10 112 for(i=0; i<buf_size; i++){
yading@10 113 state= (state<<8) | buf[i];
yading@10 114 if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
yading@10 115 i++;
yading@10 116 pic_found=1;
yading@10 117 break;
yading@10 118 }
yading@10 119 }
yading@10 120 }
yading@10 121
yading@10 122 if(pic_found){
yading@10 123 /* EOF considered as end of frame */
yading@10 124 if (buf_size == 0)
yading@10 125 return 0;
yading@10 126 for(; i<buf_size; i++){
yading@10 127 state= (state<<8) | buf[i];
yading@10 128 if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
yading@10 129 pc->frame_start_found=0;
yading@10 130 pc->state=-1;
yading@10 131 return i-3;
yading@10 132 }
yading@10 133 }
yading@10 134 }
yading@10 135 pc->frame_start_found= pic_found;
yading@10 136 pc->state= state;
yading@10 137 return END_NOT_FOUND;
yading@10 138 }
yading@10 139
yading@10 140 static int vc1_parse(AVCodecParserContext *s,
yading@10 141 AVCodecContext *avctx,
yading@10 142 const uint8_t **poutbuf, int *poutbuf_size,
yading@10 143 const uint8_t *buf, int buf_size)
yading@10 144 {
yading@10 145 VC1ParseContext *vpc = s->priv_data;
yading@10 146 int next;
yading@10 147
yading@10 148 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
yading@10 149 next= buf_size;
yading@10 150 }else{
yading@10 151 next= vc1_find_frame_end(&vpc->pc, buf, buf_size);
yading@10 152
yading@10 153 if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
yading@10 154 *poutbuf = NULL;
yading@10 155 *poutbuf_size = 0;
yading@10 156 return buf_size;
yading@10 157 }
yading@10 158 }
yading@10 159
yading@10 160 vc1_extract_headers(s, avctx, buf, buf_size);
yading@10 161
yading@10 162 *poutbuf = buf;
yading@10 163 *poutbuf_size = buf_size;
yading@10 164 return next;
yading@10 165 }
yading@10 166
yading@10 167 static int vc1_split(AVCodecContext *avctx,
yading@10 168 const uint8_t *buf, int buf_size)
yading@10 169 {
yading@10 170 int i;
yading@10 171 uint32_t state= -1;
yading@10 172 int charged=0;
yading@10 173
yading@10 174 for(i=0; i<buf_size; i++){
yading@10 175 state= (state<<8) | buf[i];
yading@10 176 if(IS_MARKER(state)){
yading@10 177 if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
yading@10 178 charged=1;
yading@10 179 }else if(charged){
yading@10 180 return i-3;
yading@10 181 }
yading@10 182 }
yading@10 183 }
yading@10 184 return 0;
yading@10 185 }
yading@10 186
yading@10 187 static int vc1_parse_init(AVCodecParserContext *s)
yading@10 188 {
yading@10 189 VC1ParseContext *vpc = s->priv_data;
yading@10 190 vpc->v.s.slice_context_count = 1;
yading@10 191 return ff_vc1_init_common(&vpc->v);
yading@10 192 }
yading@10 193
yading@10 194 AVCodecParser ff_vc1_parser = {
yading@10 195 .codec_ids = { AV_CODEC_ID_VC1 },
yading@10 196 .priv_data_size = sizeof(VC1ParseContext),
yading@10 197 .parser_init = vc1_parse_init,
yading@10 198 .parser_parse = vc1_parse,
yading@10 199 .parser_close = ff_parse_close,
yading@10 200 .split = vc1_split,
yading@10 201 };