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.

231 lines
7.0KB

  1. /*
  2. * V210 encoder
  3. *
  4. * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
  5. * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "internal.h"
  26. #include "v210enc.h"
  27. #define CLIP(v) av_clip(v, 4, 1019)
  28. #define CLIP8(v) av_clip(v, 1, 254)
  29. #define WRITE_PIXELS(a, b, c) \
  30. do { \
  31. val = CLIP(*a++); \
  32. val |= (CLIP(*b++) << 10) | \
  33. (CLIP(*c++) << 20); \
  34. AV_WL32(dst, val); \
  35. dst += 4; \
  36. } while (0)
  37. #define WRITE_PIXELS8(a, b, c) \
  38. do { \
  39. val = (CLIP8(*a++) << 2); \
  40. val |= (CLIP8(*b++) << 12) | \
  41. (CLIP8(*c++) << 22); \
  42. AV_WL32(dst, val); \
  43. dst += 4; \
  44. } while (0)
  45. static void v210_planar_pack_8_c(const uint8_t *y, const uint8_t *u,
  46. const uint8_t *v, uint8_t *dst,
  47. ptrdiff_t width)
  48. {
  49. uint32_t val;
  50. int i;
  51. /* unroll this to match the assembly */
  52. for (i = 0; i < width - 11; i += 12) {
  53. WRITE_PIXELS8(u, y, v);
  54. WRITE_PIXELS8(y, u, y);
  55. WRITE_PIXELS8(v, y, u);
  56. WRITE_PIXELS8(y, v, y);
  57. WRITE_PIXELS8(u, y, v);
  58. WRITE_PIXELS8(y, u, y);
  59. WRITE_PIXELS8(v, y, u);
  60. WRITE_PIXELS8(y, v, y);
  61. }
  62. }
  63. static void v210_planar_pack_10_c(const uint16_t *y, const uint16_t *u,
  64. const uint16_t *v, uint8_t *dst,
  65. ptrdiff_t width)
  66. {
  67. uint32_t val;
  68. int i;
  69. for (i = 0; i < width - 5; i += 6) {
  70. WRITE_PIXELS(u, y, v);
  71. WRITE_PIXELS(y, u, y);
  72. WRITE_PIXELS(v, y, u);
  73. WRITE_PIXELS(y, v, y);
  74. }
  75. }
  76. av_cold void ff_v210enc_init(V210EncContext *s)
  77. {
  78. s->pack_line_8 = v210_planar_pack_8_c;
  79. s->pack_line_10 = v210_planar_pack_10_c;
  80. if (ARCH_X86)
  81. ff_v210enc_init_x86(s);
  82. }
  83. static av_cold int encode_init(AVCodecContext *avctx)
  84. {
  85. V210EncContext *s = avctx->priv_data;
  86. if (avctx->width & 1) {
  87. av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
  88. return AVERROR(EINVAL);
  89. }
  90. #if FF_API_CODED_FRAME
  91. FF_DISABLE_DEPRECATION_WARNINGS
  92. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  93. FF_ENABLE_DEPRECATION_WARNINGS
  94. #endif
  95. ff_v210enc_init(s);
  96. return 0;
  97. }
  98. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  99. const AVFrame *pic, int *got_packet)
  100. {
  101. V210EncContext *s = avctx->priv_data;
  102. int aligned_width = ((avctx->width + 47) / 48) * 48;
  103. int stride = aligned_width * 8 / 3;
  104. int line_padding = stride - ((avctx->width * 8 + 11) / 12) * 4;
  105. int h, w, ret;
  106. uint8_t *dst;
  107. ret = ff_alloc_packet(pkt, avctx->height * stride);
  108. if (ret < 0) {
  109. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  110. return ret;
  111. }
  112. dst = pkt->data;
  113. if (pic->format == AV_PIX_FMT_YUV422P10) {
  114. const uint16_t *y = (const uint16_t *)pic->data[0];
  115. const uint16_t *u = (const uint16_t *)pic->data[1];
  116. const uint16_t *v = (const uint16_t *)pic->data[2];
  117. for (h = 0; h < avctx->height; h++) {
  118. uint32_t val;
  119. w = (avctx->width / 6) * 6;
  120. s->pack_line_10(y, u, v, dst, w);
  121. y += w;
  122. u += w >> 1;
  123. v += w >> 1;
  124. dst += (w / 6) * 16;
  125. if (w < avctx->width - 1) {
  126. WRITE_PIXELS(u, y, v);
  127. val = CLIP(*y++);
  128. if (w == avctx->width - 2) {
  129. AV_WL32(dst, val);
  130. dst += 4;
  131. }
  132. }
  133. if (w < avctx->width - 3) {
  134. val |= (CLIP(*u++) << 10) | (CLIP(*y++) << 20);
  135. AV_WL32(dst, val);
  136. dst += 4;
  137. val = CLIP(*v++) | (CLIP(*y++) << 10);
  138. AV_WL32(dst, val);
  139. dst += 4;
  140. }
  141. memset(dst, 0, line_padding);
  142. dst += line_padding;
  143. y += pic->linesize[0] / 2 - avctx->width;
  144. u += pic->linesize[1] / 2 - avctx->width / 2;
  145. v += pic->linesize[2] / 2 - avctx->width / 2;
  146. }
  147. } else if(pic->format == AV_PIX_FMT_YUV422P) {
  148. const uint8_t *y = pic->data[0];
  149. const uint8_t *u = pic->data[1];
  150. const uint8_t *v = pic->data[2];
  151. for (h = 0; h < avctx->height; h++) {
  152. uint32_t val;
  153. w = (avctx->width / 12) * 12;
  154. s->pack_line_8(y, u, v, dst, w);
  155. y += w;
  156. u += w >> 1;
  157. v += w >> 1;
  158. dst += (w / 12) * 32;
  159. for (; w < avctx->width - 5; w += 6) {
  160. WRITE_PIXELS8(u, y, v);
  161. WRITE_PIXELS8(y, u, y);
  162. WRITE_PIXELS8(v, y, u);
  163. WRITE_PIXELS8(y, v, y);
  164. }
  165. if (w < avctx->width - 1) {
  166. WRITE_PIXELS8(u, y, v);
  167. val = CLIP8(*y++) << 2;
  168. if (w == avctx->width - 2) {
  169. AV_WL32(dst, val);
  170. dst += 4;
  171. }
  172. }
  173. if (w < avctx->width - 3) {
  174. val |= (CLIP8(*u++) << 12) | (CLIP8(*y++) << 22);
  175. AV_WL32(dst, val);
  176. dst += 4;
  177. val = (CLIP8(*v++) << 2) | (CLIP8(*y++) << 12);
  178. AV_WL32(dst, val);
  179. dst += 4;
  180. }
  181. memset(dst, 0, line_padding);
  182. dst += line_padding;
  183. y += pic->linesize[0] - avctx->width;
  184. u += pic->linesize[1] - avctx->width / 2;
  185. v += pic->linesize[2] - avctx->width / 2;
  186. }
  187. }
  188. pkt->flags |= AV_PKT_FLAG_KEY;
  189. *got_packet = 1;
  190. return 0;
  191. }
  192. AVCodec ff_v210_encoder = {
  193. .name = "v210",
  194. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  195. .type = AVMEDIA_TYPE_VIDEO,
  196. .id = AV_CODEC_ID_V210,
  197. .priv_data_size = sizeof(V210EncContext),
  198. .init = encode_init,
  199. .encode2 = encode_frame,
  200. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE },
  201. };