mp_image.c 6.4 KB

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