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
24
25
28 const int *subscripts,
int32_t *write_to)
29 {
30 uint32_t magnitude;
31 int sign;
33
35
38 "%s: bitstream ended.\n",
name);
40 }
41
45
47
49 return 0;
50 }
51
55 {
56 uint32_t magnitude;
57 int sign;
58
60
63
66
69
71
72 return 0;
73 }
74
76 uint32_t range_min, uint32_t range_max,
77 const char *
name, uint32_t *write_to)
78 {
80
82
83 av_assert0(range_min <= range_max && range_max - range_min < 32);
84
88 "%s: bitstream ended.\n",
name);
90 }
93 else
94 break;
95 }
96
98
100 return 0;
101 }
102
104 uint32_t range_min, uint32_t range_max,
106 {
108
110
111 av_assert0(range_min <= range_max && range_max - range_min < 8);
112 if (value < range_min || value > range_max) {
114 "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
117 }
118
119 if (
value == range_max)
120 len = range_max - range_min;
121 else
125
128
130
131 return 0;
132 }
133
136 const int *subscripts, uint32_t *write_to)
137 {
140
142
144
147 "%s: bitstream ended.\n",
name);
149 }
150
154
156
158 return 0;
159 }
160
163 const int *subscripts, uint32_t
value)
164 {
166
168
170
173
176
178
179 return 0;
180 }
181
182 #define HEADER(name) do { \
183 ff_cbs_trace_header(ctx, name); \
184 } while (0)
185
186 #define CHECK(call) do { \
187 err = (call); \
188 if (err < 0) \
189 return err; \
190 } while (0)
191
192 #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
193 #define FUNC_VP9(rw, name) FUNC_NAME(rw, vp9, name)
194 #define FUNC(name) FUNC_VP9(READWRITE, name)
195
196 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
197
198 #define s(width, name) \
199 xs(width, name, current->name, 0, )
200 #define fs(width, name, subs, ...) \
201 xf(width, name, current->name, subs, __VA_ARGS__)
202 #define ss(width, name, subs, ...) \
203 xs(width, name, current->name, subs, __VA_ARGS__)
204
206 #define READWRITE read
207 #define RWContext GetBitContext
208
209 #define f(width, name) do { \
210 uint32_t value; \
211 CHECK(ff_cbs_read_simple_unsigned(ctx, rw, width, #name, \
212 &value)); \
213 current->name = value; \
214 } while (0)
215 #define xf(width, name, var, subs, ...) do { \
216 uint32_t value; \
217 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
218 SUBSCRIPTS(subs, __VA_ARGS__), \
219 &value, 0, (1 << width) - 1)); \
220 var = value; \
221 } while (0)
222 #define xs(width, name, var, subs, ...) do { \
223 int32_t value; \
224 CHECK(cbs_vp9_read_s(ctx, rw, width, #name, \
225 SUBSCRIPTS(subs, __VA_ARGS__), &value)); \
226 var = value; \
227 } while (0)
228
229
230 #define increment(name, min, max) do { \
231 uint32_t value; \
232 CHECK(cbs_vp9_read_increment(ctx, rw, min, max, #name, &value)); \
233 current->name = value; \
234 } while (0)
235
236 #define fle(width, name, subs, ...) do { \
237 CHECK(cbs_vp9_read_le(ctx, rw, width, #name, \
238 SUBSCRIPTS(subs, __VA_ARGS__), ¤t->name)); \
239 } while (0)
240
241 #define delta_q(name) do { \
242 uint8_t delta_coded; \
243 int8_t delta_q; \
244 xf(1, name.delta_coded, delta_coded, 0, ); \
245 if (delta_coded) \
246 xs(4, name.delta_q, delta_q, 0, ); \
247 else \
248 delta_q = 0; \
249 current->name = delta_q; \
250 } while (0)
251
252 #define prob(name, subs, ...) do { \
253 uint8_t prob_coded; \
254 uint8_t prob; \
255 xf(1, name.prob_coded, prob_coded, subs, __VA_ARGS__); \
256 if (prob_coded) \
257 xf(8, name.prob, prob, subs, __VA_ARGS__); \
258 else \
259 prob = 255; \
260 current->name = prob; \
261 } while (0)
262
263 #define fixed(width, name, value) do { \
264 av_unused uint32_t fixed_value; \
265 CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
266 0, &fixed_value, value, value)); \
267 } while (0)
268
269 #define infer(name, value) do { \
270 current->name = value; \
271 } while (0)
272
273 #define byte_alignment(rw) (get_bits_count(rw) % 8)
274
276
277 #undef READ
278 #undef READWRITE
279 #undef RWContext
280 #undef f
281 #undef xf
282 #undef xs
283 #undef increment
284 #undef fle
285 #undef delta_q
286 #undef prob
287 #undef fixed
288 #undef infer
289 #undef byte_alignment
290
291
293 #define READWRITE write
294 #define RWContext PutBitContext
295
296 #define f(width, name) do { \
297 CHECK(ff_cbs_write_simple_unsigned(ctx, rw, width, #name, \
298 current->name)); \
299 } while (0)
300 #define xf(width, name, var, subs, ...) do { \
301 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
302 SUBSCRIPTS(subs, __VA_ARGS__), \
303 var, 0, (1 << width) - 1)); \
304 } while (0)
305 #define xs(width, name, var, subs, ...) do { \
306 CHECK(cbs_vp9_write_s(ctx, rw, width, #name, \
307 SUBSCRIPTS(subs, __VA_ARGS__), var)); \
308 } while (0)
309
310 #define increment(name, min, max) do { \
311 CHECK(cbs_vp9_write_increment(ctx, rw, min, max, #name, current->name)); \
312 } while (0)
313
314 #define fle(width, name, subs, ...) do { \
315 CHECK(cbs_vp9_write_le(ctx, rw, width, #name, \
316 SUBSCRIPTS(subs, __VA_ARGS__), current->name)); \
317 } while (0)
318
319 #define delta_q(name) do { \
320 xf(1, name.delta_coded, !!current->name, 0, ); \
321 if (current->name) \
322 xs(4, name.delta_q, current->name, 0, ); \
323 } while (0)
324
325 #define prob(name, subs, ...) do { \
326 xf(1, name.prob_coded, current->name != 255, subs, __VA_ARGS__); \
327 if (current->name != 255) \
328 xf(8, name.prob, current->name, subs, __VA_ARGS__); \
329 } while (0)
330
331 #define fixed(width, name, value) do { \
332 CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
333 0, value, value, value)); \
334 } while (0)
335
336 #define infer(name, value) do { \
337 if (current->name != (value)) { \
338 av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
339 "%s does not match inferred value: " \
340 "%"PRId64", but should be %"PRId64".\n", \
341 #name, (int64_t)current->name, (int64_t)(value)); \
342 } \
343 } while (0)
344
345 #define byte_alignment(rw) (put_bits_count(rw) % 8)
346
348
349 #undef WRITE
350 #undef READWRITE
351 #undef RWContext
352 #undef f
353 #undef xf
354 #undef xs
355 #undef increment
356 #undef fle
357 #undef delta_q
358 #undef prob
359 #undef fixed
360 #undef infer
361 #undef byte_alignment
362
363
367 {
368 uint8_t superframe_header;
369 int err;
370
373
374 // Last byte in the packet.
376
377 if ((superframe_header & 0xe0) == 0xc0) {
380 size_t index_size,
pos;
382
383 index_size = 2 + (((superframe_header & 0x18) >> 3) + 1) *
384 ((superframe_header & 0x07) + 1);
385
388
390 8 * index_size);
391 if (err < 0)
392 return err;
393
394 err = cbs_vp9_read_superframe_index(
ctx, &gbc, &sfi);
395 if (err < 0)
396 return err;
397
402 "in superframe: %"PRIu32" bytes.\n",
405 }
406
407 err = ff_cbs_append_unit_data(frag, 0,
411 if (err < 0)
412 return err;
413
415 }
420 }
421
422 return 0;
423
424 } else {
425 err = ff_cbs_append_unit_data(frag, 0,
428 if (err < 0)
429 return err;
430 }
431
432 return 0;
433 }
434
437 {
441
443 if (err < 0)
444 return err;
445
446 err = ff_cbs_alloc_unit_content(
ctx, unit);
447 if (err < 0)
448 return err;
450
451 err = cbs_vp9_read_frame(
ctx, &gbc,
frame);
452 if (err < 0)
453 return err;
454
459
461 // No data (e.g. a show-existing-frame frame).
462 } else {
464 if (!
frame->data_ref)
466
469 }
470
471 return 0;
472 }
473
477 {
479 int err;
480
481 err = cbs_vp9_write_frame(
ctx, pbc,
frame);
482 if (err < 0)
483 return err;
484
485 // Frame must be byte-aligned.
487
491
495 }
496
497 return 0;
498 }
499
502 {
503 int err;
504
506 // Output is just the content of the single frame.
507
509
513
516
517 } else {
518 // Build superframe out of frames.
519
526
529 "make superframe: %d.\n", frag->
nb_units);
531 }
532
535 if (max < frag->units[
i].data_size)
537
539 size_len = 1;
540 else
543
547
552 }
553
559
566 }
568
570
571 err = cbs_vp9_write_superframe_index(
ctx, &pbc, &sfi);
572 if (err < 0) {
574 "superframe index.\n");
576 return err;
577 }
578
581
585 }
586
587 return 0;
588 }
589
591 {
593
594 memset(vp9->
ref, 0,
sizeof(vp9->
ref));
595 }
596
600 };
601
604
606
608
612
614
616 };