video-frame.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "video-frame.h"
  15. #define ALIGN_SIZE(size, align) \
  16. size = (((size)+(align-1)) & (~(align-1)))
  17. /* messy code alarm */
  18. void video_frame_init(struct video_frame *frame, enum video_format format,
  19. uint32_t width, uint32_t height)
  20. {
  21. size_t size;
  22. size_t offsets[MAX_AV_PLANES];
  23. int alignment = base_get_alignment();
  24. if (!frame) return;
  25. memset(frame, 0, sizeof(struct video_frame));
  26. memset(offsets, 0, sizeof(offsets));
  27. switch (format) {
  28. case VIDEO_FORMAT_NONE:
  29. return;
  30. case VIDEO_FORMAT_I420:
  31. size = width * height;
  32. ALIGN_SIZE(size, alignment);
  33. offsets[0] = size;
  34. size += (width/2) * (height/2);
  35. ALIGN_SIZE(size, alignment);
  36. offsets[1] = size;
  37. size += (width/2) * (height/2);
  38. ALIGN_SIZE(size, alignment);
  39. frame->data[0] = bmalloc(size);
  40. frame->data[1] = (uint8_t*)frame->data[0] + offsets[0];
  41. frame->data[2] = (uint8_t*)frame->data[0] + offsets[1];
  42. frame->linesize[0] = width;
  43. frame->linesize[1] = width/2;
  44. frame->linesize[2] = width/2;
  45. break;
  46. case VIDEO_FORMAT_NV12:
  47. size = width * height;
  48. ALIGN_SIZE(size, alignment);
  49. offsets[0] = size;
  50. size += (width/2) * (height/2) * 2;
  51. ALIGN_SIZE(size, alignment);
  52. frame->data[0] = bmalloc(size);
  53. frame->data[1] = (uint8_t*)frame->data[0] + offsets[0];
  54. frame->linesize[0] = width;
  55. frame->linesize[1] = width;
  56. break;
  57. case VIDEO_FORMAT_YVYU:
  58. case VIDEO_FORMAT_YUY2:
  59. case VIDEO_FORMAT_UYVY:
  60. size = width * height * 2;
  61. ALIGN_SIZE(size, alignment);
  62. frame->data[0] = bmalloc(size);
  63. frame->linesize[0] = width*2;
  64. break;
  65. case VIDEO_FORMAT_RGBA:
  66. case VIDEO_FORMAT_BGRA:
  67. case VIDEO_FORMAT_BGRX:
  68. size = width * height * 4;
  69. ALIGN_SIZE(size, alignment);
  70. frame->data[0] = bmalloc(size);
  71. frame->linesize[0] = width*4;
  72. break;
  73. }
  74. }