diff mbox

[FFmpeg-devel,v3] avcodec: libdav1d AV1 decoder wrapper.

Message ID 20181019234247.8284-1-jamrial@gmail.com
State Accepted
Headers show

Commit Message

James Almer Oct. 19, 2018, 11:42 p.m. UTC
Originally written by Ronald S. Bultje, with fixes, optimizations and
improvements by James Almer.

Signed-off-by: James Almer <jamrial@gmail.com>
---
Updated to work with libdav1d git head.

 configure              |   4 +
 libavcodec/Makefile    |   1 +
 libavcodec/allcodecs.c |   1 +
 libavcodec/libdav1d.c  | 271 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 277 insertions(+)
 create mode 100644 libavcodec/libdav1d.c

Comments

Dale Curtis Oct. 23, 2018, 11:15 p.m. UTC | #1
On Fri, Oct 19, 2018 at 4:50 PM James Almer <jamrial@gmail.com> wrote:

> +    s.n_tile_threads = dav1d->tile_threads;
> +    s.n_frame_threads = dav1d->frame_threads;
>

Did you consider using the AVCodecContext.threads value along
AVCodecContext.thread_type flags? That seems to be how this is handled
elsewhere.

- dale
James Almer Oct. 23, 2018, 11:42 p.m. UTC | #2
On 10/23/2018 8:15 PM, Dale Curtis wrote:
> On Fri, Oct 19, 2018 at 4:50 PM James Almer <jamrial@gmail.com> wrote:
> 
>> +    s.n_tile_threads = dav1d->tile_threads;
>> +    s.n_frame_threads = dav1d->frame_threads;
>>
> 
> Did you consider using the AVCodecContext.threads value along
> AVCodecContext.thread_type flags? That seems to be how this is handled
> elsewhere.
> 
> - dale

I did, but with the library offering options for both tiles or frames i
wasn't sure if choosing one or the other was a good idea.
I could map avctx->thread_count to n_frame_threads and leave the other
as a decoder specific option, i guess, but with the current API there's
no single optimal way for auto threads.
Rostislav Pehlivanov Oct. 24, 2018, 10:54 a.m. UTC | #3
On Sat, 20 Oct 2018 at 00:50, James Almer <jamrial@gmail.com> wrote:

> Originally written by Ronald S. Bultje, with fixes, optimizations and
> improvements by James Almer.
>
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
> Updated to work with libdav1d git head.
>
>  configure              |   4 +
>  libavcodec/Makefile    |   1 +
>  libavcodec/allcodecs.c |   1 +
>  libavcodec/libdav1d.c  | 271 +++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 277 insertions(+)
>  create mode 100644 libavcodec/libdav1d.c
>

There hasn't even been a 0.1 release yet, and there won't be one that soon.
As far as I know the promise is for that to be out by the 30th next month.
Also I'm still against this making it into the next release.
Thierry Foucu Oct. 26, 2018, 8:45 p.m. UTC | #4
On Wed, Oct 24, 2018 at 4:02 AM Rostislav Pehlivanov <atomnuker@gmail.com>
wrote:

> On Sat, 20 Oct 2018 at 00:50, James Almer <jamrial@gmail.com> wrote:
>
> > Originally written by Ronald S. Bultje, with fixes, optimizations and
> > improvements by James Almer.
> >
> > Signed-off-by: James Almer <jamrial@gmail.com>
> > ---
> > Updated to work with libdav1d git head.
> >
> >  configure              |   4 +
> >  libavcodec/Makefile    |   1 +
> >  libavcodec/allcodecs.c |   1 +
> >  libavcodec/libdav1d.c  | 271 +++++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 277 insertions(+)
> >  create mode 100644 libavcodec/libdav1d.c
> >
>
> There hasn't even been a 0.1 release yet, and there won't be one that soon.
> As far as I know the promise is for that to be out by the 30th next month.
> Also I'm still against this making it into the next release.
>

Why should we wait for a 0.1 release?
If we want to benchmark it against libaom, this is good to have in ffmpeg.
It does not have to be the default decoder...


> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
Baptiste Coudurier Oct. 26, 2018, 9:13 p.m. UTC | #5
Hey guys,

On Tue, Oct 23, 2018 at 4:42 PM James Almer <jamrial@gmail.com> wrote:

> On 10/23/2018 8:15 PM, Dale Curtis wrote:
> > On Fri, Oct 19, 2018 at 4:50 PM James Almer <jamrial@gmail.com> wrote:
> >
> >> +    s.n_tile_threads = dav1d->tile_threads;
> >> +    s.n_frame_threads = dav1d->frame_threads;
> >>
> >
> > Did you consider using the AVCodecContext.threads value along
> > AVCodecContext.thread_type flags? That seems to be how this is handled
> > elsewhere.
> >
> > - dale
>
> I did, but with the library offering options for both tiles or frames i
> wasn't sure if choosing one or the other was a good idea.
> I could map avctx->thread_count to n_frame_threads and leave the other
> as a decoder specific option, i guess, but with the current API there's
> no single optimal way for auto threads.


