mp_image.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include "config.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #if HAVE_MALLOC_H
  23. #include <malloc.h>
  24. #endif
  25. #include "img_format.h"
  26. #include "mp_image.h"
  27. #include "libvo/fastmemcpy.h"
  28. //#include "libavutil/mem.h"
  29. #include "libavutil/imgutils.h"
  30. void ff_mp_image_alloc_planes(mp_image_t *mpi) {
  31. uint32_t temp[256];
  32. if (avpriv_set_systematic_pal2(temp, ff_mp2ff_pix_fmt(mpi->imgfmt)) >= 0)
  33. mpi->flags |= MP_IMGFLAG_RGB_PALETTE;
  34. // IF09 - allocate space for 4. plane delta info - unused
  35. if (mpi->imgfmt == IMGFMT_IF09) {
  36. mpi->planes[0]=av_malloc(mpi->bpp*mpi->width*(mpi->height+2)/8+
  37. mpi->chroma_width*mpi->chroma_height);
  38. } else
  39. mpi->planes[0]=av_malloc(mpi->bpp*mpi->width*(mpi->height+2)/8);
  40. if (mpi->flags&MP_IMGFLAG_PLANAR) {
  41. int bpp = IMGFMT_IS_YUVP16(mpi->imgfmt)? 2 : 1;
  42. // YV12/I420/YVU9/IF09. feel free to add other planar formats here...
  43. mpi->stride[0]=mpi->stride[3]=bpp*mpi->width;
  44. if(mpi->num_planes > 2){
  45. mpi->stride[1]=mpi->stride[2]=bpp*mpi->chroma_width;
  46. if(mpi->flags&MP_IMGFLAG_SWAPPED){
  47. // I420/IYUV (Y,U,V)
  48. mpi->planes[1]=mpi->planes[0]+mpi->stride[0]*mpi->height;
  49. mpi->planes[2]=mpi->planes[1]+mpi->stride[1]*mpi->chroma_height;
  50. if (mpi->num_planes > 3)
  51. mpi->planes[3]=mpi->planes[2]+mpi->stride[2]*mpi->chroma_height;
  52. } else {
  53. // YV12,YVU9,IF09 (Y,V,U)
  54. mpi->planes[2]=mpi->planes[0]+mpi->stride[0]*mpi->height;
  55. mpi->planes[1]=mpi->planes[2]+mpi->stride[1]*mpi->chroma_height;
  56. if (mpi->num_planes > 3)
  57. mpi->planes[3]=mpi->planes[1]+mpi->stride[1]*mpi->chroma_height;
  58. }
  59. } else {
  60. // NV12/NV21
  61. mpi->stride[1]=mpi->chroma_width;
  62. mpi->planes[1]=mpi->planes[0]+mpi->stride[0]*mpi->height;
  63. }
  64. } else {
  65. mpi->stride[0]=mpi->width*mpi->bpp/8;
  66. if (mpi->flags & MP_IMGFLAG_RGB_PALETTE) {
  67. mpi->planes[1] = av_malloc(1024);
  68. memcpy(mpi->planes[1], temp, 1024);
  69. }
  70. }
  71. mpi->flags|=MP_IMGFLAG_ALLOCATED;
  72. }
  73. mp_image_t* ff_alloc_mpi(int w, int h, unsigned long int fmt) {
  74. mp_image_t* mpi = ff_new_mp_image(w,h);
  75. ff_mp_image_setfmt(mpi,fmt);
  76. ff_mp_image_alloc_planes(mpi);
  77. return mpi;
  78. }
  79. void ff_copy_mpi(mp_image_t *dmpi, mp_image_t *mpi) {
  80. if(mpi->flags&MP_IMGFLAG_PLANAR){
  81. memcpy_pic(dmpi->planes[0],mpi->planes[0], mpi->w, mpi->h,
  82. dmpi->stride[0],mpi->stride[0]);
  83. memcpy_pic(dmpi->planes[1],mpi->planes[1], mpi->chroma_width, mpi->chroma_height,
  84. dmpi->stride[1],mpi->stride[1]);
  85. memcpy_pic(dmpi->planes[2], mpi->planes[2], mpi->chroma_width, mpi->chroma_height,
  86. dmpi->stride[2],mpi->stride[2]);
  87. } else {
  88. memcpy_pic(dmpi->planes[0],mpi->planes[0],
  89. mpi->w*(dmpi->bpp/8), mpi->h,
  90. dmpi->stride[0],mpi->stride[0]);
  91. }
  92. }
  93. void ff_mp_image_setfmt(mp_image_t* mpi,unsigned int out_fmt){
  94. mpi->flags&=~(MP_IMGFLAG_PLANAR|MP_IMGFLAG_YUV|MP_IMGFLAG_SWAPPED);
  95. mpi->imgfmt=out_fmt;
  96. // compressed formats
  97. if(out_fmt == IMGFMT_MPEGPES ||
  98. out_fmt == IMGFMT_ZRMJPEGNI || out_fmt == IMGFMT_ZRMJPEGIT || out_fmt == IMGFMT_ZRMJPEGIB ||
  99. IMGFMT_IS_HWACCEL(out_fmt)){
  100. mpi->bpp=0;
  101. return;
  102. }
  103. mpi->num_planes=1;
  104. if (IMGFMT_IS_RGB(out_fmt)) {
  105. if (IMGFMT_RGB_DEPTH(out_fmt) < 8 && !(out_fmt&128))
  106. mpi->bpp = IMGFMT_RGB_DEPTH(out_fmt);
  107. else
  108. mpi->bpp=(IMGFMT_RGB_DEPTH(out_fmt)+7)&(~7);
  109. return;
  110. }
  111. if (IMGFMT_IS_BGR(out_fmt)) {
  112. if (IMGFMT_BGR_DEPTH(out_fmt) < 8 && !(out_fmt&128))
  113. mpi->bpp = IMGFMT_BGR_DEPTH(out_fmt);
  114. else
  115. mpi->bpp=(IMGFMT_BGR_DEPTH(out_fmt)+7)&(~7);
  116. mpi->flags|=MP_IMGFLAG_SWAPPED;
  117. return;
  118. }
  119. mpi->flags|=MP_IMGFLAG_YUV;
  120. mpi->num_planes=3;
  121. if (ff_mp_get_chroma_shift(out_fmt, NULL, NULL)) {
  122. mpi->flags|=MP_IMGFLAG_PLANAR;
  123. mpi->bpp = ff_mp_get_chroma_shift(out_fmt, &mpi->chroma_x_shift, &mpi->chroma_y_shift);
  124. mpi->chroma_width = mpi->width >> mpi->chroma_x_shift;
  125. mpi->chroma_height = mpi->height >> mpi->chroma_y_shift;
  126. }
  127. switch(out_fmt){
  128. case IMGFMT_I420:
  129. case IMGFMT_IYUV:
  130. mpi->flags|=MP_IMGFLAG_SWAPPED;
  131. case IMGFMT_YV12:
  132. return;
  133. case IMGFMT_420A:
  134. case IMGFMT_IF09:
  135. mpi->num_planes=4;
  136. case IMGFMT_YVU9:
  137. case IMGFMT_444P:
  138. case IMGFMT_422P:
  139. case IMGFMT_411P:
  140. case IMGFMT_440P:
  141. case IMGFMT_444P16_LE:
  142. case IMGFMT_444P16_BE:
  143. case IMGFMT_422P16_LE:
  144. case IMGFMT_422P16_BE:
  145. case IMGFMT_420P16_LE:
  146. case IMGFMT_420P16_BE:
  147. return;
  148. case IMGFMT_Y800:
  149. case IMGFMT_Y8:
  150. /* they're planar ones, but for easier handling use them as packed */
  151. mpi->flags&=~MP_IMGFLAG_PLANAR;
  152. mpi->num_planes=1;
  153. return;
  154. case IMGFMT_UYVY:
  155. mpi->flags|=MP_IMGFLAG_SWAPPED;
  156. case IMGFMT_YUY2:
  157. mpi->bpp=16;
  158. mpi->num_planes=1;
  159. return;
  160. case IMGFMT_NV12:
  161. mpi->flags|=MP_IMGFLAG_SWAPPED;
  162. case IMGFMT_NV21:
  163. mpi->flags|=MP_IMGFLAG_PLANAR;
  164. mpi->bpp=12;
  165. mpi->num_planes=2;
  166. mpi->chroma_width=(mpi->width>>0);
  167. mpi->chroma_height=(mpi->height>>1);
  168. mpi->chroma_x_shift=0;
  169. mpi->chroma_y_shift=1;
  170. return;
  171. }
  172. ff_mp_msg(MSGT_DECVIDEO,MSGL_WARN,"mp_image: unknown out_fmt: 0x%X\n",out_fmt);
  173. mpi->bpp=0;
  174. }
  175. mp_image_t* ff_new_mp_image(int w,int h){
  176. mp_image_t* mpi = malloc(sizeof(mp_image_t));
  177. if(!mpi) return NULL; // error!
  178. memset(mpi,0,sizeof(mp_image_t));
  179. mpi->width=mpi->w=w;
  180. mpi->height=mpi->h=h;
  181. return mpi;
  182. }
  183. void ff_free_mp_image(mp_image_t* mpi){
  184. if(!mpi) return;
  185. if(mpi->flags&MP_IMGFLAG_ALLOCATED){
  186. /* becouse we allocate the whole image in once */
  187. av_free(mpi->planes[0]);
  188. if (mpi->flags & MP_IMGFLAG_RGB_PALETTE)
  189. av_free(mpi->planes[1]);
  190. }
  191. free(mpi);
  192. }