FFmpeg
Loading...
Searching...
No Matches
dnxhd_parser.c
Go to the documentation of this file.
1/*
2 * DNxHD/VC-3 parser
3 * Copyright (c) 2008 Baptiste Coudurier <baptiste.coudurier@free.fr>
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22/**
23 * @file
24 * DNxHD/VC-3 parser
25 */
26
27#include "parser.h"
28#include "dnxhddata.h"
29#include "parser_internal.h"
30
31typedef struct {
35 int w, h;
37
39 const uint8_t *buf, int buf_size)
40{
41 ParseContext *pc = &dctx->pc;
42 uint64_t state = pc->state64;
43 int pic_found = pc->frame_start_found;
44 int i = 0;
45
46 if (!pic_found) {
47 for (i = 0; i < buf_size; i++) {
48 state = (state << 8) | buf[i];
49 if (ff_dnxhd_check_header_prefix(state & 0xffffffffff00LL) != 0) {
50 i++;
51 pic_found = 1;
52 dctx->cur_byte = 0;
53 dctx->remaining = 0;
54 break;
55 }
56 }
57 }
58
59 if (pic_found && !dctx->remaining) {
60 if (!buf_size) /* EOF considered as end of frame */
61 return 0;
62 for (; i < buf_size; i++) {
63 dctx->cur_byte++;
64 state = (state << 8) | buf[i];
65
66 if (dctx->cur_byte == 24) {
67 dctx->h = (state >> 32) & 0xFFFF;
68 } else if (dctx->cur_byte == 26) {
69 dctx->w = (state >> 32) & 0xFFFF;
70 } else if (dctx->cur_byte == 42) {
71 int cid = (state >> 32) & 0xFFFFFFFF;
72 int remaining;
73
74 if (cid <= 0)
75 continue;
76
77 remaining = ff_dnxhd_get_frame_size(cid);
78 if (remaining <= 0) {
79 remaining = ff_dnxhd_get_hr_frame_size(cid, dctx->w, dctx->h);
80 if (remaining <= 0)
81 continue;
82 }
83 remaining += i - 47;
84 dctx->remaining = remaining;
85 if (buf_size >= dctx->remaining) {
86 pc->frame_start_found = 0;
87 pc->state64 = -1;
88 dctx->cur_byte = 0;
89 dctx->remaining = 0;
90 return remaining;
91 } else {
92 dctx->remaining -= buf_size;
93 // Update variables for correctness, they are currently not used beyond here
94 state = -1;
95 dctx->cur_byte += buf_size - i;
96 break;
97 }
98 }
99 }
100 } else if (pic_found) {
101 if (dctx->remaining > buf_size) {
102 dctx->remaining -= buf_size;
103 } else {
104 int remaining = dctx->remaining;
105
106 pc->frame_start_found = 0;
107 pc->state64 = -1;
108 dctx->cur_byte = 0;
109 dctx->remaining = 0;
110 return remaining;
111 }
112 }
113 pc->frame_start_found = pic_found;
114 pc->state64 = state;
115 return END_NOT_FOUND;
116}
117
119 AVCodecContext *avctx,
120 const uint8_t **poutbuf, int *poutbuf_size,
121 const uint8_t *buf, int buf_size)
122{
123 DNXHDParserContext *dctx = s->priv_data;
124 ParseContext *pc = &dctx->pc;
125 int next;
126
127 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
128 next = buf_size;
129 } else {
130 next = dnxhd_find_frame_end(dctx, buf, buf_size);
131 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
132 *poutbuf = NULL;
133 *poutbuf_size = 0;
134 return buf_size;
135 }
136 }
137 *poutbuf = buf;
138 *poutbuf_size = buf_size;
139 return next;
140}
141
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition apv_parser.c:92
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
#define PARSER_FLAG_COMPLETE_FRAMES
Definition avcodec.h:2651
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
const FFCodecParser ff_dnxhd_parser
static int dnxhd_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
static int dnxhd_find_frame_end(DNXHDParserContext *dctx, const uint8_t *buf, int buf_size)
int ff_dnxhd_get_frame_size(int cid)
Definition dnxhddata.c:1088
int ff_dnxhd_get_hr_frame_size(int cid, int w, int h)
Definition dnxhddata.c:1096
static av_always_inline uint64_t ff_dnxhd_check_header_prefix(uint64_t prefix)
Definition dnxhddata.h:76
static struct @346255127015250356166251341105367306144006377143 state
@ AV_CODEC_ID_DNXHD
Definition codec_id.h:149
uint16_t cid
Definition mxfenc.c:2335
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition parser.c:300
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition parser.c:213
#define END_NOT_FOUND
Definition parser.h:40
#define PARSER_CODEC_LIST(...)
main external API structure.
Definition avcodec.h:443
ParseContext pc
uint64_t state64
contains the last 8 bytes in MSB order
Definition parser.h:37
int frame_start_found
Definition parser.h:34