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