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
31
41
42 #define FF_DAV1D_VERSION_AT_LEAST(x,y) \
43 (DAV1D_API_VERSION_MAJOR > (x) || DAV1D_API_VERSION_MAJOR == (x) && DAV1D_API_VERSION_MINOR >= (y))
44
51
60
66 };
67
70 };
71
73 {
75
77 }
78
80 {
84 uint8_t *aligned_ptr, *
data[4];
86
90
93 // Use twice the amount of required padding bytes for aligned_ptr below.
98 }
100 }
102 if (!buf)
104
105 // libdav1d requires DAV1D_PICTURE_ALIGNMENT aligned buffers, which av_malloc()
106 // doesn't guarantee for example when AVX is disabled at configure time.
107 // Use the extra DAV1D_PICTURE_ALIGNMENT padding bytes in the buffer to align it
108 // if required.
109 aligned_ptr = (uint8_t *)
FFALIGN((uintptr_t)buf->
data, DAV1D_PICTURE_ALIGNMENT);
111 DAV1D_PICTURE_ALIGNMENT);
115 }
116
117 p->data[0] =
data[0];
118 p->data[1] =
data[1];
119 p->data[2] =
data[2];
120 p->stride[0] = linesize[0];
121 p->stride[1] = linesize[1];
122 p->allocator_data = buf;
123
124 return 0;
125 }
126
128 {
130
132 }
133
135 {
136 c->profile = seq->profile;
137 c->level = ((seq->operating_points[0].major_level - 2) << 2)
138 | seq->operating_points[0].minor_level;
139
140 switch (seq->chr) {
141 case DAV1D_CHR_VERTICAL:
143 break;
144 case DAV1D_CHR_COLOCATED:
146 break;
147 }
152
153 if (seq->layout == DAV1D_PIXEL_LAYOUT_I444 &&
154 seq->mtrx == DAV1D_MC_IDENTITY &&
155 seq->pri == DAV1D_COLOR_PRI_BT709 &&
156 seq->trc == DAV1D_TRC_SRGB)
158 else
159 c->pix_fmt =
pix_fmt[seq->layout][seq->hbd];
160
162 (unsigned)seq->num_units_in_tick,
163 (unsigned)seq->time_scale);
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
220 int res;
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);
252 s.n_threads,
s.max_frame_delay);
253 #else
263 s.n_frame_threads,
s.n_tile_threads);
264 #endif
265
266 #if FF_DAV1D_VERSION_AT_LEAST(6,8)
268 s.decode_frame_type = DAV1D_DECODEFRAMETYPE_KEY;
270 s.decode_frame_type = DAV1D_DECODEFRAMETYPE_INTRA;
272 s.decode_frame_type = DAV1D_DECODEFRAMETYPE_REFERENCE;
273 #endif
274
276 if (res < 0)
277 return res;
278
279 res = dav1d_open(&dav1d->
c, &
s);
280 if (res < 0)
282
283 #if FF_DAV1D_VERSION_AT_LEAST(6,7)
284 res = dav1d_get_frame_delay(&
s);
285 if (res < 0) // Should not happen
287
288 // When dav1d_get_frame_delay() returns 1, there's no delay whatsoever
289 c->delay = res > 1 ? res : 0;
290 #endif
291
297 return 0;
298 }
299
301 {
303
304 dav1d_data_unref(&dav1d->
data);
305 dav1d_flush(dav1d->
c);
306 }
307
310
312 }
313
318 }
319
321 {
324 int res;
325
328
331
335 return res;
336 }
337
341 if (res < 0) {
343 return res;
344 }
345
347
348 res = dav1d_data_wrap_user_data(
data, (
const uint8_t *)
pkt,
350 if (res < 0) {
352 dav1d_data_unref(
data);
353 return res;
354 }
356 } else {
358 if (res >= 0)
360 }
361 }
362
363 res = dav1d_send_data(dav1d->
c,
data);
364 if (res < 0) {
368 dav1d_data_unref(
data);
369 return res;
370 }
371 }
372
373 res = dav1d_get_picture(dav1d->
c, p);
374 if (res < 0) {
376 dav1d_data_unref(
data);
378 }
else if (res ==
AVERROR(EAGAIN))
380 }
381
382 return res;
383 }
384
386 {
388 Dav1dPicture pic = { 0 }, *p = &pic;
390 #if FF_DAV1D_VERSION_AT_LEAST(5,1)
391 enum Dav1dEventFlags event_flags = 0;
392 #endif
393 int res;
394
395 do {
397 } while (res > 0);
398
399 if (res < 0)
400 return res;
401
403
404 // This requires the custom allocator above
406 if (!
frame->buf[0]) {
407 dav1d_picture_unref(p);
409 }
410
411 frame->data[0] = p->data[0];
412 frame->data[1] = p->data[1];
413 frame->data[2] = p->data[2];
414 frame->linesize[0] = p->stride[0];
415 frame->linesize[1] = p->stride[1];
416 frame->linesize[2] = p->stride[1];
417
418 #if FF_DAV1D_VERSION_AT_LEAST(5,1)
419 dav1d_get_event_flags(dav1d->
c, &event_flags);
421 event_flags & DAV1D_EVENT_FLAG_NEW_SEQUENCE)
422 #endif
425 if (res < 0)
427
428 frame->width = p->p.w;
429 frame->height = p->p.h;
430 if (
c->width != p->p.w ||
c->height != p->p.h) {
432 if (res < 0)
434 }
435
437 &
frame->sample_aspect_ratio.den,
440 INT_MAX);
442
444
445 // match timestamps and packet size
447 if (res < 0)
449
451 if (p->frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY)
453 else
455
456 switch (p->frame_hdr->frame_type) {
457 case DAV1D_FRAME_TYPE_KEY:
458 case DAV1D_FRAME_TYPE_INTRA:
460 break;
461 case DAV1D_FRAME_TYPE_INTER:
463 break;
464 case DAV1D_FRAME_TYPE_SWITCH:
466 break;
467 default:
470 }
471
472 if (p->mastering_display) {
474
476 if (res < 0)
478
479 if (mastering) {
480 for (
int i = 0;
i < 3;
i++) {
483 }
486
489
492 }
493 }
494 if (p->content_light) {
496
498 if (res < 0)
500
501 if (light) {
502 light->
MaxCLL = p->content_light->max_content_light_level;
503 light->
MaxFALL = p->content_light->max_frame_average_light_level;
504 }
505 }
506 if (p->itut_t35) {
507 #if FF_DAV1D_VERSION_AT_LEAST(6,9)
508 for (
size_t i = 0;
i < p->n_itut_t35;
i++) {
509 const Dav1dITUTT35 *itut_t35 = &p->itut_t35[
i];
510 #else
511 const Dav1dITUTT35 *itut_t35 = p->itut_t35;
512 #endif
514 int provider_code;
515
517
518 provider_code = bytestream2_get_be16(&gb);
519 switch (provider_code) {
521 uint32_t user_identifier = bytestream2_get_be32(&gb);
522 switch (user_identifier) {
523 case MKBETAG(
'G',
'A',
'9',
'4'): {
// closed captions
525
527 if (res < 0)
529 if (!res)
530 break;
531
533 if (res < 0)
535
537 break;
538 }
539 default: // ignore unsupported identifiers
540 break;
541 }
542 break;
543 }
546 int provider_oriented_code = bytestream2_get_be16(&gb);
547 int application_identifier = bytestream2_get_byte(&gb);
548
550 provider_oriented_code != 1 || application_identifier != 4)
551 break;
552
554 if (!hdrplus) {
557 }
558
561 if (res < 0)
563 break;
564 }
566 int provider_oriented_code = bytestream2_get_be32(&gb);
568 provider_oriented_code != 0x800)
569 break;
570
573 if (res < 0) {
575 break; // ignore
576 }
577
579 if (res < 0)
581 break;
582 }
583 default: // ignore unsupported provider codes
584 break;
585 }
586 #if FF_DAV1D_VERSION_AT_LEAST(6,9)
587 }
588 #endif
589 }
590 if (p->frame_hdr->film_grain.present && (!dav1d->
apply_grain ||
595 if (!fgp) {
598 }
599
601 fgp->
seed = p->frame_hdr->film_grain.data.seed;
618
631 memcpy(&fgp->
codec.
aom.
uv_mult, &p->frame_hdr->film_grain.data.uv_mult,
637 }
638
639 res = 0;
641 dav1d_picture_unref(p);
642 if (res < 0)
644 return res;
645 }
646
648 {
650
653 dav1d_data_unref(&dav1d->
data);
654 dav1d_close(&dav1d->
c);
655
656 return 0;
657 }
658
659 #ifndef DAV1D_MAX_FRAME_THREADS
660 #define DAV1D_MAX_FRAME_THREADS DAV1D_MAX_THREADS
661 #endif
662 #ifndef DAV1D_MAX_TILE_THREADS
663 #define DAV1D_MAX_TILE_THREADS DAV1D_MAX_THREADS
664 #endif
665 #ifndef DAV1D_MAX_FRAME_DELAY
666 #define DAV1D_MAX_FRAME_DELAY DAV1D_MAX_FRAME_THREADS
667 #endif
668
669 #define OFFSET(x) offsetof(Libdav1dContext, x)
670 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
676 {
"oppoint",
"Select an operating point of the scalable bitstream",
OFFSET(operating_point),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 31,
VD },
679 };
680
686 };
687
689 .
p.
name =
"libdav1d",
702 .p.wrapper_name = "libdav1d",
703 };