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