Agree. We should map them. Can we add frame type tiles ?
Hendrik Leppkes Oct. 26, 2018, 9:51 p.m. UTC | #6
On Fri, Oct 26, 2018 at 10:46 PM Thierry Foucu <tfoucu@gmail.com> wrote:
>
> On Wed, Oct 24, 2018 at 4:02 AM Rostislav Pehlivanov <atomnuker@gmail.com>
> wrote:
>
> > On Sat, 20 Oct 2018 at 00:50, James Almer <jamrial@gmail.com> wrote:
> >
> > > Originally written by Ronald S. Bultje, with fixes, optimizations and
> > > improvements by James Almer.
> > >
> > > Signed-off-by: James Almer <jamrial@gmail.com>
> > > ---
> > > Updated to work with libdav1d git head.
> > >
> > >  configure              |   4 +
> > >  libavcodec/Makefile    |   1 +
> > >  libavcodec/allcodecs.c |   1 +
> > >  libavcodec/libdav1d.c  | 271 +++++++++++++++++++++++++++++++++++++++++
> > >  4 files changed, 277 insertions(+)
> > >  create mode 100644 libavcodec/libdav1d.c
> > >
> >
> > There hasn't even been a 0.1 release yet, and there won't be one that soon.
> > As far as I know the promise is for that to be out by the 30th next month.
> > Also I'm still against this making it into the next release.
> >
>
> Why should we wait for a 0.1 release?
> If we want to benchmark it against libaom, this is good to have in ffmpeg.
> It does not have to be the default decoder...
>

If there is no guarantees whatsoever for API/ABI stability yet, adding
a wrapper already seems a bit early.

- Hendrik
Dale Curtis Oct. 26, 2018, 10:31 p.m. UTC | #7
The following warnings show up when compiling with clang:
../../third_party/ffmpeg/libavcodec/libdav1d.c:92:24: error: suggest braces
around initialization of subobject [-Werror,-Wmissing-braces]
    Dav1dPicture p = { 0 };
                       ^
                       {}
../../third_party/ffmpeg/libavcodec/libdav1d.c:119:24: error: suggest
braces around initialization of subobject [-Werror,-Wmissing-braces]
    Dav1dPicture p = { 0 };
                       ^
                       {}
../../third_party/ffmpeg/libavcodec/libdav1d.c:194:45: error: implicit
conversion from enumeration type 'enum Dav1dMatrixCoefficients' to
different enumeration type 'enum AVColorSpace' [-Werror,-Wenum-conversion]
    frame->colorspace = c->colorspace = p.p.mtrx;
                                      ~ ~~~~^~~~
../../third_party/ffmpeg/libavcodec/libdav1d.c:195:55: error: implicit
conversion from enumeration type 'enum Dav1dColorPrimaries' to different
enumeration type 'enum AVColorPrimaries' [-Werror,-Wenum-conversion]
    frame->color_primaries = c->color_primaries = p.p.pri;
                                                ~ ~~~~^~~
../../third_party/ffmpeg/libavcodec/libdav1d.c:196:43: error: implicit
conversion from enumeration type 'enum Dav1dTransferCharacteristics' to
different enumeration type 'enum AVColorTransferCharacteristic'
[-Werror,-Wenum-conversion]
    frame->color_trc = c->color_trc = p.p.trc;
                                    ~ ~~~~^~~
5 errors generated.



On Fri, Oct 26, 2018 at 2:59 PM Hendrik Leppkes <h.leppkes@gmail.com> wrote:

