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.

126 lines
3.8KB

  1. /*
  2. * AVPicture management routines
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. * AVPicture management routines
  24. */
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/colorspace.h"
  31. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  32. enum AVPixelFormat pix_fmt, int width, int height)
  33. {
  34. int ret;
  35. if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
  36. return ret;
  37. if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
  38. return ret;
  39. return av_image_fill_pointers(picture->data, pix_fmt,
  40. height, ptr, picture->linesize);
  41. }
  42. int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt,
  43. int width, int height,
  44. unsigned char *dest, int dest_size)
  45. {
  46. int i, j, nb_planes = 0, linesizes[4];
  47. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  48. int size = avpicture_get_size(pix_fmt, width, height);
  49. if (size > dest_size || size < 0)
  50. return AVERROR(EINVAL);
  51. for (i = 0; i < desc->nb_components; i++)
  52. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  53. nb_planes++;
  54. av_image_fill_linesizes(linesizes, pix_fmt, width);
  55. for (i = 0; i < nb_planes; i++) {
  56. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  57. const unsigned char *s = src->data[i];
  58. h = (height + (1 << shift) - 1) >> shift;
  59. for (j = 0; j < h; j++) {
  60. memcpy(dest, s, linesizes[i]);
  61. dest += linesizes[i];
  62. s += src->linesize[i];
  63. }
  64. }
  65. if (desc->flags & AV_PIX_FMT_FLAG_PAL)
  66. memcpy((unsigned char *)(((size_t)dest + 3) & ~3),
  67. src->data[1], 256 * 4);
  68. return size;
  69. }
  70. int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
  71. {
  72. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  73. AVPicture dummy_pict;
  74. int ret;
  75. if (!desc)
  76. return AVERROR(EINVAL);
  77. if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
  78. return ret;
  79. if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  80. // do not include palette for these pseudo-paletted formats
  81. return width * height;
  82. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  83. }
  84. int avpicture_alloc(AVPicture *picture,
  85. enum AVPixelFormat pix_fmt, int width, int height)
  86. {
  87. int ret = av_image_alloc(picture->data, picture->linesize,
  88. width, height, pix_fmt, 1);
  89. if (ret < 0) {
  90. memset(picture, 0, sizeof(AVPicture));
  91. return ret;
  92. }
  93. return 0;
  94. }
  95. void avpicture_free(AVPicture *picture)
  96. {
  97. av_free(picture->data[0]);
  98. }
  99. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  100. enum AVPixelFormat pix_fmt, int width, int height)
  101. {
  102. av_image_copy(dst->data, dst->linesize, src->data,
  103. src->linesize, pix_fmt, width, height);
  104. }