]> git.sesse.net Git - ffmpeg/blob - libavcodec/libdav1d.c
90c531fb91811bf0130f2f3f66bb22c0d2161c37
[ffmpeg] / libavcodec / libdav1d.c
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
24 #include "libavutil/avassert.h"
25 #include "libavutil/film_grain_params.h"
26 #include "libavutil/mastering_display_metadata.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/opt.h"
29
30 #include "atsc_a53.h"
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "decode.h"
34 #include "internal.h"
35
36 typedef struct Libdav1dContext {
37     AVClass *class;
38     Dav1dContext *c;
39     AVBufferPool *pool;
40     int pool_size;
41
42     Dav1dData data;
43     int tile_threads;
44     int frame_threads;
45     int apply_grain;
46     int operating_point;
47     int all_layers;
48 } Libdav1dContext;
49
50 static const enum AVPixelFormat pix_fmt[][3] = {
51     [DAV1D_PIXEL_LAYOUT_I400] = { AV_PIX_FMT_GRAY8,   AV_PIX_FMT_GRAY10,    AV_PIX_FMT_GRAY12 },
52     [DAV1D_PIXEL_LAYOUT_I420] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12 },
53     [DAV1D_PIXEL_LAYOUT_I422] = { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12 },
54     [DAV1D_PIXEL_LAYOUT_I444] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12 },
55 };
56
57 static const enum AVPixelFormat pix_fmt_rgb[3] = {
58     AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
59 };
60
61 static void libdav1d_log_callback(void *opaque, const char *fmt, va_list vl)
62 {
63     AVCodecContext *c = opaque;
64
65     av_vlog(c, AV_LOG_ERROR, fmt, vl);
66 }
67
68 static int libdav1d_picture_allocator(Dav1dPicture *p, void *cookie)
69 {
70     Libdav1dContext *dav1d = cookie;
71     enum AVPixelFormat format = pix_fmt[p->p.layout][p->seq_hdr->hbd];
72     int ret, linesize[4], h = FFALIGN(p->p.h, 128), w = FFALIGN(p->p.w, 128);
73     uint8_t *aligned_ptr, *data[4];
74     AVBufferRef *buf;
75
76     ret = av_image_get_buffer_size(format, w, h, DAV1D_PICTURE_ALIGNMENT);
77     if (ret < 0)
78         return ret;
79
80     if (ret != dav1d->pool_size) {
81         av_buffer_pool_uninit(&dav1d->pool);
82         // Use twice the amount of required padding bytes for aligned_ptr below.
83         dav1d->pool = av_buffer_pool_init(ret + DAV1D_PICTURE_ALIGNMENT * 2, NULL);
84         if (!dav1d->pool) {
85             dav1d->pool_size = 0;
86             return AVERROR(ENOMEM);
87         }
88         dav1d->pool_size = ret;
89     }
90     buf = av_buffer_pool_get(dav1d->pool);
91     if (!buf)
92         return AVERROR(ENOMEM);
93
94     // libdav1d requires DAV1D_PICTURE_ALIGNMENT aligned buffers, which av_malloc()
95     // doesn't guarantee for example when AVX is disabled at configure time.
96     // Use the extra DAV1D_PICTURE_ALIGNMENT padding bytes in the buffer to align it
97     // if required.
98     aligned_ptr = (uint8_t *)FFALIGN((uintptr_t)buf->data, DAV1D_PICTURE_ALIGNMENT);
99     ret = av_image_fill_arrays(data, linesize, aligned_ptr, format, w, h,
100                                DAV1D_PICTURE_ALIGNMENT);
101     if (ret < 0) {
102         av_buffer_unref(&buf);
103         return ret;
104     }
105
106     p->data[0] = data[0];
107     p->data[1] = data[1];
108     p->data[2] = data[2];
109     p->stride[0] = linesize[0];
110     p->stride[1] = linesize[1];
111     p->allocator_data = buf;
112
113     return 0;
114 }
115
116 static void libdav1d_picture_release(Dav1dPicture *p, void *cookie)
117 {
118     AVBufferRef *buf = p->allocator_data;
119
120     av_buffer_unref(&buf);
121 }
122
123 static av_cold int libdav1d_init(AVCodecContext *c)
124 {
125     Libdav1dContext *dav1d = c->priv_data;
126     Dav1dSettings s;
127     int threads = (c->thread_count ? c->thread_count : av_cpu_count()) * 3 / 2;
128     int res;
129
130     av_log(c, AV_LOG_INFO, "libdav1d %s\n", dav1d_version());
131
132     dav1d_default_settings(&s);
133     s.logger.cookie = c;
134     s.logger.callback = libdav1d_log_callback;
135     s.allocator.cookie = dav1d;
136     s.allocator.alloc_picture_callback = libdav1d_picture_allocator;
137     s.allocator.release_picture_callback = libdav1d_picture_release;
138     s.frame_size_limit = c->max_pixels;
139     if (dav1d->apply_grain >= 0)
140         s.apply_grain = dav1d->apply_grain;
141     else if (c->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN)
142         s.apply_grain = 0;
143
144     s.all_layers = dav1d->all_layers;
145     if (dav1d->operating_point >= 0)
146         s.operating_point = dav1d->operating_point;
147
148     s.n_tile_threads = dav1d->tile_threads
149                      ? dav1d->tile_threads
150                      : FFMIN(floor(sqrt(threads)), DAV1D_MAX_TILE_THREADS);
151     s.n_frame_threads = dav1d->frame_threads
152                       ? dav1d->frame_threads
153                       : FFMIN(ceil(threads / s.n_tile_threads), DAV1D_MAX_FRAME_THREADS);
154     av_log(c, AV_LOG_DEBUG, "Using %d frame threads, %d tile threads\n",
155            s.n_frame_threads, s.n_tile_threads);
156
157     res = dav1d_open(&dav1d->c, &s);
158     if (res < 0)
159         return AVERROR(ENOMEM);
160
161     return 0;
162 }
163
164 static void libdav1d_flush(AVCodecContext *c)
165 {
166     Libdav1dContext *dav1d = c->priv_data;
167
168     dav1d_data_unref(&dav1d->data);
169     dav1d_flush(dav1d->c);
170 }
171
172 static void libdav1d_data_free(const uint8_t *data, void *opaque) {
173     AVBufferRef *buf = opaque;
174
175     av_buffer_unref(&buf);
176 }
177
178 static void libdav1d_user_data_free(const uint8_t *data, void *opaque) {
179     av_assert0(data == opaque);
180     av_free(opaque);
181 }
182
183 static int libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame)
184 {
185     Libdav1dContext *dav1d = c->priv_data;
186     Dav1dData *data = &dav1d->data;
187     Dav1dPicture pic = { 0 }, *p = &pic;
188     int res;
189
190     if (!data->sz) {
191         AVPacket pkt = { 0 };
192
193         res = ff_decode_get_packet(c, &pkt);
194         if (res < 0 && res != AVERROR_EOF)
195             return res;
196
197         if (pkt.size) {
198             res = dav1d_data_wrap(data, pkt.data, pkt.size, libdav1d_data_free, pkt.buf);
199             if (res < 0) {
200                 av_packet_unref(&pkt);
201                 return res;
202             }
203
204             data->m.timestamp = pkt.pts;
205             data->m.offset = pkt.pos;
206             data->m.duration = pkt.duration;
207
208             pkt.buf = NULL;
209             av_packet_unref(&pkt);
210
211             if (c->reordered_opaque != AV_NOPTS_VALUE) {
212                 uint8_t *reordered_opaque = av_malloc(sizeof(c->reordered_opaque));
213                 if (!reordered_opaque) {
214                     dav1d_data_unref(data);
215                     return AVERROR(ENOMEM);
216                 }
217
218                 memcpy(reordered_opaque, &c->reordered_opaque, sizeof(c->reordered_opaque));
219                 res = dav1d_data_wrap_user_data(data, reordered_opaque,
220                                                 libdav1d_user_data_free, reordered_opaque);
221                 if (res < 0) {
222                     av_free(reordered_opaque);
223                     dav1d_data_unref(data);
224                     return res;
225                 }
226             }
227         }
228     }
229
230     res = dav1d_send_data(dav1d->c, data);
231     if (res < 0) {
232         if (res == AVERROR(EINVAL))
233             res = AVERROR_INVALIDDATA;
234         if (res != AVERROR(EAGAIN))
235             return res;
236     }
237
238     res = dav1d_get_picture(dav1d->c, p);
239     if (res < 0) {
240         if (res == AVERROR(EINVAL))
241             res = AVERROR_INVALIDDATA;
242         else if (res == AVERROR(EAGAIN) && c->internal->draining)
243             res = AVERROR_EOF;
244
245         return res;
246     }
247
248     av_assert0(p->data[0] && p->allocator_data);
249
250     // This requires the custom allocator above
251     frame->buf[0] = av_buffer_ref(p->allocator_data);
252     if (!frame->buf[0]) {
253         dav1d_picture_unref(p);
254         return AVERROR(ENOMEM);
255     }
256
257     frame->data[0] = p->data[0];
258     frame->data[1] = p->data[1];
259     frame->data[2] = p->data[2];
260     frame->linesize[0] = p->stride[0];
261     frame->linesize[1] = p->stride[1];
262     frame->linesize[2] = p->stride[1];
263
264     c->profile = p->seq_hdr->profile;
265     c->level = ((p->seq_hdr->operating_points[0].major_level - 2) << 2)
266                | p->seq_hdr->operating_points[0].minor_level;
267     frame->width = p->p.w;
268     frame->height = p->p.h;
269     if (c->width != p->p.w || c->height != p->p.h) {
270         res = ff_set_dimensions(c, p->p.w, p->p.h);
271         if (res < 0)
272             goto fail;
273     }
274
275     av_reduce(&frame->sample_aspect_ratio.num,
276               &frame->sample_aspect_ratio.den,
277               frame->height * (int64_t)p->frame_hdr->render_width,
278               frame->width  * (int64_t)p->frame_hdr->render_height,
279               INT_MAX);
280     ff_set_sar(c, frame->sample_aspect_ratio);
281
282     switch (p->seq_hdr->chr) {
283     case DAV1D_CHR_VERTICAL:
284         frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_LEFT;
285         break;
286     case DAV1D_CHR_COLOCATED:
287         frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
288         break;
289     }
290     frame->colorspace = c->colorspace = (enum AVColorSpace) p->seq_hdr->mtrx;
291     frame->color_primaries = c->color_primaries = (enum AVColorPrimaries) p->seq_hdr->pri;
292     frame->color_trc = c->color_trc = (enum AVColorTransferCharacteristic) p->seq_hdr->trc;
293     frame->color_range = c->color_range = p->seq_hdr->color_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
294
295     if (p->p.layout == DAV1D_PIXEL_LAYOUT_I444 &&
296         p->seq_hdr->mtrx == DAV1D_MC_IDENTITY &&
297         p->seq_hdr->pri  == DAV1D_COLOR_PRI_BT709 &&
298         p->seq_hdr->trc  == DAV1D_TRC_SRGB)
299         frame->format = c->pix_fmt = pix_fmt_rgb[p->seq_hdr->hbd];
300     else
301         frame->format = c->pix_fmt = pix_fmt[p->p.layout][p->seq_hdr->hbd];
302
303     if (p->m.user_data.data)
304         memcpy(&frame->reordered_opaque, p->m.user_data.data, sizeof(frame->reordered_opaque));
305     else
306         frame->reordered_opaque = AV_NOPTS_VALUE;
307
308     if (p->seq_hdr->num_units_in_tick && p->seq_hdr->time_scale) {
309         av_reduce(&c->framerate.den, &c->framerate.num,
310                   p->seq_hdr->num_units_in_tick, p->seq_hdr->time_scale, INT_MAX);
311         if (p->seq_hdr->equal_picture_interval)
312             c->ticks_per_frame = p->seq_hdr->num_ticks_per_picture;
313     }
314
315     // match timestamps and packet size
316     frame->pts = p->m.timestamp;
317     frame->pkt_dts = p->m.timestamp;
318     frame->pkt_pos = p->m.offset;
319     frame->pkt_size = p->m.size;
320     frame->pkt_duration = p->m.duration;
321     frame->key_frame = p->frame_hdr->frame_type == DAV1D_FRAME_TYPE_KEY;
322
323     switch (p->frame_hdr->frame_type) {
324     case DAV1D_FRAME_TYPE_KEY:
325     case DAV1D_FRAME_TYPE_INTRA:
326         frame->pict_type = AV_PICTURE_TYPE_I;
327         break;
328     case DAV1D_FRAME_TYPE_INTER:
329         frame->pict_type = AV_PICTURE_TYPE_P;
330         break;
331     case DAV1D_FRAME_TYPE_SWITCH:
332         frame->pict_type = AV_PICTURE_TYPE_SP;
333         break;
334     default:
335         res = AVERROR_INVALIDDATA;
336         goto fail;
337     }
338
339     if (p->mastering_display) {
340         AVMasteringDisplayMetadata *mastering = av_mastering_display_metadata_create_side_data(frame);
341         if (!mastering) {
342             res = AVERROR(ENOMEM);
343             goto fail;
344         }
345
346         for (int i = 0; i < 3; i++) {
347             mastering->display_primaries[i][0] = av_make_q(p->mastering_display->primaries[i][0], 1 << 16);
348             mastering->display_primaries[i][1] = av_make_q(p->mastering_display->primaries[i][1], 1 << 16);
349         }
350         mastering->white_point[0] = av_make_q(p->mastering_display->white_point[0], 1 << 16);
351         mastering->white_point[1] = av_make_q(p->mastering_display->white_point[1], 1 << 16);
352
353         mastering->max_luminance = av_make_q(p->mastering_display->max_luminance, 1 << 8);
354         mastering->min_luminance = av_make_q(p->mastering_display->min_luminance, 1 << 14);
355
356         mastering->has_primaries = 1;
357         mastering->has_luminance = 1;
358     }
359     if (p->content_light) {
360         AVContentLightMetadata *light = av_content_light_metadata_create_side_data(frame);
361         if (!light) {
362             res = AVERROR(ENOMEM);
363             goto fail;
364         }
365         light->MaxCLL = p->content_light->max_content_light_level;
366         light->MaxFALL = p->content_light->max_frame_average_light_level;
367     }
368     if (p->itut_t35) {
369         GetByteContext gb;
370         unsigned int user_identifier;
371
372         bytestream2_init(&gb, p->itut_t35->payload, p->itut_t35->payload_size);
373         bytestream2_skip(&gb, 1); // terminal provider code
374         bytestream2_skip(&gb, 1); // terminal provider oriented code
375         user_identifier = bytestream2_get_be32(&gb);
376         switch (user_identifier) {
377         case MKBETAG('G', 'A', '9', '4'): { // closed captions
378             AVBufferRef *buf = NULL;
379
380             res = ff_parse_a53_cc(&buf, gb.buffer, bytestream2_get_bytes_left(&gb));
381             if (res < 0)
382                 goto fail;
383             if (!res)
384                 break;
385
386             if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_A53_CC, buf))
387                 av_buffer_unref(&buf);
388
389             c->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
390             break;
391         }
392         default: // ignore unsupported identifiers
393             break;
394         }
395     }
396     if (p->frame_hdr->film_grain.present && (!dav1d->apply_grain ||
397         (c->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN))) {
398         AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(frame);
399         if (!fgp) {
400             res = AVERROR(ENOMEM);
401             goto fail;
402         }
403
404         fgp->type = AV_FILM_GRAIN_PARAMS_AV1;
405         fgp->seed = p->frame_hdr->film_grain.data.seed;
406         fgp->codec.aom.num_y_points = p->frame_hdr->film_grain.data.num_y_points;
407         fgp->codec.aom.chroma_scaling_from_luma = p->frame_hdr->film_grain.data.chroma_scaling_from_luma;
408         fgp->codec.aom.scaling_shift = p->frame_hdr->film_grain.data.scaling_shift;
409         fgp->codec.aom.ar_coeff_lag = p->frame_hdr->film_grain.data.ar_coeff_lag;
410         fgp->codec.aom.ar_coeff_shift = p->frame_hdr->film_grain.data.ar_coeff_shift;
411         fgp->codec.aom.grain_scale_shift = p->frame_hdr->film_grain.data.grain_scale_shift;
412         fgp->codec.aom.overlap_flag = p->frame_hdr->film_grain.data.overlap_flag;
413         fgp->codec.aom.limit_output_range = p->frame_hdr->film_grain.data.clip_to_restricted_range;
414
415         memcpy(&fgp->codec.aom.y_points, &p->frame_hdr->film_grain.data.y_points,
416                sizeof(fgp->codec.aom.y_points));
417         memcpy(&fgp->codec.aom.num_uv_points, &p->frame_hdr->film_grain.data.num_uv_points,
418                sizeof(fgp->codec.aom.num_uv_points));
419         memcpy(&fgp->codec.aom.uv_points, &p->frame_hdr->film_grain.data.uv_points,
420                sizeof(fgp->codec.aom.uv_points));
421         memcpy(&fgp->codec.aom.ar_coeffs_y, &p->frame_hdr->film_grain.data.ar_coeffs_y,
422                sizeof(fgp->codec.aom.ar_coeffs_y));
423         memcpy(&fgp->codec.aom.ar_coeffs_uv[0], &p->frame_hdr->film_grain.data.ar_coeffs_uv[0],
424                sizeof(fgp->codec.aom.ar_coeffs_uv[0]));
425         memcpy(&fgp->codec.aom.ar_coeffs_uv[1], &p->frame_hdr->film_grain.data.ar_coeffs_uv[1],
426                sizeof(fgp->codec.aom.ar_coeffs_uv[1]));
427         memcpy(&fgp->codec.aom.uv_mult, &p->frame_hdr->film_grain.data.uv_mult,
428                sizeof(fgp->codec.aom.uv_mult));
429         memcpy(&fgp->codec.aom.uv_mult_luma, &p->frame_hdr->film_grain.data.uv_luma_mult,
430                sizeof(fgp->codec.aom.uv_mult_luma));
431         memcpy(&fgp->codec.aom.uv_offset, &p->frame_hdr->film_grain.data.uv_offset,
432                sizeof(fgp->codec.aom.uv_offset));
433     }
434
435     res = 0;
436 fail:
437     dav1d_picture_unref(p);
438     if (res < 0)
439         av_frame_unref(frame);
440     return res;
441 }
442
443 static av_cold int libdav1d_close(AVCodecContext *c)
444 {
445     Libdav1dContext *dav1d = c->priv_data;
446
447     av_buffer_pool_uninit(&dav1d->pool);
448     dav1d_data_unref(&dav1d->data);
449     dav1d_close(&dav1d->c);
450
451     return 0;
452 }
453
454 #define OFFSET(x) offsetof(Libdav1dContext, x)
455 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
456 static const AVOption libdav1d_options[] = {
457     { "tilethreads", "Tile threads", OFFSET(tile_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_TILE_THREADS, VD },
458     { "framethreads", "Frame threads", OFFSET(frame_threads), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, DAV1D_MAX_FRAME_THREADS, VD },
459     { "filmgrain", "Apply Film Grain", OFFSET(apply_grain), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD | AV_OPT_FLAG_DEPRECATED },
460     { "oppoint",  "Select an operating point of the scalable bitstream", OFFSET(operating_point), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 31, VD },
461     { "alllayers", "Output all spatial layers", OFFSET(all_layers), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VD },
462     { NULL }
463 };
464
465 static const AVClass libdav1d_class = {
466     .class_name = "libdav1d decoder",
467     .item_name  = av_default_item_name,
468     .option     = libdav1d_options,
469     .version    = LIBAVUTIL_VERSION_INT,
470 };
471
472 AVCodec ff_libdav1d_decoder = {
473     .name           = "libdav1d",
474     .long_name      = NULL_IF_CONFIG_SMALL("dav1d AV1 decoder by VideoLAN"),
475     .type           = AVMEDIA_TYPE_VIDEO,
476     .id             = AV_CODEC_ID_AV1,
477     .priv_data_size = sizeof(Libdav1dContext),
478     .init           = libdav1d_init,
479     .close          = libdav1d_close,
480     .flush          = libdav1d_flush,
481     .receive_frame  = libdav1d_receive_frame,
482     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
483     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_SETS_PKT_DTS |
484                       FF_CODEC_CAP_AUTO_THREADS,
485     .priv_class     = &libdav1d_class,
486     .wrapper_name   = "libdav1d",
487 };