qml/KeyNavigableTableView: Add 'delegate' and 'contentY' aliases
[vlc.git] / modules / codec / dav1d.c
blobdcb092131869a876165ee02f2732161fb988c9cf
1 /*****************************************************************************
2 * dav1d.c: dav1d decoder (AV1) module
3 *****************************************************************************
4 * Copyright (C) 2016 VLC authors and VideoLAN
6 * Authors: Adrien Maglo <magsoft@videolan.org>
7 * Based on aom.c by: Tristan Matthews <tmatth@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35 #include <vlc_timestamp_helper.h>
37 #include <errno.h>
38 #include <dav1d/dav1d.h>
40 #include "../packetizer/iso_color_tables.h"
41 #include "../packetizer/av1_obu.h"
42 #include "cc.h"
44 /****************************************************************************
45 * Local prototypes
46 ****************************************************************************/
47 static int OpenDecoder(vlc_object_t *);
48 static void CloseDecoder(vlc_object_t *);
50 /*****************************************************************************
51 * Module descriptor
52 *****************************************************************************/
54 #define THREAD_FRAMES_TEXT N_("Frames Threads")
55 #define THREAD_FRAMES_LONGTEXT N_( "Max number of threads used for frame decoding, default 0=auto" )
56 #define THREAD_TILES_TEXT N_("Tiles Threads")
57 #define THREAD_TILES_LONGTEXT N_( "Max number of threads used for tile decoding, default 0=auto" )
60 vlc_module_begin ()
61 set_shortname("dav1d")
62 set_description(N_("Dav1d video decoder"))
63 set_capability("video decoder", 10000)
64 set_callbacks(OpenDecoder, CloseDecoder)
65 set_category(CAT_INPUT)
66 set_subcategory(SUBCAT_INPUT_VCODEC)
68 add_integer_with_range("dav1d-thread-frames", 0, 0, DAV1D_MAX_FRAME_THREADS,
69 THREAD_FRAMES_TEXT, THREAD_FRAMES_LONGTEXT, false)
70 add_integer_with_range("dav1d-thread-tiles", 0, 0, DAV1D_MAX_TILE_THREADS,
71 THREAD_TILES_TEXT, THREAD_TILES_LONGTEXT, false)
72 vlc_module_end ()
74 /*****************************************************************************
75 * decoder_sys_t: libaom decoder descriptor
76 *****************************************************************************/
77 typedef struct
79 Dav1dSettings s;
80 Dav1dContext *c;
81 cc_data_t cc;
82 } decoder_sys_t;
84 struct user_data_s
86 vlc_tick_t dts;
89 static void FreeUserData_Handler(const uint8_t *p, void *userdata)
91 VLC_UNUSED(p);
92 free(userdata);
95 static const struct
97 vlc_fourcc_t i_chroma;
98 enum Dav1dPixelLayout i_chroma_id;
99 uint8_t i_bitdepth;
100 } chroma_table[] =
102 {VLC_CODEC_GREY, DAV1D_PIXEL_LAYOUT_I400, 8},
103 {VLC_CODEC_I420, DAV1D_PIXEL_LAYOUT_I420, 8},
104 {VLC_CODEC_I422, DAV1D_PIXEL_LAYOUT_I422, 8},
105 {VLC_CODEC_I444, DAV1D_PIXEL_LAYOUT_I444, 8},
107 {VLC_CODEC_GREY_10L, DAV1D_PIXEL_LAYOUT_I400, 10},
108 {VLC_CODEC_I420_10L, DAV1D_PIXEL_LAYOUT_I420, 10},
109 {VLC_CODEC_I422_10L, DAV1D_PIXEL_LAYOUT_I422, 10},
110 {VLC_CODEC_I444_10L, DAV1D_PIXEL_LAYOUT_I444, 10},
112 {VLC_CODEC_GREY_12L, DAV1D_PIXEL_LAYOUT_I400, 12},
113 {VLC_CODEC_I420_12L, DAV1D_PIXEL_LAYOUT_I420, 12},
114 {VLC_CODEC_I422_12L, DAV1D_PIXEL_LAYOUT_I422, 12},
115 {VLC_CODEC_I444_12L, DAV1D_PIXEL_LAYOUT_I444, 12},
118 static vlc_fourcc_t FindVlcChroma(const Dav1dPicture *img)
120 for (unsigned int i = 0; i < ARRAY_SIZE(chroma_table); i++)
121 if (chroma_table[i].i_chroma_id == img->p.layout &&
122 chroma_table[i].i_bitdepth == img->p.bpc)
123 return chroma_table[i].i_chroma;
125 return 0;
128 static void UpdateDecoderOutput(decoder_t *dec, const Dav1dSequenceHeader *seq_hdr)
130 video_format_t *v = &dec->fmt_out.video;
132 if( !v->i_sar_num || !v->i_sar_den )
134 v->i_sar_num = 1;
135 v->i_sar_den = 1;
138 if(dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF && seq_hdr)
140 v->primaries = iso_23001_8_cp_to_vlc_primaries(seq_hdr->pri);
141 v->transfer = iso_23001_8_tc_to_vlc_xfer(seq_hdr->trc);
142 v->space = iso_23001_8_mc_to_vlc_coeffs(seq_hdr->mtrx);
143 v->color_range = seq_hdr->color_range ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
147 static int NewPicture(Dav1dPicture *img, void *cookie)
149 decoder_t *dec = cookie;
151 video_format_t *v = &dec->fmt_out.video;
153 v->i_visible_width = img->p.w;
154 v->i_visible_height = img->p.h;
156 UpdateDecoderOutput(dec, img->seq_hdr);
158 const Dav1dMasteringDisplay *md = img->mastering_display;
159 if( dec->fmt_in.video.mastering.max_luminance == 0 && md )
161 const uint8_t RGB2GBR[3] = {2,0,1};
162 for( size_t i=0;i<6; i++ )
164 v->mastering.primaries[i] =
165 50000 * (double) md->primaries[RGB2GBR[i >> 1]][i % 2]
166 / (double)(1 << 16);
168 v->mastering.min_luminance = 10000 * (double)md->min_luminance
169 / (double) (1<<14);
170 v->mastering.max_luminance = 10000 * (double) md->max_luminance
171 / (double) (1<<8);
172 v->mastering.white_point[0] = 50000 * (double)md->white_point[0]
173 / (double) (1<<16);
174 v->mastering.white_point[1] = 50000 * (double)md->white_point[1]
175 / (double) (1<<16);
178 const Dav1dContentLightLevel *cll = img->content_light;
179 if( dec->fmt_in.video.lighting.MaxCLL == 0 && cll )
181 v->lighting.MaxCLL = cll->max_content_light_level;
182 v->lighting.MaxFALL = cll->max_frame_average_light_level;
185 v->projection_mode = dec->fmt_in.video.projection_mode;
186 v->multiview_mode = dec->fmt_in.video.multiview_mode;
187 v->pose = dec->fmt_in.video.pose;
188 dec->fmt_out.i_codec = FindVlcChroma(img);
189 v->i_width = (img->p.w + 0x7F) & ~0x7F;
190 v->i_height = (img->p.h + 0x7F) & ~0x7F;
191 v->i_chroma = dec->fmt_out.i_codec;
193 if (decoder_UpdateVideoFormat(dec) == 0)
195 picture_t *pic;
196 pic = decoder_NewPicture(dec);
197 if (unlikely(pic == NULL))
198 return -1;
200 img->data[0] = pic->p[0].p_pixels;
201 img->stride[0] = pic->p[0].i_pitch;
202 img->data[1] = pic->p[1].p_pixels;
203 img->data[2] = pic->p[2].p_pixels;
204 assert(pic->p[1].i_pitch == pic->p[2].i_pitch);
205 img->stride[1] = pic->p[1].i_pitch;
207 img->allocator_data = pic;
208 return 0;
210 return -1;
213 static void ExtractCaptions(decoder_t *dec, const Dav1dPicture *img)
215 decoder_sys_t *p_sys = dec->p_sys;
216 const struct user_data_s *userdata = (struct user_data_s *) img->m.user_data.data;
217 const Dav1dITUTT35 *itu_t35 = img->itut_t35;
218 if(itu_t35 && itu_t35->country_code == 0xb5 &&
219 itu_t35->payload_size > 9 &&
220 !memcmp(itu_t35->payload, "\x00\x0x31GA94\x03", 7))
222 cc_Extract(&p_sys->cc, CC_PAYLOAD_GA94, true,
223 &itu_t35->payload[7], itu_t35->payload_size - 7);
224 if(p_sys->cc.b_reorder || p_sys->cc.i_data)
226 block_t *p_cc = block_Alloc(p_sys->cc.i_data);
227 if(p_cc)
229 memcpy(p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data);
230 if(p_sys->cc.b_reorder || userdata == NULL)
231 p_cc->i_dts = p_cc->i_pts = img->m.timestamp;
232 else
233 p_cc->i_pts = p_cc->i_dts = userdata->dts;
234 decoder_cc_desc_t desc;
235 desc.i_608_channels = p_sys->cc.i_608channels;
236 desc.i_708_channels = p_sys->cc.i_708channels;
237 desc.i_reorder_depth = 4;
238 decoder_QueueCc(dec, p_cc, &desc);
240 cc_Flush(&p_sys->cc);
245 static void FreePicture(Dav1dPicture *data, void *cookie)
247 picture_t *pic = data->allocator_data;
248 decoder_t *dec = cookie;
249 VLC_UNUSED(dec);
250 picture_Release(pic);
253 /****************************************************************************
254 * Flush: clears decoder between seeks
255 ****************************************************************************/
257 static void FlushDecoder(decoder_t *dec)
259 decoder_sys_t *p_sys = dec->p_sys;
260 dav1d_flush(p_sys->c);
261 cc_Flush(&p_sys->cc);
264 static void release_block(const uint8_t *buf, void *b)
266 VLC_UNUSED(buf);
267 block_t *block = b;
268 block_Release(block);
271 /****************************************************************************
272 * Decode: the whole thing
273 ****************************************************************************/
274 static int Decode(decoder_t *dec, block_t *block)
276 decoder_sys_t *p_sys = dec->p_sys;
278 if (block && block->i_flags & (BLOCK_FLAG_CORRUPTED))
280 block_Release(block);
281 return VLCDEC_SUCCESS;
284 bool b_eos = false;
285 Dav1dData data;
286 Dav1dData *p_data = NULL;
288 if (block)
290 p_data = &data;
291 if (unlikely(dav1d_data_wrap(&data, block->p_buffer, block->i_buffer,
292 release_block, block) != 0))
294 block_Release(block);
295 return VLCDEC_ECRITICAL;
298 p_data->m.timestamp = block->i_pts == VLC_TICK_INVALID ? block->i_dts : block->i_pts;
299 if(block->i_dts != p_data->m.timestamp)
301 struct user_data_s *userdata = malloc(sizeof(*userdata));
302 if(unlikely(userdata == NULL ||
303 0 != dav1d_data_wrap_user_data(&data, (const uint8_t *) userdata,
304 FreeUserData_Handler, userdata)))
306 free(userdata);
307 dav1d_data_unref(&data);
308 return VLCDEC_ECRITICAL;
310 userdata->dts = block->i_dts;
313 b_eos = (block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE);
316 bool b_draining = false;
317 int i_ret = VLCDEC_SUCCESS;
318 int res;
319 do {
320 if( p_data )
322 res = dav1d_send_data(p_sys->c, p_data);
323 if (res < 0 && res != DAV1D_ERR(EAGAIN))
325 msg_Err(dec, "Decoder feed error %d!", res);
326 i_ret = VLC_EGENERIC;
327 break;
331 bool b_output_error = false;
334 Dav1dPicture img = { 0 };
335 res = dav1d_get_picture(p_sys->c, &img);
336 if (res == 0)
338 picture_t *_pic = img.allocator_data;
339 picture_t *pic = picture_Clone(_pic);
340 if (unlikely(pic == NULL))
342 i_ret = VLC_EGENERIC;
343 picture_Release(_pic);
344 b_output_error = true;
345 break;
347 pic->b_progressive = true; /* codec does not support interlacing */
348 pic->date = img.m.timestamp;
349 decoder_QueueVideo(dec, pic);
350 ExtractCaptions(dec, &img);
351 dav1d_picture_unref(&img);
353 else if (res != DAV1D_ERR(EAGAIN))
355 msg_Warn(dec, "Decoder error %d!", res);
356 b_output_error = true;
357 break;
359 } while(res == 0);
361 if(b_output_error)
362 break;
364 /* on drain, we must ignore the 1st EAGAIN */
365 if(!b_draining && (res == DAV1D_ERR(EAGAIN) || res == 0)
366 && (p_data == NULL||b_eos))
368 b_draining = true;
369 res = 0;
371 } while (res == 0 || (p_data && p_data->sz != 0));
373 if(p_data && p_data->sz > 0)
374 dav1d_data_unref(p_data);
376 return i_ret;
379 /*****************************************************************************
380 * OpenDecoder: probe the decoder
381 *****************************************************************************/
382 static int OpenDecoder(vlc_object_t *p_this)
384 decoder_t *dec = (decoder_t *)p_this;
386 if (dec->fmt_in.i_codec != VLC_CODEC_AV1)
387 return VLC_EGENERIC;
389 decoder_sys_t *p_sys = vlc_obj_malloc(p_this, sizeof(*p_sys));
390 if (!p_sys)
391 return VLC_ENOMEM;
393 dav1d_default_settings(&p_sys->s);
394 p_sys->s.n_tile_threads = var_InheritInteger(p_this, "dav1d-thread-tiles");
395 if (p_sys->s.n_tile_threads == 0)
396 p_sys->s.n_tile_threads = VLC_CLIP(vlc_GetCPUCount(), 1, 4);
397 p_sys->s.n_frame_threads = var_InheritInteger(p_this, "dav1d-thread-frames");
398 if (p_sys->s.n_frame_threads == 0)
399 p_sys->s.n_frame_threads = __MAX(1, vlc_GetCPUCount());
400 p_sys->s.allocator.cookie = dec;
401 p_sys->s.allocator.alloc_picture_callback = NewPicture;
402 p_sys->s.allocator.release_picture_callback = FreePicture;
404 av1_OBU_sequence_header_t *sequence_hdr = NULL;
405 if (dec->fmt_in.i_extra > 4)
407 // in ISOBMFF/WebM/Matroska the first 4 bytes are from the AV1CodecConfigurationBox
408 // and then one or more OBU
409 const uint8_t *obu_start = ((const uint8_t*) dec->fmt_in.p_extra) + 4;
410 int obu_size = dec->fmt_in.i_extra - 4;
411 if (AV1_OBUIsValid(obu_start, obu_size) && AV1_OBUGetType(obu_start) == AV1_OBU_SEQUENCE_HEADER)
412 sequence_hdr = AV1_OBU_parse_sequence_header(obu_start, obu_size);
415 if (!sequence_hdr)
417 dec->fmt_out.i_codec = VLC_CODEC_I420;
418 dec->fmt_out.video.i_width = dec->fmt_in.video.i_width;
419 dec->fmt_out.video.i_height = dec->fmt_in.video.i_height;
421 else
423 // use the sequence header to get a better chroma to start with
424 dec->fmt_out.i_codec = AV1_get_chroma(sequence_hdr);
426 AV1_get_frame_max_dimensions(sequence_hdr, &dec->fmt_out.video.i_width, &dec->fmt_out.video.i_height);
428 if (dec->fmt_out.video.transfer == TRANSFER_FUNC_UNDEF)
429 AV1_get_colorimetry(sequence_hdr, &dec->fmt_out.video.primaries, &dec->fmt_out.video.transfer,
430 &dec->fmt_out.video.space, &dec->fmt_out.video.color_range);
432 dec->fmt_out.video.i_visible_width = dec->fmt_out.video.i_width;
433 dec->fmt_out.video.i_visible_height = dec->fmt_out.video.i_height;
435 if (dav1d_open(&p_sys->c, &p_sys->s) < 0)
437 msg_Err(p_this, "Could not open the Dav1d decoder");
438 return VLC_EGENERIC;
441 msg_Dbg(p_this, "Using dav1d version %s with %d/%d frame/tile threads",
442 dav1d_version(), p_sys->s.n_frame_threads, p_sys->s.n_tile_threads);
444 dec->i_extra_picture_buffers = (p_sys->s.n_frame_threads - 1);
445 dec->fmt_out.video.i_width = (dec->fmt_out.video.i_width + 0x7F) & ~0x7F;
446 dec->fmt_out.video.i_height = (dec->fmt_out.video.i_height + 0x7F) & ~0x7F;
448 dec->p_sys = p_sys;
450 if (dec->fmt_in.video.i_sar_num > 0 && dec->fmt_in.video.i_sar_den > 0) {
451 dec->fmt_out.video.i_sar_num = dec->fmt_in.video.i_sar_num;
452 dec->fmt_out.video.i_sar_den = dec->fmt_in.video.i_sar_den;
454 dec->fmt_out.video.primaries = dec->fmt_in.video.primaries;
455 dec->fmt_out.video.transfer = dec->fmt_in.video.transfer;
456 dec->fmt_out.video.space = dec->fmt_in.video.space;
457 dec->fmt_out.video.color_range = dec->fmt_in.video.color_range;
458 dec->fmt_out.video.mastering = dec->fmt_in.video.mastering;
459 dec->fmt_out.video.lighting = dec->fmt_in.video.lighting;
461 if (sequence_hdr != NULL)
463 // we have the proper chroma, make sure we can use it
464 AV1_release_sequence_header(sequence_hdr);
466 if (decoder_UpdateVideoFormat(dec) != 0)
468 CloseDecoder(VLC_OBJECT(dec));
469 return VLC_EGENERIC;
472 dec->pf_decode = Decode;
473 dec->pf_flush = FlushDecoder;
475 cc_Init(&p_sys->cc);
477 return VLC_SUCCESS;
480 /*****************************************************************************
481 * CloseDecoder: decoder destruction
482 *****************************************************************************/
483 static void CloseDecoder(vlc_object_t *p_this)
485 decoder_t *dec = (decoder_t *)p_this;
486 decoder_sys_t *p_sys = dec->p_sys;
488 /* Flush decoder */
489 FlushDecoder(dec);
491 dav1d_close(&p_sys->c);