> On Fri, Oct 26, 2018 at 10:46 PM Thierry Foucu <tfoucu@gmail.com> wrote:
> >
> > On Wed, Oct 24, 2018 at 4:02 AM Rostislav Pehlivanov <
> atomnuker@gmail.com>
> > wrote:
> >
> > > On Sat, 20 Oct 2018 at 00:50, James Almer <jamrial@gmail.com> wrote:
> > >
> > > > Originally written by Ronald S. Bultje, with fixes, optimizations and
> > > > improvements by James Almer.
> > > >
> > > > Signed-off-by: James Almer <jamrial@gmail.com>
> > > > ---
> > > > Updated to work with libdav1d git head.
> > > >
> > > >  configure              |   4 +
> > > >  libavcodec/Makefile    |   1 +
> > > >  libavcodec/allcodecs.c |   1 +
> > > >  libavcodec/libdav1d.c  | 271
> +++++++++++++++++++++++++++++++++++++++++
> > > >  4 files changed, 277 insertions(+)
> > > >  create mode 100644 libavcodec/libdav1d.c
> > > >
> > >
> > > There hasn't even been a 0.1 release yet, and there won't be one that
> soon.
> > > As far as I know the promise is for that to be out by the 30th next
> month.
> > > Also I'm still against this making it into the next release.
> > >
> >
> > Why should we wait for a 0.1 release?
> > If we want to benchmark it against libaom, this is good to have in
> ffmpeg.
> > It does not have to be the default decoder...
> >
>
> If there is no guarantees whatsoever for API/ABI stability yet, adding
> a wrapper already seems a bit early.
>
> - Hendrik
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
Dale Curtis Oct. 26, 2018, 10:50 p.m. UTC | #8
One more piece of feedback, this is not obeying the
AVCodecContext.get_buffer2 API.

- dale

On Fri, Oct 26, 2018 at 3:31 PM Dale Curtis <dalecurtis@chromium.org> wrote:

> The following warnings show up when compiling with clang:
> ../../third_party/ffmpeg/libavcodec/libdav1d.c:92:24: error: suggest
> braces around initialization of subobject [-Werror,-Wmissing-braces]
>     Dav1dPicture p = { 0 };
>                        ^
>                        {}
> ../../third_party/ffmpeg/libavcodec/libdav1d.c:119:24: error: suggest
> braces around initialization of subobject [-Werror,-Wmissing-braces]
>     Dav1dPicture p = { 0 };
>                        ^
>                        {}
> ../../third_party/ffmpeg/libavcodec/libdav1d.c:194:45: error: implicit
> conversion from enumeration type 'enum Dav1dMatrixCoefficients' to
> different enumeration type 'enum AVColorSpace' [-Werror,-Wenum-conversion]
>     frame->colorspace = c->colorspace = p.p.mtrx;
>                                       ~ ~~~~^~~~
> ../../third_party/ffmpeg/libavcodec/libdav1d.c:195:55: error: implicit
> conversion from enumeration type 'enum Dav1dColorPrimaries' to different
> enumeration type 'enum AVColorPrimaries' [-Werror,-Wenum-conversion]
>     frame->color_primaries = c->color_primaries = p.p.pri;
>                                                 ~ ~~~~^~~
> ../../third_party/ffmpeg/libavcodec/libdav1d.c:196:43: error: implicit
> conversion from enumeration type 'enum Dav1dTransferCharacteristics' to
> different enumeration type 'enum AVColorTransferCharacteristic'
> [-Werror,-Wenum-conversion]
>     frame->color_trc = c->color_trc = p.p.trc;
>                                     ~ ~~~~^~~
> 5 errors generated.
>
>
>
> On Fri, Oct 26, 2018 at 2:59 PM Hendrik Leppkes <h.leppkes@gmail.com>
> wrote:
>
>> On Fri, Oct 26, 2018 at 10:46 PM Thierry Foucu <tfoucu@gmail.com> wrote:
>> >
>> > On Wed, Oct 24, 2018 at 4:02 AM Rostislav Pehlivanov <
>> atomnuker@gmail.com>
>> > wrote:
>> >
>> > > On Sat, 20 Oct 2018 at 00:50, James Almer <jamrial@gmail.com> wrote:
>> > >
>> > > > Originally written by Ronald S. Bultje, with fixes, optimizations
>> and
>> > > > improvements by James Almer.
>> > > >
>> > > > Signed-off-by: James Almer <jamrial@gmail.com>
>> > > > ---
>> > > > Updated to work with libdav1d git head.
>> > > >
>> > > >  configure              |   4 +
>> > > >  libavcodec/Makefile    |   1 +
>> > > >  libavcodec/allcodecs.c |   1 +
>> > > >  libavcodec/libdav1d.c  | 271
>> +++++++++++++++++++++++++++++++++++++++++
>> > > >  4 files changed, 277 insertions(+)
>> > > >  create mode 100644 libavcodec/libdav1d.c
>> > > >
>> > >
>> > > There hasn't even been a 0.1 release yet, and there won't be one that
>> soon.
>> > > As far as I know the promise is for that to be out by the 30th next
>> month.
>> > > Also I'm still against this making it into the next release.
>> > >
>> >
>> > Why should we wait for a 0.1 release?
>> > If we want to benchmark it against libaom, this is good to have in
>> ffmpeg.
>> > It does not have to be the default decoder...
>> >
>>
>> If there is no guarantees whatsoever for API/ABI stability yet, adding
>> a wrapper already seems a bit early.
>>
>> - Hendrik
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel@ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>
>
James Almer Oct. 26, 2018, 11 p.m. UTC | #9
On 10/26/2018 7:50 PM, Dale Curtis wrote:
> One more piece of feedback, this is not obeying the
> AVCodecContext.get_buffer2 API.

