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.

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