FFmpeg
cbs_vp9_syntax_template.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18 
20  VP9RawFrameHeader *current)
21 {
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;
25  int err;
26 
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);
30 
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;
38  }
39 
40  return 0;
41 }
42 
44  VP9RawFrameHeader *current, int profile)
45 {
47  int err;
48 
49  if (profile >= 2) {
50  f(1, ten_or_twelve_bit);
51  vp9->bit_depth = current->ten_or_twelve_bit ? 12 : 10;
52  } else
53  vp9->bit_depth = 8;
54 
55  f(3, color_space);
56 
57  if (current->color_space != VP9_CS_RGB) {
58  f(1, color_range);
59  if (profile == 1 || profile == 3) {
60  f(1, subsampling_x);
61  f(1, subsampling_y);
62  fixed(1, reserved_zero, 0);
63  } else {
64  infer(subsampling_x, 1);
65  infer(subsampling_y, 1);
66  }
67  } else {
68  infer(color_range, 1);
69  if (profile == 1 || profile == 3) {
70  infer(subsampling_x, 0);
71  infer(subsampling_y, 0);
72  fixed(1, reserved_zero, 0);
73  }
74  }
75 
76  vp9->subsampling_x = current->subsampling_x;
77  vp9->subsampling_y = current->subsampling_y;
78 
79  return 0;
80 }
81 
83  VP9RawFrameHeader *current)
84 {
86  int err;
87 
88  f(16, frame_width_minus_1);
89  f(16, frame_height_minus_1);
90 
91  vp9->frame_width = current->frame_width_minus_1 + 1;
92  vp9->frame_height = current->frame_height_minus_1 + 1;
93 
94  vp9->mi_cols = (vp9->frame_width + 7) >> 3;
95  vp9->mi_rows = (vp9->frame_height + 7) >> 3;
96  vp9->sb64_cols = (vp9->mi_cols + 7) >> 3;
97  vp9->sb64_rows = (vp9->mi_rows + 7) >> 3;
98 
99  return 0;
100 }
101 
103  VP9RawFrameHeader *current)
104 {
105  int err;
106 
107  f(1, render_and_frame_size_different);
108 
109  if (current->render_and_frame_size_different) {
110  f(16, render_width_minus_1);
111  f(16, render_height_minus_1);
112  }
113 
114  return 0;
115 }
116 
118  VP9RawFrameHeader *current)
119 {
121  int err, i;
122 
123  for (i = 0; i < VP9_REFS_PER_FRAME; i++) {
124  fs(1, found_ref[i], 1, i);
125  if (current->found_ref[i]) {
127  &vp9->ref[current->ref_frame_idx[i]];
128 
129  vp9->frame_width = ref->frame_width;
130  vp9->frame_height = ref->frame_height;
131 
132  vp9->subsampling_x = ref->subsampling_x;
133  vp9->subsampling_y = ref->subsampling_y;
134  vp9->bit_depth = ref->bit_depth;
135 
136  break;
137  }
138  }
139  if (i >= VP9_REFS_PER_FRAME)
140  CHECK(FUNC(frame_size)(ctx, rw, current));
141  else {
142  vp9->mi_cols = (vp9->frame_width + 7) >> 3;
143  vp9->mi_rows = (vp9->frame_height + 7) >> 3;
144  vp9->sb64_cols = (vp9->mi_cols + 7) >> 3;
145  vp9->sb64_rows = (vp9->mi_rows + 7) >> 3;
146  }
147  CHECK(FUNC(render_size)(ctx, rw, current));
148 
149  return 0;
150 }
151 
153  VP9RawFrameHeader *current)
154 {
155  int err;
156 
157  f(1, is_filter_switchable);
158  if (!current->is_filter_switchable)
159  f(2, raw_interpolation_filter_type);
160 
161  return 0;
162 }
163 
165  VP9RawFrameHeader *current)
166 {
167  int err, i;
168 
169  f(6, loop_filter_level);
170  f(3, loop_filter_sharpness);
171 
172  f(1, loop_filter_delta_enabled);
173  if (current->loop_filter_delta_enabled) {
174  f(1, loop_filter_delta_update);
175  if (current->loop_filter_delta_update) {
176  for (i = 0; i < VP9_MAX_REF_FRAMES; i++) {
177  fs(1, update_ref_delta[i], 1, i);
178  if (current->update_ref_delta[i])
179  ss(6, loop_filter_ref_deltas[i], 1, i);
180  }
181  for (i = 0; i < 2; i++) {
182  fs(1, update_mode_delta[i], 1, i);
183  if (current->update_mode_delta[i])
184  ss(6, loop_filter_mode_deltas[i], 1, i);
185  }
186  }
187  }
188 
189  return 0;
190 }
191 
193  VP9RawFrameHeader *current)
194 {
195  int err;
196 
197  f(8, base_q_idx);
198 
199  delta_q(delta_q_y_dc);
200  delta_q(delta_q_uv_dc);
201  delta_q(delta_q_uv_ac);
202 
203  return 0;
204 }
205 
207  VP9RawFrameHeader *current)
208 {
209  static const uint8_t segmentation_feature_bits[VP9_SEG_LVL_MAX] = { 8, 6, 2, 0 };
210  static const uint8_t segmentation_feature_signed[VP9_SEG_LVL_MAX] = { 1, 1, 0, 0 };
211 
212  int err, i, j;
213 
214  f(1, segmentation_enabled);
215 
216  if (current->segmentation_enabled) {
217  f(1, segmentation_update_map);
218  if (current->segmentation_update_map) {
219  for (i = 0; i < 7; i++)
220  prob(segmentation_tree_probs[i], 1, i);
221  f(1, segmentation_temporal_update);
222  for (i = 0; i < 3; i++) {
223  if (current->segmentation_temporal_update)
224  prob(segmentation_pred_prob[i], 1, i);
225  else
226  infer(segmentation_pred_prob[i], 255);
227  }
228  }
229 
230  f(1, segmentation_update_data);
231  if (current->segmentation_update_data) {
232  f(1, segmentation_abs_or_delta_update);
233  for (i = 0; i < VP9_MAX_SEGMENTS; i++) {
234  for (j = 0; j < VP9_SEG_LVL_MAX; j++) {
235  fs(1, feature_enabled[i][j], 2, i, j);
236  if (current->feature_enabled[i][j] &&
237  segmentation_feature_bits[j]) {
238  fs(segmentation_feature_bits[j],
239  feature_value[i][j], 2, i, j);
240  if (segmentation_feature_signed[j])
241  fs(1, feature_sign[i][j], 2, i, j);
242  else
243  infer(feature_sign[i][j], 0);
244  } else {
245  infer(feature_value[i][j], 0);
246  infer(feature_sign[i][j], 0);
247  }
248  }
249  }
250  }
251  }
252 
253  return 0;
254 }
255 
257  VP9RawFrameHeader *current)
258 {
260  int min_log2_tile_cols, max_log2_tile_cols;
261  int err;
262 
263  min_log2_tile_cols = 0;
264  while ((VP9_MAX_TILE_WIDTH_B64 << min_log2_tile_cols) < vp9->sb64_cols)
265  ++min_log2_tile_cols;
266  max_log2_tile_cols = 0;
267  while ((vp9->sb64_cols >> (max_log2_tile_cols + 1)) >= VP9_MIN_TILE_WIDTH_B64)
268  ++max_log2_tile_cols;
269 
270  increment(tile_cols_log2, min_log2_tile_cols, max_log2_tile_cols);
271 
272  increment(tile_rows_log2, 0, 2);
273 
274  return 0;
275 }
276 
278  VP9RawFrameHeader *current)
279 {
281  int err, i;
282 
283  f(2, frame_marker);
284 
285  f(1, profile_low_bit);
286  f(1, profile_high_bit);
287  vp9->profile = (current->profile_high_bit << 1) + current->profile_low_bit;
288  if (vp9->profile == 3)
289  fixed(1, reserved_zero, 0);
290 
291  f(1, show_existing_frame);
292  if (current->show_existing_frame) {
293  f(3, frame_to_show_map_idx);
294  infer(header_size_in_bytes, 0);
295  infer(refresh_frame_flags, 0x00);
296  infer(loop_filter_level, 0);
297  return 0;
298  }
299 
300  f(1, frame_type);
301  f(1, show_frame);
302  f(1, error_resilient_mode);
303 
304  if (current->frame_type == VP9_KEY_FRAME) {
305  CHECK(FUNC(frame_sync_code)(ctx, rw, current));
306  CHECK(FUNC(color_config)(ctx, rw, current, vp9->profile));
307  CHECK(FUNC(frame_size)(ctx, rw, current));
308  CHECK(FUNC(render_size)(ctx, rw, current));
309 
310  infer(refresh_frame_flags, 0xff);
311 
312  } else {
313  if (current->show_frame == 0)
314  f(1, intra_only);
315  else
316  infer(intra_only, 0);
317 
318  if (current->error_resilient_mode == 0)
319  f(2, reset_frame_context);
320  else
321  infer(reset_frame_context, 0);
322 
323  if (current->intra_only == 1) {
324  CHECK(FUNC(frame_sync_code)(ctx, rw, current));
325 
326  if (vp9->profile > 0) {
327  CHECK(FUNC(color_config)(ctx, rw, current, vp9->profile));
328  } else {
329  infer(color_space, 1);
330  infer(subsampling_x, 1);
331  infer(subsampling_y, 1);
332  vp9->bit_depth = 8;
333 
334  vp9->subsampling_x = current->subsampling_x;
335  vp9->subsampling_y = current->subsampling_y;
336  }
337 
338  f(8, refresh_frame_flags);
339 
340  CHECK(FUNC(frame_size)(ctx, rw, current));
341  CHECK(FUNC(render_size)(ctx, rw, current));
342  } else {
343  f(8, refresh_frame_flags);
344 
345  for (i = 0; i < VP9_REFS_PER_FRAME; i++) {
346  fs(3, ref_frame_idx[i], 1, i);
347  fs(1, ref_frame_sign_bias[VP9_LAST_FRAME + i],
348  1, VP9_LAST_FRAME + i);
349  }
350 
351  CHECK(FUNC(frame_size_with_refs)(ctx, rw, current));
352  f(1, allow_high_precision_mv);
353  CHECK(FUNC(interpolation_filter)(ctx, rw, current));
354  }
355  }
356 
357  if (current->error_resilient_mode == 0) {
358  f(1, refresh_frame_context);
359  f(1, frame_parallel_decoding_mode);
360  } else {
361  infer(refresh_frame_context, 0);
362  infer(frame_parallel_decoding_mode, 1);
363  }
364 
365  f(2, frame_context_idx);
366 
367  CHECK(FUNC(loop_filter_params)(ctx, rw, current));
368  CHECK(FUNC(quantization_params)(ctx, rw, current));
369  CHECK(FUNC(segmentation_params)(ctx, rw, current));
370  CHECK(FUNC(tile_info)(ctx, rw, current));
371 
372  f(16, header_size_in_bytes);
373 
374  for (i = 0; i < VP9_NUM_REF_FRAMES; i++) {
375  if (current->refresh_frame_flags & (1 << i)) {
376  vp9->ref[i] = (VP9ReferenceFrameState) {
377  .frame_width = vp9->frame_width,
378  .frame_height = vp9->frame_height,
379  .subsampling_x = vp9->subsampling_x,
380  .subsampling_y = vp9->subsampling_y,
381  .bit_depth = vp9->bit_depth,
382  };
383  }
384  }
385 
386  av_log(ctx->log_ctx, AV_LOG_DEBUG, "Frame: size %dx%d "
387  "subsample %dx%d bit_depth %d tiles %dx%d.\n",
388  vp9->frame_width, vp9->frame_height,
389  vp9->subsampling_x, vp9->subsampling_y,
390  vp9->bit_depth, 1 << current->tile_cols_log2,
391  1 << current->tile_rows_log2);
392 
393  return 0;
394 }
395 
397 {
398  int err;
399  av_unused int zero = 0;
400  while (byte_alignment(rw) != 0)
401  xf(1, zero_bit, zero, 0);
402 
403  return 0;
404 }
405 
407  VP9RawFrame *current)
408 {
409  int err;
410 
411  HEADER("Frame");
412 
413  CHECK(FUNC(uncompressed_header)(ctx, rw, &current->header));
414 
415  CHECK(FUNC(trailing_bits)(ctx, rw));
416 
417  return 0;
418 }
419 
421  VP9RawSuperframeIndex *current)
422 {
423  int err, i;
424 
425  HEADER("Superframe Index");
426 
427  f(3, superframe_marker);
428  f(2, bytes_per_framesize_minus_1);
429  f(3, frames_in_superframe_minus_1);
430 
431  for (i = 0; i <= current->frames_in_superframe_minus_1; i++) {
432  // Surprise little-endian!
433  fle(8 * (current->bytes_per_framesize_minus_1 + 1),
434  frame_sizes[i], 1, i);
435  }
436 
437  f(3, superframe_marker);
438  f(2, bytes_per_framesize_minus_1);
439  f(3, frames_in_superframe_minus_1);
440 
441  return 0;
442 }
render_size
static int FUNC() render_size(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrameHeader *current)
Definition: cbs_vp9_syntax_template.c:102
intra_only
int intra_only
Definition: mxfenc.c:2218
byte_alignment
#define byte_alignment(rw)
Definition: cbs_av1.c:722
CodedBitstreamVP9Context::frame_width
int frame_width
Definition: cbs_vp9.h:202
tile_info
static int FUNC() tile_info(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrameHeader *current)
Definition: cbs_vp9_syntax_template.c:256
segmentation_params
static int FUNC() segmentation_params(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrameHeader *current)
Definition: cbs_vp9_syntax_template.c:206
HEADER
#define HEADER(name)
Definition: cbs_av1.c:537
av_unused
#define av_unused
Definition: attributes.h:125
profile
mfxU16 profile
Definition: qsvenc.c:44
infer
#define infer(name, value)
Definition: cbs_av1.c:713
CodedBitstreamContext
Context structure for coded bitstream operations.
Definition: cbs.h:168
CodedBitstreamVP9Context::sb64_rows
uint16_t sb64_rows
Definition: cbs_vp9.h:200
CodedBitstreamVP9Context::ref
VP9ReferenceFrameState ref[VP9_NUM_REF_FRAMES]
Definition: cbs_vp9.h:209
VP9_FRAME_SYNC_1
@ VP9_FRAME_SYNC_1
Definition: cbs_vp9.h:51
VP9_NUM_REF_FRAMES
@ VP9_NUM_REF_FRAMES
Definition: cbs_vp9.h:35
VP9_REFS_PER_FRAME
@ VP9_REFS_PER_FRAME
Definition: cbs_vp9.h:30
CHECK
CHECK(-1) CHECK(-2) }} }} CHECK(1) CHECK(2) }} }} } if(diff0+diff1 > 0) temp -
ss
#define ss(width, name, subs,...)
Definition: cbs_vp9.c:261
VP9ReferenceFrameState
Definition: cbs_vp9.h:184
show_frame
static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream, AVFormatContext *fmt_ctx)
Definition: ffprobe.c:2112
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
ctx
AVFormatContext * ctx
Definition: movenc.c:48
VP9RawFrameHeader
Definition: cbs_vp9.h:83
color_range
color_range
Definition: vf_selectivecolor.c:44
CodedBitstreamVP9Context::mi_rows
uint16_t mi_rows
Definition: cbs_vp9.h:197
f
#define f(width, name)
Definition: cbs_vp9.c:255
if
if(ret)
Definition: filter_design.txt:179
VP9_FRAME_SYNC_2
@ VP9_FRAME_SYNC_2
Definition: cbs_vp9.h:52
fs
#define fs(width, name, subs,...)
Definition: cbs_vp9.c:259
CodedBitstreamVP9Context::mi_cols
uint16_t mi_cols
Definition: cbs_vp9.h:196
frame_sync_code
static int FUNC() frame_sync_code(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrameHeader *current)
Definition: cbs_vp9_syntax_template.c:19
delta_q
#define delta_q(name)
Definition: cbs_av1.c:703
CodedBitstreamVP9Context::subsampling_y
uint8_t subsampling_y
Definition: cbs_vp9.h:206
loop_filter_params
static int FUNC() loop_filter_params(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrameHeader *current)
Definition: cbs_vp9_syntax_template.c:164
VP9RawSuperframeIndex
Definition: cbs_vp9.h:172
superframe_index
static int FUNC() superframe_index(CodedBitstreamContext *ctx, RWContext *rw, VP9RawSuperframeIndex *current)
Definition: cbs_vp9_syntax_template.c:420
interpolation_filter
static int FUNC() interpolation_filter(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrameHeader *current)
Definition: cbs_vp9_syntax_template.c:152
VP9_LAST_FRAME
@ VP9_LAST_FRAME
Definition: cbs_vp9.h:70
quantization_params
static int FUNC() quantization_params(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrameHeader *current)
Definition: cbs_vp9_syntax_template.c:192
VP9RawFrame
Definition: cbs_vp9.h:164
frame_sizes
static const uint8_t frame_sizes[]
Definition: rtpdec_qcelp.c:24
CodedBitstreamVP9Context::bit_depth
int bit_depth
Definition: cbs_vp9.h:207
CodedBitstreamVP9Context::profile
int profile
Definition: cbs_vp9.h:193
VP9_CS_RGB
@ VP9_CS_RGB
Definition: cbs_vp9.h:64
frame_size
static int FUNC() frame_size(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrameHeader *current)
Definition: cbs_vp9_syntax_template.c:82
CodedBitstreamVP9Context
Definition: cbs_vp9.h:192
VP9_KEY_FRAME
@ VP9_KEY_FRAME
Definition: cbs_vp9.h:44
VP9_MAX_TILE_WIDTH_B64
@ VP9_MAX_TILE_WIDTH_B64
Definition: cbs_vp9.h:33
frame_size_with_refs
static int FUNC() frame_size_with_refs(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrameHeader *current)
Definition: cbs_vp9_syntax_template.c:117
CodedBitstreamVP9Context::subsampling_x
uint8_t subsampling_x
Definition: cbs_vp9.h:205
trailing_bits
static int FUNC() trailing_bits(CodedBitstreamContext *ctx, RWContext *rw)
Definition: cbs_vp9_syntax_template.c:396
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
color_config
static int FUNC() color_config(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrameHeader *current, int profile)
Definition: cbs_vp9_syntax_template.c:43
increment
#define increment(name, min, max)
Definition: cbs_av1.c:692
CodedBitstreamVP9Context::frame_height
int frame_height
Definition: cbs_vp9.h:203
xf
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:668
uint8_t
uint8_t
Definition: audio_convert.c:194
VP9ReferenceFrameState::frame_width
int frame_width
Definition: cbs_vp9.h:185
prob
#define prob(name, subs,...)
Definition: cbs_vp9.c:374
VP9_MIN_TILE_WIDTH_B64
@ VP9_MIN_TILE_WIDTH_B64
Definition: cbs_vp9.h:32
fle
#define fle(width, name, subs,...)
Definition: cbs_vp9.c:363
uncompressed_header
static int FUNC() uncompressed_header(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrameHeader *current)
Definition: cbs_vp9_syntax_template.c:277
FUNC
#define FUNC(a)
Definition: bit_depth_template.c:104
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
VP9_MAX_REF_FRAMES
@ VP9_MAX_REF_FRAMES
Definition: cbs_vp9.h:36
zero
#define zero
Definition: regdef.h:64
VP9_SEG_LVL_MAX
@ VP9_SEG_LVL_MAX
Definition: cbs_vp9.h:39
CodedBitstreamVP9Context::sb64_cols
uint16_t sb64_cols
Definition: cbs_vp9.h:199
fixed
#define fixed(width, name, value)
Definition: cbs_av1.c:570
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
RWContext
#define RWContext
Definition: cbs_av1.c:666
VP9_FRAME_SYNC_0
@ VP9_FRAME_SYNC_0
Definition: cbs_vp9.h:50
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1370
VP9_MAX_SEGMENTS
@ VP9_MAX_SEGMENTS
Definition: cbs_vp9.h:38
frame
static int FUNC() frame(CodedBitstreamContext *ctx, RWContext *rw, VP9RawFrame *current)
Definition: cbs_vp9_syntax_template.c:406