imgconvert: Re-enable the deprecation warnings
[libav.git] / libavcodec / imgconvert.c
CommitLineData
de6d9b64 1/*
f1ea5c2a 2 * Misc image conversion routines
406792e7 3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
ff4ec49e
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
ff4ec49e 17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
983e3246
MN
21
22/**
ba87f080 23 * @file
f1ea5c2a 24 * misc image conversion routines
983e3246
MN
25 */
26
c50c0bc8
FB
27/* TODO:
28 * - write 'ffimg' program to test all the image related stuff
29 * - move all api to slice based system
30 * - integrate deinterlacing, postprocessing and scaling in the conversion process
31 */
983e3246 32
de6d9b64 33#include "avcodec.h"
35685a3c 34#include "imgconvert.h"
4443c0e9 35#include "internal.h"
f9279ee7 36#include "mathops.h"
2b4abbd6 37#include "libavutil/colorspace.h"
1d9c2dc8 38#include "libavutil/common.h"
8e861e1b 39#include "libavutil/pixdesc.h"
f7edcac0 40#include "libavutil/internal.h"
737eb597 41#include "libavutil/imgutils.h"
de6d9b64 42
716d413c 43void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
524c6b63 44{
50ba57e0
AK
45 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
46 *h_shift = desc->log2_chroma_w;
47 *v_shift = desc->log2_chroma_h;
524c6b63
FB
48}
49
ae3822bc
LB
50static int is_gray(const AVPixFmtDescriptor *desc)
51{
e6c4ac7b 52 return desc->nb_components - (desc->flags & AV_PIX_FMT_FLAG_ALPHA) == 1;
ae3822bc 53}
2a877875 54
ae3822bc
LB
55int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
56 enum AVPixelFormat src_pix_fmt,
b6147995
FB
57 int has_alpha)
58{
50ba57e0
AK
59 const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
60 const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
ae3822bc
LB
61 int loss, i, nb_components = FFMIN(src_desc->nb_components,
62 dst_desc->nb_components);
b6147995
FB
63
64 /* compute loss */
65 loss = 0;
ae3822bc
LB
66
67 if (dst_pix_fmt == src_pix_fmt)
68 return 0;
69
70 for (i = 0; i < nb_components; i++)
2268db2c 71 if (src_desc->comp[i].depth > dst_desc->comp[i].depth)
ae3822bc
LB
72 loss |= FF_LOSS_DEPTH;
73
14b903f3
SS
74 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
75 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
b6147995 76 loss |= FF_LOSS_RESOLUTION;
ae3822bc 77
e6c4ac7b 78 if ((src_desc->flags & AV_PIX_FMT_FLAG_RGB) != (dst_desc->flags & AV_PIX_FMT_FLAG_RGB))
ae3822bc
LB
79 loss |= FF_LOSS_COLORSPACE;
80
e6c4ac7b 81 if (has_alpha && !(dst_desc->flags & AV_PIX_FMT_FLAG_ALPHA) &&
47b447aa 82 (src_desc->flags & AV_PIX_FMT_FLAG_ALPHA))
b6147995 83 loss |= FF_LOSS_ALPHA;
b6147995 84
ae3822bc
LB
85 if (dst_pix_fmt == AV_PIX_FMT_PAL8 && !is_gray(src_desc))
86 return loss | FF_LOSS_COLORQUANT;
b6147995 87
ae3822bc
LB
88 if (src_desc->nb_components > dst_desc->nb_components)
89 if (is_gray(dst_desc))
90 loss |= FF_LOSS_CHROMA;
91
92 return loss;
b6147995
FB
93}
94
716d413c
AK
95static enum AVPixelFormat avcodec_find_best_pix_fmt1(enum AVPixelFormat *pix_fmt_list,
96 enum AVPixelFormat src_pix_fmt,
b6147995
FB
97 int has_alpha,
98 int loss_mask)
99{
a61ec8e7 100 int dist, i, loss, min_dist;
716d413c 101 enum AVPixelFormat dst_pix_fmt;
b6147995
FB
102
103 /* find exact color match with smallest size */
716d413c 104 dst_pix_fmt = AV_PIX_FMT_NONE;
b6147995 105 min_dist = 0x7fffffff;
ac29054f 106 i = 0;
716d413c
AK
107 while (pix_fmt_list[i] != AV_PIX_FMT_NONE) {
108 enum AVPixelFormat pix_fmt = pix_fmt_list[i];
ac29054f 109
716d413c 110 if (i > AV_PIX_FMT_NB) {
ac29054f
JG
111 av_log(NULL, AV_LOG_ERROR, "Pixel format list longer than expected, "
112 "it is either not properly terminated or contains duplicates\n");
716d413c 113 return AV_PIX_FMT_NONE;
ac29054f
JG
114 }
115
116 loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
117 if (loss == 0) {
ae3822bc 118 dist = av_get_bits_per_pixel(av_pix_fmt_desc_get(pix_fmt));
ac29054f
JG
119 if (dist < min_dist) {
120 min_dist = dist;
121 dst_pix_fmt = pix_fmt;
b6147995
FB
122 }
123 }
ac29054f 124 i++;
b6147995
FB
125 }
126 return dst_pix_fmt;
127}
128
716d413c
AK
129enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
130 enum AVPixelFormat src_pix_fmt,
ac29054f
JG
131 int has_alpha, int *loss_ptr)
132{
716d413c 133 enum AVPixelFormat dst_pix_fmt;
a61ec8e7 134 int loss_mask, i;
b6147995
FB
135 static const int loss_mask_order[] = {
136 ~0, /* no loss first */
137 ~FF_LOSS_ALPHA,
138 ~FF_LOSS_RESOLUTION,
139 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
140 ~FF_LOSS_COLORQUANT,
141 ~FF_LOSS_DEPTH,
142 0,
143 };
144
145 /* try with successive loss */
146 i = 0;
147 for(;;) {
148 loss_mask = loss_mask_order[i++];
ac29054f 149 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
b6147995
FB
150 has_alpha, loss_mask);
151 if (dst_pix_fmt >= 0)
152 goto found;
153 if (loss_mask == 0)
154 break;
155 }
716d413c 156 return AV_PIX_FMT_NONE;
b6147995
FB
157 found:
158 if (loss_ptr)
159 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
160 return dst_pix_fmt;
161}
162
85c242d8 163/* 2x2 -> 1x1 */
54009d42 164void ff_shrink22(uint8_t *dst, int dst_wrap,
e352ff08 165 const uint8_t *src, int src_wrap,
85c242d8
FB
166 int width, int height)
167{
168 int w;
e352ff08
FB
169 const uint8_t *s1, *s2;
170 uint8_t *d;
85c242d8
FB
171
172 for(;height > 0; height--) {
173 s1 = src;
174 s2 = s1 + src_wrap;
175 d = dst;
176 for(w = width;w >= 4; w-=4) {
0a9ad8d1
FB
177 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
178 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
179 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
180 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
85c242d8
FB
181 s1 += 8;
182 s2 += 8;
183 d += 4;
184 }
185 for(;w > 0; w--) {
0a9ad8d1 186 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
85c242d8
FB
187 s1 += 2;
188 s2 += 2;
189 d++;
190 }
191 src += 2 * src_wrap;
192 dst += dst_wrap;
193 }
194}
195
e352ff08 196/* 4x4 -> 1x1 */
54009d42 197void ff_shrink44(uint8_t *dst, int dst_wrap,
e352ff08 198 const uint8_t *src, int src_wrap,
6742d95d
FR
199 int width, int height)
200{
201 int w;
e352ff08
FB
202 const uint8_t *s1, *s2, *s3, *s4;
203 uint8_t *d;
6742d95d
FR
204
205 for(;height > 0; height--) {
206 s1 = src;
e352ff08
FB
207 s2 = s1 + src_wrap;
208 s3 = s2 + src_wrap;
209 s4 = s3 + src_wrap;
6742d95d 210 d = dst;
e352ff08
FB
211 for(w = width;w > 0; w--) {
212 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
213 s2[0] + s2[1] + s2[2] + s2[3] +
214 s3[0] + s3[1] + s3[2] + s3[3] +
215 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
216 s1 += 4;
217 s2 += 4;
218 s3 += 4;
219 s4 += 4;
6742d95d
FR
220 d++;
221 }
e352ff08
FB
222 src += 4 * src_wrap;
223 dst += dst_wrap;
224 }
225}
226
54009d42
MN
227/* 8x8 -> 1x1 */
228void ff_shrink88(uint8_t *dst, int dst_wrap,
229 const uint8_t *src, int src_wrap,
230 int width, int height)
231{
232 int w, i;
233
234 for(;height > 0; height--) {
235 for(w = width;w > 0; w--) {
236 int tmp=0;
237 for(i=0; i<8; i++){
238 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
239 src += src_wrap;
240 }
241 *(dst++) = (tmp + 32)>>6;
242 src += 8 - 8*src_wrap;
243 }
244 src += 8*src_wrap - 8*width;
245 dst += dst_wrap - width;
246 }
247}
248
c50c0bc8 249/* return true if yuv planar */
ae3822bc 250static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
c50c0bc8 251{
e6c4ac7b
AK
252 return (!(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
253 (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
c50c0bc8
FB
254}
255
dca23ffb 256#if FF_API_AVPICTURE
f7edcac0
LB
257FF_DISABLE_DEPRECATION_WARNINGS
258
636d6a4a 259int av_picture_crop(AVPicture *dst, const AVPicture *src,
716d413c 260 enum AVPixelFormat pix_fmt, int top_band, int left_band)
f2651e7a 261{
50ba57e0 262 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
f2651e7a
BC
263 int y_shift;
264 int x_shift;
265
ae3822bc 266 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(desc))
f2651e7a
BC
267 return -1;
268
50ba57e0
AK
269 y_shift = desc->log2_chroma_h;
270 x_shift = desc->log2_chroma_w;
f2651e7a
BC
271
272 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
273 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
274 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
275
276 dst->linesize[0] = src->linesize[0];
277 dst->linesize[1] = src->linesize[1];
278 dst->linesize[2] = src->linesize[2];
279 return 0;
280}
281
636d6a4a 282int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
716d413c 283 enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
6845801f 284 int *color)
5341c209 285{
50ba57e0 286 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
79acfb0e 287 uint8_t *optr;
5341c209
LA
288 int y_shift;
289 int x_shift;
290 int yheight;
291 int i, y;
292
716d413c 293 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
ae3822bc 294 !is_yuv_planar(desc)) return -1;
5341c209
LA
295
296 for (i = 0; i < 3; i++) {
50ba57e0
AK
297 x_shift = i ? desc->log2_chroma_w : 0;
298 y_shift = i ? desc->log2_chroma_h : 0;
5341c209
LA
299
300 if (padtop || padleft) {
6845801f
LB
301 memset(dst->data[i], color[i],
302 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
5341c209
LA
303 }
304
79acfb0e
LB
305 if (padleft || padright) {
306 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
307 (dst->linesize[i] - (padright >> x_shift));
308 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
309 for (y = 0; y < yheight; y++) {
310 memset(optr, color[i], (padleft + padright) >> x_shift);
311 optr += dst->linesize[i];
5341c209 312 }
79acfb0e
LB
313 }
314
315 if (src) { /* first line */
316 uint8_t *iptr = src->data[i];
317 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
318 (padleft >> x_shift);
1be97a21 319 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
79acfb0e 320 iptr += src->linesize[i];
6845801f
LB
321 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
322 (dst->linesize[i] - (padright >> x_shift));
5341c209
LA
323 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
324 for (y = 0; y < yheight; y++) {
325 memset(optr, color[i], (padleft + padright) >> x_shift);
79acfb0e 326 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1be97a21 327 (width - padleft - padright) >> x_shift);
79acfb0e 328 iptr += src->linesize[i];
5341c209
LA
329 optr += dst->linesize[i];
330 }
331 }
332
333 if (padbottom || padright) {
6845801f
LB
334 optr = dst->data[i] + dst->linesize[i] *
335 ((height - padbottom) >> y_shift) - (padright >> x_shift);
336 memset(optr, color[i],dst->linesize[i] *
337 (padbottom >> y_shift) + (padright >> x_shift));
5341c209
LA
338 }
339 }
340 return 0;
341}
f7edcac0 342
7d36474d 343FF_ENABLE_DEPRECATION_WARNINGS
dca23ffb 344#endif /* FF_API_AVPICTURE */