FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/avs2_parser.c
Date: 2025-11-01 23:31:58
Exec Total Coverage
Lines: 0 91 0.0%
Functions: 0 4 0.0%
Branches: 0 50 0.0%

Line Branch Exec Source
1 /*
2 * AVS2-P2/IEEE1857.4 video parser.
3 * Copyright (c) 2018 Huiwen Ren <hwrenx@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/avutil.h"
23 #include "avs2.h"
24 #include "get_bits.h"
25 #include "parser.h"
26 #include "parser_internal.h"
27
28 static int avs2_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
29 {
30 int pic_found = pc->frame_start_found;
31 uint32_t state = pc->state;
32 int cur = 0;
33
34 if (!pic_found) {
35 for (; cur < buf_size; ++cur) {
36 state = (state << 8) | buf[cur];
37 if ((state & 0xFFFFFF00) == 0x100 && AVS2_ISPIC(buf[cur])) {
38 cur++;
39 pic_found = 1;
40 break;
41 }
42 }
43 }
44
45 if (pic_found) {
46 if (!buf_size)
47 return END_NOT_FOUND;
48 for (; cur < buf_size; cur++) {
49 state = (state << 8) | buf[cur];
50 if ((state & 0xFFFFFF00) == 0x100 && AVS2_ISUNIT(buf[cur])) {
51 pc->frame_start_found = 0;
52 pc->state = -1;
53 return cur - 3;
54 }
55 }
56 }
57
58 pc->frame_start_found = pic_found;
59 pc->state = state;
60
61 return END_NOT_FOUND;
62 }
63
64 static void parse_avs2_seq_header(AVCodecParserContext *s, const uint8_t *buf,
65 int buf_size, AVCodecContext *avctx)
66 {
67 GetBitContext gb;
68 int profile, level;
69 int width, height;
70 int chroma, sample_precision, encoding_precision = 1;
71 // sample_precision and encoding_precision is 3 bits
72 static const uint8_t precision[8] = { 0, 8, 10 };
73 unsigned aspect_ratio;
74 unsigned frame_rate_code;
75 int low_delay;
76 av_unused int ret;
77 // update buf_size_min if parse more deeper
78 const int buf_size_min = 15;
79
80 if (buf_size < buf_size_min)
81 return;
82
83 ret = init_get_bits8(&gb, buf, buf_size_min);
84 av_assert1(ret >= 0);
85
86 s->key_frame = 1;
87 s->pict_type = AV_PICTURE_TYPE_I;
88
89 profile = get_bits(&gb, 8);
90 level = get_bits(&gb, 8);
91
92 // progressive_sequence u(1)
93 // field_coded_sequence u(1)
94 skip_bits(&gb, 2);
95
96 width = get_bits(&gb, 14);
97 height = get_bits(&gb, 14);
98
99 chroma = get_bits(&gb, 2);
100 sample_precision = get_bits(&gb, 3);
101 if (profile == AVS2_PROFILE_MAIN10)
102 encoding_precision = get_bits(&gb, 3);
103
104 aspect_ratio = get_bits(&gb, 4);
105 frame_rate_code = get_bits(&gb, 4);
106
107 // bit_rate_lower u(18)
108 // marker_bit f(1)
109 // bit_rate_upper u(12)
110 skip_bits(&gb, 31);
111
112 low_delay = get_bits(&gb, 1);
113
114 s->width = width;
115 s->height = height;
116 s->coded_width = FFALIGN(width, 8);
117 s->coded_height = FFALIGN(height, 8);
118 avctx->framerate.num =
119 ff_avs2_frame_rate_tab[frame_rate_code].num;
120 avctx->framerate.den =
121 ff_avs2_frame_rate_tab[frame_rate_code].den;
122 avctx->has_b_frames = FFMAX(avctx->has_b_frames, !low_delay);
123
124 av_log(avctx, AV_LOG_DEBUG,
125 "AVS2 parse seq HDR: profile %x, level %x, "
126 "width %d, height %d, "
127 "chroma %d, sample_precision %d bits, encoding_precision %d bits, "
128 "aspect_ratio 0x%x, framerate %d/%d, low_delay %d\n",
129 profile, level,
130 width, height,
131 chroma, precision[sample_precision], precision[encoding_precision],
132 aspect_ratio, avctx->framerate.num, avctx->framerate.den, low_delay);
133 }
134
135 static void parse_avs2_units(AVCodecParserContext *s, const uint8_t *buf,
136 int buf_size, AVCodecContext *avctx)
137 {
138 if (buf_size < 5)
139 return;
140
141 if (!(buf[0] == 0x0 && buf[1] == 0x0 && buf[2] == 0x1))
142 return;
143
144 switch (buf[3]) {
145 case AVS2_SEQ_START_CODE:
146 parse_avs2_seq_header(s, buf + 4, buf_size - 4, avctx);
147 return;
148 case AVS2_INTRA_PIC_START_CODE:
149 s->key_frame = 1;
150 s->pict_type = AV_PICTURE_TYPE_I;
151 return;
152 case AVS2_INTER_PIC_START_CODE:
153 s->key_frame = 0;
154 if (buf_size > 9) {
155 int pic_code_type = buf[8] & 0x3;
156 if (pic_code_type == 1)
157 s->pict_type = AV_PICTURE_TYPE_P;
158 else if (pic_code_type == 3)
159 s->pict_type = AV_PICTURE_TYPE_S;
160 else
161 s->pict_type = AV_PICTURE_TYPE_B;
162 }
163 return;
164 }
165 }
166
167 static int avs2_parse(AVCodecParserContext *s, AVCodecContext *avctx,
168 const uint8_t **poutbuf, int *poutbuf_size,
169 const uint8_t *buf, int buf_size)
170 {
171 ParseContext *pc = s->priv_data;
172 int next;
173
174 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
175 next = buf_size;
176 } else {
177 next = avs2_find_frame_end(pc, buf, buf_size);
178 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
179 *poutbuf = NULL;
180 *poutbuf_size = 0;
181 return buf_size;
182 }
183 }
184
185 parse_avs2_units(s, buf, buf_size, avctx);
186
187 *poutbuf = buf;
188 *poutbuf_size = buf_size;
189
190 return next;
191 }
192
193 const FFCodecParser ff_avs2_parser = {
194 PARSER_CODEC_LIST(AV_CODEC_ID_AVS2),
195 .priv_data_size = sizeof(ParseContext),
196 .parse = avs2_parse,
197 .close = ff_parse_close,
198 };
199