xref: /aosp_15_r20/external/libvpx/vpx/src/vpx_encoder.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2010 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 /*!\file
12  * \brief Provides the high level interface to wrap encoder algorithms.
13  *
14  */
15 #include <assert.h>
16 #include <limits.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include "vp8/common/blockd.h"
21 #include "vpx_config.h"
22 #include "vpx/internal/vpx_codec_internal.h"
23 
24 #define SAVE_STATUS(ctx, var) ((ctx) ? ((ctx)->err = (var)) : (var))
25 
get_alg_priv(vpx_codec_ctx_t * ctx)26 static vpx_codec_alg_priv_t *get_alg_priv(vpx_codec_ctx_t *ctx) {
27   return (vpx_codec_alg_priv_t *)ctx->priv;
28 }
29 
vpx_codec_enc_init_ver(vpx_codec_ctx_t * ctx,vpx_codec_iface_t * iface,const vpx_codec_enc_cfg_t * cfg,vpx_codec_flags_t flags,int ver)30 vpx_codec_err_t vpx_codec_enc_init_ver(vpx_codec_ctx_t *ctx,
31                                        vpx_codec_iface_t *iface,
32                                        const vpx_codec_enc_cfg_t *cfg,
33                                        vpx_codec_flags_t flags, int ver) {
34   vpx_codec_err_t res;
35 
36   if (ver != VPX_ENCODER_ABI_VERSION)
37     res = VPX_CODEC_ABI_MISMATCH;
38   else if (!ctx || !iface || !cfg)
39     res = VPX_CODEC_INVALID_PARAM;
40   else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
41     res = VPX_CODEC_ABI_MISMATCH;
42   else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
43     res = VPX_CODEC_INCAPABLE;
44   else if ((flags & VPX_CODEC_USE_PSNR) && !(iface->caps & VPX_CODEC_CAP_PSNR))
45     res = VPX_CODEC_INCAPABLE;
46   else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION) &&
47            !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
48     res = VPX_CODEC_INCAPABLE;
49   else {
50     ctx->iface = iface;
51     ctx->name = iface->name;
52     ctx->priv = NULL;
53     ctx->init_flags = flags;
54     ctx->config.enc = cfg;
55     res = ctx->iface->init(ctx, NULL);
56 
57     if (res) {
58       // IMPORTANT: ctx->priv->err_detail must be null or point to a string
59       // that remains valid after ctx->priv is destroyed, such as a C string
60       // literal. This makes it safe to call vpx_codec_error_detail() after
61       // vpx_codec_enc_init_ver() failed.
62       ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
63       vpx_codec_destroy(ctx);
64     }
65   }
66 
67   return SAVE_STATUS(ctx, res);
68 }
69 
vpx_codec_enc_init_multi_ver(vpx_codec_ctx_t * ctx,vpx_codec_iface_t * iface,vpx_codec_enc_cfg_t * cfg,int num_enc,vpx_codec_flags_t flags,vpx_rational_t * dsf,int ver)70 vpx_codec_err_t vpx_codec_enc_init_multi_ver(
71     vpx_codec_ctx_t *ctx, vpx_codec_iface_t *iface, vpx_codec_enc_cfg_t *cfg,
72     int num_enc, vpx_codec_flags_t flags, vpx_rational_t *dsf, int ver) {
73   vpx_codec_err_t res = VPX_CODEC_OK;
74 
75   if (ver != VPX_ENCODER_ABI_VERSION)
76     res = VPX_CODEC_ABI_MISMATCH;
77   else if (!ctx || !iface || !cfg || (num_enc > 16 || num_enc < 1))
78     res = VPX_CODEC_INVALID_PARAM;
79   else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
80     res = VPX_CODEC_ABI_MISMATCH;
81   else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
82     res = VPX_CODEC_INCAPABLE;
83   else if ((flags & VPX_CODEC_USE_PSNR) && !(iface->caps & VPX_CODEC_CAP_PSNR))
84     res = VPX_CODEC_INCAPABLE;
85   else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION) &&
86            !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION))
87     res = VPX_CODEC_INCAPABLE;
88   else {
89     int i;
90 #if CONFIG_MULTI_RES_ENCODING
91     int mem_loc_owned = 0;
92 #endif
93     void *mem_loc = NULL;
94 
95     if (iface->enc.mr_get_mem_loc == NULL) return VPX_CODEC_INCAPABLE;
96 
97     if (!(res = iface->enc.mr_get_mem_loc(cfg, &mem_loc))) {
98       for (i = 0; i < num_enc; i++) {
99         vpx_codec_priv_enc_mr_cfg_t mr_cfg;
100 
101         /* Validate down-sampling factor. */
102         if (dsf->num < 1 || dsf->num > 4096 || dsf->den < 1 ||
103             dsf->den > dsf->num) {
104           res = VPX_CODEC_INVALID_PARAM;
105         } else {
106           mr_cfg.mr_low_res_mode_info = mem_loc;
107           mr_cfg.mr_total_resolutions = num_enc;
108           mr_cfg.mr_encoder_id = num_enc - 1 - i;
109           mr_cfg.mr_down_sampling_factor.num = dsf->num;
110           mr_cfg.mr_down_sampling_factor.den = dsf->den;
111 
112           ctx->iface = iface;
113           ctx->name = iface->name;
114           ctx->priv = NULL;
115           ctx->init_flags = flags;
116           ctx->config.enc = cfg;
117           res = ctx->iface->init(ctx, &mr_cfg);
118         }
119 
120         if (res) {
121           const char *error_detail = ctx->priv ? ctx->priv->err_detail : NULL;
122           /* Destroy current ctx */
123           ctx->err_detail = error_detail;
124           vpx_codec_destroy(ctx);
125 
126           /* Destroy already allocated high-level ctx */
127           while (i) {
128             ctx--;
129             ctx->err_detail = error_detail;
130             vpx_codec_destroy(ctx);
131             i--;
132           }
133 #if CONFIG_MULTI_RES_ENCODING
134           if (!mem_loc_owned) {
135             assert(mem_loc);
136             free(((LOWER_RES_FRAME_INFO *)mem_loc)->mb_info);
137             free(mem_loc);
138           }
139 #endif
140           return SAVE_STATUS(ctx, res);
141         }
142 #if CONFIG_MULTI_RES_ENCODING
143         mem_loc_owned = 1;
144 #endif
145         ctx++;
146         cfg++;
147         dsf++;
148       }
149       ctx--;
150     }
151   }
152 
153   return SAVE_STATUS(ctx, res);
154 }
155 
vpx_codec_enc_config_default(vpx_codec_iface_t * iface,vpx_codec_enc_cfg_t * cfg,unsigned int usage)156 vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface,
157                                              vpx_codec_enc_cfg_t *cfg,
158                                              unsigned int usage) {
159   vpx_codec_err_t res;
160 
161   if (!iface || !cfg || usage != 0)
162     res = VPX_CODEC_INVALID_PARAM;
163   else if (!(iface->caps & VPX_CODEC_CAP_ENCODER))
164     res = VPX_CODEC_INCAPABLE;
165   else {
166     assert(iface->enc.cfg_map_count == 1);
167     *cfg = iface->enc.cfg_maps->cfg;
168     res = VPX_CODEC_OK;
169   }
170 
171   return res;
172 }
173 
174 #if VPX_ARCH_X86 || VPX_ARCH_X86_64
175 /* On X86, disable the x87 unit's internal 80 bit precision for better
176  * consistency with the SSE unit's 64 bit precision.
177  */
178 #include "vpx_ports/x86.h"
179 #define FLOATING_POINT_INIT() \
180   do {                        \
181   unsigned short x87_orig_mode = x87_set_double_precision()
182 #define FLOATING_POINT_RESTORE()       \
183   x87_set_control_word(x87_orig_mode); \
184   }                                    \
185   while (0)
186 
187 #else
FLOATING_POINT_INIT(void)188 static void FLOATING_POINT_INIT(void) {}
FLOATING_POINT_RESTORE(void)189 static void FLOATING_POINT_RESTORE(void) {}
190 #endif
191 
vpx_codec_encode(vpx_codec_ctx_t * ctx,const vpx_image_t * img,vpx_codec_pts_t pts,unsigned long duration,vpx_enc_frame_flags_t flags,vpx_enc_deadline_t deadline)192 vpx_codec_err_t vpx_codec_encode(vpx_codec_ctx_t *ctx, const vpx_image_t *img,
193                                  vpx_codec_pts_t pts, unsigned long duration,
194                                  vpx_enc_frame_flags_t flags,
195                                  vpx_enc_deadline_t deadline) {
196   vpx_codec_err_t res = VPX_CODEC_OK;
197 
198   if (!ctx || (img && !duration))
199     res = VPX_CODEC_INVALID_PARAM;
200   else if (!ctx->iface || !ctx->priv)
201     res = VPX_CODEC_ERROR;
202   else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
203     res = VPX_CODEC_INCAPABLE;
204 #if ULONG_MAX > UINT32_MAX
205   else if (duration > UINT32_MAX || deadline > UINT32_MAX)
206     res = VPX_CODEC_INVALID_PARAM;
207 #endif
208   else {
209     unsigned int num_enc = ctx->priv->enc.total_encoders;
210 
211     /* Execute in a normalized floating point environment, if the platform
212      * requires it.
213      */
214     FLOATING_POINT_INIT();
215 
216     if (num_enc == 1)
217       res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts, duration, flags,
218                                    deadline);
219     else {
220       /* Multi-resolution encoding:
221        * Encode multi-levels in reverse order. For example,
222        * if mr_total_resolutions = 3, first encode level 2,
223        * then encode level 1, and finally encode level 0.
224        */
225       int i;
226 
227       ctx += num_enc - 1;
228       if (img) img += num_enc - 1;
229 
230       for (i = num_enc - 1; i >= 0; i--) {
231         if ((res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts, duration,
232                                           flags, deadline)))
233           break;
234 
235         ctx--;
236         if (img) img--;
237       }
238       ctx++;
239     }
240 
241     FLOATING_POINT_RESTORE();
242   }
243 
244   return SAVE_STATUS(ctx, res);
245 }
246 
vpx_codec_get_cx_data(vpx_codec_ctx_t * ctx,vpx_codec_iter_t * iter)247 const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx,
248                                                 vpx_codec_iter_t *iter) {
249   const vpx_codec_cx_pkt_t *pkt = NULL;
250 
251   if (ctx) {
252     if (!iter)
253       ctx->err = VPX_CODEC_INVALID_PARAM;
254     else if (!ctx->iface || !ctx->priv)
255       ctx->err = VPX_CODEC_ERROR;
256     else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
257       ctx->err = VPX_CODEC_INCAPABLE;
258     else
259       pkt = ctx->iface->enc.get_cx_data(get_alg_priv(ctx), iter);
260   }
261 
262   if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
263     // If the application has specified a destination area for the
264     // compressed data, and the codec has not placed the data there,
265     // and it fits, copy it.
266     vpx_codec_priv_t *const priv = ctx->priv;
267     char *const dst_buf = (char *)priv->enc.cx_data_dst_buf.buf;
268 
269     if (dst_buf && pkt->data.raw.buf != dst_buf &&
270         pkt->data.raw.sz + priv->enc.cx_data_pad_before +
271                 priv->enc.cx_data_pad_after <=
272             priv->enc.cx_data_dst_buf.sz) {
273       vpx_codec_cx_pkt_t *modified_pkt = &priv->enc.cx_data_pkt;
274 
275       memcpy(dst_buf + priv->enc.cx_data_pad_before, pkt->data.raw.buf,
276              pkt->data.raw.sz);
277       *modified_pkt = *pkt;
278       modified_pkt->data.raw.buf = dst_buf;
279       modified_pkt->data.raw.sz +=
280           priv->enc.cx_data_pad_before + priv->enc.cx_data_pad_after;
281       pkt = modified_pkt;
282     }
283 
284     if (dst_buf == pkt->data.raw.buf) {
285       priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
286       priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
287     }
288   }
289 
290   return pkt;
291 }
292 
vpx_codec_set_cx_data_buf(vpx_codec_ctx_t * ctx,const vpx_fixed_buf_t * buf,unsigned int pad_before,unsigned int pad_after)293 vpx_codec_err_t vpx_codec_set_cx_data_buf(vpx_codec_ctx_t *ctx,
294                                           const vpx_fixed_buf_t *buf,
295                                           unsigned int pad_before,
296                                           unsigned int pad_after) {
297   if (!ctx || !ctx->priv) return VPX_CODEC_INVALID_PARAM;
298 
299   if (buf) {
300     ctx->priv->enc.cx_data_dst_buf = *buf;
301     ctx->priv->enc.cx_data_pad_before = pad_before;
302     ctx->priv->enc.cx_data_pad_after = pad_after;
303   } else {
304     ctx->priv->enc.cx_data_dst_buf.buf = NULL;
305     ctx->priv->enc.cx_data_dst_buf.sz = 0;
306     ctx->priv->enc.cx_data_pad_before = 0;
307     ctx->priv->enc.cx_data_pad_after = 0;
308   }
309 
310   return VPX_CODEC_OK;
311 }
312 
vpx_codec_get_preview_frame(vpx_codec_ctx_t * ctx)313 const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t *ctx) {
314   vpx_image_t *img = NULL;
315 
316   if (ctx) {
317     if (!ctx->iface || !ctx->priv)
318       ctx->err = VPX_CODEC_ERROR;
319     else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
320       ctx->err = VPX_CODEC_INCAPABLE;
321     else if (!ctx->iface->enc.get_preview)
322       ctx->err = VPX_CODEC_INCAPABLE;
323     else
324       img = ctx->iface->enc.get_preview(get_alg_priv(ctx));
325   }
326 
327   return img;
328 }
329 
vpx_codec_get_global_headers(vpx_codec_ctx_t * ctx)330 vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t *ctx) {
331   vpx_fixed_buf_t *buf = NULL;
332 
333   if (ctx) {
334     if (!ctx->iface || !ctx->priv)
335       ctx->err = VPX_CODEC_ERROR;
336     else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
337       ctx->err = VPX_CODEC_INCAPABLE;
338     else if (!ctx->iface->enc.get_glob_hdrs)
339       ctx->err = VPX_CODEC_INCAPABLE;
340     else
341       buf = ctx->iface->enc.get_glob_hdrs(get_alg_priv(ctx));
342   }
343 
344   return buf;
345 }
346 
vpx_codec_enc_config_set(vpx_codec_ctx_t * ctx,const vpx_codec_enc_cfg_t * cfg)347 vpx_codec_err_t vpx_codec_enc_config_set(vpx_codec_ctx_t *ctx,
348                                          const vpx_codec_enc_cfg_t *cfg) {
349   vpx_codec_err_t res;
350 
351   if (!ctx || !ctx->iface || !ctx->priv || !cfg)
352     res = VPX_CODEC_INVALID_PARAM;
353   else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER))
354     res = VPX_CODEC_INCAPABLE;
355   else
356     res = ctx->iface->enc.cfg_set(get_alg_priv(ctx), cfg);
357 
358   return SAVE_STATUS(ctx, res);
359 }
360 
vpx_codec_pkt_list_add(struct vpx_codec_pkt_list * list,const struct vpx_codec_cx_pkt * pkt)361 int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list,
362                            const struct vpx_codec_cx_pkt *pkt) {
363   if (list->cnt < list->max) {
364     list->pkts[list->cnt++] = *pkt;
365     return 0;
366   }
367 
368   return 1;
369 }
370 
vpx_codec_pkt_list_get(struct vpx_codec_pkt_list * list,vpx_codec_iter_t * iter)371 const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get(
372     struct vpx_codec_pkt_list *list, vpx_codec_iter_t *iter) {
373   const vpx_codec_cx_pkt_t *pkt;
374 
375   if (!(*iter)) {
376     *iter = list->pkts;
377   }
378 
379   pkt = (const vpx_codec_cx_pkt_t *)*iter;
380 
381   if ((size_t)(pkt - list->pkts) < list->cnt)
382     *iter = pkt + 1;
383   else
384     pkt = NULL;
385 
386   return pkt;
387 }
388