FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4video_parser.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 67 69 97.1%
Branches: 39 46 84.8%

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