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 /*****************************************************************************
26 *****************************************************************************/
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>
39 # include <aom/aomcx.h>
40 # include <aom/aom_image.h>
41 # define SOUT_CFG_PREFIX "sout-aom-"
44 /****************************************************************************
46 ****************************************************************************/
47 static int OpenDecoder(vlc_object_t
*);
48 static void CloseDecoder(vlc_object_t
*);
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
[] =
56 static const char *const ppsz_enc_bitdepth_text
[] =
57 { N_("8 bpp"), N_("10 bpp"), N_("12 bpp") };
60 /*****************************************************************************
62 *****************************************************************************/
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
)
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
)
84 static void aom_err_msg(vlc_object_t
*this, aom_codec_ctx_t
*ctx
,
87 const char *error
= aom_codec_error(ctx
);
88 const char *detail
= aom_codec_error_detail(ctx
);
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 *****************************************************************************/
108 struct frame_priv_s frame_priv
[AOM_MAX_FRAMES_DEPTH
];
109 unsigned i_next_frame_priv
;
114 vlc_fourcc_t i_chroma
;
115 enum aom_img_fmt i_chroma_id
;
117 uint8_t i_needs_hack
;
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
;
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
);
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
;
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
];
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
;
195 err
= aom_codec_decode(ctx
, p_buffer
, i_buffer
, priv
);
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
;
228 case AOM_CICP_MC_BT_709
:
229 v
->space
= COLOR_SPACE_BT709
;
231 case AOM_CICP_MC_BT_601
:
232 case AOM_CICP_MC_SMPTE_240
:
233 v
->space
= COLOR_SPACE_BT601
;
235 case AOM_CICP_MC_BT_2020_CL
:
236 case AOM_CICP_MC_BT_2020_NCL
:
237 v
->space
= COLOR_SPACE_BT2020
;
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
);
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 */
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
);
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
);
286 return VLCDEC_SUCCESS
;
289 /****************************************************************************
290 * Flush: clears decoder between seeks
291 ****************************************************************************/
292 static void DropFrame(decoder_t
*dec
, const struct aom_image
*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");
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
);
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
;
337 if (dec
->fmt_in
.i_codec
!= VLC_CODEC_AV1
)
340 iface
= &aom_codec_av1_dx_algo
;
343 decoder_sys_t
*sys
= malloc(sizeof(*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");
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
;
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
;
390 aom_codec_destroy(&sys
->ctx
);
397 /*****************************************************************************
398 * encoder_sys_t: libaom encoder descriptor
399 *****************************************************************************/
402 struct aom_codec_ctx ctx
;
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
)
416 /* Allocate the memory needed to store the encoder's structure */
417 p_sys
= malloc(sizeof(*p_sys
));
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
;
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. */
439 /* Main Profile: 8 and 10-bit 4:2:0. */
440 enccfg
.g_profile
= 0;
441 switch( i_bit_depth
)
444 p_enc
->fmt_in
.i_codec
= VLC_CODEC_I420_10L
;
445 enc_flags
= AOM_CODEC_USE_HIGHBITDEPTH
;
448 p_enc
->fmt_in
.i_codec
= VLC_CODEC_I420
;
452 msg_Err( p_enc
, "%d bit is unsupported for profile %d", i_bit_depth
, i_profile
);
456 enccfg
.g_bit_depth
= i_bit_depth
;
460 /* High Profile: 8 and 10-bit 4:4:4 */
463 /* Professional Profile: 8, 10 and 12-bit for 4:2:2, otherwise 12-bit. */
466 msg_Err( p_enc
, "Unsupported profile %d", i_profile
);
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");
482 p_enc
->pf_encode_video
= Encode
;
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");
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");
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
);
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
;
542 p_block
->i_flags
|= BLOCK_FLAG_TYPE_I
;
543 block_ChainAppend(&p_out
, p_block
);
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");
562 #endif /* ENABLE_SOUT */