It's not using it on purpose, wrapping the buffers dav1d allocated
itself instead. Hence the lack of AV_CODEC_CAP_DR1 flag.

> 
> - dale
> 
> On Fri, Oct 26, 2018 at 3:31 PM Dale Curtis <dalecurtis@chromium.org> wrote:
> 
>> The following warnings show up when compiling with clang:
>> ../../third_party/ffmpeg/libavcodec/libdav1d.c:92:24: error: suggest
>> braces around initialization of subobject [-Werror,-Wmissing-braces]
>>     Dav1dPicture p = { 0 };
>>                        ^
>>                        {}
>> ../../third_party/ffmpeg/libavcodec/libdav1d.c:119:24: error: suggest
>> braces around initialization of subobject [-Werror,-Wmissing-braces]
>>     Dav1dPicture p = { 0 };
>>                        ^
>>                        {}
>> ../../third_party/ffmpeg/libavcodec/libdav1d.c:194:45: error: implicit
>> conversion from enumeration type 'enum Dav1dMatrixCoefficients' to
>> different enumeration type 'enum AVColorSpace' [-Werror,-Wenum-conversion]
>>     frame->colorspace = c->colorspace = p.p.mtrx;
>>                                       ~ ~~~~^~~~
>> ../../third_party/ffmpeg/libavcodec/libdav1d.c:195:55: error: implicit
>> conversion from enumeration type 'enum Dav1dColorPrimaries' to different
>> enumeration type 'enum AVColorPrimaries' [-Werror,-Wenum-conversion]
>>     frame->color_primaries = c->color_primaries = p.p.pri;
>>                                                 ~ ~~~~^~~
>> ../../third_party/ffmpeg/libavcodec/libdav1d.c:196:43: error: implicit
>> conversion from enumeration type 'enum Dav1dTransferCharacteristics' to
>> different enumeration type 'enum AVColorTransferCharacteristic'
>> [-Werror,-Wenum-conversion]
>>     frame->color_trc = c->color_trc = p.p.trc;
>>                                     ~ ~~~~^~~
>> 5 errors generated.
>>
>>
>>
>> On Fri, Oct 26, 2018 at 2:59 PM Hendrik Leppkes <h.leppkes@gmail.com>
>> wrote:
>>
>>> On Fri, Oct 26, 2018 at 10:46 PM Thierry Foucu <tfoucu@gmail.com> wrote:
>>>>
>>>> On Wed, Oct 24, 2018 at 4:02 AM Rostislav Pehlivanov <
>>> atomnuker@gmail.com>
>>>> wrote:
>>>>
>>>>> On Sat, 20 Oct 2018 at 00:50, James Almer <jamrial@gmail.com> wrote:
>>>>>
>>>>>> Originally written by Ronald S. Bultje, with fixes, optimizations
>>> and
>>>>>> improvements by James Almer.
>>>>>>
>>>>>> Signed-off-by: James Almer <jamrial@gmail.com>
>>>>>> ---
>>>>>> Updated to work with libdav1d git head.
>>>>>>
>>>>>>  configure              |   4 +
>>>>>>  libavcodec/Makefile    |   1 +
>>>>>>  libavcodec/allcodecs.c |   1 +
>>>>>>  libavcodec/libdav1d.c  | 271
>>> +++++++++++++++++++++++++++++++++++++++++
>>>>>>  4 files changed, 277 insertions(+)
>>>>>>  create mode 100644 libavcodec/libdav1d.c
>>>>>>
>>>>>
>>>>> There hasn't even been a 0.1 release yet, and there won't be one that
>>> soon.
>>>>> As far as I know the promise is for that to be out by the 30th next
>>> month.
>>>>> Also I'm still against this making it into the next release.
>>>>>
>>>>
>>>> Why should we wait for a 0.1 release?
>>>> If we want to benchmark it against libaom, this is good to have in
>>> ffmpeg.
>>>> It does not have to be the default decoder...
>>>>
>>>
>>> If there is no guarantees whatsoever for API/ABI stability yet, adding
>>> a wrapper already seems a bit early.
>>>
>>> - Hendrik
>>> _______________________________________________
>>> ffmpeg-devel mailing list
>>> ffmpeg-devel@ffmpeg.org
>>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>>
>>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
James Almer Oct. 26, 2018, 11:05 p.m. UTC | #10
On 10/26/2018 7:31 PM, Dale Curtis wrote:
> The following warnings show up when compiling with clang:
> ../../third_party/ffmpeg/libavcodec/libdav1d.c:92:24: error: suggest braces
> around initialization of subobject [-Werror,-Wmissing-braces]
>     Dav1dPicture p = { 0 };
>                        ^
>                        {}

