dnxhd_parser.c 4.48 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
                remaining = avpriv_dnxhd_get_frame_size(cid);
                if (remaining <= 0) {
78
                    remaining = avpriv_dnxhd_get_hr_frame_size(cid, dctx->w, dctx->h);
79 80
                    if (remaining <= 0)
                        continue;
81
                }
82
                remaining += i - 47;
83
                dctx->remaining = remaining;
84
                if (buf_size >= dctx->remaining) {
85 86
                    pc->frame_start_found = 0;
                    pc->state64 = -1;
87 88 89
                    dctx->cur_byte = 0;
                    dctx->remaining = 0;
                    return remaining;
90
                } else {
91
                    dctx->remaining -= buf_size;
92 93 94 95
                    // Update variables for correctness, they are currently not used beyond here
                    state = -1;
                    dctx->cur_byte += buf_size - i;
                    break;
96
                }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
97 98
            }
        }
99 100 101 102 103 104 105 106 107 108 109 110
    } 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
111 112 113 114 115 116 117 118 119 120 121
    }
    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)
{
122 123
    DNXHDParserContext *dctx = s->priv_data;
    ParseContext *pc = &dctx->pc;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
124 125 126 127 128
    int next;

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

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