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.

226 lines
6.9KB

  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. static av_cold int encode_init(AVCodecContext *avctx)
  77. {
  78. V210EncContext *s = avctx->priv_data;
  79. if (avctx->width & 1) {
  80. av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
  81. return AVERROR(EINVAL);
  82. }
  83. #if FF_API_CODED_FRAME
  84. FF_DISABLE_DEPRECATION_WARNINGS
  85. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  86. FF_ENABLE_DEPRECATION_WARNINGS
  87. #endif
  88. s->pack_line_8 = v210_planar_pack_8_c;
  89. s->pack_line_10 = v210_planar_pack_10_c;
  90. if (ARCH_X86)
  91. ff_v210enc_init_x86(s);
  92. return 0;
  93. }
  94. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  95. const AVFrame *pic, int *got_packet)
  96. {
  97. V210EncContext *s = avctx->priv_data;
  98. int aligned_width = ((avctx->width + 47) / 48) * 48;
  99. int stride = aligned_width * 8 / 3;
  100. int line_padding = stride - ((avctx->width * 8 + 11) / 12) * 4;
  101. int h, w, ret;
  102. uint8_t *dst;
  103. ret = ff_alloc_packet(pkt, avctx->height * stride);
  104. if (ret < 0) {
  105. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  106. return ret;
  107. }
  108. dst = pkt->data;
  109. if (pic->format == AV_PIX_FMT_YUV422P10) {
  110. const uint16_t *y = (const uint16_t *)pic->data[0];
  111. const uint16_t *u = (const uint16_t *)pic->data[1];
  112. const uint16_t *v = (const uint16_t *)pic->data[2];
  113. for (h = 0; h < avctx->height; h++) {
  114. uint32_t val;
  115. w = (avctx->width / 6) * 6;
  116. s->pack_line_10(y, u, v, dst, w);
  117. y += w;
  118. u += w >> 1;
  119. v += w >> 1;
  120. dst += (w / 6) * 16;
  121. if (w < avctx->width - 1) {
  122. WRITE_PIXELS(u, y, v);
  123. val = CLIP(*y++);
  124. if (w == avctx->width - 2) {
  125. AV_WL32(dst, val);
  126. dst += 4;
  127. }
  128. }
  129. if (w < avctx->width - 3) {
  130. val |= (CLIP(*u++) << 10) | (CLIP(*y++) << 20);
  131. AV_WL32(dst, val);
  132. dst += 4;
  133. val = CLIP(*v++) | (CLIP(*y++) << 10);
  134. AV_WL32(dst, val);
  135. dst += 4;
  136. }
  137. memset(dst, 0, line_padding);
  138. dst += line_padding;
  139. y += pic->linesize[0] / 2 - avctx->width;
  140. u += pic->linesize[1] / 2 - avctx->width / 2;
  141. v += pic->linesize[2] / 2 - avctx->width / 2;
  142. }
  143. } else if(pic->format == AV_PIX_FMT_YUV422P) {
  144. const uint8_t *y = pic->data[0];
  145. const uint8_t *u = pic->data[1];
  146. const uint8_t *v = pic->data[2];
  147. for (h = 0; h < avctx->height; h++) {
  148. uint32_t val;
  149. w = (avctx->width / 12) * 12;
  150. s->pack_line_8(y, u, v, dst, w);
  151. y += w;
  152. u += w >> 1;
  153. v += w >> 1;
  154. dst += (w / 12) * 32;
  155. for (; w < avctx->width - 5; w += 6) {
  156. WRITE_PIXELS8(u, y, v);
  157. WRITE_PIXELS8(y, u, y);
  158. WRITE_PIXELS8(v, y, u);
  159. WRITE_PIXELS8(y, v, y);
  160. }
  161. if (w < avctx->width - 1) {
  162. WRITE_PIXELS8(u, y, v);
  163. val = CLIP8(*y++) << 2;
  164. if (w == avctx->width - 2) {
  165. AV_WL32(dst, val);
  166. dst += 4;
  167. }
  168. }
  169. if (w < avctx->width - 3) {
  170. val |= (CLIP8(*u++) << 12) | (CLIP8(*y++) << 22);
  171. AV_WL32(dst, val);
  172. dst += 4;
  173. val = (CLIP8(*v++) << 2) | (CLIP8(*y++) << 12);
  174. AV_WL32(dst, val);
  175. dst += 4;
  176. }
  177. memset(dst, 0, line_padding);
  178. dst += line_padding;
  179. y += pic->linesize[0] - avctx->width;
  180. u += pic->linesize[1] - avctx->width / 2;
  181. v += pic->linesize[2] - avctx->width / 2;
  182. }
  183. }
  184. pkt->flags |= AV_PKT_FLAG_KEY;
  185. *got_packet = 1;
  186. return 0;
  187. }
  188. AVCodec ff_v210_encoder = {
  189. .name = "v210",
  190. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  191. .type = AVMEDIA_TYPE_VIDEO,
  192. .id = AV_CODEC_ID_V210,
  193. .priv_data_size = sizeof(V210EncContext),
  194. .init = encode_init,
  195. .encode2 = encode_frame,
  196. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P, AV_PIX_FMT_NONE },
  197. };