1*77c1e3ccSAndroid Build Coastguard Worker /* 2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved. 3*77c1e3ccSAndroid Build Coastguard Worker * 4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and 5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License 6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can 7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open 8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the 9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent. 10*77c1e3ccSAndroid Build Coastguard Worker */ 11*77c1e3ccSAndroid Build Coastguard Worker 12*77c1e3ccSAndroid Build Coastguard Worker /*!\file 13*77c1e3ccSAndroid Build Coastguard Worker * \brief Describes look ahead buffer operations. 14*77c1e3ccSAndroid Build Coastguard Worker */ 15*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AV1_ENCODER_LOOKAHEAD_H_ 16*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AV1_ENCODER_LOOKAHEAD_H_ 17*77c1e3ccSAndroid Build Coastguard Worker 18*77c1e3ccSAndroid Build Coastguard Worker #include <stdbool.h> 19*77c1e3ccSAndroid Build Coastguard Worker 20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_scale/yv12config.h" 21*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h" 22*77c1e3ccSAndroid Build Coastguard Worker 23*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus 24*77c1e3ccSAndroid Build Coastguard Worker extern "C" { 25*77c1e3ccSAndroid Build Coastguard Worker #endif 26*77c1e3ccSAndroid Build Coastguard Worker 27*77c1e3ccSAndroid Build Coastguard Worker /*!\cond */ 28*77c1e3ccSAndroid Build Coastguard Worker #define MAX_LAG_BUFFERS 48 29*77c1e3ccSAndroid Build Coastguard Worker #define MAX_LAP_BUFFERS 48 30*77c1e3ccSAndroid Build Coastguard Worker #define MAX_TOTAL_BUFFERS (MAX_LAG_BUFFERS + MAX_LAP_BUFFERS) 31*77c1e3ccSAndroid Build Coastguard Worker #define LAP_LAG_IN_FRAMES 17 32*77c1e3ccSAndroid Build Coastguard Worker 33*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_entry { 34*77c1e3ccSAndroid Build Coastguard Worker YV12_BUFFER_CONFIG img; 35*77c1e3ccSAndroid Build Coastguard Worker int64_t ts_start; 36*77c1e3ccSAndroid Build Coastguard Worker int64_t ts_end; 37*77c1e3ccSAndroid Build Coastguard Worker int display_idx; 38*77c1e3ccSAndroid Build Coastguard Worker aom_enc_frame_flags_t flags; 39*77c1e3ccSAndroid Build Coastguard Worker }; 40*77c1e3ccSAndroid Build Coastguard Worker 41*77c1e3ccSAndroid Build Coastguard Worker // The max of past frames we want to keep in the queue. 42*77c1e3ccSAndroid Build Coastguard Worker #define MAX_PRE_FRAMES 1 43*77c1e3ccSAndroid Build Coastguard Worker 44*77c1e3ccSAndroid Build Coastguard Worker enum { ENCODE_STAGE, LAP_STAGE, MAX_STAGES } UENUM1BYTE(COMPRESSOR_STAGE); 45*77c1e3ccSAndroid Build Coastguard Worker 46*77c1e3ccSAndroid Build Coastguard Worker struct read_ctx { 47*77c1e3ccSAndroid Build Coastguard Worker int sz; /* Number of buffers currently in the queue */ 48*77c1e3ccSAndroid Build Coastguard Worker int read_idx; /* Read index */ 49*77c1e3ccSAndroid Build Coastguard Worker int pop_sz; /* Size to check for pop condition */ 50*77c1e3ccSAndroid Build Coastguard Worker int valid; /* Is this ctx valid? */ 51*77c1e3ccSAndroid Build Coastguard Worker }; 52*77c1e3ccSAndroid Build Coastguard Worker 53*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_ctx { 54*77c1e3ccSAndroid Build Coastguard Worker int max_sz; /* Absolute size of the queue */ 55*77c1e3ccSAndroid Build Coastguard Worker int write_idx; /* Write index */ 56*77c1e3ccSAndroid Build Coastguard Worker struct read_ctx read_ctxs[MAX_STAGES]; /* Read context */ 57*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_entry *buf; /* Buffer list */ 58*77c1e3ccSAndroid Build Coastguard Worker int push_frame_count; /* Number of frames that have been pushed in the queue*/ 59*77c1e3ccSAndroid Build Coastguard Worker uint8_t 60*77c1e3ccSAndroid Build Coastguard Worker max_pre_frames; /* Maximum number of past frames allowed in the queue */ 61*77c1e3ccSAndroid Build Coastguard Worker }; 62*77c1e3ccSAndroid Build Coastguard Worker /*!\endcond */ 63*77c1e3ccSAndroid Build Coastguard Worker 64*77c1e3ccSAndroid Build Coastguard Worker /**\brief Initializes the lookahead stage 65*77c1e3ccSAndroid Build Coastguard Worker * 66*77c1e3ccSAndroid Build Coastguard Worker * The lookahead stage is a queue of frame buffers on which some analysis 67*77c1e3ccSAndroid Build Coastguard Worker * may be done when buffers are enqueued. 68*77c1e3ccSAndroid Build Coastguard Worker */ 69*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_ctx *av1_lookahead_init( 70*77c1e3ccSAndroid Build Coastguard Worker unsigned int width, unsigned int height, unsigned int subsampling_x, 71*77c1e3ccSAndroid Build Coastguard Worker unsigned int subsampling_y, int use_highbitdepth, unsigned int depth, 72*77c1e3ccSAndroid Build Coastguard Worker const int border_in_pixels, int byte_alignment, int num_lap_buffers, 73*77c1e3ccSAndroid Build Coastguard Worker bool is_all_intra, bool alloc_pyramid); 74*77c1e3ccSAndroid Build Coastguard Worker 75*77c1e3ccSAndroid Build Coastguard Worker /**\brief Destroys the lookahead stage 76*77c1e3ccSAndroid Build Coastguard Worker */ 77*77c1e3ccSAndroid Build Coastguard Worker void av1_lookahead_destroy(struct lookahead_ctx *ctx); 78*77c1e3ccSAndroid Build Coastguard Worker 79*77c1e3ccSAndroid Build Coastguard Worker /**\brief Check if lookahead buffer is full 80*77c1e3ccSAndroid Build Coastguard Worker */ 81*77c1e3ccSAndroid Build Coastguard Worker int av1_lookahead_full(const struct lookahead_ctx *ctx); 82*77c1e3ccSAndroid Build Coastguard Worker 83*77c1e3ccSAndroid Build Coastguard Worker /**\brief Enqueue a source buffer 84*77c1e3ccSAndroid Build Coastguard Worker * 85*77c1e3ccSAndroid Build Coastguard Worker * This function will copy the source image into a new framebuffer with 86*77c1e3ccSAndroid Build Coastguard Worker * the expected stride/border. 87*77c1e3ccSAndroid Build Coastguard Worker * 88*77c1e3ccSAndroid Build Coastguard Worker * \param[in] ctx Pointer to the lookahead context 89*77c1e3ccSAndroid Build Coastguard Worker * \param[in] src Pointer to the image to enqueue 90*77c1e3ccSAndroid Build Coastguard Worker * \param[in] ts_start Timestamp for the start of this frame 91*77c1e3ccSAndroid Build Coastguard Worker * \param[in] ts_end Timestamp for the end of this frame 92*77c1e3ccSAndroid Build Coastguard Worker * \param[in] use_highbitdepth Tell if HBD is used 93*77c1e3ccSAndroid Build Coastguard Worker * \param[in] alloc_pyramid Whether to allocate a downsampling pyramid 94*77c1e3ccSAndroid Build Coastguard Worker * for each frame buffer 95*77c1e3ccSAndroid Build Coastguard Worker * \param[in] flags Flags set on this frame 96*77c1e3ccSAndroid Build Coastguard Worker */ 97*77c1e3ccSAndroid Build Coastguard Worker int av1_lookahead_push(struct lookahead_ctx *ctx, const YV12_BUFFER_CONFIG *src, 98*77c1e3ccSAndroid Build Coastguard Worker int64_t ts_start, int64_t ts_end, int use_highbitdepth, 99*77c1e3ccSAndroid Build Coastguard Worker bool alloc_pyramid, aom_enc_frame_flags_t flags); 100*77c1e3ccSAndroid Build Coastguard Worker 101*77c1e3ccSAndroid Build Coastguard Worker /**\brief Get the next source buffer to encode 102*77c1e3ccSAndroid Build Coastguard Worker * 103*77c1e3ccSAndroid Build Coastguard Worker * \param[in] ctx Pointer to the lookahead context 104*77c1e3ccSAndroid Build Coastguard Worker * \param[in] drain Flag indicating the buffer should be drained 105*77c1e3ccSAndroid Build Coastguard Worker * (return a buffer regardless of the current queue depth) 106*77c1e3ccSAndroid Build Coastguard Worker * \param[in] stage Encoder stage 107*77c1e3ccSAndroid Build Coastguard Worker * 108*77c1e3ccSAndroid Build Coastguard Worker * \retval Return NULL, if drain set and queue is empty, or if drain not set and 109*77c1e3ccSAndroid Build Coastguard Worker * queue not of the configured depth. 110*77c1e3ccSAndroid Build Coastguard Worker */ 111*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_entry *av1_lookahead_pop(struct lookahead_ctx *ctx, int drain, 112*77c1e3ccSAndroid Build Coastguard Worker COMPRESSOR_STAGE stage); 113*77c1e3ccSAndroid Build Coastguard Worker 114*77c1e3ccSAndroid Build Coastguard Worker /**\brief Get a future source buffer to encode 115*77c1e3ccSAndroid Build Coastguard Worker * 116*77c1e3ccSAndroid Build Coastguard Worker * \param[in] ctx Pointer to the lookahead context 117*77c1e3ccSAndroid Build Coastguard Worker * \param[in] index Index of the frame to be returned, 0 == next frame 118*77c1e3ccSAndroid Build Coastguard Worker * \param[in] stage Encoder stage 119*77c1e3ccSAndroid Build Coastguard Worker * 120*77c1e3ccSAndroid Build Coastguard Worker * \retval Return NULL, if no buffer exists at the specified index 121*77c1e3ccSAndroid Build Coastguard Worker */ 122*77c1e3ccSAndroid Build Coastguard Worker struct lookahead_entry *av1_lookahead_peek(struct lookahead_ctx *ctx, int index, 123*77c1e3ccSAndroid Build Coastguard Worker COMPRESSOR_STAGE stage); 124*77c1e3ccSAndroid Build Coastguard Worker 125*77c1e3ccSAndroid Build Coastguard Worker /**\brief Get the number of frames currently in the lookahead queue 126*77c1e3ccSAndroid Build Coastguard Worker */ 127*77c1e3ccSAndroid Build Coastguard Worker unsigned int av1_lookahead_depth(struct lookahead_ctx *ctx, 128*77c1e3ccSAndroid Build Coastguard Worker COMPRESSOR_STAGE stage); 129*77c1e3ccSAndroid Build Coastguard Worker 130*77c1e3ccSAndroid Build Coastguard Worker /**\brief Get pop_sz value 131*77c1e3ccSAndroid Build Coastguard Worker */ 132*77c1e3ccSAndroid Build Coastguard Worker int av1_lookahead_pop_sz(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage); 133*77c1e3ccSAndroid Build Coastguard Worker 134*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus 135*77c1e3ccSAndroid Build Coastguard Worker } // extern "C" 136*77c1e3ccSAndroid Build Coastguard Worker #endif 137*77c1e3ccSAndroid Build Coastguard Worker 138*77c1e3ccSAndroid Build Coastguard Worker #endif // AOM_AV1_ENCODER_LOOKAHEAD_H_ 139