FFmpeg
Loading...
Searching...
No Matches
dirac_parser.c
Go to the documentation of this file.
1/*
2 * Dirac parser
3 *
4 * Copyright (c) 2007-2008 Marco Gerards <marco@gnu.org>
5 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju@gmail.com>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24/**
25 * @file
26 * Dirac Parser
27 * @author Marco Gerards <marco@gnu.org>
28 */
29
30#include <string.h>
31
34#include "libavutil/mem.h"
35
36#include "avcodec.h"
37#include "parser_internal.h"
38
39#define DIRAC_PARSE_INFO_PREFIX 0x42424344
40
41/**
42 * Find the end of the current frame in the bitstream.
43 * @return the position of the first byte of the next frame or -1
44 */
57
59 const uint8_t *buf, int buf_size)
60{
61 uint32_t state = pc->state;
62 int i = 0;
63
64 if (!pc->is_synced) {
65 for (i = 0; i < buf_size; i++) {
66 state = (state << 8) | buf[i];
68 state = -1;
69 pc->is_synced = 1;
70 pc->header_bytes_needed = 9;
71 pc->sync_offset = i;
72 break;
73 }
74 }
75 }
76
77 if (pc->is_synced) {
78 pc->sync_offset = 0;
79 for (; i < buf_size; i++) {
81 if ((buf_size - i) >= pc->header_bytes_needed) {
82 pc->state = -1;
83 return i + pc->header_bytes_needed;
84 } else {
85 pc->header_bytes_needed = 9 - (buf_size - i);
86 break;
87 }
88 } else
89 state = (state << 8) | buf[i];
90 }
91 }
92 pc->state = state;
93 return -1;
94}
95
101
103 int offset)
104{
105 int i;
106 int8_t *start;
107 static const uint8_t valid_pu_types[] = {
108 0x00, 0x10, 0x20, 0x30, 0x08, 0x48, 0xC8, 0xE8, 0x0A, 0x0C, 0x0D, 0x0E,
109 0x4C, 0x09, 0xCC, 0x88, 0xCB
110 };
111
113 return 0;
114
115 start = pc->buffer + offset;
116 pu->pu_type = start[4];
117
118 pu->next_pu_offset = AV_RB32(start + 5);
119 pu->prev_pu_offset = AV_RB32(start + 9);
120
121 /* Check for valid parse code */
122 for (i = 0; i < 17; i++)
123 if (valid_pu_types[i] == pu->pu_type)
124 break;
125 if (i == 17)
126 return 0;
127
128 if (pu->pu_type == 0x10 && pu->next_pu_offset == 0x00)
129 pu->next_pu_offset = 13; /* The length of a parse info header */
130
131 /* Check if the parse offsets are somewhat sane */
132 if ((pu->next_pu_offset && pu->next_pu_offset < 13) ||
133 (pu->prev_pu_offset && pu->prev_pu_offset < 13))
134 return 0;
135
136 return 1;
137}
138
140 int next, const uint8_t **buf, int *buf_size)
141{
142 int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
143 s->dts == AV_NOPTS_VALUE);
144 DiracParseContext *pc = s->priv_data;
145
146 if (pc->overread_index) {
147 memmove(pc->buffer, pc->buffer + pc->overread_index,
148 pc->index - pc->overread_index);
149 pc->index -= pc->overread_index;
150 pc->overread_index = 0;
151 if (*buf_size == 0 && pc->buffer[4] == 0x10) {
152 *buf = pc->buffer;
153 *buf_size = pc->index;
154 return 0;
155 }
156 }
157
158 if (next == -1) {
159 /* Found a possible frame start but not a frame end */
160 void *new_buffer =
162 pc->index + (*buf_size - pc->sync_offset));
163 if (!new_buffer)
164 return AVERROR(ENOMEM);
165 pc->buffer = new_buffer;
166 memcpy(pc->buffer + pc->index, (*buf + pc->sync_offset),
167 *buf_size - pc->sync_offset);
168 pc->index += *buf_size - pc->sync_offset;
169 return -1;
170 } else {
171 /* Found a possible frame start and a possible frame end */
172 DiracParseUnit pu1, pu;
173 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
174 pc->index + next);
175 if (!new_buffer)
176 return AVERROR(ENOMEM);
177 pc->buffer = new_buffer;
178 memcpy(pc->buffer + pc->index, *buf, next);
179 pc->index += next;
180
181 /* Need to check if we have a valid Parse Unit. We can't go by the
182 * sync pattern 'BBCD' alone because arithmetic coding of the residual
183 * and motion data can cause the pattern triggering a false start of
184 * frame. So check if the previous parse offset of the next parse unit
185 * is equal to the next parse offset of the current parse unit then
186 * we can be pretty sure that we have a valid parse unit */
187 if (!unpack_parse_unit(&pu1, pc, pc->index - 13) ||
188 !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
189 pu.next_pu_offset != pu1.prev_pu_offset ||
190 pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset
191 ) {
192 pc->index -= 9;
193 *buf_size = next - 9;
194 pc->header_bytes_needed = 9;
195 return -1;
196 }
197
198 /* All non-frame data must be accompanied by frame data. This is to
199 * ensure that pts is set correctly. So if the current parse unit is
200 * not frame data, wait for frame data to come along */
201
202 pc->dirac_unit = pc->buffer + pc->index - 13 -
204
206
207 if ((pu.pu_type & 0x08) != 0x08) {
208 pc->header_bytes_needed = 9;
209 *buf_size = next;
210 return -1;
211 }
212
213 /* Get the picture number to set the pts and dts*/
214 if (parse_timing_info && pu1.prev_pu_offset >= 13) {
215 uint8_t *cur_pu = pc->buffer +
216 pc->index - 13 - pu1.prev_pu_offset;
217 int64_t pts = AV_RB32(cur_pu + 13);
218 if (s->last_pts == 0 && s->last_dts == 0)
219 s->dts = pts - 1;
220 else if (s->last_dts != AV_NOPTS_VALUE)
221 s->dts = s->last_dts + 1;
222 s->pts = pts;
223 if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
224 avctx->has_b_frames = 1;
225 }
226 if (avctx->has_b_frames && s->pts == s->dts)
227 s->pict_type = AV_PICTURE_TYPE_B;
228
229 /* Finally have a complete Dirac data unit */
230 *buf = pc->dirac_unit;
231 *buf_size = pc->dirac_unit_size;
232
233 pc->dirac_unit_size = 0;
234 pc->overread_index = pc->index - 13;
235 pc->header_bytes_needed = 9;
236 }
237 return next;
238}
239
241 const uint8_t **poutbuf, int *poutbuf_size,
242 const uint8_t *buf, int buf_size)
243{
244 DiracParseContext *pc = s->priv_data;
245 int next;
246
247 *poutbuf = NULL;
248 *poutbuf_size = 0;
249
250 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
251 next = buf_size;
252 *poutbuf = buf;
253 *poutbuf_size = buf_size;
254 /* Assume that data has been packetized into an encapsulation unit. */
255 } else {
256 next = find_frame_end(pc, buf, buf_size);
257 if (!pc->is_synced && next == -1)
258 /* No frame start found yet. So throw away the entire buffer. */
259 return buf_size;
260
261 if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0)
262 return buf_size;
263 }
264
265 *poutbuf = buf;
266 *poutbuf_size = buf_size;
267 return next;
268}
269
271{
272 DiracParseContext *pc = s->priv_data;
273
274 if (pc->buffer_size > 0)
275 av_freep(&pc->buffer);
276}
277
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
Libavcodec external API header.
#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
long long int64_t
Definition coverity.c:34
static int find_frame_end(DiracParseContext *pc, const uint8_t *buf, int buf_size)
static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc, int offset)
#define DIRAC_PARSE_INFO_PREFIX
const FFCodecParser ff_dirac_parser
static av_cold void dirac_parse_close(AVCodecParserContext *s)
static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx, int next, const uint8_t **buf, int *buf_size)
static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
static struct @346255127015250356166251341105367306144006377143 state
@ AV_CODEC_ID_DIRAC
Definition codec_id.h:166
#define AVERROR(e)
Definition error.h:45
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:495
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition avutil.h:280
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
int index
Definition gxfenc.c:90
#define AV_RB32(p)
unsigned offset
Definition libaomenc.c:763
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
Memory handling functions.
#define PARSER_CODEC_LIST(...)
main external API structure.
Definition avcodec.h:443
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition avcodec.h:709
Find the end of the current frame in the bitstream.
uint8_t * dirac_unit
#define av_freep(p)
static int64_t pts