vc1_parser.c 9.43 KB
Newer Older
1 2 3 4 5
/*
 * VC-1 and WMV3 parser
 * Copyright (c) 2006-2007 Konstantin Shishkov
 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
 *
6
 * This file is part of Libav.
7
 *
8
 * Libav is free software; you can redistribute it and/or
9 10 11 12
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
13
 * Libav is distributed in the hope that it will be useful,
14 15 16 17 18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with Libav; if not, write to the Free Software
20 21 22 23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
24
 * @file
25 26
 * VC-1 and WMV3 parser
 */
27

28
#include "libavutil/attributes.h"
29 30
#include "parser.h"
#include "vc1.h"
31 32
#include "get_bits.h"

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/** The maximum number of bytes of a sequence, entry point or
 *  frame header whose values we pay any attention to */
#define UNESCAPED_THRESHOLD 37

/** The maximum number of bytes of a sequence, entry point or
 *  frame header which must be valid memory (because they are
 *  used to update the bitstream cache in skip_bits() calls)
 */
#define UNESCAPED_LIMIT 144

typedef enum {
    NO_MATCH,
    ONE_ZERO,
    TWO_ZEROS,
    ONE
} VC1ParseSearchState;

50
typedef struct VC1ParseContext {
51 52
    ParseContext pc;
    VC1Context v;
53 54 55 56 57
    uint8_t prev_start_code;
    size_t bytes_to_skip;
    uint8_t unesc_buffer[UNESCAPED_LIMIT];
    size_t unesc_index;
    VC1ParseSearchState search_state;
58 59
} VC1ParseContext;

60 61
static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx,
                               const uint8_t *buf, int buf_size)
62
{
63
    /* Parse the header we just finished unescaping */
64 65 66 67
    VC1ParseContext *vpc = s->priv_data;
    GetBitContext gb;
    vpc->v.s.avctx = avctx;
    vpc->v.parse_only = 1;
68 69 70 71 72 73 74 75 76 77 78 79 80
    init_get_bits(&gb, buf, buf_size * 8);
    switch (vpc->prev_start_code) {
    case VC1_CODE_SEQHDR & 0xFF:
        ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
        break;
    case VC1_CODE_ENTRYPOINT & 0xFF:
        ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
        break;
    case VC1_CODE_FRAME & 0xFF:
        if(vpc->v.profile < PROFILE_ADVANCED)
            ff_vc1_parse_frame_header    (&vpc->v, &gb);
        else
            ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
81

82 83 84 85 86
        /* keep AV_PICTURE_TYPE_BI internal to VC1 */
        if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
            s->pict_type = AV_PICTURE_TYPE_B;
        else
            s->pict_type = vpc->v.s.pict_type;
87

88 89 90 91 92 93 94 95 96 97 98
        if (avctx->ticks_per_frame > 1){
            // process pulldown flags
            s->repeat_pict = 1;
            // Pulldown flags are only valid when 'broadcast' has been set.
            // So ticks_per_frame will be 2
            if (vpc->v.rff){
                // repeat field
                s->repeat_pict = 2;
            }else if (vpc->v.rptfrm){
                // repeat frames
                s->repeat_pict = vpc->v.rptfrm * 2 + 1;
99
            }
100 101
        }else{
            s->repeat_pict = 0;
102 103
        }

104 105 106 107 108 109
        if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
            s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
        else
            s->field_order = AV_FIELD_PROGRESSIVE;

        break;
110 111 112 113 114
    }
}

