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.

156 lines
4.7KB

  1. /*
  2. * 012v decoder
  3. *
  4. * Copyright (C) 2012 Carl Eugen Hoyos
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avcodec.h"
  23. #include "internal.h"
  24. #include "libavutil/intreadwrite.h"
  25. static av_cold int zero12v_decode_init(AVCodecContext *avctx)
  26. {
  27. avctx->pix_fmt = AV_PIX_FMT_YUV422P16;
  28. avctx->bits_per_raw_sample = 10;
  29. if (avctx->codec_tag == MKTAG('a', '1', '2', 'v'))
  30. avpriv_request_sample(avctx, "transparency");
  31. return 0;
  32. }
  33. static int zero12v_decode_frame(AVCodecContext *avctx, void *data,
  34. int *got_frame, AVPacket *avpkt)
  35. {
  36. int line, ret;
  37. const int width = avctx->width;
  38. AVFrame *pic = data;
  39. uint16_t *y, *u, *v;
  40. const uint8_t *line_end, *src = avpkt->data;
  41. int stride = avctx->width * 8 / 3;
  42. if (width <= 1 || avctx->height <= 0) {
  43. av_log(avctx, AV_LOG_ERROR, "Dimensions %dx%d not supported.\n", width, avctx->height);
  44. return AVERROR_INVALIDDATA;
  45. }
  46. if ( avctx->codec_tag == MKTAG('0', '1', '2', 'v')
  47. && avpkt->size % avctx->height == 0
  48. && avpkt->size / avctx->height * 3 >= width * 8)
  49. stride = avpkt->size / avctx->height;
  50. if (avpkt->size < avctx->height * stride) {
  51. av_log(avctx, AV_LOG_ERROR, "Packet too small: %d instead of %d\n",
  52. avpkt->size, avctx->height * stride);
  53. return AVERROR_INVALIDDATA;
  54. }
  55. if ((ret = ff_get_buffer(avctx, pic, 0)) < 0)
  56. return ret;
  57. pic->pict_type = AV_PICTURE_TYPE_I;
  58. pic->key_frame = 1;
  59. line_end = avpkt->data + stride;
  60. for (line = 0; line < avctx->height; line++) {
  61. uint16_t y_temp[6] = {0x8000, 0x8000, 0x8000, 0x8000, 0x8000, 0x8000};
  62. uint16_t u_temp[3] = {0x8000, 0x8000, 0x8000};
  63. uint16_t v_temp[3] = {0x8000, 0x8000, 0x8000};
  64. int x;
  65. y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
  66. u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
  67. v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
  68. for (x = 0; x < width; x += 6) {
  69. uint32_t t;
  70. if (width - x < 6 || line_end - src < 16) {
  71. y = y_temp;
  72. u = u_temp;
  73. v = v_temp;
  74. }
  75. if (line_end - src < 4)
  76. break;
  77. t = AV_RL32(src);
  78. src += 4;
  79. *u++ = t << 6 & 0xFFC0;
  80. *y++ = t >> 4 & 0xFFC0;
  81. *v++ = t >> 14 & 0xFFC0;
  82. if (line_end - src < 4)
  83. break;
  84. t = AV_RL32(src);
  85. src += 4;
  86. *y++ = t << 6 & 0xFFC0;
  87. *u++ = t >> 4 & 0xFFC0;
  88. *y++ = t >> 14 & 0xFFC0;
  89. if (line_end - src < 4)
  90. break;
  91. t = AV_RL32(src);
  92. src += 4;
  93. *v++ = t << 6 & 0xFFC0;
  94. *y++ = t >> 4 & 0xFFC0;
  95. *u++ = t >> 14 & 0xFFC0;
  96. if (line_end - src < 4)
  97. break;
  98. t = AV_RL32(src);
  99. src += 4;
  100. *y++ = t << 6 & 0xFFC0;
  101. *v++ = t >> 4 & 0xFFC0;
  102. *y++ = t >> 14 & 0xFFC0;
  103. if (width - x < 6)
  104. break;
  105. }
  106. if (x < width) {
  107. y = x + (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
  108. u = x/2 + (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
  109. v = x/2 + (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
  110. memcpy(y, y_temp, sizeof(*y) * (width - x));
  111. memcpy(u, u_temp, sizeof(*u) * (width - x + 1) / 2);
  112. memcpy(v, v_temp, sizeof(*v) * (width - x + 1) / 2);
  113. }
  114. line_end += stride;
  115. src = line_end - stride;
  116. }
  117. *got_frame = 1;
  118. return avpkt->size;
  119. }
  120. AVCodec ff_zero12v_decoder = {
  121. .name = "012v",
  122. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  123. .type = AVMEDIA_TYPE_VIDEO,
  124. .id = AV_CODEC_ID_012V,
  125. .init = zero12v_decode_init,
  126. .decode = zero12v_decode_frame,
  127. .capabilities = AV_CODEC_CAP_DR1,
  128. };