• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 "pixdesc.h"
00027 
00028 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
00029                                 const AVPixFmtDescriptor *pixdesc)
00030 {
00031     int i;
00032     memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
00033     if (max_pixstep_comps)
00034         memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
00035 
00036     for (i = 0; i < 4; i++) {
00037         const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
00038         if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
00039             max_pixsteps[comp->plane] = comp->step_minus1+1;
00040             if (max_pixstep_comps)
00041                 max_pixstep_comps[comp->plane] = i;
00042         }
00043     }
00044 }
00045 
00046 static inline
00047 int image_get_linesize(int width, int plane,
00048                        int max_step, int max_step_comp,
00049                        const AVPixFmtDescriptor *desc)
00050 {
00051     int s, shifted_w, linesize;
00052 
00053     if (width < 0)
00054         return AVERROR(EINVAL);
00055     s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0;
00056     shifted_w = ((width + (1 << s) - 1)) >> s;
00057     if (shifted_w && max_step > INT_MAX / shifted_w)
00058         return AVERROR(EINVAL);
00059     linesize = max_step * shifted_w;
00060     if (desc->flags & PIX_FMT_BITSTREAM)
00061         linesize = (linesize + 7) >> 3;
00062     return linesize;
00063 }
00064 
00065 int av_image_get_linesize(enum PixelFormat pix_fmt, int width, int plane)
00066 {
00067     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00068     int max_step     [4];       /* max pixel step for each plane */
00069     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
00070 
00071     if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
00072         return AVERROR(EINVAL);
00073 
00074     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
00075     return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
00076 }
00077 
00078 int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
00079 {
00080     int i, ret;
00081     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00082     int max_step     [4];       /* max pixel step for each plane */
00083     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
00084 
00085     memset(linesizes, 0, 4*sizeof(linesizes[0]));
00086 
00087     if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
00088         return AVERROR(EINVAL);
00089 
00090     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
00091     for (i = 0; i < 4; i++) {
00092         if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
00093             return ret;
00094         linesizes[i] = ret;
00095     }
00096 
00097     return 0;
00098 }
00099 
00100 int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
00101                            uint8_t *ptr, const int linesizes[4])
00102 {
00103     int i, total_size, size[4], has_plane[4];
00104 
00105     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00106     memset(data     , 0, sizeof(data[0])*4);
00107     memset(size     , 0, sizeof(size));
00108     memset(has_plane, 0, sizeof(has_plane));
00109 
00110     if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
00111         return AVERROR(EINVAL);
00112 
00113     data[0] = ptr;
00114     if (linesizes[0] > (INT_MAX - 1024) / height)
00115         return AVERROR(EINVAL);
00116     size[0] = linesizes[0] * height;
00117 
00118     if (desc->flags & PIX_FMT_PAL) {
00119         size[0] = (size[0] + 3) & ~3;
00120         data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
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);
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, 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         ff_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
00208 
00209     return ret;
00210 }
00211 
00212 typedef struct ImgUtils {
00213     const AVClass *class;
00214     int   log_offset;
00215     void *log_ctx;
00216 } ImgUtils;
00217 
00218 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
00219 
00220 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
00221 {
00222     ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
00223 
00224     if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
00225         return 0;
00226 
00227     av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
00228     return AVERROR(EINVAL);
00229 }
00230 
00231 void av_image_copy_plane(uint8_t       *dst, int dst_linesize,
00232                          const uint8_t *src, int src_linesize,
00233                          int bytewidth, int height)
00234 {
00235     if (!dst || !src)
00236         return;
00237     for (;height > 0; height--) {
00238         memcpy(dst, src, bytewidth);
00239         dst += dst_linesize;
00240         src += src_linesize;
00241     }
00242 }
00243 
00244 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
00245                    const uint8_t *src_data[4], const int src_linesizes[4],
00246                    enum PixelFormat pix_fmt, int width, int height)
00247 {
00248     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00249 
00250     if (desc->flags & PIX_FMT_HWACCEL)
00251         return;
00252 
00253     if (desc->flags & PIX_FMT_PAL) {
00254         av_image_copy_plane(dst_data[0], dst_linesizes[0],
00255                             src_data[0], src_linesizes[0],
00256                             width, height);
00257         /* copy the palette */
00258         memcpy(dst_data[1], src_data[1], 4*256);
00259     } else {
00260         int i, planes_nb = 0;
00261 
00262         for (i = 0; i < desc->nb_components; i++)
00263             planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
00264 
00265         for (i = 0; i < planes_nb; i++) {
00266             int h = height;
00267             int bwidth = av_image_get_linesize(pix_fmt, width, i);
00268             if (i == 1 || i == 2) {
00269                 h= -((-height)>>desc->log2_chroma_h);
00270             }
00271             av_image_copy_plane(dst_data[i], dst_linesizes[i],
00272                                 src_data[i], src_linesizes[i],
00273                                 bwidth, h);
00274         }
00275     }
00276 }
00277 
00278 #if FF_API_OLD_IMAGE_NAMES
00279 void av_fill_image_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
00280                                 const AVPixFmtDescriptor *pixdesc)
00281 {
00282     av_image_fill_max_pixsteps(max_pixsteps, max_pixstep_comps, pixdesc);
00283 }
00284 
00285 int av_get_image_linesize(enum PixelFormat pix_fmt, int width, int plane)
00286 {
00287     return av_image_get_linesize(pix_fmt, width, plane);
00288 }
00289 
00290 int av_fill_image_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
00291 {
00292     return av_image_fill_linesizes(linesizes, pix_fmt, width);
00293 }
00294 
00295 int av_fill_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
00296                            uint8_t *ptr, const int linesizes[4])
00297 {
00298     return av_image_fill_pointers(data, pix_fmt, height, ptr, linesizes);
00299 }
00300 
00301 int av_check_image_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
00302 {
00303     return av_image_check_size(w, h, log_offset, log_ctx);
00304 }
00305 #endif

Generated on Wed Apr 11 2012 07:31:37 for FFmpeg by  doxygen 1.7.1