static int vc1_parse(AVCodecParserContext *s,
                           AVCodecContext *avctx,
115
                           const uint8_t **poutbuf, int *poutbuf_size,
116 117
                           const uint8_t *buf, int buf_size)
{
118 119 120
    /* Here we do the searching for frame boundaries and headers at
     * the same time. Only a minimal amount at the start of each
     * header is unescaped. */
121
    VC1ParseContext *vpc = s->priv_data;
122 123 124 125
    int pic_found = vpc->pc.frame_start_found;
    uint8_t *unesc_buffer = vpc->unesc_buffer;
    size_t unesc_index = vpc->unesc_index;
    VC1ParseSearchState search_state = vpc->search_state;
126
    int start_code_found = 0;
127 128 129 130 131 132 133 134 135 136 137
    int next = END_NOT_FOUND;
    int i = vpc->bytes_to_skip;

    if (pic_found && buf_size == 0) {
        /* EOF considered as end of frame */
        memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
        vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
        next = 0;
    }
    while (i < buf_size) {
        uint8_t b;
138
        start_code_found = 0;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
            b = buf[i++];
            unesc_buffer[unesc_index++] = b;
            if (search_state <= ONE_ZERO)
                search_state = b ? NO_MATCH : search_state + 1;
            else if (search_state == TWO_ZEROS) {
                if (b == 1)
                    search_state = ONE;
                else if (b > 1) {
                    if (b == 3)
                        unesc_index--; // swallow emulation prevention byte
                    search_state = NO_MATCH;
                }
            }
            else { // search_state == ONE
                // Header unescaping terminates early due to detection of next start code
                search_state = NO_MATCH;
                start_code_found = 1;
                break;
            }
        }
        if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
                unesc_index >= UNESCAPED_THRESHOLD &&
                vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
        {
            // No need to keep scanning the rest of the buffer for
            // start codes if we know it contains a complete frame and
            // we've already unescaped all we need of the frame header
            vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
            break;
        }
        if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
            while (i < buf_size) {
                if (search_state == NO_MATCH) {
                    i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
                    if (i < buf_size) {
                        search_state = ONE_ZERO;
                    }
                    i++;
                } else {
                    b = buf[i++];
                    if (search_state == ONE_ZERO)
                        search_state = b ? NO_MATCH : TWO_ZEROS;
                    else if (search_state == TWO_ZEROS) {
                        if (b >= 1)
                            search_state = b == 1 ? ONE : NO_MATCH;
                    }
                    else { // search_state == ONE
                        search_state = NO_MATCH;
                        start_code_found = 1;
                        break;
                    }
                }
            }
        }
        if (start_code_found) {
            vc1_extract_header(s, avctx, unesc_buffer, unesc_index);

            vpc->prev_start_code = b;
            unesc_index = 0;

            if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
                if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
                    pic_found = 1;
                }
                else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
                    next = i - 4;
                    pic_found = b == (VC1_CODE_FRAME & 0xFF);
                    break;
                }
            }
        }
    }
212

213 214 215
    vpc->pc.frame_start_found = pic_found;
    vpc->unesc_index = unesc_index;
    vpc->search_state = search_state;
216

217 218 219
    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
        next = buf_size;
    } else {
220
        if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
221
            vpc->bytes_to_skip = 0;
222 223 224 225 226
            *poutbuf = NULL;
            *poutbuf_size = 0;
            return buf_size;
        }
    }
227

228 229 230 231 232 233 234 235
    /* If we return with a valid pointer to a combined frame buffer
     * then on the next call then we'll have been unhelpfully rewound
     * by up to 4 bytes (depending upon whether the start code
     * overlapped the input buffer, and if so by how much). We don't
     * want this: it will either cause spurious second detections of
     * the start code we've already seen, or cause extra bytes to be
     * inserted at the start of the unescaped buffer. */
    vpc->bytes_to_skip = 4;
236
    if (next < 0 && start_code_found)
237
        vpc->bytes_to_skip += next;
238

239
    *poutbuf = buf;
240 241 242 243 244 245 246 247 248
    *poutbuf_size = buf_size;
    return next;
}

static int vc1_split(AVCodecContext *avctx,
                           const uint8_t *buf, int buf_size)
{
    int i;
    uint32_t state= -1;
Michael Niedermayer's avatar
Michael Niedermayer committed
249
    int charged=0;
250 251 252

    for(i=0; i<buf_size; i++){
        state= (state<<8) | buf[i];
Michael Niedermayer's avatar
Michael Niedermayer committed
253 254 255 256 257 258 259
        if(IS_MARKER(state)){
            if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
                charged=1;
            }else if(charged){
                return i-3;
            }
        }
260 261 262 263
    }
    return 0;
}

264
static av_cold int vc1_parse_init(AVCodecParserContext *s)
265 266 267
{
    VC1ParseContext *vpc = s->priv_data;
    vpc->v.s.slice_context_count = 1;
268 269 270 271
    vpc->prev_start_code = 0;
    vpc->bytes_to_skip = 0;
    vpc->unesc_index = 0;
    vpc->search_state = NO_MATCH;
272
    return ff_vc1_init_common(&vpc->v);
273 274
}

275
AVCodecParser ff_vc1_parser = {
276
    .codec_ids      = { AV_CODEC_ID_VC1 },
277
    .priv_data_size = sizeof(VC1ParseContext),
278
    .parser_init    = vc1_parse_init,
279
    .parser_parse   = vc1_parse,
Rafaël Carré's avatar
Rafaël Carré committed
280
    .parser_close   = ff_parse_close,
281
    .split          = vc1_split,
282
};