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.

129 lines
3.8KB

  1. /*
  2. * Alias PIX image decoder
  3. * Copyright (C) 2014 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/intreadwrite.h"
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "internal.h"
  25. #define ALIAS_HEADER_SIZE 10
  26. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  27. AVPacket *avpkt)
  28. {
  29. AVFrame *f = data;
  30. GetByteContext gb;
  31. int width, height, ret, bits_pixel, pixel;
  32. uint8_t *out_buf;
  33. uint8_t count;
  34. int x, y;
  35. bytestream2_init(&gb, avpkt->data, avpkt->size);
  36. if (bytestream2_get_bytes_left(&gb) < ALIAS_HEADER_SIZE) {
  37. av_log(avctx, AV_LOG_ERROR, "Header too small %d.\n", avpkt->size);
  38. return AVERROR_INVALIDDATA;
  39. }
  40. width = bytestream2_get_be16u(&gb);
  41. height = bytestream2_get_be16u(&gb);
  42. bytestream2_skipu(&gb, 4); // obsolete X, Y offset
  43. bits_pixel = bytestream2_get_be16u(&gb);
  44. if (bits_pixel == 24)
  45. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  46. else if (bits_pixel == 8)
  47. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  48. else {
  49. av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
  50. return AVERROR_INVALIDDATA;
  51. }
  52. ret = ff_set_dimensions(avctx, width, height);
  53. if (ret < 0)
  54. return ret;
  55. ret = ff_get_buffer(avctx, f, 0);
  56. if (ret < 0)
  57. return ret;
  58. f->pict_type = AV_PICTURE_TYPE_I;
  59. f->key_frame = 1;
  60. x = 0;
  61. y = 1;
  62. out_buf = f->data[0];
  63. while (bytestream2_get_bytes_left(&gb) > 0) {
  64. int i;
  65. /* set buffer at the right position at every new line */
  66. if (x == avctx->width) {
  67. x = 0;
  68. out_buf = f->data[0] + f->linesize[0] * y++;
  69. if (y > avctx->height) {
  70. av_log(avctx, AV_LOG_ERROR,
  71. "Ended frame decoding with %d bytes left.\n",
  72. bytestream2_get_bytes_left(&gb));
  73. return AVERROR_INVALIDDATA;
  74. }
  75. }
  76. /* read packet and copy data */
  77. count = bytestream2_get_byteu(&gb);
  78. if (!count || x + count > avctx->width) {
  79. av_log(avctx, AV_LOG_ERROR, "Invalid run length %d.\n", count);
  80. return AVERROR_INVALIDDATA;
  81. }
  82. if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
  83. pixel = bytestream2_get_be24(&gb);
  84. for (i = 0; i < count; i++) {
  85. AV_WB24(out_buf, pixel);
  86. out_buf += 3;
  87. }
  88. } else { // AV_PIX_FMT_GRAY8
  89. pixel = bytestream2_get_byte(&gb);
  90. for (i = 0; i < count; i++)
  91. *out_buf++ = pixel;
  92. }
  93. x += i;
  94. }
  95. if (x != width || y != height) {
  96. av_log(avctx, AV_LOG_ERROR, "Picture stopped at %d,%d.\n", x, y);
  97. return AVERROR_INVALIDDATA;
  98. }
  99. *got_frame = 1;
  100. return avpkt->size;
  101. }
  102. AVCodec ff_alias_pix_decoder = {
  103. .name = "alias_pix",
  104. .long_name = NULL_IF_CONFIG_SMALL("Alias/Wavefront PIX image"),
  105. .type = AVMEDIA_TYPE_VIDEO,
  106. .id = AV_CODEC_ID_ALIAS_PIX,
  107. .decode = decode_frame,
  108. .capabilities = AV_CODEC_CAP_DR1,
  109. };