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.

135 lines
4.0KB

  1. /*
  2. * Alias PIX image encoder
  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 av_cold int encode_init(AVCodecContext *avctx)
  27. {
  28. avctx->coded_frame = av_frame_alloc();
  29. if (!avctx->coded_frame)
  30. return AVERROR(ENOMEM);
  31. return 0;
  32. }
  33. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  34. const AVFrame *frame, int *got_packet)
  35. {
  36. int width, height, bits_pixel, i, j, length, ret;
  37. uint8_t *in_buf, *buf;
  38. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  39. avctx->coded_frame->key_frame = 1;
  40. width = avctx->width;
  41. height = avctx->height;
  42. if (width > 65535 || height > 65535 ||
  43. width * height >= INT_MAX / 4 - ALIAS_HEADER_SIZE) {
  44. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n", width, height);
  45. return AVERROR_INVALIDDATA;
  46. }
  47. switch (avctx->pix_fmt) {
  48. case AV_PIX_FMT_GRAY8:
  49. bits_pixel = 8;
  50. break;
  51. case AV_PIX_FMT_BGR24:
  52. bits_pixel = 24;
  53. break;
  54. default:
  55. return AVERROR(EINVAL);
  56. }
  57. length = ALIAS_HEADER_SIZE + 4 * width * height; // max possible
  58. if ((ret = ff_alloc_packet(pkt, length)) < 0) {
  59. av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
  60. return ret;
  61. }
  62. buf = pkt->data;
  63. /* Encode header. */
  64. bytestream_put_be16(&buf, width);
  65. bytestream_put_be16(&buf, height);
  66. bytestream_put_be32(&buf, 0); /* X, Y offset */
  67. bytestream_put_be16(&buf, bits_pixel);
  68. for (j = 0; j < height; j++) {
  69. in_buf = frame->data[0] + frame->linesize[0] * j;
  70. for (i = 0; i < width; ) {
  71. int count = 0;
  72. int pixel;
  73. if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  74. pixel = *in_buf;
  75. while (count < 255 && count + i < width && pixel == *in_buf) {
  76. count++;
  77. in_buf++;
  78. }
  79. bytestream_put_byte(&buf, count);
  80. bytestream_put_byte(&buf, pixel);
  81. } else { /* AV_PIX_FMT_BGR24 */
  82. pixel = AV_RB24(in_buf);
  83. while (count < 255 && count + i < width &&
  84. pixel == AV_RB24(in_buf)) {
  85. count++;
  86. in_buf += 3;
  87. }
  88. bytestream_put_byte(&buf, count);
  89. bytestream_put_be24(&buf, pixel);
  90. }
  91. i += count;
  92. }
  93. }
  94. /* Total length */
  95. av_shrink_packet(pkt, buf - pkt->data);
  96. pkt->flags |= AV_PKT_FLAG_KEY;
  97. *got_packet = 1;
  98. return 0;
  99. }
  100. static av_cold int encode_close(AVCodecContext *avctx)
  101. {
  102. av_frame_free(&avctx->coded_frame);
  103. return 0;
  104. }
  105. AVCodec ff_alias_pix_encoder = {
  106. .name = "alias_pix",
  107. .long_name = NULL_IF_CONFIG_SMALL("Alias/Wavefront PIX image"),
  108. .type = AVMEDIA_TYPE_VIDEO,
  109. .id = AV_CODEC_ID_ALIAS_PIX,
  110. .init = encode_init,
  111. .encode2 = encode_frame,
  112. .close = encode_close,
  113. .pix_fmts = (const enum AVPixelFormat[]) {
  114. AV_PIX_FMT_BGR24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
  115. },
  116. };