FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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->flags|=MP_IMGFLAG_YUV;
132  mpi->num_planes=3;
133  if (ff_mp_get_chroma_shift(out_fmt, NULL, NULL)) {
134  mpi->flags|=MP_IMGFLAG_PLANAR;
135  mpi->bpp = ff_mp_get_chroma_shift(out_fmt, &mpi->chroma_x_shift, &mpi->chroma_y_shift);
136  mpi->chroma_width = mpi->width >> mpi->chroma_x_shift;
137  mpi->chroma_height = mpi->height >> mpi->chroma_y_shift;
138  }
139  switch(out_fmt){
140  case IMGFMT_I420:
141  case IMGFMT_IYUV:
143  case IMGFMT_YV12:
144  return;
145  case IMGFMT_420A:
146  case IMGFMT_IF09:
147  mpi->num_planes=4;
148  case IMGFMT_YVU9:
149  case IMGFMT_444P:
150  case IMGFMT_422P:
151  case IMGFMT_411P:
152  case IMGFMT_440P:
153  case IMGFMT_444P16_LE:
154  case IMGFMT_444P16_BE:
155  case IMGFMT_422P16_LE:
156  case IMGFMT_422P16_BE:
157  case IMGFMT_420P16_LE:
158  case IMGFMT_420P16_BE:
159  return;
160  case IMGFMT_Y800:
161  case IMGFMT_Y8:
162  /* they're planar ones, but for easier handling use them as packed */
163  mpi->flags&=~MP_IMGFLAG_PLANAR;
164  mpi->num_planes=1;
165  return;
166  case IMGFMT_UYVY:
168  case IMGFMT_YUY2:
169  mpi->bpp=16;
170  mpi->num_planes=1;
171  return;
172  case IMGFMT_NV12:
174  case IMGFMT_NV21:
175  mpi->flags|=MP_IMGFLAG_PLANAR;
176  mpi->bpp=12;
177  mpi->num_planes=2;
178  mpi->chroma_width=(mpi->width>>0);
179  mpi->chroma_height=(mpi->height>>1);
180  mpi->chroma_x_shift=0;
181  mpi->chroma_y_shift=1;
182  return;
183  }
184  ff_mp_msg(MSGT_DECVIDEO,MSGL_WARN,"mp_image: unknown out_fmt: 0x%X\n",out_fmt);
185  mpi->bpp=0;
186 }
187 
189  mp_image_t* mpi = malloc(sizeof(mp_image_t));
190  if(!mpi) return NULL; // error!
191  memset(mpi,0,sizeof(mp_image_t));
192  mpi->width=mpi->w=w;
193  mpi->height=mpi->h=h;
194  return mpi;
195 }
196 
198  if(!mpi) return;
199  if(mpi->flags&MP_IMGFLAG_ALLOCATED){
200  /* becouse we allocate the whole image in once */
201  av_free(mpi->planes[0]);
202  if (mpi->flags & MP_IMGFLAG_RGB_PALETTE)
203  av_free(mpi->planes[1]);
204  }
205  free(mpi);
206 }
207