]> git.sesse.net Git - ffmpeg/blob - libavcodec/cbs_vp9_syntax_template.c
lavc: Add coded bitstream read/write support for AV1
[ffmpeg] / libavcodec / cbs_vp9_syntax_template.c
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
19 static int FUNC(frame_sync_code)(CodedBitstreamContext *ctx, RWContext *rw,
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
43 static int FUNC(color_config)(CodedBitstreamContext *ctx, RWContext *rw,
44                               VP9RawFrameHeader *current, int profile)
45 {
46     int err;
47
48     if (profile >= 2)
49         f(1, ten_or_twelve_bit);
50
51     f(3, color_space);
52
53     if (current->color_space != VP9_CS_RGB) {
54         f(1, color_range);
55         if (profile == 1 || profile == 3) {
56             f(1, subsampling_x);
57             f(1, subsampling_y);
58             f(1, color_config_reserved_zero);
59         } else {
60             infer(subsampling_x, 1);
61             infer(subsampling_y, 1);
62         }
63     } else {
64         infer(color_range, 1);
65         if (profile == 1 || profile == 3) {
66             infer(subsampling_x, 0);
67             infer(subsampling_y, 0);
68         }
69     }
70
71     return 0;
72 }
73
74 static int FUNC(frame_size)(CodedBitstreamContext *ctx, RWContext *rw,
75                             VP9RawFrameHeader *current)
76 {
77     CodedBitstreamVP9Context *vp9 = ctx->priv_data;
78     int err;
79
80     f(16, frame_width_minus_1);
81     f(16, frame_height_minus_1);
82
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;
87
88     return 0;
89 }
90
91 static int FUNC(render_size)(CodedBitstreamContext *ctx, RWContext *rw,
92                              VP9RawFrameHeader *current)
93 {
94     int err;
95
96     f(1, render_and_frame_size_different);
97
98     if (current->render_and_frame_size_different) {
99         f(16, render_width_minus_1);
100         f(16, render_height_minus_1);
101     }
102
103     return 0;
104 }
105
106 static int FUNC(frame_size_with_refs)(CodedBitstreamContext *ctx, RWContext *rw,
107                                       VP9RawFrameHeader *current)
108 {
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])
114             break;
115     }
116     if (i >= VP9_REFS_PER_FRAME)
117         CHECK(FUNC(frame_size)(ctx, rw, current));
118     CHECK(FUNC(render_size)(ctx, rw, current));
119
120     return 0;
121 }
122
123 static int FUNC(interpolation_filter)(CodedBitstreamContext *ctx, RWContext *rw,
124                                       VP9RawFrameHeader *current)
125 {
126     int err;
127
128     f(1, is_filter_switchable);
129     if (!current->is_filter_switchable)
130         f(2, raw_interpolation_filter_type);
131
132     return 0;
133 }
134
135 static int FUNC(loop_filter_params)(CodedBitstreamContext *ctx, RWContext *rw,
136                                     VP9RawFrameHeader *current)
137 {
138     int err, i;
139
140     f(6, loop_filter_level);
141     f(3, loop_filter_sharpness);
142
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);
151             }
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);
156             }
157         }
158     }
159
160     return 0;
161 }
162
163 static int FUNC(quantization_params)(CodedBitstreamContext *ctx, RWContext *rw,
164                                      VP9RawFrameHeader *current)
165 {
166     int err;
167
168     f(8, base_q_idx);
169
170     delta_q(delta_q_y_dc);
171     delta_q(delta_q_uv_dc);
172     delta_q(delta_q_uv_ac);
173
174     return 0;
175 }
176
177 static int FUNC(segmentation_params)(CodedBitstreamContext *ctx, RWContext *rw,
178                                      VP9RawFrameHeader *current)
179 {
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 };
182
183     int err, i, j;
184
185     f(1, segmentation_enabled);
186
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);
196                 else
197                     infer(segmentation_pred_prob[i], 255);
198             }
199         }
200
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);
213                         else
214                             infer(feature_sign[i][j], 0);
215                     } else {
216                         infer(feature_value[i][j], 0);
217                         infer(feature_sign[i][j],  0);
218                     }
219                 }
220             }
221         }
222     }
223
224     return 0;
225 }
226
227 static int FUNC(tile_info)(CodedBitstreamContext *ctx, RWContext *rw,
228                            VP9RawFrameHeader *current)
229 {
230     CodedBitstreamVP9Context *vp9 = ctx->priv_data;
231     int min_log2_tile_cols, max_log2_tile_cols;
232     int err;
233
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;
240
241     increment(tile_cols_log2, min_log2_tile_cols, max_log2_tile_cols);
242
243     increment(tile_rows_log2, 0, 2);
244
245     return 0;
246 }
247
248 static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
249                                      VP9RawFrameHeader *current)
250 {
251     int profile, i;
252     int err;
253
254     f(2, frame_marker);
255
256     f(1, profile_low_bit);
257     f(1, profile_high_bit);
258     profile = (current->profile_high_bit << 1) + current->profile_low_bit;
259     if (profile == 3)
260         f(1, profile_reserved_zero);
261
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);
268         return 0;
269     }
270
271     f(1, frame_type);
272     f(1, show_frame);
273     f(1, error_resilient_mode);
274
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));
280
281         infer(refresh_frame_flags, 0xff);
282
283     } else {
284          if (current->show_frame == 0)
285              f(1, intra_only);
286          else
287              infer(intra_only, 0);
288
289          if (current->error_resilient_mode == 0)
290              f(2, reset_frame_context);
291          else
292              infer(reset_frame_context, 0);
293
294          if (current->intra_only == 1) {
295              CHECK(FUNC(frame_sync_code)(ctx, rw, current));
296
297              if (profile > 0) {
298                  CHECK(FUNC(color_config)(ctx, rw, current, profile));
299              } else {
300                  infer(color_space,   1);
301                  infer(subsampling_x, 1);
302                  infer(subsampling_y, 1);
303              }
304
305              f(8, refresh_frame_flags);
306
307              CHECK(FUNC(frame_size)(ctx, rw, current));
308              CHECK(FUNC(render_size)(ctx, rw, current));
309          } else {
310              f(8, refresh_frame_flags);
311
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);
316              }
317
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));
321          }
322     }
323
324     if (current->error_resilient_mode == 0) {
325         f(1, refresh_frame_context);
326         f(1, frame_parallel_decoding_mode);
327     } else {
328         infer(refresh_frame_context,        0);
329         infer(frame_parallel_decoding_mode, 1);
330     }
331
332     f(2, frame_context_idx);
333
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));
338
339     f(16, header_size_in_bytes);
340
341     return 0;
342 }
343
344 static int FUNC(trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
345 {
346     int err;
347     av_unused int zero = 0;
348     while (byte_alignment(rw) != 0)
349         xf(1, zero_bit, zero, 0);
350
351     return 0;
352 }
353
354 static int FUNC(frame)(CodedBitstreamContext *ctx, RWContext *rw,
355                        VP9RawFrame *current)
356 {
357     int err;
358
359     HEADER("Frame");
360
361     CHECK(FUNC(uncompressed_header)(ctx, rw, &current->header));
362
363     CHECK(FUNC(trailing_bits)(ctx, rw));
364
365     return 0;
366 }
367
368 static int FUNC(superframe_index)(CodedBitstreamContext *ctx, RWContext *rw,
369                                   VP9RawSuperframeIndex *current)
370 {
371     int err, i;
372
373     HEADER("Superframe Index");
374
375     f(3, superframe_marker);
376     f(2, bytes_per_framesize_minus_1);
377     f(3, frames_in_superframe_minus_1);
378
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);
383     }
384
385     f(3, superframe_marker);
386     f(2, bytes_per_framesize_minus_1);
387     f(3, frames_in_superframe_minus_1);
388
389     return 0;
390 }