]> git.sesse.net Git - ffmpeg/blob - libavcodec/imgconvert.c
Merge commit '11c9bd633f635f07a762be1ecd672de55daf4edc'
[ffmpeg] / libavcodec / imgconvert.c
1 /*
2  * Misc image conversion routines
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * misc image conversion routines
25  */
26
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  */
32
33 #include "avcodec.h"
34 #include "imgconvert.h"
35 #include "internal.h"
36 #include "mathops.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/colorspace.h"
39 #include "libavutil/common.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/internal.h"
42 #include "libavutil/imgutils.h"
43
44 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
45 {
46     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
47     av_assert0(desc);
48     *h_shift = desc->log2_chroma_w;
49     *v_shift = desc->log2_chroma_h;
50 }
51
52 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
53                              enum AVPixelFormat src_pix_fmt,
54                              int has_alpha)
55 {
56     return av_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
57 }
58
59 enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
60                                             enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
61 {
62     return av_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
63 }
64
65 #if AV_HAVE_INCOMPATIBLE_LIBAV_ABI
66 enum AVPixelFormat avcodec_find_best_pix_fmt2(const enum AVPixelFormat *pix_fmt_list,
67                                             enum AVPixelFormat src_pix_fmt,
68                                             int has_alpha, int *loss_ptr){
69     return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
70 }
71 #else
72 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
73                                             enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
74 {
75     return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
76 }
77 #endif
78
79 enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(const enum AVPixelFormat *pix_fmt_list,
80                                             enum AVPixelFormat src_pix_fmt,
81                                             int has_alpha, int *loss_ptr){
82     int i;
83
84     enum AVPixelFormat best = AV_PIX_FMT_NONE;
85
86     for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
87         best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
88
89     return best;
90 }
91
92 /* 2x2 -> 1x1 */
93 void ff_shrink22(uint8_t *dst, int dst_wrap,
94                      const uint8_t *src, int src_wrap,
95                      int width, int height)
96 {
97     int w;
98     const uint8_t *s1, *s2;
99     uint8_t *d;
100
101     for(;height > 0; height--) {
102         s1 = src;
103         s2 = s1 + src_wrap;
104         d = dst;
105         for(w = width;w >= 4; w-=4) {
106             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
107             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
108             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
109             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
110             s1 += 8;
111             s2 += 8;
112             d += 4;
113         }
114         for(;w > 0; w--) {
115             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
116             s1 += 2;
117             s2 += 2;
118             d++;
119         }
120         src += 2 * src_wrap;
121         dst += dst_wrap;
122     }
123 }
124
125 /* 4x4 -> 1x1 */
126 void ff_shrink44(uint8_t *dst, int dst_wrap,
127                      const uint8_t *src, int src_wrap,
128                      int width, int height)
129 {
130     int w;
131     const uint8_t *s1, *s2, *s3, *s4;
132     uint8_t *d;
133
134     for(;height > 0; height--) {
135         s1 = src;
136         s2 = s1 + src_wrap;
137         s3 = s2 + src_wrap;
138         s4 = s3 + src_wrap;
139         d = dst;
140         for(w = width;w > 0; w--) {
141             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
142                     s2[0] + s2[1] + s2[2] + s2[3] +
143                     s3[0] + s3[1] + s3[2] + s3[3] +
144                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
145             s1 += 4;
146             s2 += 4;
147             s3 += 4;
148             s4 += 4;
149             d++;
150         }
151         src += 4 * src_wrap;
152         dst += dst_wrap;
153     }
154 }
155
156 /* 8x8 -> 1x1 */
157 void ff_shrink88(uint8_t *dst, int dst_wrap,
158                      const uint8_t *src, int src_wrap,
159                      int width, int height)
160 {
161     int w, i;
162
163     for(;height > 0; height--) {
164         for(w = width;w > 0; w--) {
165             int tmp=0;
166             for(i=0; i<8; i++){
167                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
168                 src += src_wrap;
169             }
170             *(dst++) = (tmp + 32)>>6;
171             src += 8 - 8*src_wrap;
172         }
173         src += 8*src_wrap - 8*width;
174         dst += dst_wrap - width;
175     }
176 }
177
178 /* return true if yuv planar */
179 static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
180 {
181     int i;
182     int planes[4] = { 0 };
183
184     if (     desc->flags & AV_PIX_FMT_FLAG_RGB
185         || !(desc->flags & AV_PIX_FMT_FLAG_PLANAR))
186         return 0;
187
188     /* set the used planes */
189     for (i = 0; i < desc->nb_components; i++)
190         planes[desc->comp[i].plane] = 1;
191
192     /* if there is an unused plane, the format is not planar */
193     for (i = 0; i < desc->nb_components; i++)
194         if (!planes[i])
195             return 0;
196     return 1;
197 }
198
199 #if FF_API_AVPICTURE
200 FF_DISABLE_DEPRECATION_WARNINGS
201
202 int av_picture_crop(AVPicture *dst, const AVPicture *src,
203                     enum AVPixelFormat pix_fmt, int top_band, int left_band)
204 {
205     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
206     int y_shift;
207     int x_shift;
208     int max_step[4];
209
210     if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
211         return -1;
212
213     y_shift = desc->log2_chroma_h;
214     x_shift = desc->log2_chroma_w;
215     av_image_fill_max_pixsteps(max_step, NULL, desc);
216
217     if (is_yuv_planar(desc)) {
218     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
219     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
220     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
221     } else{
222         if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
223             return -1;
224         dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + (left_band * max_step[0]);
225     }
226
227     dst->linesize[0] = src->linesize[0];
228     dst->linesize[1] = src->linesize[1];
229     dst->linesize[2] = src->linesize[2];
230     return 0;
231 }
232
233 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
234                    enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
235             int *color)
236 {
237     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
238     uint8_t *optr;
239     int y_shift;
240     int x_shift;
241     int yheight;
242     int i, y;
243     int max_step[4];
244
245     if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
246         return -1;
247
248     if (!is_yuv_planar(desc)) {
249         if (src)
250             return -1; //TODO: Not yet implemented
251
252         av_image_fill_max_pixsteps(max_step, NULL, desc);
253
254         if (padtop || padleft) {
255             memset(dst->data[0], color[0],
256                     dst->linesize[0] * padtop + (padleft * max_step[0]));
257         }
258
259         if (padleft || padright) {
260             optr = dst->data[0] + dst->linesize[0] * padtop +
261                     (dst->linesize[0] - (padright * max_step[0]));
262             yheight = height - 1 - (padtop + padbottom);
263             for (y = 0; y < yheight; y++) {
264                 memset(optr, color[0], (padleft + padright) * max_step[0]);
265                 optr += dst->linesize[0];
266             }
267         }
268
269         if (padbottom || padright) {
270             optr = dst->data[0] + dst->linesize[0] * (height - padbottom) -
271                     (padright * max_step[0]);
272             memset(optr, color[0], dst->linesize[0] * padbottom +
273                     (padright * max_step[0]));
274         }
275
276         return 0;
277     }
278
279     for (i = 0; i < 3; i++) {
280         x_shift = i ? desc->log2_chroma_w : 0;
281         y_shift = i ? desc->log2_chroma_h : 0;
282
283         if (padtop || padleft) {
284             memset(dst->data[i], color[i],
285                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
286         }
287
288         if (padleft || padright) {
289             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
290                 (dst->linesize[i] - (padright >> x_shift));
291             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
292             for (y = 0; y < yheight; y++) {
293                 memset(optr, color[i], (padleft + padright) >> x_shift);
294                 optr += dst->linesize[i];
295             }
296         }
297
298         if (src) { /* first line */
299             uint8_t *iptr = src->data[i];
300             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
301                     (padleft >> x_shift);
302             memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
303             iptr += src->linesize[i];
304             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
305                 (dst->linesize[i] - (padright >> x_shift));
306             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
307             for (y = 0; y < yheight; y++) {
308                 memset(optr, color[i], (padleft + padright) >> x_shift);
309                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
310                        (width - padleft - padright) >> x_shift);
311                 iptr += src->linesize[i];
312                 optr += dst->linesize[i];
313             }
314         }
315
316         if (padbottom || padright) {
317             optr = dst->data[i] + dst->linesize[i] *
318                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
319             memset(optr, color[i],dst->linesize[i] *
320                 (padbottom >> y_shift) + (padright >> x_shift));
321         }
322     }
323
324     return 0;
325 }
326
327 #ifdef TEST
328
329 int main(void){
330     int i;
331     int err=0;
332     int skip = 0;
333
334     for (i=0; i<AV_PIX_FMT_NB*2; i++) {
335         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
336         if(!desc || !desc->name) {
337             skip ++;
338             continue;
339         }
340         if (skip) {
341             av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip);
342             skip = 0;
343         }
344         av_log(NULL, AV_LOG_INFO, "pix fmt %s yuv_plan:%d avg_bpp:%d\n", desc->name, is_yuv_planar(desc), av_get_padded_bits_per_pixel(desc));
345         if ((!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) {
346             av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n");
347             err = 1;
348         }
349     }
350     return err;
351 }
352
353 #endif
354 FF_ENABLE_DEPRECATION_WARNINGS
355 #endif /* FF_API_AVPICTURE */