00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include "common.h"
00025 #include "imgutils.h"
00026 #include "internal.h"
00027 #include "intreadwrite.h"
00028 #include "log.h"
00029 #include "pixdesc.h"
00030
00031 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
00032 const AVPixFmtDescriptor *pixdesc)
00033 {
00034 int i;
00035 memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
00036 if (max_pixstep_comps)
00037 memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
00038
00039 for (i = 0; i < 4; i++) {
00040 const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
00041 if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
00042 max_pixsteps[comp->plane] = comp->step_minus1+1;
00043 if (max_pixstep_comps)
00044 max_pixstep_comps[comp->plane] = i;
00045 }
00046 }
00047 }
00048
00049 static inline
00050 int image_get_linesize(int width, int plane,
00051 int max_step, int max_step_comp,
00052 const AVPixFmtDescriptor *desc)
00053 {
00054 int s, shifted_w, linesize;
00055
00056 if (width < 0)
00057 return AVERROR(EINVAL);
00058 s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0;
00059 shifted_w = ((width + (1 << s) - 1)) >> s;
00060 if (shifted_w && max_step > INT_MAX / shifted_w)
00061 return AVERROR(EINVAL);
00062 linesize = max_step * shifted_w;
00063 if (desc->flags & PIX_FMT_BITSTREAM)
00064 linesize = (linesize + 7) >> 3;
00065 return linesize;
00066 }
00067
00068 int av_image_get_linesize(enum PixelFormat pix_fmt, int width, int plane)
00069 {
00070 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00071 int max_step [4];
00072 int max_step_comp[4];
00073
00074 if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
00075 return AVERROR(EINVAL);
00076
00077 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
00078 return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
00079 }
00080
00081 int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
00082 {
00083 int i, ret;
00084 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00085 int max_step [4];
00086 int max_step_comp[4];
00087
00088 memset(linesizes, 0, 4*sizeof(linesizes[0]));
00089
00090 if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
00091 return AVERROR(EINVAL);
00092
00093 av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
00094 for (i = 0; i < 4; i++) {
00095 if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
00096 return ret;
00097 linesizes[i] = ret;
00098 }
00099
00100 return 0;
00101 }
00102
00103 int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
00104 uint8_t *ptr, const int linesizes[4])
00105 {
00106 int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
00107
00108 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00109 memset(data , 0, sizeof(data[0])*4);
00110
00111 if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
00112 return AVERROR(EINVAL);
00113
00114 data[0] = ptr;
00115 if (linesizes[0] > (INT_MAX - 1024) / height)
00116 return AVERROR(EINVAL);
00117 size[0] = linesizes[0] * height;
00118
00119 if (desc->flags & PIX_FMT_PAL ||
00120 desc->flags & PIX_FMT_PSEUDOPAL) {
00121 size[0] = (size[0] + 3) & ~3;
00122 data[1] = ptr + size[0];
00123 return size[0] + 256 * 4;
00124 }
00125
00126 for (i = 0; i < 4; i++)
00127 has_plane[desc->comp[i].plane] = 1;
00128
00129 total_size = size[0];
00130 for (i = 1; i < 4 && has_plane[i]; i++) {
00131 int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
00132 data[i] = data[i-1] + size[i-1];
00133 h = (height + (1 << s) - 1) >> s;
00134 if (linesizes[i] > INT_MAX / h)
00135 return AVERROR(EINVAL);
00136 size[i] = h * linesizes[i];
00137 if (total_size > INT_MAX - size[i])
00138 return AVERROR(EINVAL);
00139 total_size += size[i];
00140 }
00141
00142 return total_size;
00143 }
00144
00145 int ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt)
00146 {
00147 int i;
00148
00149 for (i = 0; i < 256; i++) {
00150 int r, g, b;
00151
00152 switch (pix_fmt) {
00153 case PIX_FMT_RGB8:
00154 r = (i>>5 )*36;
00155 g = ((i>>2)&7)*36;
00156 b = (i&3 )*85;
00157 break;
00158 case PIX_FMT_BGR8:
00159 b = (i>>6 )*85;
00160 g = ((i>>3)&7)*36;
00161 r = (i&7 )*36;
00162 break;
00163 case PIX_FMT_RGB4_BYTE:
00164 r = (i>>3 )*255;
00165 g = ((i>>1)&3)*85;
00166 b = (i&1 )*255;
00167 break;
00168 case PIX_FMT_BGR4_BYTE:
00169 b = (i>>3 )*255;
00170 g = ((i>>1)&3)*85;
00171 r = (i&1 )*255;
00172 break;
00173 case PIX_FMT_GRAY8:
00174 r = b = g = i;
00175 break;
00176 default:
00177 return AVERROR(EINVAL);
00178 }
00179 pal[i] = b + (g<<8) + (r<<16) + (0xFF<<24);
00180 }
00181
00182 return 0;
00183 }
00184
00185 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
00186 int w, int h, enum PixelFormat pix_fmt, int align)
00187 {
00188 int i, ret;
00189 uint8_t *buf;
00190
00191 if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
00192 return ret;
00193 if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
00194 return ret;
00195
00196 for (i = 0; i < 4; i++)
00197 linesizes[i] = FFALIGN(linesizes[i], align);
00198
00199 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
00200 return ret;
00201 buf = av_malloc(ret + align);
00202 if (!buf)
00203 return AVERROR(ENOMEM);
00204 if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
00205 av_free(buf);
00206 return ret;
00207 }
00208 if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PAL ||
00209 av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
00210 ff_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
00211
00212 return ret;
00213 }
00214
00215 typedef struct ImgUtils {
00216 const AVClass *class;
00217 int log_offset;
00218 void *log_ctx;
00219 } ImgUtils;
00220
00221 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
00222
00223 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
00224 {
00225 ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
00226
00227 if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
00228 return 0;
00229
00230 av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
00231 return AVERROR(EINVAL);
00232 }
00233
00234 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
00235 const uint8_t *src, int src_linesize,
00236 int bytewidth, int height)
00237 {
00238 if (!dst || !src)
00239 return;
00240 for (;height > 0; height--) {
00241 memcpy(dst, src, bytewidth);
00242 dst += dst_linesize;
00243 src += src_linesize;
00244 }
00245 }
00246
00247 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
00248 const uint8_t *src_data[4], const int src_linesizes[4],
00249 enum PixelFormat pix_fmt, int width, int height)
00250 {
00251 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00252
00253 if (desc->flags & PIX_FMT_HWACCEL)
00254 return;
00255
00256 if (desc->flags & PIX_FMT_PAL ||
00257 desc->flags & PIX_FMT_PSEUDOPAL) {
00258 av_image_copy_plane(dst_data[0], dst_linesizes[0],
00259 src_data[0], src_linesizes[0],
00260 width, height);
00261
00262 memcpy(dst_data[1], src_data[1], 4*256);
00263 } else {
00264 int i, planes_nb = 0;
00265
00266 for (i = 0; i < desc->nb_components; i++)
00267 planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
00268
00269 for (i = 0; i < planes_nb; i++) {
00270 int h = height;
00271 int bwidth = av_image_get_linesize(pix_fmt, width, i);
00272 if (i == 1 || i == 2) {
00273 h= -((-height)>>desc->log2_chroma_h);
00274 }
00275 av_image_copy_plane(dst_data[i], dst_linesizes[i],
00276 src_data[i], src_linesizes[i],
00277 bwidth, h);
00278 }
00279 }
00280 }
00281
00282 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
00283 const uint8_t *src,
00284 enum PixelFormat pix_fmt, int width, int height, int align)
00285 {
00286 int ret, i;
00287
00288 if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
00289 return ret;
00290
00291 if ((ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width)) < 0)
00292 return ret;
00293
00294 for (i = 0; i < 4; i++)
00295 dst_linesize[i] = FFALIGN(dst_linesize[i], align);
00296
00297 if ((ret = av_image_fill_pointers(dst_data, pix_fmt, width, NULL, dst_linesize)) < 0)
00298 return ret;
00299
00300 return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
00301 }
00302
00303 int av_image_get_buffer_size(enum PixelFormat pix_fmt, int width, int height, int align)
00304 {
00305 uint8_t *data[4];
00306 int linesize[4];
00307 if (av_image_check_size(width, height, 0, NULL) < 0)
00308 return AVERROR(EINVAL);
00309 if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
00310
00311 return width * height;
00312 return av_image_fill_arrays(data, linesize, NULL, pix_fmt, width, height, align);
00313 }
00314
00315 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
00316 const uint8_t * const src_data[4], const int src_linesize[4],
00317 enum PixelFormat pix_fmt, int width, int height, int align)
00318 {
00319 int i, j, nb_planes = 0, linesize[4];
00320 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00321 int size = av_image_get_buffer_size(pix_fmt, width, height, align);
00322
00323 if (size > dst_size || size < 0)
00324 return AVERROR(EINVAL);
00325
00326 for (i = 0; i < desc->nb_components; i++)
00327 nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
00328 nb_planes++;
00329
00330 av_image_fill_linesizes(linesize, pix_fmt, width);
00331 for (i = 0; i < nb_planes; i++) {
00332 int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
00333 const uint8_t *src = src_data[i];
00334 h = (height + (1 << shift) - 1) >> shift;
00335
00336 for (j = 0; j < h; j++) {
00337 memcpy(dst, src, linesize[i]);
00338 dst += FFALIGN(linesize[i], align);
00339 src += src_linesize[i];
00340 }
00341 }
00342
00343 if (desc->flags & PIX_FMT_PAL) {
00344 uint32_t *d32 = (uint32_t *)(((size_t)dst + 3) & ~3);
00345 for (i = 0; i<256; i++)
00346 AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
00347 }
00348
00349 return size;
00350 }