qml: remove context indicator
[vlc.git] / modules / codec / aom.c
blobd4015a5dc4ce7fffca498333cfa7f2a8bd403cdf
1 /*****************************************************************************
2 * aom.c: libaom decoder (AV1) module
3 *****************************************************************************
4 * Copyright (C) 2016 VLC authors and VideoLAN
6 * Authors: Tristan Matthews <tmatth@videolan.org>
7 * Based on vpx.c by: Rafaël Carré <funman@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
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_codec.h>
35 #include <aom/aom_decoder.h>
36 #include <aom/aomdx.h>
38 #ifdef ENABLE_SOUT
39 # include <aom/aomcx.h>
40 # include <aom/aom_image.h>
41 # define SOUT_CFG_PREFIX "sout-aom-"
42 #endif
44 #include "../packetizer/iso_color_tables.h"
46 /****************************************************************************
47 * Local prototypes
48 ****************************************************************************/
49 static int OpenDecoder(vlc_object_t *);
50 static void CloseDecoder(vlc_object_t *);
51 #ifdef ENABLE_SOUT
52 static int OpenEncoder(vlc_object_t *);
53 static void CloseEncoder(vlc_object_t *);
54 static block_t *Encode(encoder_t *p_enc, picture_t *p_pict);
56 static const int pi_enc_bitdepth_values_list[] =
57 { 8, 10, 12 };
58 static const char *const ppsz_enc_bitdepth_text [] =
59 { N_("8 bpp"), N_("10 bpp"), N_("12 bpp") };
60 #endif
62 /*****************************************************************************
63 * Module descriptor
64 *****************************************************************************/
66 vlc_module_begin ()
67 set_shortname("aom")
68 set_description(N_("AOM video decoder"))
69 set_capability("video decoder", 100)
70 set_callbacks(OpenDecoder, CloseDecoder)
71 set_category(CAT_INPUT)
72 set_subcategory(SUBCAT_INPUT_VCODEC)
73 #ifdef ENABLE_SOUT
74 add_submodule()
75 set_shortname("aom")
76 set_capability("encoder", 101)
77 set_description(N_("AOM video encoder"))
78 set_callbacks(OpenEncoder, CloseEncoder)
79 add_integer( SOUT_CFG_PREFIX "profile", 0, "Profile", NULL, true )
80 change_integer_range( 0, 3 )
81 add_integer( SOUT_CFG_PREFIX "bitdepth", 8, "Bit Depth", NULL, true )
82 change_integer_list( pi_enc_bitdepth_values_list, ppsz_enc_bitdepth_text )
83 add_integer( SOUT_CFG_PREFIX "tile-rows", 0, "Tile Rows (in log2 units)", NULL, true )
84 change_integer_range( 0, 6 ) /* 1 << 6 == MAX_TILE_ROWS */
85 add_integer( SOUT_CFG_PREFIX "tile-columns", 0, "Tile Columns (in log2 units)", NULL, true )
86 change_integer_range( 0, 6 ) /* 1 << 6 == MAX_TILE_COLS */
87 #ifdef AOM_CTRL_AV1E_SET_ROW_MT
88 add_bool( SOUT_CFG_PREFIX "row-mt", false, "Row Multithreading", NULL, true )
89 #endif
90 #endif
91 vlc_module_end ()
93 static void aom_err_msg(vlc_object_t *this, aom_codec_ctx_t *ctx,
94 const char *msg)
96 const char *error = aom_codec_error(ctx);
97 const char *detail = aom_codec_error_detail(ctx);
98 if (!detail)
99 detail = "no specific information";
100 msg_Err(this, msg, error, detail);
103 #define AOM_ERR(this, ctx, msg) aom_err_msg(VLC_OBJECT(this), ctx, msg ": %s (%s)")
104 #define AOM_MAX_FRAMES_DEPTH 64
106 /*****************************************************************************
107 * decoder_sys_t: libaom decoder descriptor
108 *****************************************************************************/
109 struct frame_priv_s
111 vlc_tick_t pts;
114 typedef struct
116 aom_codec_ctx_t ctx;
117 struct frame_priv_s frame_priv[AOM_MAX_FRAMES_DEPTH];
118 unsigned i_next_frame_priv;
119 } decoder_sys_t;
121 static const struct
123 vlc_fourcc_t i_chroma;
124 enum aom_img_fmt i_chroma_id;
125 uint8_t i_bitdepth;
126 uint8_t i_needs_hack;
128 } chroma_table[] =
130 { VLC_CODEC_I420, AOM_IMG_FMT_I420, 8, 0 },
131 { VLC_CODEC_I422, AOM_IMG_FMT_I422, 8, 0 },
132 { VLC_CODEC_I444, AOM_IMG_FMT_I444, 8, 0 },
134 { VLC_CODEC_YV12, AOM_IMG_FMT_YV12, 8, 0 },
135 { VLC_CODEC_YUVA, AOM_IMG_FMT_444A, 8, 0 },
137 { VLC_CODEC_GBR_PLANAR, AOM_IMG_FMT_I444, 8, 1 },
138 { VLC_CODEC_GBR_PLANAR_10L, AOM_IMG_FMT_I44416, 10, 1 },
140 { VLC_CODEC_I420_10L, AOM_IMG_FMT_I42016, 10, 0 },
141 { VLC_CODEC_I422_10L, AOM_IMG_FMT_I42216, 10, 0 },
142 { VLC_CODEC_I444_10L, AOM_IMG_FMT_I44416, 10, 0 },
144 { VLC_CODEC_I420_12L, AOM_IMG_FMT_I42016, 12, 0 },
145 { VLC_CODEC_I422_12L, AOM_IMG_FMT_I42216, 12, 0 },
146 { VLC_CODEC_I444_12L, AOM_IMG_FMT_I44416, 12, 0 },
148 { VLC_CODEC_I444_16L, AOM_IMG_FMT_I44416, 16, 0 },
151 static vlc_fourcc_t FindVlcChroma( struct aom_image *img )
153 uint8_t hack = (img->fmt & AOM_IMG_FMT_I444) && (img->tc == AOM_CICP_TC_SRGB);
155 for( unsigned int i = 0; i < ARRAY_SIZE(chroma_table); i++ )
156 if( chroma_table[i].i_chroma_id == img->fmt &&
157 chroma_table[i].i_bitdepth == img->bit_depth &&
158 chroma_table[i].i_needs_hack == hack )
159 return chroma_table[i].i_chroma;
161 return 0;
164 static void CopyPicture(const struct aom_image *img, picture_t *pic)
166 for (int plane = 0; plane < pic->i_planes; plane++ ) {
167 plane_t src_plane = pic->p[plane];
168 src_plane.p_pixels = img->planes[plane];
169 src_plane.i_pitch = img->stride[plane];
170 plane_CopyPixels(&pic->p[plane], &src_plane);
174 static int PushFrame(decoder_t *dec, block_t *block)
176 decoder_sys_t *p_sys = dec->p_sys;
177 aom_codec_ctx_t *ctx = &p_sys->ctx;
178 const uint8_t *p_buffer;
179 size_t i_buffer;
181 /* Associate packet PTS with decoded frame */
182 uintptr_t priv_index = p_sys->i_next_frame_priv++ % AOM_MAX_FRAMES_DEPTH;
184 if(likely(block))
186 p_buffer = block->p_buffer;
187 i_buffer = block->i_buffer;
188 p_sys->frame_priv[priv_index].pts = (block->i_pts != VLC_TICK_INVALID) ? block->i_pts : block->i_dts;
190 else
192 p_buffer = NULL;
193 i_buffer = 0;
196 aom_codec_err_t err;
197 err = aom_codec_decode(ctx, p_buffer, i_buffer, (void*)priv_index);
199 if(block)
200 block_Release(block);
202 if (err != AOM_CODEC_OK) {
203 AOM_ERR(dec, ctx, "Failed to decode frame");
204 if (err == AOM_CODEC_UNSUP_BITSTREAM)
205 return VLCDEC_ECRITICAL;
207 return VLCDEC_SUCCESS;
210 static void OutputFrame(decoder_t *dec, const struct aom_image *img)
212 video_format_t *v = &dec->fmt_out.video;
214 if (img->d_w != v->i_visible_width || img->d_h != v->i_visible_height)
216 v->i_visible_width = dec->fmt_out.video.i_width = img->d_w;
217 v->i_visible_height = dec->fmt_out.video.i_height = img->d_h;
220 if( !dec->fmt_out.video.i_sar_num || !dec->fmt_out.video.i_sar_den )
222 dec->fmt_out.video.i_sar_num = 1;
223 dec->fmt_out.video.i_sar_den = 1;
226 if(dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF)
228 v->primaries = iso_23001_8_cp_to_vlc_primaries(img->cp);
229 v->transfer = iso_23001_8_tc_to_vlc_xfer(img->tc);
230 v->space = iso_23001_8_mc_to_vlc_coeffs(img->mc);
231 v->color_range = img->range == AOM_CR_FULL_RANGE ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
234 dec->fmt_out.video.projection_mode = dec->fmt_in.video.projection_mode;
235 dec->fmt_out.video.multiview_mode = dec->fmt_in.video.multiview_mode;
236 dec->fmt_out.video.pose = dec->fmt_in.video.pose;
238 if (decoder_UpdateVideoFormat(dec) == 0)
240 picture_t *pic = decoder_NewPicture(dec);
241 if (pic)
243 decoder_sys_t *p_sys = dec->p_sys;
244 CopyPicture(img, pic);
246 /* fetches back the PTS */
248 pic->b_progressive = true; /* codec does not support interlacing */
249 pic->date = p_sys->frame_priv[(uintptr_t)img->user_priv].pts;
251 decoder_QueueVideo(dec, pic);
256 static int PopFrames(decoder_t *dec,
257 void(*pf_output)(decoder_t *, const struct aom_image *))
259 decoder_sys_t *p_sys = dec->p_sys;
260 aom_codec_ctx_t *ctx = &p_sys->ctx;
262 for(const void *iter = NULL;; )
264 struct aom_image *img = aom_codec_get_frame(ctx, &iter);
265 if (!img)
266 break;
268 dec->fmt_out.i_codec = FindVlcChroma(img);
269 if (dec->fmt_out.i_codec == 0) {
270 msg_Err(dec, "Unsupported output colorspace %d", img->fmt);
271 continue;
274 pf_output(dec, img);
277 return VLCDEC_SUCCESS;
280 /****************************************************************************
281 * Flush: clears decoder between seeks
282 ****************************************************************************/
283 static void DropFrame(decoder_t *dec, const struct aom_image *img)
285 VLC_UNUSED(dec);
286 VLC_UNUSED(img);
287 /* do nothing for now */
290 static void FlushDecoder(decoder_t *dec)
292 decoder_sys_t *p_sys = dec->p_sys;
293 aom_codec_ctx_t *ctx = &p_sys->ctx;
295 if(PushFrame(dec, NULL) != VLCDEC_SUCCESS)
296 AOM_ERR(dec, ctx, "Failed to flush decoder");
297 else
298 PopFrames(dec, DropFrame);
301 /****************************************************************************
302 * Decode: the whole thing
303 ****************************************************************************/
304 static int Decode(decoder_t *dec, block_t *block)
306 if (block && block->i_flags & (BLOCK_FLAG_CORRUPTED))
308 block_Release(block);
309 return VLCDEC_SUCCESS;
312 int i_ret = PushFrame(dec, block);
314 PopFrames(dec, OutputFrame);
316 return i_ret;
319 /*****************************************************************************
320 * OpenDecoder: probe the decoder
321 *****************************************************************************/
322 static int OpenDecoder(vlc_object_t *p_this)
324 decoder_t *dec = (decoder_t *)p_this;
325 const aom_codec_iface_t *iface;
326 int av_version;
328 if (dec->fmt_in.i_codec != VLC_CODEC_AV1)
329 return VLC_EGENERIC;
331 iface = &aom_codec_av1_dx_algo;
332 av_version = 1;
334 decoder_sys_t *sys = malloc(sizeof(*sys));
335 if (!sys)
336 return VLC_ENOMEM;
337 dec->p_sys = sys;
339 sys->i_next_frame_priv = 0;
341 struct aom_codec_dec_cfg deccfg = {
342 .threads = __MIN(vlc_GetCPUCount(), 16),
343 .allow_lowbitdepth = 1
346 msg_Dbg(p_this, "AV%d: using libaom version %s (build options %s)",
347 av_version, aom_codec_version_str(), aom_codec_build_config());
349 if (aom_codec_dec_init(&sys->ctx, iface, &deccfg, 0) != AOM_CODEC_OK) {
350 AOM_ERR(p_this, &sys->ctx, "Failed to initialize decoder");
351 free(sys);
352 return VLC_EGENERIC;;
355 dec->pf_decode = Decode;
356 dec->pf_flush = FlushDecoder;
358 dec->fmt_out.video.i_width = dec->fmt_in.video.i_width;
359 dec->fmt_out.video.i_height = dec->fmt_in.video.i_height;
360 dec->fmt_out.i_codec = VLC_CODEC_I420;
362 if (dec->fmt_in.video.i_sar_num > 0 && dec->fmt_in.video.i_sar_den > 0) {
363 dec->fmt_out.video.i_sar_num = dec->fmt_in.video.i_sar_num;
364 dec->fmt_out.video.i_sar_den = dec->fmt_in.video.i_sar_den;
366 dec->fmt_out.video.primaries = dec->fmt_in.video.primaries;
367 dec->fmt_out.video.transfer = dec->fmt_in.video.transfer;
368 dec->fmt_out.video.space = dec->fmt_in.video.space;
369 dec->fmt_out.video.color_range = dec->fmt_in.video.color_range;
371 return VLC_SUCCESS;
374 static void destroy_context(vlc_object_t *p_this, aom_codec_ctx_t *context)
376 if (aom_codec_destroy(context))
377 AOM_ERR(p_this, context, "Failed to destroy codec context");
380 /*****************************************************************************
381 * CloseDecoder: decoder destruction
382 *****************************************************************************/
383 static void CloseDecoder(vlc_object_t *p_this)
385 decoder_t *dec = (decoder_t *)p_this;
386 decoder_sys_t *sys = dec->p_sys;
388 /* Flush decoder */
389 FlushDecoder(dec);
391 destroy_context(p_this, &sys->ctx);
393 free(sys);
396 #ifdef ENABLE_SOUT
398 /*****************************************************************************
399 * encoder_sys_t: libaom encoder descriptor
400 *****************************************************************************/
401 typedef struct
403 struct aom_codec_ctx ctx;
404 } encoder_sys_t;
406 /*****************************************************************************
407 * OpenEncoder: probe the encoder
408 *****************************************************************************/
409 static int OpenEncoder(vlc_object_t *p_this)
411 encoder_t *p_enc = (encoder_t *)p_this;
412 encoder_sys_t *p_sys;
414 if (p_enc->fmt_out.i_codec != VLC_CODEC_AV1)
415 return VLC_EGENERIC;
417 /* Allocate the memory needed to store the encoder's structure */
418 p_sys = malloc(sizeof(*p_sys));
419 if (p_sys == NULL)
420 return VLC_ENOMEM;
422 p_enc->p_sys = p_sys;
424 const struct aom_codec_iface *iface = &aom_codec_av1_cx_algo;
426 struct aom_codec_enc_cfg enccfg = {};
427 aom_codec_enc_config_default(iface, &enccfg, 0);
428 enccfg.g_timebase.num = p_enc->fmt_in.video.i_frame_rate_base;
429 enccfg.g_timebase.den = p_enc->fmt_in.video.i_frame_rate;
430 enccfg.g_threads = __MIN(vlc_GetCPUCount(), 4);
431 enccfg.g_w = p_enc->fmt_in.video.i_visible_width;
432 enccfg.g_h = p_enc->fmt_in.video.i_visible_height;
433 enccfg.g_lag_in_frames = 16; /* we have no pcr on sout */
435 int enc_flags;
436 int i_profile = var_InheritInteger( p_enc, SOUT_CFG_PREFIX "profile" );
437 int i_bit_depth = var_InheritInteger( p_enc, SOUT_CFG_PREFIX "bitdepth" );
438 int i_tile_rows = var_InheritInteger( p_enc, SOUT_CFG_PREFIX "tile-rows" );
439 int i_tile_columns = var_InheritInteger( p_enc, SOUT_CFG_PREFIX "tile-columns" );
440 #ifdef AOM_CTRL_AV1E_SET_ROW_MT
441 bool b_row_mt = var_GetBool( p_enc, SOUT_CFG_PREFIX "row-mt" );
442 #endif
444 /* TODO: implement higher profiles, bit depths and other pixformats. */
445 switch( i_profile )
447 case 0:
448 /* Main Profile: 8 and 10-bit 4:2:0. */
449 enccfg.g_profile = 0;
450 switch( i_bit_depth )
452 case 10:
453 p_enc->fmt_in.i_codec = VLC_CODEC_I420_10L;
454 enc_flags = AOM_CODEC_USE_HIGHBITDEPTH;
455 break;
456 case 8:
457 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
458 enc_flags = 0;
459 break;
460 default:
461 msg_Err( p_enc, "%d bit is unsupported for profile %d", i_bit_depth, i_profile );
462 free( p_sys );
463 return VLC_EGENERIC;
465 enccfg.g_bit_depth = i_bit_depth;
466 break;
468 case 1:
469 /* High Profile: 8 and 10-bit 4:4:4 */
470 /* fallthrough */
471 case 2:
472 /* Professional Profile: 8, 10 and 12-bit for 4:2:2, otherwise 12-bit. */
473 /* fallthrough */
474 default:
475 msg_Err( p_enc, "Unsupported profile %d", i_profile );
476 free( p_sys );
477 return VLC_EGENERIC;
480 msg_Dbg(p_this, "AV1: using libaom version %s (build options %s)",
481 aom_codec_version_str(), aom_codec_build_config());
483 struct aom_codec_ctx *ctx = &p_sys->ctx;
484 if (aom_codec_enc_init(ctx, iface, &enccfg, enc_flags) != AOM_CODEC_OK)
486 AOM_ERR(p_this, ctx, "Failed to initialize encoder");
487 free(p_sys);
488 return VLC_EGENERIC;
491 if (i_tile_rows >= 0 &&
492 aom_codec_control(ctx, AV1E_SET_TILE_ROWS, i_tile_rows))
494 AOM_ERR(p_this, ctx, "Failed to set tile rows");
495 destroy_context(p_this, ctx);
496 free(p_sys);
497 return VLC_EGENERIC;
500 if (i_tile_columns >= 0 &&
501 aom_codec_control(ctx, AV1E_SET_TILE_COLUMNS, i_tile_columns))
503 AOM_ERR(p_this, ctx, "Failed to set tile columns");
504 destroy_context(p_this, ctx);
505 free(p_sys);
506 return VLC_EGENERIC;
509 #ifdef AOM_CTRL_AV1E_SET_ROW_MT
510 if (b_row_mt &&
511 aom_codec_control(ctx, AV1E_SET_ROW_MT, b_row_mt))
513 AOM_ERR(p_this, ctx, "Failed to set row-multithreading");
514 destroy_context(p_this, ctx);
515 free(p_sys);
516 return VLC_EGENERIC;
518 #endif
520 p_enc->pf_encode_video = Encode;
522 return VLC_SUCCESS;
525 /****************************************************************************
526 * Encode: the whole thing
527 ****************************************************************************/
528 static block_t *Encode(encoder_t *p_enc, picture_t *p_pict)
530 encoder_sys_t *p_sys = p_enc->p_sys;
531 struct aom_codec_ctx *ctx = &p_sys->ctx;
533 if (!p_pict) return NULL;
535 aom_image_t img = {};
536 unsigned i_w = p_enc->fmt_in.video.i_visible_width;
537 unsigned i_h = p_enc->fmt_in.video.i_visible_height;
538 const aom_img_fmt_t img_fmt = p_enc->fmt_in.i_codec == VLC_CODEC_I420_10L ?
539 AOM_IMG_FMT_I42016 : AOM_IMG_FMT_I420;
541 /* Create and initialize the aom_image */
542 if (!aom_img_wrap(&img, img_fmt, i_w, i_h, 32, p_pict->p[0].p_pixels))
544 AOM_ERR(p_enc, ctx, "Failed to wrap image");
545 return NULL;
548 /* Correct chroma plane offsets. */
549 for (int plane = 1; plane < p_pict->i_planes; plane++) {
550 img.planes[plane] = p_pict->p[plane].p_pixels;
551 img.stride[plane] = p_pict->p[plane].i_pitch;
554 aom_codec_err_t res = aom_codec_encode(ctx, &img, US_FROM_VLC_TICK(p_pict->date), 1, 0);
555 if (res != AOM_CODEC_OK) {
556 AOM_ERR(p_enc, ctx, "Failed to encode frame");
557 aom_img_free(&img);
558 return NULL;
561 const aom_codec_cx_pkt_t *pkt = NULL;
562 aom_codec_iter_t iter = NULL;
563 block_t *p_out = NULL;
564 while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL)
566 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT)
568 int keyframe = pkt->data.frame.flags & AOM_FRAME_IS_KEY;
569 block_t *p_block = block_Alloc(pkt->data.frame.sz);
570 if (unlikely(p_block == NULL)) {
571 block_ChainRelease(p_out);
572 p_out = NULL;
573 break;
576 /* FIXME: do this in-place */
577 memcpy(p_block->p_buffer, pkt->data.frame.buf, pkt->data.frame.sz);
578 p_block->i_dts = p_block->i_pts = VLC_TICK_FROM_US(pkt->data.frame.pts);
579 if (keyframe)
580 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
581 block_ChainAppend(&p_out, p_block);
584 aom_img_free(&img);
585 return p_out;
588 /*****************************************************************************
589 * CloseEncoder: encoder destruction
590 *****************************************************************************/
591 static void CloseEncoder(vlc_object_t *p_this)
593 encoder_t *p_enc = (encoder_t *)p_this;
594 encoder_sys_t *p_sys = p_enc->p_sys;
595 destroy_context(p_this, &p_sys->ctx);
596 free(p_sys);
599 #endif /* ENABLE_SOUT */