| 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 | 10793 | 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 | 10793 | AVCodecParserContext *s = NULL; | |
| 41 | const AVCodecParser *parser; | ||
| 42 | const FFCodecParser *ffparser; | ||
| 43 | 10793 | void *i = 0; | |
| 44 | int ret; | ||
| 45 | |||
| 46 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 10729 times.
|
10793 | if (codec_id == AV_CODEC_ID_NONE) |
| 47 | 64 | return NULL; | |
| 48 | |||
| 49 |
2/2✓ Branch 1 taken 538138 times.
✓ Branch 2 taken 3435 times.
|
541573 | while ((parser = av_parser_iterate(&i))) { |
| 50 |
2/2✓ Branch 0 taken 534907 times.
✓ Branch 1 taken 3231 times.
|
538138 | if (parser->codec_ids[0] == codec_id || |
| 51 |
2/2✓ Branch 0 taken 531021 times.
✓ Branch 1 taken 3886 times.
|
534907 | parser->codec_ids[1] == codec_id || |
| 52 |
2/2✓ Branch 0 taken 530864 times.
✓ Branch 1 taken 157 times.
|
531021 | parser->codec_ids[2] == codec_id || |
| 53 |
2/2✓ Branch 0 taken 530856 times.
✓ Branch 1 taken 8 times.
|
530864 | parser->codec_ids[3] == codec_id || |
| 54 |
2/2✓ Branch 0 taken 530848 times.
✓ Branch 1 taken 8 times.
|
530856 | parser->codec_ids[4] == codec_id || |
| 55 |
2/2✓ Branch 0 taken 530844 times.
✓ Branch 1 taken 4 times.
|
530848 | parser->codec_ids[5] == codec_id || |
| 56 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 530844 times.
|
530844 | parser->codec_ids[6] == codec_id) |
| 57 | 7294 | goto found; | |
| 58 | } | ||
| 59 | 3435 | return NULL; | |
| 60 | |||
| 61 | 7294 | found: | |
| 62 | 7294 | ffparser = ffcodecparser(parser); | |
| 63 | 7294 | s = av_mallocz(sizeof(AVCodecParserContext)); | |
| 64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7294 times.
|
7294 | if (!s) |
| 65 | ✗ | goto err_out; | |
| 66 | 7294 | s->parser = parser; | |
| 67 | 7294 | s->priv_data = av_mallocz(ffparser->priv_data_size); | |
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7294 times.
|
7294 | if (!s->priv_data) |
| 69 | ✗ | goto err_out; | |
| 70 | 7294 | s->fetch_timestamp=1; | |
| 71 | 7294 | s->pict_type = AV_PICTURE_TYPE_I; | |
| 72 |
2/2✓ Branch 0 taken 2242 times.
✓ Branch 1 taken 5052 times.
|
7294 | if (ffparser->init) { |
| 73 | 2242 | ret = ffparser->init(s); | |
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2242 times.
|
2242 | if (ret != 0) |
| 75 | ✗ | goto err_out; | |
| 76 | } | ||
| 77 | 7294 | s->key_frame = -1; | |
| 78 | 7294 | s->dts_sync_point = INT_MIN; | |
| 79 | 7294 | s->dts_ref_dts_delta = INT_MIN; | |
| 80 | 7294 | s->pts_dts_delta = INT_MIN; | |
| 81 | 7294 | s->format = -1; | |
| 82 | |||
| 83 | 7294 | 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 | 206865 | void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy) | |
| 93 | { | ||
| 94 | int i; | ||
| 95 | |||
| 96 |
2/2✓ Branch 0 taken 206032 times.
✓ Branch 1 taken 833 times.
|
206865 | if (!fuzzy) { |
| 97 | 206032 | s->dts = | |
| 98 | 206032 | s->pts = AV_NOPTS_VALUE; | |
| 99 | 206032 | s->pos = -1; | |
| 100 | 206032 | s->offset = 0; | |
| 101 | } | ||
| 102 |
2/2✓ Branch 0 taken 613386 times.
✓ Branch 1 taken 65184 times.
|
678570 | for (i = 0; i < AV_PARSER_PTS_NB; i++) { |
| 103 |
2/2✓ Branch 0 taken 603651 times.
✓ Branch 1 taken 9735 times.
|
613386 | if (s->cur_offset + off >= s->cur_frame_offset[i] && |
| 104 |
2/2✓ Branch 0 taken 407140 times.
✓ Branch 1 taken 196511 times.
|
603651 | (s->frame_offset < s->cur_frame_offset[i] || |
| 105 |
4/4✓ Branch 0 taken 5819 times.
✓ Branch 1 taken 401321 times.
✓ Branch 2 taken 2170 times.
✓ Branch 3 taken 3649 times.
|
407140 | (!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 197584 times.
✓ Branch 1 taken 1097 times.
|
198681 | /*s->next_frame_offset + off <*/ s->cur_frame_end[i]){ |
| 108 | |||
| 109 |
4/4✓ Branch 0 taken 1129 times.
✓ Branch 1 taken 196455 times.
✓ Branch 2 taken 256 times.
✓ Branch 3 taken 873 times.
|
197584 | if (!fuzzy || s->cur_frame_dts[i] != AV_NOPTS_VALUE) { |
| 110 | 196711 | s->dts = s->cur_frame_dts[i]; | |
| 111 | 196711 | s->pts = s->cur_frame_pts[i]; | |
| 112 | 196711 | s->pos = s->cur_frame_pos[i]; | |
| 113 | 196711 | s->offset = s->next_frame_offset - s->cur_frame_offset[i]; | |
| 114 | } | ||
| 115 |
2/2✓ Branch 0 taken 12545 times.
✓ Branch 1 taken 185039 times.
|
197584 | if (remove) |
| 116 | 12545 | s->cur_frame_offset[i] = INT64_MAX; | |
| 117 |
2/2✓ Branch 0 taken 141681 times.
✓ Branch 1 taken 55903 times.
|
197584 | if (s->cur_offset + off < s->cur_frame_end[i]) |
| 118 | 141681 | break; | |
| 119 | } | ||
| 120 | } | ||
| 121 | 206865 | } | |
| 122 | |||
| 123 | 831731 | 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 3603 times.
✓ Branch 1 taken 828128 times.
|
831731 | if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) { |
| 143 | 3603 | s->next_frame_offset = | |
| 144 | 3603 | s->cur_offset = pos; | |
| 145 | 3603 | s->flags |= PARSER_FLAG_FETCHED_OFFSET; | |
| 146 | } | ||
| 147 | |||
| 148 |
2/2✓ Branch 0 taken 3347 times.
✓ Branch 1 taken 828384 times.
|
831731 | if (buf_size == 0) { |
| 149 | /* padding is always necessary even if EOF, so we add it here */ | ||
| 150 | 3347 | memset(dummy_buf, 0, sizeof(dummy_buf)); | |
| 151 | 3347 | buf = dummy_buf; | |
| 152 |
2/2✓ Branch 0 taken 703419 times.
✓ Branch 1 taken 124965 times.
|
828384 | } 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 | 703419 | i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1); | |
| 155 | 703419 | s->cur_frame_start_index = i; | |
| 156 | 703419 | s->cur_frame_offset[i] = s->cur_offset; | |
| 157 | 703419 | s->cur_frame_end[i] = s->cur_offset + buf_size; | |
| 158 | 703419 | s->cur_frame_pts[i] = pts; | |
| 159 | 703419 | s->cur_frame_dts[i] = dts; | |
| 160 | 703419 | s->cur_frame_pos[i] = pos; | |
| 161 | } | ||
| 162 | |||
| 163 |
2/2✓ Branch 0 taken 199634 times.
✓ Branch 1 taken 632097 times.
|
831731 | if (s->fetch_timestamp) { |
| 164 | 199634 | s->fetch_timestamp = 0; | |
| 165 | 199634 | s->last_pts = s->pts; | |
| 166 | 199634 | s->last_dts = s->dts; | |
| 167 | 199634 | s->last_pos = s->pos; | |
| 168 | 199634 | ff_fetch_timestamp(s, 0, 0, 0); | |
| 169 | } | ||
| 170 | /* WARNING: the returned index can be negative */ | ||
| 171 | 831731 | 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 831731 times.
|
831731 | 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 629794 times.
✓ Branch 1 taken 201937 times.
|
831731 | if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 176 |
4/4✓ Branch 0 taken 237651 times.
✓ Branch 1 taken 392143 times.
✓ Branch 2 taken 486 times.
✓ Branch 3 taken 237165 times.
|
629794 | FILL(field_order); |
| 177 |
4/4✓ Branch 0 taken 333858 times.
✓ Branch 1 taken 295936 times.
✓ Branch 2 taken 872 times.
✓ Branch 3 taken 332986 times.
|
629794 | FILL(coded_width); |
| 178 |
4/4✓ Branch 0 taken 333858 times.
✓ Branch 1 taken 295936 times.
✓ Branch 2 taken 872 times.
✓ Branch 3 taken 332986 times.
|
629794 | FILL(coded_height); |
| 179 |
4/4✓ Branch 0 taken 336995 times.
✓ Branch 1 taken 292799 times.
✓ Branch 2 taken 539 times.
✓ Branch 3 taken 336456 times.
|
629794 | FILL(width); |
| 180 |
4/4✓ Branch 0 taken 336995 times.
✓ Branch 1 taken 292799 times.
✓ Branch 2 taken 539 times.
✓ Branch 3 taken 336456 times.
|
629794 | FILL(height); |
| 181 | } | ||
| 182 | |||
| 183 | /* update the file pointer */ | ||
| 184 |
2/2✓ Branch 0 taken 197027 times.
✓ Branch 1 taken 634704 times.
|
831731 | if (*poutbuf_size) { |
| 185 | /* fill the data for the current frame */ | ||
| 186 | 197027 | s->frame_offset = s->next_frame_offset; | |
| 187 | |||
| 188 | /* offset of the next frame */ | ||
| 189 | 197027 | s->next_frame_offset = s->cur_offset + index; | |
| 190 | 197027 | s->fetch_timestamp = 1; | |
| 191 | } else { | ||
| 192 | /* Don't return a pointer to dummy_buf. */ | ||
| 193 | 634704 | *poutbuf = NULL; | |
| 194 | } | ||
| 195 |
2/2✓ Branch 0 taken 4865 times.
✓ Branch 1 taken 826866 times.
|
831731 | if (index < 0) |
| 196 | 4865 | index = 0; | |
| 197 | 831731 | s->cur_offset += index; | |
| 198 | 831731 | return index; | |
| 199 | } | ||
| 200 | |||
| 201 | 21164 | av_cold void av_parser_close(AVCodecParserContext *s) | |
| 202 | { | ||
| 203 |
2/2✓ Branch 0 taken 7294 times.
✓ Branch 1 taken 13870 times.
|
21164 | if (s) { |
| 204 |
2/2✓ Branch 1 taken 6911 times.
✓ Branch 2 taken 383 times.
|
7294 | if (ffcodecparser(s->parser)->close) |
| 205 | 6911 | ffcodecparser(s->parser)->close(s); | |
| 206 | 7294 | av_freep(&s->priv_data); | |
| 207 | 7294 | av_free(s); | |
| 208 | } | ||
| 209 | 21164 | } | |
| 210 | |||
| 211 | 769070 | int ff_combine_frame(ParseContext *pc, int next, | |
| 212 | const uint8_t **buf, int *buf_size) | ||
| 213 | { | ||
| 214 | 769070 | 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 1378 times.
✓ Branch 1 taken 769070 times.
|
770448 | for (; pc->overread > 0; pc->overread--) |
| 223 | 1378 | pc->buffer[pc->index++] = pc->buffer[pc->overread_index++]; | |
| 224 | |||
| 225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 769070 times.
|
769070 | if (next > *buf_size) |
| 226 | ✗ | return AVERROR(EINVAL); | |
| 227 | |||
| 228 | /* flush remaining if EOF */ | ||
| 229 |
4/4✓ Branch 0 taken 2175 times.
✓ Branch 1 taken 766895 times.
✓ Branch 2 taken 1724 times.
✓ Branch 3 taken 451 times.
|
769070 | if (!*buf_size && next == END_NOT_FOUND) |
| 230 | 1724 | next = 0; | |
| 231 | |||
| 232 | 769070 | pc->last_index = pc->index; | |
| 233 | |||
| 234 | /* copy into buffer end return */ | ||
| 235 |
2/2✓ Branch 0 taken 624435 times.
✓ Branch 1 taken 144635 times.
|
769070 | if (next == END_NOT_FOUND) { |
| 236 | 624435 | void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, | |
| 237 | 624435 | *buf_size + pc->index + | |
| 238 | AV_INPUT_BUFFER_PADDING_SIZE); | ||
| 239 | |||
| 240 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 624435 times.
|
624435 | 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 | 624435 | pc->buffer = new_buffer; | |
| 246 | 624435 | memcpy(&pc->buffer[pc->index], *buf, *buf_size); | |
| 247 | 624435 | memset(&pc->buffer[pc->index + *buf_size], 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 248 | 624435 | pc->index += *buf_size; | |
| 249 | 624435 | return -1; | |
| 250 | } | ||
| 251 | |||
| 252 |
3/4✓ Branch 0 taken 275 times.
✓ Branch 1 taken 144360 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 275 times.
|
144635 | av_assert0(next >= 0 || pc->buffer); |
| 253 | |||
| 254 | 144635 | *buf_size = | |
| 255 | 144635 | pc->overread_index = pc->index + next; | |
| 256 | |||
| 257 | /* append to buffer */ | ||
| 258 |
2/2✓ Branch 0 taken 60934 times.
✓ Branch 1 taken 83701 times.
|
144635 | if (pc->index) { |
| 259 | 60934 | void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, | |
| 260 | 60934 | next + pc->index + | |
| 261 | AV_INPUT_BUFFER_PADDING_SIZE); | ||
| 262 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60934 times.
|
60934 | 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 | 60934 | pc->buffer = new_buffer; | |
| 270 |
2/2✓ Branch 0 taken 60929 times.
✓ Branch 1 taken 5 times.
|
60934 | if (next > -AV_INPUT_BUFFER_PADDING_SIZE) |
| 271 | 60929 | memcpy(&pc->buffer[pc->index], *buf, | |
| 272 | 60929 | next + AV_INPUT_BUFFER_PADDING_SIZE); | |
| 273 | 60934 | pc->index = 0; | |
| 274 | 60934 | *buf = pc->buffer; | |
| 275 | } | ||
| 276 | |||
| 277 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 144597 times.
|
144635 | if (next < -8) { |
| 278 | 38 | pc->overread += -8 - next; | |
| 279 | 38 | next = -8; | |
| 280 | } | ||
| 281 | /* store overread bytes */ | ||
| 282 |
2/2✓ Branch 0 taken 983 times.
✓ Branch 1 taken 144635 times.
|
145618 | for (; next < 0; next++) { |
| 283 | 983 | pc->state = pc->state << 8 | pc->buffer[pc->last_index + next]; | |
| 284 | 983 | pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next]; | |
| 285 | 983 | pc->overread++; | |
| 286 | } | ||
| 287 | |||
| 288 | 144635 | 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 | 144635 | return 0; | |
| 296 | } | ||
| 297 | |||
| 298 | 5587 | av_cold void ff_parse_close(AVCodecParserContext *s) | |
| 299 | { | ||
| 300 | 5587 | ParseContext *pc = s->priv_data; | |
| 301 | |||
| 302 | 5587 | av_freep(&pc->buffer); | |
| 303 | 5587 | } | |
| 304 |