You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
11KB

  1. /*
  2. * Misc image conversion routines
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * misc image conversion routines
  24. */
  25. /* TODO:
  26. * - write 'ffimg' program to test all the image related stuff
  27. * - move all api to slice based system
  28. * - integrate deinterlacing, postprocessing and scaling in the conversion process
  29. */
  30. #include "avcodec.h"
  31. #include "imgconvert.h"
  32. #include "internal.h"
  33. #include "mathops.h"
  34. #include "libavutil/avassert.h"
  35. #include "libavutil/colorspace.h"
  36. #include "libavutil/common.h"
  37. #include "libavutil/pixdesc.h"
  38. #include "libavutil/internal.h"
  39. #include "libavutil/imgutils.h"
  40. void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
  41. {
  42. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  43. av_assert0(desc);
  44. *h_shift = desc->log2_chroma_w;
  45. *v_shift = desc->log2_chroma_h;
  46. }
  47. int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
  48. enum AVPixelFormat src_pix_fmt,
  49. int has_alpha)
  50. {
  51. return av_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  52. }
  53. enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  54. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  55. {
  56. return av_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
  57. }
  58. #if AV_HAVE_INCOMPATIBLE_LIBAV_ABI
  59. enum AVPixelFormat avcodec_find_best_pix_fmt2(const enum AVPixelFormat *pix_fmt_list,
  60. enum AVPixelFormat src_pix_fmt,
  61. int has_alpha, int *loss_ptr){
  62. return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
  63. }
  64. #else
  65. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  66. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  67. {
  68. return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
  69. }
  70. #endif
  71. enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(const enum AVPixelFormat *pix_fmt_list,
  72. enum AVPixelFormat src_pix_fmt,
  73. int has_alpha, int *loss_ptr){
  74. int i;
  75. enum AVPixelFormat best = AV_PIX_FMT_NONE;
  76. for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
  77. best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
  78. return best;
  79. }
  80. /* 2x2 -> 1x1 */
  81. void ff_shrink22(uint8_t *dst, int dst_wrap,
  82. const uint8_t *src, int src_wrap,
  83. int width, int height)
  84. {
  85. int w;
  86. const uint8_t *s1, *s2;
  87. uint8_t *d;
  88. for(;height > 0; height--) {
  89. s1 = src;
  90. s2 = s1 + src_wrap;
  91. d = dst;
  92. for(w = width;w >= 4; w-=4) {
  93. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  94. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  95. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  96. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  97. s1 += 8;
  98. s2 += 8;
  99. d += 4;
  100. }
  101. for(;w > 0; w--) {
  102. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  103. s1 += 2;
  104. s2 += 2;
  105. d++;
  106. }
  107. src += 2 * src_wrap;
  108. dst += dst_wrap;
  109. }
  110. }
  111. /* 4x4 -> 1x1 */
  112. void ff_shrink44(uint8_t *dst, int dst_wrap,
  113. const uint8_t *src, int src_wrap,
  114. int width, int height)
  115. {
  116. int w;
  117. const uint8_t *s1, *s2, *s3, *s4;
  118. uint8_t *d;
  119. for(;height > 0; height--) {
  120. s1 = src;
  121. s2 = s1 + src_wrap;
  122. s3 = s2 + src_wrap;
  123. s4 = s3 + src_wrap;
  124. d = dst;
  125. for(w = width;w > 0; w--) {
  126. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  127. s2[0] + s2[1] + s2[2] + s2[3] +
  128. s3[0] + s3[1] + s3[2] + s3[3] +
  129. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  130. s1 += 4;
  131. s2 += 4;
  132. s3 += 4;
  133. s4 += 4;
  134. d++;
  135. }
  136. src += 4 * src_wrap;
  137. dst += dst_wrap;
  138. }
  139. }
  140. /* 8x8 -> 1x1 */
  141. void ff_shrink88(uint8_t *dst, int dst_wrap,
  142. const uint8_t *src, int src_wrap,
  143. int width, int height)
  144. {
  145. int w, i;
  146. for(;height > 0; height--) {
  147. for(w = width;w > 0; w--) {
  148. int tmp=0;
  149. for(i=0; i<8; i++){
  150. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  151. src += src_wrap;
  152. }
  153. *(dst++) = (tmp + 32)>>6;
  154. src += 8 - 8*src_wrap;
  155. }
  156. src += 8*src_wrap - 8*width;
  157. dst += dst_wrap - width;
  158. }
  159. }
  160. /* return true if yuv planar */
  161. static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
  162. {
  163. int i;
  164. int planes[4] = { 0 };
  165. if ( desc->flags & AV_PIX_FMT_FLAG_RGB
  166. || !(desc->flags & AV_PIX_FMT_FLAG_PLANAR))
  167. return 0;
  168. /* set the used planes */
  169. for (i = 0; i < desc->nb_components; i++)
  170. planes[desc->comp[i].plane] = 1;
  171. /* if there is an unused plane, the format is not planar */
  172. for (i = 0; i < desc->nb_components; i++)
  173. if (!planes[i])
  174. return 0;
  175. return 1;
  176. }
  177. #if FF_API_AVPICTURE
  178. FF_DISABLE_DEPRECATION_WARNINGS
  179. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  180. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  181. {
  182. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  183. int y_shift;
  184. int x_shift;
  185. int max_step[4];
  186. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
  187. return -1;
  188. y_shift = desc->log2_chroma_h;
  189. x_shift = desc->log2_chroma_w;
  190. av_image_fill_max_pixsteps(max_step, NULL, desc);
  191. if (is_yuv_planar(desc)) {
  192. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  193. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  194. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  195. } else{
  196. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  197. return -1;
  198. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + (left_band * max_step[0]);
  199. }
  200. dst->linesize[0] = src->linesize[0];
  201. dst->linesize[1] = src->linesize[1];
  202. dst->linesize[2] = src->linesize[2];
  203. return 0;
  204. }
  205. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  206. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  207. int *color)
  208. {
  209. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  210. uint8_t *optr;
  211. int y_shift;
  212. int x_shift;
  213. int yheight;
  214. int i, y;
  215. int max_step[4];
  216. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
  217. return -1;
  218. if (!is_yuv_planar(desc)) {
  219. if (src)
  220. return -1; //TODO: Not yet implemented
  221. av_image_fill_max_pixsteps(max_step, NULL, desc);
  222. if (padtop || padleft) {
  223. memset(dst->data[0], color[0],
  224. dst->linesize[0] * padtop + (padleft * max_step[0]));
  225. }
  226. if (padleft || padright) {
  227. optr = dst->data[0] + dst->linesize[0] * padtop +
  228. (dst->linesize[0] - (padright * max_step[0]));
  229. yheight = height - 1 - (padtop + padbottom);
  230. for (y = 0; y < yheight; y++) {
  231. memset(optr, color[0], (padleft + padright) * max_step[0]);
  232. optr += dst->linesize[0];
  233. }
  234. }
  235. if (padbottom || padright) {
  236. optr = dst->data[0] + dst->linesize[0] * (height - padbottom) -
  237. (padright * max_step[0]);
  238. memset(optr, color[0], dst->linesize[0] * padbottom +
  239. (padright * max_step[0]));
  240. }
  241. return 0;
  242. }
  243. for (i = 0; i < 3; i++) {
  244. x_shift = i ? desc->log2_chroma_w : 0;
  245. y_shift = i ? desc->log2_chroma_h : 0;
  246. if (padtop || padleft) {
  247. memset(dst->data[i], color[i],
  248. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  249. }
  250. if (padleft || padright) {
  251. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  252. (dst->linesize[i] - (padright >> x_shift));
  253. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  254. for (y = 0; y < yheight; y++) {
  255. memset(optr, color[i], (padleft + padright) >> x_shift);
  256. optr += dst->linesize[i];
  257. }
  258. }
  259. if (src) { /* first line */
  260. uint8_t *iptr = src->data[i];
  261. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  262. (padleft >> x_shift);
  263. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  264. iptr += src->linesize[i];
  265. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  266. (dst->linesize[i] - (padright >> x_shift));
  267. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  268. for (y = 0; y < yheight; y++) {
  269. memset(optr, color[i], (padleft + padright) >> x_shift);
  270. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  271. (width - padleft - padright) >> x_shift);
  272. iptr += src->linesize[i];
  273. optr += dst->linesize[i];
  274. }
  275. }
  276. if (padbottom || padright) {
  277. optr = dst->data[i] + dst->linesize[i] *
  278. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  279. memset(optr, color[i],dst->linesize[i] *
  280. (padbottom >> y_shift) + (padright >> x_shift));
  281. }
  282. }
  283. return 0;
  284. }
  285. #ifdef TEST
  286. int main(void){
  287. int i;
  288. int err=0;
  289. int skip = 0;
  290. for (i=0; i<AV_PIX_FMT_NB*2; i++) {
  291. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
  292. if(!desc || !desc->name) {
  293. skip ++;
  294. continue;
  295. }
  296. if (skip) {
  297. av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip);
  298. skip = 0;
  299. }
  300. av_log(NULL, AV_LOG_INFO, "pix fmt %s yuv_plan:%d avg_bpp:%d\n", desc->name, is_yuv_planar(desc), av_get_padded_bits_per_pixel(desc));
  301. if ((!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) {
  302. av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n");
  303. err = 1;
  304. }
  305. }
  306. return err;
  307. }
  308. #endif
  309. FF_DISABLE_DEPRECATION_WARNINGS
  310. #endif /* FF_API_AVPICTURE */