2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 static int FUNC(frame_sync_code)(CodedBitstreamContext *ctx, RWContext *rw,
20 VP9RawFrameHeader *current)
22 uint8_t frame_sync_byte_0 = VP9_FRAME_SYNC_0;
23 uint8_t frame_sync_byte_1 = VP9_FRAME_SYNC_1;
24 uint8_t frame_sync_byte_2 = VP9_FRAME_SYNC_2;
27 xf(8, frame_sync_byte_0, frame_sync_byte_0, 0);
28 xf(8, frame_sync_byte_1, frame_sync_byte_1, 0);
29 xf(8, frame_sync_byte_2, frame_sync_byte_2, 0);
31 if (frame_sync_byte_0 != VP9_FRAME_SYNC_0 ||
32 frame_sync_byte_1 != VP9_FRAME_SYNC_1 ||
33 frame_sync_byte_2 != VP9_FRAME_SYNC_2) {
34 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid frame sync code: "
35 "%02x %02x %02x.\n", frame_sync_byte_0,
36 frame_sync_byte_1, frame_sync_byte_2);
37 return AVERROR_INVALIDDATA;
43 static int FUNC(color_config)(CodedBitstreamContext *ctx, RWContext *rw,
44 VP9RawFrameHeader *current, int profile)
49 f(1, ten_or_twelve_bit);
53 if (current->color_space != VP9_CS_RGB) {
55 if (profile == 1 || profile == 3) {
58 f(1, color_config_reserved_zero);
60 infer(subsampling_x, 1);
61 infer(subsampling_y, 1);
64 infer(color_range, 1);
65 if (profile == 1 || profile == 3) {
66 infer(subsampling_x, 0);
67 infer(subsampling_y, 0);
74 static int FUNC(frame_size)(CodedBitstreamContext *ctx, RWContext *rw,
75 VP9RawFrameHeader *current)
77 CodedBitstreamVP9Context *vp9 = ctx->priv_data;
80 f(16, frame_width_minus_1);
81 f(16, frame_height_minus_1);
83 vp9->mi_cols = (current->frame_width_minus_1 + 8) >> 3;
84 vp9->mi_rows = (current->frame_height_minus_1 + 8) >> 3;
85 vp9->sb64_cols = (vp9->mi_cols + 7) >> 3;
86 vp9->sb64_rows = (vp9->mi_rows + 7) >> 3;
91 static int FUNC(render_size)(CodedBitstreamContext *ctx, RWContext *rw,
92 VP9RawFrameHeader *current)
96 f(1, render_and_frame_size_different);
98 if (current->render_and_frame_size_different) {
99 f(16, render_width_minus_1);
100 f(16, render_height_minus_1);
106 static int FUNC(frame_size_with_refs)(CodedBitstreamContext *ctx, RWContext *rw,
107 VP9RawFrameHeader *current)
111 for (i = 0; i < VP9_REFS_PER_FRAME; i++) {
112 fs(1, found_ref[i], 1, i);
113 if (current->found_ref[i])
116 if (i >= VP9_REFS_PER_FRAME)
117 CHECK(FUNC(frame_size)(ctx, rw, current));
118 CHECK(FUNC(render_size)(ctx, rw, current));
123 static int FUNC(interpolation_filter)(CodedBitstreamContext *ctx, RWContext *rw,
124 VP9RawFrameHeader *current)
128 f(1, is_filter_switchable);
129 if (!current->is_filter_switchable)
130 f(2, raw_interpolation_filter_type);
135 static int FUNC(loop_filter_params)(CodedBitstreamContext *ctx, RWContext *rw,
136 VP9RawFrameHeader *current)
140 f(6, loop_filter_level);
141 f(3, loop_filter_sharpness);
143 f(1, loop_filter_delta_enabled);
144 if (current->loop_filter_delta_enabled) {
145 f(1, loop_filter_delta_update);
146 if (current->loop_filter_delta_update) {
147 for (i = 0; i < VP9_MAX_REF_FRAMES; i++) {
148 fs(1, update_ref_delta[i], 1, i);
149 if (current->update_ref_delta[i])
150 ss(6, loop_filter_ref_deltas[i], 1, i);
152 for (i = 0; i < 2; i++) {
153 fs(1, update_mode_delta[i], 1, i);
154 if (current->update_mode_delta[i])
155 ss(6, loop_filter_mode_deltas[i], 1, i);
163 static int FUNC(quantization_params)(CodedBitstreamContext *ctx, RWContext *rw,
164 VP9RawFrameHeader *current)
170 delta_q(delta_q_y_dc);
171 delta_q(delta_q_uv_dc);
172 delta_q(delta_q_uv_ac);
177 static int FUNC(segmentation_params)(CodedBitstreamContext *ctx, RWContext *rw,
178 VP9RawFrameHeader *current)
180 static const int segmentation_feature_bits[VP9_SEG_LVL_MAX] = { 8, 6, 2, 0 };
181 static const int segmentation_feature_signed[VP9_SEG_LVL_MAX] = { 1, 1, 0, 0 };
185 f(1, segmentation_enabled);
187 if (current->segmentation_enabled) {
188 f(1, segmentation_update_map);
189 if (current->segmentation_update_map) {
190 for (i = 0; i < 7; i++)
191 prob(segmentation_tree_probs[i], 1, i);
192 f(1, segmentation_temporal_update);
193 for (i = 0; i < 3; i++) {
194 if (current->segmentation_temporal_update)
195 prob(segmentation_pred_prob[i], 1, i);
197 infer(segmentation_pred_prob[i], 255);
201 f(1, segmentation_update_data);
202 if (current->segmentation_update_data) {
203 f(1, segmentation_abs_or_delta_update);
204 for (i = 0; i < VP9_MAX_SEGMENTS; i++) {
205 for (j = 0; j < VP9_SEG_LVL_MAX; j++) {
206 fs(1, feature_enabled[i][j], 2, i, j);
207 if (current->feature_enabled[i][j] &&
208 segmentation_feature_bits[j]) {
209 fs(segmentation_feature_bits[j],
210 feature_value[i][j], 2, i, j);
211 if (segmentation_feature_signed[j])
212 fs(1, feature_sign[i][j], 2, i, j);
214 infer(feature_sign[i][j], 0);
216 infer(feature_value[i][j], 0);
217 infer(feature_sign[i][j], 0);
227 static int FUNC(tile_info)(CodedBitstreamContext *ctx, RWContext *rw,
228 VP9RawFrameHeader *current)
230 CodedBitstreamVP9Context *vp9 = ctx->priv_data;
231 int min_log2_tile_cols, max_log2_tile_cols;
234 min_log2_tile_cols = 0;
235 while ((VP9_MAX_TILE_WIDTH_B64 << min_log2_tile_cols) < vp9->sb64_cols)
236 ++min_log2_tile_cols;
237 max_log2_tile_cols = 0;
238 while ((vp9->sb64_cols >> (max_log2_tile_cols + 1)) >= VP9_MIN_TILE_WIDTH_B64)
239 ++max_log2_tile_cols;
241 increment(tile_cols_log2, min_log2_tile_cols, max_log2_tile_cols);
243 increment(tile_rows_log2, 0, 2);
248 static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
249 VP9RawFrameHeader *current)
256 f(1, profile_low_bit);
257 f(1, profile_high_bit);
258 profile = (current->profile_high_bit << 1) + current->profile_low_bit;
260 f(1, profile_reserved_zero);
262 f(1, show_existing_frame);
263 if (current->show_existing_frame) {
264 f(3, frame_to_show_map_idx);
265 infer(header_size_in_bytes, 0);
266 infer(refresh_frame_flags, 0x00);
267 infer(loop_filter_level, 0);
273 f(1, error_resilient_mode);
275 if (current->frame_type == VP9_KEY_FRAME) {
276 CHECK(FUNC(frame_sync_code)(ctx, rw, current));
277 CHECK(FUNC(color_config)(ctx, rw, current, profile));
278 CHECK(FUNC(frame_size)(ctx, rw, current));
279 CHECK(FUNC(render_size)(ctx, rw, current));
281 infer(refresh_frame_flags, 0xff);
284 if (current->show_frame == 0)
287 infer(intra_only, 0);
289 if (current->error_resilient_mode == 0)
290 f(2, reset_frame_context);
292 infer(reset_frame_context, 0);
294 if (current->intra_only == 1) {
295 CHECK(FUNC(frame_sync_code)(ctx, rw, current));
298 CHECK(FUNC(color_config)(ctx, rw, current, profile));
300 infer(color_space, 1);
301 infer(subsampling_x, 1);
302 infer(subsampling_y, 1);
305 f(8, refresh_frame_flags);
307 CHECK(FUNC(frame_size)(ctx, rw, current));
308 CHECK(FUNC(render_size)(ctx, rw, current));
310 f(8, refresh_frame_flags);
312 for (i = 0; i < VP9_REFS_PER_FRAME; i++) {
313 fs(3, ref_frame_idx[i], 1, i);
314 fs(1, ref_frame_sign_bias[VP9_LAST_FRAME + i],
315 1, VP9_LAST_FRAME + i);
318 CHECK(FUNC(frame_size_with_refs)(ctx, rw, current));
319 f(1, allow_high_precision_mv);
320 CHECK(FUNC(interpolation_filter)(ctx, rw, current));
324 if (current->error_resilient_mode == 0) {
325 f(1, refresh_frame_context);
326 f(1, frame_parallel_decoding_mode);
328 infer(refresh_frame_context, 0);
329 infer(frame_parallel_decoding_mode, 1);
332 f(2, frame_context_idx);
334 CHECK(FUNC(loop_filter_params)(ctx, rw, current));
335 CHECK(FUNC(quantization_params)(ctx, rw, current));
336 CHECK(FUNC(segmentation_params)(ctx, rw, current));
337 CHECK(FUNC(tile_info)(ctx, rw, current));
339 f(16, header_size_in_bytes);
344 static int FUNC(trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
347 av_unused int zero = 0;
348 while (byte_alignment(rw) != 0)
349 xf(1, zero_bit, zero, 0);
354 static int FUNC(frame)(CodedBitstreamContext *ctx, RWContext *rw,
355 VP9RawFrame *current)
361 CHECK(FUNC(uncompressed_header)(ctx, rw, ¤t->header));
363 CHECK(FUNC(trailing_bits)(ctx, rw));
368 static int FUNC(superframe_index)(CodedBitstreamContext *ctx, RWContext *rw,
369 VP9RawSuperframeIndex *current)
373 HEADER("Superframe Index");
375 f(3, superframe_marker);
376 f(2, bytes_per_framesize_minus_1);
377 f(3, frames_in_superframe_minus_1);
379 for (i = 0; i <= current->frames_in_superframe_minus_1; i++) {
380 // Surprise little-endian!
381 fle(8 * (current->bytes_per_framesize_minus_1 + 1),
382 frame_sizes[i], 1, i);
385 f(3, superframe_marker);
386 f(2, bytes_per_framesize_minus_1);
387 f(3, frames_in_superframe_minus_1);