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 #ifndef AOM_AV1_ENCODER_LOOKAHEAD_H_
13 #define AOM_AV1_ENCODER_LOOKAHEAD_H_
14 
15 #include "aom_scale/yv12config.h"
16 #include "aom/aom_integer.h"
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #define MAX_LAG_BUFFERS 35
23 #define MAX_LAP_BUFFERS 35
24 #define MAX_TOTAL_BUFFERS (MAX_LAG_BUFFERS + MAX_LAP_BUFFERS)
25 #define LAP_LAG_IN_FRAMES 17
26 
27 struct lookahead_entry {
28   YV12_BUFFER_CONFIG img;
29   int64_t ts_start;
30   int64_t ts_end;
31   aom_enc_frame_flags_t flags;
32 };
33 
34 // The max of past frames we want to keep in the queue.
35 #define MAX_PRE_FRAMES 1
36 
37 enum { ENCODE_STAGE, LAP_STAGE, MAX_STAGES } UENUM1BYTE(COMPRESSOR_STAGE);
38 
39 struct read_ctx {
40   int sz;       /* Number of buffers currently in the queue */
41   int read_idx; /* Read index */
42   int pop_sz;   /* Size to check for pop condition */
43   int valid;    /* Is this ctx valid? */
44 };
45 
46 struct lookahead_ctx {
47   int max_sz;                            /* Absolute size of the queue */
48   int write_idx;                         /* Write index */
49   struct read_ctx read_ctxs[MAX_STAGES]; /* Read context */
50   struct lookahead_entry *buf;           /* Buffer list */
51 };
52 
53 /**\brief Initializes the lookahead stage
54  *
55  * The lookahead stage is a queue of frame buffers on which some analysis
56  * may be done when buffers are enqueued.
57  */
58 struct lookahead_ctx *av1_lookahead_init(
59     unsigned int width, unsigned int height, unsigned int subsampling_x,
60     unsigned int subsampling_y, int use_highbitdepth, unsigned int depth,
61     const int border_in_pixels, int byte_alignment, int num_lap_buffers);
62 
63 /**\brief Destroys the lookahead stage
64  */
65 void av1_lookahead_destroy(struct lookahead_ctx *ctx);
66 
67 /**\brief Enqueue a source buffer
68  *
69  * This function will copy the source image into a new framebuffer with
70  * the expected stride/border.
71  *
72  * If active_map is non-NULL and there is only one frame in the queue, then copy
73  * only active macroblocks.
74  *
75  * \param[in] ctx         Pointer to the lookahead context
76  * \param[in] src         Pointer to the image to enqueue
77  * \param[in] ts_start    Timestamp for the start of this frame
78  * \param[in] ts_end      Timestamp for the end of this frame
79  * \param[in] flags       Flags set on this frame
80  * \param[in] active_map  Map that specifies which macroblock is active
81  */
82 int av1_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG *src,
83                        int64_t ts_start, int64_t ts_end, int use_highbitdepth,
84                        aom_enc_frame_flags_t flags);
85 
86 /**\brief Get the next source buffer to encode
87  *
88  *
89  * \param[in] ctx       Pointer to the lookahead context
90  * \param[in] drain     Flag indicating the buffer should be drained
91  *                      (return a buffer regardless of the current queue depth)
92  *
93  * \retval NULL, if drain set and queue is empty
94  * \retval NULL, if drain not set and queue not of the configured depth
95  */
96 struct lookahead_entry *av1_lookahead_pop(struct lookahead_ctx *ctx, int drain,
97                                           COMPRESSOR_STAGE stage);
98 
99 /**\brief Get a future source buffer to encode
100  *
101  * \param[in] ctx       Pointer to the lookahead context
102  * \param[in] index     Index of the frame to be returned, 0 == next frame
103  *
104  * \retval NULL, if no buffer exists at the specified index
105  */
106 struct lookahead_entry *av1_lookahead_peek(struct lookahead_ctx *ctx, int index,
107                                            COMPRESSOR_STAGE stage);
108 
109 /**\brief Get the number of frames currently in the lookahead queue
110  *
111  * \param[in] ctx       Pointer to the lookahead context
112  */
113 unsigned int av1_lookahead_depth(struct lookahead_ctx *ctx,
114                                  COMPRESSOR_STAGE stage);
115 
116 int av1_lookahead_pop_sz(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage);
117 
118 #ifdef __cplusplus
119 }  // extern "C"
120 #endif
121 
122 #endif  // AOM_AV1_ENCODER_LOOKAHEAD_H_
123