FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
30 typedef struct {
32  int cur_byte;
33  int remaining;
34  int w, h;
36 
37 static int dnxhd_get_hr_frame_size(int cid, int w, int h)
38 {
39  int result, i = ff_dnxhd_get_cid_table(cid);
40 
41  if (i < 0)
42  return i;
43 
44  result = ((h + 15) / 16) * ((w + 15) / 16) * ff_dnxhd_cid_table[i].packet_scale.num / ff_dnxhd_cid_table[i].packet_scale.den;
45  result = (result + 2048) / 4096 * 4096;
46 
47  return FFMAX(result, 8192);
48 }
49 
51  const uint8_t *buf, int buf_size)
52 {
53  ParseContext *pc = &dctx->pc;
54  uint64_t state = pc->state64;
55  int pic_found = pc->frame_start_found;
56  int i = 0;
57 
58  if (!pic_found) {
59  for (i = 0; i < buf_size; i++) {
60  state = (state << 8) | buf[i];
61  if (ff_dnxhd_check_header_prefix(state & 0xffffffffff00LL) != 0) {
62  i++;
63  pic_found = 1;
64  dctx->cur_byte = 0;
65  dctx->remaining = 0;
66  break;
67  }
68  }
69  }
70 
71  if (pic_found && !dctx->remaining) {
72  if (!buf_size) /* EOF considered as end of frame */
73  return 0;
74  for (; i < buf_size; i++) {
75  dctx->cur_byte++;
76  state = (state << 8) | buf[i];
77 
78  if (dctx->cur_byte == 24) {
79  dctx->h = (state >> 32) & 0xFFFF;
80  } else if (dctx->cur_byte == 26) {
81  dctx->w = (state >> 32) & 0xFFFF;
82  } else if (dctx->cur_byte == 42) {
83  int cid = (state >> 32) & 0xFFFFFFFF;
84  int remaining;
85 
86  if (cid <= 0)
87  continue;
88 
89  remaining = avpriv_dnxhd_get_frame_size(cid);
90  if (remaining <= 0) {
91  remaining = dnxhd_get_hr_frame_size(cid, dctx->w, dctx->h);
92  if (remaining <= 0)
93  continue;
94  }
95  dctx->remaining = remaining;
96  if (buf_size - i + 47 >= dctx->remaining) {
97  int remaining = dctx->remaining;
98 
99  pc->frame_start_found = 0;
100  pc->state64 = -1;
101  dctx->cur_byte = 0;
102  dctx->remaining = 0;
103  return remaining;
104  } else {
105  dctx->remaining -= buf_size;
106  }
107  }
108  }
109  } else if (pic_found) {
110  if (dctx->remaining > buf_size) {
111  dctx->remaining -= buf_size;
112  } else {
113  int remaining = dctx->remaining;
114 
115  pc->frame_start_found = 0;
116  pc->state64 = -1;
117  dctx->cur_byte = 0;
118  dctx->remaining = 0;
119  return remaining;
120  }
121  }
122  pc->frame_start_found = pic_found;
123  pc->state64 = state;
124  return END_NOT_FOUND;
125 }
126 
128  AVCodecContext *avctx,
129  const uint8_t **poutbuf, int *poutbuf_size,
130  const uint8_t *buf, int buf_size)
131 {
132  DNXHDParserContext *dctx = s->priv_data;
133  ParseContext *pc = &dctx->pc;
134  int next;
135 
137  next = buf_size;
138  } else {
139  next = dnxhd_find_frame_end(dctx, buf, buf_size);
140  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
141  *poutbuf = NULL;
142  *poutbuf_size = 0;
143  return buf_size;
144  }
145  }
146  *poutbuf = buf;
147  *poutbuf_size = buf_size;
148  return next;
149 }
150 
153  .priv_data_size = sizeof(DNXHDParserContext),
154  .parser_parse = dnxhd_parse,
155  .parser_close = ff_parse_close,
156 };
#define NULL
Definition: coverity.c:32
static av_always_inline uint64_t ff_dnxhd_check_header_prefix(uint64_t prefix)
Definition: dnxhddata.h:78
const char * s
Definition: avisynth_c.h:768
int num
Numerator.
Definition: rational.h:59
const CIDEntry ff_dnxhd_cid_table[]
Definition: dnxhddata.c:935
int codec_ids[5]
Definition: avcodec.h:5243
int frame_start_found
Definition: parser.h:34
uint8_t
static int dnxhd_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: dnxhd_parser.c:127
static int dnxhd_get_hr_frame_size(int cid, int w, int h)
Definition: dnxhd_parser.c:37
int avpriv_dnxhd_get_frame_size(int cid)
Definition: dnxhddata.c:1099
int ff_dnxhd_get_cid_table(int cid)
Definition: dnxhddata.c:1090
AVRational packet_scale
Definition: dnxhddata.h:59
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:250
static struct @253 state
AVCodecParser ff_dnxhd_parser
Definition: dnxhd_parser.c:151
#define FFMAX(a, b)
Definition: common.h:94
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:326
static int dnxhd_find_frame_end(DNXHDParserContext *dctx, const uint8_t *buf, int buf_size)
Definition: dnxhd_parser.c:50
ParseContext pc
Definition: dnxhd_parser.c:31
main external API structure.
Definition: avcodec.h:1732
void * buf
Definition: avisynth_c.h:690
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
#define END_NOT_FOUND
Definition: parser.h:40
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:5109
int den
Denominator.
Definition: rational.h:60