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
36
37 #define FF_DAV1D_VERSION_AT_LEAST(x,y) \
38 (DAV1D_API_VERSION_MAJOR > (x) || DAV1D_API_VERSION_MAJOR == (x) && DAV1D_API_VERSION_MINOR >= (y))
39
43 /* This packet coincides with AVCodecInternal.in_pkt
44 * and is not owned by us. */
48
56
62 };
63
66 };
67
69 {
71
73 }
74
76 {
80 uint8_t *aligned_ptr, *
data[4];
82
86
89 // Use twice the amount of required padding bytes for aligned_ptr below.
94 }
96 }
98 if (!buf)
100
101 // libdav1d requires DAV1D_PICTURE_ALIGNMENT aligned buffers, which av_malloc()
102 // doesn't guarantee for example when AVX is disabled at configure time.
103 // Use the extra DAV1D_PICTURE_ALIGNMENT padding bytes in the buffer to align it
104 // if required.
105 aligned_ptr = (uint8_t *)
FFALIGN((uintptr_t)buf->
data, DAV1D_PICTURE_ALIGNMENT);
107 DAV1D_PICTURE_ALIGNMENT);
111 }
112
113 p->data[0] =
data[0];
114 p->data[1] =
data[1];
115 p->data[2] =
data[2];
116 p->stride[0] = linesize[0];
117 p->stride[1] = linesize[1];
118 p->allocator_data = buf;
119
120 return 0;
121 }
122
124 {
126
128 }
129
131 {
132 c->profile = seq->profile;
133 c->level = ((seq->operating_points[0].major_level - 2) << 2)
134 | seq->operating_points[0].minor_level;
135
136 switch (seq->chr) {
137 case DAV1D_CHR_VERTICAL:
139 break;
140 case DAV1D_CHR_COLOCATED:
142 break;
143 }
148
149 if (seq->layout == DAV1D_PIXEL_LAYOUT_I444 &&
150 seq->mtrx == DAV1D_MC_IDENTITY &&
151 seq->pri == DAV1D_COLOR_PRI_BT709 &&
152 seq->trc == DAV1D_TRC_SRGB)
154 else
155 c->pix_fmt =
pix_fmt[seq->layout][seq->hbd];
156
157 if (seq->num_units_in_tick && seq->time_scale) {
159 seq->num_units_in_tick, seq->time_scale, INT_MAX);
160 if (seq->equal_picture_interval)
161 c->ticks_per_frame = seq->num_ticks_per_picture;
162 }
163
164 if (seq->film_grain_present)
166 else
168 }
169
171 {
172 Dav1dSequenceHeader seq;
174 int res;
175
176 if (!
c->extradata ||
c->extradata_size <= 0)
177 return 0;
178
179 if (
c->extradata[0] & 0x80) {
180 int version =
c->extradata[0] & 0x7F;
181
182 if (
version != 1 ||
c->extradata_size < 4) {
185 "Error decoding extradata\n");
187 }
188
189 // Do nothing if there are no configOBUs to parse
190 if (
c->extradata_size == 4)
191 return 0;
192
194 }
195
196 res = dav1d_parse_sequence_header(&seq,
c->extradata +
offset,
198 if (res < 0)
199 return 0; // Assume no seqhdr OBUs are present
200
203 if (res < 0)
204 return res;
205
206 return 0;
207 }
208
210 {
213 #if FF_DAV1D_VERSION_AT_LEAST(6,0)
214 int threads =
c->thread_count;
215 #else
216 int threads = (
c->thread_count ?
c->thread_count :
av_cpu_count()) * 3 / 2;
217 #endif
218 int res;
219
220 dav1d->
pkt =
c->internal->in_pkt;
221
223
224 dav1d_default_settings(&
s);
227 s.allocator.cookie = dav1d;
230 s.frame_size_limit =
c->max_pixels;
233 else
235
239 #if FF_DAV1D_VERSION_AT_LEAST(6,2)
240 s.strict_std_compliance =
c->strict_std_compliance > 0;
241 #endif
242
243 #if FF_DAV1D_VERSION_AT_LEAST(6,0)
246 else
247 s.n_threads =
FFMIN(threads, DAV1D_MAX_THREADS);
250 s.n_threads,
s.max_frame_delay);
251 #else
259 s.n_frame_threads,
s.n_tile_threads);
260 #endif
261
263 if (res < 0)
264 return res;
265
266 res = dav1d_open(&dav1d->
c, &
s);
267 if (res < 0)
269
270 return 0;
271 }
272
274 {
276
277 dav1d_data_unref(&dav1d->
data);
278 dav1d_flush(dav1d->
c);
279 }
280
283
285 }
286
290 }
291
293 {
296 Dav1dPicture pic = { 0 }, *p = &pic;
297 #if FF_DAV1D_VERSION_AT_LEAST(5,1)
298 enum Dav1dEventFlags event_flags = 0;
299 #endif
300 int res;
301
304
307 return res;
308
312 if (res < 0) {
314 return res;
315 }
316
320
323
325 uint8_t *reordered_opaque =
av_memdup(&
c->reordered_opaque,
326 sizeof(
c->reordered_opaque));
327 if (!reordered_opaque) {
328 dav1d_data_unref(
data);
330 }
331
332 res = dav1d_data_wrap_user_data(
data, reordered_opaque,
334 if (res < 0) {
336 dav1d_data_unref(
data);
337 return res;
338 }
339 }
340 } else if (res >= 0) {
343 }
344 }
345
346 res = dav1d_send_data(dav1d->
c,
data);
347 if (res < 0) {
351 dav1d_data_unref(
data);
352 return res;
353 }
354 }
355
356 res = dav1d_get_picture(dav1d->
c, p);
357 if (res < 0) {
360 else if (res ==
AVERROR(EAGAIN) &&
c->internal->draining)
362
363 return res;
364 }
365
367
368 // This requires the custom allocator above
370 if (!
frame->buf[0]) {
371 dav1d_picture_unref(p);
373 }
374
375 frame->data[0] = p->data[0];
376 frame->data[1] = p->data[1];
377 frame->data[2] = p->data[2];
378 frame->linesize[0] = p->stride[0];
379 frame->linesize[1] = p->stride[1];
380 frame->linesize[2] = p->stride[1];
381
382 #if FF_DAV1D_VERSION_AT_LEAST(5,1)
383 dav1d_get_event_flags(dav1d->
c, &event_flags);
385 (event_flags & DAV1D_EVENT_FLAG_NEW_SEQUENCE))
386 #endif
389 if (res < 0)
391
392 frame->width = p->p.w;
393 frame->height = p->p.h;
394 if (
c->width != p->p.w ||
c->height != p->p.h) {
396 if (res < 0)
398 }
399
401 &
frame->sample_aspect_ratio.den,
402 frame->height * (int64_t)p->frame_hdr->render_width,
403 frame->width * (int64_t)p->frame_hdr->render_height,
404 INT_MAX);
406
407 if (p->m.user_data.data)
408 memcpy(&
frame->reordered_opaque, p->m.user_data.data,
sizeof(
frame->reordered_opaque));
409 else
411
412 // match timestamps and packet size
413 frame->pts = p->m.timestamp;
414 frame->pkt_dts = p->m.timestamp;
415 frame->pkt_pos = p->m.offset;
416 frame->pkt_size = p->m.size;
417 frame->pkt_duration = p->m.duration;
418 frame->key_frame = p->frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY;
419
420 switch (p->frame_hdr->frame_type) {
421 case DAV1D_FRAME_TYPE_KEY:
422 case DAV1D_FRAME_TYPE_INTRA:
424 break;
425 case DAV1D_FRAME_TYPE_INTER:
427 break;
428 case DAV1D_FRAME_TYPE_SWITCH:
430 break;
431 default:
434 }
435
436 if (p->mastering_display) {
438 if (!mastering) {
441 }
442
443 for (
int i = 0;
i < 3;
i++) {
446 }
449
452
455 }
456 if (p->content_light) {
458 if (!light) {
461 }
462 light->
MaxCLL = p->content_light->max_content_light_level;
463 light->
MaxFALL = p->content_light->max_frame_average_light_level;
464 }
465 if (p->itut_t35) {
467 unsigned int user_identifier;
468
472 user_identifier = bytestream2_get_be32(&gb);
473 switch (user_identifier) {
474 case MKBETAG(
'G',
'A',
'9',
'4'): {
// closed captions
476
478 if (res < 0)
480 if (!res)
481 break;
482
485
487 break;
488 }
489 default: // ignore unsupported identifiers
490 break;
491 }
492 }
493 if (p->frame_hdr->film_grain.present && (!dav1d->
apply_grain ||
496 if (!fgp) {
499 }
500
502 fgp->
seed = p->frame_hdr->film_grain.data.seed;
511
524 memcpy(&fgp->
codec.
aom.
uv_mult, &p->frame_hdr->film_grain.data.uv_mult,
530 }
531
532 res = 0;
534 dav1d_picture_unref(p);
535 if (res < 0)
537 return res;
538 }
539
541 {
543
545 dav1d_data_unref(&dav1d->
data);
546 dav1d_close(&dav1d->
c);
547
548 return 0;
549 }
550
551 #ifndef DAV1D_MAX_FRAME_THREADS
552 #define DAV1D_MAX_FRAME_THREADS DAV1D_MAX_THREADS
553 #endif
554 #ifndef DAV1D_MAX_TILE_THREADS
555 #define DAV1D_MAX_TILE_THREADS DAV1D_MAX_THREADS
556 #endif
557
558 #define OFFSET(x) offsetof(Libdav1dContext, x)
559 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
564 {
"oppoint",
"Select an operating point of the scalable bitstream",
OFFSET(operating_point),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 31,
VD },
567 };
568
574 };
575
590 .wrapper_name = "libdav1d",
591 };