Does "Dav1dPicture p = { .ref = opaque };" silences this one up?

> ../../third_party/ffmpeg/libavcodec/libdav1d.c:119:24: error: suggest
> braces around initialization of subobject [-Werror,-Wmissing-braces]
>     Dav1dPicture p = { 0 };
>                        ^
>                        {}

For this one I'd have to use { { 0 } } or a memset, but maybe the struct
could start with an int or something else instead of the data pointer
arrays.

> ../../third_party/ffmpeg/libavcodec/libdav1d.c:194:45: error: implicit
> conversion from enumeration type 'enum Dav1dMatrixCoefficients' to
> different enumeration type 'enum AVColorSpace' [-Werror,-Wenum-conversion]
>     frame->colorspace = c->colorspace = p.p.mtrx;
>                                       ~ ~~~~^~~~
> ../../third_party/ffmpeg/libavcodec/libdav1d.c:195:55: error: implicit
> conversion from enumeration type 'enum Dav1dColorPrimaries' to different
> enumeration type 'enum AVColorPrimaries' [-Werror,-Wenum-conversion]
>     frame->color_primaries = c->color_primaries = p.p.pri;
>                                                 ~ ~~~~^~~
> ../../third_party/ffmpeg/libavcodec/libdav1d.c:196:43: error: implicit
> conversion from enumeration type 'enum Dav1dTransferCharacteristics' to
> different enumeration type 'enum AVColorTransferCharacteristic'
> [-Werror,-Wenum-conversion]
>     frame->color_trc = c->color_trc = p.p.trc;
>                                     ~ ~~~~^~~

I can cast the enums to silence these errors, sure.

> 5 errors generated.

5 very pedantic "errors" :p
Dale Curtis Oct. 26, 2018, 11:55 p.m. UTC | #11
On Fri, Oct 26, 2018 at 4:00 PM James Almer <jamrial@gmail.com> wrote:

> On 10/26/2018 7:50 PM, Dale Curtis wrote:
> > One more piece of feedback, this is not obeying the
> > AVCodecContext.get_buffer2 API.
>
> It's not using it on purpose, wrapping the buffers dav1d allocated
> itself instead. Hence the lack of AV_CODEC_CAP_DR1 flag.
>

Sorry for being unclear, my comment was a request. This is something
Chromium would require to use this wrapper. If it's not planned, we'd forgo
the wrapper and use dav1d directly. Otherwise we'd need to change our
common ffmpeg based decoder enough for this one case that we'd be better
off writing a pure dav1d based decoder. Thanks for your consideration in
any case.

- dale
Dale Curtis Oct. 26, 2018, 11:56 p.m. UTC | #12
Yes, all your suggestions silence the errors. Thanks!

- dale

On Fri, Oct 26, 2018 at 4:05 PM James Almer <jamrial@gmail.com> wrote:

> On 10/26/2018 7:31 PM, Dale Curtis wrote:
> > The following warnings show up when compiling with clang:
> > ../../third_party/ffmpeg/libavcodec/libdav1d.c:92:24: error: suggest
> braces
> > around initialization of subobject [-Werror,-Wmissing-braces]
> >     Dav1dPicture p = { 0 };
> >                        ^
> >                        {}
>
> Does "Dav1dPicture p = { .ref = opaque };" silences this one up?
>
> > ../../third_party/ffmpeg/libavcodec/libdav1d.c:119:24: error: suggest
> > braces around initialization of subobject [-Werror,-Wmissing-braces]
> >     Dav1dPicture p = { 0 };
> >                        ^
> >                        {}
>
> For this one I'd have to use { { 0 } } or a memset, but maybe the struct
> could start with an int or something else instead of the data pointer
> arrays.
>
> > ../../third_party/ffmpeg/libavcodec/libdav1d.c:194:45: error: implicit
> > conversion from enumeration type 'enum Dav1dMatrixCoefficients' to
> > different enumeration type 'enum AVColorSpace'
> [-Werror,-Wenum-conversion]
> >     frame->colorspace = c->colorspace = p.p.mtrx;
> >                                       ~ ~~~~^~~~
> > ../../third_party/ffmpeg/libavcodec/libdav1d.c:195:55: error: implicit
> > conversion from enumeration type 'enum Dav1dColorPrimaries' to different
> > enumeration type 'enum AVColorPrimaries' [-Werror,-Wenum-conversion]
> >     frame->color_primaries = c->color_primaries = p.p.pri;
> >                                                 ~ ~~~~^~~
> > ../../third_party/ffmpeg/libavcodec/libdav1d.c:196:43: error: implicit
> > conversion from enumeration type 'enum Dav1dTransferCharacteristics' to
> > different enumeration type 'enum AVColorTransferCharacteristic'
> > [-Werror,-Wenum-conversion]
> >     frame->color_trc = c->color_trc = p.p.trc;
> >                                     ~ ~~~~^~~
>
> I can cast the enums to silence these errors, sure.
>
> > 5 errors generated.
>
> 5 very pedantic "errors" :p
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
diff mbox

