lavc: Deprecate AVPicture structure and related functions
[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"
737eb597 40#include "libavutil/imgutils.h"
de6d9b64 41
716d413c 42void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
524c6b63 43{
50ba57e0
AK
44 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
45 *h_shift = desc->log2_chroma_w;
46 *v_shift = desc->log2_chroma_h;
524c6b63
FB
47}
48
ae3822bc
LB
49static int is_gray(const AVPixFmtDescriptor *desc)
50{
e6c4ac7b 51 return desc->nb_components - (desc->flags & AV_PIX_FMT_FLAG_ALPHA) == 1;
ae3822bc 52}
2a877875 53
ae3822bc
LB
54int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
55 enum AVPixelFormat src_pix_fmt,
b6147995
FB
56 int has_alpha)
57{
50ba57e0
AK
58 const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
59 const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
ae3822bc
LB
60 int loss, i, nb_components = FFMIN(src_desc->nb_components,
61 dst_desc->nb_components);
b6147995
FB
62
63 /* compute loss */
64 loss = 0;
ae3822bc
LB
65
66 if (dst_pix_fmt == src_pix_fmt)
67 return 0;
68
69 for (i = 0; i < nb_components; i++)
2268db2c 70 if (src_desc->comp[i].depth > dst_desc->comp[i].depth)
ae3822bc
LB
71 loss |= FF_LOSS_DEPTH;
72
14b903f3
SS
73 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
74 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
b6147995 75 loss |= FF_LOSS_RESOLUTION;
ae3822bc 76
e6c4ac7b 77 if ((src_desc->flags & AV_PIX_FMT_FLAG_RGB) != (dst_desc->flags & AV_PIX_FMT_FLAG_RGB))
ae3822bc
LB
78 loss |= FF_LOSS_COLORSPACE;
79
e6c4ac7b 80 if (has_alpha && !(dst_desc->flags & AV_PIX_FMT_FLAG_ALPHA) &&
47b447aa 81 (src_desc->flags & AV_PIX_FMT_FLAG_ALPHA))
b6147995 82 loss |= FF_LOSS_ALPHA;
b6147995 83
ae3822bc
LB
84 if (dst_pix_fmt == AV_PIX_FMT_PAL8 && !is_gray(src_desc))
85 return loss | FF_LOSS_COLORQUANT;
b6147995 86
ae3822bc
LB
87 if (src_desc->nb_components > dst_desc->nb_components)
88 if (is_gray(dst_desc))
89 loss |= FF_LOSS_CHROMA;
90
91 return loss;
b6147995
FB
92}
93
716d413c
AK
94static enum AVPixelFormat avcodec_find_best_pix_fmt1(enum AVPixelFormat *pix_fmt_list,
95 enum AVPixelFormat src_pix_fmt,
b6147995
FB
96 int has_alpha,
97 int loss_mask)
98{
a61ec8e7 99 int dist, i, loss, min_dist;
716d413c 100 enum AVPixelFormat dst_pix_fmt;
b6147995
FB
101
102 /* find exact color match with smallest size */
716d413c 103 dst_pix_fmt = AV_PIX_FMT_NONE;
b6147995 104 min_dist = 0x7fffffff;
ac29054f 105 i = 0;
716d413c
AK
106 while (pix_fmt_list[i] != AV_PIX_FMT_NONE) {
107 enum AVPixelFormat pix_fmt = pix_fmt_list[i];
ac29054f 108
716d413c 109 if (i > AV_PIX_FMT_NB) {
ac29054f
JG
110 av_log(NULL, AV_LOG_ERROR, "Pixel format list longer than expected, "
111 "it is either not properly terminated or contains duplicates\n");
716d413c 112 return AV_PIX_FMT_NONE;
ac29054f
JG
113 }
114
115 loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
116 if (loss == 0) {
ae3822bc 117 dist = av_get_bits_per_pixel(av_pix_fmt_desc_get(pix_fmt));
ac29054f
JG
118 if (dist < min_dist) {
119 min_dist = dist;
120 dst_pix_fmt = pix_fmt;
b6147995
FB
121 }
122 }
ac29054f 123 i++;
b6147995
FB
124 }
125 return dst_pix_fmt;
126}
127
716d413c
AK
128enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
129 enum AVPixelFormat src_pix_fmt,
ac29054f
JG
130 int has_alpha, int *loss_ptr)
131{
716d413c 132 enum AVPixelFormat dst_pix_fmt;
a61ec8e7 133 int loss_mask, i;
b6147995
FB
134 static const int loss_mask_order[] = {
135 ~0, /* no loss first */
136 ~FF_LOSS_ALPHA,
137 ~FF_LOSS_RESOLUTION,
138 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
139 ~FF_LOSS_COLORQUANT,
140 ~FF_LOSS_DEPTH,
141 0,
142 };
143
144 /* try with successive loss */
145 i = 0;
146 for(;;) {
147 loss_mask = loss_mask_order[i++];
ac29054f 148 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
b6147995
FB
149 has_alpha, loss_mask);
150 if (dst_pix_fmt >= 0)
151 goto found;
152 if (loss_mask == 0)
153 break;
154 }
716d413c 155 return AV_PIX_FMT_NONE;
b6147995
FB
156 found:
157 if (loss_ptr)
158 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
159 return dst_pix_fmt;
160}
161
85c242d8 162/* 2x2 -> 1x1 */
54009d42 163void ff_shrink22(uint8_t *dst, int dst_wrap,
e352ff08 164 const uint8_t *src, int src_wrap,
85c242d8
FB
165 int width, int height)
166{
167 int w;
e352ff08
FB
168 const uint8_t *s1, *s2;
169 uint8_t *d;
85c242d8
FB
170
171 for(;height > 0; height--) {
172 s1 = src;
173 s2 = s1 + src_wrap;
174 d = dst;
175 for(w = width;w >= 4; w-=4) {
0a9ad8d1
FB
176 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
177 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
178 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
179 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
85c242d8
FB
180 s1 += 8;
181 s2 += 8;
182 d += 4;
183 }
184 for(;w > 0; w--) {
0a9ad8d1 185 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
85c242d8
FB
186 s1 += 2;
187 s2 += 2;
188 d++;
189 }
190 src += 2 * src_wrap;
191 dst += dst_wrap;
192 }
193}
194
e352ff08 195/* 4x4 -> 1x1 */
54009d42 196void ff_shrink44(uint8_t *dst, int dst_wrap,
e352ff08 197 const uint8_t *src, int src_wrap,
6742d95d
FR
198 int width, int height)
199{
200 int w;
e352ff08
FB
201 const uint8_t *s1, *s2, *s3, *s4;
202 uint8_t *d;
6742d95d
FR
203
204 for(;height > 0; height--) {
205 s1 = src;
e352ff08
FB
206 s2 = s1 + src_wrap;
207 s3 = s2 + src_wrap;
208 s4 = s3 + src_wrap;
6742d95d 209 d = dst;
e352ff08
FB
210 for(w = width;w > 0; w--) {
211 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
212 s2[0] + s2[1] + s2[2] + s2[3] +
213 s3[0] + s3[1] + s3[2] + s3[3] +
214 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
215 s1 += 4;
216 s2 += 4;
217 s3 += 4;
218 s4 += 4;
6742d95d
FR
219 d++;
220 }
e352ff08
FB
221 src += 4 * src_wrap;
222 dst += dst_wrap;
223 }
224}
225
54009d42
MN
226/* 8x8 -> 1x1 */
227void ff_shrink88(uint8_t *dst, int dst_wrap,
228 const uint8_t *src, int src_wrap,
229 int width, int height)
230{
231 int w, i;
232
233 for(;height > 0; height--) {
234 for(w = width;w > 0; w--) {
235 int tmp=0;
236 for(i=0; i<8; i++){
237 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
238 src += src_wrap;
239 }
240 *(dst++) = (tmp + 32)>>6;
241 src += 8 - 8*src_wrap;
242 }
243 src += 8*src_wrap - 8*width;
244 dst += dst_wrap - width;
245 }
246}
247
c50c0bc8 248/* return true if yuv planar */
ae3822bc 249static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
c50c0bc8 250{
e6c4ac7b
AK
251 return (!(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
252 (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
c50c0bc8
FB
253}
254
dca23ffb 255#if FF_API_AVPICTURE
636d6a4a 256int av_picture_crop(AVPicture *dst, const AVPicture *src,
716d413c 257 enum AVPixelFormat pix_fmt, int top_band, int left_band)
f2651e7a 258{
50ba57e0 259 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
f2651e7a
BC
260 int y_shift;
261 int x_shift;
262
ae3822bc 263 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(desc))
f2651e7a
BC
264 return -1;
265
50ba57e0
AK
266 y_shift = desc->log2_chroma_h;
267 x_shift = desc->log2_chroma_w;
f2651e7a
BC
268
269 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
270 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
271 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
272
273 dst->linesize[0] = src->linesize[0];
274 dst->linesize[1] = src->linesize[1];
275 dst->linesize[2] = src->linesize[2];
276 return 0;
277}
278
636d6a4a 279int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
716d413c 280 enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
6845801f 281 int *color)
5341c209 282{
50ba57e0 283 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
79acfb0e 284 uint8_t *optr;
5341c209
LA
285 int y_shift;
286 int x_shift;
287 int yheight;
288 int i, y;
289
716d413c 290 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
ae3822bc 291 !is_yuv_planar(desc)) return -1;
5341c209
LA
292
293 for (i = 0; i < 3; i++) {
50ba57e0
AK
294 x_shift = i ? desc->log2_chroma_w : 0;
295 y_shift = i ? desc->log2_chroma_h : 0;
5341c209
LA
296
297 if (padtop || padleft) {
6845801f
LB
298 memset(dst->data[i], color[i],
299 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
5341c209
LA
300 }
301
79acfb0e
LB
302 if (padleft || padright) {
303 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
304 (dst->linesize[i] - (padright >> x_shift));
305 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
306 for (y = 0; y < yheight; y++) {
307 memset(optr, color[i], (padleft + padright) >> x_shift);
308 optr += dst->linesize[i];
5341c209 309 }
79acfb0e
LB
310 }
311
312 if (src) { /* first line */
313 uint8_t *iptr = src->data[i];
314 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
315 (padleft >> x_shift);
1be97a21 316 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
79acfb0e 317 iptr += src->linesize[i];
6845801f
LB
318 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
319 (dst->linesize[i] - (padright >> x_shift));
5341c209
LA
320 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
321 for (y = 0; y < yheight; y++) {
322 memset(optr, color[i], (padleft + padright) >> x_shift);
79acfb0e 323 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1be97a21 324 (width - padleft - padright) >> x_shift);
79acfb0e 325 iptr += src->linesize[i];
5341c209
LA
326 optr += dst->linesize[i];
327 }
328 }
329
330 if (padbottom || padright) {
6845801f
LB
331 optr = dst->data[i] + dst->linesize[i] *
332 ((height - padbottom) >> y_shift) - (padright >> x_shift);
333 memset(optr, color[i],dst->linesize[i] *
334 (padbottom >> y_shift) + (padright >> x_shift));
5341c209
LA
335 }
336 }
337 return 0;
338}
dca23ffb 339#endif /* FF_API_AVPICTURE */