3 * Copyright (c) 2008 Baptiste Coudurier <baptiste.coudurier@free.fr>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "dnxhddata.h"
33 int cur_field; /* first field is 0, second is 1 */
39 static int dnxhd_find_frame_end(DNXHDParserContext *dctx,
40 const uint8_t *buf, int buf_size)
42 ParseContext *pc = &dctx->pc;
43 uint64_t state = pc->state64;
44 int pic_found = pc->frame_start_found;
46 int interlaced = dctx->interlaced;
47 int cur_field = dctx->cur_field;
50 for (i = 0; i < buf_size; i++) {
51 state = (state << 8) | buf[i];
52 if (ff_dnxhd_check_header_prefix(state & 0xffffffffff00LL) != 0) {
55 interlaced = (state&2)>>1; /* byte following the 5-byte header prefix */
64 if (pic_found && !dctx->remaining) {
65 if (!buf_size) /* EOF considered as end of frame */
67 for (; i < buf_size; i++) {
69 state = (state << 8) | buf[i];
71 if (dctx->cur_byte == 24) {
72 dctx->h = (state >> 32) & 0xFFFF;
73 } else if (dctx->cur_byte == 26) {
74 dctx->w = (state >> 32) & 0xFFFF;
75 } else if (dctx->cur_byte == 42) {
76 int cid = (state >> 32) & 0xFFFFFFFF;
81 dctx->remaining = avpriv_dnxhd_get_frame_size(cid);
82 if (dctx->remaining <= 0) {
83 dctx->remaining = ff_dnxhd_get_hr_frame_size(cid, dctx->w, dctx->h);
84 if (dctx->remaining <= 0)
85 return dctx->remaining;
87 if (buf_size - i >= dctx->remaining && (!dctx->interlaced || dctx->cur_field)) {
88 int remaining = dctx->remaining;
90 pc->frame_start_found = 0;
92 dctx->interlaced = interlaced;
98 dctx->remaining -= buf_size;
102 } else if (pic_found) {
103 if (dctx->remaining > buf_size) {
104 dctx->remaining -= buf_size;
106 int remaining = dctx->remaining;
108 pc->frame_start_found = 0;
110 dctx->interlaced = interlaced;
117 pc->frame_start_found = pic_found;
119 dctx->interlaced = interlaced;
120 dctx->cur_field = cur_field;
121 return END_NOT_FOUND;
124 static int dnxhd_parse(AVCodecParserContext *s,
125 AVCodecContext *avctx,
126 const uint8_t **poutbuf, int *poutbuf_size,
127 const uint8_t *buf, int buf_size)
129 DNXHDParserContext *dctx = s->priv_data;
130 ParseContext *pc = &dctx->pc;
133 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
136 next = dnxhd_find_frame_end(dctx, buf, buf_size);
137 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
144 *poutbuf_size = buf_size;
148 AVCodecParser ff_dnxhd_parser = {
149 .codec_ids = { AV_CODEC_ID_DNXHD },
150 .priv_data_size = sizeof(DNXHDParserContext),
151 .parser_parse = dnxhd_parse,
152 .parser_close = ff_parse_close,