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.

173 lines
5.4KB

  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 = PIX_FMT_YUV422P16;
  28. avctx->bits_per_raw_sample = 10;
  29. avctx->coded_frame = avcodec_alloc_frame();
  30. if (!avctx->coded_frame)
  31. return AVERROR(ENOMEM);
  32. if (avctx->codec_tag == MKTAG('a', '1', '2', 'v'))
  33. av_log_ask_for_sample(avctx, "Samples with actual transparency needed\n");
  34. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  35. avctx->coded_frame->key_frame = 1;
  36. return 0;
  37. }
  38. static int zero12v_decode_frame(AVCodecContext *avctx, void *data,
  39. int *got_frame, AVPacket *avpkt)
  40. {
  41. int line = 0, ret;
  42. const int width = avctx->width;
  43. AVFrame *pic = avctx->coded_frame;
  44. uint16_t *y, *u, *v;
  45. const uint8_t *line_end, *src = avpkt->data;
  46. int stride = avctx->width * 8 / 3;
  47. if (pic->data[0])
  48. avctx->release_buffer(avctx, pic);
  49. if (width == 1) {
  50. av_log(avctx, AV_LOG_ERROR, "Width 1 not supported.\n");
  51. return AVERROR_INVALIDDATA;
  52. }
  53. if (avpkt->size < avctx->height * stride) {
  54. av_log(avctx, AV_LOG_ERROR, "Packet too small: %d instead of %d\n",
  55. avpkt->size, avctx->height * stride);
  56. return AVERROR_INVALIDDATA;
  57. }
  58. pic->reference = 0;
  59. if ((ret = ff_get_buffer(avctx, pic)) < 0)
  60. return ret;
  61. y = (uint16_t *)pic->data[0];
  62. u = (uint16_t *)pic->data[1];
  63. v = (uint16_t *)pic->data[2];
  64. line_end = avpkt->data + stride;
  65. while (line++ < avctx->height) {
  66. while (1) {
  67. uint32_t t = AV_RL32(src);
  68. src += 4;
  69. *u++ = t << 6 & 0xFFC0;
  70. *y++ = t >> 4 & 0xFFC0;
  71. *v++ = t >> 14 & 0xFFC0;
  72. if (src >= line_end - 1) {
  73. *y = 0x80;
  74. src++;
  75. line_end += stride;
  76. y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
  77. u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
  78. v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
  79. break;
  80. }
  81. t = AV_RL32(src);
  82. src += 4;
  83. *y++ = t << 6 & 0xFFC0;
  84. *u++ = t >> 4 & 0xFFC0;
  85. *y++ = t >> 14 & 0xFFC0;
  86. if (src >= line_end - 2) {
  87. if (!(width & 1)) {
  88. *y = 0x80;
  89. src += 2;
  90. }
  91. line_end += stride;
  92. y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
  93. u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
  94. v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
  95. break;
  96. }
  97. t = AV_RL32(src);
  98. src += 4;
  99. *v++ = t << 6 & 0xFFC0;
  100. *y++ = t >> 4 & 0xFFC0;
  101. *u++ = t >> 14 & 0xFFC0;
  102. if (src >= line_end - 1) {
  103. *y = 0x80;
  104. src++;
  105. line_end += stride;
  106. y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
  107. u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
  108. v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
  109. break;
  110. }
  111. t = AV_RL32(src);
  112. src += 4;
  113. *y++ = t << 6 & 0xFFC0;
  114. *v++ = t >> 4 & 0xFFC0;
  115. *y++ = t >> 14 & 0xFFC0;
  116. if (src >= line_end - 2) {
  117. if (width & 1) {
  118. *y = 0x80;
  119. src += 2;
  120. }
  121. line_end += stride;
  122. y = (uint16_t *)(pic->data[0] + line * pic->linesize[0]);
  123. u = (uint16_t *)(pic->data[1] + line * pic->linesize[1]);
  124. v = (uint16_t *)(pic->data[2] + line * pic->linesize[2]);
  125. break;
  126. }
  127. }
  128. }
  129. *got_frame = 1;
  130. *(AVFrame*)data= *avctx->coded_frame;
  131. return avpkt->size;
  132. }
  133. static av_cold int zero12v_decode_close(AVCodecContext *avctx)
  134. {
  135. AVFrame *pic = avctx->coded_frame;
  136. if (pic->data[0])
  137. avctx->release_buffer(avctx, pic);
  138. av_freep(&avctx->coded_frame);
  139. return 0;
  140. }
  141. AVCodec ff_zero12v_decoder = {
  142. .name = "012v",
  143. .type = AVMEDIA_TYPE_VIDEO,
  144. .id = AV_CODEC_ID_012V,
  145. .init = zero12v_decode_init,
  146. .close = zero12v_decode_close,
  147. .decode = zero12v_decode_frame,
  148. .capabilities = CODEC_CAP_DR1,
  149. .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
  150. };