• 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 Libav.
00003  *
00004  * Libav 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  * Libav 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 Libav; 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 int av_image_get_linesize(enum PixelFormat pix_fmt, int width, int plane)
00048 {
00049     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00050     int max_step     [4];       /* max pixel step for each plane */
00051     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
00052     int s;
00053 
00054     if (desc->flags & PIX_FMT_BITSTREAM)
00055         return (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
00056 
00057     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
00058     s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
00059     return max_step[plane] * (((width + (1 << s) - 1)) >> s);
00060 }
00061 
00062 int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
00063 {
00064     int i;
00065     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00066     int max_step     [4];       /* max pixel step for each plane */
00067     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
00068 
00069     memset(linesizes, 0, 4*sizeof(linesizes[0]));
00070 
00071     if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
00072         return AVERROR(EINVAL);
00073 
00074     if (desc->flags & PIX_FMT_BITSTREAM) {
00075         if (width > (INT_MAX -7) / (desc->comp[0].step_minus1+1))
00076             return AVERROR(EINVAL);
00077         linesizes[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
00078         return 0;
00079     }
00080 
00081     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
00082     for (i = 0; i < 4; i++) {
00083         int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
00084         int shifted_w = ((width + (1 << s) - 1)) >> s;
00085         if (max_step[i] > INT_MAX / shifted_w)
00086             return AVERROR(EINVAL);
00087         linesizes[i] = max_step[i] * shifted_w;
00088     }
00089 
00090     return 0;
00091 }
00092 
00093 int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
00094                            uint8_t *ptr, const int linesizes[4])
00095 {
00096     int i, total_size, size[4], has_plane[4];
00097 
00098     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00099     memset(data     , 0, sizeof(data[0])*4);
00100     memset(size     , 0, sizeof(size));
00101     memset(has_plane, 0, sizeof(has_plane));
00102 
00103     if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
00104         return AVERROR(EINVAL);
00105 
00106     data[0] = ptr;
00107     if (linesizes[0] > (INT_MAX - 1024) / height)
00108         return AVERROR(EINVAL);
00109     size[0] = linesizes[0] * height;
00110 
00111     if (desc->flags & PIX_FMT_PAL) {
00112         size[0] = (size[0] + 3) & ~3;
00113         data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
00114         return size[0] + 256 * 4;
00115     }
00116 
00117     for (i = 0; i < 4; i++)
00118         has_plane[desc->comp[i].plane] = 1;
00119 
00120     total_size = size[0];
00121     for (i = 1; i < 4 && has_plane[i]; i++) {
00122         int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
00123         data[i] = data[i-1] + size[i-1];
00124         h = (height + (1 << s) - 1) >> s;
00125         if (linesizes[i] > INT_MAX / h)
00126             return AVERROR(EINVAL);
00127         size[i] = h * linesizes[i];
00128         if (total_size > INT_MAX - size[i])
00129             return AVERROR(EINVAL);
00130         total_size += size[i];
00131     }
00132 
00133     return total_size;
00134 }
00135 
00136 int ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt)
00137 {
00138     int i;
00139 
00140     for (i = 0; i < 256; i++) {
00141         int r, g, b;
00142 
00143         switch (pix_fmt) {
00144         case PIX_FMT_RGB8:
00145             r = (i>>5    )*36;
00146             g = ((i>>2)&7)*36;
00147             b = (i&3     )*85;
00148             break;
00149         case PIX_FMT_BGR8:
00150             b = (i>>6    )*85;
00151             g = ((i>>3)&7)*36;
00152             r = (i&7     )*36;
00153             break;
00154         case PIX_FMT_RGB4_BYTE:
00155             r = (i>>3    )*255;
00156             g = ((i>>1)&3)*85;
00157             b = (i&1     )*255;
00158             break;
00159         case PIX_FMT_BGR4_BYTE:
00160             b = (i>>3    )*255;
00161             g = ((i>>1)&3)*85;
00162             r = (i&1     )*255;
00163             break;
00164         case PIX_FMT_GRAY8:
00165             r = b = g = i;
00166             break;
00167         default:
00168             return AVERROR(EINVAL);
00169         }
00170         pal[i] = b + (g<<8) + (r<<16);
00171     }
00172 
00173     return 0;
00174 }
00175 
00176 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
00177                    int w, int h, enum PixelFormat pix_fmt, int align)
00178 {
00179     int i, ret;
00180     uint8_t *buf;
00181 
00182     if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
00183         return ret;
00184     if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, w)) < 0)
00185         return ret;
00186 
00187     for (i = 0; i < 4; i++)
00188         linesizes[i] = FFALIGN(linesizes[i], align);
00189 
00190     if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
00191         return ret;
00192     buf = av_malloc(ret + align);
00193     if (!buf)
00194         return AVERROR(ENOMEM);
00195     if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
00196         av_free(buf);
00197         return ret;
00198     }
00199     if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PAL)
00200         ff_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
00201 
00202     return ret;
00203 }
00204 
00205 typedef struct ImgUtils {
00206     const AVClass *class;
00207     int   log_offset;
00208     void *log_ctx;
00209 } ImgUtils;
00210 
00211 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
00212 
00213 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
00214 {
00215     ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
00216 
00217     if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
00218         return 0;
00219 
00220     av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
00221     return AVERROR(EINVAL);
00222 }
00223 
00224 void av_image_copy_plane(uint8_t       *dst, int dst_linesize,
00225                          const uint8_t *src, int src_linesize,
00226                          int bytewidth, int height)
00227 {
00228     if (!dst || !src)
00229         return;
00230     for (;height > 0; height--) {
00231         memcpy(dst, src, bytewidth);
00232         dst += dst_linesize;
00233         src += src_linesize;
00234     }
00235 }
00236 
00237 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
00238                    const uint8_t *src_data[4], const int src_linesizes[4],
00239                    enum PixelFormat pix_fmt, int width, int height)
00240 {
00241     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
00242 
00243     if (desc->flags & PIX_FMT_HWACCEL)
00244         return;
00245 
00246     if (desc->flags & PIX_FMT_PAL) {
00247         av_image_copy_plane(dst_data[0], dst_linesizes[0],
00248                             src_data[0], src_linesizes[0],
00249                             width, height);
00250         /* copy the palette */
00251         memcpy(dst_data[1], src_data[1], 4*256);
00252     } else {
00253         int i, planes_nb = 0;
00254 
00255         for (i = 0; i < desc->nb_components; i++)
00256             planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
00257 
00258         for (i = 0; i < planes_nb; i++) {
00259             int h = height;
00260             int bwidth = av_image_get_linesize(pix_fmt, width, i);
00261             if (i == 1 || i == 2) {
00262                 h= -((-height)>>desc->log2_chroma_h);
00263             }
00264             av_image_copy_plane(dst_data[i], dst_linesizes[i],
00265                                 src_data[i], src_linesizes[i],
00266                                 bwidth, h);
00267         }
00268     }
00269 }
Generated on Sun Apr 22 2012 21:54:09 for Libav by doxygen 1.7.1