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.

234 lines
7.1KB

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