FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4video_parser.c
Date: 2021-09-22 05:58:22
Exec Total Coverage
Lines: 67 70 95.7%
Branches: 38 46 82.6%

Line Branch Exec Source
1 /*
2 * MPEG-4 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 #define UNCHECKED_BITSTREAM_READER 1
24
25 #include "internal.h"
26 #include "parser.h"
27 #include "mpegvideo.h"
28 #include "mpeg4video.h"
29 #if FF_API_FLAG_TRUNCATED
30 /* Nuke this header when removing FF_API_FLAG_TRUNCATED */
31 #include "mpeg4video_parser.h"
32 #endif
33
34 struct Mp4vParseContext {
35 ParseContext pc;
36 Mpeg4DecContext dec_ctx;
37 int first_picture;
38 };
39
40 #if FF_API_FLAG_TRUNCATED
41 8238 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
42 #else
43 /**
44 * Find the end of the current frame in the bitstream.
45 * @return the position of the first byte of the next frame, or -1
46 */
47 static int mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
48 #endif
49 {
50 int vop_found, i;
51 uint32_t state;
52
53 8238 vop_found = pc->frame_start_found;
54 8238 state = pc->state;
55
56 8238 i = 0;
57
2/2
✓ Branch 0 taken 1087 times.
✓ Branch 1 taken 7151 times.
8238 if (!vop_found) {
58
2/2
✓ Branch 0 taken 8436 times.
✓ Branch 1 taken 2 times.
8438 for (i = 0; i < buf_size; i++) {
59 8436 state = (state << 8) | buf[i];
60
2/2
✓ Branch 0 taken 1085 times.
✓ Branch 1 taken 7351 times.
8436 if (state == VOP_STARTCODE) {
61 1085 i++;
62 1085 vop_found = 1;
63 1085 break;
64 }
65 }
66 }
67
68
2/2
✓ Branch 0 taken 8236 times.
✓ Branch 1 taken 2 times.
8238 if (vop_found) {
69 /* EOF considered as end of frame */
70
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8212 times.
8236 if (buf_size == 0)
71 24 return 0;
72
2/2
✓ Branch 0 taken 8509069 times.
✓ Branch 1 taken 7139 times.
8516208 for (; i < buf_size; i++) {
73 8509069 state = (state << 8) | buf[i];
74
2/2
✓ Branch 0 taken 1073 times.
✓ Branch 1 taken 8507996 times.
8509069 if ((state & 0xFFFFFF00) == 0x100) {
75
2/4
✓ Branch 0 taken 1073 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1073 times.
1073 if (state == SLICE_STARTCODE || state == EXT_STARTCODE)
76 continue;
77 1073 pc->frame_start_found = 0;
78 1073 pc->state = -1;
79 1073 return i - 3;
80 }
81 }
82 }
83 7141 pc->frame_start_found = vop_found;
84 7141 pc->state = state;
85 7141 return END_NOT_FOUND;
86 }
87
88 /* XXX: make it use less memory */
89 3567 static int mpeg4_decode_header(AVCodecParserContext *s1, AVCodecContext *avctx,
90 const uint8_t *buf, int buf_size)
91 {
92 3567 struct Mp4vParseContext *pc = s1->priv_data;
93 3567 Mpeg4DecContext *dec_ctx = &pc->dec_ctx;
94 3567 MpegEncContext *s = &dec_ctx->m;
95 3567 GetBitContext gb1, *gb = &gb1;
96 int ret;
97
98 3567 s->avctx = avctx;
99 3567 s->current_picture_ptr = &s->current_picture;
100
101
4/4
✓ Branch 0 taken 3450 times.
✓ Branch 1 taken 117 times.
✓ Branch 2 taken 241 times.
✓ Branch 3 taken 3209 times.
3567 if (avctx->extradata_size && pc->first_picture) {
102 241 init_get_bits(gb, avctx->extradata, avctx->extradata_size * 8);
103 241 ret = ff_mpeg4_decode_picture_header(dec_ctx, gb, 1);
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 241 times.
241 if (ret < 0)
105 av_log(avctx, AV_LOG_WARNING, "Failed to parse extradata\n");
106 }
107
108 3567 init_get_bits(gb, buf, 8 * buf_size);
109 3567 ret = ff_mpeg4_decode_picture_header(dec_ctx, gb, 0);
110
5/6
✓ Branch 0 taken 3515 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 3503 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 3503 times.
✗ Branch 5 not taken.
3567 if (s->width && (!avctx->width || !avctx->height ||
111
2/4
✓ Branch 0 taken 3503 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3503 times.
3503 !avctx->coded_width || !avctx->coded_height)) {
112 12 ret = ff_set_dimensions(avctx, s->width, s->height);
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ret < 0)
114 return ret;
115 }
116
5/6
✓ Branch 0 taken 883 times.
✓ Branch 1 taken 2684 times.
✓ Branch 2 taken 883 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 874 times.
✓ Branch 5 taken 9 times.
3567 if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->time_base.den>0 && ret>=0){
117 av_assert1(s1->pts == AV_NOPTS_VALUE);
118 av_assert1(s1->dts == AV_NOPTS_VALUE);
119
120 874 s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->time_base.den}, (AVRational){1, 1200000});
121 }
122
123 3567 s1->pict_type = s->pict_type;
124 3567 pc->first_picture = 0;
125 3567 return ret;
126 }
127
128 329 static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
129 {
130 329 struct Mp4vParseContext *pc = s->priv_data;
131
132 329 ff_mpeg4videodec_static_init();
133
134 329 pc->first_picture = 1;
135 329 pc->dec_ctx.m.quant_precision = 5;
136 329 pc->dec_ctx.m.slice_context_count = 1;
137 329 pc->dec_ctx.showed_packed_warning = 1;
138 329 return 0;
139 }
140
141 10708 static int mpeg4video_parse(AVCodecParserContext *s,
142 AVCodecContext *avctx,
143 const uint8_t **poutbuf, int *poutbuf_size,
144 const uint8_t *buf, int buf_size)
145 {
146 10708 ParseContext *pc = s->priv_data;
147 int next;
148
149
2/2
✓ Branch 0 taken 2470 times.
✓ Branch 1 taken 8238 times.
10708 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
150 2470 next = buf_size;
151 } else {
152 #if FF_API_FLAG_TRUNCATED
153 8238 next = ff_mpeg4_find_frame_end(pc, buf, buf_size);
154 #else
155 next = mpeg4_find_frame_end(pc, buf, buf_size);
156 #endif
157
158
2/2
✓ Branch 1 taken 7141 times.
✓ Branch 2 taken 1097 times.
8238 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
159 7141 *poutbuf = NULL;
160 7141 *poutbuf_size = 0;
161 7141 return buf_size;
162 }
163 }
164 3567 mpeg4_decode_header(s, avctx, buf, buf_size);
165
166 3567 *poutbuf = buf;
167 3567 *poutbuf_size = buf_size;
168 3567 return next;
169 }
170
171 const AVCodecParser ff_mpeg4video_parser = {
172 .codec_ids = { AV_CODEC_ID_MPEG4 },
173 .priv_data_size = sizeof(struct Mp4vParseContext),
174 .parser_init = mpeg4video_parse_init,
175 .parser_parse = mpeg4video_parse,
176 .parser_close = ff_parse_close,
177 };
178