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.

112 lines
3.1KB

  1. /*
  2. * Dxtory decoder
  3. *
  4. * Copyright (c) 2011 Konstantin Shishkov
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/common.h"
  25. #include "libavutil/intreadwrite.h"
  26. static av_cold int decode_init(AVCodecContext *avctx)
  27. {
  28. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  29. avctx->coded_frame = avcodec_alloc_frame();
  30. if (!avctx->coded_frame)
  31. return AVERROR(ENOMEM);
  32. return 0;
  33. }
  34. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  35. AVPacket *avpkt)
  36. {
  37. int h, w;
  38. AVFrame *pic = avctx->coded_frame;
  39. const uint8_t *src = avpkt->data;
  40. uint8_t *Y1, *Y2, *U, *V;
  41. int ret;
  42. if (pic->data[0])
  43. avctx->release_buffer(avctx, pic);
  44. if (avpkt->size < avctx->width * avctx->height * 3 / 2 + 16) {
  45. av_log(avctx, AV_LOG_ERROR, "packet too small\n");
  46. return AVERROR_INVALIDDATA;
  47. }
  48. pic->reference = 0;
  49. if ((ret = ff_get_buffer(avctx, pic)) < 0)
  50. return ret;
  51. pic->pict_type = AV_PICTURE_TYPE_I;
  52. pic->key_frame = 1;
  53. if (AV_RL32(src) != 0x01000002) {
  54. av_log_ask_for_sample(avctx, "Unknown frame header %X\n", AV_RL32(src));
  55. return AVERROR_PATCHWELCOME;
  56. }
  57. src += 16;
  58. Y1 = pic->data[0];
  59. Y2 = pic->data[0] + pic->linesize[0];
  60. U = pic->data[1];
  61. V = pic->data[2];
  62. for (h = 0; h < avctx->height; h += 2) {
  63. for (w = 0; w < avctx->width; w += 2) {
  64. AV_COPY16(Y1 + w, src);
  65. AV_COPY16(Y2 + w, src + 2);
  66. U[w >> 1] = src[4] + 0x80;
  67. V[w >> 1] = src[5] + 0x80;
  68. src += 6;
  69. }
  70. Y1 += pic->linesize[0] << 1;
  71. Y2 += pic->linesize[0] << 1;
  72. U += pic->linesize[1];
  73. V += pic->linesize[2];
  74. }
  75. *got_frame = 1;
  76. *(AVFrame*)data = *pic;
  77. return avpkt->size;
  78. }
  79. static av_cold int decode_close(AVCodecContext *avctx)
  80. {
  81. AVFrame *pic = avctx->coded_frame;
  82. if (pic->data[0])
  83. avctx->release_buffer(avctx, pic);
  84. av_freep(&avctx->coded_frame);
  85. return 0;
  86. }
  87. AVCodec ff_dxtory_decoder = {
  88. .name = "dxtory",
  89. .long_name = NULL_IF_CONFIG_SMALL("Dxtory"),
  90. .type = AVMEDIA_TYPE_VIDEO,
  91. .id = AV_CODEC_ID_DXTORY,
  92. .init = decode_init,
  93. .close = decode_close,
  94. .decode = decode_frame,
  95. .capabilities = CODEC_CAP_DR1,
  96. };