Patch

diff --git a/configure b/configure
index 85d5dd5962..99e1a6b76b 100755
--- a/configure
+++ b/configure
@@ -226,6 +226,7 @@  External library support:
   --enable-libcelt         enable CELT decoding via libcelt [no]
   --enable-libcdio         enable audio CD grabbing with libcdio [no]
   --enable-libcodec2       enable codec2 en/decoding using libcodec2 [no]
+  --enable-libdav1d        enable AV1 decoding via libdav1d [no]
   --enable-libdavs2        enable AVS2 decoding via libdavs2 [no]
   --enable-libdc1394       enable IIDC-1394 grabbing using libdc1394
                            and libraw1394 [no]
@@ -1712,6 +1713,7 @@  EXTERNAL_LIBRARY_LIST="
     libcaca
     libcelt
     libcodec2
+    libdav1d
     libdc1394
     libdrm
     libflite
@@ -3088,6 +3090,7 @@  libaom_av1_encoder_select="extract_extradata_bsf"
 libcelt_decoder_deps="libcelt"
 libcodec2_decoder_deps="libcodec2"
 libcodec2_encoder_deps="libcodec2"
+libdav1d_decoder_deps="libdav1d"
 libdavs2_decoder_deps="libdavs2"
 libfdk_aac_decoder_deps="libfdk_aac"
 libfdk_aac_encoder_deps="libfdk_aac"
@@ -6063,6 +6066,7 @@  enabled libcelt           && require libcelt celt/celt.h celt_decode -lcelt0 &&
                                die "ERROR: libcelt must be installed and version must be >= 0.11.0."; }
 enabled libcaca           && require_pkg_config libcaca caca caca.h caca_create_canvas
 enabled libcodec2         && require libcodec2 codec2/codec2.h codec2_create -lcodec2
+enabled libdav1d          && require_pkg_config libdav1d "dav1d >= 0.0.1" "dav1d/dav1d.h" dav1d_version
 enabled libdavs2          && require_pkg_config libdavs2 "davs2 >= 1.5.115" davs2.h davs2_decoder_open
 enabled libdc1394         && require_pkg_config libdc1394 libdc1394-2 dc1394/dc1394.h dc1394_new
 enabled libdrm            && require_pkg_config libdrm libdrm xf86drm.h drmGetVersion
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index a97055ef3f..0f1e93d478 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -954,6 +954,7 @@  OBJS-$(CONFIG_LIBAOM_AV1_ENCODER)         += libaomenc.o
 OBJS-$(CONFIG_LIBCELT_DECODER)            += libcelt_dec.o
 OBJS-$(CONFIG_LIBCODEC2_DECODER)          += libcodec2.o codec2utils.o
 OBJS-$(CONFIG_LIBCODEC2_ENCODER)          += libcodec2.o codec2utils.o
+OBJS-$(CONFIG_LIBDAV1D_DECODER)           += libdav1d.o
 OBJS-$(CONFIG_LIBDAVS2_DECODER)           += libdavs2.o
 OBJS-$(CONFIG_LIBFDK_AAC_DECODER)         += libfdk-aacdec.o
 OBJS-$(CONFIG_LIBFDK_AAC_ENCODER)         += libfdk-aacenc.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index c0b4d56d0d..d2628df620 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -674,6 +674,7 @@  extern AVCodec ff_libaom_av1_encoder;
 extern AVCodec ff_libcelt_decoder;
 extern AVCodec ff_libcodec2_encoder;
 extern AVCodec ff_libcodec2_decoder;
+extern AVCodec ff_libdav1d_decoder;
 extern AVCodec ff_libdavs2_decoder;
 extern AVCodec ff_libfdk_aac_encoder;
 extern AVCodec ff_libfdk_aac_decoder;
