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.

130 lines
3.8KB

  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 FFmpeg.
  8. *
  9. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "libavcodec/bytestream.h"
  25. static av_cold int encode_init(AVCodecContext *avctx)
  26. {
  27. if (avctx->width & 1) {
  28. av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
  29. return -1;
  30. }
  31. if (avctx->pix_fmt != PIX_FMT_YUV422P16) {
  32. av_log(avctx, AV_LOG_ERROR, "v210 needs YUV422P16\n");
  33. return -1;
  34. }
  35. if (avctx->bits_per_raw_sample != 10)
  36. av_log(avctx, AV_LOG_WARNING, "bits per raw sample: %d != 10-bit\n",
  37. avctx->bits_per_raw_sample);
  38. avctx->coded_frame = avcodec_alloc_frame();
  39. avctx->coded_frame->key_frame = 1;
  40. avctx->coded_frame->pict_type = FF_I_TYPE;
  41. return 0;
  42. }
  43. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
  44. {
  45. const AVFrame *pic = data;
  46. int aligned_width = ((avctx->width + 47) / 48) * 48;
  47. int stride = aligned_width * 8 / 3;
  48. int h, w;
  49. const uint16_t *y = (const uint16_t*)pic->data[0];
  50. const uint16_t *u = (const uint16_t*)pic->data[1];
  51. const uint16_t *v = (const uint16_t*)pic->data[2];
  52. uint8_t *p = buf;
  53. uint8_t *pdst = buf;
  54. if (buf_size < aligned_width * avctx->height * 8 / 3) {
  55. av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
  56. return -1;
  57. }
  58. #define WRITE_PIXELS(a, b, c) \
  59. do { \
  60. val = (*a++ >> 6) | \
  61. ((*b++ & 0xFFC0) << 4); \
  62. val|= (*c++ & 0xFFC0) << 14; \
  63. bytestream_put_le32(&p, val); \
  64. } while (0);
  65. for (h = 0; h < avctx->height; h++) {
  66. uint32_t val;
  67. for (w = 0; w < avctx->width - 5; w += 6) {
  68. WRITE_PIXELS(u, y, v);
  69. WRITE_PIXELS(y, u, y);
  70. WRITE_PIXELS(v, y, u);
  71. WRITE_PIXELS(y, v, y);
  72. }
  73. if (w < avctx->width - 1) {
  74. WRITE_PIXELS(u, y, v);
  75. val = *y++ >> 6;
  76. if (w == avctx->width - 2)
  77. bytestream_put_le32(&p, val);
  78. }
  79. if (w < avctx->width - 3) {
  80. val |=((*u++ & 0xFFC0) << 4) |
  81. ((*y++ & 0xFFC0) << 14);
  82. bytestream_put_le32(&p, val);
  83. val = (*v++ >> 6) |
  84. (*y++ & 0xFFC0) << 4;
  85. bytestream_put_le32(&p, val);
  86. }
  87. pdst += stride;
  88. memset(p, 0, pdst - p);
  89. p = pdst;
  90. y += pic->linesize[0]/2 - avctx->width;
  91. u += pic->linesize[1]/2 - avctx->width/2;
  92. v += pic->linesize[2]/2 - avctx->width/2;
  93. }
  94. return p - buf;
  95. }
  96. static av_cold int encode_close(AVCodecContext *avctx)
  97. {
  98. av_freep(&avctx->coded_frame);
  99. return 0;
  100. }
  101. AVCodec v210_encoder = {
  102. "v210",
  103. CODEC_TYPE_VIDEO,
  104. CODEC_ID_V210,
  105. 0,
  106. encode_init,
  107. encode_frame,
  108. encode_close,
  109. .pix_fmts = (enum PixelFormat[]){PIX_FMT_YUV422P16, PIX_FMT_NONE},
  110. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  111. };