dnxhd_parser.c 4.29 KB
Newer Older
Baptiste Coudurier's avatar
Baptiste Coudurier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * DNxHD/VC-3 parser
 * Copyright (c) 2008 Baptiste Coudurier <baptiste.coudurier@free.fr>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * 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.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * 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
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
Baptiste Coudurier's avatar
Baptiste Coudurier committed
24 25 26 27
 * DNxHD/VC-3 parser
 */

#include "parser.h"
28
#include "dnxhddata.h"
Baptiste Coudurier's avatar
Baptiste Coudurier committed
29

30 31
typedef struct {
    ParseContext pc;
32 33 34
    int cur_byte;
    int remaining;
    int w, h;
35 36 37
} DNXHDParserContext;

static int dnxhd_find_frame_end(DNXHDParserContext *dctx,
Baptiste Coudurier's avatar
Baptiste Coudurier committed
38 39
                                const uint8_t *buf, int buf_size)
{
40
    ParseContext *pc = &dctx->pc;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
41 42 43 44 45 46
    uint64_t state = pc->state64;
    int pic_found = pc->frame_start_found;
    int i = 0;

    if (!pic_found) {
        for (i = 0; i < buf_size; i++) {
47
            state = (state << 8) | buf[i];
48
            if (ff_dnxhd_check_header_prefix(state & 0xffffffffff00LL) != 0) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
49 50
                i++;
                pic_found = 1;
51 52
                dctx->cur_byte = 0;
                dctx->remaining = 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
53 54 55 56 57
                break;
            }
        }
    }

58
    if (pic_found && !dctx->remaining) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
59 60 61
        if (!buf_size) /* EOF considered as end of frame */
            return 0;
        for (; i < buf_size; i++) {
62
            dctx->cur_byte++;
63
            state = (state << 8) | buf[i];
64 65 66 67 68 69 70

            if (dctx->cur_byte == 24) {
                dctx->h = (state >> 32) & 0xFFFF;
            } else if (dctx->cur_byte == 26) {
                dctx->w = (state >> 32) & 0xFFFF;
            } else if (dctx->cur_byte == 42) {
                int cid = (state >> 32) & 0xFFFFFFFF;
71
                int remaining;
72 73 74 75

                if (cid <= 0)
                    continue;

76 77 78 79 80
                remaining = avpriv_dnxhd_get_frame_size(cid);
                if (remaining <= 0) {
                    remaining = ff_dnxhd_get_hr_frame_size(cid, dctx->w, dctx->h);
                    if (remaining <= 0)
                        continue;
81
                }
82
                dctx->remaining = remaining;
83
                if (buf_size - i + 47 >= dctx->remaining) {
84 85
                    int remaining = dctx->remaining;

86 87
                    pc->frame_start_found = 0;
                    pc->state64 = -1;
88 89 90
                    dctx->cur_byte = 0;
                    dctx->remaining = 0;
                    return remaining;
91
                } else {
92
                    dctx->remaining -= buf_size;
93
                }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
94 95
            }
        }
96 97 98 99 100 101 102 103 104 105 106 107
    } else if (pic_found) {
        if (dctx->remaining > buf_size) {
            dctx->remaining -= buf_size;
        } else {
            int remaining = dctx->remaining;

            pc->frame_start_found = 0;
            pc->state64 = -1;
            dctx->cur_byte = 0;
            dctx->remaining = 0;
            return remaining;
        }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
108 109 110 111 112 113 114 115 116 117 118
    }
    pc->frame_start_found = pic_found;
    pc->state64 = state;
    return END_NOT_FOUND;
}

static int dnxhd_parse(AVCodecParserContext *s,
                       AVCodecContext *avctx,
                       const uint8_t **poutbuf, int *poutbuf_size,
                       const uint8_t *buf, int buf_size)
{
119 120
    DNXHDParserContext *dctx = s->priv_data;
    ParseContext *pc = &dctx->pc;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
121 122 123 124 125
    int next;

    if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
        next = buf_size;
    } else {
126
        next = dnxhd_find_frame_end(dctx, buf, buf_size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
127
        if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
128
            *poutbuf      = NULL;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
129 130 131 132
            *poutbuf_size = 0;
            return buf_size;
        }
    }
133
    *poutbuf      = buf;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
134 135 136 137
    *poutbuf_size = buf_size;
    return next;
}

138
AVCodecParser ff_dnxhd_parser = {
139
    .codec_ids      = { AV_CODEC_ID_DNXHD },
140
    .priv_data_size = sizeof(DNXHDParserContext),
141 142
    .parser_parse   = dnxhd_parse,
    .parser_close   = ff_parse_close,
Baptiste Coudurier's avatar
Baptiste Coudurier committed
143
};