contrib: cargo: use cargo/vendored-openssl if needed
[vlc.git] / modules / codec / dav1d.c
blob92f6f441464c179cd30536bc0acbc16aa31503ff
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 "cc.h"
43 /****************************************************************************
44 * Local prototypes
45 ****************************************************************************/
46 static int OpenDecoder(vlc_object_t *);
47 static void CloseDecoder(vlc_object_t *);
49 /*****************************************************************************
50 * Module descriptor
51 *****************************************************************************/
53 #define THREAD_FRAMES_TEXT N_("Frames Threads")
54 #define THREAD_FRAMES_LONGTEXT N_( "Max number of threads used for frame decoding, default 0=auto" )
55 #define THREAD_TILES_TEXT N_("Tiles Threads")
56 #define THREAD_TILES_LONGTEXT N_( "Max number of threads used for tile decoding, default 0=auto" )
59 vlc_module_begin ()
60 set_shortname("dav1d")
61 set_description(N_("Dav1d video decoder"))
62 set_capability("video decoder", 10000)
63 set_callbacks(OpenDecoder, CloseDecoder)
64 set_category(CAT_INPUT)
65 set_subcategory(SUBCAT_INPUT_VCODEC)
67 add_integer("dav1d-thread-frames", 0,
68 THREAD_FRAMES_TEXT, THREAD_FRAMES_LONGTEXT, false)
69 add_integer("dav1d-thread-tiles", 0,
70 THREAD_TILES_TEXT, THREAD_TILES_LONGTEXT, false)
71 vlc_module_end ()
73 /*****************************************************************************
74 * decoder_sys_t: libaom decoder descriptor
75 *****************************************************************************/
76 typedef struct
78 Dav1dSettings s;
79 Dav1dContext *c;
80 cc_data_t cc;
81 } decoder_sys_t;
83 struct user_data_s
85 vlc_tick_t dts;
88 static void FreeUserData_Handler(const uint8_t *p, void *userdata)
90 VLC_UNUSED(p);
91 free(userdata);
94 static const struct
96 vlc_fourcc_t i_chroma;
97 enum Dav1dPixelLayout i_chroma_id;
98 uint8_t i_bitdepth;
99 } chroma_table[] =
101 {VLC_CODEC_GREY, DAV1D_PIXEL_LAYOUT_I400, 8},
102 {VLC_CODEC_I420, DAV1D_PIXEL_LAYOUT_I420, 8},
103 {VLC_CODEC_I422, DAV1D_PIXEL_LAYOUT_I422, 8},
104 {VLC_CODEC_I444, DAV1D_PIXEL_LAYOUT_I444, 8},
106 {VLC_CODEC_GREY_10L, DAV1D_PIXEL_LAYOUT_I400, 10},
107 {VLC_CODEC_I420_10L, DAV1D_PIXEL_LAYOUT_I420, 10},
108 {VLC_CODEC_I422_10L, DAV1D_PIXEL_LAYOUT_I422, 10},
109 {VLC_CODEC_I444_10L, DAV1D_PIXEL_LAYOUT_I444, 10},
111 {VLC_CODEC_GREY_12L, DAV1D_PIXEL_LAYOUT_I400, 12},
112 {VLC_CODEC_I420_12L, DAV1D_PIXEL_LAYOUT_I420, 12},
113 {VLC_CODEC_I422_12L, DAV1D_PIXEL_LAYOUT_I422, 12},
114 {VLC_CODEC_I444_12L, DAV1D_PIXEL_LAYOUT_I444, 12},
117 static vlc_fourcc_t FindVlcChroma(const Dav1dPicture *img)
119 for (unsigned int i = 0; i < ARRAY_SIZE(chroma_table); i++)
120 if (chroma_table[i].i_chroma_id == img->p.layout &&
121 chroma_table[i].i_bitdepth == img->p.bpc)
122 return chroma_table[i].i_chroma;
124 return 0;
127 static int NewPicture(Dav1dPicture *img, void *cookie)
129 decoder_t *dec = cookie;
131 video_format_t *v = &dec->fmt_out.video;
133 v->i_visible_width = img->p.w;
134 v->i_visible_height = img->p.h;
135 v->i_width = (img->p.w + 0x7F) & ~0x7F;
136 v->i_height = (img->p.h + 0x7F) & ~0x7F;
138 if( !v->i_sar_num || !v->i_sar_den )
140 v->i_sar_num = 1;
141 v->i_sar_den = 1;
144 if(dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF && img->seq_hdr)
146 v->primaries = iso_23001_8_cp_to_vlc_primaries(img->seq_hdr->pri);
147 v->transfer = iso_23001_8_tc_to_vlc_xfer(img->seq_hdr->trc);
148 v->space = iso_23001_8_mc_to_vlc_coeffs(img->seq_hdr->mtrx);
149 v->color_range = img->seq_hdr->color_range ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
152 const Dav1dMasteringDisplay *md = img->mastering_display;
153 if( dec->fmt_in.video.mastering.max_luminance == 0 && md )
155 const uint8_t RGB2GBR[3] = {2,0,1};
156 for( size_t i=0;i<6; i++ )
158 v->mastering.primaries[i] =
159 50000 * (double) md->primaries[RGB2GBR[i >> 1]][i % 2]
160 / (double)(1 << 16);
162 v->mastering.min_luminance = 10000 * (double)md->min_luminance
163 / (double) (1<<14);
164 v->mastering.max_luminance = 10000 * (double) md->max_luminance
165 / (double) (1<<8);
166 v->mastering.white_point[0] = 50000 * (double)md->white_point[0]
167 / (double) (1<<16);
168 v->mastering.white_point[1] = 50000 * (double)md->white_point[1]
169 / (double) (1<<16);
172 const Dav1dContentLightLevel *cll = img->content_light;
173 if( dec->fmt_in.video.lighting.MaxCLL == 0 && cll )
175 v->lighting.MaxCLL = cll->max_content_light_level;
176 v->lighting.MaxFALL = cll->max_frame_average_light_level;
179 v->projection_mode = dec->fmt_in.video.projection_mode;
180 v->multiview_mode = dec->fmt_in.video.multiview_mode;
181 v->pose = dec->fmt_in.video.pose;
182 dec->fmt_out.video.i_chroma = dec->fmt_out.i_codec = FindVlcChroma(img);
184 if (decoder_UpdateVideoFormat(dec) == 0)
186 picture_t *pic = decoder_NewPicture(dec);
187 if (likely(pic != NULL))
189 img->data[0] = pic->p[0].p_pixels;
190 img->stride[0] = pic->p[0].i_pitch;
191 img->data[1] = pic->p[1].p_pixels;
192 img->data[2] = pic->p[2].p_pixels;
193 assert(pic->p[1].i_pitch == pic->p[2].i_pitch);
194 img->stride[1] = pic->p[1].i_pitch;
195 img->allocator_data = pic;
197 return 0;
200 return -1;
203 static void ExtractCaptions(decoder_t *dec, const Dav1dPicture *img)
205 decoder_sys_t *p_sys = dec->p_sys;
206 const struct user_data_s *userdata = (struct user_data_s *) img->m.user_data.data;
207 const Dav1dITUTT35 *itu_t35 = img->itut_t35;
208 if(itu_t35 && itu_t35->country_code == 0xb5 &&
209 itu_t35->payload_size > 9 &&
210 !memcmp(itu_t35->payload, "\x00\x0x31GA94\x03", 7))
212 cc_Extract(&p_sys->cc, CC_PAYLOAD_GA94, true,
213 &itu_t35->payload[7], itu_t35->payload_size - 7);
214 if(p_sys->cc.b_reorder || p_sys->cc.i_data)
216 block_t *p_cc = block_Alloc(p_sys->cc.i_data);
217 if(p_cc)
219 memcpy(p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data);
220 if(p_sys->cc.b_reorder || userdata == NULL)
221 p_cc->i_dts = p_cc->i_pts = img->m.timestamp;
222 else
223 p_cc->i_pts = p_cc->i_dts = userdata->dts;
224 decoder_cc_desc_t desc;
225 desc.i_608_channels = p_sys->cc.i_608channels;
226 desc.i_708_channels = p_sys->cc.i_708channels;
227 desc.i_reorder_depth = 4;
228 decoder_QueueCc(dec, p_cc, &desc);
230 cc_Flush(&p_sys->cc);
235 static void FreePicture(Dav1dPicture *data, void *cookie)
237 picture_t *pic = data->allocator_data;
238 decoder_t *dec = cookie;
239 VLC_UNUSED(dec);
240 picture_Release(pic);
243 /****************************************************************************
244 * Flush: clears decoder between seeks
245 ****************************************************************************/
247 static void FlushDecoder(decoder_t *dec)
249 decoder_sys_t *p_sys = dec->p_sys;
250 dav1d_flush(p_sys->c);
251 cc_Flush(&p_sys->cc);
254 static void release_block(const uint8_t *buf, void *b)
256 VLC_UNUSED(buf);
257 block_t *block = b;
258 block_Release(block);
261 /****************************************************************************
262 * Decode: the whole thing
263 ****************************************************************************/
264 static int Decode(decoder_t *dec, block_t *block)
266 decoder_sys_t *p_sys = dec->p_sys;
268 if (block && block->i_flags & (BLOCK_FLAG_CORRUPTED))
270 block_Release(block);
271 return VLCDEC_SUCCESS;
274 bool b_eos = false;
275 Dav1dData data;
276 Dav1dData *p_data = NULL;
278 if (block)
280 p_data = &data;
281 if (unlikely(dav1d_data_wrap(&data, block->p_buffer, block->i_buffer,
282 release_block, block) != 0))
284 block_Release(block);
285 return VLCDEC_ECRITICAL;
288 p_data->m.timestamp = block->i_pts == VLC_TICK_INVALID ? block->i_dts : block->i_pts;
289 if(block->i_dts != p_data->m.timestamp)
291 struct user_data_s *userdata = malloc(sizeof(*userdata));
292 if(unlikely(userdata == NULL ||
293 0 != dav1d_data_wrap_user_data(&data, (const uint8_t *) userdata,
294 FreeUserData_Handler, userdata)))
296 free(userdata);
297 dav1d_data_unref(&data);
298 return VLCDEC_ECRITICAL;
300 userdata->dts = block->i_dts;
303 b_eos = (block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE);
306 bool b_draining = false;
307 int i_ret = VLCDEC_SUCCESS;
308 int res;
309 do {
310 if( p_data )
312 res = dav1d_send_data(p_sys->c, p_data);
313 if (res < 0 && res != DAV1D_ERR(EAGAIN))
315 msg_Err(dec, "Decoder feed error %d!", res);
316 i_ret = VLC_EGENERIC;
317 break;
321 bool b_output_error = false;
324 Dav1dPicture img = { 0 };
325 res = dav1d_get_picture(p_sys->c, &img);
326 if (res == 0)
328 picture_t *_pic = img.allocator_data;
329 picture_t *pic = picture_Clone(_pic);
330 if (unlikely(pic == NULL))
332 i_ret = VLC_EGENERIC;
333 picture_Release(_pic);
334 b_output_error = true;
335 break;
337 pic->b_progressive = true; /* codec does not support interlacing */
338 pic->date = img.m.timestamp;
339 decoder_QueueVideo(dec, pic);
340 ExtractCaptions(dec, &img);
341 dav1d_picture_unref(&img);
343 else if (res != DAV1D_ERR(EAGAIN))
345 msg_Warn(dec, "Decoder error %d!", res);
346 b_output_error = true;
347 break;
349 } while(res == 0);
351 if(b_output_error)
352 break;
354 /* on drain, we must ignore the 1st EAGAIN */
355 if(!b_draining && (res == DAV1D_ERR(EAGAIN) || res == 0)
356 && (p_data == NULL||b_eos))
358 b_draining = true;
359 res = 0;
361 } while (res == 0 || (p_data && p_data->sz != 0));
363 if(p_data && p_data->sz > 0)
364 dav1d_data_unref(p_data);
366 return i_ret;
369 /*****************************************************************************
370 * OpenDecoder: probe the decoder
371 *****************************************************************************/
372 static int OpenDecoder(vlc_object_t *p_this)
374 decoder_t *dec = (decoder_t *)p_this;
376 if (dec->fmt_in.i_codec != VLC_CODEC_AV1)
377 return VLC_EGENERIC;
379 decoder_sys_t *p_sys = vlc_obj_malloc(p_this, sizeof(*p_sys));
380 if (!p_sys)
381 return VLC_ENOMEM;
383 dav1d_default_settings(&p_sys->s);
384 p_sys->s.n_tile_threads = var_InheritInteger(p_this, "dav1d-thread-tiles");
385 if (p_sys->s.n_tile_threads == 0)
386 p_sys->s.n_tile_threads = VLC_CLIP(vlc_GetCPUCount(), 1, 4);
387 p_sys->s.n_frame_threads = var_InheritInteger(p_this, "dav1d-thread-frames");
388 if (p_sys->s.n_frame_threads == 0)
389 p_sys->s.n_frame_threads = __MAX(1, vlc_GetCPUCount());
390 p_sys->s.allocator.cookie = dec;
391 p_sys->s.allocator.alloc_picture_callback = NewPicture;
392 p_sys->s.allocator.release_picture_callback = FreePicture;
394 if (dav1d_open(&p_sys->c, &p_sys->s) < 0)
396 msg_Err(p_this, "Could not open the Dav1d decoder");
397 return VLC_EGENERIC;
400 msg_Dbg(p_this, "Using dav1d version %s with %d/%d frame/tile threads",
401 dav1d_version(), p_sys->s.n_frame_threads, p_sys->s.n_tile_threads);
403 dec->pf_decode = Decode;
404 dec->pf_flush = FlushDecoder;
405 dec->i_extra_picture_buffers = (p_sys->s.n_frame_threads - 1);
407 dec->fmt_out.video.i_width = dec->fmt_in.video.i_width;
408 dec->fmt_out.video.i_height = dec->fmt_in.video.i_height;
409 dec->fmt_out.i_codec = VLC_CODEC_I420;
410 dec->p_sys = p_sys;
412 if (dec->fmt_in.video.i_sar_num > 0 && dec->fmt_in.video.i_sar_den > 0) {
413 dec->fmt_out.video.i_sar_num = dec->fmt_in.video.i_sar_num;
414 dec->fmt_out.video.i_sar_den = dec->fmt_in.video.i_sar_den;
416 dec->fmt_out.video.primaries = dec->fmt_in.video.primaries;
417 dec->fmt_out.video.transfer = dec->fmt_in.video.transfer;
418 dec->fmt_out.video.space = dec->fmt_in.video.space;
419 dec->fmt_out.video.color_range = dec->fmt_in.video.color_range;
420 dec->fmt_out.video.mastering = dec->fmt_in.video.mastering;
421 dec->fmt_out.video.lighting = dec->fmt_in.video.lighting;
423 cc_Init(&p_sys->cc);
425 return VLC_SUCCESS;
428 /*****************************************************************************
429 * CloseDecoder: decoder destruction
430 *****************************************************************************/
431 static void CloseDecoder(vlc_object_t *p_this)
433 decoder_t *dec = (decoder_t *)p_this;
434 decoder_sys_t *p_sys = dec->p_sys;
436 /* Flush decoder */
437 FlushDecoder(dec);
439 dav1d_close(&p_sys->c);