video-frame.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_Y800:
  58. size = width * height;
  59. ALIGN_SIZE(size, alignment);
  60. frame->data[0] = bmalloc(size);
  61. frame->linesize[0] = width;
  62. break;
  63. case VIDEO_FORMAT_YVYU:
  64. case VIDEO_FORMAT_YUY2:
  65. case VIDEO_FORMAT_UYVY:
  66. size = width * height * 2;
  67. ALIGN_SIZE(size, alignment);
  68. frame->data[0] = bmalloc(size);
  69. frame->linesize[0] = width*2;
  70. break;
  71. case VIDEO_FORMAT_RGBA:
  72. case VIDEO_FORMAT_BGRA:
  73. case VIDEO_FORMAT_BGRX:
  74. size = width * height * 4;
  75. ALIGN_SIZE(size, alignment);
  76. frame->data[0] = bmalloc(size);
  77. frame->linesize[0] = width*4;
  78. break;
  79. case VIDEO_FORMAT_I444:
  80. size = width * height;
  81. ALIGN_SIZE(size, alignment);
  82. frame->data[0] = bmalloc(size * 3);
  83. frame->data[1] = (uint8_t*)frame->data[0] + size;
  84. frame->data[2] = (uint8_t*)frame->data[1] + size;
  85. frame->linesize[0] = width;
  86. frame->linesize[1] = width;
  87. frame->linesize[2] = width;
  88. break;
  89. }
  90. }
  91. void video_frame_copy(struct video_frame *dst, const struct video_frame *src,
  92. enum video_format format, uint32_t cy)
  93. {
  94. switch (format) {
  95. case VIDEO_FORMAT_NONE:
  96. return;
  97. case VIDEO_FORMAT_I420:
  98. memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
  99. memcpy(dst->data[1], src->data[1], src->linesize[1] * cy / 2);
  100. memcpy(dst->data[2], src->data[2], src->linesize[2] * cy / 2);
  101. break;
  102. case VIDEO_FORMAT_NV12:
  103. memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
  104. memcpy(dst->data[1], src->data[1], src->linesize[1] * cy / 2);
  105. break;
  106. case VIDEO_FORMAT_Y800:
  107. case VIDEO_FORMAT_YVYU:
  108. case VIDEO_FORMAT_YUY2:
  109. case VIDEO_FORMAT_UYVY:
  110. case VIDEO_FORMAT_RGBA:
  111. case VIDEO_FORMAT_BGRA:
  112. case VIDEO_FORMAT_BGRX:
  113. memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
  114. break;
  115. case VIDEO_FORMAT_I444:
  116. memcpy(dst->data[0], src->data[0], src->linesize[0] * cy);
  117. memcpy(dst->data[1], src->data[1], src->linesize[1] * cy);
  118. memcpy(dst->data[2], src->data[2], src->linesize[2] * cy);
  119. break;
  120. }
  121. }