1
0

video-frame.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. case VIDEO_FORMAT_I444:
  74. size = width * height;
  75. ALIGN_SIZE(size, alignment);
  76. frame->data[0] = bmalloc(size * 3);
  77. frame->data[1] = (uint8_t*)frame->data[0] + size;
  78. frame->data[2] = (uint8_t*)frame->data[1] + size;
  79. frame->linesize[0] = width;
  80. frame->linesize[1] = width;
  81. frame->linesize[2] = width;
  82. break;
  83. }
  84. }
  85. void video_frame_copy(struct video_frame *dst, const struct video_frame *src,
  86. enum video_format format, uint32_t cy)
  87. {
  88. switch (format) {
  89. case VIDEO_FORMAT_NONE:
  90. return;
  91. case VIDEO_FORMAT_I420:
  92. memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
  93. memcpy(dst->data[1], src->data[1], src->linesize[1] * cy / 2);
  94. memcpy(dst->data[2], src->data[2], src->linesize[2] * cy / 2);
  95. break;
  96. case VIDEO_FORMAT_NV12:
  97. memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
  98. memcpy(dst->data[1], src->data[1], src->linesize[1] * cy / 2);
  99. break;
  100. case VIDEO_FORMAT_YVYU:
  101. case VIDEO_FORMAT_YUY2:
  102. case VIDEO_FORMAT_UYVY:
  103. case VIDEO_FORMAT_RGBA:
  104. case VIDEO_FORMAT_BGRA:
  105. case VIDEO_FORMAT_BGRX:
  106. memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
  107. break;
  108. case VIDEO_FORMAT_I444:
  109. memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
  110. memcpy(dst->data[1], src->data[1], src->linesize[1] * cy);
  111. memcpy(dst->data[2], src->data[2], src->linesize[2] * cy);
  112. break;
  113. }
  114. }