Libav
avpicture.c
Go to the documentation of this file.
1 /*
2  * AVPicture management routines
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #include "avcodec.h"
28 #include "internal.h"
29 #include "libavutil/common.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/colorspace.h"
33 
34 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
35  enum AVPixelFormat pix_fmt, int width, int height)
36 {
37  int ret;
38 
39  if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
40  return ret;
41 
42  if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
43  return ret;
44 
45  return av_image_fill_pointers(picture->data, pix_fmt,
46  height, ptr, picture->linesize);
47 }
48 
50  int width, int height,
51  unsigned char *dest, int dest_size)
52 {
53  int i, j, nb_planes = 0, linesizes[4];
54  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
55  int size = avpicture_get_size(pix_fmt, width, height);
56 
57  if (size > dest_size || size < 0)
58  return AVERROR(EINVAL);
59 
60  for (i = 0; i < desc->nb_components; i++)
61  nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
62 
63  nb_planes++;
64 
65  av_image_fill_linesizes(linesizes, pix_fmt, width);
66  for (i = 0; i < nb_planes; i++) {
67  int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
68  const unsigned char *s = src->data[i];
69  h = (height + (1 << shift) - 1) >> shift;
70 
71  for (j = 0; j < h; j++) {
72  memcpy(dest, s, linesizes[i]);
73  dest += linesizes[i];
74  s += src->linesize[i];
75  }
76  }
77 
78  if (desc->flags & AV_PIX_FMT_FLAG_PAL)
79  memcpy((unsigned char *)(((size_t)dest + 3) & ~3),
80  src->data[1], 256 * 4);
81 
82  return size;
83 }
84 
86 {
87  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
88  AVPicture dummy_pict;
89  int ret;
90 
91  if (!desc)
92  return AVERROR(EINVAL);
93  if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
94  return ret;
95  if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
96  // do not include palette for these pseudo-paletted formats
97  return width * height;
98  return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
99 }
100 
102  enum AVPixelFormat pix_fmt, int width, int height)
103 {
104  int ret = av_image_alloc(picture->data, picture->linesize,
105  width, height, pix_fmt, 1);
106  if (ret < 0) {
107  memset(picture, 0, sizeof(AVPicture));
108  return ret;
109  }
110 
111  return 0;
112 }
113 
114 void avpicture_free(AVPicture *picture)
115 {
116  av_free(picture->data[0]);
117 }
118 
119 void av_picture_copy(AVPicture *dst, const AVPicture *src,
120  enum AVPixelFormat pix_fmt, int width, int height)
121 {
122  av_image_copy(dst->data, dst->linesize, src->data,
123  src->linesize, pix_fmt, width, height);
124 }
125 
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:112
int size
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
Definition: avcodec.h:3022
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1599
int avpicture_fill(AVPicture *picture, uint8_t *ptr, enum AVPixelFormat pix_fmt, int width, int height)
Fill in the AVPicture fields.
Definition: avpicture.c:34
misc image utilities
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
Definition: imgutils.c:181
Various defines for YUV<->RGB conversion.
int avpicture_layout(const AVPicture *src, enum AVPixelFormat pix_fmt, int width, int height, unsigned char *dest, int dest_size)
Copy pixel data from an AVPicture into a buffer.
Definition: avpicture.c:49
four components are given, that's all.
Definition: avcodec.h:3020
void av_picture_copy(AVPicture *dst, const AVPicture *src, enum AVPixelFormat pix_fmt, int width, int height)
Copy image src to dst.
Definition: avpicture.c:119
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:97
uint8_t
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition: avcodec.h:3021
void avpicture_free(AVPicture *picture)
Free a picture previously allocated by avpicture_alloc().
Definition: avpicture.c:114
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
#define AVERROR(e)
Definition: error.h:43
#define FFMAX(a, b)
Definition: common.h:55
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:267
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:222
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
#define AV_PIX_FMT_FLAG_PSEUDOPAL
The pixel format is "pseudo-paletted".
Definition: pixdesc.h:134
enum AVPixelFormat pix_fmt
Definition: movenc.c:843
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height, uint8_t *ptr, const int linesizes[4])
Fill plane data pointers for an image with pixel format pix_fmt and height height.
Definition: imgutils.c:99
Libavcodec external API header.
uint8_t flags
Definition: pixdesc.h:90
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
int avpicture_alloc(AVPicture *picture, enum AVPixelFormat pix_fmt, int width, int height)
Allocate memory for a picture.
Definition: avpicture.c:101
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:68
int height
Definition: gxfenc.c:72
uint16_t plane
Which of the 4 planes contains the component.
Definition: pixdesc.h:34
common internal api header.
common internal and external API header
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: avpicture.c:85
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63