1 /*
2 * Copyright (c) 2018 Ronald S. Bultje <rsbultje gmail com>
3 * Copyright (c) 2018 James Almer <jamrial gmail com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <dav1d/dav1d.h>
23
30
37
38 #define FF_DAV1D_VERSION_AT_LEAST(x,y) \
39 (DAV1D_API_VERSION_MAJOR > (x) || DAV1D_API_VERSION_MAJOR == (x) && DAV1D_API_VERSION_MINOR >= (y))
40
44 /* This packet coincides with AVCodecInternal.in_pkt
45 * and is not owned by us. */
49
57
63 };
64
67 };
68
70 {
72
74 }
75
77 {
81 uint8_t *aligned_ptr, *
data[4];
83
87
90 // Use twice the amount of required padding bytes for aligned_ptr below.
95 }
97 }
99 if (!buf)
101
102 // libdav1d requires DAV1D_PICTURE_ALIGNMENT aligned buffers, which av_malloc()
103 // doesn't guarantee for example when AVX is disabled at configure time.
104 // Use the extra DAV1D_PICTURE_ALIGNMENT padding bytes in the buffer to align it
105 // if required.
106 aligned_ptr = (uint8_t *)
FFALIGN((uintptr_t)buf->
data, DAV1D_PICTURE_ALIGNMENT);
108 DAV1D_PICTURE_ALIGNMENT);
112 }
113
114 p->data[0] =
data[0];
115 p->data[1] =
data[1];
116 p->data[2] =
data[2];
117 p->stride[0] = linesize[0];
118 p->stride[1] = linesize[1];
119 p->allocator_data = buf;
120
121 return 0;
122 }
123
125 {
127
129 }
130
132 {
133 c->profile = seq->profile;
134 c->level = ((seq->operating_points[0].major_level - 2) << 2)
135 | seq->operating_points[0].minor_level;
136
137 switch (seq->chr) {
138 case DAV1D_CHR_VERTICAL:
140 break;
141 case DAV1D_CHR_COLOCATED:
143 break;
144 }
149
150 if (seq->layout == DAV1D_PIXEL_LAYOUT_I444 &&
151 seq->mtrx == DAV1D_MC_IDENTITY &&
152 seq->pri == DAV1D_COLOR_PRI_BT709 &&
153 seq->trc == DAV1D_TRC_SRGB)
155 else
156 c->pix_fmt =
pix_fmt[seq->layout][seq->hbd];
157
158 if (seq->num_units_in_tick && seq->time_scale) {
160 seq->num_units_in_tick, seq->time_scale, INT_MAX);
161 if (seq->equal_picture_interval)
162 c->ticks_per_frame = seq->num_ticks_per_picture;
163 }
164
165 if (seq->film_grain_present)
167 else
169 }
170
172 {
173 Dav1dSequenceHeader seq;
175 int res;
176
177 if (!
c->extradata ||
c->extradata_size <= 0)
178 return 0;
179
180 if (
c->extradata[0] & 0x80) {
181 int version =
c->extradata[0] & 0x7F;
182
183 if (
version != 1 ||
c->extradata_size < 4) {
186 "Error decoding extradata\n");
188 }
189
190 // Do nothing if there are no configOBUs to parse
191 if (
c->extradata_size == 4)
192 return 0;
193
195 }
196
197 res = dav1d_parse_sequence_header(&seq,
c->extradata +
offset,
199 if (res < 0)
200 return 0; // Assume no seqhdr OBUs are present
201
204 if (res < 0)
205 return res;
206
207 return 0;
208 }
209
211 {
214 #if FF_DAV1D_VERSION_AT_LEAST(6,0)
215 int threads =
c->thread_count;
216 #else
217 int threads = (
c->thread_count ?
c->thread_count :
av_cpu_count()) * 3 / 2;
218 #endif
219 int res;
220
221 dav1d->
pkt =
c->internal->in_pkt;
222
224
225 dav1d_default_settings(&
s);
228 s.allocator.cookie = dav1d;
231 s.frame_size_limit =
c->max_pixels;
234 else
236
240 #if FF_DAV1D_VERSION_AT_LEAST(6,2)
241 s.strict_std_compliance =
c->strict_std_compliance > 0;
242 #endif
243
244 #if FF_DAV1D_VERSION_AT_LEAST(6,0)
247 else
248 s.n_threads =
FFMIN(threads, DAV1D_MAX_THREADS);
251 s.n_threads,
s.max_frame_delay);
252 #else
260 s.n_frame_threads,
s.n_tile_threads);
261 #endif
262
264 if (res < 0)
265 return res;
266
267 res = dav1d_open(&dav1d->
c, &
s);
268 if (res < 0)
270
271 return 0;
272 }
273
275 {
277
278 dav1d_data_unref(&dav1d->
data);
279 dav1d_flush(dav1d->
c);
280 }
281
284
286 }
287
291 }
292
294 {
297 Dav1dPicture pic = { 0 }, *p = &pic;
298 #if FF_DAV1D_VERSION_AT_LEAST(5,1)
299 enum Dav1dEventFlags event_flags = 0;
300 #endif
301 int res;
302
305
308 return res;
309
313 if (res < 0) {
315 return res;
316 }
317
321
324
326 uint8_t *reordered_opaque =
av_memdup(&
c->reordered_opaque,
327 sizeof(
c->reordered_opaque));
328 if (!reordered_opaque) {
329 dav1d_data_unref(
data);
331 }
332
333 res = dav1d_data_wrap_user_data(
data, reordered_opaque,
335 if (res < 0) {
337 dav1d_data_unref(
data);
338 return res;
339 }
340 }
341 } else if (res >= 0) {
344 }
345 }
346
347 res = dav1d_send_data(dav1d->
c,
data);
348 if (res < 0) {
352 dav1d_data_unref(
data);
353 return res;
354 }
355 }
356
357 res = dav1d_get_picture(dav1d->
c, p);
358 if (res < 0) {
361 else if (res ==
AVERROR(EAGAIN) &&
c->internal->draining)
363
364 return res;
365 }
366
368
369 // This requires the custom allocator above
371 if (!
frame->buf[0]) {
372 dav1d_picture_unref(p);
374 }
375
376 frame->data[0] = p->data[0];
377 frame->data[1] = p->data[1];
378 frame->data[2] = p->data[2];
379 frame->linesize[0] = p->stride[0];
380 frame->linesize[1] = p->stride[1];
381 frame->linesize[2] = p->stride[1];
382
383 #if FF_DAV1D_VERSION_AT_LEAST(5,1)
384 dav1d_get_event_flags(dav1d->
c, &event_flags);
386 event_flags & DAV1D_EVENT_FLAG_NEW_SEQUENCE)
387 #endif
390 if (res < 0)
392
393 frame->width = p->p.w;
394 frame->height = p->p.h;
395 if (
c->width != p->p.w ||
c->height != p->p.h) {
397 if (res < 0)
399 }
400
402 &
frame->sample_aspect_ratio.den,
403 frame->height * (int64_t)p->frame_hdr->render_width,
404 frame->width * (int64_t)p->frame_hdr->render_height,
405 INT_MAX);
407
408 if (p->m.user_data.data)
409 memcpy(&
frame->reordered_opaque, p->m.user_data.data,
sizeof(
frame->reordered_opaque));
410 else
412
413 // match timestamps and packet size
414 frame->pts = p->m.timestamp;
415 frame->pkt_dts = p->m.timestamp;
416 frame->pkt_pos = p->m.offset;
417 frame->pkt_size = p->m.size;
418 frame->pkt_duration = p->m.duration;
419 frame->key_frame = p->frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY;
420
421 switch (p->frame_hdr->frame_type) {
422 case DAV1D_FRAME_TYPE_KEY:
423 case DAV1D_FRAME_TYPE_INTRA:
425 break;
426 case DAV1D_FRAME_TYPE_INTER:
428 break;
429 case DAV1D_FRAME_TYPE_SWITCH:
431 break;
432 default:
435 }
436
437 if (p->mastering_display) {
439 if (!mastering) {
442 }
443
444 for (
int i = 0;
i < 3;
i++) {
447 }
450
453
456 }
457 if (p->content_light) {
459 if (!light) {
462 }
463 light->
MaxCLL = p->content_light->max_content_light_level;
464 light->
MaxFALL = p->content_light->max_frame_average_light_level;
465 }
466 if (p->itut_t35) {
468 unsigned int user_identifier;
469
473 user_identifier = bytestream2_get_be32(&gb);
474 switch (user_identifier) {
475 case MKBETAG(
'G',
'A',
'9',
'4'): {
// closed captions
477
479 if (res < 0)
481 if (!res)
482 break;
483
486
488 break;
489 }
490 default: // ignore unsupported identifiers
491 break;
492 }
493 }
494 if (p->frame_hdr->film_grain.present && (!dav1d->
apply_grain ||
497 if (!fgp) {
500 }
501
503 fgp->
seed = p->frame_hdr->film_grain.data.seed;
512
525 memcpy(&fgp->
codec.
aom.
uv_mult, &p->frame_hdr->film_grain.data.uv_mult,
531 }
532
533 res = 0;
535 dav1d_picture_unref(p);
536 if (res < 0)
538 return res;
539 }
540
542 {
544
546 dav1d_data_unref(&dav1d->
data);
547 dav1d_close(&dav1d->
c);
548
549 return 0;
550 }
551
552 #ifndef DAV1D_MAX_FRAME_THREADS
553 #define DAV1D_MAX_FRAME_THREADS DAV1D_MAX_THREADS
554 #endif
555 #ifndef DAV1D_MAX_TILE_THREADS
556 #define DAV1D_MAX_TILE_THREADS DAV1D_MAX_THREADS
557 #endif
558
559 #define OFFSET(x) offsetof(Libdav1dContext, x)
560 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
565 {
"oppoint",
"Select an operating point of the scalable bitstream",
OFFSET(operating_point),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 31,
VD },
568 };
569
575 };
576
578 .
p.
name =
"libdav1d",
591 .p.wrapper_name = "libdav1d",
592 };