FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/parser.c
Date: 2026-01-23 19:11:46
Exec Total Coverage
Lines: 143 160 89.4%
Functions: 6 6 100.0%
Branches: 105 116 90.5%

Line Branch Exec Source
1 /*
2 * Audio and Video frame extraction
3 * Copyright (c) 2003 Fabrice Bellard
4 * Copyright (c) 2003 Michael Niedermayer
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <inttypes.h>
24 #include <stdint.h>
25 #include <string.h>
26
27 #include "libavutil/attributes.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/mem.h"
30
31 #include "parser.h"
32 #include "parser_internal.h"
33
34 #if FF_API_PARSER_CODECID
35 10838 av_cold AVCodecParserContext *av_parser_init(int codec_id)
36 #else
37 av_cold AVCodecParserContext *av_parser_init(enum AVCodecID codec_id)
38 #endif
39 {
40 10838 AVCodecParserContext *s = NULL;
41 const AVCodecParser *parser;
42 const FFCodecParser *ffparser;
43 10838 void *i = 0;
44 int ret;
45
46
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 10774 times.
10838 if (codec_id == AV_CODEC_ID_NONE)
47 64 return NULL;
48
49
2/2
✓ Branch 1 taken 548987 times.
✓ Branch 2 taken 3446 times.
552433 while ((parser = av_parser_iterate(&i))) {
50
2/2
✓ Branch 0 taken 545724 times.
✓ Branch 1 taken 3263 times.
548987 if (parser->codec_ids[0] == codec_id ||
51
2/2
✓ Branch 0 taken 541836 times.
✓ Branch 1 taken 3888 times.
545724 parser->codec_ids[1] == codec_id ||
52
2/2
✓ Branch 0 taken 541679 times.
✓ Branch 1 taken 157 times.
541836 parser->codec_ids[2] == codec_id ||
53
2/2
✓ Branch 0 taken 541671 times.
✓ Branch 1 taken 8 times.
541679 parser->codec_ids[3] == codec_id ||
54
2/2
✓ Branch 0 taken 541663 times.
✓ Branch 1 taken 8 times.
541671 parser->codec_ids[4] == codec_id ||
55
2/2
✓ Branch 0 taken 541659 times.
✓ Branch 1 taken 4 times.
541663 parser->codec_ids[5] == codec_id ||
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 541659 times.
541659 parser->codec_ids[6] == codec_id)
57 7328 goto found;
58 }
59 3446 return NULL;
60
61 7328 found:
62 7328 ffparser = ffcodecparser(parser);
63 7328 s = av_mallocz(sizeof(AVCodecParserContext));
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7328 times.
7328 if (!s)
65 goto err_out;
66 7328 s->parser = parser;
67 7328 s->priv_data = av_mallocz(ffparser->priv_data_size);
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7328 times.
7328 if (!s->priv_data)
69 goto err_out;
70 7328 s->fetch_timestamp=1;
71 7328 s->pict_type = AV_PICTURE_TYPE_I;
72
2/2
✓ Branch 0 taken 2255 times.
✓ Branch 1 taken 5073 times.
7328 if (ffparser->init) {
73 2255 ret = ffparser->init(s);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2255 times.
2255 if (ret != 0)
75 goto err_out;
76 }
77 7328 s->key_frame = -1;
78 7328 s->dts_sync_point = INT_MIN;
79 7328 s->dts_ref_dts_delta = INT_MIN;
80 7328 s->pts_dts_delta = INT_MIN;
81 7328 s->format = -1;
82
83 7328 return s;
84
85 err_out:
86 if (s)
87 av_freep(&s->priv_data);
88 av_free(s);
89 return NULL;
90 }
91
92 207544 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
93 {
94 int i;
95
96
2/2
✓ Branch 0 taken 206705 times.
✓ Branch 1 taken 839 times.
207544 if (!fuzzy) {
97 206705 s->dts =
98 206705 s->pts = AV_NOPTS_VALUE;
99 206705 s->pos = -1;
100 206705 s->offset = 0;
101 }
102
2/2
✓ Branch 0 taken 615375 times.
✓ Branch 1 taken 65392 times.
680767 for (i = 0; i < AV_PARSER_PTS_NB; i++) {
103
2/2
✓ Branch 0 taken 605640 times.
✓ Branch 1 taken 9735 times.
615375 if (s->cur_offset + off >= s->cur_frame_offset[i] &&
104
2/2
✓ Branch 0 taken 408270 times.
✓ Branch 1 taken 197370 times.
605640 (s->frame_offset < s->cur_frame_offset[i] ||
105
4/4
✓ Branch 0 taken 5830 times.
✓ Branch 1 taken 402440 times.
✓ Branch 2 taken 2174 times.
✓ Branch 3 taken 3656 times.
408270 (!s->frame_offset && !s->next_frame_offset)) && // first field/frame
106 // check disabled since MPEG-TS does not send complete PES packets
107
2/2
✓ Branch 0 taken 198445 times.
✓ Branch 1 taken 1099 times.
199544 /*s->next_frame_offset + off <*/ s->cur_frame_end[i]){
108
109
4/4
✓ Branch 0 taken 1139 times.
✓ Branch 1 taken 197306 times.
✓ Branch 2 taken 262 times.
✓ Branch 3 taken 877 times.
198445 if (!fuzzy || s->cur_frame_dts[i] != AV_NOPTS_VALUE) {
110 197568 s->dts = s->cur_frame_dts[i];
111 197568 s->pts = s->cur_frame_pts[i];
112 197568 s->pos = s->cur_frame_pos[i];
113 197568 s->offset = s->next_frame_offset - s->cur_frame_offset[i];
114 }
115
2/2
✓ Branch 0 taken 12727 times.
✓ Branch 1 taken 185718 times.
198445 if (remove)
116 12727 s->cur_frame_offset[i] = INT64_MAX;
117
2/2
✓ Branch 0 taken 142152 times.
✓ Branch 1 taken 56293 times.
198445 if (s->cur_offset + off < s->cur_frame_end[i])
118 142152 break;
119 }
120 }
121 207544 }
122
123 834404 int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
124 uint8_t **poutbuf, int *poutbuf_size,
125 const uint8_t *buf, int buf_size,
126 int64_t pts, int64_t dts, int64_t pos)
127 {
128 int index, i;
129 uint8_t dummy_buf[AV_INPUT_BUFFER_PADDING_SIZE];
130
131 av_assert1(avctx->codec_id != AV_CODEC_ID_NONE);
132
133 /* Parsers only work for the specified codec ids. */
134 av_assert1(avctx->codec_id == s->parser->codec_ids[0] ||
135 avctx->codec_id == s->parser->codec_ids[1] ||
136 avctx->codec_id == s->parser->codec_ids[2] ||
137 avctx->codec_id == s->parser->codec_ids[3] ||
138 avctx->codec_id == s->parser->codec_ids[4] ||
139 avctx->codec_id == s->parser->codec_ids[5] ||
140 avctx->codec_id == s->parser->codec_ids[6]);
141
142
2/2
✓ Branch 0 taken 3608 times.
✓ Branch 1 taken 830796 times.
834404 if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
143 3608 s->next_frame_offset =
144 3608 s->cur_offset = pos;
145 3608 s->flags |= PARSER_FLAG_FETCHED_OFFSET;
146 }
147
148
2/2
✓ Branch 0 taken 3355 times.
✓ Branch 1 taken 831049 times.
834404 if (buf_size == 0) {
149 /* padding is always necessary even if EOF, so we add it here */
150 3355 memset(dummy_buf, 0, sizeof(dummy_buf));
151 3355 buf = dummy_buf;
152
2/2
✓ Branch 0 taken 705495 times.
✓ Branch 1 taken 125554 times.
831049 } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
153 /* add a new packet descriptor */
154 705495 i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
155 705495 s->cur_frame_start_index = i;
156 705495 s->cur_frame_offset[i] = s->cur_offset;
157 705495 s->cur_frame_end[i] = s->cur_offset + buf_size;
158 705495 s->cur_frame_pts[i] = pts;
159 705495 s->cur_frame_dts[i] = dts;
160 705495 s->cur_frame_pos[i] = pos;
161 }
162
163
2/2
✓ Branch 0 taken 200233 times.
✓ Branch 1 taken 634171 times.
834404 if (s->fetch_timestamp) {
164 200233 s->fetch_timestamp = 0;
165 200233 s->last_pts = s->pts;
166 200233 s->last_dts = s->dts;
167 200233 s->last_pos = s->pos;
168 200233 ff_fetch_timestamp(s, 0, 0, 0);
169 }
170 /* WARNING: the returned index can be negative */
171 834404 index = ffcodecparser(s->parser)->parse(s, avctx, (const uint8_t **) poutbuf,
172 poutbuf_size, buf, buf_size);
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 834404 times.
834404 av_assert0(index > -0x20000000); // The API does not allow returning AVERROR codes
174 #define FILL(name) if(s->name > 0 && avctx->name <= 0) avctx->name = s->name
175
2/2
✓ Branch 0 taken 632425 times.
✓ Branch 1 taken 201979 times.
834404 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
176
4/4
✓ Branch 0 taken 240081 times.
✓ Branch 1 taken 392344 times.
✓ Branch 2 taken 489 times.
✓ Branch 3 taken 239592 times.
632425 FILL(field_order);
177
4/4
✓ Branch 0 taken 339045 times.
✓ Branch 1 taken 293380 times.
✓ Branch 2 taken 880 times.
✓ Branch 3 taken 338165 times.
632425 FILL(coded_width);
178
4/4
✓ Branch 0 taken 339045 times.
✓ Branch 1 taken 293380 times.
✓ Branch 2 taken 880 times.
✓ Branch 3 taken 338165 times.
632425 FILL(coded_height);
179
4/4
✓ Branch 0 taken 342189 times.
✓ Branch 1 taken 290236 times.
✓ Branch 2 taken 544 times.
✓ Branch 3 taken 341645 times.
632425 FILL(width);
180
4/4
✓ Branch 0 taken 342189 times.
✓ Branch 1 taken 290236 times.
✓ Branch 2 taken 544 times.
✓ Branch 3 taken 341645 times.
632425 FILL(height);
181 }
182
183 /* update the file pointer */
184
2/2
✓ Branch 0 taken 197622 times.
✓ Branch 1 taken 636782 times.
834404 if (*poutbuf_size) {
185 /* fill the data for the current frame */
186 197622 s->frame_offset = s->next_frame_offset;
187
188 /* offset of the next frame */
189 197622 s->next_frame_offset = s->cur_offset + index;
190 197622 s->fetch_timestamp = 1;
191 } else {
192 /* Don't return a pointer to dummy_buf. */
193 636782 *poutbuf = NULL;
194 }
195
2/2
✓ Branch 0 taken 4869 times.
✓ Branch 1 taken 829535 times.
834404 if (index < 0)
196 4869 index = 0;
197 834404 s->cur_offset += index;
198 834404 return index;
199 }
200
201 21242 av_cold void av_parser_close(AVCodecParserContext *s)
202 {
203
2/2
✓ Branch 0 taken 7328 times.
✓ Branch 1 taken 13914 times.
21242 if (s) {
204
2/2
✓ Branch 1 taken 6945 times.
✓ Branch 2 taken 383 times.
7328 if (ffcodecparser(s->parser)->close)
205 6945 ffcodecparser(s->parser)->close(s);
206 7328 av_freep(&s->priv_data);
207 7328 av_free(s);
208 }
209 21242 }
210
211 771732 int ff_combine_frame(ParseContext *pc, int next,
212 const uint8_t **buf, int *buf_size)
213 {
214 771732 if (pc->overread) {
215 ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
216 pc->overread, pc->state, next, pc->index, pc->overread_index);
217 ff_dlog(NULL, "%X %X %X %X\n",
218 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
219 }
220
221 /* Copy overread bytes from last frame into buffer. */
222
2/2
✓ Branch 0 taken 1381 times.
✓ Branch 1 taken 771732 times.
773113 for (; pc->overread > 0; pc->overread--)
223 1381 pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
224
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 771732 times.
771732 if (next > *buf_size)
226 return AVERROR(EINVAL);
227
228 /* flush remaining if EOF */
229
4/4
✓ Branch 0 taken 2183 times.
✓ Branch 1 taken 769549 times.
✓ Branch 2 taken 1726 times.
✓ Branch 3 taken 457 times.
771732 if (!*buf_size && next == END_NOT_FOUND)
230 1726 next = 0;
231
232 771732 pc->last_index = pc->index;
233
234 /* copy into buffer end return */
235
2/2
✓ Branch 0 taken 626509 times.
✓ Branch 1 taken 145223 times.
771732 if (next == END_NOT_FOUND) {
236 626509 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
237 626509 *buf_size + pc->index +
238 AV_INPUT_BUFFER_PADDING_SIZE);
239
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 626509 times.
626509 if (!new_buffer) {
241 av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", *buf_size + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
242 pc->index = 0;
243 return AVERROR(ENOMEM);
244 }
245 626509 pc->buffer = new_buffer;
246 626509 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
247 626509 memset(&pc->buffer[pc->index + *buf_size], 0, AV_INPUT_BUFFER_PADDING_SIZE);
248 626509 pc->index += *buf_size;
249 626509 return -1;
250 }
251
252
3/4
✓ Branch 0 taken 277 times.
✓ Branch 1 taken 144946 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 277 times.
145223 av_assert0(next >= 0 || pc->buffer);
253
254 145223 *buf_size =
255 145223 pc->overread_index = pc->index + next;
256
257 /* append to buffer */
258
2/2
✓ Branch 0 taken 61323 times.
✓ Branch 1 taken 83900 times.
145223 if (pc->index) {
259 61323 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
260 61323 next + pc->index +
261 AV_INPUT_BUFFER_PADDING_SIZE);
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61323 times.
61323 if (!new_buffer) {
263 av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", next + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
264 *buf_size =
265 pc->overread_index =
266 pc->index = 0;
267 return AVERROR(ENOMEM);
268 }
269 61323 pc->buffer = new_buffer;
270
2/2
✓ Branch 0 taken 61318 times.
✓ Branch 1 taken 5 times.
61323 if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
271 61318 memcpy(&pc->buffer[pc->index], *buf,
272 61318 next + AV_INPUT_BUFFER_PADDING_SIZE);
273 61323 pc->index = 0;
274 61323 *buf = pc->buffer;
275 }
276
277
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 145185 times.
145223 if (next < -8) {
278 38 pc->overread += -8 - next;
279 38 next = -8;
280 }
281 /* store overread bytes */
282
2/2
✓ Branch 0 taken 986 times.
✓ Branch 1 taken 145223 times.
146209 for (; next < 0; next++) {
283 986 pc->state = pc->state << 8 | pc->buffer[pc->last_index + next];
284 986 pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
285 986 pc->overread++;
286 }
287
288 145223 if (pc->overread) {
289 ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
290 pc->overread, pc->state, next, pc->index, pc->overread_index);
291 ff_dlog(NULL, "%X %X %X %X\n",
292 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
293 }
294
295 145223 return 0;
296 }
297
298 5610 av_cold void ff_parse_close(AVCodecParserContext *s)
299 {
300 5610 ParseContext *pc = s->priv_data;
301
302 5610 av_freep(&pc->buffer);
303 5610 }
304