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