1 /*
2  *  Copyright (c) 2011 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef VPX_VP9_ENCODER_VP9_LOOKAHEAD_H_
12 #define VPX_VP9_ENCODER_VP9_LOOKAHEAD_H_
13 
14 #include "vpx_scale/yv12config.h"
15 #include "vpx/vpx_encoder.h"
16 #include "vpx/vpx_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   int show_idx; /*The show_idx of this frame*/
29   vpx_enc_frame_flags_t flags;
30 };
31 
32 // The max of past frames we want to keep in the queue.
33 #define MAX_PRE_FRAMES 1
34 
35 struct lookahead_ctx {
36   int max_sz;        /* Absolute size of the queue */
37   int sz;            /* Number of buffers currently in the queue */
38   int read_idx;      /* Read index */
39   int write_idx;     /* Write index */
40   int next_show_idx; /* The show_idx that will be assigned to the next frame
41                         being pushed in the queue*/
42   struct lookahead_entry *buf; /* Buffer list */
43 };
44 
45 /**\brief Initializes the lookahead stage
46  *
47  * The lookahead stage is a queue of frame buffers on which some analysis
48  * may be done when buffers are enqueued.
49  */
50 struct lookahead_ctx *vp9_lookahead_init(unsigned int width,
51                                          unsigned int height,
52                                          unsigned int subsampling_x,
53                                          unsigned int subsampling_y,
54 #if CONFIG_VP9_HIGHBITDEPTH
55                                          int use_highbitdepth,
56 #endif
57                                          unsigned int depth);
58 
59 /**\brief Destroys the lookahead stage
60  */
61 void vp9_lookahead_destroy(struct lookahead_ctx *ctx);
62 
63 /**\brief Check if lookahead is full
64  *
65  * \param[in] ctx         Pointer to the lookahead context
66  *
67  * Return 1 if lookahead is full, otherwise return 0.
68  */
69 int vp9_lookahead_full(const struct lookahead_ctx *ctx);
70 
71 /**\brief Return the next_show_idx
72  *
73  * \param[in] ctx         Pointer to the lookahead context
74  *
75  * Return the show_idx that will be assigned to the next
76  * frame pushed by vp9_lookahead_push()
77  */
78 int vp9_lookahead_next_show_idx(const struct lookahead_ctx *ctx);
79 
80 /**\brief Enqueue a source buffer
81  *
82  * This function will copy the source image into a new framebuffer with
83  * the expected stride/border.
84  *
85  * If active_map is non-NULL and there is only one frame in the queue, then copy
86  * only active macroblocks.
87  *
88  * \param[in] ctx         Pointer to the lookahead context
89  * \param[in] src         Pointer to the image to enqueue
90  * \param[in] ts_start    Timestamp for the start of this frame
91  * \param[in] ts_end      Timestamp for the end of this frame
92  * \param[in] flags       Flags set on this frame
93  * \param[in] active_map  Map that specifies which macroblock is active
94  */
95 int vp9_lookahead_push(struct lookahead_ctx *ctx, YV12_BUFFER_CONFIG *src,
96                        int64_t ts_start, int64_t ts_end, int use_highbitdepth,
97                        vpx_enc_frame_flags_t flags);
98 
99 /**\brief Get the next source buffer to encode
100  *
101  *
102  * \param[in] ctx       Pointer to the lookahead context
103  * \param[in] drain     Flag indicating the buffer should be drained
104  *                      (return a buffer regardless of the current queue depth)
105  *
106  * \retval NULL, if drain set and queue is empty
107  * \retval NULL, if drain not set and queue not of the configured depth
108  */
109 struct lookahead_entry *vp9_lookahead_pop(struct lookahead_ctx *ctx, int drain);
110 
111 /**\brief Get a future source buffer to encode
112  *
113  * \param[in] ctx       Pointer to the lookahead context
114  * \param[in] index     Index of the frame to be returned, 0 == next frame
115  *
116  * \retval NULL, if no buffer exists at the specified index
117  */
118 struct lookahead_entry *vp9_lookahead_peek(struct lookahead_ctx *ctx,
119                                            int index);
120 
121 /**\brief Get the number of frames currently in the lookahead queue
122  *
123  * \param[in] ctx       Pointer to the lookahead context
124  */
125 unsigned int vp9_lookahead_depth(struct lookahead_ctx *ctx);
126 
127 #ifdef __cplusplus
128 }  // extern "C"
129 #endif
130 
131 #endif  // VPX_VP9_ENCODER_VP9_LOOKAHEAD_H_
132