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.

160 lines
5.1KB

  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 = 0, 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. y = (uint16_t *)pic->data[0];
  60. u = (uint16_t *)pic->data[1];
  61. v = (uint16_t *)pic->data[2];
  62. line_end = avpkt->data + stride;
  63. while (line++ < avctx->height) {
  64. while (1) {
  65. uint32_t t = AV_RL32(src);
  66. src += 4;
  67. *u++ = t << 6 & 0xFFC0;
  68. *y++ = t >> 4 & 0xFFC0;
  69. *v++ = t >> 14 & 0xFFC0;
  70. if (src >= line_end - 1) {
  71. *y = 0x80;
  72. src++;
  73. line_end += stride;
  74. y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
  75. u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
  76. v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
  77. break;
  78. }
  79. t = AV_RL32(src);
  80. src += 4;
  81. *y++ = t << 6 & 0xFFC0;
  82. *u++ = t >> 4 & 0xFFC0;
  83. *y++ = t >> 14 & 0xFFC0;
  84. if (src >= line_end - 2) {
  85. if (!(width & 1)) {
  86. *y = 0x80;
  87. src += 2;
  88. }
  89. line_end += stride;
  90. y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
  91. u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
  92. v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
  93. break;
  94. }
  95. t = AV_RL32(src);
  96. src += 4;
  97. *v++ = t << 6 & 0xFFC0;
  98. *y++ = t >> 4 & 0xFFC0;
  99. *u++ = t >> 14 & 0xFFC0;
  100. if (src >= line_end - 1) {
  101. *y = 0x80;
  102. src++;
  103. line_end += stride;
  104. y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
  105. u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
  106. v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
  107. break;
  108. }
  109. t = AV_RL32(src);
  110. src += 4;
  111. *y++ = t << 6 & 0xFFC0;
  112. *v++ = t >> 4 & 0xFFC0;
  113. *y++ = t >> 14 & 0xFFC0;
  114. if (src >= line_end - 2) {
  115. if (width & 1) {
  116. *y = 0x80;
  117. src += 2;
  118. }
  119. line_end += stride;
  120. y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
  121. u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
  122. v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
  123. break;
  124. }
  125. }
  126. }
  127. *got_frame = 1;
  128. return avpkt->size;
  129. }
  130. AVCodec ff_zero12v_decoder = {
  131. .name = "012v",
  132. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  133. .type = AVMEDIA_TYPE_VIDEO,
  134. .id = AV_CODEC_ID_012V,
  135. .init = zero12v_decode_init,
  136. .decode = zero12v_decode_frame,
  137. .capabilities = CODEC_CAP_DR1,
  138. };