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 |
|
|
|
27 |
|
✗ |
static int avs2_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) |
28 |
|
|
{ |
29 |
|
✗ |
int pic_found = pc->frame_start_found; |
30 |
|
✗ |
uint32_t state = pc->state; |
31 |
|
✗ |
int cur = 0; |
32 |
|
|
|
33 |
|
✗ |
if (!pic_found) { |
34 |
|
✗ |
for (; cur < buf_size; ++cur) { |
35 |
|
✗ |
state = (state << 8) | buf[cur]; |
36 |
|
✗ |
if ((state & 0xFFFFFF00) == 0x100 && AVS2_ISPIC(buf[cur])) { |
37 |
|
✗ |
cur++; |
38 |
|
✗ |
pic_found = 1; |
39 |
|
✗ |
break; |
40 |
|
|
} |
41 |
|
|
} |
42 |
|
|
} |
43 |
|
|
|
44 |
|
✗ |
if (pic_found) { |
45 |
|
✗ |
if (!buf_size) |
46 |
|
✗ |
return END_NOT_FOUND; |
47 |
|
✗ |
for (; cur < buf_size; cur++) { |
48 |
|
✗ |
state = (state << 8) | buf[cur]; |
49 |
|
✗ |
if ((state & 0xFFFFFF00) == 0x100 && AVS2_ISUNIT(buf[cur])) { |
50 |
|
✗ |
pc->frame_start_found = 0; |
51 |
|
✗ |
pc->state = -1; |
52 |
|
✗ |
return cur - 3; |
53 |
|
|
} |
54 |
|
|
} |
55 |
|
|
} |
56 |
|
|
|
57 |
|
✗ |
pc->frame_start_found = pic_found; |
58 |
|
✗ |
pc->state = state; |
59 |
|
|
|
60 |
|
✗ |
return END_NOT_FOUND; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
✗ |
static void parse_avs2_seq_header(AVCodecParserContext *s, const uint8_t *buf, |
64 |
|
|
int buf_size, AVCodecContext *avctx) |
65 |
|
|
{ |
66 |
|
|
GetBitContext gb; |
67 |
|
|
int profile, level; |
68 |
|
|
int width, height; |
69 |
|
✗ |
int chroma, sample_precision, encoding_precision = 1; |
70 |
|
|
// sample_precision and encoding_precision is 3 bits |
71 |
|
|
static const uint8_t precision[8] = { 0, 8, 10 }; |
72 |
|
|
unsigned aspect_ratio; |
73 |
|
|
unsigned frame_rate_code; |
74 |
|
|
int low_delay; |
75 |
|
|
av_unused int ret; |
76 |
|
|
// update buf_size_min if parse more deeper |
77 |
|
✗ |
const int buf_size_min = 15; |
78 |
|
|
|
79 |
|
✗ |
if (buf_size < buf_size_min) |
80 |
|
✗ |
return; |
81 |
|
|
|
82 |
|
✗ |
ret = init_get_bits8(&gb, buf, buf_size_min); |
83 |
|
|
av_assert1(ret >= 0); |
84 |
|
|
|
85 |
|
✗ |
s->key_frame = 1; |
86 |
|
✗ |
s->pict_type = AV_PICTURE_TYPE_I; |
87 |
|
|
|
88 |
|
✗ |
profile = get_bits(&gb, 8); |
89 |
|
✗ |
level = get_bits(&gb, 8); |
90 |
|
|
|
91 |
|
|
// progressive_sequence u(1) |
92 |
|
|
// field_coded_sequence u(1) |
93 |
|
✗ |
skip_bits(&gb, 2); |
94 |
|
|
|
95 |
|
✗ |
width = get_bits(&gb, 14); |
96 |
|
✗ |
height = get_bits(&gb, 14); |
97 |
|
|
|
98 |
|
✗ |
chroma = get_bits(&gb, 2); |
99 |
|
✗ |
sample_precision = get_bits(&gb, 3); |
100 |
|
✗ |
if (profile == AVS2_PROFILE_MAIN10) |
101 |
|
✗ |
encoding_precision = get_bits(&gb, 3); |
102 |
|
|
|
103 |
|
✗ |
aspect_ratio = get_bits(&gb, 4); |
104 |
|
✗ |
frame_rate_code = get_bits(&gb, 4); |
105 |
|
|
|
106 |
|
|
// bit_rate_lower u(18) |
107 |
|
|
// marker_bit f(1) |
108 |
|
|
// bit_rate_upper u(12) |
109 |
|
✗ |
skip_bits(&gb, 31); |
110 |
|
|
|
111 |
|
✗ |
low_delay = get_bits(&gb, 1); |
112 |
|
|
|
113 |
|
✗ |
s->width = width; |
114 |
|
✗ |
s->height = height; |
115 |
|
✗ |
s->coded_width = FFALIGN(width, 8); |
116 |
|
✗ |
s->coded_height = FFALIGN(height, 8); |
117 |
|
✗ |
avctx->framerate.num = |
118 |
|
✗ |
ff_avs2_frame_rate_tab[frame_rate_code].num; |
119 |
|
✗ |
avctx->framerate.den = |
120 |
|
✗ |
ff_avs2_frame_rate_tab[frame_rate_code].den; |
121 |
|
✗ |
avctx->has_b_frames = FFMAX(avctx->has_b_frames, !low_delay); |
122 |
|
|
|
123 |
|
✗ |
av_log(avctx, AV_LOG_DEBUG, |
124 |
|
|
"AVS2 parse seq HDR: profile %x, level %x, " |
125 |
|
|
"width %d, height %d, " |
126 |
|
|
"chroma %d, sample_precision %d bits, encoding_precision %d bits, " |
127 |
|
|
"aspect_ratio 0x%x, framerate %d/%d, low_delay %d\n", |
128 |
|
|
profile, level, |
129 |
|
|
width, height, |
130 |
|
✗ |
chroma, precision[sample_precision], precision[encoding_precision], |
131 |
|
|
aspect_ratio, avctx->framerate.num, avctx->framerate.den, low_delay); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
✗ |
static void parse_avs2_units(AVCodecParserContext *s, const uint8_t *buf, |
135 |
|
|
int buf_size, AVCodecContext *avctx) |
136 |
|
|
{ |
137 |
|
✗ |
if (buf_size < 5) |
138 |
|
✗ |
return; |
139 |
|
|
|
140 |
|
✗ |
if (!(buf[0] == 0x0 && buf[1] == 0x0 && buf[2] == 0x1)) |
141 |
|
✗ |
return; |
142 |
|
|
|
143 |
|
✗ |
switch (buf[3]) { |
144 |
|
✗ |
case AVS2_SEQ_START_CODE: |
145 |
|
✗ |
parse_avs2_seq_header(s, buf + 4, buf_size - 4, avctx); |
146 |
|
✗ |
return; |
147 |
|
✗ |
case AVS2_INTRA_PIC_START_CODE: |
148 |
|
✗ |
s->key_frame = 1; |
149 |
|
✗ |
s->pict_type = AV_PICTURE_TYPE_I; |
150 |
|
✗ |
return; |
151 |
|
✗ |
case AVS2_INTER_PIC_START_CODE: |
152 |
|
✗ |
s->key_frame = 0; |
153 |
|
✗ |
if (buf_size > 9) { |
154 |
|
✗ |
int pic_code_type = buf[8] & 0x3; |
155 |
|
✗ |
if (pic_code_type == 1) |
156 |
|
✗ |
s->pict_type = AV_PICTURE_TYPE_P; |
157 |
|
✗ |
else if (pic_code_type == 3) |
158 |
|
✗ |
s->pict_type = AV_PICTURE_TYPE_S; |
159 |
|
|
else |
160 |
|
✗ |
s->pict_type = AV_PICTURE_TYPE_B; |
161 |
|
|
} |
162 |
|
✗ |
return; |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
|
166 |
|
✗ |
static int avs2_parse(AVCodecParserContext *s, AVCodecContext *avctx, |
167 |
|
|
const uint8_t **poutbuf, int *poutbuf_size, |
168 |
|
|
const uint8_t *buf, int buf_size) |
169 |
|
|
{ |
170 |
|
✗ |
ParseContext *pc = s->priv_data; |
171 |
|
|
int next; |
172 |
|
|
|
173 |
|
✗ |
if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
174 |
|
✗ |
next = buf_size; |
175 |
|
|
} else { |
176 |
|
✗ |
next = avs2_find_frame_end(pc, buf, buf_size); |
177 |
|
✗ |
if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
178 |
|
✗ |
*poutbuf = NULL; |
179 |
|
✗ |
*poutbuf_size = 0; |
180 |
|
✗ |
return buf_size; |
181 |
|
|
} |
182 |
|
|
} |
183 |
|
|
|
184 |
|
✗ |
parse_avs2_units(s, buf, buf_size, avctx); |
185 |
|
|
|
186 |
|
✗ |
*poutbuf = buf; |
187 |
|
✗ |
*poutbuf_size = buf_size; |
188 |
|
|
|
189 |
|
✗ |
return next; |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
const AVCodecParser ff_avs2_parser = { |
193 |
|
|
.codec_ids = { AV_CODEC_ID_AVS2 }, |
194 |
|
|
.priv_data_size = sizeof(ParseContext), |
195 |
|
|
.parser_parse = avs2_parse, |
196 |
|
|
.parser_close = ff_parse_close, |
197 |
|
|
}; |
198 |
|
|
|