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