/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 <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 | 152k | #define SAVE_STATUS(ctx, var) ((ctx) ? ((ctx)->err = (var)) : (var)) |
25 | | |
26 | 400k | static vpx_codec_alg_priv_t *get_alg_priv(vpx_codec_ctx_t *ctx) { |
27 | 400k | return (vpx_codec_alg_priv_t *)ctx->priv; |
28 | 400k | } |
29 | | |
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 | 8.09k | vpx_codec_flags_t flags, int ver) { |
34 | 8.09k | vpx_codec_err_t res; |
35 | | |
36 | 8.09k | if (ver != VPX_ENCODER_ABI_VERSION) |
37 | 0 | res = VPX_CODEC_ABI_MISMATCH; |
38 | 8.09k | else if (!ctx || !iface || !cfg) |
39 | 0 | res = VPX_CODEC_INVALID_PARAM; |
40 | 8.09k | else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION) |
41 | 0 | res = VPX_CODEC_ABI_MISMATCH; |
42 | 8.09k | else if (!(iface->caps & VPX_CODEC_CAP_ENCODER)) |
43 | 0 | res = VPX_CODEC_INCAPABLE; |
44 | 8.09k | else if ((flags & VPX_CODEC_USE_PSNR) && !(iface->caps & VPX_CODEC_CAP_PSNR)) |
45 | 0 | res = VPX_CODEC_INCAPABLE; |
46 | 8.09k | else if ((flags & VPX_CODEC_USE_OUTPUT_PARTITION) && |
47 | 8.09k | !(iface->caps & VPX_CODEC_CAP_OUTPUT_PARTITION)) |
48 | 0 | res = VPX_CODEC_INCAPABLE; |
49 | 8.09k | else { |
50 | 8.09k | ctx->iface = iface; |
51 | 8.09k | ctx->name = iface->name; |
52 | 8.09k | ctx->priv = NULL; |
53 | 8.09k | ctx->init_flags = flags; |
54 | 8.09k | ctx->config.enc = cfg; |
55 | 8.09k | res = ctx->iface->init(ctx, NULL); |
56 | | |
57 | 8.09k | 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 | 197 | ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL; |
63 | 197 | vpx_codec_destroy(ctx); |
64 | 197 | } |
65 | 8.09k | } |
66 | | |
67 | 8.09k | return SAVE_STATUS(ctx, res); |
68 | 8.09k | } |
69 | | |
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 | 0 | int num_enc, vpx_codec_flags_t flags, 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)) |
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 = NULL; |
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.num = dsf->num; |
110 | 0 | mr_cfg.mr_down_sampling_factor.den = dsf->den; |
111 | |
|
112 | 0 | ctx->iface = iface; |
113 | 0 | ctx->name = iface->name; |
114 | 0 | ctx->priv = NULL; |
115 | 0 | ctx->init_flags = flags; |
116 | 0 | ctx->config.enc = cfg; |
117 | 0 | res = ctx->iface->init(ctx, &mr_cfg); |
118 | 0 | } |
119 | |
|
120 | 0 | if (res) { |
121 | 0 | const char *error_detail = ctx->priv ? ctx->priv->err_detail : NULL; |
122 | | /* Destroy current ctx */ |
123 | 0 | ctx->err_detail = error_detail; |
124 | 0 | vpx_codec_destroy(ctx); |
125 | | |
126 | | /* Destroy already allocated high-level ctx */ |
127 | 0 | while (i) { |
128 | 0 | ctx--; |
129 | 0 | ctx->err_detail = error_detail; |
130 | 0 | vpx_codec_destroy(ctx); |
131 | 0 | i--; |
132 | 0 | } |
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 | 0 | return SAVE_STATUS(ctx, res); |
141 | 0 | } |
142 | | #if CONFIG_MULTI_RES_ENCODING |
143 | | mem_loc_owned = 1; |
144 | | #endif |
145 | 0 | ctx++; |
146 | 0 | cfg++; |
147 | 0 | dsf++; |
148 | 0 | } |
149 | 0 | ctx--; |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | 0 | return SAVE_STATUS(ctx, res); |
154 | 0 | } |
155 | | |
156 | | vpx_codec_err_t vpx_codec_enc_config_default(vpx_codec_iface_t *iface, |
157 | | vpx_codec_enc_cfg_t *cfg, |
158 | 8.06k | unsigned int usage) { |
159 | 8.06k | vpx_codec_err_t res; |
160 | | |
161 | 8.06k | if (!iface || !cfg || usage != 0) |
162 | 0 | res = VPX_CODEC_INVALID_PARAM; |
163 | 8.06k | else if (!(iface->caps & VPX_CODEC_CAP_ENCODER)) |
164 | 0 | res = VPX_CODEC_INCAPABLE; |
165 | 8.06k | else { |
166 | 8.06k | assert(iface->enc.cfg_map_count == 1); |
167 | 8.06k | *cfg = iface->enc.cfg_maps->cfg; |
168 | 8.06k | res = VPX_CODEC_OK; |
169 | 8.06k | } |
170 | | |
171 | 8.06k | return res; |
172 | 8.06k | } |
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 | 144k | do { \ |
181 | 144k | unsigned short x87_orig_mode = x87_set_double_precision() |
182 | | #define FLOATING_POINT_RESTORE() \ |
183 | 144k | x87_set_control_word(x87_orig_mode); \ |
184 | 144k | } \ |
185 | 144k | while (0) |
186 | | |
187 | | #else |
188 | | static void FLOATING_POINT_INIT(void) {} |
189 | | static void FLOATING_POINT_RESTORE(void) {} |
190 | | #endif |
191 | | |
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 | 144k | vpx_enc_deadline_t deadline) { |
196 | 144k | vpx_codec_err_t res = VPX_CODEC_OK; |
197 | | |
198 | 144k | if (!ctx || (img && !duration)) |
199 | 159 | res = VPX_CODEC_INVALID_PARAM; |
200 | 144k | else if (!ctx->iface || !ctx->priv) |
201 | 0 | res = VPX_CODEC_ERROR; |
202 | 144k | else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER)) |
203 | 0 | res = VPX_CODEC_INCAPABLE; |
204 | 144k | #if ULONG_MAX > UINT32_MAX |
205 | 144k | else if (duration > UINT32_MAX || deadline > UINT32_MAX) |
206 | 52 | res = VPX_CODEC_INVALID_PARAM; |
207 | 144k | #endif |
208 | 144k | else { |
209 | 144k | 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 | 144k | FLOATING_POINT_INIT(); |
215 | | |
216 | 144k | if (num_enc == 1) |
217 | 144k | res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts, duration, flags, |
218 | 144k | deadline); |
219 | 0 | 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 | 0 | int i; |
226 | |
|
227 | 0 | ctx += num_enc - 1; |
228 | 0 | if (img) img += num_enc - 1; |
229 | |
|
230 | 0 | for (i = num_enc - 1; i >= 0; i--) { |
231 | 0 | if ((res = ctx->iface->enc.encode(get_alg_priv(ctx), img, pts, duration, |
232 | 0 | flags, deadline))) |
233 | 0 | break; |
234 | | |
235 | 0 | ctx--; |
236 | 0 | if (img) img--; |
237 | 0 | } |
238 | 0 | ctx++; |
239 | 0 | } |
240 | | |
241 | 144k | FLOATING_POINT_RESTORE(); |
242 | 144k | } |
243 | | |
244 | 144k | return SAVE_STATUS(ctx, res); |
245 | 144k | } |
246 | | |
247 | | const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx, |
248 | 256k | vpx_codec_iter_t *iter) { |
249 | 256k | const vpx_codec_cx_pkt_t *pkt = NULL; |
250 | | |
251 | 256k | if (ctx) { |
252 | 256k | if (!iter) |
253 | 0 | ctx->err = VPX_CODEC_INVALID_PARAM; |
254 | 256k | else if (!ctx->iface || !ctx->priv) |
255 | 0 | ctx->err = VPX_CODEC_ERROR; |
256 | 256k | else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER)) |
257 | 0 | ctx->err = VPX_CODEC_INCAPABLE; |
258 | 256k | else |
259 | 256k | pkt = ctx->iface->enc.get_cx_data(get_alg_priv(ctx), iter); |
260 | 256k | } |
261 | | |
262 | 256k | 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 | 112k | vpx_codec_priv_t *const priv = ctx->priv; |
267 | 112k | char *const dst_buf = (char *)priv->enc.cx_data_dst_buf.buf; |
268 | | |
269 | 112k | if (dst_buf && pkt->data.raw.buf != dst_buf && |
270 | 112k | pkt->data.raw.sz + priv->enc.cx_data_pad_before + |
271 | 0 | priv->enc.cx_data_pad_after <= |
272 | 0 | priv->enc.cx_data_dst_buf.sz) { |
273 | 0 | vpx_codec_cx_pkt_t *modified_pkt = &priv->enc.cx_data_pkt; |
274 | |
|
275 | 0 | memcpy(dst_buf + priv->enc.cx_data_pad_before, pkt->data.raw.buf, |
276 | 0 | pkt->data.raw.sz); |
277 | 0 | *modified_pkt = *pkt; |
278 | 0 | modified_pkt->data.raw.buf = dst_buf; |
279 | 0 | modified_pkt->data.raw.sz += |
280 | 0 | priv->enc.cx_data_pad_before + priv->enc.cx_data_pad_after; |
281 | 0 | pkt = modified_pkt; |
282 | 0 | } |
283 | | |
284 | 112k | if (dst_buf == pkt->data.raw.buf) { |
285 | 0 | priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz; |
286 | 0 | priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz; |
287 | 0 | } |
288 | 112k | } |
289 | | |
290 | 256k | return pkt; |
291 | 256k | } |
292 | | |
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 | 0 | unsigned int pad_after) { |
297 | 0 | if (!ctx || !ctx->priv) return VPX_CODEC_INVALID_PARAM; |
298 | | |
299 | 0 | if (buf) { |
300 | 0 | ctx->priv->enc.cx_data_dst_buf = *buf; |
301 | 0 | ctx->priv->enc.cx_data_pad_before = pad_before; |
302 | 0 | ctx->priv->enc.cx_data_pad_after = pad_after; |
303 | 0 | } else { |
304 | 0 | ctx->priv->enc.cx_data_dst_buf.buf = NULL; |
305 | 0 | ctx->priv->enc.cx_data_dst_buf.sz = 0; |
306 | 0 | ctx->priv->enc.cx_data_pad_before = 0; |
307 | 0 | ctx->priv->enc.cx_data_pad_after = 0; |
308 | 0 | } |
309 | |
|
310 | 0 | return VPX_CODEC_OK; |
311 | 0 | } |
312 | | |
313 | 0 | const vpx_image_t *vpx_codec_get_preview_frame(vpx_codec_ctx_t *ctx) { |
314 | 0 | vpx_image_t *img = NULL; |
315 | |
|
316 | 0 | if (ctx) { |
317 | 0 | if (!ctx->iface || !ctx->priv) |
318 | 0 | ctx->err = VPX_CODEC_ERROR; |
319 | 0 | else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER)) |
320 | 0 | ctx->err = VPX_CODEC_INCAPABLE; |
321 | 0 | else if (!ctx->iface->enc.get_preview) |
322 | 0 | ctx->err = VPX_CODEC_INCAPABLE; |
323 | 0 | else |
324 | 0 | img = ctx->iface->enc.get_preview(get_alg_priv(ctx)); |
325 | 0 | } |
326 | |
|
327 | 0 | return img; |
328 | 0 | } |
329 | | |
330 | 0 | vpx_fixed_buf_t *vpx_codec_get_global_headers(vpx_codec_ctx_t *ctx) { |
331 | 0 | vpx_fixed_buf_t *buf = NULL; |
332 | |
|
333 | 0 | if (ctx) { |
334 | 0 | if (!ctx->iface || !ctx->priv) |
335 | 0 | ctx->err = VPX_CODEC_ERROR; |
336 | 0 | else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER)) |
337 | 0 | ctx->err = VPX_CODEC_INCAPABLE; |
338 | 0 | else if (!ctx->iface->enc.get_glob_hdrs) |
339 | 0 | ctx->err = VPX_CODEC_INCAPABLE; |
340 | 0 | else |
341 | 0 | buf = ctx->iface->enc.get_glob_hdrs(get_alg_priv(ctx)); |
342 | 0 | } |
343 | |
|
344 | 0 | return buf; |
345 | 0 | } |
346 | | |
347 | | vpx_codec_err_t vpx_codec_enc_config_set(vpx_codec_ctx_t *ctx, |
348 | 0 | const vpx_codec_enc_cfg_t *cfg) { |
349 | 0 | vpx_codec_err_t res; |
350 | |
|
351 | 0 | if (!ctx || !ctx->iface || !ctx->priv || !cfg) |
352 | 0 | res = VPX_CODEC_INVALID_PARAM; |
353 | 0 | else if (!(ctx->iface->caps & VPX_CODEC_CAP_ENCODER)) |
354 | 0 | res = VPX_CODEC_INCAPABLE; |
355 | 0 | else |
356 | 0 | res = ctx->iface->enc.cfg_set(get_alg_priv(ctx), cfg); |
357 | |
|
358 | 0 | return SAVE_STATUS(ctx, res); |
359 | 0 | } |
360 | | |
361 | | int vpx_codec_pkt_list_add(struct vpx_codec_pkt_list *list, |
362 | 112k | const struct vpx_codec_cx_pkt *pkt) { |
363 | 112k | if (list->cnt < list->max) { |
364 | 112k | list->pkts[list->cnt++] = *pkt; |
365 | 112k | return 0; |
366 | 112k | } |
367 | | |
368 | 0 | return 1; |
369 | 112k | } |
370 | | |
371 | | const vpx_codec_cx_pkt_t *vpx_codec_pkt_list_get( |
372 | 256k | struct vpx_codec_pkt_list *list, vpx_codec_iter_t *iter) { |
373 | 256k | const vpx_codec_cx_pkt_t *pkt; |
374 | | |
375 | 256k | if (!(*iter)) { |
376 | 143k | *iter = list->pkts; |
377 | 143k | } |
378 | | |
379 | 256k | pkt = (const vpx_codec_cx_pkt_t *)*iter; |
380 | | |
381 | 256k | if ((size_t)(pkt - list->pkts) < list->cnt) |
382 | 112k | *iter = pkt + 1; |
383 | 143k | else |
384 | 143k | pkt = NULL; |
385 | | |
386 | 256k | return pkt; |
387 | 256k | } |