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.

132 lines
3.9KB

  1. /*
  2. * Alias PIX image decoder
  3. * Copyright (C) 2014 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. if (bytestream2_get_bytes_left(&gb) < width*height / 255)
  56. return AVERROR_INVALIDDATA;
  57. ret = ff_get_buffer(avctx, f, 0);
  58. if (ret < 0)
  59. return ret;
  60. f->pict_type = AV_PICTURE_TYPE_I;
  61. f->key_frame = 1;
  62. x = 0;
  63. y = 1;
  64. out_buf = f->data[0];
  65. while (bytestream2_get_bytes_left(&gb) > 0) {
  66. int i;
  67. /* set buffer at the right position at every new line */
  68. if (x == avctx->width) {
  69. x = 0;
  70. out_buf = f->data[0] + f->linesize[0] * y++;
  71. if (y > avctx->height) {
  72. av_log(avctx, AV_LOG_ERROR,
  73. "Ended frame decoding with %d bytes left.\n",
  74. bytestream2_get_bytes_left(&gb));
  75. return AVERROR_INVALIDDATA;
  76. }
  77. }
  78. /* read packet and copy data */
  79. count = bytestream2_get_byteu(&gb);
  80. if (!count || x + count > avctx->width) {
  81. av_log(avctx, AV_LOG_ERROR, "Invalid run length %d.\n", count);
  82. return AVERROR_INVALIDDATA;
  83. }
  84. if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
  85. pixel = bytestream2_get_be24(&gb);
  86. for (i = 0; i < count; i++) {
  87. AV_WB24(out_buf, pixel);
  88. out_buf += 3;
  89. }
  90. } else { // AV_PIX_FMT_GRAY8
  91. pixel = bytestream2_get_byte(&gb);
  92. for (i = 0; i < count; i++)
  93. *out_buf++ = pixel;
  94. }
  95. x += i;
  96. }
  97. if (x != width || y != height) {
  98. av_log(avctx, AV_LOG_ERROR, "Picture stopped at %d,%d.\n", x, y);
  99. return AVERROR_INVALIDDATA;
  100. }
  101. *got_frame = 1;
  102. return avpkt->size;
  103. }
  104. AVCodec ff_alias_pix_decoder = {
  105. .name = "alias_pix",
  106. .long_name = NULL_IF_CONFIG_SMALL("Alias/Wavefront PIX image"),
  107. .type = AVMEDIA_TYPE_VIDEO,
  108. .id = AV_CODEC_ID_ALIAS_PIX,
  109. .decode = decode_frame,
  110. .capabilities = AV_CODEC_CAP_DR1,
  111. };