FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mem.h"
34 
35 #include "parser.h"
36 
37 #define DIRAC_PARSE_INFO_PREFIX 0x42424344
38 
39 /**
40  * Find the end of the current frame in the bitstream.
41  * @return the position of the first byte of the next frame or -1
42  */
43 typedef struct DiracParseContext {
44  int state;
45  int is_synced;
50  int index;
55 
57  const uint8_t *buf, int buf_size)
58 {
59  uint32_t state = pc->state;
60  int i = 0;
61 
62  if (!pc->is_synced) {
63  for (i = 0; i < buf_size; i++) {
64  state = (state << 8) | buf[i];
65  if (state == DIRAC_PARSE_INFO_PREFIX) {
66  state = -1;
67  pc->is_synced = 1;
68  pc->header_bytes_needed = 9;
69  pc->sync_offset = i;
70  break;
71  }
72  }
73  }
74 
75  if (pc->is_synced) {
76  pc->sync_offset = 0;
77  for (; i < buf_size; i++) {
78  if (state == DIRAC_PARSE_INFO_PREFIX) {
79  if ((buf_size - i) >= pc->header_bytes_needed) {
80  pc->state = -1;
81  return i + pc->header_bytes_needed;
82  } else {
83  pc->header_bytes_needed = 9 - (buf_size - i);
84  break;
85  }
86  } else
87  state = (state << 8) | buf[i];
88  }
89  }
90  pc->state = state;
91  return -1;
92 }
93 
94 typedef struct DiracParseUnit {
99 
101  int offset)
102 {
103  uint8_t *start = pc->buffer + offset;
104  uint8_t *end = pc->buffer + pc->index;
105  if (start < pc->buffer || (start + 13 > end))
106  return 0;
107  pu->pu_type = start[4];
108 
109  pu->next_pu_offset = AV_RB32(start + 5);
110  pu->prev_pu_offset = AV_RB32(start + 9);
111 
112  if (pu->pu_type == 0x10 && pu->next_pu_offset == 0)
113  pu->next_pu_offset = 13;
114 
115  return 1;
116 }
117 
119  int next, const uint8_t **buf, int *buf_size)
120 {
121  int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
122  s->dts == AV_NOPTS_VALUE);
123  DiracParseContext *pc = s->priv_data;
124 
125  if (pc->overread_index) {
126  memcpy(pc->buffer, pc->buffer + pc->overread_index,
127  pc->index - pc->overread_index);
128  pc->index -= pc->overread_index;
129  pc->overread_index = 0;
130  if (*buf_size == 0 && pc->buffer[4] == 0x10) {
131  *buf = pc->buffer;
132  *buf_size = pc->index;
133  return 0;
134  }
135  }
136 
137  if (next == -1) {
138  /* Found a possible frame start but not a frame end */
139  void *new_buffer =
141  pc->index + (*buf_size - pc->sync_offset));
142  if (!new_buffer)
143  return AVERROR(ENOMEM);
144  pc->buffer = new_buffer;
145  memcpy(pc->buffer + pc->index, (*buf + pc->sync_offset),
146  *buf_size - pc->sync_offset);
147  pc->index += *buf_size - pc->sync_offset;
148  return -1;
149  } else {
150  /* Found a possible frame start and a possible frame end */
151  DiracParseUnit pu1, pu;
152  void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
153  pc->index + next);
154  if (!new_buffer)
155  return AVERROR(ENOMEM);
156  pc->buffer = new_buffer;
157  memcpy(pc->buffer + pc->index, *buf, next);
158  pc->index += next;
159 
160  /* Need to check if we have a valid Parse Unit. We can't go by the
161  * sync pattern 'BBCD' alone because arithmetic coding of the residual
162  * and motion data can cause the pattern triggering a false start of
163  * frame. So check if the previous parse offset of the next parse unit
164  * is equal to the next parse offset of the current parse unit then
165  * we can be pretty sure that we have a valid parse unit */
166  if (!unpack_parse_unit(&pu1, pc, pc->index - 13) ||
167  !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
168  pu.next_pu_offset != pu1.prev_pu_offset ||
169  pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset
170  ) {
171  pc->index -= 9;
172  *buf_size = next - 9;
173  pc->header_bytes_needed = 9;
174  return -1;
175  }
176 
177  /* All non-frame data must be accompanied by frame data. This is to
178  * ensure that pts is set correctly. So if the current parse unit is
179  * not frame data, wait for frame data to come along */
180 
181  pc->dirac_unit = pc->buffer + pc->index - 13 -
183 
185 
186  if ((pu.pu_type & 0x08) != 0x08) {
187  pc->header_bytes_needed = 9;
188  *buf_size = next;
189  return -1;
190  }
191 
192  /* Get the picture number to set the pts and dts*/
193  if (parse_timing_info) {
194  uint8_t *cur_pu = pc->buffer +
195  pc->index - 13 - pu1.prev_pu_offset;
196  int pts = AV_RB32(cur_pu + 13);
197  if (s->last_pts == 0 && s->last_dts == 0)
198  s->dts = pts - 1;
199  else
200  s->dts = s->last_dts + 1;
201  s->pts = pts;
202  if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
203  avctx->has_b_frames = 1;
204  }
205  if (avctx->has_b_frames && s->pts == s->dts)
207 
208  /* Finally have a complete Dirac data unit */
209  *buf = pc->dirac_unit;
210  *buf_size = pc->dirac_unit_size;
211 
212  pc->dirac_unit_size = 0;
213  pc->overread_index = pc->index - 13;
214  pc->header_bytes_needed = 9;
215  }
216  return next;
217 }
218 
220  const uint8_t **poutbuf, int *poutbuf_size,
221  const uint8_t *buf, int buf_size)
222 {
223  DiracParseContext *pc = s->priv_data;
224  int next;
225 
226  *poutbuf = NULL;
227  *poutbuf_size = 0;
228 
230  next = buf_size;
231  *poutbuf = buf;
232  *poutbuf_size = buf_size;
233  /* Assume that data has been packetized into an encapsulation unit. */
234  } else {
235  next = find_frame_end(pc, buf, buf_size);
236  if (!pc->is_synced && next == -1)
237  /* No frame start found yet. So throw away the entire buffer. */
238  return buf_size;
239 
240  if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0)
241  return buf_size;
242  }
243 
244  *poutbuf = buf;
245  *poutbuf_size = buf_size;
246  return next;
247 }
248 
250 {
251  DiracParseContext *pc = s->priv_data;
252 
253  if (pc->buffer_size > 0)
254  av_freep(&pc->buffer);
255 }
256 
259  .priv_data_size = sizeof(DiracParseContext),
260  .parser_parse = dirac_parse,
261  .parser_close = dirac_parse_close,
262 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
memory handling functions
int codec_ids[5]
Definition: avcodec.h:4718
uint8_t * buffer
Definition: dirac_parser.c:51
uint8_t
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
static int find_frame_end(DiracParseContext *pc, const uint8_t *buf, int buf_size)
Definition: dirac_parser.c:56
AVCodecParser ff_dirac_parser
Definition: dirac_parser.c:257
uint8_t pu_type
Definition: dirac_parser.c:97
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1812
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: mem.c:480
#define AVERROR(e)
Definition: error.h:43
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:4573
static void dirac_parse_close(AVCodecParserContext *s)
Definition: dirac_parser.c:249
static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc, int offset)
Definition: dirac_parser.c:100
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
static struct @197 state
uint8_t * dirac_unit
Definition: dirac_parser.c:53
main external API structure.
Definition: avcodec.h:1502
void * buf
Definition: avisynth_c.h:553
static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: dirac_parser.c:219
static int64_t pts
Global timestamp for the audio frames.
#define DIRAC_PARSE_INFO_PREFIX
Definition: dirac_parser.c:37
Bi-dir predicted.
Definition: avutil.h:268
Find the end of the current frame in the bitstream.
Definition: dirac_parser.c:43
static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx, int next, const uint8_t **buf, int *buf_size)
Definition: dirac_parser.c:118
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:553
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240
GLuint buffer
Definition: opengl_enc.c:102