FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dirac_parser.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 115 127 90.6%
Functions: 5 5 100.0%
Branches: 64 90 71.1%

Line Branch Exec Source
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/attributes.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/mem.h"
35
36 #include "avcodec.h"
37
38 #define DIRAC_PARSE_INFO_PREFIX 0x42424344
39
40 /**
41 * Find the end of the current frame in the bitstream.
42 * @return the position of the first byte of the next frame or -1
43 */
44 typedef struct DiracParseContext {
45 int state;
46 int is_synced;
47 int sync_offset;
48 int header_bytes_needed;
49 int overread_index;
50 int buffer_size;
51 int index;
52 uint8_t *buffer;
53 int dirac_unit_size;
54 uint8_t *dirac_unit;
55 } DiracParseContext;
56
57 812 static int find_frame_end(DiracParseContext *pc,
58 const uint8_t *buf, int buf_size)
59 {
60 812 uint32_t state = pc->state;
61 812 int i = 0;
62
63
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 810 times.
812 if (!pc->is_synced) {
64
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 for (i = 0; i < buf_size; i++) {
65 8 state = (state << 8) | buf[i];
66
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (state == DIRAC_PARSE_INFO_PREFIX) {
67 2 state = -1;
68 2 pc->is_synced = 1;
69 2 pc->header_bytes_needed = 9;
70 2 pc->sync_offset = i;
71 2 break;
72 }
73 }
74 }
75
76
1/2
✓ Branch 0 taken 812 times.
✗ Branch 1 not taken.
812 if (pc->is_synced) {
77 812 pc->sync_offset = 0;
78
2/2
✓ Branch 0 taken 669478 times.
✓ Branch 1 taken 656 times.
670134 for (; i < buf_size; i++) {
79
2/2
✓ Branch 0 taken 156 times.
✓ Branch 1 taken 669322 times.
669478 if (state == DIRAC_PARSE_INFO_PREFIX) {
80
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 2 times.
156 if ((buf_size - i) >= pc->header_bytes_needed) {
81 154 pc->state = -1;
82 154 return i + pc->header_bytes_needed;
83 } else {
84 2 pc->header_bytes_needed = 9 - (buf_size - i);
85 2 break;
86 }
87 } else
88 669322 state = (state << 8) | buf[i];
89 }
90 }
91 658 pc->state = state;
92 658 return -1;
93 }
94
95 typedef struct DiracParseUnit {
96 int next_pu_offset;
97 int prev_pu_offset;
98 uint8_t pu_type;
99 } DiracParseUnit;
100
101 308 static int unpack_parse_unit(DiracParseUnit *pu, DiracParseContext *pc,
102 int offset)
103 {
104 int i;
105 int8_t *start;
106 static const uint8_t valid_pu_types[] = {
107 0x00, 0x10, 0x20, 0x30, 0x08, 0x48, 0xC8, 0xE8, 0x0A, 0x0C, 0x0D, 0x0E,
108 0x4C, 0x09, 0xCC, 0x88, 0xCB
109 };
110
111
2/4
✓ Branch 0 taken 308 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 308 times.
308 if (offset < 0 || pc->index - 13 < offset)
112 return 0;
113
114 308 start = pc->buffer + offset;
115 308 pu->pu_type = start[4];
116
117 308 pu->next_pu_offset = AV_RB32(start + 5);
118 308 pu->prev_pu_offset = AV_RB32(start + 9);
119
120 /* Check for valid parse code */
121
1/2
✓ Branch 0 taken 1990 times.
✗ Branch 1 not taken.
1990 for (i = 0; i < 17; i++)
122
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 1682 times.
1990 if (valid_pu_types[i] == pu->pu_type)
123 308 break;
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (i == 17)
125 return 0;
126
127
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 306 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
308 if (pu->pu_type == 0x10 && pu->next_pu_offset == 0x00)
128 2 pu->next_pu_offset = 13; /* The length of a parse info header */
129
130 /* Check if the parse offsets are somewhat sane */
131
2/4
✓ Branch 0 taken 308 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 308 times.
✗ Branch 3 not taken.
308 if ((pu->next_pu_offset && pu->next_pu_offset < 13) ||
132
3/4
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 306 times.
308 (pu->prev_pu_offset && pu->prev_pu_offset < 13))
133 return 0;
134
135 308 return 1;
136 }
137
138 812 static int dirac_combine_frame(AVCodecParserContext *s, AVCodecContext *avctx,
139 int next, const uint8_t **buf, int *buf_size)
140 {
141
1/2
✓ Branch 0 taken 812 times.
✗ Branch 1 not taken.
1624 int parse_timing_info = (s->pts == AV_NOPTS_VALUE &&
142
1/2
✓ Branch 0 taken 812 times.
✗ Branch 1 not taken.
812 s->dts == AV_NOPTS_VALUE);
143 812 DiracParseContext *pc = s->priv_data;
144
145
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 752 times.
812 if (pc->overread_index) {
146 60 memmove(pc->buffer, pc->buffer + pc->overread_index,
147 60 pc->index - pc->overread_index);
148 60 pc->index -= pc->overread_index;
149 60 pc->overread_index = 0;
150
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
60 if (*buf_size == 0 && pc->buffer[4] == 0x10) {
151 2 *buf = pc->buffer;
152 2 *buf_size = pc->index;
153 2 return 0;
154 }
155 }
156
157
2/2
✓ Branch 0 taken 656 times.
✓ Branch 1 taken 154 times.
810 if (next == -1) {
158 /* Found a possible frame start but not a frame end */
159 void *new_buffer =
160 656 av_fast_realloc(pc->buffer, &pc->buffer_size,
161 656 pc->index + (*buf_size - pc->sync_offset));
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 656 times.
656 if (!new_buffer)
163 return AVERROR(ENOMEM);
164 656 pc->buffer = new_buffer;
165 656 memcpy(pc->buffer + pc->index, (*buf + pc->sync_offset),
166 656 *buf_size - pc->sync_offset);
167 656 pc->index += *buf_size - pc->sync_offset;
168 656 return -1;
169 } else {
170 /* Found a possible frame start and a possible frame end */
171 DiracParseUnit pu1, pu;
172 154 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
173 154 pc->index + next);
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
154 if (!new_buffer)
175 94 return AVERROR(ENOMEM);
176 154 pc->buffer = new_buffer;
177 154 memcpy(pc->buffer + pc->index, *buf, next);
178 154 pc->index += next;
179
180 /* Need to check if we have a valid Parse Unit. We can't go by the
181 * sync pattern 'BBCD' alone because arithmetic coding of the residual
182 * and motion data can cause the pattern triggering a false start of
183 * frame. So check if the previous parse offset of the next parse unit
184 * is equal to the next parse offset of the current parse unit then
185 * we can be pretty sure that we have a valid parse unit */
186
2/4
✓ Branch 1 taken 154 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 154 times.
✗ Branch 4 not taken.
308 if (!unpack_parse_unit(&pu1, pc, pc->index - 13) ||
187 154 !unpack_parse_unit(&pu, pc, pc->index - 13 - pu1.prev_pu_offset) ||
188
1/2
✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
154 pu.next_pu_offset != pu1.prev_pu_offset ||
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
154 pc->index < pc->dirac_unit_size + 13LL + pu1.prev_pu_offset
190 ) {
191 pc->index -= 9;
192 *buf_size = next - 9;
193 pc->header_bytes_needed = 9;
194 return -1;
195 }
196
197 /* All non-frame data must be accompanied by frame data. This is to
198 * ensure that pts is set correctly. So if the current parse unit is
199 * not frame data, wait for frame data to come along */
200
201 154 pc->dirac_unit = pc->buffer + pc->index - 13 -
202 154 pu1.prev_pu_offset - pc->dirac_unit_size;
203
204 154 pc->dirac_unit_size += pu.next_pu_offset;
205
206
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 60 times.
154 if ((pu.pu_type & 0x08) != 0x08) {
207 94 pc->header_bytes_needed = 9;
208 94 *buf_size = next;
209 94 return -1;
210 }
211
212 /* Get the picture number to set the pts and dts*/
213
2/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
60 if (parse_timing_info && pu1.prev_pu_offset >= 13) {
214 60 uint8_t *cur_pu = pc->buffer +
215 60 pc->index - 13 - pu1.prev_pu_offset;
216 60 int64_t pts = AV_RB32(cur_pu + 13);
217
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
60 if (s->last_pts == 0 && s->last_dts == 0)
218 2 s->dts = pts - 1;
219
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 else if (s->last_dts != AV_NOPTS_VALUE)
220 58 s->dts = s->last_dts + 1;
221 60 s->pts = pts;
222
4/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 31 times.
60 if (!avctx->has_b_frames && (cur_pu[4] & 0x03))
223 1 avctx->has_b_frames = 1;
224 }
225
4/4
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 8 times.
60 if (avctx->has_b_frames && s->pts == s->dts)
226 21 s->pict_type = AV_PICTURE_TYPE_B;
227
228 /* Finally have a complete Dirac data unit */
229 60 *buf = pc->dirac_unit;
230 60 *buf_size = pc->dirac_unit_size;
231
232 60 pc->dirac_unit_size = 0;
233 60 pc->overread_index = pc->index - 13;
234 60 pc->header_bytes_needed = 9;
235 }
236 60 return next;
237 }
238
239 812 static int dirac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
240 const uint8_t **poutbuf, int *poutbuf_size,
241 const uint8_t *buf, int buf_size)
242 {
243 812 DiracParseContext *pc = s->priv_data;
244 int next;
245
246 812 *poutbuf = NULL;
247 812 *poutbuf_size = 0;
248
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 812 times.
812 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
250 next = buf_size;
251 *poutbuf = buf;
252 *poutbuf_size = buf_size;
253 /* Assume that data has been packetized into an encapsulation unit. */
254 } else {
255 812 next = find_frame_end(pc, buf, buf_size);
256
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 812 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
812 if (!pc->is_synced && next == -1)
257 /* No frame start found yet. So throw away the entire buffer. */
258 return buf_size;
259
260
2/2
✓ Branch 1 taken 750 times.
✓ Branch 2 taken 62 times.
812 if (dirac_combine_frame(s, avctx, next, &buf, &buf_size) < 0)
261 750 return buf_size;
262 }
263
264 62 *poutbuf = buf;
265 62 *poutbuf_size = buf_size;
266 62 return next;
267 }
268
269 35 static av_cold void dirac_parse_close(AVCodecParserContext *s)
270 {
271 35 DiracParseContext *pc = s->priv_data;
272
273
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 33 times.
35 if (pc->buffer_size > 0)
274 2 av_freep(&pc->buffer);
275 35 }
276
277 const AVCodecParser ff_dirac_parser = {
278 .codec_ids = { AV_CODEC_ID_DIRAC },
279 .priv_data_size = sizeof(DiracParseContext),
280 .parser_parse = dirac_parse,
281 .parser_close = dirac_parse_close,
282 };
283