FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/parser.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 137 153 89.5%
Branches: 88 100 88.0%

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/avassert.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/mem.h"
30
31 #include "internal.h"
32 #include "parser.h"
33
34 8989 AVCodecParserContext *av_parser_init(int codec_id)
35 {
36 8989 AVCodecParserContext *s = NULL;
37 const AVCodecParser *parser;
38 8989 void *i = 0;
39 int ret;
40
41
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 8935 times.
8989 if (codec_id == AV_CODEC_ID_NONE)
42 54 return NULL;
43
44
2/2
✓ Branch 1 taken 357317 times.
✓ Branch 2 taken 3132 times.
360449 while ((parser = av_parser_iterate(&i))) {
45
2/2
✓ Branch 0 taken 354801 times.
✓ Branch 1 taken 2516 times.
357317 if (parser->codec_ids[0] == codec_id ||
46
2/2
✓ Branch 0 taken 351678 times.
✓ Branch 1 taken 3123 times.
354801 parser->codec_ids[1] == codec_id ||
47
2/2
✓ Branch 0 taken 351530 times.
✓ Branch 1 taken 148 times.
351678 parser->codec_ids[2] == codec_id ||
48
2/2
✓ Branch 0 taken 351522 times.
✓ Branch 1 taken 8 times.
351530 parser->codec_ids[3] == codec_id ||
49
2/2
✓ Branch 0 taken 351514 times.
✓ Branch 1 taken 8 times.
351522 parser->codec_ids[4] == codec_id ||
50
1/2
✓ Branch 0 taken 351514 times.
✗ Branch 1 not taken.
351514 parser->codec_ids[5] == codec_id ||
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 351514 times.
351514 parser->codec_ids[6] == codec_id)
52 5803 goto found;
53 }
54 3132 return NULL;
55
56 5803 found:
57 5803 s = av_mallocz(sizeof(AVCodecParserContext));
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5803 times.
5803 if (!s)
59 goto err_out;
60 5803 s->parser = parser;
61 5803 s->priv_data = av_mallocz(parser->priv_data_size);
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5803 times.
5803 if (!s->priv_data)
63 goto err_out;
64 5803 s->fetch_timestamp=1;
65 5803 s->pict_type = AV_PICTURE_TYPE_I;
66
2/2
✓ Branch 0 taken 1884 times.
✓ Branch 1 taken 3919 times.
5803 if (parser->parser_init) {
67 1884 ret = parser->parser_init(s);
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1884 times.
1884 if (ret != 0)
69 goto err_out;
70 }
71 5803 s->key_frame = -1;
72 5803 s->dts_sync_point = INT_MIN;
73 5803 s->dts_ref_dts_delta = INT_MIN;
74 5803 s->pts_dts_delta = INT_MIN;
75 5803 s->format = -1;
76
77 5803 return s;
78
79 err_out:
80 if (s)
81 av_freep(&s->priv_data);
82 av_free(s);
83 return NULL;
84 }
85
86 153550 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
87 {
88 int i;
89
90
2/2
✓ Branch 0 taken 152795 times.
✓ Branch 1 taken 755 times.
153550 if (!fuzzy) {
91 152795 s->dts =
92 152795 s->pts = AV_NOPTS_VALUE;
93 152795 s->pos = -1;
94 152795 s->offset = 0;
95 }
96
2/2
✓ Branch 0 taken 438904 times.
✓ Branch 1 taken 37452 times.
476356 for (i = 0; i < AV_PARSER_PTS_NB; i++) {
97
2/2
✓ Branch 0 taken 431375 times.
✓ Branch 1 taken 7529 times.
438904 if (s->cur_offset + off >= s->cur_frame_offset[i] &&
98
2/2
✓ Branch 0 taken 271484 times.
✓ Branch 1 taken 159891 times.
431375 (s->frame_offset < s->cur_frame_offset[i] ||
99
4/4
✓ Branch 0 taken 4897 times.
✓ Branch 1 taken 266587 times.
✓ Branch 2 taken 1732 times.
✓ Branch 3 taken 3165 times.
271484 (!s->frame_offset && !s->next_frame_offset)) && // first field/frame
100 // check disabled since MPEG-TS does not send complete PES packets
101
2/2
✓ Branch 0 taken 160745 times.
✓ Branch 1 taken 878 times.
161623 /*s->next_frame_offset + off <*/ s->cur_frame_end[i]){
102
103
4/4
✓ Branch 0 taken 1009 times.
✓ Branch 1 taken 159736 times.
✓ Branch 2 taken 200 times.
✓ Branch 3 taken 809 times.
160745 if (!fuzzy || s->cur_frame_dts[i] != AV_NOPTS_VALUE) {
104 159936 s->dts = s->cur_frame_dts[i];
105 159936 s->pts = s->cur_frame_pts[i];
106 159936 s->pos = s->cur_frame_pos[i];
107 159936 s->offset = s->next_frame_offset - s->cur_frame_offset[i];
108 }
109
2/2
✓ Branch 0 taken 10109 times.
✓ Branch 1 taken 150636 times.
160745 if (remove)
110 10109 s->cur_frame_offset[i] = INT64_MAX;
111
2/2
✓ Branch 0 taken 116098 times.
✓ Branch 1 taken 44647 times.
160745 if (s->cur_offset + off < s->cur_frame_end[i])
112 116098 break;
113 }
114 }
115 153550 }
116
117 669610 int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
118 uint8_t **poutbuf, int *poutbuf_size,
119 const uint8_t *buf, int buf_size,
120 int64_t pts, int64_t dts, int64_t pos)
121 {
122 int index, i;
123 uint8_t dummy_buf[AV_INPUT_BUFFER_PADDING_SIZE];
124
125 av_assert1(avctx->codec_id != AV_CODEC_ID_NONE);
126
127 /* Parsers only work for the specified codec ids. */
128 av_assert1(avctx->codec_id == s->parser->codec_ids[0] ||
129 avctx->codec_id == s->parser->codec_ids[1] ||
130 avctx->codec_id == s->parser->codec_ids[2] ||
131 avctx->codec_id == s->parser->codec_ids[3] ||
132 avctx->codec_id == s->parser->codec_ids[4] ||
133 avctx->codec_id == s->parser->codec_ids[5] ||
134 avctx->codec_id == s->parser->codec_ids[6]);
135
136
2/2
✓ Branch 0 taken 3100 times.
✓ Branch 1 taken 666510 times.
669610 if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
137 3100 s->next_frame_offset =
138 3100 s->cur_offset = pos;
139 3100 s->flags |= PARSER_FLAG_FETCHED_OFFSET;
140 }
141
142
2/2
✓ Branch 0 taken 2684 times.
✓ Branch 1 taken 666926 times.
669610 if (buf_size == 0) {
143 /* padding is always necessary even if EOF, so we add it here */
144 2684 memset(dummy_buf, 0, sizeof(dummy_buf));
145 2684 buf = dummy_buf;
146
2/2
✓ Branch 0 taken 583308 times.
✓ Branch 1 taken 83618 times.
666926 } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
147 /* add a new packet descriptor */
148 583308 i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
149 583308 s->cur_frame_start_index = i;
150 583308 s->cur_frame_offset[i] = s->cur_offset;
151 583308 s->cur_frame_end[i] = s->cur_offset + buf_size;
152 583308 s->cur_frame_pts[i] = pts;
153 583308 s->cur_frame_dts[i] = dts;
154 583308 s->cur_frame_pos[i] = pos;
155 }
156
157
2/2
✓ Branch 0 taken 147771 times.
✓ Branch 1 taken 521839 times.
669610 if (s->fetch_timestamp) {
158 147771 s->fetch_timestamp = 0;
159 147771 s->last_pts = s->pts;
160 147771 s->last_dts = s->dts;
161 147771 s->last_pos = s->pos;
162 147771 ff_fetch_timestamp(s, 0, 0, 0);
163 }
164 /* WARNING: the returned index can be negative */
165 669610 index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
166 poutbuf_size, buf, buf_size);
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669610 times.
669610 av_assert0(index > -0x20000000); // The API does not allow returning AVERROR codes
168 #define FILL(name) if(s->name > 0 && avctx->name <= 0) avctx->name = s->name
169
2/2
✓ Branch 0 taken 529996 times.
✓ Branch 1 taken 139614 times.
669610 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
170
4/4
✓ Branch 0 taken 211009 times.
✓ Branch 1 taken 318987 times.
✓ Branch 2 taken 405 times.
✓ Branch 3 taken 210604 times.
529996 FILL(field_order);
171 }
172
173 /* update the file pointer */
174
2/2
✓ Branch 0 taken 145622 times.
✓ Branch 1 taken 523988 times.
669610 if (*poutbuf_size) {
175 /* fill the data for the current frame */
176 145622 s->frame_offset = s->next_frame_offset;
177
178 /* offset of the next frame */
179 145622 s->next_frame_offset = s->cur_offset + index;
180 145622 s->fetch_timestamp = 1;
181 } else {
182 /* Don't return a pointer to dummy_buf. */
183 523988 *poutbuf = NULL;
184 }
185
2/2
✓ Branch 0 taken 4125 times.
✓ Branch 1 taken 665485 times.
669610 if (index < 0)
186 4125 index = 0;
187 669610 s->cur_offset += index;
188 669610 return index;
189 }
190
191 16184 void av_parser_close(AVCodecParserContext *s)
192 {
193
2/2
✓ Branch 0 taken 5803 times.
✓ Branch 1 taken 10381 times.
16184 if (s) {
194
2/2
✓ Branch 0 taken 5496 times.
✓ Branch 1 taken 307 times.
5803 if (s->parser->parser_close)
195 5496 s->parser->parser_close(s);
196 5803 av_freep(&s->priv_data);
197 5803 av_free(s);
198 }
199 16184 }
200
201 620153 int ff_combine_frame(ParseContext *pc, int next,
202 const uint8_t **buf, int *buf_size)
203 {
204 620153 if (pc->overread) {
205 ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
206 pc->overread, pc->state, next, pc->index, pc->overread_index);
207 ff_dlog(NULL, "%X %X %X %X\n",
208 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
209 }
210
211 /* Copy overread bytes from last frame into buffer. */
212
2/2
✓ Branch 0 taken 466 times.
✓ Branch 1 taken 620153 times.
620619 for (; pc->overread > 0; pc->overread--)
213 466 pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
214
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 620153 times.
620153 if (next > *buf_size)
216 return AVERROR(EINVAL);
217
218 /* flush remaining if EOF */
219
4/4
✓ Branch 0 taken 1743 times.
✓ Branch 1 taken 618410 times.
✓ Branch 2 taken 1369 times.
✓ Branch 3 taken 374 times.
620153 if (!*buf_size && next == END_NOT_FOUND)
220 1369 next = 0;
221
222 620153 pc->last_index = pc->index;
223
224 /* copy into buffer end return */
225
2/2
✓ Branch 0 taken 518814 times.
✓ Branch 1 taken 101339 times.
620153 if (next == END_NOT_FOUND) {
226 518814 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
227 518814 *buf_size + pc->index +
228 AV_INPUT_BUFFER_PADDING_SIZE);
229
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 518814 times.
518814 if (!new_buffer) {
231 av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", *buf_size + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
232 pc->index = 0;
233 return AVERROR(ENOMEM);
234 }
235 518814 pc->buffer = new_buffer;
236 518814 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
237 518814 pc->index += *buf_size;
238 518814 return -1;
239 }
240
241
3/4
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 101136 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 203 times.
101339 av_assert0(next >= 0 || pc->buffer);
242
243 101339 *buf_size =
244 101339 pc->overread_index = pc->index + next;
245
246 /* append to buffer */
247
2/2
✓ Branch 0 taken 47002 times.
✓ Branch 1 taken 54337 times.
101339 if (pc->index) {
248 47002 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
249 47002 next + pc->index +
250 AV_INPUT_BUFFER_PADDING_SIZE);
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47002 times.
47002 if (!new_buffer) {
252 av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", next + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
253 pc->overread_index =
254 pc->index = 0;
255 return AVERROR(ENOMEM);
256 }
257 47002 pc->buffer = new_buffer;
258
2/2
✓ Branch 0 taken 46998 times.
✓ Branch 1 taken 4 times.
47002 if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
259 46998 memcpy(&pc->buffer[pc->index], *buf,
260 46998 next + AV_INPUT_BUFFER_PADDING_SIZE);
261 47002 pc->index = 0;
262 47002 *buf = pc->buffer;
263 }
264
265
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 101302 times.
101339 if (next < -8) {
266 37 pc->overread += -8 - next;
267 37 next = -8;
268 }
269 /* store overread bytes */
270
2/2
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 101339 times.
102101 for (; next < 0; next++) {
271 762 pc->state = pc->state << 8 | pc->buffer[pc->last_index + next];
272 762 pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
273 762 pc->overread++;
274 }
275
276 101339 if (pc->overread) {
277 ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
278 pc->overread, pc->state, next, pc->index, pc->overread_index);
279 ff_dlog(NULL, "%X %X %X %X\n",
280 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
281 }
282
283 101339 return 0;
284 }
285
286 4588 void ff_parse_close(AVCodecParserContext *s)
287 {
288 4588 ParseContext *pc = s->priv_data;
289
290 4588 av_freep(&pc->buffer);
291 4588 }
292