FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/parser.c
Date: 2022-01-28 07:56: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 9223 AVCodecParserContext *av_parser_init(int codec_id)
35 {
36 9223 AVCodecParserContext *s = NULL;
37 const AVCodecParser *parser;
38 9223 void *i = 0;
39 int ret;
40
41
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 9163 times.
9223 if (codec_id == AV_CODEC_ID_NONE)
42 60 return NULL;
43
44
2/2
✓ Branch 1 taken 373936 times.
✓ Branch 2 taken 3176 times.
377112 while ((parser = av_parser_iterate(&i))) {
45
2/2
✓ Branch 0 taken 371358 times.
✓ Branch 1 taken 2578 times.
373936 if (parser->codec_ids[0] == codec_id ||
46
2/2
✓ Branch 0 taken 368116 times.
✓ Branch 1 taken 3242 times.
371358 parser->codec_ids[1] == codec_id ||
47
2/2
✓ Branch 0 taken 367965 times.
✓ Branch 1 taken 151 times.
368116 parser->codec_ids[2] == codec_id ||
48
2/2
✓ Branch 0 taken 367957 times.
✓ Branch 1 taken 8 times.
367965 parser->codec_ids[3] == codec_id ||
49
2/2
✓ Branch 0 taken 367949 times.
✓ Branch 1 taken 8 times.
367957 parser->codec_ids[4] == codec_id ||
50
1/2
✓ Branch 0 taken 367949 times.
✗ Branch 1 not taken.
367949 parser->codec_ids[5] == codec_id ||
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 367949 times.
367949 parser->codec_ids[6] == codec_id)
52 5987 goto found;
53 }
54 3176 return NULL;
55
56 5987 found:
57 5987 s = av_mallocz(sizeof(AVCodecParserContext));
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5987 times.
5987 if (!s)
59 goto err_out;
60 5987 s->parser = parser;
61 5987 s->priv_data = av_mallocz(parser->priv_data_size);
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5987 times.
5987 if (!s->priv_data)
63 goto err_out;
64 5987 s->fetch_timestamp=1;
65 5987 s->pict_type = AV_PICTURE_TYPE_I;
66
2/2
✓ Branch 0 taken 1939 times.
✓ Branch 1 taken 4048 times.
5987 if (parser->parser_init) {
67 1939 ret = parser->parser_init(s);
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1939 times.
1939 if (ret != 0)
69 goto err_out;
70 }
71 5987 s->key_frame = -1;
72 5987 s->dts_sync_point = INT_MIN;
73 5987 s->dts_ref_dts_delta = INT_MIN;
74 5987 s->pts_dts_delta = INT_MIN;
75 5987 s->format = -1;
76
77 5987 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 158793 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
87 {
88 int i;
89
90
2/2
✓ Branch 0 taken 158010 times.
✓ Branch 1 taken 783 times.
158793 if (!fuzzy) {
91 158010 s->dts =
92 158010 s->pts = AV_NOPTS_VALUE;
93 158010 s->pos = -1;
94 158010 s->offset = 0;
95 }
96
2/2
✓ Branch 0 taken 456452 times.
✓ Branch 1 taken 40417 times.
496869 for (i = 0; i < AV_PARSER_PTS_NB; i++) {
97
2/2
✓ Branch 0 taken 447462 times.
✓ Branch 1 taken 8990 times.
456452 if (s->cur_offset + off >= s->cur_frame_offset[i] &&
98
2/2
✓ Branch 0 taken 284859 times.
✓ Branch 1 taken 162603 times.
447462 (s->frame_offset < s->cur_frame_offset[i] ||
99
4/4
✓ Branch 0 taken 4947 times.
✓ Branch 1 taken 279912 times.
✓ Branch 2 taken 1738 times.
✓ Branch 3 taken 3209 times.
284859 (!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 163460 times.
✓ Branch 1 taken 881 times.
164341 /*s->next_frame_offset + off <*/ s->cur_frame_end[i]){
102
103
4/4
✓ Branch 0 taken 1049 times.
✓ Branch 1 taken 162411 times.
✓ Branch 2 taken 216 times.
✓ Branch 3 taken 833 times.
163460 if (!fuzzy || s->cur_frame_dts[i] != AV_NOPTS_VALUE) {
104 162627 s->dts = s->cur_frame_dts[i];
105 162627 s->pts = s->cur_frame_pts[i];
106 162627 s->pos = s->cur_frame_pos[i];
107 162627 s->offset = s->next_frame_offset - s->cur_frame_offset[i];
108 }
109
2/2
✓ Branch 0 taken 10934 times.
✓ Branch 1 taken 152526 times.
163460 if (remove)
110 10934 s->cur_frame_offset[i] = INT64_MAX;
111
2/2
✓ Branch 0 taken 118376 times.
✓ Branch 1 taken 45084 times.
163460 if (s->cur_offset + off < s->cur_frame_end[i])
112 118376 break;
113 }
114 }
115 158793 }
116
117 676391 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 3138 times.
✓ Branch 1 taken 673253 times.
676391 if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
137 3138 s->next_frame_offset =
138 3138 s->cur_offset = pos;
139 3138 s->flags |= PARSER_FLAG_FETCHED_OFFSET;
140 }
141
142
2/2
✓ Branch 0 taken 2742 times.
✓ Branch 1 taken 673649 times.
676391 if (buf_size == 0) {
143 /* padding is always necessary even if EOF, so we add it here */
144 2742 memset(dummy_buf, 0, sizeof(dummy_buf));
145 2742 buf = dummy_buf;
146
2/2
✓ Branch 0 taken 586404 times.
✓ Branch 1 taken 87245 times.
673649 } 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 586404 i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
149 586404 s->cur_frame_start_index = i;
150 586404 s->cur_frame_offset[i] = s->cur_offset;
151 586404 s->cur_frame_end[i] = s->cur_offset + buf_size;
152 586404 s->cur_frame_pts[i] = pts;
153 586404 s->cur_frame_dts[i] = dts;
154 586404 s->cur_frame_pos[i] = pos;
155 }
156
157
2/2
✓ Branch 0 taken 152388 times.
✓ Branch 1 taken 524003 times.
676391 if (s->fetch_timestamp) {
158 152388 s->fetch_timestamp = 0;
159 152388 s->last_pts = s->pts;
160 152388 s->last_dts = s->dts;
161 152388 s->last_pos = s->pos;
162 152388 ff_fetch_timestamp(s, 0, 0, 0);
163 }
164 /* WARNING: the returned index can be negative */
165 676391 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 676391 times.
676391 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 532848 times.
✓ Branch 1 taken 143543 times.
676391 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
170
4/4
✓ Branch 0 taken 213836 times.
✓ Branch 1 taken 319012 times.
✓ Branch 2 taken 412 times.
✓ Branch 3 taken 213424 times.
532848 FILL(field_order);
171 }
172
173 /* update the file pointer */
174
2/2
✓ Branch 0 taken 150204 times.
✓ Branch 1 taken 526187 times.
676391 if (*poutbuf_size) {
175 /* fill the data for the current frame */
176 150204 s->frame_offset = s->next_frame_offset;
177
178 /* offset of the next frame */
179 150204 s->next_frame_offset = s->cur_offset + index;
180 150204 s->fetch_timestamp = 1;
181 } else {
182 /* Don't return a pointer to dummy_buf. */
183 526187 *poutbuf = NULL;
184 }
185
2/2
✓ Branch 0 taken 4170 times.
✓ Branch 1 taken 672221 times.
676391 if (index < 0)
186 4170 index = 0;
187 676391 s->cur_offset += index;
188 676391 return index;
189 }
190
191 16616 void av_parser_close(AVCodecParserContext *s)
192 {
193
2/2
✓ Branch 0 taken 5987 times.
✓ Branch 1 taken 10629 times.
16616 if (s) {
194
2/2
✓ Branch 0 taken 5680 times.
✓ Branch 1 taken 307 times.
5987 if (s->parser->parser_close)
195 5680 s->parser->parser_close(s);
196 5987 av_freep(&s->priv_data);
197 5987 av_free(s);
198 }
199 16616 }
200
201 626696 int ff_combine_frame(ParseContext *pc, int next,
202 const uint8_t **buf, int *buf_size)
203 {
204 626696 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 626696 times.
627162 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 626696 times.
626696 if (next > *buf_size)
216 return AVERROR(EINVAL);
217
218 /* flush remaining if EOF */
219
4/4
✓ Branch 0 taken 1789 times.
✓ Branch 1 taken 624907 times.
✓ Branch 2 taken 1408 times.
✓ Branch 3 taken 381 times.
626696 if (!*buf_size && next == END_NOT_FOUND)
220 1408 next = 0;
221
222 626696 pc->last_index = pc->index;
223
224 /* copy into buffer end return */
225
2/2
✓ Branch 0 taken 520947 times.
✓ Branch 1 taken 105749 times.
626696 if (next == END_NOT_FOUND) {
226 520947 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
227 520947 *buf_size + pc->index +
228 AV_INPUT_BUFFER_PADDING_SIZE);
229
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520947 times.
520947 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 520947 pc->buffer = new_buffer;
236 520947 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
237 520947 pc->index += *buf_size;
238 520947 return -1;
239 }
240
241
3/4
✓ Branch 0 taken 203 times.
✓ Branch 1 taken 105546 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 203 times.
105749 av_assert0(next >= 0 || pc->buffer);
242
243 105749 *buf_size =
244 105749 pc->overread_index = pc->index + next;
245
246 /* append to buffer */
247
2/2
✓ Branch 0 taken 47721 times.
✓ Branch 1 taken 58028 times.
105749 if (pc->index) {
248 47721 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
249 47721 next + pc->index +
250 AV_INPUT_BUFFER_PADDING_SIZE);
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47721 times.
47721 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 47721 pc->buffer = new_buffer;
258
2/2
✓ Branch 0 taken 47717 times.
✓ Branch 1 taken 4 times.
47721 if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
259 47717 memcpy(&pc->buffer[pc->index], *buf,
260 47717 next + AV_INPUT_BUFFER_PADDING_SIZE);
261 47721 pc->index = 0;
262 47721 *buf = pc->buffer;
263 }
264
265
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 105712 times.
105749 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 105749 times.
106511 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 105749 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 105749 return 0;
284 }
285
286 4758 void ff_parse_close(AVCodecParserContext *s)
287 {
288 4758 ParseContext *pc = s->priv_data;
289
290 4758 av_freep(&pc->buffer);
291 4758 }
292