FFmpeg  2.1.1
mp_image.c
Go to the documentation of this file.
1 /*
2  * This file is part of MPlayer.
3  *
4  * MPlayer is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * MPlayer is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include "config.h"
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #if HAVE_MALLOC_H
26 #include <malloc.h>
27 #endif
28 
29 #include "img_format.h"
30 #include "mp_image.h"
31 
32 #include "libvo/fastmemcpy.h"
33 //#include "libavutil/mem.h"
34 #include "libavutil/imgutils.h"
35 
37  uint32_t temp[256];
40 
41  // IF09 - allocate space for 4. plane delta info - unused
42  if (mpi->imgfmt == IMGFMT_IF09) {
43  mpi->planes[0]=av_malloc(mpi->bpp*mpi->width*(mpi->height+2)/8+
44  mpi->chroma_width*mpi->chroma_height);
45  } else
46  mpi->planes[0]=av_malloc(mpi->bpp*mpi->width*(mpi->height+2)/8);
47  if (mpi->flags&MP_IMGFLAG_PLANAR) {
48  int bpp = IMGFMT_IS_YUVP16(mpi->imgfmt)? 2 : 1;
49  // YV12/I420/YVU9/IF09. feel free to add other planar formats here...
50  mpi->stride[0]=mpi->stride[3]=bpp*mpi->width;
51  if(mpi->num_planes > 2){
52  mpi->stride[1]=mpi->stride[2]=bpp*mpi->chroma_width;
53  if(mpi->flags&MP_IMGFLAG_SWAPPED){
54  // I420/IYUV (Y,U,V)
55  mpi->planes[1]=mpi->planes[0]+mpi->stride[0]*mpi->height;
56  mpi->planes[2]=mpi->planes[1]+mpi->stride[1]*mpi->chroma_height;
57  if (mpi->num_planes > 3)
58  mpi->planes[3]=mpi->planes[2]+mpi->stride[2]*mpi->chroma_height;
59  } else {
60  // YV12,YVU9,IF09 (Y,V,U)
61  mpi->planes[2]=mpi->planes[0]+mpi->stride[0]*mpi->height;
62  mpi->planes[1]=mpi->planes[2]+mpi->stride[1]*mpi->chroma_height;
63  if (mpi->num_planes > 3)
64  mpi->planes[3]=mpi->planes[1]+mpi->stride[1]*mpi->chroma_height;
65  }
66  } else {
67  // NV12/NV21
68  mpi->stride[1]=mpi->chroma_width;
69  mpi->planes[1]=mpi->planes[0]+mpi->stride[0]*mpi->height;
70  }
71  } else {
72  mpi->stride[0]=mpi->width*mpi->bpp/8;
73  if (mpi->flags & MP_IMGFLAG_RGB_PALETTE) {
74  mpi->planes[1] = av_malloc(1024);
75  memcpy(mpi->planes[1], temp, 1024);
76  }
77  }
79 }
80 
81 mp_image_t* ff_alloc_mpi(int w, int h, unsigned long int fmt) {
82  mp_image_t* mpi = ff_new_mp_image(w,h);
83 
84  ff_mp_image_setfmt(mpi,fmt);
86 
87  return mpi;
88 }
89 
90 void ff_copy_mpi(mp_image_t *dmpi, mp_image_t *mpi) {
91  if(mpi->flags&MP_IMGFLAG_PLANAR){
92  memcpy_pic(dmpi->planes[0],mpi->planes[0], mpi->w, mpi->h,
93  dmpi->stride[0],mpi->stride[0]);
94  memcpy_pic(dmpi->planes[1],mpi->planes[1], mpi->chroma_width, mpi->chroma_height,
95  dmpi->stride[1],mpi->stride[1]);
96  memcpy_pic(dmpi->planes[2], mpi->planes[2], mpi->chroma_width, mpi->chroma_height,
97  dmpi->stride[2],mpi->stride[2]);
98  } else {
99  memcpy_pic(dmpi->planes[0],mpi->planes[0],
100  mpi->w*(dmpi->bpp/8), mpi->h,
101  dmpi->stride[0],mpi->stride[0]);
102  }
103 }
104 
105 void ff_mp_image_setfmt(mp_image_t* mpi,unsigned int out_fmt){
107  mpi->imgfmt=out_fmt;
108  // compressed formats
109  if(out_fmt == IMGFMT_MPEGPES ||
110  out_fmt == IMGFMT_ZRMJPEGNI || out_fmt == IMGFMT_ZRMJPEGIT || out_fmt == IMGFMT_ZRMJPEGIB ||
111  IMGFMT_IS_HWACCEL(out_fmt)){
112  mpi->bpp=0;
113  return;
114  }
115  mpi->num_planes=1;
116  if (IMGFMT_IS_RGB(out_fmt)) {
117  if (IMGFMT_RGB_DEPTH(out_fmt) < 8 && !(out_fmt&128))
118  mpi->bpp = IMGFMT_RGB_DEPTH(out_fmt);
119  else
120  mpi->bpp=(IMGFMT_RGB_DEPTH(out_fmt)+7)&(~7);
121  return;
122  }
123  if (IMGFMT_IS_BGR(out_fmt)) {
124  if (IMGFMT_BGR_DEPTH(out_fmt) < 8 && !(out_fmt&128))
125  mpi->bpp = IMGFMT_BGR_DEPTH(out_fmt);
126  else
127  mpi->bpp=(IMGFMT_BGR_DEPTH(out_fmt)+7)&(~7);
129  return;
130  }
131  mpi->num_planes=3;
132  if (out_fmt == IMGFMT_GBR24P) {
133  mpi->bpp=24;
134  mpi->flags|=MP_IMGFLAG_PLANAR;
135  return;
136  } else if (out_fmt == IMGFMT_GBR12P) {
137  mpi->bpp=36;
138  mpi->flags|=MP_IMGFLAG_PLANAR;
139  return;
140  } else if (out_fmt == IMGFMT_GBR14P) {
141  mpi->bpp=42;
142  mpi->flags|=MP_IMGFLAG_PLANAR;
143  return;
144  }
145  mpi->flags|=MP_IMGFLAG_YUV;
146  if (ff_mp_get_chroma_shift(out_fmt, NULL, NULL, NULL)) {
147  mpi->flags|=MP_IMGFLAG_PLANAR;
148  mpi->bpp = ff_mp_get_chroma_shift(out_fmt, &mpi->chroma_x_shift, &mpi->chroma_y_shift, NULL);
149  mpi->chroma_width = mpi->width >> mpi->chroma_x_shift;
150  mpi->chroma_height = mpi->height >> mpi->chroma_y_shift;
151  }
152  switch(out_fmt){
153  case IMGFMT_I420:
154  case IMGFMT_IYUV:
156  case IMGFMT_YV12:
157  return;
158  case IMGFMT_420A:
159  case IMGFMT_422A:
160  case IMGFMT_444A:
161  case IMGFMT_IF09:
162  mpi->num_planes=4;
163  case IMGFMT_YVU9:
164  case IMGFMT_444P:
165  case IMGFMT_422P:
166  case IMGFMT_411P:
167  case IMGFMT_440P:
168  case IMGFMT_444P16_LE:
169  case IMGFMT_444P16_BE:
170  case IMGFMT_444P14_LE:
171  case IMGFMT_444P14_BE:
172  case IMGFMT_444P12_LE:
173  case IMGFMT_444P12_BE:
174  case IMGFMT_444P10_LE:
175  case IMGFMT_444P10_BE:
176  case IMGFMT_444P9_LE:
177  case IMGFMT_444P9_BE:
178  case IMGFMT_422P16_LE:
179  case IMGFMT_422P16_BE:
180  case IMGFMT_422P14_LE:
181  case IMGFMT_422P14_BE:
182  case IMGFMT_422P12_LE:
183  case IMGFMT_422P12_BE:
184  case IMGFMT_422P10_LE:
185  case IMGFMT_422P10_BE:
186  case IMGFMT_422P9_LE:
187  case IMGFMT_422P9_BE:
188  case IMGFMT_420P16_LE:
189  case IMGFMT_420P16_BE:
190  case IMGFMT_420P14_LE:
191  case IMGFMT_420P14_BE:
192  case IMGFMT_420P12_LE:
193  case IMGFMT_420P12_BE:
194  case IMGFMT_420P10_LE:
195  case IMGFMT_420P10_BE:
196  case IMGFMT_420P9_LE:
197  case IMGFMT_420P9_BE:
198  return;
199  case IMGFMT_Y16_LE:
200  case IMGFMT_Y16_BE:
201  mpi->bpp=16;
202  case IMGFMT_Y800:
203  case IMGFMT_Y8:
204  /* they're planar ones, but for easier handling use them as packed */
205  mpi->flags&=~MP_IMGFLAG_PLANAR;
206  mpi->num_planes=1;
207  return;
208  case IMGFMT_Y8A:
209  mpi->num_planes=2;
210  return;
211  case IMGFMT_UYVY:
213  case IMGFMT_YUY2:
214  mpi->chroma_x_shift = 1;
215  mpi->bpp=16;
216  mpi->num_planes=1;
217  return;
218  case IMGFMT_NV12:
220  case IMGFMT_NV21:
221  mpi->flags|=MP_IMGFLAG_PLANAR;
222  mpi->bpp=12;
223  mpi->num_planes=2;
224  mpi->chroma_width=(mpi->width>>0);
225  mpi->chroma_height=(mpi->height>>1);
226  mpi->chroma_x_shift=0;
227  mpi->chroma_y_shift=1;
228  return;
229  }
230  ff_mp_msg(MSGT_DECVIDEO,MSGL_WARN,"mp_image: unknown out_fmt: 0x%X\n",out_fmt);
231  mpi->bpp=0;
232 }
233 
235  mp_image_t* mpi = malloc(sizeof(mp_image_t));
236  if(!mpi) return NULL; // error!
237  memset(mpi,0,sizeof(mp_image_t));
238  mpi->width=mpi->w=w;
239  mpi->height=mpi->h=h;
240  return mpi;
241 }
242 
244  if(!mpi) return;
245  if(mpi->flags&MP_IMGFLAG_ALLOCATED){
246  /* becouse we allocate the whole image in once */
247  av_free(mpi->planes[0]);
248  if (mpi->flags & MP_IMGFLAG_RGB_PALETTE)
249  av_free(mpi->planes[1]);
250  }
251  free(mpi);
252 }
253 
#define IMGFMT_YUY2
Definition: img_format.h:240
#define IMGFMT_444P16_LE
Definition: img_format.h:146
#define IMGFMT_444P14_LE
Definition: img_format.h:148
#define IMGFMT_Y16_BE
Definition: img_format.h:135
#define IMGFMT_Y8A
Definition: img_format.h:138
const char * fmt
Definition: avisynth_c.h:669
#define IMGFMT_422P16_LE
Definition: img_format.h:156
else temp
Definition: vf_mcdeint.c:258
#define IMGFMT_GBR12P
Definition: img_format.h:100
#define IMGFMT_444P12_LE
Definition: img_format.h:150
#define IMGFMT_NV12
Definition: img_format.h:125
#define IMGFMT_420P9_BE
Definition: img_format.h:175
#define IMGFMT_YVU9
Definition: img_format.h:117
#define IMGFMT_420P14_LE
Definition: img_format.h:168
#define IMGFMT_UYVY
Definition: img_format.h:236
#define IMGFMT_YV12
Definition: img_format.h:119
unsigned int flags
Definition: mp_image.h:126
#define IMGFMT_Y8
Definition: img_format.h:124
#define IMGFMT_420P10_LE
Definition: img_format.h:172
#define IMGFMT_GBR14P
Definition: img_format.h:101
#define MP_IMGFLAG_YUV
Definition: mp_image.h:78
#define IMGFMT_444P9_LE
Definition: img_format.h:154
#define IMGFMT_IS_HWACCEL(fmt)
Definition: img_format.h:281
#define IMGFMT_444P16_BE
Definition: img_format.h:147
#define IMGFMT_422P9_BE
Definition: img_format.h:165
#define IMGFMT_444P9_BE
Definition: img_format.h:155
#define IMGFMT_ZRMJPEGNI
Definition: img_format.h:258
#define IMGFMT_444A
Definition: img_format.h:144
void ff_mp_image_setfmt(mp_image_t *mpi, unsigned int out_fmt)
Definition: mp_image.c:105
void ff_free_mp_image(mp_image_t *mpi)
Definition: mp_image.c:243
#define IMGFMT_444P10_BE
Definition: img_format.h:153
#define IMGFMT_GBR24P
Definition: img_format.h:54
#define IMGFMT_IF09
Definition: img_format.h:118
#define IMGFMT_422P12_LE
Definition: img_format.h:160
#define IMGFMT_422P14_LE
Definition: img_format.h:158
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
#define IMGFMT_422P10_BE
Definition: img_format.h:163
#define IMGFMT_422P12_BE
Definition: img_format.h:161
#define IMGFMT_422P16_BE
Definition: img_format.h:157
int chroma_x_shift
Definition: mp_image.h:144
#define IMGFMT_IYUV
Definition: img_format.h:121
#define IMGFMT_NV21
Definition: img_format.h:126
int num_planes
Definition: mp_image.h:140
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
Definition: imgutils.c:150
#define IMGFMT_IS_BGR(fmt)
Definition: img_format.h:109
#define IMGFMT_IS_RGB(fmt)
Definition: img_format.h:108
#define IMGFMT_IS_YUVP16(fmt)
Definition: img_format.h:216
enum AVPixelFormat ff_mp2ff_pix_fmt(int mp)
Definition: vf_mp.c:170
#define IMGFMT_422A
Definition: img_format.h:142
#define IMGFMT_444P12_BE
Definition: img_format.h:151
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
#define IMGFMT_BGR_DEPTH(fmt)
Definition: img_format.h:112
#define IMGFMT_RGB_DEPTH(fmt)
Definition: img_format.h:111
void ff_mp_image_alloc_planes(mp_image_t *mpi)
Definition: mp_image.c:36
#define IMGFMT_444P10_LE
Definition: img_format.h:152
int chroma_height
Definition: mp_image.h:143
#define MP_IMGFLAG_RGB_PALETTE
Definition: mp_image.h:82
#define IMGFMT_440P
Definition: img_format.h:133
void ff_copy_mpi(mp_image_t *dmpi, mp_image_t *mpi)
Definition: mp_image.c:90
mp_image_t * ff_alloc_mpi(int w, int h, unsigned long int fmt)
Definition: mp_image.c:81
#define IMGFMT_420P16_LE
Definition: img_format.h:166
#define memcpy_pic(d, s, b, h, ds, ss)
Definition: fastmemcpy.h:62
#define IMGFMT_444P14_BE
Definition: img_format.h:149
int chroma_y_shift
Definition: mp_image.h:145
int stride[MP_MAX_PLANES]
Definition: mp_image.h:134
#define MP_IMGFLAG_ALLOCATED
Definition: mp_image.h:93
unsigned char * planes[MP_MAX_PLANES]
Definition: mp_image.h:133
#define IMGFMT_Y800
Definition: img_format.h:123
#define IMGFMT_422P
Definition: img_format.h:131
#define IMGFMT_422P10_LE
Definition: img_format.h:162
#define IMGFMT_MPEGPES
Definition: img_format.h:254
#define IMGFMT_ZRMJPEGIT
Definition: img_format.h:259
#define IMGFMT_422P9_LE
Definition: img_format.h:164
#define IMGFMT_420P10_BE
Definition: img_format.h:173
#define IMGFMT_420P16_BE
Definition: img_format.h:167
unsigned char bpp
Definition: mp_image.h:129
#define IMGFMT_420P14_BE
Definition: img_format.h:169
#define IMGFMT_444P
Definition: img_format.h:130
#define IMGFMT_Y16_LE
Definition: img_format.h:127
#define MSGL_WARN
Definition: mp_msg.h:34
void ff_mp_msg(int mod, int lev, const char *format,...)
Definition: vf_mp.c:196
#define MP_IMGFLAG_PLANAR
Definition: mp_image.h:76
#define IMGFMT_420P12_LE
Definition: img_format.h:170
int ff_mp_get_chroma_shift(int format, int *x_shift, int *y_shift, int *component_bits)
Calculates the scale shifts for the chroma planes for planar YUV.
Definition: img_format.c:145
int height
Definition: mp_image.h:131
#define IMGFMT_420P9_LE
Definition: img_format.h:174
#define IMGFMT_422P14_BE
Definition: img_format.h:159
#define IMGFMT_ZRMJPEGIB
Definition: img_format.h:260
mp_image_t * ff_new_mp_image(int w, int h)
Definition: mp_image.c:234
#define IMGFMT_420A
Definition: img_format.h:140
int width
Definition: mp_image.h:131
#define MSGT_DECVIDEO
Definition: mp_msg.h:67
#define IMGFMT_I420
Definition: img_format.h:120
#define IMGFMT_420P12_BE
Definition: img_format.h:171
unsigned int imgfmt
Definition: mp_image.h:130
int chroma_width
Definition: mp_image.h:142
#define MP_IMGFLAG_SWAPPED
Definition: mp_image.h:80
#define IMGFMT_411P
Definition: img_format.h:132