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