libavutil/imgutils.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of FFmpeg.
00003  *
00004  * FFmpeg is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * FFmpeg is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with FFmpeg; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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];       /* max pixel step for each plane */
00070     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
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];       /* max pixel step for each plane */
00084     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
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], has_plane[4];
00105 
00106     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00107     memset(data     , 0, sizeof(data[0])*4);
00108     memset(size     , 0, sizeof(size));
00109     memset(has_plane, 0, sizeof(has_plane));
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         size[0] = (size[0] + 3) & ~3;
00121         data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
00122         return size[0] + 256 * 4;
00123     }
00124 
00125     for (i = 0; i < 4; i++)
00126         has_plane[desc->comp[i].plane] = 1;
00127 
00128     total_size = size[0];
00129     for (i = 1; i < 4 && has_plane[i]; i++) {
00130         int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
00131         data[i] = data[i-1] + size[i-1];
00132         h = (height + (1 << s) - 1) >> s;
00133         if (linesizes[i] > INT_MAX / h)
00134             return AVERROR(EINVAL);
00135         size[i] = h * linesizes[i];
00136         if (total_size > INT_MAX - size[i])
00137             return AVERROR(EINVAL);
00138         total_size += size[i];
00139     }
00140 
00141     return total_size;
00142 }
00143 
00144 int ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt)
00145 {
00146     int i;
00147 
00148     for (i = 0; i < 256; i++) {
00149         int r, g, b;
00150 
00151         switch (pix_fmt) {
00152         case PIX_FMT_RGB8:
00153             r = (i>>5    )*36;
00154             g = ((i>>2)&7)*36;
00155             b = (i&3     )*85;
00156             break;
00157         case PIX_FMT_BGR8:
00158             b = (i>>6    )*85;
00159             g = ((i>>3)&7)*36;
00160             r = (i&7     )*36;
00161             break;
00162         case PIX_FMT_RGB4_BYTE:
00163             r = (i>>3    )*255;
00164             g = ((i>>1)&3)*85;
00165             b = (i&1     )*255;
00166             break;
00167         case PIX_FMT_BGR4_BYTE:
00168             b = (i>>3    )*255;
00169             g = ((i>>1)&3)*85;
00170             r = (i&1     )*255;
00171             break;
00172         case PIX_FMT_GRAY8:
00173             r = b = g = i;
00174             break;
00175         default:
00176             return AVERROR(EINVAL);
00177         }
00178         pal[i] = b + (g<<8) + (r<<16) + (0xFF<<24);
00179     }
00180 
00181     return 0;
00182 }
00183 
00184 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
00185                    int w, int h, enum PixelFormat pix_fmt, int align)
00186 {
00187     int i, ret;
00188     uint8_t *buf;
00189 
00190     if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
00191         return ret;
00192     if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, w)) < 0)
00193         return ret;
00194 
00195     for (i = 0; i < 4; i++)
00196         linesizes[i] = FFALIGN(linesizes[i], align);
00197 
00198     if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
00199         return ret;
00200     buf = av_malloc(ret + align);
00201     if (!buf)
00202         return AVERROR(ENOMEM);
00203     if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
00204         av_free(buf);
00205         return ret;
00206     }
00207     if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PAL)
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         av_image_copy_plane(dst_data[0], dst_linesizes[0],
00256                             src_data[0], src_linesizes[0],
00257                             width, height);
00258         /* copy the palette */
00259         memcpy(dst_data[1], src_data[1], 4*256);
00260     } else {
00261         int i, planes_nb = 0;
00262 
00263         for (i = 0; i < desc->nb_components; i++)
00264             planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
00265 
00266         for (i = 0; i < planes_nb; i++) {
00267             int h = height;
00268             int bwidth = av_image_get_linesize(pix_fmt, width, i);
00269             if (i == 1 || i == 2) {
00270                 h= -((-height)>>desc->log2_chroma_h);
00271             }
00272             av_image_copy_plane(dst_data[i], dst_linesizes[i],
00273                                 src_data[i], src_linesizes[i],
00274                                 bwidth, h);
00275         }
00276     }
00277 }