diff --git a/libavcodec/libdav1d.c b/libavcodec/libdav1d.c
new file mode 100644
index 0000000000..da826a42ed
--- /dev/null
+++ b/libavcodec/libdav1d.c
@@ -0,0 +1,271 @@ 
+/*
+ * Copyright (c) 2018 Ronald S. Bultje <rsbultje gmail com>
+ * Copyright (c) 2018 James Almer <jamrial gmail com>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <dav1d/dav1d.h>
+
+#include "libavutil/avassert.h"
+#include "libavutil/fifo.h"
+#include "libavutil/opt.h"
+
+#include "avcodec.h"
+#include "decode.h"
+#include "internal.h"
+
+typedef struct Libdav1dContext {
+    AVClass *class;
+    Dav1dContext *c;
+
+    AVFifoBuffer *cache;
+    Dav1dData data;
+    int frame_threads, tile_threads;
+} Libdav1dContext;
+
+static av_cold int libdav1d_init(AVCodecContext *c)
+{
+    Libdav1dContext *dav1d = c->priv_data;
+    Dav1dSettings s;
+    int res;
+
+    av_log(c, AV_LOG_INFO, "libdav1d %s\n", dav1d_version());
+
+    dav1d_default_settings(&s);
+    s.n_tile_threads = dav1d->tile_threads;
+    s.n_frame_threads = dav1d->frame_threads;
+
+    dav1d->cache = av_fifo_alloc(8 * sizeof(AVPacket));
+    if (!dav1d->cache)
+        return AVERROR(ENOMEM);
+
+    res = dav1d_open(&dav1d->c, &s);
+    if (res < 0)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
+static void libdav1d_flush(AVCodecContext *c)
+{
+    Libdav1dContext *dav1d = c->priv_data;
+
+    av_fifo_reset(dav1d->cache);
+    dav1d_data_unref(&dav1d->data);
+    dav1d_flush(dav1d->c);
+}
+
+static int libdav1d_fifo_write(void *src, void *dst, int dst_size) {
+    AVPacket *pkt_dst = dst, *pkt_src = src;
+
+    av_assert2(dst_size >= sizeof(AVPacket));
+
+    pkt_src->buf = NULL;
+    av_packet_free_side_data(pkt_src);
+    *pkt_dst = *pkt_src;
+
+    return sizeof(AVPacket);
+}
+
+static void libdav1d_data_free(uint8_t *data, void *opaque) {
+    AVBufferRef *buf = opaque;
+
+    av_buffer_unref(&buf);
+}
+
+static void libdav1d_frame_free(void *opaque, uint8_t *data) {
+    Dav1dPicture p = { 0 };
+
+    p.ref = opaque;
+    p.data[0] = (void *) 0x1; // this has to be non-NULL
+    dav1d_picture_unref(&p);
+}
+
+static const enum AVPixelFormat pix_fmt[][2] = {
+    [DAV1D_PIXEL_LAYOUT_I400] = { AV_PIX_FMT_GRAY8,   AV_PIX_FMT_GRAY10 },
+    [DAV1D_PIXEL_LAYOUT_I420] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10 },
+    [DAV1D_PIXEL_LAYOUT_I422] = { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10 },
+    [DAV1D_PIXEL_LAYOUT_I444] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10 },
+};
+
+// TODO: Update once 12bit support is added.
+static const int profile[] = {
+    [DAV1D_PIXEL_LAYOUT_I400] = FF_PROFILE_AV1_MAIN,
+    [DAV1D_PIXEL_LAYOUT_I420] = FF_PROFILE_AV1_MAIN,
+    [DAV1D_PIXEL_LAYOUT_I422] = FF_PROFILE_AV1_PROFESSIONAL,
+    [DAV1D_PIXEL_LAYOUT_I444] = FF_PROFILE_AV1_HIGH,
+};
+
+static int libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame)
+{
+    Libdav1dContext *dav1d = c->priv_data;
+    Dav1dData *data = &dav1d->data;
+    AVPacket pkt = { 0 };
+    Dav1dPicture p = { 0 };
+    int res;
+
+    if (!data->sz) {
+        res = ff_decode_get_packet(c, &pkt);
+        if (res < 0 && res != AVERROR_EOF)
+            return res;
+
+        if (pkt.size) {
+            if (!av_fifo_space(dav1d->cache)) {
+                res = av_fifo_grow(dav1d->cache, 8 * sizeof(pkt));
+                if (res < 0) {
+                    av_packet_unref(&pkt);
+                    return res;
+                }
+            }
+
+            res = dav1d_data_wrap(data, pkt.data, pkt.size, libdav1d_data_free, pkt.buf);
+            if (res < 0) {
+                av_packet_unref(&pkt);
+                return res;
+            }
+
+            av_fifo_generic_write(dav1d->cache, &pkt, sizeof(pkt), libdav1d_fifo_write);
+        } else {
+            data = NULL;
+        }
+    }
+
+    res = dav1d_decode(dav1d->c, data, &p);
+    if (res < 0) {
+        if (res == -EINVAL)
+            res = AVERROR_INVALIDDATA;
+        else if (res == -EAGAIN && c->internal->draining)
+            res = AVERROR_EOF;
+
+        return res;
+    }
+
+    av_assert0(p.data[0] != NULL);
+
+    av_fifo_generic_read(dav1d->cache, &pkt, sizeof(pkt), NULL);
+
+    frame->buf[0] = av_buffer_create(NULL, 0, libdav1d_frame_free,
+                                     p.ref, AV_BUFFER_FLAG_READONLY);
+    if (!frame->buf[0]) {
+        dav1d_picture_unref(&p);
+        return AVERROR(ENOMEM);
+    }
+
+    frame->data[0] = p.data[0];
+    frame->data[1] = p.data[1];
+    frame->data[2] = p.data[2];
+    frame->linesize[0] = p.stride[0];
+    frame->linesize[1] = p.stride[1];
+    frame->linesize[2] = p.stride[1];
+
+    c->profile = profile[p.p.layout];
+    frame->format = c->pix_fmt = pix_fmt[p.p.layout][p.p.bpc == 10];
+    frame->width = p.p.w;
+    frame->height = p.p.h;
+    if (c->width != p.p.w || c->height != p.p.h) {
+        res = ff_set_dimensions(c, p.p.w, p.p.h);
+        if (res < 0)
+            return res;
+    }
+
+    switch (p.p.chr) {
+    case DAV1D_CHR_VERTICAL:
+        frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_LEFT;
+        break;
+    case DAV1D_CHR_COLOCATED:
+        frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
+        break;
+    }
+    frame->colorspace = c->colorspace = p.p.mtrx;
+    frame->color_primaries = c->color_primaries = p.p.pri;
+    frame->color_trc = c->color_trc = p.p.trc;
+    frame->color_range = c->color_range = p.p.fullrange ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
+
+    // match timestamps and packet size
+    frame->pts = frame->best_effort_timestamp = pkt.pts;
+#if FF_API_PKT_PTS
+FF_DISABLE_DEPRECATION_WARNINGS
+    frame->pkt_pts = pkt.pts;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+    frame->pkt_dts = pkt.dts;
+    frame->pkt_pos = pkt.pos;
+    frame->pkt_size = pkt.size;
+    frame->pkt_duration = pkt.duration;
+    frame->key_frame = p.p.type == DAV1D_FRAME_TYPE_KEY;
+
+    switch (p.p.type) {
+    case DAV1D_FRAME_TYPE_KEY:
+    case DAV1D_FRAME_TYPE_INTRA:
+        frame->pict_type = AV_PICTURE_TYPE_I;
+        break;
+    case DAV1D_FRAME_TYPE_INTER:
+        frame->pict_type = AV_PICTURE_TYPE_P;
+        break;
+    case DAV1D_FRAME_TYPE_SWITCH:
+        frame->pict_type = AV_PICTURE_TYPE_SP;
+        break;
+    default:
+        return AVERROR_INVALIDDATA;
+    }
+
+    return 0;
+}
+
+static av_cold int libdav1d_close(AVCodecContext *c)
+{
+    Libdav1dContext *dav1d = c->priv_data;
+
+    av_fifo_freep(&dav1d->cache);
+    dav1d_data_unref(&dav1d->data);
+    dav1d_close(&dav1d->c);
+
+    return 0;
+}
+
+#define OFFSET(x) offsetof(Libdav1dContext, x)
+#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
+static const AVOption libdav1d_options[] = {
+    { "framethreads", "Frame threads", OFFSET(frame_threads), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 256, VD, NULL },
+    { "tilethreads",  "Tile threads",  OFFSET(tile_threads),  AV_OPT_TYPE_INT, { .i64 = 1 }, 1,  64, VD, NULL },
+    { NULL }
+};
+
+static const AVClass libdav1d_class = {
+    .class_name = "libdav1d decoder",
+    .item_name  = av_default_item_name,
+    .option     = libdav1d_options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVCodec ff_libdav1d_decoder = {
+    .name           = "libdav1d",
+    .long_name      = NULL_IF_CONFIG_SMALL("dav1d AV1 decoder by VideoLAN"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_AV1,
+    .priv_data_size = sizeof(Libdav1dContext),
+    .init           = libdav1d_init,
+    .close          = libdav1d_close,
+    .flush          = libdav1d_flush,
+    .receive_frame  = libdav1d_receive_frame,
+    .capabilities   = AV_CODEC_CAP_DELAY,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
+                      FF_CODEC_CAP_SETS_PKT_DTS,
+    .priv_class     = &libdav1d_class,
+    .wrapper_name   = "libdav1d",
+};