input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / codec / aom.c
blobb256a8ada2948b60730b0402e96c229553f747c3
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 /****************************************************************************
45 * Local prototypes
46 ****************************************************************************/
47 static int OpenDecoder(vlc_object_t *);
48 static void CloseDecoder(vlc_object_t *);
49 #ifdef ENABLE_SOUT
50 static int OpenEncoder(vlc_object_t *);
51 static void CloseEncoder(vlc_object_t *);
52 static block_t *Encode(encoder_t *p_enc, picture_t *p_pict);
54 static const int pi_enc_bitdepth_values_list[] =
55 { 8, 10, 12 };
56 static const char *const ppsz_enc_bitdepth_text [] =
57 { N_("8 bpp"), N_("10 bpp"), N_("12 bpp") };
58 #endif
60 /*****************************************************************************
61 * Module descriptor
62 *****************************************************************************/
64 vlc_module_begin ()
65 set_shortname("aom")
66 set_description(N_("AOM video decoder"))
67 set_capability("video decoder", 100)
68 set_callbacks(OpenDecoder, CloseDecoder)
69 set_category(CAT_INPUT)
70 set_subcategory(SUBCAT_INPUT_VCODEC)
71 #ifdef ENABLE_SOUT
72 add_submodule()
73 set_shortname("aom")
74 set_capability("encoder", 101)
75 set_description(N_("AOM video encoder"))
76 set_callbacks(OpenEncoder, CloseEncoder)
77 add_integer( SOUT_CFG_PREFIX "profile", 0, "Profile", NULL, true )
78 change_integer_range( 0, 3 )
79 add_integer( SOUT_CFG_PREFIX "bitdepth", 8, "Bit Depth", NULL, true )
80 change_integer_list( pi_enc_bitdepth_values_list, ppsz_enc_bitdepth_text )
81 #endif
82 vlc_module_end ()
84 static void aom_err_msg(vlc_object_t *this, aom_codec_ctx_t *ctx,
85 const char *msg)
87 const char *error = aom_codec_error(ctx);
88 const char *detail = aom_codec_error_detail(ctx);
89 if (!detail)
90 detail = "no specific information";
91 msg_Err(this, msg, error, detail);
94 #define AOM_ERR(this, ctx, msg) aom_err_msg(VLC_OBJECT(this), ctx, msg ": %s (%s)")
95 #define AOM_MAX_FRAMES_DEPTH 64
97 /*****************************************************************************
98 * decoder_sys_t: libaom decoder descriptor
99 *****************************************************************************/
100 struct frame_priv_s
102 vlc_tick_t pts;
105 typedef struct
107 aom_codec_ctx_t ctx;
108 struct frame_priv_s frame_priv[AOM_MAX_FRAMES_DEPTH];
109 unsigned i_next_frame_priv;
110 } decoder_sys_t;
112 static const struct
114 vlc_fourcc_t i_chroma;
115 enum aom_img_fmt i_chroma_id;
116 uint8_t i_bitdepth;
117 uint8_t i_needs_hack;
119 } chroma_table[] =
121 { VLC_CODEC_I420, AOM_IMG_FMT_I420, 8, 0 },
122 { VLC_CODEC_I422, AOM_IMG_FMT_I422, 8, 0 },
123 { VLC_CODEC_I444, AOM_IMG_FMT_I444, 8, 0 },
125 { VLC_CODEC_YV12, AOM_IMG_FMT_YV12, 8, 0 },
126 { VLC_CODEC_YUVA, AOM_IMG_FMT_444A, 8, 0 },
128 { VLC_CODEC_GBR_PLANAR, AOM_IMG_FMT_I444, 8, 1 },
129 { VLC_CODEC_GBR_PLANAR_10L, AOM_IMG_FMT_I44416, 10, 1 },
131 { VLC_CODEC_I420_10L, AOM_IMG_FMT_I42016, 10, 0 },
132 { VLC_CODEC_I422_10L, AOM_IMG_FMT_I42216, 10, 0 },
133 { VLC_CODEC_I444_10L, AOM_IMG_FMT_I44416, 10, 0 },
135 { VLC_CODEC_I420_12L, AOM_IMG_FMT_I42016, 12, 0 },
136 { VLC_CODEC_I422_12L, AOM_IMG_FMT_I42216, 12, 0 },
137 { VLC_CODEC_I444_12L, AOM_IMG_FMT_I44416, 12, 0 },
139 { VLC_CODEC_I444_16L, AOM_IMG_FMT_I44416, 16, 0 },
142 static vlc_fourcc_t FindVlcChroma( struct aom_image *img )
144 uint8_t hack = (img->fmt & AOM_IMG_FMT_I444) && (img->tc == AOM_CICP_TC_SRGB);
146 for( unsigned int i = 0; i < ARRAY_SIZE(chroma_table); i++ )
147 if( chroma_table[i].i_chroma_id == img->fmt &&
148 chroma_table[i].i_bitdepth == img->bit_depth &&
149 chroma_table[i].i_needs_hack == hack )
150 return chroma_table[i].i_chroma;
152 return 0;
155 static void CopyPicture(const struct aom_image *img, picture_t *pic)
157 for (int plane = 0; plane < pic->i_planes; plane++ ) {
158 uint8_t *src = img->planes[plane];
159 uint8_t *dst = pic->p[plane].p_pixels;
160 int src_stride = img->stride[plane];
161 int dst_stride = pic->p[plane].i_pitch;
163 int size = __MIN( src_stride, dst_stride );
164 for( int line = 0; line < pic->p[plane].i_visible_lines; line++ ) {
165 memcpy( dst, src, size );
166 src += src_stride;
167 dst += dst_stride;
172 static int PushFrame(decoder_t *dec, block_t *block)
174 decoder_sys_t *p_sys = dec->p_sys;
175 aom_codec_ctx_t *ctx = &p_sys->ctx;
176 const uint8_t *p_buffer;
177 size_t i_buffer;
179 /* Associate packet PTS with decoded frame */
180 struct frame_priv_s *priv = &p_sys->frame_priv[p_sys->i_next_frame_priv++ % AOM_MAX_FRAMES_DEPTH];
182 if(likely(block))
184 p_buffer = block->p_buffer;
185 i_buffer = block->i_buffer;
186 priv->pts = (block->i_pts != VLC_TICK_INVALID) ? block->i_pts : block->i_dts;
188 else
190 p_buffer = NULL;
191 i_buffer = 0;
194 aom_codec_err_t err;
195 err = aom_codec_decode(ctx, p_buffer, i_buffer, priv);
197 if(block)
198 block_Release(block);
200 if (err != AOM_CODEC_OK) {
201 AOM_ERR(dec, ctx, "Failed to decode frame");
202 if (err == AOM_CODEC_UNSUP_BITSTREAM)
203 return VLCDEC_ECRITICAL;
205 return VLCDEC_SUCCESS;
208 static void OutputFrame(decoder_t *dec, const struct aom_image *img)
210 video_format_t *v = &dec->fmt_out.video;
212 if (img->d_w != v->i_visible_width || img->d_h != v->i_visible_height)
214 v->i_visible_width = dec->fmt_out.video.i_width = img->d_w;
215 v->i_visible_height = dec->fmt_out.video.i_height = img->d_h;
218 if( !dec->fmt_out.video.i_sar_num || !dec->fmt_out.video.i_sar_den )
220 dec->fmt_out.video.i_sar_num = 1;
221 dec->fmt_out.video.i_sar_den = 1;
224 v->b_color_range_full = img->range == AOM_CR_FULL_RANGE;
226 switch( img->mc )
228 case AOM_CICP_MC_BT_709:
229 v->space = COLOR_SPACE_BT709;
230 break;
231 case AOM_CICP_MC_BT_601:
232 case AOM_CICP_MC_SMPTE_240:
233 v->space = COLOR_SPACE_BT601;
234 break;
235 case AOM_CICP_MC_BT_2020_CL:
236 case AOM_CICP_MC_BT_2020_NCL:
237 v->space = COLOR_SPACE_BT2020;
238 break;
239 default:
240 break;
243 dec->fmt_out.video.projection_mode = dec->fmt_in.video.projection_mode;
244 dec->fmt_out.video.multiview_mode = dec->fmt_in.video.multiview_mode;
245 dec->fmt_out.video.pose = dec->fmt_in.video.pose;
247 if (decoder_UpdateVideoFormat(dec) == VLC_SUCCESS)
249 picture_t *pic = decoder_NewPicture(dec);
250 if (pic)
252 CopyPicture(img, pic);
254 /* fetches back the PTS */
255 vlc_tick_t pts = ((struct frame_priv_s *) img->user_priv)->pts;
257 pic->b_progressive = true; /* codec does not support interlacing */
258 pic->date = pts;
260 decoder_QueueVideo(dec, pic);
265 static int PopFrames(decoder_t *dec,
266 void(*pf_output)(decoder_t *, const struct aom_image *))
268 decoder_sys_t *p_sys = dec->p_sys;
269 aom_codec_ctx_t *ctx = &p_sys->ctx;
271 for(const void *iter = NULL;; )
273 struct aom_image *img = aom_codec_get_frame(ctx, &iter);
274 if (!img)
275 break;
277 dec->fmt_out.i_codec = FindVlcChroma(img);
278 if (dec->fmt_out.i_codec == 0) {
279 msg_Err(dec, "Unsupported output colorspace %d", img->fmt);
280 continue;
283 pf_output(dec, img);
286 return VLCDEC_SUCCESS;
289 /****************************************************************************
290 * Flush: clears decoder between seeks
291 ****************************************************************************/
292 static void DropFrame(decoder_t *dec, const struct aom_image *img)
294 VLC_UNUSED(dec);
295 VLC_UNUSED(img);
296 /* do nothing for now */
299 static void FlushDecoder(decoder_t *dec)
301 decoder_sys_t *p_sys = dec->p_sys;
302 aom_codec_ctx_t *ctx = &p_sys->ctx;
304 if(PushFrame(dec, NULL) != VLCDEC_SUCCESS)
305 AOM_ERR(dec, ctx, "Failed to flush decoder");
306 else
307 PopFrames(dec, DropFrame);
310 /****************************************************************************
311 * Decode: the whole thing
312 ****************************************************************************/
313 static int Decode(decoder_t *dec, block_t *block)
315 if (block && block->i_flags & (BLOCK_FLAG_CORRUPTED))
317 block_Release(block);
318 return VLCDEC_SUCCESS;
321 int i_ret = PushFrame(dec, block);
323 PopFrames(dec, OutputFrame);
325 return i_ret;
328 /*****************************************************************************
329 * OpenDecoder: probe the decoder
330 *****************************************************************************/
331 static int OpenDecoder(vlc_object_t *p_this)
333 decoder_t *dec = (decoder_t *)p_this;
334 const aom_codec_iface_t *iface;
335 int av_version;
337 if (dec->fmt_in.i_codec != VLC_CODEC_AV1)
338 return VLC_EGENERIC;
340 iface = &aom_codec_av1_dx_algo;
341 av_version = 1;
343 decoder_sys_t *sys = malloc(sizeof(*sys));
344 if (!sys)
345 return VLC_ENOMEM;
346 dec->p_sys = sys;
348 sys->i_next_frame_priv = 0;
350 struct aom_codec_dec_cfg deccfg = {
351 .threads = __MIN(vlc_GetCPUCount(), 16),
352 .allow_lowbitdepth = 1
355 msg_Dbg(p_this, "AV%d: using libaom version %s (build options %s)",
356 av_version, aom_codec_version_str(), aom_codec_build_config());
358 if (aom_codec_dec_init(&sys->ctx, iface, &deccfg, 0) != AOM_CODEC_OK) {
359 AOM_ERR(p_this, &sys->ctx, "Failed to initialize decoder");
360 free(sys);
361 return VLC_EGENERIC;;
364 dec->pf_decode = Decode;
365 dec->pf_flush = FlushDecoder;
367 dec->fmt_out.video.i_width = dec->fmt_in.video.i_width;
368 dec->fmt_out.video.i_height = dec->fmt_in.video.i_height;
369 dec->fmt_out.i_codec = VLC_CODEC_I420;
371 if (dec->fmt_in.video.i_sar_num > 0 && dec->fmt_in.video.i_sar_den > 0) {
372 dec->fmt_out.video.i_sar_num = dec->fmt_in.video.i_sar_num;
373 dec->fmt_out.video.i_sar_den = dec->fmt_in.video.i_sar_den;
376 return VLC_SUCCESS;
379 /*****************************************************************************
380 * CloseDecoder: decoder destruction
381 *****************************************************************************/
382 static void CloseDecoder(vlc_object_t *p_this)
384 decoder_t *dec = (decoder_t *)p_this;
385 decoder_sys_t *sys = dec->p_sys;
387 /* Flush decoder */
388 FlushDecoder(dec);
390 aom_codec_destroy(&sys->ctx);
392 free(sys);
395 #ifdef ENABLE_SOUT
397 /*****************************************************************************
398 * encoder_sys_t: libaom encoder descriptor
399 *****************************************************************************/
400 typedef struct
402 struct aom_codec_ctx ctx;
403 } encoder_sys_t;
405 /*****************************************************************************
406 * OpenEncoder: probe the encoder
407 *****************************************************************************/
408 static int OpenEncoder(vlc_object_t *p_this)
410 encoder_t *p_enc = (encoder_t *)p_this;
411 encoder_sys_t *p_sys;
413 if (p_enc->fmt_out.i_codec != VLC_CODEC_AV1)
414 return VLC_EGENERIC;
416 /* Allocate the memory needed to store the encoder's structure */
417 p_sys = malloc(sizeof(*p_sys));
418 if (p_sys == NULL)
419 return VLC_ENOMEM;
421 p_enc->p_sys = p_sys;
423 const struct aom_codec_iface *iface = &aom_codec_av1_cx_algo;
425 struct aom_codec_enc_cfg enccfg = {};
426 aom_codec_enc_config_default(iface, &enccfg, 0);
427 enccfg.g_threads = __MIN(vlc_GetCPUCount(), 4);
428 enccfg.g_w = p_enc->fmt_in.video.i_visible_width;
429 enccfg.g_h = p_enc->fmt_in.video.i_visible_height;
431 int enc_flags;
432 int i_profile = var_InheritInteger( p_enc, SOUT_CFG_PREFIX "profile" );
433 int i_bit_depth = var_InheritInteger( p_enc, SOUT_CFG_PREFIX "bitdepth" );
435 /* TODO: implement higher profiles, bit depths and other pixformats. */
436 switch( i_profile )
438 case 0:
439 /* Main Profile: 8 and 10-bit 4:2:0. */
440 enccfg.g_profile = 0;
441 switch( i_bit_depth )
443 case 10:
444 p_enc->fmt_in.i_codec = VLC_CODEC_I420_10L;
445 enc_flags = AOM_CODEC_USE_HIGHBITDEPTH;
446 break;
447 case 8:
448 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
449 enc_flags = 0;
450 break;
451 default:
452 msg_Err( p_enc, "%d bit is unsupported for profile %d", i_bit_depth, i_profile );
453 free( p_sys );
454 return VLC_EGENERIC;
456 enccfg.g_bit_depth = i_bit_depth;
457 break;
459 case 1:
460 /* High Profile: 8 and 10-bit 4:4:4 */
461 /* fallthrough */
462 case 2:
463 /* Professional Profile: 8, 10 and 12-bit for 4:2:2, otherwise 12-bit. */
464 /* fallthrough */
465 default:
466 msg_Err( p_enc, "Unsupported profile %d", i_profile );
467 free( p_sys );
468 return VLC_EGENERIC;
471 msg_Dbg(p_this, "AV1: using libaom version %s (build options %s)",
472 aom_codec_version_str(), aom_codec_build_config());
474 struct aom_codec_ctx *ctx = &p_sys->ctx;
475 if (aom_codec_enc_init(ctx, iface, &enccfg, enc_flags) != AOM_CODEC_OK)
477 AOM_ERR(p_this, ctx, "Failed to initialize encoder");
478 free(p_sys);
479 return VLC_EGENERIC;
482 p_enc->pf_encode_video = Encode;
484 return VLC_SUCCESS;
487 /****************************************************************************
488 * Encode: the whole thing
489 ****************************************************************************/
490 static block_t *Encode(encoder_t *p_enc, picture_t *p_pict)
492 encoder_sys_t *p_sys = p_enc->p_sys;
493 struct aom_codec_ctx *ctx = &p_sys->ctx;
495 if (!p_pict) return NULL;
497 aom_image_t img = {};
498 unsigned i_w = p_enc->fmt_in.video.i_visible_width;
499 unsigned i_h = p_enc->fmt_in.video.i_visible_height;
500 const aom_img_fmt_t img_fmt = p_enc->fmt_in.i_codec == VLC_CODEC_I420_10L ?
501 AOM_IMG_FMT_I42016 : AOM_IMG_FMT_I420;
503 /* Create and initialize the aom_image */
504 if (!aom_img_wrap(&img, img_fmt, i_w, i_h, 32, p_pict->p[0].p_pixels))
506 AOM_ERR(p_enc, ctx, "Failed to wrap image");
507 return NULL;
510 /* Correct chroma plane offsets. */
511 for (int plane = 1; plane < p_pict->i_planes; plane++) {
512 img.planes[plane] = p_pict->p[plane].p_pixels;
513 img.stride[plane] = p_pict->p[plane].i_pitch;
516 aom_codec_err_t res = aom_codec_encode(ctx, &img, p_pict->date, 1, 0);
517 if (res != AOM_CODEC_OK) {
518 AOM_ERR(p_enc, ctx, "Failed to encode frame");
519 aom_img_free(&img);
520 return NULL;
523 const aom_codec_cx_pkt_t *pkt = NULL;
524 aom_codec_iter_t iter = NULL;
525 block_t *p_out = NULL;
526 while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL)
528 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT)
530 int keyframe = pkt->data.frame.flags & AOM_FRAME_IS_KEY;
531 block_t *p_block = block_Alloc(pkt->data.frame.sz);
532 if (unlikely(p_block == NULL)) {
533 block_ChainRelease(p_out);
534 p_out = NULL;
535 break;
538 /* FIXME: do this in-place */
539 memcpy(p_block->p_buffer, pkt->data.frame.buf, pkt->data.frame.sz);
540 p_block->i_dts = p_block->i_pts = pkt->data.frame.pts;
541 if (keyframe)
542 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
543 block_ChainAppend(&p_out, p_block);
546 aom_img_free(&img);
547 return p_out;
550 /*****************************************************************************
551 * CloseEncoder: encoder destruction
552 *****************************************************************************/
553 static void CloseEncoder(vlc_object_t *p_this)
555 encoder_t *p_enc = (encoder_t *)p_this;
556 encoder_sys_t *p_sys = p_enc->p_sys;
557 if (aom_codec_destroy(&p_sys->ctx))
558 AOM_ERR(p_this, &p_sys->ctx, "Failed to destroy codec");
559 free(p_sys);
562 #endif /* ENABLE_SOUT */