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