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