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.

135 lines
3.9KB

  1. /*
  2. * V210 decoder
  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 "libavutil/bswap.h"
  25. static av_cold int decode_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. avctx->pix_fmt = PIX_FMT_YUV422P10;
  32. avctx->bits_per_raw_sample = 10;
  33. avctx->coded_frame = avcodec_alloc_frame();
  34. if (!avctx->coded_frame)
  35. return AVERROR(ENOMEM);
  36. return 0;
  37. }
  38. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  39. AVPacket *avpkt)
  40. {
  41. int h, w;
  42. AVFrame *pic = avctx->coded_frame;
  43. const uint8_t *psrc = avpkt->data;
  44. uint16_t *y, *u, *v;
  45. int aligned_width = ((avctx->width + 47) / 48) * 48;
  46. int stride = aligned_width * 8 / 3;
  47. if (pic->data[0])
  48. avctx->release_buffer(avctx, pic);
  49. if (avpkt->size < stride * avctx->height) {
  50. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  51. return -1;
  52. }
  53. pic->reference = 0;
  54. if (avctx->get_buffer(avctx, pic) < 0)
  55. return -1;
  56. y = (uint16_t*)pic->data[0];
  57. u = (uint16_t*)pic->data[1];
  58. v = (uint16_t*)pic->data[2];
  59. pic->pict_type = AV_PICTURE_TYPE_I;
  60. pic->key_frame = 1;
  61. #define READ_PIXELS(a, b, c) \
  62. do { \
  63. val = av_le2ne32(*src++); \
  64. *a++ = val & 0x3FF; \
  65. *b++ = (val >> 10) & 0x3FF; \
  66. *c++ = (val >> 20) & 0x3FF; \
  67. } while (0)
  68. for (h = 0; h < avctx->height; h++) {
  69. const uint32_t *src = (const uint32_t*)psrc;
  70. uint32_t val;
  71. for (w = 0; w < avctx->width - 5; w += 6) {
  72. READ_PIXELS(u, y, v);
  73. READ_PIXELS(y, u, y);
  74. READ_PIXELS(v, y, u);
  75. READ_PIXELS(y, v, y);
  76. }
  77. if (w < avctx->width - 1) {
  78. READ_PIXELS(u, y, v);
  79. val = av_le2ne32(*src++);
  80. *y++ = val & 0x3FF;
  81. }
  82. if (w < avctx->width - 3) {
  83. *u++ = (val >> 10) & 0x3FF;
  84. *y++ = (val >> 20) & 0x3FF;
  85. val = av_le2ne32(*src++);
  86. *v++ = val & 0x3FF;
  87. *y++ = (val >> 10) & 0x3FF;
  88. }
  89. psrc += stride;
  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. *data_size = sizeof(AVFrame);
  95. *(AVFrame*)data = *avctx->coded_frame;
  96. return avpkt->size;
  97. }
  98. static av_cold int decode_close(AVCodecContext *avctx)
  99. {
  100. AVFrame *pic = avctx->coded_frame;
  101. if (pic->data[0])
  102. avctx->release_buffer(avctx, pic);
  103. av_freep(&avctx->coded_frame);
  104. return 0;
  105. }
  106. AVCodec ff_v210_decoder = {
  107. .name = "v210",
  108. .type = AVMEDIA_TYPE_VIDEO,
  109. .id = CODEC_ID_V210,
  110. .init = decode_init,
  111. .close = decode_close,
  112. .decode = decode_frame,
  113. .capabilities = CODEC_CAP_DR1,
  114. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  115. };