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.

123 lines
4.0KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg 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 GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * misc image utilities
  21. */
  22. #include "imgutils.h"
  23. #include "libavutil/pixdesc.h"
  24. int av_get_image_linesize(enum PixelFormat pix_fmt, int width, int plane)
  25. {
  26. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  27. int max_step [4]; /* max pixel step for each plane */
  28. int max_step_comp[4]; /* the component for each plane which has the max pixel step */
  29. int s;
  30. if (desc->flags & PIX_FMT_BITSTREAM)
  31. return (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
  32. av_fill_image_max_pixsteps(max_step, max_step_comp, desc);
  33. s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
  34. return max_step[plane] * (((width + (1 << s) - 1)) >> s);
  35. }
  36. int av_fill_image_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
  37. {
  38. int i;
  39. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  40. int max_step [4]; /* max pixel step for each plane */
  41. int max_step_comp[4]; /* the component for each plane which has the max pixel step */
  42. memset(linesizes, 0, 4*sizeof(linesizes[0]));
  43. if (desc->flags & PIX_FMT_HWACCEL)
  44. return AVERROR(EINVAL);
  45. if (desc->flags & PIX_FMT_BITSTREAM) {
  46. linesizes[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
  47. return 0;
  48. }
  49. av_fill_image_max_pixsteps(max_step, max_step_comp, desc);
  50. for (i = 0; i < 4; i++) {
  51. int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
  52. linesizes[i] = max_step[i] * (((width + (1 << s) - 1)) >> s);
  53. }
  54. return 0;
  55. }
  56. int av_fill_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
  57. uint8_t *ptr, const int linesizes[4])
  58. {
  59. int i, total_size, size[4], has_plane[4];
  60. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  61. memset(data , 0, sizeof(data[0])*4);
  62. memset(size , 0, sizeof(size));
  63. memset(has_plane, 0, sizeof(has_plane));
  64. if (desc->flags & PIX_FMT_HWACCEL)
  65. return AVERROR(EINVAL);
  66. data[0] = ptr;
  67. size[0] = linesizes[0] * height;
  68. if (desc->flags & PIX_FMT_PAL) {
  69. size[0] = (size[0] + 3) & ~3;
  70. data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
  71. return size[0] + 256 * 4;
  72. }
  73. for (i = 0; i < 4; i++)
  74. has_plane[desc->comp[i].plane] = 1;
  75. total_size = size[0];
  76. for (i = 1; has_plane[i] && i < 4; i++) {
  77. int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  78. data[i] = data[i-1] + size[i-1];
  79. h = (height + (1 << s) - 1) >> s;
  80. size[i] = h * linesizes[i];
  81. total_size += size[i];
  82. }
  83. return total_size;
  84. }
  85. typedef struct ImgUtils {
  86. const AVClass *class;
  87. int log_offset;
  88. void *log_ctx;
  89. } ImgUtils;
  90. static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
  91. int av_check_image_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
  92. {
  93. ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
  94. if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
  95. return 0;
  96. av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
  97. return AVERROR(EINVAL);
  98. }